xref: /openbmc/linux/drivers/regulator/fan53555.c (revision ae40e94f)
1 /*
2  * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
3  *
4  * Supported Part Numbers:
5  * FAN53555UC00X/01X/03X/04X/05X
6  *
7  * Copyright (c) 2012 Marvell Technology Ltd.
8  * Yunfan Zhang <yfzhang@marvell.com>
9  *
10  * This package is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15 #include <linux/module.h>
16 #include <linux/param.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/regulator/of_regulator.h>
22 #include <linux/of_device.h>
23 #include <linux/i2c.h>
24 #include <linux/slab.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/fan53555.h>
27 
28 /* Voltage setting */
29 #define FAN53555_VSEL0		0x00
30 #define FAN53555_VSEL1		0x01
31 /* Control register */
32 #define FAN53555_CONTROL	0x02
33 /* IC Type */
34 #define FAN53555_ID1		0x03
35 /* IC mask version */
36 #define FAN53555_ID2		0x04
37 /* Monitor register */
38 #define FAN53555_MONITOR	0x05
39 
40 /* VSEL bit definitions */
41 #define VSEL_BUCK_EN	(1 << 7)
42 #define VSEL_MODE		(1 << 6)
43 /* Chip ID and Verison */
44 #define DIE_ID		0x0F	/* ID1 */
45 #define DIE_REV		0x0F	/* ID2 */
46 /* Control bit definitions */
47 #define CTL_OUTPUT_DISCHG	(1 << 7)
48 #define CTL_SLEW_MASK		(0x7 << 4)
49 #define CTL_SLEW_SHIFT		4
50 #define CTL_RESET			(1 << 2)
51 #define CTL_MODE_VSEL0_MODE	BIT(0)
52 #define CTL_MODE_VSEL1_MODE	BIT(1)
53 
54 #define FAN53555_NVOLTAGES	64	/* Numbers of voltages */
55 #define FAN53526_NVOLTAGES	128
56 
57 enum fan53555_vendor {
58 	FAN53526_VENDOR_FAIRCHILD = 0,
59 	FAN53555_VENDOR_FAIRCHILD,
60 	FAN53555_VENDOR_SILERGY,
61 };
62 
63 enum {
64 	FAN53526_CHIP_ID_01 = 1,
65 };
66 
67 enum {
68 	FAN53526_CHIP_REV_08 = 8,
69 };
70 
71 /* IC Type */
72 enum {
73 	FAN53555_CHIP_ID_00 = 0,
74 	FAN53555_CHIP_ID_01,
75 	FAN53555_CHIP_ID_02,
76 	FAN53555_CHIP_ID_03,
77 	FAN53555_CHIP_ID_04,
78 	FAN53555_CHIP_ID_05,
79 	FAN53555_CHIP_ID_08 = 8,
80 };
81 
82 /* IC mask revision */
83 enum {
84 	FAN53555_CHIP_REV_00 = 0x3,
85 	FAN53555_CHIP_REV_13 = 0xf,
86 };
87 
88 enum {
89 	SILERGY_SYR82X = 8,
90 };
91 
92 struct fan53555_device_info {
93 	enum fan53555_vendor vendor;
94 	struct regmap *regmap;
95 	struct device *dev;
96 	struct regulator_desc desc;
97 	struct regulator_dev *rdev;
98 	struct regulator_init_data *regulator;
99 	/* IC Type and Rev */
100 	int chip_id;
101 	int chip_rev;
102 	/* Voltage setting register */
103 	unsigned int vol_reg;
104 	unsigned int sleep_reg;
105 	/* Voltage range and step(linear) */
106 	unsigned int vsel_min;
107 	unsigned int vsel_step;
108 	unsigned int vsel_count;
109 	/* Voltage slew rate limiting */
110 	unsigned int slew_rate;
111 	/* Mode */
112 	unsigned int mode_reg;
113 	unsigned int mode_mask;
114 	/* Sleep voltage cache */
115 	unsigned int sleep_vol_cache;
116 };
117 
118 static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
119 {
120 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
121 	int ret;
122 
123 	if (di->sleep_vol_cache == uV)
124 		return 0;
125 	ret = regulator_map_voltage_linear(rdev, uV, uV);
126 	if (ret < 0)
127 		return ret;
128 	ret = regmap_update_bits(di->regmap, di->sleep_reg,
129 				 di->desc.vsel_mask, ret);
130 	if (ret < 0)
131 		return ret;
132 	/* Cache the sleep voltage setting.
133 	 * Might not be the real voltage which is rounded */
134 	di->sleep_vol_cache = uV;
135 
136 	return 0;
137 }
138 
139 static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
140 {
141 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
142 
143 	return regmap_update_bits(di->regmap, di->sleep_reg,
144 				  VSEL_BUCK_EN, VSEL_BUCK_EN);
145 }
146 
147 static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
148 {
149 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
150 
151 	return regmap_update_bits(di->regmap, di->sleep_reg,
152 				  VSEL_BUCK_EN, 0);
153 }
154 
155 static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
156 {
157 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
158 
159 	switch (mode) {
160 	case REGULATOR_MODE_FAST:
161 		regmap_update_bits(di->regmap, di->mode_reg,
162 				   di->mode_mask, di->mode_mask);
163 		break;
164 	case REGULATOR_MODE_NORMAL:
165 		regmap_update_bits(di->regmap, di->vol_reg, di->mode_mask, 0);
166 		break;
167 	default:
168 		return -EINVAL;
169 	}
170 	return 0;
171 }
172 
173 static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
174 {
175 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
176 	unsigned int val;
177 	int ret = 0;
178 
179 	ret = regmap_read(di->regmap, di->mode_reg, &val);
180 	if (ret < 0)
181 		return ret;
182 	if (val & di->mode_mask)
183 		return REGULATOR_MODE_FAST;
184 	else
185 		return REGULATOR_MODE_NORMAL;
186 }
187 
188 static const int slew_rates[] = {
189 	64000,
190 	32000,
191 	16000,
192 	 8000,
193 	 4000,
194 	 2000,
195 	 1000,
196 	  500,
197 };
198 
199 static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
200 {
201 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
202 	int regval = -1, i;
203 
204 	for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
205 		if (ramp <= slew_rates[i])
206 			regval = i;
207 		else
208 			break;
209 	}
210 
211 	if (regval < 0) {
212 		dev_err(di->dev, "unsupported ramp value %d\n", ramp);
213 		return -EINVAL;
214 	}
215 
216 	return regmap_update_bits(di->regmap, FAN53555_CONTROL,
217 				  CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
218 }
219 
220 static const struct regulator_ops fan53555_regulator_ops = {
221 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
222 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
223 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
224 	.map_voltage = regulator_map_voltage_linear,
225 	.list_voltage = regulator_list_voltage_linear,
226 	.set_suspend_voltage = fan53555_set_suspend_voltage,
227 	.enable = regulator_enable_regmap,
228 	.disable = regulator_disable_regmap,
229 	.is_enabled = regulator_is_enabled_regmap,
230 	.set_mode = fan53555_set_mode,
231 	.get_mode = fan53555_get_mode,
232 	.set_ramp_delay = fan53555_set_ramp,
233 	.set_suspend_enable = fan53555_set_suspend_enable,
234 	.set_suspend_disable = fan53555_set_suspend_disable,
235 };
236 
237 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
238 {
239 	/* Init voltage range and step */
240 	switch (di->chip_id) {
241 	case FAN53526_CHIP_ID_01:
242 		switch (di->chip_rev) {
243 		case FAN53526_CHIP_REV_08:
244 			di->vsel_min = 600000;
245 			di->vsel_step = 6250;
246 			break;
247 		default:
248 			dev_err(di->dev,
249 				"Chip ID %d with rev %d not supported!\n",
250 				di->chip_id, di->chip_rev);
251 			return -EINVAL;
252 		}
253 		break;
254 	default:
255 		dev_err(di->dev,
256 			"Chip ID %d not supported!\n", di->chip_id);
257 		return -EINVAL;
258 	}
259 
260 	di->vsel_count = FAN53526_NVOLTAGES;
261 
262 	return 0;
263 }
264 
265 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
266 {
267 	/* Init voltage range and step */
268 	switch (di->chip_id) {
269 	case FAN53555_CHIP_ID_00:
270 		switch (di->chip_rev) {
271 		case FAN53555_CHIP_REV_00:
272 			di->vsel_min = 600000;
273 			di->vsel_step = 10000;
274 			break;
275 		case FAN53555_CHIP_REV_13:
276 			di->vsel_min = 800000;
277 			di->vsel_step = 10000;
278 			break;
279 		default:
280 			dev_err(di->dev,
281 				"Chip ID %d with rev %d not supported!\n",
282 				di->chip_id, di->chip_rev);
283 			return -EINVAL;
284 		}
285 		break;
286 	case FAN53555_CHIP_ID_01:
287 	case FAN53555_CHIP_ID_03:
288 	case FAN53555_CHIP_ID_05:
289 	case FAN53555_CHIP_ID_08:
290 		di->vsel_min = 600000;
291 		di->vsel_step = 10000;
292 		break;
293 	case FAN53555_CHIP_ID_04:
294 		di->vsel_min = 603000;
295 		di->vsel_step = 12826;
296 		break;
297 	default:
298 		dev_err(di->dev,
299 			"Chip ID %d not supported!\n", di->chip_id);
300 		return -EINVAL;
301 	}
302 
303 	di->vsel_count = FAN53555_NVOLTAGES;
304 
305 	return 0;
306 }
307 
308 static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
309 {
310 	/* Init voltage range and step */
311 	switch (di->chip_id) {
312 	case SILERGY_SYR82X:
313 		di->vsel_min = 712500;
314 		di->vsel_step = 12500;
315 		break;
316 	default:
317 		dev_err(di->dev,
318 			"Chip ID %d not supported!\n", di->chip_id);
319 		return -EINVAL;
320 	}
321 
322 	di->vsel_count = FAN53555_NVOLTAGES;
323 
324 	return 0;
325 }
326 
327 /* For 00,01,03,05 options:
328  * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
329  * For 04 option:
330  * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
331  * */
332 static int fan53555_device_setup(struct fan53555_device_info *di,
333 				struct fan53555_platform_data *pdata)
334 {
335 	int ret = 0;
336 
337 	/* Setup voltage control register */
338 	switch (pdata->sleep_vsel_id) {
339 	case FAN53555_VSEL_ID_0:
340 		di->sleep_reg = FAN53555_VSEL0;
341 		di->vol_reg = FAN53555_VSEL1;
342 		break;
343 	case FAN53555_VSEL_ID_1:
344 		di->sleep_reg = FAN53555_VSEL1;
345 		di->vol_reg = FAN53555_VSEL0;
346 		break;
347 	default:
348 		dev_err(di->dev, "Invalid VSEL ID!\n");
349 		return -EINVAL;
350 	}
351 
352 	/* Setup mode control register */
353 	switch (di->vendor) {
354 	case FAN53526_VENDOR_FAIRCHILD:
355 		di->mode_reg = FAN53555_CONTROL;
356 
357 		switch (pdata->sleep_vsel_id) {
358 		case FAN53555_VSEL_ID_0:
359 			di->mode_mask = CTL_MODE_VSEL1_MODE;
360 			break;
361 		case FAN53555_VSEL_ID_1:
362 			di->mode_mask = CTL_MODE_VSEL0_MODE;
363 			break;
364 		}
365 		break;
366 	case FAN53555_VENDOR_FAIRCHILD:
367 	case FAN53555_VENDOR_SILERGY:
368 		di->mode_reg = di->vol_reg;
369 		di->mode_mask = VSEL_MODE;
370 		break;
371 	default:
372 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
373 		return -EINVAL;
374 	}
375 
376 	/* Setup voltage range */
377 	switch (di->vendor) {
378 	case FAN53526_VENDOR_FAIRCHILD:
379 		ret = fan53526_voltages_setup_fairchild(di);
380 		break;
381 	case FAN53555_VENDOR_FAIRCHILD:
382 		ret = fan53555_voltages_setup_fairchild(di);
383 		break;
384 	case FAN53555_VENDOR_SILERGY:
385 		ret = fan53555_voltages_setup_silergy(di);
386 		break;
387 	default:
388 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
389 		return -EINVAL;
390 	}
391 
392 	return ret;
393 }
394 
395 static int fan53555_regulator_register(struct fan53555_device_info *di,
396 			struct regulator_config *config)
397 {
398 	struct regulator_desc *rdesc = &di->desc;
399 
400 	rdesc->name = "fan53555-reg";
401 	rdesc->supply_name = "vin";
402 	rdesc->ops = &fan53555_regulator_ops;
403 	rdesc->type = REGULATOR_VOLTAGE;
404 	rdesc->n_voltages = di->vsel_count;
405 	rdesc->enable_reg = di->vol_reg;
406 	rdesc->enable_mask = VSEL_BUCK_EN;
407 	rdesc->min_uV = di->vsel_min;
408 	rdesc->uV_step = di->vsel_step;
409 	rdesc->vsel_reg = di->vol_reg;
410 	rdesc->vsel_mask = di->vsel_count - 1;
411 	rdesc->owner = THIS_MODULE;
412 
413 	di->rdev = devm_regulator_register(di->dev, &di->desc, config);
414 	return PTR_ERR_OR_ZERO(di->rdev);
415 }
416 
417 static const struct regmap_config fan53555_regmap_config = {
418 	.reg_bits = 8,
419 	.val_bits = 8,
420 };
421 
422 static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
423 					      struct device_node *np,
424 					      const struct regulator_desc *desc)
425 {
426 	struct fan53555_platform_data *pdata;
427 	int ret;
428 	u32 tmp;
429 
430 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
431 	if (!pdata)
432 		return NULL;
433 
434 	pdata->regulator = of_get_regulator_init_data(dev, np, desc);
435 
436 	ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
437 				   &tmp);
438 	if (!ret)
439 		pdata->sleep_vsel_id = tmp;
440 
441 	return pdata;
442 }
443 
444 static const struct of_device_id fan53555_dt_ids[] = {
445 	{
446 		.compatible = "fcs,fan53526",
447 		.data = (void *)FAN53526_VENDOR_FAIRCHILD,
448 	}, {
449 		.compatible = "fcs,fan53555",
450 		.data = (void *)FAN53555_VENDOR_FAIRCHILD
451 	}, {
452 		.compatible = "silergy,syr827",
453 		.data = (void *)FAN53555_VENDOR_SILERGY,
454 	}, {
455 		.compatible = "silergy,syr828",
456 		.data = (void *)FAN53555_VENDOR_SILERGY,
457 	},
458 	{ }
459 };
460 MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
461 
462 static int fan53555_regulator_probe(struct i2c_client *client,
463 				const struct i2c_device_id *id)
464 {
465 	struct device_node *np = client->dev.of_node;
466 	struct fan53555_device_info *di;
467 	struct fan53555_platform_data *pdata;
468 	struct regulator_config config = { };
469 	unsigned int val;
470 	int ret;
471 
472 	di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
473 					GFP_KERNEL);
474 	if (!di)
475 		return -ENOMEM;
476 
477 	pdata = dev_get_platdata(&client->dev);
478 	if (!pdata)
479 		pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
480 
481 	if (!pdata || !pdata->regulator) {
482 		dev_err(&client->dev, "Platform data not found!\n");
483 		return -ENODEV;
484 	}
485 
486 	di->regulator = pdata->regulator;
487 	if (client->dev.of_node) {
488 		di->vendor =
489 			(unsigned long)of_device_get_match_data(&client->dev);
490 	} else {
491 		/* if no ramp constraint set, get the pdata ramp_delay */
492 		if (!di->regulator->constraints.ramp_delay) {
493 			if (pdata->slew_rate >= ARRAY_SIZE(slew_rates)) {
494 				dev_err(&client->dev, "Invalid slew_rate\n");
495 				return -EINVAL;
496 			}
497 
498 			di->regulator->constraints.ramp_delay
499 					= slew_rates[pdata->slew_rate];
500 		}
501 
502 		di->vendor = id->driver_data;
503 	}
504 
505 	di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
506 	if (IS_ERR(di->regmap)) {
507 		dev_err(&client->dev, "Failed to allocate regmap!\n");
508 		return PTR_ERR(di->regmap);
509 	}
510 	di->dev = &client->dev;
511 	i2c_set_clientdata(client, di);
512 	/* Get chip ID */
513 	ret = regmap_read(di->regmap, FAN53555_ID1, &val);
514 	if (ret < 0) {
515 		dev_err(&client->dev, "Failed to get chip ID!\n");
516 		return ret;
517 	}
518 	di->chip_id = val & DIE_ID;
519 	/* Get chip revision */
520 	ret = regmap_read(di->regmap, FAN53555_ID2, &val);
521 	if (ret < 0) {
522 		dev_err(&client->dev, "Failed to get chip Rev!\n");
523 		return ret;
524 	}
525 	di->chip_rev = val & DIE_REV;
526 	dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
527 				di->chip_id, di->chip_rev);
528 	/* Device init */
529 	ret = fan53555_device_setup(di, pdata);
530 	if (ret < 0) {
531 		dev_err(&client->dev, "Failed to setup device!\n");
532 		return ret;
533 	}
534 	/* Register regulator */
535 	config.dev = di->dev;
536 	config.init_data = di->regulator;
537 	config.regmap = di->regmap;
538 	config.driver_data = di;
539 	config.of_node = np;
540 
541 	ret = fan53555_regulator_register(di, &config);
542 	if (ret < 0)
543 		dev_err(&client->dev, "Failed to register regulator!\n");
544 	return ret;
545 
546 }
547 
548 static const struct i2c_device_id fan53555_id[] = {
549 	{
550 		.name = "fan53526",
551 		.driver_data = FAN53526_VENDOR_FAIRCHILD
552 	}, {
553 		.name = "fan53555",
554 		.driver_data = FAN53555_VENDOR_FAIRCHILD
555 	}, {
556 		.name = "syr827",
557 		.driver_data = FAN53555_VENDOR_SILERGY
558 	}, {
559 		.name = "syr828",
560 		.driver_data = FAN53555_VENDOR_SILERGY
561 	},
562 	{ },
563 };
564 MODULE_DEVICE_TABLE(i2c, fan53555_id);
565 
566 static struct i2c_driver fan53555_regulator_driver = {
567 	.driver = {
568 		.name = "fan53555-regulator",
569 		.of_match_table = of_match_ptr(fan53555_dt_ids),
570 	},
571 	.probe = fan53555_regulator_probe,
572 	.id_table = fan53555_id,
573 };
574 
575 module_i2c_driver(fan53555_regulator_driver);
576 
577 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
578 MODULE_DESCRIPTION("FAN53555 regulator driver");
579 MODULE_LICENSE("GPL v2");
580