1 // SPDX-License-Identifier: GPL-2.0
3 // Linear Technology LTC3589,LTC3589-1 regulator support
5 // Copyright (c) 2014 Philipp Zabel <p.zabel@pengutronix.de>, Pengutronix
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
13 #include <linux/of_device.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/of_regulator.h>
18 #define DRIVER_NAME "ltc3589"
20 #define LTC3589_IRQSTAT 0x02
21 #define LTC3589_SCR1 0x07
22 #define LTC3589_OVEN 0x10
23 #define LTC3589_SCR2 0x12
24 #define LTC3589_PGSTAT 0x13
25 #define LTC3589_VCCR 0x20
26 #define LTC3589_CLIRQ 0x21
27 #define LTC3589_B1DTV1 0x23
28 #define LTC3589_B1DTV2 0x24
29 #define LTC3589_VRRCR 0x25
30 #define LTC3589_B2DTV1 0x26
31 #define LTC3589_B2DTV2 0x27
32 #define LTC3589_B3DTV1 0x29
33 #define LTC3589_B3DTV2 0x2a
34 #define LTC3589_L2DTV1 0x32
35 #define LTC3589_L2DTV2 0x33
37 #define LTC3589_IRQSTAT_PGOOD_TIMEOUT BIT(3)
38 #define LTC3589_IRQSTAT_UNDERVOLT_WARN BIT(4)
39 #define LTC3589_IRQSTAT_UNDERVOLT_FAULT BIT(5)
40 #define LTC3589_IRQSTAT_THERMAL_WARN BIT(6)
41 #define LTC3589_IRQSTAT_THERMAL_FAULT BIT(7)
43 #define LTC3589_OVEN_SW1 BIT(0)
44 #define LTC3589_OVEN_SW2 BIT(1)
45 #define LTC3589_OVEN_SW3 BIT(2)
46 #define LTC3589_OVEN_BB_OUT BIT(3)
47 #define LTC3589_OVEN_LDO2 BIT(4)
48 #define LTC3589_OVEN_LDO3 BIT(5)
49 #define LTC3589_OVEN_LDO4 BIT(6)
50 #define LTC3589_OVEN_SW_CTRL BIT(7)
52 #define LTC3589_VCCR_SW1_GO BIT(0)
53 #define LTC3589_VCCR_SW2_GO BIT(2)
54 #define LTC3589_VCCR_SW3_GO BIT(4)
55 #define LTC3589_VCCR_LDO2_GO BIT(6)
57 enum ltc3589_variant
{
72 LTC3589_NUM_REGULATORS
,
76 struct regmap
*regmap
;
78 enum ltc3589_variant variant
;
79 struct regulator_desc regulator_descs
[LTC3589_NUM_REGULATORS
];
80 struct regulator_dev
*regulators
[LTC3589_NUM_REGULATORS
];
83 static const int ltc3589_ldo4
[] = {
84 2800000, 2500000, 1800000, 3300000,
87 static const int ltc3589_12_ldo4
[] = {
88 1200000, 1800000, 2500000, 3200000,
91 static int ltc3589_set_ramp_delay(struct regulator_dev
*rdev
, int ramp_delay
)
93 struct ltc3589
*ltc3589
= rdev_get_drvdata(rdev
);
96 if (unlikely(ramp_delay
<= 0))
99 /* VRRCR slew rate offsets are the same as VCCR go bit offsets */
100 shift
= ffs(rdev
->desc
->apply_bit
) - 1;
102 /* The slew rate can be set to 0.88, 1.75, 3.5, or 7 mV/uS */
103 for (sel
= 0; sel
< 4; sel
++) {
104 if ((880 << sel
) >= ramp_delay
) {
105 return regmap_update_bits(ltc3589
->regmap
,
107 0x3 << shift
, sel
<< shift
);
113 static int ltc3589_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
115 struct ltc3589
*ltc3589
= rdev_get_drvdata(rdev
);
118 sel
= regulator_map_voltage_linear(rdev
, uV
, uV
);
122 /* DTV2 register follows right after the corresponding DTV1 register */
123 return regmap_update_bits(ltc3589
->regmap
, rdev
->desc
->vsel_reg
+ 1,
124 rdev
->desc
->vsel_mask
, sel
);
127 static int ltc3589_set_suspend_mode(struct regulator_dev
*rdev
,
130 struct ltc3589
*ltc3589
= rdev_get_drvdata(rdev
);
133 /* VCCR reference selects are right next to the VCCR go bits */
134 mask
= rdev
->desc
->apply_bit
<< 1;
136 if (mode
== REGULATOR_MODE_STANDBY
)
137 bit
= mask
; /* Select DTV2 */
139 mask
|= rdev
->desc
->apply_bit
;
140 bit
|= rdev
->desc
->apply_bit
;
141 return regmap_update_bits(ltc3589
->regmap
, LTC3589_VCCR
, mask
, bit
);
144 /* SW1, SW2, SW3, LDO2 */
145 static const struct regulator_ops ltc3589_linear_regulator_ops
= {
146 .enable
= regulator_enable_regmap
,
147 .disable
= regulator_disable_regmap
,
148 .is_enabled
= regulator_is_enabled_regmap
,
149 .list_voltage
= regulator_list_voltage_linear
,
150 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
151 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
152 .set_ramp_delay
= ltc3589_set_ramp_delay
,
153 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
154 .set_suspend_voltage
= ltc3589_set_suspend_voltage
,
155 .set_suspend_mode
= ltc3589_set_suspend_mode
,
159 static const struct regulator_ops ltc3589_fixed_regulator_ops
= {
160 .enable
= regulator_enable_regmap
,
161 .disable
= regulator_disable_regmap
,
162 .is_enabled
= regulator_is_enabled_regmap
,
166 static const struct regulator_ops ltc3589_fixed_standby_regulator_ops
= {
170 static const struct regulator_ops ltc3589_table_regulator_ops
= {
171 .enable
= regulator_enable_regmap
,
172 .disable
= regulator_disable_regmap
,
173 .is_enabled
= regulator_is_enabled_regmap
,
174 .list_voltage
= regulator_list_voltage_table
,
175 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
176 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
179 static inline unsigned int ltc3589_scale(unsigned int uV
, u32 r1
, u32 r2
)
186 tmp
= (uint64_t)uV
* r1
;
188 return uV
+ (unsigned int)tmp
;
191 static int ltc3589_of_parse_cb(struct device_node
*np
,
192 const struct regulator_desc
*desc
,
193 struct regulator_config
*config
)
195 struct ltc3589
*ltc3589
= config
->driver_data
;
196 struct regulator_desc
*rdesc
= <c3589
->regulator_descs
[desc
->id
];
200 /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
201 if (desc
->id
>= LTC3589_LDO3
)
204 ret
= of_property_read_u32_array(np
, "lltc,fb-voltage-divider", r
, 2);
206 dev_err(ltc3589
->dev
, "Failed to parse voltage divider: %d\n",
214 rdesc
->min_uV
= ltc3589_scale(desc
->min_uV
, r
[0], r
[1]);
215 rdesc
->uV_step
= ltc3589_scale(desc
->uV_step
, r
[0], r
[1]);
216 rdesc
->fixed_uV
= ltc3589_scale(desc
->fixed_uV
, r
[0], r
[1]);
221 #define LTC3589_REG(_name, _of_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit)\
222 [LTC3589_ ## _name] = { \
224 .of_match = of_match_ptr(#_of_name), \
225 .regulators_node = of_match_ptr("regulators"), \
226 .of_parse_cb = ltc3589_of_parse_cb, \
227 .n_voltages = (dtv_mask) + 1, \
228 .min_uV = (go_bit) ? 362500 : 0, \
229 .uV_step = (go_bit) ? 12500 : 0, \
230 .ramp_delay = (go_bit) ? 1750 : 0, \
231 .fixed_uV = (dtv_mask) ? 0 : 800000, \
232 .ops = <c3589_ ## _ops ## _regulator_ops, \
233 .type = REGULATOR_VOLTAGE, \
234 .id = LTC3589_ ## _name, \
235 .owner = THIS_MODULE, \
236 .vsel_reg = (dtv1_reg), \
237 .vsel_mask = (dtv_mask), \
238 .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
239 .apply_bit = (go_bit), \
240 .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
241 .enable_mask = (en_bit), \
244 #define LTC3589_LINEAR_REG(_name, _of_name, _dtv1) \
245 LTC3589_REG(_name, _of_name, linear, LTC3589_OVEN_ ## _name, \
246 LTC3589_ ## _dtv1, 0x1f, \
247 LTC3589_VCCR_ ## _name ## _GO)
249 #define LTC3589_FIXED_REG(_name, _of_name) \
250 LTC3589_REG(_name, _of_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
252 static const struct regulator_desc ltc3589_regulators
[] = {
253 LTC3589_LINEAR_REG(SW1
, sw1
, B1DTV1
),
254 LTC3589_LINEAR_REG(SW2
, sw2
, B2DTV1
),
255 LTC3589_LINEAR_REG(SW3
, sw3
, B3DTV1
),
256 LTC3589_FIXED_REG(BB_OUT
, bb
-out
),
257 LTC3589_REG(LDO1
, ldo1
, fixed_standby
, 0, 0, 0, 0),
258 LTC3589_LINEAR_REG(LDO2
, ldo2
, L2DTV1
),
259 LTC3589_FIXED_REG(LDO3
, ldo3
),
260 LTC3589_REG(LDO4
, ldo4
, table
, LTC3589_OVEN_LDO4
, LTC3589_L2DTV2
,
264 static bool ltc3589_writeable_reg(struct device
*dev
, unsigned int reg
)
267 case LTC3589_IRQSTAT
:
287 static bool ltc3589_readable_reg(struct device
*dev
, unsigned int reg
)
290 case LTC3589_IRQSTAT
:
310 static bool ltc3589_volatile_reg(struct device
*dev
, unsigned int reg
)
313 case LTC3589_IRQSTAT
:
321 static const struct reg_default ltc3589_reg_defaults
[] = {
322 { LTC3589_SCR1
, 0x00 },
323 { LTC3589_OVEN
, 0x00 },
324 { LTC3589_SCR2
, 0x00 },
325 { LTC3589_VCCR
, 0x00 },
326 { LTC3589_B1DTV1
, 0x19 },
327 { LTC3589_B1DTV2
, 0x19 },
328 { LTC3589_VRRCR
, 0xff },
329 { LTC3589_B2DTV1
, 0x19 },
330 { LTC3589_B2DTV2
, 0x19 },
331 { LTC3589_B3DTV1
, 0x19 },
332 { LTC3589_B3DTV2
, 0x19 },
333 { LTC3589_L2DTV1
, 0x19 },
334 { LTC3589_L2DTV2
, 0x19 },
337 static const struct regmap_config ltc3589_regmap_config
= {
340 .writeable_reg
= ltc3589_writeable_reg
,
341 .readable_reg
= ltc3589_readable_reg
,
342 .volatile_reg
= ltc3589_volatile_reg
,
343 .max_register
= LTC3589_L2DTV2
,
344 .reg_defaults
= ltc3589_reg_defaults
,
345 .num_reg_defaults
= ARRAY_SIZE(ltc3589_reg_defaults
),
346 .use_single_read
= true,
347 .use_single_write
= true,
348 .cache_type
= REGCACHE_RBTREE
,
351 static irqreturn_t
ltc3589_isr(int irq
, void *dev_id
)
353 struct ltc3589
*ltc3589
= dev_id
;
354 unsigned int i
, irqstat
, event
;
356 regmap_read(ltc3589
->regmap
, LTC3589_IRQSTAT
, &irqstat
);
358 if (irqstat
& LTC3589_IRQSTAT_THERMAL_WARN
) {
359 event
= REGULATOR_EVENT_OVER_TEMP
;
360 for (i
= 0; i
< LTC3589_NUM_REGULATORS
; i
++)
361 regulator_notifier_call_chain(ltc3589
->regulators
[i
],
365 if (irqstat
& LTC3589_IRQSTAT_UNDERVOLT_WARN
) {
366 event
= REGULATOR_EVENT_UNDER_VOLTAGE
;
367 for (i
= 0; i
< LTC3589_NUM_REGULATORS
; i
++)
368 regulator_notifier_call_chain(ltc3589
->regulators
[i
],
372 /* Clear warning condition */
373 regmap_write(ltc3589
->regmap
, LTC3589_CLIRQ
, 0);
378 static int ltc3589_probe(struct i2c_client
*client
,
379 const struct i2c_device_id
*id
)
381 struct device
*dev
= &client
->dev
;
382 struct regulator_desc
*descs
;
383 struct ltc3589
*ltc3589
;
386 ltc3589
= devm_kzalloc(dev
, sizeof(*ltc3589
), GFP_KERNEL
);
390 i2c_set_clientdata(client
, ltc3589
);
391 if (client
->dev
.of_node
)
392 ltc3589
->variant
= (enum ltc3589_variant
)
393 of_device_get_match_data(&client
->dev
);
395 ltc3589
->variant
= id
->driver_data
;
398 descs
= ltc3589
->regulator_descs
;
399 memcpy(descs
, ltc3589_regulators
, sizeof(ltc3589_regulators
));
400 if (ltc3589
->variant
== LTC3589
) {
401 descs
[LTC3589_LDO3
].fixed_uV
= 1800000;
402 descs
[LTC3589_LDO4
].volt_table
= ltc3589_ldo4
;
404 descs
[LTC3589_LDO3
].fixed_uV
= 2800000;
405 descs
[LTC3589_LDO4
].volt_table
= ltc3589_12_ldo4
;
408 ltc3589
->regmap
= devm_regmap_init_i2c(client
, <c3589_regmap_config
);
409 if (IS_ERR(ltc3589
->regmap
)) {
410 ret
= PTR_ERR(ltc3589
->regmap
);
411 dev_err(dev
, "failed to initialize regmap: %d\n", ret
);
415 for (i
= 0; i
< LTC3589_NUM_REGULATORS
; i
++) {
416 struct regulator_desc
*desc
= <c3589
->regulator_descs
[i
];
417 struct regulator_config config
= { };
420 config
.driver_data
= ltc3589
;
422 ltc3589
->regulators
[i
] = devm_regulator_register(dev
, desc
,
424 if (IS_ERR(ltc3589
->regulators
[i
])) {
425 ret
= PTR_ERR(ltc3589
->regulators
[i
]);
426 dev_err(dev
, "failed to register regulator %s: %d\n",
433 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
435 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
436 client
->name
, ltc3589
);
438 dev_err(dev
, "Failed to request IRQ: %d\n", ret
);
446 static const struct i2c_device_id ltc3589_i2c_id
[] = {
447 { "ltc3589", LTC3589
},
448 { "ltc3589-1", LTC3589_1
},
449 { "ltc3589-2", LTC3589_2
},
452 MODULE_DEVICE_TABLE(i2c
, ltc3589_i2c_id
);
454 static const struct of_device_id __maybe_unused ltc3589_of_match
[] = {
456 .compatible
= "lltc,ltc3589",
457 .data
= (void *)LTC3589
,
460 .compatible
= "lltc,ltc3589-1",
461 .data
= (void *)LTC3589_1
,
464 .compatible
= "lltc,ltc3589-2",
465 .data
= (void *)LTC3589_2
,
469 MODULE_DEVICE_TABLE(of
, ltc3589_of_match
);
471 static struct i2c_driver ltc3589_driver
= {
474 .of_match_table
= of_match_ptr(ltc3589_of_match
),
476 .probe
= ltc3589_probe
,
477 .id_table
= ltc3589_i2c_id
,
479 module_i2c_driver(ltc3589_driver
);
481 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
482 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3589(-1,2)");
483 MODULE_LICENSE("GPL v2");