1 // SPDX-License-Identifier: GPL-2.0
3 // FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
5 // Supported Part Numbers:
6 // FAN53555UC00X/01X/03X/04X/05X
8 // Copyright (c) 2012 Marvell Technology Ltd.
9 // Yunfan Zhang <yfzhang@marvell.com>
11 #include <linux/bitops.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/module.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>
26 #define FAN53555_VSEL0 0x00
27 #define FAN53555_VSEL1 0x01
29 #define RK8602_VSEL0 0x06
30 #define RK8602_VSEL1 0x07
32 #define TCS4525_VSEL0 0x11
33 #define TCS4525_VSEL1 0x10
34 #define TCS4525_TIME 0x13
35 #define TCS4525_COMMAND 0x14
37 /* Control register */
38 #define FAN53555_CONTROL 0x02
40 #define FAN53555_ID1 0x03
42 #define FAN53555_ID2 0x04
43 /* Monitor register */
44 #define FAN53555_MONITOR 0x05
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)
59 #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
60 #define FAN53526_NVOLTAGES 128
61 #define RK8602_NVOLTAGES 160
63 #define TCS_VSEL0_MODE BIT(7)
64 #define TCS_VSEL1_MODE BIT(6)
66 #define TCS_SLEW_MASK GENMASK(4, 3)
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,
78 FAN53526_CHIP_ID_01 = 1,
82 FAN53526_CHIP_REV_08 = 8,
87 FAN53555_CHIP_ID_00 = 0,
93 FAN53555_CHIP_ID_08 = 8,
97 RK8600_CHIP_ID_08 = 8, /* RK8600, RK8601 */
101 RK8602_CHIP_ID_10 = 10, /* RK8602, RK8603 */
105 TCS4525_CHIP_ID_12 = 12,
109 TCS4526_CHIP_ID_00 = 0,
112 /* IC mask revision */
114 FAN53555_CHIP_REV_00 = 0x3,
115 FAN53555_CHIP_REV_13 = 0xf,
123 struct fan53555_device_info {
124 enum fan53555_vendor vendor;
126 struct regulator_desc desc;
127 struct regulator_init_data *regulator;
128 /* IC Type and Rev */
131 /* Voltage setting register */
132 unsigned int vol_reg;
133 unsigned int sleep_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;
141 unsigned int mode_reg;
142 unsigned int mode_mask;
143 /* Sleep voltage cache */
144 unsigned int sleep_vol_cache;
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;
153 static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
155 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
158 if (di->sleep_vol_cache == uV)
160 ret = regulator_map_voltage_linear(rdev, uV, uV);
163 ret = regmap_update_bits(rdev->regmap, di->sleep_reg,
164 di->desc.vsel_mask, ret);
167 /* Cache the sleep voltage setting.
168 * Might not be the real voltage which is rounded */
169 di->sleep_vol_cache = uV;
174 static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
176 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
178 return regmap_update_bits(rdev->regmap, di->sleep_en_reg,
179 VSEL_BUCK_EN, VSEL_BUCK_EN);
182 static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
184 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
186 return regmap_update_bits(rdev->regmap, di->sleep_en_reg,
190 static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
192 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
195 case REGULATOR_MODE_FAST:
196 regmap_update_bits(rdev->regmap, di->mode_reg,
197 di->mode_mask, di->mode_mask);
199 case REGULATOR_MODE_NORMAL:
200 regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0);
208 static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
210 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
214 ret = regmap_read(rdev->regmap, di->mode_reg, &val);
217 if (val & di->mode_mask)
218 return REGULATOR_MODE_FAST;
220 return REGULATOR_MODE_NORMAL;
223 static const unsigned int slew_rates[] = {
234 static const unsigned int tcs_slew_rates[] = {
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,
258 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
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;
270 "Chip ID %d with rev %d not supported!\n",
271 di->chip_id, di->chip_rev);
277 "Chip ID %d not supported!\n", di->chip_id);
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;
290 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
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;
300 case FAN53555_CHIP_REV_13:
301 di->vsel_min = 800000;
302 di->vsel_step = 10000;
306 "Chip ID %d with rev %d not supported!\n",
307 di->chip_id, di->chip_rev);
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;
318 case FAN53555_CHIP_ID_04:
319 di->vsel_min = 603000;
320 di->vsel_step = 12826;
324 "Chip ID %d not supported!\n", di->chip_id);
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;
336 static int fan53555_voltages_setup_rockchip(struct fan53555_device_info *di)
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;
346 "Chip ID %d not supported!\n", di->chip_id);
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;
358 static int rk8602_voltages_setup_rockchip(struct fan53555_device_info *di)
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;
368 "Chip ID %d not supported!\n", di->chip_id);
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;
380 static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
382 /* Init voltage range and step */
383 switch (di->chip_id) {
386 di->vsel_min = 712500;
387 di->vsel_step = 12500;
391 "Chip ID %d not supported!\n", di->chip_id);
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;
403 static int fan53526_voltages_setup_tcs(struct fan53555_device_info *di)
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);
413 /* Init voltage range and step */
414 di->vsel_min = 600000;
415 di->vsel_step = 6250;
416 di->vsel_count = FAN53526_NVOLTAGES;
419 dev_err(di->dev, "Chip ID %d not supported!\n", di->chip_id);
426 /* For 00,01,03,05 options:
427 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
429 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
431 static int fan53555_device_setup(struct fan53555_device_info *di,
432 struct fan53555_platform_data *pdata)
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;
447 case FAN53555_VSEL_ID_1:
448 di->sleep_reg = FAN53555_VSEL1;
449 di->vol_reg = FAN53555_VSEL0;
452 dev_err(di->dev, "Invalid VSEL ID!\n");
455 di->sleep_en_reg = di->sleep_reg;
456 di->en_reg = di->vol_reg;
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;
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;
473 dev_err(di->dev, "Invalid VSEL ID!\n");
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;
483 case FAN53555_VSEL_ID_1:
484 di->sleep_reg = TCS4525_VSEL1;
485 di->vol_reg = TCS4525_VSEL0;
488 dev_err(di->dev, "Invalid VSEL ID!\n");
491 di->sleep_en_reg = di->sleep_reg;
492 di->en_reg = di->vol_reg;
495 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
499 /* Setup mode control register */
500 switch (di->vendor) {
501 case FAN53526_VENDOR_FAIRCHILD:
502 di->mode_reg = FAN53555_CONTROL;
504 switch (pdata->sleep_vsel_id) {
505 case FAN53555_VSEL_ID_0:
506 di->mode_mask = CTL_MODE_VSEL1_MODE;
508 case FAN53555_VSEL_ID_1:
509 di->mode_mask = CTL_MODE_VSEL0_MODE;
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;
519 case RK8602_VENDOR_ROCKCHIP:
520 di->mode_mask = VSEL_MODE;
522 switch (pdata->sleep_vsel_id) {
523 case FAN53555_VSEL_ID_0:
524 di->mode_reg = FAN53555_VSEL1;
526 case FAN53555_VSEL_ID_1:
527 di->mode_reg = FAN53555_VSEL0;
531 case FAN53526_VENDOR_TCS:
532 di->mode_reg = TCS4525_COMMAND;
534 switch (pdata->sleep_vsel_id) {
535 case FAN53555_VSEL_ID_0:
536 di->mode_mask = TCS_VSEL1_MODE;
538 case FAN53555_VSEL_ID_1:
539 di->mode_mask = TCS_VSEL0_MODE;
544 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
548 /* Setup voltage range */
549 switch (di->vendor) {
550 case FAN53526_VENDOR_FAIRCHILD:
551 ret = fan53526_voltages_setup_fairchild(di);
553 case FAN53555_VENDOR_FAIRCHILD:
554 ret = fan53555_voltages_setup_fairchild(di);
556 case FAN53555_VENDOR_ROCKCHIP:
557 ret = fan53555_voltages_setup_rockchip(di);
559 case RK8602_VENDOR_ROCKCHIP:
560 ret = rk8602_voltages_setup_rockchip(di);
562 case FAN53555_VENDOR_SILERGY:
563 ret = fan53555_voltages_setup_silergy(di);
565 case FAN53526_VENDOR_TCS:
566 ret = fan53526_voltages_setup_tcs(di);
569 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
576 static int fan53555_regulator_register(struct fan53555_device_info *di,
577 struct regulator_config *config)
579 struct regulator_desc *rdesc = &di->desc;
580 struct regulator_dev *rdev;
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;
599 rdev = devm_regulator_register(di->dev, &di->desc, config);
600 return PTR_ERR_OR_ZERO(rdev);
603 static const struct regmap_config fan53555_regmap_config = {
608 static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
609 struct device_node *np,
610 const struct regulator_desc *desc)
612 struct fan53555_platform_data *pdata;
616 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
620 pdata->regulator = of_get_regulator_init_data(dev, np, desc);
622 ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
625 pdata->sleep_vsel_id = tmp;
630 static const struct of_device_id __maybe_unused fan53555_dt_ids[] = {
632 .compatible = "fcs,fan53526",
633 .data = (void *)FAN53526_VENDOR_FAIRCHILD,
635 .compatible = "fcs,fan53555",
636 .data = (void *)FAN53555_VENDOR_FAIRCHILD
638 .compatible = "rockchip,rk8600",
639 .data = (void *)FAN53555_VENDOR_ROCKCHIP
641 .compatible = "rockchip,rk8602",
642 .data = (void *)RK8602_VENDOR_ROCKCHIP
644 .compatible = "silergy,syr827",
645 .data = (void *)FAN53555_VENDOR_SILERGY,
647 .compatible = "silergy,syr828",
648 .data = (void *)FAN53555_VENDOR_SILERGY,
650 .compatible = "tcs,tcs4525",
651 .data = (void *)FAN53526_VENDOR_TCS
653 .compatible = "tcs,tcs4526",
654 .data = (void *)FAN53526_VENDOR_TCS
658 MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
660 static int fan53555_regulator_probe(struct i2c_client *client)
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;
671 di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
676 pdata = dev_get_platdata(&client->dev);
678 pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
680 if (!pdata || !pdata->regulator)
681 return dev_err_probe(&client->dev, -ENODEV,
682 "Platform data not found!\n");
684 di->regulator = pdata->regulator;
685 if (client->dev.of_node) {
687 (unsigned long)of_device_get_match_data(&client->dev);
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");
695 di->regulator->constraints.ramp_delay
696 = slew_rates[pdata->slew_rate];
699 di->vendor = id->driver_data;
702 regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
704 return dev_err_probe(&client->dev, PTR_ERR(regmap),
705 "Failed to allocate regmap!\n");
707 di->dev = &client->dev;
708 i2c_set_clientdata(client, di);
710 ret = regmap_read(regmap, FAN53555_ID1, &val);
712 return dev_err_probe(&client->dev, ret, "Failed to get chip ID!\n");
714 di->chip_id = val & DIE_ID;
715 /* Get chip revision */
716 ret = regmap_read(regmap, FAN53555_ID2, &val);
718 return dev_err_probe(&client->dev, ret, "Failed to get chip Rev!\n");
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);
724 ret = fan53555_device_setup(di, pdata);
726 return dev_err_probe(&client->dev, ret, "Failed to setup device!\n");
728 /* Register regulator */
729 config.dev = di->dev;
730 config.init_data = di->regulator;
731 config.regmap = regmap;
732 config.driver_data = di;
735 ret = fan53555_regulator_register(di, &config);
737 dev_err_probe(&client->dev, ret, "Failed to register regulator!\n");
742 static const struct i2c_device_id fan53555_id[] = {
745 .driver_data = FAN53526_VENDOR_FAIRCHILD
748 .driver_data = FAN53555_VENDOR_FAIRCHILD
751 .driver_data = FAN53555_VENDOR_ROCKCHIP
754 .driver_data = RK8602_VENDOR_ROCKCHIP
757 .driver_data = FAN53555_VENDOR_SILERGY
760 .driver_data = FAN53555_VENDOR_SILERGY
763 .driver_data = FAN53526_VENDOR_TCS
766 .driver_data = FAN53526_VENDOR_TCS
770 MODULE_DEVICE_TABLE(i2c, fan53555_id);
772 static struct i2c_driver fan53555_regulator_driver = {
774 .name = "fan53555-regulator",
775 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
776 .of_match_table = of_match_ptr(fan53555_dt_ids),
778 .probe = fan53555_regulator_probe,
779 .id_table = fan53555_id,
782 module_i2c_driver(fan53555_regulator_driver);
784 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
785 MODULE_DESCRIPTION("FAN53555 regulator driver");
786 MODULE_LICENSE("GPL v2");