asix: fix infinite loop in rx_fixup()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / power / max8925_power.c
bloba70e16d3a3dc1d31942ccf6cf788d06b1c0ea3b1
1 /*
2 * Battery driver for Maxim MAX8925
4 * Copyright (c) 2009-2010 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
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.
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/mfd/max8925.h>
21 /* registers in GPM */
22 #define MAX8925_OUT5VEN 0x54
23 #define MAX8925_OUT3VEN 0x58
24 #define MAX8925_CHG_CNTL1 0x7c
26 /* bits definition */
27 #define MAX8925_CHG_STAT_VSYSLOW (1 << 0)
28 #define MAX8925_CHG_STAT_MODE_MASK (3 << 2)
29 #define MAX8925_CHG_STAT_EN_MASK (1 << 4)
30 #define MAX8925_CHG_MBDET (1 << 1)
31 #define MAX8925_CHG_AC_RANGE_MASK (3 << 6)
33 /* registers in ADC */
34 #define MAX8925_ADC_RES_CNFG1 0x06
35 #define MAX8925_ADC_AVG_CNFG1 0x07
36 #define MAX8925_ADC_ACQ_CNFG1 0x08
37 #define MAX8925_ADC_ACQ_CNFG2 0x09
38 /* 2 bytes registers in below. MSB is 1st, LSB is 2nd. */
39 #define MAX8925_ADC_AUX2 0x62
40 #define MAX8925_ADC_VCHG 0x64
41 #define MAX8925_ADC_VBBATT 0x66
42 #define MAX8925_ADC_VMBATT 0x68
43 #define MAX8925_ADC_ISNS 0x6a
44 #define MAX8925_ADC_THM 0x6c
45 #define MAX8925_ADC_TDIE 0x6e
46 #define MAX8925_CMD_AUX2 0xc8
47 #define MAX8925_CMD_VCHG 0xd0
48 #define MAX8925_CMD_VBBATT 0xd8
49 #define MAX8925_CMD_VMBATT 0xe0
50 #define MAX8925_CMD_ISNS 0xe8
51 #define MAX8925_CMD_THM 0xf0
52 #define MAX8925_CMD_TDIE 0xf8
54 enum {
55 MEASURE_AUX2,
56 MEASURE_VCHG,
57 MEASURE_VBBATT,
58 MEASURE_VMBATT,
59 MEASURE_ISNS,
60 MEASURE_THM,
61 MEASURE_TDIE,
62 MEASURE_MAX,
65 struct max8925_power_info {
66 struct max8925_chip *chip;
67 struct i2c_client *gpm;
68 struct i2c_client *adc;
70 struct power_supply ac;
71 struct power_supply usb;
72 struct power_supply battery;
73 int irq_base;
74 unsigned ac_online:1;
75 unsigned usb_online:1;
76 unsigned bat_online:1;
77 unsigned chg_mode:2;
78 unsigned batt_detect:1; /* detecing MB by ID pin */
79 unsigned topoff_threshold:2;
80 unsigned fast_charge:3;
82 int (*set_charger) (int);
85 static int __set_charger(struct max8925_power_info *info, int enable)
87 struct max8925_chip *chip = info->chip;
88 if (enable) {
89 /* enable charger in platform */
90 if (info->set_charger)
91 info->set_charger(1);
92 /* enable charger */
93 max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 0);
94 } else {
95 /* disable charge */
96 max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 1 << 7);
97 if (info->set_charger)
98 info->set_charger(0);
100 dev_dbg(chip->dev, "%s\n", (enable) ? "Enable charger"
101 : "Disable charger");
102 return 0;
105 static irqreturn_t max8925_charger_handler(int irq, void *data)
107 struct max8925_power_info *info = (struct max8925_power_info *)data;
108 struct max8925_chip *chip = info->chip;
110 switch (irq - chip->irq_base) {
111 case MAX8925_IRQ_VCHG_DC_R:
112 info->ac_online = 1;
113 __set_charger(info, 1);
114 dev_dbg(chip->dev, "Adapter inserted\n");
115 break;
116 case MAX8925_IRQ_VCHG_DC_F:
117 info->ac_online = 0;
118 __set_charger(info, 0);
119 dev_dbg(chip->dev, "Adapter is removal\n");
120 break;
121 case MAX8925_IRQ_VCHG_USB_R:
122 info->usb_online = 1;
123 __set_charger(info, 1);
124 dev_dbg(chip->dev, "USB inserted\n");
125 break;
126 case MAX8925_IRQ_VCHG_USB_F:
127 info->usb_online = 0;
128 __set_charger(info, 0);
129 dev_dbg(chip->dev, "USB is removal\n");
130 break;
131 case MAX8925_IRQ_VCHG_THM_OK_F:
132 /* Battery is not ready yet */
133 dev_dbg(chip->dev, "Battery temperature is out of range\n");
134 case MAX8925_IRQ_VCHG_DC_OVP:
135 dev_dbg(chip->dev, "Error detection\n");
136 __set_charger(info, 0);
137 break;
138 case MAX8925_IRQ_VCHG_THM_OK_R:
139 /* Battery is ready now */
140 dev_dbg(chip->dev, "Battery temperature is in range\n");
141 break;
142 case MAX8925_IRQ_VCHG_SYSLOW_R:
143 /* VSYS is low */
144 dev_info(chip->dev, "Sys power is too low\n");
145 break;
146 case MAX8925_IRQ_VCHG_SYSLOW_F:
147 dev_dbg(chip->dev, "Sys power is above low threshold\n");
148 break;
149 case MAX8925_IRQ_VCHG_DONE:
150 __set_charger(info, 0);
151 dev_dbg(chip->dev, "Charging is done\n");
152 break;
153 case MAX8925_IRQ_VCHG_TOPOFF:
154 dev_dbg(chip->dev, "Charging in top-off mode\n");
155 break;
156 case MAX8925_IRQ_VCHG_TMR_FAULT:
157 __set_charger(info, 0);
158 dev_dbg(chip->dev, "Safe timer is expired\n");
159 break;
160 case MAX8925_IRQ_VCHG_RST:
161 __set_charger(info, 0);
162 dev_dbg(chip->dev, "Charger is reset\n");
163 break;
165 return IRQ_HANDLED;
168 static int start_measure(struct max8925_power_info *info, int type)
170 unsigned char buf[2] = {0, 0};
171 int meas_reg = 0, ret;
173 switch (type) {
174 case MEASURE_VCHG:
175 meas_reg = MAX8925_ADC_VCHG;
176 break;
177 case MEASURE_VBBATT:
178 meas_reg = MAX8925_ADC_VBBATT;
179 break;
180 case MEASURE_VMBATT:
181 meas_reg = MAX8925_ADC_VMBATT;
182 break;
183 case MEASURE_ISNS:
184 meas_reg = MAX8925_ADC_ISNS;
185 break;
186 default:
187 return -EINVAL;
190 max8925_bulk_read(info->adc, meas_reg, 2, buf);
191 ret = (buf[0] << 4) | (buf[1] >> 4);
193 return ret;
196 static int max8925_ac_get_prop(struct power_supply *psy,
197 enum power_supply_property psp,
198 union power_supply_propval *val)
200 struct max8925_power_info *info = dev_get_drvdata(psy->dev->parent);
201 int ret = 0;
203 switch (psp) {
204 case POWER_SUPPLY_PROP_ONLINE:
205 val->intval = info->ac_online;
206 break;
207 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
208 if (info->ac_online) {
209 ret = start_measure(info, MEASURE_VCHG);
210 if (ret >= 0) {
211 val->intval = ret << 1; /* unit is mV */
212 goto out;
215 ret = -ENODATA;
216 break;
217 default:
218 ret = -ENODEV;
219 break;
221 out:
222 return ret;
225 static enum power_supply_property max8925_ac_props[] = {
226 POWER_SUPPLY_PROP_ONLINE,
227 POWER_SUPPLY_PROP_VOLTAGE_NOW,
230 static int max8925_usb_get_prop(struct power_supply *psy,
231 enum power_supply_property psp,
232 union power_supply_propval *val)
234 struct max8925_power_info *info = dev_get_drvdata(psy->dev->parent);
235 int ret = 0;
237 switch (psp) {
238 case POWER_SUPPLY_PROP_ONLINE:
239 val->intval = info->usb_online;
240 break;
241 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
242 if (info->usb_online) {
243 ret = start_measure(info, MEASURE_VCHG);
244 if (ret >= 0) {
245 val->intval = ret << 1; /* unit is mV */
246 goto out;
249 ret = -ENODATA;
250 break;
251 default:
252 ret = -ENODEV;
253 break;
255 out:
256 return ret;
259 static enum power_supply_property max8925_usb_props[] = {
260 POWER_SUPPLY_PROP_ONLINE,
261 POWER_SUPPLY_PROP_VOLTAGE_NOW,
264 static int max8925_bat_get_prop(struct power_supply *psy,
265 enum power_supply_property psp,
266 union power_supply_propval *val)
268 struct max8925_power_info *info = dev_get_drvdata(psy->dev->parent);
269 long long int tmp = 0;
270 int ret = 0;
272 switch (psp) {
273 case POWER_SUPPLY_PROP_ONLINE:
274 val->intval = info->bat_online;
275 break;
276 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
277 if (info->bat_online) {
278 ret = start_measure(info, MEASURE_VMBATT);
279 if (ret >= 0) {
280 val->intval = ret << 1; /* unit is mV */
281 ret = 0;
282 break;
285 ret = -ENODATA;
286 break;
287 case POWER_SUPPLY_PROP_CURRENT_NOW:
288 if (info->bat_online) {
289 ret = start_measure(info, MEASURE_ISNS);
290 if (ret >= 0) {
291 tmp = (long long int)ret * 6250 / 4096 - 3125;
292 ret = (int)tmp;
293 val->intval = 0;
294 if (ret > 0)
295 val->intval = ret; /* unit is mA */
296 ret = 0;
297 break;
300 ret = -ENODATA;
301 break;
302 case POWER_SUPPLY_PROP_CHARGE_TYPE:
303 if (!info->bat_online) {
304 ret = -ENODATA;
305 break;
307 ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
308 ret = (ret & MAX8925_CHG_STAT_MODE_MASK) >> 2;
309 switch (ret) {
310 case 1:
311 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
312 break;
313 case 0:
314 case 2:
315 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
316 break;
317 case 3:
318 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
319 break;
321 ret = 0;
322 break;
323 case POWER_SUPPLY_PROP_STATUS:
324 if (!info->bat_online) {
325 ret = -ENODATA;
326 break;
328 ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
329 if (info->usb_online || info->ac_online) {
330 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
331 if (ret & MAX8925_CHG_STAT_EN_MASK)
332 val->intval = POWER_SUPPLY_STATUS_CHARGING;
333 } else
334 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
335 ret = 0;
336 break;
337 default:
338 ret = -ENODEV;
339 break;
341 return ret;
344 static enum power_supply_property max8925_battery_props[] = {
345 POWER_SUPPLY_PROP_ONLINE,
346 POWER_SUPPLY_PROP_VOLTAGE_NOW,
347 POWER_SUPPLY_PROP_CURRENT_NOW,
348 POWER_SUPPLY_PROP_CHARGE_TYPE,
349 POWER_SUPPLY_PROP_STATUS,
352 #define REQUEST_IRQ(_irq, _name) \
353 do { \
354 ret = request_threaded_irq(chip->irq_base + _irq, NULL, \
355 max8925_charger_handler, \
356 IRQF_ONESHOT, _name, info); \
357 if (ret) \
358 dev_err(chip->dev, "Failed to request IRQ #%d: %d\n", \
359 _irq, ret); \
360 } while (0)
362 static __devinit int max8925_init_charger(struct max8925_chip *chip,
363 struct max8925_power_info *info)
365 int ret;
367 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_OVP, "ac-ovp");
368 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_F, "ac-remove");
369 REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_R, "ac-insert");
370 REQUEST_IRQ(MAX8925_IRQ_VCHG_USB_OVP, "usb-ovp");
371 REQUEST_IRQ(MAX8925_IRQ_VCHG_USB_F, "usb-remove");
372 REQUEST_IRQ(MAX8925_IRQ_VCHG_USB_R, "usb-insert");
373 REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_R, "batt-temp-in-range");
374 REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_F, "batt-temp-out-range");
375 REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_F, "vsys-high");
376 REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_R, "vsys-low");
377 REQUEST_IRQ(MAX8925_IRQ_VCHG_RST, "charger-reset");
378 REQUEST_IRQ(MAX8925_IRQ_VCHG_DONE, "charger-done");
379 REQUEST_IRQ(MAX8925_IRQ_VCHG_TOPOFF, "charger-topoff");
380 REQUEST_IRQ(MAX8925_IRQ_VCHG_TMR_FAULT, "charger-timer-expire");
382 info->ac_online = 0;
383 info->usb_online = 0;
384 info->bat_online = 0;
385 ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
386 if (ret >= 0) {
388 * If battery detection is enabled, ID pin of battery is
389 * connected to MBDET pin of MAX8925. It could be used to
390 * detect battery presence.
391 * Otherwise, we have to assume that battery is always on.
393 if (info->batt_detect)
394 info->bat_online = (ret & MAX8925_CHG_MBDET) ? 0 : 1;
395 else
396 info->bat_online = 1;
397 if (ret & MAX8925_CHG_AC_RANGE_MASK)
398 info->ac_online = 1;
399 else
400 info->ac_online = 0;
402 /* disable charge */
403 max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 1 << 7);
404 /* set charging current in charge topoff mode */
405 max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 3 << 5,
406 info->topoff_threshold << 5);
407 /* set charing current in fast charge mode */
408 max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 7, info->fast_charge);
410 return 0;
413 static __devexit int max8925_deinit_charger(struct max8925_power_info *info)
415 struct max8925_chip *chip = info->chip;
416 int irq;
418 irq = chip->irq_base + MAX8925_IRQ_VCHG_DC_OVP;
419 for (; irq <= chip->irq_base + MAX8925_IRQ_VCHG_TMR_FAULT; irq++)
420 free_irq(irq, info);
422 return 0;
425 static __devinit int max8925_power_probe(struct platform_device *pdev)
427 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
428 struct max8925_power_pdata *pdata = NULL;
429 struct max8925_power_info *info;
430 int ret;
432 pdata = pdev->dev.platform_data;
433 if (!pdata) {
434 dev_err(&pdev->dev, "platform data isn't assigned to "
435 "power supply\n");
436 return -EINVAL;
439 info = kzalloc(sizeof(struct max8925_power_info), GFP_KERNEL);
440 if (!info)
441 return -ENOMEM;
442 info->chip = chip;
443 info->gpm = chip->i2c;
444 info->adc = chip->adc;
445 platform_set_drvdata(pdev, info);
447 info->ac.name = "max8925-ac";
448 info->ac.type = POWER_SUPPLY_TYPE_MAINS;
449 info->ac.properties = max8925_ac_props;
450 info->ac.num_properties = ARRAY_SIZE(max8925_ac_props);
451 info->ac.get_property = max8925_ac_get_prop;
452 ret = power_supply_register(&pdev->dev, &info->ac);
453 if (ret)
454 goto out;
455 info->ac.dev->parent = &pdev->dev;
457 info->usb.name = "max8925-usb";
458 info->usb.type = POWER_SUPPLY_TYPE_USB;
459 info->usb.properties = max8925_usb_props;
460 info->usb.num_properties = ARRAY_SIZE(max8925_usb_props);
461 info->usb.get_property = max8925_usb_get_prop;
462 ret = power_supply_register(&pdev->dev, &info->usb);
463 if (ret)
464 goto out_usb;
465 info->usb.dev->parent = &pdev->dev;
467 info->battery.name = "max8925-battery";
468 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
469 info->battery.properties = max8925_battery_props;
470 info->battery.num_properties = ARRAY_SIZE(max8925_battery_props);
471 info->battery.get_property = max8925_bat_get_prop;
472 ret = power_supply_register(&pdev->dev, &info->battery);
473 if (ret)
474 goto out_battery;
475 info->battery.dev->parent = &pdev->dev;
477 info->batt_detect = pdata->batt_detect;
478 info->topoff_threshold = pdata->topoff_threshold;
479 info->fast_charge = pdata->fast_charge;
480 info->set_charger = pdata->set_charger;
482 max8925_init_charger(chip, info);
483 return 0;
484 out_battery:
485 power_supply_unregister(&info->battery);
486 out_usb:
487 power_supply_unregister(&info->ac);
488 out:
489 kfree(info);
490 return ret;
493 static __devexit int max8925_power_remove(struct platform_device *pdev)
495 struct max8925_power_info *info = platform_get_drvdata(pdev);
497 if (info) {
498 power_supply_unregister(&info->ac);
499 power_supply_unregister(&info->usb);
500 power_supply_unregister(&info->battery);
501 max8925_deinit_charger(info);
502 kfree(info);
504 return 0;
507 static struct platform_driver max8925_power_driver = {
508 .probe = max8925_power_probe,
509 .remove = __devexit_p(max8925_power_remove),
510 .driver = {
511 .name = "max8925-power",
515 static int __init max8925_power_init(void)
517 return platform_driver_register(&max8925_power_driver);
519 module_init(max8925_power_init);
521 static void __exit max8925_power_exit(void)
523 platform_driver_unregister(&max8925_power_driver);
525 module_exit(max8925_power_exit);
527 MODULE_LICENSE("GPL");
528 MODULE_DESCRIPTION("Power supply driver for MAX8925");
529 MODULE_ALIAS("platform:max8925-power");