blob: 991f517c8dc88e26bbaa59a22d9e7bdad5bc1905 [file] [log] [blame]
Haojian Zhuanga71b7972010-01-25 10:24:09 -05001/*
2 * Regulators driver for Maxim max8649
3 *
4 * Copyright (C) 2009-2010 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/platform_device.h>
16#include <linux/regulator/driver.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Haojian Zhuanga71b7972010-01-25 10:24:09 -050018#include <linux/regulator/max8649.h>
Jonghwan Choic5b68d472011-10-24 22:26:26 +090019#include <linux/regmap.h>
Haojian Zhuanga71b7972010-01-25 10:24:09 -050020
21#define MAX8649_DCDC_VMIN 750000 /* uV */
22#define MAX8649_DCDC_VMAX 1380000 /* uV */
23#define MAX8649_DCDC_STEP 10000 /* uV */
24#define MAX8649_VOL_MASK 0x3f
25
26/* Registers */
27#define MAX8649_MODE0 0x00
28#define MAX8649_MODE1 0x01
29#define MAX8649_MODE2 0x02
30#define MAX8649_MODE3 0x03
31#define MAX8649_CONTROL 0x04
32#define MAX8649_SYNC 0x05
33#define MAX8649_RAMP 0x06
34#define MAX8649_CHIP_ID1 0x08
35#define MAX8649_CHIP_ID2 0x09
36
37/* Bits */
38#define MAX8649_EN_PD (1 << 7)
39#define MAX8649_VID0_PD (1 << 6)
40#define MAX8649_VID1_PD (1 << 5)
41#define MAX8649_VID_MASK (3 << 5)
42
43#define MAX8649_FORCE_PWM (1 << 7)
44#define MAX8649_SYNC_EXTCLK (1 << 6)
45
46#define MAX8649_EXT_MASK (3 << 6)
47
48#define MAX8649_RAMP_MASK (7 << 5)
49#define MAX8649_RAMP_DOWN (1 << 1)
50
51struct max8649_regulator_info {
52 struct regulator_dev *regulator;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050053 struct device *dev;
Jonghwan Choic5b68d472011-10-24 22:26:26 +090054 struct regmap *regmap;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050055
56 int vol_reg;
57 unsigned mode:2; /* bit[1:0] = VID1, VID0 */
58 unsigned extclk_freq:2;
59 unsigned extclk:1;
60 unsigned ramp_timing:3;
61 unsigned ramp_down:1;
62};
63
64/* I2C operations */
65
Haojian Zhuanga71b7972010-01-25 10:24:09 -050066static inline int check_range(int min_uV, int max_uV)
67{
68 if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX)
69 || (min_uV > max_uV))
70 return -EINVAL;
71 return 0;
72}
73
74static int max8649_list_voltage(struct regulator_dev *rdev, unsigned index)
75{
76 return (MAX8649_DCDC_VMIN + index * MAX8649_DCDC_STEP);
77}
78
79static int max8649_get_voltage(struct regulator_dev *rdev)
80{
81 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
Jonghwan Choic5b68d472011-10-24 22:26:26 +090082 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050083 unsigned char data;
84 int ret;
85
Jonghwan Choic5b68d472011-10-24 22:26:26 +090086 ret = regmap_read(info->regmap, info->vol_reg, &val);
87 if (ret != 0)
Haojian Zhuanga71b7972010-01-25 10:24:09 -050088 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +090089 data = (unsigned char)val & MAX8649_VOL_MASK;
Haojian Zhuanga71b7972010-01-25 10:24:09 -050090 return max8649_list_voltage(rdev, data);
91}
92
93static int max8649_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +000094 int min_uV, int max_uV, unsigned *selector)
Haojian Zhuanga71b7972010-01-25 10:24:09 -050095{
96 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
97 unsigned char data, mask;
98
99 if (check_range(min_uV, max_uV)) {
100 dev_err(info->dev, "invalid voltage range (%d, %d) uV\n",
101 min_uV, max_uV);
102 return -EINVAL;
103 }
Axel Lin21c9e5f2012-03-08 10:05:24 +0800104 data = DIV_ROUND_UP(min_uV - MAX8649_DCDC_VMIN, MAX8649_DCDC_STEP);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500105 mask = MAX8649_VOL_MASK;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000106 *selector = data & mask;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500107
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900108 return regmap_update_bits(info->regmap, info->vol_reg, mask, data);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500109}
110
111/* EN_PD means pulldown on EN input */
112static int max8649_enable(struct regulator_dev *rdev)
113{
114 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900115 return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, 0);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500116}
117
118/*
119 * Applied internal pulldown resistor on EN input pin.
120 * If pulldown EN pin outside, it would be better.
121 */
122static int max8649_disable(struct regulator_dev *rdev)
123{
124 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900125 return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD,
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500126 MAX8649_EN_PD);
127}
128
129static int max8649_is_enabled(struct regulator_dev *rdev)
130{
131 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900132 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500133 int ret;
134
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900135 ret = regmap_read(info->regmap, MAX8649_CONTROL, &val);
136 if (ret != 0)
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500137 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900138 return !((unsigned char)val & MAX8649_EN_PD);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500139}
140
141static int max8649_enable_time(struct regulator_dev *rdev)
142{
143 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
144 int voltage, rate, ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900145 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500146
147 /* get voltage */
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900148 ret = regmap_read(info->regmap, info->vol_reg, &val);
149 if (ret != 0)
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500150 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900151 val &= MAX8649_VOL_MASK;
Axel Lin9fc886a2012-02-07 11:12:55 +0800152 voltage = max8649_list_voltage(rdev, (unsigned char)val); /* uV */
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500153
154 /* get rate */
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900155 ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
156 if (ret != 0)
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500157 return ret;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900158 ret = (val & MAX8649_RAMP_MASK) >> 5;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500159 rate = (32 * 1000) >> ret; /* uV/uS */
160
Axel Line69c4992011-08-02 12:54:56 +0800161 return DIV_ROUND_UP(voltage, rate);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500162}
163
164static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
165{
166 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
167
168 switch (mode) {
169 case REGULATOR_MODE_FAST:
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900170 regmap_update_bits(info->regmap, info->vol_reg, MAX8649_FORCE_PWM,
171 MAX8649_FORCE_PWM);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500172 break;
173 case REGULATOR_MODE_NORMAL:
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900174 regmap_update_bits(info->regmap, info->vol_reg,
175 MAX8649_FORCE_PWM, 0);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500176 break;
177 default:
178 return -EINVAL;
179 }
180 return 0;
181}
182
183static unsigned int max8649_get_mode(struct regulator_dev *rdev)
184{
185 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900186 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500187 int ret;
188
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900189 ret = regmap_read(info->regmap, info->vol_reg, &val);
190 if (ret != 0)
191 return ret;
192 if (val & MAX8649_FORCE_PWM)
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500193 return REGULATOR_MODE_FAST;
194 return REGULATOR_MODE_NORMAL;
195}
196
197static struct regulator_ops max8649_dcdc_ops = {
198 .set_voltage = max8649_set_voltage,
199 .get_voltage = max8649_get_voltage,
200 .list_voltage = max8649_list_voltage,
201 .enable = max8649_enable,
202 .disable = max8649_disable,
203 .is_enabled = max8649_is_enabled,
204 .enable_time = max8649_enable_time,
205 .set_mode = max8649_set_mode,
206 .get_mode = max8649_get_mode,
207
208};
209
Axel Lin09de3472012-04-06 08:26:06 +0800210static const struct regulator_desc dcdc_desc = {
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500211 .name = "max8649",
212 .ops = &max8649_dcdc_ops,
213 .type = REGULATOR_VOLTAGE,
214 .n_voltages = 1 << 6,
215 .owner = THIS_MODULE,
216};
217
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900218static struct regmap_config max8649_regmap_config = {
219 .reg_bits = 8,
220 .val_bits = 8,
221};
222
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500223static int __devinit max8649_regulator_probe(struct i2c_client *client,
224 const struct i2c_device_id *id)
225{
226 struct max8649_platform_data *pdata = client->dev.platform_data;
227 struct max8649_regulator_info *info = NULL;
Mark Brownc1727082012-04-04 00:50:22 +0100228 struct regulator_config config = { };
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900229 unsigned int val;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500230 unsigned char data;
231 int ret;
232
233 info = kzalloc(sizeof(struct max8649_regulator_info), GFP_KERNEL);
234 if (!info) {
235 dev_err(&client->dev, "No enough memory\n");
236 return -ENOMEM;
237 }
238
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900239 info->regmap = regmap_init_i2c(client, &max8649_regmap_config);
240 if (IS_ERR(info->regmap)) {
241 ret = PTR_ERR(info->regmap);
242 dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
243 goto fail;
244 }
245
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500246 info->dev = &client->dev;
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500247 i2c_set_clientdata(client, info);
248
249 info->mode = pdata->mode;
250 switch (info->mode) {
251 case 0:
252 info->vol_reg = MAX8649_MODE0;
253 break;
254 case 1:
255 info->vol_reg = MAX8649_MODE1;
256 break;
257 case 2:
258 info->vol_reg = MAX8649_MODE2;
259 break;
260 case 3:
261 info->vol_reg = MAX8649_MODE3;
262 break;
263 default:
264 break;
265 }
266
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900267 ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
268 if (ret != 0) {
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500269 dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
270 ret);
271 goto out;
272 }
Axel Linda7de6a2012-02-06 20:37:46 +0800273 dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500274
275 /* enable VID0 & VID1 */
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900276 regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500277
278 /* enable/disable external clock synchronization */
279 info->extclk = pdata->extclk;
280 data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900281 regmap_update_bits(info->regmap, info->vol_reg, MAX8649_SYNC_EXTCLK, data);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500282 if (info->extclk) {
283 /* set external clock frequency */
284 info->extclk_freq = pdata->extclk_freq;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900285 regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
286 info->extclk_freq << 6);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500287 }
288
289 if (pdata->ramp_timing) {
290 info->ramp_timing = pdata->ramp_timing;
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900291 regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
292 info->ramp_timing << 5);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500293 }
294
295 info->ramp_down = pdata->ramp_down;
296 if (info->ramp_down) {
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900297 regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
298 MAX8649_RAMP_DOWN);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500299 }
300
Mark Brownc1727082012-04-04 00:50:22 +0100301 config.dev = &client->dev;
302 config.init_data = pdata->regulator;
303 config.driver_data = info;
304
305 info->regulator = regulator_register(&dcdc_desc, &config);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500306 if (IS_ERR(info->regulator)) {
307 dev_err(info->dev, "failed to register regulator %s\n",
308 dcdc_desc.name);
309 ret = PTR_ERR(info->regulator);
310 goto out;
311 }
312
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500313 return 0;
314out:
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900315 regmap_exit(info->regmap);
316fail:
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500317 kfree(info);
318 return ret;
319}
320
321static int __devexit max8649_regulator_remove(struct i2c_client *client)
322{
323 struct max8649_regulator_info *info = i2c_get_clientdata(client);
324
325 if (info) {
326 if (info->regulator)
327 regulator_unregister(info->regulator);
Jonghwan Choic5b68d472011-10-24 22:26:26 +0900328 regmap_exit(info->regmap);
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500329 kfree(info);
330 }
Haojian Zhuanga71b7972010-01-25 10:24:09 -0500331
332 return 0;
333}
334
335static const struct i2c_device_id max8649_id[] = {
336 { "max8649", 0 },
337 { }
338};
339MODULE_DEVICE_TABLE(i2c, max8649_id);
340
341static struct i2c_driver max8649_driver = {
342 .probe = max8649_regulator_probe,
343 .remove = __devexit_p(max8649_regulator_remove),
344 .driver = {
345 .name = "max8649",
346 },
347 .id_table = max8649_id,
348};
349
350static int __init max8649_init(void)
351{
352 return i2c_add_driver(&max8649_driver);
353}
354subsys_initcall(max8649_init);
355
356static void __exit max8649_exit(void)
357{
358 i2c_del_driver(&max8649_driver);
359}
360module_exit(max8649_exit);
361
362/* Module information */
363MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
364MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
365MODULE_LICENSE("GPL");
366