blob: 3ee26387b1211e07ca8f3e9e567aa511aa82b7bd [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>
29#include <linux/platform_device.h>
30#include <linux/regulator/driver.h>
31#include <linux/regulator/machine.h>
32#include <linux/regulator/max8973-regulator.h>
33#include <linux/gpio.h>
34#include <linux/i2c.h>
35#include <linux/slab.h>
36#include <linux/regmap.h>
37
38/* Register definitions */
39#define MAX8973_VOUT 0x0
40#define MAX8973_VOUT_DVS 0x1
41#define MAX8973_CONTROL1 0x2
42#define MAX8973_CONTROL2 0x3
43#define MAX8973_CHIPID1 0x4
44#define MAX8973_CHIPID2 0x5
45
46#define MAX8973_MAX_VOUT_REG 2
47
48/* MAX8973_VOUT */
49#define MAX8973_VOUT_ENABLE BIT(7)
50#define MAX8973_VOUT_MASK 0x7F
51
52/* MAX8973_VOUT_DVS */
53#define MAX8973_DVS_VOUT_MASK 0x7F
54
55/* MAX8973_CONTROL1 */
56#define MAX8973_SNS_ENABLE BIT(7)
57#define MAX8973_FPWM_EN_M BIT(6)
58#define MAX8973_NFSR_ENABLE BIT(5)
59#define MAX8973_AD_ENABLE BIT(4)
60#define MAX8973_BIAS_ENABLE BIT(3)
61#define MAX8973_FREQSHIFT_9PER BIT(2)
62
63#define MAX8973_RAMP_12mV_PER_US 0x0
64#define MAX8973_RAMP_25mV_PER_US 0x1
65#define MAX8973_RAMP_50mV_PER_US 0x2
66#define MAX8973_RAMP_200mV_PER_US 0x3
67
68/* MAX8973_CONTROL2 */
69#define MAX8973_WDTMR_ENABLE BIT(6)
70#define MAX8973_DISCH_ENBABLE BIT(5)
71#define MAX8973_FT_ENABLE BIT(4)
72
73#define MAX8973_CKKADV_TRIP_DISABLE 0xC
74#define MAX8973_CKKADV_TRIP_75mV_PER_US 0x0
75#define MAX8973_CKKADV_TRIP_150mV_PER_US 0x4
76#define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS 0x8
77#define MAX8973_CONTROL_CLKADV_TRIP_MASK 0x00030000
78
79#define MAX8973_INDUCTOR_MIN_30_PER 0x0
80#define MAX8973_INDUCTOR_NOMINAL 0x1
81#define MAX8973_INDUCTOR_PLUS_30_PER 0x2
82#define MAX8973_INDUCTOR_PLUS_60_PER 0x3
83#define MAX8973_CONTROL_INDUCTOR_VALUE_MASK 0x00300000
84
85#define MAX8973_MIN_VOLATGE 606250
86#define MAX8973_MAX_VOLATGE 1400000
87#define MAX8973_VOLATGE_STEP 6250
88#define MAX8973_BUCK_N_VOLTAGE 0x80
89
90/* Maxim 8973 chip information */
91struct max8973_chip {
92 struct device *dev;
93 struct regulator_desc desc;
94 struct regulator_dev *rdev;
95 struct regmap *regmap;
96 bool enable_external_control;
97 int dvs_gpio;
98 int lru_index[MAX8973_MAX_VOUT_REG];
99 int curr_vout_val[MAX8973_MAX_VOUT_REG];
100 int curr_vout_reg;
101 int curr_gpio_val;
102 bool valid_dvs_gpio;
103};
104
105/*
106 * find_voltage_set_register: Find new voltage configuration register (VOUT).
107 * The finding of the new VOUT register will be based on the LRU mechanism.
108 * Each VOUT register will have different voltage configured . This
109 * Function will look if any of the VOUT register have requested voltage set
110 * or not.
111 * - If it is already there then it will make that register as most
112 * recently used and return as found so that caller need not to set
113 * the VOUT register but need to set the proper gpios to select this
114 * VOUT register.
115 * - If requested voltage is not found then it will use the least
116 * recently mechanism to get new VOUT register for new configuration
117 * and will return not_found so that caller need to set new VOUT
118 * register and then gpios (both).
119 */
120static bool find_voltage_set_register(struct max8973_chip *tps,
121 int req_vsel, int *vout_reg, int *gpio_val)
122{
123 int i;
124 bool found = false;
125 int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
126 int found_index = MAX8973_MAX_VOUT_REG - 1;
127
128 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
129 if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
130 new_vout_reg = tps->lru_index[i];
131 found_index = i;
132 found = true;
133 goto update_lru_index;
134 }
135 }
136
137update_lru_index:
138 for (i = found_index; i > 0; i--)
139 tps->lru_index[i] = tps->lru_index[i - 1];
140
141 tps->lru_index[0] = new_vout_reg;
142 *gpio_val = new_vout_reg;
143 *vout_reg = MAX8973_VOUT + new_vout_reg;
144 return found;
145}
146
147static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
148{
149 struct max8973_chip *max = rdev_get_drvdata(rdev);
150 unsigned int data;
151 int ret;
152
153 ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
154 if (ret < 0) {
155 dev_err(max->dev, "register %d read failed, err = %d\n",
156 max->curr_vout_reg, ret);
157 return ret;
158 }
159 return data & MAX8973_VOUT_MASK;
160}
161
162static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
163 unsigned vsel)
164{
165 struct max8973_chip *max = rdev_get_drvdata(rdev);
166 int ret;
167 bool found = false;
168 int vout_reg = max->curr_vout_reg;
169 int gpio_val = max->curr_gpio_val;
170
171 /*
172 * If gpios are available to select the VOUT register then least
173 * recently used register for new configuration.
174 */
175 if (max->valid_dvs_gpio)
176 found = find_voltage_set_register(max, vsel,
177 &vout_reg, &gpio_val);
178
179 if (!found) {
180 ret = regmap_update_bits(max->regmap, vout_reg,
181 MAX8973_VOUT_MASK, vsel);
182 if (ret < 0) {
183 dev_err(max->dev, "register %d update failed, err %d\n",
184 vout_reg, ret);
185 return ret;
186 }
187 max->curr_vout_reg = vout_reg;
188 max->curr_vout_val[gpio_val] = vsel;
189 }
190
191 /* Select proper VOUT register vio gpios */
192 if (max->valid_dvs_gpio) {
193 gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
194 max->curr_gpio_val = gpio_val;
195 }
196 return 0;
197}
198
199static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
200{
201 struct max8973_chip *max = rdev_get_drvdata(rdev);
202 int ret;
203 int pwm;
204
205 /* Enable force PWM mode in FAST mode only. */
206 switch (mode) {
207 case REGULATOR_MODE_FAST:
208 pwm = MAX8973_FPWM_EN_M;
209 break;
210
211 case REGULATOR_MODE_NORMAL:
212 pwm = 0;
213 break;
214
215 default:
216 return -EINVAL;
217 }
218
219 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
220 MAX8973_FPWM_EN_M, pwm);
221 if (ret < 0)
222 dev_err(max->dev, "register %d update failed, err %d\n",
223 MAX8973_CONTROL1, ret);
224 return ret;
225}
226
227static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
228{
229 struct max8973_chip *max = rdev_get_drvdata(rdev);
230 unsigned int data;
231 int ret;
232
233 ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
234 if (ret < 0) {
235 dev_err(max->dev, "register %d read failed, err %d\n",
236 MAX8973_CONTROL1, ret);
237 return ret;
238 }
239 return (data & MAX8973_FPWM_EN_M) ?
240 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
241}
242
243static struct regulator_ops max8973_dcdc_ops = {
244 .get_voltage_sel = max8973_dcdc_get_voltage_sel,
245 .set_voltage_sel = max8973_dcdc_set_voltage_sel,
246 .list_voltage = regulator_list_voltage_linear,
247 .set_mode = max8973_dcdc_set_mode,
248 .get_mode = max8973_dcdc_get_mode,
249};
250
251static int __devinit max8973_init_dcdc(struct max8973_chip *max,
252 struct max8973_regulator_platform_data *pdata)
253{
254 int ret;
255 uint8_t control1 = 0;
256 uint8_t control2 = 0;
257
258 if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
259 control1 |= MAX8973_SNS_ENABLE;
260
261 if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
262 control1 |= MAX8973_NFSR_ENABLE;
263
264 if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
265 control1 |= MAX8973_AD_ENABLE;
266
267 if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE)
268 control1 |= MAX8973_BIAS_ENABLE;
269
270 if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
271 control1 |= MAX8973_FREQSHIFT_9PER;
272
273 /* Set ramp delay */
274 if (pdata->reg_init_data &&
275 pdata->reg_init_data->constraints.ramp_delay) {
276 if (pdata->reg_init_data->constraints.ramp_delay < 25000)
277 control1 = MAX8973_RAMP_12mV_PER_US;
278 else if (pdata->reg_init_data->constraints.ramp_delay < 50000)
279 control1 = MAX8973_RAMP_25mV_PER_US;
280 else if (pdata->reg_init_data->constraints.ramp_delay < 200000)
281 control1 = MAX8973_RAMP_50mV_PER_US;
282 else
283 control1 = MAX8973_RAMP_200mV_PER_US;
284 } else {
285 control1 = MAX8973_RAMP_12mV_PER_US;
286 max->desc.ramp_delay = 12500;
287 }
288
289 if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
290 control2 |= MAX8973_DISCH_ENBABLE;
291
292 /* Clock advance trip configuration */
293 switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
294 case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
295 control2 |= MAX8973_CKKADV_TRIP_DISABLE;
296 break;
297
298 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
299 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
300 break;
301
302 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
303 control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
304 break;
305
306 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
307 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
308 break;
309 }
310
311 /* Configure inductor value */
312 switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
313 case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
314 control2 |= MAX8973_INDUCTOR_NOMINAL;
315 break;
316
317 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
318 control2 |= MAX8973_INDUCTOR_MIN_30_PER;
319 break;
320
321 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
322 control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
323 break;
324
325 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
326 control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
327 break;
328 }
329
330 ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
331 if (ret < 0) {
332 dev_err(max->dev, "register %d write failed, err = %d",
333 MAX8973_CONTROL1, ret);
334 return ret;
335 }
336
337 ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
338 if (ret < 0) {
339 dev_err(max->dev, "register %d write failed, err = %d",
340 MAX8973_CONTROL2, ret);
341 return ret;
342 }
343
344 /* If external control is enabled then disable EN bit */
345 if (max->enable_external_control) {
346 ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
347 MAX8973_VOUT_ENABLE, 0);
348 if (ret < 0)
349 dev_err(max->dev, "register %d update failed, err = %d",
350 MAX8973_VOUT, ret);
351 }
352 return ret;
353}
354
355static const struct regmap_config max8973_regmap_config = {
356 .reg_bits = 8,
357 .val_bits = 8,
358 .max_register = MAX8973_CHIPID2,
359 .cache_type = REGCACHE_RBTREE,
360};
361
362static int __devinit max8973_probe(struct i2c_client *client,
363 const struct i2c_device_id *id)
364{
365 struct max8973_regulator_platform_data *pdata;
366 struct regulator_config config = { };
367 struct regulator_dev *rdev;
368 struct max8973_chip *max;
369 int ret;
370
371 pdata = client->dev.platform_data;
372 if (!pdata) {
373 dev_err(&client->dev, "No Platform data");
374 return -EIO;
375 }
376
377 max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
378 if (!max) {
379 dev_err(&client->dev, "Memory allocation for max failed\n");
380 return -ENOMEM;
381 }
382
383 max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
384 if (IS_ERR(max->regmap)) {
385 ret = PTR_ERR(max->regmap);
386 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
387 return ret;
388 }
389
390 i2c_set_clientdata(client, max);
391 max->dev = &client->dev;
392 max->desc.name = id->name;
393 max->desc.id = 0;
394 max->desc.ops = &max8973_dcdc_ops;
395 max->desc.type = REGULATOR_VOLTAGE;
396 max->desc.owner = THIS_MODULE;
397 max->desc.min_uV = MAX8973_MIN_VOLATGE;
398 max->desc.uV_step = MAX8973_VOLATGE_STEP;
399 max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
400
Laxman Dewangan56968652012-11-23 14:07:42 +0530401 if (!pdata->enable_ext_control) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530402 max->desc.enable_reg = MAX8973_VOUT;
403 max->desc.enable_mask = MAX8973_VOUT_ENABLE;
Axel Line05f70c2012-11-22 09:47:08 +0800404 max8973_dcdc_ops.enable = regulator_enable_regmap;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530405 max8973_dcdc_ops.disable = regulator_disable_regmap;
406 max8973_dcdc_ops.is_enabled = regulator_is_enabled_regmap;
407 }
408
409 max->enable_external_control = pdata->enable_ext_control;
410 max->dvs_gpio = pdata->dvs_gpio;
411 max->curr_gpio_val = pdata->dvs_def_state;
412 max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
413 max->lru_index[0] = max->curr_vout_reg;
414 max->valid_dvs_gpio = false;
415
416 if (gpio_is_valid(max->dvs_gpio)) {
417 int gpio_flags;
418 int i;
419
420 gpio_flags = (pdata->dvs_def_state) ?
421 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
422 ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
423 gpio_flags, "max8973-dvs");
424 if (ret) {
425 dev_err(&client->dev,
426 "gpio_request for gpio %d failed, err = %d\n",
427 max->dvs_gpio, ret);
428 return ret;
429 }
430 max->valid_dvs_gpio = true;
431
432 /*
433 * Initialize the lru index with vout_reg id
434 * The index 0 will be most recently used and
435 * set with the max->curr_vout_reg */
436 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
437 max->lru_index[i] = i;
438 max->lru_index[0] = max->curr_vout_reg;
439 max->lru_index[max->curr_vout_reg] = 0;
440 }
441
442 ret = max8973_init_dcdc(max, pdata);
443 if (ret < 0) {
444 dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
445 return ret;
446 }
447
448 config.dev = &client->dev;
449 config.init_data = pdata->reg_init_data;
450 config.driver_data = max;
451 config.of_node = client->dev.of_node;
452 config.regmap = max->regmap;
453
454 /* Register the regulators */
455 rdev = regulator_register(&max->desc, &config);
456 if (IS_ERR(rdev)) {
457 ret = PTR_ERR(rdev);
458 dev_err(max->dev, "regulator register failed, err %d\n", ret);
459 return ret;
460 }
461
462 max->rdev = rdev;
463 return 0;
464}
465
466static int __devexit max8973_remove(struct i2c_client *client)
467{
468 struct max8973_chip *max = i2c_get_clientdata(client);
469
470 regulator_unregister(max->rdev);
471 return 0;
472}
473
474static const struct i2c_device_id max8973_id[] = {
475 {.name = "max8973",},
476 {},
477};
478
479MODULE_DEVICE_TABLE(i2c, max8973_id);
480
481static struct i2c_driver max8973_i2c_driver = {
482 .driver = {
483 .name = "max8973",
484 .owner = THIS_MODULE,
485 },
486 .probe = max8973_probe,
487 .remove = __devexit_p(max8973_remove),
488 .id_table = max8973_id,
489};
490
491static int __init max8973_init(void)
492{
493 return i2c_add_driver(&max8973_i2c_driver);
494}
495subsys_initcall(max8973_init);
496
497static void __exit max8973_cleanup(void)
498{
499 i2c_del_driver(&max8973_i2c_driver);
500}
501module_exit(max8973_cleanup);
502
503MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
504MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
505MODULE_LICENSE("GPL v2");