perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / regulator / pv88060-regulator.c
blob3d3415839ba242aba2407173e81a332ae85b2466
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // pv88060-regulator.c - Regulator device driver for PV88060
4 // Copyright (C) 2015 Powerventure Semiconductor Ltd.
6 #include <linux/err.h>
7 #include <linux/i2c.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/regulator/driver.h>
12 #include <linux/regulator/machine.h>
13 #include <linux/regmap.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/regulator/of_regulator.h>
17 #include "pv88060-regulator.h"
19 #define PV88060_MAX_REGULATORS 14
21 /* PV88060 REGULATOR IDs */
22 enum {
23 /* BUCKs */
24 PV88060_ID_BUCK1,
26 /* LDOs */
27 PV88060_ID_LDO1,
28 PV88060_ID_LDO2,
29 PV88060_ID_LDO3,
30 PV88060_ID_LDO4,
31 PV88060_ID_LDO5,
32 PV88060_ID_LDO6,
33 PV88060_ID_LDO7,
35 /* SWTs */
36 PV88060_ID_SW1,
37 PV88060_ID_SW2,
38 PV88060_ID_SW3,
39 PV88060_ID_SW4,
40 PV88060_ID_SW5,
41 PV88060_ID_SW6,
44 struct pv88060_regulator {
45 struct regulator_desc desc;
46 unsigned int conf; /* buck configuration register */
49 struct pv88060 {
50 struct device *dev;
51 struct regmap *regmap;
52 struct regulator_dev *rdev[PV88060_MAX_REGULATORS];
55 static const struct regmap_config pv88060_regmap_config = {
56 .reg_bits = 8,
57 .val_bits = 8,
60 /* Current limits array (in uA) for BUCK1
61 * Entry indexes corresponds to register values.
64 static const unsigned int pv88060_buck1_limits[] = {
65 1496000, 2393000, 3291000, 4189000
68 static unsigned int pv88060_buck_get_mode(struct regulator_dev *rdev)
70 struct pv88060_regulator *info = rdev_get_drvdata(rdev);
71 unsigned int data;
72 int ret, mode = 0;
74 ret = regmap_read(rdev->regmap, info->conf, &data);
75 if (ret < 0)
76 return ret;
78 switch (data & PV88060_BUCK_MODE_MASK) {
79 case PV88060_BUCK_MODE_SYNC:
80 mode = REGULATOR_MODE_FAST;
81 break;
82 case PV88060_BUCK_MODE_AUTO:
83 mode = REGULATOR_MODE_NORMAL;
84 break;
85 case PV88060_BUCK_MODE_SLEEP:
86 mode = REGULATOR_MODE_STANDBY;
87 break;
90 return mode;
93 static int pv88060_buck_set_mode(struct regulator_dev *rdev,
94 unsigned int mode)
96 struct pv88060_regulator *info = rdev_get_drvdata(rdev);
97 int val = 0;
99 switch (mode) {
100 case REGULATOR_MODE_FAST:
101 val = PV88060_BUCK_MODE_SYNC;
102 break;
103 case REGULATOR_MODE_NORMAL:
104 val = PV88060_BUCK_MODE_AUTO;
105 break;
106 case REGULATOR_MODE_STANDBY:
107 val = PV88060_BUCK_MODE_SLEEP;
108 break;
109 default:
110 return -EINVAL;
113 return regmap_update_bits(rdev->regmap, info->conf,
114 PV88060_BUCK_MODE_MASK, val);
117 static const struct regulator_ops pv88060_buck_ops = {
118 .get_mode = pv88060_buck_get_mode,
119 .set_mode = pv88060_buck_set_mode,
120 .enable = regulator_enable_regmap,
121 .disable = regulator_disable_regmap,
122 .is_enabled = regulator_is_enabled_regmap,
123 .set_voltage_sel = regulator_set_voltage_sel_regmap,
124 .get_voltage_sel = regulator_get_voltage_sel_regmap,
125 .list_voltage = regulator_list_voltage_linear,
126 .set_current_limit = regulator_set_current_limit_regmap,
127 .get_current_limit = regulator_get_current_limit_regmap,
130 static const struct regulator_ops pv88060_ldo_ops = {
131 .enable = regulator_enable_regmap,
132 .disable = regulator_disable_regmap,
133 .is_enabled = regulator_is_enabled_regmap,
134 .set_voltage_sel = regulator_set_voltage_sel_regmap,
135 .get_voltage_sel = regulator_get_voltage_sel_regmap,
136 .list_voltage = regulator_list_voltage_linear,
139 static const struct regulator_ops pv88060_sw_ops = {
140 .enable = regulator_enable_regmap,
141 .disable = regulator_disable_regmap,
142 .is_enabled = regulator_is_enabled_regmap,
145 #define PV88060_BUCK(chip, regl_name, min, step, max, limits_array) \
147 .desc = {\
148 .id = chip##_ID_##regl_name,\
149 .name = __stringify(chip##_##regl_name),\
150 .of_match = of_match_ptr(#regl_name),\
151 .regulators_node = of_match_ptr("regulators"),\
152 .type = REGULATOR_VOLTAGE,\
153 .owner = THIS_MODULE,\
154 .ops = &pv88060_buck_ops,\
155 .min_uV = min,\
156 .uV_step = step,\
157 .n_voltages = ((max) - (min))/(step) + 1,\
158 .enable_reg = PV88060_REG_##regl_name##_CONF0,\
159 .enable_mask = PV88060_BUCK_EN, \
160 .vsel_reg = PV88060_REG_##regl_name##_CONF0,\
161 .vsel_mask = PV88060_VBUCK_MASK,\
162 .curr_table = limits_array,\
163 .n_current_limits = ARRAY_SIZE(limits_array),\
164 .csel_reg = PV88060_REG_##regl_name##_CONF1,\
165 .csel_mask = PV88060_BUCK_ILIM_MASK,\
167 .conf = PV88060_REG_##regl_name##_CONF1,\
170 #define PV88060_LDO(chip, regl_name, min, step, max) \
172 .desc = {\
173 .id = chip##_ID_##regl_name,\
174 .name = __stringify(chip##_##regl_name),\
175 .of_match = of_match_ptr(#regl_name),\
176 .regulators_node = of_match_ptr("regulators"),\
177 .type = REGULATOR_VOLTAGE,\
178 .owner = THIS_MODULE,\
179 .ops = &pv88060_ldo_ops,\
180 .min_uV = min, \
181 .uV_step = step, \
182 .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
183 .enable_reg = PV88060_REG_##regl_name##_CONF, \
184 .enable_mask = PV88060_LDO_EN, \
185 .vsel_reg = PV88060_REG_##regl_name##_CONF, \
186 .vsel_mask = PV88060_VLDO_MASK, \
190 #define PV88060_SW(chip, regl_name, max) \
192 .desc = {\
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_sw_ops,\
200 .fixed_uV = max,\
201 .n_voltages = 1,\
202 .enable_reg = PV88060_REG_##regl_name##_CONF,\
203 .enable_mask = PV88060_SW_EN,\
207 static const struct pv88060_regulator pv88060_regulator_info[] = {
208 PV88060_BUCK(PV88060, BUCK1, 2800000, 12500, 4387500,
209 pv88060_buck1_limits),
210 PV88060_LDO(PV88060, LDO1, 1200000, 50000, 3350000),
211 PV88060_LDO(PV88060, LDO2, 1200000, 50000, 3350000),
212 PV88060_LDO(PV88060, LDO3, 1200000, 50000, 3350000),
213 PV88060_LDO(PV88060, LDO4, 1200000, 50000, 3350000),
214 PV88060_LDO(PV88060, LDO5, 1200000, 50000, 3350000),
215 PV88060_LDO(PV88060, LDO6, 1200000, 50000, 3350000),
216 PV88060_LDO(PV88060, LDO7, 1200000, 50000, 3350000),
217 PV88060_SW(PV88060, SW1, 5000000),
218 PV88060_SW(PV88060, SW2, 5000000),
219 PV88060_SW(PV88060, SW3, 5000000),
220 PV88060_SW(PV88060, SW4, 5000000),
221 PV88060_SW(PV88060, SW5, 5000000),
222 PV88060_SW(PV88060, SW6, 5000000),
225 static irqreturn_t pv88060_irq_handler(int irq, void *data)
227 struct pv88060 *chip = data;
228 int i, reg_val, err, ret = IRQ_NONE;
230 err = regmap_read(chip->regmap, PV88060_REG_EVENT_A, &reg_val);
231 if (err < 0)
232 goto error_i2c;
234 if (reg_val & PV88060_E_VDD_FLT) {
235 for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
236 if (chip->rdev[i] != NULL) {
237 regulator_lock(chip->rdev[i]);
238 regulator_notifier_call_chain(chip->rdev[i],
239 REGULATOR_EVENT_UNDER_VOLTAGE,
240 NULL);
241 regulator_unlock(chip->rdev[i]);
245 err = regmap_write(chip->regmap, PV88060_REG_EVENT_A,
246 PV88060_E_VDD_FLT);
247 if (err < 0)
248 goto error_i2c;
250 ret = IRQ_HANDLED;
253 if (reg_val & PV88060_E_OVER_TEMP) {
254 for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
255 if (chip->rdev[i] != NULL) {
256 regulator_lock(chip->rdev[i]);
257 regulator_notifier_call_chain(chip->rdev[i],
258 REGULATOR_EVENT_OVER_TEMP,
259 NULL);
260 regulator_unlock(chip->rdev[i]);
264 err = regmap_write(chip->regmap, PV88060_REG_EVENT_A,
265 PV88060_E_OVER_TEMP);
266 if (err < 0)
267 goto error_i2c;
269 ret = IRQ_HANDLED;
272 return ret;
274 error_i2c:
275 dev_err(chip->dev, "I2C error : %d\n", err);
276 return IRQ_NONE;
280 * I2C driver interface functions
282 static int pv88060_i2c_probe(struct i2c_client *i2c,
283 const struct i2c_device_id *id)
285 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
286 struct pv88060 *chip;
287 struct regulator_config config = { };
288 int error, i, ret = 0;
290 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88060), GFP_KERNEL);
291 if (!chip)
292 return -ENOMEM;
294 chip->dev = &i2c->dev;
295 chip->regmap = devm_regmap_init_i2c(i2c, &pv88060_regmap_config);
296 if (IS_ERR(chip->regmap)) {
297 error = PTR_ERR(chip->regmap);
298 dev_err(chip->dev, "Failed to allocate register map: %d\n",
299 error);
300 return error;
303 i2c_set_clientdata(i2c, chip);
305 if (i2c->irq != 0) {
306 ret = regmap_write(chip->regmap, PV88060_REG_MASK_A, 0xFF);
307 if (ret < 0) {
308 dev_err(chip->dev,
309 "Failed to mask A reg: %d\n", ret);
310 return ret;
313 ret = regmap_write(chip->regmap, PV88060_REG_MASK_B, 0xFF);
314 if (ret < 0) {
315 dev_err(chip->dev,
316 "Failed to mask B reg: %d\n", ret);
317 return ret;
320 ret = regmap_write(chip->regmap, PV88060_REG_MASK_C, 0xFF);
321 if (ret < 0) {
322 dev_err(chip->dev,
323 "Failed to mask C reg: %d\n", ret);
324 return ret;
327 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
328 pv88060_irq_handler,
329 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
330 "pv88060", chip);
331 if (ret != 0) {
332 dev_err(chip->dev, "Failed to request IRQ: %d\n",
333 i2c->irq);
334 return ret;
337 ret = regmap_update_bits(chip->regmap, PV88060_REG_MASK_A,
338 PV88060_M_VDD_FLT | PV88060_M_OVER_TEMP, 0);
339 if (ret < 0) {
340 dev_err(chip->dev,
341 "Failed to update mask reg: %d\n", ret);
342 return ret;
345 } else {
346 dev_warn(chip->dev, "No IRQ configured\n");
349 config.dev = chip->dev;
350 config.regmap = chip->regmap;
352 for (i = 0; i < PV88060_MAX_REGULATORS; i++) {
353 if (init_data)
354 config.init_data = &init_data[i];
356 config.driver_data = (void *)&pv88060_regulator_info[i];
357 chip->rdev[i] = devm_regulator_register(chip->dev,
358 &pv88060_regulator_info[i].desc, &config);
359 if (IS_ERR(chip->rdev[i])) {
360 dev_err(chip->dev,
361 "Failed to register PV88060 regulator\n");
362 return PTR_ERR(chip->rdev[i]);
366 return 0;
369 static const struct i2c_device_id pv88060_i2c_id[] = {
370 {"pv88060", 0},
373 MODULE_DEVICE_TABLE(i2c, pv88060_i2c_id);
375 #ifdef CONFIG_OF
376 static const struct of_device_id pv88060_dt_ids[] = {
377 { .compatible = "pvs,pv88060", .data = &pv88060_i2c_id[0] },
380 MODULE_DEVICE_TABLE(of, pv88060_dt_ids);
381 #endif
383 static struct i2c_driver pv88060_regulator_driver = {
384 .driver = {
385 .name = "pv88060",
386 .of_match_table = of_match_ptr(pv88060_dt_ids),
388 .probe = pv88060_i2c_probe,
389 .id_table = pv88060_i2c_id,
392 module_i2c_driver(pv88060_regulator_driver);
394 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
395 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060");
396 MODULE_LICENSE("GPL");