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/module.h>
12 #include <linux/param.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/of_device.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/fan53555.h>
25 #define FAN53555_VSEL0 0x00
26 #define FAN53555_VSEL1 0x01
28 #define TCS4525_VSEL0 0x11
29 #define TCS4525_VSEL1 0x10
30 #define TCS4525_TIME 0x13
31 #define TCS4525_COMMAND 0x14
33 /* Control register */
34 #define FAN53555_CONTROL 0x02
36 #define FAN53555_ID1 0x03
38 #define FAN53555_ID2 0x04
39 /* Monitor register */
40 #define FAN53555_MONITOR 0x05
42 /* VSEL bit definitions */
43 #define VSEL_BUCK_EN (1 << 7)
44 #define VSEL_MODE (1 << 6)
45 /* Chip ID and Verison */
46 #define DIE_ID 0x0F /* ID1 */
47 #define DIE_REV 0x0F /* ID2 */
48 /* Control bit definitions */
49 #define CTL_OUTPUT_DISCHG (1 << 7)
50 #define CTL_SLEW_MASK (0x7 << 4)
51 #define CTL_SLEW_SHIFT 4
52 #define CTL_RESET (1 << 2)
53 #define CTL_MODE_VSEL0_MODE BIT(0)
54 #define CTL_MODE_VSEL1_MODE BIT(1)
56 #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
57 #define FAN53526_NVOLTAGES 128
59 #define TCS_VSEL0_MODE (1 << 7)
60 #define TCS_VSEL1_MODE (1 << 6)
62 #define TCS_SLEW_SHIFT 3
63 #define TCS_SLEW_MASK (0x3 < 3)
65 enum fan53555_vendor {
66 FAN53526_VENDOR_FAIRCHILD = 0,
67 FAN53555_VENDOR_FAIRCHILD,
68 FAN53555_VENDOR_SILERGY,
73 FAN53526_CHIP_ID_01 = 1,
77 FAN53526_CHIP_REV_08 = 8,
82 FAN53555_CHIP_ID_00 = 0,
88 FAN53555_CHIP_ID_08 = 8,
92 TCS4525_CHIP_ID_12 = 12,
96 TCS4526_CHIP_ID_00 = 0,
99 /* IC mask revision */
101 FAN53555_CHIP_REV_00 = 0x3,
102 FAN53555_CHIP_REV_13 = 0xf,
110 struct fan53555_device_info {
111 enum fan53555_vendor vendor;
113 struct regulator_desc desc;
114 struct regulator_init_data *regulator;
115 /* IC Type and Rev */
118 /* Voltage setting register */
119 unsigned int vol_reg;
120 unsigned int sleep_reg;
121 /* Voltage range and step(linear) */
122 unsigned int vsel_min;
123 unsigned int vsel_step;
124 unsigned int vsel_count;
126 unsigned int mode_reg;
127 unsigned int mode_mask;
128 /* Sleep voltage cache */
129 unsigned int sleep_vol_cache;
131 unsigned int slew_reg;
132 unsigned int slew_mask;
133 const unsigned int *ramp_delay_table;
134 unsigned int n_ramp_values;
135 unsigned int slew_rate;
138 static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
140 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
143 if (di->sleep_vol_cache == uV)
145 ret = regulator_map_voltage_linear(rdev, uV, uV);
148 ret = regmap_update_bits(rdev->regmap, di->sleep_reg,
149 di->desc.vsel_mask, ret);
152 /* Cache the sleep voltage setting.
153 * Might not be the real voltage which is rounded */
154 di->sleep_vol_cache = uV;
159 static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
161 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
163 return regmap_update_bits(rdev->regmap, di->sleep_reg,
164 VSEL_BUCK_EN, VSEL_BUCK_EN);
167 static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
169 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
171 return regmap_update_bits(rdev->regmap, di->sleep_reg,
175 static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
177 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
180 case REGULATOR_MODE_FAST:
181 regmap_update_bits(rdev->regmap, di->mode_reg,
182 di->mode_mask, di->mode_mask);
184 case REGULATOR_MODE_NORMAL:
185 regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0);
193 static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
195 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
199 ret = regmap_read(rdev->regmap, di->mode_reg, &val);
202 if (val & di->mode_mask)
203 return REGULATOR_MODE_FAST;
205 return REGULATOR_MODE_NORMAL;
208 static const unsigned int slew_rates[] = {
219 static const unsigned int tcs_slew_rates[] = {
226 static const struct regulator_ops fan53555_regulator_ops = {
227 .set_voltage_sel = regulator_set_voltage_sel_regmap,
228 .get_voltage_sel = regulator_get_voltage_sel_regmap,
229 .set_voltage_time_sel = regulator_set_voltage_time_sel,
230 .map_voltage = regulator_map_voltage_linear,
231 .list_voltage = regulator_list_voltage_linear,
232 .set_suspend_voltage = fan53555_set_suspend_voltage,
233 .enable = regulator_enable_regmap,
234 .disable = regulator_disable_regmap,
235 .is_enabled = regulator_is_enabled_regmap,
236 .set_mode = fan53555_set_mode,
237 .get_mode = fan53555_get_mode,
238 .set_ramp_delay = regulator_set_ramp_delay_regmap,
239 .set_suspend_enable = fan53555_set_suspend_enable,
240 .set_suspend_disable = fan53555_set_suspend_disable,
243 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
245 /* Init voltage range and step */
246 switch (di->chip_id) {
247 case FAN53526_CHIP_ID_01:
248 switch (di->chip_rev) {
249 case FAN53526_CHIP_REV_08:
250 di->vsel_min = 600000;
251 di->vsel_step = 6250;
255 "Chip ID %d with rev %d not supported!\n",
256 di->chip_id, di->chip_rev);
262 "Chip ID %d not supported!\n", di->chip_id);
266 di->slew_reg = FAN53555_CONTROL;
267 di->slew_mask = CTL_SLEW_MASK;
268 di->ramp_delay_table = slew_rates;
269 di->n_ramp_values = ARRAY_SIZE(slew_rates);
270 di->vsel_count = FAN53526_NVOLTAGES;
275 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
277 /* Init voltage range and step */
278 switch (di->chip_id) {
279 case FAN53555_CHIP_ID_00:
280 switch (di->chip_rev) {
281 case FAN53555_CHIP_REV_00:
282 di->vsel_min = 600000;
283 di->vsel_step = 10000;
285 case FAN53555_CHIP_REV_13:
286 di->vsel_min = 800000;
287 di->vsel_step = 10000;
291 "Chip ID %d with rev %d not supported!\n",
292 di->chip_id, di->chip_rev);
296 case FAN53555_CHIP_ID_01:
297 case FAN53555_CHIP_ID_03:
298 case FAN53555_CHIP_ID_05:
299 case FAN53555_CHIP_ID_08:
300 di->vsel_min = 600000;
301 di->vsel_step = 10000;
303 case FAN53555_CHIP_ID_04:
304 di->vsel_min = 603000;
305 di->vsel_step = 12826;
309 "Chip ID %d not supported!\n", di->chip_id);
312 di->slew_reg = FAN53555_CONTROL;
313 di->slew_mask = CTL_SLEW_MASK;
314 di->ramp_delay_table = slew_rates;
315 di->n_ramp_values = ARRAY_SIZE(slew_rates);
316 di->vsel_count = FAN53555_NVOLTAGES;
321 static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
323 /* Init voltage range and step */
324 switch (di->chip_id) {
327 di->vsel_min = 712500;
328 di->vsel_step = 12500;
332 "Chip ID %d not supported!\n", di->chip_id);
335 di->slew_reg = FAN53555_CONTROL;
336 di->slew_mask = CTL_SLEW_MASK;
337 di->ramp_delay_table = slew_rates;
338 di->n_ramp_values = ARRAY_SIZE(slew_rates);
339 di->vsel_count = FAN53555_NVOLTAGES;
344 static int fan53526_voltages_setup_tcs(struct fan53555_device_info *di)
346 switch (di->chip_id) {
347 case TCS4525_CHIP_ID_12:
348 case TCS4526_CHIP_ID_00:
349 di->slew_reg = TCS4525_TIME;
350 di->slew_mask = TCS_SLEW_MASK;
351 di->ramp_delay_table = tcs_slew_rates;
352 di->n_ramp_values = ARRAY_SIZE(tcs_slew_rates);
354 /* Init voltage range and step */
355 di->vsel_min = 600000;
356 di->vsel_step = 6250;
357 di->vsel_count = FAN53526_NVOLTAGES;
360 dev_err(di->dev, "Chip ID %d not supported!\n", di->chip_id);
367 /* For 00,01,03,05 options:
368 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
370 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
372 static int fan53555_device_setup(struct fan53555_device_info *di,
373 struct fan53555_platform_data *pdata)
377 /* Setup voltage control register */
378 switch (di->vendor) {
379 case FAN53526_VENDOR_FAIRCHILD:
380 case FAN53555_VENDOR_FAIRCHILD:
381 case FAN53555_VENDOR_SILERGY:
382 switch (pdata->sleep_vsel_id) {
383 case FAN53555_VSEL_ID_0:
384 di->sleep_reg = FAN53555_VSEL0;
385 di->vol_reg = FAN53555_VSEL1;
387 case FAN53555_VSEL_ID_1:
388 di->sleep_reg = FAN53555_VSEL1;
389 di->vol_reg = FAN53555_VSEL0;
392 dev_err(di->dev, "Invalid VSEL ID!\n");
396 case FAN53526_VENDOR_TCS:
397 switch (pdata->sleep_vsel_id) {
398 case FAN53555_VSEL_ID_0:
399 di->sleep_reg = TCS4525_VSEL0;
400 di->vol_reg = TCS4525_VSEL1;
402 case FAN53555_VSEL_ID_1:
403 di->sleep_reg = TCS4525_VSEL1;
404 di->vol_reg = TCS4525_VSEL0;
407 dev_err(di->dev, "Invalid VSEL ID!\n");
412 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
416 /* Setup mode control register */
417 switch (di->vendor) {
418 case FAN53526_VENDOR_FAIRCHILD:
419 di->mode_reg = FAN53555_CONTROL;
421 switch (pdata->sleep_vsel_id) {
422 case FAN53555_VSEL_ID_0:
423 di->mode_mask = CTL_MODE_VSEL1_MODE;
425 case FAN53555_VSEL_ID_1:
426 di->mode_mask = CTL_MODE_VSEL0_MODE;
430 case FAN53555_VENDOR_FAIRCHILD:
431 case FAN53555_VENDOR_SILERGY:
432 di->mode_reg = di->vol_reg;
433 di->mode_mask = VSEL_MODE;
435 case FAN53526_VENDOR_TCS:
436 di->mode_reg = TCS4525_COMMAND;
438 switch (pdata->sleep_vsel_id) {
439 case FAN53555_VSEL_ID_0:
440 di->mode_mask = TCS_VSEL1_MODE;
442 case FAN53555_VSEL_ID_1:
443 di->mode_mask = TCS_VSEL0_MODE;
448 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
452 /* Setup voltage range */
453 switch (di->vendor) {
454 case FAN53526_VENDOR_FAIRCHILD:
455 ret = fan53526_voltages_setup_fairchild(di);
457 case FAN53555_VENDOR_FAIRCHILD:
458 ret = fan53555_voltages_setup_fairchild(di);
460 case FAN53555_VENDOR_SILERGY:
461 ret = fan53555_voltages_setup_silergy(di);
463 case FAN53526_VENDOR_TCS:
464 ret = fan53526_voltages_setup_tcs(di);
467 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
474 static int fan53555_regulator_register(struct fan53555_device_info *di,
475 struct regulator_config *config)
477 struct regulator_desc *rdesc = &di->desc;
478 struct regulator_dev *rdev;
480 rdesc->name = "fan53555-reg";
481 rdesc->supply_name = "vin";
482 rdesc->ops = &fan53555_regulator_ops;
483 rdesc->type = REGULATOR_VOLTAGE;
484 rdesc->n_voltages = di->vsel_count;
485 rdesc->enable_reg = di->vol_reg;
486 rdesc->enable_mask = VSEL_BUCK_EN;
487 rdesc->min_uV = di->vsel_min;
488 rdesc->uV_step = di->vsel_step;
489 rdesc->vsel_reg = di->vol_reg;
490 rdesc->vsel_mask = di->vsel_count - 1;
491 rdesc->ramp_reg = di->slew_reg;
492 rdesc->ramp_mask = di->slew_mask;
493 rdesc->ramp_delay_table = di->ramp_delay_table;
494 rdesc->n_ramp_values = di->n_ramp_values;
495 rdesc->owner = THIS_MODULE;
497 rdev = devm_regulator_register(di->dev, &di->desc, config);
498 return PTR_ERR_OR_ZERO(rdev);
501 static const struct regmap_config fan53555_regmap_config = {
506 static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
507 struct device_node *np,
508 const struct regulator_desc *desc)
510 struct fan53555_platform_data *pdata;
514 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
518 pdata->regulator = of_get_regulator_init_data(dev, np, desc);
520 ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
523 pdata->sleep_vsel_id = tmp;
528 static const struct of_device_id __maybe_unused fan53555_dt_ids[] = {
530 .compatible = "fcs,fan53526",
531 .data = (void *)FAN53526_VENDOR_FAIRCHILD,
533 .compatible = "fcs,fan53555",
534 .data = (void *)FAN53555_VENDOR_FAIRCHILD
536 .compatible = "silergy,syr827",
537 .data = (void *)FAN53555_VENDOR_SILERGY,
539 .compatible = "silergy,syr828",
540 .data = (void *)FAN53555_VENDOR_SILERGY,
542 .compatible = "tcs,tcs4525",
543 .data = (void *)FAN53526_VENDOR_TCS
545 .compatible = "tcs,tcs4526",
546 .data = (void *)FAN53526_VENDOR_TCS
550 MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
552 static int fan53555_regulator_probe(struct i2c_client *client,
553 const struct i2c_device_id *id)
555 struct device_node *np = client->dev.of_node;
556 struct fan53555_device_info *di;
557 struct fan53555_platform_data *pdata;
558 struct regulator_config config = { };
559 struct regmap *regmap;
563 di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
568 pdata = dev_get_platdata(&client->dev);
570 pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
572 if (!pdata || !pdata->regulator) {
573 dev_err(&client->dev, "Platform data not found!\n");
577 di->regulator = pdata->regulator;
578 if (client->dev.of_node) {
580 (unsigned long)of_device_get_match_data(&client->dev);
582 /* if no ramp constraint set, get the pdata ramp_delay */
583 if (!di->regulator->constraints.ramp_delay) {
584 if (pdata->slew_rate >= ARRAY_SIZE(slew_rates)) {
585 dev_err(&client->dev, "Invalid slew_rate\n");
589 di->regulator->constraints.ramp_delay
590 = slew_rates[pdata->slew_rate];
593 di->vendor = id->driver_data;
596 regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
597 if (IS_ERR(regmap)) {
598 dev_err(&client->dev, "Failed to allocate regmap!\n");
599 return PTR_ERR(regmap);
601 di->dev = &client->dev;
602 i2c_set_clientdata(client, di);
604 ret = regmap_read(regmap, FAN53555_ID1, &val);
606 dev_err(&client->dev, "Failed to get chip ID!\n");
609 di->chip_id = val & DIE_ID;
610 /* Get chip revision */
611 ret = regmap_read(regmap, FAN53555_ID2, &val);
613 dev_err(&client->dev, "Failed to get chip Rev!\n");
616 di->chip_rev = val & DIE_REV;
617 dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
618 di->chip_id, di->chip_rev);
620 ret = fan53555_device_setup(di, pdata);
622 dev_err(&client->dev, "Failed to setup device!\n");
625 /* Register regulator */
626 config.dev = di->dev;
627 config.init_data = di->regulator;
628 config.regmap = regmap;
629 config.driver_data = di;
632 ret = fan53555_regulator_register(di, &config);
634 dev_err(&client->dev, "Failed to register regulator!\n");
639 static const struct i2c_device_id fan53555_id[] = {
642 .driver_data = FAN53526_VENDOR_FAIRCHILD
645 .driver_data = FAN53555_VENDOR_FAIRCHILD
648 .driver_data = FAN53555_VENDOR_SILERGY
651 .driver_data = FAN53555_VENDOR_SILERGY
654 .driver_data = FAN53526_VENDOR_TCS
657 .driver_data = FAN53526_VENDOR_TCS
661 MODULE_DEVICE_TABLE(i2c, fan53555_id);
663 static struct i2c_driver fan53555_regulator_driver = {
665 .name = "fan53555-regulator",
666 .of_match_table = of_match_ptr(fan53555_dt_ids),
668 .probe = fan53555_regulator_probe,
669 .id_table = fan53555_id,
672 module_i2c_driver(fan53555_regulator_driver);
674 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
675 MODULE_DESCRIPTION("FAN53555 regulator driver");
676 MODULE_LICENSE("GPL v2");