1 /*
2  * da9211-regulator.c - Regulator device driver for DA9211/DA9213
3  * Copyright (C) 2014  Dialog Semiconductor Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Library General Public License for more details.
14  */
15 
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/regulator/da9211.h>
29 #include "da9211-regulator.h"
30 
31 /* DEVICE IDs */
32 #define DA9211_DEVICE_ID	0x22
33 #define DA9213_DEVICE_ID	0x23
34 
35 #define DA9211_BUCK_MODE_SLEEP	1
36 #define DA9211_BUCK_MODE_SYNC	2
37 #define DA9211_BUCK_MODE_AUTO	3
38 
39 /* DA9211 REGULATOR IDs */
40 #define DA9211_ID_BUCKA	0
41 #define DA9211_ID_BUCKB	1
42 
43 struct da9211 {
44 	struct device *dev;
45 	struct regmap *regmap;
46 	struct da9211_pdata *pdata;
47 	struct regulator_dev *rdev[DA9211_MAX_REGULATORS];
48 	int num_regulator;
49 	int chip_irq;
50 	int chip_id;
51 };
52 
53 static const struct regmap_range_cfg da9211_regmap_range[] = {
54 	{
55 		.selector_reg = DA9211_REG_PAGE_CON,
56 		.selector_mask  = DA9211_REG_PAGE_MASK,
57 		.selector_shift = DA9211_REG_PAGE_SHIFT,
58 		.window_start = 0,
59 		.window_len = 256,
60 		.range_min = 0,
61 		.range_max = 5*128,
62 	},
63 };
64 
65 static const struct regmap_config da9211_regmap_config = {
66 	.reg_bits = 8,
67 	.val_bits = 8,
68 	.max_register = 5 * 128,
69 	.ranges = da9211_regmap_range,
70 	.num_ranges = ARRAY_SIZE(da9211_regmap_range),
71 };
72 
73 /* Default limits measured in millivolts and milliamps */
74 #define DA9211_MIN_MV		300
75 #define DA9211_MAX_MV		1570
76 #define DA9211_STEP_MV		10
77 
78 /* Current limits for DA9211 buck (uA) indices
79  * corresponds with register values
80  */
81 static const int da9211_current_limits[] = {
82 	2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000,
83 	3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000
84 };
85 /* Current limits for DA9213 buck (uA) indices
86  * corresponds with register values
87  */
88 static const int da9213_current_limits[] = {
89 	3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000,
90 	4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000
91 };
92 
93 static unsigned int da9211_buck_get_mode(struct regulator_dev *rdev)
94 {
95 	int id = rdev_get_id(rdev);
96 	struct da9211 *chip = rdev_get_drvdata(rdev);
97 	unsigned int data;
98 	int ret, mode = 0;
99 
100 	ret = regmap_read(chip->regmap, DA9211_REG_BUCKA_CONF+id, &data);
101 	if (ret < 0)
102 		return ret;
103 
104 	switch (data & 0x03) {
105 	case DA9211_BUCK_MODE_SYNC:
106 		mode = REGULATOR_MODE_FAST;
107 		break;
108 	case DA9211_BUCK_MODE_AUTO:
109 		mode = REGULATOR_MODE_NORMAL;
110 		break;
111 	case DA9211_BUCK_MODE_SLEEP:
112 		mode = REGULATOR_MODE_STANDBY;
113 		break;
114 	}
115 
116 	return mode;
117 }
118 
119 static int da9211_buck_set_mode(struct regulator_dev *rdev,
120 					unsigned int mode)
121 {
122 	int id = rdev_get_id(rdev);
123 	struct da9211 *chip = rdev_get_drvdata(rdev);
124 	int val = 0;
125 
126 	switch (mode) {
127 	case REGULATOR_MODE_FAST:
128 		val = DA9211_BUCK_MODE_SYNC;
129 		break;
130 	case REGULATOR_MODE_NORMAL:
131 		val = DA9211_BUCK_MODE_AUTO;
132 		break;
133 	case REGULATOR_MODE_STANDBY:
134 		val = DA9211_BUCK_MODE_SLEEP;
135 		break;
136 	}
137 
138 	return regmap_update_bits(chip->regmap, DA9211_REG_BUCKA_CONF+id,
139 					0x03, val);
140 }
141 
142 static int da9211_set_current_limit(struct regulator_dev *rdev, int min,
143 				    int max)
144 {
145 	int id = rdev_get_id(rdev);
146 	struct da9211 *chip = rdev_get_drvdata(rdev);
147 	int i, max_size;
148 	const int *current_limits;
149 
150 	switch (chip->chip_id) {
151 	case DA9211:
152 		current_limits = da9211_current_limits;
153 		max_size = ARRAY_SIZE(da9211_current_limits)-1;
154 		break;
155 	case DA9213:
156 		current_limits = da9213_current_limits;
157 		max_size = ARRAY_SIZE(da9213_current_limits)-1;
158 		break;
159 	default:
160 		return -EINVAL;
161 	}
162 
163 	/* search for closest to maximum */
164 	for (i = max_size; i >= 0; i--) {
165 		if (min <= current_limits[i] &&
166 		    max >= current_limits[i]) {
167 				return regmap_update_bits(chip->regmap,
168 					DA9211_REG_BUCK_ILIM,
169 					(0x0F << id*4), (i << id*4));
170 		}
171 	}
172 
173 	return -EINVAL;
174 }
175 
176 static int da9211_get_current_limit(struct regulator_dev *rdev)
177 {
178 	int id = rdev_get_id(rdev);
179 	struct da9211 *chip = rdev_get_drvdata(rdev);
180 	unsigned int data;
181 	int ret;
182 	const int *current_limits;
183 
184 	switch (chip->chip_id) {
185 	case DA9211:
186 		current_limits = da9211_current_limits;
187 		break;
188 	case DA9213:
189 		current_limits = da9213_current_limits;
190 		break;
191 	default:
192 		return -EINVAL;
193 	}
194 
195 	ret = regmap_read(chip->regmap, DA9211_REG_BUCK_ILIM, &data);
196 	if (ret < 0)
197 		return ret;
198 
199 	/* select one of 16 values: 0000 (2000mA or 3000mA)
200 	 * to 1111 (5000mA or 6000mA).
201 	 */
202 	data = (data >> id*4) & 0x0F;
203 	return current_limits[data];
204 }
205 
206 static struct regulator_ops da9211_buck_ops = {
207 	.get_mode = da9211_buck_get_mode,
208 	.set_mode = da9211_buck_set_mode,
209 	.enable = regulator_enable_regmap,
210 	.disable = regulator_disable_regmap,
211 	.is_enabled = regulator_is_enabled_regmap,
212 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
213 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
214 	.list_voltage = regulator_list_voltage_linear,
215 	.set_current_limit = da9211_set_current_limit,
216 	.get_current_limit = da9211_get_current_limit,
217 };
218 
219 #define DA9211_BUCK(_id) \
220 {\
221 	.name = #_id,\
222 	.ops = &da9211_buck_ops,\
223 	.type = REGULATOR_VOLTAGE,\
224 	.id = DA9211_ID_##_id,\
225 	.n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\
226 	.min_uV = (DA9211_MIN_MV * 1000),\
227 	.uV_step = (DA9211_STEP_MV * 1000),\
228 	.enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\
229 	.enable_mask = DA9211_BUCKA_EN,\
230 	.vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\
231 	.vsel_mask = DA9211_VBUCK_MASK,\
232 	.owner = THIS_MODULE,\
233 }
234 
235 static struct regulator_desc da9211_regulators[] = {
236 	DA9211_BUCK(BUCKA),
237 	DA9211_BUCK(BUCKB),
238 };
239 
240 #ifdef CONFIG_OF
241 static struct of_regulator_match da9211_matches[] = {
242 	[DA9211_ID_BUCKA] = { .name = "BUCKA" },
243 	[DA9211_ID_BUCKB] = { .name = "BUCKB" },
244 	};
245 
246 static struct da9211_pdata *da9211_parse_regulators_dt(
247 		struct device *dev)
248 {
249 	struct da9211_pdata *pdata;
250 	struct device_node *node;
251 	int i, num, n;
252 
253 	node = of_get_child_by_name(dev->of_node, "regulators");
254 	if (!node) {
255 		dev_err(dev, "regulators node not found\n");
256 		return ERR_PTR(-ENODEV);
257 	}
258 
259 	num = of_regulator_match(dev, node, da9211_matches,
260 				 ARRAY_SIZE(da9211_matches));
261 	of_node_put(node);
262 	if (num < 0) {
263 		dev_err(dev, "Failed to match regulators\n");
264 		return ERR_PTR(-EINVAL);
265 	}
266 
267 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
268 	if (!pdata)
269 		return ERR_PTR(-ENOMEM);
270 
271 	pdata->num_buck = num;
272 
273 	n = 0;
274 	for (i = 0; i < ARRAY_SIZE(da9211_matches); i++) {
275 		if (!da9211_matches[i].init_data)
276 			continue;
277 
278 		pdata->init_data[n] = da9211_matches[i].init_data;
279 
280 		n++;
281 	}
282 
283 	return pdata;
284 }
285 #else
286 static struct da9211_pdata *da9211_parse_regulators_dt(
287 		struct device *dev)
288 {
289 	return ERR_PTR(-ENODEV);
290 }
291 #endif
292 
293 static irqreturn_t da9211_irq_handler(int irq, void *data)
294 {
295 	struct da9211 *chip = data;
296 	int reg_val, err, ret = IRQ_NONE;
297 
298 	err = regmap_read(chip->regmap, DA9211_REG_EVENT_B, &reg_val);
299 	if (err < 0)
300 		goto error_i2c;
301 
302 	if (reg_val & DA9211_E_OV_CURR_A) {
303 		regulator_notifier_call_chain(chip->rdev[0],
304 			REGULATOR_EVENT_OVER_CURRENT,
305 			rdev_get_drvdata(chip->rdev[0]));
306 
307 		err = regmap_write(chip->regmap, DA9211_REG_EVENT_B,
308 			DA9211_E_OV_CURR_A);
309 		if (err < 0)
310 			goto error_i2c;
311 
312 		ret = IRQ_HANDLED;
313 	}
314 
315 	if (reg_val & DA9211_E_OV_CURR_B) {
316 		regulator_notifier_call_chain(chip->rdev[1],
317 			REGULATOR_EVENT_OVER_CURRENT,
318 			rdev_get_drvdata(chip->rdev[1]));
319 
320 		err = regmap_write(chip->regmap, DA9211_REG_EVENT_B,
321 			DA9211_E_OV_CURR_B);
322 		if (err < 0)
323 			goto error_i2c;
324 
325 		ret = IRQ_HANDLED;
326 	}
327 
328 	return ret;
329 
330 error_i2c:
331 	dev_err(chip->dev, "I2C error : %d\n", err);
332 	return IRQ_NONE;
333 }
334 
335 static int da9211_regulator_init(struct da9211 *chip)
336 {
337 	struct regulator_config config = { };
338 	int i, ret;
339 	unsigned int data;
340 
341 	ret = regmap_read(chip->regmap, DA9211_REG_CONFIG_E, &data);
342 	if (ret < 0) {
343 		dev_err(chip->dev, "Failed to read CONTROL_E reg: %d\n", ret);
344 		return ret;
345 	}
346 
347 	data &= DA9211_SLAVE_SEL;
348 	/* If configuration for 1/2 bucks is different between platform data
349 	 * and the register, driver should exit.
350 	 */
351 	if ((chip->pdata->num_buck == 2 && data == 0x40)
352 		|| (chip->pdata->num_buck == 1 && data == 0x00)) {
353 		if (data == 0)
354 			chip->num_regulator = 1;
355 		else
356 			chip->num_regulator = 2;
357 	} else {
358 		dev_err(chip->dev, "Configuration is mismatched\n");
359 		return -EINVAL;
360 	}
361 
362 	for (i = 0; i < chip->num_regulator; i++) {
363 		config.init_data = chip->pdata->init_data[i];
364 		config.dev = chip->dev;
365 		config.driver_data = chip;
366 		config.regmap = chip->regmap;
367 		config.of_node = chip->dev->of_node;
368 
369 		chip->rdev[i] = devm_regulator_register(chip->dev,
370 			&da9211_regulators[i], &config);
371 		if (IS_ERR(chip->rdev[i])) {
372 			dev_err(chip->dev,
373 				"Failed to register DA9211 regulator\n");
374 			return PTR_ERR(chip->rdev[i]);
375 		}
376 
377 		if (chip->chip_irq != 0) {
378 			ret = regmap_update_bits(chip->regmap,
379 				DA9211_REG_MASK_B, DA9211_M_OV_CURR_A << i, 0);
380 			if (ret < 0) {
381 				dev_err(chip->dev,
382 					"Failed to update mask reg: %d\n", ret);
383 				return ret;
384 			}
385 		}
386 	}
387 
388 	return 0;
389 }
390 
391 /*
392  * I2C driver interface functions
393  */
394 static int da9211_i2c_probe(struct i2c_client *i2c,
395 		const struct i2c_device_id *id)
396 {
397 	struct da9211 *chip;
398 	int error, ret;
399 	unsigned int data;
400 
401 	chip = devm_kzalloc(&i2c->dev, sizeof(struct da9211), GFP_KERNEL);
402 	if (!chip)
403 		return -ENOMEM;
404 
405 	chip->dev = &i2c->dev;
406 	chip->regmap = devm_regmap_init_i2c(i2c, &da9211_regmap_config);
407 	if (IS_ERR(chip->regmap)) {
408 		error = PTR_ERR(chip->regmap);
409 		dev_err(chip->dev, "Failed to allocate register map: %d\n",
410 			error);
411 		return error;
412 	}
413 
414 	i2c_set_clientdata(i2c, chip);
415 
416 	chip->pdata = i2c->dev.platform_data;
417 
418 	ret = regmap_read(chip->regmap, DA9211_REG_DEVICE_ID, &data);
419 	if (ret < 0) {
420 		dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
421 		return ret;
422 	}
423 
424 	switch (data) {
425 	case DA9211_DEVICE_ID:
426 		chip->chip_id = DA9211;
427 		break;
428 	case DA9213_DEVICE_ID:
429 		chip->chip_id = DA9213;
430 		break;
431 	default:
432 		dev_err(chip->dev, "Unsupported device id = 0x%x.\n", data);
433 		return -ENODEV;
434 	}
435 
436 	if (!chip->pdata)
437 		chip->pdata = da9211_parse_regulators_dt(chip->dev);
438 
439 	if (IS_ERR(chip->pdata)) {
440 		dev_err(chip->dev, "No regulators defined for the platform\n");
441 		return PTR_ERR(chip->pdata);
442 	}
443 
444 	chip->chip_irq = i2c->irq;
445 
446 	if (chip->chip_irq != 0) {
447 		ret = devm_request_threaded_irq(chip->dev, chip->chip_irq, NULL,
448 					da9211_irq_handler,
449 					IRQF_TRIGGER_LOW|IRQF_ONESHOT,
450 					"da9211", chip);
451 		if (ret != 0) {
452 			dev_err(chip->dev, "Failed to request IRQ: %d\n",
453 				chip->chip_irq);
454 			return ret;
455 		}
456 	} else {
457 		dev_warn(chip->dev, "No IRQ configured\n");
458 	}
459 
460 	ret = da9211_regulator_init(chip);
461 
462 	if (ret < 0)
463 		dev_err(chip->dev, "Failed to initialize regulator: %d\n", ret);
464 
465 	return ret;
466 }
467 
468 static const struct i2c_device_id da9211_i2c_id[] = {
469 	{"da9211", DA9211},
470 	{"da9213", DA9213},
471 	{},
472 };
473 MODULE_DEVICE_TABLE(i2c, da9211_i2c_id);
474 
475 #ifdef CONFIG_OF
476 static const struct of_device_id da9211_dt_ids[] = {
477 	{ .compatible = "dlg,da9211", .data = &da9211_i2c_id[0] },
478 	{ .compatible = "dlg,da9213", .data = &da9211_i2c_id[1] },
479 	{},
480 };
481 MODULE_DEVICE_TABLE(of, da9211_dt_ids);
482 #endif
483 
484 static struct i2c_driver da9211_regulator_driver = {
485 	.driver = {
486 		.name = "da9211",
487 		.owner = THIS_MODULE,
488 		.of_match_table = of_match_ptr(da9211_dt_ids),
489 	},
490 	.probe = da9211_i2c_probe,
491 	.id_table = da9211_i2c_id,
492 };
493 
494 module_i2c_driver(da9211_regulator_driver);
495 
496 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
497 MODULE_DESCRIPTION("Regulator device driver for Dialog DA9211/DA9213");
498 MODULE_LICENSE("GPL v2");
499