Linux 5.1.15
[linux/fpc-iii.git] / drivers / regulator / pv88080-regulator.c
blob6279216fb2540d2b4981c7c8a4c9a69770ac97cb
1 /*
2 * pv88080-regulator.c - Regulator device driver for PV88080
3 * Copyright (C) 2016 Powerventure Semiconductor Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/regmap.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
27 #include <linux/regulator/of_regulator.h>
28 #include "pv88080-regulator.h"
30 #define PV88080_MAX_REGULATORS 4
32 /* PV88080 REGULATOR IDs */
33 enum {
34 /* BUCKs */
35 PV88080_ID_BUCK1,
36 PV88080_ID_BUCK2,
37 PV88080_ID_BUCK3,
38 PV88080_ID_HVBUCK,
41 enum pv88080_types {
42 TYPE_PV88080_AA,
43 TYPE_PV88080_BA,
46 struct pv88080_regulator {
47 struct regulator_desc desc;
48 unsigned int mode_reg;
49 unsigned int conf2;
50 unsigned int conf5;
53 struct pv88080 {
54 struct device *dev;
55 struct regmap *regmap;
56 struct regulator_dev *rdev[PV88080_MAX_REGULATORS];
57 unsigned long type;
58 const struct pv88080_compatible_regmap *regmap_config;
61 struct pv88080_buck_voltage {
62 int min_uV;
63 int max_uV;
64 int uV_step;
67 struct pv88080_buck_regmap {
68 /* REGS */
69 int buck_enable_reg;
70 int buck_vsel_reg;
71 int buck_mode_reg;
72 int buck_limit_reg;
73 int buck_vdac_range_reg;
74 int buck_vrange_gain_reg;
75 /* MASKS */
76 int buck_enable_mask;
77 int buck_vsel_mask;
78 int buck_limit_mask;
81 struct pv88080_compatible_regmap {
82 /* BUCK1, 2, 3 */
83 struct pv88080_buck_regmap buck_regmap[PV88080_MAX_REGULATORS-1];
84 /* HVBUCK */
85 int hvbuck_enable_reg;
86 int hvbuck_vsel_reg;
87 int hvbuck_enable_mask;
88 int hvbuck_vsel_mask;
91 static const struct regmap_config pv88080_regmap_config = {
92 .reg_bits = 8,
93 .val_bits = 8,
96 /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
97 * Entry indexes corresponds to register values.
100 static const unsigned int pv88080_buck1_limits[] = {
101 3230000, 5130000, 6960000, 8790000
104 static const unsigned int pv88080_buck23_limits[] = {
105 1496000, 2393000, 3291000, 4189000
108 static const struct pv88080_buck_voltage pv88080_buck_vol[2] = {
110 .min_uV = 600000,
111 .max_uV = 1393750,
112 .uV_step = 6250,
115 .min_uV = 1400000,
116 .max_uV = 2193750,
117 .uV_step = 6250,
121 static const struct pv88080_compatible_regmap pv88080_aa_regs = {
122 /* BUCK1 */
123 .buck_regmap[0] = {
124 .buck_enable_reg = PV88080AA_REG_BUCK1_CONF0,
125 .buck_vsel_reg = PV88080AA_REG_BUCK1_CONF0,
126 .buck_mode_reg = PV88080AA_REG_BUCK1_CONF1,
127 .buck_limit_reg = PV88080AA_REG_BUCK1_CONF1,
128 .buck_vdac_range_reg = PV88080AA_REG_BUCK1_CONF2,
129 .buck_vrange_gain_reg = PV88080AA_REG_BUCK1_CONF5,
130 .buck_enable_mask = PV88080_BUCK1_EN,
131 .buck_vsel_mask = PV88080_VBUCK1_MASK,
132 .buck_limit_mask = PV88080_BUCK1_ILIM_MASK,
134 /* BUCK2 */
135 .buck_regmap[1] = {
136 .buck_enable_reg = PV88080AA_REG_BUCK2_CONF0,
137 .buck_vsel_reg = PV88080AA_REG_BUCK2_CONF0,
138 .buck_mode_reg = PV88080AA_REG_BUCK2_CONF1,
139 .buck_limit_reg = PV88080AA_REG_BUCK2_CONF1,
140 .buck_vdac_range_reg = PV88080AA_REG_BUCK2_CONF2,
141 .buck_vrange_gain_reg = PV88080AA_REG_BUCK2_CONF5,
142 .buck_enable_mask = PV88080_BUCK2_EN,
143 .buck_vsel_mask = PV88080_VBUCK2_MASK,
144 .buck_limit_mask = PV88080_BUCK2_ILIM_MASK,
146 /* BUCK3 */
147 .buck_regmap[2] = {
148 .buck_enable_reg = PV88080AA_REG_BUCK3_CONF0,
149 .buck_vsel_reg = PV88080AA_REG_BUCK3_CONF0,
150 .buck_mode_reg = PV88080AA_REG_BUCK3_CONF1,
151 .buck_limit_reg = PV88080AA_REG_BUCK3_CONF1,
152 .buck_vdac_range_reg = PV88080AA_REG_BUCK3_CONF2,
153 .buck_vrange_gain_reg = PV88080AA_REG_BUCK3_CONF5,
154 .buck_enable_mask = PV88080_BUCK3_EN,
155 .buck_vsel_mask = PV88080_VBUCK3_MASK,
156 .buck_limit_mask = PV88080_BUCK3_ILIM_MASK,
158 /* HVBUCK */
159 .hvbuck_enable_reg = PV88080AA_REG_HVBUCK_CONF2,
160 .hvbuck_vsel_reg = PV88080AA_REG_HVBUCK_CONF1,
161 .hvbuck_enable_mask = PV88080_HVBUCK_EN,
162 .hvbuck_vsel_mask = PV88080_VHVBUCK_MASK,
165 static const struct pv88080_compatible_regmap pv88080_ba_regs = {
166 /* BUCK1 */
167 .buck_regmap[0] = {
168 .buck_enable_reg = PV88080BA_REG_BUCK1_CONF0,
169 .buck_vsel_reg = PV88080BA_REG_BUCK1_CONF0,
170 .buck_mode_reg = PV88080BA_REG_BUCK1_CONF1,
171 .buck_limit_reg = PV88080BA_REG_BUCK1_CONF1,
172 .buck_vdac_range_reg = PV88080BA_REG_BUCK1_CONF2,
173 .buck_vrange_gain_reg = PV88080BA_REG_BUCK1_CONF5,
174 .buck_enable_mask = PV88080_BUCK1_EN,
175 .buck_vsel_mask = PV88080_VBUCK1_MASK,
176 .buck_limit_mask = PV88080_BUCK1_ILIM_MASK,
178 /* BUCK2 */
179 .buck_regmap[1] = {
180 .buck_enable_reg = PV88080BA_REG_BUCK2_CONF0,
181 .buck_vsel_reg = PV88080BA_REG_BUCK2_CONF0,
182 .buck_mode_reg = PV88080BA_REG_BUCK2_CONF1,
183 .buck_limit_reg = PV88080BA_REG_BUCK2_CONF1,
184 .buck_vdac_range_reg = PV88080BA_REG_BUCK2_CONF2,
185 .buck_vrange_gain_reg = PV88080BA_REG_BUCK2_CONF5,
186 .buck_enable_mask = PV88080_BUCK2_EN,
187 .buck_vsel_mask = PV88080_VBUCK2_MASK,
188 .buck_limit_mask = PV88080_BUCK2_ILIM_MASK,
190 /* BUCK3 */
191 .buck_regmap[2] = {
192 .buck_enable_reg = PV88080BA_REG_BUCK3_CONF0,
193 .buck_vsel_reg = PV88080BA_REG_BUCK3_CONF0,
194 .buck_mode_reg = PV88080BA_REG_BUCK3_CONF1,
195 .buck_limit_reg = PV88080BA_REG_BUCK3_CONF1,
196 .buck_vdac_range_reg = PV88080BA_REG_BUCK3_CONF2,
197 .buck_vrange_gain_reg = PV88080BA_REG_BUCK3_CONF5,
198 .buck_enable_mask = PV88080_BUCK3_EN,
199 .buck_vsel_mask = PV88080_VBUCK3_MASK,
200 .buck_limit_mask = PV88080_BUCK3_ILIM_MASK,
202 /* HVBUCK */
203 .hvbuck_enable_reg = PV88080BA_REG_HVBUCK_CONF2,
204 .hvbuck_vsel_reg = PV88080BA_REG_HVBUCK_CONF1,
205 .hvbuck_enable_mask = PV88080_HVBUCK_EN,
206 .hvbuck_vsel_mask = PV88080_VHVBUCK_MASK,
209 #ifdef CONFIG_OF
210 static const struct of_device_id pv88080_dt_ids[] = {
211 { .compatible = "pvs,pv88080", .data = (void *)TYPE_PV88080_AA },
212 { .compatible = "pvs,pv88080-aa", .data = (void *)TYPE_PV88080_AA },
213 { .compatible = "pvs,pv88080-ba", .data = (void *)TYPE_PV88080_BA },
216 MODULE_DEVICE_TABLE(of, pv88080_dt_ids);
217 #endif
219 static unsigned int pv88080_buck_get_mode(struct regulator_dev *rdev)
221 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
222 unsigned int data;
223 int ret, mode = 0;
225 ret = regmap_read(rdev->regmap, info->mode_reg, &data);
226 if (ret < 0)
227 return ret;
229 switch (data & PV88080_BUCK1_MODE_MASK) {
230 case PV88080_BUCK_MODE_SYNC:
231 mode = REGULATOR_MODE_FAST;
232 break;
233 case PV88080_BUCK_MODE_AUTO:
234 mode = REGULATOR_MODE_NORMAL;
235 break;
236 case PV88080_BUCK_MODE_SLEEP:
237 mode = REGULATOR_MODE_STANDBY;
238 break;
239 default:
240 return -EINVAL;
243 return mode;
246 static int pv88080_buck_set_mode(struct regulator_dev *rdev,
247 unsigned int mode)
249 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
250 int val = 0;
252 switch (mode) {
253 case REGULATOR_MODE_FAST:
254 val = PV88080_BUCK_MODE_SYNC;
255 break;
256 case REGULATOR_MODE_NORMAL:
257 val = PV88080_BUCK_MODE_AUTO;
258 break;
259 case REGULATOR_MODE_STANDBY:
260 val = PV88080_BUCK_MODE_SLEEP;
261 break;
262 default:
263 return -EINVAL;
266 return regmap_update_bits(rdev->regmap, info->mode_reg,
267 PV88080_BUCK1_MODE_MASK, val);
270 static const struct regulator_ops pv88080_buck_ops = {
271 .get_mode = pv88080_buck_get_mode,
272 .set_mode = pv88080_buck_set_mode,
273 .enable = regulator_enable_regmap,
274 .disable = regulator_disable_regmap,
275 .is_enabled = regulator_is_enabled_regmap,
276 .set_voltage_sel = regulator_set_voltage_sel_regmap,
277 .get_voltage_sel = regulator_get_voltage_sel_regmap,
278 .list_voltage = regulator_list_voltage_linear,
279 .set_current_limit = regulator_set_current_limit_regmap,
280 .get_current_limit = regulator_get_current_limit_regmap,
283 static const struct regulator_ops pv88080_hvbuck_ops = {
284 .enable = regulator_enable_regmap,
285 .disable = regulator_disable_regmap,
286 .is_enabled = regulator_is_enabled_regmap,
287 .set_voltage_sel = regulator_set_voltage_sel_regmap,
288 .get_voltage_sel = regulator_get_voltage_sel_regmap,
289 .list_voltage = regulator_list_voltage_linear,
292 #define PV88080_BUCK(chip, regl_name, min, step, max, limits_array) \
294 .desc = {\
295 .id = chip##_ID_##regl_name,\
296 .name = __stringify(chip##_##regl_name),\
297 .of_match = of_match_ptr(#regl_name),\
298 .regulators_node = of_match_ptr("regulators"),\
299 .type = REGULATOR_VOLTAGE,\
300 .owner = THIS_MODULE,\
301 .ops = &pv88080_buck_ops,\
302 .min_uV = min, \
303 .uV_step = step, \
304 .n_voltages = ((max) - (min))/(step) + 1, \
305 .curr_table = limits_array, \
306 .n_current_limits = ARRAY_SIZE(limits_array), \
310 #define PV88080_HVBUCK(chip, regl_name, min, step, max) \
312 .desc = {\
313 .id = chip##_ID_##regl_name,\
314 .name = __stringify(chip##_##regl_name),\
315 .of_match = of_match_ptr(#regl_name),\
316 .regulators_node = of_match_ptr("regulators"),\
317 .type = REGULATOR_VOLTAGE,\
318 .owner = THIS_MODULE,\
319 .ops = &pv88080_hvbuck_ops,\
320 .min_uV = min, \
321 .uV_step = step, \
322 .n_voltages = ((max) - (min))/(step) + 1, \
326 static struct pv88080_regulator pv88080_regulator_info[] = {
327 PV88080_BUCK(PV88080, BUCK1, 600000, 6250, 1393750,
328 pv88080_buck1_limits),
329 PV88080_BUCK(PV88080, BUCK2, 600000, 6250, 1393750,
330 pv88080_buck23_limits),
331 PV88080_BUCK(PV88080, BUCK3, 600000, 6250, 1393750,
332 pv88080_buck23_limits),
333 PV88080_HVBUCK(PV88080, HVBUCK, 0, 5000, 1275000),
336 static irqreturn_t pv88080_irq_handler(int irq, void *data)
338 struct pv88080 *chip = data;
339 int i, reg_val, err, ret = IRQ_NONE;
341 err = regmap_read(chip->regmap, PV88080_REG_EVENT_A, &reg_val);
342 if (err < 0)
343 goto error_i2c;
345 if (reg_val & PV88080_E_VDD_FLT) {
346 for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
347 if (chip->rdev[i] != NULL) {
348 regulator_lock(chip->rdev[i]);
349 regulator_notifier_call_chain(chip->rdev[i],
350 REGULATOR_EVENT_UNDER_VOLTAGE,
351 NULL);
352 regulator_unlock(chip->rdev[i]);
356 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
357 PV88080_E_VDD_FLT);
358 if (err < 0)
359 goto error_i2c;
361 ret = IRQ_HANDLED;
364 if (reg_val & PV88080_E_OVER_TEMP) {
365 for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
366 if (chip->rdev[i] != NULL) {
367 regulator_lock(chip->rdev[i]);
368 regulator_notifier_call_chain(chip->rdev[i],
369 REGULATOR_EVENT_OVER_TEMP,
370 NULL);
371 regulator_unlock(chip->rdev[i]);
375 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
376 PV88080_E_OVER_TEMP);
377 if (err < 0)
378 goto error_i2c;
380 ret = IRQ_HANDLED;
383 return ret;
385 error_i2c:
386 dev_err(chip->dev, "I2C error : %d\n", err);
387 return IRQ_NONE;
391 * I2C driver interface functions
393 static int pv88080_i2c_probe(struct i2c_client *i2c,
394 const struct i2c_device_id *id)
396 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
397 struct pv88080 *chip;
398 const struct pv88080_compatible_regmap *regmap_config;
399 const struct of_device_id *match;
400 struct regulator_config config = { };
401 int i, error, ret;
402 unsigned int conf2, conf5;
404 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88080), GFP_KERNEL);
405 if (!chip)
406 return -ENOMEM;
408 chip->dev = &i2c->dev;
409 chip->regmap = devm_regmap_init_i2c(i2c, &pv88080_regmap_config);
410 if (IS_ERR(chip->regmap)) {
411 error = PTR_ERR(chip->regmap);
412 dev_err(chip->dev, "Failed to allocate register map: %d\n",
413 error);
414 return error;
417 if (i2c->dev.of_node) {
418 match = of_match_node(pv88080_dt_ids, i2c->dev.of_node);
419 if (!match) {
420 dev_err(chip->dev, "Failed to get of_match_node\n");
421 return -EINVAL;
423 chip->type = (unsigned long)match->data;
424 } else {
425 chip->type = id->driver_data;
428 i2c_set_clientdata(i2c, chip);
430 if (i2c->irq != 0) {
431 ret = regmap_write(chip->regmap, PV88080_REG_MASK_A, 0xFF);
432 if (ret < 0) {
433 dev_err(chip->dev,
434 "Failed to mask A reg: %d\n", ret);
435 return ret;
437 ret = regmap_write(chip->regmap, PV88080_REG_MASK_B, 0xFF);
438 if (ret < 0) {
439 dev_err(chip->dev,
440 "Failed to mask B reg: %d\n", ret);
441 return ret;
443 ret = regmap_write(chip->regmap, PV88080_REG_MASK_C, 0xFF);
444 if (ret < 0) {
445 dev_err(chip->dev,
446 "Failed to mask C reg: %d\n", ret);
447 return ret;
450 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
451 pv88080_irq_handler,
452 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
453 "pv88080", chip);
454 if (ret != 0) {
455 dev_err(chip->dev, "Failed to request IRQ: %d\n",
456 i2c->irq);
457 return ret;
460 ret = regmap_update_bits(chip->regmap, PV88080_REG_MASK_A,
461 PV88080_M_VDD_FLT | PV88080_M_OVER_TEMP, 0);
462 if (ret < 0) {
463 dev_err(chip->dev,
464 "Failed to update mask reg: %d\n", ret);
465 return ret;
467 } else {
468 dev_warn(chip->dev, "No IRQ configured\n");
471 switch (chip->type) {
472 case TYPE_PV88080_AA:
473 chip->regmap_config = &pv88080_aa_regs;
474 break;
475 case TYPE_PV88080_BA:
476 chip->regmap_config = &pv88080_ba_regs;
477 break;
480 regmap_config = chip->regmap_config;
481 config.dev = chip->dev;
482 config.regmap = chip->regmap;
484 /* Registeration for BUCK1, 2, 3 */
485 for (i = 0; i < PV88080_MAX_REGULATORS-1; i++) {
486 if (init_data)
487 config.init_data = &init_data[i];
489 pv88080_regulator_info[i].desc.csel_reg
490 = regmap_config->buck_regmap[i].buck_limit_reg;
491 pv88080_regulator_info[i].desc.csel_mask
492 = regmap_config->buck_regmap[i].buck_limit_mask;
493 pv88080_regulator_info[i].mode_reg
494 = regmap_config->buck_regmap[i].buck_mode_reg;
495 pv88080_regulator_info[i].conf2
496 = regmap_config->buck_regmap[i].buck_vdac_range_reg;
497 pv88080_regulator_info[i].conf5
498 = regmap_config->buck_regmap[i].buck_vrange_gain_reg;
499 pv88080_regulator_info[i].desc.enable_reg
500 = regmap_config->buck_regmap[i].buck_enable_reg;
501 pv88080_regulator_info[i].desc.enable_mask
502 = regmap_config->buck_regmap[i].buck_enable_mask;
503 pv88080_regulator_info[i].desc.vsel_reg
504 = regmap_config->buck_regmap[i].buck_vsel_reg;
505 pv88080_regulator_info[i].desc.vsel_mask
506 = regmap_config->buck_regmap[i].buck_vsel_mask;
508 ret = regmap_read(chip->regmap,
509 pv88080_regulator_info[i].conf2, &conf2);
510 if (ret < 0)
511 return ret;
512 conf2 = ((conf2 >> PV88080_BUCK_VDAC_RANGE_SHIFT) &
513 PV88080_BUCK_VDAC_RANGE_MASK);
515 ret = regmap_read(chip->regmap,
516 pv88080_regulator_info[i].conf5, &conf5);
517 if (ret < 0)
518 return ret;
519 conf5 = ((conf5 >> PV88080_BUCK_VRANGE_GAIN_SHIFT) &
520 PV88080_BUCK_VRANGE_GAIN_MASK);
522 pv88080_regulator_info[i].desc.min_uV =
523 pv88080_buck_vol[conf2].min_uV * (conf5+1);
524 pv88080_regulator_info[i].desc.uV_step =
525 pv88080_buck_vol[conf2].uV_step * (conf5+1);
526 pv88080_regulator_info[i].desc.n_voltages =
527 ((pv88080_buck_vol[conf2].max_uV * (conf5+1))
528 - (pv88080_regulator_info[i].desc.min_uV))
529 /(pv88080_regulator_info[i].desc.uV_step) + 1;
531 config.driver_data = (void *)&pv88080_regulator_info[i];
532 chip->rdev[i] = devm_regulator_register(chip->dev,
533 &pv88080_regulator_info[i].desc, &config);
534 if (IS_ERR(chip->rdev[i])) {
535 dev_err(chip->dev,
536 "Failed to register PV88080 regulator\n");
537 return PTR_ERR(chip->rdev[i]);
541 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.enable_reg
542 = regmap_config->hvbuck_enable_reg;
543 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.enable_mask
544 = regmap_config->hvbuck_enable_mask;
545 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.vsel_reg
546 = regmap_config->hvbuck_vsel_reg;
547 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.vsel_mask
548 = regmap_config->hvbuck_vsel_mask;
550 /* Registeration for HVBUCK */
551 if (init_data)
552 config.init_data = &init_data[PV88080_ID_HVBUCK];
554 config.driver_data = (void *)&pv88080_regulator_info[PV88080_ID_HVBUCK];
555 chip->rdev[PV88080_ID_HVBUCK] = devm_regulator_register(chip->dev,
556 &pv88080_regulator_info[PV88080_ID_HVBUCK].desc, &config);
557 if (IS_ERR(chip->rdev[PV88080_ID_HVBUCK])) {
558 dev_err(chip->dev, "Failed to register PV88080 regulator\n");
559 return PTR_ERR(chip->rdev[PV88080_ID_HVBUCK]);
562 return 0;
565 static const struct i2c_device_id pv88080_i2c_id[] = {
566 { "pv88080", TYPE_PV88080_AA },
567 { "pv88080-aa", TYPE_PV88080_AA },
568 { "pv88080-ba", TYPE_PV88080_BA },
571 MODULE_DEVICE_TABLE(i2c, pv88080_i2c_id);
573 static struct i2c_driver pv88080_regulator_driver = {
574 .driver = {
575 .name = "pv88080",
576 .of_match_table = of_match_ptr(pv88080_dt_ids),
578 .probe = pv88080_i2c_probe,
579 .id_table = pv88080_i2c_id,
582 module_i2c_driver(pv88080_regulator_driver);
584 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
585 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88080");
586 MODULE_LICENSE("GPL");