mfd: Add tps6507x board data structure
[linux-2.6.git] / drivers / regulator / tps6507x-regulator.c
1 /*
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>
27 #include <linux/slab.h>
28 #include <linux/mfd/tps6507x.h>
29
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) */
48 static 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
67 static 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
74 static 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
93 static 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
99 struct tps_info {
100         const char *name;
101         unsigned min_uV;
102         unsigned max_uV;
103         u8 table_len;
104         const u16 *table;
105 };
106
107 struct tps_pmic {
108         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
115 static inline int tps_6507x_read(struct tps_pmic *tps, u8 reg)
116 {
117         return i2c_smbus_read_byte_data(tps->client, reg);
118 }
119
120 static inline int tps_6507x_write(struct tps_pmic *tps, u8 reg, u8 val)
121 {
122         return i2c_smbus_write_byte_data(tps->client, reg, val);
123 }
124
125 static int tps_6507x_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
126 {
127         int err, data;
128
129         mutex_lock(&tps->io_lock);
130
131         data = tps_6507x_read(tps, reg);
132         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;
139         err = tps_6507x_write(tps, reg, data);
140         if (err)
141                 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
142
143 out:
144         mutex_unlock(&tps->io_lock);
145         return err;
146 }
147
148 static int tps_6507x_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
149 {
150         int err, data;
151
152         mutex_lock(&tps->io_lock);
153
154         data = tps_6507x_read(tps, reg);
155         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;
162         err = tps_6507x_write(tps, reg, data);
163         if (err)
164                 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
165
166 out:
167         mutex_unlock(&tps->io_lock);
168         return err;
169 }
170
171 static int tps_6507x_reg_read(struct tps_pmic *tps, u8 reg)
172 {
173         int data;
174
175         mutex_lock(&tps->io_lock);
176
177         data = tps_6507x_read(tps, reg);
178         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
185 static int tps_6507x_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
186 {
187         int err;
188
189         mutex_lock(&tps->io_lock);
190
191         err = tps_6507x_write(tps, reg, val);
192         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
199 static int tps6507x_dcdc_is_enabled(struct regulator_dev *dev)
200 {
201         struct tps_pmic *tps = rdev_get_drvdata(dev);
202         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;
209         data = tps_6507x_reg_read(tps, TPS6507X_REG_CON_CTRL1);
210
211         if (data < 0)
212                 return data;
213         else
214                 return (data & 1<<shift) ? 1 : 0;
215 }
216
217 static int tps6507x_ldo_is_enabled(struct regulator_dev *dev)
218 {
219         struct tps_pmic *tps = rdev_get_drvdata(dev);
220         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;
227         data = tps_6507x_reg_read(tps, TPS6507X_REG_CON_CTRL1);
228
229         if (data < 0)
230                 return data;
231         else
232                 return (data & 1<<shift) ? 1 : 0;
233 }
234
235 static int tps6507x_dcdc_enable(struct regulator_dev *dev)
236 {
237         struct tps_pmic *tps = rdev_get_drvdata(dev);
238         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;
245         return tps_6507x_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
246 }
247
248 static int tps6507x_dcdc_disable(struct regulator_dev *dev)
249 {
250         struct tps_pmic *tps = rdev_get_drvdata(dev);
251         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;
258         return tps_6507x_clear_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
259 }
260
261 static int tps6507x_ldo_enable(struct regulator_dev *dev)
262 {
263         struct tps_pmic *tps = rdev_get_drvdata(dev);
264         int ldo = rdev_get_id(dev);
265         u8 shift;
266
267         if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
268                 return -EINVAL;
269
270         shift = TPS6507X_MAX_REG_ID - ldo;
271         return tps_6507x_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
272 }
273
274 static int tps6507x_ldo_disable(struct regulator_dev *dev)
275 {
276         struct tps_pmic *tps = rdev_get_drvdata(dev);
277         int ldo = rdev_get_id(dev);
278         u8 shift;
279
280         if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
281                 return -EINVAL;
282
283         shift = TPS6507X_MAX_REG_ID - ldo;
284         return tps_6507x_clear_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
285 }
286
287 static int tps6507x_dcdc_get_voltage(struct regulator_dev *dev)
288 {
289         struct tps_pmic *tps = rdev_get_drvdata(dev);
290         int data, dcdc = rdev_get_id(dev);
291         u8 reg;
292
293         switch (dcdc) {
294         case TPS6507X_DCDC_1:
295                 reg = TPS6507X_REG_DEFDCDC1;
296                 break;
297         case TPS6507X_DCDC_2:
298                 reg = TPS6507X_REG_DEFDCDC2_LOW;
299                 break;
300         case TPS6507X_DCDC_3:
301                 reg = TPS6507X_REG_DEFDCDC3_LOW;
302                 break;
303         default:
304                 return -EINVAL;
305         }
306
307         data = tps_6507x_reg_read(tps, reg);
308         if (data < 0)
309                 return data;
310
311         data &= TPS6507X_DEFDCDCX_DCDC_MASK;
312         return tps->info[dcdc]->table[data] * 1000;
313 }
314
315 static int tps6507x_dcdc_set_voltage(struct regulator_dev *dev,
316                                 int min_uV, int max_uV)
317 {
318         struct tps_pmic *tps = rdev_get_drvdata(dev);
319         int data, vsel, dcdc = rdev_get_id(dev);
320         u8 reg;
321
322         switch (dcdc) {
323         case TPS6507X_DCDC_1:
324                 reg = TPS6507X_REG_DEFDCDC1;
325                 break;
326         case TPS6507X_DCDC_2:
327                 reg = TPS6507X_REG_DEFDCDC2_LOW;
328                 break;
329         case TPS6507X_DCDC_3:
330                 reg = TPS6507X_REG_DEFDCDC3_LOW;
331                 break;
332         default:
333                 return -EINVAL;
334         }
335
336         if (min_uV < tps->info[dcdc]->min_uV
337                 || min_uV > tps->info[dcdc]->max_uV)
338                 return -EINVAL;
339         if (max_uV < tps->info[dcdc]->min_uV
340                 || max_uV > tps->info[dcdc]->max_uV)
341                 return -EINVAL;
342
343         for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
344                 int mV = tps->info[dcdc]->table[vsel];
345                 int uV = mV * 1000;
346
347                 /* Break at the first in-range value */
348                 if (min_uV <= uV && uV <= max_uV)
349                         break;
350         }
351
352         /* write to the register in case we found a match */
353         if (vsel == tps->info[dcdc]->table_len)
354                 return -EINVAL;
355
356         data = tps_6507x_reg_read(tps, reg);
357         if (data < 0)
358                 return data;
359
360         data &= ~TPS6507X_DEFDCDCX_DCDC_MASK;
361         data |= vsel;
362
363         return tps_6507x_reg_write(tps, reg, data);
364 }
365
366 static int tps6507x_ldo_get_voltage(struct regulator_dev *dev)
367 {
368         struct tps_pmic *tps = rdev_get_drvdata(dev);
369         int data, ldo = rdev_get_id(dev);
370         u8 reg, mask;
371
372         if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
373                 return -EINVAL;
374         else {
375                 reg = (ldo == TPS6507X_LDO_1 ?
376                         TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
377                 mask = (ldo == TPS6507X_LDO_1 ?
378                         TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
379                                 TPS6507X_REG_DEFLDO2_LDO2_MASK);
380         }
381
382         data = tps_6507x_reg_read(tps, reg);
383         if (data < 0)
384                 return data;
385
386         data &= mask;
387         return tps->info[ldo]->table[data] * 1000;
388 }
389
390 static int tps6507x_ldo_set_voltage(struct regulator_dev *dev,
391                                 int min_uV, int max_uV)
392 {
393         struct tps_pmic *tps = rdev_get_drvdata(dev);
394         int data, vsel, ldo = rdev_get_id(dev);
395         u8 reg, mask;
396
397         if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
398                 return -EINVAL;
399         else {
400                 reg = (ldo == TPS6507X_LDO_1 ?
401                         TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
402                 mask = (ldo == TPS6507X_LDO_1 ?
403                         TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
404                                 TPS6507X_REG_DEFLDO2_LDO2_MASK);
405         }
406
407         if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
408                 return -EINVAL;
409         if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
410                 return -EINVAL;
411
412         for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
413                 int mV = tps->info[ldo]->table[vsel];
414                 int uV = mV * 1000;
415
416                 /* Break at the first in-range value */
417                 if (min_uV <= uV && uV <= max_uV)
418                         break;
419         }
420
421         if (vsel == tps->info[ldo]->table_len)
422                 return -EINVAL;
423
424         data = tps_6507x_reg_read(tps, reg);
425         if (data < 0)
426                 return data;
427
428         data &= ~mask;
429         data |= vsel;
430
431         return tps_6507x_reg_write(tps, reg, data);
432 }
433
434 static int tps6507x_dcdc_list_voltage(struct regulator_dev *dev,
435                                         unsigned selector)
436 {
437         struct tps_pmic *tps = rdev_get_drvdata(dev);
438         int dcdc = rdev_get_id(dev);
439
440         if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
441                 return -EINVAL;
442
443         if (selector >= tps->info[dcdc]->table_len)
444                 return -EINVAL;
445         else
446                 return tps->info[dcdc]->table[selector] * 1000;
447 }
448
449 static int tps6507x_ldo_list_voltage(struct regulator_dev *dev,
450                                         unsigned selector)
451 {
452         struct tps_pmic *tps = rdev_get_drvdata(dev);
453         int ldo = rdev_get_id(dev);
454
455         if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
456                 return -EINVAL;
457
458         if (selector >= tps->info[ldo]->table_len)
459                 return -EINVAL;
460         else
461                 return tps->info[ldo]->table[selector] * 1000;
462 }
463
464 /* Operations permitted on VDCDCx */
465 static struct regulator_ops tps6507x_dcdc_ops = {
466         .is_enabled = tps6507x_dcdc_is_enabled,
467         .enable = tps6507x_dcdc_enable,
468         .disable = tps6507x_dcdc_disable,
469         .get_voltage = tps6507x_dcdc_get_voltage,
470         .set_voltage = tps6507x_dcdc_set_voltage,
471         .list_voltage = tps6507x_dcdc_list_voltage,
472 };
473
474 /* Operations permitted on LDOx */
475 static struct regulator_ops tps6507x_ldo_ops = {
476         .is_enabled = tps6507x_ldo_is_enabled,
477         .enable = tps6507x_ldo_enable,
478         .disable = tps6507x_ldo_disable,
479         .get_voltage = tps6507x_ldo_get_voltage,
480         .set_voltage = tps6507x_ldo_set_voltage,
481         .list_voltage = tps6507x_ldo_list_voltage,
482 };
483
484 static int __devinit tps_6507x_probe(struct i2c_client *client,
485                                      const struct i2c_device_id *id)
486 {
487         static int desc_id;
488         const struct tps_info *info = (void *)id->driver_data;
489         struct regulator_init_data *init_data;
490         struct regulator_dev *rdev;
491         struct tps_pmic *tps;
492         struct tps6507x_board *tps_board;
493         int i;
494         int error;
495
496         if (!i2c_check_functionality(client->adapter,
497                                 I2C_FUNC_SMBUS_BYTE_DATA))
498                 return -EIO;
499
500         /**
501          * tps_board points to pmic related constants
502          * coming from the board-evm file.
503          */
504
505         tps_board = dev_get_platdata(&client->dev);
506         if (!tps_board)
507                 return -EINVAL;
508
509         /**
510          * init_data points to array of regulator_init structures
511          * coming from the board-evm file.
512          */
513         init_data = tps_board->tps6507x_pmic_init_data;
514         if (!init_data)
515                 return -EINVAL;
516
517         tps = kzalloc(sizeof(*tps), GFP_KERNEL);
518         if (!tps)
519                 return -ENOMEM;
520
521         mutex_init(&tps->io_lock);
522
523         /* common for all regulators */
524         tps->client = client;
525
526         for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) {
527                 /* Register the regulators */
528                 tps->info[i] = info;
529                 tps->desc[i].name = info->name;
530                 tps->desc[i].id = desc_id++;
531                 tps->desc[i].n_voltages = num_voltages[i];
532                 tps->desc[i].ops = (i > TPS6507X_DCDC_3 ?
533                                 &tps6507x_ldo_ops : &tps6507x_dcdc_ops);
534                 tps->desc[i].type = REGULATOR_VOLTAGE;
535                 tps->desc[i].owner = THIS_MODULE;
536
537                 rdev = regulator_register(&tps->desc[i],
538                                         &client->dev, init_data, tps);
539                 if (IS_ERR(rdev)) {
540                         dev_err(&client->dev, "failed to register %s\n",
541                                 id->name);
542                         error = PTR_ERR(rdev);
543                         goto fail;
544                 }
545
546                 /* Save regulator for cleanup */
547                 tps->rdev[i] = rdev;
548         }
549
550         i2c_set_clientdata(client, tps);
551
552         return 0;
553
554 fail:
555         while (--i >= 0)
556                 regulator_unregister(tps->rdev[i]);
557
558         kfree(tps);
559         return error;
560 }
561
562 /**
563  * tps_6507x_remove - TPS6507x driver i2c remove handler
564  * @client: i2c driver client device structure
565  *
566  * Unregister TPS driver as an i2c client device driver
567  */
568 static int __devexit tps_6507x_remove(struct i2c_client *client)
569 {
570         struct tps_pmic *tps = i2c_get_clientdata(client);
571         int i;
572
573         /* clear the client data in i2c */
574         i2c_set_clientdata(client, NULL);
575
576         for (i = 0; i < TPS6507X_NUM_REGULATOR; i++)
577                 regulator_unregister(tps->rdev[i]);
578
579         kfree(tps);
580
581         return 0;
582 }
583
584 static const struct tps_info tps6507x_regs[] = {
585         {
586                 .name = "VDCDC1",
587                 .min_uV = 725000,
588                 .max_uV = 3300000,
589                 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
590                 .table = VDCDCx_VSEL_table,
591         },
592         {
593                 .name = "VDCDC2",
594                 .min_uV = 725000,
595                 .max_uV = 3300000,
596                 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
597                 .table = VDCDCx_VSEL_table,
598         },
599         {
600                 .name = "VDCDC3",
601                 .min_uV = 725000,
602                 .max_uV = 3300000,
603                 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
604                 .table = VDCDCx_VSEL_table,
605         },
606         {
607                 .name = "LDO1",
608                 .min_uV = 1000000,
609                 .max_uV = 3300000,
610                 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
611                 .table = LDO1_VSEL_table,
612         },
613         {
614                 .name = "LDO2",
615                 .min_uV = 725000,
616                 .max_uV = 3300000,
617                 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
618                 .table = LDO2_VSEL_table,
619         },
620 };
621
622 static const struct i2c_device_id tps_6507x_id[] = {
623         {.name = "tps6507x",
624         .driver_data = (unsigned long) tps6507x_regs,},
625         { },
626 };
627 MODULE_DEVICE_TABLE(i2c, tps_6507x_id);
628
629 static struct i2c_driver tps_6507x_i2c_driver = {
630         .driver = {
631                 .name = "tps6507x",
632                 .owner = THIS_MODULE,
633         },
634         .probe = tps_6507x_probe,
635         .remove = __devexit_p(tps_6507x_remove),
636         .id_table = tps_6507x_id,
637 };
638
639 /**
640  * tps_6507x_init
641  *
642  * Module init function
643  */
644 static int __init tps_6507x_init(void)
645 {
646         return i2c_add_driver(&tps_6507x_i2c_driver);
647 }
648 subsys_initcall(tps_6507x_init);
649
650 /**
651  * tps_6507x_cleanup
652  *
653  * Module exit function
654  */
655 static void __exit tps_6507x_cleanup(void)
656 {
657         i2c_del_driver(&tps_6507x_i2c_driver);
658 }
659 module_exit(tps_6507x_cleanup);
660
661 MODULE_AUTHOR("Texas Instruments");
662 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
663 MODULE_LICENSE("GPL v2");