ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / regulator / tps65023-regulator.c
blobfbddc15e1811fefdec139965a425f45a5c05d473
1 /*
2 * tps65023-regulator.c
4 * Supports TPS65023 Regulator
6 * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
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.
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.
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>
29 /* Register definitions */
30 #define TPS65023_REG_VERSION 0
31 #define TPS65023_REG_PGOODZ 1
32 #define TPS65023_REG_MASK 2
33 #define TPS65023_REG_REG_CTRL 3
34 #define TPS65023_REG_CON_CTRL 4
35 #define TPS65023_REG_CON_CTRL2 5
36 #define TPS65023_REG_DEF_CORE 6
37 #define TPS65023_REG_DEFSLEW 7
38 #define TPS65023_REG_LDO_CTRL 8
40 /* PGOODZ bitfields */
41 #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
42 #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
43 #define TPS65023_PGOODZ_VDCDC1 BIT(5)
44 #define TPS65023_PGOODZ_VDCDC2 BIT(4)
45 #define TPS65023_PGOODZ_VDCDC3 BIT(3)
46 #define TPS65023_PGOODZ_LDO2 BIT(2)
47 #define TPS65023_PGOODZ_LDO1 BIT(1)
49 /* MASK bitfields */
50 #define TPS65023_MASK_PWRFAILZ BIT(7)
51 #define TPS65023_MASK_LOWBATTZ BIT(6)
52 #define TPS65023_MASK_VDCDC1 BIT(5)
53 #define TPS65023_MASK_VDCDC2 BIT(4)
54 #define TPS65023_MASK_VDCDC3 BIT(3)
55 #define TPS65023_MASK_LDO2 BIT(2)
56 #define TPS65023_MASK_LDO1 BIT(1)
58 /* REG_CTRL bitfields */
59 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
60 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
61 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
62 #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
63 #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
65 /* LDO_CTRL bitfields */
66 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
67 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
69 /* Number of step-down converters available */
70 #define TPS65023_NUM_DCDC 3
71 /* Number of LDO voltage regulators available */
72 #define TPS65023_NUM_LDO 2
73 /* Number of total regulators available */
74 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
76 /* DCDCs */
77 #define TPS65023_DCDC_1 0
78 #define TPS65023_DCDC_2 1
79 #define TPS65023_DCDC_3 2
80 /* LDOs */
81 #define TPS65023_LDO_1 3
82 #define TPS65023_LDO_2 4
84 #define TPS65023_MAX_REG_ID TPS65023_LDO_2
86 /* Supported voltage values for regulators */
87 static const u16 VDCDC1_VSEL_table[] = {
88 800, 825, 850, 875,
89 900, 925, 950, 975,
90 1000, 1025, 1050, 1075,
91 1100, 1125, 1150, 1175,
92 1200, 1225, 1250, 1275,
93 1300, 1325, 1350, 1375,
94 1400, 1425, 1450, 1475,
95 1500, 1525, 1550, 1600,
98 static const u16 LDO1_VSEL_table[] = {
99 1000, 1100, 1300, 1800,
100 2200, 2600, 2800, 3150,
103 static const u16 LDO2_VSEL_table[] = {
104 1050, 1200, 1300, 1800,
105 2500, 2800, 3000, 3300,
108 static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
109 0, 0, ARRAY_SIZE(LDO1_VSEL_table),
110 ARRAY_SIZE(LDO2_VSEL_table)};
112 /* Regulator specific details */
113 struct tps_info {
114 const char *name;
115 unsigned min_uV;
116 unsigned max_uV;
117 bool fixed;
118 u8 table_len;
119 const u16 *table;
122 /* PMIC details */
123 struct tps_pmic {
124 struct regulator_desc desc[TPS65023_NUM_REGULATOR];
125 struct i2c_client *client;
126 struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
127 const struct tps_info *info[TPS65023_NUM_REGULATOR];
128 struct mutex io_lock;
131 static inline int tps_65023_read(struct tps_pmic *tps, u8 reg)
133 return i2c_smbus_read_byte_data(tps->client, reg);
136 static inline int tps_65023_write(struct tps_pmic *tps, u8 reg, u8 val)
138 return i2c_smbus_write_byte_data(tps->client, reg, val);
141 static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
143 int err, data;
145 mutex_lock(&tps->io_lock);
147 data = tps_65023_read(tps, reg);
148 if (data < 0) {
149 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
150 err = data;
151 goto out;
154 data |= mask;
155 err = tps_65023_write(tps, reg, data);
156 if (err)
157 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
159 out:
160 mutex_unlock(&tps->io_lock);
161 return err;
164 static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
166 int err, data;
168 mutex_lock(&tps->io_lock);
170 data = tps_65023_read(tps, reg);
171 if (data < 0) {
172 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
173 err = data;
174 goto out;
177 data &= ~mask;
179 err = tps_65023_write(tps, reg, data);
180 if (err)
181 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
183 out:
184 mutex_unlock(&tps->io_lock);
185 return err;
189 static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
191 int data;
193 mutex_lock(&tps->io_lock);
195 data = tps_65023_read(tps, reg);
196 if (data < 0)
197 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
199 mutex_unlock(&tps->io_lock);
200 return data;
203 static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
205 int err;
207 mutex_lock(&tps->io_lock);
209 err = tps_65023_write(tps, reg, val);
210 if (err < 0)
211 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
213 mutex_unlock(&tps->io_lock);
214 return err;
217 static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
219 struct tps_pmic *tps = rdev_get_drvdata(dev);
220 int data, dcdc = rdev_get_id(dev);
221 u8 shift;
223 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
224 return -EINVAL;
226 shift = TPS65023_NUM_REGULATOR - dcdc;
227 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
229 if (data < 0)
230 return data;
231 else
232 return (data & 1<<shift) ? 1 : 0;
235 static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
237 struct tps_pmic *tps = rdev_get_drvdata(dev);
238 int data, ldo = rdev_get_id(dev);
239 u8 shift;
241 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
242 return -EINVAL;
244 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
245 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
247 if (data < 0)
248 return data;
249 else
250 return (data & 1<<shift) ? 1 : 0;
253 static int tps65023_dcdc_enable(struct regulator_dev *dev)
255 struct tps_pmic *tps = rdev_get_drvdata(dev);
256 int dcdc = rdev_get_id(dev);
257 u8 shift;
259 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
260 return -EINVAL;
262 shift = TPS65023_NUM_REGULATOR - dcdc;
263 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
266 static int tps65023_dcdc_disable(struct regulator_dev *dev)
268 struct tps_pmic *tps = rdev_get_drvdata(dev);
269 int dcdc = rdev_get_id(dev);
270 u8 shift;
272 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
273 return -EINVAL;
275 shift = TPS65023_NUM_REGULATOR - dcdc;
276 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
279 static int tps65023_ldo_enable(struct regulator_dev *dev)
281 struct tps_pmic *tps = rdev_get_drvdata(dev);
282 int ldo = rdev_get_id(dev);
283 u8 shift;
285 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
286 return -EINVAL;
288 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
289 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
292 static int tps65023_ldo_disable(struct regulator_dev *dev)
294 struct tps_pmic *tps = rdev_get_drvdata(dev);
295 int ldo = rdev_get_id(dev);
296 u8 shift;
298 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
299 return -EINVAL;
301 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
302 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
305 static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
307 struct tps_pmic *tps = rdev_get_drvdata(dev);
308 int data, dcdc = rdev_get_id(dev);
310 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
311 return -EINVAL;
313 if (dcdc == TPS65023_DCDC_1) {
314 data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
315 if (data < 0)
316 return data;
317 data &= (tps->info[dcdc]->table_len - 1);
318 return tps->info[dcdc]->table[data] * 1000;
319 } else
320 return tps->info[dcdc]->min_uV;
323 static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
324 int min_uV, int max_uV,
325 unsigned *selector)
327 struct tps_pmic *tps = rdev_get_drvdata(dev);
328 int dcdc = rdev_get_id(dev);
329 int vsel;
331 if (dcdc != TPS65023_DCDC_1)
332 return -EINVAL;
334 if (min_uV < tps->info[dcdc]->min_uV
335 || min_uV > tps->info[dcdc]->max_uV)
336 return -EINVAL;
337 if (max_uV < tps->info[dcdc]->min_uV
338 || max_uV > tps->info[dcdc]->max_uV)
339 return -EINVAL;
341 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
342 int mV = tps->info[dcdc]->table[vsel];
343 int uV = mV * 1000;
345 /* Break at the first in-range value */
346 if (min_uV <= uV && uV <= max_uV)
347 break;
350 *selector = vsel;
352 /* write to the register in case we found a match */
353 if (vsel == tps->info[dcdc]->table_len)
354 return -EINVAL;
355 else
356 return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
359 static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
361 struct tps_pmic *tps = rdev_get_drvdata(dev);
362 int data, ldo = rdev_get_id(dev);
364 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
365 return -EINVAL;
367 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
368 if (data < 0)
369 return data;
371 data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
372 data &= (tps->info[ldo]->table_len - 1);
373 return tps->info[ldo]->table[data] * 1000;
376 static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
377 int min_uV, int max_uV, unsigned *selector)
379 struct tps_pmic *tps = rdev_get_drvdata(dev);
380 int data, vsel, ldo = rdev_get_id(dev);
382 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
383 return -EINVAL;
385 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
386 return -EINVAL;
387 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
388 return -EINVAL;
390 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
391 int mV = tps->info[ldo]->table[vsel];
392 int uV = mV * 1000;
394 /* Break at the first in-range value */
395 if (min_uV <= uV && uV <= max_uV)
396 break;
399 if (vsel == tps->info[ldo]->table_len)
400 return -EINVAL;
402 *selector = vsel;
404 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
405 if (data < 0)
406 return data;
408 data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
409 data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
410 return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
413 static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
414 unsigned selector)
416 struct tps_pmic *tps = rdev_get_drvdata(dev);
417 int dcdc = rdev_get_id(dev);
419 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
420 return -EINVAL;
422 if (dcdc == TPS65023_DCDC_1) {
423 if (selector >= tps->info[dcdc]->table_len)
424 return -EINVAL;
425 else
426 return tps->info[dcdc]->table[selector] * 1000;
427 } else
428 return tps->info[dcdc]->min_uV;
431 static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
432 unsigned selector)
434 struct tps_pmic *tps = rdev_get_drvdata(dev);
435 int ldo = rdev_get_id(dev);
437 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
438 return -EINVAL;
440 if (selector >= tps->info[ldo]->table_len)
441 return -EINVAL;
442 else
443 return tps->info[ldo]->table[selector] * 1000;
446 /* Operations permitted on VDCDCx */
447 static struct regulator_ops tps65023_dcdc_ops = {
448 .is_enabled = tps65023_dcdc_is_enabled,
449 .enable = tps65023_dcdc_enable,
450 .disable = tps65023_dcdc_disable,
451 .get_voltage = tps65023_dcdc_get_voltage,
452 .set_voltage = tps65023_dcdc_set_voltage,
453 .list_voltage = tps65023_dcdc_list_voltage,
456 /* Operations permitted on LDOx */
457 static struct regulator_ops tps65023_ldo_ops = {
458 .is_enabled = tps65023_ldo_is_enabled,
459 .enable = tps65023_ldo_enable,
460 .disable = tps65023_ldo_disable,
461 .get_voltage = tps65023_ldo_get_voltage,
462 .set_voltage = tps65023_ldo_set_voltage,
463 .list_voltage = tps65023_ldo_list_voltage,
466 static int __devinit tps_65023_probe(struct i2c_client *client,
467 const struct i2c_device_id *id)
469 const struct tps_info *info = (void *)id->driver_data;
470 struct regulator_init_data *init_data;
471 struct regulator_dev *rdev;
472 struct tps_pmic *tps;
473 int i;
474 int error;
476 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
477 return -EIO;
480 * init_data points to array of regulator_init structures
481 * coming from the board-evm file.
483 init_data = client->dev.platform_data;
484 if (!init_data)
485 return -EIO;
487 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
488 if (!tps)
489 return -ENOMEM;
491 mutex_init(&tps->io_lock);
493 /* common for all regulators */
494 tps->client = client;
496 for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
497 /* Store regulator specific information */
498 tps->info[i] = info;
500 tps->desc[i].name = info->name;
501 tps->desc[i].id = i;
502 tps->desc[i].n_voltages = num_voltages[i];
503 tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
504 &tps65023_ldo_ops : &tps65023_dcdc_ops);
505 tps->desc[i].type = REGULATOR_VOLTAGE;
506 tps->desc[i].owner = THIS_MODULE;
508 /* Register the regulators */
509 rdev = regulator_register(&tps->desc[i], &client->dev,
510 init_data, tps);
511 if (IS_ERR(rdev)) {
512 dev_err(&client->dev, "failed to register %s\n",
513 id->name);
514 error = PTR_ERR(rdev);
515 goto fail;
518 /* Save regulator for cleanup */
519 tps->rdev[i] = rdev;
522 i2c_set_clientdata(client, tps);
524 return 0;
526 fail:
527 while (--i >= 0)
528 regulator_unregister(tps->rdev[i]);
530 kfree(tps);
531 return error;
535 * tps_65023_remove - TPS65023 driver i2c remove handler
536 * @client: i2c driver client device structure
538 * Unregister TPS driver as an i2c client device driver
540 static int __devexit tps_65023_remove(struct i2c_client *client)
542 struct tps_pmic *tps = i2c_get_clientdata(client);
543 int i;
545 for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
546 regulator_unregister(tps->rdev[i]);
548 kfree(tps);
550 return 0;
553 static const struct tps_info tps65023_regs[] = {
555 .name = "VDCDC1",
556 .min_uV = 800000,
557 .max_uV = 1600000,
558 .table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
559 .table = VDCDC1_VSEL_table,
562 .name = "VDCDC2",
563 .min_uV = 3300000,
564 .max_uV = 3300000,
565 .fixed = 1,
568 .name = "VDCDC3",
569 .min_uV = 1800000,
570 .max_uV = 1800000,
571 .fixed = 1,
574 .name = "LDO1",
575 .min_uV = 1000000,
576 .max_uV = 3150000,
577 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
578 .table = LDO1_VSEL_table,
581 .name = "LDO2",
582 .min_uV = 1050000,
583 .max_uV = 3300000,
584 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
585 .table = LDO2_VSEL_table,
589 static const struct i2c_device_id tps_65023_id[] = {
590 {.name = "tps65023",
591 .driver_data = (unsigned long) tps65023_regs,},
592 {.name = "tps65021",
593 .driver_data = (unsigned long) tps65023_regs,},
594 { },
597 MODULE_DEVICE_TABLE(i2c, tps_65023_id);
599 static struct i2c_driver tps_65023_i2c_driver = {
600 .driver = {
601 .name = "tps65023",
602 .owner = THIS_MODULE,
604 .probe = tps_65023_probe,
605 .remove = __devexit_p(tps_65023_remove),
606 .id_table = tps_65023_id,
610 * tps_65023_init
612 * Module init function
614 static int __init tps_65023_init(void)
616 return i2c_add_driver(&tps_65023_i2c_driver);
618 subsys_initcall(tps_65023_init);
621 * tps_65023_cleanup
623 * Module exit function
625 static void __exit tps_65023_cleanup(void)
627 i2c_del_driver(&tps_65023_i2c_driver);
629 module_exit(tps_65023_cleanup);
631 MODULE_AUTHOR("Texas Instruments");
632 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
633 MODULE_LICENSE("GPL v2");