]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/regulator/lp3971.c
rbd: void data pointers for rbd_obj_method_sync()
[mirror_ubuntu-artful-kernel.git] / drivers / regulator / lp3971.c
CommitLineData
0cbdf7bc
MS
1/*
2 * Regulator driver for National Semiconductors LP3971 PMIC chip
3 *
4 * Copyright (C) 2009 Samsung Electronics
5 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
6 *
7 * Based on wm8350.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/bug.h>
16#include <linux/err.h>
17#include <linux/i2c.h>
18#include <linux/kernel.h>
65602c32 19#include <linux/module.h>
0cbdf7bc
MS
20#include <linux/regulator/driver.h>
21#include <linux/regulator/lp3971.h>
5a0e3ad6 22#include <linux/slab.h>
0cbdf7bc
MS
23
24struct lp3971 {
25 struct device *dev;
26 struct mutex io_lock;
27 struct i2c_client *i2c;
28 int num_regulators;
29 struct regulator_dev **rdev;
30};
31
32static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg);
33static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val);
34
35#define LP3971_SYS_CONTROL1_REG 0x07
36
37/* System control register 1 initial value,
38 bits 4 and 5 are EPROM programmable */
39#define SYS_CONTROL1_INIT_VAL 0x40
40#define SYS_CONTROL1_INIT_MASK 0xCF
41
42#define LP3971_BUCK_VOL_ENABLE_REG 0x10
43#define LP3971_BUCK_VOL_CHANGE_REG 0x20
44
45/* Voltage control registers shift:
46 LP3971_BUCK1 -> 0
47 LP3971_BUCK2 -> 4
48 LP3971_BUCK3 -> 6
49*/
451a73cd 50#define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
0cbdf7bc
MS
51#define BUCK_VOL_CHANGE_FLAG_GO 0x01
52#define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
53#define BUCK_VOL_CHANGE_FLAG_MASK 0x03
54
55#define LP3971_BUCK1_BASE 0x23
56#define LP3971_BUCK2_BASE 0x29
57#define LP3971_BUCK3_BASE 0x32
58
6faa7e0a 59static const int buck_base_addr[] = {
0cbdf7bc
MS
60 LP3971_BUCK1_BASE,
61 LP3971_BUCK2_BASE,
62 LP3971_BUCK3_BASE,
63};
64
65#define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
66#define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
67
cad8d76e
AL
68static const unsigned int buck_voltage_map[] = {
69 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
70 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
71 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
72 3000000, 3300000,
0cbdf7bc
MS
73};
74
75#define BUCK_TARGET_VOL_MASK 0x3f
0cbdf7bc
MS
76
77#define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
78
79#define LP3971_LDO_ENABLE_REG 0x12
80#define LP3971_LDO_VOL_CONTR_BASE 0x39
81
82/* Voltage control registers:
83 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
84 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
85 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
86 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
87 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
88*/
89#define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
90
91/* Voltage control registers shift:
92 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
93 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
94 LP3971_LDO5 -> 0
95*/
96#define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
97#define LDO_VOL_CONTR_MASK 0x0f
98
cad8d76e
AL
99static const unsigned int ldo45_voltage_map[] = {
100 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
101 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
0cbdf7bc
MS
102};
103
cad8d76e
AL
104static const unsigned int ldo123_voltage_map[] = {
105 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
106 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
0cbdf7bc
MS
107};
108
0cbdf7bc
MS
109#define LDO_VOL_MIN_IDX 0x00
110#define LDO_VOL_MAX_IDX 0x0f
111
0cbdf7bc
MS
112static int lp3971_ldo_is_enabled(struct regulator_dev *dev)
113{
114 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
115 int ldo = rdev_get_id(dev) - LP3971_LDO1;
116 u16 mask = 1 << (1 + ldo);
117 u16 val;
118
119 val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
120 return (val & mask) != 0;
121}
122
123static int lp3971_ldo_enable(struct regulator_dev *dev)
124{
125 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
126 int ldo = rdev_get_id(dev) - LP3971_LDO1;
127 u16 mask = 1 << (1 + ldo);
128
129 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
130}
131
132static int lp3971_ldo_disable(struct regulator_dev *dev)
133{
134 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
135 int ldo = rdev_get_id(dev) - LP3971_LDO1;
136 u16 mask = 1 << (1 + ldo);
137
138 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
139}
140
f38482fa 141static int lp3971_ldo_get_voltage_sel(struct regulator_dev *dev)
0cbdf7bc
MS
142{
143 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
144 int ldo = rdev_get_id(dev) - LP3971_LDO1;
145 u16 val, reg;
146
147 reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
148 val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
149
f38482fa 150 return val;
0cbdf7bc
MS
151}
152
dd8e2314
AL
153static int lp3971_ldo_set_voltage_sel(struct regulator_dev *dev,
154 unsigned int selector)
0cbdf7bc
MS
155{
156 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
157 int ldo = rdev_get_id(dev) - LP3971_LDO1;
3a93f2a9 158
0cbdf7bc 159 return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo),
cdb868f5 160 LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo),
dd8e2314 161 selector << LDO_VOL_CONTR_SHIFT(ldo));
0cbdf7bc
MS
162}
163
164static struct regulator_ops lp3971_ldo_ops = {
cad8d76e 165 .list_voltage = regulator_list_voltage_table,
0cbdf7bc
MS
166 .is_enabled = lp3971_ldo_is_enabled,
167 .enable = lp3971_ldo_enable,
168 .disable = lp3971_ldo_disable,
f38482fa 169 .get_voltage_sel = lp3971_ldo_get_voltage_sel,
dd8e2314 170 .set_voltage_sel = lp3971_ldo_set_voltage_sel,
0cbdf7bc
MS
171};
172
0cbdf7bc
MS
173static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
174{
175 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
176 int buck = rdev_get_id(dev) - LP3971_DCDC1;
177 u16 mask = 1 << (buck * 2);
178 u16 val;
179
180 val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
181 return (val & mask) != 0;
182}
183
184static int lp3971_dcdc_enable(struct regulator_dev *dev)
185{
186 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
187 int buck = rdev_get_id(dev) - LP3971_DCDC1;
188 u16 mask = 1 << (buck * 2);
189
190 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
191}
192
193static int lp3971_dcdc_disable(struct regulator_dev *dev)
194{
195 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
196 int buck = rdev_get_id(dev) - LP3971_DCDC1;
197 u16 mask = 1 << (buck * 2);
198
199 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
200}
201
f38482fa 202static int lp3971_dcdc_get_voltage_sel(struct regulator_dev *dev)
0cbdf7bc
MS
203{
204 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
205 int buck = rdev_get_id(dev) - LP3971_DCDC1;
206 u16 reg;
0cbdf7bc
MS
207
208 reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
209 reg &= BUCK_TARGET_VOL_MASK;
210
f38482fa 211 return reg;
0cbdf7bc
MS
212}
213
dd8e2314
AL
214static int lp3971_dcdc_set_voltage_sel(struct regulator_dev *dev,
215 unsigned int selector)
0cbdf7bc
MS
216{
217 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
218 int buck = rdev_get_id(dev) - LP3971_DCDC1;
0cbdf7bc
MS
219 int ret;
220
0cbdf7bc 221 ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
dd8e2314 222 BUCK_TARGET_VOL_MASK, selector);
0cbdf7bc
MS
223 if (ret)
224 return ret;
225
226 ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
227 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
228 BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck));
229 if (ret)
230 return ret;
231
232 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
233 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
234 0 << BUCK_VOL_CHANGE_SHIFT(buck));
235}
236
237static struct regulator_ops lp3971_dcdc_ops = {
cad8d76e 238 .list_voltage = regulator_list_voltage_table,
0cbdf7bc
MS
239 .is_enabled = lp3971_dcdc_is_enabled,
240 .enable = lp3971_dcdc_enable,
241 .disable = lp3971_dcdc_disable,
f38482fa 242 .get_voltage_sel = lp3971_dcdc_get_voltage_sel,
dd8e2314 243 .set_voltage_sel = lp3971_dcdc_set_voltage_sel,
0cbdf7bc
MS
244};
245
14add4ff 246static const struct regulator_desc regulators[] = {
0cbdf7bc
MS
247 {
248 .name = "LDO1",
249 .id = LP3971_LDO1,
250 .ops = &lp3971_ldo_ops,
251 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
cad8d76e 252 .volt_table = ldo123_voltage_map,
0cbdf7bc
MS
253 .type = REGULATOR_VOLTAGE,
254 .owner = THIS_MODULE,
255 },
256 {
257 .name = "LDO2",
258 .id = LP3971_LDO2,
259 .ops = &lp3971_ldo_ops,
260 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
cad8d76e 261 .volt_table = ldo123_voltage_map,
0cbdf7bc
MS
262 .type = REGULATOR_VOLTAGE,
263 .owner = THIS_MODULE,
264 },
265 {
266 .name = "LDO3",
267 .id = LP3971_LDO3,
268 .ops = &lp3971_ldo_ops,
269 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
cad8d76e 270 .volt_table = ldo123_voltage_map,
0cbdf7bc
MS
271 .type = REGULATOR_VOLTAGE,
272 .owner = THIS_MODULE,
273 },
274 {
275 .name = "LDO4",
276 .id = LP3971_LDO4,
277 .ops = &lp3971_ldo_ops,
278 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
cad8d76e 279 .volt_table = ldo45_voltage_map,
0cbdf7bc
MS
280 .type = REGULATOR_VOLTAGE,
281 .owner = THIS_MODULE,
282 },
283 {
284 .name = "LDO5",
285 .id = LP3971_LDO5,
286 .ops = &lp3971_ldo_ops,
287 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
cad8d76e 288 .volt_table = ldo45_voltage_map,
0cbdf7bc
MS
289 .type = REGULATOR_VOLTAGE,
290 .owner = THIS_MODULE,
291 },
292 {
293 .name = "DCDC1",
294 .id = LP3971_DCDC1,
295 .ops = &lp3971_dcdc_ops,
296 .n_voltages = ARRAY_SIZE(buck_voltage_map),
cad8d76e 297 .volt_table = buck_voltage_map,
0cbdf7bc
MS
298 .type = REGULATOR_VOLTAGE,
299 .owner = THIS_MODULE,
300 },
301 {
302 .name = "DCDC2",
303 .id = LP3971_DCDC2,
304 .ops = &lp3971_dcdc_ops,
305 .n_voltages = ARRAY_SIZE(buck_voltage_map),
cad8d76e 306 .volt_table = buck_voltage_map,
0cbdf7bc
MS
307 .type = REGULATOR_VOLTAGE,
308 .owner = THIS_MODULE,
309 },
310 {
311 .name = "DCDC3",
312 .id = LP3971_DCDC3,
313 .ops = &lp3971_dcdc_ops,
314 .n_voltages = ARRAY_SIZE(buck_voltage_map),
cad8d76e 315 .volt_table = buck_voltage_map,
0cbdf7bc
MS
316 .type = REGULATOR_VOLTAGE,
317 .owner = THIS_MODULE,
318 },
319};
320
321static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
322 u16 *dest)
323{
324 int ret;
325
326 if (count != 1)
327 return -EIO;
328 ret = i2c_smbus_read_byte_data(i2c, reg);
27ef7f00 329 if (ret < 0)
0cbdf7bc
MS
330 return -EIO;
331
332 *dest = ret;
333 return 0;
334}
335
336static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
337 const u16 *src)
338{
0cbdf7bc
MS
339 if (count != 1)
340 return -EIO;
1bddc2f5 341 return i2c_smbus_write_byte_data(i2c, reg, *src);
0cbdf7bc
MS
342}
343
344static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
345{
346 u16 val = 0;
347
348 mutex_lock(&lp3971->io_lock);
349
350 lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
351
352 dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
353 (unsigned)val&0xff);
354
355 mutex_unlock(&lp3971->io_lock);
356
357 return val & 0xff;
358}
359
360static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
361{
362 u16 tmp;
363 int ret;
364
365 mutex_lock(&lp3971->io_lock);
366
367 ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
368 tmp = (tmp & ~mask) | val;
369 if (ret == 0) {
370 ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp);
371 dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
372 (unsigned)val&0xff);
373 }
374 mutex_unlock(&lp3971->io_lock);
375
376 return ret;
377}
378
a5023574 379static int setup_regulators(struct lp3971 *lp3971,
ebbed04f 380 struct lp3971_platform_data *pdata)
0cbdf7bc
MS
381{
382 int i, err;
ebbed04f
DT
383
384 lp3971->num_regulators = pdata->num_regulators;
385 lp3971->rdev = kcalloc(pdata->num_regulators,
386 sizeof(struct regulator_dev *), GFP_KERNEL);
67e46f34
DC
387 if (!lp3971->rdev) {
388 err = -ENOMEM;
389 goto err_nomem;
390 }
0cbdf7bc
MS
391
392 /* Instantiate the regulators */
ebbed04f 393 for (i = 0; i < pdata->num_regulators; i++) {
c172708d 394 struct regulator_config config = { };
ebbed04f 395 struct lp3971_regulator_subdev *reg = &pdata->regulators[i];
0cbdf7bc 396
c172708d
MB
397 config.dev = lp3971->dev;
398 config.init_data = reg->initdata;
399 config.driver_data = lp3971;
400
401 lp3971->rdev[i] = regulator_register(&regulators[reg->id],
402 &config);
d662fc82
JL
403 if (IS_ERR(lp3971->rdev[i])) {
404 err = PTR_ERR(lp3971->rdev[i]);
0cbdf7bc
MS
405 dev_err(lp3971->dev, "regulator init failed: %d\n",
406 err);
407 goto error;
408 }
409 }
410
411 return 0;
ebbed04f 412
0cbdf7bc 413error:
ebbed04f
DT
414 while (--i >= 0)
415 regulator_unregister(lp3971->rdev[i]);
0cbdf7bc
MS
416 kfree(lp3971->rdev);
417 lp3971->rdev = NULL;
67e46f34 418err_nomem:
0cbdf7bc
MS
419 return err;
420}
421
a5023574 422static int lp3971_i2c_probe(struct i2c_client *i2c,
0cbdf7bc
MS
423 const struct i2c_device_id *id)
424{
425 struct lp3971 *lp3971;
426 struct lp3971_platform_data *pdata = i2c->dev.platform_data;
427 int ret;
428 u16 val;
429
ebbed04f
DT
430 if (!pdata) {
431 dev_dbg(&i2c->dev, "No platform init data supplied\n");
432 return -ENODEV;
0cbdf7bc
MS
433 }
434
ebbed04f
DT
435 lp3971 = kzalloc(sizeof(struct lp3971), GFP_KERNEL);
436 if (lp3971 == NULL)
437 return -ENOMEM;
438
0cbdf7bc
MS
439 lp3971->i2c = i2c;
440 lp3971->dev = &i2c->dev;
0cbdf7bc
MS
441
442 mutex_init(&lp3971->io_lock);
443
444 /* Detect LP3971 */
445 ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val);
446 if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL)
447 ret = -ENODEV;
448 if (ret < 0) {
449 dev_err(&i2c->dev, "failed to detect device\n");
450 goto err_detect;
451 }
452
ebbed04f
DT
453 ret = setup_regulators(lp3971, pdata);
454 if (ret < 0)
455 goto err_detect;
0cbdf7bc 456
ebbed04f 457 i2c_set_clientdata(i2c, lp3971);
0cbdf7bc
MS
458 return 0;
459
460err_detect:
0cbdf7bc 461 kfree(lp3971);
0cbdf7bc
MS
462 return ret;
463}
464
8dc995f5 465static int lp3971_i2c_remove(struct i2c_client *i2c)
0cbdf7bc
MS
466{
467 struct lp3971 *lp3971 = i2c_get_clientdata(i2c);
468 int i;
ebbed04f 469
0cbdf7bc 470 for (i = 0; i < lp3971->num_regulators; i++)
ebbed04f
DT
471 regulator_unregister(lp3971->rdev[i]);
472
0cbdf7bc 473 kfree(lp3971->rdev);
0cbdf7bc
MS
474 kfree(lp3971);
475
476 return 0;
477}
478
479static const struct i2c_device_id lp3971_i2c_id[] = {
480 { "lp3971", 0 },
481 { }
482};
483MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
484
485static struct i2c_driver lp3971_i2c_driver = {
486 .driver = {
487 .name = "LP3971",
488 .owner = THIS_MODULE,
489 },
490 .probe = lp3971_i2c_probe,
5eb9f2b9 491 .remove = lp3971_i2c_remove,
0cbdf7bc
MS
492 .id_table = lp3971_i2c_id,
493};
494
5af34e60 495module_i2c_driver(lp3971_i2c_driver);
0cbdf7bc
MS
496
497MODULE_LICENSE("GPL");
498MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
499MODULE_DESCRIPTION("LP3971 PMIC driver");