Version:  2.0.40 2.2.26 2.4.37 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19

Linux/drivers/power/gpio-charger.c

  1 /*
  2  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3  *  Driver for chargers which report their online 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/device.h>
 17 #include <linux/gpio.h>
 18 #include <linux/init.h>
 19 #include <linux/interrupt.h>
 20 #include <linux/kernel.h>
 21 #include <linux/module.h>
 22 #include <linux/platform_device.h>
 23 #include <linux/power_supply.h>
 24 #include <linux/slab.h>
 25 #include <linux/of.h>
 26 #include <linux/of_gpio.h>
 27 
 28 #include <linux/power/gpio-charger.h>
 29 
 30 struct gpio_charger {
 31         const struct gpio_charger_platform_data *pdata;
 32         unsigned int irq;
 33         bool wakeup_enabled;
 34 
 35         struct power_supply charger;
 36 };
 37 
 38 static irqreturn_t gpio_charger_irq(int irq, void *devid)
 39 {
 40         struct power_supply *charger = devid;
 41 
 42         power_supply_changed(charger);
 43 
 44         return IRQ_HANDLED;
 45 }
 46 
 47 static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
 48 {
 49         return container_of(psy, struct gpio_charger, charger);
 50 }
 51 
 52 static int gpio_charger_get_property(struct power_supply *psy,
 53                 enum power_supply_property psp, union power_supply_propval *val)
 54 {
 55         struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
 56         const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
 57 
 58         switch (psp) {
 59         case POWER_SUPPLY_PROP_ONLINE:
 60                 val->intval = !!gpio_get_value_cansleep(pdata->gpio);
 61                 val->intval ^= pdata->gpio_active_low;
 62                 break;
 63         default:
 64                 return -EINVAL;
 65         }
 66 
 67         return 0;
 68 }
 69 
 70 static enum power_supply_property gpio_charger_properties[] = {
 71         POWER_SUPPLY_PROP_ONLINE,
 72 };
 73 
 74 static
 75 struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
 76 {
 77         struct device_node *np = dev->of_node;
 78         struct gpio_charger_platform_data *pdata;
 79         const char *chargetype;
 80         enum of_gpio_flags flags;
 81         int ret;
 82 
 83         if (!np)
 84                 return ERR_PTR(-ENOENT);
 85 
 86         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
 87         if (!pdata)
 88                 return ERR_PTR(-ENOMEM);
 89 
 90         pdata->name = np->name;
 91 
 92         pdata->gpio = of_get_gpio_flags(np, 0, &flags);
 93         if (pdata->gpio < 0) {
 94                 if (pdata->gpio != -EPROBE_DEFER)
 95                         dev_err(dev, "could not get charger gpio\n");
 96                 return ERR_PTR(pdata->gpio);
 97         }
 98 
 99         pdata->gpio_active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
100 
101         pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
102         ret = of_property_read_string(np, "charger-type", &chargetype);
103         if (ret >= 0) {
104                 if (!strncmp("unknown", chargetype, 7))
105                         pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
106                 else if (!strncmp("battery", chargetype, 7))
107                         pdata->type = POWER_SUPPLY_TYPE_BATTERY;
108                 else if (!strncmp("ups", chargetype, 3))
109                         pdata->type = POWER_SUPPLY_TYPE_UPS;
110                 else if (!strncmp("mains", chargetype, 5))
111                         pdata->type = POWER_SUPPLY_TYPE_MAINS;
112                 else if (!strncmp("usb-sdp", chargetype, 7))
113                         pdata->type = POWER_SUPPLY_TYPE_USB;
114                 else if (!strncmp("usb-dcp", chargetype, 7))
115                         pdata->type = POWER_SUPPLY_TYPE_USB_DCP;
116                 else if (!strncmp("usb-cdp", chargetype, 7))
117                         pdata->type = POWER_SUPPLY_TYPE_USB_CDP;
118                 else if (!strncmp("usb-aca", chargetype, 7))
119                         pdata->type = POWER_SUPPLY_TYPE_USB_ACA;
120                 else
121                         dev_warn(dev, "unknown charger type %s\n", chargetype);
122         }
123 
124         return pdata;
125 }
126 
127 static int gpio_charger_probe(struct platform_device *pdev)
128 {
129         const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
130         struct gpio_charger *gpio_charger;
131         struct power_supply *charger;
132         int ret;
133         int irq;
134 
135         if (!pdata) {
136                 pdata = gpio_charger_parse_dt(&pdev->dev);
137                 if (IS_ERR(pdata)) {
138                         ret = PTR_ERR(pdata);
139                         if (ret != -EPROBE_DEFER)
140                                 dev_err(&pdev->dev, "No platform data\n");
141                         return ret;
142                 }
143         }
144 
145         if (!gpio_is_valid(pdata->gpio)) {
146                 dev_err(&pdev->dev, "Invalid gpio pin\n");
147                 return -EINVAL;
148         }
149 
150         gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger),
151                                         GFP_KERNEL);
152         if (!gpio_charger) {
153                 dev_err(&pdev->dev, "Failed to alloc driver structure\n");
154                 return -ENOMEM;
155         }
156 
157         charger = &gpio_charger->charger;
158 
159         charger->name = pdata->name ? pdata->name : "gpio-charger";
160         charger->type = pdata->type;
161         charger->properties = gpio_charger_properties;
162         charger->num_properties = ARRAY_SIZE(gpio_charger_properties);
163         charger->get_property = gpio_charger_get_property;
164         charger->supplied_to = pdata->supplied_to;
165         charger->num_supplicants = pdata->num_supplicants;
166         charger->of_node = pdev->dev.of_node;
167 
168         ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
169         if (ret) {
170                 dev_err(&pdev->dev, "Failed to request gpio pin: %d\n", ret);
171                 goto err_free;
172         }
173         ret = gpio_direction_input(pdata->gpio);
174         if (ret) {
175                 dev_err(&pdev->dev, "Failed to set gpio to input: %d\n", ret);
176                 goto err_gpio_free;
177         }
178 
179         gpio_charger->pdata = pdata;
180 
181         ret = power_supply_register(&pdev->dev, charger);
182         if (ret < 0) {
183                 dev_err(&pdev->dev, "Failed to register power supply: %d\n",
184                         ret);
185                 goto err_gpio_free;
186         }
187 
188         irq = gpio_to_irq(pdata->gpio);
189         if (irq > 0) {
190                 ret = request_any_context_irq(irq, gpio_charger_irq,
191                                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
192                                 dev_name(&pdev->dev), charger);
193                 if (ret < 0)
194                         dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret);
195                 else
196                         gpio_charger->irq = irq;
197         }
198 
199         platform_set_drvdata(pdev, gpio_charger);
200 
201         device_init_wakeup(&pdev->dev, 1);
202 
203         return 0;
204 
205 err_gpio_free:
206         gpio_free(pdata->gpio);
207 err_free:
208         return ret;
209 }
210 
211 static int gpio_charger_remove(struct platform_device *pdev)
212 {
213         struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
214 
215         if (gpio_charger->irq)
216                 free_irq(gpio_charger->irq, &gpio_charger->charger);
217 
218         power_supply_unregister(&gpio_charger->charger);
219 
220         gpio_free(gpio_charger->pdata->gpio);
221 
222         return 0;
223 }
224 
225 #ifdef CONFIG_PM_SLEEP
226 static int gpio_charger_suspend(struct device *dev)
227 {
228         struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
229 
230         if (device_may_wakeup(dev))
231                 gpio_charger->wakeup_enabled =
232                         enable_irq_wake(gpio_charger->irq);
233 
234         return 0;
235 }
236 
237 static int gpio_charger_resume(struct device *dev)
238 {
239         struct platform_device *pdev = to_platform_device(dev);
240         struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
241 
242         if (gpio_charger->wakeup_enabled)
243                 disable_irq_wake(gpio_charger->irq);
244         power_supply_changed(&gpio_charger->charger);
245 
246         return 0;
247 }
248 #endif
249 
250 static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
251                 gpio_charger_suspend, gpio_charger_resume);
252 
253 static const struct of_device_id gpio_charger_match[] = {
254         { .compatible = "gpio-charger" },
255         { }
256 };
257 MODULE_DEVICE_TABLE(of, gpio_charger_match);
258 
259 static struct platform_driver gpio_charger_driver = {
260         .probe = gpio_charger_probe,
261         .remove = gpio_charger_remove,
262         .driver = {
263                 .name = "gpio-charger",
264                 .pm = &gpio_charger_pm_ops,
265                 .of_match_table = gpio_charger_match,
266         },
267 };
268 
269 module_platform_driver(gpio_charger_driver);
270 
271 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
272 MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
273 MODULE_LICENSE("GPL");
274 MODULE_ALIAS("platform:gpio-charger");
275 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us