blob: 75d89400c1234db391556ec14e0ced14775580ac [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>
MyungJoo Ham202f4f52010-08-20 14:43:56 +090029#include <linux/gpio.h>
30#include <linux/io.h>
31#include <linux/slab.h>
32
33/* Registers */
34enum {
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,
44};
45
46struct max8952_data {
47 struct i2c_client *client;
48 struct device *dev;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090049 struct max8952_platform_data *pdata;
50 struct regulator_dev *rdev;
51
52 bool vid0;
53 bool vid1;
54 bool en;
55};
56
57static int max8952_read_reg(struct max8952_data *max8952, u8 reg)
58{
59 int ret = i2c_smbus_read_byte_data(max8952->client, reg);
60 if (ret > 0)
61 ret &= 0xff;
62
63 return ret;
64}
65
66static int max8952_write_reg(struct max8952_data *max8952,
67 u8 reg, u8 value)
68{
69 return i2c_smbus_write_byte_data(max8952->client, reg, value);
70}
71
72static int max8952_voltage(struct max8952_data *max8952, u8 mode)
73{
74 return (max8952->pdata->dvs_mode[mode] * 10 + 770) * 1000;
75}
76
77static int max8952_list_voltage(struct regulator_dev *rdev,
78 unsigned int selector)
79{
80 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
81
82 if (rdev_get_id(rdev) != 0)
83 return -EINVAL;
84
85 return max8952_voltage(max8952, selector);
86}
87
88static int max8952_is_enabled(struct regulator_dev *rdev)
89{
90 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
91 return max8952->en;
92}
93
94static int max8952_enable(struct regulator_dev *rdev)
95{
96 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
97
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);
101
102 max8952->en = true;
103 return 0;
104}
105
106static int max8952_disable(struct regulator_dev *rdev)
107{
108 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
109
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;
115
116 max8952->en = false;
117 return 0;
118}
119
120static int max8952_get_voltage(struct regulator_dev *rdev)
121{
122 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
123 u8 vid = 0;
124
125 if (max8952->vid0)
126 vid += 1;
127 if (max8952->vid1)
128 vid += 2;
129
130 return max8952_voltage(max8952, vid);
131}
132
133static int max8952_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000134 int min_uV, int max_uV, unsigned *selector)
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900135{
136 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
Axel Linec10b0e2010-10-26 07:55:54 +0800137 s8 vid = -1, i;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900138
139 if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
Jonghwan Choie2cf3132011-06-15 17:54:12 +0900140 !gpio_is_valid(max8952->pdata->gpio_vid1)) {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900141 /* DVS not supported */
142 return -EPERM;
143 }
144
145 for (i = 0; i < MAX8952_NUM_DVS_MODE; i++) {
146 int volt = max8952_voltage(max8952, i);
147
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;
152 }
153
154 if (vid >= 0 && vid < MAX8952_NUM_DVS_MODE) {
155 max8952->vid0 = (vid % 2 == 1);
156 max8952->vid1 = (((vid >> 1) % 2) == 1);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000157 *selector = vid;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900158 gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
159 gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
160 } else
161 return -EINVAL;
162
163 return 0;
164}
165
166static 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,
174};
175
176static 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,
183};
184
185static int __devinit max8952_pmic_probe(struct i2c_client *client,
186 const struct i2c_device_id *i2c_id)
187{
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;
191
192 int ret = 0, err = 0;
193
194 if (!pdata) {
195 dev_err(&client->dev, "Require the platform data\n");
196 return -EINVAL;
197 }
198
199 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
200 return -EIO;
201
202 max8952 = kzalloc(sizeof(struct max8952_data), GFP_KERNEL);
203 if (!max8952)
204 return -ENOMEM;
205
206 max8952->client = client;
207 max8952->dev = &client->dev;
208 max8952->pdata = pdata;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900209
210 max8952->rdev = regulator_register(&regulator, max8952->dev,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +0530211 &pdata->reg_data, max8952, NULL);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900212
Axel Linda057382010-10-25 10:11:07 +0800213 if (IS_ERR(max8952->rdev)) {
214 ret = PTR_ERR(max8952->rdev);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900215 dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
Axel Linda057382010-10-25 10:11:07 +0800216 goto err_reg;
217 }
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900218
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;
222
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;
230
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 */
236 }
237
238 err = 0;
239
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;
247
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;
255 }
256
257 } else
258 err = 3;
259
260 if (err) {
261 dev_warn(max8952->dev, "VID0/1 gpio invalid: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300262 "DVS not available.\n");
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900263 max8952->vid0 = 0;
264 max8952->vid1 = 0;
265 /* Mark invalid */
266 pdata->gpio_vid0 = -1;
267 pdata->gpio_vid1 = -1;
268
269 /* Disable Pulldown of EN only */
270 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
271
272 dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1"
273 " do not have proper controls.\n");
274 } else {
275 /*
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.
282 */
283 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
284 }
285
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));
302
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));
309
310 i2c_set_clientdata(client, max8952);
311
Axel Linda057382010-10-25 10:11:07 +0800312 return 0;
313
314err_reg:
315 kfree(max8952);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900316 return ret;
317}
318
319static int __devexit max8952_pmic_remove(struct i2c_client *client)
320{
321 struct max8952_data *max8952 = i2c_get_clientdata(client);
322 struct max8952_platform_data *pdata = max8952->pdata;
323 struct regulator_dev *rdev = max8952->rdev;
324
325 regulator_unregister(rdev);
326
327 gpio_free(pdata->gpio_vid0);
328 gpio_free(pdata->gpio_vid1);
329 gpio_free(pdata->gpio_en);
330
331 kfree(max8952);
332 return 0;
333}
334
335static const struct i2c_device_id max8952_ids[] = {
336 { "max8952", 0 },
337 { },
338};
339MODULE_DEVICE_TABLE(i2c, max8952_ids);
340
341static struct i2c_driver max8952_pmic_driver = {
342 .probe = max8952_pmic_probe,
343 .remove = __devexit_p(max8952_pmic_remove),
344 .driver = {
345 .name = "max8952",
346 },
347 .id_table = max8952_ids,
348};
349
350static int __init max8952_pmic_init(void)
351{
352 return i2c_add_driver(&max8952_pmic_driver);
353}
354subsys_initcall(max8952_pmic_init);
355
356static void __exit max8952_pmic_exit(void)
357{
358 i2c_del_driver(&max8952_pmic_driver);
359}
360module_exit(max8952_pmic_exit);
361
362MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
363MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
364MODULE_LICENSE("GPL");