blob: 5b75b7c2e3ea441c266a13c367d555b08d46077d [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 Dewangan0f7d6ec2015-06-09 19:17:53 +053030#include <linux/of_device.h>
Laxman Dewangan5928f532012-11-19 06:58:29 +053031#include <linux/platform_device.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
34#include <linux/regulator/max8973-regulator.h>
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +020035#include <linux/regulator/of_regulator.h>
Laxman Dewangan5928f532012-11-19 06:58:29 +053036#include <linux/gpio.h>
Laxman Dewanganc2ffa972015-04-23 16:10:23 +053037#include <linux/of_gpio.h>
Laxman Dewangan5928f532012-11-19 06:58:29 +053038#include <linux/i2c.h>
39#include <linux/slab.h>
40#include <linux/regmap.h>
41
42/* Register definitions */
43#define MAX8973_VOUT 0x0
44#define MAX8973_VOUT_DVS 0x1
45#define MAX8973_CONTROL1 0x2
46#define MAX8973_CONTROL2 0x3
47#define MAX8973_CHIPID1 0x4
48#define MAX8973_CHIPID2 0x5
49
50#define MAX8973_MAX_VOUT_REG 2
51
52/* MAX8973_VOUT */
53#define MAX8973_VOUT_ENABLE BIT(7)
54#define MAX8973_VOUT_MASK 0x7F
55
56/* MAX8973_VOUT_DVS */
57#define MAX8973_DVS_VOUT_MASK 0x7F
58
59/* MAX8973_CONTROL1 */
60#define MAX8973_SNS_ENABLE BIT(7)
61#define MAX8973_FPWM_EN_M BIT(6)
62#define MAX8973_NFSR_ENABLE BIT(5)
63#define MAX8973_AD_ENABLE BIT(4)
64#define MAX8973_BIAS_ENABLE BIT(3)
65#define MAX8973_FREQSHIFT_9PER BIT(2)
66
67#define MAX8973_RAMP_12mV_PER_US 0x0
68#define MAX8973_RAMP_25mV_PER_US 0x1
69#define MAX8973_RAMP_50mV_PER_US 0x2
70#define MAX8973_RAMP_200mV_PER_US 0x3
Laxman Dewangan3064c1f2015-06-09 19:17:52 +053071#define MAX8973_RAMP_MASK 0x3
Laxman Dewangan5928f532012-11-19 06:58:29 +053072
73/* MAX8973_CONTROL2 */
74#define MAX8973_WDTMR_ENABLE BIT(6)
75#define MAX8973_DISCH_ENBABLE BIT(5)
76#define MAX8973_FT_ENABLE BIT(4)
77
Laxman Dewanganffaab992015-07-01 18:31:44 +053078#define MAX8973_CKKADV_TRIP_MASK 0xC
Laxman Dewangan5928f532012-11-19 06:58:29 +053079#define MAX8973_CKKADV_TRIP_DISABLE 0xC
80#define MAX8973_CKKADV_TRIP_75mV_PER_US 0x0
81#define MAX8973_CKKADV_TRIP_150mV_PER_US 0x4
82#define MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS 0x8
83#define MAX8973_CONTROL_CLKADV_TRIP_MASK 0x00030000
84
85#define MAX8973_INDUCTOR_MIN_30_PER 0x0
86#define MAX8973_INDUCTOR_NOMINAL 0x1
87#define MAX8973_INDUCTOR_PLUS_30_PER 0x2
88#define MAX8973_INDUCTOR_PLUS_60_PER 0x3
89#define MAX8973_CONTROL_INDUCTOR_VALUE_MASK 0x00300000
90
91#define MAX8973_MIN_VOLATGE 606250
92#define MAX8973_MAX_VOLATGE 1400000
93#define MAX8973_VOLATGE_STEP 6250
94#define MAX8973_BUCK_N_VOLTAGE 0x80
95
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +053096enum device_id {
97 MAX8973,
98 MAX77621
99};
100
Laxman Dewangan5928f532012-11-19 06:58:29 +0530101/* Maxim 8973 chip information */
102struct max8973_chip {
103 struct device *dev;
104 struct regulator_desc desc;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530105 struct regmap *regmap;
106 bool enable_external_control;
Laxman Dewangan69eb0982015-04-23 16:10:24 +0530107 int enable_gpio;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530108 int dvs_gpio;
109 int lru_index[MAX8973_MAX_VOUT_REG];
110 int curr_vout_val[MAX8973_MAX_VOUT_REG];
111 int curr_vout_reg;
112 int curr_gpio_val;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200113 struct regulator_ops ops;
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530114 enum device_id id;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530115};
116
117/*
118 * find_voltage_set_register: Find new voltage configuration register (VOUT).
119 * The finding of the new VOUT register will be based on the LRU mechanism.
120 * Each VOUT register will have different voltage configured . This
121 * Function will look if any of the VOUT register have requested voltage set
122 * or not.
123 * - If it is already there then it will make that register as most
124 * recently used and return as found so that caller need not to set
125 * the VOUT register but need to set the proper gpios to select this
126 * VOUT register.
127 * - If requested voltage is not found then it will use the least
128 * recently mechanism to get new VOUT register for new configuration
129 * and will return not_found so that caller need to set new VOUT
130 * register and then gpios (both).
131 */
132static bool find_voltage_set_register(struct max8973_chip *tps,
133 int req_vsel, int *vout_reg, int *gpio_val)
134{
135 int i;
136 bool found = false;
137 int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
138 int found_index = MAX8973_MAX_VOUT_REG - 1;
139
140 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
141 if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
142 new_vout_reg = tps->lru_index[i];
143 found_index = i;
144 found = true;
145 goto update_lru_index;
146 }
147 }
148
149update_lru_index:
150 for (i = found_index; i > 0; i--)
151 tps->lru_index[i] = tps->lru_index[i - 1];
152
153 tps->lru_index[0] = new_vout_reg;
154 *gpio_val = new_vout_reg;
155 *vout_reg = MAX8973_VOUT + new_vout_reg;
156 return found;
157}
158
159static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
160{
161 struct max8973_chip *max = rdev_get_drvdata(rdev);
162 unsigned int data;
163 int ret;
164
165 ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
166 if (ret < 0) {
167 dev_err(max->dev, "register %d read failed, err = %d\n",
168 max->curr_vout_reg, ret);
169 return ret;
170 }
171 return data & MAX8973_VOUT_MASK;
172}
173
174static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
175 unsigned vsel)
176{
177 struct max8973_chip *max = rdev_get_drvdata(rdev);
178 int ret;
179 bool found = false;
180 int vout_reg = max->curr_vout_reg;
181 int gpio_val = max->curr_gpio_val;
182
183 /*
184 * If gpios are available to select the VOUT register then least
185 * recently used register for new configuration.
186 */
Laxman Dewanganfa5f5092015-05-06 18:20:10 +0530187 if (gpio_is_valid(max->dvs_gpio))
Laxman Dewangan5928f532012-11-19 06:58:29 +0530188 found = find_voltage_set_register(max, vsel,
189 &vout_reg, &gpio_val);
190
191 if (!found) {
192 ret = regmap_update_bits(max->regmap, vout_reg,
193 MAX8973_VOUT_MASK, vsel);
194 if (ret < 0) {
195 dev_err(max->dev, "register %d update failed, err %d\n",
196 vout_reg, ret);
197 return ret;
198 }
199 max->curr_vout_reg = vout_reg;
200 max->curr_vout_val[gpio_val] = vsel;
201 }
202
203 /* Select proper VOUT register vio gpios */
Laxman Dewanganfa5f5092015-05-06 18:20:10 +0530204 if (gpio_is_valid(max->dvs_gpio)) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530205 gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
206 max->curr_gpio_val = gpio_val;
207 }
208 return 0;
209}
210
211static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
212{
213 struct max8973_chip *max = rdev_get_drvdata(rdev);
214 int ret;
215 int pwm;
216
217 /* Enable force PWM mode in FAST mode only. */
218 switch (mode) {
219 case REGULATOR_MODE_FAST:
220 pwm = MAX8973_FPWM_EN_M;
221 break;
222
223 case REGULATOR_MODE_NORMAL:
224 pwm = 0;
225 break;
226
227 default:
228 return -EINVAL;
229 }
230
231 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
232 MAX8973_FPWM_EN_M, pwm);
233 if (ret < 0)
234 dev_err(max->dev, "register %d update failed, err %d\n",
235 MAX8973_CONTROL1, ret);
236 return ret;
237}
238
239static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
240{
241 struct max8973_chip *max = rdev_get_drvdata(rdev);
242 unsigned int data;
243 int ret;
244
245 ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
246 if (ret < 0) {
247 dev_err(max->dev, "register %d read failed, err %d\n",
248 MAX8973_CONTROL1, ret);
249 return ret;
250 }
251 return (data & MAX8973_FPWM_EN_M) ?
252 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
253}
254
Laxman Dewangan3064c1f2015-06-09 19:17:52 +0530255static int max8973_set_ramp_delay(struct regulator_dev *rdev,
256 int ramp_delay)
257{
258 struct max8973_chip *max = rdev_get_drvdata(rdev);
259 unsigned int control;
260 int ret;
261 int ret_val;
262
263 /* Set ramp delay */
264 if (ramp_delay < 25000) {
265 control = MAX8973_RAMP_12mV_PER_US;
266 ret_val = 12000;
267 } else if (ramp_delay < 50000) {
268 control = MAX8973_RAMP_25mV_PER_US;
269 ret_val = 25000;
270 } else if (ramp_delay < 200000) {
271 control = MAX8973_RAMP_50mV_PER_US;
272 ret_val = 50000;
273 } else {
274 control = MAX8973_RAMP_200mV_PER_US;
275 ret_val = 200000;
276 }
277
278 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
279 MAX8973_RAMP_MASK, control);
280 if (ret < 0)
281 dev_err(max->dev, "register %d update failed, %d",
282 MAX8973_CONTROL1, ret);
283 return ret;
284}
285
Laxman Dewanganffaab992015-07-01 18:31:44 +0530286static int max8973_set_current_limit(struct regulator_dev *rdev,
287 int min_ua, int max_ua)
288{
289 struct max8973_chip *max = rdev_get_drvdata(rdev);
290 unsigned int val;
291 int ret;
292
293 if (max_ua <= 9000000)
294 val = MAX8973_CKKADV_TRIP_75mV_PER_US;
295 else if (max_ua <= 12000000)
296 val = MAX8973_CKKADV_TRIP_150mV_PER_US;
297 else
298 val = MAX8973_CKKADV_TRIP_DISABLE;
299
300 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL2,
301 MAX8973_CKKADV_TRIP_MASK, val);
302 if (ret < 0) {
303 dev_err(max->dev, "register %d update failed: %d\n",
304 MAX8973_CONTROL2, ret);
305 return ret;
306 }
307 return 0;
308}
309
310static int max8973_get_current_limit(struct regulator_dev *rdev)
311{
312 struct max8973_chip *max = rdev_get_drvdata(rdev);
313 unsigned int control2;
314 int ret;
315
316 ret = regmap_read(max->regmap, MAX8973_CONTROL2, &control2);
317 if (ret < 0) {
318 dev_err(max->dev, "register %d read failed: %d\n",
319 MAX8973_CONTROL2, ret);
320 return ret;
321 }
322 switch (control2 & MAX8973_CKKADV_TRIP_MASK) {
323 case MAX8973_CKKADV_TRIP_DISABLE:
324 return 15000000;
325 case MAX8973_CKKADV_TRIP_150mV_PER_US:
326 return 12000000;
327 case MAX8973_CKKADV_TRIP_75mV_PER_US:
328 return 9000000;
329 default:
330 break;
331 }
332 return 9000000;
333}
334
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200335static const struct regulator_ops max8973_dcdc_ops = {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530336 .get_voltage_sel = max8973_dcdc_get_voltage_sel,
337 .set_voltage_sel = max8973_dcdc_set_voltage_sel,
338 .list_voltage = regulator_list_voltage_linear,
339 .set_mode = max8973_dcdc_set_mode,
340 .get_mode = max8973_dcdc_get_mode,
Laxman Dewangan3064c1f2015-06-09 19:17:52 +0530341 .set_voltage_time_sel = regulator_set_voltage_time_sel,
342 .set_ramp_delay = max8973_set_ramp_delay,
Laxman Dewangan5928f532012-11-19 06:58:29 +0530343};
344
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800345static int max8973_init_dcdc(struct max8973_chip *max,
346 struct max8973_regulator_platform_data *pdata)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530347{
348 int ret;
349 uint8_t control1 = 0;
350 uint8_t control2 = 0;
Laxman Dewangan3064c1f2015-06-09 19:17:52 +0530351 unsigned int data;
352
353 ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
354 if (ret < 0) {
355 dev_err(max->dev, "register %d read failed, err = %d",
356 MAX8973_CONTROL1, ret);
357 return ret;
358 }
359 control1 = data & MAX8973_RAMP_MASK;
360 switch (control1) {
361 case MAX8973_RAMP_12mV_PER_US:
362 max->desc.ramp_delay = 12000;
363 break;
364 case MAX8973_RAMP_25mV_PER_US:
Axel Lin366604e2015-06-10 18:52:11 +0800365 max->desc.ramp_delay = 25000;
Laxman Dewangan3064c1f2015-06-09 19:17:52 +0530366 break;
367 case MAX8973_RAMP_50mV_PER_US:
368 max->desc.ramp_delay = 50000;
369 break;
370 case MAX8973_RAMP_200mV_PER_US:
371 max->desc.ramp_delay = 200000;
372 break;
373 }
Laxman Dewangan5928f532012-11-19 06:58:29 +0530374
375 if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
376 control1 |= MAX8973_SNS_ENABLE;
377
378 if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
379 control1 |= MAX8973_NFSR_ENABLE;
380
381 if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
382 control1 |= MAX8973_AD_ENABLE;
383
Laxman Dewangandcd9ec62015-04-23 16:10:22 +0530384 if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530385 control1 |= MAX8973_BIAS_ENABLE;
Laxman Dewangandcd9ec62015-04-23 16:10:22 +0530386 max->desc.enable_time = 20;
387 } else {
388 max->desc.enable_time = 240;
389 }
Laxman Dewangan5928f532012-11-19 06:58:29 +0530390
391 if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
392 control1 |= MAX8973_FREQSHIFT_9PER;
393
Laxman Dewangan5928f532012-11-19 06:58:29 +0530394 if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
395 control2 |= MAX8973_DISCH_ENBABLE;
396
397 /* Clock advance trip configuration */
398 switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
399 case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
400 control2 |= MAX8973_CKKADV_TRIP_DISABLE;
401 break;
402
403 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
404 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
405 break;
406
407 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
408 control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
409 break;
410
411 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
412 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
413 break;
414 }
415
416 /* Configure inductor value */
417 switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
418 case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
419 control2 |= MAX8973_INDUCTOR_NOMINAL;
420 break;
421
422 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
423 control2 |= MAX8973_INDUCTOR_MIN_30_PER;
424 break;
425
426 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
427 control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
428 break;
429
430 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
431 control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
432 break;
433 }
434
435 ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
436 if (ret < 0) {
437 dev_err(max->dev, "register %d write failed, err = %d",
438 MAX8973_CONTROL1, ret);
439 return ret;
440 }
441
442 ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
443 if (ret < 0) {
444 dev_err(max->dev, "register %d write failed, err = %d",
445 MAX8973_CONTROL2, ret);
446 return ret;
447 }
448
449 /* If external control is enabled then disable EN bit */
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530450 if (max->enable_external_control && (max->id == MAX8973)) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530451 ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
452 MAX8973_VOUT_ENABLE, 0);
453 if (ret < 0)
454 dev_err(max->dev, "register %d update failed, err = %d",
455 MAX8973_VOUT, ret);
456 }
457 return ret;
458}
459
460static const struct regmap_config max8973_regmap_config = {
461 .reg_bits = 8,
462 .val_bits = 8,
463 .max_register = MAX8973_CHIPID2,
464 .cache_type = REGCACHE_RBTREE,
465};
466
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530467static struct max8973_regulator_platform_data *max8973_parse_dt(
468 struct device *dev)
469{
470 struct max8973_regulator_platform_data *pdata;
471 struct device_node *np = dev->of_node;
472 int ret;
473 u32 pval;
Laxman Dewangan3692db32015-07-01 18:31:43 +0530474 bool etr_enable;
475 bool etr_sensitivity_high;
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530476
477 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
478 if (!pdata)
479 return NULL;
480
481 pdata->enable_ext_control = of_property_read_bool(np,
482 "maxim,externally-enable");
Laxman Dewangan69eb0982015-04-23 16:10:24 +0530483 pdata->enable_gpio = of_get_named_gpio(np, "maxim,enable-gpio", 0);
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530484 pdata->dvs_gpio = of_get_named_gpio(np, "maxim,dvs-gpio", 0);
485
486 ret = of_property_read_u32(np, "maxim,dvs-default-state", &pval);
487 if (!ret)
488 pdata->dvs_def_state = pval;
489
490 if (of_property_read_bool(np, "maxim,enable-remote-sense"))
491 pdata->control_flags |= MAX8973_CONTROL_REMOTE_SENSE_ENABLE;
492
493 if (of_property_read_bool(np, "maxim,enable-falling-slew-rate"))
494 pdata->control_flags |=
495 MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE;
496
497 if (of_property_read_bool(np, "maxim,enable-active-discharge"))
498 pdata->control_flags |=
499 MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE;
500
501 if (of_property_read_bool(np, "maxim,enable-frequency-shift"))
502 pdata->control_flags |= MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE;
503
504 if (of_property_read_bool(np, "maxim,enable-bias-control"))
Laxman Dewangan127e1062015-07-01 18:31:42 +0530505 pdata->control_flags |= MAX8973_CONTROL_BIAS_ENABLE;
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530506
Laxman Dewangan3692db32015-07-01 18:31:43 +0530507 etr_enable = of_property_read_bool(np, "maxim,enable-etr");
508 etr_sensitivity_high = of_property_read_bool(np,
509 "maxim,enable-high-etr-sensitivity");
510 if (etr_sensitivity_high)
511 etr_enable = true;
512
513 if (etr_enable) {
514 if (etr_sensitivity_high)
515 pdata->control_flags |=
516 MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US;
517 else
518 pdata->control_flags |=
519 MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US;
520 } else {
521 pdata->control_flags |= MAX8973_CONTROL_CLKADV_TRIP_DISABLED;
522 }
523
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530524 return pdata;
525}
526
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530527static const struct of_device_id of_max8973_match_tbl[] = {
528 { .compatible = "maxim,max8973", .data = (void *)MAX8973, },
529 { .compatible = "maxim,max77621", .data = (void *)MAX77621, },
530 { },
531};
532MODULE_DEVICE_TABLE(of, of_max8973_match_tbl);
533
Greg Kroah-Hartman3d68dfe2012-12-21 13:26:06 -0800534static int max8973_probe(struct i2c_client *client,
535 const struct i2c_device_id *id)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530536{
537 struct max8973_regulator_platform_data *pdata;
Laxman Dewangan69eb0982015-04-23 16:10:24 +0530538 struct regulator_init_data *ridata;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530539 struct regulator_config config = { };
540 struct regulator_dev *rdev;
541 struct max8973_chip *max;
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530542 bool pdata_from_dt = false;
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530543 unsigned int chip_id;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530544 int ret;
545
Jingoo Handff91d02013-07-30 17:20:47 +0900546 pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetski42dc3022013-06-24 12:50:16 +0200547
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530548 if (!pdata && client->dev.of_node) {
549 pdata = max8973_parse_dt(&client->dev);
550 pdata_from_dt = true;
551 }
552
553 if (!pdata) {
Laxman Dewangan5928f532012-11-19 06:58:29 +0530554 dev_err(&client->dev, "No Platform data");
555 return -EIO;
556 }
557
Laxman Dewangan69eb0982015-04-23 16:10:24 +0530558 if ((pdata->dvs_gpio == -EPROBE_DEFER) ||
559 (pdata->enable_gpio == -EPROBE_DEFER))
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530560 return -EPROBE_DEFER;
561
Laxman Dewangan5928f532012-11-19 06:58:29 +0530562 max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
Sachin Kamat21024de2014-02-18 16:11:00 +0530563 if (!max)
Laxman Dewangan5928f532012-11-19 06:58:29 +0530564 return -ENOMEM;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530565
566 max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
567 if (IS_ERR(max->regmap)) {
568 ret = PTR_ERR(max->regmap);
569 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
570 return ret;
571 }
572
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530573 if (client->dev.of_node) {
574 const struct of_device_id *match;
575
576 match = of_match_device(of_match_ptr(of_max8973_match_tbl),
577 &client->dev);
578 if (!match)
579 return -ENODATA;
580 max->id = (u32)((uintptr_t)match->data);
581 } else {
582 max->id = id->driver_data;
583 }
584
585 ret = regmap_read(max->regmap, MAX8973_CHIPID1, &chip_id);
586 if (ret < 0) {
587 dev_err(&client->dev, "register CHIPID1 read failed, %d", ret);
588 return ret;
589 }
590
591 dev_info(&client->dev, "CHIP-ID OTP: 0x%02x ID_M: 0x%02x\n",
592 (chip_id >> 4) & 0xF, (chip_id >> 1) & 0x7);
593
Laxman Dewangan5928f532012-11-19 06:58:29 +0530594 i2c_set_clientdata(client, max);
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200595 max->ops = max8973_dcdc_ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530596 max->dev = &client->dev;
597 max->desc.name = id->name;
598 max->desc.id = 0;
Guennadi Liakhovetskidb892ff2013-06-21 08:30:22 +0200599 max->desc.ops = &max->ops;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530600 max->desc.type = REGULATOR_VOLTAGE;
601 max->desc.owner = THIS_MODULE;
602 max->desc.min_uV = MAX8973_MIN_VOLATGE;
603 max->desc.uV_step = MAX8973_VOLATGE_STEP;
604 max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
605
Laxman Dewangan69eb0982015-04-23 16:10:24 +0530606 max->dvs_gpio = (pdata->dvs_gpio) ? pdata->dvs_gpio : -EINVAL;
607 max->enable_gpio = (pdata->enable_gpio) ? pdata->enable_gpio : -EINVAL;
608 max->enable_external_control = pdata->enable_ext_control;
609 max->curr_gpio_val = pdata->dvs_def_state;
610 max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
611
612 if (gpio_is_valid(max->enable_gpio))
613 max->enable_external_control = true;
614
Laxman Dewangan5928f532012-11-19 06:58:29 +0530615 max->lru_index[0] = max->curr_vout_reg;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530616
617 if (gpio_is_valid(max->dvs_gpio)) {
618 int gpio_flags;
619 int i;
620
621 gpio_flags = (pdata->dvs_def_state) ?
622 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
623 ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
624 gpio_flags, "max8973-dvs");
625 if (ret) {
626 dev_err(&client->dev,
627 "gpio_request for gpio %d failed, err = %d\n",
628 max->dvs_gpio, ret);
629 return ret;
630 }
Laxman Dewangan5928f532012-11-19 06:58:29 +0530631
632 /*
633 * Initialize the lru index with vout_reg id
634 * The index 0 will be most recently used and
635 * set with the max->curr_vout_reg */
636 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
637 max->lru_index[i] = i;
638 max->lru_index[0] = max->curr_vout_reg;
639 max->lru_index[max->curr_vout_reg] = 0;
Mikko Perttunenb10c7f32015-07-28 11:34:11 +0300640 } else {
641 /*
642 * If there is no DVS GPIO, the VOUT register
643 * address is fixed.
644 */
645 max->ops.set_voltage_sel = regulator_set_voltage_sel_regmap;
646 max->ops.get_voltage_sel = regulator_get_voltage_sel_regmap;
647 max->desc.vsel_reg = max->curr_vout_reg;
648 max->desc.vsel_mask = MAX8973_VOUT_MASK;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530649 }
650
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530651 if (pdata_from_dt)
652 pdata->reg_init_data = of_get_regulator_init_data(&client->dev,
653 client->dev.of_node, &max->desc);
654
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530655 ridata = pdata->reg_init_data;
656 switch (max->id) {
657 case MAX8973:
658 if (!pdata->enable_ext_control) {
659 max->desc.enable_reg = MAX8973_VOUT;
660 max->desc.enable_mask = MAX8973_VOUT_ENABLE;
661 max->ops.enable = regulator_enable_regmap;
662 max->ops.disable = regulator_disable_regmap;
663 max->ops.is_enabled = regulator_is_enabled_regmap;
664 break;
665 }
666
667 if (gpio_is_valid(max->enable_gpio)) {
668 config.ena_gpio_flags = GPIOF_OUT_INIT_LOW;
669 if (ridata && (ridata->constraints.always_on ||
670 ridata->constraints.boot_on))
671 config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
672 config.ena_gpio = max->enable_gpio;
673 }
674 break;
675
676 case MAX77621:
677 if (gpio_is_valid(max->enable_gpio)) {
678 ret = devm_gpio_request_one(&client->dev,
679 max->enable_gpio, GPIOF_OUT_INIT_HIGH,
680 "max8973-en-gpio");
681 if (ret) {
682 dev_err(&client->dev,
683 "gpio_request for gpio %d failed: %d\n",
684 max->enable_gpio, ret);
685 return ret;
686 }
687 }
688
689 max->desc.enable_reg = MAX8973_VOUT;
690 max->desc.enable_mask = MAX8973_VOUT_ENABLE;
691 max->ops.enable = regulator_enable_regmap;
692 max->ops.disable = regulator_disable_regmap;
693 max->ops.is_enabled = regulator_is_enabled_regmap;
Laxman Dewanganffaab992015-07-01 18:31:44 +0530694 max->ops.set_current_limit = max8973_set_current_limit;
695 max->ops.get_current_limit = max8973_get_current_limit;
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530696 break;
697 default:
698 break;
699 }
700
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530701 ret = max8973_init_dcdc(max, pdata);
702 if (ret < 0) {
703 dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
704 return ret;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530705 }
706
707 config.dev = &client->dev;
Laxman Dewanganc2ffa972015-04-23 16:10:23 +0530708 config.init_data = pdata->reg_init_data;
Laxman Dewangan5928f532012-11-19 06:58:29 +0530709 config.driver_data = max;
710 config.of_node = client->dev.of_node;
711 config.regmap = max->regmap;
712
713 /* Register the regulators */
Sachin Kamat8d581fd2013-09-04 11:08:00 +0530714 rdev = devm_regulator_register(&client->dev, &max->desc, &config);
Laxman Dewangan5928f532012-11-19 06:58:29 +0530715 if (IS_ERR(rdev)) {
716 ret = PTR_ERR(rdev);
717 dev_err(max->dev, "regulator register failed, err %d\n", ret);
718 return ret;
719 }
720
Laxman Dewangan5928f532012-11-19 06:58:29 +0530721 return 0;
722}
723
Laxman Dewangan5928f532012-11-19 06:58:29 +0530724static const struct i2c_device_id max8973_id[] = {
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530725 {.name = "max8973", .driver_data = MAX8973},
726 {.name = "max77621", .driver_data = MAX77621},
Laxman Dewangan5928f532012-11-19 06:58:29 +0530727 {},
728};
Laxman Dewangan5928f532012-11-19 06:58:29 +0530729MODULE_DEVICE_TABLE(i2c, max8973_id);
730
731static struct i2c_driver max8973_i2c_driver = {
732 .driver = {
733 .name = "max8973",
Laxman Dewangan0f7d6ec2015-06-09 19:17:53 +0530734 .of_match_table = of_max8973_match_tbl,
Laxman Dewangan5928f532012-11-19 06:58:29 +0530735 },
736 .probe = max8973_probe,
Laxman Dewangan5928f532012-11-19 06:58:29 +0530737 .id_table = max8973_id,
738};
739
740static int __init max8973_init(void)
741{
742 return i2c_add_driver(&max8973_i2c_driver);
743}
744subsys_initcall(max8973_init);
745
746static void __exit max8973_cleanup(void)
747{
748 i2c_del_driver(&max8973_i2c_driver);
749}
750module_exit(max8973_cleanup);
751
752MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
753MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
754MODULE_LICENSE("GPL v2");