2 * max8952.c - Voltage and current regulation for the Maxim 8952
4 * Copyright (C) 2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/i2c.h>
25 #include <linux/err.h>
26 #include <linux/platform_device.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/max8952.h>
29 #include <linux/gpio.h>
31 #include <linux/slab.h>
47 struct i2c_client
*client
;
49 struct max8952_platform_data
*pdata
;
50 struct regulator_dev
*rdev
;
57 static int max8952_read_reg(struct max8952_data
*max8952
, u8 reg
)
59 int ret
= i2c_smbus_read_byte_data(max8952
->client
, reg
);
66 static int max8952_write_reg(struct max8952_data
*max8952
,
69 return i2c_smbus_write_byte_data(max8952
->client
, reg
, value
);
72 static int max8952_voltage(struct max8952_data
*max8952
, u8 mode
)
74 return (max8952
->pdata
->dvs_mode
[mode
] * 10 + 770) * 1000;
77 static int max8952_list_voltage(struct regulator_dev
*rdev
,
78 unsigned int selector
)
80 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
82 if (rdev_get_id(rdev
) != 0)
85 return max8952_voltage(max8952
, selector
);
88 static int max8952_is_enabled(struct regulator_dev
*rdev
)
90 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
94 static int max8952_enable(struct regulator_dev
*rdev
)
96 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
98 /* If not valid, assume "ALWAYS_HIGH" */
99 if (gpio_is_valid(max8952
->pdata
->gpio_en
))
100 gpio_set_value(max8952
->pdata
->gpio_en
, 1);
106 static int max8952_disable(struct regulator_dev
*rdev
)
108 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
110 /* If not valid, assume "ALWAYS_HIGH" -> not permitted */
111 if (gpio_is_valid(max8952
->pdata
->gpio_en
))
112 gpio_set_value(max8952
->pdata
->gpio_en
, 0);
120 static int max8952_get_voltage(struct regulator_dev
*rdev
)
122 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
130 return max8952_voltage(max8952
, vid
);
133 static int max8952_set_voltage(struct regulator_dev
*rdev
,
134 int min_uV
, int max_uV
, unsigned *selector
)
136 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
139 if (!gpio_is_valid(max8952
->pdata
->gpio_vid0
) ||
140 !gpio_is_valid(max8952
->pdata
->gpio_vid1
)) {
141 /* DVS not supported */
145 for (i
= 0; i
< MAX8952_NUM_DVS_MODE
; i
++) {
146 int volt
= max8952_voltage(max8952
, i
);
148 /* Set the voltage as low as possible within the range */
149 if (volt
<= max_uV
&& volt
>= min_uV
)
150 if (vid
== -1 || max8952_voltage(max8952
, vid
) > volt
)
154 if (vid
>= 0 && vid
< MAX8952_NUM_DVS_MODE
) {
155 max8952
->vid0
= (vid
% 2 == 1);
156 max8952
->vid1
= (((vid
>> 1) % 2) == 1);
158 gpio_set_value(max8952
->pdata
->gpio_vid0
, max8952
->vid0
);
159 gpio_set_value(max8952
->pdata
->gpio_vid1
, max8952
->vid1
);
166 static struct regulator_ops max8952_ops
= {
167 .list_voltage
= max8952_list_voltage
,
168 .is_enabled
= max8952_is_enabled
,
169 .enable
= max8952_enable
,
170 .disable
= max8952_disable
,
171 .get_voltage
= max8952_get_voltage
,
172 .set_voltage
= max8952_set_voltage
,
173 .set_suspend_disable
= max8952_disable
,
176 static struct regulator_desc regulator
= {
177 .name
= "MAX8952_VOUT",
179 .n_voltages
= MAX8952_NUM_DVS_MODE
,
181 .type
= REGULATOR_VOLTAGE
,
182 .owner
= THIS_MODULE
,
185 static int __devinit
max8952_pmic_probe(struct i2c_client
*client
,
186 const struct i2c_device_id
*i2c_id
)
188 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
189 struct max8952_platform_data
*pdata
= client
->dev
.platform_data
;
190 struct max8952_data
*max8952
;
192 int ret
= 0, err
= 0;
195 dev_err(&client
->dev
, "Require the platform data\n");
199 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
202 max8952
= kzalloc(sizeof(struct max8952_data
), GFP_KERNEL
);
206 max8952
->client
= client
;
207 max8952
->dev
= &client
->dev
;
208 max8952
->pdata
= pdata
;
210 max8952
->rdev
= regulator_register(®ulator
, max8952
->dev
,
211 &pdata
->reg_data
, max8952
, NULL
);
213 if (IS_ERR(max8952
->rdev
)) {
214 ret
= PTR_ERR(max8952
->rdev
);
215 dev_err(max8952
->dev
, "regulator init failed (%d)\n", ret
);
219 max8952
->en
= !!(pdata
->reg_data
.constraints
.boot_on
);
220 max8952
->vid0
= (pdata
->default_mode
% 2) == 1;
221 max8952
->vid1
= ((pdata
->default_mode
>> 1) % 2) == 1;
223 if (gpio_is_valid(pdata
->gpio_en
)) {
224 if (!gpio_request(pdata
->gpio_en
, "MAX8952 EN"))
225 gpio_direction_output(pdata
->gpio_en
, max8952
->en
);
232 dev_info(max8952
->dev
, "EN gpio invalid: assume that EN"
235 pdata
->gpio_en
= -1; /* Mark invalid */
240 if (gpio_is_valid(pdata
->gpio_vid0
) &&
241 gpio_is_valid(pdata
->gpio_vid1
)) {
242 if (!gpio_request(pdata
->gpio_vid0
, "MAX8952 VID0"))
243 gpio_direction_output(pdata
->gpio_vid0
,
244 (pdata
->default_mode
) % 2);
248 if (!gpio_request(pdata
->gpio_vid1
, "MAX8952 VID1"))
249 gpio_direction_output(pdata
->gpio_vid1
,
250 (pdata
->default_mode
>> 1) % 2);
253 gpio_free(pdata
->gpio_vid0
);
261 dev_warn(max8952
->dev
, "VID0/1 gpio invalid: "
262 "DVS not available.\n");
266 pdata
->gpio_vid0
= -1;
267 pdata
->gpio_vid1
= -1;
269 /* Disable Pulldown of EN only */
270 max8952_write_reg(max8952
, MAX8952_REG_CONTROL
, 0x60);
272 dev_err(max8952
->dev
, "DVS modes disabled because VID0 and VID1"
273 " do not have proper controls.\n");
276 * Disable Pulldown on EN, VID0, VID1 to reduce
277 * leakage current of MAX8952 assuming that MAX8952
278 * is turned on (EN==1). Note that without having VID0/1
279 * properly connected, turning pulldown off can be
280 * problematic. Thus, turn this off only when they are
281 * controllable by GPIO.
283 max8952_write_reg(max8952
, MAX8952_REG_CONTROL
, 0x0);
286 max8952_write_reg(max8952
, MAX8952_REG_MODE0
,
287 (max8952_read_reg(max8952
,
288 MAX8952_REG_MODE0
) & 0xC0) |
289 (pdata
->dvs_mode
[0] & 0x3F));
290 max8952_write_reg(max8952
, MAX8952_REG_MODE1
,
291 (max8952_read_reg(max8952
,
292 MAX8952_REG_MODE1
) & 0xC0) |
293 (pdata
->dvs_mode
[1] & 0x3F));
294 max8952_write_reg(max8952
, MAX8952_REG_MODE2
,
295 (max8952_read_reg(max8952
,
296 MAX8952_REG_MODE2
) & 0xC0) |
297 (pdata
->dvs_mode
[2] & 0x3F));
298 max8952_write_reg(max8952
, MAX8952_REG_MODE3
,
299 (max8952_read_reg(max8952
,
300 MAX8952_REG_MODE3
) & 0xC0) |
301 (pdata
->dvs_mode
[3] & 0x3F));
303 max8952_write_reg(max8952
, MAX8952_REG_SYNC
,
304 (max8952_read_reg(max8952
, MAX8952_REG_SYNC
) & 0x3F) |
305 ((pdata
->sync_freq
& 0x3) << 6));
306 max8952_write_reg(max8952
, MAX8952_REG_RAMP
,
307 (max8952_read_reg(max8952
, MAX8952_REG_RAMP
) & 0x1F) |
308 ((pdata
->ramp_speed
& 0x7) << 5));
310 i2c_set_clientdata(client
, max8952
);
319 static int __devexit
max8952_pmic_remove(struct i2c_client
*client
)
321 struct max8952_data
*max8952
= i2c_get_clientdata(client
);
322 struct max8952_platform_data
*pdata
= max8952
->pdata
;
323 struct regulator_dev
*rdev
= max8952
->rdev
;
325 regulator_unregister(rdev
);
327 gpio_free(pdata
->gpio_vid0
);
328 gpio_free(pdata
->gpio_vid1
);
329 gpio_free(pdata
->gpio_en
);
335 static const struct i2c_device_id max8952_ids
[] = {
339 MODULE_DEVICE_TABLE(i2c
, max8952_ids
);
341 static struct i2c_driver max8952_pmic_driver
= {
342 .probe
= max8952_pmic_probe
,
343 .remove
= __devexit_p(max8952_pmic_remove
),
347 .id_table
= max8952_ids
,
350 static int __init
max8952_pmic_init(void)
352 return i2c_add_driver(&max8952_pmic_driver
);
354 subsys_initcall(max8952_pmic_init
);
356 static void __exit
max8952_pmic_exit(void)
358 i2c_del_driver(&max8952_pmic_driver
);
360 module_exit(max8952_pmic_exit
);
362 MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
363 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
364 MODULE_LICENSE("GPL");