1 /*
2  * Copyright 2023 Cirrus Logic, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT sbs_sbs_charger
8 
9 #include <zephyr/drivers/charger.h>
10 #include <zephyr/drivers/i2c.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/sys/byteorder.h>
13 
14 #include "sbs_charger.h"
15 
16 struct sbs_charger_config {
17 	struct i2c_dt_spec i2c;
18 };
19 
20 LOG_MODULE_REGISTER(sbs_charger);
21 
sbs_cmd_reg_read(const struct device * dev,uint8_t reg_addr,uint16_t * val)22 static int sbs_cmd_reg_read(const struct device *dev, uint8_t reg_addr, uint16_t *val)
23 {
24 	const struct sbs_charger_config *cfg;
25 	uint8_t i2c_data[2];
26 	int status;
27 
28 	cfg = dev->config;
29 	status = i2c_burst_read_dt(&cfg->i2c, reg_addr, i2c_data, sizeof(i2c_data));
30 	if (status < 0) {
31 		LOG_ERR("Unable to read register");
32 		return status;
33 	}
34 
35 	*val = sys_get_le16(i2c_data);
36 
37 	return 0;
38 }
39 
sbs_cmd_reg_write(const struct device * dev,uint8_t reg_addr,uint16_t val)40 static int sbs_cmd_reg_write(const struct device *dev, uint8_t reg_addr, uint16_t val)
41 {
42 	const struct sbs_charger_config *config = dev->config;
43 	uint8_t buf[2];
44 
45 	sys_put_le16(val, buf);
46 
47 	return i2c_burst_write_dt(&config->i2c, reg_addr, buf, sizeof(buf));
48 }
49 
sbs_cmd_reg_update(const struct device * dev,uint8_t reg_addr,uint16_t mask,uint16_t val)50 static int sbs_cmd_reg_update(const struct device *dev, uint8_t reg_addr, uint16_t mask,
51 			      uint16_t val)
52 {
53 	uint16_t old_val, new_val;
54 	int ret;
55 
56 	ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &old_val);
57 	if (ret < 0) {
58 		return ret;
59 	}
60 
61 	new_val = (old_val & ~mask) | (val & mask);
62 	if (new_val == old_val) {
63 		return 0;
64 	}
65 
66 	return sbs_cmd_reg_write(dev, reg_addr, new_val);
67 }
68 
sbs_charger_charge_enable(const struct device * dev,const bool enable)69 static int sbs_charger_charge_enable(const struct device *dev, const bool enable)
70 {
71 	uint16_t reg_val;
72 
73 	if (!enable) {
74 		reg_val = SBS_CHARGER_MODE_INHIBIT_CHARGE;
75 	} else {
76 		reg_val = 0;
77 	}
78 
79 	return sbs_cmd_reg_update(dev, SBS_CHARGER_REG_CHARGER_MODE,
80 				  SBS_CHARGER_MODE_INHIBIT_CHARGE, reg_val);
81 }
82 
sbs_charger_get_prop(const struct device * dev,const charger_prop_t prop,union charger_propval * val)83 static int sbs_charger_get_prop(const struct device *dev, const charger_prop_t prop,
84 				union charger_propval *val)
85 {
86 	uint16_t reg_val;
87 	int ret;
88 
89 	switch (prop) {
90 	case CHARGER_PROP_ONLINE:
91 		ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &reg_val);
92 		if (ret < 0) {
93 			return ret;
94 		}
95 
96 		if (reg_val & SBS_CHARGER_STATUS_AC_PRESENT) {
97 			val->online = CHARGER_ONLINE_FIXED;
98 		} else {
99 			val->online = CHARGER_ONLINE_OFFLINE;
100 		}
101 
102 		return 0;
103 	case CHARGER_PROP_PRESENT:
104 		ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &reg_val);
105 		if (ret < 0) {
106 			return ret;
107 		}
108 
109 		if (reg_val & SBS_CHARGER_STATUS_BATTERY_PRESENT) {
110 			val->present = true;
111 		} else {
112 			val->present = false;
113 		}
114 
115 		return 0;
116 	case CHARGER_PROP_STATUS:
117 		ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &reg_val);
118 		if (ret < 0) {
119 			return ret;
120 		}
121 
122 		if (!(reg_val & SBS_CHARGER_STATUS_BATTERY_PRESENT)) {
123 			val->status = CHARGER_STATUS_NOT_CHARGING;
124 		} else if (reg_val & SBS_CHARGER_STATUS_AC_PRESENT &&
125 			   !(reg_val & SBS_CHARGER_STATUS_CHARGE_INHIBITED)) {
126 			val->status = CHARGER_STATUS_CHARGING;
127 		} else {
128 			val->status = CHARGER_STATUS_DISCHARGING;
129 		}
130 
131 		return 0;
132 	default:
133 		return -ENOTSUP;
134 	}
135 }
136 
sbs_charger_set_prop(const struct device * dev,const charger_prop_t prop,const union charger_propval * val)137 static int sbs_charger_set_prop(const struct device *dev, const charger_prop_t prop,
138 				const union charger_propval *val)
139 {
140 	return -ENOTSUP;
141 }
142 
143 /**
144  * @brief initialize the charger
145  *
146  * @return 0 for success
147  */
sbs_charger_init(const struct device * dev)148 static int sbs_charger_init(const struct device *dev)
149 {
150 	const struct sbs_charger_config *cfg = dev->config;
151 
152 	if (!i2c_is_ready_dt(&cfg->i2c)) {
153 		LOG_ERR("Bus device is not ready");
154 		return -ENODEV;
155 	}
156 
157 	return 0;
158 }
159 
160 static DEVICE_API(charger, sbs_charger_driver_api) = {
161 	.get_property = &sbs_charger_get_prop,
162 	.set_property = &sbs_charger_set_prop,
163 	.charge_enable = &sbs_charger_charge_enable,
164 };
165 
166 #define SBS_CHARGER_INIT(inst)                                                                     \
167                                                                                                    \
168 	static const struct sbs_charger_config sbs_charger_config_##inst = {                       \
169 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
170 	};                                                                                         \
171                                                                                                    \
172 	DEVICE_DT_INST_DEFINE(inst, &sbs_charger_init, NULL, NULL, &sbs_charger_config_##inst,     \
173 			      POST_KERNEL, CONFIG_CHARGER_INIT_PRIORITY, &sbs_charger_driver_api);
174 
175 DT_INST_FOREACH_STATUS_OKAY(SBS_CHARGER_INIT)
176