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 #define VSEL_NSEL_MASK 0x3F
44 /* Chip ID and Verison */
45 #define DIE_ID 0x0F /* ID1 */
46 #define DIE_REV 0x0F /* ID2 */
47 /* Control bit definitions */
48 #define CTL_OUTPUT_DISCHG (1 << 7)
49 #define CTL_SLEW_MASK (0x7 << 4)
50 #define CTL_SLEW_SHIFT 4
51 #define CTL_RESET (1 << 2)
53 #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
55 enum fan53555_vendor
{
56 FAN53555_VENDOR_FAIRCHILD
= 0,
57 FAN53555_VENDOR_SILERGY
,
62 FAN53555_CHIP_ID_00
= 0,
74 struct fan53555_device_info
{
75 enum fan53555_vendor vendor
;
76 struct regmap
*regmap
;
78 struct regulator_desc desc
;
79 struct regulator_dev
*rdev
;
80 struct regulator_init_data
*regulator
;
84 /* Voltage setting register */
86 unsigned int sleep_reg
;
87 /* Voltage range and step(linear) */
88 unsigned int vsel_min
;
89 unsigned int vsel_step
;
90 /* Voltage slew rate limiting */
91 unsigned int slew_rate
;
92 /* Sleep voltage cache */
93 unsigned int sleep_vol_cache
;
96 static int fan53555_set_suspend_voltage(struct regulator_dev
*rdev
, int uV
)
98 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
101 if (di
->sleep_vol_cache
== uV
)
103 ret
= regulator_map_voltage_linear(rdev
, uV
, uV
);
106 ret
= regmap_update_bits(di
->regmap
, di
->sleep_reg
,
107 VSEL_NSEL_MASK
, ret
);
110 /* Cache the sleep voltage setting.
111 * Might not be the real voltage which is rounded */
112 di
->sleep_vol_cache
= uV
;
117 static int fan53555_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
119 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
122 case REGULATOR_MODE_FAST
:
123 regmap_update_bits(di
->regmap
, di
->vol_reg
,
124 VSEL_MODE
, VSEL_MODE
);
126 case REGULATOR_MODE_NORMAL
:
127 regmap_update_bits(di
->regmap
, di
->vol_reg
, VSEL_MODE
, 0);
135 static unsigned int fan53555_get_mode(struct regulator_dev
*rdev
)
137 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
141 ret
= regmap_read(di
->regmap
, di
->vol_reg
, &val
);
145 return REGULATOR_MODE_FAST
;
147 return REGULATOR_MODE_NORMAL
;
150 static const int slew_rates
[] = {
161 static int fan53555_set_ramp(struct regulator_dev
*rdev
, int ramp
)
163 struct fan53555_device_info
*di
= rdev_get_drvdata(rdev
);
166 for (i
= 0; i
< ARRAY_SIZE(slew_rates
); i
++) {
167 if (ramp
<= slew_rates
[i
])
174 dev_err(di
->dev
, "unsupported ramp value %d\n", ramp
);
178 return regmap_update_bits(di
->regmap
, FAN53555_CONTROL
,
179 CTL_SLEW_MASK
, regval
<< CTL_SLEW_SHIFT
);
182 static struct regulator_ops fan53555_regulator_ops
= {
183 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
184 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
185 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
186 .map_voltage
= regulator_map_voltage_linear
,
187 .list_voltage
= regulator_list_voltage_linear
,
188 .set_suspend_voltage
= fan53555_set_suspend_voltage
,
189 .enable
= regulator_enable_regmap
,
190 .disable
= regulator_disable_regmap
,
191 .is_enabled
= regulator_is_enabled_regmap
,
192 .set_mode
= fan53555_set_mode
,
193 .get_mode
= fan53555_get_mode
,
194 .set_ramp_delay
= fan53555_set_ramp
,
197 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info
*di
)
199 /* Init voltage range and step */
200 switch (di
->chip_id
) {
201 case FAN53555_CHIP_ID_00
:
202 case FAN53555_CHIP_ID_01
:
203 case FAN53555_CHIP_ID_03
:
204 case FAN53555_CHIP_ID_05
:
205 di
->vsel_min
= 600000;
206 di
->vsel_step
= 10000;
208 case FAN53555_CHIP_ID_04
:
209 di
->vsel_min
= 603000;
210 di
->vsel_step
= 12826;
214 "Chip ID %d not supported!\n", di
->chip_id
);
221 static int fan53555_voltages_setup_silergy(struct fan53555_device_info
*di
)
223 /* Init voltage range and step */
224 switch (di
->chip_id
) {
226 di
->vsel_min
= 712500;
227 di
->vsel_step
= 12500;
231 "Chip ID %d not supported!\n", di
->chip_id
);
238 /* For 00,01,03,05 options:
239 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
241 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
243 static int fan53555_device_setup(struct fan53555_device_info
*di
,
244 struct fan53555_platform_data
*pdata
)
248 /* Setup voltage control register */
249 switch (pdata
->sleep_vsel_id
) {
250 case FAN53555_VSEL_ID_0
:
251 di
->sleep_reg
= FAN53555_VSEL0
;
252 di
->vol_reg
= FAN53555_VSEL1
;
254 case FAN53555_VSEL_ID_1
:
255 di
->sleep_reg
= FAN53555_VSEL1
;
256 di
->vol_reg
= FAN53555_VSEL0
;
259 dev_err(di
->dev
, "Invalid VSEL ID!\n");
263 switch (di
->vendor
) {
264 case FAN53555_VENDOR_FAIRCHILD
:
265 ret
= fan53555_voltages_setup_fairchild(di
);
267 case FAN53555_VENDOR_SILERGY
:
268 ret
= fan53555_voltages_setup_silergy(di
);
271 dev_err(di
->dev
, "vendor %d not supported!\n", di
->vendor
);
278 static int fan53555_regulator_register(struct fan53555_device_info
*di
,
279 struct regulator_config
*config
)
281 struct regulator_desc
*rdesc
= &di
->desc
;
283 rdesc
->name
= "fan53555-reg";
284 rdesc
->supply_name
= "vin";
285 rdesc
->ops
= &fan53555_regulator_ops
;
286 rdesc
->type
= REGULATOR_VOLTAGE
;
287 rdesc
->n_voltages
= FAN53555_NVOLTAGES
;
288 rdesc
->enable_reg
= di
->vol_reg
;
289 rdesc
->enable_mask
= VSEL_BUCK_EN
;
290 rdesc
->min_uV
= di
->vsel_min
;
291 rdesc
->uV_step
= di
->vsel_step
;
292 rdesc
->vsel_reg
= di
->vol_reg
;
293 rdesc
->vsel_mask
= VSEL_NSEL_MASK
;
294 rdesc
->owner
= THIS_MODULE
;
296 di
->rdev
= devm_regulator_register(di
->dev
, &di
->desc
, config
);
297 return PTR_ERR_OR_ZERO(di
->rdev
);
300 static const struct regmap_config fan53555_regmap_config
= {
305 static struct fan53555_platform_data
*fan53555_parse_dt(struct device
*dev
,
306 struct device_node
*np
,
307 const struct regulator_desc
*desc
)
309 struct fan53555_platform_data
*pdata
;
313 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
317 pdata
->regulator
= of_get_regulator_init_data(dev
, np
, desc
);
319 ret
= of_property_read_u32(np
, "fcs,suspend-voltage-selector",
322 pdata
->sleep_vsel_id
= tmp
;
327 static const struct of_device_id fan53555_dt_ids
[] = {
329 .compatible
= "fcs,fan53555",
330 .data
= (void *)FAN53555_VENDOR_FAIRCHILD
332 .compatible
= "silergy,syr827",
333 .data
= (void *)FAN53555_VENDOR_SILERGY
,
335 .compatible
= "silergy,syr828",
336 .data
= (void *)FAN53555_VENDOR_SILERGY
,
340 MODULE_DEVICE_TABLE(of
, fan53555_dt_ids
);
342 static int fan53555_regulator_probe(struct i2c_client
*client
,
343 const struct i2c_device_id
*id
)
345 struct device_node
*np
= client
->dev
.of_node
;
346 struct fan53555_device_info
*di
;
347 struct fan53555_platform_data
*pdata
;
348 struct regulator_config config
= { };
352 di
= devm_kzalloc(&client
->dev
, sizeof(struct fan53555_device_info
),
357 pdata
= dev_get_platdata(&client
->dev
);
359 pdata
= fan53555_parse_dt(&client
->dev
, np
, &di
->desc
);
361 if (!pdata
|| !pdata
->regulator
) {
362 dev_err(&client
->dev
, "Platform data not found!\n");
366 di
->regulator
= pdata
->regulator
;
367 if (client
->dev
.of_node
) {
368 const struct of_device_id
*match
;
370 match
= of_match_device(of_match_ptr(fan53555_dt_ids
),
375 di
->vendor
= (unsigned long) match
->data
;
377 /* if no ramp constraint set, get the pdata ramp_delay */
378 if (!di
->regulator
->constraints
.ramp_delay
) {
379 int slew_idx
= (pdata
->slew_rate
& 0x7)
380 ? pdata
->slew_rate
: 0;
382 di
->regulator
->constraints
.ramp_delay
383 = slew_rates
[slew_idx
];
386 di
->vendor
= id
->driver_data
;
389 di
->regmap
= devm_regmap_init_i2c(client
, &fan53555_regmap_config
);
390 if (IS_ERR(di
->regmap
)) {
391 dev_err(&client
->dev
, "Failed to allocate regmap!\n");
392 return PTR_ERR(di
->regmap
);
394 di
->dev
= &client
->dev
;
395 i2c_set_clientdata(client
, di
);
397 ret
= regmap_read(di
->regmap
, FAN53555_ID1
, &val
);
399 dev_err(&client
->dev
, "Failed to get chip ID!\n");
402 di
->chip_id
= val
& DIE_ID
;
403 /* Get chip revision */
404 ret
= regmap_read(di
->regmap
, FAN53555_ID2
, &val
);
406 dev_err(&client
->dev
, "Failed to get chip Rev!\n");
409 di
->chip_rev
= val
& DIE_REV
;
410 dev_info(&client
->dev
, "FAN53555 Option[%d] Rev[%d] Detected!\n",
411 di
->chip_id
, di
->chip_rev
);
413 ret
= fan53555_device_setup(di
, pdata
);
415 dev_err(&client
->dev
, "Failed to setup device!\n");
418 /* Register regulator */
419 config
.dev
= di
->dev
;
420 config
.init_data
= di
->regulator
;
421 config
.regmap
= di
->regmap
;
422 config
.driver_data
= di
;
425 ret
= fan53555_regulator_register(di
, &config
);
427 dev_err(&client
->dev
, "Failed to register regulator!\n");
432 static const struct i2c_device_id fan53555_id
[] = {
435 .driver_data
= FAN53555_VENDOR_FAIRCHILD
438 .driver_data
= FAN53555_VENDOR_SILERGY
442 MODULE_DEVICE_TABLE(i2c
, fan53555_id
);
444 static struct i2c_driver fan53555_regulator_driver
= {
446 .name
= "fan53555-regulator",
447 .of_match_table
= of_match_ptr(fan53555_dt_ids
),
449 .probe
= fan53555_regulator_probe
,
450 .id_table
= fan53555_id
,
453 module_i2c_driver(fan53555_regulator_driver
);
455 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
456 MODULE_DESCRIPTION("FAN53555 regulator driver");
457 MODULE_LICENSE("GPL v2");