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_get_prop(const struct device * dev,const charger_prop_t prop,union charger_propval * val)69 static int sbs_charger_get_prop(const struct device *dev, const charger_prop_t prop,
70 				union charger_propval *val)
71 {
72 	uint16_t reg_val;
73 	int ret;
74 
75 	switch (prop) {
76 	case CHARGER_PROP_ONLINE:
77 		ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &reg_val);
78 		if (ret < 0) {
79 			return ret;
80 		}
81 
82 		if (reg_val & SBS_CHARGER_STATUS_AC_PRESENT) {
83 			val->online = CHARGER_ONLINE_FIXED;
84 		} else {
85 			val->online = CHARGER_ONLINE_OFFLINE;
86 		}
87 
88 		return 0;
89 	case CHARGER_PROP_PRESENT:
90 		ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &reg_val);
91 		if (ret < 0) {
92 			return ret;
93 		}
94 
95 		if (reg_val & SBS_CHARGER_STATUS_BATTERY_PRESENT) {
96 			val->present = true;
97 		} else {
98 			val->present = false;
99 		}
100 
101 		return 0;
102 	case CHARGER_PROP_STATUS:
103 		ret = sbs_cmd_reg_read(dev, SBS_CHARGER_REG_STATUS, &reg_val);
104 		if (ret < 0) {
105 			return ret;
106 		}
107 
108 		if (!(reg_val & SBS_CHARGER_STATUS_BATTERY_PRESENT)) {
109 			val->status = CHARGER_STATUS_NOT_CHARGING;
110 		} else if (reg_val & SBS_CHARGER_STATUS_AC_PRESENT &&
111 			   !(reg_val & SBS_CHARGER_STATUS_CHARGE_INHIBITED)) {
112 			val->status = CHARGER_STATUS_CHARGING;
113 		} else {
114 			val->status = CHARGER_STATUS_DISCHARGING;
115 		}
116 
117 		return 0;
118 	default:
119 		return -ENOTSUP;
120 	}
121 }
122 
sbs_charger_set_prop(const struct device * dev,const charger_prop_t prop,const union charger_propval * val)123 static int sbs_charger_set_prop(const struct device *dev, const charger_prop_t prop,
124 				const union charger_propval *val)
125 {
126 	uint16_t reg_val = 0;
127 
128 	switch (prop) {
129 	case CHARGER_PROP_STATUS:
130 		if (val->status != CHARGER_STATUS_CHARGING) {
131 			reg_val = SBS_CHARGER_MODE_INHIBIT_CHARGE;
132 		}
133 		return sbs_cmd_reg_update(dev, SBS_CHARGER_REG_CHARGER_MODE,
134 					  SBS_CHARGER_MODE_INHIBIT_CHARGE, reg_val);
135 	default:
136 		return -ENOTSUP;
137 	}
138 }
139 
140 /**
141  * @brief initialize the charger
142  *
143  * @return 0 for success
144  */
sbs_charger_init(const struct device * dev)145 static int sbs_charger_init(const struct device *dev)
146 {
147 	const struct sbs_charger_config *cfg = dev->config;
148 
149 	if (!i2c_is_ready_dt(&cfg->i2c)) {
150 		LOG_ERR("Bus device is not ready");
151 		return -ENODEV;
152 	}
153 
154 	return 0;
155 }
156 
157 static const struct charger_driver_api sbs_charger_driver_api = {
158 	.get_property = &sbs_charger_get_prop,
159 	.set_property = &sbs_charger_set_prop,
160 };
161 
162 #define SBS_CHARGER_INIT(inst)                                                                     \
163                                                                                                    \
164 	static const struct sbs_charger_config sbs_charger_config_##inst = {                       \
165 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
166 	};                                                                                         \
167                                                                                                    \
168 	DEVICE_DT_INST_DEFINE(inst, &sbs_charger_init, NULL, NULL, &sbs_charger_config_##inst,     \
169 			      POST_KERNEL, CONFIG_CHARGER_INIT_PRIORITY, &sbs_charger_driver_api);
170 
171 DT_INST_FOREACH_STATUS_OKAY(SBS_CHARGER_INIT)
172