blob: c3d55c2db593b8f37a253c4a07bd21130db99353 [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;
Laxman Dewangan5928f532012-11-19 06:58:29 +053096 struct regmap *regmap;
97 bool enable_external_control;
98 int dvs_gpio;
99 int lru_index[MAX8973_MAX_VOUT_REG];
100 int curr_vout_val[MAX8973_MAX_VOUT_REG];
101 int curr_vout_reg;
102 int curr_gpio_val;
103 bool valid_dvs_gpio;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200104 struct regulator_ops ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530105};
106
107/*
108 * find_voltage_set_register: Find new voltage configuration register (VOUT).
109 * The finding of the new VOUT register will be based on the LRU mechanism.
110 * Each VOUT register will have different voltage configured . This
111 * Function will look if any of the VOUT register have requested voltage set
112 * or not.
113 * - If it is already there then it will make that register as most
114 * recently used and return as found so that caller need not to set
115 * the VOUT register but need to set the proper gpios to select this
116 * VOUT register.
117 * - If requested voltage is not found then it will use the least
118 * recently mechanism to get new VOUT register for new configuration
119 * and will return not_found so that caller need to set new VOUT
120 * register and then gpios (both).
121 */
122static bool find_voltage_set_register(struct max8973_chip *tps,
123 int req_vsel, int *vout_reg, int *gpio_val)
124{
125 int i;
126 bool found = false;
127 int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
128 int found_index = MAX8973_MAX_VOUT_REG - 1;
129
130 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
131 if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
132 new_vout_reg = tps->lru_index[i];
133 found_index = i;
134 found = true;
135 goto update_lru_index;
136 }
137 }
138
139update_lru_index:
140 for (i = found_index; i > 0; i--)
141 tps->lru_index[i] = tps->lru_index[i - 1];
142
143 tps->lru_index[0] = new_vout_reg;
144 *gpio_val = new_vout_reg;
145 *vout_reg = MAX8973_VOUT + new_vout_reg;
146 return found;
147}
148
149static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
150{
151 struct max8973_chip *max = rdev_get_drvdata(rdev);
152 unsigned int data;
153 int ret;
154
155 ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
156 if (ret < 0) {
157 dev_err(max->dev, "register %d read failed, err = %d\n",
158 max->curr_vout_reg, ret);
159 return ret;
160 }
161 return data & MAX8973_VOUT_MASK;
162}
163
164static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
165 unsigned vsel)
166{
167 struct max8973_chip *max = rdev_get_drvdata(rdev);
168 int ret;
169 bool found = false;
170 int vout_reg = max->curr_vout_reg;
171 int gpio_val = max->curr_gpio_val;
172
173 /*
174 * If gpios are available to select the VOUT register then least
175 * recently used register for new configuration.
176 */
177 if (max->valid_dvs_gpio)
178 found = find_voltage_set_register(max, vsel,
179 &vout_reg, &gpio_val);
180
181 if (!found) {
182 ret = regmap_update_bits(max->regmap, vout_reg,
183 MAX8973_VOUT_MASK, vsel);
184 if (ret < 0) {
185 dev_err(max->dev, "register %d update failed, err %d\n",
186 vout_reg, ret);
187 return ret;
188 }
189 max->curr_vout_reg = vout_reg;
190 max->curr_vout_val[gpio_val] = vsel;
191 }
192
193 /* Select proper VOUT register vio gpios */
194 if (max->valid_dvs_gpio) {
195 gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
196 max->curr_gpio_val = gpio_val;
197 }
198 return 0;
199}
200
201static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
202{
203 struct max8973_chip *max = rdev_get_drvdata(rdev);
204 int ret;
205 int pwm;
206
207 /* Enable force PWM mode in FAST mode only. */
208 switch (mode) {
209 case REGULATOR_MODE_FAST:
210 pwm = MAX8973_FPWM_EN_M;
211 break;
212
213 case REGULATOR_MODE_NORMAL:
214 pwm = 0;
215 break;
216
217 default:
218 return -EINVAL;
219 }
220
221 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
222 MAX8973_FPWM_EN_M, pwm);
223 if (ret < 0)
224 dev_err(max->dev, "register %d update failed, err %d\n",
225 MAX8973_CONTROL1, ret);
226 return ret;
227}
228
229static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
230{
231 struct max8973_chip *max = rdev_get_drvdata(rdev);
232 unsigned int data;
233 int ret;
234
235 ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
236 if (ret < 0) {
237 dev_err(max->dev, "register %d read failed, err %d\n",
238 MAX8973_CONTROL1, ret);
239 return ret;
240 }
241 return (data & MAX8973_FPWM_EN_M) ?
242 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
243}
244
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200245static const struct regulator_ops max8973_dcdc_ops = {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530246 .get_voltage_sel = max8973_dcdc_get_voltage_sel,
247 .set_voltage_sel = max8973_dcdc_set_voltage_sel,
248 .list_voltage = regulator_list_voltage_linear,
249 .set_mode = max8973_dcdc_set_mode,
250 .get_mode = max8973_dcdc_get_mode,
251};
252
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800253static int max8973_init_dcdc(struct max8973_chip *max,
254 struct max8973_regulator_platform_data *pdata)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530255{
256 int ret;
257 uint8_t control1 = 0;
258 uint8_t control2 = 0;
259
260 if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
261 control1 |= MAX8973_SNS_ENABLE;
262
263 if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
264 control1 |= MAX8973_NFSR_ENABLE;
265
266 if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
267 control1 |= MAX8973_AD_ENABLE;
268
269 if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE)
270 control1 |= MAX8973_BIAS_ENABLE;
271
272 if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
273 control1 |= MAX8973_FREQSHIFT_9PER;
274
275 /* Set ramp delay */
276 if (pdata->reg_init_data &&
277 pdata->reg_init_data->constraints.ramp_delay) {
278 if (pdata->reg_init_data->constraints.ramp_delay < 25000)
Axel Linb27032c2013-04-24 00:14:55 +0800279 control1 |= MAX8973_RAMP_12mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530280 else if (pdata->reg_init_data->constraints.ramp_delay < 50000)
Axel Linb27032c2013-04-24 00:14:55 +0800281 control1 |= MAX8973_RAMP_25mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530282 else if (pdata->reg_init_data->constraints.ramp_delay < 200000)
Axel Linb27032c2013-04-24 00:14:55 +0800283 control1 |= MAX8973_RAMP_50mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530284 else
Axel Linb27032c2013-04-24 00:14:55 +0800285 control1 |= MAX8973_RAMP_200mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530286 } else {
Axel Linb27032c2013-04-24 00:14:55 +0800287 control1 |= MAX8973_RAMP_12mV_PER_US;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530288 max->desc.ramp_delay = 12500;
289 }
290
291 if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
292 control2 |= MAX8973_DISCH_ENBABLE;
293
294 /* Clock advance trip configuration */
295 switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
296 case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
297 control2 |= MAX8973_CKKADV_TRIP_DISABLE;
298 break;
299
300 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
301 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
302 break;
303
304 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
305 control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
306 break;
307
308 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
309 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
310 break;
311 }
312
313 /* Configure inductor value */
314 switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
315 case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
316 control2 |= MAX8973_INDUCTOR_NOMINAL;
317 break;
318
319 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
320 control2 |= MAX8973_INDUCTOR_MIN_30_PER;
321 break;
322
323 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
324 control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
325 break;
326
327 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
328 control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
329 break;
330 }
331
332 ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
333 if (ret < 0) {
334 dev_err(max->dev, "register %d write failed, err = %d",
335 MAX8973_CONTROL1, ret);
336 return ret;
337 }
338
339 ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
340 if (ret < 0) {
341 dev_err(max->dev, "register %d write failed, err = %d",
342 MAX8973_CONTROL2, ret);
343 return ret;
344 }
345
346 /* If external control is enabled then disable EN bit */
347 if (max->enable_external_control) {
348 ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
349 MAX8973_VOUT_ENABLE, 0);
350 if (ret < 0)
351 dev_err(max->dev, "register %d update failed, err = %d",
352 MAX8973_VOUT, ret);
353 }
354 return ret;
355}
356
357static const struct regmap_config max8973_regmap_config = {
358 .reg_bits = 8,
359 .val_bits = 8,
360 .max_register = MAX8973_CHIPID2,
361 .cache_type = REGCACHE_RBTREE,
362};
363
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800364static int max8973_probe(struct i2c_client *client,
365 const struct i2c_device_id *id)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530366{
367 struct max8973_regulator_platform_data *pdata;
368 struct regulator_config config = { };
369 struct regulator_dev *rdev;
370 struct max8973_chip *max;
371 int ret;
372
Jingoo Handff91d02013-07-30 17:20:47 +0900373 pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200374
375 if (!pdata && !client->dev.of_node) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530376 dev_err(&client->dev, "No Platform data");
377 return -EIO;
378 }
379
380 max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
Sachin Kamat21024de2014-02-18 16:11:00 +0530381 if (!max)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530382 return -ENOMEM;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530383
384 max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
385 if (IS_ERR(max->regmap)) {
386 ret = PTR_ERR(max->regmap);
387 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
388 return ret;
389 }
390
391 i2c_set_clientdata(client, max);
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200392 max->ops = max8973_dcdc_ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530393 max->dev = &client->dev;
394 max->desc.name = id->name;
395 max->desc.id = 0;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200396 max->desc.ops = &max->ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530397 max->desc.type = REGULATOR_VOLTAGE;
398 max->desc.owner = THIS_MODULE;
399 max->desc.min_uV = MAX8973_MIN_VOLATGE;
400 max->desc.uV_step = MAX8973_VOLATGE_STEP;
401 max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
402
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200403 if (!pdata || !pdata->enable_ext_control) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530404 max->desc.enable_reg = MAX8973_VOUT;
405 max->desc.enable_mask = MAX8973_VOUT_ENABLE;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200406 max->ops.enable = regulator_enable_regmap;
407 max->ops.disable = regulator_disable_regmap;
408 max->ops.is_enabled = regulator_is_enabled_regmap;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530409 }
410
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200411 if (pdata) {
412 max->dvs_gpio = pdata->dvs_gpio;
413 max->enable_external_control = pdata->enable_ext_control;
414 max->curr_gpio_val = pdata->dvs_def_state;
415 max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
416 } else {
417 max->dvs_gpio = -EINVAL;
418 max->curr_vout_reg = MAX8973_VOUT;
419 }
420
Laxman Dewangan5928f532012-11-19 06:58:29 +0530421 max->lru_index[0] = max->curr_vout_reg;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530422
423 if (gpio_is_valid(max->dvs_gpio)) {
424 int gpio_flags;
425 int i;
426
427 gpio_flags = (pdata->dvs_def_state) ?
428 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
429 ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
430 gpio_flags, "max8973-dvs");
431 if (ret) {
432 dev_err(&client->dev,
433 "gpio_request for gpio %d failed, err = %d\n",
434 max->dvs_gpio, ret);
435 return ret;
436 }
437 max->valid_dvs_gpio = true;
438
439 /*
440 * Initialize the lru index with vout_reg id
441 * The index 0 will be most recently used and
442 * set with the max->curr_vout_reg */
443 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
444 max->lru_index[i] = i;
445 max->lru_index[0] = max->curr_vout_reg;
446 max->lru_index[max->curr_vout_reg] = 0;
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200447 } else {
448 max->valid_dvs_gpio = false;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530449 }
450
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200451 if (pdata) {
452 ret = max8973_init_dcdc(max, pdata);
453 if (ret < 0) {
454 dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
455 return ret;
456 }
Laxman Dewangan5928f532012-11-19 06:58:29 +0530457 }
458
459 config.dev = &client->dev;
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200460 config.init_data = pdata ? pdata->reg_init_data :
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100461 of_get_regulator_init_data(&client->dev, client->dev.of_node,
462 &max->desc);
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
Laxman Dewangan5928f532012-11-19 06:58:29 +0530475 return 0;
476}
477
Laxman Dewangan5928f532012-11-19 06:58:29 +0530478static const struct i2c_device_id max8973_id[] = {
479 {.name = "max8973",},
480 {},
481};
482
483MODULE_DEVICE_TABLE(i2c, max8973_id);
484
485static struct i2c_driver max8973_i2c_driver = {
486 .driver = {
487 .name = "max8973",
488 .owner = THIS_MODULE,
489 },
490 .probe = max8973_probe,
Laxman Dewangan5928f532012-11-19 06:58:29 +0530491 .id_table = max8973_id,
492};
493
494static int __init max8973_init(void)
495{
496 return i2c_add_driver(&max8973_i2c_driver);
497}
498subsys_initcall(max8973_init);
499
500static void __exit max8973_cleanup(void)
501{
502 i2c_del_driver(&max8973_i2c_driver);
503}
504module_exit(max8973_cleanup);
505
506MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
507MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
508MODULE_LICENSE("GPL v2");