Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / regulator / max8952.c
blob75d89400c1234db391556ec14e0ced14775580ac
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/gpio.h>
30 #include <linux/io.h>
31 #include <linux/slab.h>
33 /* Registers */
34 enum {
35 MAX8952_REG_MODE0,
36 MAX8952_REG_MODE1,
37 MAX8952_REG_MODE2,
38 MAX8952_REG_MODE3,
39 MAX8952_REG_CONTROL,
40 MAX8952_REG_SYNC,
41 MAX8952_REG_RAMP,
42 MAX8952_REG_CHIP_ID1,
43 MAX8952_REG_CHIP_ID2,
46 struct max8952_data {
47 struct i2c_client *client;
48 struct device *dev;
49 struct max8952_platform_data *pdata;
50 struct regulator_dev *rdev;
52 bool vid0;
53 bool vid1;
54 bool en;
57 static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
59 int ret = i2c_smbus_read_byte_data(max8952->client, reg);
60 if (ret > 0)
61 ret &= 0xff;
63 return ret;
66 static int max8952_write_reg(struct max8952_data *max8952,
67 u8 reg, u8 value)
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)
83 return -EINVAL;
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);
91 return max8952->en;
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);
102 max8952->en = true;
103 return 0;
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);
113 else
114 return -EPERM;
116 max8952->en = false;
117 return 0;
120 static int max8952_get_voltage(struct regulator_dev *rdev)
122 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
123 u8 vid = 0;
125 if (max8952->vid0)
126 vid += 1;
127 if (max8952->vid1)
128 vid += 2;
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);
137 s8 vid = -1, i;
139 if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
140 !gpio_is_valid(max8952->pdata->gpio_vid1)) {
141 /* DVS not supported */
142 return -EPERM;
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)
151 vid = i;
154 if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) {
155 max8952->vid0 = (vid % 2 == 1);
156 max8952->vid1 = (((vid >> 1) % 2) == 1);
157 *selector = vid;
158 gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
159 gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
160 } else
161 return -EINVAL;
163 return 0;
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",
178 .id = 0,
179 .n_voltages = MAX8952_NUM_DVS_MODE,
180 .ops = &max8952_ops,
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;
194 if (!pdata) {
195 dev_err(&client->dev, "Require the platform data\n");
196 return -EINVAL;
199 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
200 return -EIO;
202 max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL);
203 if (!max8952)
204 return -ENOMEM;
206 max8952->client = client;
207 max8952->dev = &client->dev;
208 max8952->pdata = pdata;
210 max8952->rdev = regulator_register(&regulator, 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);
216 goto err_reg;
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);
226 else
227 err = 1;
228 } else
229 err = 2;
231 if (err) {
232 dev_info(max8952->dev, "EN gpio invalid: assume that EN"
233 "is always High\n");
234 max8952->en = 1;
235 pdata->gpio_en = -1; /* Mark invalid */
238 err = 0;
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);
245 else
246 err = 1;
248 if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
249 gpio_direction_output(pdata->gpio_vid1,
250 (pdata->default_mode >> 1) % 2);
251 else {
252 if (!err)
253 gpio_free(pdata->gpio_vid0);
254 err = 2;
257 } else
258 err = 3;
260 if (err) {
261 dev_warn(max8952->dev, "VID0/1 gpio invalid: "
262 "DVS not available.\n");
263 max8952->vid0 = 0;
264 max8952->vid1 = 0;
265 /* Mark invalid */
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");
274 } else {
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);
312 return 0;
314 err_reg:
315 kfree(max8952);
316 return ret;
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);
331 kfree(max8952);
332 return 0;
335 static const struct i2c_device_id max8952_ids[] = {
336 { "max8952", 0 },
337 { },
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),
344 .driver = {
345 .name = "max8952",
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");