[xburst] Seperate charger logic from battery driver
[openwrt.git] / target / linux / xburst / files-2.6.32 / drivers / power / gpio-charger.c
1 /*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * Driver for chargers indicating their status through a GPIO pin
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/device.h>
21 #include <linux/platform_device.h>
22 #include <linux/power_supply.h>
23 #include <linux/interrupt.h>
24 #include <linux/gpio.h>
25
26 #include <linux/power/gpio-charger.h>
27
28 struct gpio_charger {
29 const struct gpio_charger_platform_data *pdata;
30
31 int irq;
32
33 struct power_supply charger;
34 };
35
36 static irqreturn_t gpio_charger_irq(int irq, void *devid)
37 {
38 struct power_supply *charger = devid;
39 power_supply_changed(charger);
40
41 return IRQ_HANDLED;
42 }
43
44 static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
45 {
46 return container_of(psy, struct gpio_charger, charger);
47 }
48
49 static int gpio_charger_get_property(struct power_supply *psy,
50 enum power_supply_property psp,
51 union power_supply_propval *val)
52 {
53 struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
54 const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
55
56 switch (psp) {
57 case POWER_SUPPLY_PROP_ONLINE:
58 val->intval = gpio_get_value(pdata->gpio);
59 val->intval ^= pdata->gpio_active_low;
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 return 0;
66 }
67
68 static enum power_supply_property gpio_charger_properties[] = {
69 POWER_SUPPLY_PROP_ONLINE,
70 };
71
72 static int __devinit gpio_charger_probe(struct platform_device *pdev)
73 {
74 const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
75 struct gpio_charger *gpio_charger;
76 struct power_supply *charger;
77 int ret;
78
79 if (!pdata) {
80 dev_err(&pdev->dev, "No platform data");
81 return -EINVAL;
82 }
83
84 gpio_charger = kzalloc(sizeof(*gpio_charger), GFP_KERNEL);
85
86 charger = &gpio_charger->charger;
87
88 charger->name = pdata->name;
89 charger->type = pdata->type;
90 charger->properties = gpio_charger_properties;
91 charger->num_properties = ARRAY_SIZE(gpio_charger_properties);
92 charger->get_property = gpio_charger_get_property;
93 charger->supplied_to = pdata->batteries;
94 charger->num_supplicants = pdata->num_batteries;
95
96 if (gpio_is_valid(pdata->gpio)) {
97 ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
98 if (ret) {
99 dev_err(&pdev->dev, "Failed to request gpio pin: %d\n", ret);
100 goto err;
101 }
102 ret = gpio_direction_input(pdata->gpio);
103 if (ret) {
104 dev_err(&pdev->dev, "Failed to set gpio to input: %d\n", ret);
105 goto err_gpio_free;
106 }
107
108 gpio_charger->irq = gpio_to_irq(pdata->gpio);
109 if (gpio_charger->irq >= 0) {
110 ret = request_irq(gpio_charger->irq, gpio_charger_irq,
111 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
112 dev_name(&pdev->dev), charger);
113 if (ret) {
114 dev_warn(&pdev->dev, "Failed to request online gpio irq: %d\n", ret);
115 gpio_charger->irq = -1;
116 }
117 }
118 }
119
120 ret = power_supply_register(&pdev->dev, charger);
121 if (ret < 0) {
122 dev_err(&pdev->dev, "Failed to register power supply: %d\n", ret);
123 goto err_gpio_free;
124 }
125
126 gpio_charger->pdata = pdata;
127 platform_set_drvdata(pdev, gpio_charger);
128
129 return 0;
130
131 err_gpio_free:
132 if (gpio_is_valid(pdata->gpio)) {
133 if (gpio_charger->irq >= 0)
134 free_irq(gpio_charger->irq, charger);
135 gpio_free(pdata->gpio);
136 }
137 err:
138 return ret;
139 }
140
141 static int __devexit gpio_charger_remove(struct platform_device *pdev)
142 {
143 struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
144 const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
145
146 power_supply_unregister(&gpio_charger->charger);
147
148 if (gpio_is_valid(pdata->gpio)) {
149 if (gpio_charger->irq >= 0)
150 free_irq(gpio_charger->irq, &gpio_charger->charger);
151 gpio_free(pdata->gpio);
152 }
153
154 platform_set_drvdata(pdev, NULL);
155 kfree(gpio_charger);
156
157 return 0;
158 }
159
160 static struct platform_driver gpio_charger_driver = {
161 .probe = gpio_charger_probe,
162 .remove = __devexit_p(gpio_charger_remove),
163 .driver = {
164 .name = "gpio-charger",
165 .owner = THIS_MODULE,
166 },
167 };
168
169 static int __init gpio_charger_init(void)
170 {
171 return platform_driver_register(&gpio_charger_driver);
172 }
173 module_init(gpio_charger_init);
174
175 static void gpio_charger_exit(void)
176 {
177 platform_driver_unregister(&gpio_charger_driver);
178 }
179 module_exit(gpio_charger_exit);
180
181 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
182 MODULE_DESCRIPTION("Driver for chargers indicating their status through a gpio");
183 MODULE_LICENSE("GPL");
184 MODULE_ALIAS("platform:gpio-charger");
This page took 0.058727 seconds and 5 git commands to generate.