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>
32 #include <linux/of_gpio.h>
33 #include <linux/regulator/of_regulator.h>
34 #include <linux/slab.h>
50 struct i2c_client
*client
;
51 struct max8952_platform_data
*pdata
;
57 static int max8952_read_reg(struct max8952_data
*max8952
, u8 reg
)
59 int ret
= i2c_smbus_read_byte_data(max8952
->client
, reg
);
67 static int max8952_write_reg(struct max8952_data
*max8952
,
70 return i2c_smbus_write_byte_data(max8952
->client
, reg
, value
);
73 static int max8952_list_voltage(struct regulator_dev
*rdev
,
74 unsigned int selector
)
76 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
78 if (rdev_get_id(rdev
) != 0)
81 return (max8952
->pdata
->dvs_mode
[selector
] * 10 + 770) * 1000;
84 static int max8952_get_voltage_sel(struct regulator_dev
*rdev
)
86 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
97 static int max8952_set_voltage_sel(struct regulator_dev
*rdev
,
100 struct max8952_data
*max8952
= rdev_get_drvdata(rdev
);
102 if (!gpio_is_valid(max8952
->pdata
->gpio_vid0
) ||
103 !gpio_is_valid(max8952
->pdata
->gpio_vid1
)) {
104 /* DVS not supported */
108 max8952
->vid0
= selector
& 0x1;
109 max8952
->vid1
= (selector
>> 1) & 0x1;
110 gpio_set_value(max8952
->pdata
->gpio_vid0
, max8952
->vid0
);
111 gpio_set_value(max8952
->pdata
->gpio_vid1
, max8952
->vid1
);
116 static struct regulator_ops max8952_ops
= {
117 .list_voltage
= max8952_list_voltage
,
118 .get_voltage_sel
= max8952_get_voltage_sel
,
119 .set_voltage_sel
= max8952_set_voltage_sel
,
122 static const struct regulator_desc regulator
= {
123 .name
= "MAX8952_VOUT",
125 .n_voltages
= MAX8952_NUM_DVS_MODE
,
127 .type
= REGULATOR_VOLTAGE
,
128 .owner
= THIS_MODULE
,
132 static const struct of_device_id max8952_dt_match
[] = {
133 { .compatible
= "maxim,max8952" },
136 MODULE_DEVICE_TABLE(of
, max8952_dt_match
);
138 static struct max8952_platform_data
*max8952_parse_dt(struct device
*dev
)
140 struct max8952_platform_data
*pd
;
141 struct device_node
*np
= dev
->of_node
;
145 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
149 pd
->gpio_vid0
= of_get_named_gpio(np
, "max8952,vid-gpios", 0);
150 pd
->gpio_vid1
= of_get_named_gpio(np
, "max8952,vid-gpios", 1);
151 pd
->gpio_en
= of_get_named_gpio(np
, "max8952,en-gpio", 0);
153 if (of_property_read_u32(np
, "max8952,default-mode", &pd
->default_mode
))
154 dev_warn(dev
, "Default mode not specified, assuming 0\n");
156 ret
= of_property_read_u32_array(np
, "max8952,dvs-mode-microvolt",
157 pd
->dvs_mode
, ARRAY_SIZE(pd
->dvs_mode
));
159 dev_err(dev
, "max8952,dvs-mode-microvolt property not specified");
163 for (i
= 0; i
< ARRAY_SIZE(pd
->dvs_mode
); ++i
) {
164 if (pd
->dvs_mode
[i
] < 770000 || pd
->dvs_mode
[i
] > 1400000) {
165 dev_err(dev
, "DVS voltage %d out of range\n", i
);
168 pd
->dvs_mode
[i
] = (pd
->dvs_mode
[i
] - 770000) / 10000;
171 if (of_property_read_u32(np
, "max8952,sync-freq", &pd
->sync_freq
))
172 dev_warn(dev
, "max8952,sync-freq property not specified, defaulting to 26MHz\n");
174 if (of_property_read_u32(np
, "max8952,ramp-speed", &pd
->ramp_speed
))
175 dev_warn(dev
, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n");
177 pd
->reg_data
= of_get_regulator_init_data(dev
, np
);
179 dev_err(dev
, "Failed to parse regulator init data\n");
186 static struct max8952_platform_data
*max8952_parse_dt(struct device
*dev
)
192 static int max8952_pmic_probe(struct i2c_client
*client
,
193 const struct i2c_device_id
*i2c_id
)
195 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
196 struct max8952_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
197 struct regulator_config config
= { };
198 struct max8952_data
*max8952
;
199 struct regulator_dev
*rdev
;
201 int ret
= 0, err
= 0;
203 if (client
->dev
.of_node
)
204 pdata
= max8952_parse_dt(&client
->dev
);
207 dev_err(&client
->dev
, "Require the platform data\n");
211 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
))
214 max8952
= devm_kzalloc(&client
->dev
, sizeof(struct max8952_data
),
219 max8952
->client
= client
;
220 max8952
->pdata
= pdata
;
222 config
.dev
= &client
->dev
;
223 config
.init_data
= pdata
->reg_data
;
224 config
.driver_data
= max8952
;
225 config
.of_node
= client
->dev
.of_node
;
227 config
.ena_gpio
= pdata
->gpio_en
;
228 if (pdata
->reg_data
->constraints
.boot_on
)
229 config
.ena_gpio_flags
|= GPIOF_OUT_INIT_HIGH
;
231 rdev
= devm_regulator_register(&client
->dev
, ®ulator
, &config
);
234 dev_err(&client
->dev
, "regulator init failed (%d)\n", ret
);
238 max8952
->vid0
= pdata
->default_mode
& 0x1;
239 max8952
->vid1
= (pdata
->default_mode
>> 1) & 0x1;
241 if (gpio_is_valid(pdata
->gpio_vid0
) &&
242 gpio_is_valid(pdata
->gpio_vid1
)) {
243 unsigned long gpio_flags
;
245 gpio_flags
= max8952
->vid0
?
246 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
247 if (devm_gpio_request_one(&client
->dev
, pdata
->gpio_vid0
,
248 gpio_flags
, "MAX8952 VID0"))
251 gpio_flags
= max8952
->vid1
?
252 GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
253 if (devm_gpio_request_one(&client
->dev
, pdata
->gpio_vid1
,
254 gpio_flags
, "MAX8952 VID1"))
260 dev_warn(&client
->dev
, "VID0/1 gpio invalid: "
261 "DVS not available.\n");
265 pdata
->gpio_vid0
= -1;
266 pdata
->gpio_vid1
= -1;
268 /* Disable Pulldown of EN only */
269 max8952_write_reg(max8952
, MAX8952_REG_CONTROL
, 0x60);
271 dev_err(&client
->dev
, "DVS modes disabled because VID0 and VID1"
272 " do not have proper controls.\n");
275 * Disable Pulldown on EN, VID0, VID1 to reduce
276 * leakage current of MAX8952 assuming that MAX8952
277 * is turned on (EN==1). Note that without having VID0/1
278 * properly connected, turning pulldown off can be
279 * problematic. Thus, turn this off only when they are
280 * controllable by GPIO.
282 max8952_write_reg(max8952
, MAX8952_REG_CONTROL
, 0x0);
285 max8952_write_reg(max8952
, MAX8952_REG_MODE0
,
286 (max8952_read_reg(max8952
,
287 MAX8952_REG_MODE0
) & 0xC0) |
288 (pdata
->dvs_mode
[0] & 0x3F));
289 max8952_write_reg(max8952
, MAX8952_REG_MODE1
,
290 (max8952_read_reg(max8952
,
291 MAX8952_REG_MODE1
) & 0xC0) |
292 (pdata
->dvs_mode
[1] & 0x3F));
293 max8952_write_reg(max8952
, MAX8952_REG_MODE2
,
294 (max8952_read_reg(max8952
,
295 MAX8952_REG_MODE2
) & 0xC0) |
296 (pdata
->dvs_mode
[2] & 0x3F));
297 max8952_write_reg(max8952
, MAX8952_REG_MODE3
,
298 (max8952_read_reg(max8952
,
299 MAX8952_REG_MODE3
) & 0xC0) |
300 (pdata
->dvs_mode
[3] & 0x3F));
302 max8952_write_reg(max8952
, MAX8952_REG_SYNC
,
303 (max8952_read_reg(max8952
, MAX8952_REG_SYNC
) & 0x3F) |
304 ((pdata
->sync_freq
& 0x3) << 6));
305 max8952_write_reg(max8952
, MAX8952_REG_RAMP
,
306 (max8952_read_reg(max8952
, MAX8952_REG_RAMP
) & 0x1F) |
307 ((pdata
->ramp_speed
& 0x7) << 5));
309 i2c_set_clientdata(client
, max8952
);
314 static const struct i2c_device_id max8952_ids
[] = {
318 MODULE_DEVICE_TABLE(i2c
, max8952_ids
);
320 static struct i2c_driver max8952_pmic_driver
= {
321 .probe
= max8952_pmic_probe
,
324 .of_match_table
= of_match_ptr(max8952_dt_match
),
326 .id_table
= max8952_ids
,
329 static int __init
max8952_pmic_init(void)
331 return i2c_add_driver(&max8952_pmic_driver
);
333 subsys_initcall(max8952_pmic_init
);
335 static void __exit
max8952_pmic_exit(void)
337 i2c_del_driver(&max8952_pmic_driver
);
339 module_exit(max8952_pmic_exit
);
341 MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
342 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
343 MODULE_LICENSE("GPL");