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>
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 */
46 struct pv88080_regulator {
47 struct regulator_desc desc;
48 unsigned int mode_reg;
55 struct regmap *regmap;
56 struct regulator_dev *rdev[PV88080_MAX_REGULATORS];
58 const struct pv88080_compatible_regmap *regmap_config;
61 struct pv88080_buck_voltage {
67 struct pv88080_buck_regmap {
73 int buck_vdac_range_reg;
74 int buck_vrange_gain_reg;
81 struct pv88080_compatible_regmap {
83 struct pv88080_buck_regmap buck_regmap[PV88080_MAX_REGULATORS-1];
85 int hvbuck_enable_reg;
87 int hvbuck_enable_mask;
91 static const struct regmap_config pv88080_regmap_config = {
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] = {
121 static const struct pv88080_compatible_regmap pv88080_aa_regs = {
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,
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,
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,
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 = {
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,
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,
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,
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,
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);
219 static unsigned int pv88080_buck_get_mode(struct regulator_dev *rdev)
221 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
225 ret = regmap_read(rdev->regmap, info->mode_reg, &data);
229 switch (data & PV88080_BUCK1_MODE_MASK) {
230 case PV88080_BUCK_MODE_SYNC:
231 mode = REGULATOR_MODE_FAST;
233 case PV88080_BUCK_MODE_AUTO:
234 mode = REGULATOR_MODE_NORMAL;
236 case PV88080_BUCK_MODE_SLEEP:
237 mode = REGULATOR_MODE_STANDBY;
246 static int pv88080_buck_set_mode(struct regulator_dev *rdev,
249 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
253 case REGULATOR_MODE_FAST:
254 val = PV88080_BUCK_MODE_SYNC;
256 case REGULATOR_MODE_NORMAL:
257 val = PV88080_BUCK_MODE_AUTO;
259 case REGULATOR_MODE_STANDBY:
260 val = PV88080_BUCK_MODE_SLEEP;
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) \
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,\
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) \
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,\
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, ®_val);
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_notifier_call_chain(chip->rdev[i],
349 REGULATOR_EVENT_UNDER_VOLTAGE,
354 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
362 if (reg_val & PV88080_E_OVER_TEMP) {
363 for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
364 if (chip->rdev[i] != NULL) {
365 regulator_notifier_call_chain(chip->rdev[i],
366 REGULATOR_EVENT_OVER_TEMP,
371 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
372 PV88080_E_OVER_TEMP);
382 dev_err(chip->dev, "I2C error : %d\n", err);
387 * I2C driver interface functions
389 static int pv88080_i2c_probe(struct i2c_client *i2c,
390 const struct i2c_device_id *id)
392 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
393 struct pv88080 *chip;
394 const struct pv88080_compatible_regmap *regmap_config;
395 const struct of_device_id *match;
396 struct regulator_config config = { };
398 unsigned int conf2, conf5;
400 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88080), GFP_KERNEL);
404 chip->dev = &i2c->dev;
405 chip->regmap = devm_regmap_init_i2c(i2c, &pv88080_regmap_config);
406 if (IS_ERR(chip->regmap)) {
407 error = PTR_ERR(chip->regmap);
408 dev_err(chip->dev, "Failed to allocate register map: %d\n",
413 if (i2c->dev.of_node) {
414 match = of_match_node(pv88080_dt_ids, i2c->dev.of_node);
416 dev_err(chip->dev, "Failed to get of_match_node\n");
419 chip->type = (unsigned long)match->data;
421 chip->type = id->driver_data;
424 i2c_set_clientdata(i2c, chip);
427 ret = regmap_write(chip->regmap, PV88080_REG_MASK_A, 0xFF);
430 "Failed to mask A reg: %d\n", ret);
433 ret = regmap_write(chip->regmap, PV88080_REG_MASK_B, 0xFF);
436 "Failed to mask B reg: %d\n", ret);
439 ret = regmap_write(chip->regmap, PV88080_REG_MASK_C, 0xFF);
442 "Failed to mask C reg: %d\n", ret);
446 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
448 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
451 dev_err(chip->dev, "Failed to request IRQ: %d\n",
456 ret = regmap_update_bits(chip->regmap, PV88080_REG_MASK_A,
457 PV88080_M_VDD_FLT | PV88080_M_OVER_TEMP, 0);
460 "Failed to update mask reg: %d\n", ret);
464 dev_warn(chip->dev, "No IRQ configured\n");
467 switch (chip->type) {
468 case TYPE_PV88080_AA:
469 chip->regmap_config = &pv88080_aa_regs;
471 case TYPE_PV88080_BA:
472 chip->regmap_config = &pv88080_ba_regs;
476 regmap_config = chip->regmap_config;
477 config.dev = chip->dev;
478 config.regmap = chip->regmap;
480 /* Registeration for BUCK1, 2, 3 */
481 for (i = 0; i < PV88080_MAX_REGULATORS-1; i++) {
483 config.init_data = &init_data[i];
485 pv88080_regulator_info[i].desc.csel_reg
486 = regmap_config->buck_regmap[i].buck_limit_reg;
487 pv88080_regulator_info[i].desc.csel_mask
488 = regmap_config->buck_regmap[i].buck_limit_mask;
489 pv88080_regulator_info[i].mode_reg
490 = regmap_config->buck_regmap[i].buck_mode_reg;
491 pv88080_regulator_info[i].conf2
492 = regmap_config->buck_regmap[i].buck_vdac_range_reg;
493 pv88080_regulator_info[i].conf5
494 = regmap_config->buck_regmap[i].buck_vrange_gain_reg;
495 pv88080_regulator_info[i].desc.enable_reg
496 = regmap_config->buck_regmap[i].buck_enable_reg;
497 pv88080_regulator_info[i].desc.enable_mask
498 = regmap_config->buck_regmap[i].buck_enable_mask;
499 pv88080_regulator_info[i].desc.vsel_reg
500 = regmap_config->buck_regmap[i].buck_vsel_reg;
501 pv88080_regulator_info[i].desc.vsel_mask
502 = regmap_config->buck_regmap[i].buck_vsel_mask;
504 ret = regmap_read(chip->regmap,
505 pv88080_regulator_info[i].conf2, &conf2);
508 conf2 = ((conf2 >> PV88080_BUCK_VDAC_RANGE_SHIFT) &
509 PV88080_BUCK_VDAC_RANGE_MASK);
511 ret = regmap_read(chip->regmap,
512 pv88080_regulator_info[i].conf5, &conf5);
515 conf5 = ((conf5 >> PV88080_BUCK_VRANGE_GAIN_SHIFT) &
516 PV88080_BUCK_VRANGE_GAIN_MASK);
518 pv88080_regulator_info[i].desc.min_uV =
519 pv88080_buck_vol[conf2].min_uV * (conf5+1);
520 pv88080_regulator_info[i].desc.uV_step =
521 pv88080_buck_vol[conf2].uV_step * (conf5+1);
522 pv88080_regulator_info[i].desc.n_voltages =
523 ((pv88080_buck_vol[conf2].max_uV * (conf5+1))
524 - (pv88080_regulator_info[i].desc.min_uV))
525 /(pv88080_regulator_info[i].desc.uV_step) + 1;
527 config.driver_data = (void *)&pv88080_regulator_info[i];
528 chip->rdev[i] = devm_regulator_register(chip->dev,
529 &pv88080_regulator_info[i].desc, &config);
530 if (IS_ERR(chip->rdev[i])) {
532 "Failed to register PV88080 regulator\n");
533 return PTR_ERR(chip->rdev[i]);
537 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.enable_reg
538 = regmap_config->hvbuck_enable_reg;
539 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.enable_mask
540 = regmap_config->hvbuck_enable_mask;
541 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.vsel_reg
542 = regmap_config->hvbuck_vsel_reg;
543 pv88080_regulator_info[PV88080_ID_HVBUCK].desc.vsel_mask
544 = regmap_config->hvbuck_vsel_mask;
546 /* Registeration for HVBUCK */
548 config.init_data = &init_data[PV88080_ID_HVBUCK];
550 config.driver_data = (void *)&pv88080_regulator_info[PV88080_ID_HVBUCK];
551 chip->rdev[PV88080_ID_HVBUCK] = devm_regulator_register(chip->dev,
552 &pv88080_regulator_info[PV88080_ID_HVBUCK].desc, &config);
553 if (IS_ERR(chip->rdev[PV88080_ID_HVBUCK])) {
554 dev_err(chip->dev, "Failed to register PV88080 regulator\n");
555 return PTR_ERR(chip->rdev[PV88080_ID_HVBUCK]);
561 static const struct i2c_device_id pv88080_i2c_id[] = {
562 { "pv88080", TYPE_PV88080_AA },
563 { "pv88080-aa", TYPE_PV88080_AA },
564 { "pv88080-ba", TYPE_PV88080_BA },
567 MODULE_DEVICE_TABLE(i2c, pv88080_i2c_id);
569 static struct i2c_driver pv88080_regulator_driver = {
572 .of_match_table = of_match_ptr(pv88080_dt_ids),
574 .probe = pv88080_i2c_probe,
575 .id_table = pv88080_i2c_id,
578 module_i2c_driver(pv88080_regulator_driver);
580 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
581 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88080");
582 MODULE_LICENSE("GPL");