1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | // |
3 | // da9211-regulator.c - Regulator device driver for DA9211/DA9212 |
4 | // /DA9213/DA9223/DA9214/DA9224/DA9215/DA9225 |
5 | // Copyright (C) 2015 Dialog Semiconductor Ltd. |
6 | |
7 | #include <linux/err.h> |
8 | #include <linux/i2c.h> |
9 | #include <linux/module.h> |
10 | #include <linux/init.h> |
11 | #include <linux/slab.h> |
12 | #include <linux/regulator/driver.h> |
13 | #include <linux/regulator/machine.h> |
14 | #include <linux/regmap.h> |
15 | #include <linux/irq.h> |
16 | #include <linux/interrupt.h> |
17 | #include <linux/gpio/consumer.h> |
18 | #include <linux/regulator/of_regulator.h> |
19 | #include <linux/regulator/da9211.h> |
20 | #include <dt-bindings/regulator/dlg,da9211-regulator.h> |
21 | #include "da9211-regulator.h" |
22 | |
23 | /* DEVICE IDs */ |
24 | #define DA9211_DEVICE_ID 0x22 |
25 | #define DA9213_DEVICE_ID 0x23 |
26 | #define DA9215_DEVICE_ID 0x24 |
27 | |
28 | /* DA9211 REGULATOR IDs */ |
29 | #define DA9211_ID_BUCKA 0 |
30 | #define DA9211_ID_BUCKB 1 |
31 | |
32 | struct da9211 { |
33 | struct device *dev; |
34 | struct regmap *regmap; |
35 | struct da9211_pdata *pdata; |
36 | struct regulator_dev *rdev[DA9211_MAX_REGULATORS]; |
37 | int num_regulator; |
38 | int chip_irq; |
39 | int chip_id; |
40 | }; |
41 | |
42 | static const struct regmap_range_cfg da9211_regmap_range[] = { |
43 | { |
44 | .selector_reg = DA9211_REG_PAGE_CON, |
45 | .selector_mask = DA9211_REG_PAGE_MASK, |
46 | .selector_shift = DA9211_REG_PAGE_SHIFT, |
47 | .window_start = 0, |
48 | .window_len = 256, |
49 | .range_min = 0, |
50 | .range_max = 5*128, |
51 | }, |
52 | }; |
53 | |
54 | static bool da9211_volatile_reg(struct device *dev, unsigned int reg) |
55 | { |
56 | switch (reg) { |
57 | case DA9211_REG_STATUS_A: |
58 | case DA9211_REG_STATUS_B: |
59 | case DA9211_REG_EVENT_A: |
60 | case DA9211_REG_EVENT_B: |
61 | return true; |
62 | } |
63 | return false; |
64 | } |
65 | |
66 | static const struct regmap_config da9211_regmap_config = { |
67 | .reg_bits = 8, |
68 | .val_bits = 8, |
69 | .max_register = 5 * 128, |
70 | .volatile_reg = da9211_volatile_reg, |
71 | .cache_type = REGCACHE_RBTREE, |
72 | .ranges = da9211_regmap_range, |
73 | .num_ranges = ARRAY_SIZE(da9211_regmap_range), |
74 | }; |
75 | |
76 | /* Default limits measured in millivolts and milliamps */ |
77 | #define DA9211_MIN_MV 300 |
78 | #define DA9211_MAX_MV 1570 |
79 | #define DA9211_STEP_MV 10 |
80 | |
81 | /* Current limits for DA9211 buck (uA) indices |
82 | * corresponds with register values |
83 | */ |
84 | static const int da9211_current_limits[] = { |
85 | 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000, |
86 | 3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000 |
87 | }; |
88 | /* Current limits for DA9213 buck (uA) indices |
89 | * corresponds with register values |
90 | */ |
91 | static const int da9213_current_limits[] = { |
92 | 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, |
93 | 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000 |
94 | }; |
95 | /* Current limits for DA9215 buck (uA) indices |
96 | * corresponds with register values |
97 | */ |
98 | static const int da9215_current_limits[] = { |
99 | 4000000, 4200000, 4400000, 4600000, 4800000, 5000000, 5200000, 5400000, |
100 | 5600000, 5800000, 6000000, 6200000, 6400000, 6600000, 6800000, 7000000 |
101 | }; |
102 | |
103 | static unsigned int da9211_map_buck_mode(unsigned int mode) |
104 | { |
105 | switch (mode) { |
106 | case DA9211_BUCK_MODE_SLEEP: |
107 | return REGULATOR_MODE_STANDBY; |
108 | case DA9211_BUCK_MODE_SYNC: |
109 | return REGULATOR_MODE_FAST; |
110 | case DA9211_BUCK_MODE_AUTO: |
111 | return REGULATOR_MODE_NORMAL; |
112 | default: |
113 | return REGULATOR_MODE_INVALID; |
114 | } |
115 | } |
116 | |
117 | static unsigned int da9211_buck_get_mode(struct regulator_dev *rdev) |
118 | { |
119 | int id = rdev_get_id(rdev); |
120 | struct da9211 *chip = rdev_get_drvdata(rdev); |
121 | unsigned int data; |
122 | int ret, mode = 0; |
123 | |
124 | ret = regmap_read(map: chip->regmap, DA9211_REG_BUCKA_CONF+id, val: &data); |
125 | if (ret < 0) |
126 | return ret; |
127 | |
128 | switch (data & 0x03) { |
129 | case DA9211_BUCK_MODE_SYNC: |
130 | mode = REGULATOR_MODE_FAST; |
131 | break; |
132 | case DA9211_BUCK_MODE_AUTO: |
133 | mode = REGULATOR_MODE_NORMAL; |
134 | break; |
135 | case DA9211_BUCK_MODE_SLEEP: |
136 | mode = REGULATOR_MODE_STANDBY; |
137 | break; |
138 | } |
139 | |
140 | return mode; |
141 | } |
142 | |
143 | static int da9211_buck_set_mode(struct regulator_dev *rdev, |
144 | unsigned int mode) |
145 | { |
146 | int id = rdev_get_id(rdev); |
147 | struct da9211 *chip = rdev_get_drvdata(rdev); |
148 | int val = 0; |
149 | |
150 | switch (mode) { |
151 | case REGULATOR_MODE_FAST: |
152 | val = DA9211_BUCK_MODE_SYNC; |
153 | break; |
154 | case REGULATOR_MODE_NORMAL: |
155 | val = DA9211_BUCK_MODE_AUTO; |
156 | break; |
157 | case REGULATOR_MODE_STANDBY: |
158 | val = DA9211_BUCK_MODE_SLEEP; |
159 | break; |
160 | } |
161 | |
162 | return regmap_update_bits(map: chip->regmap, DA9211_REG_BUCKA_CONF+id, |
163 | mask: 0x03, val); |
164 | } |
165 | |
166 | static int da9211_set_current_limit(struct regulator_dev *rdev, int min, |
167 | int max) |
168 | { |
169 | int id = rdev_get_id(rdev); |
170 | struct da9211 *chip = rdev_get_drvdata(rdev); |
171 | int i, max_size; |
172 | const int *current_limits; |
173 | |
174 | switch (chip->chip_id) { |
175 | case DA9211: |
176 | current_limits = da9211_current_limits; |
177 | max_size = ARRAY_SIZE(da9211_current_limits)-1; |
178 | break; |
179 | case DA9213: |
180 | current_limits = da9213_current_limits; |
181 | max_size = ARRAY_SIZE(da9213_current_limits)-1; |
182 | break; |
183 | case DA9215: |
184 | current_limits = da9215_current_limits; |
185 | max_size = ARRAY_SIZE(da9215_current_limits)-1; |
186 | break; |
187 | default: |
188 | return -EINVAL; |
189 | } |
190 | |
191 | /* search for closest to maximum */ |
192 | for (i = max_size; i >= 0; i--) { |
193 | if (min <= current_limits[i] && |
194 | max >= current_limits[i]) { |
195 | return regmap_update_bits(map: chip->regmap, |
196 | DA9211_REG_BUCK_ILIM, |
197 | mask: (0x0F << id*4), val: (i << id*4)); |
198 | } |
199 | } |
200 | |
201 | return -EINVAL; |
202 | } |
203 | |
204 | static int da9211_get_current_limit(struct regulator_dev *rdev) |
205 | { |
206 | int id = rdev_get_id(rdev); |
207 | struct da9211 *chip = rdev_get_drvdata(rdev); |
208 | unsigned int data; |
209 | int ret; |
210 | const int *current_limits; |
211 | |
212 | switch (chip->chip_id) { |
213 | case DA9211: |
214 | current_limits = da9211_current_limits; |
215 | break; |
216 | case DA9213: |
217 | current_limits = da9213_current_limits; |
218 | break; |
219 | case DA9215: |
220 | current_limits = da9215_current_limits; |
221 | break; |
222 | default: |
223 | return -EINVAL; |
224 | } |
225 | |
226 | ret = regmap_read(map: chip->regmap, DA9211_REG_BUCK_ILIM, val: &data); |
227 | if (ret < 0) |
228 | return ret; |
229 | |
230 | /* select one of 16 values: 0000 (2000mA or 3000mA) |
231 | * to 1111 (5000mA or 6000mA). |
232 | */ |
233 | data = (data >> id*4) & 0x0F; |
234 | return current_limits[data]; |
235 | } |
236 | |
237 | static const struct regulator_ops da9211_buck_ops = { |
238 | .get_mode = da9211_buck_get_mode, |
239 | .set_mode = da9211_buck_set_mode, |
240 | .enable = regulator_enable_regmap, |
241 | .disable = regulator_disable_regmap, |
242 | .is_enabled = regulator_is_enabled_regmap, |
243 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
244 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
245 | .list_voltage = regulator_list_voltage_linear, |
246 | .set_current_limit = da9211_set_current_limit, |
247 | .get_current_limit = da9211_get_current_limit, |
248 | }; |
249 | |
250 | #define DA9211_BUCK(_id) \ |
251 | {\ |
252 | .name = #_id,\ |
253 | .ops = &da9211_buck_ops,\ |
254 | .type = REGULATOR_VOLTAGE,\ |
255 | .id = DA9211_ID_##_id,\ |
256 | .n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\ |
257 | .min_uV = (DA9211_MIN_MV * 1000),\ |
258 | .uV_step = (DA9211_STEP_MV * 1000),\ |
259 | .enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\ |
260 | .enable_mask = DA9211_BUCKA_EN,\ |
261 | .vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\ |
262 | .vsel_mask = DA9211_VBUCK_MASK,\ |
263 | .owner = THIS_MODULE,\ |
264 | .of_map_mode = da9211_map_buck_mode,\ |
265 | } |
266 | |
267 | static struct regulator_desc da9211_regulators[] = { |
268 | DA9211_BUCK(BUCKA), |
269 | DA9211_BUCK(BUCKB), |
270 | }; |
271 | |
272 | #ifdef CONFIG_OF |
273 | static struct of_regulator_match da9211_matches[] = { |
274 | [DA9211_ID_BUCKA] = { |
275 | .name = "BUCKA" , |
276 | .desc = &da9211_regulators[DA9211_ID_BUCKA], |
277 | }, |
278 | [DA9211_ID_BUCKB] = { |
279 | .name = "BUCKB" , |
280 | .desc = &da9211_regulators[DA9211_ID_BUCKB], |
281 | }, |
282 | }; |
283 | |
284 | static struct da9211_pdata *da9211_parse_regulators_dt( |
285 | struct device *dev) |
286 | { |
287 | struct da9211_pdata *pdata; |
288 | struct device_node *node; |
289 | int i, num, n; |
290 | |
291 | node = of_get_child_by_name(node: dev->of_node, name: "regulators" ); |
292 | if (!node) { |
293 | dev_err(dev, "regulators node not found\n" ); |
294 | return ERR_PTR(error: -ENODEV); |
295 | } |
296 | |
297 | num = of_regulator_match(dev, node, matches: da9211_matches, |
298 | ARRAY_SIZE(da9211_matches)); |
299 | of_node_put(node); |
300 | if (num < 0) { |
301 | dev_err(dev, "Failed to match regulators\n" ); |
302 | return ERR_PTR(error: -EINVAL); |
303 | } |
304 | |
305 | pdata = devm_kzalloc(dev, size: sizeof(*pdata), GFP_KERNEL); |
306 | if (!pdata) |
307 | return ERR_PTR(error: -ENOMEM); |
308 | |
309 | pdata->num_buck = num; |
310 | |
311 | n = 0; |
312 | for (i = 0; i < ARRAY_SIZE(da9211_matches); i++) { |
313 | if (!da9211_matches[i].init_data) |
314 | continue; |
315 | |
316 | pdata->init_data[n] = da9211_matches[i].init_data; |
317 | pdata->reg_node[n] = da9211_matches[i].of_node; |
318 | pdata->gpiod_ren[n] = devm_fwnode_gpiod_get(dev, |
319 | of_fwnode_handle(pdata->reg_node[n]), |
320 | con_id: "enable" , |
321 | flags: GPIOD_OUT_HIGH | |
322 | GPIOD_FLAGS_BIT_NONEXCLUSIVE, |
323 | label: "da9211-enable" ); |
324 | if (IS_ERR(ptr: pdata->gpiod_ren[n])) |
325 | pdata->gpiod_ren[n] = NULL; |
326 | n++; |
327 | } |
328 | |
329 | return pdata; |
330 | } |
331 | #else |
332 | static struct da9211_pdata *da9211_parse_regulators_dt( |
333 | struct device *dev) |
334 | { |
335 | return ERR_PTR(-ENODEV); |
336 | } |
337 | #endif |
338 | |
339 | static irqreturn_t da9211_irq_handler(int irq, void *data) |
340 | { |
341 | struct da9211 *chip = data; |
342 | int reg_val, err, ret = IRQ_NONE; |
343 | |
344 | err = regmap_read(map: chip->regmap, DA9211_REG_EVENT_B, val: ®_val); |
345 | if (err < 0) |
346 | goto error_i2c; |
347 | |
348 | if (reg_val & DA9211_E_OV_CURR_A) { |
349 | regulator_notifier_call_chain(rdev: chip->rdev[0], |
350 | REGULATOR_EVENT_OVER_CURRENT, NULL); |
351 | |
352 | err = regmap_write(map: chip->regmap, DA9211_REG_EVENT_B, |
353 | DA9211_E_OV_CURR_A); |
354 | if (err < 0) |
355 | goto error_i2c; |
356 | |
357 | ret = IRQ_HANDLED; |
358 | } |
359 | |
360 | if (reg_val & DA9211_E_OV_CURR_B) { |
361 | regulator_notifier_call_chain(rdev: chip->rdev[1], |
362 | REGULATOR_EVENT_OVER_CURRENT, NULL); |
363 | |
364 | err = regmap_write(map: chip->regmap, DA9211_REG_EVENT_B, |
365 | DA9211_E_OV_CURR_B); |
366 | if (err < 0) |
367 | goto error_i2c; |
368 | |
369 | ret = IRQ_HANDLED; |
370 | } |
371 | |
372 | return ret; |
373 | |
374 | error_i2c: |
375 | dev_err(chip->dev, "I2C error : %d\n" , err); |
376 | return IRQ_NONE; |
377 | } |
378 | |
379 | static int da9211_regulator_init(struct da9211 *chip) |
380 | { |
381 | struct regulator_config config = { }; |
382 | int i, ret; |
383 | unsigned int data; |
384 | |
385 | ret = regmap_read(map: chip->regmap, DA9211_REG_CONFIG_E, val: &data); |
386 | if (ret < 0) { |
387 | dev_err(chip->dev, "Failed to read CONFIG_E reg: %d\n" , ret); |
388 | return ret; |
389 | } |
390 | |
391 | data &= DA9211_SLAVE_SEL; |
392 | /* If configuration for 1/2 bucks is different between platform data |
393 | * and the register, driver should exit. |
394 | */ |
395 | if (chip->pdata->num_buck == 1 && data == 0x00) |
396 | chip->num_regulator = 1; |
397 | else if (chip->pdata->num_buck == 2 && data != 0x00) |
398 | chip->num_regulator = 2; |
399 | else { |
400 | dev_err(chip->dev, "Configuration is mismatched\n" ); |
401 | return -EINVAL; |
402 | } |
403 | |
404 | for (i = 0; i < chip->num_regulator; i++) { |
405 | config.init_data = chip->pdata->init_data[i]; |
406 | config.dev = chip->dev; |
407 | config.driver_data = chip; |
408 | config.regmap = chip->regmap; |
409 | config.of_node = chip->pdata->reg_node[i]; |
410 | |
411 | if (chip->pdata->gpiod_ren[i]) |
412 | config.ena_gpiod = chip->pdata->gpiod_ren[i]; |
413 | else |
414 | config.ena_gpiod = NULL; |
415 | |
416 | /* |
417 | * Hand the GPIO descriptor management over to the regulator |
418 | * core, remove it from GPIO devres management. |
419 | */ |
420 | if (config.ena_gpiod) |
421 | devm_gpiod_unhinge(dev: chip->dev, desc: config.ena_gpiod); |
422 | chip->rdev[i] = devm_regulator_register(dev: chip->dev, |
423 | regulator_desc: &da9211_regulators[i], config: &config); |
424 | if (IS_ERR(ptr: chip->rdev[i])) { |
425 | dev_err(chip->dev, |
426 | "Failed to register DA9211 regulator\n" ); |
427 | return PTR_ERR(ptr: chip->rdev[i]); |
428 | } |
429 | |
430 | if (chip->chip_irq != 0) { |
431 | ret = regmap_update_bits(map: chip->regmap, |
432 | DA9211_REG_MASK_B, DA9211_M_OV_CURR_A << i, val: 0); |
433 | if (ret < 0) { |
434 | dev_err(chip->dev, |
435 | "Failed to update mask reg: %d\n" , ret); |
436 | return ret; |
437 | } |
438 | } |
439 | } |
440 | |
441 | return 0; |
442 | } |
443 | |
444 | /* |
445 | * I2C driver interface functions |
446 | */ |
447 | static int da9211_i2c_probe(struct i2c_client *i2c) |
448 | { |
449 | struct da9211 *chip; |
450 | int error, ret; |
451 | unsigned int data; |
452 | |
453 | chip = devm_kzalloc(dev: &i2c->dev, size: sizeof(struct da9211), GFP_KERNEL); |
454 | if (!chip) |
455 | return -ENOMEM; |
456 | |
457 | chip->dev = &i2c->dev; |
458 | chip->regmap = devm_regmap_init_i2c(i2c, &da9211_regmap_config); |
459 | if (IS_ERR(ptr: chip->regmap)) { |
460 | error = PTR_ERR(ptr: chip->regmap); |
461 | dev_err(chip->dev, "Failed to allocate register map: %d\n" , |
462 | error); |
463 | return error; |
464 | } |
465 | |
466 | i2c_set_clientdata(client: i2c, data: chip); |
467 | |
468 | chip->pdata = i2c->dev.platform_data; |
469 | |
470 | ret = regmap_read(map: chip->regmap, DA9211_REG_DEVICE_ID, val: &data); |
471 | if (ret < 0) { |
472 | dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n" , ret); |
473 | return ret; |
474 | } |
475 | |
476 | switch (data) { |
477 | case DA9211_DEVICE_ID: |
478 | chip->chip_id = DA9211; |
479 | break; |
480 | case DA9213_DEVICE_ID: |
481 | chip->chip_id = DA9213; |
482 | break; |
483 | case DA9215_DEVICE_ID: |
484 | chip->chip_id = DA9215; |
485 | break; |
486 | default: |
487 | dev_err(chip->dev, "Unsupported device id = 0x%x.\n" , data); |
488 | return -ENODEV; |
489 | } |
490 | |
491 | if (!chip->pdata) |
492 | chip->pdata = da9211_parse_regulators_dt(dev: chip->dev); |
493 | |
494 | if (IS_ERR(ptr: chip->pdata)) { |
495 | dev_err(chip->dev, "No regulators defined for the platform\n" ); |
496 | return PTR_ERR(ptr: chip->pdata); |
497 | } |
498 | |
499 | chip->chip_irq = i2c->irq; |
500 | |
501 | ret = da9211_regulator_init(chip); |
502 | if (ret < 0) { |
503 | dev_err(chip->dev, "Failed to initialize regulator: %d\n" , ret); |
504 | return ret; |
505 | } |
506 | |
507 | if (chip->chip_irq != 0) { |
508 | ret = devm_request_threaded_irq(dev: chip->dev, irq: chip->chip_irq, NULL, |
509 | thread_fn: da9211_irq_handler, |
510 | IRQF_TRIGGER_LOW|IRQF_ONESHOT, |
511 | devname: "da9211" , dev_id: chip); |
512 | if (ret != 0) { |
513 | dev_err(chip->dev, "Failed to request IRQ: %d\n" , |
514 | chip->chip_irq); |
515 | return ret; |
516 | } |
517 | } else { |
518 | dev_warn(chip->dev, "No IRQ configured\n" ); |
519 | } |
520 | |
521 | return ret; |
522 | } |
523 | |
524 | static const struct i2c_device_id da9211_i2c_id[] = { |
525 | {"da9211" , DA9211}, |
526 | {"da9212" , DA9212}, |
527 | {"da9213" , DA9213}, |
528 | {"da9223" , DA9223}, |
529 | {"da9214" , DA9214}, |
530 | {"da9224" , DA9224}, |
531 | {"da9215" , DA9215}, |
532 | {"da9225" , DA9225}, |
533 | {}, |
534 | }; |
535 | MODULE_DEVICE_TABLE(i2c, da9211_i2c_id); |
536 | |
537 | #ifdef CONFIG_OF |
538 | static const struct of_device_id da9211_dt_ids[] = { |
539 | { .compatible = "dlg,da9211" , .data = &da9211_i2c_id[0] }, |
540 | { .compatible = "dlg,da9212" , .data = &da9211_i2c_id[1] }, |
541 | { .compatible = "dlg,da9213" , .data = &da9211_i2c_id[2] }, |
542 | { .compatible = "dlg,da9223" , .data = &da9211_i2c_id[3] }, |
543 | { .compatible = "dlg,da9214" , .data = &da9211_i2c_id[4] }, |
544 | { .compatible = "dlg,da9224" , .data = &da9211_i2c_id[5] }, |
545 | { .compatible = "dlg,da9215" , .data = &da9211_i2c_id[6] }, |
546 | { .compatible = "dlg,da9225" , .data = &da9211_i2c_id[7] }, |
547 | {}, |
548 | }; |
549 | MODULE_DEVICE_TABLE(of, da9211_dt_ids); |
550 | #endif |
551 | |
552 | static struct i2c_driver da9211_regulator_driver = { |
553 | .driver = { |
554 | .name = "da9211" , |
555 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
556 | .of_match_table = of_match_ptr(da9211_dt_ids), |
557 | }, |
558 | .probe = da9211_i2c_probe, |
559 | .id_table = da9211_i2c_id, |
560 | }; |
561 | |
562 | module_i2c_driver(da9211_regulator_driver); |
563 | |
564 | MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>" ); |
565 | MODULE_DESCRIPTION("DA9211/DA9212/DA9213/DA9223/DA9214/DA9224/DA9215/DA9225 regulator driver" ); |
566 | MODULE_LICENSE("GPL" ); |
567 | |