regulator: tps51632: add support for shutdown functionality
[linux-2.6.git] / drivers / regulator / tps51632-regulator.c
1 /*
2  * tps51632-regulator.c -- TI 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         int shutdown_complete;
85         struct mutex mutex;
86 };
87
88 static int tps51632_dcdc_get_voltage_sel(struct regulator_dev *rdev)
89 {
90         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
91         unsigned int data;
92         int ret;
93         unsigned int reg = TPS51632_VOLTAGE_SELECT_REG;
94
95         mutex_lock(&tps->mutex);
96         if (tps && tps->shutdown_complete) {
97                 mutex_unlock(&tps->mutex);
98                 return -ENODEV;
99         }
100         if (tps->pwm_enabled)
101                 reg = TPS51632_VOLTAGE_BASE_REG;
102         ret = regmap_read(tps->regmap, reg, &data);
103         if (ret < 0) {
104                 dev_err(tps->dev, "reg read failed, err %d\n", ret);
105                 mutex_unlock(&tps->mutex);
106                 return ret;
107         }
108         mutex_unlock(&tps->mutex);
109         return data & TPS51632_VOUT_MASK;
110 }
111
112 static int tps51632_dcdc_set_voltage(struct regulator_dev *rdev,
113              int min_uV, int max_uV, unsigned *selector)
114 {
115         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
116         unsigned int reg = TPS51632_VOLTAGE_SELECT_REG;
117         int vsel;
118         int ret;
119
120         mutex_lock(&tps->mutex);
121         if (tps && tps->shutdown_complete) {
122                 mutex_unlock(&tps->mutex);
123                 return -ENODEV;
124         }
125
126         if ((max_uV < min_uV) || (max_uV < TPS51632_MIN_VOLATGE) ||
127                         (min_uV > TPS51632_MAX_VOLATGE)) {
128                 mutex_unlock(&tps->mutex);
129                 return -EINVAL;
130         }
131
132         vsel = DIV_ROUND_UP(min_uV - TPS51632_MIN_VOLATGE,
133                         TPS51632_VOLATGE_STEP) + 0x19;
134         if (selector)
135                 *selector = (vsel & TPS51632_VOUT_MASK);
136
137         if (tps->pwm_enabled)
138                 reg = TPS51632_VOLTAGE_BASE_REG;
139
140         ret = regmap_write(tps->regmap, reg, vsel);
141         if (ret < 0)
142                 dev_err(tps->dev, "reg write failed, err %d\n", ret);
143         mutex_unlock(&tps->mutex);
144         return ret;
145 }
146
147 static int tps51632_dcdc_list_voltage(struct regulator_dev *rdev,
148                                         unsigned selector)
149 {
150         if (selector > TPS51632_MAX_SEL)
151                 return -EINVAL;
152
153         return TPS51632_MIN_VOLATGE + (selector - 0x19) * TPS51632_VOLATGE_STEP;
154 }
155
156 static int tps51632_dcdc_set_voltage_time_sel(struct regulator_dev *rdev,
157                 unsigned int old_selector, unsigned int new_selector)
158 {
159         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
160         int old_uV, new_uV;
161
162         old_uV = tps51632_dcdc_list_voltage(rdev, old_selector);
163         if (old_uV < 0)
164                 return old_uV;
165
166         new_uV = tps51632_dcdc_list_voltage(rdev, new_selector);
167         if (new_uV < 0)
168                 return new_uV;
169
170         return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
171 }
172
173 static int tps51632_dcdc_set_control_mode(struct regulator_dev *rdev,
174                 unsigned int mode)
175 {
176         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
177         int ret;
178
179         mutex_lock(&tps->mutex);
180         if (tps && tps->shutdown_complete) {
181                 mutex_unlock(&tps->mutex);
182                 return -ENODEV;
183         }
184         if (mode == REGULATOR_CONTROL_MODE_I2C)
185              ret = regmap_update_bits(tps->regmap,
186                         TPS51632_DVFS_CONTROL_REG, TPS51632_DVFS_PWMEN, 0);
187         else if (mode == REGULATOR_CONTROL_MODE_PWM)
188               ret = regmap_update_bits(tps->regmap,
189                         TPS51632_DVFS_CONTROL_REG, TPS51632_DVFS_PWMEN,
190                         TPS51632_DVFS_PWMEN);
191         else {
192                 mutex_unlock(&tps->mutex);
193                 return -EINVAL;
194         }
195         if (ret < 0) {
196                 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
197                 mutex_unlock(&tps->mutex);
198                 return ret;
199         }
200         tps->pwm_enabled = (mode == REGULATOR_CONTROL_MODE_PWM);
201         mutex_unlock(&tps->mutex);
202         return ret;
203 }
204
205 static unsigned int tps51632_dcdc_get_control_mode(struct regulator_dev *rdev)
206 {
207         struct tps51632_chip *tps = rdev_get_drvdata(rdev);
208
209         if (tps->pwm_enabled)
210                 return REGULATOR_CONTROL_MODE_PWM;
211         return REGULATOR_CONTROL_MODE_I2C;
212 }
213
214
215 static struct regulator_ops tps51632_dcdc_ops = {
216         .get_voltage_sel        = tps51632_dcdc_get_voltage_sel,
217         .set_voltage            = tps51632_dcdc_set_voltage,
218         .list_voltage           = tps51632_dcdc_list_voltage,
219         .set_voltage_time_sel   = tps51632_dcdc_set_voltage_time_sel,
220         .set_control_mode       = tps51632_dcdc_set_control_mode,
221         .get_control_mode       = tps51632_dcdc_get_control_mode,
222 };
223
224 static int __devinit tps51632_init_dcdc(struct tps51632_chip *tps,
225                 struct tps51632_regulator_platform_data *pdata)
226 {
227         int ret;
228         uint8_t control = 0;
229         int vsel;
230         unsigned int vmax;
231
232         mutex_lock(&tps->mutex);
233         if (tps && tps->shutdown_complete) {
234                 mutex_unlock(&tps->mutex);
235                 return -ENODEV;
236         }
237         if (pdata->enable_pwm) {
238                 control |= TPS51632_DVFS_PWMEN;
239                 tps->pwm_enabled = pdata->enable_pwm;
240         }
241         vsel = DIV_ROUND_UP(pdata->base_voltage_uV -
242                 TPS51632_MIN_VOLATGE, TPS51632_VOLATGE_STEP) + 0x19;
243         ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
244         if (ret < 0) {
245                 dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
246                 mutex_unlock(&tps->mutex);
247                 return ret;
248         }
249         if (pdata->dvfs_step_20mV)
250                 control |= TPS51632_DVFS_STEP_20;
251         if (pdata->enable_vmax_alarm)
252                 control |= TPS51632_DVFS_VMAX_PG;
253         if (pdata->enable_overcurrent_alram)
254                 control |= TPS51632_DVFS_OCA_EN;
255         if (pdata->max_voltage_uV) {
256                 /**
257                  * TPS51632 hw behavior: VMAX register can be write only
258                  * once as it get locked after first write. The lock get
259                  * reset only when device is power-reset.
260                  * Write register only when lock bit is not enabled.
261                  */
262                 ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
263                 if (ret < 0) {
264                         dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
265                         mutex_unlock(&tps->mutex);
266                         return ret;
267                 }
268                 if (vmax & TPS51632_VMAX_LOCK)
269                         goto skip_vmax_config;
270
271                 vsel = DIV_ROUND_UP(pdata->max_voltage_uV -
272                         TPS51632_MIN_VOLATGE, TPS51632_VOLATGE_STEP) + 0x19;
273                 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG, vsel);
274                 if (ret < 0) {
275                         dev_err(tps->dev, "VMAX write failed, err %d\n", ret);
276                         mutex_unlock(&tps->mutex);
277                         return ret;
278                 }
279         }
280
281 skip_vmax_config:
282         ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
283         if (ret < 0) {
284                 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
285                 mutex_unlock(&tps->mutex);
286                 return ret;
287         }
288
289         tps->change_uv_per_us = max(6000u, pdata->slew_rate_uv_per_us);
290
291         vsel = BIT(tps->change_uv_per_us/6000 - 1);
292
293         ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, vsel);
294         if (ret < 0)
295                 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
296         mutex_unlock(&tps->mutex);
297         return ret;
298 }
299
300 static const struct regmap_config tps51632_regmap_config = {
301         .reg_bits               = 8,
302         .val_bits               = 8,
303         .max_register           = TPS51632_MAX_REG - 1,
304         .cache_type             = REGCACHE_RBTREE,
305 };
306
307 static void tps51632_shutdown(struct i2c_client *client)
308 {
309         struct tps51632_chip *tps = i2c_get_clientdata(client);
310
311         mutex_lock(&tps->mutex);
312         tps->shutdown_complete = 1;
313         mutex_unlock(&tps->mutex);
314
315 }
316 static int __devinit tps51632_probe(struct i2c_client *client,
317                                 const struct i2c_device_id *id)
318 {
319         struct tps51632_regulator_platform_data *pdata;
320         struct regulator_dev *rdev;
321         struct tps51632_chip *tps;
322         int ret;
323
324         pdata = client->dev.platform_data;
325         if (!pdata) {
326                 dev_err(&client->dev, "No Platform data\n");
327                 return -EINVAL;
328         }
329
330         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
331         if (!tps) {
332                 dev_err(&client->dev, "Memory allocation failed\n");
333                 return -ENOMEM;
334         }
335
336         tps->dev = &client->dev;
337         tps->desc.name = id->name;
338         tps->desc.id = 0;
339         tps->desc.ops = &tps51632_dcdc_ops;
340         tps->desc.type = REGULATOR_VOLTAGE;
341         tps->desc.owner = THIS_MODULE;
342         tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
343         if (IS_ERR(tps->regmap)) {
344                 ret = PTR_ERR(tps->regmap);
345                 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
346                 return ret;
347         }
348         i2c_set_clientdata(client, tps);
349
350         mutex_init(&tps->mutex);
351         ret = tps51632_init_dcdc(tps, pdata);
352         if (ret < 0) {
353                 dev_err(tps->dev, "Init failed, err = %d\n", ret);
354                 return ret;
355         }
356
357         /* Register the regulators */
358         rdev = regulator_register(&tps->desc, &client->dev,
359                         pdata->reg_init_data, tps, NULL);
360         if (IS_ERR(rdev)) {
361                 dev_err(tps->dev, "regulator register failed\n");
362                 return PTR_ERR(rdev);
363         }
364
365         tps->shutdown_complete = 0;
366         tps->rdev = rdev;
367         return 0;
368 }
369
370 static int __devexit tps51632_remove(struct i2c_client *client)
371 {
372         struct tps51632_chip *tps = i2c_get_clientdata(client);
373
374         mutex_destroy(&tps->mutex);
375         regulator_unregister(tps->rdev);
376         return 0;
377 }
378
379 static const struct i2c_device_id tps51632_id[] = {
380         {.name = "tps51632",},
381         {},
382 };
383
384 MODULE_DEVICE_TABLE(i2c, tps51632_id);
385
386 static struct i2c_driver tps51632_i2c_driver = {
387         .driver = {
388                 .name = "tps51632",
389                 .owner = THIS_MODULE,
390         },
391         .probe = tps51632_probe,
392         .shutdown = tps51632_shutdown,
393         .remove = __devexit_p(tps51632_remove),
394         .id_table = tps51632_id,
395 };
396
397 static int __init tps51632_init(void)
398 {
399         return i2c_add_driver(&tps51632_i2c_driver);
400 }
401 subsys_initcall(tps51632_init);
402
403 static void __exit tps51632_cleanup(void)
404 {
405         i2c_del_driver(&tps51632_i2c_driver);
406 }
407 module_exit(tps51632_cleanup);
408
409 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
410 MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
411 MODULE_LICENSE("GPL v2");