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