Date:2010-04-24 12:29:31 (13 years 11 months ago)
Author:Lars C.
Commit:77c61e9d04daab74596cdf9b814c01fa57cd7ce6
Message:Add gpio chager driver

Files: drivers/power/Kconfig (1 diff)
drivers/power/Makefile (1 diff)
drivers/power/gpio-charger.c (1 diff)
include/linux/power/gpio-charger.h (1 diff)

Change Details

drivers/power/Kconfig
185185    help
186186      Say Y here to enable support for TWL4030 Battery Charge Interface.
187187
188config CHARGER_GPIO
189    tristate "GPIO charger"
190    depends on GPIOLIB
191    help
192      Say Y to include support for chargers indicating their status through
193      a GPIO pin.
194
188195endif # POWER_SUPPLY
drivers/power/Makefile
3232obj-$(CONFIG_BATTERY_INTEL_MID) += intel_mid_battery.o
3333obj-$(CONFIG_CHARGER_ISP1704) += isp1704_charger.o
3434obj-$(CONFIG_CHARGER_TWL4030) += twl4030_charger.o
35obj-$(CONFIG_CHARGER_GPIO) += gpio-charger.o
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/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/types.h>
26
27#include <linux/power/gpio-charger.h>
28
29struct gpio_charger {
30    const struct gpio_charger_platform_data *pdata;
31
32    int irq;
33
34    struct power_supply charger;
35};
36
37static irqreturn_t gpio_charger_irq(int irq, void *devid)
38{
39    struct power_supply *charger = devid;
40    power_supply_changed(charger);
41
42    return IRQ_HANDLED;
43}
44
45static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
46{
47    return container_of(psy, struct gpio_charger, charger);
48}
49
50static int gpio_charger_get_property(struct power_supply *psy,
51    enum power_supply_property psp, 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
68static enum power_supply_property gpio_charger_properties[] = {
69    POWER_SUPPLY_PROP_ONLINE,
70};
71
72static 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    gpio_charger->pdata = pdata;
121
122    ret = power_supply_register(&pdev->dev, charger);
123    if (ret < 0) {
124        dev_err(&pdev->dev, "Failed to register power supply: %d\n", ret);
125        goto err_gpio_free;
126    }
127
128    platform_set_drvdata(pdev, gpio_charger);
129
130    return 0;
131
132err_gpio_free:
133    if (gpio_is_valid(pdata->gpio)) {
134        if (gpio_charger->irq >= 0)
135            free_irq(gpio_charger->irq, charger);
136        gpio_free(pdata->gpio);
137    }
138err:
139    return ret;
140}
141
142static int __devexit gpio_charger_remove(struct platform_device *pdev)
143{
144    struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
145    const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
146
147    power_supply_unregister(&gpio_charger->charger);
148
149    if (gpio_is_valid(pdata->gpio)) {
150        if (gpio_charger->irq >= 0)
151            free_irq(gpio_charger->irq, &gpio_charger->charger);
152        gpio_free(pdata->gpio);
153    }
154
155    platform_set_drvdata(pdev, NULL);
156    kfree(gpio_charger);
157
158    return 0;
159}
160
161static struct platform_driver gpio_charger_driver = {
162    .probe = gpio_charger_probe,
163    .remove = __devexit_p(gpio_charger_remove),
164    .driver = {
165        .name = "gpio-charger",
166        .owner = THIS_MODULE,
167    },
168};
169
170static int __init gpio_charger_init(void)
171{
172    return platform_driver_register(&gpio_charger_driver);
173}
174module_init(gpio_charger_init);
175
176static void __exit gpio_charger_exit(void)
177{
178    platform_driver_unregister(&gpio_charger_driver);
179}
180module_exit(gpio_charger_exit);
181
182MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
183MODULE_DESCRIPTION("Driver for chargers indicating their status through a gpio");
184MODULE_LICENSE("GPL");
185MODULE_ALIAS("platform:gpio-charger");
include/linux/power/gpio-charger.h
1/*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * You should have received a copy of the GNU General Public License along
10 * with this program; if not, write to the Free Software Foundation, Inc.,
11 * 675 Mass Ave, Cambridge, MA 02139, USA.
12 *
13 */
14
15#ifndef __LINUX_POWER_GPIO_CHARGER_H__
16#define __LINUX_POWER_GPIO_CHARGER_H__
17
18struct gpio_charger_platform_data {
19    const char *name;
20    enum power_supply_type type;
21    int gpio;
22    int gpio_active_low;
23
24    char **batteries;
25    size_t num_batteries;
26};
27
28#endif

Archive Download the corresponding diff file



interactive