1/*
2 * pv88080-regulator.c - Regulator device driver for PV88080
3 * Copyright (C) 2016 Powerventure Semiconductor Ltd.
4 *
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.
9 *
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.
14 */
15
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"
29
30#define PV88080_MAX_REGULATORS 4
31
32/* PV88080 REGULATOR IDs */
33enum {
34 /* BUCKs */
35 PV88080_ID_BUCK1,
36 PV88080_ID_BUCK2,
37 PV88080_ID_BUCK3,
38 PV88080_ID_HVBUCK,
39};
40
41enum pv88080_types {
42 TYPE_PV88080_AA,
43 TYPE_PV88080_BA,
44};
45
46struct pv88080_regulator {
47 struct regulator_desc desc;
48 unsigned int mode_reg;
49 unsigned int conf2;
50 unsigned int conf5;
51};
52
53struct 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;
59};
60
61struct pv88080_buck_voltage {
62 int min_uV;
63 int max_uV;
64 int uV_step;
65};
66
67struct 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;
79};
80
81struct 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;
89};
90
91static const struct regmap_config pv88080_regmap_config = {
92 .reg_bits = 8,
93 .val_bits = 8,
94};
95
96/* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
97 * Entry indexes corresponds to register values.
98 */
99
100static const unsigned int pv88080_buck1_limits[] = {
101 3230000, 5130000, 6960000, 8790000
102};
103
104static const unsigned int pv88080_buck23_limits[] = {
105 1496000, 2393000, 3291000, 4189000
106};
107
108static const struct pv88080_buck_voltage pv88080_buck_vol[2] = {
109 {
110 .min_uV = 600000,
111 .max_uV = 1393750,
112 .uV_step = 6250,
113 },
114 {
115 .min_uV = 1400000,
116 .max_uV = 2193750,
117 .uV_step = 6250,
118 },
119};
120
121static 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,
133 },
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,
145 },
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,
157 },
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,
163};
164
165static 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,
177 },
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,
189 },
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,
201 },
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,
207};
208
209#ifdef CONFIG_OF
210static 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 },
214 {},
215};
216MODULE_DEVICE_TABLE(of, pv88080_dt_ids);
217#endif
218
219static unsigned int pv88080_buck_get_mode(struct regulator_dev *rdev)
220{
221 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
222 unsigned int data;
223 int ret, mode = 0;
224
225 ret = regmap_read(rdev->regmap, info->mode_reg, &data);
226 if (ret < 0)
227 return ret;
228
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;
241 }
242
243 return mode;
244}
245
246static int pv88080_buck_set_mode(struct regulator_dev *rdev,
247 unsigned int mode)
248{
249 struct pv88080_regulator *info = rdev_get_drvdata(rdev);
250 int val = 0;
251
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;
264 }
265
266 return regmap_update_bits(rdev->regmap, info->mode_reg,
267 PV88080_BUCK1_MODE_MASK, val);
268}
269
270static 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,
281};
282
283static 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,
290};
291
292#define PV88080_BUCK(chip, regl_name, min, step, max, limits_array) \
293{\
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), \
307 },\
308}
309
310#define PV88080_HVBUCK(chip, regl_name, min, step, max) \
311{\
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, \
323 },\
324}
325
326static 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),
334};
335
336static irqreturn_t pv88080_irq_handler(int irq, void *data)
337{
338 struct pv88080 *chip = data;
339 int i, reg_val, err, ret = IRQ_NONE;
340
341 err = regmap_read(chip->regmap, PV88080_REG_EVENT_A, &reg_val);
342 if (err < 0)
343 goto error_i2c;
344
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,
350 NULL);
351 }
352 }
353
354 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
355 PV88080_E_VDD_FLT);
356 if (err < 0)
357 goto error_i2c;
358
359 ret = IRQ_HANDLED;
360 }
361
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,
367 NULL);
368 }
369 }
370
371 err = regmap_write(chip->regmap, PV88080_REG_EVENT_A,
372 PV88080_E_OVER_TEMP);
373 if (err < 0)
374 goto error_i2c;
375
376 ret = IRQ_HANDLED;
377 }
378
379 return ret;
380
381error_i2c:
382 dev_err(chip->dev, "I2C error : %d\n", err);
383 return IRQ_NONE;
384}
385
386/*
387 * I2C driver interface functions
388 */
389static int pv88080_i2c_probe(struct i2c_client *i2c,
390 const struct i2c_device_id *id)
391{
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 = { };
397 int i, error, ret;
398 unsigned int conf2, conf5;
399
400 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88080), GFP_KERNEL);
401 if (!chip)
402 return -ENOMEM;
403
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",
409 error);
410 return error;
411 }
412
413 if (i2c->dev.of_node) {
414 match = of_match_node(pv88080_dt_ids, i2c->dev.of_node);
415 if (!match) {
416 dev_err(chip->dev, "Failed to get of_match_node\n");
417 return -EINVAL;
418 }
419 chip->type = (unsigned long)match->data;
420 } else {
421 chip->type = id->driver_data;
422 }
423
424 i2c_set_clientdata(i2c, chip);
425
426 if (i2c->irq != 0) {
427 ret = regmap_write(chip->regmap, PV88080_REG_MASK_A, 0xFF);
428 if (ret < 0) {
429 dev_err(chip->dev,
430 "Failed to mask A reg: %d\n", ret);
431 return ret;
432 }
433 ret = regmap_write(chip->regmap, PV88080_REG_MASK_B, 0xFF);
434 if (ret < 0) {
435 dev_err(chip->dev,
436 "Failed to mask B reg: %d\n", ret);
437 return ret;
438 }
439 ret = regmap_write(chip->regmap, PV88080_REG_MASK_C, 0xFF);
440 if (ret < 0) {
441 dev_err(chip->dev,
442 "Failed to mask C reg: %d\n", ret);
443 return ret;
444 }
445
446 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
447 pv88080_irq_handler,
448 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
449 "pv88080", chip);
450 if (ret != 0) {
451 dev_err(chip->dev, "Failed to request IRQ: %d\n",
452 i2c->irq);
453 return ret;
454 }
455
456 ret = regmap_update_bits(chip->regmap, PV88080_REG_MASK_A,
457 PV88080_M_VDD_FLT | PV88080_M_OVER_TEMP, 0);
458 if (ret < 0) {
459 dev_err(chip->dev,
460 "Failed to update mask reg: %d\n", ret);
461 return ret;
462 }
463 } else {
464 dev_warn(chip->dev, "No IRQ configured\n");
465 }
466
467 switch (chip->type) {
468 case TYPE_PV88080_AA:
469 chip->regmap_config = &pv88080_aa_regs;
470 break;
471 case TYPE_PV88080_BA:
472 chip->regmap_config = &pv88080_ba_regs;
473 break;
474 }
475
476 regmap_config = chip->regmap_config;
477 config.dev = chip->dev;
478 config.regmap = chip->regmap;
479
480 /* Registeration for BUCK1, 2, 3 */
481 for (i = 0; i < PV88080_MAX_REGULATORS-1; i++) {
482 if (init_data)
483 config.init_data = &init_data[i];
484
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;
503
504 ret = regmap_read(chip->regmap,
505 pv88080_regulator_info[i].conf2, &conf2);
506 if (ret < 0)
507 return ret;
508 conf2 = ((conf2 >> PV88080_BUCK_VDAC_RANGE_SHIFT) &
509 PV88080_BUCK_VDAC_RANGE_MASK);
510
511 ret = regmap_read(chip->regmap,
512 pv88080_regulator_info[i].conf5, &conf5);
513 if (ret < 0)
514 return ret;
515 conf5 = ((conf5 >> PV88080_BUCK_VRANGE_GAIN_SHIFT) &
516 PV88080_BUCK_VRANGE_GAIN_MASK);
517
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;
526
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])) {
531 dev_err(chip->dev,
532 "Failed to register PV88080 regulator\n");
533 return PTR_ERR(chip->rdev[i]);
534 }
535 }
536
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;
545
546 /* Registeration for HVBUCK */
547 if (init_data)
548 config.init_data = &init_data[PV88080_ID_HVBUCK];
549
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]);
556 }
557
558 return 0;
559}
560
561static 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 },
565 {},
566};
567MODULE_DEVICE_TABLE(i2c, pv88080_i2c_id);
568
569static struct i2c_driver pv88080_regulator_driver = {
570 .driver = {
571 .name = "pv88080",
572 .of_match_table = of_match_ptr(pv88080_dt_ids),
573 },
574 .probe = pv88080_i2c_probe,
575 .id_table = pv88080_i2c_id,
576};
577
578module_i2c_driver(pv88080_regulator_driver);
579
580MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
581MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88080");
582MODULE_LICENSE("GPL");
583