blob: 5f1bf004dfbc8f80f06304591ff730fea7eaaf99 [file] [log] [blame]
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -07001/*
2 * Battery driver for a Greybus module.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * Released under the GPLv2 only.
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <linux/power_supply.h>
13#include "greybus.h"
14
15struct gb_battery {
16 struct power_supply bat;
Greg Kroah-Hartmand47aa762014-09-07 15:54:24 -070017 // FIXME
18 // we will want to keep the battery stats in here as we will be getting
19 // updates from the SVC "on the fly" so we don't have to always go ask
20 // the battery for some information. Hopefully...
Alex Elder778c69c2014-09-22 19:19:03 -050021 struct greybus_module *gmod;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -070022};
23#define to_gb_battery(x) container_of(x, struct gb_battery, bat)
24
25static const struct greybus_module_id id_table[] = {
26 { GREYBUS_DEVICE(0x42, 0x42) }, /* make shit up */
27 { }, /* terminating NULL entry */
28};
29
30static int get_status(struct gb_battery *gb)
31{
32 // FIXME!!!
33 return 0;
34}
35
36static int get_capacity(struct gb_battery *gb)
37{
38 // FIXME!!!
39 return 0;
40}
41
42static int get_temp(struct gb_battery *gb)
43{
44 // FIXME!!!
45 return 0;
46}
47
48static int get_voltage(struct gb_battery *gb)
49{
50 // FIXME!!!
51 return 0;
52}
53
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -070054static int get_property(struct power_supply *b,
55 enum power_supply_property psp,
56 union power_supply_propval *val)
57{
58 struct gb_battery *gb = to_gb_battery(b);
59
60 switch (psp) {
61 case POWER_SUPPLY_PROP_TECHNOLOGY:
62 // FIXME - guess!
63 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
64 break;
65
66 case POWER_SUPPLY_PROP_STATUS:
67 val->intval = get_status(gb);
68 break;
69
70 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
71 val->intval = 4700000; // FIXME - guess???
72 break;
73
74 case POWER_SUPPLY_PROP_CAPACITY:
75 val->intval = get_capacity(gb);
76 break;
77
78 case POWER_SUPPLY_PROP_TEMP:
79 val->intval = get_temp(gb);
80 break;
81
82 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
83 val->intval = get_voltage(gb);
84 break;
85
86 default:
87 return -EINVAL;
88 }
89
90 return 0;
91}
92
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -070093// FIXME - verify this list, odds are some can be removed and others added.
94static enum power_supply_property battery_props[] = {
95 POWER_SUPPLY_PROP_TECHNOLOGY,
96 POWER_SUPPLY_PROP_STATUS,
97 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
98 POWER_SUPPLY_PROP_CAPACITY,
99 POWER_SUPPLY_PROP_TEMP,
100 POWER_SUPPLY_PROP_VOLTAGE_NOW,
101};
102
Alex Elder778c69c2014-09-22 19:19:03 -0500103int gb_battery_probe(struct greybus_module *gmod,
Greg Kroah-Hartmanfe327042014-09-07 15:57:07 -0700104 const struct greybus_module_id *id)
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700105{
106 struct gb_battery *gb;
107 struct power_supply *b;
108 int retval;
109
110 gb = kzalloc(sizeof(*gb), GFP_KERNEL);
111 if (!gb)
112 return -ENOMEM;
113
114 b = &gb->bat;
115 // FIXME - get a better (i.e. unique) name
116 // FIXME - anything else needs to be set?
117 b->name = "gb_battery";
118 b->type = POWER_SUPPLY_TYPE_BATTERY,
119 b->properties = battery_props,
120 b->num_properties = ARRAY_SIZE(battery_props),
121 b->get_property = get_property,
122
Alex Elder778c69c2014-09-22 19:19:03 -0500123 retval = power_supply_register(&gmod->dev, b);
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700124 if (retval) {
125 kfree(gb);
126 return retval;
127 }
Alex Elder778c69c2014-09-22 19:19:03 -0500128 gmod->gb_battery = gb;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700129
130 return 0;
131}
132
Alex Elder778c69c2014-09-22 19:19:03 -0500133void gb_battery_disconnect(struct greybus_module *gmod)
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700134{
135 struct gb_battery *gb;
136
Alex Elder778c69c2014-09-22 19:19:03 -0500137 gb = gmod->gb_battery;
Greg Kroah-Hartman33ea3a32014-09-07 15:39:34 -0700138
139 power_supply_unregister(&gb->bat);
140
141 kfree(gb);
142}
143
144#if 0
145static struct greybus_driver battery_gb_driver = {
146 .probe = gb_battery_probe,
147 .disconnect = gb_battery_disconnect,
148 .id_table = id_table,
149};
150
151module_greybus_driver(battery_gb_driver);
152MODULE_LICENSE("GPL");
153MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
154#endif