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
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>
25 #include <linux/regmap.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/of_regulator.h>
29 #define DRIVER_NAME "ltc3589"
31 #define LTC3589_IRQSTAT 0x02
32 #define LTC3589_SCR1 0x07
33 #define LTC3589_OVEN 0x10
34 #define LTC3589_SCR2 0x12
35 #define LTC3589_PGSTAT 0x13
36 #define LTC3589_VCCR 0x20
37 #define LTC3589_CLIRQ 0x21
38 #define LTC3589_B1DTV1 0x23
39 #define LTC3589_B1DTV2 0x24
40 #define LTC3589_VRRCR 0x25
41 #define LTC3589_B2DTV1 0x26
42 #define LTC3589_B2DTV2 0x27
43 #define LTC3589_B3DTV1 0x29
44 #define LTC3589_B3DTV2 0x2a
45 #define LTC3589_L2DTV1 0x32
46 #define LTC3589_L2DTV2 0x33
48 #define LTC3589_IRQSTAT_PGOOD_TIMEOUT BIT(3)
49 #define LTC3589_IRQSTAT_UNDERVOLT_WARN BIT(4)
50 #define LTC3589_IRQSTAT_UNDERVOLT_FAULT BIT(5)
51 #define LTC3589_IRQSTAT_THERMAL_WARN BIT(6)
52 #define LTC3589_IRQSTAT_THERMAL_FAULT BIT(7)
54 #define LTC3589_OVEN_SW1 BIT(0)
55 #define LTC3589_OVEN_SW2 BIT(1)
56 #define LTC3589_OVEN_SW3 BIT(2)
57 #define LTC3589_OVEN_BB_OUT BIT(3)
58 #define LTC3589_OVEN_LDO2 BIT(4)
59 #define LTC3589_OVEN_LDO3 BIT(5)
60 #define LTC3589_OVEN_LDO4 BIT(6)
61 #define LTC3589_OVEN_SW_CTRL BIT(7)
63 #define LTC3589_VCCR_SW1_GO BIT(0)
64 #define LTC3589_VCCR_SW2_GO BIT(2)
65 #define LTC3589_VCCR_SW3_GO BIT(4)
66 #define LTC3589_VCCR_LDO2_GO BIT(6)
68 enum ltc3589_variant {
83 LTC3589_NUM_REGULATORS,
86 struct ltc3589_regulator {
87 struct regulator_desc desc;
89 /* External feedback voltage divider */
95 struct regmap *regmap;
97 enum ltc3589_variant variant;
98 struct ltc3589_regulator regulator_descs[LTC3589_NUM_REGULATORS];
99 struct regulator_dev *regulators[LTC3589_NUM_REGULATORS];
102 static const int ltc3589_ldo4[] = {
103 2800000, 2500000, 1800000, 3300000,
106 static const int ltc3589_12_ldo4[] = {
107 1200000, 1800000, 2500000, 3200000,
110 static int ltc3589_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
112 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
115 if (unlikely(ramp_delay <= 0))
118 /* VRRCR slew rate offsets are the same as VCCR go bit offsets */
119 shift = ffs(rdev->desc->apply_bit) - 1;
121 /* The slew rate can be set to 0.88, 1.75, 3.5, or 7 mV/uS */
122 for (sel = 0; sel < 4; sel++) {
123 if ((880 << sel) >= ramp_delay) {
124 return regmap_update_bits(ltc3589->regmap,
126 0x3 << shift, sel << shift);
132 static int ltc3589_set_suspend_voltage(struct regulator_dev *rdev, int uV)
134 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
137 sel = regulator_map_voltage_linear(rdev, uV, uV);
141 /* DTV2 register follows right after the corresponding DTV1 register */
142 return regmap_update_bits(ltc3589->regmap, rdev->desc->vsel_reg + 1,
143 rdev->desc->vsel_mask, sel);
146 static int ltc3589_set_suspend_mode(struct regulator_dev *rdev,
149 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
152 /* VCCR reference selects are right next to the VCCR go bits */
153 mask = rdev->desc->apply_bit << 1;
155 if (mode == REGULATOR_MODE_STANDBY)
156 bit = mask; /* Select DTV2 */
158 mask |= rdev->desc->apply_bit;
159 bit |= rdev->desc->apply_bit;
160 return regmap_update_bits(ltc3589->regmap, LTC3589_VCCR, mask, bit);
163 /* SW1, SW2, SW3, LDO2 */
164 static struct regulator_ops ltc3589_linear_regulator_ops = {
165 .enable = regulator_enable_regmap,
166 .disable = regulator_disable_regmap,
167 .is_enabled = regulator_is_enabled_regmap,
168 .list_voltage = regulator_list_voltage_linear,
169 .set_voltage_sel = regulator_set_voltage_sel_regmap,
170 .get_voltage_sel = regulator_get_voltage_sel_regmap,
171 .set_ramp_delay = ltc3589_set_ramp_delay,
172 .set_voltage_time_sel = regulator_set_voltage_time_sel,
173 .set_suspend_voltage = ltc3589_set_suspend_voltage,
174 .set_suspend_mode = ltc3589_set_suspend_mode,
178 static struct regulator_ops ltc3589_fixed_regulator_ops = {
179 .enable = regulator_enable_regmap,
180 .disable = regulator_disable_regmap,
181 .is_enabled = regulator_is_enabled_regmap,
185 static struct regulator_ops ltc3589_fixed_standby_regulator_ops = {
189 static struct regulator_ops ltc3589_table_regulator_ops = {
190 .enable = regulator_enable_regmap,
191 .disable = regulator_disable_regmap,
192 .is_enabled = regulator_is_enabled_regmap,
193 .list_voltage = regulator_list_voltage_table,
194 .set_voltage_sel = regulator_set_voltage_sel_regmap,
195 .get_voltage_sel = regulator_get_voltage_sel_regmap,
199 #define LTC3589_REG(_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit) \
200 [LTC3589_ ## _name] = { \
203 .n_voltages = (dtv_mask) + 1, \
204 .min_uV = (go_bit) ? 362500 : 0, \
205 .uV_step = (go_bit) ? 12500 : 0, \
206 .ramp_delay = (go_bit) ? 1750 : 0, \
207 .fixed_uV = (dtv_mask) ? 0 : 800000, \
208 .ops = <c3589_ ## _ops ## _regulator_ops, \
209 .type = REGULATOR_VOLTAGE, \
210 .id = LTC3589_ ## _name, \
211 .owner = THIS_MODULE, \
212 .vsel_reg = (dtv1_reg), \
213 .vsel_mask = (dtv_mask), \
214 .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
215 .apply_bit = (go_bit), \
216 .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
217 .enable_mask = (en_bit), \
221 #define LTC3589_LINEAR_REG(_name, _dtv1) \
222 LTC3589_REG(_name, linear, LTC3589_OVEN_ ## _name, \
223 LTC3589_ ## _dtv1, 0x1f, \
224 LTC3589_VCCR_ ## _name ## _GO)
226 #define LTC3589_FIXED_REG(_name) \
227 LTC3589_REG(_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
229 static struct ltc3589_regulator ltc3589_regulators[LTC3589_NUM_REGULATORS] = {
230 LTC3589_LINEAR_REG(SW1, B1DTV1),
231 LTC3589_LINEAR_REG(SW2, B2DTV1),
232 LTC3589_LINEAR_REG(SW3, B3DTV1),
233 LTC3589_FIXED_REG(BB_OUT),
234 LTC3589_REG(LDO1, fixed_standby, 0, 0, 0, 0),
235 LTC3589_LINEAR_REG(LDO2, L2DTV1),
236 LTC3589_FIXED_REG(LDO3),
237 LTC3589_REG(LDO4, table, LTC3589_OVEN_LDO4, LTC3589_L2DTV2, 0x60, 0),
241 static struct of_regulator_match ltc3589_matches[LTC3589_NUM_REGULATORS] = {
245 { .name = "bb-out", },
246 { .name = "ldo1", }, /* standby */
252 static int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
254 struct device *dev = ltc3589->dev;
255 struct device_node *node;
258 node = of_get_child_by_name(dev->of_node, "regulators");
260 dev_err(dev, "regulators node not found\n");
264 ret = of_regulator_match(dev, node, ltc3589_matches,
265 ARRAY_SIZE(ltc3589_matches));
268 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
271 if (ret != LTC3589_NUM_REGULATORS) {
272 dev_err(dev, "Only %d regulators described in device tree\n",
277 /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
278 for (i = 0; i < LTC3589_LDO3; i++) {
279 struct ltc3589_regulator *desc = <c3589->regulator_descs[i];
280 struct device_node *np = ltc3589_matches[i].of_node;
283 ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider",
286 dev_err(dev, "Failed to parse voltage divider: %d\n",
298 static inline struct regulator_init_data *match_init_data(int index)
300 return ltc3589_matches[index].init_data;
303 static inline struct device_node *match_of_node(int index)
305 return ltc3589_matches[index].of_node;
308 static inline int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
313 static inline struct regulator_init_data *match_init_data(int index)
318 static inline struct device_node *match_of_node(int index)
324 static bool ltc3589_writeable_reg(struct device *dev, unsigned int reg)
327 case LTC3589_IRQSTAT:
347 static bool ltc3589_readable_reg(struct device *dev, unsigned int reg)
350 case LTC3589_IRQSTAT:
370 static bool ltc3589_volatile_reg(struct device *dev, unsigned int reg)
373 case LTC3589_IRQSTAT:
381 static struct reg_default ltc3589_reg_defaults[] = {
382 { LTC3589_SCR1, 0x00 },
383 { LTC3589_OVEN, 0x00 },
384 { LTC3589_SCR2, 0x00 },
385 { LTC3589_VCCR, 0x00 },
386 { LTC3589_B1DTV1, 0x19 },
387 { LTC3589_B1DTV2, 0x19 },
388 { LTC3589_VRRCR, 0xff },
389 { LTC3589_B2DTV1, 0x19 },
390 { LTC3589_B2DTV2, 0x19 },
391 { LTC3589_B3DTV1, 0x19 },
392 { LTC3589_B3DTV2, 0x19 },
393 { LTC3589_L2DTV1, 0x19 },
394 { LTC3589_L2DTV2, 0x19 },
397 static const struct regmap_config ltc3589_regmap_config = {
400 .writeable_reg = ltc3589_writeable_reg,
401 .readable_reg = ltc3589_readable_reg,
402 .volatile_reg = ltc3589_volatile_reg,
403 .max_register = LTC3589_L2DTV2,
404 .reg_defaults = ltc3589_reg_defaults,
405 .num_reg_defaults = ARRAY_SIZE(ltc3589_reg_defaults),
406 .use_single_rw = true,
407 .cache_type = REGCACHE_RBTREE,
411 static irqreturn_t ltc3589_isr(int irq, void *dev_id)
413 struct ltc3589 *ltc3589 = dev_id;
414 unsigned int i, irqstat, event;
416 regmap_read(ltc3589->regmap, LTC3589_IRQSTAT, &irqstat);
418 if (irqstat & LTC3589_IRQSTAT_THERMAL_WARN) {
419 event = REGULATOR_EVENT_OVER_TEMP;
420 for (i = 0; i < LTC3589_NUM_REGULATORS; i++)
421 regulator_notifier_call_chain(ltc3589->regulators[i],
425 if (irqstat & LTC3589_IRQSTAT_UNDERVOLT_WARN) {
426 event = REGULATOR_EVENT_UNDER_VOLTAGE;
427 for (i = 0; i < LTC3589_NUM_REGULATORS; i++)
428 regulator_notifier_call_chain(ltc3589->regulators[i],
432 /* Clear warning condition */
433 regmap_write(ltc3589->regmap, LTC3589_CLIRQ, 0);
438 static inline unsigned int ltc3589_scale(unsigned int uV, u32 r1, u32 r2)
443 tmp = (uint64_t)uV * r1;
445 return uV + (unsigned int)tmp;
448 static void ltc3589_apply_fb_voltage_divider(struct ltc3589_regulator *rdesc)
450 struct regulator_desc *desc = &rdesc->desc;
452 if (!rdesc->r1 || !rdesc->r2)
455 desc->min_uV = ltc3589_scale(desc->min_uV, rdesc->r1, rdesc->r2);
456 desc->uV_step = ltc3589_scale(desc->uV_step, rdesc->r1, rdesc->r2);
457 desc->fixed_uV = ltc3589_scale(desc->fixed_uV, rdesc->r1, rdesc->r2);
460 static int ltc3589_probe(struct i2c_client *client,
461 const struct i2c_device_id *id)
463 struct device *dev = &client->dev;
464 struct ltc3589_regulator *descs;
465 struct ltc3589 *ltc3589;
468 ltc3589 = devm_kzalloc(dev, sizeof(*ltc3589), GFP_KERNEL);
472 i2c_set_clientdata(client, ltc3589);
473 ltc3589->variant = id->driver_data;
476 descs = ltc3589->regulator_descs;
477 memcpy(descs, ltc3589_regulators, sizeof(ltc3589_regulators));
478 if (ltc3589->variant == LTC3589) {
479 descs[LTC3589_LDO3].desc.fixed_uV = 1800000;
480 descs[LTC3589_LDO4].desc.volt_table = ltc3589_ldo4;
482 descs[LTC3589_LDO3].desc.fixed_uV = 2800000;
483 descs[LTC3589_LDO4].desc.volt_table = ltc3589_12_ldo4;
486 ltc3589->regmap = devm_regmap_init_i2c(client, <c3589_regmap_config);
487 if (IS_ERR(ltc3589->regmap)) {
488 ret = PTR_ERR(ltc3589->regmap);
489 dev_err(dev, "failed to initialize regmap: %d\n", ret);
493 ret = ltc3589_parse_regulators_dt(ltc3589);
497 for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
498 struct ltc3589_regulator *rdesc = <c3589->regulator_descs[i];
499 struct regulator_desc *desc = &rdesc->desc;
500 struct regulator_init_data *init_data;
501 struct regulator_config config = { };
503 init_data = match_init_data(i);
505 if (i < LTC3589_LDO3)
506 ltc3589_apply_fb_voltage_divider(rdesc);
509 config.init_data = init_data;
510 config.driver_data = ltc3589;
511 config.of_node = match_of_node(i);
513 ltc3589->regulators[i] = devm_regulator_register(dev, desc,
515 if (IS_ERR(ltc3589->regulators[i])) {
516 ret = PTR_ERR(ltc3589->regulators[i]);
517 dev_err(dev, "failed to register regulator %s: %d\n",
523 ret = devm_request_threaded_irq(dev, client->irq, NULL, ltc3589_isr,
524 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
525 client->name, ltc3589);
527 dev_err(dev, "Failed to request IRQ: %d\n", ret);
534 static struct i2c_device_id ltc3589_i2c_id[] = {
535 { "ltc3589", LTC3589 },
536 { "ltc3589-1", LTC3589_1 },
537 { "ltc3589-2", LTC3589_2 },
540 MODULE_DEVICE_TABLE(i2c, ltc3589_i2c_id);
542 static struct i2c_driver ltc3589_driver = {
545 .owner = THIS_MODULE,
547 .probe = ltc3589_probe,
548 .id_table = ltc3589_i2c_id,
550 module_i2c_driver(ltc3589_driver);
552 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
553 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3589(-1,2)");
554 MODULE_LICENSE("GPL v2");
555 MODULE_ALIAS("i2c:ltc3589");