regulator: max8660: Remove regulator_dev pointer from state container
[deliverable/linux.git] / drivers / regulator / max8660.c
CommitLineData
27f37e4b
WS
1/*
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>
5a0e3ad6 45#include <linux/slab.h>
27f37e4b 46#include <linux/regulator/max8660.h>
abe4c51a
DM
47#include <linux/of.h>
48#include <linux/of_device.h>
49#include <linux/regulator/of_regulator.h>
27f37e4b
WS
50
51#define MAX8660_DCDC_MIN_UV 725000
52#define MAX8660_DCDC_MAX_UV 1800000
53#define MAX8660_DCDC_STEP 25000
54#define MAX8660_DCDC_MAX_SEL 0x2b
55
56#define MAX8660_LDO5_MIN_UV 1700000
57#define MAX8660_LDO5_MAX_UV 2000000
58#define MAX8660_LDO5_STEP 25000
59#define MAX8660_LDO5_MAX_SEL 0x0c
60
61#define MAX8660_LDO67_MIN_UV 1800000
62#define MAX8660_LDO67_MAX_UV 3300000
63#define MAX8660_LDO67_STEP 100000
64#define MAX8660_LDO67_MAX_SEL 0x0f
65
66enum {
67 MAX8660_OVER1,
68 MAX8660_OVER2,
69 MAX8660_VCC1,
70 MAX8660_ADTV1,
71 MAX8660_ADTV2,
72 MAX8660_SDTV1,
73 MAX8660_SDTV2,
74 MAX8660_MDTV1,
75 MAX8660_MDTV2,
76 MAX8660_L12VCR,
77 MAX8660_FPWM,
78 MAX8660_N_REGS, /* not a real register */
79};
80
81struct max8660 {
82 struct i2c_client *client;
83 u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */
27f37e4b
WS
84};
85
86static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val)
87{
88 static const u8 max8660_addresses[MAX8660_N_REGS] =
89 { 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80 };
90
91 int ret;
92 u8 reg_val = (max8660->shadow_regs[reg] & mask) | val;
93 dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n",
94 max8660_addresses[reg], reg_val);
95
96 ret = i2c_smbus_write_byte_data(max8660->client,
97 max8660_addresses[reg], reg_val);
98 if (ret == 0)
99 max8660->shadow_regs[reg] = reg_val;
100
101 return ret;
102}
103
104
105/*
106 * DCDC functions
107 */
108
109static int max8660_dcdc_is_enabled(struct regulator_dev *rdev)
110{
111 struct max8660 *max8660 = rdev_get_drvdata(rdev);
112 u8 val = max8660->shadow_regs[MAX8660_OVER1];
113 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
114 return !!(val & mask);
115}
116
117static int max8660_dcdc_enable(struct regulator_dev *rdev)
118{
119 struct max8660 *max8660 = rdev_get_drvdata(rdev);
120 u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
121 return max8660_write(max8660, MAX8660_OVER1, 0xff, bit);
122}
123
124static int max8660_dcdc_disable(struct regulator_dev *rdev)
125{
126 struct max8660 *max8660 = rdev_get_drvdata(rdev);
127 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4;
128 return max8660_write(max8660, MAX8660_OVER1, mask, 0);
129}
130
9392c4f0 131static int max8660_dcdc_get_voltage_sel(struct regulator_dev *rdev)
27f37e4b
WS
132{
133 struct max8660 *max8660 = rdev_get_drvdata(rdev);
9392c4f0 134
27f37e4b
WS
135 u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
136 u8 selector = max8660->shadow_regs[reg];
9392c4f0 137 return selector;
27f37e4b
WS
138}
139
5eb3394f
AL
140static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev,
141 unsigned int selector)
27f37e4b
WS
142{
143 struct max8660 *max8660 = rdev_get_drvdata(rdev);
5eb3394f 144 u8 reg, bits;
27f37e4b
WS
145 int ret;
146
27f37e4b
WS
147 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
148 ret = max8660_write(max8660, reg, 0, selector);
149 if (ret)
150 return ret;
151
152 /* Select target voltage register and activate regulation */
153 bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30;
154 return max8660_write(max8660, MAX8660_VCC1, 0xff, bits);
155}
156
157static struct regulator_ops max8660_dcdc_ops = {
158 .is_enabled = max8660_dcdc_is_enabled,
134d34a8 159 .list_voltage = regulator_list_voltage_linear,
5eb3394f
AL
160 .map_voltage = regulator_map_voltage_linear,
161 .set_voltage_sel = max8660_dcdc_set_voltage_sel,
9392c4f0 162 .get_voltage_sel = max8660_dcdc_get_voltage_sel,
27f37e4b
WS
163};
164
165
166/*
167 * LDO5 functions
168 */
169
9392c4f0 170static int max8660_ldo5_get_voltage_sel(struct regulator_dev *rdev)
27f37e4b
WS
171{
172 struct max8660 *max8660 = rdev_get_drvdata(rdev);
27f37e4b 173
9392c4f0
AL
174 u8 selector = max8660->shadow_regs[MAX8660_MDTV2];
175 return selector;
27f37e4b
WS
176}
177
5eb3394f
AL
178static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev,
179 unsigned int selector)
27f37e4b
WS
180{
181 struct max8660 *max8660 = rdev_get_drvdata(rdev);
27f37e4b
WS
182 int ret;
183
27f37e4b
WS
184 ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
185 if (ret)
186 return ret;
187
188 /* Select target voltage register and activate regulation */
189 return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0);
190}
191
192static struct regulator_ops max8660_ldo5_ops = {
134d34a8 193 .list_voltage = regulator_list_voltage_linear,
5eb3394f
AL
194 .map_voltage = regulator_map_voltage_linear,
195 .set_voltage_sel = max8660_ldo5_set_voltage_sel,
9392c4f0 196 .get_voltage_sel = max8660_ldo5_get_voltage_sel,
27f37e4b
WS
197};
198
199
200/*
201 * LDO67 functions
202 */
203
204static int max8660_ldo67_is_enabled(struct regulator_dev *rdev)
205{
206 struct max8660 *max8660 = rdev_get_drvdata(rdev);
207 u8 val = max8660->shadow_regs[MAX8660_OVER2];
208 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
209 return !!(val & mask);
210}
211
212static int max8660_ldo67_enable(struct regulator_dev *rdev)
213{
214 struct max8660 *max8660 = rdev_get_drvdata(rdev);
215 u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
216 return max8660_write(max8660, MAX8660_OVER2, 0xff, bit);
217}
218
219static int max8660_ldo67_disable(struct regulator_dev *rdev)
220{
221 struct max8660 *max8660 = rdev_get_drvdata(rdev);
222 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4;
223 return max8660_write(max8660, MAX8660_OVER2, mask, 0);
224}
225
9392c4f0 226static int max8660_ldo67_get_voltage_sel(struct regulator_dev *rdev)
27f37e4b
WS
227{
228 struct max8660 *max8660 = rdev_get_drvdata(rdev);
9392c4f0 229
27f37e4b
WS
230 u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4;
231 u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf;
9392c4f0 232 return selector;
27f37e4b
WS
233}
234
5eb3394f
AL
235static int max8660_ldo67_set_voltage_sel(struct regulator_dev *rdev,
236 unsigned int selector)
27f37e4b
WS
237{
238 struct max8660 *max8660 = rdev_get_drvdata(rdev);
3a93f2a9 239
27f37e4b
WS
240 if (rdev_get_id(rdev) == MAX8660_V6)
241 return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector);
242 else
5eb3394f
AL
243 return max8660_write(max8660, MAX8660_L12VCR, 0x0f,
244 selector << 4);
27f37e4b
WS
245}
246
247static struct regulator_ops max8660_ldo67_ops = {
248 .is_enabled = max8660_ldo67_is_enabled,
249 .enable = max8660_ldo67_enable,
250 .disable = max8660_ldo67_disable,
134d34a8 251 .list_voltage = regulator_list_voltage_linear,
5eb3394f 252 .map_voltage = regulator_map_voltage_linear,
9392c4f0 253 .get_voltage_sel = max8660_ldo67_get_voltage_sel,
5eb3394f 254 .set_voltage_sel = max8660_ldo67_set_voltage_sel,
27f37e4b
WS
255};
256
621adb30 257static const struct regulator_desc max8660_reg[] = {
27f37e4b
WS
258 {
259 .name = "V3(DCDC)",
260 .id = MAX8660_V3,
261 .ops = &max8660_dcdc_ops,
262 .type = REGULATOR_VOLTAGE,
263 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
264 .owner = THIS_MODULE,
134d34a8
AL
265 .min_uV = MAX8660_DCDC_MIN_UV,
266 .uV_step = MAX8660_DCDC_STEP,
27f37e4b
WS
267 },
268 {
269 .name = "V4(DCDC)",
270 .id = MAX8660_V4,
271 .ops = &max8660_dcdc_ops,
272 .type = REGULATOR_VOLTAGE,
273 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
274 .owner = THIS_MODULE,
134d34a8
AL
275 .min_uV = MAX8660_DCDC_MIN_UV,
276 .uV_step = MAX8660_DCDC_STEP,
27f37e4b
WS
277 },
278 {
279 .name = "V5(LDO)",
280 .id = MAX8660_V5,
281 .ops = &max8660_ldo5_ops,
282 .type = REGULATOR_VOLTAGE,
283 .n_voltages = MAX8660_LDO5_MAX_SEL + 1,
284 .owner = THIS_MODULE,
134d34a8
AL
285 .min_uV = MAX8660_LDO5_MIN_UV,
286 .uV_step = MAX8660_LDO5_STEP,
27f37e4b
WS
287 },
288 {
289 .name = "V6(LDO)",
290 .id = MAX8660_V6,
291 .ops = &max8660_ldo67_ops,
292 .type = REGULATOR_VOLTAGE,
293 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
294 .owner = THIS_MODULE,
134d34a8
AL
295 .min_uV = MAX8660_LDO67_MIN_UV,
296 .uV_step = MAX8660_LDO67_STEP,
27f37e4b
WS
297 },
298 {
299 .name = "V7(LDO)",
300 .id = MAX8660_V7,
301 .ops = &max8660_ldo67_ops,
302 .type = REGULATOR_VOLTAGE,
303 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
304 .owner = THIS_MODULE,
134d34a8
AL
305 .min_uV = MAX8660_LDO67_MIN_UV,
306 .uV_step = MAX8660_LDO67_STEP,
27f37e4b
WS
307 },
308};
309
4a678f03
DM
310enum {
311 MAX8660 = 0,
312 MAX8661 = 1,
313};
314
abe4c51a
DM
315#ifdef CONFIG_OF
316static const struct of_device_id max8660_dt_ids[] = {
317 { .compatible = "maxim,max8660", .data = (void *) MAX8660 },
318 { .compatible = "maxim,max8661", .data = (void *) MAX8661 },
319 { }
320};
321MODULE_DEVICE_TABLE(of, max8660_dt_ids);
322
323static int max8660_pdata_from_dt(struct device *dev,
324 struct device_node **of_node,
325 struct max8660_platform_data *pdata)
326{
327 int matched, i;
328 struct device_node *np;
329 struct max8660_subdev_data *sub;
330 struct of_regulator_match rmatch[ARRAY_SIZE(max8660_reg)];
331
826adb49 332 np = of_get_child_by_name(dev->of_node, "regulators");
abe4c51a
DM
333 if (!np) {
334 dev_err(dev, "missing 'regulators' subnode in DT\n");
335 return -EINVAL;
336 }
337
338 for (i = 0; i < ARRAY_SIZE(rmatch); i++)
339 rmatch[i].name = max8660_reg[i].name;
340
341 matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
cde248f9 342 of_node_put(np);
abe4c51a
DM
343 if (matched <= 0)
344 return matched;
345
346 pdata->subdevs = devm_kzalloc(dev, sizeof(struct max8660_subdev_data) *
347 matched, GFP_KERNEL);
348 if (!pdata->subdevs)
349 return -ENOMEM;
350
351 pdata->num_subdevs = matched;
352 sub = pdata->subdevs;
353
354 for (i = 0; i < matched; i++) {
355 sub->id = i;
356 sub->name = rmatch[i].name;
357 sub->platform_data = rmatch[i].init_data;
358 of_node[i] = rmatch[i].of_node;
359 sub++;
360 }
361
362 return 0;
363}
364#else
365static inline int max8660_pdata_from_dt(struct device *dev,
366 struct device_node **of_node,
d6c7e113 367 struct max8660_platform_data *pdata)
abe4c51a
DM
368{
369 return 0;
370}
371#endif
372
a5023574 373static int max8660_probe(struct i2c_client *client,
308f100f 374 const struct i2c_device_id *i2c_id)
27f37e4b 375{
de492e8d
DM
376 struct device *dev = &client->dev;
377 struct max8660_platform_data *pdata = dev_get_platdata(dev);
c172708d 378 struct regulator_config config = { };
27f37e4b
WS
379 struct max8660 *max8660;
380 int boot_on, i, id, ret = -EINVAL;
abe4c51a 381 struct device_node *of_node[MAX8660_V_END];
d6c7e113 382 unsigned long type;
27f37e4b 383
abe4c51a
DM
384 if (dev->of_node && !pdata) {
385 const struct of_device_id *id;
386 struct max8660_platform_data pdata_of;
387
388 id = of_match_device(of_match_ptr(max8660_dt_ids), dev);
389 if (!id)
390 return -ENODEV;
391
392 ret = max8660_pdata_from_dt(dev, of_node, &pdata_of);
393 if (ret < 0)
394 return ret;
395
396 pdata = &pdata_of;
d6c7e113 397 type = (unsigned long) id->data;
abe4c51a
DM
398 } else {
399 type = i2c_id->driver_data;
400 memset(of_node, 0, sizeof(of_node));
401 }
402
27f37e4b 403 if (pdata->num_subdevs > MAX8660_V_END) {
de492e8d 404 dev_err(dev, "Too many regulators found!\n");
4d26f7d5 405 return -EINVAL;
27f37e4b
WS
406 }
407
67f76a93 408 max8660 = devm_kzalloc(dev, sizeof(struct max8660), GFP_KERNEL);
4d26f7d5
AL
409 if (!max8660)
410 return -ENOMEM;
27f37e4b
WS
411
412 max8660->client = client;
27f37e4b
WS
413
414 if (pdata->en34_is_high) {
415 /* Simulate always on */
416 max8660->shadow_regs[MAX8660_OVER1] = 5;
417 } else {
418 /* Otherwise devices can be toggled via software */
419 max8660_dcdc_ops.enable = max8660_dcdc_enable;
420 max8660_dcdc_ops.disable = max8660_dcdc_disable;
421 }
422
423 /*
424 * First, set up shadow registers to prevent glitches. As some
425 * registers are shared between regulators, everything must be properly
426 * set up for all regulators in advance.
427 */
428 max8660->shadow_regs[MAX8660_ADTV1] =
429 max8660->shadow_regs[MAX8660_ADTV2] =
430 max8660->shadow_regs[MAX8660_SDTV1] =
431 max8660->shadow_regs[MAX8660_SDTV2] = 0x1b;
432 max8660->shadow_regs[MAX8660_MDTV1] =
433 max8660->shadow_regs[MAX8660_MDTV2] = 0x04;
434
435 for (i = 0; i < pdata->num_subdevs; i++) {
436
437 if (!pdata->subdevs[i].platform_data)
dcfb6b56 438 return ret;
27f37e4b
WS
439
440 boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
441
442 switch (pdata->subdevs[i].id) {
443 case MAX8660_V3:
444 if (boot_on)
445 max8660->shadow_regs[MAX8660_OVER1] |= 1;
446 break;
447
448 case MAX8660_V4:
449 if (boot_on)
450 max8660->shadow_regs[MAX8660_OVER1] |= 4;
451 break;
452
453 case MAX8660_V5:
454 break;
455
456 case MAX8660_V6:
457 if (boot_on)
458 max8660->shadow_regs[MAX8660_OVER2] |= 2;
459 break;
460
461 case MAX8660_V7:
4a678f03 462 if (type == MAX8661) {
de492e8d 463 dev_err(dev, "Regulator not on this chip!\n");
dcfb6b56 464 return -EINVAL;
27f37e4b
WS
465 }
466
467 if (boot_on)
468 max8660->shadow_regs[MAX8660_OVER2] |= 4;
469 break;
470
471 default:
de492e8d 472 dev_err(dev, "invalid regulator %s\n",
27f37e4b 473 pdata->subdevs[i].name);
dcfb6b56 474 return ret;
27f37e4b
WS
475 }
476 }
477
478 /* Finally register devices */
479 for (i = 0; i < pdata->num_subdevs; i++) {
67f76a93 480 struct regulator_dev *rdev;
27f37e4b
WS
481
482 id = pdata->subdevs[i].id;
483
de492e8d 484 config.dev = dev;
c172708d 485 config.init_data = pdata->subdevs[i].platform_data;
abe4c51a 486 config.of_node = of_node[i];
c172708d
MB
487 config.driver_data = max8660;
488
67f76a93 489 rdev = devm_regulator_register(&client->dev,
dcfb6b56 490 &max8660_reg[id], &config);
67f76a93
KK
491 if (IS_ERR(rdev)) {
492 ret = PTR_ERR(rdev);
dcfb6b56 493 dev_err(&client->dev, "failed to register %s\n",
27f37e4b 494 max8660_reg[id].name);
67f76a93 495 return PTR_ERR(rdev);
27f37e4b
WS
496 }
497 }
498
53a4befa 499 i2c_set_clientdata(client, max8660);
27f37e4b 500 return 0;
27f37e4b
WS
501}
502
503static const struct i2c_device_id max8660_id[] = {
4a678f03
DM
504 { .name = "max8660", .driver_data = MAX8660 },
505 { .name = "max8661", .driver_data = MAX8661 },
27f37e4b
WS
506 { }
507};
508MODULE_DEVICE_TABLE(i2c, max8660_id);
509
510static struct i2c_driver max8660_driver = {
511 .probe = max8660_probe,
27f37e4b
WS
512 .driver = {
513 .name = "max8660",
308f100f 514 .owner = THIS_MODULE,
27f37e4b
WS
515 },
516 .id_table = max8660_id,
517};
518
519static int __init max8660_init(void)
520{
521 return i2c_add_driver(&max8660_driver);
522}
523subsys_initcall(max8660_init);
524
525static void __exit max8660_exit(void)
526{
527 i2c_del_driver(&max8660_driver);
528}
529module_exit(max8660_exit);
530
531/* Module information */
532MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
533MODULE_AUTHOR("Wolfram Sang");
534MODULE_LICENSE("GPL v2");
This page took 0.273346 seconds and 5 git commands to generate.