Linux 5.1.15
[linux/fpc-iii.git] / drivers / regulator / ltc3589.c
blob75089b037b723eae9467d8419b3ec54acc570862
1 /*
2 * Linear Technology LTC3589,LTC3589-1 regulator support
4 * Copyright (c) 2014 Philipp Zabel <p.zabel@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/regmap.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/of_regulator.h>
30 #define DRIVER_NAME "ltc3589"
32 #define LTC3589_IRQSTAT 0x02
33 #define LTC3589_SCR1 0x07
34 #define LTC3589_OVEN 0x10
35 #define LTC3589_SCR2 0x12
36 #define LTC3589_PGSTAT 0x13
37 #define LTC3589_VCCR 0x20
38 #define LTC3589_CLIRQ 0x21
39 #define LTC3589_B1DTV1 0x23
40 #define LTC3589_B1DTV2 0x24
41 #define LTC3589_VRRCR 0x25
42 #define LTC3589_B2DTV1 0x26
43 #define LTC3589_B2DTV2 0x27
44 #define LTC3589_B3DTV1 0x29
45 #define LTC3589_B3DTV2 0x2a
46 #define LTC3589_L2DTV1 0x32
47 #define LTC3589_L2DTV2 0x33
49 #define LTC3589_IRQSTAT_PGOOD_TIMEOUT BIT(3)
50 #define LTC3589_IRQSTAT_UNDERVOLT_WARN BIT(4)
51 #define LTC3589_IRQSTAT_UNDERVOLT_FAULT BIT(5)
52 #define LTC3589_IRQSTAT_THERMAL_WARN BIT(6)
53 #define LTC3589_IRQSTAT_THERMAL_FAULT BIT(7)
55 #define LTC3589_OVEN_SW1 BIT(0)
56 #define LTC3589_OVEN_SW2 BIT(1)
57 #define LTC3589_OVEN_SW3 BIT(2)
58 #define LTC3589_OVEN_BB_OUT BIT(3)
59 #define LTC3589_OVEN_LDO2 BIT(4)
60 #define LTC3589_OVEN_LDO3 BIT(5)
61 #define LTC3589_OVEN_LDO4 BIT(6)
62 #define LTC3589_OVEN_SW_CTRL BIT(7)
64 #define LTC3589_VCCR_SW1_GO BIT(0)
65 #define LTC3589_VCCR_SW2_GO BIT(2)
66 #define LTC3589_VCCR_SW3_GO BIT(4)
67 #define LTC3589_VCCR_LDO2_GO BIT(6)
69 enum ltc3589_variant {
70 LTC3589,
71 LTC3589_1,
72 LTC3589_2,
75 enum ltc3589_reg {
76 LTC3589_SW1,
77 LTC3589_SW2,
78 LTC3589_SW3,
79 LTC3589_BB_OUT,
80 LTC3589_LDO1,
81 LTC3589_LDO2,
82 LTC3589_LDO3,
83 LTC3589_LDO4,
84 LTC3589_NUM_REGULATORS,
87 struct ltc3589_regulator {
88 struct regulator_desc desc;
90 /* External feedback voltage divider */
91 unsigned int r1;
92 unsigned int r2;
95 struct ltc3589 {
96 struct regmap *regmap;
97 struct device *dev;
98 enum ltc3589_variant variant;
99 struct ltc3589_regulator regulator_descs[LTC3589_NUM_REGULATORS];
100 struct regulator_dev *regulators[LTC3589_NUM_REGULATORS];
103 static const int ltc3589_ldo4[] = {
104 2800000, 2500000, 1800000, 3300000,
107 static const int ltc3589_12_ldo4[] = {
108 1200000, 1800000, 2500000, 3200000,
111 static int ltc3589_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
113 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
114 int sel, shift;
116 if (unlikely(ramp_delay <= 0))
117 return -EINVAL;
119 /* VRRCR slew rate offsets are the same as VCCR go bit offsets */
120 shift = ffs(rdev->desc->apply_bit) - 1;
122 /* The slew rate can be set to 0.88, 1.75, 3.5, or 7 mV/uS */
123 for (sel = 0; sel < 4; sel++) {
124 if ((880 << sel) >= ramp_delay) {
125 return regmap_update_bits(ltc3589->regmap,
126 LTC3589_VRRCR,
127 0x3 << shift, sel << shift);
130 return -EINVAL;
133 static int ltc3589_set_suspend_voltage(struct regulator_dev *rdev, int uV)
135 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
136 int sel;
138 sel = regulator_map_voltage_linear(rdev, uV, uV);
139 if (sel < 0)
140 return sel;
142 /* DTV2 register follows right after the corresponding DTV1 register */
143 return regmap_update_bits(ltc3589->regmap, rdev->desc->vsel_reg + 1,
144 rdev->desc->vsel_mask, sel);
147 static int ltc3589_set_suspend_mode(struct regulator_dev *rdev,
148 unsigned int mode)
150 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
151 int mask, bit = 0;
153 /* VCCR reference selects are right next to the VCCR go bits */
154 mask = rdev->desc->apply_bit << 1;
156 if (mode == REGULATOR_MODE_STANDBY)
157 bit = mask; /* Select DTV2 */
159 mask |= rdev->desc->apply_bit;
160 bit |= rdev->desc->apply_bit;
161 return regmap_update_bits(ltc3589->regmap, LTC3589_VCCR, mask, bit);
164 /* SW1, SW2, SW3, LDO2 */
165 static const struct regulator_ops ltc3589_linear_regulator_ops = {
166 .enable = regulator_enable_regmap,
167 .disable = regulator_disable_regmap,
168 .is_enabled = regulator_is_enabled_regmap,
169 .list_voltage = regulator_list_voltage_linear,
170 .set_voltage_sel = regulator_set_voltage_sel_regmap,
171 .get_voltage_sel = regulator_get_voltage_sel_regmap,
172 .set_ramp_delay = ltc3589_set_ramp_delay,
173 .set_voltage_time_sel = regulator_set_voltage_time_sel,
174 .set_suspend_voltage = ltc3589_set_suspend_voltage,
175 .set_suspend_mode = ltc3589_set_suspend_mode,
178 /* BB_OUT, LDO3 */
179 static const struct regulator_ops ltc3589_fixed_regulator_ops = {
180 .enable = regulator_enable_regmap,
181 .disable = regulator_disable_regmap,
182 .is_enabled = regulator_is_enabled_regmap,
185 /* LDO1 */
186 static const struct regulator_ops ltc3589_fixed_standby_regulator_ops = {
189 /* LDO4 */
190 static const struct regulator_ops ltc3589_table_regulator_ops = {
191 .enable = regulator_enable_regmap,
192 .disable = regulator_disable_regmap,
193 .is_enabled = regulator_is_enabled_regmap,
194 .list_voltage = regulator_list_voltage_table,
195 .set_voltage_sel = regulator_set_voltage_sel_regmap,
196 .get_voltage_sel = regulator_get_voltage_sel_regmap,
200 #define LTC3589_REG(_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit) \
201 [LTC3589_ ## _name] = { \
202 .desc = { \
203 .name = #_name, \
204 .n_voltages = (dtv_mask) + 1, \
205 .min_uV = (go_bit) ? 362500 : 0, \
206 .uV_step = (go_bit) ? 12500 : 0, \
207 .ramp_delay = (go_bit) ? 1750 : 0, \
208 .fixed_uV = (dtv_mask) ? 0 : 800000, \
209 .ops = &ltc3589_ ## _ops ## _regulator_ops, \
210 .type = REGULATOR_VOLTAGE, \
211 .id = LTC3589_ ## _name, \
212 .owner = THIS_MODULE, \
213 .vsel_reg = (dtv1_reg), \
214 .vsel_mask = (dtv_mask), \
215 .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
216 .apply_bit = (go_bit), \
217 .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
218 .enable_mask = (en_bit), \
219 }, \
222 #define LTC3589_LINEAR_REG(_name, _dtv1) \
223 LTC3589_REG(_name, linear, LTC3589_OVEN_ ## _name, \
224 LTC3589_ ## _dtv1, 0x1f, \
225 LTC3589_VCCR_ ## _name ## _GO)
227 #define LTC3589_FIXED_REG(_name) \
228 LTC3589_REG(_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
230 static struct ltc3589_regulator ltc3589_regulators[LTC3589_NUM_REGULATORS] = {
231 LTC3589_LINEAR_REG(SW1, B1DTV1),
232 LTC3589_LINEAR_REG(SW2, B2DTV1),
233 LTC3589_LINEAR_REG(SW3, B3DTV1),
234 LTC3589_FIXED_REG(BB_OUT),
235 LTC3589_REG(LDO1, fixed_standby, 0, 0, 0, 0),
236 LTC3589_LINEAR_REG(LDO2, L2DTV1),
237 LTC3589_FIXED_REG(LDO3),
238 LTC3589_REG(LDO4, table, LTC3589_OVEN_LDO4, LTC3589_L2DTV2, 0x60, 0),
241 #ifdef CONFIG_OF
242 static struct of_regulator_match ltc3589_matches[LTC3589_NUM_REGULATORS] = {
243 { .name = "sw1", },
244 { .name = "sw2", },
245 { .name = "sw3", },
246 { .name = "bb-out", },
247 { .name = "ldo1", }, /* standby */
248 { .name = "ldo2", },
249 { .name = "ldo3", },
250 { .name = "ldo4", },
253 static int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
255 struct device *dev = ltc3589->dev;
256 struct device_node *node;
257 int i, ret;
259 node = of_get_child_by_name(dev->of_node, "regulators");
260 if (!node) {
261 dev_err(dev, "regulators node not found\n");
262 return -EINVAL;
265 ret = of_regulator_match(dev, node, ltc3589_matches,
266 ARRAY_SIZE(ltc3589_matches));
267 of_node_put(node);
268 if (ret < 0) {
269 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
270 return ret;
272 if (ret != LTC3589_NUM_REGULATORS) {
273 dev_err(dev, "Only %d regulators described in device tree\n",
274 ret);
275 return -EINVAL;
278 /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
279 for (i = 0; i < LTC3589_LDO3; i++) {
280 struct ltc3589_regulator *desc = &ltc3589->regulator_descs[i];
281 struct device_node *np = ltc3589_matches[i].of_node;
282 u32 vdiv[2];
284 ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider",
285 vdiv, 2);
286 if (ret) {
287 dev_err(dev, "Failed to parse voltage divider: %d\n",
288 ret);
289 return ret;
292 desc->r1 = vdiv[0];
293 desc->r2 = vdiv[1];
296 return 0;
299 static inline struct regulator_init_data *match_init_data(int index)
301 return ltc3589_matches[index].init_data;
304 static inline struct device_node *match_of_node(int index)
306 return ltc3589_matches[index].of_node;
308 #else
309 static inline int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
311 return 0;
314 static inline struct regulator_init_data *match_init_data(int index)
316 return NULL;
319 static inline struct device_node *match_of_node(int index)
321 return NULL;
323 #endif
325 static bool ltc3589_writeable_reg(struct device *dev, unsigned int reg)
327 switch (reg) {
328 case LTC3589_IRQSTAT:
329 case LTC3589_SCR1:
330 case LTC3589_OVEN:
331 case LTC3589_SCR2:
332 case LTC3589_VCCR:
333 case LTC3589_CLIRQ:
334 case LTC3589_B1DTV1:
335 case LTC3589_B1DTV2:
336 case LTC3589_VRRCR:
337 case LTC3589_B2DTV1:
338 case LTC3589_B2DTV2:
339 case LTC3589_B3DTV1:
340 case LTC3589_B3DTV2:
341 case LTC3589_L2DTV1:
342 case LTC3589_L2DTV2:
343 return true;
345 return false;
348 static bool ltc3589_readable_reg(struct device *dev, unsigned int reg)
350 switch (reg) {
351 case LTC3589_IRQSTAT:
352 case LTC3589_SCR1:
353 case LTC3589_OVEN:
354 case LTC3589_SCR2:
355 case LTC3589_PGSTAT:
356 case LTC3589_VCCR:
357 case LTC3589_B1DTV1:
358 case LTC3589_B1DTV2:
359 case LTC3589_VRRCR:
360 case LTC3589_B2DTV1:
361 case LTC3589_B2DTV2:
362 case LTC3589_B3DTV1:
363 case LTC3589_B3DTV2:
364 case LTC3589_L2DTV1:
365 case LTC3589_L2DTV2:
366 return true;
368 return false;
371 static bool ltc3589_volatile_reg(struct device *dev, unsigned int reg)
373 switch (reg) {
374 case LTC3589_IRQSTAT:
375 case LTC3589_PGSTAT:
376 case LTC3589_VCCR:
377 return true;
379 return false;
382 static const struct reg_default ltc3589_reg_defaults[] = {
383 { LTC3589_SCR1, 0x00 },
384 { LTC3589_OVEN, 0x00 },
385 { LTC3589_SCR2, 0x00 },
386 { LTC3589_VCCR, 0x00 },
387 { LTC3589_B1DTV1, 0x19 },
388 { LTC3589_B1DTV2, 0x19 },
389 { LTC3589_VRRCR, 0xff },
390 { LTC3589_B2DTV1, 0x19 },
391 { LTC3589_B2DTV2, 0x19 },
392 { LTC3589_B3DTV1, 0x19 },
393 { LTC3589_B3DTV2, 0x19 },
394 { LTC3589_L2DTV1, 0x19 },
395 { LTC3589_L2DTV2, 0x19 },
398 static const struct regmap_config ltc3589_regmap_config = {
399 .reg_bits = 8,
400 .val_bits = 8,
401 .writeable_reg = ltc3589_writeable_reg,
402 .readable_reg = ltc3589_readable_reg,
403 .volatile_reg = ltc3589_volatile_reg,
404 .max_register = LTC3589_L2DTV2,
405 .reg_defaults = ltc3589_reg_defaults,
406 .num_reg_defaults = ARRAY_SIZE(ltc3589_reg_defaults),
407 .use_single_read = true,
408 .use_single_write = true,
409 .cache_type = REGCACHE_RBTREE,
413 static irqreturn_t ltc3589_isr(int irq, void *dev_id)
415 struct ltc3589 *ltc3589 = dev_id;
416 unsigned int i, irqstat, event;
418 regmap_read(ltc3589->regmap, LTC3589_IRQSTAT, &irqstat);
420 if (irqstat & LTC3589_IRQSTAT_THERMAL_WARN) {
421 event = REGULATOR_EVENT_OVER_TEMP;
422 for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
423 regulator_lock(ltc3589->regulators[i]);
424 regulator_notifier_call_chain(ltc3589->regulators[i],
425 event, NULL);
426 regulator_unlock(ltc3589->regulators[i]);
430 if (irqstat & LTC3589_IRQSTAT_UNDERVOLT_WARN) {
431 event = REGULATOR_EVENT_UNDER_VOLTAGE;
432 for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
433 regulator_lock(ltc3589->regulators[i]);
434 regulator_notifier_call_chain(ltc3589->regulators[i],
435 event, NULL);
436 regulator_unlock(ltc3589->regulators[i]);
440 /* Clear warning condition */
441 regmap_write(ltc3589->regmap, LTC3589_CLIRQ, 0);
443 return IRQ_HANDLED;
446 static inline unsigned int ltc3589_scale(unsigned int uV, u32 r1, u32 r2)
448 uint64_t tmp;
449 if (uV == 0)
450 return 0;
451 tmp = (uint64_t)uV * r1;
452 do_div(tmp, r2);
453 return uV + (unsigned int)tmp;
456 static void ltc3589_apply_fb_voltage_divider(struct ltc3589_regulator *rdesc)
458 struct regulator_desc *desc = &rdesc->desc;
460 if (!rdesc->r1 || !rdesc->r2)
461 return;
463 desc->min_uV = ltc3589_scale(desc->min_uV, rdesc->r1, rdesc->r2);
464 desc->uV_step = ltc3589_scale(desc->uV_step, rdesc->r1, rdesc->r2);
465 desc->fixed_uV = ltc3589_scale(desc->fixed_uV, rdesc->r1, rdesc->r2);
468 static int ltc3589_probe(struct i2c_client *client,
469 const struct i2c_device_id *id)
471 struct device *dev = &client->dev;
472 struct ltc3589_regulator *descs;
473 struct ltc3589 *ltc3589;
474 int i, ret;
476 ltc3589 = devm_kzalloc(dev, sizeof(*ltc3589), GFP_KERNEL);
477 if (!ltc3589)
478 return -ENOMEM;
480 i2c_set_clientdata(client, ltc3589);
481 if (client->dev.of_node)
482 ltc3589->variant = (enum ltc3589_variant)
483 of_device_get_match_data(&client->dev);
484 else
485 ltc3589->variant = id->driver_data;
486 ltc3589->dev = dev;
488 descs = ltc3589->regulator_descs;
489 memcpy(descs, ltc3589_regulators, sizeof(ltc3589_regulators));
490 if (ltc3589->variant == LTC3589) {
491 descs[LTC3589_LDO3].desc.fixed_uV = 1800000;
492 descs[LTC3589_LDO4].desc.volt_table = ltc3589_ldo4;
493 } else {
494 descs[LTC3589_LDO3].desc.fixed_uV = 2800000;
495 descs[LTC3589_LDO4].desc.volt_table = ltc3589_12_ldo4;
498 ltc3589->regmap = devm_regmap_init_i2c(client, &ltc3589_regmap_config);
499 if (IS_ERR(ltc3589->regmap)) {
500 ret = PTR_ERR(ltc3589->regmap);
501 dev_err(dev, "failed to initialize regmap: %d\n", ret);
502 return ret;
505 ret = ltc3589_parse_regulators_dt(ltc3589);
506 if (ret)
507 return ret;
509 for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
510 struct ltc3589_regulator *rdesc = &ltc3589->regulator_descs[i];
511 struct regulator_desc *desc = &rdesc->desc;
512 struct regulator_init_data *init_data;
513 struct regulator_config config = { };
515 init_data = match_init_data(i);
517 if (i < LTC3589_LDO3)
518 ltc3589_apply_fb_voltage_divider(rdesc);
520 config.dev = dev;
521 config.init_data = init_data;
522 config.driver_data = ltc3589;
523 config.of_node = match_of_node(i);
525 ltc3589->regulators[i] = devm_regulator_register(dev, desc,
526 &config);
527 if (IS_ERR(ltc3589->regulators[i])) {
528 ret = PTR_ERR(ltc3589->regulators[i]);
529 dev_err(dev, "failed to register regulator %s: %d\n",
530 desc->name, ret);
531 return ret;
535 if (client->irq) {
536 ret = devm_request_threaded_irq(dev, client->irq, NULL,
537 ltc3589_isr,
538 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
539 client->name, ltc3589);
540 if (ret) {
541 dev_err(dev, "Failed to request IRQ: %d\n", ret);
542 return ret;
546 return 0;
549 static const struct i2c_device_id ltc3589_i2c_id[] = {
550 { "ltc3589", LTC3589 },
551 { "ltc3589-1", LTC3589_1 },
552 { "ltc3589-2", LTC3589_2 },
555 MODULE_DEVICE_TABLE(i2c, ltc3589_i2c_id);
557 static const struct of_device_id ltc3589_of_match[] = {
559 .compatible = "lltc,ltc3589",
560 .data = (void *)LTC3589,
563 .compatible = "lltc,ltc3589-1",
564 .data = (void *)LTC3589_1,
567 .compatible = "lltc,ltc3589-2",
568 .data = (void *)LTC3589_2,
570 { },
572 MODULE_DEVICE_TABLE(of, ltc3589_of_match);
574 static struct i2c_driver ltc3589_driver = {
575 .driver = {
576 .name = DRIVER_NAME,
577 .of_match_table = of_match_ptr(ltc3589_of_match),
579 .probe = ltc3589_probe,
580 .id_table = ltc3589_i2c_id,
582 module_i2c_driver(ltc3589_driver);
584 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
585 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3589(-1,2)");
586 MODULE_LICENSE("GPL v2");