ux500: remove ambiguous irq macros
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / max8952.c
blob0d5dda4fd911549689a1a6c1eefb1c6e3985b226
1 /*
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/mutex.h>
30 #include <linux/gpio.h>
31 #include <linux/io.h>
32 #include <linux/slab.h>
34 /* Registers */
35 enum {
36 MAX8952_REG_MODE0,
37 MAX8952_REG_MODE1,
38 MAX8952_REG_MODE2,
39 MAX8952_REG_MODE3,
40 MAX8952_REG_CONTROL,
41 MAX8952_REG_SYNC,
42 MAX8952_REG_RAMP,
43 MAX8952_REG_CHIP_ID1,
44 MAX8952_REG_CHIP_ID2,
47 struct max8952_data {
48 struct i2c_client *client;
49 struct device *dev;
50 struct mutex mutex;
51 struct max8952_platform_data *pdata;
52 struct regulator_dev *rdev;
54 bool vid0;
55 bool vid1;
56 bool en;
59 static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
61 int ret = i2c_smbus_read_byte_data(max8952->client, reg);
62 if (ret > 0)
63 ret &= 0xff;
65 return ret;
68 static int max8952_write_reg(struct max8952_data *max8952,
69 u8 reg, u8 value)
71 return i2c_smbus_write_byte_data(max8952->client, reg, value);
74 static int max8952_voltage(struct max8952_data *max8952, u8 mode)
76 return (max8952->pdata->dvs_mode[mode] * 10 + 770) * 1000;
79 static int max8952_list_voltage(struct regulator_dev *rdev,
80 unsigned int selector)
82 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
84 if (rdev_get_id(rdev) != 0)
85 return -EINVAL;
87 return max8952_voltage(max8952, selector);
90 static int max8952_is_enabled(struct regulator_dev *rdev)
92 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
93 return max8952->en;
96 static int max8952_enable(struct regulator_dev *rdev)
98 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
100 /* If not valid, assume "ALWAYS_HIGH" */
101 if (gpio_is_valid(max8952->pdata->gpio_en))
102 gpio_set_value(max8952->pdata->gpio_en, 1);
104 max8952->en = true;
105 return 0;
108 static int max8952_disable(struct regulator_dev *rdev)
110 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
112 /* If not valid, assume "ALWAYS_HIGH" -> not permitted */
113 if (gpio_is_valid(max8952->pdata->gpio_en))
114 gpio_set_value(max8952->pdata->gpio_en, 0);
115 else
116 return -EPERM;
118 max8952->en = false;
119 return 0;
122 static int max8952_get_voltage(struct regulator_dev *rdev)
124 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
125 u8 vid = 0;
127 if (max8952->vid0)
128 vid += 1;
129 if (max8952->vid1)
130 vid += 2;
132 return max8952_voltage(max8952, vid);
135 static int max8952_set_voltage(struct regulator_dev *rdev,
136 int min_uV, int max_uV)
138 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
139 s8 vid = -1, i;
141 if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
142 !gpio_is_valid(max8952->pdata->gpio_vid0)) {
143 /* DVS not supported */
144 return -EPERM;
147 for (i = 0; i < MAX8952_NUM_DVS_MODE; i++) {
148 int volt = max8952_voltage(max8952, i);
150 /* Set the voltage as low as possible within the range */
151 if (volt <= max_uV && volt >= min_uV)
152 if (vid == -1 || max8952_voltage(max8952, vid) > volt)
153 vid = i;
156 if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) {
157 max8952->vid0 = (vid % 2 == 1);
158 max8952->vid1 = (((vid >> 1) % 2) == 1);
159 gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
160 gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
161 } else
162 return -EINVAL;
164 return 0;
167 static struct regulator_ops max8952_ops = {
168 .list_voltage = max8952_list_voltage,
169 .is_enabled = max8952_is_enabled,
170 .enable = max8952_enable,
171 .disable = max8952_disable,
172 .get_voltage = max8952_get_voltage,
173 .set_voltage = max8952_set_voltage,
174 .set_suspend_disable = max8952_disable,
177 static struct regulator_desc regulator = {
178 .name = "MAX8952_VOUT",
179 .id = 0,
180 .n_voltages = MAX8952_NUM_DVS_MODE,
181 .ops = &max8952_ops,
182 .type = REGULATOR_VOLTAGE,
183 .owner = THIS_MODULE,
186 static int __devinit max8952_pmic_probe(struct i2c_client *client,
187 const struct i2c_device_id *i2c_id)
189 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
190 struct max8952_platform_data *pdata = client->dev.platform_data;
191 struct max8952_data *max8952;
193 int ret = 0, err = 0;
195 if (!pdata) {
196 dev_err(&client->dev, "Require the platform data\n");
197 return -EINVAL;
200 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
201 return -EIO;
203 max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL);
204 if (!max8952)
205 return -ENOMEM;
207 max8952->client = client;
208 max8952->dev = &client->dev;
209 max8952->pdata = pdata;
210 mutex_init(&max8952->mutex);
212 max8952->rdev = regulator_register(&regulator, max8952->dev,
213 &pdata->reg_data, max8952);
215 if (IS_ERR(max8952->rdev)) {
216 ret = PTR_ERR(max8952->rdev);
217 dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
218 goto err_reg;
221 max8952->en = !!(pdata->reg_data.constraints.boot_on);
222 max8952->vid0 = (pdata->default_mode % 2) == 1;
223 max8952->vid1 = ((pdata->default_mode >> 1) % 2) == 1;
225 if (gpio_is_valid(pdata->gpio_en)) {
226 if (!gpio_request(pdata->gpio_en, "MAX8952 EN"))
227 gpio_direction_output(pdata->gpio_en, max8952->en);
228 else
229 err = 1;
230 } else
231 err = 2;
233 if (err) {
234 dev_info(max8952->dev, "EN gpio invalid: assume that EN"
235 "is always High\n");
236 max8952->en = 1;
237 pdata->gpio_en = -1; /* Mark invalid */
240 err = 0;
242 if (gpio_is_valid(pdata->gpio_vid0) &&
243 gpio_is_valid(pdata->gpio_vid1)) {
244 if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
245 gpio_direction_output(pdata->gpio_vid0,
246 (pdata->default_mode) % 2);
247 else
248 err = 1;
250 if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
251 gpio_direction_output(pdata->gpio_vid1,
252 (pdata->default_mode >> 1) % 2);
253 else {
254 if (!err)
255 gpio_free(pdata->gpio_vid0);
256 err = 2;
259 } else
260 err = 3;
262 if (err) {
263 dev_warn(max8952->dev, "VID0/1 gpio invalid: "
264 "DVS not avilable.\n");
265 max8952->vid0 = 0;
266 max8952->vid1 = 0;
267 /* Mark invalid */
268 pdata->gpio_vid0 = -1;
269 pdata->gpio_vid1 = -1;
271 /* Disable Pulldown of EN only */
272 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
274 dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1"
275 " do not have proper controls.\n");
276 } else {
278 * Disable Pulldown on EN, VID0, VID1 to reduce
279 * leakage current of MAX8952 assuming that MAX8952
280 * is turned on (EN==1). Note that without having VID0/1
281 * properly connected, turning pulldown off can be
282 * problematic. Thus, turn this off only when they are
283 * controllable by GPIO.
285 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
288 max8952_write_reg(max8952, MAX8952_REG_MODE0,
289 (max8952_read_reg(max8952,
290 MAX8952_REG_MODE0) & 0xC0) |
291 (pdata->dvs_mode[0] & 0x3F));
292 max8952_write_reg(max8952, MAX8952_REG_MODE1,
293 (max8952_read_reg(max8952,
294 MAX8952_REG_MODE1) & 0xC0) |
295 (pdata->dvs_mode[1] & 0x3F));
296 max8952_write_reg(max8952, MAX8952_REG_MODE2,
297 (max8952_read_reg(max8952,
298 MAX8952_REG_MODE2) & 0xC0) |
299 (pdata->dvs_mode[2] & 0x3F));
300 max8952_write_reg(max8952, MAX8952_REG_MODE3,
301 (max8952_read_reg(max8952,
302 MAX8952_REG_MODE3) & 0xC0) |
303 (pdata->dvs_mode[3] & 0x3F));
305 max8952_write_reg(max8952, MAX8952_REG_SYNC,
306 (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
307 ((pdata->sync_freq & 0x3) << 6));
308 max8952_write_reg(max8952, MAX8952_REG_RAMP,
309 (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
310 ((pdata->ramp_speed & 0x7) << 5));
312 i2c_set_clientdata(client, max8952);
314 return 0;
316 err_reg:
317 kfree(max8952);
318 return ret;
321 static int __devexit max8952_pmic_remove(struct i2c_client *client)
323 struct max8952_data *max8952 = i2c_get_clientdata(client);
324 struct max8952_platform_data *pdata = max8952->pdata;
325 struct regulator_dev *rdev = max8952->rdev;
327 regulator_unregister(rdev);
329 gpio_free(pdata->gpio_vid0);
330 gpio_free(pdata->gpio_vid1);
331 gpio_free(pdata->gpio_en);
333 kfree(max8952);
334 return 0;
337 static const struct i2c_device_id max8952_ids[] = {
338 { "max8952", 0 },
339 { },
341 MODULE_DEVICE_TABLE(i2c, max8952_ids);
343 static struct i2c_driver max8952_pmic_driver = {
344 .probe = max8952_pmic_probe,
345 .remove = __devexit_p(max8952_pmic_remove),
346 .driver = {
347 .name = "max8952",
349 .id_table = max8952_ids,
352 static int __init max8952_pmic_init(void)
354 return i2c_add_driver(&max8952_pmic_driver);
356 subsys_initcall(max8952_pmic_init);
358 static void __exit max8952_pmic_exit(void)
360 i2c_del_driver(&max8952_pmic_driver);
362 module_exit(max8952_pmic_exit);
364 MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
365 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
366 MODULE_LICENSE("GPL");