2 * pv88060-regulator.c - Regulator device driver for PV88060
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 "pv88060-regulator.h"
32 #define PV88060_MAX_REGULATORS 14
34 /* PV88060 REGULATOR IDs */
57 struct pv88060_regulator
{
58 struct regulator_desc desc
;
59 /* Current limiting */
60 unsigned n_current_limits
;
61 const int *current_limits
;
62 unsigned int limit_mask
;
63 unsigned int conf
; /* buck configuration register */
68 struct regmap
*regmap
;
69 struct regulator_dev
*rdev
[PV88060_MAX_REGULATORS
];
72 static const struct regmap_config pv88060_regmap_config
= {
77 /* Current limits array (in uA) for BUCK1
78 * Entry indexes corresponds to register values.
81 static const int pv88060_buck1_limits
[] = {
82 1496000, 2393000, 3291000, 4189000
85 static unsigned int pv88060_buck_get_mode(struct regulator_dev
*rdev
)
87 struct pv88060_regulator
*info
= rdev_get_drvdata(rdev
);
91 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
95 switch (data
& PV88060_BUCK_MODE_MASK
) {
96 case PV88060_BUCK_MODE_SYNC
:
97 mode
= REGULATOR_MODE_FAST
;
99 case PV88060_BUCK_MODE_AUTO
:
100 mode
= REGULATOR_MODE_NORMAL
;
102 case PV88060_BUCK_MODE_SLEEP
:
103 mode
= REGULATOR_MODE_STANDBY
;
110 static int pv88060_buck_set_mode(struct regulator_dev
*rdev
,
113 struct pv88060_regulator
*info
= rdev_get_drvdata(rdev
);
117 case REGULATOR_MODE_FAST
:
118 val
= PV88060_BUCK_MODE_SYNC
;
120 case REGULATOR_MODE_NORMAL
:
121 val
= PV88060_BUCK_MODE_AUTO
;
123 case REGULATOR_MODE_STANDBY
:
124 val
= PV88060_BUCK_MODE_SLEEP
;
130 return regmap_update_bits(rdev
->regmap
, info
->conf
,
131 PV88060_BUCK_MODE_MASK
, val
);
134 static int pv88060_set_current_limit(struct regulator_dev
*rdev
, int min
,
137 struct pv88060_regulator
*info
= rdev_get_drvdata(rdev
);
140 /* search for closest to maximum */
141 for (i
= info
->n_current_limits
; i
>= 0; i
--) {
142 if (min
<= info
->current_limits
[i
]
143 && max
>= info
->current_limits
[i
]) {
144 return regmap_update_bits(rdev
->regmap
,
147 i
<< PV88060_BUCK_ILIM_SHIFT
);
154 static int pv88060_get_current_limit(struct regulator_dev
*rdev
)
156 struct pv88060_regulator
*info
= rdev_get_drvdata(rdev
);
160 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
164 data
= (data
& info
->limit_mask
) >> PV88060_BUCK_ILIM_SHIFT
;
165 return info
->current_limits
[data
];
168 static struct regulator_ops pv88060_buck_ops
= {
169 .get_mode
= pv88060_buck_get_mode
,
170 .set_mode
= pv88060_buck_set_mode
,
171 .enable
= regulator_enable_regmap
,
172 .disable
= regulator_disable_regmap
,
173 .is_enabled
= regulator_is_enabled_regmap
,
174 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
175 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
176 .list_voltage
= regulator_list_voltage_linear
,
177 .set_current_limit
= pv88060_set_current_limit
,
178 .get_current_limit
= pv88060_get_current_limit
,
181 static struct regulator_ops pv88060_ldo_ops
= {
182 .enable
= regulator_enable_regmap
,
183 .disable
= regulator_disable_regmap
,
184 .is_enabled
= regulator_is_enabled_regmap
,
185 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
186 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
187 .list_voltage
= regulator_list_voltage_linear
,
190 #define PV88060_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 = &pv88060_buck_ops,\
202 .n_voltages = ((max) - (min))/(step) + 1,\
203 .enable_reg = PV88060_REG_##regl_name##_CONF0,\
204 .enable_mask = PV88060_BUCK_EN, \
205 .vsel_reg = PV88060_REG_##regl_name##_CONF0,\
206 .vsel_mask = PV88060_VBUCK_MASK,\
208 .current_limits = limits_array,\
209 .n_current_limits = ARRAY_SIZE(limits_array),\
210 .limit_mask = PV88060_BUCK_ILIM_MASK, \
211 .conf = PV88060_REG_##regl_name##_CONF1,\
214 #define PV88060_LDO(chip, regl_name, min, step, max) \
217 .id = chip##_ID_##regl_name,\
218 .name = __stringify(chip##_##regl_name),\
219 .of_match = of_match_ptr(#regl_name),\
220 .regulators_node = of_match_ptr("regulators"),\
221 .type = REGULATOR_VOLTAGE,\
222 .owner = THIS_MODULE,\
223 .ops = &pv88060_ldo_ops,\
226 .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
227 .enable_reg = PV88060_REG_##regl_name##_CONF, \
228 .enable_mask = PV88060_LDO_EN, \
229 .vsel_reg = PV88060_REG_##regl_name##_CONF, \
230 .vsel_mask = PV88060_VLDO_MASK, \
234 #define PV88060_SW(chip, regl_name, 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 = &pv88060_ldo_ops,\
247 .enable_reg = PV88060_REG_##regl_name##_CONF,\
248 .enable_mask = PV88060_SW_EN,\
252 static const struct pv88060_regulator pv88060_regulator_info
[] = {
253 PV88060_BUCK(PV88060
, BUCK1
, 2800000, 12500, 4387500,
254 pv88060_buck1_limits
),
255 PV88060_LDO(PV88060
, LDO1
, 1200000, 50000, 3350000),
256 PV88060_LDO(PV88060
, LDO2
, 1200000, 50000, 3350000),
257 PV88060_LDO(PV88060
, LDO3
, 1200000, 50000, 3350000),
258 PV88060_LDO(PV88060
, LDO4
, 1200000, 50000, 3350000),
259 PV88060_LDO(PV88060
, LDO5
, 1200000, 50000, 3350000),
260 PV88060_LDO(PV88060
, LDO6
, 1200000, 50000, 3350000),
261 PV88060_LDO(PV88060
, LDO7
, 1200000, 50000, 3350000),
262 PV88060_SW(PV88060
, SW1
, 5000000),
263 PV88060_SW(PV88060
, SW2
, 5000000),
264 PV88060_SW(PV88060
, SW3
, 5000000),
265 PV88060_SW(PV88060
, SW4
, 5000000),
266 PV88060_SW(PV88060
, SW5
, 5000000),
267 PV88060_SW(PV88060
, SW6
, 5000000),
270 static irqreturn_t
pv88060_irq_handler(int irq
, void *data
)
272 struct pv88060
*chip
= data
;
273 int i
, reg_val
, err
, ret
= IRQ_NONE
;
275 err
= regmap_read(chip
->regmap
, PV88060_REG_EVENT_A
, ®_val
);
279 if (reg_val
& PV88060_E_VDD_FLT
) {
280 for (i
= 0; i
< PV88060_MAX_REGULATORS
; i
++) {
281 if (chip
->rdev
[i
] != NULL
) {
282 regulator_notifier_call_chain(chip
->rdev
[i
],
283 REGULATOR_EVENT_UNDER_VOLTAGE
,
288 err
= regmap_write(chip
->regmap
, PV88060_REG_EVENT_A
,
296 if (reg_val
& PV88060_E_OVER_TEMP
) {
297 for (i
= 0; i
< PV88060_MAX_REGULATORS
; i
++) {
298 if (chip
->rdev
[i
] != NULL
) {
299 regulator_notifier_call_chain(chip
->rdev
[i
],
300 REGULATOR_EVENT_OVER_TEMP
,
305 err
= regmap_write(chip
->regmap
, PV88060_REG_EVENT_A
,
306 PV88060_E_OVER_TEMP
);
316 dev_err(chip
->dev
, "I2C error : %d\n", err
);
321 * I2C driver interface functions
323 static int pv88060_i2c_probe(struct i2c_client
*i2c
,
324 const struct i2c_device_id
*id
)
326 struct regulator_init_data
*init_data
= dev_get_platdata(&i2c
->dev
);
327 struct pv88060
*chip
;
328 struct regulator_config config
= { };
329 int error
, i
, ret
= 0;
331 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct pv88060
), GFP_KERNEL
);
335 chip
->dev
= &i2c
->dev
;
336 chip
->regmap
= devm_regmap_init_i2c(i2c
, &pv88060_regmap_config
);
337 if (IS_ERR(chip
->regmap
)) {
338 error
= PTR_ERR(chip
->regmap
);
339 dev_err(chip
->dev
, "Failed to allocate register map: %d\n",
344 i2c_set_clientdata(i2c
, chip
);
347 ret
= regmap_write(chip
->regmap
, PV88060_REG_MASK_A
, 0xFF);
350 "Failed to mask A reg: %d\n", ret
);
354 ret
= regmap_write(chip
->regmap
, PV88060_REG_MASK_B
, 0xFF);
357 "Failed to mask B reg: %d\n", ret
);
361 ret
= regmap_write(chip
->regmap
, PV88060_REG_MASK_C
, 0xFF);
364 "Failed to mask C reg: %d\n", ret
);
368 ret
= devm_request_threaded_irq(&i2c
->dev
, i2c
->irq
, NULL
,
370 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
373 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
378 ret
= regmap_update_bits(chip
->regmap
, PV88060_REG_MASK_A
,
379 PV88060_M_VDD_FLT
| PV88060_M_OVER_TEMP
, 0);
382 "Failed to update mask reg: %d\n", ret
);
387 dev_warn(chip
->dev
, "No IRQ configured\n");
390 config
.dev
= chip
->dev
;
391 config
.regmap
= chip
->regmap
;
393 for (i
= 0; i
< PV88060_MAX_REGULATORS
; i
++) {
395 config
.init_data
= &init_data
[i
];
397 config
.driver_data
= (void *)&pv88060_regulator_info
[i
];
398 chip
->rdev
[i
] = devm_regulator_register(chip
->dev
,
399 &pv88060_regulator_info
[i
].desc
, &config
);
400 if (IS_ERR(chip
->rdev
[i
])) {
402 "Failed to register PV88060 regulator\n");
403 return PTR_ERR(chip
->rdev
[i
]);
410 static const struct i2c_device_id pv88060_i2c_id
[] = {
414 MODULE_DEVICE_TABLE(i2c
, pv88060_i2c_id
);
417 static const struct of_device_id pv88060_dt_ids
[] = {
418 { .compatible
= "pvs,pv88060", .data
= &pv88060_i2c_id
[0] },
421 MODULE_DEVICE_TABLE(of
, pv88060_dt_ids
);
424 static struct i2c_driver pv88060_regulator_driver
= {
427 .of_match_table
= of_match_ptr(pv88060_dt_ids
),
429 .probe
= pv88060_i2c_probe
,
430 .id_table
= pv88060_i2c_id
,
433 module_i2c_driver(pv88060_regulator_driver
);
435 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
436 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060");
437 MODULE_LICENSE("GPL");