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/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 "pv88090-regulator.h"
29 #define PV88090_MAX_REGULATORS 5
31 /* PV88090 REGULATOR IDs */
43 struct pv88090_regulator
{
44 struct regulator_desc desc
;
45 /* Current limiting */
46 unsigned n_current_limits
;
47 const int *current_limits
;
48 unsigned int limit_mask
;
55 struct regmap
*regmap
;
56 struct regulator_dev
*rdev
[PV88090_MAX_REGULATORS
];
59 struct pv88090_buck_voltage
{
65 static const struct regmap_config pv88090_regmap_config
= {
70 /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
71 * Entry indexes corresponds to register values.
74 static const int pv88090_buck1_limits
[] = {
75 220000, 440000, 660000, 880000, 1100000, 1320000, 1540000, 1760000,
76 1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
77 3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
78 5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
81 static const int pv88090_buck23_limits
[] = {
82 1496000, 2393000, 3291000, 4189000
85 static const struct pv88090_buck_voltage pv88090_buck_vol
[3] = {
104 static unsigned int pv88090_buck_get_mode(struct regulator_dev
*rdev
)
106 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
110 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
114 switch (data
& PV88090_BUCK1_MODE_MASK
) {
115 case PV88090_BUCK_MODE_SYNC
:
116 mode
= REGULATOR_MODE_FAST
;
118 case PV88090_BUCK_MODE_AUTO
:
119 mode
= REGULATOR_MODE_NORMAL
;
121 case PV88090_BUCK_MODE_SLEEP
:
122 mode
= REGULATOR_MODE_STANDBY
;
129 static int pv88090_buck_set_mode(struct regulator_dev
*rdev
,
132 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
136 case REGULATOR_MODE_FAST
:
137 val
= PV88090_BUCK_MODE_SYNC
;
139 case REGULATOR_MODE_NORMAL
:
140 val
= PV88090_BUCK_MODE_AUTO
;
142 case REGULATOR_MODE_STANDBY
:
143 val
= PV88090_BUCK_MODE_SLEEP
;
149 return regmap_update_bits(rdev
->regmap
, info
->conf
,
150 PV88090_BUCK1_MODE_MASK
, val
);
153 static int pv88090_set_current_limit(struct regulator_dev
*rdev
, int min
,
156 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
159 /* search for closest to maximum */
160 for (i
= info
->n_current_limits
; i
>= 0; i
--) {
161 if (min
<= info
->current_limits
[i
]
162 && max
>= info
->current_limits
[i
]) {
163 return regmap_update_bits(rdev
->regmap
,
166 i
<< PV88090_BUCK1_ILIM_SHIFT
);
173 static int pv88090_get_current_limit(struct regulator_dev
*rdev
)
175 struct pv88090_regulator
*info
= rdev_get_drvdata(rdev
);
179 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
183 data
= (data
& info
->limit_mask
) >> PV88090_BUCK1_ILIM_SHIFT
;
184 return info
->current_limits
[data
];
187 static struct regulator_ops pv88090_buck_ops
= {
188 .get_mode
= pv88090_buck_get_mode
,
189 .set_mode
= pv88090_buck_set_mode
,
190 .enable
= regulator_enable_regmap
,
191 .disable
= regulator_disable_regmap
,
192 .is_enabled
= regulator_is_enabled_regmap
,
193 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
194 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
195 .list_voltage
= regulator_list_voltage_linear
,
196 .set_current_limit
= pv88090_set_current_limit
,
197 .get_current_limit
= pv88090_get_current_limit
,
200 static struct regulator_ops pv88090_ldo_ops
= {
201 .enable
= regulator_enable_regmap
,
202 .disable
= regulator_disable_regmap
,
203 .is_enabled
= regulator_is_enabled_regmap
,
204 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
205 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
206 .list_voltage
= regulator_list_voltage_linear
,
209 #define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
212 .id = chip##_ID_##regl_name,\
213 .name = __stringify(chip##_##regl_name),\
214 .of_match = of_match_ptr(#regl_name),\
215 .regulators_node = of_match_ptr("regulators"),\
216 .type = REGULATOR_VOLTAGE,\
217 .owner = THIS_MODULE,\
218 .ops = &pv88090_buck_ops,\
221 .n_voltages = ((max) - (min))/(step) + 1, \
222 .enable_reg = PV88090_REG_##regl_name##_CONF0, \
223 .enable_mask = PV88090_##regl_name##_EN, \
224 .vsel_reg = PV88090_REG_##regl_name##_CONF0, \
225 .vsel_mask = PV88090_V##regl_name##_MASK, \
227 .current_limits = limits_array, \
228 .n_current_limits = ARRAY_SIZE(limits_array), \
229 .limit_mask = PV88090_##regl_name##_ILIM_MASK, \
230 .conf = PV88090_REG_##regl_name##_CONF1, \
231 .conf2 = PV88090_REG_##regl_name##_CONF2, \
234 #define PV88090_LDO(chip, regl_name, min, step, max) \
237 .id = chip##_ID_##regl_name,\
238 .name = __stringify(chip##_##regl_name),\
239 .of_match = of_match_ptr(#regl_name),\
240 .regulators_node = of_match_ptr("regulators"),\
241 .type = REGULATOR_VOLTAGE,\
242 .owner = THIS_MODULE,\
243 .ops = &pv88090_ldo_ops,\
246 .n_voltages = ((max) - (min))/(step) + 1, \
247 .enable_reg = PV88090_REG_##regl_name##_CONT, \
248 .enable_mask = PV88090_##regl_name##_EN, \
249 .vsel_reg = PV88090_REG_##regl_name##_CONT, \
250 .vsel_mask = PV88090_V##regl_name##_MASK, \
254 static struct pv88090_regulator pv88090_regulator_info
[] = {
255 PV88090_BUCK(PV88090
, BUCK1
, 600000, 6250, 1393750,
256 pv88090_buck1_limits
),
257 PV88090_BUCK(PV88090
, BUCK2
, 600000, 6250, 1393750,
258 pv88090_buck23_limits
),
259 PV88090_BUCK(PV88090
, BUCK3
, 600000, 6250, 1393750,
260 pv88090_buck23_limits
),
261 PV88090_LDO(PV88090
, LDO1
, 1200000, 50000, 4350000),
262 PV88090_LDO(PV88090
, LDO2
, 650000, 25000, 2225000),
265 static irqreturn_t
pv88090_irq_handler(int irq
, void *data
)
267 struct pv88090
*chip
= data
;
268 int i
, reg_val
, err
, ret
= IRQ_NONE
;
270 err
= regmap_read(chip
->regmap
, PV88090_REG_EVENT_A
, ®_val
);
274 if (reg_val
& PV88090_E_VDD_FLT
) {
275 for (i
= 0; i
< PV88090_MAX_REGULATORS
; i
++) {
276 if (chip
->rdev
[i
] != NULL
) {
277 regulator_notifier_call_chain(chip
->rdev
[i
],
278 REGULATOR_EVENT_UNDER_VOLTAGE
,
283 err
= regmap_write(chip
->regmap
, PV88090_REG_EVENT_A
,
291 if (reg_val
& PV88090_E_OVER_TEMP
) {
292 for (i
= 0; i
< PV88090_MAX_REGULATORS
; i
++) {
293 if (chip
->rdev
[i
] != NULL
) {
294 regulator_notifier_call_chain(chip
->rdev
[i
],
295 REGULATOR_EVENT_OVER_TEMP
,
300 err
= regmap_write(chip
->regmap
, PV88090_REG_EVENT_A
,
301 PV88090_E_OVER_TEMP
);
311 dev_err(chip
->dev
, "I2C error : %d\n", err
);
316 * I2C driver interface functions
318 static int pv88090_i2c_probe(struct i2c_client
*i2c
,
319 const struct i2c_device_id
*id
)
321 struct regulator_init_data
*init_data
= dev_get_platdata(&i2c
->dev
);
322 struct pv88090
*chip
;
323 struct regulator_config config
= { };
324 int error
, i
, ret
= 0;
325 unsigned int conf2
, range
, index
;
327 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct pv88090
), GFP_KERNEL
);
331 chip
->dev
= &i2c
->dev
;
332 chip
->regmap
= devm_regmap_init_i2c(i2c
, &pv88090_regmap_config
);
333 if (IS_ERR(chip
->regmap
)) {
334 error
= PTR_ERR(chip
->regmap
);
335 dev_err(chip
->dev
, "Failed to allocate register map: %d\n",
340 i2c_set_clientdata(i2c
, chip
);
343 ret
= regmap_write(chip
->regmap
, PV88090_REG_MASK_A
, 0xFF);
346 "Failed to mask A reg: %d\n", ret
);
350 ret
= regmap_write(chip
->regmap
, PV88090_REG_MASK_B
, 0xFF);
353 "Failed to mask B reg: %d\n", ret
);
357 ret
= devm_request_threaded_irq(&i2c
->dev
, i2c
->irq
, NULL
,
359 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
362 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
367 ret
= regmap_update_bits(chip
->regmap
, PV88090_REG_MASK_A
,
368 PV88090_M_VDD_FLT
| PV88090_M_OVER_TEMP
, 0);
371 "Failed to update mask reg: %d\n", ret
);
376 dev_warn(chip
->dev
, "No IRQ configured\n");
379 config
.dev
= chip
->dev
;
380 config
.regmap
= chip
->regmap
;
382 for (i
= 0; i
< PV88090_MAX_REGULATORS
; i
++) {
384 config
.init_data
= &init_data
[i
];
386 if (i
== PV88090_ID_BUCK2
|| i
== PV88090_ID_BUCK3
) {
387 ret
= regmap_read(chip
->regmap
,
388 pv88090_regulator_info
[i
].conf2
, &conf2
);
392 conf2
= (conf2
>> PV88090_BUCK_VDAC_RANGE_SHIFT
) &
393 PV88090_BUCK_VDAC_RANGE_MASK
;
395 ret
= regmap_read(chip
->regmap
,
396 PV88090_REG_BUCK_FOLD_RANGE
, &range
);
401 (PV88080_BUCK_VRANGE_GAIN_SHIFT
+ i
- 1)) &
402 PV88080_BUCK_VRANGE_GAIN_MASK
;
403 index
= ((range
<< 1) | conf2
);
405 pv88090_regulator_info
[i
].desc
.min_uV
406 = pv88090_buck_vol
[index
].min_uV
;
407 pv88090_regulator_info
[i
].desc
.uV_step
408 = pv88090_buck_vol
[index
].uV_step
;
409 pv88090_regulator_info
[i
].desc
.n_voltages
410 = ((pv88090_buck_vol
[index
].max_uV
)
411 - (pv88090_buck_vol
[index
].min_uV
))
412 /(pv88090_buck_vol
[index
].uV_step
) + 1;
415 config
.driver_data
= (void *)&pv88090_regulator_info
[i
];
416 chip
->rdev
[i
] = devm_regulator_register(chip
->dev
,
417 &pv88090_regulator_info
[i
].desc
, &config
);
418 if (IS_ERR(chip
->rdev
[i
])) {
420 "Failed to register PV88090 regulator\n");
421 return PTR_ERR(chip
->rdev
[i
]);
428 static const struct i2c_device_id pv88090_i2c_id
[] = {
432 MODULE_DEVICE_TABLE(i2c
, pv88090_i2c_id
);
435 static const struct of_device_id pv88090_dt_ids
[] = {
436 { .compatible
= "pvs,pv88090", .data
= &pv88090_i2c_id
[0] },
439 MODULE_DEVICE_TABLE(of
, pv88090_dt_ids
);
442 static struct i2c_driver pv88090_regulator_driver
= {
445 .of_match_table
= of_match_ptr(pv88090_dt_ids
),
447 .probe
= pv88090_i2c_probe
,
448 .id_table
= pv88090_i2c_id
,
451 module_i2c_driver(pv88090_regulator_driver
);
453 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
454 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
455 MODULE_LICENSE("GPL");