2 * pv88090-regulator.c - Regulator device driver for PV88090
3 * Copyright (C) 2015 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/gpio.h>
18 #include <linux/i2c.h>
19 #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 <linux/proc_fs.h>
29 #include <linux/uaccess.h>
30 #include "pv88090-regulator.h"
32 #define PV88090_MAX_REGULATORS 5
34 /* PV88090 REGULATOR IDs */
46 struct pv88090_regulator
{
47 struct regulator_desc desc
;
48 /* Current limiting */
49 unsigned n_current_limits
;
50 const int *current_limits
;
51 unsigned int limit_mask
;
58 struct regmap
*regmap
;
59 struct regulator_dev
*rdev
[PV88090_MAX_REGULATORS
];
62 struct pv88090_buck_voltage
{
68 static const struct regmap_config pv88090_regmap_config
= {
73 /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
74 * Entry indexes corresponds to register values.
77 static const int pv88090_buck1_limits
[] = {
78 220000, 440000, 660000, 880000, 1100000, 1320000, 1540000, 1760000,
79 1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
80 3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
81 5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
84 static const int pv88090_buck23_limits
[] = {
85 1496000, 2393000, 3291000, 4189000
88 static const struct pv88090_buck_voltage pv88090_buck_vol
[3] = {
107 static unsigned int pv88090_buck_get_mode(struct regulator_dev
*rdev
)
109 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
113 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
117 switch (data
& PV88090_BUCK1_MODE_MASK
) {
118 case PV88090_BUCK_MODE_SYNC
:
119 mode
= REGULATOR_MODE_FAST
;
121 case PV88090_BUCK_MODE_AUTO
:
122 mode
= REGULATOR_MODE_NORMAL
;
124 case PV88090_BUCK_MODE_SLEEP
:
125 mode
= REGULATOR_MODE_STANDBY
;
132 static int pv88090_buck_set_mode(struct regulator_dev
*rdev
,
135 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
139 case REGULATOR_MODE_FAST
:
140 val
= PV88090_BUCK_MODE_SYNC
;
142 case REGULATOR_MODE_NORMAL
:
143 val
= PV88090_BUCK_MODE_AUTO
;
145 case REGULATOR_MODE_STANDBY
:
146 val
= PV88090_BUCK_MODE_SLEEP
;
152 return regmap_update_bits(rdev
->regmap
, info
->conf
,
153 PV88090_BUCK1_MODE_MASK
, val
);
156 static int pv88090_set_current_limit(struct regulator_dev
*rdev
, int min
,
159 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
162 /* search for closest to maximum */
163 for (i
= info
->n_current_limits
; i
>= 0; i
--) {
164 if (min
<= info
->current_limits
[i
]
165 && max
>= info
->current_limits
[i
]) {
166 return regmap_update_bits(rdev
->regmap
,
169 i
<< PV88090_BUCK1_ILIM_SHIFT
);
176 static int pv88090_get_current_limit(struct regulator_dev
*rdev
)
178 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
182 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
186 data
= (data
& info
->limit_mask
) >> PV88090_BUCK1_ILIM_SHIFT
;
187 return info
->current_limits
[data
];
190 static struct regulator_ops pv88090_buck_ops
= {
191 .get_mode
= pv88090_buck_get_mode
,
192 .set_mode
= pv88090_buck_set_mode
,
193 .enable
= regulator_enable_regmap
,
194 .disable
= regulator_disable_regmap
,
195 .is_enabled
= regulator_is_enabled_regmap
,
196 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
197 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
198 .list_voltage
= regulator_list_voltage_linear
,
199 .set_current_limit
= pv88090_set_current_limit
,
200 .get_current_limit
= pv88090_get_current_limit
,
203 static struct regulator_ops pv88090_ldo_ops
= {
204 .enable
= regulator_enable_regmap
,
205 .disable
= regulator_disable_regmap
,
206 .is_enabled
= regulator_is_enabled_regmap
,
207 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
208 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
209 .list_voltage
= regulator_list_voltage_linear
,
212 #define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
215 .id = chip##_ID_##regl_name,\
216 .name = __stringify(chip##_##regl_name),\
217 .of_match = of_match_ptr(#regl_name),\
218 .regulators_node = of_match_ptr("regulators"),\
219 .type = REGULATOR_VOLTAGE,\
220 .owner = THIS_MODULE,\
221 .ops = &pv88090_buck_ops,\
224 .n_voltages = ((max) - (min))/(step) + 1, \
225 .enable_reg = PV88090_REG_##regl_name##_CONF0, \
226 .enable_mask = PV88090_##regl_name##_EN, \
227 .vsel_reg = PV88090_REG_##regl_name##_CONF0, \
228 .vsel_mask = PV88090_V##regl_name##_MASK, \
230 .current_limits = limits_array, \
231 .n_current_limits = ARRAY_SIZE(limits_array), \
232 .limit_mask = PV88090_##regl_name##_ILIM_MASK, \
233 .conf = PV88090_REG_##regl_name##_CONF1, \
234 .conf2 = PV88090_REG_##regl_name##_CONF2, \
237 #define PV88090_LDO(chip, regl_name, min, step, max) \
240 .id = chip##_ID_##regl_name,\
241 .name = __stringify(chip##_##regl_name),\
242 .of_match = of_match_ptr(#regl_name),\
243 .regulators_node = of_match_ptr("regulators"),\
244 .type = REGULATOR_VOLTAGE,\
245 .owner = THIS_MODULE,\
246 .ops = &pv88090_ldo_ops,\
249 .n_voltages = ((max) - (min))/(step) + 1, \
250 .enable_reg = PV88090_REG_##regl_name##_CONT, \
251 .enable_mask = PV88090_##regl_name##_EN, \
252 .vsel_reg = PV88090_REG_##regl_name##_CONT, \
253 .vsel_mask = PV88090_V##regl_name##_MASK, \
257 static struct pv88090_regulator pv88090_regulator_info
[] = {
258 PV88090_BUCK(PV88090
, BUCK1
, 600000, 6250, 1393750,
259 pv88090_buck1_limits
),
260 PV88090_BUCK(PV88090
, BUCK2
, 600000, 6250, 1393750,
261 pv88090_buck23_limits
),
262 PV88090_BUCK(PV88090
, BUCK3
, 600000, 6250, 1393750,
263 pv88090_buck23_limits
),
264 PV88090_LDO(PV88090
, LDO1
, 1200000, 50000, 4350000),
265 PV88090_LDO(PV88090
, LDO2
, 650000, 25000, 2225000),
268 static irqreturn_t
pv88090_irq_handler(int irq
, void *data
)
270 struct pv88090
*chip
= data
;
271 int i
, reg_val
, err
, ret
= IRQ_NONE
;
273 err
= regmap_read(chip
->regmap
, PV88090_REG_EVENT_A
, ®_val
);
277 if (reg_val
& PV88090_E_VDD_FLT
) {
278 for (i
= 0; i
< PV88090_MAX_REGULATORS
; i
++) {
279 if (chip
->rdev
[i
] != NULL
) {
280 regulator_notifier_call_chain(chip
->rdev
[i
],
281 REGULATOR_EVENT_UNDER_VOLTAGE
,
286 err
= regmap_update_bits(chip
->regmap
, PV88090_REG_EVENT_A
,
287 PV88090_E_VDD_FLT
, PV88090_E_VDD_FLT
);
294 if (reg_val
& PV88090_E_OVER_TEMP
) {
295 for (i
= 0; i
< PV88090_MAX_REGULATORS
; i
++) {
296 if (chip
->rdev
[i
] != NULL
) {
297 regulator_notifier_call_chain(chip
->rdev
[i
],
298 REGULATOR_EVENT_OVER_TEMP
,
303 err
= regmap_update_bits(chip
->regmap
, PV88090_REG_EVENT_A
,
304 PV88090_E_OVER_TEMP
, PV88090_E_OVER_TEMP
);
314 dev_err(chip
->dev
, "I2C error : %d\n", err
);
319 * I2C driver interface functions
321 static int pv88090_i2c_probe(struct i2c_client
*i2c
,
322 const struct i2c_device_id
*id
)
324 struct regulator_init_data
*init_data
= dev_get_platdata(&i2c
->dev
);
325 struct pv88090
*chip
;
326 struct regulator_config config
= { };
327 int error
, i
, ret
= 0;
328 unsigned int conf2
, range
, index
;
330 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct pv88090
), GFP_KERNEL
);
334 chip
->dev
= &i2c
->dev
;
335 chip
->regmap
= devm_regmap_init_i2c(i2c
, &pv88090_regmap_config
);
336 if (IS_ERR(chip
->regmap
)) {
337 error
= PTR_ERR(chip
->regmap
);
338 dev_err(chip
->dev
, "Failed to allocate register map: %d\n",
343 i2c_set_clientdata(i2c
, chip
);
346 ret
= regmap_write(chip
->regmap
, PV88090_REG_MASK_A
, 0xFF);
349 "Failed to mask A reg: %d\n", ret
);
353 ret
= regmap_write(chip
->regmap
, PV88090_REG_MASK_B
, 0xFF);
356 "Failed to mask B reg: %d\n", ret
);
360 ret
= devm_request_threaded_irq(&i2c
->dev
, i2c
->irq
, NULL
,
362 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
365 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
370 ret
= regmap_update_bits(chip
->regmap
, PV88090_REG_MASK_A
,
371 PV88090_M_VDD_FLT
| PV88090_M_OVER_TEMP
, 0);
374 "Failed to update mask reg: %d\n", ret
);
379 dev_warn(chip
->dev
, "No IRQ configured\n");
382 config
.dev
= chip
->dev
;
383 config
.regmap
= chip
->regmap
;
385 for (i
= 0; i
< PV88090_MAX_REGULATORS
; i
++) {
387 config
.init_data
= &init_data
[i
];
389 if (i
== PV88090_ID_BUCK2
|| i
== PV88090_ID_BUCK3
) {
390 ret
= regmap_read(chip
->regmap
,
391 pv88090_regulator_info
[i
].conf2
, &conf2
);
395 conf2
= (conf2
>> PV88090_BUCK_VDAC_RANGE_SHIFT
) &
396 PV88090_BUCK_VDAC_RANGE_MASK
;
398 ret
= regmap_read(chip
->regmap
,
399 PV88090_REG_BUCK_FOLD_RANGE
, &range
);
404 (PV88080_BUCK_VRANGE_GAIN_SHIFT
+ i
- 1)) &
405 PV88080_BUCK_VRANGE_GAIN_MASK
;
406 index
= ((range
<< 1) | conf2
);
408 pv88090_regulator_info
[i
].desc
.min_uV
409 = pv88090_buck_vol
[index
].min_uV
;
410 pv88090_regulator_info
[i
].desc
.uV_step
411 = pv88090_buck_vol
[index
].uV_step
;
412 pv88090_regulator_info
[i
].desc
.n_voltages
413 = ((pv88090_buck_vol
[index
].max_uV
)
414 - (pv88090_buck_vol
[index
].min_uV
))
415 /(pv88090_buck_vol
[index
].uV_step
) + 1;
418 config
.driver_data
= (void *)&pv88090_regulator_info
[i
];
419 chip
->rdev
[i
] = devm_regulator_register(chip
->dev
,
420 &pv88090_regulator_info
[i
].desc
, &config
);
421 if (IS_ERR(chip
->rdev
[i
])) {
423 "Failed to register PV88090 regulator\n");
424 return PTR_ERR(chip
->rdev
[i
]);
431 static const struct i2c_device_id pv88090_i2c_id
[] = {
435 MODULE_DEVICE_TABLE(i2c
, pv88090_i2c_id
);
438 static const struct of_device_id pv88090_dt_ids
[] = {
439 { .compatible
= "pvs,pv88090", .data
= &pv88090_i2c_id
[0] },
442 MODULE_DEVICE_TABLE(of
, pv88090_dt_ids
);
445 static struct i2c_driver pv88090_regulator_driver
= {
448 .of_match_table
= of_match_ptr(pv88090_dt_ids
),
450 .probe
= pv88090_i2c_probe
,
451 .id_table
= pv88090_i2c_id
,
454 module_i2c_driver(pv88090_regulator_driver
);
456 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
457 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
458 MODULE_LICENSE("GPL");