dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / power / ipaq_micro_battery.c
blobf03014ea1dc4a4c229d57acacf040002c875ed8b
1 /*
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.
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>
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>
20 #define BATT_PERIOD 100000 /* 100 seconds in milliseconds */
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
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
41 struct 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;
52 static void micro_battery_work(struct work_struct *work)
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,
59 struct ipaq_micro_msg msg_sensor = {
60 .id = MSG_THERMAL_SENSOR,
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");
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
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];
84 if (msg_battery.rx_len == 9)
85 pr_debug("second battery ignored\n");
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];
91 queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD));
94 static int get_capacity(struct power_supply *b)
96 struct micro_battery *mb = dev_get_drvdata(b->dev.parent);
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;
111 return 0;
114 static int get_status(struct power_supply *b)
116 struct micro_battery *mb = dev_get_drvdata(b->dev.parent);
118 if (mb->flag == MICRO_BATT_STATUS_UNKNOWN)
119 return POWER_SUPPLY_STATUS_UNKNOWN;
121 if (mb->flag & MICRO_BATT_STATUS_FULL)
122 return POWER_SUPPLY_STATUS_FULL;
124 if ((mb->flag & MICRO_BATT_STATUS_CHARGING) ||
125 (mb->flag & MICRO_BATT_STATUS_CHARGEMAIN))
126 return POWER_SUPPLY_STATUS_CHARGING;
128 return POWER_SUPPLY_STATUS_DISCHARGING;
131 static int micro_batt_get_property(struct power_supply *b,
132 enum power_supply_property psp,
133 union power_supply_propval *val)
135 struct micro_battery *mb = dev_get_drvdata(b->dev.parent);
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;
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;
176 return 0;
179 static int micro_ac_get_property(struct power_supply *b,
180 enum power_supply_property psp,
181 union power_supply_propval *val)
183 struct micro_battery *mb = dev_get_drvdata(b->dev.parent);
185 switch (psp) {
186 case POWER_SUPPLY_PROP_ONLINE:
187 val->intval = mb->ac;
188 break;
189 default:
190 return -EINVAL;
193 return 0;
196 static 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,
205 static const struct power_supply_desc micro_batt_power_desc = {
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,
214 static enum power_supply_property micro_ac_power_props[] = {
215 POWER_SUPPLY_PROP_ONLINE,
218 static const struct power_supply_desc micro_ac_power_desc = {
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,
226 static struct power_supply *micro_batt_power, *micro_ac_power;
228 static int micro_batt_probe(struct platform_device *pdev)
230 struct micro_battery *mb;
231 int ret;
233 mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
234 if (!mb)
235 return -ENOMEM;
237 mb->micro = dev_get_drvdata(pdev->dev.parent);
238 mb->wq = create_singlethread_workqueue("ipaq-battery-wq");
239 if (!mb->wq)
240 return -ENOMEM;
242 INIT_DELAYED_WORK(&mb->update, micro_battery_work);
243 platform_set_drvdata(pdev, mb);
244 queue_delayed_work(mb->wq, &mb->update, 1);
246 micro_batt_power = power_supply_register(&pdev->dev,
247 &micro_batt_power_desc, NULL);
248 if (IS_ERR(micro_batt_power)) {
249 ret = PTR_ERR(micro_batt_power);
250 goto batt_err;
253 micro_ac_power = power_supply_register(&pdev->dev,
254 &micro_ac_power_desc, NULL);
255 if (IS_ERR(micro_ac_power)) {
256 ret = PTR_ERR(micro_ac_power);
257 goto ac_err;
260 dev_info(&pdev->dev, "iPAQ micro battery driver\n");
261 return 0;
263 ac_err:
264 power_supply_unregister(micro_ac_power);
265 batt_err:
266 cancel_delayed_work_sync(&mb->update);
267 destroy_workqueue(mb->wq);
268 return ret;
271 static int micro_batt_remove(struct platform_device *pdev)
274 struct micro_battery *mb = platform_get_drvdata(pdev);
276 power_supply_unregister(micro_ac_power);
277 power_supply_unregister(micro_batt_power);
278 cancel_delayed_work_sync(&mb->update);
279 destroy_workqueue(mb->wq);
281 return 0;
284 static int micro_batt_suspend(struct device *dev)
286 struct micro_battery *mb = dev_get_drvdata(dev);
288 cancel_delayed_work_sync(&mb->update);
289 return 0;
292 static int micro_batt_resume(struct device *dev)
294 struct micro_battery *mb = dev_get_drvdata(dev);
296 queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD));
297 return 0;
300 static const struct dev_pm_ops micro_batt_dev_pm_ops = {
301 SET_SYSTEM_SLEEP_PM_OPS(micro_batt_suspend, micro_batt_resume)
304 static struct platform_driver micro_batt_device_driver = {
305 .driver = {
306 .name = "ipaq-micro-battery",
307 .pm = &micro_batt_dev_pm_ops,
309 .probe = micro_batt_probe,
310 .remove = micro_batt_remove,
312 module_platform_driver(micro_batt_device_driver);
314 MODULE_LICENSE("GPL");
315 MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery");
316 MODULE_ALIAS("platform:battery-ipaq-micro");