blob: 4d7c635c36c2d6cc92b1ecd8fafb9d533213ae21 [file] [log] [blame]
Wolfram Sang27f37e42009-09-25 09:39:26 +02001/*
2 * max8660.c -- Voltage regulation for the Maxim 8660/8661
3 *
4 * based on max1586.c and wm8400-regulator.c
5 *
6 * Copyright (C) 2009 Wolfram Sang, Pengutronix e.K.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Some info:
22 *
23 * Datasheet: http://datasheets.maxim-ic.com/en/ds/MAX8660-MAX8661.pdf
24 *
25 * This chip is a bit nasty because it is a write-only device. Thus, the driver
26 * uses shadow registers to keep track of its values. The main problem appears
27 * to be the initialization: When Linux boots up, we cannot know if the chip is
28 * in the default state or not, so we would have to pass such information in
29 * platform_data. As this adds a bit of complexity to the driver, this is left
30 * out for now until it is really needed.
31 *
32 * [A|S|M]DTV1 registers are currently not used, but [A|S|M]DTV2.
33 *
34 * If the driver is feature complete, it might be worth to check if one set of
35 * functions for V3-V7 is sufficient. For maximum flexibility during
36 * development, they are separated for now.
37 *
38 */
39
40#include <linux/module.h>
41#include <linux/err.h>
42#include <linux/i2c.h>
43#include <linux/platform_device.h>
44#include <linux/regulator/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Wolfram Sang27f37e42009-09-25 09:39:26 +020046#include <linux/regulator/max8660.h>
47
48#define MAX8660_DCDC_MIN_UV 725000
49#define MAX8660_DCDC_MAX_UV 1800000
50#define MAX8660_DCDC_STEP 25000
51#define MAX8660_DCDC_MAX_SEL 0x2b
52
53#define MAX8660_LDO5_MIN_UV 1700000
54#define MAX8660_LDO5_MAX_UV 2000000
55#define MAX8660_LDO5_STEP 25000
56#define MAX8660_LDO5_MAX_SEL 0x0c
57
58#define MAX8660_LDO67_MIN_UV 1800000
59#define MAX8660_LDO67_MAX_UV 3300000
60#define MAX8660_LDO67_STEP 100000
61#define MAX8660_LDO67_MAX_SEL 0x0f
62
63enum {
64 MAX8660_OVER1,
65 MAX8660_OVER2,
66 MAX8660_VCC1,
67 MAX8660_ADTV1,
68 MAX8660_ADTV2,
69 MAX8660_SDTV1,
70 MAX8660_SDTV2,
71 MAX8660_MDTV1,
72 MAX8660_MDTV2,
73 MAX8660_L12VCR,
74 MAX8660_FPWM,
75 MAX8660_N_REGS, /* not a real register */
76};
77
78struct max8660 {
79 struct i2c_client *client;
80 u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */
81 struct regulator_dev *rdev[];
82};
83
84static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val)
85{
86 static const u8 max8660_addresses[MAX8660_N_REGS] =
87 { 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80 };
88
89 int ret;
90 u8 reg_val = (max8660->shadow_regs[reg] & mask) | val;
91 dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n",
92 max8660_addresses[reg], reg_val);
93
94 ret = i2c_smbus_write_byte_data(max8660->client,
95 max8660_addresses[reg], reg_val);
96 if (ret == 0)
97 max8660->shadow_regs[reg] = reg_val;
98
99 return ret;
100}
101
102
103/*
104 * DCDC functions
105 */
106
107static int max8660_dcdc_is_enabled(struct regulator_dev *rdev)
108{
109 struct max8660 *max8660 = rdev_get_drvdata(rdev);
110 u8 val = max8660->shadow_regs[MAX8660_OVER1];
111 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
112 return !!(val & mask);
113}
114
115static int max8660_dcdc_enable(struct regulator_dev *rdev)
116{
117 struct max8660 *max8660 = rdev_get_drvdata(rdev);
118 u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
119 return max8660_write(max8660, MAX8660_OVER1, 0xff, bit);
120}
121
122static int max8660_dcdc_disable(struct regulator_dev *rdev)
123{
124 struct max8660 *max8660 = rdev_get_drvdata(rdev);
125 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4;
126 return max8660_write(max8660, MAX8660_OVER1, mask, 0);
127}
128
Axel Lin9392c4f2012-05-15 21:42:12 +0800129static int max8660_dcdc_get_voltage_sel(struct regulator_dev *rdev)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200130{
131 struct max8660 *max8660 = rdev_get_drvdata(rdev);
Axel Lin9392c4f2012-05-15 21:42:12 +0800132
Wolfram Sang27f37e42009-09-25 09:39:26 +0200133 u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
134 u8 selector = max8660->shadow_regs[reg];
Axel Lin9392c4f2012-05-15 21:42:12 +0800135 return selector;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200136}
137
Axel Lin5eb33942012-05-15 21:43:29 +0800138static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev,
139 unsigned int selector)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200140{
141 struct max8660 *max8660 = rdev_get_drvdata(rdev);
Axel Lin5eb33942012-05-15 21:43:29 +0800142 u8 reg, bits;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200143 int ret;
144
Wolfram Sang27f37e42009-09-25 09:39:26 +0200145 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
146 ret = max8660_write(max8660, reg, 0, selector);
147 if (ret)
148 return ret;
149
150 /* Select target voltage register and activate regulation */
151 bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30;
152 return max8660_write(max8660, MAX8660_VCC1, 0xff, bits);
153}
154
155static struct regulator_ops max8660_dcdc_ops = {
156 .is_enabled = max8660_dcdc_is_enabled,
Axel Lin134d34a2012-05-15 21:41:18 +0800157 .list_voltage = regulator_list_voltage_linear,
Axel Lin5eb33942012-05-15 21:43:29 +0800158 .map_voltage = regulator_map_voltage_linear,
159 .set_voltage_sel = max8660_dcdc_set_voltage_sel,
Axel Lin9392c4f2012-05-15 21:42:12 +0800160 .get_voltage_sel = max8660_dcdc_get_voltage_sel,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200161};
162
163
164/*
165 * LDO5 functions
166 */
167
Axel Lin9392c4f2012-05-15 21:42:12 +0800168static int max8660_ldo5_get_voltage_sel(struct regulator_dev *rdev)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200169{
170 struct max8660 *max8660 = rdev_get_drvdata(rdev);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200171
Axel Lin9392c4f2012-05-15 21:42:12 +0800172 u8 selector = max8660->shadow_regs[MAX8660_MDTV2];
173 return selector;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200174}
175
Axel Lin5eb33942012-05-15 21:43:29 +0800176static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev,
177 unsigned int selector)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200178{
179 struct max8660 *max8660 = rdev_get_drvdata(rdev);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200180 int ret;
181
Wolfram Sang27f37e42009-09-25 09:39:26 +0200182 ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
183 if (ret)
184 return ret;
185
186 /* Select target voltage register and activate regulation */
187 return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0);
188}
189
190static struct regulator_ops max8660_ldo5_ops = {
Axel Lin134d34a2012-05-15 21:41:18 +0800191 .list_voltage = regulator_list_voltage_linear,
Axel Lin5eb33942012-05-15 21:43:29 +0800192 .map_voltage = regulator_map_voltage_linear,
193 .set_voltage_sel = max8660_ldo5_set_voltage_sel,
Axel Lin9392c4f2012-05-15 21:42:12 +0800194 .get_voltage_sel = max8660_ldo5_get_voltage_sel,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200195};
196
197
198/*
199 * LDO67 functions
200 */
201
202static int max8660_ldo67_is_enabled(struct regulator_dev *rdev)
203{
204 struct max8660 *max8660 = rdev_get_drvdata(rdev);
205 u8 val = max8660->shadow_regs[MAX8660_OVER2];
206 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
207 return !!(val & mask);
208}
209
210static int max8660_ldo67_enable(struct regulator_dev *rdev)
211{
212 struct max8660 *max8660 = rdev_get_drvdata(rdev);
213 u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
214 return max8660_write(max8660, MAX8660_OVER2, 0xff, bit);
215}
216
217static int max8660_ldo67_disable(struct regulator_dev *rdev)
218{
219 struct max8660 *max8660 = rdev_get_drvdata(rdev);
220 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4;
221 return max8660_write(max8660, MAX8660_OVER2, mask, 0);
222}
223
Axel Lin9392c4f2012-05-15 21:42:12 +0800224static int max8660_ldo67_get_voltage_sel(struct regulator_dev *rdev)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200225{
226 struct max8660 *max8660 = rdev_get_drvdata(rdev);
Axel Lin9392c4f2012-05-15 21:42:12 +0800227
Wolfram Sang27f37e42009-09-25 09:39:26 +0200228 u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4;
229 u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf;
Axel Lin9392c4f2012-05-15 21:42:12 +0800230 return selector;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200231}
232
Axel Lin5eb33942012-05-15 21:43:29 +0800233static int max8660_ldo67_set_voltage_sel(struct regulator_dev *rdev,
234 unsigned int selector)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200235{
236 struct max8660 *max8660 = rdev_get_drvdata(rdev);
Mark Brown3a93f2a2010-11-10 14:38:29 +0000237
Wolfram Sang27f37e42009-09-25 09:39:26 +0200238 if (rdev_get_id(rdev) == MAX8660_V6)
239 return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector);
240 else
Axel Lin5eb33942012-05-15 21:43:29 +0800241 return max8660_write(max8660, MAX8660_L12VCR, 0x0f,
242 selector << 4);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200243}
244
245static struct regulator_ops max8660_ldo67_ops = {
246 .is_enabled = max8660_ldo67_is_enabled,
247 .enable = max8660_ldo67_enable,
248 .disable = max8660_ldo67_disable,
Axel Lin134d34a2012-05-15 21:41:18 +0800249 .list_voltage = regulator_list_voltage_linear,
Axel Lin5eb33942012-05-15 21:43:29 +0800250 .map_voltage = regulator_map_voltage_linear,
Axel Lin9392c4f2012-05-15 21:42:12 +0800251 .get_voltage_sel = max8660_ldo67_get_voltage_sel,
Axel Lin5eb33942012-05-15 21:43:29 +0800252 .set_voltage_sel = max8660_ldo67_set_voltage_sel,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200253};
254
Axel Lin621adb32012-04-06 08:26:59 +0800255static const struct regulator_desc max8660_reg[] = {
Wolfram Sang27f37e42009-09-25 09:39:26 +0200256 {
257 .name = "V3(DCDC)",
258 .id = MAX8660_V3,
259 .ops = &max8660_dcdc_ops,
260 .type = REGULATOR_VOLTAGE,
261 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
262 .owner = THIS_MODULE,
Axel Lin134d34a2012-05-15 21:41:18 +0800263 .min_uV = MAX8660_DCDC_MIN_UV,
264 .uV_step = MAX8660_DCDC_STEP,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200265 },
266 {
267 .name = "V4(DCDC)",
268 .id = MAX8660_V4,
269 .ops = &max8660_dcdc_ops,
270 .type = REGULATOR_VOLTAGE,
271 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
272 .owner = THIS_MODULE,
Axel Lin134d34a2012-05-15 21:41:18 +0800273 .min_uV = MAX8660_DCDC_MIN_UV,
274 .uV_step = MAX8660_DCDC_STEP,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200275 },
276 {
277 .name = "V5(LDO)",
278 .id = MAX8660_V5,
279 .ops = &max8660_ldo5_ops,
280 .type = REGULATOR_VOLTAGE,
281 .n_voltages = MAX8660_LDO5_MAX_SEL + 1,
282 .owner = THIS_MODULE,
Axel Lin134d34a2012-05-15 21:41:18 +0800283 .min_uV = MAX8660_LDO5_MIN_UV,
284 .uV_step = MAX8660_LDO5_STEP,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200285 },
286 {
287 .name = "V6(LDO)",
288 .id = MAX8660_V6,
289 .ops = &max8660_ldo67_ops,
290 .type = REGULATOR_VOLTAGE,
291 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
292 .owner = THIS_MODULE,
Axel Lin134d34a2012-05-15 21:41:18 +0800293 .min_uV = MAX8660_LDO67_MIN_UV,
294 .uV_step = MAX8660_LDO67_STEP,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200295 },
296 {
297 .name = "V7(LDO)",
298 .id = MAX8660_V7,
299 .ops = &max8660_ldo67_ops,
300 .type = REGULATOR_VOLTAGE,
301 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
302 .owner = THIS_MODULE,
Axel Lin134d34a2012-05-15 21:41:18 +0800303 .min_uV = MAX8660_LDO67_MIN_UV,
304 .uV_step = MAX8660_LDO67_STEP,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200305 },
306};
307
Bill Pembertona5023572012-11-19 13:22:22 -0500308static int max8660_probe(struct i2c_client *client,
Dmitry Torokhov308f1002010-02-23 23:38:28 -0800309 const struct i2c_device_id *i2c_id)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200310{
311 struct regulator_dev **rdev;
312 struct max8660_platform_data *pdata = client->dev.platform_data;
Mark Brownc1727082012-04-04 00:50:22 +0100313 struct regulator_config config = { };
Wolfram Sang27f37e42009-09-25 09:39:26 +0200314 struct max8660 *max8660;
315 int boot_on, i, id, ret = -EINVAL;
316
317 if (pdata->num_subdevs > MAX8660_V_END) {
Dmitry Torokhov308f1002010-02-23 23:38:28 -0800318 dev_err(&client->dev, "Too many regulators found!\n");
Axel Lin4d26f7d2012-04-11 23:08:27 +0800319 return -EINVAL;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200320 }
321
Axel Lin575f6902012-04-17 18:06:50 +0800322 max8660 = devm_kzalloc(&client->dev, sizeof(struct max8660) +
Wolfram Sang27f37e42009-09-25 09:39:26 +0200323 sizeof(struct regulator_dev *) * MAX8660_V_END,
324 GFP_KERNEL);
Axel Lin4d26f7d2012-04-11 23:08:27 +0800325 if (!max8660)
326 return -ENOMEM;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200327
328 max8660->client = client;
329 rdev = max8660->rdev;
330
331 if (pdata->en34_is_high) {
332 /* Simulate always on */
333 max8660->shadow_regs[MAX8660_OVER1] = 5;
334 } else {
335 /* Otherwise devices can be toggled via software */
336 max8660_dcdc_ops.enable = max8660_dcdc_enable;
337 max8660_dcdc_ops.disable = max8660_dcdc_disable;
338 }
339
340 /*
341 * First, set up shadow registers to prevent glitches. As some
342 * registers are shared between regulators, everything must be properly
343 * set up for all regulators in advance.
344 */
345 max8660->shadow_regs[MAX8660_ADTV1] =
346 max8660->shadow_regs[MAX8660_ADTV2] =
347 max8660->shadow_regs[MAX8660_SDTV1] =
348 max8660->shadow_regs[MAX8660_SDTV2] = 0x1b;
349 max8660->shadow_regs[MAX8660_MDTV1] =
350 max8660->shadow_regs[MAX8660_MDTV2] = 0x04;
351
352 for (i = 0; i < pdata->num_subdevs; i++) {
353
354 if (!pdata->subdevs[i].platform_data)
Axel Lin4d26f7d2012-04-11 23:08:27 +0800355 goto err_out;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200356
357 boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
358
359 switch (pdata->subdevs[i].id) {
360 case MAX8660_V3:
361 if (boot_on)
362 max8660->shadow_regs[MAX8660_OVER1] |= 1;
363 break;
364
365 case MAX8660_V4:
366 if (boot_on)
367 max8660->shadow_regs[MAX8660_OVER1] |= 4;
368 break;
369
370 case MAX8660_V5:
371 break;
372
373 case MAX8660_V6:
374 if (boot_on)
375 max8660->shadow_regs[MAX8660_OVER2] |= 2;
376 break;
377
378 case MAX8660_V7:
379 if (!strcmp(i2c_id->name, "max8661")) {
380 dev_err(&client->dev, "Regulator not on this chip!\n");
Axel Lin4d26f7d2012-04-11 23:08:27 +0800381 goto err_out;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200382 }
383
384 if (boot_on)
385 max8660->shadow_regs[MAX8660_OVER2] |= 4;
386 break;
387
388 default:
389 dev_err(&client->dev, "invalid regulator %s\n",
390 pdata->subdevs[i].name);
Axel Lin4d26f7d2012-04-11 23:08:27 +0800391 goto err_out;
Wolfram Sang27f37e42009-09-25 09:39:26 +0200392 }
393 }
394
395 /* Finally register devices */
396 for (i = 0; i < pdata->num_subdevs; i++) {
397
398 id = pdata->subdevs[i].id;
399
Mark Brownc1727082012-04-04 00:50:22 +0100400 config.dev = &client->dev;
401 config.init_data = pdata->subdevs[i].platform_data;
402 config.driver_data = max8660;
403
404 rdev[i] = regulator_register(&max8660_reg[id], &config);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200405 if (IS_ERR(rdev[i])) {
406 ret = PTR_ERR(rdev[i]);
407 dev_err(&client->dev, "failed to register %s\n",
408 max8660_reg[id].name);
409 goto err_unregister;
410 }
411 }
412
Axel Lin53a4bef2010-08-06 13:35:27 +0800413 i2c_set_clientdata(client, max8660);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200414 return 0;
415
416err_unregister:
417 while (--i >= 0)
418 regulator_unregister(rdev[i]);
Axel Lin4d26f7d2012-04-11 23:08:27 +0800419err_out:
Wolfram Sang27f37e42009-09-25 09:39:26 +0200420 return ret;
421}
422
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500423static int max8660_remove(struct i2c_client *client)
Wolfram Sang27f37e42009-09-25 09:39:26 +0200424{
Axel Lin53a4bef2010-08-06 13:35:27 +0800425 struct max8660 *max8660 = i2c_get_clientdata(client);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200426 int i;
427
428 for (i = 0; i < MAX8660_V_END; i++)
Axel Lin53a4bef2010-08-06 13:35:27 +0800429 if (max8660->rdev[i])
430 regulator_unregister(max8660->rdev[i]);
Wolfram Sang27f37e42009-09-25 09:39:26 +0200431 return 0;
432}
433
434static const struct i2c_device_id max8660_id[] = {
435 { "max8660", 0 },
436 { "max8661", 0 },
437 { }
438};
439MODULE_DEVICE_TABLE(i2c, max8660_id);
440
441static struct i2c_driver max8660_driver = {
442 .probe = max8660_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500443 .remove = max8660_remove,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200444 .driver = {
445 .name = "max8660",
Dmitry Torokhov308f1002010-02-23 23:38:28 -0800446 .owner = THIS_MODULE,
Wolfram Sang27f37e42009-09-25 09:39:26 +0200447 },
448 .id_table = max8660_id,
449};
450
451static int __init max8660_init(void)
452{
453 return i2c_add_driver(&max8660_driver);
454}
455subsys_initcall(max8660_init);
456
457static void __exit max8660_exit(void)
458{
459 i2c_del_driver(&max8660_driver);
460}
461module_exit(max8660_exit);
462
463/* Module information */
464MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
465MODULE_AUTHOR("Wolfram Sang");
466MODULE_LICENSE("GPL v2");