xref: /openbmc/linux/drivers/regulator/fan53555.c (revision 045a44d4)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
4 //
5 // Supported Part Numbers:
6 // FAN53555UC00X/01X/03X/04X/05X
7 //
8 // Copyright (c) 2012 Marvell Technology Ltd.
9 // Yunfan Zhang <yfzhang@marvell.com>
10 
11 #include <linux/bitops.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/param.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/fan53555.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/slab.h>
24 
25 /* Voltage setting */
26 #define FAN53555_VSEL0		0x00
27 #define FAN53555_VSEL1		0x01
28 
29 #define RK8602_VSEL0		0x06
30 #define RK8602_VSEL1		0x07
31 
32 #define TCS4525_VSEL0		0x11
33 #define TCS4525_VSEL1		0x10
34 #define TCS4525_TIME		0x13
35 #define TCS4525_COMMAND		0x14
36 
37 /* Control register */
38 #define FAN53555_CONTROL	0x02
39 /* IC Type */
40 #define FAN53555_ID1		0x03
41 /* IC mask version */
42 #define FAN53555_ID2		0x04
43 /* Monitor register */
44 #define FAN53555_MONITOR	0x05
45 
46 /* VSEL bit definitions */
47 #define VSEL_BUCK_EN		BIT(7)
48 #define VSEL_MODE		BIT(6)
49 /* Chip ID and Verison */
50 #define DIE_ID			0x0F	/* ID1 */
51 #define DIE_REV			0x0F	/* ID2 */
52 /* Control bit definitions */
53 #define CTL_OUTPUT_DISCHG	BIT(7)
54 #define CTL_SLEW_MASK		GENMASK(6, 4)
55 #define CTL_RESET		BIT(2)
56 #define CTL_MODE_VSEL0_MODE	BIT(0)
57 #define CTL_MODE_VSEL1_MODE	BIT(1)
58 
59 #define FAN53555_NVOLTAGES	64	/* Numbers of voltages */
60 #define FAN53526_NVOLTAGES	128
61 #define RK8602_NVOLTAGES	160
62 
63 #define TCS_VSEL0_MODE		BIT(7)
64 #define TCS_VSEL1_MODE		BIT(6)
65 
66 #define TCS_SLEW_MASK		GENMASK(4, 3)
67 
68 enum fan53555_vendor {
69 	FAN53526_VENDOR_FAIRCHILD = 0,
70 	FAN53555_VENDOR_FAIRCHILD,
71 	FAN53555_VENDOR_ROCKCHIP,	/* RK8600, RK8601 */
72 	RK8602_VENDOR_ROCKCHIP,		/* RK8602, RK8603 */
73 	FAN53555_VENDOR_SILERGY,
74 	FAN53526_VENDOR_TCS,
75 };
76 
77 enum {
78 	FAN53526_CHIP_ID_01 = 1,
79 };
80 
81 enum {
82 	FAN53526_CHIP_REV_08 = 8,
83 };
84 
85 /* IC Type */
86 enum {
87 	FAN53555_CHIP_ID_00 = 0,
88 	FAN53555_CHIP_ID_01,
89 	FAN53555_CHIP_ID_02,
90 	FAN53555_CHIP_ID_03,
91 	FAN53555_CHIP_ID_04,
92 	FAN53555_CHIP_ID_05,
93 	FAN53555_CHIP_ID_08 = 8,
94 };
95 
96 enum {
97 	RK8600_CHIP_ID_08 = 8,		/* RK8600, RK8601 */
98 };
99 
100 enum {
101 	RK8602_CHIP_ID_10 = 10,		/* RK8602, RK8603 */
102 };
103 
104 enum {
105 	TCS4525_CHIP_ID_12 = 12,
106 };
107 
108 enum {
109 	TCS4526_CHIP_ID_00 = 0,
110 };
111 
112 /* IC mask revision */
113 enum {
114 	FAN53555_CHIP_REV_00 = 0x3,
115 	FAN53555_CHIP_REV_13 = 0xf,
116 };
117 
118 enum {
119 	SILERGY_SYR82X = 8,
120 	SILERGY_SYR83X = 9,
121 };
122 
123 struct fan53555_device_info {
124 	enum fan53555_vendor vendor;
125 	struct device *dev;
126 	struct regulator_desc desc;
127 	struct regulator_init_data *regulator;
128 	/* IC Type and Rev */
129 	int chip_id;
130 	int chip_rev;
131 	/* Voltage setting register */
132 	unsigned int vol_reg;
133 	unsigned int sleep_reg;
134 	unsigned int en_reg;
135 	unsigned int sleep_en_reg;
136 	/* Voltage range and step(linear) */
137 	unsigned int vsel_min;
138 	unsigned int vsel_step;
139 	unsigned int vsel_count;
140 	/* Mode */
141 	unsigned int mode_reg;
142 	unsigned int mode_mask;
143 	/* Sleep voltage cache */
144 	unsigned int sleep_vol_cache;
145 	/* Slew rate */
146 	unsigned int slew_reg;
147 	unsigned int slew_mask;
148 	const unsigned int *ramp_delay_table;
149 	unsigned int n_ramp_values;
150 	unsigned int slew_rate;
151 };
152 
fan53555_set_suspend_voltage(struct regulator_dev * rdev,int uV)153 static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
154 {
155 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
156 	int ret;
157 
158 	if (di->sleep_vol_cache == uV)
159 		return 0;
160 	ret = regulator_map_voltage_linear(rdev, uV, uV);
161 	if (ret < 0)
162 		return ret;
163 	ret = regmap_update_bits(rdev->regmap, di->sleep_reg,
164 				 di->desc.vsel_mask, ret);
165 	if (ret < 0)
166 		return ret;
167 	/* Cache the sleep voltage setting.
168 	 * Might not be the real voltage which is rounded */
169 	di->sleep_vol_cache = uV;
170 
171 	return 0;
172 }
173 
fan53555_set_suspend_enable(struct regulator_dev * rdev)174 static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
175 {
176 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
177 
178 	return regmap_update_bits(rdev->regmap, di->sleep_en_reg,
179 				  VSEL_BUCK_EN, VSEL_BUCK_EN);
180 }
181 
fan53555_set_suspend_disable(struct regulator_dev * rdev)182 static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
183 {
184 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
185 
186 	return regmap_update_bits(rdev->regmap, di->sleep_en_reg,
187 				  VSEL_BUCK_EN, 0);
188 }
189 
fan53555_set_mode(struct regulator_dev * rdev,unsigned int mode)190 static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
191 {
192 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
193 
194 	switch (mode) {
195 	case REGULATOR_MODE_FAST:
196 		regmap_update_bits(rdev->regmap, di->mode_reg,
197 				   di->mode_mask, di->mode_mask);
198 		break;
199 	case REGULATOR_MODE_NORMAL:
200 		regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0);
201 		break;
202 	default:
203 		return -EINVAL;
204 	}
205 	return 0;
206 }
207 
fan53555_get_mode(struct regulator_dev * rdev)208 static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
209 {
210 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
211 	unsigned int val;
212 	int ret = 0;
213 
214 	ret = regmap_read(rdev->regmap, di->mode_reg, &val);
215 	if (ret < 0)
216 		return ret;
217 	if (val & di->mode_mask)
218 		return REGULATOR_MODE_FAST;
219 	else
220 		return REGULATOR_MODE_NORMAL;
221 }
222 
223 static const unsigned int slew_rates[] = {
224 	64000,
225 	32000,
226 	16000,
227 	 8000,
228 	 4000,
229 	 2000,
230 	 1000,
231 	  500,
232 };
233 
234 static const unsigned int tcs_slew_rates[] = {
235 	18700,
236 	 9300,
237 	 4600,
238 	 2300,
239 };
240 
241 static const struct regulator_ops fan53555_regulator_ops = {
242 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
243 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
244 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
245 	.map_voltage = regulator_map_voltage_linear,
246 	.list_voltage = regulator_list_voltage_linear,
247 	.set_suspend_voltage = fan53555_set_suspend_voltage,
248 	.enable = regulator_enable_regmap,
249 	.disable = regulator_disable_regmap,
250 	.is_enabled = regulator_is_enabled_regmap,
251 	.set_mode = fan53555_set_mode,
252 	.get_mode = fan53555_get_mode,
253 	.set_ramp_delay = regulator_set_ramp_delay_regmap,
254 	.set_suspend_enable = fan53555_set_suspend_enable,
255 	.set_suspend_disable = fan53555_set_suspend_disable,
256 };
257 
fan53526_voltages_setup_fairchild(struct fan53555_device_info * di)258 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
259 {
260 	/* Init voltage range and step */
261 	switch (di->chip_id) {
262 	case FAN53526_CHIP_ID_01:
263 		switch (di->chip_rev) {
264 		case FAN53526_CHIP_REV_08:
265 			di->vsel_min = 600000;
266 			di->vsel_step = 6250;
267 			break;
268 		default:
269 			dev_err(di->dev,
270 				"Chip ID %d with rev %d not supported!\n",
271 				di->chip_id, di->chip_rev);
272 			return -EINVAL;
273 		}
274 		break;
275 	default:
276 		dev_err(di->dev,
277 			"Chip ID %d not supported!\n", di->chip_id);
278 		return -EINVAL;
279 	}
280 
281 	di->slew_reg = FAN53555_CONTROL;
282 	di->slew_mask = CTL_SLEW_MASK;
283 	di->ramp_delay_table = slew_rates;
284 	di->n_ramp_values = ARRAY_SIZE(slew_rates);
285 	di->vsel_count = FAN53526_NVOLTAGES;
286 
287 	return 0;
288 }
289 
fan53555_voltages_setup_fairchild(struct fan53555_device_info * di)290 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
291 {
292 	/* Init voltage range and step */
293 	switch (di->chip_id) {
294 	case FAN53555_CHIP_ID_00:
295 		switch (di->chip_rev) {
296 		case FAN53555_CHIP_REV_00:
297 			di->vsel_min = 600000;
298 			di->vsel_step = 10000;
299 			break;
300 		case FAN53555_CHIP_REV_13:
301 			di->vsel_min = 800000;
302 			di->vsel_step = 10000;
303 			break;
304 		default:
305 			dev_err(di->dev,
306 				"Chip ID %d with rev %d not supported!\n",
307 				di->chip_id, di->chip_rev);
308 			return -EINVAL;
309 		}
310 		break;
311 	case FAN53555_CHIP_ID_01:
312 	case FAN53555_CHIP_ID_03:
313 	case FAN53555_CHIP_ID_05:
314 	case FAN53555_CHIP_ID_08:
315 		di->vsel_min = 600000;
316 		di->vsel_step = 10000;
317 		break;
318 	case FAN53555_CHIP_ID_04:
319 		di->vsel_min = 603000;
320 		di->vsel_step = 12826;
321 		break;
322 	default:
323 		dev_err(di->dev,
324 			"Chip ID %d not supported!\n", di->chip_id);
325 		return -EINVAL;
326 	}
327 	di->slew_reg = FAN53555_CONTROL;
328 	di->slew_mask = CTL_SLEW_MASK;
329 	di->ramp_delay_table = slew_rates;
330 	di->n_ramp_values = ARRAY_SIZE(slew_rates);
331 	di->vsel_count = FAN53555_NVOLTAGES;
332 
333 	return 0;
334 }
335 
fan53555_voltages_setup_rockchip(struct fan53555_device_info * di)336 static int fan53555_voltages_setup_rockchip(struct fan53555_device_info *di)
337 {
338 	/* Init voltage range and step */
339 	switch (di->chip_id) {
340 	case RK8600_CHIP_ID_08:
341 		di->vsel_min = 712500;
342 		di->vsel_step = 12500;
343 		break;
344 	default:
345 		dev_err(di->dev,
346 			"Chip ID %d not supported!\n", di->chip_id);
347 		return -EINVAL;
348 	}
349 	di->slew_reg = FAN53555_CONTROL;
350 	di->slew_mask = CTL_SLEW_MASK;
351 	di->ramp_delay_table = slew_rates;
352 	di->n_ramp_values = ARRAY_SIZE(slew_rates);
353 	di->vsel_count = FAN53555_NVOLTAGES;
354 
355 	return 0;
356 }
357 
rk8602_voltages_setup_rockchip(struct fan53555_device_info * di)358 static int rk8602_voltages_setup_rockchip(struct fan53555_device_info *di)
359 {
360 	/* Init voltage range and step */
361 	switch (di->chip_id) {
362 	case RK8602_CHIP_ID_10:
363 		di->vsel_min = 500000;
364 		di->vsel_step = 6250;
365 		break;
366 	default:
367 		dev_err(di->dev,
368 			"Chip ID %d not supported!\n", di->chip_id);
369 		return -EINVAL;
370 	}
371 	di->slew_reg = FAN53555_CONTROL;
372 	di->slew_mask = CTL_SLEW_MASK;
373 	di->ramp_delay_table = slew_rates;
374 	di->n_ramp_values = ARRAY_SIZE(slew_rates);
375 	di->vsel_count = RK8602_NVOLTAGES;
376 
377 	return 0;
378 }
379 
fan53555_voltages_setup_silergy(struct fan53555_device_info * di)380 static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
381 {
382 	/* Init voltage range and step */
383 	switch (di->chip_id) {
384 	case SILERGY_SYR82X:
385 	case SILERGY_SYR83X:
386 		di->vsel_min = 712500;
387 		di->vsel_step = 12500;
388 		break;
389 	default:
390 		dev_err(di->dev,
391 			"Chip ID %d not supported!\n", di->chip_id);
392 		return -EINVAL;
393 	}
394 	di->slew_reg = FAN53555_CONTROL;
395 	di->slew_mask = CTL_SLEW_MASK;
396 	di->ramp_delay_table = slew_rates;
397 	di->n_ramp_values = ARRAY_SIZE(slew_rates);
398 	di->vsel_count = FAN53555_NVOLTAGES;
399 
400 	return 0;
401 }
402 
fan53526_voltages_setup_tcs(struct fan53555_device_info * di)403 static int fan53526_voltages_setup_tcs(struct fan53555_device_info *di)
404 {
405 	switch (di->chip_id) {
406 	case TCS4525_CHIP_ID_12:
407 	case TCS4526_CHIP_ID_00:
408 		di->slew_reg = TCS4525_TIME;
409 		di->slew_mask = TCS_SLEW_MASK;
410 		di->ramp_delay_table = tcs_slew_rates;
411 		di->n_ramp_values = ARRAY_SIZE(tcs_slew_rates);
412 
413 		/* Init voltage range and step */
414 		di->vsel_min = 600000;
415 		di->vsel_step = 6250;
416 		di->vsel_count = FAN53526_NVOLTAGES;
417 		break;
418 	default:
419 		dev_err(di->dev, "Chip ID %d not supported!\n", di->chip_id);
420 		return -EINVAL;
421 	}
422 
423 	return 0;
424 }
425 
426 /* For 00,01,03,05 options:
427  * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
428  * For 04 option:
429  * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
430  * */
fan53555_device_setup(struct fan53555_device_info * di,struct fan53555_platform_data * pdata)431 static int fan53555_device_setup(struct fan53555_device_info *di,
432 				struct fan53555_platform_data *pdata)
433 {
434 	int ret = 0;
435 
436 	/* Setup voltage control register */
437 	switch (di->vendor) {
438 	case FAN53526_VENDOR_FAIRCHILD:
439 	case FAN53555_VENDOR_FAIRCHILD:
440 	case FAN53555_VENDOR_ROCKCHIP:
441 	case FAN53555_VENDOR_SILERGY:
442 		switch (pdata->sleep_vsel_id) {
443 		case FAN53555_VSEL_ID_0:
444 			di->sleep_reg = FAN53555_VSEL0;
445 			di->vol_reg = FAN53555_VSEL1;
446 			break;
447 		case FAN53555_VSEL_ID_1:
448 			di->sleep_reg = FAN53555_VSEL1;
449 			di->vol_reg = FAN53555_VSEL0;
450 			break;
451 		default:
452 			dev_err(di->dev, "Invalid VSEL ID!\n");
453 			return -EINVAL;
454 		}
455 		di->sleep_en_reg = di->sleep_reg;
456 		di->en_reg = di->vol_reg;
457 		break;
458 	case RK8602_VENDOR_ROCKCHIP:
459 		switch (pdata->sleep_vsel_id) {
460 		case FAN53555_VSEL_ID_0:
461 			di->sleep_reg = RK8602_VSEL0;
462 			di->vol_reg = RK8602_VSEL1;
463 			di->sleep_en_reg = FAN53555_VSEL0;
464 			di->en_reg = FAN53555_VSEL1;
465 			break;
466 		case FAN53555_VSEL_ID_1:
467 			di->sleep_reg = RK8602_VSEL1;
468 			di->vol_reg = RK8602_VSEL0;
469 			di->sleep_en_reg = FAN53555_VSEL1;
470 			di->en_reg = FAN53555_VSEL0;
471 			break;
472 		default:
473 			dev_err(di->dev, "Invalid VSEL ID!\n");
474 			return -EINVAL;
475 		}
476 		break;
477 	case FAN53526_VENDOR_TCS:
478 		switch (pdata->sleep_vsel_id) {
479 		case FAN53555_VSEL_ID_0:
480 			di->sleep_reg = TCS4525_VSEL0;
481 			di->vol_reg = TCS4525_VSEL1;
482 			break;
483 		case FAN53555_VSEL_ID_1:
484 			di->sleep_reg = TCS4525_VSEL1;
485 			di->vol_reg = TCS4525_VSEL0;
486 			break;
487 		default:
488 			dev_err(di->dev, "Invalid VSEL ID!\n");
489 			return -EINVAL;
490 		}
491 		di->sleep_en_reg = di->sleep_reg;
492 		di->en_reg = di->vol_reg;
493 		break;
494 	default:
495 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
496 		return -EINVAL;
497 	}
498 
499 	/* Setup mode control register */
500 	switch (di->vendor) {
501 	case FAN53526_VENDOR_FAIRCHILD:
502 		di->mode_reg = FAN53555_CONTROL;
503 
504 		switch (pdata->sleep_vsel_id) {
505 		case FAN53555_VSEL_ID_0:
506 			di->mode_mask = CTL_MODE_VSEL1_MODE;
507 			break;
508 		case FAN53555_VSEL_ID_1:
509 			di->mode_mask = CTL_MODE_VSEL0_MODE;
510 			break;
511 		}
512 		break;
513 	case FAN53555_VENDOR_FAIRCHILD:
514 	case FAN53555_VENDOR_ROCKCHIP:
515 	case FAN53555_VENDOR_SILERGY:
516 		di->mode_reg = di->vol_reg;
517 		di->mode_mask = VSEL_MODE;
518 		break;
519 	case RK8602_VENDOR_ROCKCHIP:
520 		di->mode_mask = VSEL_MODE;
521 
522 		switch (pdata->sleep_vsel_id) {
523 		case FAN53555_VSEL_ID_0:
524 			di->mode_reg = FAN53555_VSEL1;
525 			break;
526 		case FAN53555_VSEL_ID_1:
527 			di->mode_reg = FAN53555_VSEL0;
528 			break;
529 		}
530 		break;
531 	case FAN53526_VENDOR_TCS:
532 		di->mode_reg = TCS4525_COMMAND;
533 
534 		switch (pdata->sleep_vsel_id) {
535 		case FAN53555_VSEL_ID_0:
536 			di->mode_mask = TCS_VSEL1_MODE;
537 			break;
538 		case FAN53555_VSEL_ID_1:
539 			di->mode_mask = TCS_VSEL0_MODE;
540 			break;
541 		}
542 		break;
543 	default:
544 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
545 		return -EINVAL;
546 	}
547 
548 	/* Setup voltage range */
549 	switch (di->vendor) {
550 	case FAN53526_VENDOR_FAIRCHILD:
551 		ret = fan53526_voltages_setup_fairchild(di);
552 		break;
553 	case FAN53555_VENDOR_FAIRCHILD:
554 		ret = fan53555_voltages_setup_fairchild(di);
555 		break;
556 	case FAN53555_VENDOR_ROCKCHIP:
557 		ret = fan53555_voltages_setup_rockchip(di);
558 		break;
559 	case RK8602_VENDOR_ROCKCHIP:
560 		ret = rk8602_voltages_setup_rockchip(di);
561 		break;
562 	case FAN53555_VENDOR_SILERGY:
563 		ret = fan53555_voltages_setup_silergy(di);
564 		break;
565 	case FAN53526_VENDOR_TCS:
566 		ret = fan53526_voltages_setup_tcs(di);
567 		break;
568 	default:
569 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
570 		return -EINVAL;
571 	}
572 
573 	return ret;
574 }
575 
fan53555_regulator_register(struct fan53555_device_info * di,struct regulator_config * config)576 static int fan53555_regulator_register(struct fan53555_device_info *di,
577 			struct regulator_config *config)
578 {
579 	struct regulator_desc *rdesc = &di->desc;
580 	struct regulator_dev *rdev;
581 
582 	rdesc->name = "fan53555-reg";
583 	rdesc->supply_name = "vin";
584 	rdesc->ops = &fan53555_regulator_ops;
585 	rdesc->type = REGULATOR_VOLTAGE;
586 	rdesc->n_voltages = di->vsel_count;
587 	rdesc->enable_reg = di->en_reg;
588 	rdesc->enable_mask = VSEL_BUCK_EN;
589 	rdesc->min_uV = di->vsel_min;
590 	rdesc->uV_step = di->vsel_step;
591 	rdesc->vsel_reg = di->vol_reg;
592 	rdesc->vsel_mask = BIT(fls(di->vsel_count - 1)) - 1;
593 	rdesc->ramp_reg = di->slew_reg;
594 	rdesc->ramp_mask = di->slew_mask;
595 	rdesc->ramp_delay_table = di->ramp_delay_table;
596 	rdesc->n_ramp_values = di->n_ramp_values;
597 	rdesc->owner = THIS_MODULE;
598 
599 	rdev = devm_regulator_register(di->dev, &di->desc, config);
600 	return PTR_ERR_OR_ZERO(rdev);
601 }
602 
603 static const struct regmap_config fan53555_regmap_config = {
604 	.reg_bits = 8,
605 	.val_bits = 8,
606 };
607 
fan53555_parse_dt(struct device * dev,struct device_node * np,const struct regulator_desc * desc)608 static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
609 					      struct device_node *np,
610 					      const struct regulator_desc *desc)
611 {
612 	struct fan53555_platform_data *pdata;
613 	int ret;
614 	u32 tmp;
615 
616 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
617 	if (!pdata)
618 		return NULL;
619 
620 	pdata->regulator = of_get_regulator_init_data(dev, np, desc);
621 
622 	ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
623 				   &tmp);
624 	if (!ret)
625 		pdata->sleep_vsel_id = tmp;
626 
627 	return pdata;
628 }
629 
630 static const struct of_device_id __maybe_unused fan53555_dt_ids[] = {
631 	{
632 		.compatible = "fcs,fan53526",
633 		.data = (void *)FAN53526_VENDOR_FAIRCHILD,
634 	}, {
635 		.compatible = "fcs,fan53555",
636 		.data = (void *)FAN53555_VENDOR_FAIRCHILD
637 	}, {
638 		.compatible = "rockchip,rk8600",
639 		.data = (void *)FAN53555_VENDOR_ROCKCHIP
640 	}, {
641 		.compatible = "rockchip,rk8602",
642 		.data = (void *)RK8602_VENDOR_ROCKCHIP
643 	}, {
644 		.compatible = "silergy,syr827",
645 		.data = (void *)FAN53555_VENDOR_SILERGY,
646 	}, {
647 		.compatible = "silergy,syr828",
648 		.data = (void *)FAN53555_VENDOR_SILERGY,
649 	}, {
650 		.compatible = "tcs,tcs4525",
651 		.data = (void *)FAN53526_VENDOR_TCS
652 	}, {
653 		.compatible = "tcs,tcs4526",
654 		.data = (void *)FAN53526_VENDOR_TCS
655 	},
656 	{ }
657 };
658 MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
659 
fan53555_regulator_probe(struct i2c_client * client)660 static int fan53555_regulator_probe(struct i2c_client *client)
661 {
662 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
663 	struct device_node *np = client->dev.of_node;
664 	struct fan53555_device_info *di;
665 	struct fan53555_platform_data *pdata;
666 	struct regulator_config config = { };
667 	struct regmap *regmap;
668 	unsigned int val;
669 	int ret;
670 
671 	di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
672 					GFP_KERNEL);
673 	if (!di)
674 		return -ENOMEM;
675 
676 	pdata = dev_get_platdata(&client->dev);
677 	if (!pdata)
678 		pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
679 
680 	if (!pdata || !pdata->regulator)
681 		return dev_err_probe(&client->dev, -ENODEV,
682 				     "Platform data not found!\n");
683 
684 	di->regulator = pdata->regulator;
685 	if (client->dev.of_node) {
686 		di->vendor =
687 			(unsigned long)of_device_get_match_data(&client->dev);
688 	} else {
689 		/* if no ramp constraint set, get the pdata ramp_delay */
690 		if (!di->regulator->constraints.ramp_delay) {
691 			if (pdata->slew_rate >= ARRAY_SIZE(slew_rates))
692 				return dev_err_probe(&client->dev, -EINVAL,
693 						     "Invalid slew_rate\n");
694 
695 			di->regulator->constraints.ramp_delay
696 					= slew_rates[pdata->slew_rate];
697 		}
698 
699 		di->vendor = id->driver_data;
700 	}
701 
702 	regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
703 	if (IS_ERR(regmap))
704 		return dev_err_probe(&client->dev, PTR_ERR(regmap),
705 				     "Failed to allocate regmap!\n");
706 
707 	di->dev = &client->dev;
708 	i2c_set_clientdata(client, di);
709 	/* Get chip ID */
710 	ret = regmap_read(regmap, FAN53555_ID1, &val);
711 	if (ret < 0)
712 		return dev_err_probe(&client->dev, ret, "Failed to get chip ID!\n");
713 
714 	di->chip_id = val & DIE_ID;
715 	/* Get chip revision */
716 	ret = regmap_read(regmap, FAN53555_ID2, &val);
717 	if (ret < 0)
718 		return dev_err_probe(&client->dev, ret, "Failed to get chip Rev!\n");
719 
720 	di->chip_rev = val & DIE_REV;
721 	dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
722 				di->chip_id, di->chip_rev);
723 	/* Device init */
724 	ret = fan53555_device_setup(di, pdata);
725 	if (ret < 0)
726 		return dev_err_probe(&client->dev, ret, "Failed to setup device!\n");
727 
728 	/* Register regulator */
729 	config.dev = di->dev;
730 	config.init_data = di->regulator;
731 	config.regmap = regmap;
732 	config.driver_data = di;
733 	config.of_node = np;
734 
735 	ret = fan53555_regulator_register(di, &config);
736 	if (ret < 0)
737 		dev_err_probe(&client->dev, ret, "Failed to register regulator!\n");
738 
739 	return ret;
740 }
741 
742 static const struct i2c_device_id fan53555_id[] = {
743 	{
744 		.name = "fan53526",
745 		.driver_data = FAN53526_VENDOR_FAIRCHILD
746 	}, {
747 		.name = "fan53555",
748 		.driver_data = FAN53555_VENDOR_FAIRCHILD
749 	}, {
750 		.name = "rk8600",
751 		.driver_data = FAN53555_VENDOR_ROCKCHIP
752 	}, {
753 		.name = "rk8602",
754 		.driver_data = RK8602_VENDOR_ROCKCHIP
755 	}, {
756 		.name = "syr827",
757 		.driver_data = FAN53555_VENDOR_SILERGY
758 	}, {
759 		.name = "syr828",
760 		.driver_data = FAN53555_VENDOR_SILERGY
761 	}, {
762 		.name = "tcs4525",
763 		.driver_data = FAN53526_VENDOR_TCS
764 	}, {
765 		.name = "tcs4526",
766 		.driver_data = FAN53526_VENDOR_TCS
767 	},
768 	{ },
769 };
770 MODULE_DEVICE_TABLE(i2c, fan53555_id);
771 
772 static struct i2c_driver fan53555_regulator_driver = {
773 	.driver = {
774 		.name = "fan53555-regulator",
775 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
776 		.of_match_table = of_match_ptr(fan53555_dt_ids),
777 	},
778 	.probe = fan53555_regulator_probe,
779 	.id_table = fan53555_id,
780 };
781 
782 module_i2c_driver(fan53555_regulator_driver);
783 
784 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
785 MODULE_DESCRIPTION("FAN53555 regulator driver");
786 MODULE_LICENSE("GPL v2");
787