Merge tag 'dmaengine-fix-5.2' of git://git.infradead.org/users/vkoul/slave-dma
[linux-2.6-microblaze.git] / drivers / regulator / pv88060-regulator.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // pv88060-regulator.c - Regulator device driver for PV88060
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 "pv88060-regulator.h"
18
19 #define PV88060_MAX_REGULATORS  14
20
21 /* PV88060 REGULATOR IDs */
22 enum {
23         /* BUCKs */
24         PV88060_ID_BUCK1,
25
26         /* LDOs */
27         PV88060_ID_LDO1,
28         PV88060_ID_LDO2,
29         PV88060_ID_LDO3,
30         PV88060_ID_LDO4,
31         PV88060_ID_LDO5,
32         PV88060_ID_LDO6,
33         PV88060_ID_LDO7,
34
35         /* SWTs */
36         PV88060_ID_SW1,
37         PV88060_ID_SW2,
38         PV88060_ID_SW3,
39         PV88060_ID_SW4,
40         PV88060_ID_SW5,
41         PV88060_ID_SW6,
42 };
43
44 struct pv88060_regulator {
45         struct regulator_desc desc;
46         unsigned int conf;              /* buck configuration register */
47 };
48
49 struct pv88060 {
50         struct device *dev;
51         struct regmap *regmap;
52         struct regulator_dev *rdev[PV88060_MAX_REGULATORS];
53 };
54
55 static const struct regmap_config pv88060_regmap_config = {
56         .reg_bits = 8,
57         .val_bits = 8,
58 };
59
60 /* Current limits array (in uA) for BUCK1
61  * Entry indexes corresponds to register values.
62  */
63
64 static const unsigned int pv88060_buck1_limits[] = {
65         1496000, 2393000, 3291000, 4189000
66 };
67
68 static unsigned int pv88060_buck_get_mode(struct regulator_dev *rdev)
69 {
70         struct pv88060_regulator *info = rdev_get_drvdata(rdev);
71         unsigned int data;
72         int ret, mode = 0;
73
74         ret = regmap_read(rdev->regmap, info->conf, &data);
75         if (ret < 0)
76                 return ret;
77
78         switch (data & PV88060_BUCK_MODE_MASK) {
79         case PV88060_BUCK_MODE_SYNC:
80                 mode = REGULATOR_MODE_FAST;
81                 break;
82         case PV88060_BUCK_MODE_AUTO:
83                 mode = REGULATOR_MODE_NORMAL;
84                 break;
85         case PV88060_BUCK_MODE_SLEEP:
86                 mode = REGULATOR_MODE_STANDBY;
87                 break;
88         }
89
90         return mode;
91 }
92
93 static int pv88060_buck_set_mode(struct regulator_dev *rdev,
94                                         unsigned int mode)
95 {
96         struct pv88060_regulator *info = rdev_get_drvdata(rdev);
97         int val = 0;
98
99         switch (mode) {
100         case REGULATOR_MODE_FAST:
101                 val = PV88060_BUCK_MODE_SYNC;
102                 break;
103         case REGULATOR_MODE_NORMAL:
104                 val = PV88060_BUCK_MODE_AUTO;
105                 break;
106         case REGULATOR_MODE_STANDBY:
107                 val = PV88060_BUCK_MODE_SLEEP;
108                 break;
109         default:
110                 return -EINVAL;
111         }
112
113         return regmap_update_bits(rdev->regmap, info->conf,
114                                         PV88060_BUCK_MODE_MASK, val);
115 }
116
117 static const struct regulator_ops pv88060_buck_ops = {
118         .get_mode = pv88060_buck_get_mode,
119         .set_mode = pv88060_buck_set_mode,
120         .enable = regulator_enable_regmap,
121         .disable = regulator_disable_regmap,
122         .is_enabled = regulator_is_enabled_regmap,
123         .set_voltage_sel = regulator_set_voltage_sel_regmap,
124         .get_voltage_sel = regulator_get_voltage_sel_regmap,
125         .list_voltage = regulator_list_voltage_linear,
126         .set_current_limit = regulator_set_current_limit_regmap,
127         .get_current_limit = regulator_get_current_limit_regmap,
128 };
129
130 static const struct regulator_ops pv88060_ldo_ops = {
131         .enable = regulator_enable_regmap,
132         .disable = regulator_disable_regmap,
133         .is_enabled = regulator_is_enabled_regmap,
134         .set_voltage_sel = regulator_set_voltage_sel_regmap,
135         .get_voltage_sel = regulator_get_voltage_sel_regmap,
136         .list_voltage = regulator_list_voltage_linear,
137 };
138
139 static const struct regulator_ops pv88060_sw_ops = {
140         .enable = regulator_enable_regmap,
141         .disable = regulator_disable_regmap,
142         .is_enabled = regulator_is_enabled_regmap,
143 };
144
145 #define PV88060_BUCK(chip, regl_name, min, step, max, limits_array) \
146 {\
147         .desc   =       {\
148                 .id = chip##_ID_##regl_name,\
149                 .name = __stringify(chip##_##regl_name),\
150                 .of_match = of_match_ptr(#regl_name),\
151                 .regulators_node = of_match_ptr("regulators"),\
152                 .type = REGULATOR_VOLTAGE,\
153                 .owner = THIS_MODULE,\
154                 .ops = &pv88060_buck_ops,\
155                 .min_uV = min,\
156                 .uV_step = step,\
157                 .n_voltages = ((max) - (min))/(step) + 1,\
158                 .enable_reg = PV88060_REG_##regl_name##_CONF0,\
159                 .enable_mask = PV88060_BUCK_EN, \
160                 .vsel_reg = PV88060_REG_##regl_name##_CONF0,\
161                 .vsel_mask = PV88060_VBUCK_MASK,\
162                 .curr_table = limits_array,\
163                 .n_current_limits = ARRAY_SIZE(limits_array),\
164                 .csel_reg = PV88060_REG_##regl_name##_CONF1,\
165                 .csel_mask = PV88060_BUCK_ILIM_MASK,\
166         },\
167         .conf = PV88060_REG_##regl_name##_CONF1,\
168 }
169
170 #define PV88060_LDO(chip, regl_name, min, step, max) \
171 {\
172         .desc   =       {\
173                 .id = chip##_ID_##regl_name,\
174                 .name = __stringify(chip##_##regl_name),\
175                 .of_match = of_match_ptr(#regl_name),\
176                 .regulators_node = of_match_ptr("regulators"),\
177                 .type = REGULATOR_VOLTAGE,\
178                 .owner = THIS_MODULE,\
179                 .ops = &pv88060_ldo_ops,\
180                 .min_uV = min, \
181                 .uV_step = step, \
182                 .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
183                 .enable_reg = PV88060_REG_##regl_name##_CONF, \
184                 .enable_mask = PV88060_LDO_EN, \
185                 .vsel_reg = PV88060_REG_##regl_name##_CONF, \
186                 .vsel_mask = PV88060_VLDO_MASK, \
187         },\
188 }
189
190 #define PV88060_SW(chip, regl_name, max) \
191 {\
192         .desc   =       {\
193                 .id = chip##_ID_##regl_name,\
194                 .name = __stringify(chip##_##regl_name),\
195                 .of_match = of_match_ptr(#regl_name),\
196                 .regulators_node = of_match_ptr("regulators"),\
197                 .type = REGULATOR_VOLTAGE,\
198                 .owner = THIS_MODULE,\
199                 .ops = &pv88060_sw_ops,\
200                 .fixed_uV = max,\
201                 .n_voltages = 1,\
202                 .enable_reg = PV88060_REG_##regl_name##_CONF,\
203                 .enable_mask = PV88060_SW_EN,\
204         },\
205 }
206
207 static const struct pv88060_regulator pv88060_regulator_info[] = {
208         PV88060_BUCK(PV88060, BUCK1, 2800000, 12500, 4387500,
209                 pv88060_buck1_limits),
210         PV88060_LDO(PV88060, LDO1, 1200000, 50000, 3350000),
211         PV88060_LDO(PV88060, LDO2, 1200000, 50000, 3350000),
212         PV88060_LDO(PV88060, LDO3, 1200000, 50000, 3350000),
213         PV88060_LDO(PV88060, LDO4, 1200000, 50000, 3350000),
214         PV88060_LDO(PV88060, LDO5, 1200000, 50000, 3350000),
215         PV88060_LDO(PV88060, LDO6, 1200000, 50000, 3350000),
216         PV88060_LDO(PV88060, LDO7, 1200000, 50000, 3350000),
217         PV88060_SW(PV88060, SW1, 5000000),
218         PV88060_SW(PV88060, SW2, 5000000),
219         PV88060_SW(PV88060, SW3, 5000000),
220         PV88060_SW(PV88060, SW4, 5000000),
221         PV88060_SW(PV88060, SW5, 5000000),
222         PV88060_SW(PV88060, SW6, 5000000),
223 };
224
225 static irqreturn_t pv88060_irq_handler(int irq, void *data)
226 {
227         struct pv88060 *chip = data;
228         int i, reg_val, err, ret = IRQ_NONE;
229
230         err = regmap_read(chip->regmap, PV88060_REG_EVENT_A, &reg_val);
231         if (err < 0)
232                 goto error_i2c;
233
234         if (reg_val & PV88060_E_VDD_FLT) {
235                 for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
236                         if (chip->rdev[i] != NULL) {
237                                 regulator_lock(chip->rdev[i]);
238                                 regulator_notifier_call_chain(chip->rdev[i],
239                                         REGULATOR_EVENT_UNDER_VOLTAGE,
240                                         NULL);
241                                 regulator_unlock(chip->rdev[i]);
242                         }
243                 }
244
245                 err = regmap_write(chip->regmap, PV88060_REG_EVENT_A,
246                         PV88060_E_VDD_FLT);
247                 if (err < 0)
248                         goto error_i2c;
249
250                 ret = IRQ_HANDLED;
251         }
252
253         if (reg_val & PV88060_E_OVER_TEMP) {
254                 for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
255                         if (chip->rdev[i] != NULL) {
256                                 regulator_lock(chip->rdev[i]);
257                                 regulator_notifier_call_chain(chip->rdev[i],
258                                         REGULATOR_EVENT_OVER_TEMP,
259                                         NULL);
260                                 regulator_unlock(chip->rdev[i]);
261                         }
262                 }
263
264                 err = regmap_write(chip->regmap, PV88060_REG_EVENT_A,
265                         PV88060_E_OVER_TEMP);
266                 if (err < 0)
267                         goto error_i2c;
268
269                 ret = IRQ_HANDLED;
270         }
271
272         return ret;
273
274 error_i2c:
275         dev_err(chip->dev, "I2C error : %d\n", err);
276         return IRQ_NONE;
277 }
278
279 /*
280  * I2C driver interface functions
281  */
282 static int pv88060_i2c_probe(struct i2c_client *i2c,
283                 const struct i2c_device_id *id)
284 {
285         struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
286         struct pv88060 *chip;
287         struct regulator_config config = { };
288         int error, i, ret = 0;
289
290         chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88060), GFP_KERNEL);
291         if (!chip)
292                 return -ENOMEM;
293
294         chip->dev = &i2c->dev;
295         chip->regmap = devm_regmap_init_i2c(i2c, &pv88060_regmap_config);
296         if (IS_ERR(chip->regmap)) {
297                 error = PTR_ERR(chip->regmap);
298                 dev_err(chip->dev, "Failed to allocate register map: %d\n",
299                         error);
300                 return error;
301         }
302
303         i2c_set_clientdata(i2c, chip);
304
305         if (i2c->irq != 0) {
306                 ret = regmap_write(chip->regmap, PV88060_REG_MASK_A, 0xFF);
307                 if (ret < 0) {
308                         dev_err(chip->dev,
309                                 "Failed to mask A reg: %d\n", ret);
310                         return ret;
311                 }
312
313                 ret = regmap_write(chip->regmap, PV88060_REG_MASK_B, 0xFF);
314                 if (ret < 0) {
315                         dev_err(chip->dev,
316                                 "Failed to mask B reg: %d\n", ret);
317                         return ret;
318                 }
319
320                 ret = regmap_write(chip->regmap, PV88060_REG_MASK_C, 0xFF);
321                 if (ret < 0) {
322                         dev_err(chip->dev,
323                                 "Failed to mask C reg: %d\n", ret);
324                         return ret;
325                 }
326
327                 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
328                                         pv88060_irq_handler,
329                                         IRQF_TRIGGER_LOW|IRQF_ONESHOT,
330                                         "pv88060", chip);
331                 if (ret != 0) {
332                         dev_err(chip->dev, "Failed to request IRQ: %d\n",
333                                 i2c->irq);
334                         return ret;
335                 }
336
337                 ret = regmap_update_bits(chip->regmap, PV88060_REG_MASK_A,
338                         PV88060_M_VDD_FLT | PV88060_M_OVER_TEMP, 0);
339                 if (ret < 0) {
340                         dev_err(chip->dev,
341                                 "Failed to update mask reg: %d\n", ret);
342                         return ret;
343                 }
344
345         } else {
346                 dev_warn(chip->dev, "No IRQ configured\n");
347         }
348
349         config.dev = chip->dev;
350         config.regmap = chip->regmap;
351
352         for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
353                 if (init_data)
354                         config.init_data = &init_data[i];
355
356                 config.driver_data = (void *)&pv88060_regulator_info[i];
357                 chip->rdev[i] = devm_regulator_register(chip->dev,
358                         &pv88060_regulator_info[i].desc, &config);
359                 if (IS_ERR(chip->rdev[i])) {
360                         dev_err(chip->dev,
361                                 "Failed to register PV88060 regulator\n");
362                         return PTR_ERR(chip->rdev[i]);
363                 }
364         }
365
366         return 0;
367 }
368
369 static const struct i2c_device_id pv88060_i2c_id[] = {
370         {"pv88060", 0},
371         {},
372 };
373 MODULE_DEVICE_TABLE(i2c, pv88060_i2c_id);
374
375 #ifdef CONFIG_OF
376 static const struct of_device_id pv88060_dt_ids[] = {
377         { .compatible = "pvs,pv88060", .data = &pv88060_i2c_id[0] },
378         {},
379 };
380 MODULE_DEVICE_TABLE(of, pv88060_dt_ids);
381 #endif
382
383 static struct i2c_driver pv88060_regulator_driver = {
384         .driver = {
385                 .name = "pv88060",
386                 .of_match_table = of_match_ptr(pv88060_dt_ids),
387         },
388         .probe = pv88060_i2c_probe,
389         .id_table = pv88060_i2c_id,
390 };
391
392 module_i2c_driver(pv88060_regulator_driver);
393
394 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
395 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060");
396 MODULE_LICENSE("GPL");