blob: 3ebdf698c648f6cb588a0637cbcd15ab6782f3e3 [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>
17#include <linux/regulator/max8649.h>
18
19#define MAX8649_DCDC_VMIN 750000 /* uV */
20#define MAX8649_DCDC_VMAX 1380000 /* uV */
21#define MAX8649_DCDC_STEP 10000 /* uV */
22#define MAX8649_VOL_MASK 0x3f
23
24/* Registers */
25#define MAX8649_MODE0 0x00
26#define MAX8649_MODE1 0x01
27#define MAX8649_MODE2 0x02
28#define MAX8649_MODE3 0x03
29#define MAX8649_CONTROL 0x04
30#define MAX8649_SYNC 0x05
31#define MAX8649_RAMP 0x06
32#define MAX8649_CHIP_ID1 0x08
33#define MAX8649_CHIP_ID2 0x09
34
35/* Bits */
36#define MAX8649_EN_PD (1 << 7)
37#define MAX8649_VID0_PD (1 << 6)
38#define MAX8649_VID1_PD (1 << 5)
39#define MAX8649_VID_MASK (3 << 5)
40
41#define MAX8649_FORCE_PWM (1 << 7)
42#define MAX8649_SYNC_EXTCLK (1 << 6)
43
44#define MAX8649_EXT_MASK (3 << 6)
45
46#define MAX8649_RAMP_MASK (7 << 5)
47#define MAX8649_RAMP_DOWN (1 << 1)
48
49struct max8649_regulator_info {
50 struct regulator_dev *regulator;
51 struct i2c_client *i2c;
52 struct device *dev;
53 struct mutex io_lock;
54
55 int vol_reg;
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
65static inline int max8649_read_device(struct i2c_client *i2c,
66 int reg, int bytes, void *dest)
67{
68 unsigned char data;
69 int ret;
70
71 data = (unsigned char)reg;
72 ret = i2c_master_send(i2c, &data, 1);
73 if (ret < 0)
74 return ret;
75 ret = i2c_master_recv(i2c, dest, bytes);
76 if (ret < 0)
77 return ret;
78 return 0;
79}
80
81static inline int max8649_write_device(struct i2c_client *i2c,
82 int reg, int bytes, void *src)
83{
84 unsigned char buf[bytes + 1];
85 int ret;
86
87 buf[0] = (unsigned char)reg;
88 memcpy(&buf[1], src, bytes);
89
90 ret = i2c_master_send(i2c, buf, bytes + 1);
91 if (ret < 0)
92 return ret;
93 return 0;
94}
95
96static int max8649_reg_read(struct i2c_client *i2c, int reg)
97{
98 struct max8649_regulator_info *info = i2c_get_clientdata(i2c);
99 unsigned char data;
100 int ret;
101
102 mutex_lock(&info->io_lock);
103 ret = max8649_read_device(i2c, reg, 1, &data);
104 mutex_unlock(&info->io_lock);
105
106 if (ret < 0)
107 return ret;
108 return (int)data;
109}
110
111static int max8649_set_bits(struct i2c_client *i2c, int reg,
112 unsigned char mask, unsigned char data)
113{
114 struct max8649_regulator_info *info = i2c_get_clientdata(i2c);
115 unsigned char value;
116 int ret;
117
118 mutex_lock(&info->io_lock);
119 ret = max8649_read_device(i2c, reg, 1, &value);
120 if (ret < 0)
121 goto out;
122 value &= ~mask;
123 value |= data;
124 ret = max8649_write_device(i2c, reg, 1, &value);
125out:
126 mutex_unlock(&info->io_lock);
127 return ret;
128}
129
130static inline int check_range(int min_uV, int max_uV)
131{
132 if ((min_uV < MAX8649_DCDC_VMIN) || (max_uV > MAX8649_DCDC_VMAX)
133 || (min_uV > max_uV))
134 return -EINVAL;
135 return 0;
136}
137
138static int max8649_list_voltage(struct regulator_dev *rdev, unsigned index)
139{
140 return (MAX8649_DCDC_VMIN + index * MAX8649_DCDC_STEP);
141}
142
143static int max8649_get_voltage(struct regulator_dev *rdev)
144{
145 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
146 unsigned char data;
147 int ret;
148
149 ret = max8649_reg_read(info->i2c, info->vol_reg);
150 if (ret < 0)
151 return ret;
152 data = (unsigned char)ret & MAX8649_VOL_MASK;
153 return max8649_list_voltage(rdev, data);
154}
155
156static int max8649_set_voltage(struct regulator_dev *rdev,
157 int min_uV, int max_uV)
158{
159 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
160 unsigned char data, mask;
161
162 if (check_range(min_uV, max_uV)) {
163 dev_err(info->dev, "invalid voltage range (%d, %d) uV\n",
164 min_uV, max_uV);
165 return -EINVAL;
166 }
167 data = (min_uV - MAX8649_DCDC_VMIN + MAX8649_DCDC_STEP - 1)
168 / MAX8649_DCDC_STEP;
169 mask = MAX8649_VOL_MASK;
170
171 return max8649_set_bits(info->i2c, info->vol_reg, mask, data);
172}
173
174/* EN_PD means pulldown on EN input */
175static int max8649_enable(struct regulator_dev *rdev)
176{
177 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
178 return max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_EN_PD, 0);
179}
180
181/*
182 * Applied internal pulldown resistor on EN input pin.
183 * If pulldown EN pin outside, it would be better.
184 */
185static int max8649_disable(struct regulator_dev *rdev)
186{
187 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
188 return max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_EN_PD,
189 MAX8649_EN_PD);
190}
191
192static int max8649_is_enabled(struct regulator_dev *rdev)
193{
194 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
195 int ret;
196
197 ret = max8649_reg_read(info->i2c, MAX8649_CONTROL);
198 if (ret < 0)
199 return ret;
200 return !((unsigned char)ret & MAX8649_EN_PD);
201}
202
203static int max8649_enable_time(struct regulator_dev *rdev)
204{
205 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
206 int voltage, rate, ret;
207
208 /* get voltage */
209 ret = max8649_reg_read(info->i2c, info->vol_reg);
210 if (ret < 0)
211 return ret;
212 ret &= MAX8649_VOL_MASK;
213 voltage = max8649_list_voltage(rdev, (unsigned char)ret); /* uV */
214
215 /* get rate */
216 ret = max8649_reg_read(info->i2c, MAX8649_RAMP);
217 if (ret < 0)
218 return ret;
219 ret = (ret & MAX8649_RAMP_MASK) >> 5;
220 rate = (32 * 1000) >> ret; /* uV/uS */
221
222 return (voltage / rate);
223}
224
225static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
226{
227 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
228
229 switch (mode) {
230 case REGULATOR_MODE_FAST:
231 max8649_set_bits(info->i2c, info->vol_reg, MAX8649_FORCE_PWM,
232 MAX8649_FORCE_PWM);
233 break;
234 case REGULATOR_MODE_NORMAL:
235 max8649_set_bits(info->i2c, info->vol_reg,
236 MAX8649_FORCE_PWM, 0);
237 break;
238 default:
239 return -EINVAL;
240 }
241 return 0;
242}
243
244static unsigned int max8649_get_mode(struct regulator_dev *rdev)
245{
246 struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
247 int ret;
248
249 ret = max8649_reg_read(info->i2c, info->vol_reg);
250 if (ret & MAX8649_FORCE_PWM)
251 return REGULATOR_MODE_FAST;
252 return REGULATOR_MODE_NORMAL;
253}
254
255static struct regulator_ops max8649_dcdc_ops = {
256 .set_voltage = max8649_set_voltage,
257 .get_voltage = max8649_get_voltage,
258 .list_voltage = max8649_list_voltage,
259 .enable = max8649_enable,
260 .disable = max8649_disable,
261 .is_enabled = max8649_is_enabled,
262 .enable_time = max8649_enable_time,
263 .set_mode = max8649_set_mode,
264 .get_mode = max8649_get_mode,
265
266};
267
268static struct regulator_desc dcdc_desc = {
269 .name = "max8649",
270 .ops = &max8649_dcdc_ops,
271 .type = REGULATOR_VOLTAGE,
272 .n_voltages = 1 << 6,
273 .owner = THIS_MODULE,
274};
275
276static int __devinit max8649_regulator_probe(struct i2c_client *client,
277 const struct i2c_device_id *id)
278{
279 struct max8649_platform_data *pdata = client->dev.platform_data;
280 struct max8649_regulator_info *info = NULL;
281 unsigned char data;
282 int ret;
283
284 info = kzalloc(sizeof(struct max8649_regulator_info), GFP_KERNEL);
285 if (!info) {
286 dev_err(&client->dev, "No enough memory\n");
287 return -ENOMEM;
288 }
289
290 info->i2c = client;
291 info->dev = &client->dev;
292 mutex_init(&info->io_lock);
293 i2c_set_clientdata(client, info);
294
295 info->mode = pdata->mode;
296 switch (info->mode) {
297 case 0:
298 info->vol_reg = MAX8649_MODE0;
299 break;
300 case 1:
301 info->vol_reg = MAX8649_MODE1;
302 break;
303 case 2:
304 info->vol_reg = MAX8649_MODE2;
305 break;
306 case 3:
307 info->vol_reg = MAX8649_MODE3;
308 break;
309 default:
310 break;
311 }
312
313 ret = max8649_reg_read(info->i2c, MAX8649_CHIP_ID1);
314 if (ret < 0) {
315 dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
316 ret);
317 goto out;
318 }
319 dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", ret);
320
321 /* enable VID0 & VID1 */
322 max8649_set_bits(info->i2c, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
323
324 /* enable/disable external clock synchronization */
325 info->extclk = pdata->extclk;
326 data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
327 max8649_set_bits(info->i2c, info->vol_reg, MAX8649_SYNC_EXTCLK, data);
328 if (info->extclk) {
329 /* set external clock frequency */
330 info->extclk_freq = pdata->extclk_freq;
331 max8649_set_bits(info->i2c, MAX8649_SYNC, MAX8649_EXT_MASK,
332 info->extclk_freq);
333 }
334
335 if (pdata->ramp_timing) {
336 info->ramp_timing = pdata->ramp_timing;
337 max8649_set_bits(info->i2c, MAX8649_RAMP, MAX8649_RAMP_MASK,
338 info->ramp_timing << 5);
339 }
340
341 info->ramp_down = pdata->ramp_down;
342 if (info->ramp_down) {
343 max8649_set_bits(info->i2c, MAX8649_RAMP, MAX8649_RAMP_DOWN,
344 MAX8649_RAMP_DOWN);
345 }
346
347 info->regulator = regulator_register(&dcdc_desc, &client->dev,
348 pdata->regulator, info);
349 if (IS_ERR(info->regulator)) {
350 dev_err(info->dev, "failed to register regulator %s\n",
351 dcdc_desc.name);
352 ret = PTR_ERR(info->regulator);
353 goto out;
354 }
355
356 dev_info(info->dev, "Max8649 regulator device is detected.\n");
357 return 0;
358out:
359 kfree(info);
360 return ret;
361}
362
363static int __devexit max8649_regulator_remove(struct i2c_client *client)
364{
365 struct max8649_regulator_info *info = i2c_get_clientdata(client);
366
367 if (info) {
368 if (info->regulator)
369 regulator_unregister(info->regulator);
370 kfree(info);
371 }
372 i2c_set_clientdata(client, NULL);
373
374 return 0;
375}
376
377static const struct i2c_device_id max8649_id[] = {
378 { "max8649", 0 },
379 { }
380};
381MODULE_DEVICE_TABLE(i2c, max8649_id);
382
383static struct i2c_driver max8649_driver = {
384 .probe = max8649_regulator_probe,
385 .remove = __devexit_p(max8649_regulator_remove),
386 .driver = {
387 .name = "max8649",
388 },
389 .id_table = max8649_id,
390};
391
392static int __init max8649_init(void)
393{
394 return i2c_add_driver(&max8649_driver);
395}
396subsys_initcall(max8649_init);
397
398static void __exit max8649_exit(void)
399{
400 i2c_del_driver(&max8649_driver);
401}
402module_exit(max8649_exit);
403
404/* Module information */
405MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
406MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
407MODULE_LICENSE("GPL");
408