blob: ea44741fcd4777316ebeae22e1ddc69611521eb6 [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>
25#include <linux/i2c.h>
26#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Todd Fischerd183fcc2010-04-05 20:23:56 -060028#include <linux/mfd/tps6507x.h>
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +053029
30/* DCDC's */
31#define TPS6507X_DCDC_1 0
32#define TPS6507X_DCDC_2 1
33#define TPS6507X_DCDC_3 2
34/* LDOs */
35#define TPS6507X_LDO_1 3
36#define TPS6507X_LDO_2 4
37
38#define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
39
40/* Number of step-down converters available */
41#define TPS6507X_NUM_DCDC 3
42/* Number of LDO voltage regulators available */
43#define TPS6507X_NUM_LDO 2
44/* Number of total regulators available */
45#define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
46
47/* Supported voltage values for regulators (in milliVolts) */
48static const u16 VDCDCx_VSEL_table[] = {
49 725, 750, 775, 800,
50 825, 850, 875, 900,
51 925, 950, 975, 1000,
52 1025, 1050, 1075, 1100,
53 1125, 1150, 1175, 1200,
54 1225, 1250, 1275, 1300,
55 1325, 1350, 1375, 1400,
56 1425, 1450, 1475, 1500,
57 1550, 1600, 1650, 1700,
58 1750, 1800, 1850, 1900,
59 1950, 2000, 2050, 2100,
60 2150, 2200, 2250, 2300,
61 2350, 2400, 2450, 2500,
62 2550, 2600, 2650, 2700,
63 2750, 2800, 2850, 2900,
64 3000, 3100, 3200, 3300,
65};
66
67static const u16 LDO1_VSEL_table[] = {
68 1000, 1100, 1200, 1250,
69 1300, 1350, 1400, 1500,
70 1600, 1800, 2500, 2750,
71 2800, 3000, 3100, 3300,
72};
73
74static const u16 LDO2_VSEL_table[] = {
75 725, 750, 775, 800,
76 825, 850, 875, 900,
77 925, 950, 975, 1000,
78 1025, 1050, 1075, 1100,
79 1125, 1150, 1175, 1200,
80 1225, 1250, 1275, 1300,
81 1325, 1350, 1375, 1400,
82 1425, 1450, 1475, 1500,
83 1550, 1600, 1650, 1700,
84 1750, 1800, 1850, 1900,
85 1950, 2000, 2050, 2100,
86 2150, 2200, 2250, 2300,
87 2350, 2400, 2450, 2500,
88 2550, 2600, 2650, 2700,
89 2750, 2800, 2850, 2900,
90 3000, 3100, 3200, 3300,
91};
92
93static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDCx_VSEL_table),
94 ARRAY_SIZE(VDCDCx_VSEL_table),
95 ARRAY_SIZE(VDCDCx_VSEL_table),
96 ARRAY_SIZE(LDO1_VSEL_table),
97 ARRAY_SIZE(LDO2_VSEL_table)};
98
99struct tps_info {
100 const char *name;
101 unsigned min_uV;
102 unsigned max_uV;
103 u8 table_len;
104 const u16 *table;
105};
106
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600107struct tps6507x_pmic {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530108 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
109 struct i2c_client *client;
110 struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR];
111 const struct tps_info *info[TPS6507X_NUM_REGULATOR];
112 struct mutex io_lock;
113};
114
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600115static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530116{
117 return i2c_smbus_read_byte_data(tps->client, reg);
118}
119
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600120static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530121{
122 return i2c_smbus_write_byte_data(tps->client, reg, val);
123}
124
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600125static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530126{
127 int err, data;
128
129 mutex_lock(&tps->io_lock);
130
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600131 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530132 if (data < 0) {
133 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
134 err = data;
135 goto out;
136 }
137
138 data |= mask;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600139 err = tps6507x_pmic_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530140 if (err)
141 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
142
143out:
144 mutex_unlock(&tps->io_lock);
145 return err;
146}
147
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600148static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530149{
150 int err, data;
151
152 mutex_lock(&tps->io_lock);
153
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600154 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530155 if (data < 0) {
156 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
157 err = data;
158 goto out;
159 }
160
161 data &= ~mask;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600162 err = tps6507x_pmic_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530163 if (err)
164 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
165
166out:
167 mutex_unlock(&tps->io_lock);
168 return err;
169}
170
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600171static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530172{
173 int data;
174
175 mutex_lock(&tps->io_lock);
176
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600177 data = tps6507x_pmic_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530178 if (data < 0)
179 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
180
181 mutex_unlock(&tps->io_lock);
182 return data;
183}
184
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600185static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530186{
187 int err;
188
189 mutex_lock(&tps->io_lock);
190
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600191 err = tps6507x_pmic_write(tps, reg, val);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530192 if (err < 0)
193 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
194
195 mutex_unlock(&tps->io_lock);
196 return err;
197}
198
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600199static int tps6507x_pmic_dcdc_is_enabled(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530200{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600201 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530202 int data, dcdc = rdev_get_id(dev);
203 u8 shift;
204
205 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
206 return -EINVAL;
207
208 shift = TPS6507X_MAX_REG_ID - dcdc;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600209 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530210
211 if (data < 0)
212 return data;
213 else
214 return (data & 1<<shift) ? 1 : 0;
215}
216
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600217static int tps6507x_pmic_ldo_is_enabled(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530218{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600219 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530220 int data, ldo = rdev_get_id(dev);
221 u8 shift;
222
223 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
224 return -EINVAL;
225
226 shift = TPS6507X_MAX_REG_ID - ldo;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600227 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530228
229 if (data < 0)
230 return data;
231 else
232 return (data & 1<<shift) ? 1 : 0;
233}
234
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600235static int tps6507x_pmic_dcdc_enable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530236{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600237 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530238 int dcdc = rdev_get_id(dev);
239 u8 shift;
240
241 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
242 return -EINVAL;
243
244 shift = TPS6507X_MAX_REG_ID - dcdc;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600245 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530246}
247
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600248static int tps6507x_pmic_dcdc_disable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530249{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600250 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530251 int dcdc = rdev_get_id(dev);
252 u8 shift;
253
254 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
255 return -EINVAL;
256
257 shift = TPS6507X_MAX_REG_ID - dcdc;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600258 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
259 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530260}
261
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600262static int tps6507x_pmic_ldo_enable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530263{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600264 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530265 int ldo = rdev_get_id(dev);
266 u8 shift;
267
268 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
269 return -EINVAL;
270
271 shift = TPS6507X_MAX_REG_ID - ldo;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600272 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530273}
274
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600275static int tps6507x_pmic_ldo_disable(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530276{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600277 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530278 int ldo = rdev_get_id(dev);
279 u8 shift;
280
281 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
282 return -EINVAL;
283
284 shift = TPS6507X_MAX_REG_ID - ldo;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600285 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
286 1 << shift);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530287}
288
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600289static int tps6507x_pmic_dcdc_get_voltage(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530290{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600291 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530292 int data, dcdc = rdev_get_id(dev);
293 u8 reg;
294
295 switch (dcdc) {
296 case TPS6507X_DCDC_1:
297 reg = TPS6507X_REG_DEFDCDC1;
298 break;
299 case TPS6507X_DCDC_2:
300 reg = TPS6507X_REG_DEFDCDC2_LOW;
301 break;
302 case TPS6507X_DCDC_3:
303 reg = TPS6507X_REG_DEFDCDC3_LOW;
304 break;
305 default:
306 return -EINVAL;
307 }
308
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600309 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530310 if (data < 0)
311 return data;
312
313 data &= TPS6507X_DEFDCDCX_DCDC_MASK;
314 return tps->info[dcdc]->table[data] * 1000;
315}
316
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600317static int tps6507x_pmic_dcdc_set_voltage(struct regulator_dev *dev,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530318 int min_uV, int max_uV)
319{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600320 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530321 int data, vsel, dcdc = rdev_get_id(dev);
322 u8 reg;
323
324 switch (dcdc) {
325 case TPS6507X_DCDC_1:
326 reg = TPS6507X_REG_DEFDCDC1;
327 break;
328 case TPS6507X_DCDC_2:
329 reg = TPS6507X_REG_DEFDCDC2_LOW;
330 break;
331 case TPS6507X_DCDC_3:
332 reg = TPS6507X_REG_DEFDCDC3_LOW;
333 break;
334 default:
335 return -EINVAL;
336 }
337
338 if (min_uV < tps->info[dcdc]->min_uV
339 || min_uV > tps->info[dcdc]->max_uV)
340 return -EINVAL;
341 if (max_uV < tps->info[dcdc]->min_uV
342 || max_uV > tps->info[dcdc]->max_uV)
343 return -EINVAL;
344
345 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
346 int mV = tps->info[dcdc]->table[vsel];
347 int uV = mV * 1000;
348
349 /* Break at the first in-range value */
350 if (min_uV <= uV && uV <= max_uV)
351 break;
352 }
353
354 /* write to the register in case we found a match */
355 if (vsel == tps->info[dcdc]->table_len)
356 return -EINVAL;
357
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600358 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530359 if (data < 0)
360 return data;
361
362 data &= ~TPS6507X_DEFDCDCX_DCDC_MASK;
363 data |= vsel;
364
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600365 return tps6507x_pmic_reg_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530366}
367
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600368static int tps6507x_pmic_ldo_get_voltage(struct regulator_dev *dev)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530369{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600370 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530371 int data, ldo = rdev_get_id(dev);
372 u8 reg, mask;
373
374 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
375 return -EINVAL;
376 else {
377 reg = (ldo == TPS6507X_LDO_1 ?
378 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
379 mask = (ldo == TPS6507X_LDO_1 ?
380 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
381 TPS6507X_REG_DEFLDO2_LDO2_MASK);
382 }
383
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600384 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530385 if (data < 0)
386 return data;
387
388 data &= mask;
389 return tps->info[ldo]->table[data] * 1000;
390}
391
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600392static int tps6507x_pmic_ldo_set_voltage(struct regulator_dev *dev,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530393 int min_uV, int max_uV)
394{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600395 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530396 int data, vsel, ldo = rdev_get_id(dev);
397 u8 reg, mask;
398
399 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
400 return -EINVAL;
401 else {
402 reg = (ldo == TPS6507X_LDO_1 ?
403 TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
404 mask = (ldo == TPS6507X_LDO_1 ?
405 TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
406 TPS6507X_REG_DEFLDO2_LDO2_MASK);
407 }
408
409 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
410 return -EINVAL;
411 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
412 return -EINVAL;
413
414 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
415 int mV = tps->info[ldo]->table[vsel];
416 int uV = mV * 1000;
417
418 /* Break at the first in-range value */
419 if (min_uV <= uV && uV <= max_uV)
420 break;
421 }
422
423 if (vsel == tps->info[ldo]->table_len)
424 return -EINVAL;
425
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600426 data = tps6507x_pmic_reg_read(tps, reg);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530427 if (data < 0)
428 return data;
429
430 data &= ~mask;
431 data |= vsel;
432
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600433 return tps6507x_pmic_reg_write(tps, reg, data);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530434}
435
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600436static int tps6507x_pmic_dcdc_list_voltage(struct regulator_dev *dev,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530437 unsigned selector)
438{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600439 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530440 int dcdc = rdev_get_id(dev);
441
442 if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
443 return -EINVAL;
444
445 if (selector >= tps->info[dcdc]->table_len)
446 return -EINVAL;
447 else
448 return tps->info[dcdc]->table[selector] * 1000;
449}
450
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600451static int tps6507x_pmic_ldo_list_voltage(struct regulator_dev *dev,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530452 unsigned selector)
453{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600454 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530455 int ldo = rdev_get_id(dev);
456
457 if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
458 return -EINVAL;
459
460 if (selector >= tps->info[ldo]->table_len)
461 return -EINVAL;
462 else
463 return tps->info[ldo]->table[selector] * 1000;
464}
465
466/* Operations permitted on VDCDCx */
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600467static struct regulator_ops tps6507x_pmic_dcdc_ops = {
468 .is_enabled = tps6507x_pmic_dcdc_is_enabled,
469 .enable = tps6507x_pmic_dcdc_enable,
470 .disable = tps6507x_pmic_dcdc_disable,
471 .get_voltage = tps6507x_pmic_dcdc_get_voltage,
472 .set_voltage = tps6507x_pmic_dcdc_set_voltage,
473 .list_voltage = tps6507x_pmic_dcdc_list_voltage,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530474};
475
476/* Operations permitted on LDOx */
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600477static struct regulator_ops tps6507x_pmic_ldo_ops = {
478 .is_enabled = tps6507x_pmic_ldo_is_enabled,
479 .enable = tps6507x_pmic_ldo_enable,
480 .disable = tps6507x_pmic_ldo_disable,
481 .get_voltage = tps6507x_pmic_ldo_get_voltage,
482 .set_voltage = tps6507x_pmic_ldo_set_voltage,
483 .list_voltage = tps6507x_pmic_ldo_list_voltage,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530484};
485
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600486static int __devinit tps6507x_pmic_probe(struct i2c_client *client,
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800487 const struct i2c_device_id *id)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530488{
489 static int desc_id;
490 const struct tps_info *info = (void *)id->driver_data;
491 struct regulator_init_data *init_data;
492 struct regulator_dev *rdev;
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600493 struct tps6507x_pmic *tps;
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600494 struct tps6507x_board *tps_board;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530495 int i;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800496 int error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530497
498 if (!i2c_check_functionality(client->adapter,
499 I2C_FUNC_SMBUS_BYTE_DATA))
500 return -EIO;
501
502 /**
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600503 * tps_board points to pmic related constants
504 * coming from the board-evm file.
505 */
506
507 tps_board = dev_get_platdata(&client->dev);
508 if (!tps_board)
509 return -EINVAL;
510
511 /**
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530512 * init_data points to array of regulator_init structures
513 * coming from the board-evm file.
514 */
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600515 init_data = tps_board->tps6507x_pmic_init_data;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530516 if (!init_data)
Todd Fischer0bc20bb2010-04-05 20:23:57 -0600517 return -EINVAL;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530518
519 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
520 if (!tps)
521 return -ENOMEM;
522
523 mutex_init(&tps->io_lock);
524
525 /* common for all regulators */
526 tps->client = client;
527
528 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
529 /* Register the regulators */
530 tps->info[i] = info;
531 tps->desc[i].name = info->name;
532 tps->desc[i].id = desc_id++;
533 tps->desc[i].n_voltages = num_voltages[i];
534 tps->desc[i].ops = (i > TPS6507X_DCDC_3 ?
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600535 &tps6507x_pmic_ldo_ops : &tps6507x_pmic_dcdc_ops);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530536 tps->desc[i].type = REGULATOR_VOLTAGE;
537 tps->desc[i].owner = THIS_MODULE;
538
539 rdev = regulator_register(&tps->desc[i],
540 &client->dev, init_data, tps);
541 if (IS_ERR(rdev)) {
542 dev_err(&client->dev, "failed to register %s\n",
543 id->name);
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800544 error = PTR_ERR(rdev);
545 goto fail;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530546 }
547
548 /* Save regulator for cleanup */
549 tps->rdev[i] = rdev;
550 }
551
552 i2c_set_clientdata(client, tps);
553
554 return 0;
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800555
556fail:
557 while (--i >= 0)
558 regulator_unregister(tps->rdev[i]);
559
560 kfree(tps);
561 return error;
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530562}
563
564/**
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600565 * tps6507x_remove - TPS6507x driver i2c remove handler
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530566 * @client: i2c driver client device structure
567 *
568 * Unregister TPS driver as an i2c client device driver
569 */
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600570static int __devexit tps6507x_pmic_remove(struct i2c_client *client)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530571{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600572 struct tps6507x_pmic *tps = i2c_get_clientdata(client);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530573 int i;
574
Dmitry Torokhov56c23492010-02-23 23:38:12 -0800575 /* clear the client data in i2c */
576 i2c_set_clientdata(client, NULL);
577
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530578 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
579 regulator_unregister(tps->rdev[i]);
580
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530581 kfree(tps);
582
583 return 0;
584}
585
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600586static const struct tps_info tps6507x_pmic_regs[] = {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530587 {
588 .name = "VDCDC1",
589 .min_uV = 725000,
590 .max_uV = 3300000,
591 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
592 .table = VDCDCx_VSEL_table,
593 },
594 {
595 .name = "VDCDC2",
596 .min_uV = 725000,
597 .max_uV = 3300000,
598 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
599 .table = VDCDCx_VSEL_table,
600 },
601 {
602 .name = "VDCDC3",
603 .min_uV = 725000,
604 .max_uV = 3300000,
605 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
606 .table = VDCDCx_VSEL_table,
607 },
608 {
609 .name = "LDO1",
610 .min_uV = 1000000,
611 .max_uV = 3300000,
612 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
613 .table = LDO1_VSEL_table,
614 },
615 {
616 .name = "LDO2",
617 .min_uV = 725000,
618 .max_uV = 3300000,
619 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
620 .table = LDO2_VSEL_table,
621 },
622};
623
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600624static const struct i2c_device_id tps6507x_pmic_id[] = {
Liam Girdwood9e108d32009-08-24 10:31:34 +0100625 {.name = "tps6507x",
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600626 .driver_data = (unsigned long) tps6507x_pmic_regs,},
Liam Girdwood9e108d32009-08-24 10:31:34 +0100627 { },
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530628};
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600629MODULE_DEVICE_TABLE(i2c, tps6507x_pmic_id);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530630
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600631static struct i2c_driver tps6507x_i2c_driver = {
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530632 .driver = {
633 .name = "tps6507x",
634 .owner = THIS_MODULE,
635 },
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600636 .probe = tps6507x_pmic_probe,
637 .remove = __devexit_p(tps6507x_pmic_remove),
638 .id_table = tps6507x_pmic_id,
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530639};
640
641/**
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600642 * tps6507x_pmic_init
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530643 *
644 * Module init function
645 */
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600646static int __init tps6507x_pmic_init(void)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530647{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600648 return i2c_add_driver(&tps6507x_i2c_driver);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530649}
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600650subsys_initcall(tps6507x_pmic_init);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530651
652/**
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600653 * tps6507x_pmic_cleanup
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530654 *
655 * Module exit function
656 */
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600657static void __exit tps6507x_pmic_cleanup(void)
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530658{
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600659 i2c_del_driver(&tps6507x_i2c_driver);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530660}
Todd Fischer4ce5ba52010-04-05 20:23:58 -0600661module_exit(tps6507x_pmic_cleanup);
Anuj Aggarwal3fa5b8e2009-08-21 00:39:39 +0530662
663MODULE_AUTHOR("Texas Instruments");
664MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
Liam Girdwood9e108d32009-08-24 10:31:34 +0100665MODULE_LICENSE("GPL v2");