PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / regulator / max8973-regulator.c
blob892aa1e5b96c65dc2d879be20b3bb97b290af67a
1 /*
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
22 * 02111-1307, USA
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/err.h>
29 #include <linux/of.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
50 /* MAX8973_VOUT */
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 */
93 struct max8973_chip {
94 struct device *dev;
95 struct regulator_desc desc;
96 struct regulator_dev *rdev;
97 struct regmap *regmap;
98 bool enable_external_control;
99 int dvs_gpio;
100 int lru_index[MAX8973_MAX_VOUT_REG];
101 int curr_vout_val[MAX8973_MAX_VOUT_REG];
102 int curr_vout_reg;
103 int curr_gpio_val;
104 bool valid_dvs_gpio;
105 struct regulator_ops ops;
109 * find_voltage_set_register: Find new voltage configuration register (VOUT).
110 * The finding of the new VOUT register will be based on the LRU mechanism.
111 * Each VOUT register will have different voltage configured . This
112 * Function will look if any of the VOUT register have requested voltage set
113 * or not.
114 * - If it is already there then it will make that register as most
115 * recently used and return as found so that caller need not to set
116 * the VOUT register but need to set the proper gpios to select this
117 * VOUT register.
118 * - If requested voltage is not found then it will use the least
119 * recently mechanism to get new VOUT register for new configuration
120 * and will return not_found so that caller need to set new VOUT
121 * register and then gpios (both).
123 static bool find_voltage_set_register(struct max8973_chip *tps,
124 int req_vsel, int *vout_reg, int *gpio_val)
126 int i;
127 bool found = false;
128 int new_vout_reg = tps->lru_index[MAX8973_MAX_VOUT_REG - 1];
129 int found_index = MAX8973_MAX_VOUT_REG - 1;
131 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i) {
132 if (tps->curr_vout_val[tps->lru_index[i]] == req_vsel) {
133 new_vout_reg = tps->lru_index[i];
134 found_index = i;
135 found = true;
136 goto update_lru_index;
140 update_lru_index:
141 for (i = found_index; i > 0; i--)
142 tps->lru_index[i] = tps->lru_index[i - 1];
144 tps->lru_index[0] = new_vout_reg;
145 *gpio_val = new_vout_reg;
146 *vout_reg = MAX8973_VOUT + new_vout_reg;
147 return found;
150 static int max8973_dcdc_get_voltage_sel(struct regulator_dev *rdev)
152 struct max8973_chip *max = rdev_get_drvdata(rdev);
153 unsigned int data;
154 int ret;
156 ret = regmap_read(max->regmap, max->curr_vout_reg, &data);
157 if (ret < 0) {
158 dev_err(max->dev, "register %d read failed, err = %d\n",
159 max->curr_vout_reg, ret);
160 return ret;
162 return data & MAX8973_VOUT_MASK;
165 static int max8973_dcdc_set_voltage_sel(struct regulator_dev *rdev,
166 unsigned vsel)
168 struct max8973_chip *max = rdev_get_drvdata(rdev);
169 int ret;
170 bool found = false;
171 int vout_reg = max->curr_vout_reg;
172 int gpio_val = max->curr_gpio_val;
175 * If gpios are available to select the VOUT register then least
176 * recently used register for new configuration.
178 if (max->valid_dvs_gpio)
179 found = find_voltage_set_register(max, vsel,
180 &vout_reg, &gpio_val);
182 if (!found) {
183 ret = regmap_update_bits(max->regmap, vout_reg,
184 MAX8973_VOUT_MASK, vsel);
185 if (ret < 0) {
186 dev_err(max->dev, "register %d update failed, err %d\n",
187 vout_reg, ret);
188 return ret;
190 max->curr_vout_reg = vout_reg;
191 max->curr_vout_val[gpio_val] = vsel;
194 /* Select proper VOUT register vio gpios */
195 if (max->valid_dvs_gpio) {
196 gpio_set_value_cansleep(max->dvs_gpio, gpio_val & 0x1);
197 max->curr_gpio_val = gpio_val;
199 return 0;
202 static int max8973_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
204 struct max8973_chip *max = rdev_get_drvdata(rdev);
205 int ret;
206 int pwm;
208 /* Enable force PWM mode in FAST mode only. */
209 switch (mode) {
210 case REGULATOR_MODE_FAST:
211 pwm = MAX8973_FPWM_EN_M;
212 break;
214 case REGULATOR_MODE_NORMAL:
215 pwm = 0;
216 break;
218 default:
219 return -EINVAL;
222 ret = regmap_update_bits(max->regmap, MAX8973_CONTROL1,
223 MAX8973_FPWM_EN_M, pwm);
224 if (ret < 0)
225 dev_err(max->dev, "register %d update failed, err %d\n",
226 MAX8973_CONTROL1, ret);
227 return ret;
230 static unsigned int max8973_dcdc_get_mode(struct regulator_dev *rdev)
232 struct max8973_chip *max = rdev_get_drvdata(rdev);
233 unsigned int data;
234 int ret;
236 ret = regmap_read(max->regmap, MAX8973_CONTROL1, &data);
237 if (ret < 0) {
238 dev_err(max->dev, "register %d read failed, err %d\n",
239 MAX8973_CONTROL1, ret);
240 return ret;
242 return (data & MAX8973_FPWM_EN_M) ?
243 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
246 static const struct regulator_ops max8973_dcdc_ops = {
247 .get_voltage_sel = max8973_dcdc_get_voltage_sel,
248 .set_voltage_sel = max8973_dcdc_set_voltage_sel,
249 .list_voltage = regulator_list_voltage_linear,
250 .set_mode = max8973_dcdc_set_mode,
251 .get_mode = max8973_dcdc_get_mode,
254 static int max8973_init_dcdc(struct max8973_chip *max,
255 struct max8973_regulator_platform_data *pdata)
257 int ret;
258 uint8_t control1 = 0;
259 uint8_t control2 = 0;
261 if (pdata->control_flags & MAX8973_CONTROL_REMOTE_SENSE_ENABLE)
262 control1 |= MAX8973_SNS_ENABLE;
264 if (!(pdata->control_flags & MAX8973_CONTROL_FALLING_SLEW_RATE_ENABLE))
265 control1 |= MAX8973_NFSR_ENABLE;
267 if (pdata->control_flags & MAX8973_CONTROL_OUTPUT_ACTIVE_DISCH_ENABLE)
268 control1 |= MAX8973_AD_ENABLE;
270 if (pdata->control_flags & MAX8973_CONTROL_BIAS_ENABLE)
271 control1 |= MAX8973_BIAS_ENABLE;
273 if (pdata->control_flags & MAX8973_CONTROL_FREQ_SHIFT_9PER_ENABLE)
274 control1 |= MAX8973_FREQSHIFT_9PER;
276 /* Set ramp delay */
277 if (pdata->reg_init_data &&
278 pdata->reg_init_data->constraints.ramp_delay) {
279 if (pdata->reg_init_data->constraints.ramp_delay < 25000)
280 control1 |= MAX8973_RAMP_12mV_PER_US;
281 else if (pdata->reg_init_data->constraints.ramp_delay < 50000)
282 control1 |= MAX8973_RAMP_25mV_PER_US;
283 else if (pdata->reg_init_data->constraints.ramp_delay < 200000)
284 control1 |= MAX8973_RAMP_50mV_PER_US;
285 else
286 control1 |= MAX8973_RAMP_200mV_PER_US;
287 } else {
288 control1 |= MAX8973_RAMP_12mV_PER_US;
289 max->desc.ramp_delay = 12500;
292 if (!(pdata->control_flags & MAX8973_CONTROL_PULL_DOWN_ENABLE))
293 control2 |= MAX8973_DISCH_ENBABLE;
295 /* Clock advance trip configuration */
296 switch (pdata->control_flags & MAX8973_CONTROL_CLKADV_TRIP_MASK) {
297 case MAX8973_CONTROL_CLKADV_TRIP_DISABLED:
298 control2 |= MAX8973_CKKADV_TRIP_DISABLE;
299 break;
301 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US:
302 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US;
303 break;
305 case MAX8973_CONTROL_CLKADV_TRIP_150mV_PER_US:
306 control2 |= MAX8973_CKKADV_TRIP_150mV_PER_US;
307 break;
309 case MAX8973_CONTROL_CLKADV_TRIP_75mV_PER_US_HIST_DIS:
310 control2 |= MAX8973_CKKADV_TRIP_75mV_PER_US_HIST_DIS;
311 break;
314 /* Configure inductor value */
315 switch (pdata->control_flags & MAX8973_CONTROL_INDUCTOR_VALUE_MASK) {
316 case MAX8973_CONTROL_INDUCTOR_VALUE_NOMINAL:
317 control2 |= MAX8973_INDUCTOR_NOMINAL;
318 break;
320 case MAX8973_CONTROL_INDUCTOR_VALUE_MINUS_30_PER:
321 control2 |= MAX8973_INDUCTOR_MIN_30_PER;
322 break;
324 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_30_PER:
325 control2 |= MAX8973_INDUCTOR_PLUS_30_PER;
326 break;
328 case MAX8973_CONTROL_INDUCTOR_VALUE_PLUS_60_PER:
329 control2 |= MAX8973_INDUCTOR_PLUS_60_PER;
330 break;
333 ret = regmap_write(max->regmap, MAX8973_CONTROL1, control1);
334 if (ret < 0) {
335 dev_err(max->dev, "register %d write failed, err = %d",
336 MAX8973_CONTROL1, ret);
337 return ret;
340 ret = regmap_write(max->regmap, MAX8973_CONTROL2, control2);
341 if (ret < 0) {
342 dev_err(max->dev, "register %d write failed, err = %d",
343 MAX8973_CONTROL2, ret);
344 return ret;
347 /* If external control is enabled then disable EN bit */
348 if (max->enable_external_control) {
349 ret = regmap_update_bits(max->regmap, MAX8973_VOUT,
350 MAX8973_VOUT_ENABLE, 0);
351 if (ret < 0)
352 dev_err(max->dev, "register %d update failed, err = %d",
353 MAX8973_VOUT, ret);
355 return ret;
358 static const struct regmap_config max8973_regmap_config = {
359 .reg_bits = 8,
360 .val_bits = 8,
361 .max_register = MAX8973_CHIPID2,
362 .cache_type = REGCACHE_RBTREE,
365 static int max8973_probe(struct i2c_client *client,
366 const struct i2c_device_id *id)
368 struct max8973_regulator_platform_data *pdata;
369 struct regulator_config config = { };
370 struct regulator_dev *rdev;
371 struct max8973_chip *max;
372 int ret;
374 pdata = dev_get_platdata(&client->dev);
376 if (!pdata && !client->dev.of_node) {
377 dev_err(&client->dev, "No Platform data");
378 return -EIO;
381 max = devm_kzalloc(&client->dev, sizeof(*max), GFP_KERNEL);
382 if (!max) {
383 dev_err(&client->dev, "Memory allocation for max failed\n");
384 return -ENOMEM;
387 max->regmap = devm_regmap_init_i2c(client, &max8973_regmap_config);
388 if (IS_ERR(max->regmap)) {
389 ret = PTR_ERR(max->regmap);
390 dev_err(&client->dev, "regmap init failed, err %d\n", ret);
391 return ret;
394 i2c_set_clientdata(client, max);
395 max->ops = max8973_dcdc_ops;
396 max->dev = &client->dev;
397 max->desc.name = id->name;
398 max->desc.id = 0;
399 max->desc.ops = &max->ops;
400 max->desc.type = REGULATOR_VOLTAGE;
401 max->desc.owner = THIS_MODULE;
402 max->desc.min_uV = MAX8973_MIN_VOLATGE;
403 max->desc.uV_step = MAX8973_VOLATGE_STEP;
404 max->desc.n_voltages = MAX8973_BUCK_N_VOLTAGE;
406 if (!pdata || !pdata->enable_ext_control) {
407 max->desc.enable_reg = MAX8973_VOUT;
408 max->desc.enable_mask = MAX8973_VOUT_ENABLE;
409 max->ops.enable = regulator_enable_regmap;
410 max->ops.disable = regulator_disable_regmap;
411 max->ops.is_enabled = regulator_is_enabled_regmap;
414 if (pdata) {
415 max->dvs_gpio = pdata->dvs_gpio;
416 max->enable_external_control = pdata->enable_ext_control;
417 max->curr_gpio_val = pdata->dvs_def_state;
418 max->curr_vout_reg = MAX8973_VOUT + pdata->dvs_def_state;
419 } else {
420 max->dvs_gpio = -EINVAL;
421 max->curr_vout_reg = MAX8973_VOUT;
424 max->lru_index[0] = max->curr_vout_reg;
426 if (gpio_is_valid(max->dvs_gpio)) {
427 int gpio_flags;
428 int i;
430 gpio_flags = (pdata->dvs_def_state) ?
431 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
432 ret = devm_gpio_request_one(&client->dev, max->dvs_gpio,
433 gpio_flags, "max8973-dvs");
434 if (ret) {
435 dev_err(&client->dev,
436 "gpio_request for gpio %d failed, err = %d\n",
437 max->dvs_gpio, ret);
438 return ret;
440 max->valid_dvs_gpio = true;
443 * Initialize the lru index with vout_reg id
444 * The index 0 will be most recently used and
445 * set with the max->curr_vout_reg */
446 for (i = 0; i < MAX8973_MAX_VOUT_REG; ++i)
447 max->lru_index[i] = i;
448 max->lru_index[0] = max->curr_vout_reg;
449 max->lru_index[max->curr_vout_reg] = 0;
450 } else {
451 max->valid_dvs_gpio = false;
454 if (pdata) {
455 ret = max8973_init_dcdc(max, pdata);
456 if (ret < 0) {
457 dev_err(max->dev, "Max8973 Init failed, err = %d\n", ret);
458 return ret;
462 config.dev = &client->dev;
463 config.init_data = pdata ? pdata->reg_init_data :
464 of_get_regulator_init_data(&client->dev, client->dev.of_node);
465 config.driver_data = max;
466 config.of_node = client->dev.of_node;
467 config.regmap = max->regmap;
469 /* Register the regulators */
470 rdev = devm_regulator_register(&client->dev, &max->desc, &config);
471 if (IS_ERR(rdev)) {
472 ret = PTR_ERR(rdev);
473 dev_err(max->dev, "regulator register failed, err %d\n", ret);
474 return ret;
477 max->rdev = rdev;
478 return 0;
481 static const struct i2c_device_id max8973_id[] = {
482 {.name = "max8973",},
486 MODULE_DEVICE_TABLE(i2c, max8973_id);
488 static struct i2c_driver max8973_i2c_driver = {
489 .driver = {
490 .name = "max8973",
491 .owner = THIS_MODULE,
493 .probe = max8973_probe,
494 .id_table = max8973_id,
497 static int __init max8973_init(void)
499 return i2c_add_driver(&max8973_i2c_driver);
501 subsys_initcall(max8973_init);
503 static void __exit max8973_cleanup(void)
505 i2c_del_driver(&max8973_i2c_driver);
507 module_exit(max8973_cleanup);
509 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
510 MODULE_DESCRIPTION("MAX8973 voltage regulator driver");
511 MODULE_LICENSE("GPL v2");