xref: /linux/drivers/regulator/sgm3804-regulator.c (revision b3438e5ca785565a65ef231bc03cd5a05c3be5c7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // SGMicro SGM3804 regulator Driver
4 //
5 // Copyright (C) 2025 Kancy Joe <kancy2333@outlook.com>
6 // Copyright (C) 2026 Linaro Limited
7 // Author: Neil Armstrong <neil.armstrong@linaro.org>
8 
9 #include <linux/err.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/regulator/of_regulator.h>
15 #include <linux/gpio/consumer.h>
16 
17 #define SGM3804_POS_RAIL_VOLTAGE_REG	0x0
18 #define SGM3804_NEG_RAIL_VOLTAGE_REG	0x1
19 #define SGM3804_RAIL_DISCHARGE_REG	0x3
20 
21 #define RAIL_VOLTAGE_MASK	GENMASK(5, 0)
22 
23 #define POS_RAIL_DISCHARGE_EN	BIT(1)
24 #define NEG_RAIL_DISCHARGE_EN	BIT(0)
25 
26 #define RAIL_VOLTAGE_INVALID		RAIL_VOLTAGE_MASK
27 #define RAIL_DISCHARGE_REG_DEFAULT	(POS_RAIL_DISCHARGE_EN | NEG_RAIL_DISCHARGE_EN)
28 
29 #define SGM3804_VOLTAGES_MAX_SELECTOR	0x2f
30 
31 enum {
32 	SGM3804_POS_RAIL = 0,
33 	SGM3804_NEG_RAIL,
34 	SGM3804_RAIL_COUNT,
35 };
36 
37 /*
38  * The registers are only writable when the gpio is enabled, so
39  * we need to use the cache for read operations and set the regmap
40  * as cache_only when both GPIOs are down.
41  */
42 struct sgm3804_data {
43 	struct regmap *regmap;
44 	/* Protects the regcache state update */
45 	struct mutex lock;
46 	struct gpio_desc *gpios[SGM3804_RAIL_COUNT];
47 };
48 
49 static const struct linear_range sgm3804_voltages[] = {
50 	REGULATOR_LINEAR_RANGE(2400000, 0x20, 0x2f, 100000),
51 	REGULATOR_LINEAR_RANGE(4000000, 0x00, 0x17, 100000),
52 };
53 
54 /*
55  * The cache is populated with those hardware default values
56  * so the regmap_update_bits operation will use the cached
57  * value to build a new register value and write it when GPIOs
58  * are enabled.
59  */
60 static const struct reg_default sgm3804_reg_defaults[] = {
61 	{ SGM3804_POS_RAIL_VOLTAGE_REG, RAIL_VOLTAGE_INVALID },
62 	{ SGM3804_NEG_RAIL_VOLTAGE_REG, RAIL_VOLTAGE_INVALID },
63 	{ SGM3804_RAIL_DISCHARGE_REG, RAIL_DISCHARGE_REG_DEFAULT },
64 };
65 
66 /* Registers are only writable */
67 static bool sgm3804_writeable_reg(struct device *dev, unsigned int reg)
68 {
69 	switch (reg) {
70 	case SGM3804_POS_RAIL_VOLTAGE_REG:
71 	case SGM3804_NEG_RAIL_VOLTAGE_REG:
72 	case SGM3804_RAIL_DISCHARGE_REG:
73 		return true;
74 	default:
75 		return false;
76 	}
77 }
78 
79 /*
80  * Since all registers are only writeable, regmap will only read from the cache data.
81  */
82 static bool sgm3804_readable_reg(struct device *dev, unsigned int reg)
83 {
84 	return false;
85 }
86 
87 static const struct regmap_config sgm3804_regmap_config = {
88 	.reg_bits = 8,
89 	.val_bits = 8,
90 	.max_register = 0x03,
91 	.writeable_reg = sgm3804_writeable_reg,
92 	.readable_reg = sgm3804_readable_reg,
93 	.cache_type = REGCACHE_MAPLE,
94 	.reg_defaults = sgm3804_reg_defaults,
95 	.num_reg_defaults = ARRAY_SIZE(sgm3804_reg_defaults),
96 };
97 
98 static int sgm3804_sync_regcache_state(struct sgm3804_data *ctx)
99 {
100 	guard(mutex)(&ctx->lock);
101 
102 	/* If both GPIOs are down, IC is powered down and I2C writes will fail */
103 	if (!gpiod_get_value_cansleep(ctx->gpios[SGM3804_POS_RAIL]) &&
104 	    !gpiod_get_value_cansleep(ctx->gpios[SGM3804_NEG_RAIL])) {
105 		regcache_cache_only(ctx->regmap, true);
106 		regcache_mark_dirty(ctx->regmap);
107 	} else {
108 		int ret;
109 
110 		/* At least a GPIO is up, we can write registers */
111 		regcache_cache_only(ctx->regmap, false);
112 		ret = regcache_sync(ctx->regmap);
113 		if (ret) {
114 			regcache_cache_only(ctx->regmap, true);
115 			return ret;
116 		}
117 	}
118 
119 	return 0;
120 }
121 
122 static int sgm3804_get_voltage_sel(struct regulator_dev *rdev)
123 {
124 	int ret;
125 
126 	ret = regulator_get_voltage_sel_regmap(rdev);
127 	if (ret < 0)
128 		return ret;
129 
130 	/* Force setting a voltage on probe */
131 	if (ret == RAIL_VOLTAGE_INVALID)
132 		return -ENOTRECOVERABLE;
133 
134 	return ret;
135 }
136 
137 static int sgm3804_enable(struct regulator_dev *rdev)
138 {
139 	struct sgm3804_data *ctx = rdev->reg_data;
140 	int ret;
141 
142 	ret = gpiod_set_value_cansleep(ctx->gpios[rdev_get_id(rdev)], 1);
143 	if (ret)
144 		return ret;
145 
146 	ret = sgm3804_sync_regcache_state(ctx);
147 	if (ret)
148 		goto err;
149 
150 	return 0;
151 
152 err:
153 	gpiod_set_value_cansleep(ctx->gpios[rdev_get_id(rdev)], 0);
154 	return ret;
155 }
156 
157 static int sgm3804_disable(struct regulator_dev *rdev)
158 {
159 	struct sgm3804_data *ctx = rdev->reg_data;
160 	int ret;
161 
162 	ret = gpiod_set_value_cansleep(ctx->gpios[rdev_get_id(rdev)], 0);
163 	if (ret)
164 		return ret;
165 
166 	return sgm3804_sync_regcache_state(ctx);
167 }
168 
169 static int sgm3804_is_enabled(struct regulator_dev *rdev)
170 {
171 	struct sgm3804_data *ctx = rdev->reg_data;
172 
173 	return gpiod_get_value_cansleep(ctx->gpios[rdev_get_id(rdev)]);
174 }
175 
176 static const struct regulator_ops sgm3804_ops = {
177 	.list_voltage = regulator_list_voltage_linear_range,
178 	.map_voltage = regulator_map_voltage_linear_range,
179 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
180 	.get_voltage_sel = sgm3804_get_voltage_sel,
181 	.set_active_discharge = regulator_set_active_discharge_regmap,
182 	.enable = sgm3804_enable,
183 	.disable = sgm3804_disable,
184 	.is_enabled = sgm3804_is_enabled,
185 };
186 
187 static const struct regulator_desc sgm3804_regulator_desc[] = {
188 	/* Positive Output */
189 	{
190 		.name = "pos",
191 		.of_match = "pos",
192 		.supply_name = "vin",
193 		.id = SGM3804_POS_RAIL,
194 		.ops = &sgm3804_ops,
195 		.type = REGULATOR_VOLTAGE,
196 		.linear_ranges = sgm3804_voltages,
197 		.n_linear_ranges = ARRAY_SIZE(sgm3804_voltages),
198 		.n_voltages = SGM3804_VOLTAGES_MAX_SELECTOR + 1,
199 		.vsel_reg = SGM3804_POS_RAIL_VOLTAGE_REG,
200 		.vsel_mask = RAIL_VOLTAGE_MASK,
201 		.active_discharge_on = POS_RAIL_DISCHARGE_EN,
202 		.active_discharge_mask = POS_RAIL_DISCHARGE_EN,
203 		.active_discharge_reg = SGM3804_RAIL_DISCHARGE_REG,
204 		.enable_time = 40000,
205 		.owner = THIS_MODULE,
206 	},
207 	/* Negative Output */
208 	{
209 		.name = "neg",
210 		.of_match = "neg",
211 		.supply_name = "vin",
212 		.id = SGM3804_NEG_RAIL,
213 		.ops = &sgm3804_ops,
214 		.type = REGULATOR_VOLTAGE,
215 		.linear_ranges = sgm3804_voltages,
216 		.n_linear_ranges = ARRAY_SIZE(sgm3804_voltages),
217 		.n_voltages = SGM3804_VOLTAGES_MAX_SELECTOR + 1,
218 		.vsel_reg = SGM3804_NEG_RAIL_VOLTAGE_REG,
219 		.vsel_mask = RAIL_VOLTAGE_MASK,
220 		.active_discharge_on = NEG_RAIL_DISCHARGE_EN,
221 		.active_discharge_mask = NEG_RAIL_DISCHARGE_EN,
222 		.active_discharge_reg = SGM3804_RAIL_DISCHARGE_REG,
223 		.enable_time = 40000,
224 		.owner = THIS_MODULE,
225 	},
226 };
227 
228 static int sgm3804_probe(struct i2c_client *i2c)
229 {
230 	struct device *dev = &i2c->dev;
231 	struct sgm3804_data *ctx;
232 	int ret, i;
233 
234 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
235 	if (!ctx)
236 		return -ENOMEM;
237 
238 	mutex_init(&ctx->lock);
239 
240 	ctx->regmap = devm_regmap_init_i2c(i2c, &sgm3804_regmap_config);
241 	if (IS_ERR(ctx->regmap))
242 		return dev_err_probe(dev, PTR_ERR(ctx->regmap),
243 				     "failed to init regmap\n");
244 
245 	/* Get enable GPIOs */
246 	for (i = 0; i < ARRAY_SIZE(sgm3804_regulator_desc); i++) {
247 		const struct regulator_desc *reg = &sgm3804_regulator_desc[i];
248 		struct fwnode_handle *child;
249 
250 		child = device_get_named_child_node(dev, reg->of_match);
251 		if (!child) {
252 			dev_err(dev, "missing child '%s'\n", reg->of_match);
253 			return -EINVAL;
254 		}
255 
256 		ctx->gpios[i] = devm_fwnode_gpiod_get(dev, child, "enable",
257 						      GPIOD_ASIS, reg->name);
258 		fwnode_handle_put(child);
259 		if (IS_ERR(ctx->gpios[i]))
260 			return dev_err_probe(dev, PTR_ERR(ctx->gpios[i]),
261 					     "failed to get '%s' enable GPIO\n",
262 					     reg->name);
263 	}
264 
265 	ret = sgm3804_sync_regcache_state(ctx);
266 	if (ret)
267 		return ret;
268 
269 	for (i = 0; i < ARRAY_SIZE(sgm3804_regulator_desc); i++) {
270 		struct regulator_config config = { };
271 		struct regulator_dev *rdev;
272 
273 		config.dev = dev;
274 		config.regmap = ctx->regmap;
275 		config.of_node = dev_of_node(dev);
276 		config.driver_data = ctx;
277 		rdev = devm_regulator_register(dev, &sgm3804_regulator_desc[i],
278 					       &config);
279 		if (IS_ERR(rdev))
280 			return dev_err_probe(dev, PTR_ERR(rdev),
281 					     "failed to register regulator %d\n", i);
282 	}
283 
284 	return 0;
285 }
286 
287 static const struct i2c_device_id sgm3804_id[] = {
288 	{ .name = "sgm3804" },
289 	{ }
290 };
291 MODULE_DEVICE_TABLE(i2c, sgm3804_id);
292 
293 static const struct of_device_id sgm3804_of_match[] = {
294 	{ .compatible = "sgmicro,sgm3804" },
295 	{ }
296 };
297 MODULE_DEVICE_TABLE(of, sgm3804_of_match);
298 
299 static struct i2c_driver sgm3804_regulator_driver = {
300 	.driver = {
301 		.name = "sgm3804",
302 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
303 		.of_match_table = sgm3804_of_match,
304 	},
305 	.probe = sgm3804_probe,
306 	.id_table = sgm3804_id,
307 };
308 
309 module_i2c_driver(sgm3804_regulator_driver);
310 
311 MODULE_DESCRIPTION("SGMicro SGM3804 regulator Driver");
312 MODULE_AUTHOR("Kancy Joe <kancy2333@outlook.com>");
313 MODULE_AUTHOR("Neil Armstrong <neil.armstrong@linaro.org>");
314 MODULE_LICENSE("GPL");
315