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>
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>
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
46 #define MAX8973_MAX_VOUT_REG 2
49 #define MAX8973_VOUT_ENABLE BIT(7)
50 #define MAX8973_VOUT_MASK 0x7F
52 /* MAX8973_VOUT_DVS */
53 #define MAX8973_DVS_VOUT_MASK 0x7F
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)
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
68 /* MAX8973_CONTROL2 */
69 #define MAX8973_WDTMR_ENABLE BIT(6)
70 #define MAX8973_DISCH_ENBABLE BIT(5)
71 #define MAX8973_FT_ENABLE BIT(4)
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
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
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
90 /* Maxim 8973 chip information */
93 struct regulator_desc desc
;
94 struct regulator_dev
*rdev
;
95 struct regmap
*regmap
;
96 bool enable_external_control
;
98 int lru_index
[MAX8973_MAX_VOUT_REG
];
99 int curr_vout_val
[MAX8973_MAX_VOUT_REG
];
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
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
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).
120 static bool find_voltage_set_register(struct max8973_chip
*tps
,
121 int req_vsel
, int *vout_reg
, int *gpio_val
)
125 int new_vout_reg
= tps
->lru_index
[MAX8973_MAX_VOUT_REG
- 1];
126 int found_index
= MAX8973_MAX_VOUT_REG
- 1;
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
];
133 goto update_lru_index
;
138 for (i
= found_index
; i
> 0; i
--)
139 tps
->lru_index
[i
] = tps
->lru_index
[i
- 1];
141 tps
->lru_index
[0] = new_vout_reg
;
142 *gpio_val
= new_vout_reg
;
143 *vout_reg
= MAX8973_VOUT
+ new_vout_reg
;
147 static int max8973_dcdc_get_voltage_sel(struct regulator_dev
*rdev
)
149 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
153 ret
= regmap_read(max
->regmap
, max
->curr_vout_reg
, &data
);
155 dev_err(max
->dev
, "register %d read failed, err = %d\n",
156 max
->curr_vout_reg
, ret
);
159 return data
& MAX8973_VOUT_MASK
;
162 static int max8973_dcdc_set_voltage_sel(struct regulator_dev
*rdev
,
165 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
168 int vout_reg
= max
->curr_vout_reg
;
169 int gpio_val
= max
->curr_gpio_val
;
172 * If gpios are available to select the VOUT register then least
173 * recently used register for new configuration.
175 if (max
->valid_dvs_gpio
)
176 found
= find_voltage_set_register(max
, vsel
,
177 &vout_reg
, &gpio_val
);
180 ret
= regmap_update_bits(max
->regmap
, vout_reg
,
181 MAX8973_VOUT_MASK
, vsel
);
183 dev_err(max
->dev
, "register %d update failed, err %d\n",
187 max
->curr_vout_reg
= vout_reg
;
188 max
->curr_vout_val
[gpio_val
] = vsel
;
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
;
199 static int max8973_dcdc_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
201 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
205 /* Enable force PWM mode in FAST mode only. */
207 case REGULATOR_MODE_FAST
:
208 pwm
= MAX8973_FPWM_EN_M
;
211 case REGULATOR_MODE_NORMAL
:
219 ret
= regmap_update_bits(max
->regmap
, MAX8973_CONTROL1
,
220 MAX8973_FPWM_EN_M
, pwm
);
222 dev_err(max
->dev
, "register %d update failed, err %d\n",
223 MAX8973_CONTROL1
, ret
);
227 static unsigned int max8973_dcdc_get_mode(struct regulator_dev
*rdev
)
229 struct max8973_chip
*max
= rdev_get_drvdata(rdev
);
233 ret
= regmap_read(max
->regmap
, MAX8973_CONTROL1
, &data
);
235 dev_err(max
->dev
, "register %d read failed, err %d\n",
236 MAX8973_CONTROL1
, ret
);
239 return (data
& MAX8973_FPWM_EN_M
) ?
240 REGULATOR_MODE_FAST
: REGULATOR_MODE_NORMAL
;
243 static 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
,
251 static int max8973_init_dcdc(struct max8973_chip
*max
,
252 struct max8973_regulator_platform_data
*pdata
)
255 uint8_t control1
= 0;
256 uint8_t control2
= 0;
258 if (pdata
->control_flags
& MAX8973_CONTROL_REMOTE_SENSE_ENABLE
)
259 control1
|= MAX8973_SNS_ENABLE
;
261 if (!(pdata
->control_flags
& MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE
))
262 control1
|= MAX8973_NFSR_ENABLE
;
264 if (pdata
->control_flags
& MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE
)
265 control1
|= MAX8973_AD_ENABLE
;
267 if (pdata
->control_flags
& MAX8973_CONTROL_BIAS_ENABLE
)
268 control1
|= MAX8973_BIAS_ENABLE
;
270 if (pdata
->control_flags
& MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE
)
271 control1
|= MAX8973_FREQSHIFT_9PER
;
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
;
283 control1
|= MAX8973_RAMP_200mV_PER_US
;
285 control1
|= MAX8973_RAMP_12mV_PER_US
;
286 max
->desc
.ramp_delay
= 12500;
289 if (!(pdata
->control_flags
& MAX8973_CONTROL_PULL_DOWN_ENABLE
))
290 control2
|= MAX8973_DISCH_ENBABLE
;
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
;
298 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US
:
299 control2
|= MAX8973_CKKADV_TRIP_75mV_PER_US
;
302 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US
:
303 control2
|= MAX8973_CKKADV_TRIP_150mV_PER_US
;
306 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS
:
307 control2
|= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS
;
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
;
317 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER
:
318 control2
|= MAX8973_INDUCTOR_MIN_30_PER
;
321 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER
:
322 control2
|= MAX8973_INDUCTOR_PLUS_30_PER
;
325 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER
:
326 control2
|= MAX8973_INDUCTOR_PLUS_60_PER
;
330 ret
= regmap_write(max
->regmap
, MAX8973_CONTROL1
, control1
);
332 dev_err(max
->dev
, "register %d write failed, err = %d",
333 MAX8973_CONTROL1
, ret
);
337 ret
= regmap_write(max
->regmap
, MAX8973_CONTROL2
, control2
);
339 dev_err(max
->dev
, "register %d write failed, err = %d",
340 MAX8973_CONTROL2
, ret
);
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);
349 dev_err(max
->dev
, "register %d update failed, err = %d",
355 static const struct regmap_config max8973_regmap_config
= {
358 .max_register
= MAX8973_CHIPID2
,
359 .cache_type
= REGCACHE_RBTREE
,
362 static int max8973_probe(struct i2c_client
*client
,
363 const struct i2c_device_id
*id
)
365 struct max8973_regulator_platform_data
*pdata
;
366 struct regulator_config config
= { };
367 struct regulator_dev
*rdev
;
368 struct max8973_chip
*max
;
371 pdata
= client
->dev
.platform_data
;
373 dev_err(&client
->dev
, "No Platform data");
377 max
= devm_kzalloc(&client
->dev
, sizeof(*max
), GFP_KERNEL
);
379 dev_err(&client
->dev
, "Memory allocation for max failed\n");
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
);
390 i2c_set_clientdata(client
, max
);
391 max
->dev
= &client
->dev
;
392 max
->desc
.name
= id
->name
;
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
;
401 if (!pdata
->enable_ext_control
) {
402 max
->desc
.enable_reg
= MAX8973_VOUT
;
403 max
->desc
.enable_mask
= MAX8973_VOUT_ENABLE
;
404 max8973_dcdc_ops
.enable
= regulator_enable_regmap
;
405 max8973_dcdc_ops
.disable
= regulator_disable_regmap
;
406 max8973_dcdc_ops
.is_enabled
= regulator_is_enabled_regmap
;
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;
416 if (gpio_is_valid(max
->dvs_gpio
)) {
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");
425 dev_err(&client
->dev
,
426 "gpio_request for gpio %d failed, err = %d\n",
430 max
->valid_dvs_gpio
= true;
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;
442 ret
= max8973_init_dcdc(max
, pdata
);
444 dev_err(max
->dev
, "Max8973 Init failed, err = %d\n", ret
);
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
;
454 /* Register the regulators */
455 rdev
= regulator_register(&max
->desc
, &config
);
458 dev_err(max
->dev
, "regulator register failed, err %d\n", ret
);
466 static int max8973_remove(struct i2c_client
*client
)
468 struct max8973_chip
*max
= i2c_get_clientdata(client
);
470 regulator_unregister(max
->rdev
);
474 static const struct i2c_device_id max8973_id
[] = {
475 {.name
= "max8973",},
479 MODULE_DEVICE_TABLE(i2c
, max8973_id
);
481 static struct i2c_driver max8973_i2c_driver
= {
484 .owner
= THIS_MODULE
,
486 .probe
= max8973_probe
,
487 .remove
= max8973_remove
,
488 .id_table
= max8973_id
,
491 static int __init
max8973_init(void)
493 return i2c_add_driver(&max8973_i2c_driver
);
495 subsys_initcall(max8973_init
);
497 static void __exit
max8973_cleanup(void)
499 i2c_del_driver(&max8973_i2c_driver
);
501 module_exit(max8973_cleanup
);
503 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
504 MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
505 MODULE_LICENSE("GPL v2");