]>
Commit | Line | Data |
---|---|---|
a71b797f HZ |
1 | /* |
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> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
a71b797f | 18 | #include <linux/regulator/max8649.h> |
c5b68d47 | 19 | #include <linux/regmap.h> |
a71b797f HZ |
20 | |
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 | ||
51 | struct max8649_regulator_info { | |
52 | struct regulator_dev *regulator; | |
a71b797f | 53 | struct device *dev; |
c5b68d47 | 54 | struct regmap *regmap; |
a71b797f | 55 | |
a71b797f HZ |
56 | unsigned mode:2; /* bit[1:0] = VID1, VID0 */ |
57 | unsigned extclk_freq:2; | |
58 | unsigned extclk:1; | |
59 | unsigned ramp_timing:3; | |
60 | unsigned ramp_down:1; | |
61 | }; | |
62 | ||
63 | /* I2C operations */ | |
64 | ||
a71b797f HZ |
65 | static inline int check_range(int min_uV, int max_uV) |
66 | { | |
67 | if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX) | |
68 | || (min_uV > max_uV)) | |
69 | return -EINVAL; | |
70 | return 0; | |
71 | } | |
72 | ||
a71b797f | 73 | static int max8649_set_voltage(struct regulator_dev *rdev, |
3a93f2a9 | 74 | int min_uV, int max_uV, unsigned *selector) |
a71b797f HZ |
75 | { |
76 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
77 | unsigned char data, mask; | |
78 | ||
79 | if (check_range(min_uV, max_uV)) { | |
80 | dev_err(info->dev, "invalid voltage range (%d, %d) uV\n", | |
81 | min_uV, max_uV); | |
82 | return -EINVAL; | |
83 | } | |
21c9e5f1 | 84 | data = DIV_ROUND_UP(min_uV - MAX8649_DCDC_VMIN, MAX8649_DCDC_STEP); |
a71b797f | 85 | mask = MAX8649_VOL_MASK; |
3a93f2a9 | 86 | *selector = data & mask; |
a71b797f | 87 | |
817d8c2d AL |
88 | return regmap_update_bits(info->regmap, rdev->desc->vsel_reg, mask, |
89 | data); | |
a71b797f HZ |
90 | } |
91 | ||
92 | /* EN_PD means pulldown on EN input */ | |
93 | static int max8649_enable(struct regulator_dev *rdev) | |
94 | { | |
95 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
c5b68d47 | 96 | return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, 0); |
a71b797f HZ |
97 | } |
98 | ||
99 | /* | |
100 | * Applied internal pulldown resistor on EN input pin. | |
101 | * If pulldown EN pin outside, it would be better. | |
102 | */ | |
103 | static int max8649_disable(struct regulator_dev *rdev) | |
104 | { | |
105 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
c5b68d47 | 106 | return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, |
a71b797f HZ |
107 | MAX8649_EN_PD); |
108 | } | |
109 | ||
110 | static int max8649_is_enabled(struct regulator_dev *rdev) | |
111 | { | |
112 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
c5b68d47 | 113 | unsigned int val; |
a71b797f HZ |
114 | int ret; |
115 | ||
c5b68d47 JC |
116 | ret = regmap_read(info->regmap, MAX8649_CONTROL, &val); |
117 | if (ret != 0) | |
a71b797f | 118 | return ret; |
c5b68d47 | 119 | return !((unsigned char)val & MAX8649_EN_PD); |
a71b797f HZ |
120 | } |
121 | ||
122 | static int max8649_enable_time(struct regulator_dev *rdev) | |
123 | { | |
124 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
125 | int voltage, rate, ret; | |
c5b68d47 | 126 | unsigned int val; |
a71b797f HZ |
127 | |
128 | /* get voltage */ | |
817d8c2d | 129 | ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val); |
c5b68d47 | 130 | if (ret != 0) |
a71b797f | 131 | return ret; |
c5b68d47 | 132 | val &= MAX8649_VOL_MASK; |
0fb737b5 | 133 | voltage = regulator_list_voltage_linear(rdev, (unsigned char)val); |
a71b797f HZ |
134 | |
135 | /* get rate */ | |
c5b68d47 JC |
136 | ret = regmap_read(info->regmap, MAX8649_RAMP, &val); |
137 | if (ret != 0) | |
a71b797f | 138 | return ret; |
c5b68d47 | 139 | ret = (val & MAX8649_RAMP_MASK) >> 5; |
a71b797f HZ |
140 | rate = (32 * 1000) >> ret; /* uV/uS */ |
141 | ||
e69c4997 | 142 | return DIV_ROUND_UP(voltage, rate); |
a71b797f HZ |
143 | } |
144 | ||
145 | static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode) | |
146 | { | |
147 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
148 | ||
149 | switch (mode) { | |
150 | case REGULATOR_MODE_FAST: | |
817d8c2d AL |
151 | regmap_update_bits(info->regmap, rdev->desc->vsel_reg, |
152 | MAX8649_FORCE_PWM, MAX8649_FORCE_PWM); | |
a71b797f HZ |
153 | break; |
154 | case REGULATOR_MODE_NORMAL: | |
817d8c2d | 155 | regmap_update_bits(info->regmap, rdev->desc->vsel_reg, |
c5b68d47 | 156 | MAX8649_FORCE_PWM, 0); |
a71b797f HZ |
157 | break; |
158 | default: | |
159 | return -EINVAL; | |
160 | } | |
161 | return 0; | |
162 | } | |
163 | ||
164 | static unsigned int max8649_get_mode(struct regulator_dev *rdev) | |
165 | { | |
166 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | |
c5b68d47 | 167 | unsigned int val; |
a71b797f HZ |
168 | int ret; |
169 | ||
817d8c2d | 170 | ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val); |
c5b68d47 JC |
171 | if (ret != 0) |
172 | return ret; | |
173 | if (val & MAX8649_FORCE_PWM) | |
a71b797f HZ |
174 | return REGULATOR_MODE_FAST; |
175 | return REGULATOR_MODE_NORMAL; | |
176 | } | |
177 | ||
178 | static struct regulator_ops max8649_dcdc_ops = { | |
179 | .set_voltage = max8649_set_voltage, | |
817d8c2d | 180 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
0fb737b5 | 181 | .list_voltage = regulator_list_voltage_linear, |
a71b797f HZ |
182 | .enable = max8649_enable, |
183 | .disable = max8649_disable, | |
184 | .is_enabled = max8649_is_enabled, | |
185 | .enable_time = max8649_enable_time, | |
186 | .set_mode = max8649_set_mode, | |
187 | .get_mode = max8649_get_mode, | |
188 | ||
189 | }; | |
190 | ||
817d8c2d | 191 | static struct regulator_desc dcdc_desc = { |
a71b797f HZ |
192 | .name = "max8649", |
193 | .ops = &max8649_dcdc_ops, | |
194 | .type = REGULATOR_VOLTAGE, | |
195 | .n_voltages = 1 << 6, | |
196 | .owner = THIS_MODULE, | |
817d8c2d | 197 | .vsel_mask = MAX8649_VOL_MASK, |
0fb737b5 AL |
198 | .min_uV = MAX8649_DCDC_VMIN, |
199 | .uV_step = MAX8649_DCDC_STEP, | |
a71b797f HZ |
200 | }; |
201 | ||
c5b68d47 JC |
202 | static struct regmap_config max8649_regmap_config = { |
203 | .reg_bits = 8, | |
204 | .val_bits = 8, | |
205 | }; | |
206 | ||
a71b797f HZ |
207 | static int __devinit max8649_regulator_probe(struct i2c_client *client, |
208 | const struct i2c_device_id *id) | |
209 | { | |
210 | struct max8649_platform_data *pdata = client->dev.platform_data; | |
211 | struct max8649_regulator_info *info = NULL; | |
c172708d | 212 | struct regulator_config config = { }; |
c5b68d47 | 213 | unsigned int val; |
a71b797f HZ |
214 | unsigned char data; |
215 | int ret; | |
216 | ||
dc553a79 AL |
217 | info = devm_kzalloc(&client->dev, sizeof(struct max8649_regulator_info), |
218 | GFP_KERNEL); | |
a71b797f HZ |
219 | if (!info) { |
220 | dev_err(&client->dev, "No enough memory\n"); | |
221 | return -ENOMEM; | |
222 | } | |
223 | ||
dc553a79 | 224 | info->regmap = devm_regmap_init_i2c(client, &max8649_regmap_config); |
c5b68d47 JC |
225 | if (IS_ERR(info->regmap)) { |
226 | ret = PTR_ERR(info->regmap); | |
227 | dev_err(&client->dev, "Failed to allocate register map: %d\n", ret); | |
dc553a79 | 228 | return ret; |
c5b68d47 JC |
229 | } |
230 | ||
a71b797f | 231 | info->dev = &client->dev; |
a71b797f HZ |
232 | i2c_set_clientdata(client, info); |
233 | ||
234 | info->mode = pdata->mode; | |
235 | switch (info->mode) { | |
236 | case 0: | |
817d8c2d | 237 | dcdc_desc.vsel_reg = MAX8649_MODE0; |
a71b797f HZ |
238 | break; |
239 | case 1: | |
817d8c2d | 240 | dcdc_desc.vsel_reg = MAX8649_MODE1; |
a71b797f HZ |
241 | break; |
242 | case 2: | |
817d8c2d | 243 | dcdc_desc.vsel_reg = MAX8649_MODE2; |
a71b797f HZ |
244 | break; |
245 | case 3: | |
817d8c2d | 246 | dcdc_desc.vsel_reg = MAX8649_MODE3; |
a71b797f HZ |
247 | break; |
248 | default: | |
249 | break; | |
250 | } | |
251 | ||
c5b68d47 JC |
252 | ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val); |
253 | if (ret != 0) { | |
a71b797f HZ |
254 | dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n", |
255 | ret); | |
dc553a79 | 256 | return ret; |
a71b797f | 257 | } |
da7de6a1 | 258 | dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val); |
a71b797f HZ |
259 | |
260 | /* enable VID0 & VID1 */ | |
c5b68d47 | 261 | regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0); |
a71b797f HZ |
262 | |
263 | /* enable/disable external clock synchronization */ | |
264 | info->extclk = pdata->extclk; | |
265 | data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0; | |
817d8c2d AL |
266 | regmap_update_bits(info->regmap, dcdc_desc.vsel_reg, |
267 | MAX8649_SYNC_EXTCLK, data); | |
a71b797f HZ |
268 | if (info->extclk) { |
269 | /* set external clock frequency */ | |
270 | info->extclk_freq = pdata->extclk_freq; | |
c5b68d47 JC |
271 | regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK, |
272 | info->extclk_freq << 6); | |
a71b797f HZ |
273 | } |
274 | ||
275 | if (pdata->ramp_timing) { | |
276 | info->ramp_timing = pdata->ramp_timing; | |
c5b68d47 JC |
277 | regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK, |
278 | info->ramp_timing << 5); | |
a71b797f HZ |
279 | } |
280 | ||
281 | info->ramp_down = pdata->ramp_down; | |
282 | if (info->ramp_down) { | |
c5b68d47 JC |
283 | regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN, |
284 | MAX8649_RAMP_DOWN); | |
a71b797f HZ |
285 | } |
286 | ||
c172708d MB |
287 | config.dev = &client->dev; |
288 | config.init_data = pdata->regulator; | |
289 | config.driver_data = info; | |
290 | ||
291 | info->regulator = regulator_register(&dcdc_desc, &config); | |
a71b797f HZ |
292 | if (IS_ERR(info->regulator)) { |
293 | dev_err(info->dev, "failed to register regulator %s\n", | |
294 | dcdc_desc.name); | |
dc553a79 | 295 | return PTR_ERR(info->regulator); |
a71b797f HZ |
296 | } |
297 | ||
a71b797f | 298 | return 0; |
a71b797f HZ |
299 | } |
300 | ||
301 | static int __devexit max8649_regulator_remove(struct i2c_client *client) | |
302 | { | |
303 | struct max8649_regulator_info *info = i2c_get_clientdata(client); | |
304 | ||
305 | if (info) { | |
306 | if (info->regulator) | |
307 | regulator_unregister(info->regulator); | |
a71b797f | 308 | } |
a71b797f HZ |
309 | |
310 | return 0; | |
311 | } | |
312 | ||
313 | static const struct i2c_device_id max8649_id[] = { | |
314 | { "max8649", 0 }, | |
315 | { } | |
316 | }; | |
317 | MODULE_DEVICE_TABLE(i2c, max8649_id); | |
318 | ||
319 | static struct i2c_driver max8649_driver = { | |
320 | .probe = max8649_regulator_probe, | |
321 | .remove = __devexit_p(max8649_regulator_remove), | |
322 | .driver = { | |
323 | .name = "max8649", | |
324 | }, | |
325 | .id_table = max8649_id, | |
326 | }; | |
327 | ||
328 | static int __init max8649_init(void) | |
329 | { | |
330 | return i2c_add_driver(&max8649_driver); | |
331 | } | |
332 | subsys_initcall(max8649_init); | |
333 | ||
334 | static void __exit max8649_exit(void) | |
335 | { | |
336 | i2c_del_driver(&max8649_driver); | |
337 | } | |
338 | module_exit(max8649_exit); | |
339 | ||
340 | /* Module information */ | |
341 | MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver"); | |
342 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); | |
343 | MODULE_LICENSE("GPL"); |