Merge tag 'linux-watchdog-5.7-rc1' of git://www.linux-watchdog.org/linux-watchdog
[sfrench/cifs-2.6.git] / drivers / regulator / pv88090-regulator.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // pv88090-regulator.c - Regulator device driver for PV88090
4 // Copyright (C) 2015  Powerventure Semiconductor Ltd.
5
6 #include <linux/err.h>
7 #include <linux/i2c.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regmap.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/regulator/of_regulator.h>
17 #include "pv88090-regulator.h"
18
19 #define PV88090_MAX_REGULATORS  5
20
21 /* PV88090 REGULATOR IDs */
22 enum {
23         /* BUCKs */
24         PV88090_ID_BUCK1,
25         PV88090_ID_BUCK2,
26         PV88090_ID_BUCK3,
27
28         /* LDOs */
29         PV88090_ID_LDO1,
30         PV88090_ID_LDO2,
31 };
32
33 struct pv88090_regulator {
34         struct regulator_desc desc;
35         unsigned int conf;
36         unsigned int conf2;
37 };
38
39 struct pv88090 {
40         struct device *dev;
41         struct regmap *regmap;
42         struct regulator_dev *rdev[PV88090_MAX_REGULATORS];
43 };
44
45 struct pv88090_buck_voltage {
46         int min_uV;
47         int max_uV;
48         int uV_step;
49 };
50
51 static const struct regmap_config pv88090_regmap_config = {
52         .reg_bits = 8,
53         .val_bits = 8,
54 };
55
56 /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
57  *  Entry indexes corresponds to register values.
58  */
59
60 static const unsigned int pv88090_buck1_limits[] = {
61          220000,  440000,  660000,  880000, 1100000, 1320000, 1540000, 1760000,
62         1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
63         3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
64         5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
65 };
66
67 static const unsigned int pv88090_buck23_limits[] = {
68         1496000, 2393000, 3291000, 4189000
69 };
70
71 static const struct pv88090_buck_voltage pv88090_buck_vol[3] = {
72         {
73                 .min_uV = 600000,
74                 .max_uV = 1393750,
75                 .uV_step = 6250,
76         },
77
78         {
79                 .min_uV = 1400000,
80                 .max_uV = 2193750,
81                 .uV_step = 6250,
82         },
83         {
84                 .min_uV = 1250000,
85                 .max_uV = 2837500,
86                 .uV_step = 12500,
87         },
88 };
89
90 static unsigned int pv88090_buck_get_mode(struct regulator_dev *rdev)
91 {
92         struct pv88090_regulator *info = rdev_get_drvdata(rdev);
93         unsigned int data;
94         int ret, mode = 0;
95
96         ret = regmap_read(rdev->regmap, info->conf, &data);
97         if (ret < 0)
98                 return ret;
99
100         switch (data & PV88090_BUCK1_MODE_MASK) {
101         case PV88090_BUCK_MODE_SYNC:
102                 mode = REGULATOR_MODE_FAST;
103                 break;
104         case PV88090_BUCK_MODE_AUTO:
105                 mode = REGULATOR_MODE_NORMAL;
106                 break;
107         case PV88090_BUCK_MODE_SLEEP:
108                 mode = REGULATOR_MODE_STANDBY;
109                 break;
110         }
111
112         return mode;
113 }
114
115 static int pv88090_buck_set_mode(struct regulator_dev *rdev,
116                                         unsigned int mode)
117 {
118         struct pv88090_regulator *info = rdev_get_drvdata(rdev);
119         int val = 0;
120
121         switch (mode) {
122         case REGULATOR_MODE_FAST:
123                 val = PV88090_BUCK_MODE_SYNC;
124                 break;
125         case REGULATOR_MODE_NORMAL:
126                 val = PV88090_BUCK_MODE_AUTO;
127                 break;
128         case REGULATOR_MODE_STANDBY:
129                 val = PV88090_BUCK_MODE_SLEEP;
130                 break;
131         default:
132                 return -EINVAL;
133         }
134
135         return regmap_update_bits(rdev->regmap, info->conf,
136                                         PV88090_BUCK1_MODE_MASK, val);
137 }
138
139 static const struct regulator_ops pv88090_buck_ops = {
140         .get_mode = pv88090_buck_get_mode,
141         .set_mode = pv88090_buck_set_mode,
142         .enable = regulator_enable_regmap,
143         .disable = regulator_disable_regmap,
144         .is_enabled = regulator_is_enabled_regmap,
145         .set_voltage_sel = regulator_set_voltage_sel_regmap,
146         .get_voltage_sel = regulator_get_voltage_sel_regmap,
147         .list_voltage = regulator_list_voltage_linear,
148         .set_current_limit = regulator_set_current_limit_regmap,
149         .get_current_limit = regulator_get_current_limit_regmap,
150 };
151
152 static const struct regulator_ops pv88090_ldo_ops = {
153         .enable = regulator_enable_regmap,
154         .disable = regulator_disable_regmap,
155         .is_enabled = regulator_is_enabled_regmap,
156         .set_voltage_sel = regulator_set_voltage_sel_regmap,
157         .get_voltage_sel = regulator_get_voltage_sel_regmap,
158         .list_voltage = regulator_list_voltage_linear,
159 };
160
161 #define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
162 {\
163         .desc   =       {\
164                 .id = chip##_ID_##regl_name,\
165                 .name = __stringify(chip##_##regl_name),\
166                 .of_match = of_match_ptr(#regl_name),\
167                 .regulators_node = of_match_ptr("regulators"),\
168                 .type = REGULATOR_VOLTAGE,\
169                 .owner = THIS_MODULE,\
170                 .ops = &pv88090_buck_ops,\
171                 .min_uV = min, \
172                 .uV_step = step, \
173                 .n_voltages = ((max) - (min))/(step) + 1, \
174                 .enable_reg = PV88090_REG_##regl_name##_CONF0, \
175                 .enable_mask = PV88090_##regl_name##_EN, \
176                 .vsel_reg = PV88090_REG_##regl_name##_CONF0, \
177                 .vsel_mask = PV88090_V##regl_name##_MASK, \
178                 .curr_table = limits_array, \
179                 .n_current_limits = ARRAY_SIZE(limits_array), \
180                 .csel_reg = PV88090_REG_##regl_name##_CONF1, \
181                 .csel_mask = PV88090_##regl_name##_ILIM_MASK, \
182         },\
183         .conf = PV88090_REG_##regl_name##_CONF1, \
184         .conf2 = PV88090_REG_##regl_name##_CONF2, \
185 }
186
187 #define PV88090_LDO(chip, regl_name, min, step, max) \
188 {\
189         .desc   =       {\
190                 .id = chip##_ID_##regl_name,\
191                 .name = __stringify(chip##_##regl_name),\
192                 .of_match = of_match_ptr(#regl_name),\
193                 .regulators_node = of_match_ptr("regulators"),\
194                 .type = REGULATOR_VOLTAGE,\
195                 .owner = THIS_MODULE,\
196                 .ops = &pv88090_ldo_ops,\
197                 .min_uV = min, \
198                 .uV_step = step, \
199                 .n_voltages = ((max) - (min))/(step) + 1, \
200                 .enable_reg = PV88090_REG_##regl_name##_CONT, \
201                 .enable_mask = PV88090_##regl_name##_EN, \
202                 .vsel_reg = PV88090_REG_##regl_name##_CONT, \
203                 .vsel_mask = PV88090_V##regl_name##_MASK, \
204         },\
205 }
206
207 static struct pv88090_regulator pv88090_regulator_info[] = {
208         PV88090_BUCK(PV88090, BUCK1, 600000, 6250, 1393750,
209                 pv88090_buck1_limits),
210         PV88090_BUCK(PV88090, BUCK2, 600000, 6250, 1393750,
211                 pv88090_buck23_limits),
212         PV88090_BUCK(PV88090, BUCK3, 600000, 6250, 1393750,
213                 pv88090_buck23_limits),
214         PV88090_LDO(PV88090, LDO1, 1200000, 50000, 4350000),
215         PV88090_LDO(PV88090, LDO2,  650000, 25000, 2225000),
216 };
217
218 static irqreturn_t pv88090_irq_handler(int irq, void *data)
219 {
220         struct pv88090 *chip = data;
221         int i, reg_val, err, ret = IRQ_NONE;
222
223         err = regmap_read(chip->regmap, PV88090_REG_EVENT_A, &reg_val);
224         if (err < 0)
225                 goto error_i2c;
226
227         if (reg_val & PV88090_E_VDD_FLT) {
228                 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
229                         if (chip->rdev[i] != NULL) {
230                                 regulator_lock(chip->rdev[i]);
231                                 regulator_notifier_call_chain(chip->rdev[i],
232                                         REGULATOR_EVENT_UNDER_VOLTAGE,
233                                         NULL);
234                                 regulator_unlock(chip->rdev[i]);
235                         }
236                 }
237
238                 err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
239                         PV88090_E_VDD_FLT);
240                 if (err < 0)
241                         goto error_i2c;
242
243                 ret = IRQ_HANDLED;
244         }
245
246         if (reg_val & PV88090_E_OVER_TEMP) {
247                 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
248                         if (chip->rdev[i] != NULL) {
249                                 regulator_lock(chip->rdev[i]);
250                                 regulator_notifier_call_chain(chip->rdev[i],
251                                         REGULATOR_EVENT_OVER_TEMP,
252                                         NULL);
253                                 regulator_unlock(chip->rdev[i]);
254                         }
255                 }
256
257                 err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
258                         PV88090_E_OVER_TEMP);
259                 if (err < 0)
260                         goto error_i2c;
261
262                 ret = IRQ_HANDLED;
263         }
264
265         return ret;
266
267 error_i2c:
268         dev_err(chip->dev, "I2C error : %d\n", err);
269         return IRQ_NONE;
270 }
271
272 /*
273  * I2C driver interface functions
274  */
275 static int pv88090_i2c_probe(struct i2c_client *i2c)
276 {
277         struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
278         struct pv88090 *chip;
279         struct regulator_config config = { };
280         int error, i, ret = 0;
281         unsigned int conf2, range, index;
282
283         chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88090), GFP_KERNEL);
284         if (!chip)
285                 return -ENOMEM;
286
287         chip->dev = &i2c->dev;
288         chip->regmap = devm_regmap_init_i2c(i2c, &pv88090_regmap_config);
289         if (IS_ERR(chip->regmap)) {
290                 error = PTR_ERR(chip->regmap);
291                 dev_err(chip->dev, "Failed to allocate register map: %d\n",
292                         error);
293                 return error;
294         }
295
296         i2c_set_clientdata(i2c, chip);
297
298         if (i2c->irq != 0) {
299                 ret = regmap_write(chip->regmap, PV88090_REG_MASK_A, 0xFF);
300                 if (ret < 0) {
301                         dev_err(chip->dev,
302                                 "Failed to mask A reg: %d\n", ret);
303                         return ret;
304                 }
305
306                 ret = regmap_write(chip->regmap, PV88090_REG_MASK_B, 0xFF);
307                 if (ret < 0) {
308                         dev_err(chip->dev,
309                                 "Failed to mask B reg: %d\n", ret);
310                         return ret;
311                 }
312
313                 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
314                                         pv88090_irq_handler,
315                                         IRQF_TRIGGER_LOW|IRQF_ONESHOT,
316                                         "pv88090", chip);
317                 if (ret != 0) {
318                         dev_err(chip->dev, "Failed to request IRQ: %d\n",
319                                 i2c->irq);
320                         return ret;
321                 }
322
323                 ret = regmap_update_bits(chip->regmap, PV88090_REG_MASK_A,
324                         PV88090_M_VDD_FLT | PV88090_M_OVER_TEMP, 0);
325                 if (ret < 0) {
326                         dev_err(chip->dev,
327                                 "Failed to update mask reg: %d\n", ret);
328                         return ret;
329                 }
330
331         } else {
332                 dev_warn(chip->dev, "No IRQ configured\n");
333         }
334
335         config.dev = chip->dev;
336         config.regmap = chip->regmap;
337
338         for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
339                 if (init_data)
340                         config.init_data = &init_data[i];
341
342                 if (i == PV88090_ID_BUCK2 || i == PV88090_ID_BUCK3) {
343                         ret = regmap_read(chip->regmap,
344                                 pv88090_regulator_info[i].conf2, &conf2);
345                         if (ret < 0)
346                                 return ret;
347
348                         conf2 = (conf2 >> PV88090_BUCK_VDAC_RANGE_SHIFT) &
349                                 PV88090_BUCK_VDAC_RANGE_MASK;
350
351                         ret = regmap_read(chip->regmap,
352                                 PV88090_REG_BUCK_FOLD_RANGE, &range);
353                         if (ret < 0)
354                                 return ret;
355
356                         range = (range >>
357                                  (PV88090_BUCK_VRANGE_GAIN_SHIFT + i - 1)) &
358                                 PV88090_BUCK_VRANGE_GAIN_MASK;
359                         index = ((range << 1) | conf2);
360                         if (index > PV88090_ID_BUCK3) {
361                                 dev_err(chip->dev,
362                                         "Invalid index(%d)\n", index);
363                                 return -EINVAL;
364                         }
365
366                         pv88090_regulator_info[i].desc.min_uV
367                                 = pv88090_buck_vol[index].min_uV;
368                         pv88090_regulator_info[i].desc.uV_step
369                                 = pv88090_buck_vol[index].uV_step;
370                         pv88090_regulator_info[i].desc.n_voltages
371                                 = ((pv88090_buck_vol[index].max_uV)
372                                 - (pv88090_buck_vol[index].min_uV))
373                                 /(pv88090_buck_vol[index].uV_step) + 1;
374                 }
375
376                 config.driver_data = (void *)&pv88090_regulator_info[i];
377                 chip->rdev[i] = devm_regulator_register(chip->dev,
378                         &pv88090_regulator_info[i].desc, &config);
379                 if (IS_ERR(chip->rdev[i])) {
380                         dev_err(chip->dev,
381                                 "Failed to register PV88090 regulator\n");
382                         return PTR_ERR(chip->rdev[i]);
383                 }
384         }
385
386         return 0;
387 }
388
389 static const struct i2c_device_id pv88090_i2c_id[] = {
390         {"pv88090", 0},
391         {},
392 };
393 MODULE_DEVICE_TABLE(i2c, pv88090_i2c_id);
394
395 #ifdef CONFIG_OF
396 static const struct of_device_id pv88090_dt_ids[] = {
397         { .compatible = "pvs,pv88090", .data = &pv88090_i2c_id[0] },
398         {},
399 };
400 MODULE_DEVICE_TABLE(of, pv88090_dt_ids);
401 #endif
402
403 static struct i2c_driver pv88090_regulator_driver = {
404         .driver = {
405                 .name = "pv88090",
406                 .of_match_table = of_match_ptr(pv88090_dt_ids),
407         },
408         .probe_new = pv88090_i2c_probe,
409         .id_table = pv88090_i2c_id,
410 };
411
412 module_i2c_driver(pv88090_regulator_driver);
413
414 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
415 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
416 MODULE_LICENSE("GPL");