regulator: tps65023: Fixes i2c configuration issues
[linux-2.6.git] / drivers / regulator / tps65023-regulator.c
1 /*
2  * tps65023-regulator.c
3  *
4  * Supports TPS65023 Regulator
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>
27 #include <linux/slab.h>
28 #include <linux/regmap.h>
29
30 /* Register definitions */
31 #define TPS65023_REG_VERSION            0
32 #define TPS65023_REG_PGOODZ             1
33 #define TPS65023_REG_MASK               2
34 #define TPS65023_REG_REG_CTRL           3
35 #define TPS65023_REG_CON_CTRL           4
36 #define TPS65023_REG_CON_CTRL2          5
37 #define TPS65023_REG_DEF_CORE           6
38 #define TPS65023_REG_DEFSLEW            7
39 #define TPS65023_REG_LDO_CTRL           8
40
41 /* PGOODZ bitfields */
42 #define TPS65023_PGOODZ_PWRFAILZ        BIT(7)
43 #define TPS65023_PGOODZ_LOWBATTZ        BIT(6)
44 #define TPS65023_PGOODZ_VDCDC1          BIT(5)
45 #define TPS65023_PGOODZ_VDCDC2          BIT(4)
46 #define TPS65023_PGOODZ_VDCDC3          BIT(3)
47 #define TPS65023_PGOODZ_LDO2            BIT(2)
48 #define TPS65023_PGOODZ_LDO1            BIT(1)
49
50 /* MASK bitfields */
51 #define TPS65023_MASK_PWRFAILZ          BIT(7)
52 #define TPS65023_MASK_LOWBATTZ          BIT(6)
53 #define TPS65023_MASK_VDCDC1            BIT(5)
54 #define TPS65023_MASK_VDCDC2            BIT(4)
55 #define TPS65023_MASK_VDCDC3            BIT(3)
56 #define TPS65023_MASK_LDO2              BIT(2)
57 #define TPS65023_MASK_LDO1              BIT(1)
58
59 /* REG_CTRL bitfields */
60 #define TPS65023_REG_CTRL_VDCDC1_EN     BIT(5)
61 #define TPS65023_REG_CTRL_VDCDC2_EN     BIT(4)
62 #define TPS65023_REG_CTRL_VDCDC3_EN     BIT(3)
63 #define TPS65023_REG_CTRL_LDO2_EN       BIT(2)
64 #define TPS65023_REG_CTRL_LDO1_EN       BIT(1)
65
66 /* REG_CTRL2 bitfields */
67 #define TPS65023_REG_CTRL2_GO           BIT(7)
68 #define TPS65023_REG_CTRL2_CORE_ADJ     BIT(6)
69 #define TPS65023_REG_CTRL2_DCDC2        BIT(2)
70 #define TPS65023_REG_CTRL2_DCDC1        BIT(2)
71 #define TPS65023_REG_CTRL2_DCDC3        BIT(0)
72
73 /* REG_CTRL2 bitfields */
74 #define TPS65023_REG_CTRL2_GO           BIT(7)
75 #define TPS65023_REG_CTRL2_CORE_ADJ     BIT(6)
76 #define TPS65023_REG_CTRL2_DCDC2        BIT(2)
77 #define TPS65023_REG_CTRL2_DCDC1        BIT(1)
78 #define TPS65023_REG_CTRL2_DCDC3        BIT(0)
79
80 /* LDO_CTRL bitfields */
81 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id)    ((ldo_id)*4)
82 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id)     (0xF0 >> ((ldo_id)*4))
83
84 /* Number of step-down converters available */
85 #define TPS65023_NUM_DCDC               3
86 /* Number of LDO voltage regulators  available */
87 #define TPS65023_NUM_LDO                2
88 /* Number of total regulators available */
89 #define TPS65023_NUM_REGULATOR  (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
90
91 /* DCDCs */
92 #define TPS65023_DCDC_1                 0
93 #define TPS65023_DCDC_2                 1
94 #define TPS65023_DCDC_3                 2
95 /* LDOs */
96 #define TPS65023_LDO_1                  3
97 #define TPS65023_LDO_2                  4
98
99 #define TPS65023_MAX_REG_ID             TPS65023_LDO_2
100
101 /* Supported voltage values for regulators */
102 static const u16 VDCDC1_VSEL_table[] = {
103         800, 825, 850, 875,
104         900, 925, 950, 975,
105         1000, 1025, 1050, 1075,
106         1100, 1125, 1150, 1175,
107         1200, 1225, 1250, 1275,
108         1300, 1325, 1350, 1375,
109         1400, 1425, 1450, 1475,
110         1500, 1525, 1550, 1600,
111 };
112
113 static const u16 LDO1_VSEL_table[] = {
114         1000, 1100, 1300, 1800,
115         2200, 2600, 2800, 3150,
116 };
117
118 static const u16 LDO2_VSEL_table[] = {
119         1050, 1200, 1300, 1800,
120         2500, 2800, 3000, 3300,
121 };
122
123 static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
124                                 0, 0, ARRAY_SIZE(LDO1_VSEL_table),
125                                 ARRAY_SIZE(LDO2_VSEL_table)};
126
127 /* Regulator specific details */
128 struct tps_info {
129         const char *name;
130         unsigned min_uV;
131         unsigned max_uV;
132         bool fixed;
133         u8 table_len;
134         const u16 *table;
135 };
136
137 /* PMIC details */
138 struct tps_pmic {
139         struct regulator_desc desc[TPS65023_NUM_REGULATOR];
140         struct i2c_client *client;
141         struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
142         const struct tps_info *info[TPS65023_NUM_REGULATOR];
143         struct regmap *regmap;
144 };
145
146 static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
147 {
148         return regmap_update_bits(tps->regmap, reg, mask, mask);
149 }
150
151 static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
152 {
153         return regmap_update_bits(tps->regmap, reg, mask, 0);
154 }
155
156 static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
157 {
158         unsigned int val;
159         int ret;
160
161         ret = regmap_read(tps->regmap, reg, &val);
162
163         if (ret != 0)
164                 return ret;
165         else
166                 return val;
167 }
168
169 static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
170 {
171         return regmap_write(tps->regmap, reg, val);
172 }
173
174 static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
175 {
176         struct tps_pmic *tps = rdev_get_drvdata(dev);
177         int data, dcdc = rdev_get_id(dev);
178         u8 shift;
179
180         if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
181                 return -EINVAL;
182
183         shift = TPS65023_NUM_REGULATOR - dcdc;
184         data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
185
186         if (data < 0)
187                 return data;
188         else
189                 return (data & 1<<shift) ? 1 : 0;
190 }
191
192 static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
193 {
194         struct tps_pmic *tps = rdev_get_drvdata(dev);
195         int data, ldo = rdev_get_id(dev);
196         u8 shift;
197
198         if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
199                 return -EINVAL;
200
201         shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
202         data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
203
204         if (data < 0)
205                 return data;
206         else
207                 return (data & 1<<shift) ? 1 : 0;
208 }
209
210 static int tps65023_dcdc_enable(struct regulator_dev *dev)
211 {
212         struct tps_pmic *tps = rdev_get_drvdata(dev);
213         int dcdc = rdev_get_id(dev);
214         u8 shift;
215
216         if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
217                 return -EINVAL;
218
219         shift = TPS65023_NUM_REGULATOR - dcdc;
220         return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
221 }
222
223 static int tps65023_dcdc_disable(struct regulator_dev *dev)
224 {
225         struct tps_pmic *tps = rdev_get_drvdata(dev);
226         int dcdc = rdev_get_id(dev);
227         u8 shift;
228
229         if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
230                 return -EINVAL;
231
232         shift = TPS65023_NUM_REGULATOR - dcdc;
233         return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
234 }
235
236 static int tps65023_ldo_enable(struct regulator_dev *dev)
237 {
238         struct tps_pmic *tps = rdev_get_drvdata(dev);
239         int ldo = rdev_get_id(dev);
240         u8 shift;
241
242         if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
243                 return -EINVAL;
244
245         shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
246         return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
247 }
248
249 static int tps65023_ldo_disable(struct regulator_dev *dev)
250 {
251         struct tps_pmic *tps = rdev_get_drvdata(dev);
252         int ldo = rdev_get_id(dev);
253         u8 shift;
254
255         if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
256                 return -EINVAL;
257
258         shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
259         return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
260 }
261
262 static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
263 {
264         struct tps_pmic *tps = rdev_get_drvdata(dev);
265         int data, dcdc = rdev_get_id(dev);
266
267         if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
268                 return -EINVAL;
269
270         if (dcdc == TPS65023_DCDC_1) {
271                 data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
272                 if (data < 0)
273                         return data;
274                 data &= (tps->info[dcdc]->table_len - 1);
275                 return tps->info[dcdc]->table[data] * 1000;
276         } else
277                 return tps->info[dcdc]->min_uV;
278 }
279
280 static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
281                                      int min_uV, int max_uV,
282                                      unsigned *selector)
283 {
284         struct tps_pmic *tps = rdev_get_drvdata(dev);
285         int dcdc = rdev_get_id(dev);
286         int vsel;
287
288         if (dcdc != TPS65023_DCDC_1)
289                 return -EINVAL;
290
291         if (min_uV < tps->info[dcdc]->min_uV
292                         || min_uV > tps->info[dcdc]->max_uV)
293                 return -EINVAL;
294         if (max_uV < tps->info[dcdc]->min_uV
295                         || max_uV > tps->info[dcdc]->max_uV)
296                 return -EINVAL;
297
298         for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
299                 int mV = tps->info[dcdc]->table[vsel];
300                 int uV = mV * 1000;
301
302                 /* Break at the first in-range value */
303                 if (min_uV <= uV && uV <= max_uV)
304                         break;
305         }
306
307         *selector = vsel;
308
309         /* write to the register in case we found a match */
310         if (vsel == tps->info[dcdc]->table_len)
311                 return -EINVAL;
312         else
313                 return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
314 }
315
316 static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
317 {
318         struct tps_pmic *tps = rdev_get_drvdata(dev);
319         int data, ldo = rdev_get_id(dev);
320
321         if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
322                 return -EINVAL;
323
324         data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
325         if (data < 0)
326                 return data;
327
328         data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
329         data &= (tps->info[ldo]->table_len - 1);
330         return tps->info[ldo]->table[data] * 1000;
331 }
332
333 static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
334                                     int min_uV, int max_uV, unsigned *selector)
335 {
336         struct tps_pmic *tps = rdev_get_drvdata(dev);
337         int data, vsel, ldo = rdev_get_id(dev);
338
339         if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
340                 return -EINVAL;
341
342         if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
343                 return -EINVAL;
344         if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
345                 return -EINVAL;
346
347         for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
348                 int mV = tps->info[ldo]->table[vsel];
349                 int uV = mV * 1000;
350
351                 /* Break at the first in-range value */
352                 if (min_uV <= uV && uV <= max_uV)
353                         break;
354         }
355
356         if (vsel == tps->info[ldo]->table_len)
357                 return -EINVAL;
358
359         *selector = vsel;
360
361         data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
362         if (data < 0)
363                 return data;
364
365         data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
366         data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
367         return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
368 }
369
370 static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
371                                         unsigned selector)
372 {
373         struct tps_pmic *tps = rdev_get_drvdata(dev);
374         int dcdc = rdev_get_id(dev);
375
376         if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
377                 return -EINVAL;
378
379         if (dcdc == TPS65023_DCDC_1) {
380                 if (selector >= tps->info[dcdc]->table_len)
381                         return -EINVAL;
382                 else
383                         return tps->info[dcdc]->table[selector] * 1000;
384         } else
385                 return tps->info[dcdc]->min_uV;
386 }
387
388 static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
389                                         unsigned selector)
390 {
391         struct tps_pmic *tps = rdev_get_drvdata(dev);
392         int ldo = rdev_get_id(dev);
393
394         if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
395                 return -EINVAL;
396
397         if (selector >= tps->info[ldo]->table_len)
398                 return -EINVAL;
399         else
400                 return tps->info[ldo]->table[selector] * 1000;
401 }
402
403 /* Operations permitted on VDCDCx */
404 static struct regulator_ops tps65023_dcdc_ops = {
405         .is_enabled = tps65023_dcdc_is_enabled,
406         .enable = tps65023_dcdc_enable,
407         .disable = tps65023_dcdc_disable,
408         .get_voltage = tps65023_dcdc_get_voltage,
409         .set_voltage = tps65023_dcdc_set_voltage,
410         .list_voltage = tps65023_dcdc_list_voltage,
411 };
412
413 /* Operations permitted on LDOx */
414 static struct regulator_ops tps65023_ldo_ops = {
415         .is_enabled = tps65023_ldo_is_enabled,
416         .enable = tps65023_ldo_enable,
417         .disable = tps65023_ldo_disable,
418         .get_voltage = tps65023_ldo_get_voltage,
419         .set_voltage = tps65023_ldo_set_voltage,
420         .list_voltage = tps65023_ldo_list_voltage,
421 };
422
423 static struct regmap_config tps65023_regmap_config = {
424         .reg_bits = 8,
425         .val_bits = 8,
426 };
427
428 static int __devinit tps_65023_probe(struct i2c_client *client,
429                                      const struct i2c_device_id *id)
430 {
431         const struct tps_info *info = (void *)id->driver_data;
432         struct regulator_init_data *init_data;
433         struct regulator_dev *rdev;
434         struct tps_pmic *tps;
435         int i;
436         int error;
437
438         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
439                 return -EIO;
440
441         /**
442          * init_data points to array of regulator_init structures
443          * coming from the board-evm file.
444          */
445         init_data = client->dev.platform_data;
446         if (!init_data)
447                 return -EIO;
448
449         tps = kzalloc(sizeof(*tps), GFP_KERNEL);
450         if (!tps)
451                 return -ENOMEM;
452
453         tps->regmap = regmap_init_i2c(client, &tps65023_regmap_config);
454         if (IS_ERR(tps->regmap)) {
455                 error = PTR_ERR(tps->regmap);
456                 dev_err(&client->dev, "Failed to allocate register map: %d\n",
457                         error);
458                 goto fail_alloc;
459         }
460
461         /* common for all regulators */
462         tps->client = client;
463
464         for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
465                 /* Store regulator specific information */
466                 tps->info[i] = info;
467
468                 tps->desc[i].name = info->name;
469                 tps->desc[i].id = i;
470                 tps->desc[i].n_voltages = num_voltages[i];
471                 tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
472                                         &tps65023_ldo_ops : &tps65023_dcdc_ops);
473                 tps->desc[i].type = REGULATOR_VOLTAGE;
474                 tps->desc[i].owner = THIS_MODULE;
475
476                 /* Register the regulators */
477                 rdev = regulator_register(&tps->desc[i], &client->dev,
478                                           init_data, tps);
479                 if (IS_ERR(rdev)) {
480                         dev_err(&client->dev, "failed to register %s\n",
481                                 id->name);
482                         error = PTR_ERR(rdev);
483                         goto fail;
484                 }
485
486                 /* Save regulator for cleanup */
487                 tps->rdev[i] = rdev;
488         }
489
490         i2c_set_clientdata(client, tps);
491
492         /* Enable setting output voltage by I2C */
493         tps_65023_clear_bits(tps, TPS65023_REG_CON_CTRL2,
494                                                 TPS65023_REG_CTRL2_CORE_ADJ);
495
496         /* Enable setting output voltage by I2C */
497         tps_65023_clear_bits(tps, TPS65023_REG_CON_CTRL2,
498                                                 TPS65023_REG_CTRL2_CORE_ADJ);
499
500         return 0;
501
502  fail:
503         while (--i >= 0)
504                 regulator_unregister(tps->rdev[i]);
505
506         regmap_exit(tps->regmap);
507  fail_alloc:
508         kfree(tps);
509         return error;
510 }
511
512 /**
513  * tps_65023_remove - TPS65023 driver i2c remove handler
514  * @client: i2c driver client device structure
515  *
516  * Unregister TPS driver as an i2c client device driver
517  */
518 static int __devexit tps_65023_remove(struct i2c_client *client)
519 {
520         struct tps_pmic *tps = i2c_get_clientdata(client);
521         int i;
522
523         for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
524                 regulator_unregister(tps->rdev[i]);
525
526         regmap_exit(tps->regmap);
527         kfree(tps);
528
529         return 0;
530 }
531
532 static const struct tps_info tps65023_regs[] = {
533         {
534                 .name = "VDCDC1",
535                 .min_uV =  800000,
536                 .max_uV = 1600000,
537                 .table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
538                 .table = VDCDC1_VSEL_table,
539         },
540         {
541                 .name = "VDCDC2",
542                 .min_uV =  3300000,
543                 .max_uV = 3300000,
544                 .fixed = 1,
545         },
546         {
547                 .name = "VDCDC3",
548                 .min_uV =  1800000,
549                 .max_uV = 1800000,
550                 .fixed = 1,
551         },
552         {
553                 .name = "LDO1",
554                 .min_uV = 1000000,
555                 .max_uV = 3150000,
556                 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
557                 .table = LDO1_VSEL_table,
558         },
559         {
560                 .name = "LDO2",
561                 .min_uV = 1050000,
562                 .max_uV = 3300000,
563                 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
564                 .table = LDO2_VSEL_table,
565         },
566 };
567
568 static const struct i2c_device_id tps_65023_id[] = {
569         {.name = "tps65023",
570         .driver_data = (unsigned long) tps65023_regs,},
571         {.name = "tps65021",
572         .driver_data = (unsigned long) tps65023_regs,},
573         { },
574 };
575
576 MODULE_DEVICE_TABLE(i2c, tps_65023_id);
577
578 static struct i2c_driver tps_65023_i2c_driver = {
579         .driver = {
580                 .name = "tps65023",
581                 .owner = THIS_MODULE,
582         },
583         .probe = tps_65023_probe,
584         .remove = __devexit_p(tps_65023_remove),
585         .id_table = tps_65023_id,
586 };
587
588 /**
589  * tps_65023_init
590  *
591  * Module init function
592  */
593 static int __init tps_65023_init(void)
594 {
595         return i2c_add_driver(&tps_65023_i2c_driver);
596 }
597 subsys_initcall(tps_65023_init);
598
599 /**
600  * tps_65023_cleanup
601  *
602  * Module exit function
603  */
604 static void __exit tps_65023_cleanup(void)
605 {
606         i2c_del_driver(&tps_65023_i2c_driver);
607 }
608 module_exit(tps_65023_cleanup);
609
610 MODULE_AUTHOR("Texas Instruments");
611 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
612 MODULE_LICENSE("GPL v2");