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/init.h>
20 #include <linux/slab.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regmap.h>
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/regulator/of_regulator.h>
27 #include "pv88080-regulator.h"
29 #define PV88080_MAX_REGULATORS 3
31 /* PV88080 REGULATOR IDs */
39 struct pv88080_regulator {
40 struct regulator_desc desc;
41 /* Current limiting */
42 unsigned int n_current_limits;
43 const int *current_limits;
44 unsigned int limit_mask;
52 struct regmap *regmap;
53 struct regulator_dev *rdev[PV88080_MAX_REGULATORS];
56 struct pv88080_buck_voltage {
62 static const struct regmap_config pv88080_regmap_config = {
67 /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
68 * Entry indexes corresponds to register values.
71 static const int pv88080_buck1_limits[] = {
72 3230000, 5130000, 6960000, 8790000
75 static const int pv88080_buck23_limits[] = {
76 1496000, 2393000, 3291000, 4189000
79 static const struct pv88080_buck_voltage pv88080_buck_vol[2] = {
92 static unsigned int pv88080_buck_get_mode(struct regulator_dev *rdev)
94 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
98 ret = regmap_read(rdev->regmap, info->conf, &data);
102 switch (data & PV88080_BUCK1_MODE_MASK) {
103 case PV88080_BUCK_MODE_SYNC:
104 mode = REGULATOR_MODE_FAST;
106 case PV88080_BUCK_MODE_AUTO:
107 mode = REGULATOR_MODE_NORMAL;
109 case PV88080_BUCK_MODE_SLEEP:
110 mode = REGULATOR_MODE_STANDBY;
119 static int pv88080_buck_set_mode(struct regulator_dev *rdev,
122 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
126 case REGULATOR_MODE_FAST:
127 val = PV88080_BUCK_MODE_SYNC;
129 case REGULATOR_MODE_NORMAL:
130 val = PV88080_BUCK_MODE_AUTO;
132 case REGULATOR_MODE_STANDBY:
133 val = PV88080_BUCK_MODE_SLEEP;
139 return regmap_update_bits(rdev->regmap, info->conf,
140 PV88080_BUCK1_MODE_MASK, val);
143 static int pv88080_set_current_limit(struct regulator_dev *rdev, int min,
146 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
149 /* search for closest to maximum */
150 for (i = info->n_current_limits; i >= 0; i--) {
151 if (min <= info->current_limits[i]
152 && max >= info->current_limits[i]) {
153 return regmap_update_bits(rdev->regmap,
156 i << PV88080_BUCK1_ILIM_SHIFT);
163 static int pv88080_get_current_limit(struct regulator_dev *rdev)
165 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
169 ret = regmap_read(rdev->regmap, info->conf, &data);
173 data = (data & info->limit_mask) >> PV88080_BUCK1_ILIM_SHIFT;
174 return info->current_limits[data];
177 static struct regulator_ops pv88080_buck_ops = {
178 .get_mode = pv88080_buck_get_mode,
179 .set_mode = pv88080_buck_set_mode,
180 .enable = regulator_enable_regmap,
181 .disable = regulator_disable_regmap,
182 .is_enabled = regulator_is_enabled_regmap,
183 .set_voltage_sel = regulator_set_voltage_sel_regmap,
184 .get_voltage_sel = regulator_get_voltage_sel_regmap,
185 .list_voltage = regulator_list_voltage_linear,
186 .set_current_limit = pv88080_set_current_limit,
187 .get_current_limit = pv88080_get_current_limit,
190 #define PV88080_BUCK(chip, regl_name, min, step, max, limits_array) \
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 = &pv88080_buck_ops,\
202 .n_voltages = ((max) - (min))/(step) + 1, \
203 .enable_reg = PV88080_REG_##regl_name##_CONF0, \
204 .enable_mask = PV88080_##regl_name##_EN, \
205 .vsel_reg = PV88080_REG_##regl_name##_CONF0, \
206 .vsel_mask = PV88080_V##regl_name##_MASK, \
208 .current_limits = limits_array, \
209 .n_current_limits = ARRAY_SIZE(limits_array), \
210 .limit_mask = PV88080_##regl_name##_ILIM_MASK, \
211 .conf = PV88080_REG_##regl_name##_CONF1, \
212 .conf2 = PV88080_REG_##regl_name##_CONF2, \
213 .conf5 = PV88080_REG_##regl_name##_CONF5, \
216 static struct pv88080_regulator pv88080_regulator_info[] = {
217 PV88080_BUCK(PV88080, BUCK1, 600000, 6250, 1393750,
218 pv88080_buck1_limits),
219 PV88080_BUCK(PV88080, BUCK2, 600000, 6250, 1393750,
220 pv88080_buck23_limits),
221 PV88080_BUCK(PV88080, BUCK3, 600000, 6250, 1393750,
222 pv88080_buck23_limits),
225 static irqreturn_t pv88080_irq_handler(int irq, void *data)
227 struct pv88080 *chip = data;
228 int i, reg_val, err, ret = IRQ_NONE;
230 err = regmap_read(chip->regmap, PV88080_REG_EVENT_A, ®_val);
234 if (reg_val & PV88080_E_VDD_FLT) {
235 for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
236 if (chip->rdev[i] != NULL) {
237 regulator_notifier_call_chain(chip->rdev[i],
238 REGULATOR_EVENT_UNDER_VOLTAGE,
243 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
251 if (reg_val & PV88080_E_OVER_TEMP) {
252 for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
253 if (chip->rdev[i] != NULL) {
254 regulator_notifier_call_chain(chip->rdev[i],
255 REGULATOR_EVENT_OVER_TEMP,
260 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
261 PV88080_E_OVER_TEMP);
271 dev_err(chip->dev, "I2C error : %d\n", err);
276 * I2C driver interface functions
278 static int pv88080_i2c_probe(struct i2c_client *i2c,
279 const struct i2c_device_id *id)
281 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
282 struct pv88080 *chip;
283 struct regulator_config config = { };
285 unsigned int conf2, conf5;
287 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88080), GFP_KERNEL);
291 chip->dev = &i2c->dev;
292 chip->regmap = devm_regmap_init_i2c(i2c, &pv88080_regmap_config);
293 if (IS_ERR(chip->regmap)) {
294 error = PTR_ERR(chip->regmap);
295 dev_err(chip->dev, "Failed to allocate register map: %d\n",
300 i2c_set_clientdata(i2c, chip);
303 ret = regmap_write(chip->regmap, PV88080_REG_MASK_A, 0xFF);
306 "Failed to mask A reg: %d\n", ret);
309 ret = regmap_write(chip->regmap, PV88080_REG_MASK_B, 0xFF);
312 "Failed to mask B reg: %d\n", ret);
315 ret = regmap_write(chip->regmap, PV88080_REG_MASK_C, 0xFF);
318 "Failed to mask C reg: %d\n", ret);
322 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
324 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
327 dev_err(chip->dev, "Failed to request IRQ: %d\n",
332 ret = regmap_update_bits(chip->regmap, PV88080_REG_MASK_A,
333 PV88080_M_VDD_FLT | PV88080_M_OVER_TEMP, 0);
336 "Failed to update mask reg: %d\n", ret);
341 dev_warn(chip->dev, "No IRQ configured\n");
344 config.dev = chip->dev;
345 config.regmap = chip->regmap;
347 for (i = 0; i < PV88080_MAX_REGULATORS; i++) {
349 config.init_data = &init_data[i];
351 ret = regmap_read(chip->regmap,
352 pv88080_regulator_info[i].conf2, &conf2);
356 conf2 = ((conf2 >> PV88080_BUCK_VDAC_RANGE_SHIFT) &
357 PV88080_BUCK_VDAC_RANGE_MASK);
359 ret = regmap_read(chip->regmap,
360 pv88080_regulator_info[i].conf5, &conf5);
364 conf5 = ((conf5 >> PV88080_BUCK_VRANGE_GAIN_SHIFT) &
365 PV88080_BUCK_VRANGE_GAIN_MASK);
367 pv88080_regulator_info[i].desc.min_uV =
368 pv88080_buck_vol[conf2].min_uV * (conf5+1);
369 pv88080_regulator_info[i].desc.uV_step =
370 pv88080_buck_vol[conf2].uV_step * (conf5+1);
371 pv88080_regulator_info[i].desc.n_voltages =
372 ((pv88080_buck_vol[conf2].max_uV * (conf5+1))
373 - (pv88080_regulator_info[i].desc.min_uV))
374 /(pv88080_regulator_info[i].desc.uV_step) + 1;
376 config.driver_data = (void *)&pv88080_regulator_info[i];
377 chip->rdev[i] = devm_regulator_register(chip->dev,
378 &pv88080_regulator_info[i].desc, &config);
379 if (IS_ERR(chip->rdev[i])) {
381 "Failed to register PV88080 regulator\n");
382 return PTR_ERR(chip->rdev[i]);
389 static const struct i2c_device_id pv88080_i2c_id[] = {
393 MODULE_DEVICE_TABLE(i2c, pv88080_i2c_id);
396 static const struct of_device_id pv88080_dt_ids[] = {
397 { .compatible = "pvs,pv88080", .data = &pv88080_i2c_id[0] },
400 MODULE_DEVICE_TABLE(of, pv88080_dt_ids);
403 static struct i2c_driver pv88080_regulator_driver = {
406 .of_match_table = of_match_ptr(pv88080_dt_ids),
408 .probe = pv88080_i2c_probe,
409 .id_table = pv88080_i2c_id,
412 module_i2c_driver(pv88080_regulator_driver);
414 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
415 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88080");
416 MODULE_LICENSE("GPL");