xref: /openbmc/linux/drivers/regulator/pv88090-regulator.c (revision bf8981a2aa082d9d64771b47c8a1c9c388d8cd40)
1 /*
2  * pv88090-regulator.c - Regulator device driver for PV88090
3  * Copyright (C) 2015  Powerventure Semiconductor Ltd.
4  *
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.
9  *
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.
14  */
15 
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"
28 
29 #define PV88090_MAX_REGULATORS	5
30 
31 /* PV88090 REGULATOR IDs */
32 enum {
33 	/* BUCKs */
34 	PV88090_ID_BUCK1,
35 	PV88090_ID_BUCK2,
36 	PV88090_ID_BUCK3,
37 
38 	/* LDOs */
39 	PV88090_ID_LDO1,
40 	PV88090_ID_LDO2,
41 };
42 
43 struct pv88090_regulator {
44 	struct regulator_desc desc;
45 	unsigned int conf;
46 	unsigned int conf2;
47 };
48 
49 struct pv88090 {
50 	struct device *dev;
51 	struct regmap *regmap;
52 	struct regulator_dev *rdev[PV88090_MAX_REGULATORS];
53 };
54 
55 struct pv88090_buck_voltage {
56 	int min_uV;
57 	int max_uV;
58 	int uV_step;
59 };
60 
61 static const struct regmap_config pv88090_regmap_config = {
62 	.reg_bits = 8,
63 	.val_bits = 8,
64 };
65 
66 /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
67  *  Entry indexes corresponds to register values.
68  */
69 
70 static const unsigned int pv88090_buck1_limits[] = {
71 	 220000,  440000,  660000,  880000, 1100000, 1320000, 1540000, 1760000,
72 	1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
73 	3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
74 	5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
75 };
76 
77 static const unsigned int pv88090_buck23_limits[] = {
78 	1496000, 2393000, 3291000, 4189000
79 };
80 
81 static const struct pv88090_buck_voltage pv88090_buck_vol[3] = {
82 	{
83 		.min_uV = 600000,
84 		.max_uV = 1393750,
85 		.uV_step = 6250,
86 	},
87 
88 	{
89 		.min_uV = 1400000,
90 		.max_uV = 2193750,
91 		.uV_step = 6250,
92 	},
93 	{
94 		.min_uV = 1250000,
95 		.max_uV = 2837500,
96 		.uV_step = 12500,
97 	},
98 };
99 
100 static unsigned int pv88090_buck_get_mode(struct regulator_dev *rdev)
101 {
102 	struct pv88090_regulator *info = rdev_get_drvdata(rdev);
103 	unsigned int data;
104 	int ret, mode = 0;
105 
106 	ret = regmap_read(rdev->regmap, info->conf, &data);
107 	if (ret < 0)
108 		return ret;
109 
110 	switch (data & PV88090_BUCK1_MODE_MASK) {
111 	case PV88090_BUCK_MODE_SYNC:
112 		mode = REGULATOR_MODE_FAST;
113 		break;
114 	case PV88090_BUCK_MODE_AUTO:
115 		mode = REGULATOR_MODE_NORMAL;
116 		break;
117 	case PV88090_BUCK_MODE_SLEEP:
118 		mode = REGULATOR_MODE_STANDBY;
119 		break;
120 	}
121 
122 	return mode;
123 }
124 
125 static int pv88090_buck_set_mode(struct regulator_dev *rdev,
126 					unsigned int mode)
127 {
128 	struct pv88090_regulator *info = rdev_get_drvdata(rdev);
129 	int val = 0;
130 
131 	switch (mode) {
132 	case REGULATOR_MODE_FAST:
133 		val = PV88090_BUCK_MODE_SYNC;
134 		break;
135 	case REGULATOR_MODE_NORMAL:
136 		val = PV88090_BUCK_MODE_AUTO;
137 		break;
138 	case REGULATOR_MODE_STANDBY:
139 		val = PV88090_BUCK_MODE_SLEEP;
140 		break;
141 	default:
142 		return -EINVAL;
143 	}
144 
145 	return regmap_update_bits(rdev->regmap, info->conf,
146 					PV88090_BUCK1_MODE_MASK, val);
147 }
148 
149 static const struct regulator_ops pv88090_buck_ops = {
150 	.get_mode = pv88090_buck_get_mode,
151 	.set_mode = pv88090_buck_set_mode,
152 	.enable = regulator_enable_regmap,
153 	.disable = regulator_disable_regmap,
154 	.is_enabled = regulator_is_enabled_regmap,
155 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
156 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
157 	.list_voltage = regulator_list_voltage_linear,
158 	.set_current_limit = regulator_set_current_limit_regmap,
159 	.get_current_limit = regulator_get_current_limit_regmap,
160 };
161 
162 static const struct regulator_ops pv88090_ldo_ops = {
163 	.enable = regulator_enable_regmap,
164 	.disable = regulator_disable_regmap,
165 	.is_enabled = regulator_is_enabled_regmap,
166 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
167 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
168 	.list_voltage = regulator_list_voltage_linear,
169 };
170 
171 #define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
172 {\
173 	.desc	=	{\
174 		.id = chip##_ID_##regl_name,\
175 		.name = __stringify(chip##_##regl_name),\
176 		.of_match = of_match_ptr(#regl_name),\
177 		.regulators_node = of_match_ptr("regulators"),\
178 		.type = REGULATOR_VOLTAGE,\
179 		.owner = THIS_MODULE,\
180 		.ops = &pv88090_buck_ops,\
181 		.min_uV = min, \
182 		.uV_step = step, \
183 		.n_voltages = ((max) - (min))/(step) + 1, \
184 		.enable_reg = PV88090_REG_##regl_name##_CONF0, \
185 		.enable_mask = PV88090_##regl_name##_EN, \
186 		.vsel_reg = PV88090_REG_##regl_name##_CONF0, \
187 		.vsel_mask = PV88090_V##regl_name##_MASK, \
188 		.curr_table = limits_array, \
189 		.n_current_limits = ARRAY_SIZE(limits_array), \
190 		.csel_reg = PV88090_REG_##regl_name##_CONF1, \
191 		.csel_mask = PV88090_##regl_name##_ILIM_MASK, \
192 	},\
193 	.conf = PV88090_REG_##regl_name##_CONF1, \
194 	.conf2 = PV88090_REG_##regl_name##_CONF2, \
195 }
196 
197 #define PV88090_LDO(chip, regl_name, min, step, max) \
198 {\
199 	.desc	=	{\
200 		.id = chip##_ID_##regl_name,\
201 		.name = __stringify(chip##_##regl_name),\
202 		.of_match = of_match_ptr(#regl_name),\
203 		.regulators_node = of_match_ptr("regulators"),\
204 		.type = REGULATOR_VOLTAGE,\
205 		.owner = THIS_MODULE,\
206 		.ops = &pv88090_ldo_ops,\
207 		.min_uV = min, \
208 		.uV_step = step, \
209 		.n_voltages = ((max) - (min))/(step) + 1, \
210 		.enable_reg = PV88090_REG_##regl_name##_CONT, \
211 		.enable_mask = PV88090_##regl_name##_EN, \
212 		.vsel_reg = PV88090_REG_##regl_name##_CONT, \
213 		.vsel_mask = PV88090_V##regl_name##_MASK, \
214 	},\
215 }
216 
217 static struct pv88090_regulator pv88090_regulator_info[] = {
218 	PV88090_BUCK(PV88090, BUCK1, 600000, 6250, 1393750,
219 		pv88090_buck1_limits),
220 	PV88090_BUCK(PV88090, BUCK2, 600000, 6250, 1393750,
221 		pv88090_buck23_limits),
222 	PV88090_BUCK(PV88090, BUCK3, 600000, 6250, 1393750,
223 		pv88090_buck23_limits),
224 	PV88090_LDO(PV88090, LDO1, 1200000, 50000, 4350000),
225 	PV88090_LDO(PV88090, LDO2,  650000, 25000, 2225000),
226 };
227 
228 static irqreturn_t pv88090_irq_handler(int irq, void *data)
229 {
230 	struct pv88090 *chip = data;
231 	int i, reg_val, err, ret = IRQ_NONE;
232 
233 	err = regmap_read(chip->regmap, PV88090_REG_EVENT_A, &reg_val);
234 	if (err < 0)
235 		goto error_i2c;
236 
237 	if (reg_val & PV88090_E_VDD_FLT) {
238 		for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
239 			if (chip->rdev[i] != NULL) {
240 				regulator_notifier_call_chain(chip->rdev[i],
241 					REGULATOR_EVENT_UNDER_VOLTAGE,
242 					NULL);
243 			}
244 		}
245 
246 		err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
247 			PV88090_E_VDD_FLT);
248 		if (err < 0)
249 			goto error_i2c;
250 
251 		ret = IRQ_HANDLED;
252 	}
253 
254 	if (reg_val & PV88090_E_OVER_TEMP) {
255 		for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
256 			if (chip->rdev[i] != NULL) {
257 				regulator_notifier_call_chain(chip->rdev[i],
258 					REGULATOR_EVENT_OVER_TEMP,
259 					NULL);
260 			}
261 		}
262 
263 		err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
264 			PV88090_E_OVER_TEMP);
265 		if (err < 0)
266 			goto error_i2c;
267 
268 		ret = IRQ_HANDLED;
269 	}
270 
271 	return ret;
272 
273 error_i2c:
274 	dev_err(chip->dev, "I2C error : %d\n", err);
275 	return IRQ_NONE;
276 }
277 
278 /*
279  * I2C driver interface functions
280  */
281 static int pv88090_i2c_probe(struct i2c_client *i2c,
282 		const struct i2c_device_id *id)
283 {
284 	struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
285 	struct pv88090 *chip;
286 	struct regulator_config config = { };
287 	int error, i, ret = 0;
288 	unsigned int conf2, range, index;
289 
290 	chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88090), GFP_KERNEL);
291 	if (!chip)
292 		return -ENOMEM;
293 
294 	chip->dev = &i2c->dev;
295 	chip->regmap = devm_regmap_init_i2c(i2c, &pv88090_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;
301 	}
302 
303 	i2c_set_clientdata(i2c, chip);
304 
305 	if (i2c->irq != 0) {
306 		ret = regmap_write(chip->regmap, PV88090_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;
311 		}
312 
313 		ret = regmap_write(chip->regmap, PV88090_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;
318 		}
319 
320 		ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
321 					pv88090_irq_handler,
322 					IRQF_TRIGGER_LOW|IRQF_ONESHOT,
323 					"pv88090", chip);
324 		if (ret != 0) {
325 			dev_err(chip->dev, "Failed to request IRQ: %d\n",
326 				i2c->irq);
327 			return ret;
328 		}
329 
330 		ret = regmap_update_bits(chip->regmap, PV88090_REG_MASK_A,
331 			PV88090_M_VDD_FLT | PV88090_M_OVER_TEMP, 0);
332 		if (ret < 0) {
333 			dev_err(chip->dev,
334 				"Failed to update mask reg: %d\n", ret);
335 			return ret;
336 		}
337 
338 	} else {
339 		dev_warn(chip->dev, "No IRQ configured\n");
340 	}
341 
342 	config.dev = chip->dev;
343 	config.regmap = chip->regmap;
344 
345 	for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
346 		if (init_data)
347 			config.init_data = &init_data[i];
348 
349 		if (i == PV88090_ID_BUCK2 || i == PV88090_ID_BUCK3) {
350 			ret = regmap_read(chip->regmap,
351 				pv88090_regulator_info[i].conf2, &conf2);
352 			if (ret < 0)
353 				return ret;
354 
355 			conf2 = (conf2 >> PV88090_BUCK_VDAC_RANGE_SHIFT) &
356 				PV88090_BUCK_VDAC_RANGE_MASK;
357 
358 			ret = regmap_read(chip->regmap,
359 				PV88090_REG_BUCK_FOLD_RANGE, &range);
360 			if (ret < 0)
361 				return ret;
362 
363 			range = (range >>
364 				 (PV88090_BUCK_VRANGE_GAIN_SHIFT + i - 1)) &
365 				PV88090_BUCK_VRANGE_GAIN_MASK;
366 			index = ((range << 1) | conf2);
367 			if (index > PV88090_ID_BUCK3) {
368 				dev_err(chip->dev,
369 					"Invalid index(%d)\n", index);
370 				return -EINVAL;
371 			}
372 
373 			pv88090_regulator_info[i].desc.min_uV
374 				= pv88090_buck_vol[index].min_uV;
375 			pv88090_regulator_info[i].desc.uV_step
376 				= pv88090_buck_vol[index].uV_step;
377 			pv88090_regulator_info[i].desc.n_voltages
378 				= ((pv88090_buck_vol[index].max_uV)
379 				- (pv88090_buck_vol[index].min_uV))
380 				/(pv88090_buck_vol[index].uV_step) + 1;
381 		}
382 
383 		config.driver_data = (void *)&pv88090_regulator_info[i];
384 		chip->rdev[i] = devm_regulator_register(chip->dev,
385 			&pv88090_regulator_info[i].desc, &config);
386 		if (IS_ERR(chip->rdev[i])) {
387 			dev_err(chip->dev,
388 				"Failed to register PV88090 regulator\n");
389 			return PTR_ERR(chip->rdev[i]);
390 		}
391 	}
392 
393 	return 0;
394 }
395 
396 static const struct i2c_device_id pv88090_i2c_id[] = {
397 	{"pv88090", 0},
398 	{},
399 };
400 MODULE_DEVICE_TABLE(i2c, pv88090_i2c_id);
401 
402 #ifdef CONFIG_OF
403 static const struct of_device_id pv88090_dt_ids[] = {
404 	{ .compatible = "pvs,pv88090", .data = &pv88090_i2c_id[0] },
405 	{},
406 };
407 MODULE_DEVICE_TABLE(of, pv88090_dt_ids);
408 #endif
409 
410 static struct i2c_driver pv88090_regulator_driver = {
411 	.driver = {
412 		.name = "pv88090",
413 		.of_match_table = of_match_ptr(pv88090_dt_ids),
414 	},
415 	.probe = pv88090_i2c_probe,
416 	.id_table = pv88090_i2c_id,
417 };
418 
419 module_i2c_driver(pv88090_regulator_driver);
420 
421 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
422 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
423 MODULE_LICENSE("GPL");
424