GTA01: Add "present" property to power_supply
[kernel.git] / drivers / power / gta01_battery.c
1 /*
2  * Dumb driver for gta01 battery
3  *
4  * Copyright 2009 Openmoko, Inc
5  * Balaji Rao <balajirrao@openmoko.org>
6  */
7
8 #include <linux/module.h>
9 #include <linux/param.h>
10 #include <linux/delay.h>
11 #include <linux/workqueue.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/gta01_battery.h>
15
16 struct gta01_battery {
17         struct power_supply psy;
18         struct gta01_bat_platform_data *pdata;
19 };
20
21 static enum power_supply_property gta01_bat_props[] = {
22         POWER_SUPPLY_PROP_PRESENT,
23         POWER_SUPPLY_PROP_STATUS,
24         POWER_SUPPLY_PROP_VOLTAGE_NOW,
25         POWER_SUPPLY_PROP_CURRENT_NOW,
26 };
27
28 static int gta01_bat_get_property(struct power_supply *psy,
29                                        enum power_supply_property psp,
30                                        union power_supply_propval *val)
31 {
32         struct gta01_battery *bat = container_of(psy, struct gta01_battery, psy);
33         
34         switch(psp) {
35         case POWER_SUPPLY_PROP_STATUS:
36                 if (bat->pdata->get_charging_status())
37                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
38                 else
39                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
40                 break;
41         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
42                 val->intval = bat->pdata->get_voltage();
43                 break;
44         case POWER_SUPPLY_PROP_CURRENT_NOW:
45                 val->intval = bat->pdata->get_current();
46                 break;
47         case POWER_SUPPLY_PROP_PRESENT:
48                 val->intval = 1; /* You must never run GTA01 without battery. */
49                 break;
50         default:
51                 return -EINVAL;
52         }
53
54         return 0;
55 }
56
57 static void gta01_bat_ext_changed(struct power_supply *psy)
58 {
59         struct gta01_battery *bat = container_of(psy, struct gta01_battery, psy);
60         power_supply_changed(&bat->psy);
61 }
62
63 static int gta01_battery_probe(struct platform_device *pdev)
64 {
65         struct gta01_battery *gta01_bat;
66
67         gta01_bat = kzalloc(sizeof(*gta01_bat), GFP_KERNEL);
68         if (!gta01_bat)
69                 return -ENOMEM;
70
71         gta01_bat->psy.name = "battery";
72         gta01_bat->psy.type = POWER_SUPPLY_TYPE_BATTERY;
73         gta01_bat->psy.properties = gta01_bat_props;
74         gta01_bat->psy.num_properties = ARRAY_SIZE(gta01_bat_props);
75         gta01_bat->psy.get_property = gta01_bat_get_property;
76         gta01_bat->psy.external_power_changed = gta01_bat_ext_changed;
77
78         gta01_bat->pdata = pdev->dev.platform_data;
79         power_supply_register(&pdev->dev, &gta01_bat->psy);
80
81         return 0;
82 }
83
84 static int gta01_battery_remove(struct platform_device *pdev)
85 {
86         struct gta01_battery *bat = platform_get_drvdata(pdev);
87
88         power_supply_unregister(&bat->psy);
89         kfree(bat);
90
91         return 0;
92 }
93
94 static struct platform_driver gta01_battery_driver = {
95         .driver = {
96                 .name = "gta01_battery",
97         },
98         .probe    = gta01_battery_probe,
99         .remove   = gta01_battery_remove,
100 };
101
102 static int __init gta01_battery_init(void)
103 {
104         return platform_driver_register(&gta01_battery_driver);
105 }
106
107 static void __exit gta01_battery_exit(void)
108 {
109         platform_driver_unregister(&gta01_battery_driver);
110 }
111
112 module_init(gta01_battery_init);
113 module_exit(gta01_battery_exit);
114
115 MODULE_LICENSE("GPL");
116 MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
117 MODULE_DESCRIPTION("gta01 battery driver");