regulator: core: support of supply name through desc
[linux-2.6.git] / drivers / regulator / tps51632-regulator.c
1 /*
2  * tps51632-regulator.c -- Maxim tps51632
3  *
4  * Regulator driver for TPS51632 3-2-1 Phase D-Cap Step Down Driverless
5  * Controller with serial VID control and DVFS.
6  *
7  * Copyright (c) 2012, NVIDIA Corporation.
8  *
9  * Author: Laxman Dewangan <ldewangan@nvidia.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation version 2.
14  *
15  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
16  * whether express or implied; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  * 02111-1307, USA
24  */
25
26 #include <linux/err.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/regmap.h>
33 #include <linux/regulator/driver.h>
34 #include <linux/regulator/machine.h>
35 #include <linux/regulator/tps51632-regulator.h>
36 #include <linux/slab.h>
37
38 /* Register definitions */
39 #define TPS51632_VOLTAGE_SELECT_REG             0x0
40 #define TPS51632_VOLTAGE_BASE_REG               0x1
41 #define TPS51632_OFFSET_REG                     0x2
42 #define TPS51632_IMON_REG                       0x3
43 #define TPS51632_VMAX_REG                       0x4
44 #define TPS51632_DVFS_CONTROL_REG               0x5
45 #define TPS51632_POWER_STATE_REG                0x6
46 #define TPS51632_SLEW_REGS                      0x7
47 #define TPS51632_FAULT_REG                      0x14
48
49 #define TPS51632_MAX_REG                        0x15
50
51 #define TPS51632_VOUT_MASK              0x7F
52 #define TPS51632_VOUT_OFFSET_MASK       0x1F
53 #define TPS51632_VMAX_MASK              0x7F
54 #define TPS51632_VMAX_LOCK              0x80
55
56 /* TPS51632_DVFS_CONTROL_REG */
57 #define TPS51632_DVFS_PWMEN             0x1
58 #define TPS51632_DVFS_STEP_20           0x2
59 #define TPS51632_DVFS_VMAX_PG           0x4
60 #define TPS51632_DVFS_PWMRST            0x8
61 #define TPS51632_DVFS_OCA_EN            0x10
62 #define TPS51632_DVFS_FCCM              0x20
63
64 /* TPS51632_POWER_STATE_REG */
65 #define TPS51632_POWER_STATE_MASK               0x03
66 #define TPS51632_POWER_STATE_MULTI_PHASE_CCM    0x0
67 #define TPS51632_POWER_STATE_SINGLE_PHASE_CCM   0x1
68 #define TPS51632_POWER_STATE_SINGLE_PHASE_DCM   0x2
69
70 #define TPS51632_MIN_VOLATGE    500000
71 #define TPS51632_MAX_VOLATGE    1520000
72 #define TPS51632_VOLATGE_STEP   10000
73 #define TPS51632_MAX_SEL        0x7F
74
75 /* TPS51632 chip information */
76 struct tps51632_chip {
77         struct device *dev;
78         struct regulator_desc desc;
79         struct regulator_dev *rdev;
80         struct regmap *regmap;
81
82         bool pwm_enabled;
83         unsigned int change_uv_per_us;
84 };
85
86 static int tps51632_dcdc_get_voltage_sel(struct regulator_dev *rdev)
87 {
88         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
89         unsigned int data;
90         int ret;
91         unsigned int reg = TPS51632_VOLTAGE_SELECT_REG;
92
93         if (tps->pwm_enabled)
94                 reg = TPS51632_VOLTAGE_BASE_REG;
95         ret = regmap_read(tps->regmap, reg, &data);
96         if (ret < 0) {
97                 dev_err(tps->dev, "reg read failed, err %d\n", ret);
98                 return ret;
99         }
100         return data & TPS51632_VOUT_MASK;
101 }
102
103 static int tps51632_dcdc_set_voltage(struct regulator_dev *rdev,
104              int min_uV, int max_uV, unsigned *selector)
105 {
106         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
107         int vsel;
108         int ret;
109
110         if ((max_uV < min_uV) || (max_uV < TPS51632_MIN_VOLATGE) ||
111                         (min_uV > TPS51632_MAX_VOLATGE))
112                 return -EINVAL;
113
114         vsel = DIV_ROUND_UP(min_uV - TPS51632_MIN_VOLATGE,
115                         TPS51632_VOLATGE_STEP) + 0x19;
116         if (selector)
117                 *selector = (vsel & TPS51632_VOUT_MASK);
118
119         ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_SELECT_REG, vsel);
120         if (ret < 0)
121                 dev_err(tps->dev, "reg write failed, err %d\n", ret);
122         return ret;
123 }
124
125 static int tps51632_dcdc_list_voltage(struct regulator_dev *rdev,
126                                         unsigned selector)
127 {
128         if (selector > TPS51632_MAX_SEL)
129                 return -EINVAL;
130
131         return TPS51632_MIN_VOLATGE + (selector - 0x19) * TPS51632_VOLATGE_STEP;
132 }
133
134 static int tps51632_dcdc_set_voltage_time_sel(struct regulator_dev *rdev,
135                 unsigned int old_selector, unsigned int new_selector)
136 {
137         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
138         int old_uV, new_uV;
139
140         old_uV = tps51632_dcdc_list_voltage(rdev, old_selector);
141         if (old_uV < 0)
142                 return old_uV;
143
144         new_uV = tps51632_dcdc_list_voltage(rdev, new_selector);
145         if (new_uV < 0)
146                 return new_uV;
147
148         return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
149 }
150
151 static struct regulator_ops tps51632_dcdc_ops = {
152         .get_voltage_sel        = tps51632_dcdc_get_voltage_sel,
153         .set_voltage            = tps51632_dcdc_set_voltage,
154         .list_voltage           = tps51632_dcdc_list_voltage,
155         .set_voltage_time_sel   = tps51632_dcdc_set_voltage_time_sel,
156 };
157
158 static int __devinit tps51632_init_dcdc(struct tps51632_chip *tps,
159                 struct tps51632_regulator_platform_data *pdata)
160 {
161         int ret;
162         uint8_t control = 0;
163         int vsel;
164
165         if (pdata->enable_pwm) {
166                 control = TPS51632_DVFS_PWMEN;
167                 tps->pwm_enabled = pdata->enable_pwm;
168                 vsel = DIV_ROUND_UP(pdata->base_voltage_uV -
169                         TPS51632_MIN_VOLATGE, TPS51632_VOLATGE_STEP) + 0x19;
170                 ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG,
171                                                         vsel);
172                 if (ret < 0) {
173                         dev_err(tps->dev, "BASE reg write failed, err %d\n",
174                                         ret);
175                         return ret;
176                 }
177         }
178         if (pdata->dvfs_step_20mV)
179                 control = TPS51632_DVFS_STEP_20;
180         if (pdata->enable_vmax_alarm)
181                 control = TPS51632_DVFS_VMAX_PG;
182         if (pdata->enable_overcurrent_alram)
183                 control = TPS51632_DVFS_OCA_EN;
184         if (pdata->max_voltage_uV) {
185                 vsel = DIV_ROUND_UP(pdata->max_voltage_uV -
186                         TPS51632_MIN_VOLATGE, TPS51632_VOLATGE_STEP) + 0x19;
187                 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG, vsel);
188                 if (ret < 0) {
189                         dev_err(tps->dev, "VMAX write failed, err %d\n", ret);
190                         return ret;
191                 }
192         }
193         ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
194         if (ret < 0) {
195                 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
196                 return ret;
197         }
198
199         tps->change_uv_per_us = max(6000u, pdata->slew_rate_uv_per_us);
200
201         vsel = BIT(tps->change_uv_per_us/6000 - 1);
202
203         ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, vsel);
204         if (ret < 0)
205                 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
206         return ret;
207 }
208
209 static const struct regmap_config tps51632_regmap_config = {
210         .reg_bits               = 8,
211         .val_bits               = 8,
212         .max_register           = TPS51632_MAX_REG - 1,
213         .cache_type             = REGCACHE_RBTREE,
214 };
215
216 static int __devinit tps51632_probe(struct i2c_client *client,
217                                 const struct i2c_device_id *id)
218 {
219         struct tps51632_regulator_platform_data *pdata;
220         struct regulator_dev *rdev;
221         struct tps51632_chip *tps;
222         int ret;
223
224         pdata = client->dev.platform_data;
225         if (!pdata) {
226                 dev_err(&client->dev, "No Platform data\n");
227                 return -EINVAL;
228         }
229
230         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
231         if (!tps) {
232                 dev_err(&client->dev, "Memory allocation failed\n");
233                 return -ENOMEM;
234         }
235
236         tps->dev = &client->dev;
237         tps->desc.name = id->name;
238         tps->desc.id = 0;
239         tps->desc.ops = &tps51632_dcdc_ops;
240         tps->desc.type = REGULATOR_VOLTAGE;
241         tps->desc.owner = THIS_MODULE;
242         tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
243         if (IS_ERR(tps->regmap)) {
244                 ret = PTR_ERR(tps->regmap);
245                 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
246                 return ret;
247         }
248         i2c_set_clientdata(client, tps);
249
250         ret = tps51632_init_dcdc(tps, pdata);
251         if (ret < 0) {
252                 dev_err(tps->dev, "Init failed, err = %d\n", ret);
253                 return ret;
254         }
255
256         /* Register the regulators */
257         rdev = regulator_register(&tps->desc, &client->dev,
258                         pdata->reg_init_data, tps, NULL);
259         if (IS_ERR(rdev)) {
260                 dev_err(tps->dev, "regulator register failed\n");
261                 return PTR_ERR(rdev);
262         }
263
264         tps->rdev = rdev;
265         return 0;
266 }
267
268 static int __devexit tps51632_remove(struct i2c_client *client)
269 {
270         struct tps51632_chip *tps = i2c_get_clientdata(client);
271
272         regulator_unregister(tps->rdev);
273         return 0;
274 }
275
276 static const struct i2c_device_id tps51632_id[] = {
277         {.name = "tps51632",},
278         {},
279 };
280
281 MODULE_DEVICE_TABLE(i2c, tps51632_id);
282
283 static struct i2c_driver tps51632_i2c_driver = {
284         .driver = {
285                 .name = "tps51632",
286                 .owner = THIS_MODULE,
287         },
288         .probe = tps51632_probe,
289         .remove = __devexit_p(tps51632_remove),
290         .id_table = tps51632_id,
291 };
292
293 static int __init tps51632_init(void)
294 {
295         return i2c_add_driver(&tps51632_i2c_driver);
296 }
297 subsys_initcall(tps51632_init);
298
299 static void __exit tps51632_cleanup(void)
300 {
301         i2c_del_driver(&tps51632_i2c_driver);
302 }
303 module_exit(tps51632_cleanup);
304
305 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
306 MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
307 MODULE_LICENSE("GPL v2");