blob: 79f57fef0038057a1da01f1f2ab155ed35b9aa3d [file] [log] [blame]
Laxman Dewangan5928f532012-11-19 06:58:29 +05301/*
2 * max8973-regulator.c -- Maxim max8973
3 *
4 * Regulator driver for MAXIM 8973 DC-DC step-down switching regulator.
5 *
6 * Copyright (c) 2012, NVIDIA Corporation.
7 *
8 * Author: Laxman Dewangan <ldewangan@nvidia.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation version 2.
13 *
14 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
15 * whether express or implied; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307, USA
23 */
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/err.h>
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +020029#include <linux/of.h>
Laxman Dewangan5928f532012-11-19 06:58:29 +053030#include <linux/platform_device.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
33#include <linux/regulator/max8973-regulator.h>
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +020034#include <linux/regulator/of_regulator.h>
Laxman Dewangan5928f532012-11-19 06:58:29 +053035#include <linux/gpio.h>
36#include <linux/i2c.h>
37#include <linux/slab.h>
38#include <linux/regmap.h>
39
40/* Register definitions */
41#define MAX8973_VOUT 0x0
42#define MAX8973_VOUT_DVS 0x1
43#define MAX8973_CONTROL1 0x2
44#define MAX8973_CONTROL2 0x3
45#define MAX8973_CHIPID1 0x4
46#define MAX8973_CHIPID2 0x5
47
48#define MAX8973_MAX_VOUT_REG 2
49
50/* MAX8973_VOUT */
51#define MAX8973_VOUT_ENABLE BIT(7)
52#define MAX8973_VOUT_MASK 0x7F
53
54/* MAX8973_VOUT_DVS */
55#define MAX8973_DVS_VOUT_MASK 0x7F
56
57/* MAX8973_CONTROL1 */
58#define MAX8973_SNS_ENABLE BIT(7)
59#define MAX8973_FPWM_EN_M BIT(6)
60#define MAX8973_NFSR_ENABLE BIT(5)
61#define MAX8973_AD_ENABLE BIT(4)
62#define MAX8973_BIAS_ENABLE BIT(3)
63#define MAX8973_FREQSHIFT_9PER BIT(2)
64
65#define MAX8973_RAMP_12mV_PER_US 0x0
66#define MAX8973_RAMP_25mV_PER_US 0x1
67#define MAX8973_RAMP_50mV_PER_US 0x2
68#define MAX8973_RAMP_200mV_PER_US 0x3
69
70/* MAX8973_CONTROL2 */
71#define MAX8973_WDTMR_ENABLE BIT(6)
72#define MAX8973_DISCH_ENBABLE BIT(5)
73#define MAX8973_FT_ENABLE BIT(4)
74
75#define MAX8973_CKKADV_TRIP_DISABLE 0xC
76#define MAX8973_CKKADV_TRIP_75mV_PER_US 0x0
77#define MAX8973_CKKADV_TRIP_150mV_PER_US 0x4
78#define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS 0x8
79#define MAX8973_CONTROL_CLKADV_TRIP_MASK 0x00030000
80
81#define MAX8973_INDUCTOR_MIN_30_PER 0x0
82#define MAX8973_INDUCTOR_NOMINAL 0x1
83#define MAX8973_INDUCTOR_PLUS_30_PER 0x2
84#define MAX8973_INDUCTOR_PLUS_60_PER 0x3
85#define MAX8973_CONTROL_INDUCTOR_VALUE_MASK 0x00300000
86
87#define MAX8973_MIN_VOLATGE 606250
88#define MAX8973_MAX_VOLATGE 1400000
89#define MAX8973_VOLATGE_STEP 6250
90#define MAX8973_BUCK_N_VOLTAGE 0x80
91
92/* Maxim 8973 chip information */
93struct max8973_chip {
94 struct device *dev;
95 struct regulator_desc desc;
96 struct regulator_dev *rdev;
97 struct regmap *regmap;
98 bool enable_external_control;
99 int dvs_gpio;
100 int lru_index[MAX8973_MAX_VOUT_REG];
101 int curr_vout_val[MAX8973_MAX_VOUT_REG];
102 int curr_vout_reg;
103 int curr_gpio_val;
104 bool valid_dvs_gpio;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200105 struct regulator_ops ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530106};
107
108/*
109 * find_voltage_set_register: Find new voltage configuration register (VOUT).
110 * The finding of the new VOUT register will be based on the LRU mechanism.
111 * Each VOUT register will have different voltage configured . This
112 * Function will look if any of the VOUT register have requested voltage set
113 * or not.
114 * - If it is already there then it will make that register as most
115 * recently used and return as found so that caller need not to set
116 * the VOUT register but need to set the proper gpios to select this
117 * VOUT register.
118 * - If requested voltage is not found then it will use the least
119 * recently mechanism to get new VOUT register for new configuration
120 * and will return not_found so that caller need to set new VOUT
121 * register and then gpios (both).
122 */
123static bool find_voltage_set_register(struct max8973_chip *tps,
124 int req_vsel, int *vout_reg, int *gpio_val)
125{
126 int i;
127 bool found = false;
128 int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
129 int found_index = MAX8973_MAX_VOUT_REG - 1;
130
131 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
132 if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
133 new_vout_reg = tps->lru_index[i];
134 found_index = i;
135 found = true;
136 goto update_lru_index;
137 }
138 }
139
140update_lru_index:
141 for (i = found_index; i > 0; i--)
142 tps->lru_index[i] = tps->lru_index[i - 1];
143
144 tps->lru_index[0] = new_vout_reg;
145 *gpio_val = new_vout_reg;
146 *vout_reg = MAX8973_VOUT + new_vout_reg;
147 return found;
148}
149
150static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
151{
152 struct max8973_chip *max = rdev_get_drvdata(rdev);
153 unsigned int data;
154 int ret;
155
156 ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
157 if (ret < 0) {
158 dev_err(max->dev, "register %d read failed, err = %d\n",
159 max->curr_vout_reg, ret);
160 return ret;
161 }
162 return data & MAX8973_VOUT_MASK;
163}
164
165static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
166 unsigned vsel)
167{
168 struct max8973_chip *max = rdev_get_drvdata(rdev);
169 int ret;
170 bool found = false;
171 int vout_reg = max->curr_vout_reg;
172 int gpio_val = max->curr_gpio_val;
173
174 /*
175 * If gpios are available to select the VOUT register then least
176 * recently used register for new configuration.
177 */
178 if (max->valid_dvs_gpio)
179 found = find_voltage_set_register(max, vsel,
180 &vout_reg, &gpio_val);
181
182 if (!found) {
183 ret = regmap_update_bits(max->regmap, vout_reg,
184 MAX8973_VOUT_MASK, vsel);
185 if (ret < 0) {
186 dev_err(max->dev, "register %d update failed, err %d\n",
187 vout_reg, ret);
188 return ret;
189 }
190 max->curr_vout_reg = vout_reg;
191 max->curr_vout_val[gpio_val] = vsel;
192 }
193
194 /* Select proper VOUT register vio gpios */
195 if (max->valid_dvs_gpio) {
196 gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
197 max->curr_gpio_val = gpio_val;
198 }
199 return 0;
200}
201
202static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
203{
204 struct max8973_chip *max = rdev_get_drvdata(rdev);
205 int ret;
206 int pwm;
207
208 /* Enable force PWM mode in FAST mode only. */
209 switch (mode) {
210 case REGULATOR_MODE_FAST:
211 pwm = MAX8973_FPWM_EN_M;
212 break;
213
214 case REGULATOR_MODE_NORMAL:
215 pwm = 0;
216 break;
217
218 default:
219 return -EINVAL;
220 }
221
222 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
223 MAX8973_FPWM_EN_M, pwm);
224 if (ret < 0)
225 dev_err(max->dev, "register %d update failed, err %d\n",
226 MAX8973_CONTROL1, ret);
227 return ret;
228}
229
230static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
231{
232 struct max8973_chip *max = rdev_get_drvdata(rdev);
233 unsigned int data;
234 int ret;
235
236 ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
237 if (ret < 0) {
238 dev_err(max->dev, "register %d read failed, err %d\n",
239 MAX8973_CONTROL1, ret);
240 return ret;
241 }
242 return (data & MAX8973_FPWM_EN_M) ?
243 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
244}
245
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200246static const struct regulator_ops max8973_dcdc_ops = {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530247 .get_voltage_sel = max8973_dcdc_get_voltage_sel,
248 .set_voltage_sel = max8973_dcdc_set_voltage_sel,
249 .list_voltage = regulator_list_voltage_linear,
250 .set_mode = max8973_dcdc_set_mode,
251 .get_mode = max8973_dcdc_get_mode,
252};
253
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800254static int max8973_init_dcdc(struct max8973_chip *max,
255 struct max8973_regulator_platform_data *pdata)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530256{
257 int ret;
258 uint8_t control1 = 0;
259 uint8_t control2 = 0;
260
261 if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
262 control1 |= MAX8973_SNS_ENABLE;
263
264 if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
265 control1 |= MAX8973_NFSR_ENABLE;
266
267 if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
268 control1 |= MAX8973_AD_ENABLE;
269
270 if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE)
271 control1 |= MAX8973_BIAS_ENABLE;
272
273 if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
274 control1 |= MAX8973_FREQSHIFT_9PER;
275
276 /* Set ramp delay */
277 if (pdata->reg_init_data &&
278 pdata->reg_init_data->constraints.ramp_delay) {
279 if (pdata->reg_init_data->constraints.ramp_delay < 25000)
Axel Linb27032c2013-04-24 00:14:55 +0800280 control1 |= MAX8973_RAMP_12mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530281 else if (pdata->reg_init_data->constraints.ramp_delay < 50000)
Axel Linb27032c2013-04-24 00:14:55 +0800282 control1 |= MAX8973_RAMP_25mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530283 else if (pdata->reg_init_data->constraints.ramp_delay < 200000)
Axel Linb27032c2013-04-24 00:14:55 +0800284 control1 |= MAX8973_RAMP_50mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530285 else
Axel Linb27032c2013-04-24 00:14:55 +0800286 control1 |= MAX8973_RAMP_200mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530287 } else {
Axel Linb27032c2013-04-24 00:14:55 +0800288 control1 |= MAX8973_RAMP_12mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530289 max->desc.ramp_delay = 12500;
290 }
291
292 if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
293 control2 |= MAX8973_DISCH_ENBABLE;
294
295 /* Clock advance trip configuration */
296 switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
297 case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
298 control2 |= MAX8973_CKKADV_TRIP_DISABLE;
299 break;
300
301 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
302 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
303 break;
304
305 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
306 control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
307 break;
308
309 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
310 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
311 break;
312 }
313
314 /* Configure inductor value */
315 switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
316 case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
317 control2 |= MAX8973_INDUCTOR_NOMINAL;
318 break;
319
320 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
321 control2 |= MAX8973_INDUCTOR_MIN_30_PER;
322 break;
323
324 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
325 control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
326 break;
327
328 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
329 control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
330 break;
331 }
332
333 ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
334 if (ret < 0) {
335 dev_err(max->dev, "register %d write failed, err = %d",
336 MAX8973_CONTROL1, ret);
337 return ret;
338 }
339
340 ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
341 if (ret < 0) {
342 dev_err(max->dev, "register %d write failed, err = %d",
343 MAX8973_CONTROL2, ret);
344 return ret;
345 }
346
347 /* If external control is enabled then disable EN bit */
348 if (max->enable_external_control) {
349 ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
350 MAX8973_VOUT_ENABLE, 0);
351 if (ret < 0)
352 dev_err(max->dev, "register %d update failed, err = %d",
353 MAX8973_VOUT, ret);
354 }
355 return ret;
356}
357
358static const struct regmap_config max8973_regmap_config = {
359 .reg_bits = 8,
360 .val_bits = 8,
361 .max_register = MAX8973_CHIPID2,
362 .cache_type = REGCACHE_RBTREE,
363};
364
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800365static int max8973_probe(struct i2c_client *client,
366 const struct i2c_device_id *id)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530367{
368 struct max8973_regulator_platform_data *pdata;
369 struct regulator_config config = { };
370 struct regulator_dev *rdev;
371 struct max8973_chip *max;
372 int ret;
373
Jingoo Handff91d02013-07-30 17:20:47 +0900374 pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200375
376 if (!pdata && !client->dev.of_node) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530377 dev_err(&client->dev, "No Platform data");
378 return -EIO;
379 }
380
381 max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
Sachin Kamat21024de2014-02-18 16:11:00 +0530382 if (!max)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530383 return -ENOMEM;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530384
385 max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
386 if (IS_ERR(max->regmap)) {
387 ret = PTR_ERR(max->regmap);
388 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
389 return ret;
390 }
391
392 i2c_set_clientdata(client, max);
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200393 max->ops = max8973_dcdc_ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530394 max->dev = &client->dev;
395 max->desc.name = id->name;
396 max->desc.id = 0;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200397 max->desc.ops = &max->ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530398 max->desc.type = REGULATOR_VOLTAGE;
399 max->desc.owner = THIS_MODULE;
400 max->desc.min_uV = MAX8973_MIN_VOLATGE;
401 max->desc.uV_step = MAX8973_VOLATGE_STEP;
402 max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
403
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200404 if (!pdata || !pdata->enable_ext_control) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530405 max->desc.enable_reg = MAX8973_VOUT;
406 max->desc.enable_mask = MAX8973_VOUT_ENABLE;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200407 max->ops.enable = regulator_enable_regmap;
408 max->ops.disable = regulator_disable_regmap;
409 max->ops.is_enabled = regulator_is_enabled_regmap;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530410 }
411
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200412 if (pdata) {
413 max->dvs_gpio = pdata->dvs_gpio;
414 max->enable_external_control = pdata->enable_ext_control;
415 max->curr_gpio_val = pdata->dvs_def_state;
416 max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
417 } else {
418 max->dvs_gpio = -EINVAL;
419 max->curr_vout_reg = MAX8973_VOUT;
420 }
421
Laxman Dewangan5928f532012-11-19 06:58:29 +0530422 max->lru_index[0] = max->curr_vout_reg;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530423
424 if (gpio_is_valid(max->dvs_gpio)) {
425 int gpio_flags;
426 int i;
427
428 gpio_flags = (pdata->dvs_def_state) ?
429 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
430 ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
431 gpio_flags, "max8973-dvs");
432 if (ret) {
433 dev_err(&client->dev,
434 "gpio_request for gpio %d failed, err = %d\n",
435 max->dvs_gpio, ret);
436 return ret;
437 }
438 max->valid_dvs_gpio = true;
439
440 /*
441 * Initialize the lru index with vout_reg id
442 * The index 0 will be most recently used and
443 * set with the max->curr_vout_reg */
444 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
445 max->lru_index[i] = i;
446 max->lru_index[0] = max->curr_vout_reg;
447 max->lru_index[max->curr_vout_reg] = 0;
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200448 } else {
449 max->valid_dvs_gpio = false;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530450 }
451
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200452 if (pdata) {
453 ret = max8973_init_dcdc(max, pdata);
454 if (ret < 0) {
455 dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
456 return ret;
457 }
Laxman Dewangan5928f532012-11-19 06:58:29 +0530458 }
459
460 config.dev = &client->dev;
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200461 config.init_data = pdata ? pdata->reg_init_data :
462 of_get_regulator_init_data(&client->dev, client->dev.of_node);
Laxman Dewangan5928f532012-11-19 06:58:29 +0530463 config.driver_data = max;
464 config.of_node = client->dev.of_node;
465 config.regmap = max->regmap;
466
467 /* Register the regulators */
Sachin Kamat8d581fd2013-09-04 11:08:00 +0530468 rdev = devm_regulator_register(&client->dev, &max->desc, &config);
Laxman Dewangan5928f532012-11-19 06:58:29 +0530469 if (IS_ERR(rdev)) {
470 ret = PTR_ERR(rdev);
471 dev_err(max->dev, "regulator register failed, err %d\n", ret);
472 return ret;
473 }
474
475 max->rdev = rdev;
476 return 0;
477}
478
Laxman Dewangan5928f532012-11-19 06:58:29 +0530479static const struct i2c_device_id max8973_id[] = {
480 {.name = "max8973",},
481 {},
482};
483
484MODULE_DEVICE_TABLE(i2c, max8973_id);
485
486static struct i2c_driver max8973_i2c_driver = {
487 .driver = {
488 .name = "max8973",
489 .owner = THIS_MODULE,
490 },
491 .probe = max8973_probe,
Laxman Dewangan5928f532012-11-19 06:58:29 +0530492 .id_table = max8973_id,
493};
494
495static int __init max8973_init(void)
496{
497 return i2c_add_driver(&max8973_i2c_driver);
498}
499subsys_initcall(max8973_init);
500
501static void __exit max8973_cleanup(void)
502{
503 i2c_del_driver(&max8973_i2c_driver);
504}
505module_exit(max8973_cleanup);
506
507MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
508MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
509MODULE_LICENSE("GPL v2");