blob: 7560d0768b774c937ef5503af130b6d68262f581 [file] [log] [blame]
Laxman Dewangan0c570672012-10-06 20:47:46 +05301/*
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>
Laxman Dewanganc51ce402012-12-25 20:36:00 +053031#include <linux/of.h>
32#include <linux/of_device.h>
Laxman Dewangan0c570672012-10-06 20:47:46 +053033#include <linux/platform_device.h>
34#include <linux/regmap.h>
35#include <linux/regulator/driver.h>
36#include <linux/regulator/machine.h>
Laxman Dewanganc51ce402012-12-25 20:36:00 +053037#include <linux/regulator/of_regulator.h>
Laxman Dewangan0c570672012-10-06 20:47:46 +053038#include <linux/regulator/tps51632-regulator.h>
39#include <linux/slab.h>
40
41/* Register definitions */
42#define TPS51632_VOLTAGE_SELECT_REG 0x0
43#define TPS51632_VOLTAGE_BASE_REG 0x1
44#define TPS51632_OFFSET_REG 0x2
45#define TPS51632_IMON_REG 0x3
46#define TPS51632_VMAX_REG 0x4
47#define TPS51632_DVFS_CONTROL_REG 0x5
48#define TPS51632_POWER_STATE_REG 0x6
49#define TPS51632_SLEW_REGS 0x7
50#define TPS51632_FAULT_REG 0x14
51
52#define TPS51632_MAX_REG 0x15
53
54#define TPS51632_VOUT_MASK 0x7F
55#define TPS51632_VOUT_OFFSET_MASK 0x1F
56#define TPS51632_VMAX_MASK 0x7F
57#define TPS51632_VMAX_LOCK 0x80
58
59/* TPS51632_DVFS_CONTROL_REG */
60#define TPS51632_DVFS_PWMEN 0x1
61#define TPS51632_DVFS_STEP_20 0x2
62#define TPS51632_DVFS_VMAX_PG 0x4
63#define TPS51632_DVFS_PWMRST 0x8
64#define TPS51632_DVFS_OCA_EN 0x10
65#define TPS51632_DVFS_FCCM 0x20
66
67/* TPS51632_POWER_STATE_REG */
68#define TPS51632_POWER_STATE_MASK 0x03
69#define TPS51632_POWER_STATE_MULTI_PHASE_CCM 0x0
70#define TPS51632_POWER_STATE_SINGLE_PHASE_CCM 0x1
71#define TPS51632_POWER_STATE_SINGLE_PHASE_DCM 0x2
72
73#define TPS51632_MIN_VOLATGE 500000
74#define TPS51632_MAX_VOLATGE 1520000
75#define TPS51632_VOLATGE_STEP_10mV 10000
76#define TPS51632_VOLATGE_STEP_20mV 20000
77#define TPS51632_MAX_VSEL 0x7F
78#define TPS51632_MIN_VSEL 0x19
79#define TPS51632_DEFAULT_RAMP_DELAY 6000
80#define TPS51632_VOLT_VSEL(uV) \
81 (DIV_ROUND_UP(uV - TPS51632_MIN_VOLATGE, \
82 TPS51632_VOLATGE_STEP_10mV) + \
83 TPS51632_MIN_VSEL)
84
85/* TPS51632 chip information */
86struct tps51632_chip {
87 struct device *dev;
88 struct regulator_desc desc;
89 struct regulator_dev *rdev;
90 struct regmap *regmap;
91 bool enable_pwm_dvfs;
92};
93
94static int tps51632_dcdc_get_voltage_sel(struct regulator_dev *rdev)
95{
96 struct tps51632_chip *tps = rdev_get_drvdata(rdev);
97 unsigned int data;
98 int ret;
99 unsigned int reg = TPS51632_VOLTAGE_SELECT_REG;
100 int vsel;
101
102 if (tps->enable_pwm_dvfs)
103 reg = TPS51632_VOLTAGE_BASE_REG;
104
105 ret = regmap_read(tps->regmap, reg, &data);
106 if (ret < 0) {
107 dev_err(tps->dev, "reg read failed, err %d\n", ret);
108 return ret;
109 }
110
111 vsel = data & TPS51632_VOUT_MASK;
Axel Linbd0ec7c2012-11-28 07:22:54 +0800112 return vsel;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530113}
114
115static int tps51632_dcdc_set_voltage_sel(struct regulator_dev *rdev,
116 unsigned selector)
117{
118 struct tps51632_chip *tps = rdev_get_drvdata(rdev);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530119 int ret;
120 unsigned int reg = TPS51632_VOLTAGE_SELECT_REG;
121
122 if (tps->enable_pwm_dvfs)
123 reg = TPS51632_VOLTAGE_BASE_REG;
124
Axel Linbd0ec7c2012-11-28 07:22:54 +0800125 if (selector > TPS51632_MAX_VSEL)
Laxman Dewangan0c570672012-10-06 20:47:46 +0530126 return -EINVAL;
127
Mark Brown5d6e6ff2012-11-28 19:24:48 +0000128 ret = regmap_write(tps->regmap, reg, selector);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530129 if (ret < 0)
130 dev_err(tps->dev, "reg write failed, err %d\n", ret);
131 return ret;
132}
133
134static int tps51632_dcdc_set_ramp_delay(struct regulator_dev *rdev,
135 int ramp_delay)
136{
137 struct tps51632_chip *tps = rdev_get_drvdata(rdev);
138 int bit = ramp_delay/6000;
139 int ret;
140
141 if (bit)
142 bit--;
143 ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, BIT(bit));
144 if (ret < 0)
145 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
146 return ret;
147}
148
149static struct regulator_ops tps51632_dcdc_ops = {
150 .get_voltage_sel = tps51632_dcdc_get_voltage_sel,
151 .set_voltage_sel = tps51632_dcdc_set_voltage_sel,
152 .list_voltage = regulator_list_voltage_linear,
153 .set_voltage_time_sel = regulator_set_voltage_time_sel,
154 .set_ramp_delay = tps51632_dcdc_set_ramp_delay,
155};
156
Bill Pembertona5023572012-11-19 13:22:22 -0500157static int tps51632_init_dcdc(struct tps51632_chip *tps,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530158 struct tps51632_regulator_platform_data *pdata)
159{
160 int ret;
161 uint8_t control = 0;
162 int vsel;
163
164 if (!pdata->enable_pwm_dvfs)
165 goto skip_pwm_config;
166
167 control |= TPS51632_DVFS_PWMEN;
168 tps->enable_pwm_dvfs = pdata->enable_pwm_dvfs;
169 vsel = TPS51632_VOLT_VSEL(pdata->base_voltage_uV);
170 ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
171 if (ret < 0) {
172 dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
173 return ret;
174 }
175
176 if (pdata->dvfs_step_20mV)
177 control |= TPS51632_DVFS_STEP_20;
178
179 if (pdata->max_voltage_uV) {
180 unsigned int vmax;
181 /**
182 * TPS51632 hw behavior: VMAX register can be write only
183 * once as it get locked after first write. The lock get
184 * reset only when device is power-reset.
185 * Write register only when lock bit is not enabled.
186 */
187 ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
188 if (ret < 0) {
189 dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
190 return ret;
191 }
192 if (!(vmax & TPS51632_VMAX_LOCK)) {
193 vsel = TPS51632_VOLT_VSEL(pdata->max_voltage_uV);
194 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG,
195 vsel);
196 if (ret < 0) {
197 dev_err(tps->dev,
198 "VMAX write failed, err %d\n", ret);
199 return ret;
200 }
201 }
202 }
203
204skip_pwm_config:
205 ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
206 if (ret < 0)
207 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
208 return ret;
209}
210
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530211static bool is_volatile_reg(struct device *dev, unsigned int reg)
Laxman Dewangan0c570672012-10-06 20:47:46 +0530212{
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530213 switch (reg) {
214 case TPS51632_OFFSET_REG:
215 case TPS51632_FAULT_REG:
216 case TPS51632_IMON_REG:
217 return true;
218 default:
Laxman Dewangan0c570672012-10-06 20:47:46 +0530219 return false;
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530220 }
221}
222
223static bool is_read_reg(struct device *dev, unsigned int reg)
224{
225 switch (reg) {
226 case 0x08 ... 0x0F:
227 return false;
228 default:
229 return true;
230 }
231}
232
233static bool is_write_reg(struct device *dev, unsigned int reg)
234{
235 switch (reg) {
236 case TPS51632_VOLTAGE_SELECT_REG:
237 case TPS51632_VOLTAGE_BASE_REG:
238 case TPS51632_VMAX_REG:
239 case TPS51632_DVFS_CONTROL_REG:
240 case TPS51632_POWER_STATE_REG:
241 case TPS51632_SLEW_REGS:
242 return true;
243 default:
244 return false;
245 }
Laxman Dewangan0c570672012-10-06 20:47:46 +0530246}
247
248static const struct regmap_config tps51632_regmap_config = {
249 .reg_bits = 8,
250 .val_bits = 8,
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530251 .writeable_reg = is_write_reg,
252 .readable_reg = is_read_reg,
253 .volatile_reg = is_volatile_reg,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530254 .max_register = TPS51632_MAX_REG - 1,
255 .cache_type = REGCACHE_RBTREE,
256};
257
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530258#if defined(CONFIG_OF)
259static const struct of_device_id tps51632_of_match[] = {
260 { .compatible = "ti,tps51632",},
261 {},
262};
263MODULE_DEVICE_TABLE(of, tps51632_of_match);
264
265static struct tps51632_regulator_platform_data *
266 of_get_tps51632_platform_data(struct device *dev)
267{
268 struct tps51632_regulator_platform_data *pdata;
269 struct device_node *np = dev->of_node;
270
271 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
272 if (!pdata) {
273 dev_err(dev, "Memory alloc failed for platform data\n");
274 return NULL;
275 }
276
277 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node);
278 if (!pdata->reg_init_data) {
279 dev_err(dev, "Not able to get OF regulator init data\n");
280 return NULL;
281 }
282
283 pdata->enable_pwm_dvfs =
284 of_property_read_bool(np, "ti,enable-pwm-dvfs");
285 pdata->dvfs_step_20mV = of_property_read_bool(np, "ti,dvfs-step-20mV");
286
287 pdata->base_voltage_uV = pdata->reg_init_data->constraints.min_uV ? :
288 TPS51632_MIN_VOLATGE;
289 pdata->max_voltage_uV = pdata->reg_init_data->constraints.max_uV ? :
290 TPS51632_MAX_VOLATGE;
291 return pdata;
292}
293#else
294static struct tps51632_regulator_platform_data *
295 of_get_tps51632_platform_data(struct device *dev)
296{
297 return NULL;
298}
299#endif
300
Bill Pembertona5023572012-11-19 13:22:22 -0500301static int tps51632_probe(struct i2c_client *client,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530302 const struct i2c_device_id *id)
303{
304 struct tps51632_regulator_platform_data *pdata;
305 struct regulator_dev *rdev;
306 struct tps51632_chip *tps;
307 int ret;
308 struct regulator_config config = { };
309
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530310 if (client->dev.of_node) {
311 const struct of_device_id *match;
312 match = of_match_device(of_match_ptr(tps51632_of_match),
313 &client->dev);
314 if (!match) {
315 dev_err(&client->dev, "Error: No device match found\n");
316 return -ENODEV;
317 }
318 }
319
Laxman Dewangan0c570672012-10-06 20:47:46 +0530320 pdata = client->dev.platform_data;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530321 if (!pdata && client->dev.of_node)
322 pdata = of_get_tps51632_platform_data(&client->dev);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530323 if (!pdata) {
324 dev_err(&client->dev, "No Platform data\n");
325 return -EINVAL;
326 }
327
Axel Lindbc70512012-11-30 16:52:49 +0800328 if (pdata->enable_pwm_dvfs) {
329 if ((pdata->base_voltage_uV < TPS51632_MIN_VOLATGE) ||
330 (pdata->base_voltage_uV > TPS51632_MAX_VOLATGE)) {
331 dev_err(&client->dev, "Invalid base_voltage_uV setting\n");
332 return -EINVAL;
333 }
334
335 if ((pdata->max_voltage_uV) &&
336 ((pdata->max_voltage_uV < TPS51632_MIN_VOLATGE) ||
337 (pdata->max_voltage_uV > TPS51632_MAX_VOLATGE))) {
338 dev_err(&client->dev, "Invalid max_voltage_uV setting\n");
339 return -EINVAL;
340 }
341 }
342
Laxman Dewangan0c570672012-10-06 20:47:46 +0530343 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
344 if (!tps) {
345 dev_err(&client->dev, "Memory allocation failed\n");
346 return -ENOMEM;
347 }
348
349 tps->dev = &client->dev;
350 tps->desc.name = id->name;
351 tps->desc.id = 0;
352 tps->desc.ramp_delay = TPS51632_DEFAULT_RAMP_DELAY;
353 tps->desc.min_uV = TPS51632_MIN_VOLATGE;
354 tps->desc.uV_step = TPS51632_VOLATGE_STEP_10mV;
Axel Linbd0ec7c2012-11-28 07:22:54 +0800355 tps->desc.linear_min_sel = TPS51632_MIN_VSEL;
356 tps->desc.n_voltages = TPS51632_MAX_VSEL + 1;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530357 tps->desc.ops = &tps51632_dcdc_ops;
358 tps->desc.type = REGULATOR_VOLTAGE;
359 tps->desc.owner = THIS_MODULE;
360
361 tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
362 if (IS_ERR(tps->regmap)) {
363 ret = PTR_ERR(tps->regmap);
364 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
365 return ret;
366 }
367 i2c_set_clientdata(client, tps);
368
369 ret = tps51632_init_dcdc(tps, pdata);
370 if (ret < 0) {
371 dev_err(tps->dev, "Init failed, err = %d\n", ret);
372 return ret;
373 }
374
375 /* Register the regulators */
376 config.dev = &client->dev;
377 config.init_data = pdata->reg_init_data;
378 config.driver_data = tps;
379 config.regmap = tps->regmap;
380 config.of_node = client->dev.of_node;
381
382 rdev = regulator_register(&tps->desc, &config);
383 if (IS_ERR(rdev)) {
384 dev_err(tps->dev, "regulator register failed\n");
385 return PTR_ERR(rdev);
386 }
387
388 tps->rdev = rdev;
389 return 0;
390}
391
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500392static int tps51632_remove(struct i2c_client *client)
Laxman Dewangan0c570672012-10-06 20:47:46 +0530393{
394 struct tps51632_chip *tps = i2c_get_clientdata(client);
395
396 regulator_unregister(tps->rdev);
397 return 0;
398}
399
400static const struct i2c_device_id tps51632_id[] = {
401 {.name = "tps51632",},
402 {},
403};
404
405MODULE_DEVICE_TABLE(i2c, tps51632_id);
406
407static struct i2c_driver tps51632_i2c_driver = {
408 .driver = {
409 .name = "tps51632",
410 .owner = THIS_MODULE,
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530411 .of_match_table = of_match_ptr(tps51632_of_match),
Laxman Dewangan0c570672012-10-06 20:47:46 +0530412 },
413 .probe = tps51632_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500414 .remove = tps51632_remove,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530415 .id_table = tps51632_id,
416};
417
418static int __init tps51632_init(void)
419{
420 return i2c_add_driver(&tps51632_i2c_driver);
421}
422subsys_initcall(tps51632_init);
423
424static void __exit tps51632_cleanup(void)
425{
426 i2c_del_driver(&tps51632_i2c_driver);
427}
428module_exit(tps51632_cleanup);
429
430MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
431MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
432MODULE_LICENSE("GPL v2");