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
27 /* Control register */
28 #define FAN53555_CONTROL 0x02
30 #define FAN53555_ID1 0x03
32 #define FAN53555_ID2 0x04
33 /* Monitor register */
34 #define FAN53555_MONITOR 0x05
36 /* VSEL bit definitions */
37 #define VSEL_BUCK_EN (1 << 7)
38 #define VSEL_MODE (1 << 6)
39 /* Chip ID and Verison */
40 #define DIE_ID 0x0F /* ID1 */
41 #define DIE_REV 0x0F /* ID2 */
42 /* Control bit definitions */
43 #define CTL_OUTPUT_DISCHG (1 << 7)
44 #define CTL_SLEW_MASK (0x7 << 4)
45 #define CTL_SLEW_SHIFT 4
46 #define CTL_RESET (1 << 2)
47 #define CTL_MODE_VSEL0_MODE BIT(0)
48 #define CTL_MODE_VSEL1_MODE BIT(1)
50 #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
51 #define FAN53526_NVOLTAGES 128
53 enum fan53555_vendor
{
54 FAN53526_VENDOR_FAIRCHILD
= 0,
55 FAN53555_VENDOR_FAIRCHILD
,
56 FAN53555_VENDOR_SILERGY
,
60 FAN53526_CHIP_ID_01
= 1,
64 FAN53526_CHIP_REV_08
= 8,
69 FAN53555_CHIP_ID_00
= 0,
75 FAN53555_CHIP_ID_08
= 8,
78 /* IC mask revision */
80 FAN53555_CHIP_REV_00
= 0x3,
81 FAN53555_CHIP_REV_13
= 0xf,
89 struct fan53555_device_info
{
90 enum fan53555_vendor vendor
;
92 struct regulator_desc desc
;
93 struct regulator_init_data
*regulator
;
97 /* Voltage setting register */
99 unsigned int sleep_reg
;
100 /* Voltage range and step(linear) */
101 unsigned int vsel_min
;
102 unsigned int vsel_step
;
103 unsigned int vsel_count
;
105 unsigned int mode_reg
;
106 unsigned int mode_mask
;
107 /* Sleep voltage cache */
108 unsigned int sleep_vol_cache
;
111 static int fan53555_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
113 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
116 if (di
->sleep_vol_cache
== uV
)
118 ret
= regulator_map_voltage_linear(rdev
, uV
, uV
);
121 ret
= regmap_update_bits(rdev
->regmap
, di
->sleep_reg
,
122 di
->desc
.vsel_mask
, ret
);
125 /* Cache the sleep voltage setting.
126 * Might not be the real voltage which is rounded */
127 di
->sleep_vol_cache
= uV
;
132 static int fan53555_set_suspend_enable(struct regulator_dev
*rdev
)
134 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
136 return regmap_update_bits(rdev
->regmap
, di
->sleep_reg
,
137 VSEL_BUCK_EN
, VSEL_BUCK_EN
);
140 static int fan53555_set_suspend_disable(struct regulator_dev
*rdev
)
142 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
144 return regmap_update_bits(rdev
->regmap
, di
->sleep_reg
,
148 static int fan53555_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
150 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
153 case REGULATOR_MODE_FAST
:
154 regmap_update_bits(rdev
->regmap
, di
->mode_reg
,
155 di
->mode_mask
, di
->mode_mask
);
157 case REGULATOR_MODE_NORMAL
:
158 regmap_update_bits(rdev
->regmap
, di
->vol_reg
, di
->mode_mask
, 0);
166 static unsigned int fan53555_get_mode(struct regulator_dev
*rdev
)
168 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
172 ret
= regmap_read(rdev
->regmap
, di
->mode_reg
, &val
);
175 if (val
& di
->mode_mask
)
176 return REGULATOR_MODE_FAST
;
178 return REGULATOR_MODE_NORMAL
;
181 static const int slew_rates
[] = {
192 static int fan53555_set_ramp(struct regulator_dev
*rdev
, int ramp
)
194 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
197 for (i
= 0; i
< ARRAY_SIZE(slew_rates
); i
++) {
198 if (ramp
<= slew_rates
[i
])
205 dev_err(di
->dev
, "unsupported ramp value %d\n", ramp
);
209 return regmap_update_bits(rdev
->regmap
, FAN53555_CONTROL
,
210 CTL_SLEW_MASK
, regval
<< CTL_SLEW_SHIFT
);
213 static const struct regulator_ops fan53555_regulator_ops
= {
214 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
215 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
216 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
217 .map_voltage
= regulator_map_voltage_linear
,
218 .list_voltage
= regulator_list_voltage_linear
,
219 .set_suspend_voltage
= fan53555_set_suspend_voltage
,
220 .enable
= regulator_enable_regmap
,
221 .disable
= regulator_disable_regmap
,
222 .is_enabled
= regulator_is_enabled_regmap
,
223 .set_mode
= fan53555_set_mode
,
224 .get_mode
= fan53555_get_mode
,
225 .set_ramp_delay
= fan53555_set_ramp
,
226 .set_suspend_enable
= fan53555_set_suspend_enable
,
227 .set_suspend_disable
= fan53555_set_suspend_disable
,
230 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info
*di
)
232 /* Init voltage range and step */
233 switch (di
->chip_id
) {
234 case FAN53526_CHIP_ID_01
:
235 switch (di
->chip_rev
) {
236 case FAN53526_CHIP_REV_08
:
237 di
->vsel_min
= 600000;
238 di
->vsel_step
= 6250;
242 "Chip ID %d with rev %d not supported!\n",
243 di
->chip_id
, di
->chip_rev
);
249 "Chip ID %d not supported!\n", di
->chip_id
);
253 di
->vsel_count
= FAN53526_NVOLTAGES
;
258 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info
*di
)
260 /* Init voltage range and step */
261 switch (di
->chip_id
) {
262 case FAN53555_CHIP_ID_00
:
263 switch (di
->chip_rev
) {
264 case FAN53555_CHIP_REV_00
:
265 di
->vsel_min
= 600000;
266 di
->vsel_step
= 10000;
268 case FAN53555_CHIP_REV_13
:
269 di
->vsel_min
= 800000;
270 di
->vsel_step
= 10000;
274 "Chip ID %d with rev %d not supported!\n",
275 di
->chip_id
, di
->chip_rev
);
279 case FAN53555_CHIP_ID_01
:
280 case FAN53555_CHIP_ID_03
:
281 case FAN53555_CHIP_ID_05
:
282 case FAN53555_CHIP_ID_08
:
283 di
->vsel_min
= 600000;
284 di
->vsel_step
= 10000;
286 case FAN53555_CHIP_ID_04
:
287 di
->vsel_min
= 603000;
288 di
->vsel_step
= 12826;
292 "Chip ID %d not supported!\n", di
->chip_id
);
296 di
->vsel_count
= FAN53555_NVOLTAGES
;
301 static int fan53555_voltages_setup_silergy(struct fan53555_device_info
*di
)
303 /* Init voltage range and step */
304 switch (di
->chip_id
) {
307 di
->vsel_min
= 712500;
308 di
->vsel_step
= 12500;
312 "Chip ID %d not supported!\n", di
->chip_id
);
316 di
->vsel_count
= FAN53555_NVOLTAGES
;
321 /* For 00,01,03,05 options:
322 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
324 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
326 static int fan53555_device_setup(struct fan53555_device_info
*di
,
327 struct fan53555_platform_data
*pdata
)
331 /* Setup voltage control register */
332 switch (pdata
->sleep_vsel_id
) {
333 case FAN53555_VSEL_ID_0
:
334 di
->sleep_reg
= FAN53555_VSEL0
;
335 di
->vol_reg
= FAN53555_VSEL1
;
337 case FAN53555_VSEL_ID_1
:
338 di
->sleep_reg
= FAN53555_VSEL1
;
339 di
->vol_reg
= FAN53555_VSEL0
;
342 dev_err(di
->dev
, "Invalid VSEL ID!\n");
346 /* Setup mode control register */
347 switch (di
->vendor
) {
348 case FAN53526_VENDOR_FAIRCHILD
:
349 di
->mode_reg
= FAN53555_CONTROL
;
351 switch (pdata
->sleep_vsel_id
) {
352 case FAN53555_VSEL_ID_0
:
353 di
->mode_mask
= CTL_MODE_VSEL1_MODE
;
355 case FAN53555_VSEL_ID_1
:
356 di
->mode_mask
= CTL_MODE_VSEL0_MODE
;
360 case FAN53555_VENDOR_FAIRCHILD
:
361 case FAN53555_VENDOR_SILERGY
:
362 di
->mode_reg
= di
->vol_reg
;
363 di
->mode_mask
= VSEL_MODE
;
366 dev_err(di
->dev
, "vendor %d not supported!\n", di
->vendor
);
370 /* Setup voltage range */
371 switch (di
->vendor
) {
372 case FAN53526_VENDOR_FAIRCHILD
:
373 ret
= fan53526_voltages_setup_fairchild(di
);
375 case FAN53555_VENDOR_FAIRCHILD
:
376 ret
= fan53555_voltages_setup_fairchild(di
);
378 case FAN53555_VENDOR_SILERGY
:
379 ret
= fan53555_voltages_setup_silergy(di
);
382 dev_err(di
->dev
, "vendor %d not supported!\n", di
->vendor
);
389 static int fan53555_regulator_register(struct fan53555_device_info
*di
,
390 struct regulator_config
*config
)
392 struct regulator_desc
*rdesc
= &di
->desc
;
393 struct regulator_dev
*rdev
;
395 rdesc
->name
= "fan53555-reg";
396 rdesc
->supply_name
= "vin";
397 rdesc
->ops
= &fan53555_regulator_ops
;
398 rdesc
->type
= REGULATOR_VOLTAGE
;
399 rdesc
->n_voltages
= di
->vsel_count
;
400 rdesc
->enable_reg
= di
->vol_reg
;
401 rdesc
->enable_mask
= VSEL_BUCK_EN
;
402 rdesc
->min_uV
= di
->vsel_min
;
403 rdesc
->uV_step
= di
->vsel_step
;
404 rdesc
->vsel_reg
= di
->vol_reg
;
405 rdesc
->vsel_mask
= di
->vsel_count
- 1;
406 rdesc
->owner
= THIS_MODULE
;
408 rdev
= devm_regulator_register(di
->dev
, &di
->desc
, config
);
409 return PTR_ERR_OR_ZERO(rdev
);
412 static const struct regmap_config fan53555_regmap_config
= {
417 static struct fan53555_platform_data
*fan53555_parse_dt(struct device
*dev
,
418 struct device_node
*np
,
419 const struct regulator_desc
*desc
)
421 struct fan53555_platform_data
*pdata
;
425 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
429 pdata
->regulator
= of_get_regulator_init_data(dev
, np
, desc
);
431 ret
= of_property_read_u32(np
, "fcs,suspend-voltage-selector",
434 pdata
->sleep_vsel_id
= tmp
;
439 static const struct of_device_id __maybe_unused fan53555_dt_ids
[] = {
441 .compatible
= "fcs,fan53526",
442 .data
= (void *)FAN53526_VENDOR_FAIRCHILD
,
444 .compatible
= "fcs,fan53555",
445 .data
= (void *)FAN53555_VENDOR_FAIRCHILD
447 .compatible
= "silergy,syr827",
448 .data
= (void *)FAN53555_VENDOR_SILERGY
,
450 .compatible
= "silergy,syr828",
451 .data
= (void *)FAN53555_VENDOR_SILERGY
,
455 MODULE_DEVICE_TABLE(of
, fan53555_dt_ids
);
457 static int fan53555_regulator_probe(struct i2c_client
*client
,
458 const struct i2c_device_id
*id
)
460 struct device_node
*np
= client
->dev
.of_node
;
461 struct fan53555_device_info
*di
;
462 struct fan53555_platform_data
*pdata
;
463 struct regulator_config config
= { };
464 struct regmap
*regmap
;
468 di
= devm_kzalloc(&client
->dev
, sizeof(struct fan53555_device_info
),
473 pdata
= dev_get_platdata(&client
->dev
);
475 pdata
= fan53555_parse_dt(&client
->dev
, np
, &di
->desc
);
477 if (!pdata
|| !pdata
->regulator
) {
478 dev_err(&client
->dev
, "Platform data not found!\n");
482 di
->regulator
= pdata
->regulator
;
483 if (client
->dev
.of_node
) {
485 (unsigned long)of_device_get_match_data(&client
->dev
);
487 /* if no ramp constraint set, get the pdata ramp_delay */
488 if (!di
->regulator
->constraints
.ramp_delay
) {
489 if (pdata
->slew_rate
>= ARRAY_SIZE(slew_rates
)) {
490 dev_err(&client
->dev
, "Invalid slew_rate\n");
494 di
->regulator
->constraints
.ramp_delay
495 = slew_rates
[pdata
->slew_rate
];
498 di
->vendor
= id
->driver_data
;
501 regmap
= devm_regmap_init_i2c(client
, &fan53555_regmap_config
);
502 if (IS_ERR(regmap
)) {
503 dev_err(&client
->dev
, "Failed to allocate regmap!\n");
504 return PTR_ERR(regmap
);
506 di
->dev
= &client
->dev
;
507 i2c_set_clientdata(client
, di
);
509 ret
= regmap_read(regmap
, FAN53555_ID1
, &val
);
511 dev_err(&client
->dev
, "Failed to get chip ID!\n");
514 di
->chip_id
= val
& DIE_ID
;
515 /* Get chip revision */
516 ret
= regmap_read(regmap
, FAN53555_ID2
, &val
);
518 dev_err(&client
->dev
, "Failed to get chip Rev!\n");
521 di
->chip_rev
= val
& DIE_REV
;
522 dev_info(&client
->dev
, "FAN53555 Option[%d] Rev[%d] Detected!\n",
523 di
->chip_id
, di
->chip_rev
);
525 ret
= fan53555_device_setup(di
, pdata
);
527 dev_err(&client
->dev
, "Failed to setup device!\n");
530 /* Register regulator */
531 config
.dev
= di
->dev
;
532 config
.init_data
= di
->regulator
;
533 config
.regmap
= regmap
;
534 config
.driver_data
= di
;
537 ret
= fan53555_regulator_register(di
, &config
);
539 dev_err(&client
->dev
, "Failed to register regulator!\n");
544 static const struct i2c_device_id fan53555_id
[] = {
547 .driver_data
= FAN53526_VENDOR_FAIRCHILD
550 .driver_data
= FAN53555_VENDOR_FAIRCHILD
553 .driver_data
= FAN53555_VENDOR_SILERGY
556 .driver_data
= FAN53555_VENDOR_SILERGY
560 MODULE_DEVICE_TABLE(i2c
, fan53555_id
);
562 static struct i2c_driver fan53555_regulator_driver
= {
564 .name
= "fan53555-regulator",
565 .of_match_table
= of_match_ptr(fan53555_dt_ids
),
567 .probe
= fan53555_regulator_probe
,
568 .id_table
= fan53555_id
,
571 module_i2c_driver(fan53555_regulator_driver
);
573 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
574 MODULE_DESCRIPTION("FAN53555 regulator driver");
575 MODULE_LICENSE("GPL v2");