summaryrefslogtreecommitdiff
path: root/arch/arm/mach-sa1100/h3xxx-sleeve.c
blob: ed0edaf43d9ccfaee8f6eaf51b439308f5279f5b (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
#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>

#define SLEEVE_POWER_DELAY_MS 10
#define SLEEVE_NVRAM_POWER_MS 5

struct opt_info {
	struct device *dev;
	struct ipaq_option_id id;
	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;
};

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 int ipaq_option_read_id(struct opt_info *opt, struct ipaq_option_id *id)
{
	__le16 vendor, device;
	int ret;

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

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

	ret = ipaq_option_spi_read(&vendor, 6, 2);
	if (ret)
		goto err;

	ret = ipaq_option_spi_read(&device, 8, 2);
	if (ret)
		goto err;

	id->vendor = le16_to_cpu(vendor);
	id->device = le16_to_cpu(device);

 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);
	gpiod_set_value(opt->opt_reset, 0);

	if (noirq)
		mdelay(SLEEVE_POWER_DELAY_MS);
	else
		msleep(SLEEVE_POWER_DELAY_MS);
}

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, "Option pack %04x:%04x detected\n",
		 opt->id.vendor, opt->id.device);

	ipaq_option_power_on(opt, false);

	if (!opt->sleeve_present)
		ipaq_option_device_add(opt->dev, opt->id);
	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);
}

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);

	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;

	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_option_id id;
	int ret;

	if (opt->sleeve_present && gpiod_get_value_cansleep(opt->detect)) {
		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 (opt->id.vendor != id.vendor ||
		    opt->id.device != id.device) {
			/* sleeve changed */
			dev_warn(opt->dev, "sleeve changed\n");
			opt->sleeve_changed = true;
		} else {
			dev_info(opt->dev, "same sleeve type inserted\n");
			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");