blob: a8f4ecfb0843b1ce31f79b9a95b14f8e8d1f5466 [file] [log] [blame]
MyungJoo Ham202f4f52010-08-20 14:43:56 +09001/*
2 * max8952.c - Voltage and current regulation for the Maxim 8952
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
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>
33
34/* Registers */
35enum {
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,
45};
46
47struct 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;
53
54 bool vid0;
55 bool vid1;
56 bool en;
57};
58
59static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
60{
61 int ret = i2c_smbus_read_byte_data(max8952->client, reg);
62 if (ret > 0)
63 ret &= 0xff;
64
65 return ret;
66}
67
68static int max8952_write_reg(struct max8952_data *max8952,
69 u8 reg, u8 value)
70{
71 return i2c_smbus_write_byte_data(max8952->client, reg, value);
72}
73
74static int max8952_voltage(struct max8952_data *max8952, u8 mode)
75{
76 return (max8952->pdata->dvs_mode[mode] * 10 + 770) * 1000;
77}
78
79static int max8952_list_voltage(struct regulator_dev *rdev,
80 unsigned int selector)
81{
82 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
83
84 if (rdev_get_id(rdev) != 0)
85 return -EINVAL;
86
87 return max8952_voltage(max8952, selector);
88}
89
90static int max8952_is_enabled(struct regulator_dev *rdev)
91{
92 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
93 return max8952->en;
94}
95
96static int max8952_enable(struct regulator_dev *rdev)
97{
98 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
99
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);
103
104 max8952->en = true;
105 return 0;
106}
107
108static int max8952_disable(struct regulator_dev *rdev)
109{
110 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
111
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;
117
118 max8952->en = false;
119 return 0;
120}
121
122static int max8952_get_voltage(struct regulator_dev *rdev)
123{
124 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
125 u8 vid = 0;
126
127 if (max8952->vid0)
128 vid += 1;
129 if (max8952->vid1)
130 vid += 2;
131
132 return max8952_voltage(max8952, vid);
133}
134
135static int max8952_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000136 int min_uV, int max_uV, unsigned *selector)
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900137{
138 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
Axel Linec10b0e2010-10-26 07:55:54 +0800139 s8 vid = -1, i;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900140
141 if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
142 !gpio_is_valid(max8952->pdata->gpio_vid0)) {
143 /* DVS not supported */
144 return -EPERM;
145 }
146
147 for (i = 0; i < MAX8952_NUM_DVS_MODE; i++) {
148 int volt = max8952_voltage(max8952, i);
149
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;
154 }
155
156 if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) {
157 max8952->vid0 = (vid % 2 == 1);
158 max8952->vid1 = (((vid >> 1) % 2) == 1);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000159 *selector = vid;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900160 gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
161 gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
162 } else
163 return -EINVAL;
164
165 return 0;
166}
167
168static struct regulator_ops max8952_ops = {
169 .list_voltage = max8952_list_voltage,
170 .is_enabled = max8952_is_enabled,
171 .enable = max8952_enable,
172 .disable = max8952_disable,
173 .get_voltage = max8952_get_voltage,
174 .set_voltage = max8952_set_voltage,
175 .set_suspend_disable = max8952_disable,
176};
177
178static struct regulator_desc regulator = {
179 .name = "MAX8952_VOUT",
180 .id = 0,
181 .n_voltages = MAX8952_NUM_DVS_MODE,
182 .ops = &max8952_ops,
183 .type = REGULATOR_VOLTAGE,
184 .owner = THIS_MODULE,
185};
186
187static int __devinit max8952_pmic_probe(struct i2c_client *client,
188 const struct i2c_device_id *i2c_id)
189{
190 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
191 struct max8952_platform_data *pdata = client->dev.platform_data;
192 struct max8952_data *max8952;
193
194 int ret = 0, err = 0;
195
196 if (!pdata) {
197 dev_err(&client->dev, "Require the platform data\n");
198 return -EINVAL;
199 }
200
201 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
202 return -EIO;
203
204 max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL);
205 if (!max8952)
206 return -ENOMEM;
207
208 max8952->client = client;
209 max8952->dev = &client->dev;
210 max8952->pdata = pdata;
211 mutex_init(&max8952->mutex);
212
213 max8952->rdev = regulator_register(&regulator, max8952->dev,
214 &pdata->reg_data, max8952);
215
Axel Linda057382010-10-25 10:11:07 +0800216 if (IS_ERR(max8952->rdev)) {
217 ret = PTR_ERR(max8952->rdev);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900218 dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
Axel Linda057382010-10-25 10:11:07 +0800219 goto err_reg;
220 }
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900221
222 max8952->en = !!(pdata->reg_data.constraints.boot_on);
223 max8952->vid0 = (pdata->default_mode % 2) == 1;
224 max8952->vid1 = ((pdata->default_mode >> 1) % 2) == 1;
225
226 if (gpio_is_valid(pdata->gpio_en)) {
227 if (!gpio_request(pdata->gpio_en, "MAX8952 EN"))
228 gpio_direction_output(pdata->gpio_en, max8952->en);
229 else
230 err = 1;
231 } else
232 err = 2;
233
234 if (err) {
235 dev_info(max8952->dev, "EN gpio invalid: assume that EN"
236 "is always High\n");
237 max8952->en = 1;
238 pdata->gpio_en = -1; /* Mark invalid */
239 }
240
241 err = 0;
242
243 if (gpio_is_valid(pdata->gpio_vid0) &&
244 gpio_is_valid(pdata->gpio_vid1)) {
245 if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
246 gpio_direction_output(pdata->gpio_vid0,
247 (pdata->default_mode) % 2);
248 else
249 err = 1;
250
251 if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
252 gpio_direction_output(pdata->gpio_vid1,
253 (pdata->default_mode >> 1) % 2);
254 else {
255 if (!err)
256 gpio_free(pdata->gpio_vid0);
257 err = 2;
258 }
259
260 } else
261 err = 3;
262
263 if (err) {
264 dev_warn(max8952->dev, "VID0/1 gpio invalid: "
265 "DVS not avilable.\n");
266 max8952->vid0 = 0;
267 max8952->vid1 = 0;
268 /* Mark invalid */
269 pdata->gpio_vid0 = -1;
270 pdata->gpio_vid1 = -1;
271
272 /* Disable Pulldown of EN only */
273 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
274
275 dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1"
276 " do not have proper controls.\n");
277 } else {
278 /*
279 * Disable Pulldown on EN, VID0, VID1 to reduce
280 * leakage current of MAX8952 assuming that MAX8952
281 * is turned on (EN==1). Note that without having VID0/1
282 * properly connected, turning pulldown off can be
283 * problematic. Thus, turn this off only when they are
284 * controllable by GPIO.
285 */
286 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
287 }
288
289 max8952_write_reg(max8952, MAX8952_REG_MODE0,
290 (max8952_read_reg(max8952,
291 MAX8952_REG_MODE0) & 0xC0) |
292 (pdata->dvs_mode[0] & 0x3F));
293 max8952_write_reg(max8952, MAX8952_REG_MODE1,
294 (max8952_read_reg(max8952,
295 MAX8952_REG_MODE1) & 0xC0) |
296 (pdata->dvs_mode[1] & 0x3F));
297 max8952_write_reg(max8952, MAX8952_REG_MODE2,
298 (max8952_read_reg(max8952,
299 MAX8952_REG_MODE2) & 0xC0) |
300 (pdata->dvs_mode[2] & 0x3F));
301 max8952_write_reg(max8952, MAX8952_REG_MODE3,
302 (max8952_read_reg(max8952,
303 MAX8952_REG_MODE3) & 0xC0) |
304 (pdata->dvs_mode[3] & 0x3F));
305
306 max8952_write_reg(max8952, MAX8952_REG_SYNC,
307 (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
308 ((pdata->sync_freq & 0x3) << 6));
309 max8952_write_reg(max8952, MAX8952_REG_RAMP,
310 (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
311 ((pdata->ramp_speed & 0x7) << 5));
312
313 i2c_set_clientdata(client, max8952);
314
Axel Linda057382010-10-25 10:11:07 +0800315 return 0;
316
317err_reg:
318 kfree(max8952);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900319 return ret;
320}
321
322static int __devexit max8952_pmic_remove(struct i2c_client *client)
323{
324 struct max8952_data *max8952 = i2c_get_clientdata(client);
325 struct max8952_platform_data *pdata = max8952->pdata;
326 struct regulator_dev *rdev = max8952->rdev;
327
328 regulator_unregister(rdev);
329
330 gpio_free(pdata->gpio_vid0);
331 gpio_free(pdata->gpio_vid1);
332 gpio_free(pdata->gpio_en);
333
334 kfree(max8952);
335 return 0;
336}
337
338static const struct i2c_device_id max8952_ids[] = {
339 { "max8952", 0 },
340 { },
341};
342MODULE_DEVICE_TABLE(i2c, max8952_ids);
343
344static struct i2c_driver max8952_pmic_driver = {
345 .probe = max8952_pmic_probe,
346 .remove = __devexit_p(max8952_pmic_remove),
347 .driver = {
348 .name = "max8952",
349 },
350 .id_table = max8952_ids,
351};
352
353static int __init max8952_pmic_init(void)
354{
355 return i2c_add_driver(&max8952_pmic_driver);
356}
357subsys_initcall(max8952_pmic_init);
358
359static void __exit max8952_pmic_exit(void)
360{
361 i2c_del_driver(&max8952_pmic_driver);
362}
363module_exit(max8952_pmic_exit);
364
365MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
366MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
367MODULE_LICENSE("GPL");