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>
20 #define MAX8649_DCDC_VMIN 750000 /* uV */
21 #define MAX8649_DCDC_VMAX 1380000 /* uV */
22 #define MAX8649_DCDC_STEP 10000 /* uV */
23 #define MAX8649_VOL_MASK 0x3f
26 #define MAX8649_MODE0 0x00
27 #define MAX8649_MODE1 0x01
28 #define MAX8649_MODE2 0x02
29 #define MAX8649_MODE3 0x03
30 #define MAX8649_CONTROL 0x04
31 #define MAX8649_SYNC 0x05
32 #define MAX8649_RAMP 0x06
33 #define MAX8649_CHIP_ID1 0x08
34 #define MAX8649_CHIP_ID2 0x09
37 #define MAX8649_EN_PD (1 << 7)
38 #define MAX8649_VID0_PD (1 << 6)
39 #define MAX8649_VID1_PD (1 << 5)
40 #define MAX8649_VID_MASK (3 << 5)
42 #define MAX8649_FORCE_PWM (1 << 7)
43 #define MAX8649_SYNC_EXTCLK (1 << 6)
45 #define MAX8649_EXT_MASK (3 << 6)
47 #define MAX8649_RAMP_MASK (7 << 5)
48 #define MAX8649_RAMP_DOWN (1 << 1)
50 struct max8649_regulator_info
{
51 struct regulator_dev
*regulator
;
52 struct i2c_client
*i2c
;
57 unsigned mode
:2; /* bit[1:0] = VID1, VID0 */
58 unsigned extclk_freq
:2;
60 unsigned ramp_timing
:3;
66 static inline int max8649_read_device(struct i2c_client
*i2c
,
67 int reg
, int bytes
, void *dest
)
72 data
= (unsigned char)reg
;
73 ret
= i2c_master_send(i2c
, &data
, 1);
76 ret
= i2c_master_recv(i2c
, dest
, bytes
);
82 static inline int max8649_write_device(struct i2c_client
*i2c
,
83 int reg
, int bytes
, void *src
)
85 unsigned char buf
[bytes
+ 1];
88 buf
[0] = (unsigned char)reg
;
89 memcpy(&buf
[1], src
, bytes
);
91 ret
= i2c_master_send(i2c
, buf
, bytes
+ 1);
97 static int max8649_reg_read(struct i2c_client
*i2c
, int reg
)
99 struct max8649_regulator_info
*info
= i2c_get_clientdata(i2c
);
103 mutex_lock(&info
->io_lock
);
104 ret
= max8649_read_device(i2c
, reg
, 1, &data
);
105 mutex_unlock(&info
->io_lock
);
112 static int max8649_set_bits(struct i2c_client
*i2c
, int reg
,
113 unsigned char mask
, unsigned char data
)
115 struct max8649_regulator_info
*info
= i2c_get_clientdata(i2c
);
119 mutex_lock(&info
->io_lock
);
120 ret
= max8649_read_device(i2c
, reg
, 1, &value
);
125 ret
= max8649_write_device(i2c
, reg
, 1, &value
);
127 mutex_unlock(&info
->io_lock
);
131 static inline int check_range(int min_uV
, int max_uV
)
133 if ((min_uV
< MAX8649_DCDC_VMIN
) || (max_uV
> MAX8649_DCDC_VMAX
)
134 || (min_uV
> max_uV
))
139 static int max8649_list_voltage(struct regulator_dev
*rdev
, unsigned index
)
141 return (MAX8649_DCDC_VMIN
+ index
* MAX8649_DCDC_STEP
);
144 static int max8649_get_voltage(struct regulator_dev
*rdev
)
146 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
150 ret
= max8649_reg_read(info
->i2c
, info
->vol_reg
);
153 data
= (unsigned char)ret
& MAX8649_VOL_MASK
;
154 return max8649_list_voltage(rdev
, data
);
157 static int max8649_set_voltage(struct regulator_dev
*rdev
,
158 int min_uV
, int max_uV
, unsigned *selector
)
160 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
161 unsigned char data
, mask
;
163 if (check_range(min_uV
, max_uV
)) {
164 dev_err(info
->dev
, "invalid voltage range (%d, %d) uV\n",
168 data
= (min_uV
- MAX8649_DCDC_VMIN
+ MAX8649_DCDC_STEP
- 1)
170 mask
= MAX8649_VOL_MASK
;
171 *selector
= data
& mask
;
173 return max8649_set_bits(info
->i2c
, info
->vol_reg
, mask
, data
);
176 /* EN_PD means pulldown on EN input */
177 static int max8649_enable(struct regulator_dev
*rdev
)
179 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
180 return max8649_set_bits(info
->i2c
, MAX8649_CONTROL
, MAX8649_EN_PD
, 0);
184 * Applied internal pulldown resistor on EN input pin.
185 * If pulldown EN pin outside, it would be better.
187 static int max8649_disable(struct regulator_dev
*rdev
)
189 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
190 return max8649_set_bits(info
->i2c
, MAX8649_CONTROL
, MAX8649_EN_PD
,
194 static int max8649_is_enabled(struct regulator_dev
*rdev
)
196 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
199 ret
= max8649_reg_read(info
->i2c
, MAX8649_CONTROL
);
202 return !((unsigned char)ret
& MAX8649_EN_PD
);
205 static int max8649_enable_time(struct regulator_dev
*rdev
)
207 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
208 int voltage
, rate
, ret
;
211 ret
= max8649_reg_read(info
->i2c
, info
->vol_reg
);
214 ret
&= MAX8649_VOL_MASK
;
215 voltage
= max8649_list_voltage(rdev
, (unsigned char)ret
); /* uV */
218 ret
= max8649_reg_read(info
->i2c
, MAX8649_RAMP
);
221 ret
= (ret
& MAX8649_RAMP_MASK
) >> 5;
222 rate
= (32 * 1000) >> ret
; /* uV/uS */
224 return (voltage
/ rate
);
227 static int max8649_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
229 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
232 case REGULATOR_MODE_FAST
:
233 max8649_set_bits(info
->i2c
, info
->vol_reg
, MAX8649_FORCE_PWM
,
236 case REGULATOR_MODE_NORMAL
:
237 max8649_set_bits(info
->i2c
, info
->vol_reg
,
238 MAX8649_FORCE_PWM
, 0);
246 static unsigned int max8649_get_mode(struct regulator_dev
*rdev
)
248 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
251 ret
= max8649_reg_read(info
->i2c
, info
->vol_reg
);
252 if (ret
& MAX8649_FORCE_PWM
)
253 return REGULATOR_MODE_FAST
;
254 return REGULATOR_MODE_NORMAL
;
257 static struct regulator_ops max8649_dcdc_ops
= {
258 .set_voltage
= max8649_set_voltage
,
259 .get_voltage
= max8649_get_voltage
,
260 .list_voltage
= max8649_list_voltage
,
261 .enable
= max8649_enable
,
262 .disable
= max8649_disable
,
263 .is_enabled
= max8649_is_enabled
,
264 .enable_time
= max8649_enable_time
,
265 .set_mode
= max8649_set_mode
,
266 .get_mode
= max8649_get_mode
,
270 static struct regulator_desc dcdc_desc
= {
272 .ops
= &max8649_dcdc_ops
,
273 .type
= REGULATOR_VOLTAGE
,
274 .n_voltages
= 1 << 6,
275 .owner
= THIS_MODULE
,
278 static int __devinit
max8649_regulator_probe(struct i2c_client
*client
,
279 const struct i2c_device_id
*id
)
281 struct max8649_platform_data
*pdata
= client
->dev
.platform_data
;
282 struct max8649_regulator_info
*info
= NULL
;
286 info
= kzalloc(sizeof(struct max8649_regulator_info
), GFP_KERNEL
);
288 dev_err(&client
->dev
, "No enough memory\n");
293 info
->dev
= &client
->dev
;
294 mutex_init(&info
->io_lock
);
295 i2c_set_clientdata(client
, info
);
297 info
->mode
= pdata
->mode
;
298 switch (info
->mode
) {
300 info
->vol_reg
= MAX8649_MODE0
;
303 info
->vol_reg
= MAX8649_MODE1
;
306 info
->vol_reg
= MAX8649_MODE2
;
309 info
->vol_reg
= MAX8649_MODE3
;
315 ret
= max8649_reg_read(info
->i2c
, MAX8649_CHIP_ID1
);
317 dev_err(info
->dev
, "Failed to detect ID of MAX8649:%d\n",
321 dev_info(info
->dev
, "Detected MAX8649 (ID:%x)\n", ret
);
323 /* enable VID0 & VID1 */
324 max8649_set_bits(info
->i2c
, MAX8649_CONTROL
, MAX8649_VID_MASK
, 0);
326 /* enable/disable external clock synchronization */
327 info
->extclk
= pdata
->extclk
;
328 data
= (info
->extclk
) ? MAX8649_SYNC_EXTCLK
: 0;
329 max8649_set_bits(info
->i2c
, info
->vol_reg
, MAX8649_SYNC_EXTCLK
, data
);
331 /* set external clock frequency */
332 info
->extclk_freq
= pdata
->extclk_freq
;
333 max8649_set_bits(info
->i2c
, MAX8649_SYNC
, MAX8649_EXT_MASK
,
334 info
->extclk_freq
<< 6);
337 if (pdata
->ramp_timing
) {
338 info
->ramp_timing
= pdata
->ramp_timing
;
339 max8649_set_bits(info
->i2c
, MAX8649_RAMP
, MAX8649_RAMP_MASK
,
340 info
->ramp_timing
<< 5);
343 info
->ramp_down
= pdata
->ramp_down
;
344 if (info
->ramp_down
) {
345 max8649_set_bits(info
->i2c
, MAX8649_RAMP
, MAX8649_RAMP_DOWN
,
349 info
->regulator
= regulator_register(&dcdc_desc
, &client
->dev
,
350 pdata
->regulator
, info
);
351 if (IS_ERR(info
->regulator
)) {
352 dev_err(info
->dev
, "failed to register regulator %s\n",
354 ret
= PTR_ERR(info
->regulator
);
358 dev_info(info
->dev
, "Max8649 regulator device is detected.\n");
365 static int __devexit
max8649_regulator_remove(struct i2c_client
*client
)
367 struct max8649_regulator_info
*info
= i2c_get_clientdata(client
);
371 regulator_unregister(info
->regulator
);
378 static const struct i2c_device_id max8649_id
[] = {
382 MODULE_DEVICE_TABLE(i2c
, max8649_id
);
384 static struct i2c_driver max8649_driver
= {
385 .probe
= max8649_regulator_probe
,
386 .remove
= __devexit_p(max8649_regulator_remove
),
390 .id_table
= max8649_id
,
393 static int __init
max8649_init(void)
395 return i2c_add_driver(&max8649_driver
);
397 subsys_initcall(max8649_init
);
399 static void __exit
max8649_exit(void)
401 i2c_del_driver(&max8649_driver
);
403 module_exit(max8649_exit
);
405 /* Module information */
406 MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
407 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
408 MODULE_LICENSE("GPL");