summaryrefslogtreecommitdiff
path: root/arch/arm/mach-sa1100/h3xxx-sleeve.c
blob: 8aef18062e4057d708736fd76ab95b809d4da893 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/mfd/ipaq-micro.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/ipaq-sleeve.h>
#include <asm/unaligned.h>

#define SLEEVE_NVRAM_POWER_MS 5

enum {
	MAX_HEADER = 256,
	TYPE_I = 1,
	TYPE_II = 2,
	NO_RESET = 0xffff,
};

struct ipaq_sleeve_id {
	struct ipaq_option_id id;
	const char *description;
	u8 version;
	u8 extended_battery;
	u16 reset_width;
	u16 reset_wait;
	u16 skt_type[2];
	u8 opt_on_ctl;
	u8 nvram[256];
};

struct opt_info {
	struct device *dev;
	struct delayed_work detect_work;
	bool detect_disabled;
	bool sleeve_changed;
	bool sleeve_present;
	bool sleeve_wakeup;
	struct gpio_desc *detect;
	struct gpio_desc *nvram_on;
	struct gpio_desc *opt_on;
	struct gpio_desc *opt_reset;

	struct ipaq_sleeve_id id;
};

static int ipaq_option_spi_read(void *buf, unsigned int addr, size_t len)
{
	struct device *mdev = bus_find_device_by_name(&platform_bus_type, NULL,
						      "ipaq-h3xxx-micro");
	struct ipaq_micro *micro;
	struct ipaq_micro_msg msg;

	memset(buf, 0, len);

	if (!mdev || !mdev->driver)
		return -EINVAL;

	micro = dev_get_drvdata(mdev);
	if (!micro)
		return -EINVAL;

	memset(&msg, 0, sizeof(msg));

	while (len) {
		size_t this_len = len;

		if (this_len > 8)
			this_len = 8;

		msg.id = MSG_SPI_READ;
		msg.tx_len = 3;
		msg.tx_data[0] = addr;
		msg.tx_data[1] = addr >> 8;
		msg.tx_data[2] = this_len;

		ipaq_micro_tx_msg_sync(micro, &msg);

		memcpy(buf, msg.rx_data, msg.rx_len);

		buf += msg.rx_len;
		len -= msg.rx_len;
		addr += msg.rx_len;
	}

	return 0;
}

static char *ipaq_option_get_string(u8 **ptr, u8 *end)
{
	u8 *p = *ptr;
	unsigned int i;

	for (i = 0; p[i] && &p[i] < end; i++)
		if (!isprint(p[i]) || &p[i] == end)
			return NULL;

	*ptr = p + i + 1;

	return kstrndup(p, i, GFP_KERNEL);
}

static u8 *ipaq_option_get_ptr(u8 **ptr, u8 *end, size_t size)
{
	u8 *p = *ptr;

	if (p - end < size)
		return NULL;

	*ptr += size;

	return p;
}

static u8 ipaq_option_get_u8(u8 **ptr, u8 *end)
{
	u8 *p = ipaq_option_get_ptr(ptr, end, 1);
	return p ? *p : 0;
}

static u16 ipaq_option_get_le16(u8 **ptr, u8 *end)
{
	u8 *p = ipaq_option_get_ptr(ptr, end, 2);
	return p ? get_unaligned_le16(p) : 0;
}

static u32 ipaq_option_get_le32(u8 **ptr, u8 *end)
{
	u8 *p = ipaq_option_get_ptr(ptr, end, 4);
	return p ? get_unaligned_le32(p) : 0;
}

static int ipaq_option_read_id(struct opt_info *opt, struct ipaq_sleeve_id *id)
{
	unsigned int length;
	u8 header[5], *ptr, *end;
	int ret;

	/* Enable the CF bus */
	gpiod_set_value_cansleep(opt->nvram_on, 1);

	/* Wait for power to stabilise */
	msleep(SLEEVE_NVRAM_POWER_MS);

	/* Read the first few bytes from the NVRAM */
	ret = ipaq_option_spi_read(header, 0, sizeof(header));
	if (ret)
		goto err;

	ptr = header;
	end = header + sizeof(header);
	if (ipaq_option_get_u8(&ptr, end) != 0xaa) {
		dev_err(opt->dev, "invalid ID structure\n");
		ret = -EINVAL;
		goto err;
	}

	length = ipaq_option_get_le32(&ptr, end);
	if (length < sizeof(header) || length >= MAX_HEADER - 1) {
		dev_err(opt->dev, "invalid ID length\n");
		ret = -EINVAL;
		goto err;
	}

	/* Read the full NVRAM contents */
	ret = ipaq_option_spi_read(id->nvram, 0, length + 1);
	if (ret)
		goto err;

	/* reset the pointer */
	ptr = id->nvram + (ptr - header);
	end = ptr + length + 1;

	id->version = ipaq_option_get_u8(&ptr, end);
	id->id.vendor = ipaq_option_get_le16(&ptr, end);
	id->id.device = ipaq_option_get_le16(&ptr, end);

	id->description = ipaq_option_get_string(&ptr, end);
	if (!id->description) {
		ret = -ENOMEM;
		goto err;
	}

	ipaq_option_get_ptr(&ptr, end, 2); /* two reserved bytes */
	id->extended_battery = ipaq_option_get_u8(&ptr, end);
	ipaq_option_get_ptr(&ptr, end, 2); /* two reserved bytes */
	id->reset_width = ipaq_option_get_le16(&ptr, end);
	id->reset_wait = ipaq_option_get_le16(&ptr, end);
	ipaq_option_get_le16(&ptr, end); /* bootstrap */
	ipaq_option_get_le16(&ptr, end); /* OEM table */

	if (id->version == TYPE_II) {
		id->skt_type[0] = ipaq_option_get_le16(&ptr, end);
		id->skt_type[1] = ipaq_option_get_le16(&ptr, end);
		id->opt_on_ctl = ipaq_option_get_u8(&ptr, end);
	} else {
		id->skt_type[0] = 0;
		id->skt_type[1] = 0;
		id->opt_on_ctl = 1;
	}

	if (ipaq_option_get_le32(&ptr, end) != 0x0f0f0f0f) {
		dev_err(opt->dev, "missing ID terminator\n");
		ret = -EINVAL;
		goto err;
	}

 err:
	gpiod_set_value_cansleep(opt->nvram_on, 0);
	return ret;
}

static void __ipaq_option_power_on(struct opt_info *opt, bool noirq)
{
	gpiod_set_value(opt->opt_reset, 1);
	gpiod_set_value(opt->opt_on, 1);

	if (opt->id.reset_wait != NO_RESET) {
		if (noirq)
			mdelay(opt->id.reset_width);
		else
			msleep(opt->id.reset_width);
	}

	gpiod_set_value(opt->opt_reset, 0);

	if (opt->id.reset_wait != NO_RESET) {
		if (noirq)
			mdelay(opt->id.reset_wait);
		else
			msleep(opt->id.reset_wait);
	}
}

static void ipaq_option_power_on(struct opt_info *opt)
{
	__ipaq_option_power_on(opt, false);
}

static void ipaq_option_power_off(struct opt_info *opt, bool noirq)
{
	gpiod_set_value(opt->opt_reset, 1);
	gpiod_set_value(opt->opt_on, 0);
}

static void ipaq_option_insert(struct opt_info *opt)
{
	int ret;

	ret = ipaq_option_read_id(opt, &opt->id);
	if (ret)
		return;

	dev_info(opt->dev, "%s sleeve inserted\n", opt->id.description);

	if (opt->id.opt_on_ctl)
		ipaq_option_power_on(opt);

	if (!opt->sleeve_present)
		ipaq_option_device_add(opt->dev, opt->id.id,
				       opt->id.description);
	opt->sleeve_present = true;
}

static void ipaq_option_remove(struct opt_info *opt)
{
	if (opt->sleeve_present)
		ipaq_option_device_del();
	opt->sleeve_present = false;

	ipaq_option_power_off(opt, false);

	kfree(opt->id.description);
	memset(&opt->id, 0, sizeof(opt->id));
}

static void ipaq_option_work(struct work_struct *work)
{
	struct opt_info *opt = container_of(work, struct opt_info, detect_work.work);
	int present = gpiod_get_value_cansleep(opt->detect);

	if (opt->sleeve_changed)
		ipaq_option_remove(opt);

	if (present)
		ipaq_option_insert(opt);
	else
		ipaq_option_remove(opt);
}

static irqreturn_t ipaq_option_irq(int irq, void *data)
{
	struct opt_info *opt = data;

	dev_info(opt->dev, "irq: option pack: %u\n", gpiod_get_value(opt->detect));

	if (!opt->detect_disabled)
		mod_delayed_work(system_wq, &opt->detect_work,
				 msecs_to_jiffies(100));

	return IRQ_HANDLED;
}

static int ipaq_h3xxx_sleeve_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct opt_info *opt;
	int irq, ret;

	opt = devm_kzalloc(dev, sizeof(*opt), GFP_KERNEL);
	if (!opt)
		return -ENOMEM;

	platform_set_drvdata(pdev, opt);
	opt->dev = dev;
	INIT_DELAYED_WORK(&opt->detect_work, ipaq_option_work);

	opt->nvram_on = devm_gpiod_get(dev, "nvram-on", GPIOD_OUT_LOW);
	if (IS_ERR(opt->nvram_on))
		return PTR_ERR(opt->nvram_on);

	opt->opt_on = devm_gpiod_get(dev, "option-on", GPIOD_OUT_LOW);
	if (IS_ERR(opt->opt_on))
		return PTR_ERR(opt->opt_on);

	opt->opt_reset = devm_gpiod_get(dev, "option-reset", GPIOD_OUT_LOW);
	if (IS_ERR(opt->opt_reset))
		return PTR_ERR(opt->opt_reset);

	opt->detect = devm_gpiod_get_optional(dev, "option-detect", GPIOD_IN);
	if (IS_ERR(opt->detect))
		return PTR_ERR(opt->detect);

	if (opt->detect) {
		irq = gpiod_to_irq(opt->detect);
		if (irq < 0)
			return irq;

		ret = devm_request_irq(dev, irq, ipaq_option_irq,
					IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
					dev_name(dev), opt);
		if (ret)
			return ret;
	}

	schedule_delayed_work(&opt->detect_work, 0);
	flush_delayed_work(&opt->detect_work);

	device_set_wakeup_capable(dev, true);
	device_set_wakeup_enable(dev, false);

	return 0;
}

static int ipaq_h3xxx_sleeve_remove(struct platform_device *pdev)
{
	struct opt_info *opt = platform_get_drvdata(pdev);

	opt->detect_disabled = true;
	cancel_delayed_work_sync(&opt->detect_work);

	ipaq_option_remove(opt);

	return 0;
}

static int ipaq_h3xxx_check_wakeup(struct device *dev, void *data)
{
	if (device_may_wakeup(dev))
		return 1;

	return device_for_each_child(dev, data, ipaq_h3xxx_check_wakeup);
}

static int ipaq_h3xxx_sleeve_suspend(struct device *dev)
{
	struct opt_info *opt = dev_get_drvdata(dev);
	int may_wakeup;

	opt->detect_disabled = true;
	cancel_delayed_work_sync(&opt->detect_work);

	/* Disable sleeve power if device isn't a wakeup source */
	may_wakeup = device_for_each_child(dev, NULL, ipaq_h3xxx_check_wakeup);

	opt->sleeve_wakeup = may_wakeup;

	if (device_may_wakeup(dev))
		enable_irq_wake(gpiod_to_irq(opt->detect));

	return 0;
}

static int ipaq_h3xxx_sleeve_suspend_noirq(struct device *dev)
{
	struct opt_info *opt = dev_get_drvdata(dev);

	if (!opt->sleeve_wakeup && opt->sleeve_present) {
		ipaq_option_power_off(opt, true);

		/*
		 * Give the power time to collapse before bringing the
		 * reset signal to logic 0.
		 */
		mdelay(1);
		gpiod_set_raw_value(opt->opt_reset, 0);
	}

	return 0;
}

static int ipaq_h3xxx_sleeve_resume_noirq(struct device *dev)
{
	struct opt_info *opt = dev_get_drvdata(dev);

	if (!opt->sleeve_wakeup && gpiod_get_value(opt->detect)) {
		/*
		 * We can't check the ID of the sleeve here, so assume that
		 * it is the same sleeve inserted, and power it up.  This
		 * avoids an eject/insert cycle on any inserted PCMCIA card.
		 */
		__ipaq_option_power_on(opt, true);
	}

	return 0;
}

static int ipaq_h3xxx_sleeve_resume(struct device *dev)
{
	struct opt_info *opt = dev_get_drvdata(dev);
	struct ipaq_sleeve_id id;
	int ret;

	if (device_may_wakeup(dev))
		disable_irq_wake(gpiod_to_irq(opt->detect));

	if (opt->sleeve_present && gpiod_get_value_cansleep(opt->detect)) {
		memset(&id, 0, sizeof(id));
		ret = ipaq_option_read_id(opt, &id);
		if (ret) {
			dev_warn(opt->dev, "unable to read sleeve id: %d\n",
				 ret);
			opt->sleeve_changed = true;
			goto finish;
		}

		if (memcmp(opt->id.nvram, id.nvram, sizeof(id.nvram))) {
			/* sleeve changed */
			dev_warn(opt->dev, "sleeve changed\n");
			opt->sleeve_changed = true;
		} else {
			opt->detect_disabled = false;
			return 0;
		}
	}

finish:
	opt->detect_disabled = false;
	schedule_delayed_work(&opt->detect_work, 0);
	flush_delayed_work(&opt->detect_work);

	return 0;
}

static struct dev_pm_ops ipaq_h3xxx_sleeve_pm = {
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(ipaq_h3xxx_sleeve_suspend_noirq,
				      ipaq_h3xxx_sleeve_resume_noirq)
	SET_SYSTEM_SLEEP_PM_OPS(ipaq_h3xxx_sleeve_suspend,
				ipaq_h3xxx_sleeve_resume)
};

static struct platform_driver ipaq_h3xxx_sleeve = {
	.driver = {
		.name = "ipaq-h3xxx-sleeve",
		.pm = &ipaq_h3xxx_sleeve_pm,
	},
	.probe = ipaq_h3xxx_sleeve_probe,
	.remove = ipaq_h3xxx_sleeve_remove,
};

module_platform_driver(ipaq_h3xxx_sleeve);

MODULE_ALIAS("platform:ipaq-h3xxx-sleeve");
MODULE_LICENSE("GPL");