x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / drivers / regulator / tps65023-regulator.c
blob07fda0a75adf4aa0ef9ae012fea8a1fb50cb97f4
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>
28 /* Register definitions */
29 #define TPS65023_REG_VERSION 0
30 #define TPS65023_REG_PGOODZ 1
31 #define TPS65023_REG_MASK 2
32 #define TPS65023_REG_REG_CTRL 3
33 #define TPS65023_REG_CON_CTRL 4
34 #define TPS65023_REG_CON_CTRL2 5
35 #define TPS65023_REG_DEF_CORE 6
36 #define TPS65023_REG_DEFSLEW 7
37 #define TPS65023_REG_LDO_CTRL 8
39 /* PGOODZ bitfields */
40 #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
41 #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
42 #define TPS65023_PGOODZ_VDCDC1 BIT(5)
43 #define TPS65023_PGOODZ_VDCDC2 BIT(4)
44 #define TPS65023_PGOODZ_VDCDC3 BIT(3)
45 #define TPS65023_PGOODZ_LDO2 BIT(2)
46 #define TPS65023_PGOODZ_LDO1 BIT(1)
48 /* MASK bitfields */
49 #define TPS65023_MASK_PWRFAILZ BIT(7)
50 #define TPS65023_MASK_LOWBATTZ BIT(6)
51 #define TPS65023_MASK_VDCDC1 BIT(5)
52 #define TPS65023_MASK_VDCDC2 BIT(4)
53 #define TPS65023_MASK_VDCDC3 BIT(3)
54 #define TPS65023_MASK_LDO2 BIT(2)
55 #define TPS65023_MASK_LDO1 BIT(1)
57 /* REG_CTRL bitfields */
58 #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
59 #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
60 #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
61 #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
62 #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
64 /* LDO_CTRL bitfields */
65 #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
66 #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
68 /* Number of step-down converters available */
69 #define TPS65023_NUM_DCDC 3
70 /* Number of LDO voltage regulators available */
71 #define TPS65023_NUM_LDO 2
72 /* Number of total regulators available */
73 #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
75 /* DCDCs */
76 #define TPS65023_DCDC_1 0
77 #define TPS65023_DCDC_2 1
78 #define TPS65023_DCDC_3 2
79 /* LDOs */
80 #define TPS65023_LDO_1 3
81 #define TPS65023_LDO_2 4
83 #define TPS65023_MAX_REG_ID TPS65023_LDO_2
85 /* Supported voltage values for regulators */
86 static const u16 VDCDC1_VSEL_table[] = {
87 800, 825, 850, 875,
88 900, 925, 950, 975,
89 1000, 1025, 1050, 1075,
90 1100, 1125, 1150, 1175,
91 1200, 1225, 1250, 1275,
92 1300, 1325, 1350, 1375,
93 1400, 1425, 1450, 1475,
94 1500, 1525, 1550, 1600,
97 static const u16 LDO1_VSEL_table[] = {
98 1000, 1100, 1300, 1800,
99 2200, 2600, 2800, 3150,
102 static const u16 LDO2_VSEL_table[] = {
103 1050, 1200, 1300, 1800,
104 2500, 2800, 3000, 3300,
107 static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
108 0, 0, ARRAY_SIZE(LDO1_VSEL_table),
109 ARRAY_SIZE(LDO2_VSEL_table)};
111 /* Regulator specific details */
112 struct tps_info {
113 const char *name;
114 unsigned min_uV;
115 unsigned max_uV;
116 bool fixed;
117 u8 table_len;
118 const u16 *table;
121 /* PMIC details */
122 struct tps_pmic {
123 struct regulator_desc desc[TPS65023_NUM_REGULATOR];
124 struct i2c_client *client;
125 struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
126 const struct tps_info *info[TPS65023_NUM_REGULATOR];
127 struct mutex io_lock;
130 static inline int tps_65023_read(struct tps_pmic *tps, u8 reg)
132 return i2c_smbus_read_byte_data(tps->client, reg);
135 static inline int tps_65023_write(struct tps_pmic *tps, u8 reg, u8 val)
137 return i2c_smbus_write_byte_data(tps->client, reg, val);
140 static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
142 int err, data;
144 mutex_lock(&tps->io_lock);
146 data = tps_65023_read(tps, reg);
147 if (data < 0) {
148 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
149 err = data;
150 goto out;
153 data |= mask;
154 err = tps_65023_write(tps, reg, data);
155 if (err)
156 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
158 out:
159 mutex_unlock(&tps->io_lock);
160 return err;
163 static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
165 int err, data;
167 mutex_lock(&tps->io_lock);
169 data = tps_65023_read(tps, reg);
170 if (data < 0) {
171 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
172 err = data;
173 goto out;
176 data &= ~mask;
178 err = tps_65023_write(tps, reg, data);
179 if (err)
180 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
182 out:
183 mutex_unlock(&tps->io_lock);
184 return err;
188 static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
190 int data;
192 mutex_lock(&tps->io_lock);
194 data = tps_65023_read(tps, reg);
195 if (data < 0)
196 dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);
198 mutex_unlock(&tps->io_lock);
199 return data;
202 static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
204 int err;
206 mutex_lock(&tps->io_lock);
208 err = tps_65023_write(tps, reg, val);
209 if (err < 0)
210 dev_err(&tps->client->dev, "Write for reg 0x%x failed\n", reg);
212 mutex_unlock(&tps->io_lock);
213 return err;
216 static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
218 struct tps_pmic *tps = rdev_get_drvdata(dev);
219 int data, dcdc = rdev_get_id(dev);
220 u8 shift;
222 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
223 return -EINVAL;
225 shift = TPS65023_NUM_REGULATOR - dcdc;
226 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
228 if (data < 0)
229 return data;
230 else
231 return (data & 1<<shift) ? 1 : 0;
234 static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
236 struct tps_pmic *tps = rdev_get_drvdata(dev);
237 int data, ldo = rdev_get_id(dev);
238 u8 shift;
240 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
241 return -EINVAL;
243 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
244 data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
246 if (data < 0)
247 return data;
248 else
249 return (data & 1<<shift) ? 1 : 0;
252 static int tps65023_dcdc_enable(struct regulator_dev *dev)
254 struct tps_pmic *tps = rdev_get_drvdata(dev);
255 int dcdc = rdev_get_id(dev);
256 u8 shift;
258 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
259 return -EINVAL;
261 shift = TPS65023_NUM_REGULATOR - dcdc;
262 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
265 static int tps65023_dcdc_disable(struct regulator_dev *dev)
267 struct tps_pmic *tps = rdev_get_drvdata(dev);
268 int dcdc = rdev_get_id(dev);
269 u8 shift;
271 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
272 return -EINVAL;
274 shift = TPS65023_NUM_REGULATOR - dcdc;
275 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
278 static int tps65023_ldo_enable(struct regulator_dev *dev)
280 struct tps_pmic *tps = rdev_get_drvdata(dev);
281 int ldo = rdev_get_id(dev);
282 u8 shift;
284 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
285 return -EINVAL;
287 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
288 return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
291 static int tps65023_ldo_disable(struct regulator_dev *dev)
293 struct tps_pmic *tps = rdev_get_drvdata(dev);
294 int ldo = rdev_get_id(dev);
295 u8 shift;
297 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
298 return -EINVAL;
300 shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
301 return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
304 static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
306 struct tps_pmic *tps = rdev_get_drvdata(dev);
307 int data, dcdc = rdev_get_id(dev);
309 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
310 return -EINVAL;
312 if (dcdc == TPS65023_DCDC_1) {
313 data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
314 if (data < 0)
315 return data;
316 data &= (tps->info[dcdc]->table_len - 1);
317 return tps->info[dcdc]->table[data] * 1000;
318 } else
319 return tps->info[dcdc]->min_uV;
322 static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
323 int min_uV, int max_uV)
325 struct tps_pmic *tps = rdev_get_drvdata(dev);
326 int dcdc = rdev_get_id(dev);
327 int vsel;
329 if (dcdc != TPS65023_DCDC_1)
330 return -EINVAL;
332 if (min_uV < tps->info[dcdc]->min_uV
333 || min_uV > tps->info[dcdc]->max_uV)
334 return -EINVAL;
335 if (max_uV < tps->info[dcdc]->min_uV
336 || max_uV > tps->info[dcdc]->max_uV)
337 return -EINVAL;
339 for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
340 int mV = tps->info[dcdc]->table[vsel];
341 int uV = mV * 1000;
343 /* Break at the first in-range value */
344 if (min_uV <= uV && uV <= max_uV)
345 break;
348 /* write to the register in case we found a match */
349 if (vsel == tps->info[dcdc]->table_len)
350 return -EINVAL;
351 else
352 return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
355 static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
357 struct tps_pmic *tps = rdev_get_drvdata(dev);
358 int data, ldo = rdev_get_id(dev);
360 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
361 return -EINVAL;
363 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
364 if (data < 0)
365 return data;
367 data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
368 data &= (tps->info[ldo]->table_len - 1);
369 return tps->info[ldo]->table[data] * 1000;
372 static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
373 int min_uV, int max_uV)
375 struct tps_pmic *tps = rdev_get_drvdata(dev);
376 int data, vsel, ldo = rdev_get_id(dev);
378 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
379 return -EINVAL;
381 if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
382 return -EINVAL;
383 if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
384 return -EINVAL;
386 for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
387 int mV = tps->info[ldo]->table[vsel];
388 int uV = mV * 1000;
390 /* Break at the first in-range value */
391 if (min_uV <= uV && uV <= max_uV)
392 break;
395 if (vsel == tps->info[ldo]->table_len)
396 return -EINVAL;
398 data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
399 if (data < 0)
400 return data;
402 data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
403 data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
404 return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
407 static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
408 unsigned selector)
410 struct tps_pmic *tps = rdev_get_drvdata(dev);
411 int dcdc = rdev_get_id(dev);
413 if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
414 return -EINVAL;
416 if (dcdc == TPS65023_DCDC_1) {
417 if (selector >= tps->info[dcdc]->table_len)
418 return -EINVAL;
419 else
420 return tps->info[dcdc]->table[selector] * 1000;
421 } else
422 return tps->info[dcdc]->min_uV;
425 static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
426 unsigned selector)
428 struct tps_pmic *tps = rdev_get_drvdata(dev);
429 int ldo = rdev_get_id(dev);
431 if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
432 return -EINVAL;
434 if (selector >= tps->info[ldo]->table_len)
435 return -EINVAL;
436 else
437 return tps->info[ldo]->table[selector] * 1000;
440 /* Operations permitted on VDCDCx */
441 static struct regulator_ops tps65023_dcdc_ops = {
442 .is_enabled = tps65023_dcdc_is_enabled,
443 .enable = tps65023_dcdc_enable,
444 .disable = tps65023_dcdc_disable,
445 .get_voltage = tps65023_dcdc_get_voltage,
446 .set_voltage = tps65023_dcdc_set_voltage,
447 .list_voltage = tps65023_dcdc_list_voltage,
450 /* Operations permitted on LDOx */
451 static struct regulator_ops tps65023_ldo_ops = {
452 .is_enabled = tps65023_ldo_is_enabled,
453 .enable = tps65023_ldo_enable,
454 .disable = tps65023_ldo_disable,
455 .get_voltage = tps65023_ldo_get_voltage,
456 .set_voltage = tps65023_ldo_set_voltage,
457 .list_voltage = tps65023_ldo_list_voltage,
460 static
461 int tps_65023_probe(struct i2c_client *client, const struct i2c_device_id *id)
463 static int desc_id;
464 const struct tps_info *info = (void *)id->driver_data;
465 struct regulator_init_data *init_data;
466 struct regulator_dev *rdev;
467 struct tps_pmic *tps;
468 int i;
470 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
471 return -EIO;
474 * init_data points to array of regulator_init structures
475 * coming from the board-evm file.
477 init_data = client->dev.platform_data;
479 if (!init_data)
480 return -EIO;
482 tps = kzalloc(sizeof(*tps), GFP_KERNEL);
483 if (!tps)
484 return -ENOMEM;
486 mutex_init(&tps->io_lock);
488 /* common for all regulators */
489 tps->client = client;
491 for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
492 /* Store regulator specific information */
493 tps->info[i] = info;
495 tps->desc[i].name = info->name;
496 tps->desc[i].id = desc_id++;
497 tps->desc[i].n_voltages = num_voltages[i];
498 tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
499 &tps65023_ldo_ops : &tps65023_dcdc_ops);
500 tps->desc[i].type = REGULATOR_VOLTAGE;
501 tps->desc[i].owner = THIS_MODULE;
503 /* Register the regulators */
504 rdev = regulator_register(&tps->desc[i], &client->dev,
505 init_data, tps);
506 if (IS_ERR(rdev)) {
507 dev_err(&client->dev, "failed to register %s\n",
508 id->name);
510 /* Unregister */
511 while (i)
512 regulator_unregister(tps->rdev[--i]);
514 tps->client = NULL;
516 /* clear the client data in i2c */
517 i2c_set_clientdata(client, NULL);
518 kfree(tps);
519 return PTR_ERR(rdev);
522 /* Save regulator for cleanup */
523 tps->rdev[i] = rdev;
526 i2c_set_clientdata(client, tps);
528 return 0;
532 * tps_65023_remove - TPS65023 driver i2c remove handler
533 * @client: i2c driver client device structure
535 * Unregister TPS driver as an i2c client device driver
537 static int __devexit tps_65023_remove(struct i2c_client *client)
539 struct tps_pmic *tps = i2c_get_clientdata(client);
540 int i;
542 for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
543 regulator_unregister(tps->rdev[i]);
545 tps->client = NULL;
547 /* clear the client data in i2c */
548 i2c_set_clientdata(client, NULL);
549 kfree(tps);
551 return 0;
554 static const struct tps_info tps65023_regs[] = {
556 .name = "VDCDC1",
557 .min_uV = 800000,
558 .max_uV = 1600000,
559 .table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
560 .table = VDCDC1_VSEL_table,
563 .name = "VDCDC2",
564 .min_uV = 3300000,
565 .max_uV = 3300000,
566 .fixed = 1,
569 .name = "VDCDC3",
570 .min_uV = 1800000,
571 .max_uV = 1800000,
572 .fixed = 1,
575 .name = "LDO1",
576 .min_uV = 1000000,
577 .max_uV = 3150000,
578 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
579 .table = LDO1_VSEL_table,
582 .name = "LDO2",
583 .min_uV = 1050000,
584 .max_uV = 3300000,
585 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
586 .table = LDO2_VSEL_table,
590 static const struct i2c_device_id tps_65023_id[] = {
591 {.name = "tps65023",
592 .driver_data = (unsigned long) tps65023_regs,},
593 { },
596 MODULE_DEVICE_TABLE(i2c, tps_65023_id);
598 static struct i2c_driver tps_65023_i2c_driver = {
599 .driver = {
600 .name = "tps65023",
601 .owner = THIS_MODULE,
603 .probe = tps_65023_probe,
604 .remove = __devexit_p(tps_65023_remove),
605 .id_table = tps_65023_id,
609 * tps_65023_init
611 * Module init function
613 static int __init tps_65023_init(void)
615 return i2c_add_driver(&tps_65023_i2c_driver);
617 subsys_initcall(tps_65023_init);
620 * tps_65023_cleanup
622 * Module exit function
624 static void __exit tps_65023_cleanup(void)
626 i2c_del_driver(&tps_65023_i2c_driver);
628 module_exit(tps_65023_cleanup);
630 MODULE_AUTHOR("Texas Instruments");
631 MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
632 MODULE_LICENSE("GPL v2");