diff options
author | Jonghwan Choi <jhbird.choi@gmail.com> | 2011-10-24 09:26:26 -0400 |
---|---|---|
committer | Mark Brown <broonie@opensource.wolfsonmicro.com> | 2011-11-23 09:02:48 -0500 |
commit | c5b68d47bb06ca0df9c4d1a1ce5a46ee879aa85c (patch) | |
tree | 047a6a03bf90ab34963e44c131b148fccceac12d /drivers/regulator/max8649.c | |
parent | 43530b69d758328d3ffe6ab98fd640463e8e3667 (diff) |
regulator: max8649 Convert max8649 to use regmap api
Signed-off-by: Jonghwan Choi <jhbird.choi@gmail.com>
Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Diffstat (limited to 'drivers/regulator/max8649.c')
-rw-r--r-- | drivers/regulator/max8649.c | 155 |
1 files changed, 55 insertions, 100 deletions
diff --git a/drivers/regulator/max8649.c b/drivers/regulator/max8649.c index 1062cf9f02dc..c54d0ad18c73 100644 --- a/drivers/regulator/max8649.c +++ b/drivers/regulator/max8649.c | |||
@@ -16,6 +16,7 @@ | |||
16 | #include <linux/regulator/driver.h> | 16 | #include <linux/regulator/driver.h> |
17 | #include <linux/slab.h> | 17 | #include <linux/slab.h> |
18 | #include <linux/regulator/max8649.h> | 18 | #include <linux/regulator/max8649.h> |
19 | #include <linux/regmap.h> | ||
19 | 20 | ||
20 | #define MAX8649_DCDC_VMIN 750000 /* uV */ | 21 | #define MAX8649_DCDC_VMIN 750000 /* uV */ |
21 | #define MAX8649_DCDC_VMAX 1380000 /* uV */ | 22 | #define MAX8649_DCDC_VMAX 1380000 /* uV */ |
@@ -49,9 +50,8 @@ | |||
49 | 50 | ||
50 | struct max8649_regulator_info { | 51 | struct max8649_regulator_info { |
51 | struct regulator_dev *regulator; | 52 | struct regulator_dev *regulator; |
52 | struct i2c_client *i2c; | ||
53 | struct device *dev; | 53 | struct device *dev; |
54 | struct mutex io_lock; | 54 | struct regmap *regmap; |
55 | 55 | ||
56 | int vol_reg; | 56 | int vol_reg; |
57 | unsigned mode:2; /* bit[1:0] = VID1, VID0 */ | 57 | unsigned mode:2; /* bit[1:0] = VID1, VID0 */ |
@@ -63,71 +63,6 @@ struct max8649_regulator_info { | |||
63 | 63 | ||
64 | /* I2C operations */ | 64 | /* I2C operations */ |
65 | 65 | ||
66 | static inline int max8649_read_device(struct i2c_client *i2c, | ||
67 | int reg, int bytes, void *dest) | ||
68 | { | ||
69 | unsigned char data; | ||
70 | int ret; | ||
71 | |||
72 | data = (unsigned char)reg; | ||
73 | ret = i2c_master_send(i2c, &data, 1); | ||
74 | if (ret < 0) | ||
75 | return ret; | ||
76 | ret = i2c_master_recv(i2c, dest, bytes); | ||
77 | if (ret < 0) | ||
78 | return ret; | ||
79 | return 0; | ||
80 | } | ||
81 | |||
82 | static inline int max8649_write_device(struct i2c_client *i2c, | ||
83 | int reg, int bytes, void *src) | ||
84 | { | ||
85 | unsigned char buf[bytes + 1]; | ||
86 | int ret; | ||
87 | |||
88 | buf[0] = (unsigned char)reg; | ||
89 | memcpy(&buf[1], src, bytes); | ||
90 | |||
91 | ret = i2c_master_send(i2c, buf, bytes + 1); | ||
92 | if (ret < 0) | ||
93 | return ret; | ||
94 | return 0; | ||
95 | } | ||
96 | |||
97 | static int max8649_reg_read(struct i2c_client *i2c, int reg) | ||
98 | { | ||
99 | struct max8649_regulator_info *info = i2c_get_clientdata(i2c); | ||
100 | unsigned char data; | ||
101 | int ret; | ||
102 | |||
103 | mutex_lock(&info->io_lock); | ||
104 | ret = max8649_read_device(i2c, reg, 1, &data); | ||
105 | mutex_unlock(&info->io_lock); | ||
106 | |||
107 | if (ret < 0) | ||
108 | return ret; | ||
109 | return (int)data; | ||
110 | } | ||
111 | |||
112 | static int max8649_set_bits(struct i2c_client *i2c, int reg, | ||
113 | unsigned char mask, unsigned char data) | ||
114 | { | ||
115 | struct max8649_regulator_info *info = i2c_get_clientdata(i2c); | ||
116 | unsigned char value; | ||
117 | int ret; | ||
118 | |||
119 | mutex_lock(&info->io_lock); | ||
120 | ret = max8649_read_device(i2c, reg, 1, &value); | ||
121 | if (ret < 0) | ||
122 | goto out; | ||
123 | value &= ~mask; | ||
124 | value |= data; | ||
125 | ret = max8649_write_device(i2c, reg, 1, &value); | ||
126 | out: | ||
127 | mutex_unlock(&info->io_lock); | ||
128 | return ret; | ||
129 | } | ||
130 | |||
131 | static inline int check_range(int min_uV, int max_uV) | 66 | static inline int check_range(int min_uV, int max_uV) |
132 | { | 67 | { |
133 | if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX) | 68 | if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX) |
@@ -144,13 +79,14 @@ static int max8649_list_voltage(struct regulator_dev *rdev, unsigned index) | |||
144 | static int max8649_get_voltage(struct regulator_dev *rdev) | 79 | static int max8649_get_voltage(struct regulator_dev *rdev) |
145 | { | 80 | { |
146 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | 81 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
82 | unsigned int val; | ||
147 | unsigned char data; | 83 | unsigned char data; |
148 | int ret; | 84 | int ret; |
149 | 85 | ||
150 | ret = max8649_reg_read(info->i2c, info->vol_reg); | 86 | ret = regmap_read(info->regmap, info->vol_reg, &val); |
151 | if (ret < 0) | 87 | if (ret != 0) |
152 | return ret; | 88 | return ret; |
153 | data = (unsigned char)ret & MAX8649_VOL_MASK; | 89 | data = (unsigned char)val & MAX8649_VOL_MASK; |
154 | return max8649_list_voltage(rdev, data); | 90 | return max8649_list_voltage(rdev, data); |
155 | } | 91 | } |
156 | 92 | ||
@@ -170,14 +106,14 @@ static int max8649_set_voltage(struct regulator_dev *rdev, | |||
170 | mask = MAX8649_VOL_MASK; | 106 | mask = MAX8649_VOL_MASK; |
171 | *selector = data & mask; | 107 | *selector = data & mask; |
172 | 108 | ||
173 | return max8649_set_bits(info->i2c, info->vol_reg, mask, data); | 109 | return regmap_update_bits(info->regmap, info->vol_reg, mask, data); |
174 | } | 110 | } |
175 | 111 | ||
176 | /* EN_PD means pulldown on EN input */ | 112 | /* EN_PD means pulldown on EN input */ |
177 | static int max8649_enable(struct regulator_dev *rdev) | 113 | static int max8649_enable(struct regulator_dev *rdev) |
178 | { | 114 | { |
179 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | 115 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
180 | return max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_EN_PD, 0); | 116 | return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, 0); |
181 | } | 117 | } |
182 | 118 | ||
183 | /* | 119 | /* |
@@ -187,38 +123,40 @@ static int max8649_enable(struct regulator_dev *rdev) | |||
187 | static int max8649_disable(struct regulator_dev *rdev) | 123 | static int max8649_disable(struct regulator_dev *rdev) |
188 | { | 124 | { |
189 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | 125 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
190 | return max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_EN_PD, | 126 | return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, |
191 | MAX8649_EN_PD); | 127 | MAX8649_EN_PD); |
192 | } | 128 | } |
193 | 129 | ||
194 | static int max8649_is_enabled(struct regulator_dev *rdev) | 130 | static int max8649_is_enabled(struct regulator_dev *rdev) |
195 | { | 131 | { |
196 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | 132 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
133 | unsigned int val; | ||
197 | int ret; | 134 | int ret; |
198 | 135 | ||
199 | ret = max8649_reg_read(info->i2c, MAX8649_CONTROL); | 136 | ret = regmap_read(info->regmap, MAX8649_CONTROL, &val); |
200 | if (ret < 0) | 137 | if (ret != 0) |
201 | return ret; | 138 | return ret; |
202 | return !((unsigned char)ret & MAX8649_EN_PD); | 139 | return !((unsigned char)val & MAX8649_EN_PD); |
203 | } | 140 | } |
204 | 141 | ||
205 | static int max8649_enable_time(struct regulator_dev *rdev) | 142 | static int max8649_enable_time(struct regulator_dev *rdev) |
206 | { | 143 | { |
207 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | 144 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
208 | int voltage, rate, ret; | 145 | int voltage, rate, ret; |
146 | unsigned int val; | ||
209 | 147 | ||
210 | /* get voltage */ | 148 | /* get voltage */ |
211 | ret = max8649_reg_read(info->i2c, info->vol_reg); | 149 | ret = regmap_read(info->regmap, info->vol_reg, &val); |
212 | if (ret < 0) | 150 | if (ret != 0) |
213 | return ret; | 151 | return ret; |
214 | ret &= MAX8649_VOL_MASK; | 152 | val &= MAX8649_VOL_MASK; |
215 | voltage = max8649_list_voltage(rdev, (unsigned char)ret); /* uV */ | 153 | voltage = max8649_list_voltage(rdev, (unsigned char)ret); /* uV */ |
216 | 154 | ||
217 | /* get rate */ | 155 | /* get rate */ |
218 | ret = max8649_reg_read(info->i2c, MAX8649_RAMP); | 156 | ret = regmap_read(info->regmap, MAX8649_RAMP, &val); |
219 | if (ret < 0) | 157 | if (ret != 0) |
220 | return ret; | 158 | return ret; |
221 | ret = (ret & MAX8649_RAMP_MASK) >> 5; | 159 | ret = (val & MAX8649_RAMP_MASK) >> 5; |
222 | rate = (32 * 1000) >> ret; /* uV/uS */ | 160 | rate = (32 * 1000) >> ret; /* uV/uS */ |
223 | 161 | ||
224 | return DIV_ROUND_UP(voltage, rate); | 162 | return DIV_ROUND_UP(voltage, rate); |
@@ -230,12 +168,12 @@ static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode) | |||
230 | 168 | ||
231 | switch (mode) { | 169 | switch (mode) { |
232 | case REGULATOR_MODE_FAST: | 170 | case REGULATOR_MODE_FAST: |
233 | max8649_set_bits(info->i2c, info->vol_reg, MAX8649_FORCE_PWM, | 171 | regmap_update_bits(info->regmap, info->vol_reg, MAX8649_FORCE_PWM, |
234 | MAX8649_FORCE_PWM); | 172 | MAX8649_FORCE_PWM); |
235 | break; | 173 | break; |
236 | case REGULATOR_MODE_NORMAL: | 174 | case REGULATOR_MODE_NORMAL: |
237 | max8649_set_bits(info->i2c, info->vol_reg, | 175 | regmap_update_bits(info->regmap, info->vol_reg, |
238 | MAX8649_FORCE_PWM, 0); | 176 | MAX8649_FORCE_PWM, 0); |
239 | break; | 177 | break; |
240 | default: | 178 | default: |
241 | return -EINVAL; | 179 | return -EINVAL; |
@@ -246,10 +184,13 @@ static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode) | |||
246 | static unsigned int max8649_get_mode(struct regulator_dev *rdev) | 184 | static unsigned int max8649_get_mode(struct regulator_dev *rdev) |
247 | { | 185 | { |
248 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); | 186 | struct max8649_regulator_info *info = rdev_get_drvdata(rdev); |
187 | unsigned int val; | ||
249 | int ret; | 188 | int ret; |
250 | 189 | ||
251 | ret = max8649_reg_read(info->i2c, info->vol_reg); | 190 | ret = regmap_read(info->regmap, info->vol_reg, &val); |
252 | if (ret & MAX8649_FORCE_PWM) | 191 | if (ret != 0) |
192 | return ret; | ||
193 | if (val & MAX8649_FORCE_PWM) | ||
253 | return REGULATOR_MODE_FAST; | 194 | return REGULATOR_MODE_FAST; |
254 | return REGULATOR_MODE_NORMAL; | 195 | return REGULATOR_MODE_NORMAL; |
255 | } | 196 | } |
@@ -275,11 +216,17 @@ static struct regulator_desc dcdc_desc = { | |||
275 | .owner = THIS_MODULE, | 216 | .owner = THIS_MODULE, |
276 | }; | 217 | }; |
277 | 218 | ||
219 | static struct regmap_config max8649_regmap_config = { | ||
220 | .reg_bits = 8, | ||
221 | .val_bits = 8, | ||
222 | }; | ||
223 | |||
278 | static int __devinit max8649_regulator_probe(struct i2c_client *client, | 224 | static int __devinit max8649_regulator_probe(struct i2c_client *client, |
279 | const struct i2c_device_id *id) | 225 | const struct i2c_device_id *id) |
280 | { | 226 | { |
281 | struct max8649_platform_data *pdata = client->dev.platform_data; | 227 | struct max8649_platform_data *pdata = client->dev.platform_data; |
282 | struct max8649_regulator_info *info = NULL; | 228 | struct max8649_regulator_info *info = NULL; |
229 | unsigned int val; | ||
283 | unsigned char data; | 230 | unsigned char data; |
284 | int ret; | 231 | int ret; |
285 | 232 | ||
@@ -289,9 +236,14 @@ static int __devinit max8649_regulator_probe(struct i2c_client *client, | |||
289 | return -ENOMEM; | 236 | return -ENOMEM; |
290 | } | 237 | } |
291 | 238 | ||
292 | info->i2c = client; | 239 | 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 | |||
293 | info->dev = &client->dev; | 246 | info->dev = &client->dev; |
294 | mutex_init(&info->io_lock); | ||
295 | i2c_set_clientdata(client, info); | 247 | i2c_set_clientdata(client, info); |
296 | 248 | ||
297 | info->mode = pdata->mode; | 249 | info->mode = pdata->mode; |
@@ -312,8 +264,8 @@ static int __devinit max8649_regulator_probe(struct i2c_client *client, | |||
312 | break; | 264 | break; |
313 | } | 265 | } |
314 | 266 | ||
315 | ret = max8649_reg_read(info->i2c, MAX8649_CHIP_ID1); | 267 | ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val); |
316 | if (ret < 0) { | 268 | if (ret != 0) { |
317 | dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n", | 269 | dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n", |
318 | ret); | 270 | ret); |
319 | goto out; | 271 | goto out; |
@@ -321,29 +273,29 @@ static int __devinit max8649_regulator_probe(struct i2c_client *client, | |||
321 | dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", ret); | 273 | dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", ret); |
322 | 274 | ||
323 | /* enable VID0 & VID1 */ | 275 | /* enable VID0 & VID1 */ |
324 | max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_VID_MASK, 0); | 276 | regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0); |
325 | 277 | ||
326 | /* enable/disable external clock synchronization */ | 278 | /* enable/disable external clock synchronization */ |
327 | info->extclk = pdata->extclk; | 279 | info->extclk = pdata->extclk; |
328 | data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0; | 280 | data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0; |
329 | max8649_set_bits(info->i2c, info->vol_reg, MAX8649_SYNC_EXTCLK, data); | 281 | regmap_update_bits(info->regmap, info->vol_reg, MAX8649_SYNC_EXTCLK, data); |
330 | if (info->extclk) { | 282 | if (info->extclk) { |
331 | /* set external clock frequency */ | 283 | /* set external clock frequency */ |
332 | info->extclk_freq = pdata->extclk_freq; | 284 | info->extclk_freq = pdata->extclk_freq; |
333 | max8649_set_bits(info->i2c, MAX8649_SYNC, MAX8649_EXT_MASK, | 285 | regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK, |
334 | info->extclk_freq << 6); | 286 | info->extclk_freq << 6); |
335 | } | 287 | } |
336 | 288 | ||
337 | if (pdata->ramp_timing) { | 289 | if (pdata->ramp_timing) { |
338 | info->ramp_timing = pdata->ramp_timing; | 290 | info->ramp_timing = pdata->ramp_timing; |
339 | max8649_set_bits(info->i2c, MAX8649_RAMP, MAX8649_RAMP_MASK, | 291 | regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK, |
340 | info->ramp_timing << 5); | 292 | info->ramp_timing << 5); |
341 | } | 293 | } |
342 | 294 | ||
343 | info->ramp_down = pdata->ramp_down; | 295 | info->ramp_down = pdata->ramp_down; |
344 | if (info->ramp_down) { | 296 | if (info->ramp_down) { |
345 | max8649_set_bits(info->i2c, MAX8649_RAMP, MAX8649_RAMP_DOWN, | 297 | regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN, |
346 | MAX8649_RAMP_DOWN); | 298 | MAX8649_RAMP_DOWN); |
347 | } | 299 | } |
348 | 300 | ||
349 | info->regulator = regulator_register(&dcdc_desc, &client->dev, | 301 | info->regulator = regulator_register(&dcdc_desc, &client->dev, |
@@ -358,6 +310,8 @@ static int __devinit max8649_regulator_probe(struct i2c_client *client, | |||
358 | dev_info(info->dev, "Max8649 regulator device is detected.\n"); | 310 | dev_info(info->dev, "Max8649 regulator device is detected.\n"); |
359 | return 0; | 311 | return 0; |
360 | out: | 312 | out: |
313 | regmap_exit(info->regmap); | ||
314 | fail: | ||
361 | kfree(info); | 315 | kfree(info); |
362 | return ret; | 316 | return ret; |
363 | } | 317 | } |
@@ -369,6 +323,7 @@ static int __devexit max8649_regulator_remove(struct i2c_client *client) | |||
369 | if (info) { | 323 | if (info) { |
370 | if (info->regulator) | 324 | if (info->regulator) |
371 | regulator_unregister(info->regulator); | 325 | regulator_unregister(info->regulator); |
326 | regmap_exit(info->regmap); | ||
372 | kfree(info); | 327 | kfree(info); |
373 | } | 328 | } |
374 | 329 | ||