blob: 7baff2e8765da9da25d4873c9f6fabcf03dfd327 [file] [log] [blame]
Venu Byravarasu452534e2012-03-22 18:34:09 +05301/*
2 * Regulator driver for tps65090 power management chip.
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 */
18
19#include <linux/module.h>
20#include <linux/delay.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/err.h>
24#include <linux/platform_device.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27#include <linux/mfd/tps65090.h>
28#include <linux/regulator/tps65090-regulator.h>
29
30struct tps65090_regulator {
31 int id;
32 /* Regulator register address.*/
33 u8 reg_en_reg;
34 u8 en_bit;
35
36 /* used by regulator core */
37 struct regulator_desc desc;
38
39 /* Device */
40 struct device *dev;
41};
42
43static inline struct device *to_tps65090_dev(struct regulator_dev *rdev)
44{
45 return rdev_get_dev(rdev)->parent->parent;
46}
47
48static int tps65090_reg_is_enabled(struct regulator_dev *rdev)
49{
50 struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
51 struct device *parent = to_tps65090_dev(rdev);
52 uint8_t control;
53 int ret;
54
55 ret = tps65090_read(parent, ri->reg_en_reg, &control);
56 if (ret < 0) {
57 dev_err(&rdev->dev, "Error in reading reg 0x%x\n",
58 ri->reg_en_reg);
59 return ret;
60 }
61 return (((control >> ri->en_bit) & 1) == 1);
62}
63
64static int tps65090_reg_enable(struct regulator_dev *rdev)
65{
66 struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
67 struct device *parent = to_tps65090_dev(rdev);
68 int ret;
69
70 ret = tps65090_set_bits(parent, ri->reg_en_reg, ri->en_bit);
71 if (ret < 0)
72 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
73 ri->reg_en_reg);
74 return ret;
75}
76
77static int tps65090_reg_disable(struct regulator_dev *rdev)
78{
79 struct tps65090_regulator *ri = rdev_get_drvdata(rdev);
80 struct device *parent = to_tps65090_dev(rdev);
81 int ret;
82
83 ret = tps65090_clr_bits(parent, ri->reg_en_reg, ri->en_bit);
84 if (ret < 0)
85 dev_err(&rdev->dev, "Error in updating reg 0x%x\n",
86 ri->reg_en_reg);
87
88 return ret;
89}
90
91static struct regulator_ops tps65090_ops = {
92 .enable = tps65090_reg_enable,
93 .disable = tps65090_reg_disable,
94 .is_enabled = tps65090_reg_is_enabled,
95};
96
Axel Lin4b3bd552012-03-24 10:57:53 +080097#define tps65090_REG(_id) \
Venu Byravarasu452534e2012-03-22 18:34:09 +053098{ \
Axel Lin4b3bd552012-03-24 10:57:53 +080099 .reg_en_reg = (TPS65090_ID_##_id) + 12, \
100 .en_bit = 0, \
Venu Byravarasu452534e2012-03-22 18:34:09 +0530101 .id = TPS65090_ID_##_id, \
102 .desc = { \
103 .name = tps65090_rails(_id), \
104 .id = TPS65090_ID_##_id, \
Axel Lin4b3bd552012-03-24 10:57:53 +0800105 .ops = &tps65090_ops, \
Venu Byravarasu452534e2012-03-22 18:34:09 +0530106 .type = REGULATOR_VOLTAGE, \
107 .owner = THIS_MODULE, \
108 }, \
109}
110
111static struct tps65090_regulator TPS65090_regulator[] = {
Axel Lin4b3bd552012-03-24 10:57:53 +0800112 tps65090_REG(DCDC1),
113 tps65090_REG(DCDC2),
114 tps65090_REG(DCDC3),
115 tps65090_REG(FET1),
116 tps65090_REG(FET2),
117 tps65090_REG(FET3),
118 tps65090_REG(FET4),
119 tps65090_REG(FET5),
120 tps65090_REG(FET6),
121 tps65090_REG(FET7),
Venu Byravarasu452534e2012-03-22 18:34:09 +0530122};
123
124static inline struct tps65090_regulator *find_regulator_info(int id)
125{
126 struct tps65090_regulator *ri;
127 int i;
128
129 for (i = 0; i < ARRAY_SIZE(TPS65090_regulator); i++) {
130 ri = &TPS65090_regulator[i];
131 if (ri->desc.id == id)
132 return ri;
133 }
134 return NULL;
135}
136
137static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
138{
139 struct tps65090_regulator *ri = NULL;
140 struct regulator_dev *rdev;
141 struct tps65090_regulator_platform_data *tps_pdata;
142 int id = pdev->id;
143
144 dev_dbg(&pdev->dev, "Probing regulator %d\n", id);
145
146 ri = find_regulator_info(id);
147 if (ri == NULL) {
148 dev_err(&pdev->dev, "invalid regulator ID specified\n");
149 return -EINVAL;
150 }
151 tps_pdata = pdev->dev.platform_data;
152 ri->dev = &pdev->dev;
153
154 rdev = regulator_register(&ri->desc, &pdev->dev,
155 &tps_pdata->regulator, ri, NULL);
Axel Lin0ca2d6e2012-03-24 10:56:00 +0800156 if (IS_ERR(rdev)) {
Venu Byravarasu452534e2012-03-22 18:34:09 +0530157 dev_err(&pdev->dev, "failed to register regulator %s\n",
158 ri->desc.name);
159 return PTR_ERR(rdev);
160 }
161
162 platform_set_drvdata(pdev, rdev);
163 return 0;
164}
165
166static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
167{
168 struct regulator_dev *rdev = platform_get_drvdata(pdev);
169
170 regulator_unregister(rdev);
171 return 0;
172}
173
174static struct platform_driver tps65090_regulator_driver = {
175 .driver = {
176 .name = "tps65090-regulator",
177 .owner = THIS_MODULE,
178 },
179 .probe = tps65090_regulator_probe,
180 .remove = __devexit_p(tps65090_regulator_remove),
181};
182
183static int __init tps65090_regulator_init(void)
184{
185 return platform_driver_register(&tps65090_regulator_driver);
186}
187subsys_initcall(tps65090_regulator_init);
188
189static void __exit tps65090_regulator_exit(void)
190{
191 platform_driver_unregister(&tps65090_regulator_driver);
192}
193module_exit(tps65090_regulator_exit);
194
195MODULE_DESCRIPTION("tps65090 regulator driver");
196MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
197MODULE_LICENSE("GPL v2");