blob: c771e1077cc17142d037b827e8820c9b9ecb9223 [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 Lin055917a2012-05-20 10:39:12 +080073static const unsigned int LDO2_VSEL_table[] = {
74 725000, 750000, 775000, 800000,
75 825000, 850000, 875000, 900000,
76 925000, 950000, 975000, 1000000,
77 1025000, 1050000, 1075000, 1100000,
78 1125000, 1150000, 1175000, 1200000,
79 1225000, 1250000, 1275000, 1300000,
80 1325000, 1350000, 1375000, 1400000,
81 1425000, 1450000, 1475000, 1500000,
82 1550000, 1600000, 1650000, 1700000,
83 1750000, 1800000, 1850000, 1900000,
84 1950000, 2000000, 2050000, 2100000,
85 2150000, 2200000, 2250000, 2300000,
86 2350000, 2400000, 2450000, 2500000,
87 2550000, 2600000, 2650000, 2700000,
88 2750000, 2800000, 2850000, 2900000,
89 3000000, 3100000, 3200000, 3300000,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053090};
91
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053092struct tps_info {
93 const char *name;
94 unsigned min_uV;
95 unsigned max_uV;
96 u8 table_len;
Axel Lin055917a2012-05-20 10:39:12 +080097 const unsigned int *table;
Anuj Aggarwal7d148312010-07-12 17:54:06 +053098
99 /* Does DCDC high or the low register defines output voltage? */
100 bool defdcdc_default;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530101};
102
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530103static struct tps_info tps6507x_pmic_regs[] = {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200104 {
105 .name = "VDCDC1",
106 .min_uV = 725000,
107 .max_uV = 3300000,
108 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
109 .table = VDCDCx_VSEL_table,
110 },
111 {
112 .name = "VDCDC2",
113 .min_uV = 725000,
114 .max_uV = 3300000,
115 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
116 .table = VDCDCx_VSEL_table,
117 },
118 {
119 .name = "VDCDC3",
120 .min_uV = 725000,
121 .max_uV = 3300000,
122 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
123 .table = VDCDCx_VSEL_table,
124 },
125 {
126 .name = "LDO1",
127 .min_uV = 1000000,
128 .max_uV = 3300000,
129 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
130 .table = LDO1_VSEL_table,
131 },
132 {
133 .name = "LDO2",
134 .min_uV = 725000,
135 .max_uV = 3300000,
136 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
137 .table = LDO2_VSEL_table,
138 },
139};
140
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600141struct tps6507x_pmic {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530142 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
Todd Fischer31dd6a22010-04-08 09:04:55 +0200143 struct tps6507x_dev *mfd;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530144 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530145 struct tps_info *info[TPS6507X_NUM_REGULATOR];
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530146 struct mutex io_lock;
147};
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600148static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530149{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200150 u8 val;
151 int err;
152
153 err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
154
155 if (err)
156 return err;
157
158 return val;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530159}
160
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600161static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530162{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200163 return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530164}
165
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600166static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530167{
168 int err, data;
169
170 mutex_lock(&tps->io_lock);
171
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600172 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530173 if (data < 0) {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200174 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530175 err = data;
176 goto out;
177 }
178
179 data |= mask;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600180 err = tps6507x_pmic_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530181 if (err)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200182 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530183
184out:
185 mutex_unlock(&tps->io_lock);
186 return err;
187}
188
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600189static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530190{
191 int err, data;
192
193 mutex_lock(&tps->io_lock);
194
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600195 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530196 if (data < 0) {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200197 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530198 err = data;
199 goto out;
200 }
201
202 data &= ~mask;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600203 err = tps6507x_pmic_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530204 if (err)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200205 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530206
207out:
208 mutex_unlock(&tps->io_lock);
209 return err;
210}
211
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600212static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530213{
214 int data;
215
216 mutex_lock(&tps->io_lock);
217
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600218 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530219 if (data < 0)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200220 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530221
222 mutex_unlock(&tps->io_lock);
223 return data;
224}
225
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600226static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530227{
228 int err;
229
230 mutex_lock(&tps->io_lock);
231
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600232 err = tps6507x_pmic_write(tps, reg, val);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530233 if (err < 0)
Todd Fischer31dd6a22010-04-08 09:04:55 +0200234 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530235
236 mutex_unlock(&tps->io_lock);
237 return err;
238}
239
Axel Linf2933d32012-03-12 15:57:50 +0800240static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530241{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600242 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800243 int data, rid = rdev_get_id(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530244 u8 shift;
245
Axel Linf2933d32012-03-12 15:57:50 +0800246 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530247 return -EINVAL;
248
Axel Linf2933d32012-03-12 15:57:50 +0800249 shift = TPS6507X_MAX_REG_ID - rid;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600250 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530251
252 if (data < 0)
253 return data;
254 else
255 return (data & 1<<shift) ? 1 : 0;
256}
257
Axel Linf2933d32012-03-12 15:57:50 +0800258static int tps6507x_pmic_enable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530259{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600260 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800261 int rid = rdev_get_id(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530262 u8 shift;
263
Axel Linf2933d32012-03-12 15:57:50 +0800264 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530265 return -EINVAL;
266
Axel Linf2933d32012-03-12 15:57:50 +0800267 shift = TPS6507X_MAX_REG_ID - rid;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600268 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530269}
270
Axel Linf2933d32012-03-12 15:57:50 +0800271static int tps6507x_pmic_disable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530272{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600273 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800274 int rid = rdev_get_id(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530275 u8 shift;
276
Axel Linf2933d32012-03-12 15:57:50 +0800277 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530278 return -EINVAL;
279
Axel Linf2933d32012-03-12 15:57:50 +0800280 shift = TPS6507X_MAX_REG_ID - rid;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600281 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
282 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530283}
284
Axel Lin7c842a12012-04-20 18:33:52 +0800285static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530286{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600287 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800288 int data, rid = rdev_get_id(dev);
289 u8 reg, mask;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530290
Axel Linf2933d32012-03-12 15:57:50 +0800291 switch (rid) {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530292 case TPS6507X_DCDC_1:
293 reg = TPS6507X_REG_DEFDCDC1;
Axel Linf2933d32012-03-12 15:57:50 +0800294 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530295 break;
296 case TPS6507X_DCDC_2:
Axel Linf2933d32012-03-12 15:57:50 +0800297 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530298 reg = TPS6507X_REG_DEFDCDC2_HIGH;
299 else
300 reg = TPS6507X_REG_DEFDCDC2_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800301 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530302 break;
303 case TPS6507X_DCDC_3:
Axel Linf2933d32012-03-12 15:57:50 +0800304 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530305 reg = TPS6507X_REG_DEFDCDC3_HIGH;
306 else
307 reg = TPS6507X_REG_DEFDCDC3_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800308 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
309 break;
310 case TPS6507X_LDO_1:
311 reg = TPS6507X_REG_LDO_CTRL1;
312 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
313 break;
314 case TPS6507X_LDO_2:
315 reg = TPS6507X_REG_DEFLDO2;
316 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530317 break;
318 default:
319 return -EINVAL;
320 }
321
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600322 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530323 if (data < 0)
324 return data;
325
Axel Linf2933d32012-03-12 15:57:50 +0800326 data &= mask;
Axel Lin7c842a12012-04-20 18:33:52 +0800327 return data;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530328}
329
Axel Linca61a7b2012-03-12 15:59:54 +0800330static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
331 unsigned selector)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530332{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600333 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Axel Linca61a7b2012-03-12 15:59:54 +0800334 int data, rid = rdev_get_id(dev);
Axel Linf2933d32012-03-12 15:57:50 +0800335 u8 reg, mask;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530336
Axel Linf2933d32012-03-12 15:57:50 +0800337 switch (rid) {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530338 case TPS6507X_DCDC_1:
339 reg = TPS6507X_REG_DEFDCDC1;
Axel Linf2933d32012-03-12 15:57:50 +0800340 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530341 break;
342 case TPS6507X_DCDC_2:
Axel Linf2933d32012-03-12 15:57:50 +0800343 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530344 reg = TPS6507X_REG_DEFDCDC2_HIGH;
345 else
346 reg = TPS6507X_REG_DEFDCDC2_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800347 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530348 break;
349 case TPS6507X_DCDC_3:
Axel Linf2933d32012-03-12 15:57:50 +0800350 if (tps->info[rid]->defdcdc_default)
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530351 reg = TPS6507X_REG_DEFDCDC3_HIGH;
352 else
353 reg = TPS6507X_REG_DEFDCDC3_LOW;
Axel Linf2933d32012-03-12 15:57:50 +0800354 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
355 break;
356 case TPS6507X_LDO_1:
357 reg = TPS6507X_REG_LDO_CTRL1;
358 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
359 break;
360 case TPS6507X_LDO_2:
361 reg = TPS6507X_REG_DEFLDO2;
362 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530363 break;
364 default:
365 return -EINVAL;
366 }
367
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600368 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530369 if (data < 0)
370 return data;
371
372 data &= ~mask;
Axel Linca61a7b2012-03-12 15:59:54 +0800373 data |= selector;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530374
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600375 return tps6507x_pmic_reg_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530376}
377
Axel Linf2933d32012-03-12 15:57:50 +0800378static struct regulator_ops tps6507x_pmic_ops = {
379 .is_enabled = tps6507x_pmic_is_enabled,
380 .enable = tps6507x_pmic_enable,
381 .disable = tps6507x_pmic_disable,
Axel Lin7c842a12012-04-20 18:33:52 +0800382 .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
Axel Linca61a7b2012-03-12 15:59:54 +0800383 .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
Axel Lin055917a2012-05-20 10:39:12 +0800384 .list_voltage = regulator_list_voltage_table,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530385};
386
Axel Linf2933d32012-03-12 15:57:50 +0800387static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530388{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200389 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530390 struct tps_info *info = &tps6507x_pmic_regs[0];
Mark Brownc1727082012-04-04 00:50:22 +0100391 struct regulator_config config = { };
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530392 struct regulator_init_data *init_data;
393 struct regulator_dev *rdev;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600394 struct tps6507x_pmic *tps;
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600395 struct tps6507x_board *tps_board;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530396 int i;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800397 int error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530398
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530399 /**
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600400 * tps_board points to pmic related constants
401 * coming from the board-evm file.
402 */
403
Todd Fischer31dd6a22010-04-08 09:04:55 +0200404 tps_board = dev_get_platdata(tps6507x_dev->dev);
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600405 if (!tps_board)
406 return -EINVAL;
407
408 /**
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530409 * init_data points to array of regulator_init structures
410 * coming from the board-evm file.
411 */
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600412 init_data = tps_board->tps6507x_pmic_init_data;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530413 if (!init_data)
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600414 return -EINVAL;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530415
Axel Lin9eb0c422012-04-11 14:40:18 +0800416 tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530417 if (!tps)
418 return -ENOMEM;
419
420 mutex_init(&tps->io_lock);
421
422 /* common for all regulators */
Todd Fischer31dd6a22010-04-08 09:04:55 +0200423 tps->mfd = tps6507x_dev;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530424
425 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
426 /* Register the regulators */
427 tps->info[i] = info;
Anuj Aggarwal7d148312010-07-12 17:54:06 +0530428 if (init_data->driver_data) {
429 struct tps6507x_reg_platform_data *data =
430 init_data->driver_data;
431 tps->info[i]->defdcdc_default = data->defdcdc_default;
432 }
433
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530434 tps->desc[i].name = info->name;
Axel Lin77fa44d2011-05-12 13:47:50 +0800435 tps->desc[i].id = i;
Axel Lin0fcdb102011-08-02 15:34:12 +0800436 tps->desc[i].n_voltages = info->table_len;
Axel Lin055917a2012-05-20 10:39:12 +0800437 tps->desc[i].volt_table = info->table;
Axel Linf2933d32012-03-12 15:57:50 +0800438 tps->desc[i].ops = &tps6507x_pmic_ops;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530439 tps->desc[i].type = REGULATOR_VOLTAGE;
440 tps->desc[i].owner = THIS_MODULE;
441
Mark Brownc1727082012-04-04 00:50:22 +0100442 config.dev = tps6507x_dev->dev;
443 config.init_data = init_data;
444 config.driver_data = tps;
445
446 rdev = regulator_register(&tps->desc[i], &config);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530447 if (IS_ERR(rdev)) {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200448 dev_err(tps6507x_dev->dev,
449 "failed to register %s regulator\n",
450 pdev->name);
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800451 error = PTR_ERR(rdev);
452 goto fail;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530453 }
454
455 /* Save regulator for cleanup */
456 tps->rdev[i] = rdev;
457 }
458
Todd Fischer31dd6a22010-04-08 09:04:55 +0200459 tps6507x_dev->pmic = tps;
Axel Lind7399fa2010-08-09 15:51:23 +0800460 platform_set_drvdata(pdev, tps6507x_dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530461
462 return 0;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800463
464fail:
465 while (--i >= 0)
466 regulator_unregister(tps->rdev[i]);
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800467 return error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530468}
469
Todd Fischer31dd6a22010-04-08 09:04:55 +0200470static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530471{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200472 struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
473 struct tps6507x_pmic *tps = tps6507x_dev->pmic;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530474 int i;
475
476 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
477 regulator_unregister(tps->rdev[i]);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530478 return 0;
479}
480
Todd Fischer31dd6a22010-04-08 09:04:55 +0200481static struct platform_driver tps6507x_pmic_driver = {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530482 .driver = {
Todd Fischer31dd6a22010-04-08 09:04:55 +0200483 .name = "tps6507x-pmic",
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530484 .owner = THIS_MODULE,
485 },
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600486 .probe = tps6507x_pmic_probe,
487 .remove = __devexit_p(tps6507x_pmic_remove),
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530488};
489
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600490static int __init tps6507x_pmic_init(void)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530491{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200492 return platform_driver_register(&tps6507x_pmic_driver);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530493}
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600494subsys_initcall(tps6507x_pmic_init);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530495
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600496static void __exit tps6507x_pmic_cleanup(void)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530497{
Todd Fischer31dd6a22010-04-08 09:04:55 +0200498 platform_driver_unregister(&tps6507x_pmic_driver);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530499}
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600500module_exit(tps6507x_pmic_cleanup);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530501
502MODULE_AUTHOR("Texas Instruments");
503MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
Liam Girdwood9e108d32009-08-24 10:31:34 +0100504MODULE_LICENSE("GPL v2");
Todd Fischer31dd6a22010-04-08 09:04:55 +0200505MODULE_ALIAS("platform:tps6507x-pmic");