1 // SPDX-License-Identifier: GPL-2.0-only
4 * h3xxx atmel micro companion support, battery subdevice
5 * based on previous kernel 2.4 version
6 * Author : Alessandro Gardich <gremlin@gremlin.it>
7 * Author : Linus Walleij <linus.walleij@linaro.org>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/mfd/ipaq-micro.h>
14 #include <linux/power_supply.h>
15 #include <linux/workqueue.h>
17 #define BATT_PERIOD 100000 /* 100 seconds in milliseconds */
19 #define MICRO_BATT_CHEM_ALKALINE 0x01
20 #define MICRO_BATT_CHEM_NICD 0x02
21 #define MICRO_BATT_CHEM_NIMH 0x03
22 #define MICRO_BATT_CHEM_LION 0x04
23 #define MICRO_BATT_CHEM_LIPOLY 0x05
24 #define MICRO_BATT_CHEM_NOT_INSTALLED 0x06
25 #define MICRO_BATT_CHEM_UNKNOWN 0xff
27 #define MICRO_BATT_STATUS_HIGH 0x01
28 #define MICRO_BATT_STATUS_LOW 0x02
29 #define MICRO_BATT_STATUS_CRITICAL 0x04
30 #define MICRO_BATT_STATUS_CHARGING 0x08
31 #define MICRO_BATT_STATUS_CHARGEMAIN 0x10
32 #define MICRO_BATT_STATUS_DEAD 0x20 /* Battery will not charge */
33 #define MICRO_BATT_STATUS_NOTINSTALLED 0x20 /* For expansion pack batteries */
34 #define MICRO_BATT_STATUS_FULL 0x40 /* Battery fully charged */
35 #define MICRO_BATT_STATUS_NOBATTERY 0x80
36 #define MICRO_BATT_STATUS_UNKNOWN 0xff
38 struct micro_battery
{
39 struct ipaq_micro
*micro
;
40 struct workqueue_struct
*wq
;
41 struct delayed_work update
;
49 static void micro_battery_work(struct work_struct
*work
)
51 struct micro_battery
*mb
= container_of(work
,
52 struct micro_battery
, update
.work
);
53 struct ipaq_micro_msg msg_battery
= {
56 struct ipaq_micro_msg msg_sensor
= {
57 .id
= MSG_THERMAL_SENSOR
,
60 /* First send battery message */
61 ipaq_micro_tx_msg_sync(mb
->micro
, &msg_battery
);
62 if (msg_battery
.rx_len
< 4)
66 * Returned message format:
67 * byte 0: 0x00 = Not plugged in
68 * 0x01 = AC adapter plugged in
73 * byte 5-9: same for battery 2
75 mb
->ac
= msg_battery
.rx_data
[0];
76 mb
->chemistry
= msg_battery
.rx_data
[1];
77 mb
->voltage
= ((((unsigned short)msg_battery
.rx_data
[3] << 8) +
78 msg_battery
.rx_data
[2]) * 5000L) * 1000 / 1024;
79 mb
->flag
= msg_battery
.rx_data
[4];
81 if (msg_battery
.rx_len
== 9)
82 pr_debug("second battery ignored\n");
84 /* Then read the sensor */
85 ipaq_micro_tx_msg_sync(mb
->micro
, &msg_sensor
);
86 mb
->temperature
= msg_sensor
.rx_data
[1] << 8 | msg_sensor
.rx_data
[0];
88 queue_delayed_work(mb
->wq
, &mb
->update
, msecs_to_jiffies(BATT_PERIOD
));
91 static int get_capacity(struct power_supply
*b
)
93 struct micro_battery
*mb
= dev_get_drvdata(b
->dev
.parent
);
95 switch (mb
->flag
& 0x07) {
96 case MICRO_BATT_STATUS_HIGH
:
99 case MICRO_BATT_STATUS_LOW
:
102 case MICRO_BATT_STATUS_CRITICAL
:
111 static int get_status(struct power_supply
*b
)
113 struct micro_battery
*mb
= dev_get_drvdata(b
->dev
.parent
);
115 if (mb
->flag
== MICRO_BATT_STATUS_UNKNOWN
)
116 return POWER_SUPPLY_STATUS_UNKNOWN
;
118 if (mb
->flag
& MICRO_BATT_STATUS_FULL
)
119 return POWER_SUPPLY_STATUS_FULL
;
121 if ((mb
->flag
& MICRO_BATT_STATUS_CHARGING
) ||
122 (mb
->flag
& MICRO_BATT_STATUS_CHARGEMAIN
))
123 return POWER_SUPPLY_STATUS_CHARGING
;
125 return POWER_SUPPLY_STATUS_DISCHARGING
;
128 static int micro_batt_get_property(struct power_supply
*b
,
129 enum power_supply_property psp
,
130 union power_supply_propval
*val
)
132 struct micro_battery
*mb
= dev_get_drvdata(b
->dev
.parent
);
135 case POWER_SUPPLY_PROP_TECHNOLOGY
:
136 switch (mb
->chemistry
) {
137 case MICRO_BATT_CHEM_NICD
:
138 val
->intval
= POWER_SUPPLY_TECHNOLOGY_NiCd
;
140 case MICRO_BATT_CHEM_NIMH
:
141 val
->intval
= POWER_SUPPLY_TECHNOLOGY_NiMH
;
143 case MICRO_BATT_CHEM_LION
:
144 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
146 case MICRO_BATT_CHEM_LIPOLY
:
147 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LIPO
;
150 val
->intval
= POWER_SUPPLY_TECHNOLOGY_UNKNOWN
;
154 case POWER_SUPPLY_PROP_STATUS
:
155 val
->intval
= get_status(b
);
157 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
158 val
->intval
= 4700000;
160 case POWER_SUPPLY_PROP_CAPACITY
:
161 val
->intval
= get_capacity(b
);
163 case POWER_SUPPLY_PROP_TEMP
:
164 val
->intval
= mb
->temperature
;
166 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
167 val
->intval
= mb
->voltage
;
176 static int micro_ac_get_property(struct power_supply
*b
,
177 enum power_supply_property psp
,
178 union power_supply_propval
*val
)
180 struct micro_battery
*mb
= dev_get_drvdata(b
->dev
.parent
);
183 case POWER_SUPPLY_PROP_ONLINE
:
184 val
->intval
= mb
->ac
;
193 static enum power_supply_property micro_batt_power_props
[] = {
194 POWER_SUPPLY_PROP_TECHNOLOGY
,
195 POWER_SUPPLY_PROP_STATUS
,
196 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
197 POWER_SUPPLY_PROP_CAPACITY
,
198 POWER_SUPPLY_PROP_TEMP
,
199 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
202 static const struct power_supply_desc micro_batt_power_desc
= {
203 .name
= "main-battery",
204 .type
= POWER_SUPPLY_TYPE_BATTERY
,
205 .properties
= micro_batt_power_props
,
206 .num_properties
= ARRAY_SIZE(micro_batt_power_props
),
207 .get_property
= micro_batt_get_property
,
211 static enum power_supply_property micro_ac_power_props
[] = {
212 POWER_SUPPLY_PROP_ONLINE
,
215 static const struct power_supply_desc micro_ac_power_desc
= {
217 .type
= POWER_SUPPLY_TYPE_MAINS
,
218 .properties
= micro_ac_power_props
,
219 .num_properties
= ARRAY_SIZE(micro_ac_power_props
),
220 .get_property
= micro_ac_get_property
,
223 static struct power_supply
*micro_batt_power
, *micro_ac_power
;
225 static int micro_batt_probe(struct platform_device
*pdev
)
227 struct micro_battery
*mb
;
230 mb
= devm_kzalloc(&pdev
->dev
, sizeof(*mb
), GFP_KERNEL
);
234 mb
->micro
= dev_get_drvdata(pdev
->dev
.parent
);
235 mb
->wq
= alloc_workqueue("ipaq-battery-wq", WQ_MEM_RECLAIM
, 0);
239 INIT_DELAYED_WORK(&mb
->update
, micro_battery_work
);
240 platform_set_drvdata(pdev
, mb
);
241 queue_delayed_work(mb
->wq
, &mb
->update
, 1);
243 micro_batt_power
= power_supply_register(&pdev
->dev
,
244 µ_batt_power_desc
, NULL
);
245 if (IS_ERR(micro_batt_power
)) {
246 ret
= PTR_ERR(micro_batt_power
);
250 micro_ac_power
= power_supply_register(&pdev
->dev
,
251 µ_ac_power_desc
, NULL
);
252 if (IS_ERR(micro_ac_power
)) {
253 ret
= PTR_ERR(micro_ac_power
);
257 dev_info(&pdev
->dev
, "iPAQ micro battery driver\n");
261 power_supply_unregister(micro_batt_power
);
263 cancel_delayed_work_sync(&mb
->update
);
264 destroy_workqueue(mb
->wq
);
268 static int micro_batt_remove(struct platform_device
*pdev
)
271 struct micro_battery
*mb
= platform_get_drvdata(pdev
);
273 power_supply_unregister(micro_ac_power
);
274 power_supply_unregister(micro_batt_power
);
275 cancel_delayed_work_sync(&mb
->update
);
276 destroy_workqueue(mb
->wq
);
281 static int __maybe_unused
micro_batt_suspend(struct device
*dev
)
283 struct micro_battery
*mb
= dev_get_drvdata(dev
);
285 cancel_delayed_work_sync(&mb
->update
);
289 static int __maybe_unused
micro_batt_resume(struct device
*dev
)
291 struct micro_battery
*mb
= dev_get_drvdata(dev
);
293 queue_delayed_work(mb
->wq
, &mb
->update
, msecs_to_jiffies(BATT_PERIOD
));
297 static const struct dev_pm_ops micro_batt_dev_pm_ops
= {
298 SET_SYSTEM_SLEEP_PM_OPS(micro_batt_suspend
, micro_batt_resume
)
301 static struct platform_driver micro_batt_device_driver
= {
303 .name
= "ipaq-micro-battery",
304 .pm
= µ_batt_dev_pm_ops
,
306 .probe
= micro_batt_probe
,
307 .remove
= micro_batt_remove
,
309 module_platform_driver(micro_batt_device_driver
);
311 MODULE_LICENSE("GPL");
312 MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery");
313 MODULE_ALIAS("platform:ipaq-micro-battery");