blob: f31f22e3e1bd56c0b6f1e36a07152f8c3d0039bc [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);
97 int bit = ramp_delay/6000;
98 int ret;
99
100 if (bit)
101 bit--;
102 ret = regmap_write(tps->regmap, TPS51632_SLEW_REGS, BIT(bit));
103 if (ret < 0)
104 dev_err(tps->dev, "SLEW reg write failed, err %d\n", ret);
105 return ret;
106}
107
108static struct regulator_ops tps51632_dcdc_ops = {
Axel Lind94d9ac2013-02-13 17:01:09 +0800109 .get_voltage_sel = regulator_get_voltage_sel_regmap,
110 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530111 .list_voltage = regulator_list_voltage_linear,
112 .set_voltage_time_sel = regulator_set_voltage_time_sel,
113 .set_ramp_delay = tps51632_dcdc_set_ramp_delay,
114};
115
Bill Pembertona5023572012-11-19 13:22:22 -0500116static int tps51632_init_dcdc(struct tps51632_chip *tps,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530117 struct tps51632_regulator_platform_data *pdata)
118{
119 int ret;
120 uint8_t control = 0;
121 int vsel;
122
123 if (!pdata->enable_pwm_dvfs)
124 goto skip_pwm_config;
125
126 control |= TPS51632_DVFS_PWMEN;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530127 vsel = TPS51632_VOLT_VSEL(pdata->base_voltage_uV);
128 ret = regmap_write(tps->regmap, TPS51632_VOLTAGE_BASE_REG, vsel);
129 if (ret < 0) {
130 dev_err(tps->dev, "BASE reg write failed, err %d\n", ret);
131 return ret;
132 }
133
134 if (pdata->dvfs_step_20mV)
135 control |= TPS51632_DVFS_STEP_20;
136
137 if (pdata->max_voltage_uV) {
138 unsigned int vmax;
139 /**
140 * TPS51632 hw behavior: VMAX register can be write only
141 * once as it get locked after first write. The lock get
142 * reset only when device is power-reset.
143 * Write register only when lock bit is not enabled.
144 */
145 ret = regmap_read(tps->regmap, TPS51632_VMAX_REG, &vmax);
146 if (ret < 0) {
147 dev_err(tps->dev, "VMAX read failed, err %d\n", ret);
148 return ret;
149 }
150 if (!(vmax & TPS51632_VMAX_LOCK)) {
151 vsel = TPS51632_VOLT_VSEL(pdata->max_voltage_uV);
152 ret = regmap_write(tps->regmap, TPS51632_VMAX_REG,
153 vsel);
154 if (ret < 0) {
155 dev_err(tps->dev,
156 "VMAX write failed, err %d\n", ret);
157 return ret;
158 }
159 }
160 }
161
162skip_pwm_config:
163 ret = regmap_write(tps->regmap, TPS51632_DVFS_CONTROL_REG, control);
164 if (ret < 0)
165 dev_err(tps->dev, "DVFS reg write failed, err %d\n", ret);
166 return ret;
167}
168
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530169static bool is_volatile_reg(struct device *dev, unsigned int reg)
Laxman Dewangan0c570672012-10-06 20:47:46 +0530170{
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530171 switch (reg) {
172 case TPS51632_OFFSET_REG:
173 case TPS51632_FAULT_REG:
174 case TPS51632_IMON_REG:
175 return true;
176 default:
Laxman Dewangan0c570672012-10-06 20:47:46 +0530177 return false;
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530178 }
179}
180
181static bool is_read_reg(struct device *dev, unsigned int reg)
182{
183 switch (reg) {
184 case 0x08 ... 0x0F:
185 return false;
186 default:
187 return true;
188 }
189}
190
191static bool is_write_reg(struct device *dev, unsigned int reg)
192{
193 switch (reg) {
194 case TPS51632_VOLTAGE_SELECT_REG:
195 case TPS51632_VOLTAGE_BASE_REG:
196 case TPS51632_VMAX_REG:
197 case TPS51632_DVFS_CONTROL_REG:
198 case TPS51632_POWER_STATE_REG:
199 case TPS51632_SLEW_REGS:
200 return true;
201 default:
202 return false;
203 }
Laxman Dewangan0c570672012-10-06 20:47:46 +0530204}
205
206static const struct regmap_config tps51632_regmap_config = {
207 .reg_bits = 8,
208 .val_bits = 8,
Laxman Dewanganfaa3b2d2012-12-25 20:35:59 +0530209 .writeable_reg = is_write_reg,
210 .readable_reg = is_read_reg,
211 .volatile_reg = is_volatile_reg,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530212 .max_register = TPS51632_MAX_REG - 1,
213 .cache_type = REGCACHE_RBTREE,
214};
215
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530216#if defined(CONFIG_OF)
217static const struct of_device_id tps51632_of_match[] = {
218 { .compatible = "ti,tps51632",},
219 {},
220};
221MODULE_DEVICE_TABLE(of, tps51632_of_match);
222
223static struct tps51632_regulator_platform_data *
224 of_get_tps51632_platform_data(struct device *dev)
225{
226 struct tps51632_regulator_platform_data *pdata;
227 struct device_node *np = dev->of_node;
228
229 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Sachin Kamatef4bcf82014-02-20 14:23:12 +0530230 if (!pdata)
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530231 return NULL;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530232
233 pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node);
234 if (!pdata->reg_init_data) {
235 dev_err(dev, "Not able to get OF regulator init data\n");
236 return NULL;
237 }
238
239 pdata->enable_pwm_dvfs =
240 of_property_read_bool(np, "ti,enable-pwm-dvfs");
241 pdata->dvfs_step_20mV = of_property_read_bool(np, "ti,dvfs-step-20mV");
242
243 pdata->base_voltage_uV = pdata->reg_init_data->constraints.min_uV ? :
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200244 TPS51632_MIN_VOLTAGE;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530245 pdata->max_voltage_uV = pdata->reg_init_data->constraints.max_uV ? :
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200246 TPS51632_MAX_VOLTAGE;
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530247 return pdata;
248}
249#else
250static struct tps51632_regulator_platform_data *
251 of_get_tps51632_platform_data(struct device *dev)
252{
253 return NULL;
254}
255#endif
256
Bill Pembertona5023572012-11-19 13:22:22 -0500257static int tps51632_probe(struct i2c_client *client,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530258 const struct i2c_device_id *id)
259{
260 struct tps51632_regulator_platform_data *pdata;
261 struct regulator_dev *rdev;
262 struct tps51632_chip *tps;
263 int ret;
264 struct regulator_config config = { };
265
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530266 if (client->dev.of_node) {
267 const struct of_device_id *match;
268 match = of_match_device(of_match_ptr(tps51632_of_match),
269 &client->dev);
270 if (!match) {
271 dev_err(&client->dev, "Error: No device match found\n");
272 return -ENODEV;
273 }
274 }
275
Jingoo Handff91d02013-07-30 17:20:47 +0900276 pdata = dev_get_platdata(&client->dev);
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530277 if (!pdata && client->dev.of_node)
278 pdata = of_get_tps51632_platform_data(&client->dev);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530279 if (!pdata) {
280 dev_err(&client->dev, "No Platform data\n");
281 return -EINVAL;
282 }
283
Axel Lindbc70512012-11-30 16:52:49 +0800284 if (pdata->enable_pwm_dvfs) {
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200285 if ((pdata->base_voltage_uV < TPS51632_MIN_VOLTAGE) ||
286 (pdata->base_voltage_uV > TPS51632_MAX_VOLTAGE)) {
Axel Lindbc70512012-11-30 16:52:49 +0800287 dev_err(&client->dev, "Invalid base_voltage_uV setting\n");
288 return -EINVAL;
289 }
290
291 if ((pdata->max_voltage_uV) &&
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200292 ((pdata->max_voltage_uV < TPS51632_MIN_VOLTAGE) ||
293 (pdata->max_voltage_uV > TPS51632_MAX_VOLTAGE))) {
Axel Lindbc70512012-11-30 16:52:49 +0800294 dev_err(&client->dev, "Invalid max_voltage_uV setting\n");
295 return -EINVAL;
296 }
297 }
298
Laxman Dewangan0c570672012-10-06 20:47:46 +0530299 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
Sachin Kamatef4bcf82014-02-20 14:23:12 +0530300 if (!tps)
Laxman Dewangan0c570672012-10-06 20:47:46 +0530301 return -ENOMEM;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530302
303 tps->dev = &client->dev;
Mikko Perttunen6930db12013-06-18 13:30:13 +0300304 tps->desc.name = client->name;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530305 tps->desc.id = 0;
306 tps->desc.ramp_delay = TPS51632_DEFAULT_RAMP_DELAY;
Fabio Estevam9d9339d2013-12-20 11:43:21 -0200307 tps->desc.min_uV = TPS51632_MIN_VOLTAGE;
308 tps->desc.uV_step = TPS51632_VOLTAGE_STEP_10mV;
Axel Linbd0ec7c2012-11-28 07:22:54 +0800309 tps->desc.linear_min_sel = TPS51632_MIN_VSEL;
310 tps->desc.n_voltages = TPS51632_MAX_VSEL + 1;
Laxman Dewangan0c570672012-10-06 20:47:46 +0530311 tps->desc.ops = &tps51632_dcdc_ops;
312 tps->desc.type = REGULATOR_VOLTAGE;
313 tps->desc.owner = THIS_MODULE;
314
Axel Lind94d9ac2013-02-13 17:01:09 +0800315 if (pdata->enable_pwm_dvfs)
316 tps->desc.vsel_reg = TPS51632_VOLTAGE_BASE_REG;
317 else
318 tps->desc.vsel_reg = TPS51632_VOLTAGE_SELECT_REG;
319 tps->desc.vsel_mask = TPS51632_VOUT_MASK;
320
Laxman Dewangan0c570672012-10-06 20:47:46 +0530321 tps->regmap = devm_regmap_init_i2c(client, &tps51632_regmap_config);
322 if (IS_ERR(tps->regmap)) {
323 ret = PTR_ERR(tps->regmap);
324 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
325 return ret;
326 }
327 i2c_set_clientdata(client, tps);
328
329 ret = tps51632_init_dcdc(tps, pdata);
330 if (ret < 0) {
331 dev_err(tps->dev, "Init failed, err = %d\n", ret);
332 return ret;
333 }
334
335 /* Register the regulators */
336 config.dev = &client->dev;
337 config.init_data = pdata->reg_init_data;
338 config.driver_data = tps;
339 config.regmap = tps->regmap;
340 config.of_node = client->dev.of_node;
341
Sachin Kamat10840812013-09-04 17:17:44 +0530342 rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
Laxman Dewangan0c570672012-10-06 20:47:46 +0530343 if (IS_ERR(rdev)) {
344 dev_err(tps->dev, "regulator register failed\n");
345 return PTR_ERR(rdev);
346 }
347
348 tps->rdev = rdev;
349 return 0;
350}
351
Laxman Dewangan0c570672012-10-06 20:47:46 +0530352static const struct i2c_device_id tps51632_id[] = {
353 {.name = "tps51632",},
354 {},
355};
356
357MODULE_DEVICE_TABLE(i2c, tps51632_id);
358
359static struct i2c_driver tps51632_i2c_driver = {
360 .driver = {
361 .name = "tps51632",
362 .owner = THIS_MODULE,
Laxman Dewanganc51ce402012-12-25 20:36:00 +0530363 .of_match_table = of_match_ptr(tps51632_of_match),
Laxman Dewangan0c570672012-10-06 20:47:46 +0530364 },
365 .probe = tps51632_probe,
Laxman Dewangan0c570672012-10-06 20:47:46 +0530366 .id_table = tps51632_id,
367};
368
369static int __init tps51632_init(void)
370{
371 return i2c_add_driver(&tps51632_i2c_driver);
372}
373subsys_initcall(tps51632_init);
374
375static void __exit tps51632_cleanup(void)
376{
377 i2c_del_driver(&tps51632_i2c_driver);
378}
379module_exit(tps51632_cleanup);
380
381MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
382MODULE_DESCRIPTION("TPS51632 voltage regulator driver");
383MODULE_LICENSE("GPL v2");