blob: c139890c1514401b38ff02367c67c2475f3d2739 [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
Fabio Estevam9d9339d2013-12-20 11:43:21 -020073#define TPS51632_MIN_VOLTAGE 500000
74#define TPS51632_MAX_VOLTAGE 1520000
75#define TPS51632_VOLTAGE_STEP_10mV 10000
76#define TPS51632_VOLTAGE_STEP_20mV 20000
Laxman Dewangan0c570672012-10-06 20:47:46 +053077#define TPS51632_MAX_VSEL 0x7F
78#define TPS51632_MIN_VSEL 0x19
79#define TPS51632_DEFAULT_RAMP_DELAY 6000
80#define TPS51632_VOLT_VSEL(uV) \
Fabio Estevam9d9339d2013-12-20 11:43:21 -020081 (DIV_ROUND_UP(uV - TPS51632_MIN_VOLTAGE, \
82 TPS51632_VOLTAGE_STEP_10mV) + \
Laxman Dewangan0c570672012-10-06 20:47:46 +053083 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;
Laxman Dewangan0c570672012-10-06 20:47:46 +053091};
92
Laxman Dewangan0c570672012-10-06 20:47:46 +053093static int tps51632_dcdc_set_ramp_delay(struct regulator_dev *rdev,
94 int ramp_delay)
95{
96 struct tps51632_chip *tps = rdev_get_drvdata(rdev);
Axel Linc714a582016-05-31 17:26:00 +080097 int bit;
Laxman Dewangan0c570672012-10-06 20:47:46 +053098 int ret;
99
Axel Linc714a582016-05-31 17:26:00 +0800100 if (ramp_delay == 0)
101 bit = 0;
102 else
103 bit = DIV_ROUND_UP(ramp_delay, 6000) - 1;
104
Laxman Dewangan0c570672012-10-06 20:47:46 +0530105 ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, BIT(bit));
106 if (ret < 0)
107 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
108 return ret;
109}
110
111static struct regulator_ops tps51632_dcdc_ops = {
Axel Lind94d9ac2013-02-13 17:01:09 +0800112 .get_voltage_sel = regulator_get_voltage_sel_regmap,
113 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530114 .list_voltage = regulator_list_voltage_linear,
115 .set_voltage_time_sel = regulator_set_voltage_time_sel,
116 .set_ramp_delay = tps51632_dcdc_set_ramp_delay,
117};
118
Bill Pembertona5023572012-11-19 13:22:22 -0500119static int tps51632_init_dcdc(struct tps51632_chip *tps,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530120 struct tps51632_regulator_platform_data *pdata)
121{
122 int ret;
123 uint8_t control = 0;
124 int vsel;
125
126 if (!pdata->enable_pwm_dvfs)
127 goto skip_pwm_config;
128
129 control |= TPS51632_DVFS_PWMEN;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530130 vsel = TPS51632_VOLT_VSEL(pdata->base_voltage_uV);
131 ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
132 if (ret < 0) {
133 dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
134 return ret;
135 }
136
137 if (pdata->dvfs_step_20mV)
138 control |= TPS51632_DVFS_STEP_20;
139
140 if (pdata->max_voltage_uV) {
141 unsigned int vmax;
142 /**
143 * TPS51632 hw behavior: VMAX register can be write only
144 * once as it get locked after first write. The lock get
145 * reset only when device is power-reset.
146 * Write register only when lock bit is not enabled.
147 */
148 ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
149 if (ret < 0) {
150 dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
151 return ret;
152 }
153 if (!(vmax & TPS51632_VMAX_LOCK)) {
154 vsel = TPS51632_VOLT_VSEL(pdata->max_voltage_uV);
155 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG,
156 vsel);
157 if (ret < 0) {
158 dev_err(tps->dev,
159 "VMAX write failed, err %d\n", ret);
160 return ret;
161 }
162 }
163 }
164
165skip_pwm_config:
166 ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
167 if (ret < 0)
168 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
169 return ret;
170}
171
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530172static bool is_volatile_reg(struct device *dev, unsigned int reg)
Laxman Dewangan0c570672012-10-06 20:47:46 +0530173{
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530174 switch (reg) {
175 case TPS51632_OFFSET_REG:
176 case TPS51632_FAULT_REG:
177 case TPS51632_IMON_REG:
178 return true;
179 default:
Laxman Dewangan0c570672012-10-06 20:47:46 +0530180 return false;
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530181 }
182}
183
184static bool is_read_reg(struct device *dev, unsigned int reg)
185{
186 switch (reg) {
187 case 0x08 ... 0x0F:
188 return false;
189 default:
190 return true;
191 }
192}
193
194static bool is_write_reg(struct device *dev, unsigned int reg)
195{
196 switch (reg) {
197 case TPS51632_VOLTAGE_SELECT_REG:
198 case TPS51632_VOLTAGE_BASE_REG:
199 case TPS51632_VMAX_REG:
200 case TPS51632_DVFS_CONTROL_REG:
201 case TPS51632_POWER_STATE_REG:
202 case TPS51632_SLEW_REGS:
203 return true;
204 default:
205 return false;
206 }
Laxman Dewangan0c570672012-10-06 20:47:46 +0530207}
208
209static const struct regmap_config tps51632_regmap_config = {
210 .reg_bits = 8,
211 .val_bits = 8,
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530212 .writeable_reg = is_write_reg,
213 .readable_reg = is_read_reg,
214 .volatile_reg = is_volatile_reg,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530215 .max_register = TPS51632_MAX_REG - 1,
216 .cache_type = REGCACHE_RBTREE,
217};
218
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530219#if defined(CONFIG_OF)
220static const struct of_device_id tps51632_of_match[] = {
221 { .compatible = "ti,tps51632",},
222 {},
223};
224MODULE_DEVICE_TABLE(of, tps51632_of_match);
225
226static struct tps51632_regulator_platform_data *
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100227 of_get_tps51632_platform_data(struct device *dev,
228 const struct regulator_desc *desc)
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530229{
230 struct tps51632_regulator_platform_data *pdata;
231 struct device_node *np = dev->of_node;
232
233 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Sachin Kamatef4bcf82014-02-20 14:23:12 +0530234 if (!pdata)
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530235 return NULL;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530236
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100237 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
238 desc);
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530239 if (!pdata->reg_init_data) {
240 dev_err(dev, "Not able to get OF regulator init data\n");
241 return NULL;
242 }
243
244 pdata->enable_pwm_dvfs =
245 of_property_read_bool(np, "ti,enable-pwm-dvfs");
246 pdata->dvfs_step_20mV = of_property_read_bool(np, "ti,dvfs-step-20mV");
247
248 pdata->base_voltage_uV = pdata->reg_init_data->constraints.min_uV ? :
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200249 TPS51632_MIN_VOLTAGE;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530250 pdata->max_voltage_uV = pdata->reg_init_data->constraints.max_uV ? :
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200251 TPS51632_MAX_VOLTAGE;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530252 return pdata;
253}
254#else
255static struct tps51632_regulator_platform_data *
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100256 of_get_tps51632_platform_data(struct device *dev,
257 const struct regulator_desc *desc)
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530258{
259 return NULL;
260}
261#endif
262
Bill Pembertona5023572012-11-19 13:22:22 -0500263static int tps51632_probe(struct i2c_client *client,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530264 const struct i2c_device_id *id)
265{
266 struct tps51632_regulator_platform_data *pdata;
267 struct regulator_dev *rdev;
268 struct tps51632_chip *tps;
269 int ret;
270 struct regulator_config config = { };
271
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530272 if (client->dev.of_node) {
273 const struct of_device_id *match;
274 match = of_match_device(of_match_ptr(tps51632_of_match),
275 &client->dev);
276 if (!match) {
277 dev_err(&client->dev, "Error: No device match found\n");
278 return -ENODEV;
279 }
280 }
281
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100282 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
283 if (!tps)
284 return -ENOMEM;
285
286 tps->dev = &client->dev;
287 tps->desc.name = client->name;
288 tps->desc.id = 0;
289 tps->desc.ramp_delay = TPS51632_DEFAULT_RAMP_DELAY;
290 tps->desc.min_uV = TPS51632_MIN_VOLTAGE;
291 tps->desc.uV_step = TPS51632_VOLTAGE_STEP_10mV;
292 tps->desc.linear_min_sel = TPS51632_MIN_VSEL;
293 tps->desc.n_voltages = TPS51632_MAX_VSEL + 1;
294 tps->desc.ops = &tps51632_dcdc_ops;
295 tps->desc.type = REGULATOR_VOLTAGE;
296 tps->desc.owner = THIS_MODULE;
297
Jingoo Handff91d02013-07-30 17:20:47 +0900298 pdata = dev_get_platdata(&client->dev);
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530299 if (!pdata && client->dev.of_node)
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100300 pdata = of_get_tps51632_platform_data(&client->dev, &tps->desc);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530301 if (!pdata) {
302 dev_err(&client->dev, "No Platform data\n");
303 return -EINVAL;
304 }
305
Axel Lindbc70512012-11-30 16:52:49 +0800306 if (pdata->enable_pwm_dvfs) {
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200307 if ((pdata->base_voltage_uV < TPS51632_MIN_VOLTAGE) ||
308 (pdata->base_voltage_uV > TPS51632_MAX_VOLTAGE)) {
Axel Lindbc70512012-11-30 16:52:49 +0800309 dev_err(&client->dev, "Invalid base_voltage_uV setting\n");
310 return -EINVAL;
311 }
312
313 if ((pdata->max_voltage_uV) &&
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200314 ((pdata->max_voltage_uV < TPS51632_MIN_VOLTAGE) ||
315 (pdata->max_voltage_uV > TPS51632_MAX_VOLTAGE))) {
Axel Lindbc70512012-11-30 16:52:49 +0800316 dev_err(&client->dev, "Invalid max_voltage_uV setting\n");
317 return -EINVAL;
318 }
319 }
320
Axel Lind94d9ac2013-02-13 17:01:09 +0800321 if (pdata->enable_pwm_dvfs)
322 tps->desc.vsel_reg = TPS51632_VOLTAGE_BASE_REG;
323 else
324 tps->desc.vsel_reg = TPS51632_VOLTAGE_SELECT_REG;
325 tps->desc.vsel_mask = TPS51632_VOUT_MASK;
326
Laxman Dewangan0c570672012-10-06 20:47:46 +0530327 tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
328 if (IS_ERR(tps->regmap)) {
329 ret = PTR_ERR(tps->regmap);
330 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
331 return ret;
332 }
333 i2c_set_clientdata(client, tps);
334
335 ret = tps51632_init_dcdc(tps, pdata);
336 if (ret < 0) {
337 dev_err(tps->dev, "Init failed, err = %d\n", ret);
338 return ret;
339 }
340
341 /* Register the regulators */
342 config.dev = &client->dev;
343 config.init_data = pdata->reg_init_data;
344 config.driver_data = tps;
345 config.regmap = tps->regmap;
346 config.of_node = client->dev.of_node;
347
Sachin Kamat10840812013-09-04 17:17:44 +0530348 rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530349 if (IS_ERR(rdev)) {
350 dev_err(tps->dev, "regulator register failed\n");
351 return PTR_ERR(rdev);
352 }
353
354 tps->rdev = rdev;
355 return 0;
356}
357
Laxman Dewangan0c570672012-10-06 20:47:46 +0530358static const struct i2c_device_id tps51632_id[] = {
359 {.name = "tps51632",},
360 {},
361};
362
363MODULE_DEVICE_TABLE(i2c, tps51632_id);
364
365static struct i2c_driver tps51632_i2c_driver = {
366 .driver = {
367 .name = "tps51632",
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530368 .of_match_table = of_match_ptr(tps51632_of_match),
Laxman Dewangan0c570672012-10-06 20:47:46 +0530369 },
370 .probe = tps51632_probe,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530371 .id_table = tps51632_id,
372};
373
374static int __init tps51632_init(void)
375{
376 return i2c_add_driver(&tps51632_i2c_driver);
377}
378subsys_initcall(tps51632_init);
379
380static void __exit tps51632_cleanup(void)
381{
382 i2c_del_driver(&tps51632_i2c_driver);
383}
384module_exit(tps51632_cleanup);
385
386MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
387MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
388MODULE_LICENSE("GPL v2");