blob: 910c9b26d499f45f50034b417790921649fc4a6f [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
MyungJoo Ham202f4f52010-08-20 14:43:56 +090072static int max8952_list_voltage(struct regulator_dev *rdev,
73 unsigned int selector)
74{
75 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
76
77 if (rdev_get_id(rdev) != 0)
78 return -EINVAL;
79
Axel Linb9b49af2012-04-20 18:36:14 +080080 return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000;
MyungJoo Ham202f4f52010-08-20 14:43:56 +090081}
82
83static int max8952_is_enabled(struct regulator_dev *rdev)
84{
85 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
86 return max8952->en;
87}
88
89static int max8952_enable(struct regulator_dev *rdev)
90{
91 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
92
93 /* If not valid, assume "ALWAYS_HIGH" */
94 if (gpio_is_valid(max8952->pdata->gpio_en))
95 gpio_set_value(max8952->pdata->gpio_en, 1);
96
97 max8952->en = true;
98 return 0;
99}
100
101static int max8952_disable(struct regulator_dev *rdev)
102{
103 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
104
105 /* If not valid, assume "ALWAYS_HIGH" -> not permitted */
106 if (gpio_is_valid(max8952->pdata->gpio_en))
107 gpio_set_value(max8952->pdata->gpio_en, 0);
108 else
109 return -EPERM;
110
111 max8952->en = false;
112 return 0;
113}
114
Axel Linb9b49af2012-04-20 18:36:14 +0800115static int max8952_get_voltage_sel(struct regulator_dev *rdev)
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900116{
117 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
118 u8 vid = 0;
119
120 if (max8952->vid0)
121 vid += 1;
122 if (max8952->vid1)
123 vid += 2;
124
Axel Linb9b49af2012-04-20 18:36:14 +0800125 return vid;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900126}
127
Axel Lin6ea67d02012-03-26 09:20:58 +0800128static int max8952_set_voltage_sel(struct regulator_dev *rdev,
129 unsigned selector)
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900130{
131 struct max8952_data *max8952 = rdev_get_drvdata(rdev);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900132
133 if (!gpio_is_valid(max8952->pdata->gpio_vid0) ||
Jonghwan Choie2cf3132011-06-15 17:54:12 +0900134 !gpio_is_valid(max8952->pdata->gpio_vid1)) {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900135 /* DVS not supported */
136 return -EPERM;
137 }
138
Axel Lin6ea67d02012-03-26 09:20:58 +0800139 max8952->vid0 = selector & 0x1;
140 max8952->vid1 = (selector >> 1) & 0x1;
141 gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0);
142 gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900143
144 return 0;
145}
146
147static struct regulator_ops max8952_ops = {
148 .list_voltage = max8952_list_voltage,
149 .is_enabled = max8952_is_enabled,
150 .enable = max8952_enable,
151 .disable = max8952_disable,
Axel Linb9b49af2012-04-20 18:36:14 +0800152 .get_voltage_sel = max8952_get_voltage_sel,
Axel Lin6ea67d02012-03-26 09:20:58 +0800153 .set_voltage_sel = max8952_set_voltage_sel,
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900154 .set_suspend_disable = max8952_disable,
155};
156
Axel Linbd6ff0d2012-04-06 08:29:49 +0800157static const struct regulator_desc regulator = {
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900158 .name = "MAX8952_VOUT",
159 .id = 0,
160 .n_voltages = MAX8952_NUM_DVS_MODE,
161 .ops = &max8952_ops,
162 .type = REGULATOR_VOLTAGE,
163 .owner = THIS_MODULE,
164};
165
166static int __devinit max8952_pmic_probe(struct i2c_client *client,
167 const struct i2c_device_id *i2c_id)
168{
169 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
170 struct max8952_platform_data *pdata = client->dev.platform_data;
Mark Brownc1727082012-04-04 00:50:22 +0100171 struct regulator_config config = { };
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900172 struct max8952_data *max8952;
173
174 int ret = 0, err = 0;
175
176 if (!pdata) {
177 dev_err(&client->dev, "Require the platform data\n");
178 return -EINVAL;
179 }
180
181 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
182 return -EIO;
183
Axel Lin372de4a2012-05-16 12:50:47 +0800184 max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data),
185 GFP_KERNEL);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900186 if (!max8952)
187 return -ENOMEM;
188
189 max8952->client = client;
190 max8952->dev = &client->dev;
191 max8952->pdata = pdata;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900192
Mark Brownc1727082012-04-04 00:50:22 +0100193 config.dev = max8952->dev;
194 config.init_data = &pdata->reg_data;
195 config.driver_data = max8952;
196
197 max8952->rdev = regulator_register(&regulator, &config);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900198
Axel Linda057382010-10-25 10:11:07 +0800199 if (IS_ERR(max8952->rdev)) {
200 ret = PTR_ERR(max8952->rdev);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900201 dev_err(max8952->dev, "regulator init failed (%d)\n", ret);
Axel Lin372de4a2012-05-16 12:50:47 +0800202 return ret;
Axel Linda057382010-10-25 10:11:07 +0800203 }
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900204
205 max8952->en = !!(pdata->reg_data.constraints.boot_on);
Axel Linc8237f02012-03-15 08:47:48 +0800206 max8952->vid0 = pdata->default_mode & 0x1;
207 max8952->vid1 = (pdata->default_mode >> 1) & 0x1;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900208
209 if (gpio_is_valid(pdata->gpio_en)) {
210 if (!gpio_request(pdata->gpio_en, "MAX8952 EN"))
211 gpio_direction_output(pdata->gpio_en, max8952->en);
212 else
213 err = 1;
214 } else
215 err = 2;
216
217 if (err) {
218 dev_info(max8952->dev, "EN gpio invalid: assume that EN"
219 "is always High\n");
220 max8952->en = 1;
221 pdata->gpio_en = -1; /* Mark invalid */
222 }
223
224 err = 0;
225
226 if (gpio_is_valid(pdata->gpio_vid0) &&
227 gpio_is_valid(pdata->gpio_vid1)) {
228 if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0"))
229 gpio_direction_output(pdata->gpio_vid0,
Axel Linc8237f02012-03-15 08:47:48 +0800230 (pdata->default_mode) & 0x1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900231 else
232 err = 1;
233
234 if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1"))
235 gpio_direction_output(pdata->gpio_vid1,
Axel Linc8237f02012-03-15 08:47:48 +0800236 (pdata->default_mode >> 1) & 0x1);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900237 else {
238 if (!err)
239 gpio_free(pdata->gpio_vid0);
240 err = 2;
241 }
242
243 } else
244 err = 3;
245
246 if (err) {
247 dev_warn(max8952->dev, "VID0/1 gpio invalid: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300248 "DVS not available.\n");
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900249 max8952->vid0 = 0;
250 max8952->vid1 = 0;
251 /* Mark invalid */
252 pdata->gpio_vid0 = -1;
253 pdata->gpio_vid1 = -1;
254
255 /* Disable Pulldown of EN only */
256 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60);
257
258 dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1"
259 " do not have proper controls.\n");
260 } else {
261 /*
262 * Disable Pulldown on EN, VID0, VID1 to reduce
263 * leakage current of MAX8952 assuming that MAX8952
264 * is turned on (EN==1). Note that without having VID0/1
265 * properly connected, turning pulldown off can be
266 * problematic. Thus, turn this off only when they are
267 * controllable by GPIO.
268 */
269 max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0);
270 }
271
272 max8952_write_reg(max8952, MAX8952_REG_MODE0,
273 (max8952_read_reg(max8952,
274 MAX8952_REG_MODE0) & 0xC0) |
275 (pdata->dvs_mode[0] & 0x3F));
276 max8952_write_reg(max8952, MAX8952_REG_MODE1,
277 (max8952_read_reg(max8952,
278 MAX8952_REG_MODE1) & 0xC0) |
279 (pdata->dvs_mode[1] & 0x3F));
280 max8952_write_reg(max8952, MAX8952_REG_MODE2,
281 (max8952_read_reg(max8952,
282 MAX8952_REG_MODE2) & 0xC0) |
283 (pdata->dvs_mode[2] & 0x3F));
284 max8952_write_reg(max8952, MAX8952_REG_MODE3,
285 (max8952_read_reg(max8952,
286 MAX8952_REG_MODE3) & 0xC0) |
287 (pdata->dvs_mode[3] & 0x3F));
288
289 max8952_write_reg(max8952, MAX8952_REG_SYNC,
290 (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) |
291 ((pdata->sync_freq & 0x3) << 6));
292 max8952_write_reg(max8952, MAX8952_REG_RAMP,
293 (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) |
294 ((pdata->ramp_speed & 0x7) << 5));
295
296 i2c_set_clientdata(client, max8952);
297
Axel Linda057382010-10-25 10:11:07 +0800298 return 0;
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900299}
300
301static int __devexit max8952_pmic_remove(struct i2c_client *client)
302{
303 struct max8952_data *max8952 = i2c_get_clientdata(client);
304 struct max8952_platform_data *pdata = max8952->pdata;
305 struct regulator_dev *rdev = max8952->rdev;
306
307 regulator_unregister(rdev);
308
309 gpio_free(pdata->gpio_vid0);
310 gpio_free(pdata->gpio_vid1);
311 gpio_free(pdata->gpio_en);
MyungJoo Ham202f4f52010-08-20 14:43:56 +0900312 return 0;
313}
314
315static const struct i2c_device_id max8952_ids[] = {
316 { "max8952", 0 },
317 { },
318};
319MODULE_DEVICE_TABLE(i2c, max8952_ids);
320
321static struct i2c_driver max8952_pmic_driver = {
322 .probe = max8952_pmic_probe,
323 .remove = __devexit_p(max8952_pmic_remove),
324 .driver = {
325 .name = "max8952",
326 },
327 .id_table = max8952_ids,
328};
329
330static int __init max8952_pmic_init(void)
331{
332 return i2c_add_driver(&max8952_pmic_driver);
333}
334subsys_initcall(max8952_pmic_init);
335
336static void __exit max8952_pmic_exit(void)
337{
338 i2c_del_driver(&max8952_pmic_driver);
339}
340module_exit(max8952_pmic_exit);
341
342MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver");
343MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
344MODULE_LICENSE("GPL");