2 * Regulators driver for Maxim max8649
4 * Copyright (C) 2009-2010 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/slab.h>
18 #include <linux/regulator/max8649.h>
19 #include <linux/regmap.h>
21 #define MAX8649_DCDC_VMIN 750000 /* uV */
22 #define MAX8649_DCDC_VMAX 1380000 /* uV */
23 #define MAX8649_DCDC_STEP 10000 /* uV */
24 #define MAX8649_VOL_MASK 0x3f
27 #define MAX8649_MODE0 0x00
28 #define MAX8649_MODE1 0x01
29 #define MAX8649_MODE2 0x02
30 #define MAX8649_MODE3 0x03
31 #define MAX8649_CONTROL 0x04
32 #define MAX8649_SYNC 0x05
33 #define MAX8649_RAMP 0x06
34 #define MAX8649_CHIP_ID1 0x08
35 #define MAX8649_CHIP_ID2 0x09
38 #define MAX8649_EN_PD (1 << 7)
39 #define MAX8649_VID0_PD (1 << 6)
40 #define MAX8649_VID1_PD (1 << 5)
41 #define MAX8649_VID_MASK (3 << 5)
43 #define MAX8649_FORCE_PWM (1 << 7)
44 #define MAX8649_SYNC_EXTCLK (1 << 6)
46 #define MAX8649_EXT_MASK (3 << 6)
48 #define MAX8649_RAMP_MASK (7 << 5)
49 #define MAX8649_RAMP_DOWN (1 << 1)
51 struct max8649_regulator_info
{
52 struct regulator_dev
*regulator
;
54 struct regmap
*regmap
;
57 unsigned mode
:2; /* bit[1:0] = VID1, VID0 */
58 unsigned extclk_freq
:2;
60 unsigned ramp_timing
:3;
66 static inline int check_range(int min_uV
, int max_uV
)
68 if ((min_uV
< MAX8649_DCDC_VMIN
) || (max_uV
> MAX8649_DCDC_VMAX
)
74 static int max8649_list_voltage(struct regulator_dev
*rdev
, unsigned index
)
76 return (MAX8649_DCDC_VMIN
+ index
* MAX8649_DCDC_STEP
);
79 static int max8649_get_voltage(struct regulator_dev
*rdev
)
81 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
86 ret
= regmap_read(info
->regmap
, info
->vol_reg
, &val
);
89 data
= (unsigned char)val
& MAX8649_VOL_MASK
;
90 return max8649_list_voltage(rdev
, data
);
93 static int max8649_set_voltage(struct regulator_dev
*rdev
,
94 int min_uV
, int max_uV
, unsigned *selector
)
96 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
97 unsigned char data
, mask
;
99 if (check_range(min_uV
, max_uV
)) {
100 dev_err(info
->dev
, "invalid voltage range (%d, %d) uV\n",
104 data
= (min_uV
- MAX8649_DCDC_VMIN
+ MAX8649_DCDC_STEP
- 1)
106 mask
= MAX8649_VOL_MASK
;
107 *selector
= data
& mask
;
109 return regmap_update_bits(info
->regmap
, info
->vol_reg
, mask
, data
);
112 /* EN_PD means pulldown on EN input */
113 static int max8649_enable(struct regulator_dev
*rdev
)
115 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
116 return regmap_update_bits(info
->regmap
, MAX8649_CONTROL
, MAX8649_EN_PD
, 0);
120 * Applied internal pulldown resistor on EN input pin.
121 * If pulldown EN pin outside, it would be better.
123 static int max8649_disable(struct regulator_dev
*rdev
)
125 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
126 return regmap_update_bits(info
->regmap
, MAX8649_CONTROL
, MAX8649_EN_PD
,
130 static int max8649_is_enabled(struct regulator_dev
*rdev
)
132 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
136 ret
= regmap_read(info
->regmap
, MAX8649_CONTROL
, &val
);
139 return !((unsigned char)val
& MAX8649_EN_PD
);
142 static int max8649_enable_time(struct regulator_dev
*rdev
)
144 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
145 int voltage
, rate
, ret
;
149 ret
= regmap_read(info
->regmap
, info
->vol_reg
, &val
);
152 val
&= MAX8649_VOL_MASK
;
153 voltage
= max8649_list_voltage(rdev
, (unsigned char)val
); /* uV */
156 ret
= regmap_read(info
->regmap
, MAX8649_RAMP
, &val
);
159 ret
= (val
& MAX8649_RAMP_MASK
) >> 5;
160 rate
= (32 * 1000) >> ret
; /* uV/uS */
162 return DIV_ROUND_UP(voltage
, rate
);
165 static int max8649_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
167 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
170 case REGULATOR_MODE_FAST
:
171 regmap_update_bits(info
->regmap
, info
->vol_reg
, MAX8649_FORCE_PWM
,
174 case REGULATOR_MODE_NORMAL
:
175 regmap_update_bits(info
->regmap
, info
->vol_reg
,
176 MAX8649_FORCE_PWM
, 0);
184 static unsigned int max8649_get_mode(struct regulator_dev
*rdev
)
186 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
190 ret
= regmap_read(info
->regmap
, info
->vol_reg
, &val
);
193 if (val
& MAX8649_FORCE_PWM
)
194 return REGULATOR_MODE_FAST
;
195 return REGULATOR_MODE_NORMAL
;
198 static struct regulator_ops max8649_dcdc_ops
= {
199 .set_voltage
= max8649_set_voltage
,
200 .get_voltage
= max8649_get_voltage
,
201 .list_voltage
= max8649_list_voltage
,
202 .enable
= max8649_enable
,
203 .disable
= max8649_disable
,
204 .is_enabled
= max8649_is_enabled
,
205 .enable_time
= max8649_enable_time
,
206 .set_mode
= max8649_set_mode
,
207 .get_mode
= max8649_get_mode
,
211 static struct regulator_desc dcdc_desc
= {
213 .ops
= &max8649_dcdc_ops
,
214 .type
= REGULATOR_VOLTAGE
,
215 .n_voltages
= 1 << 6,
216 .owner
= THIS_MODULE
,
219 static struct regmap_config max8649_regmap_config
= {
224 static int __devinit
max8649_regulator_probe(struct i2c_client
*client
,
225 const struct i2c_device_id
*id
)
227 struct max8649_platform_data
*pdata
= client
->dev
.platform_data
;
228 struct max8649_regulator_info
*info
= NULL
;
233 info
= kzalloc(sizeof(struct max8649_regulator_info
), GFP_KERNEL
);
235 dev_err(&client
->dev
, "No enough memory\n");
239 info
->regmap
= regmap_init_i2c(client
, &max8649_regmap_config
);
240 if (IS_ERR(info
->regmap
)) {
241 ret
= PTR_ERR(info
->regmap
);
242 dev_err(&client
->dev
, "Failed to allocate register map: %d\n", ret
);
246 info
->dev
= &client
->dev
;
247 i2c_set_clientdata(client
, info
);
249 info
->mode
= pdata
->mode
;
250 switch (info
->mode
) {
252 info
->vol_reg
= MAX8649_MODE0
;
255 info
->vol_reg
= MAX8649_MODE1
;
258 info
->vol_reg
= MAX8649_MODE2
;
261 info
->vol_reg
= MAX8649_MODE3
;
267 ret
= regmap_read(info
->regmap
, MAX8649_CHIP_ID1
, &val
);
269 dev_err(info
->dev
, "Failed to detect ID of MAX8649:%d\n",
273 dev_info(info
->dev
, "Detected MAX8649 (ID:%x)\n", ret
);
275 /* enable VID0 & VID1 */
276 regmap_update_bits(info
->regmap
, MAX8649_CONTROL
, MAX8649_VID_MASK
, 0);
278 /* enable/disable external clock synchronization */
279 info
->extclk
= pdata
->extclk
;
280 data
= (info
->extclk
) ? MAX8649_SYNC_EXTCLK
: 0;
281 regmap_update_bits(info
->regmap
, info
->vol_reg
, MAX8649_SYNC_EXTCLK
, data
);
283 /* set external clock frequency */
284 info
->extclk_freq
= pdata
->extclk_freq
;
285 regmap_update_bits(info
->regmap
, MAX8649_SYNC
, MAX8649_EXT_MASK
,
286 info
->extclk_freq
<< 6);
289 if (pdata
->ramp_timing
) {
290 info
->ramp_timing
= pdata
->ramp_timing
;
291 regmap_update_bits(info
->regmap
, MAX8649_RAMP
, MAX8649_RAMP_MASK
,
292 info
->ramp_timing
<< 5);
295 info
->ramp_down
= pdata
->ramp_down
;
296 if (info
->ramp_down
) {
297 regmap_update_bits(info
->regmap
, MAX8649_RAMP
, MAX8649_RAMP_DOWN
,
301 info
->regulator
= regulator_register(&dcdc_desc
, &client
->dev
,
302 pdata
->regulator
, info
, NULL
);
303 if (IS_ERR(info
->regulator
)) {
304 dev_err(info
->dev
, "failed to register regulator %s\n",
306 ret
= PTR_ERR(info
->regulator
);
310 dev_info(info
->dev
, "Max8649 regulator device is detected.\n");
313 regmap_exit(info
->regmap
);
319 static int __devexit
max8649_regulator_remove(struct i2c_client
*client
)
321 struct max8649_regulator_info
*info
= i2c_get_clientdata(client
);
325 regulator_unregister(info
->regulator
);
326 regmap_exit(info
->regmap
);
333 static const struct i2c_device_id max8649_id
[] = {
337 MODULE_DEVICE_TABLE(i2c
, max8649_id
);
339 static struct i2c_driver max8649_driver
= {
340 .probe
= max8649_regulator_probe
,
341 .remove
= __devexit_p(max8649_regulator_remove
),
345 .id_table
= max8649_id
,
348 static int __init
max8649_init(void)
350 return i2c_add_driver(&max8649_driver
);
352 subsys_initcall(max8649_init
);
354 static void __exit
max8649_exit(void)
356 i2c_del_driver(&max8649_driver
);
358 module_exit(max8649_exit
);
360 /* Module information */
361 MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
362 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
363 MODULE_LICENSE("GPL");