xref: /openbmc/linux/drivers/regulator/max8649.c (revision 63dc02bd)
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>
17 #include <linux/slab.h>
18 #include <linux/regulator/max8649.h>
19 #include <linux/regmap.h>
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;
53 	struct device		*dev;
54 	struct regmap		*regmap;
55 
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 
66 static 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 
74 static int max8649_list_voltage(struct regulator_dev *rdev, unsigned index)
75 {
76 	return (MAX8649_DCDC_VMIN + index * MAX8649_DCDC_STEP);
77 }
78 
79 static int max8649_get_voltage(struct regulator_dev *rdev)
80 {
81 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
82 	unsigned int val;
83 	unsigned char data;
84 	int ret;
85 
86 	ret = regmap_read(info->regmap, info->vol_reg, &val);
87 	if (ret != 0)
88 		return ret;
89 	data = (unsigned char)val & MAX8649_VOL_MASK;
90 	return max8649_list_voltage(rdev, data);
91 }
92 
93 static int max8649_set_voltage(struct regulator_dev *rdev,
94 			       int min_uV, int max_uV, unsigned *selector)
95 {
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 	}
104 	data = DIV_ROUND_UP(min_uV - MAX8649_DCDC_VMIN, MAX8649_DCDC_STEP);
105 	mask = MAX8649_VOL_MASK;
106 	*selector = data & mask;
107 
108 	return regmap_update_bits(info->regmap, info->vol_reg, mask, data);
109 }
110 
111 /* EN_PD means pulldown on EN input */
112 static int max8649_enable(struct regulator_dev *rdev)
113 {
114 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
115 	return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD, 0);
116 }
117 
118 /*
119  * Applied internal pulldown resistor on EN input pin.
120  * If pulldown EN pin outside, it would be better.
121  */
122 static int max8649_disable(struct regulator_dev *rdev)
123 {
124 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
125 	return regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_EN_PD,
126 				MAX8649_EN_PD);
127 }
128 
129 static int max8649_is_enabled(struct regulator_dev *rdev)
130 {
131 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
132 	unsigned int val;
133 	int ret;
134 
135 	ret = regmap_read(info->regmap, MAX8649_CONTROL, &val);
136 	if (ret != 0)
137 		return ret;
138 	return !((unsigned char)val & MAX8649_EN_PD);
139 }
140 
141 static int max8649_enable_time(struct regulator_dev *rdev)
142 {
143 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
144 	int voltage, rate, ret;
145 	unsigned int val;
146 
147 	/* get voltage */
148 	ret = regmap_read(info->regmap, info->vol_reg, &val);
149 	if (ret != 0)
150 		return ret;
151 	val &= MAX8649_VOL_MASK;
152 	voltage = max8649_list_voltage(rdev, (unsigned char)val); /* uV */
153 
154 	/* get rate */
155 	ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
156 	if (ret != 0)
157 		return ret;
158 	ret = (val & MAX8649_RAMP_MASK) >> 5;
159 	rate = (32 * 1000) >> ret;	/* uV/uS */
160 
161 	return DIV_ROUND_UP(voltage, rate);
162 }
163 
164 static 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:
170 		regmap_update_bits(info->regmap, info->vol_reg, MAX8649_FORCE_PWM,
171 				   MAX8649_FORCE_PWM);
172 		break;
173 	case REGULATOR_MODE_NORMAL:
174 		regmap_update_bits(info->regmap, info->vol_reg,
175 				   MAX8649_FORCE_PWM, 0);
176 		break;
177 	default:
178 		return -EINVAL;
179 	}
180 	return 0;
181 }
182 
183 static unsigned int max8649_get_mode(struct regulator_dev *rdev)
184 {
185 	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
186 	unsigned int val;
187 	int ret;
188 
189 	ret = regmap_read(info->regmap, info->vol_reg, &val);
190 	if (ret != 0)
191 		return ret;
192 	if (val & MAX8649_FORCE_PWM)
193 		return REGULATOR_MODE_FAST;
194 	return REGULATOR_MODE_NORMAL;
195 }
196 
197 static 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 
210 static struct regulator_desc dcdc_desc = {
211 	.name		= "max8649",
212 	.ops		= &max8649_dcdc_ops,
213 	.type		= REGULATOR_VOLTAGE,
214 	.n_voltages	= 1 << 6,
215 	.owner		= THIS_MODULE,
216 };
217 
218 static struct regmap_config max8649_regmap_config = {
219 	.reg_bits = 8,
220 	.val_bits = 8,
221 };
222 
223 static 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;
228 	unsigned int val;
229 	unsigned char data;
230 	int ret;
231 
232 	info = kzalloc(sizeof(struct max8649_regulator_info), GFP_KERNEL);
233 	if (!info) {
234 		dev_err(&client->dev, "No enough memory\n");
235 		return -ENOMEM;
236 	}
237 
238 	info->regmap = regmap_init_i2c(client, &max8649_regmap_config);
239 	if (IS_ERR(info->regmap)) {
240 		ret = PTR_ERR(info->regmap);
241 		dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
242 		goto fail;
243 	}
244 
245 	info->dev = &client->dev;
246 	i2c_set_clientdata(client, info);
247 
248 	info->mode = pdata->mode;
249 	switch (info->mode) {
250 	case 0:
251 		info->vol_reg = MAX8649_MODE0;
252 		break;
253 	case 1:
254 		info->vol_reg = MAX8649_MODE1;
255 		break;
256 	case 2:
257 		info->vol_reg = MAX8649_MODE2;
258 		break;
259 	case 3:
260 		info->vol_reg = MAX8649_MODE3;
261 		break;
262 	default:
263 		break;
264 	}
265 
266 	ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
267 	if (ret != 0) {
268 		dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
269 			ret);
270 		goto out;
271 	}
272 	dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
273 
274 	/* enable VID0 & VID1 */
275 	regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
276 
277 	/* enable/disable external clock synchronization */
278 	info->extclk = pdata->extclk;
279 	data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
280 	regmap_update_bits(info->regmap, info->vol_reg, MAX8649_SYNC_EXTCLK, data);
281 	if (info->extclk) {
282 		/* set external clock frequency */
283 		info->extclk_freq = pdata->extclk_freq;
284 		regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
285 				   info->extclk_freq << 6);
286 	}
287 
288 	if (pdata->ramp_timing) {
289 		info->ramp_timing = pdata->ramp_timing;
290 		regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
291 				   info->ramp_timing << 5);
292 	}
293 
294 	info->ramp_down = pdata->ramp_down;
295 	if (info->ramp_down) {
296 		regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
297 				   MAX8649_RAMP_DOWN);
298 	}
299 
300 	info->regulator = regulator_register(&dcdc_desc, &client->dev,
301 					     pdata->regulator, info, NULL);
302 	if (IS_ERR(info->regulator)) {
303 		dev_err(info->dev, "failed to register regulator %s\n",
304 			dcdc_desc.name);
305 		ret = PTR_ERR(info->regulator);
306 		goto out;
307 	}
308 
309 	dev_info(info->dev, "Max8649 regulator device is detected.\n");
310 	return 0;
311 out:
312 	regmap_exit(info->regmap);
313 fail:
314 	kfree(info);
315 	return ret;
316 }
317 
318 static int __devexit max8649_regulator_remove(struct i2c_client *client)
319 {
320 	struct max8649_regulator_info *info = i2c_get_clientdata(client);
321 
322 	if (info) {
323 		if (info->regulator)
324 			regulator_unregister(info->regulator);
325 		regmap_exit(info->regmap);
326 		kfree(info);
327 	}
328 
329 	return 0;
330 }
331 
332 static const struct i2c_device_id max8649_id[] = {
333 	{ "max8649", 0 },
334 	{ }
335 };
336 MODULE_DEVICE_TABLE(i2c, max8649_id);
337 
338 static struct i2c_driver max8649_driver = {
339 	.probe		= max8649_regulator_probe,
340 	.remove		= __devexit_p(max8649_regulator_remove),
341 	.driver		= {
342 		.name	= "max8649",
343 	},
344 	.id_table	= max8649_id,
345 };
346 
347 static int __init max8649_init(void)
348 {
349 	return i2c_add_driver(&max8649_driver);
350 }
351 subsys_initcall(max8649_init);
352 
353 static void __exit max8649_exit(void)
354 {
355 	i2c_del_driver(&max8649_driver);
356 }
357 module_exit(max8649_exit);
358 
359 /* Module information */
360 MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
361 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
362 MODULE_LICENSE("GPL");
363 
364