blob: 07d01ccdf308fdf2a6871ce60f6d60f380d4060c [file] [log] [blame]
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +05301/*
2 * tps6507x-regulator.c
3 *
4 * Regulator driver for TPS65073 PMIC
5 *
6 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
Anuj Aggarwal7d148312010-07-12 17:54:06 +053025#include <linux/regulator/tps6507x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Todd Fischerd183fcc2010-04-05 20:23:56 -060027#include <linux/mfd/tps6507x.h>
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053028
29/* DCDC's */
30#define TPS6507X_DCDC_1 0
31#define TPS6507X_DCDC_2 1
32#define TPS6507X_DCDC_3 2
33/* LDOs */
34#define TPS6507X_LDO_1 3
35#define TPS6507X_LDO_2 4
36
37#define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
38
39/* Number of step-down converters available */
40#define TPS6507X_NUM_DCDC 3
41/* Number of LDO voltage regulators available */
42#define TPS6507X_NUM_LDO 2
43/* Number of total regulators available */
44#define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
45
Axel Lin055917a2012-05-20 10:39:12 +080046/* Supported voltage values for regulators (in microVolts) */
47static const unsigned int VDCDCx_VSEL_table[] = {
48 725000, 750000, 775000, 800000,
49 825000, 850000, 875000, 900000,
50 925000, 950000, 975000, 1000000,
51 1025000, 1050000, 1075000, 1100000,
52 1125000, 1150000, 1175000, 1200000,
53 1225000, 1250000, 1275000, 1300000,
54 1325000, 1350000, 1375000, 1400000,
55 1425000, 1450000, 1475000, 1500000,
56 1550000, 1600000, 1650000, 1700000,
57 1750000, 1800000, 1850000, 1900000,
58 1950000, 2000000, 2050000, 2100000,
59 2150000, 2200000, 2250000, 2300000,
60 2350000, 2400000, 2450000, 2500000,
61 2550000, 2600000, 2650000, 2700000,
62 2750000, 2800000, 2850000, 2900000,
63 3000000, 3100000, 3200000, 3300000,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053064};
65
Axel Lin055917a2012-05-20 10:39:12 +080066static const unsigned int LDO1_VSEL_table[] = {
67 1000000, 1100000, 1200000, 1250000,
68 1300000, 1350000, 1400000, 1500000,
69 1600000, 1800000, 2500000, 2750000,
70 2800000, 3000000, 3100000, 3300000,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053071};
72
Axel Lin93b07e72012-06-05 08:58:13 +080073/* The voltage mapping table for LDO2 is the same as VDCDCx */
74#define LDO2_VSEL_table VDCDCx_VSEL_table
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053075
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053076struct tps_info {
77 const char *name;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053078 u8 table_len;
Axel Lin055917a2012-05-20 10:39:12 +080079 const unsigned int *table;
Anuj Aggarwal7d148312010-07-12 17:54:06 +053080
81 /* Does DCDC high or the low register defines output voltage? */
82 bool defdcdc_default;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053083};
84
Anuj Aggarwal7d148312010-07-12 17:54:06 +053085static struct tps_info tps6507x_pmic_regs[] = {
Todd Fischer31dd6a22010-04-08 09:04:55 +020086 {
87 .name = "VDCDC1",
Todd Fischer31dd6a22010-04-08 09:04:55 +020088 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
89 .table = VDCDCx_VSEL_table,
90 },
91 {
92 .name = "VDCDC2",
Todd Fischer31dd6a22010-04-08 09:04:55 +020093 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
94 .table = VDCDCx_VSEL_table,
95 },
96 {
97 .name = "VDCDC3",
Todd Fischer31dd6a22010-04-08 09:04:55 +020098 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
99 .table = VDCDCx_VSEL_table,
100 },
101 {
102 .name = "LDO1",
Todd Fischer31dd6a22010-04-08 09:04:55 +0200103 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
104 .table = LDO1_VSEL_table,
105 },
106 {
107 .name = "LDO2",
Todd Fischer31dd6a22010-04-08 09:04:55 +0200108 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
109 .table = LDO2_VSEL_table,
110 },
111};
112
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600113struct tps6507x_pmic {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530114 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
Todd Fischer31dd6a22010-04-08 09:04:55 +0200115 struct tps6507x_dev *mfd;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530116 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530117 struct tps_info *info[TPS6507X_NUM_REGULATOR];
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530118 struct mutex io_lock;
119};
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600120static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530121{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200122 u8 val;
123 int err;
124
125 err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
126
127 if (err)
128 return err;
129
130 return val;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530131}
132
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600133static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530134{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200135 return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530136}
137
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600138static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530139{
140 int err, data;
141
142 mutex_lock(&tps->io_lock);
143
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600144 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530145 if (data < 0) {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200146 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530147 err = data;
148 goto out;
149 }
150
151 data |= mask;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600152 err = tps6507x_pmic_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530153 if (err)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200154 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530155
156out:
157 mutex_unlock(&tps->io_lock);
158 return err;
159}
160
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600161static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530162{
163 int err, data;
164
165 mutex_lock(&tps->io_lock);
166
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600167 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530168 if (data < 0) {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200169 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530170 err = data;
171 goto out;
172 }
173
174 data &= ~mask;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600175 err = tps6507x_pmic_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530176 if (err)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200177 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530178
179out:
180 mutex_unlock(&tps->io_lock);
181 return err;
182}
183
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600184static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530185{
186 int data;
187
188 mutex_lock(&tps->io_lock);
189
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600190 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530191 if (data < 0)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200192 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530193
194 mutex_unlock(&tps->io_lock);
195 return data;
196}
197
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600198static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530199{
200 int err;
201
202 mutex_lock(&tps->io_lock);
203
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600204 err = tps6507x_pmic_write(tps, reg, val);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530205 if (err < 0)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200206 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530207
208 mutex_unlock(&tps->io_lock);
209 return err;
210}
211
Axel Linf2933d32012-03-12 15:57:50 +0800212static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530213{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600214 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800215 int data, rid = rdev_get_id(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530216 u8 shift;
217
Axel Linf2933d32012-03-12 15:57:50 +0800218 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530219 return -EINVAL;
220
Axel Linf2933d32012-03-12 15:57:50 +0800221 shift = TPS6507X_MAX_REG_ID - rid;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600222 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530223
224 if (data < 0)
225 return data;
226 else
227 return (data & 1<<shift) ? 1 : 0;
228}
229
Axel Linf2933d32012-03-12 15:57:50 +0800230static int tps6507x_pmic_enable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530231{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600232 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800233 int rid = rdev_get_id(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530234 u8 shift;
235
Axel Linf2933d32012-03-12 15:57:50 +0800236 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530237 return -EINVAL;
238
Axel Linf2933d32012-03-12 15:57:50 +0800239 shift = TPS6507X_MAX_REG_ID - rid;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600240 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530241}
242
Axel Linf2933d32012-03-12 15:57:50 +0800243static int tps6507x_pmic_disable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530244{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600245 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800246 int rid = rdev_get_id(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530247 u8 shift;
248
Axel Linf2933d32012-03-12 15:57:50 +0800249 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530250 return -EINVAL;
251
Axel Linf2933d32012-03-12 15:57:50 +0800252 shift = TPS6507X_MAX_REG_ID - rid;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600253 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
254 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530255}
256
Axel Lin7c842a12012-04-20 18:33:52 +0800257static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530258{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600259 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800260 int data, rid = rdev_get_id(dev);
261 u8 reg, mask;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530262
Axel Linf2933d32012-03-12 15:57:50 +0800263 switch (rid) {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530264 case TPS6507X_DCDC_1:
265 reg = TPS6507X_REG_DEFDCDC1;
Axel Linf2933d32012-03-12 15:57:50 +0800266 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530267 break;
268 case TPS6507X_DCDC_2:
Axel Linf2933d32012-03-12 15:57:50 +0800269 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530270 reg = TPS6507X_REG_DEFDCDC2_HIGH;
271 else
272 reg = TPS6507X_REG_DEFDCDC2_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800273 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530274 break;
275 case TPS6507X_DCDC_3:
Axel Linf2933d32012-03-12 15:57:50 +0800276 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530277 reg = TPS6507X_REG_DEFDCDC3_HIGH;
278 else
279 reg = TPS6507X_REG_DEFDCDC3_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800280 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
281 break;
282 case TPS6507X_LDO_1:
283 reg = TPS6507X_REG_LDO_CTRL1;
284 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
285 break;
286 case TPS6507X_LDO_2:
287 reg = TPS6507X_REG_DEFLDO2;
288 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530289 break;
290 default:
291 return -EINVAL;
292 }
293
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600294 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530295 if (data < 0)
296 return data;
297
Axel Linf2933d32012-03-12 15:57:50 +0800298 data &= mask;
Axel Lin7c842a12012-04-20 18:33:52 +0800299 return data;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530300}
301
Axel Linca61a7b2012-03-12 15:59:54 +0800302static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
303 unsigned selector)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530304{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600305 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linca61a7b2012-03-12 15:59:54 +0800306 int data, rid = rdev_get_id(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800307 u8 reg, mask;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530308
Axel Linf2933d32012-03-12 15:57:50 +0800309 switch (rid) {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530310 case TPS6507X_DCDC_1:
311 reg = TPS6507X_REG_DEFDCDC1;
Axel Linf2933d32012-03-12 15:57:50 +0800312 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530313 break;
314 case TPS6507X_DCDC_2:
Axel Linf2933d32012-03-12 15:57:50 +0800315 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530316 reg = TPS6507X_REG_DEFDCDC2_HIGH;
317 else
318 reg = TPS6507X_REG_DEFDCDC2_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800319 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530320 break;
321 case TPS6507X_DCDC_3:
Axel Linf2933d32012-03-12 15:57:50 +0800322 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530323 reg = TPS6507X_REG_DEFDCDC3_HIGH;
324 else
325 reg = TPS6507X_REG_DEFDCDC3_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800326 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
327 break;
328 case TPS6507X_LDO_1:
329 reg = TPS6507X_REG_LDO_CTRL1;
330 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
331 break;
332 case TPS6507X_LDO_2:
333 reg = TPS6507X_REG_DEFLDO2;
334 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530335 break;
336 default:
337 return -EINVAL;
338 }
339
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600340 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530341 if (data < 0)
342 return data;
343
344 data &= ~mask;
Axel Linca61a7b2012-03-12 15:59:54 +0800345 data |= selector;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530346
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600347 return tps6507x_pmic_reg_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530348}
349
Axel Linf2933d32012-03-12 15:57:50 +0800350static struct regulator_ops tps6507x_pmic_ops = {
351 .is_enabled = tps6507x_pmic_is_enabled,
352 .enable = tps6507x_pmic_enable,
353 .disable = tps6507x_pmic_disable,
Axel Lin7c842a12012-04-20 18:33:52 +0800354 .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
Axel Linca61a7b2012-03-12 15:59:54 +0800355 .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
Axel Lin055917a2012-05-20 10:39:12 +0800356 .list_voltage = regulator_list_voltage_table,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530357};
358
Axel Linf2933d32012-03-12 15:57:50 +0800359static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530360{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200361 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530362 struct tps_info *info = &tps6507x_pmic_regs[0];
Mark Brownc1727082012-04-04 00:50:22 +0100363 struct regulator_config config = { };
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530364 struct regulator_init_data *init_data;
365 struct regulator_dev *rdev;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600366 struct tps6507x_pmic *tps;
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600367 struct tps6507x_board *tps_board;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530368 int i;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800369 int error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530370
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530371 /**
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600372 * tps_board points to pmic related constants
373 * coming from the board-evm file.
374 */
375
Todd Fischer31dd6a22010-04-08 09:04:55 +0200376 tps_board = dev_get_platdata(tps6507x_dev->dev);
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600377 if (!tps_board)
378 return -EINVAL;
379
380 /**
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530381 * init_data points to array of regulator_init structures
382 * coming from the board-evm file.
383 */
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600384 init_data = tps_board->tps6507x_pmic_init_data;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530385 if (!init_data)
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600386 return -EINVAL;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530387
Axel Lin9eb0c422012-04-11 14:40:18 +0800388 tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530389 if (!tps)
390 return -ENOMEM;
391
392 mutex_init(&tps->io_lock);
393
394 /* common for all regulators */
Todd Fischer31dd6a22010-04-08 09:04:55 +0200395 tps->mfd = tps6507x_dev;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530396
397 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
398 /* Register the regulators */
399 tps->info[i] = info;
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530400 if (init_data->driver_data) {
401 struct tps6507x_reg_platform_data *data =
402 init_data->driver_data;
403 tps->info[i]->defdcdc_default = data->defdcdc_default;
404 }
405
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530406 tps->desc[i].name = info->name;
Axel Lin77fa44d2011-05-12 13:47:50 +0800407 tps->desc[i].id = i;
Axel Lin0fcdb102011-08-02 15:34:12 +0800408 tps->desc[i].n_voltages = info->table_len;
Axel Lin055917a2012-05-20 10:39:12 +0800409 tps->desc[i].volt_table = info->table;
Axel Linf2933d32012-03-12 15:57:50 +0800410 tps->desc[i].ops = &tps6507x_pmic_ops;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530411 tps->desc[i].type = REGULATOR_VOLTAGE;
412 tps->desc[i].owner = THIS_MODULE;
413
Mark Brownc1727082012-04-04 00:50:22 +0100414 config.dev = tps6507x_dev->dev;
415 config.init_data = init_data;
416 config.driver_data = tps;
417
418 rdev = regulator_register(&tps->desc[i], &config);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530419 if (IS_ERR(rdev)) {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200420 dev_err(tps6507x_dev->dev,
421 "failed to register %s regulator\n",
422 pdev->name);
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800423 error = PTR_ERR(rdev);
424 goto fail;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530425 }
426
427 /* Save regulator for cleanup */
428 tps->rdev[i] = rdev;
429 }
430
Todd Fischer31dd6a22010-04-08 09:04:55 +0200431 tps6507x_dev->pmic = tps;
Axel Lind7399fa2010-08-09 15:51:23 +0800432 platform_set_drvdata(pdev, tps6507x_dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530433
434 return 0;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800435
436fail:
437 while (--i >= 0)
438 regulator_unregister(tps->rdev[i]);
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800439 return error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530440}
441
Todd Fischer31dd6a22010-04-08 09:04:55 +0200442static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530443{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200444 struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
445 struct tps6507x_pmic *tps = tps6507x_dev->pmic;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530446 int i;
447
448 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
449 regulator_unregister(tps->rdev[i]);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530450 return 0;
451}
452
Todd Fischer31dd6a22010-04-08 09:04:55 +0200453static struct platform_driver tps6507x_pmic_driver = {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530454 .driver = {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200455 .name = "tps6507x-pmic",
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530456 .owner = THIS_MODULE,
457 },
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600458 .probe = tps6507x_pmic_probe,
459 .remove = __devexit_p(tps6507x_pmic_remove),
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530460};
461
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600462static int __init tps6507x_pmic_init(void)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530463{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200464 return platform_driver_register(&tps6507x_pmic_driver);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530465}
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600466subsys_initcall(tps6507x_pmic_init);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530467
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600468static void __exit tps6507x_pmic_cleanup(void)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530469{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200470 platform_driver_unregister(&tps6507x_pmic_driver);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530471}
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600472module_exit(tps6507x_pmic_cleanup);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530473
474MODULE_AUTHOR("Texas Instruments");
475MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
Liam Girdwood9e108d32009-08-24 10:31:34 +0100476MODULE_LICENSE("GPL v2");
Todd Fischer31dd6a22010-04-08 09:04:55 +0200477MODULE_ALIAS("platform:tps6507x-pmic");