aboutsummaryrefslogtreecommitdiff
path: root/drivers/power/ipaq_micro_battery.c
blob: 54632eabf420c93d6eb7f979f0ac8b839e13b996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * h3xxx atmel micro companion support, battery subdevice
 * based on previous kernel 2.4 version
 * Author : Alessandro Gardich <gremlin@gremlin.it>
 * Author : Linus Walleij <linus.walleij@linaro.org>
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/mfd/ipaq-micro.h>
#include <linux/power_supply.h>
#include <linux/workqueue.h>

#define BATT_PERIOD 100000 /* 100 seconds in milliseconds */

#define MICRO_BATT_CHEM_ALKALINE	0x01
#define MICRO_BATT_CHEM_NICD		0x02
#define MICRO_BATT_CHEM_NIMH		0x03
#define MICRO_BATT_CHEM_LION		0x04
#define MICRO_BATT_CHEM_LIPOLY		0x05
#define MICRO_BATT_CHEM_NOT_INSTALLED	0x06
#define MICRO_BATT_CHEM_UNKNOWN		0xff

#define MICRO_BATT_STATUS_HIGH		0x01
#define MICRO_BATT_STATUS_LOW		0x02
#define MICRO_BATT_STATUS_CRITICAL	0x04
#define MICRO_BATT_STATUS_CHARGING	0x08
#define MICRO_BATT_STATUS_CHARGEMAIN	0x10
#define MICRO_BATT_STATUS_DEAD		0x20 /* Battery will not charge */
#define MICRO_BATT_STATUS_NOTINSTALLED	0x20 /* For expansion pack batteries */
#define MICRO_BATT_STATUS_FULL		0x40 /* Battery fully charged */
#define MICRO_BATT_STATUS_NOBATTERY	0x80
#define MICRO_BATT_STATUS_UNKNOWN	0xff

struct micro_battery {
	struct ipaq_micro *micro;
	struct workqueue_struct *wq;
	struct delayed_work update;
	u8 ac;
	u8 chemistry;
	unsigned int voltage;
	u16 temperature;
	u8 flag;
};

static void micro_battery_work(struct work_struct *work)
{
	struct micro_battery *mb = container_of(work,
				struct micro_battery, update.work);
	struct ipaq_micro_msg msg_battery = {
		.id = MSG_BATTERY,
	};
	struct ipaq_micro_msg msg_sensor = {
		.id = MSG_THERMAL_SENSOR,
	};

	/* First send battery message */
	ipaq_micro_tx_msg_sync(mb->micro, &msg_battery);
	if (msg_battery.rx_len < 4)
		pr_info("ERROR");

	/*
	 * Returned message format:
	 * byte 0:   0x00 = Not plugged in
	 *           0x01 = AC adapter plugged in
	 * byte 1:   chemistry
	 * byte 2:   voltage LSB
	 * byte 3:   voltage MSB
	 * byte 4:   flags
	 * byte 5-9: same for battery 2
	 */
	mb->ac = msg_battery.rx_data[0];
	mb->chemistry = msg_battery.rx_data[1];
	mb->voltage = ((((unsigned short)msg_battery.rx_data[3] << 8) +
			msg_battery.rx_data[2]) * 5000L) * 1000 / 1024;
	mb->flag = msg_battery.rx_data[4];

	if (msg_battery.rx_len == 9)
		pr_debug("second battery ignored\n");

	/* Then read the sensor */
	ipaq_micro_tx_msg_sync(mb->micro, &msg_sensor);
	mb->temperature = msg_sensor.rx_data[1] << 8 | msg_sensor.rx_data[0];

	queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD));
}

static int get_capacity(struct power_supply *b)
{
	struct micro_battery *mb = dev_get_drvdata(b->dev->parent);

	switch (mb->flag & 0x07) {
	case MICRO_BATT_STATUS_HIGH:
		return 100;
		break;
	case MICRO_BATT_STATUS_LOW:
		return 50;
		break;
	case MICRO_BATT_STATUS_CRITICAL:
		return 5;
		break;
	default:
		break;
	}
	return 0;
}

static int get_status(struct power_supply *b)
{
	struct micro_battery *mb = dev_get_drvdata(b->dev->parent);

	if (mb->flag == MICRO_BATT_STATUS_UNKNOWN)
		return POWER_SUPPLY_STATUS_UNKNOWN;

	if (mb->flag & MICRO_BATT_STATUS_FULL)
		return POWER_SUPPLY_STATUS_FULL;

	if ((mb->flag & MICRO_BATT_STATUS_CHARGING) ||
		(mb->flag & MICRO_BATT_STATUS_CHARGEMAIN))
		return POWER_SUPPLY_STATUS_CHARGING;

	return POWER_SUPPLY_STATUS_DISCHARGING;
}

static int micro_batt_get_property(struct power_supply *b,
					enum power_supply_property psp,
					union power_supply_propval *val)
{
	struct micro_battery *mb = dev_get_drvdata(b->dev->parent);

	switch (psp) {
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		switch (mb->chemistry) {
		case MICRO_BATT_CHEM_NICD:
			val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
			break;
		case MICRO_BATT_CHEM_NIMH:
			val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
			break;
		case MICRO_BATT_CHEM_LION:
			val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
			break;
		case MICRO_BATT_CHEM_LIPOLY:
			val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
			break;
		default:
			val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
			break;
		};
		break;
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = get_status(b);
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
		val->intval = 4700000;
		break;
	case POWER_SUPPLY_PROP_CAPACITY:
		val->intval = get_capacity(b);
		break;
	case POWER_SUPPLY_PROP_TEMP:
		val->intval = mb->temperature;
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		val->intval = mb->voltage;
		break;
	default:
		return -EINVAL;
	};

	return 0;
}

static int micro_ac_get_property(struct power_supply *b,
				 enum power_supply_property psp,
				 union power_supply_propval *val)
{
	struct micro_battery *mb = dev_get_drvdata(b->dev->parent);

	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = mb->ac;
		break;
	default:
		return -EINVAL;
	};

	return 0;
}

static enum power_supply_property micro_batt_power_props[] = {
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
};

static struct power_supply micro_batt_power = {
	.name			= "main-battery",
	.type			= POWER_SUPPLY_TYPE_BATTERY,
	.properties		= micro_batt_power_props,
	.num_properties		= ARRAY_SIZE(micro_batt_power_props),
	.get_property		= micro_batt_get_property,
	.use_for_apm		= 1,
};

static enum power_supply_property micro_ac_power_props[] = {
	POWER_SUPPLY_PROP_ONLINE,
};

static struct power_supply micro_ac_power = {
	.name			= "ac",
	.type			= POWER_SUPPLY_TYPE_MAINS,
	.properties		= micro_ac_power_props,
	.num_properties		= ARRAY_SIZE(micro_ac_power_props),
	.get_property		= micro_ac_get_property,
};

static int micro_batt_probe(struct platform_device *pdev)
{
	struct micro_battery *mb;

	mb = devm_kzalloc(&pdev->dev, sizeof(*mb), GFP_KERNEL);
	if (!mb)
		return -ENOMEM;

	mb->micro = dev_get_drvdata(pdev->dev.parent);
	mb->wq = create_singlethread_workqueue("ipaq-battery-wq");
	INIT_DELAYED_WORK(&mb->update, micro_battery_work);
	platform_set_drvdata(pdev, mb);
	queue_delayed_work(mb->wq, &mb->update, 1);
	power_supply_register(&pdev->dev, &micro_batt_power);
	power_supply_register(&pdev->dev, &micro_ac_power);

	dev_info(&pdev->dev, "iPAQ micro battery driver\n");
	return 0;
}

static int micro_batt_remove(struct platform_device *pdev)

{
	struct micro_battery *mb = platform_get_drvdata(pdev);

	power_supply_unregister(&micro_ac_power);
	power_supply_unregister(&micro_batt_power);
	cancel_delayed_work_sync(&mb->update);

	return 0;
}

static int micro_batt_suspend(struct device *dev)
{
	struct micro_battery *mb = dev_get_drvdata(dev);

	cancel_delayed_work_sync(&mb->update);
	return 0;
}

static int micro_batt_resume(struct device *dev)
{
	struct micro_battery *mb = dev_get_drvdata(dev);

	queue_delayed_work(mb->wq, &mb->update, msecs_to_jiffies(BATT_PERIOD));
	return 0;
}

static const struct dev_pm_ops micro_batt_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(micro_batt_suspend, micro_batt_resume)
};

struct platform_driver micro_batt_device_driver = {
	.driver		= {
		.name	= "ipaq-micro-battery",
		.pm	= &micro_batt_dev_pm_ops,
	},
	.probe		= micro_batt_probe,
	.remove		= micro_batt_remove,
};
module_platform_driver(micro_batt_device_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("driver for iPAQ Atmel micro battery");
MODULE_ALIAS("platform:battery-ipaq-micro");