1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Gateworks Corporation, Inc. All Rights Reserved.
6 #include <linux/init.h>
7 #include <linux/interrupt.h>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
14 #include <linux/regulator/of_regulator.h>
16 #define DRIVER_NAME "ltc3676"
18 /* LTC3676 Registers */
19 #define LTC3676_BUCK1 0x01
20 #define LTC3676_BUCK2 0x02
21 #define LTC3676_BUCK3 0x03
22 #define LTC3676_BUCK4 0x04
23 #define LTC3676_LDOA 0x05
24 #define LTC3676_LDOB 0x06
25 #define LTC3676_SQD1 0x07
26 #define LTC3676_SQD2 0x08
27 #define LTC3676_CNTRL 0x09
28 #define LTC3676_DVB1A 0x0A
29 #define LTC3676_DVB1B 0x0B
30 #define LTC3676_DVB2A 0x0C
31 #define LTC3676_DVB2B 0x0D
32 #define LTC3676_DVB3A 0x0E
33 #define LTC3676_DVB3B 0x0F
34 #define LTC3676_DVB4A 0x10
35 #define LTC3676_DVB4B 0x11
36 #define LTC3676_MSKIRQ 0x12
37 #define LTC3676_MSKPG 0x13
38 #define LTC3676_USER 0x14
39 #define LTC3676_IRQSTAT 0x15
40 #define LTC3676_PGSTATL 0x16
41 #define LTC3676_PGSTATRT 0x17
42 #define LTC3676_HRST 0x1E
43 #define LTC3676_CLIRQ 0x1F
45 #define LTC3676_DVBxA_REF_SELECT BIT(5)
46 #define LTC3676_DVBxB_PGOOD_MASK BIT(5)
48 #define LTC3676_IRQSTAT_PGOOD_TIMEOUT BIT(3)
49 #define LTC3676_IRQSTAT_UNDERVOLT_WARN BIT(4)
50 #define LTC3676_IRQSTAT_UNDERVOLT_FAULT BIT(5)
51 #define LTC3676_IRQSTAT_THERMAL_WARN BIT(6)
52 #define LTC3676_IRQSTAT_THERMAL_FAULT BIT(7)
63 LTC3676_NUM_REGULATORS
,
67 struct regmap
*regmap
;
69 struct regulator_desc regulator_descs
[LTC3676_NUM_REGULATORS
];
70 struct regulator_dev
*regulators
[LTC3676_NUM_REGULATORS
];
73 static int ltc3676_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
75 struct ltc3676
*ltc3676
= rdev_get_drvdata(rdev
);
76 struct device
*dev
= ltc3676
->dev
;
77 int dcdc
= rdev_get_id(rdev
);
80 dev_dbg(dev
, "%s id=%d uV=%d\n", __func__
, dcdc
, uV
);
81 sel
= regulator_map_voltage_linear(rdev
, uV
, uV
);
85 /* DVBB register follows right after the corresponding DVBA register */
86 return regmap_update_bits(ltc3676
->regmap
, rdev
->desc
->vsel_reg
+ 1,
87 rdev
->desc
->vsel_mask
, sel
);
90 static int ltc3676_set_suspend_mode(struct regulator_dev
*rdev
,
93 struct ltc3676
*ltc3676
= rdev_get_drvdata(rdev
);
94 struct device
*dev
= ltc3676
->dev
;
96 int dcdc
= rdev_get_id(rdev
);
98 dev_dbg(dev
, "%s id=%d mode=%d\n", __func__
, dcdc
, mode
);
100 mask
= LTC3676_DVBxA_REF_SELECT
;
102 case REGULATOR_MODE_STANDBY
:
103 val
= 0; /* select DVBxA */
105 case REGULATOR_MODE_NORMAL
:
106 val
= LTC3676_DVBxA_REF_SELECT
; /* select DVBxB */
109 dev_warn(&rdev
->dev
, "%s: regulator mode: 0x%x not supported\n",
110 rdev
->desc
->name
, mode
);
114 return regmap_update_bits(ltc3676
->regmap
, rdev
->desc
->vsel_reg
,
118 static int ltc3676_set_voltage_sel(struct regulator_dev
*rdev
, unsigned selector
)
120 struct ltc3676
*ltc3676
= rdev_get_drvdata(rdev
);
121 struct device
*dev
= ltc3676
->dev
;
122 int ret
, dcdc
= rdev_get_id(rdev
);
124 dev_dbg(dev
, "%s id=%d selector=%d\n", __func__
, dcdc
, selector
);
126 ret
= regmap_update_bits(ltc3676
->regmap
, rdev
->desc
->vsel_reg
+ 1,
127 LTC3676_DVBxB_PGOOD_MASK
,
128 LTC3676_DVBxB_PGOOD_MASK
);
132 return regulator_set_voltage_sel_regmap(rdev
, selector
);
135 static inline unsigned int ltc3676_scale(unsigned int uV
, u32 r1
, u32 r2
)
140 tmp
= (uint64_t)uV
* r1
;
142 return uV
+ (unsigned int)tmp
;
145 static int ltc3676_of_parse_cb(struct device_node
*np
,
146 const struct regulator_desc
*desc
,
147 struct regulator_config
*config
)
149 struct ltc3676
*ltc3676
= config
->driver_data
;
150 struct regulator_desc
*rdesc
= <c3676
->regulator_descs
[desc
->id
];
154 /* LDO3 has a fixed output */
155 if (desc
->id
== LTC3676_LDO3
)
158 ret
= of_property_read_u32_array(np
, "lltc,fb-voltage-divider", r
, 2);
160 dev_err(ltc3676
->dev
, "Failed to parse voltage divider: %d\n",
165 rdesc
->min_uV
= ltc3676_scale(desc
->min_uV
, r
[0], r
[1]);
166 rdesc
->uV_step
= ltc3676_scale(desc
->uV_step
, r
[0], r
[1]);
167 rdesc
->fixed_uV
= ltc3676_scale(desc
->fixed_uV
, r
[0], r
[1]);
172 /* SW1, SW2, SW3, SW4 linear 0.8V-3.3V with scalar via R1/R2 feeback res */
173 static const struct regulator_ops ltc3676_linear_regulator_ops
= {
174 .enable
= regulator_enable_regmap
,
175 .disable
= regulator_disable_regmap
,
176 .is_enabled
= regulator_is_enabled_regmap
,
177 .list_voltage
= regulator_list_voltage_linear
,
178 .set_voltage_sel
= ltc3676_set_voltage_sel
,
179 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
180 .set_suspend_voltage
= ltc3676_set_suspend_voltage
,
181 .set_suspend_mode
= ltc3676_set_suspend_mode
,
184 /* LDO1 always on fixed 0.8V-3.3V via scalar via R1/R2 feeback res */
185 static const struct regulator_ops ltc3676_fixed_standby_regulator_ops
= {
188 /* LDO2, LDO3 fixed (LDO2 has external scalar via R1/R2 feedback res) */
189 static const struct regulator_ops ltc3676_fixed_regulator_ops
= {
190 .enable
= regulator_enable_regmap
,
191 .disable
= regulator_disable_regmap
,
192 .is_enabled
= regulator_is_enabled_regmap
,
195 #define LTC3676_REG(_id, _name, _ops, en_reg, en_bit, dvba_reg, dvb_mask) \
196 [LTC3676_ ## _id] = { \
198 .of_match = of_match_ptr(#_name), \
199 .regulators_node = of_match_ptr("regulators"), \
200 .of_parse_cb = ltc3676_of_parse_cb, \
201 .n_voltages = (dvb_mask) + 1, \
202 .min_uV = (dvba_reg) ? 412500 : 0, \
203 .uV_step = (dvba_reg) ? 12500 : 0, \
204 .ramp_delay = (dvba_reg) ? 800 : 0, \
205 .fixed_uV = (dvb_mask) ? 0 : 725000, \
206 .ops = <c3676_ ## _ops ## _regulator_ops, \
207 .type = REGULATOR_VOLTAGE, \
208 .id = LTC3676_ ## _id, \
209 .owner = THIS_MODULE, \
210 .vsel_reg = (dvba_reg), \
211 .vsel_mask = (dvb_mask), \
212 .enable_reg = (en_reg), \
213 .enable_mask = (1 << en_bit), \
216 #define LTC3676_LINEAR_REG(_id, _name, _en, _dvba) \
217 LTC3676_REG(_id, _name, linear, \
218 LTC3676_ ## _en, 7, \
219 LTC3676_ ## _dvba, 0x1f)
221 #define LTC3676_FIXED_REG(_id, _name, _en_reg, _en_bit) \
222 LTC3676_REG(_id, _name, fixed, LTC3676_ ## _en_reg, _en_bit, 0, 0)
224 static const struct regulator_desc ltc3676_regulators
[LTC3676_NUM_REGULATORS
] = {
225 LTC3676_LINEAR_REG(SW1
, sw1
, BUCK1
, DVB1A
),
226 LTC3676_LINEAR_REG(SW2
, sw2
, BUCK2
, DVB2A
),
227 LTC3676_LINEAR_REG(SW3
, sw3
, BUCK3
, DVB3A
),
228 LTC3676_LINEAR_REG(SW4
, sw4
, BUCK4
, DVB4A
),
229 LTC3676_REG(LDO1
, ldo1
, fixed_standby
, 0, 0, 0, 0),
230 LTC3676_FIXED_REG(LDO2
, ldo2
, LDOA
, 2),
231 LTC3676_FIXED_REG(LDO3
, ldo3
, LDOA
, 5),
232 LTC3676_FIXED_REG(LDO4
, ldo4
, LDOB
, 2),
235 static bool ltc3676_readable_writeable_reg(struct device
*dev
, unsigned int reg
)
238 case LTC3676_BUCK1
... LTC3676_IRQSTAT
:
246 static bool ltc3676_volatile_reg(struct device
*dev
, unsigned int reg
)
249 case LTC3676_IRQSTAT
... LTC3676_PGSTATRT
:
255 static const struct regmap_config ltc3676_regmap_config
= {
258 .writeable_reg
= ltc3676_readable_writeable_reg
,
259 .readable_reg
= ltc3676_readable_writeable_reg
,
260 .volatile_reg
= ltc3676_volatile_reg
,
261 .max_register
= LTC3676_CLIRQ
,
262 .use_single_read
= true,
263 .use_single_write
= true,
264 .cache_type
= REGCACHE_RBTREE
,
267 static irqreturn_t
ltc3676_isr(int irq
, void *dev_id
)
269 struct ltc3676
*ltc3676
= dev_id
;
270 struct device
*dev
= ltc3676
->dev
;
271 unsigned int i
, irqstat
, event
;
273 regmap_read(ltc3676
->regmap
, LTC3676_IRQSTAT
, &irqstat
);
275 dev_dbg(dev
, "irq%d irqstat=0x%02x\n", irq
, irqstat
);
276 if (irqstat
& LTC3676_IRQSTAT_THERMAL_WARN
) {
277 dev_warn(dev
, "Over-temperature Warning\n");
278 event
= REGULATOR_EVENT_OVER_TEMP
;
279 for (i
= 0; i
< LTC3676_NUM_REGULATORS
; i
++)
280 regulator_notifier_call_chain(ltc3676
->regulators
[i
],
284 if (irqstat
& LTC3676_IRQSTAT_UNDERVOLT_WARN
) {
285 dev_info(dev
, "Undervoltage Warning\n");
286 event
= REGULATOR_EVENT_UNDER_VOLTAGE
;
287 for (i
= 0; i
< LTC3676_NUM_REGULATORS
; i
++)
288 regulator_notifier_call_chain(ltc3676
->regulators
[i
],
292 /* Clear warning condition */
293 regmap_write(ltc3676
->regmap
, LTC3676_CLIRQ
, 0);
298 static int ltc3676_regulator_probe(struct i2c_client
*client
)
300 struct device
*dev
= &client
->dev
;
301 struct regulator_init_data
*init_data
= dev_get_platdata(dev
);
302 struct regulator_desc
*descs
;
303 struct ltc3676
*ltc3676
;
306 ltc3676
= devm_kzalloc(dev
, sizeof(*ltc3676
), GFP_KERNEL
);
310 i2c_set_clientdata(client
, ltc3676
);
313 descs
= ltc3676
->regulator_descs
;
314 memcpy(descs
, ltc3676_regulators
, sizeof(ltc3676_regulators
));
315 descs
[LTC3676_LDO3
].fixed_uV
= 1800000; /* LDO3 is fixed 1.8V */
317 ltc3676
->regmap
= devm_regmap_init_i2c(client
, <c3676_regmap_config
);
318 if (IS_ERR(ltc3676
->regmap
)) {
319 ret
= PTR_ERR(ltc3676
->regmap
);
320 dev_err(dev
, "failed to initialize regmap: %d\n", ret
);
324 for (i
= 0; i
< LTC3676_NUM_REGULATORS
; i
++) {
325 struct regulator_desc
*desc
= <c3676
->regulator_descs
[i
];
326 struct regulator_config config
= { };
329 config
.init_data
= &init_data
[i
];
332 config
.driver_data
= ltc3676
;
334 ltc3676
->regulators
[i
] = devm_regulator_register(dev
, desc
,
336 if (IS_ERR(ltc3676
->regulators
[i
])) {
337 ret
= PTR_ERR(ltc3676
->regulators
[i
]);
338 dev_err(dev
, "failed to register regulator %s: %d\n",
344 regmap_write(ltc3676
->regmap
, LTC3676_CLIRQ
, 0);
346 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
348 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
349 client
->name
, ltc3676
);
351 dev_err(dev
, "Failed to request IRQ: %d\n", ret
);
359 static const struct i2c_device_id ltc3676_i2c_id
[] = {
363 MODULE_DEVICE_TABLE(i2c
, ltc3676_i2c_id
);
365 static const struct of_device_id __maybe_unused ltc3676_of_match
[] = {
366 { .compatible
= "lltc,ltc3676" },
369 MODULE_DEVICE_TABLE(of
, ltc3676_of_match
);
371 static struct i2c_driver ltc3676_driver
= {
374 .of_match_table
= of_match_ptr(ltc3676_of_match
),
376 .probe_new
= ltc3676_regulator_probe
,
377 .id_table
= ltc3676_i2c_id
,
379 module_i2c_driver(ltc3676_driver
);
381 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
382 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3676");
383 MODULE_LICENSE("GPL v2");