2 * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
4 * Supported Part Numbers:
5 * FAN53555UC00X/01X/03X/04X/05X
7 * Copyright (c) 2012 Marvell Technology Ltd.
8 * Yunfan Zhang <yfzhang@marvell.com>
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.
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>
29 #define FAN53555_VSEL0 0x00
30 #define FAN53555_VSEL1 0x01
31 /* Control register */
32 #define FAN53555_CONTROL 0x02
34 #define FAN53555_ID1 0x03
36 #define FAN53555_ID2 0x04
37 /* Monitor register */
38 #define FAN53555_MONITOR 0x05
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)
54 #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
55 #define FAN53526_NVOLTAGES 128
57 enum fan53555_vendor
{
58 FAN53526_VENDOR_FAIRCHILD
= 0,
59 FAN53555_VENDOR_FAIRCHILD
,
60 FAN53555_VENDOR_SILERGY
,
64 FAN53526_CHIP_ID_01
= 1,
68 FAN53526_CHIP_REV_08
= 8,
73 FAN53555_CHIP_ID_00
= 0,
79 FAN53555_CHIP_ID_08
= 8,
82 /* IC mask revision */
84 FAN53555_CHIP_REV_00
= 0x3,
85 FAN53555_CHIP_REV_13
= 0xf,
92 struct fan53555_device_info
{
93 enum fan53555_vendor vendor
;
94 struct regmap
*regmap
;
96 struct regulator_desc desc
;
97 struct regulator_dev
*rdev
;
98 struct regulator_init_data
*regulator
;
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
;
112 unsigned int mode_reg
;
113 unsigned int mode_mask
;
114 /* Sleep voltage cache */
115 unsigned int sleep_vol_cache
;
118 static int fan53555_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
120 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
123 if (di
->sleep_vol_cache
== uV
)
125 ret
= regulator_map_voltage_linear(rdev
, uV
, uV
);
128 ret
= regmap_update_bits(di
->regmap
, di
->sleep_reg
,
129 di
->desc
.vsel_mask
, ret
);
132 /* Cache the sleep voltage setting.
133 * Might not be the real voltage which is rounded */
134 di
->sleep_vol_cache
= uV
;
139 static int fan53555_set_suspend_enable(struct regulator_dev
*rdev
)
141 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
143 return regmap_update_bits(di
->regmap
, di
->sleep_reg
,
144 VSEL_BUCK_EN
, VSEL_BUCK_EN
);
147 static int fan53555_set_suspend_disable(struct regulator_dev
*rdev
)
149 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
151 return regmap_update_bits(di
->regmap
, di
->sleep_reg
,
155 static int fan53555_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
157 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
160 case REGULATOR_MODE_FAST
:
161 regmap_update_bits(di
->regmap
, di
->mode_reg
,
162 di
->mode_mask
, di
->mode_mask
);
164 case REGULATOR_MODE_NORMAL
:
165 regmap_update_bits(di
->regmap
, di
->vol_reg
, di
->mode_mask
, 0);
173 static unsigned int fan53555_get_mode(struct regulator_dev
*rdev
)
175 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
179 ret
= regmap_read(di
->regmap
, di
->mode_reg
, &val
);
182 if (val
& di
->mode_mask
)
183 return REGULATOR_MODE_FAST
;
185 return REGULATOR_MODE_NORMAL
;
188 static const int slew_rates
[] = {
199 static int fan53555_set_ramp(struct regulator_dev
*rdev
, int ramp
)
201 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
204 for (i
= 0; i
< ARRAY_SIZE(slew_rates
); i
++) {
205 if (ramp
<= slew_rates
[i
])
212 dev_err(di
->dev
, "unsupported ramp value %d\n", ramp
);
216 return regmap_update_bits(di
->regmap
, FAN53555_CONTROL
,
217 CTL_SLEW_MASK
, regval
<< CTL_SLEW_SHIFT
);
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
,
237 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info
*di
)
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;
249 "Chip ID %d with rev %d not supported!\n",
250 di
->chip_id
, di
->chip_rev
);
256 "Chip ID %d not supported!\n", di
->chip_id
);
260 di
->vsel_count
= FAN53526_NVOLTAGES
;
265 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info
*di
)
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;
275 case FAN53555_CHIP_REV_13
:
276 di
->vsel_min
= 800000;
277 di
->vsel_step
= 10000;
281 "Chip ID %d with rev %d not supported!\n",
282 di
->chip_id
, di
->chip_rev
);
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;
293 case FAN53555_CHIP_ID_04
:
294 di
->vsel_min
= 603000;
295 di
->vsel_step
= 12826;
299 "Chip ID %d not supported!\n", di
->chip_id
);
303 di
->vsel_count
= FAN53555_NVOLTAGES
;
308 static int fan53555_voltages_setup_silergy(struct fan53555_device_info
*di
)
310 /* Init voltage range and step */
311 switch (di
->chip_id
) {
313 di
->vsel_min
= 712500;
314 di
->vsel_step
= 12500;
318 "Chip ID %d not supported!\n", di
->chip_id
);
322 di
->vsel_count
= FAN53555_NVOLTAGES
;
327 /* For 00,01,03,05 options:
328 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
330 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
332 static int fan53555_device_setup(struct fan53555_device_info
*di
,
333 struct fan53555_platform_data
*pdata
)
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
;
343 case FAN53555_VSEL_ID_1
:
344 di
->sleep_reg
= FAN53555_VSEL1
;
345 di
->vol_reg
= FAN53555_VSEL0
;
348 dev_err(di
->dev
, "Invalid VSEL ID!\n");
352 /* Setup mode control register */
353 switch (di
->vendor
) {
354 case FAN53526_VENDOR_FAIRCHILD
:
355 di
->mode_reg
= FAN53555_CONTROL
;
357 switch (pdata
->sleep_vsel_id
) {
358 case FAN53555_VSEL_ID_0
:
359 di
->mode_mask
= CTL_MODE_VSEL1_MODE
;
361 case FAN53555_VSEL_ID_1
:
362 di
->mode_mask
= CTL_MODE_VSEL0_MODE
;
366 case FAN53555_VENDOR_FAIRCHILD
:
367 case FAN53555_VENDOR_SILERGY
:
368 di
->mode_reg
= di
->vol_reg
;
369 di
->mode_mask
= VSEL_MODE
;
372 dev_err(di
->dev
, "vendor %d not supported!\n", di
->vendor
);
376 /* Setup voltage range */
377 switch (di
->vendor
) {
378 case FAN53526_VENDOR_FAIRCHILD
:
379 ret
= fan53526_voltages_setup_fairchild(di
);
381 case FAN53555_VENDOR_FAIRCHILD
:
382 ret
= fan53555_voltages_setup_fairchild(di
);
384 case FAN53555_VENDOR_SILERGY
:
385 ret
= fan53555_voltages_setup_silergy(di
);
388 dev_err(di
->dev
, "vendor %d not supported!\n", di
->vendor
);
395 static int fan53555_regulator_register(struct fan53555_device_info
*di
,
396 struct regulator_config
*config
)
398 struct regulator_desc
*rdesc
= &di
->desc
;
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
;
413 di
->rdev
= devm_regulator_register(di
->dev
, &di
->desc
, config
);
414 return PTR_ERR_OR_ZERO(di
->rdev
);
417 static const struct regmap_config fan53555_regmap_config
= {
422 static struct fan53555_platform_data
*fan53555_parse_dt(struct device
*dev
,
423 struct device_node
*np
,
424 const struct regulator_desc
*desc
)
426 struct fan53555_platform_data
*pdata
;
430 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
434 pdata
->regulator
= of_get_regulator_init_data(dev
, np
, desc
);
436 ret
= of_property_read_u32(np
, "fcs,suspend-voltage-selector",
439 pdata
->sleep_vsel_id
= tmp
;
444 static const struct of_device_id fan53555_dt_ids
[] = {
446 .compatible
= "fcs,fan53526",
447 .data
= (void *)FAN53526_VENDOR_FAIRCHILD
,
449 .compatible
= "fcs,fan53555",
450 .data
= (void *)FAN53555_VENDOR_FAIRCHILD
452 .compatible
= "silergy,syr827",
453 .data
= (void *)FAN53555_VENDOR_SILERGY
,
455 .compatible
= "silergy,syr828",
456 .data
= (void *)FAN53555_VENDOR_SILERGY
,
460 MODULE_DEVICE_TABLE(of
, fan53555_dt_ids
);
462 static int fan53555_regulator_probe(struct i2c_client
*client
,
463 const struct i2c_device_id
*id
)
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
= { };
472 di
= devm_kzalloc(&client
->dev
, sizeof(struct fan53555_device_info
),
477 pdata
= dev_get_platdata(&client
->dev
);
479 pdata
= fan53555_parse_dt(&client
->dev
, np
, &di
->desc
);
481 if (!pdata
|| !pdata
->regulator
) {
482 dev_err(&client
->dev
, "Platform data not found!\n");
486 di
->regulator
= pdata
->regulator
;
487 if (client
->dev
.of_node
) {
489 (unsigned long)of_device_get_match_data(&client
->dev
);
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");
498 di
->regulator
->constraints
.ramp_delay
499 = slew_rates
[pdata
->slew_rate
];
502 di
->vendor
= id
->driver_data
;
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
);
510 di
->dev
= &client
->dev
;
511 i2c_set_clientdata(client
, di
);
513 ret
= regmap_read(di
->regmap
, FAN53555_ID1
, &val
);
515 dev_err(&client
->dev
, "Failed to get chip ID!\n");
518 di
->chip_id
= val
& DIE_ID
;
519 /* Get chip revision */
520 ret
= regmap_read(di
->regmap
, FAN53555_ID2
, &val
);
522 dev_err(&client
->dev
, "Failed to get chip Rev!\n");
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
);
529 ret
= fan53555_device_setup(di
, pdata
);
531 dev_err(&client
->dev
, "Failed to setup device!\n");
534 /* Register regulator */
535 config
.dev
= di
->dev
;
536 config
.init_data
= di
->regulator
;
537 config
.regmap
= di
->regmap
;
538 config
.driver_data
= di
;
541 ret
= fan53555_regulator_register(di
, &config
);
543 dev_err(&client
->dev
, "Failed to register regulator!\n");
548 static const struct i2c_device_id fan53555_id
[] = {
551 .driver_data
= FAN53526_VENDOR_FAIRCHILD
554 .driver_data
= FAN53555_VENDOR_FAIRCHILD
557 .driver_data
= FAN53555_VENDOR_SILERGY
560 .driver_data
= FAN53555_VENDOR_SILERGY
564 MODULE_DEVICE_TABLE(i2c
, fan53555_id
);
566 static struct i2c_driver fan53555_regulator_driver
= {
568 .name
= "fan53555-regulator",
569 .of_match_table
= of_match_ptr(fan53555_dt_ids
),
571 .probe
= fan53555_regulator_probe
,
572 .id_table
= fan53555_id
,
575 module_i2c_driver(fan53555_regulator_driver
);
577 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
578 MODULE_DESCRIPTION("FAN53555 regulator driver");
579 MODULE_LICENSE("GPL v2");