Linux 3.4.102
[linux/fpc-iii.git] / drivers / regulator / tps6524x-regulator.c
blob4a421be6d4f2fa7d7b700b43a24367d354b0a020
1 /*
2 * Regulator driver for TPS6524x PMIC
4 * Copyright (C) 2010 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
11 * whether express or implied; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/spi/spi.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
25 #define REG_LDO_SET 0x0
26 #define LDO_ILIM_MASK 1 /* 0 = 400-800, 1 = 900-1500 */
27 #define LDO_VSEL_MASK 0x0f
28 #define LDO2_ILIM_SHIFT 12
29 #define LDO2_VSEL_SHIFT 4
30 #define LDO1_ILIM_SHIFT 8
31 #define LDO1_VSEL_SHIFT 0
33 #define REG_BLOCK_EN 0x1
34 #define BLOCK_MASK 1
35 #define BLOCK_LDO1_SHIFT 0
36 #define BLOCK_LDO2_SHIFT 1
37 #define BLOCK_LCD_SHIFT 2
38 #define BLOCK_USB_SHIFT 3
40 #define REG_DCDC_SET 0x2
41 #define DCDC_VDCDC_MASK 0x1f
42 #define DCDC_VDCDC1_SHIFT 0
43 #define DCDC_VDCDC2_SHIFT 5
44 #define DCDC_VDCDC3_SHIFT 10
46 #define REG_DCDC_EN 0x3
47 #define DCDCDCDC_EN_MASK 0x1
48 #define DCDCDCDC1_EN_SHIFT 0
49 #define DCDCDCDC1_PG_MSK BIT(1)
50 #define DCDCDCDC2_EN_SHIFT 2
51 #define DCDCDCDC2_PG_MSK BIT(3)
52 #define DCDCDCDC3_EN_SHIFT 4
53 #define DCDCDCDC3_PG_MSK BIT(5)
55 #define REG_USB 0x4
56 #define USB_ILIM_SHIFT 0
57 #define USB_ILIM_MASK 0x3
58 #define USB_TSD_SHIFT 2
59 #define USB_TSD_MASK 0x3
60 #define USB_TWARN_SHIFT 4
61 #define USB_TWARN_MASK 0x3
62 #define USB_IWARN_SD BIT(6)
63 #define USB_FAST_LOOP BIT(7)
65 #define REG_ALARM 0x5
66 #define ALARM_LDO1 BIT(0)
67 #define ALARM_DCDC1 BIT(1)
68 #define ALARM_DCDC2 BIT(2)
69 #define ALARM_DCDC3 BIT(3)
70 #define ALARM_LDO2 BIT(4)
71 #define ALARM_USB_WARN BIT(5)
72 #define ALARM_USB_ALARM BIT(6)
73 #define ALARM_LCD BIT(9)
74 #define ALARM_TEMP_WARM BIT(10)
75 #define ALARM_TEMP_HOT BIT(11)
76 #define ALARM_NRST BIT(14)
77 #define ALARM_POWERUP BIT(15)
79 #define REG_INT_ENABLE 0x6
80 #define INT_LDO1 BIT(0)
81 #define INT_DCDC1 BIT(1)
82 #define INT_DCDC2 BIT(2)
83 #define INT_DCDC3 BIT(3)
84 #define INT_LDO2 BIT(4)
85 #define INT_USB_WARN BIT(5)
86 #define INT_USB_ALARM BIT(6)
87 #define INT_LCD BIT(9)
88 #define INT_TEMP_WARM BIT(10)
89 #define INT_TEMP_HOT BIT(11)
90 #define INT_GLOBAL_EN BIT(15)
92 #define REG_INT_STATUS 0x7
93 #define STATUS_LDO1 BIT(0)
94 #define STATUS_DCDC1 BIT(1)
95 #define STATUS_DCDC2 BIT(2)
96 #define STATUS_DCDC3 BIT(3)
97 #define STATUS_LDO2 BIT(4)
98 #define STATUS_USB_WARN BIT(5)
99 #define STATUS_USB_ALARM BIT(6)
100 #define STATUS_LCD BIT(9)
101 #define STATUS_TEMP_WARM BIT(10)
102 #define STATUS_TEMP_HOT BIT(11)
104 #define REG_SOFTWARE_RESET 0xb
105 #define REG_WRITE_ENABLE 0xd
106 #define REG_REV_ID 0xf
108 #define N_DCDC 3
109 #define N_LDO 2
110 #define N_SWITCH 2
111 #define N_REGULATORS (N_DCDC + N_LDO + N_SWITCH)
113 #define FIXED_ILIMSEL BIT(0)
114 #define FIXED_VOLTAGE BIT(1)
116 #define CMD_READ(reg) ((reg) << 6)
117 #define CMD_WRITE(reg) (BIT(5) | (reg) << 6)
118 #define STAT_CLK BIT(3)
119 #define STAT_WRITE BIT(2)
120 #define STAT_INVALID BIT(1)
121 #define STAT_WP BIT(0)
123 struct field {
124 int reg;
125 int shift;
126 int mask;
129 struct supply_info {
130 const char *name;
131 int n_voltages;
132 const int *voltages;
133 int fixed_voltage;
134 int n_ilimsels;
135 const int *ilimsels;
136 int fixed_ilimsel;
137 int flags;
138 struct field enable, voltage, ilimsel;
141 struct tps6524x {
142 struct device *dev;
143 struct spi_device *spi;
144 struct mutex lock;
145 struct regulator_desc desc[N_REGULATORS];
146 struct regulator_dev *rdev[N_REGULATORS];
149 static int __read_reg(struct tps6524x *hw, int reg)
151 int error = 0;
152 u16 cmd = CMD_READ(reg), in;
153 u8 status;
154 struct spi_message m;
155 struct spi_transfer t[3];
157 spi_message_init(&m);
158 memset(t, 0, sizeof(t));
160 t[0].tx_buf = &cmd;
161 t[0].len = 2;
162 t[0].bits_per_word = 12;
163 spi_message_add_tail(&t[0], &m);
165 t[1].rx_buf = &in;
166 t[1].len = 2;
167 t[1].bits_per_word = 16;
168 spi_message_add_tail(&t[1], &m);
170 t[2].rx_buf = &status;
171 t[2].len = 1;
172 t[2].bits_per_word = 4;
173 spi_message_add_tail(&t[2], &m);
175 error = spi_sync(hw->spi, &m);
176 if (error < 0)
177 return error;
179 dev_dbg(hw->dev, "read reg %d, data %x, status %x\n",
180 reg, in, status);
182 if (!(status & STAT_CLK) || (status & STAT_WRITE))
183 return -EIO;
185 if (status & STAT_INVALID)
186 return -EINVAL;
188 return in;
191 static int read_reg(struct tps6524x *hw, int reg)
193 int ret;
195 mutex_lock(&hw->lock);
196 ret = __read_reg(hw, reg);
197 mutex_unlock(&hw->lock);
199 return ret;
202 static int __write_reg(struct tps6524x *hw, int reg, int val)
204 int error = 0;
205 u16 cmd = CMD_WRITE(reg), out = val;
206 u8 status;
207 struct spi_message m;
208 struct spi_transfer t[3];
210 spi_message_init(&m);
211 memset(t, 0, sizeof(t));
213 t[0].tx_buf = &cmd;
214 t[0].len = 2;
215 t[0].bits_per_word = 12;
216 spi_message_add_tail(&t[0], &m);
218 t[1].tx_buf = &out;
219 t[1].len = 2;
220 t[1].bits_per_word = 16;
221 spi_message_add_tail(&t[1], &m);
223 t[2].rx_buf = &status;
224 t[2].len = 1;
225 t[2].bits_per_word = 4;
226 spi_message_add_tail(&t[2], &m);
228 error = spi_sync(hw->spi, &m);
229 if (error < 0)
230 return error;
232 dev_dbg(hw->dev, "wrote reg %d, data %x, status %x\n",
233 reg, out, status);
235 if (!(status & STAT_CLK) || !(status & STAT_WRITE))
236 return -EIO;
238 if (status & (STAT_INVALID | STAT_WP))
239 return -EINVAL;
241 return error;
244 static int __rmw_reg(struct tps6524x *hw, int reg, int mask, int val)
246 int ret;
248 ret = __read_reg(hw, reg);
249 if (ret < 0)
250 return ret;
252 ret &= ~mask;
253 ret |= val;
255 ret = __write_reg(hw, reg, ret);
257 return (ret < 0) ? ret : 0;
260 static int rmw_protect(struct tps6524x *hw, int reg, int mask, int val)
262 int ret;
264 mutex_lock(&hw->lock);
266 ret = __write_reg(hw, REG_WRITE_ENABLE, 1);
267 if (ret) {
268 dev_err(hw->dev, "failed to set write enable\n");
269 goto error;
272 ret = __rmw_reg(hw, reg, mask, val);
273 if (ret)
274 dev_err(hw->dev, "failed to rmw register %d\n", reg);
276 ret = __write_reg(hw, REG_WRITE_ENABLE, 0);
277 if (ret) {
278 dev_err(hw->dev, "failed to clear write enable\n");
279 goto error;
282 error:
283 mutex_unlock(&hw->lock);
285 return ret;
288 static int read_field(struct tps6524x *hw, const struct field *field)
290 int tmp;
292 tmp = read_reg(hw, field->reg);
293 if (tmp < 0)
294 return tmp;
296 return (tmp >> field->shift) & field->mask;
299 static int write_field(struct tps6524x *hw, const struct field *field,
300 int val)
302 if (val & ~field->mask)
303 return -EOVERFLOW;
305 return rmw_protect(hw, field->reg,
306 field->mask << field->shift,
307 val << field->shift);
310 static const int dcdc1_voltages[] = {
311 800000, 825000, 850000, 875000,
312 900000, 925000, 950000, 975000,
313 1000000, 1025000, 1050000, 1075000,
314 1100000, 1125000, 1150000, 1175000,
315 1200000, 1225000, 1250000, 1275000,
316 1300000, 1325000, 1350000, 1375000,
317 1400000, 1425000, 1450000, 1475000,
318 1500000, 1525000, 1550000, 1575000,
321 static const int dcdc2_voltages[] = {
322 1400000, 1450000, 1500000, 1550000,
323 1600000, 1650000, 1700000, 1750000,
324 1800000, 1850000, 1900000, 1950000,
325 2000000, 2050000, 2100000, 2150000,
326 2200000, 2250000, 2300000, 2350000,
327 2400000, 2450000, 2500000, 2550000,
328 2600000, 2650000, 2700000, 2750000,
329 2800000, 2850000, 2900000, 2950000,
332 static const int dcdc3_voltages[] = {
333 2400000, 2450000, 2500000, 2550000, 2600000,
334 2650000, 2700000, 2750000, 2800000, 2850000,
335 2900000, 2950000, 3000000, 3050000, 3100000,
336 3150000, 3200000, 3250000, 3300000, 3350000,
337 3400000, 3450000, 3500000, 3550000, 3600000,
340 static const int ldo1_voltages[] = {
341 4300000, 4350000, 4400000, 4450000,
342 4500000, 4550000, 4600000, 4650000,
343 4700000, 4750000, 4800000, 4850000,
344 4900000, 4950000, 5000000, 5050000,
347 static const int ldo2_voltages[] = {
348 1100000, 1150000, 1200000, 1250000,
349 1300000, 1700000, 1750000, 1800000,
350 1850000, 1900000, 3150000, 3200000,
351 3250000, 3300000, 3350000, 3400000,
354 static const int ldo_ilimsel[] = {
355 400000, 1500000
358 static const int usb_ilimsel[] = {
359 200000, 400000, 800000, 1000000
362 #define __MK_FIELD(_reg, _mask, _shift) \
363 { .reg = (_reg), .mask = (_mask), .shift = (_shift), }
365 static const struct supply_info supply_info[N_REGULATORS] = {
367 .name = "DCDC1",
368 .flags = FIXED_ILIMSEL,
369 .n_voltages = ARRAY_SIZE(dcdc1_voltages),
370 .voltages = dcdc1_voltages,
371 .fixed_ilimsel = 2400000,
372 .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
373 DCDCDCDC1_EN_SHIFT),
374 .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
375 DCDC_VDCDC1_SHIFT),
378 .name = "DCDC2",
379 .flags = FIXED_ILIMSEL,
380 .n_voltages = ARRAY_SIZE(dcdc2_voltages),
381 .voltages = dcdc2_voltages,
382 .fixed_ilimsel = 1200000,
383 .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
384 DCDCDCDC2_EN_SHIFT),
385 .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
386 DCDC_VDCDC2_SHIFT),
389 .name = "DCDC3",
390 .flags = FIXED_ILIMSEL,
391 .n_voltages = ARRAY_SIZE(dcdc3_voltages),
392 .voltages = dcdc3_voltages,
393 .fixed_ilimsel = 1200000,
394 .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
395 DCDCDCDC3_EN_SHIFT),
396 .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
397 DCDC_VDCDC3_SHIFT),
400 .name = "LDO1",
401 .n_voltages = ARRAY_SIZE(ldo1_voltages),
402 .voltages = ldo1_voltages,
403 .n_ilimsels = ARRAY_SIZE(ldo_ilimsel),
404 .ilimsels = ldo_ilimsel,
405 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
406 BLOCK_LDO1_SHIFT),
407 .voltage = __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
408 LDO1_VSEL_SHIFT),
409 .ilimsel = __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
410 LDO1_ILIM_SHIFT),
413 .name = "LDO2",
414 .n_voltages = ARRAY_SIZE(ldo2_voltages),
415 .voltages = ldo2_voltages,
416 .n_ilimsels = ARRAY_SIZE(ldo_ilimsel),
417 .ilimsels = ldo_ilimsel,
418 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
419 BLOCK_LDO2_SHIFT),
420 .voltage = __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
421 LDO2_VSEL_SHIFT),
422 .ilimsel = __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
423 LDO2_ILIM_SHIFT),
426 .name = "USB",
427 .flags = FIXED_VOLTAGE,
428 .fixed_voltage = 5000000,
429 .n_ilimsels = ARRAY_SIZE(usb_ilimsel),
430 .ilimsels = usb_ilimsel,
431 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
432 BLOCK_USB_SHIFT),
433 .ilimsel = __MK_FIELD(REG_USB, USB_ILIM_MASK,
434 USB_ILIM_SHIFT),
437 .name = "LCD",
438 .flags = FIXED_VOLTAGE | FIXED_ILIMSEL,
439 .fixed_voltage = 5000000,
440 .fixed_ilimsel = 400000,
441 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
442 BLOCK_LCD_SHIFT),
446 static int list_voltage(struct regulator_dev *rdev, unsigned selector)
448 const struct supply_info *info;
449 struct tps6524x *hw;
451 hw = rdev_get_drvdata(rdev);
452 info = &supply_info[rdev_get_id(rdev)];
454 if (info->flags & FIXED_VOLTAGE)
455 return selector ? -EINVAL : info->fixed_voltage;
457 return ((selector < info->n_voltages) ?
458 info->voltages[selector] : -EINVAL);
461 static int set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
462 unsigned *selector)
464 const struct supply_info *info;
465 struct tps6524x *hw;
466 unsigned i;
468 hw = rdev_get_drvdata(rdev);
469 info = &supply_info[rdev_get_id(rdev)];
471 if (info->flags & FIXED_VOLTAGE)
472 return -EINVAL;
474 for (i = 0; i < info->n_voltages; i++)
475 if (min_uV <= info->voltages[i] &&
476 max_uV >= info->voltages[i])
477 break;
479 if (i >= info->n_voltages)
480 i = info->n_voltages - 1;
482 *selector = i;
484 return write_field(hw, &info->voltage, i);
487 static int get_voltage(struct regulator_dev *rdev)
489 const struct supply_info *info;
490 struct tps6524x *hw;
491 int ret;
493 hw = rdev_get_drvdata(rdev);
494 info = &supply_info[rdev_get_id(rdev)];
496 if (info->flags & FIXED_VOLTAGE)
497 return info->fixed_voltage;
499 ret = read_field(hw, &info->voltage);
500 if (ret < 0)
501 return ret;
502 if (WARN_ON(ret >= info->n_voltages))
503 return -EIO;
505 return info->voltages[ret];
508 static int set_current_limit(struct regulator_dev *rdev, int min_uA,
509 int max_uA)
511 const struct supply_info *info;
512 struct tps6524x *hw;
513 int i;
515 hw = rdev_get_drvdata(rdev);
516 info = &supply_info[rdev_get_id(rdev)];
518 if (info->flags & FIXED_ILIMSEL)
519 return -EINVAL;
521 for (i = 0; i < info->n_ilimsels; i++)
522 if (min_uA <= info->ilimsels[i] &&
523 max_uA >= info->ilimsels[i])
524 break;
526 if (i >= info->n_ilimsels)
527 return -EINVAL;
529 return write_field(hw, &info->ilimsel, i);
532 static int get_current_limit(struct regulator_dev *rdev)
534 const struct supply_info *info;
535 struct tps6524x *hw;
536 int ret;
538 hw = rdev_get_drvdata(rdev);
539 info = &supply_info[rdev_get_id(rdev)];
541 if (info->flags & FIXED_ILIMSEL)
542 return info->fixed_ilimsel;
544 ret = read_field(hw, &info->ilimsel);
545 if (ret < 0)
546 return ret;
547 if (WARN_ON(ret >= info->n_ilimsels))
548 return -EIO;
550 return info->ilimsels[ret];
553 static int enable_supply(struct regulator_dev *rdev)
555 const struct supply_info *info;
556 struct tps6524x *hw;
558 hw = rdev_get_drvdata(rdev);
559 info = &supply_info[rdev_get_id(rdev)];
561 return write_field(hw, &info->enable, 1);
564 static int disable_supply(struct regulator_dev *rdev)
566 const struct supply_info *info;
567 struct tps6524x *hw;
569 hw = rdev_get_drvdata(rdev);
570 info = &supply_info[rdev_get_id(rdev)];
572 return write_field(hw, &info->enable, 0);
575 static int is_supply_enabled(struct regulator_dev *rdev)
577 const struct supply_info *info;
578 struct tps6524x *hw;
580 hw = rdev_get_drvdata(rdev);
581 info = &supply_info[rdev_get_id(rdev)];
583 return read_field(hw, &info->enable);
586 static struct regulator_ops regulator_ops = {
587 .is_enabled = is_supply_enabled,
588 .enable = enable_supply,
589 .disable = disable_supply,
590 .get_voltage = get_voltage,
591 .set_voltage = set_voltage,
592 .list_voltage = list_voltage,
593 .set_current_limit = set_current_limit,
594 .get_current_limit = get_current_limit,
597 static int pmic_remove(struct spi_device *spi)
599 struct tps6524x *hw = spi_get_drvdata(spi);
600 int i;
602 if (!hw)
603 return 0;
604 for (i = 0; i < N_REGULATORS; i++) {
605 if (hw->rdev[i])
606 regulator_unregister(hw->rdev[i]);
607 hw->rdev[i] = NULL;
609 spi_set_drvdata(spi, NULL);
610 kfree(hw);
611 return 0;
614 static int __devinit pmic_probe(struct spi_device *spi)
616 struct tps6524x *hw;
617 struct device *dev = &spi->dev;
618 const struct supply_info *info = supply_info;
619 struct regulator_init_data *init_data;
620 int ret = 0, i;
622 init_data = dev->platform_data;
623 if (!init_data) {
624 dev_err(dev, "could not find regulator platform data\n");
625 return -EINVAL;
628 hw = kzalloc(sizeof(struct tps6524x), GFP_KERNEL);
629 if (!hw) {
630 dev_err(dev, "cannot allocate regulator private data\n");
631 return -ENOMEM;
633 spi_set_drvdata(spi, hw);
635 memset(hw, 0, sizeof(struct tps6524x));
636 hw->dev = dev;
637 hw->spi = spi_dev_get(spi);
638 mutex_init(&hw->lock);
640 for (i = 0; i < N_REGULATORS; i++, info++, init_data++) {
641 hw->desc[i].name = info->name;
642 hw->desc[i].id = i;
643 hw->desc[i].n_voltages = info->n_voltages;
644 hw->desc[i].ops = &regulator_ops;
645 hw->desc[i].type = REGULATOR_VOLTAGE;
646 hw->desc[i].owner = THIS_MODULE;
648 if (info->flags & FIXED_VOLTAGE)
649 hw->desc[i].n_voltages = 1;
651 hw->rdev[i] = regulator_register(&hw->desc[i], dev,
652 init_data, hw, NULL);
653 if (IS_ERR(hw->rdev[i])) {
654 ret = PTR_ERR(hw->rdev[i]);
655 hw->rdev[i] = NULL;
656 goto fail;
660 return 0;
662 fail:
663 pmic_remove(spi);
664 return ret;
667 static struct spi_driver pmic_driver = {
668 .probe = pmic_probe,
669 .remove = __devexit_p(pmic_remove),
670 .driver = {
671 .name = "tps6524x",
672 .owner = THIS_MODULE,
676 static int __init pmic_driver_init(void)
678 return spi_register_driver(&pmic_driver);
680 module_init(pmic_driver_init);
682 static void __exit pmic_driver_exit(void)
684 spi_unregister_driver(&pmic_driver);
686 module_exit(pmic_driver_exit);
688 MODULE_DESCRIPTION("TPS6524X PMIC Driver");
689 MODULE_AUTHOR("Cyril Chemparathy");
690 MODULE_LICENSE("GPL");
691 MODULE_ALIAS("spi:tps6524x");