blob: 9d694605cdb77f35456dad106425b760266f7927 [file] [log] [blame]
Dmitry Artamonow00a588f2014-07-24 16:00:32 +02001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 *
6 * h3xxx atmel micro companion support, battery subdevice
7 * based on previous kernel 2.4 version
8 * Author : Alessandro Gardich <gremlin@gremlin.it>
9 * Author : Linus Walleij <linus.walleij@linaro.org>
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/platform_device.h>
16#include <linux/mfd/ipaq-micro.h>
17#include <linux/power_supply.h>
18#include <linux/workqueue.h>
19
20#define BATT_PERIOD 100000 /* 100 seconds in milliseconds */
21
22#define MICRO_BATT_CHEM_ALKALINE 0x01
23#define MICRO_BATT_CHEM_NICD 0x02
24#define MICRO_BATT_CHEM_NIMH 0x03
25#define MICRO_BATT_CHEM_LION 0x04
26#define MICRO_BATT_CHEM_LIPOLY 0x05
27#define MICRO_BATT_CHEM_NOT_INSTALLED 0x06
28#define MICRO_BATT_CHEM_UNKNOWN 0xff
29
30#define MICRO_BATT_STATUS_HIGH 0x01
31#define MICRO_BATT_STATUS_LOW 0x02
32#define MICRO_BATT_STATUS_CRITICAL 0x04
33#define MICRO_BATT_STATUS_CHARGING 0x08
34#define MICRO_BATT_STATUS_CHARGEMAIN 0x10
35#define MICRO_BATT_STATUS_DEAD 0x20 /* Battery will not charge */
36#define MICRO_BATT_STATUS_NOTINSTALLED 0x20 /* For expansion pack batteries */
37#define MICRO_BATT_STATUS_FULL 0x40 /* Battery fully charged */
38#define MICRO_BATT_STATUS_NOBATTERY 0x80
39#define MICRO_BATT_STATUS_UNKNOWN 0xff
40
41struct micro_battery {
42 struct ipaq_micro *micro;
43 struct workqueue_struct *wq;
44 struct delayed_work update;
45 u8 ac;
46 u8 chemistry;
47 unsigned int voltage;
48 u16 temperature;
49 u8 flag;
50};
51
52static void micro_battery_work(struct work_struct *work)
53{
54 struct micro_battery *mb = container_of(work,
55 struct micro_battery, update.work);
56 struct ipaq_micro_msg msg_battery = {
57 .id = MSG_BATTERY,
58 };
59 struct ipaq_micro_msg msg_sensor = {
60 .id = MSG_THERMAL_SENSOR,
61 };
62
63 /* First send battery message */
64 ipaq_micro_tx_msg_sync(mb->micro, &msg_battery);
65 if (msg_battery.rx_len < 4)
66 pr_info("ERROR");
67
68 /*
69 * Returned message format:
70 * byte 0: 0x00 = Not plugged in
71 * 0x01 = AC adapter plugged in
72 * byte 1: chemistry
73 * byte 2: voltage LSB
74 * byte 3: voltage MSB
75 * byte 4: flags
76 * byte 5-9: same for battery 2
77 */
78 mb->ac = msg_battery.rx_data[0];
79 mb->chemistry = msg_battery.rx_data[1];
80 mb->voltage = ((((unsigned short)msg_battery.rx_data[3] << 8) +
81 msg_battery.rx_data[2]) * 5000L) * 1000 / 1024;
82 mb->flag = msg_battery.rx_data[4];
83
84 if (msg_battery.rx_len == 9)
85 pr_debug("second battery ignored\n");
86
87 /* Then read the sensor */
88 ipaq_micro_tx_msg_sync(mb->micro, &msg_sensor);
89 mb->temperature = msg_sensor.rx_data[1] << 8 | msg_sensor.rx_data[0];
90
91 queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD));
92}
93
94static int get_capacity(struct power_supply *b)
95{
96 struct micro_battery *mb = dev_get_drvdata(b->dev->parent);
97
98 switch (mb->flag & 0x07) {
99 case MICRO_BATT_STATUS_HIGH:
100 return 100;
101 break;
102 case MICRO_BATT_STATUS_LOW:
103 return 50;
104 break;
105 case MICRO_BATT_STATUS_CRITICAL:
106 return 5;
107 break;
108 default:
109 break;
110 }
111 return 0;
112}
113
114static int get_status(struct power_supply *b)
115{
116 struct micro_battery *mb = dev_get_drvdata(b->dev->parent);
117
118 if (mb->flag == MICRO_BATT_STATUS_UNKNOWN)
119 return POWER_SUPPLY_STATUS_UNKNOWN;
120
121 if (mb->flag & MICRO_BATT_STATUS_FULL)
122 return POWER_SUPPLY_STATUS_FULL;
123
124 if ((mb->flag & MICRO_BATT_STATUS_CHARGING) ||
125 (mb->flag & MICRO_BATT_STATUS_CHARGEMAIN))
126 return POWER_SUPPLY_STATUS_CHARGING;
127
128 return POWER_SUPPLY_STATUS_DISCHARGING;
129}
130
131static int micro_batt_get_property(struct power_supply *b,
132 enum power_supply_property psp,
133 union power_supply_propval *val)
134{
135 struct micro_battery *mb = dev_get_drvdata(b->dev->parent);
136
137 switch (psp) {
138 case POWER_SUPPLY_PROP_TECHNOLOGY:
139 switch (mb->chemistry) {
140 case MICRO_BATT_CHEM_NICD:
141 val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
142 break;
143 case MICRO_BATT_CHEM_NIMH:
144 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
145 break;
146 case MICRO_BATT_CHEM_LION:
147 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
148 break;
149 case MICRO_BATT_CHEM_LIPOLY:
150 val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
151 break;
152 default:
153 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
154 break;
155 };
156 break;
157 case POWER_SUPPLY_PROP_STATUS:
158 val->intval = get_status(b);
159 break;
160 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
161 val->intval = 4700000;
162 break;
163 case POWER_SUPPLY_PROP_CAPACITY:
164 val->intval = get_capacity(b);
165 break;
166 case POWER_SUPPLY_PROP_TEMP:
167 val->intval = mb->temperature;
168 break;
169 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
170 val->intval = mb->voltage;
171 break;
172 default:
173 return -EINVAL;
174 };
175
176 return 0;
177}
178
179static int micro_ac_get_property(struct power_supply *b,
180 enum power_supply_property psp,
181 union power_supply_propval *val)
182{
183 struct micro_battery *mb = dev_get_drvdata(b->dev->parent);
184
185 switch (psp) {
186 case POWER_SUPPLY_PROP_ONLINE:
187 val->intval = mb->ac;
188 break;
189 default:
190 return -EINVAL;
191 };
192
193 return 0;
194}
195
196static enum power_supply_property micro_batt_power_props[] = {
197 POWER_SUPPLY_PROP_TECHNOLOGY,
198 POWER_SUPPLY_PROP_STATUS,
199 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
200 POWER_SUPPLY_PROP_CAPACITY,
201 POWER_SUPPLY_PROP_TEMP,
202 POWER_SUPPLY_PROP_VOLTAGE_NOW,
203};
204
205static struct power_supply micro_batt_power = {
206 .name = "main-battery",
207 .type = POWER_SUPPLY_TYPE_BATTERY,
208 .properties = micro_batt_power_props,
209 .num_properties = ARRAY_SIZE(micro_batt_power_props),
210 .get_property = micro_batt_get_property,
211 .use_for_apm = 1,
212};
213
214static enum power_supply_property micro_ac_power_props[] = {
215 POWER_SUPPLY_PROP_ONLINE,
216};
217
218static struct power_supply micro_ac_power = {
219 .name = "ac",
220 .type = POWER_SUPPLY_TYPE_MAINS,
221 .properties = micro_ac_power_props,
222 .num_properties = ARRAY_SIZE(micro_ac_power_props),
223 .get_property = micro_ac_get_property,
224};
225
226static int micro_batt_probe(struct platform_device *pdev)
227{
228 struct micro_battery *mb;
229
230 mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
231 if (!mb)
232 return -ENOMEM;
233
234 mb->micro = dev_get_drvdata(pdev->dev.parent);
235 mb->wq = create_singlethread_workqueue("ipaq-battery-wq");
236 INIT_DELAYED_WORK(&mb->update, micro_battery_work);
237 platform_set_drvdata(pdev, mb);
238 queue_delayed_work(mb->wq, &mb->update, 1);
239 power_supply_register(&pdev->dev, &micro_batt_power);
240 power_supply_register(&pdev->dev, &micro_ac_power);
241
242 dev_info(&pdev->dev, "iPAQ micro battery driver\n");
243 return 0;
244}
245
246static int micro_batt_remove(struct platform_device *pdev)
247
248{
249 struct micro_battery *mb = platform_get_drvdata(pdev);
250
251 power_supply_unregister(&micro_ac_power);
252 power_supply_unregister(&micro_batt_power);
253 cancel_delayed_work_sync(&mb->update);
254
255 return 0;
256}
257
258static int micro_batt_suspend(struct device *dev)
259{
260 struct micro_battery *mb = dev_get_drvdata(dev);
261
262 cancel_delayed_work_sync(&mb->update);
263 return 0;
264}
265
266static int micro_batt_resume(struct device *dev)
267{
268 struct micro_battery *mb = dev_get_drvdata(dev);
269
270 queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD));
271 return 0;
272}
273
274static const struct dev_pm_ops micro_batt_dev_pm_ops = {
275 SET_SYSTEM_SLEEP_PM_OPS(micro_batt_suspend, micro_batt_resume)
276};
277
Wei Yongjun5e371952014-07-28 21:14:21 +0800278static struct platform_driver micro_batt_device_driver = {
Dmitry Artamonow00a588f2014-07-24 16:00:32 +0200279 .driver = {
280 .name = "ipaq-micro-battery",
281 .pm = &micro_batt_dev_pm_ops,
282 },
283 .probe = micro_batt_probe,
284 .remove = micro_batt_remove,
285};
286module_platform_driver(micro_batt_device_driver);
287
288MODULE_LICENSE("GPL");
289MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery");
290MODULE_ALIAS("platform:battery-ipaq-micro");