2 * max8973-regulator.c -- Maxim max8973
4 * Regulator driver for MAXIM 8973 DC-DC step-down switching regulator.
6 * Copyright (c) 2012, NVIDIA Corporation.
8 * Author: Laxman Dewangan <ldewangan@nvidia.com>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
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>
34 #include <linux/regulator/of_regulator.h>
35 #include <linux/gpio.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
38 #include <linux/regmap.h>
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
48 #define MAX8973_MAX_VOUT_REG 2
51 #define MAX8973_VOUT_ENABLE BIT(7)
52 #define MAX8973_VOUT_MASK 0x7F
54 /* MAX8973_VOUT_DVS */
55 #define MAX8973_DVS_VOUT_MASK 0x7F
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)
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
70 /* MAX8973_CONTROL2 */
71 #define MAX8973_WDTMR_ENABLE BIT(6)
72 #define MAX8973_DISCH_ENBABLE BIT(5)
73 #define MAX8973_FT_ENABLE BIT(4)
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
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
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
92 /* Maxim 8973 chip information */
95 struct regulator_desc desc
;
96 struct regmap
*regmap
;
97 bool enable_external_control
;
99 int lru_index
[MAX8973_MAX_VOUT_REG
];
100 int curr_vout_val
[MAX8973_MAX_VOUT_REG
];
104 struct regulator_ops ops
;
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
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
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).
122 static bool find_voltage_set_register(struct max8973_chip
*tps
,
123 int req_vsel
, int *vout_reg
, int *gpio_val
)
127 int new_vout_reg
= tps
->lru_index
[MAX8973_MAX_VOUT_REG
- 1];
128 int found_index
= MAX8973_MAX_VOUT_REG
- 1;
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
];
135 goto update_lru_index
;
140 for (i
= found_index
; i
> 0; i
--)
141 tps
->lru_index
[i
] = tps
->lru_index
[i
- 1];
143 tps
->lru_index
[0] = new_vout_reg
;
144 *gpio_val
= new_vout_reg
;
145 *vout_reg
= MAX8973_VOUT
+ new_vout_reg
;
149 static int max8973_dcdc_get_voltage_sel(struct regulator_dev
*rdev
)
151 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
155 ret
= regmap_read(max
->regmap
, max
->curr_vout_reg
, &data
);
157 dev_err(max
->dev
, "register %d read failed, err = %d\n",
158 max
->curr_vout_reg
, ret
);
161 return data
& MAX8973_VOUT_MASK
;
164 static int max8973_dcdc_set_voltage_sel(struct regulator_dev
*rdev
,
167 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
170 int vout_reg
= max
->curr_vout_reg
;
171 int gpio_val
= max
->curr_gpio_val
;
174 * If gpios are available to select the VOUT register then least
175 * recently used register for new configuration.
177 if (max
->valid_dvs_gpio
)
178 found
= find_voltage_set_register(max
, vsel
,
179 &vout_reg
, &gpio_val
);
182 ret
= regmap_update_bits(max
->regmap
, vout_reg
,
183 MAX8973_VOUT_MASK
, vsel
);
185 dev_err(max
->dev
, "register %d update failed, err %d\n",
189 max
->curr_vout_reg
= vout_reg
;
190 max
->curr_vout_val
[gpio_val
] = vsel
;
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
;
201 static int max8973_dcdc_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
203 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
207 /* Enable force PWM mode in FAST mode only. */
209 case REGULATOR_MODE_FAST
:
210 pwm
= MAX8973_FPWM_EN_M
;
213 case REGULATOR_MODE_NORMAL
:
221 ret
= regmap_update_bits(max
->regmap
, MAX8973_CONTROL1
,
222 MAX8973_FPWM_EN_M
, pwm
);
224 dev_err(max
->dev
, "register %d update failed, err %d\n",
225 MAX8973_CONTROL1
, ret
);
229 static unsigned int max8973_dcdc_get_mode(struct regulator_dev
*rdev
)
231 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
235 ret
= regmap_read(max
->regmap
, MAX8973_CONTROL1
, &data
);
237 dev_err(max
->dev
, "register %d read failed, err %d\n",
238 MAX8973_CONTROL1
, ret
);
241 return (data
& MAX8973_FPWM_EN_M
) ?
242 REGULATOR_MODE_FAST
: REGULATOR_MODE_NORMAL
;
245 static const struct regulator_ops max8973_dcdc_ops
= {
246 .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
,
253 static int max8973_init_dcdc(struct max8973_chip
*max
,
254 struct max8973_regulator_platform_data
*pdata
)
257 uint8_t control1
= 0;
258 uint8_t control2
= 0;
260 if (pdata
->control_flags
& MAX8973_CONTROL_REMOTE_SENSE_ENABLE
)
261 control1
|= MAX8973_SNS_ENABLE
;
263 if (!(pdata
->control_flags
& MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE
))
264 control1
|= MAX8973_NFSR_ENABLE
;
266 if (pdata
->control_flags
& MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE
)
267 control1
|= MAX8973_AD_ENABLE
;
269 if (pdata
->control_flags
& MAX8973_CONTROL_BIAS_ENABLE
)
270 control1
|= MAX8973_BIAS_ENABLE
;
272 if (pdata
->control_flags
& MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE
)
273 control1
|= MAX8973_FREQSHIFT_9PER
;
276 if (pdata
->reg_init_data
&&
277 pdata
->reg_init_data
->constraints
.ramp_delay
) {
278 if (pdata
->reg_init_data
->constraints
.ramp_delay
< 25000)
279 control1
|= MAX8973_RAMP_12mV_PER_US
;
280 else if (pdata
->reg_init_data
->constraints
.ramp_delay
< 50000)
281 control1
|= MAX8973_RAMP_25mV_PER_US
;
282 else if (pdata
->reg_init_data
->constraints
.ramp_delay
< 200000)
283 control1
|= MAX8973_RAMP_50mV_PER_US
;
285 control1
|= MAX8973_RAMP_200mV_PER_US
;
287 control1
|= MAX8973_RAMP_12mV_PER_US
;
288 max
->desc
.ramp_delay
= 12500;
291 if (!(pdata
->control_flags
& MAX8973_CONTROL_PULL_DOWN_ENABLE
))
292 control2
|= MAX8973_DISCH_ENBABLE
;
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
;
300 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US
:
301 control2
|= MAX8973_CKKADV_TRIP_75mV_PER_US
;
304 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US
:
305 control2
|= MAX8973_CKKADV_TRIP_150mV_PER_US
;
308 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS
:
309 control2
|= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS
;
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
;
319 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER
:
320 control2
|= MAX8973_INDUCTOR_MIN_30_PER
;
323 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER
:
324 control2
|= MAX8973_INDUCTOR_PLUS_30_PER
;
327 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER
:
328 control2
|= MAX8973_INDUCTOR_PLUS_60_PER
;
332 ret
= regmap_write(max
->regmap
, MAX8973_CONTROL1
, control1
);
334 dev_err(max
->dev
, "register %d write failed, err = %d",
335 MAX8973_CONTROL1
, ret
);
339 ret
= regmap_write(max
->regmap
, MAX8973_CONTROL2
, control2
);
341 dev_err(max
->dev
, "register %d write failed, err = %d",
342 MAX8973_CONTROL2
, ret
);
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);
351 dev_err(max
->dev
, "register %d update failed, err = %d",
357 static const struct regmap_config max8973_regmap_config
= {
360 .max_register
= MAX8973_CHIPID2
,
361 .cache_type
= REGCACHE_RBTREE
,
364 static int max8973_probe(struct i2c_client
*client
,
365 const struct i2c_device_id
*id
)
367 struct max8973_regulator_platform_data
*pdata
;
368 struct regulator_config config
= { };
369 struct regulator_dev
*rdev
;
370 struct max8973_chip
*max
;
373 pdata
= dev_get_platdata(&client
->dev
);
375 if (!pdata
&& !client
->dev
.of_node
) {
376 dev_err(&client
->dev
, "No Platform data");
380 max
= devm_kzalloc(&client
->dev
, sizeof(*max
), GFP_KERNEL
);
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
);
391 i2c_set_clientdata(client
, max
);
392 max
->ops
= max8973_dcdc_ops
;
393 max
->dev
= &client
->dev
;
394 max
->desc
.name
= id
->name
;
396 max
->desc
.ops
= &max
->ops
;
397 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
;
403 if (!pdata
|| !pdata
->enable_ext_control
) {
404 max
->desc
.enable_reg
= MAX8973_VOUT
;
405 max
->desc
.enable_mask
= MAX8973_VOUT_ENABLE
;
406 max
->ops
.enable
= regulator_enable_regmap
;
407 max
->ops
.disable
= regulator_disable_regmap
;
408 max
->ops
.is_enabled
= regulator_is_enabled_regmap
;
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
;
417 max
->dvs_gpio
= -EINVAL
;
418 max
->curr_vout_reg
= MAX8973_VOUT
;
421 max
->lru_index
[0] = max
->curr_vout_reg
;
423 if (gpio_is_valid(max
->dvs_gpio
)) {
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");
432 dev_err(&client
->dev
,
433 "gpio_request for gpio %d failed, err = %d\n",
437 max
->valid_dvs_gpio
= true;
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;
448 max
->valid_dvs_gpio
= false;
452 ret
= max8973_init_dcdc(max
, pdata
);
454 dev_err(max
->dev
, "Max8973 Init failed, err = %d\n", ret
);
459 config
.dev
= &client
->dev
;
460 config
.init_data
= pdata
? pdata
->reg_init_data
:
461 of_get_regulator_init_data(&client
->dev
, client
->dev
.of_node
,
463 config
.driver_data
= max
;
464 config
.of_node
= client
->dev
.of_node
;
465 config
.regmap
= max
->regmap
;
467 /* Register the regulators */
468 rdev
= devm_regulator_register(&client
->dev
, &max
->desc
, &config
);
471 dev_err(max
->dev
, "regulator register failed, err %d\n", ret
);
478 static const struct i2c_device_id max8973_id
[] = {
479 {.name
= "max8973",},
483 MODULE_DEVICE_TABLE(i2c
, max8973_id
);
485 static struct i2c_driver max8973_i2c_driver
= {
488 .owner
= THIS_MODULE
,
490 .probe
= max8973_probe
,
491 .id_table
= max8973_id
,
494 static int __init
max8973_init(void)
496 return i2c_add_driver(&max8973_i2c_driver
);
498 subsys_initcall(max8973_init
);
500 static void __exit
max8973_cleanup(void)
502 i2c_del_driver(&max8973_i2c_driver
);
504 module_exit(max8973_cleanup
);
506 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
507 MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
508 MODULE_LICENSE("GPL v2");