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_lock(chip
->rdev
[i
]);
349 regulator_notifier_call_chain(chip
->rdev
[i
],
350 REGULATOR_EVENT_UNDER_VOLTAGE
,
352 regulator_unlock(chip
->rdev
[i
]);
356 err
= regmap_write(chip
->regmap
, PV88080_REG_EVENT_A
,
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
,
371 regulator_unlock(chip
->rdev
[i
]);
375 err
= regmap_write(chip
->regmap
, PV88080_REG_EVENT_A
,
376 PV88080_E_OVER_TEMP
);
386 dev_err(chip
->dev
, "I2C error : %d\n", err
);
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
= { };
402 unsigned int conf2
, conf5
;
404 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct pv88080
), GFP_KERNEL
);
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",
417 if (i2c
->dev
.of_node
) {
418 match
= of_match_node(pv88080_dt_ids
, i2c
->dev
.of_node
);
420 dev_err(chip
->dev
, "Failed to get of_match_node\n");
423 chip
->type
= (unsigned long)match
->data
;
425 chip
->type
= id
->driver_data
;
428 i2c_set_clientdata(i2c
, chip
);
431 ret
= regmap_write(chip
->regmap
, PV88080_REG_MASK_A
, 0xFF);
434 "Failed to mask A reg: %d\n", ret
);
437 ret
= regmap_write(chip
->regmap
, PV88080_REG_MASK_B
, 0xFF);
440 "Failed to mask B reg: %d\n", ret
);
443 ret
= regmap_write(chip
->regmap
, PV88080_REG_MASK_C
, 0xFF);
446 "Failed to mask C reg: %d\n", ret
);
450 ret
= devm_request_threaded_irq(&i2c
->dev
, i2c
->irq
, NULL
,
452 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
455 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
460 ret
= regmap_update_bits(chip
->regmap
, PV88080_REG_MASK_A
,
461 PV88080_M_VDD_FLT
| PV88080_M_OVER_TEMP
, 0);
464 "Failed to update mask reg: %d\n", ret
);
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
;
475 case TYPE_PV88080_BA
:
476 chip
->regmap_config
= &pv88080_ba_regs
;
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
++) {
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
);
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
);
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
])) {
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 */
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
]);
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
= {
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");