Merge branch 'drm-fixes-4.19' of git://people.freedesktop.org/~agd5f/linux into drm...
[linux-2.6-microblaze.git] / drivers / regulator / ltc3676.c
1 /*
2  * Copyright (C) 2016 Gateworks Corporation, Inc. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/of.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
24
25 #define DRIVER_NAME             "ltc3676"
26
27 /* LTC3676 Registers */
28 #define LTC3676_BUCK1     0x01
29 #define LTC3676_BUCK2     0x02
30 #define LTC3676_BUCK3     0x03
31 #define LTC3676_BUCK4     0x04
32 #define LTC3676_LDOA      0x05
33 #define LTC3676_LDOB      0x06
34 #define LTC3676_SQD1      0x07
35 #define LTC3676_SQD2      0x08
36 #define LTC3676_CNTRL     0x09
37 #define LTC3676_DVB1A     0x0A
38 #define LTC3676_DVB1B     0x0B
39 #define LTC3676_DVB2A     0x0C
40 #define LTC3676_DVB2B     0x0D
41 #define LTC3676_DVB3A     0x0E
42 #define LTC3676_DVB3B     0x0F
43 #define LTC3676_DVB4A     0x10
44 #define LTC3676_DVB4B     0x11
45 #define LTC3676_MSKIRQ    0x12
46 #define LTC3676_MSKPG     0x13
47 #define LTC3676_USER      0x14
48 #define LTC3676_IRQSTAT   0x15
49 #define LTC3676_PGSTATL   0x16
50 #define LTC3676_PGSTATRT  0x17
51 #define LTC3676_HRST      0x1E
52 #define LTC3676_CLIRQ     0x1F
53
54 #define LTC3676_DVBxA_REF_SELECT        BIT(5)
55 #define LTC3676_DVBxB_PGOOD_MASK        BIT(5)
56
57 #define LTC3676_IRQSTAT_PGOOD_TIMEOUT   BIT(3)
58 #define LTC3676_IRQSTAT_UNDERVOLT_WARN  BIT(4)
59 #define LTC3676_IRQSTAT_UNDERVOLT_FAULT BIT(5)
60 #define LTC3676_IRQSTAT_THERMAL_WARN    BIT(6)
61 #define LTC3676_IRQSTAT_THERMAL_FAULT   BIT(7)
62
63 enum ltc3676_reg {
64         LTC3676_SW1,
65         LTC3676_SW2,
66         LTC3676_SW3,
67         LTC3676_SW4,
68         LTC3676_LDO1,
69         LTC3676_LDO2,
70         LTC3676_LDO3,
71         LTC3676_LDO4,
72         LTC3676_NUM_REGULATORS,
73 };
74
75 struct ltc3676 {
76         struct regmap *regmap;
77         struct device *dev;
78         struct regulator_desc regulator_descs[LTC3676_NUM_REGULATORS];
79         struct regulator_dev *regulators[LTC3676_NUM_REGULATORS];
80 };
81
82 static int ltc3676_set_suspend_voltage(struct regulator_dev *rdev, int uV)
83 {
84         struct ltc3676 *ltc3676 = rdev_get_drvdata(rdev);
85         struct device *dev = ltc3676->dev;
86         int dcdc = rdev_get_id(rdev);
87         int sel;
88
89         dev_dbg(dev, "%s id=%d uV=%d\n", __func__, dcdc, uV);
90         sel = regulator_map_voltage_linear(rdev, uV, uV);
91         if (sel < 0)
92                 return sel;
93
94         /* DVBB register follows right after the corresponding DVBA register */
95         return regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg + 1,
96                                   rdev->desc->vsel_mask, sel);
97 }
98
99 static int ltc3676_set_suspend_mode(struct regulator_dev *rdev,
100                                     unsigned int mode)
101 {
102         struct ltc3676 *ltc3676= rdev_get_drvdata(rdev);
103         struct device *dev = ltc3676->dev;
104         int mask, val;
105         int dcdc = rdev_get_id(rdev);
106
107         dev_dbg(dev, "%s id=%d mode=%d\n", __func__, dcdc, mode);
108
109         mask = LTC3676_DVBxA_REF_SELECT;
110         switch (mode) {
111         case REGULATOR_MODE_STANDBY:
112                 val = 0; /* select DVBxA */
113                 break;
114         case REGULATOR_MODE_NORMAL:
115                 val = LTC3676_DVBxA_REF_SELECT; /* select DVBxB */
116                 break;
117         default:
118                 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
119                          rdev->desc->name, mode);
120                 return -EINVAL;
121         }
122
123         return regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg,
124                                   mask, val);
125 }
126
127 static int ltc3676_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
128 {
129         struct ltc3676 *ltc3676 = rdev_get_drvdata(rdev);
130         struct device *dev = ltc3676->dev;
131         int ret, dcdc = rdev_get_id(rdev);
132
133         dev_dbg(dev, "%s id=%d selector=%d\n", __func__, dcdc, selector);
134
135         ret = regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg + 1,
136                                  LTC3676_DVBxB_PGOOD_MASK,
137                                  LTC3676_DVBxB_PGOOD_MASK);
138         if (ret)
139                 return ret;
140
141         return regulator_set_voltage_sel_regmap(rdev, selector);
142 }
143
144 static inline unsigned int ltc3676_scale(unsigned int uV, u32 r1, u32 r2)
145 {
146         uint64_t tmp;
147         if (uV == 0)
148                 return 0;
149         tmp = (uint64_t)uV * r1;
150         do_div(tmp, r2);
151         return uV + (unsigned int)tmp;
152 }
153
154 static int ltc3676_of_parse_cb(struct device_node *np,
155                                const struct regulator_desc *desc,
156                                struct regulator_config *config)
157 {
158         struct ltc3676 *ltc3676 = config->driver_data;
159         struct regulator_desc *rdesc = &ltc3676->regulator_descs[desc->id];
160         u32 r[2];
161         int ret;
162
163         /* LDO3 has a fixed output */
164         if (desc->id == LTC3676_LDO3)
165                 return 0;
166
167         ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider", r, 2);
168         if (ret) {
169                 dev_err(ltc3676->dev, "Failed to parse voltage divider: %d\n",
170                         ret);
171                 return ret;
172         }
173
174         rdesc->min_uV = ltc3676_scale(desc->min_uV, r[0], r[1]);
175         rdesc->uV_step = ltc3676_scale(desc->uV_step, r[0], r[1]);
176         rdesc->fixed_uV = ltc3676_scale(desc->fixed_uV, r[0], r[1]);
177
178         return 0;
179 }
180
181 /* SW1, SW2, SW3, SW4 linear 0.8V-3.3V with scalar via R1/R2 feeback res */
182 static const struct regulator_ops ltc3676_linear_regulator_ops = {
183         .enable = regulator_enable_regmap,
184         .disable = regulator_disable_regmap,
185         .is_enabled = regulator_is_enabled_regmap,
186         .list_voltage = regulator_list_voltage_linear,
187         .set_voltage_sel = ltc3676_set_voltage_sel,
188         .get_voltage_sel = regulator_get_voltage_sel_regmap,
189         .set_suspend_voltage = ltc3676_set_suspend_voltage,
190         .set_suspend_mode = ltc3676_set_suspend_mode,
191 };
192
193 /* LDO1 always on fixed 0.8V-3.3V via scalar via R1/R2 feeback res */
194 static const struct regulator_ops ltc3676_fixed_standby_regulator_ops = {
195 };
196
197 /* LDO2, LDO3 fixed (LDO2 has external scalar via R1/R2 feedback res) */
198 static const struct regulator_ops ltc3676_fixed_regulator_ops = {
199         .enable = regulator_enable_regmap,
200         .disable = regulator_disable_regmap,
201         .is_enabled = regulator_is_enabled_regmap,
202 };
203
204 #define LTC3676_REG(_id, _name, _ops, en_reg, en_bit, dvba_reg, dvb_mask)   \
205         [LTC3676_ ## _id] = {                                        \
206                 .name = #_name,                                \
207                 .of_match = of_match_ptr(#_name),              \
208                 .regulators_node = of_match_ptr("regulators"), \
209                 .of_parse_cb = ltc3676_of_parse_cb,            \
210                 .n_voltages = (dvb_mask) + 1,                  \
211                 .min_uV = (dvba_reg) ? 412500 : 0,             \
212                 .uV_step = (dvba_reg) ? 12500 : 0,             \
213                 .ramp_delay = (dvba_reg) ? 800 : 0,            \
214                 .fixed_uV = (dvb_mask) ? 0 : 725000,           \
215                 .ops = &ltc3676_ ## _ops ## _regulator_ops,    \
216                 .type = REGULATOR_VOLTAGE,                     \
217                 .id = LTC3676_ ## _id,                         \
218                 .owner = THIS_MODULE,                          \
219                 .vsel_reg = (dvba_reg),                        \
220                 .vsel_mask = (dvb_mask),                       \
221                 .enable_reg = (en_reg),                        \
222                 .enable_mask = (1 << en_bit),                  \
223         }
224
225 #define LTC3676_LINEAR_REG(_id, _name, _en, _dvba)                     \
226         LTC3676_REG(_id, _name, linear,                                \
227                     LTC3676_ ## _en, 7,                                \
228                     LTC3676_ ## _dvba, 0x1f)
229
230 #define LTC3676_FIXED_REG(_id, _name, _en_reg, _en_bit)                \
231         LTC3676_REG(_id, _name, fixed, LTC3676_ ## _en_reg, _en_bit, 0, 0)
232
233 static struct regulator_desc ltc3676_regulators[LTC3676_NUM_REGULATORS] = {
234         LTC3676_LINEAR_REG(SW1, sw1, BUCK1, DVB1A),
235         LTC3676_LINEAR_REG(SW2, sw2, BUCK2, DVB2A),
236         LTC3676_LINEAR_REG(SW3, sw3, BUCK3, DVB3A),
237         LTC3676_LINEAR_REG(SW4, sw4, BUCK4, DVB4A),
238         LTC3676_REG(LDO1, ldo1, fixed_standby, 0, 0, 0, 0),
239         LTC3676_FIXED_REG(LDO2, ldo2, LDOA, 2),
240         LTC3676_FIXED_REG(LDO3, ldo3, LDOA, 5),
241         LTC3676_FIXED_REG(LDO4, ldo4, LDOB, 2),
242 };
243
244 static bool ltc3676_writeable_reg(struct device *dev, unsigned int reg)
245 {
246         switch (reg) {
247         case LTC3676_IRQSTAT:
248         case LTC3676_BUCK1:
249         case LTC3676_BUCK2:
250         case LTC3676_BUCK3:
251         case LTC3676_BUCK4:
252         case LTC3676_LDOA:
253         case LTC3676_LDOB:
254         case LTC3676_SQD1:
255         case LTC3676_SQD2:
256         case LTC3676_CNTRL:
257         case LTC3676_DVB1A:
258         case LTC3676_DVB1B:
259         case LTC3676_DVB2A:
260         case LTC3676_DVB2B:
261         case LTC3676_DVB3A:
262         case LTC3676_DVB3B:
263         case LTC3676_DVB4A:
264         case LTC3676_DVB4B:
265         case LTC3676_MSKIRQ:
266         case LTC3676_MSKPG:
267         case LTC3676_USER:
268         case LTC3676_HRST:
269         case LTC3676_CLIRQ:
270                 return true;
271         }
272         return false;
273 }
274
275 static bool ltc3676_readable_reg(struct device *dev, unsigned int reg)
276 {
277         switch (reg) {
278         case LTC3676_IRQSTAT:
279         case LTC3676_BUCK1:
280         case LTC3676_BUCK2:
281         case LTC3676_BUCK3:
282         case LTC3676_BUCK4:
283         case LTC3676_LDOA:
284         case LTC3676_LDOB:
285         case LTC3676_SQD1:
286         case LTC3676_SQD2:
287         case LTC3676_CNTRL:
288         case LTC3676_DVB1A:
289         case LTC3676_DVB1B:
290         case LTC3676_DVB2A:
291         case LTC3676_DVB2B:
292         case LTC3676_DVB3A:
293         case LTC3676_DVB3B:
294         case LTC3676_DVB4A:
295         case LTC3676_DVB4B:
296         case LTC3676_MSKIRQ:
297         case LTC3676_MSKPG:
298         case LTC3676_USER:
299         case LTC3676_HRST:
300         case LTC3676_CLIRQ:
301                 return true;
302         }
303         return false;
304 }
305
306 static bool ltc3676_volatile_reg(struct device *dev, unsigned int reg)
307 {
308         switch (reg) {
309         case LTC3676_IRQSTAT:
310         case LTC3676_PGSTATL:
311         case LTC3676_PGSTATRT:
312                 return true;
313         }
314         return false;
315 }
316
317 static const struct regmap_config ltc3676_regmap_config = {
318         .reg_bits = 8,
319         .val_bits = 8,
320         .writeable_reg = ltc3676_writeable_reg,
321         .readable_reg = ltc3676_readable_reg,
322         .volatile_reg = ltc3676_volatile_reg,
323         .max_register = LTC3676_CLIRQ,
324         .use_single_rw = true,
325         .cache_type = REGCACHE_RBTREE,
326 };
327
328 static irqreturn_t ltc3676_isr(int irq, void *dev_id)
329 {
330         struct ltc3676 *ltc3676 = dev_id;
331         struct device *dev = ltc3676->dev;
332         unsigned int i, irqstat, event;
333
334         regmap_read(ltc3676->regmap, LTC3676_IRQSTAT, &irqstat);
335
336         dev_dbg(dev, "irq%d irqstat=0x%02x\n", irq, irqstat);
337         if (irqstat & LTC3676_IRQSTAT_THERMAL_WARN) {
338                 dev_warn(dev, "Over-temperature Warning\n");
339                 event = REGULATOR_EVENT_OVER_TEMP;
340                 for (i = 0; i < LTC3676_NUM_REGULATORS; i++)
341                         regulator_notifier_call_chain(ltc3676->regulators[i],
342                                                       event, NULL);
343         }
344
345         if (irqstat & LTC3676_IRQSTAT_UNDERVOLT_WARN) {
346                 dev_info(dev, "Undervoltage Warning\n");
347                 event = REGULATOR_EVENT_UNDER_VOLTAGE;
348                 for (i = 0; i < LTC3676_NUM_REGULATORS; i++)
349                         regulator_notifier_call_chain(ltc3676->regulators[i],
350                                                       event, NULL);
351         }
352
353         /* Clear warning condition */
354         regmap_write(ltc3676->regmap, LTC3676_CLIRQ, 0);
355
356         return IRQ_HANDLED;
357 }
358
359 static int ltc3676_regulator_probe(struct i2c_client *client,
360                                     const struct i2c_device_id *id)
361 {
362         struct device *dev = &client->dev;
363         struct regulator_init_data *init_data = dev_get_platdata(dev);
364         struct regulator_desc *descs;
365         struct ltc3676 *ltc3676;
366         int i, ret;
367
368         ltc3676 = devm_kzalloc(dev, sizeof(*ltc3676), GFP_KERNEL);
369         if (!ltc3676)
370                 return -ENOMEM;
371
372         i2c_set_clientdata(client, ltc3676);
373         ltc3676->dev = dev;
374
375         descs = ltc3676->regulator_descs;
376         memcpy(descs, ltc3676_regulators, sizeof(ltc3676_regulators));
377         descs[LTC3676_LDO3].fixed_uV = 1800000; /* LDO3 is fixed 1.8V */
378
379         ltc3676->regmap = devm_regmap_init_i2c(client, &ltc3676_regmap_config);
380         if (IS_ERR(ltc3676->regmap)) {
381                 ret = PTR_ERR(ltc3676->regmap);
382                 dev_err(dev, "failed to initialize regmap: %d\n", ret);
383                 return ret;
384         }
385
386         for (i = 0; i < LTC3676_NUM_REGULATORS; i++) {
387                 struct regulator_desc *desc = &ltc3676->regulator_descs[i];
388                 struct regulator_config config = { };
389
390                 if (init_data)
391                         config.init_data = &init_data[i];
392
393                 config.dev = dev;
394                 config.driver_data = ltc3676;
395
396                 ltc3676->regulators[i] = devm_regulator_register(dev, desc,
397                                                                  &config);
398                 if (IS_ERR(ltc3676->regulators[i])) {
399                         ret = PTR_ERR(ltc3676->regulators[i]);
400                         dev_err(dev, "failed to register regulator %s: %d\n",
401                                 desc->name, ret);
402                         return ret;
403                 }
404         }
405
406         regmap_write(ltc3676->regmap, LTC3676_CLIRQ, 0);
407         if (client->irq) {
408                 ret = devm_request_threaded_irq(dev, client->irq, NULL,
409                                                 ltc3676_isr,
410                                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
411                                                 client->name, ltc3676);
412                 if (ret) {
413                         dev_err(dev, "Failed to request IRQ: %d\n", ret);
414                         return ret;
415                 }
416         }
417
418         return 0;
419 }
420
421 static const struct i2c_device_id ltc3676_i2c_id[] = {
422         { "ltc3676" },
423         { }
424 };
425 MODULE_DEVICE_TABLE(i2c, ltc3676_i2c_id);
426
427 static const struct of_device_id ltc3676_of_match[] = {
428         { .compatible = "lltc,ltc3676" },
429         { },
430 };
431 MODULE_DEVICE_TABLE(of, ltc3676_of_match);
432
433 static struct i2c_driver ltc3676_driver = {
434         .driver = {
435                 .name = DRIVER_NAME,
436                 .of_match_table = of_match_ptr(ltc3676_of_match),
437         },
438         .probe = ltc3676_regulator_probe,
439         .id_table = ltc3676_i2c_id,
440 };
441 module_i2c_driver(ltc3676_driver);
442
443 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
444 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC1376");
445 MODULE_LICENSE("GPL v2");