1 /*
2  * Support for OLPC XO-1 System Control Interrupts (SCI)
3  *
4  * Copyright (C) 2010 One Laptop per Child
5  * Copyright (C) 2006 Red Hat, Inc.
6  * Copyright (C) 2006 Advanced Micro Devices, Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <linux/cs5535.h>
15 #include <linux/device.h>
16 #include <linux/gpio.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm.h>
21 #include <linux/pm_wakeup.h>
22 #include <linux/mfd/core.h>
23 #include <linux/power_supply.h>
24 #include <linux/suspend.h>
25 #include <linux/workqueue.h>
26 
27 #include <asm/io.h>
28 #include <asm/msr.h>
29 #include <asm/olpc.h>
30 
31 #define DRV_NAME	"olpc-xo1-sci"
32 #define PFX		DRV_NAME ": "
33 
34 static unsigned long acpi_base;
35 static struct input_dev *power_button_idev;
36 static struct input_dev *ebook_switch_idev;
37 static struct input_dev *lid_switch_idev;
38 
39 static int sci_irq;
40 
41 static bool lid_open;
42 static bool lid_inverted;
43 static int lid_wake_mode;
44 
45 enum lid_wake_modes {
46 	LID_WAKE_ALWAYS,
47 	LID_WAKE_OPEN,
48 	LID_WAKE_CLOSE,
49 };
50 
51 static const char * const lid_wake_mode_names[] = {
52 	[LID_WAKE_ALWAYS] = "always",
53 	[LID_WAKE_OPEN] = "open",
54 	[LID_WAKE_CLOSE] = "close",
55 };
56 
57 static void battery_status_changed(void)
58 {
59 	struct power_supply *psy = power_supply_get_by_name("olpc-battery");
60 
61 	if (psy) {
62 		power_supply_changed(psy);
63 		put_device(psy->dev);
64 	}
65 }
66 
67 static void ac_status_changed(void)
68 {
69 	struct power_supply *psy = power_supply_get_by_name("olpc-ac");
70 
71 	if (psy) {
72 		power_supply_changed(psy);
73 		put_device(psy->dev);
74 	}
75 }
76 
77 /* Report current ebook switch state through input layer */
78 static void send_ebook_state(void)
79 {
80 	unsigned char state;
81 
82 	if (olpc_ec_cmd(EC_READ_EB_MODE, NULL, 0, &state, 1)) {
83 		pr_err(PFX "failed to get ebook state\n");
84 		return;
85 	}
86 
87 	if (!!test_bit(SW_TABLET_MODE, ebook_switch_idev->sw) == state)
88 		return; /* Nothing new to report. */
89 
90 	input_report_switch(ebook_switch_idev, SW_TABLET_MODE, state);
91 	input_sync(ebook_switch_idev);
92 	pm_wakeup_event(&ebook_switch_idev->dev, 0);
93 }
94 
95 static void flip_lid_inverter(void)
96 {
97 	/* gpio is high; invert so we'll get l->h event interrupt */
98 	if (lid_inverted)
99 		cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
100 	else
101 		cs5535_gpio_set(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
102 	lid_inverted = !lid_inverted;
103 }
104 
105 static void detect_lid_state(void)
106 {
107 	/*
108 	 * the edge detector hookup on the gpio inputs on the geode is
109 	 * odd, to say the least.  See http://dev.laptop.org/ticket/5703
110 	 * for details, but in a nutshell:  we don't use the edge
111 	 * detectors.  instead, we make use of an anomoly:  with the both
112 	 * edge detectors turned off, we still get an edge event on a
113 	 * positive edge transition.  to take advantage of this, we use the
114 	 * front-end inverter to ensure that that's the edge we're always
115 	 * going to see next.
116 	 */
117 
118 	int state;
119 
120 	state = cs5535_gpio_isset(OLPC_GPIO_LID, GPIO_READ_BACK);
121 	lid_open = !state ^ !lid_inverted; /* x ^^ y */
122 	if (!state)
123 		return;
124 
125 	flip_lid_inverter();
126 }
127 
128 /* Report current lid switch state through input layer */
129 static void send_lid_state(void)
130 {
131 	if (!!test_bit(SW_LID, lid_switch_idev->sw) == !lid_open)
132 		return; /* Nothing new to report. */
133 
134 	input_report_switch(lid_switch_idev, SW_LID, !lid_open);
135 	input_sync(lid_switch_idev);
136 	pm_wakeup_event(&lid_switch_idev->dev, 0);
137 }
138 
139 static ssize_t lid_wake_mode_show(struct device *dev,
140 				  struct device_attribute *attr, char *buf)
141 {
142 	const char *mode = lid_wake_mode_names[lid_wake_mode];
143 	return sprintf(buf, "%s\n", mode);
144 }
145 static ssize_t lid_wake_mode_set(struct device *dev,
146 				 struct device_attribute *attr,
147 				 const char *buf, size_t count)
148 {
149 	int i;
150 	for (i = 0; i < ARRAY_SIZE(lid_wake_mode_names); i++) {
151 		const char *mode = lid_wake_mode_names[i];
152 		if (strlen(mode) != count || strncasecmp(mode, buf, count))
153 			continue;
154 
155 		lid_wake_mode = i;
156 		return count;
157 	}
158 	return -EINVAL;
159 }
160 static DEVICE_ATTR(lid_wake_mode, S_IWUSR | S_IRUGO, lid_wake_mode_show,
161 		   lid_wake_mode_set);
162 
163 /*
164  * Process all items in the EC's SCI queue.
165  *
166  * This is handled in a workqueue because olpc_ec_cmd can be slow (and
167  * can even timeout).
168  *
169  * If propagate_events is false, the queue is drained without events being
170  * generated for the interrupts.
171  */
172 static void process_sci_queue(bool propagate_events)
173 {
174 	int r;
175 	u16 data;
176 
177 	do {
178 		r = olpc_ec_sci_query(&data);
179 		if (r || !data)
180 			break;
181 
182 		pr_debug(PFX "SCI 0x%x received\n", data);
183 
184 		switch (data) {
185 		case EC_SCI_SRC_BATERR:
186 		case EC_SCI_SRC_BATSOC:
187 		case EC_SCI_SRC_BATTERY:
188 		case EC_SCI_SRC_BATCRIT:
189 			battery_status_changed();
190 			break;
191 		case EC_SCI_SRC_ACPWR:
192 			ac_status_changed();
193 			break;
194 		}
195 
196 		if (data == EC_SCI_SRC_EBOOK && propagate_events)
197 			send_ebook_state();
198 	} while (data);
199 
200 	if (r)
201 		pr_err(PFX "Failed to clear SCI queue");
202 }
203 
204 static void process_sci_queue_work(struct work_struct *work)
205 {
206 	process_sci_queue(true);
207 }
208 
209 static DECLARE_WORK(sci_work, process_sci_queue_work);
210 
211 static irqreturn_t xo1_sci_intr(int irq, void *dev_id)
212 {
213 	struct platform_device *pdev = dev_id;
214 	u32 sts;
215 	u32 gpe;
216 
217 	sts = inl(acpi_base + CS5536_PM1_STS);
218 	outl(sts | 0xffff, acpi_base + CS5536_PM1_STS);
219 
220 	gpe = inl(acpi_base + CS5536_PM_GPE0_STS);
221 	outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
222 
223 	dev_dbg(&pdev->dev, "sts %x gpe %x\n", sts, gpe);
224 
225 	if (sts & CS5536_PWRBTN_FLAG) {
226 		if (!(sts & CS5536_WAK_FLAG)) {
227 			/* Only report power button input when it was pressed
228 			 * during regular operation (as opposed to when it
229 			 * was used to wake the system). */
230 			input_report_key(power_button_idev, KEY_POWER, 1);
231 			input_sync(power_button_idev);
232 			input_report_key(power_button_idev, KEY_POWER, 0);
233 			input_sync(power_button_idev);
234 		}
235 		/* Report the wakeup event in all cases. */
236 		pm_wakeup_event(&power_button_idev->dev, 0);
237 	}
238 
239 	if (gpe & CS5536_GPIOM7_PME_FLAG) { /* EC GPIO */
240 		cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
241 		schedule_work(&sci_work);
242 	}
243 
244 	cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
245 	cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
246 	detect_lid_state();
247 	send_lid_state();
248 
249 	return IRQ_HANDLED;
250 }
251 
252 static int xo1_sci_suspend(struct platform_device *pdev, pm_message_t state)
253 {
254 	if (device_may_wakeup(&power_button_idev->dev))
255 		olpc_xo1_pm_wakeup_set(CS5536_PM_PWRBTN);
256 	else
257 		olpc_xo1_pm_wakeup_clear(CS5536_PM_PWRBTN);
258 
259 	if (device_may_wakeup(&ebook_switch_idev->dev))
260 		olpc_ec_wakeup_set(EC_SCI_SRC_EBOOK);
261 	else
262 		olpc_ec_wakeup_clear(EC_SCI_SRC_EBOOK);
263 
264 	if (!device_may_wakeup(&lid_switch_idev->dev)) {
265 		cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
266 	} else if ((lid_open && lid_wake_mode == LID_WAKE_OPEN) ||
267 		   (!lid_open && lid_wake_mode == LID_WAKE_CLOSE)) {
268 		flip_lid_inverter();
269 
270 		/* we may have just caused an event */
271 		cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
272 		cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
273 
274 		cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
275 	}
276 
277 	return 0;
278 }
279 
280 static int xo1_sci_resume(struct platform_device *pdev)
281 {
282 	/*
283 	 * We don't know what may have happened while we were asleep.
284 	 * Reestablish our lid setup so we're sure to catch all transitions.
285 	 */
286 	detect_lid_state();
287 	send_lid_state();
288 	cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
289 
290 	/* Enable all EC events */
291 	olpc_ec_mask_write(EC_SCI_SRC_ALL);
292 
293 	/* Power/battery status might have changed too */
294 	battery_status_changed();
295 	ac_status_changed();
296 	return 0;
297 }
298 
299 static int __devinit setup_sci_interrupt(struct platform_device *pdev)
300 {
301 	u32 lo, hi;
302 	u32 sts;
303 	int r;
304 
305 	rdmsr(0x51400020, lo, hi);
306 	sci_irq = (lo >> 20) & 15;
307 
308 	if (sci_irq) {
309 		dev_info(&pdev->dev, "SCI is mapped to IRQ %d\n", sci_irq);
310 	} else {
311 		/* Zero means masked */
312 		dev_info(&pdev->dev, "SCI unmapped. Mapping to IRQ 3\n");
313 		sci_irq = 3;
314 		lo |= 0x00300000;
315 		wrmsrl(0x51400020, lo);
316 	}
317 
318 	/* Select level triggered in PIC */
319 	if (sci_irq < 8) {
320 		lo = inb(CS5536_PIC_INT_SEL1);
321 		lo |= 1 << sci_irq;
322 		outb(lo, CS5536_PIC_INT_SEL1);
323 	} else {
324 		lo = inb(CS5536_PIC_INT_SEL2);
325 		lo |= 1 << (sci_irq - 8);
326 		outb(lo, CS5536_PIC_INT_SEL2);
327 	}
328 
329 	/* Enable SCI from power button, and clear pending interrupts */
330 	sts = inl(acpi_base + CS5536_PM1_STS);
331 	outl((CS5536_PM_PWRBTN << 16) | 0xffff, acpi_base + CS5536_PM1_STS);
332 
333 	r = request_irq(sci_irq, xo1_sci_intr, 0, DRV_NAME, pdev);
334 	if (r)
335 		dev_err(&pdev->dev, "can't request interrupt\n");
336 
337 	return r;
338 }
339 
340 static int __devinit setup_ec_sci(void)
341 {
342 	int r;
343 
344 	r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");
345 	if (r)
346 		return r;
347 
348 	gpio_direction_input(OLPC_GPIO_ECSCI);
349 
350 	/* Clear pending EC SCI events */
351 	cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_NEGATIVE_EDGE_STS);
352 	cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_POSITIVE_EDGE_STS);
353 
354 	/*
355 	 * Enable EC SCI events, and map them to both a PME and the SCI
356 	 * interrupt.
357 	 *
358 	 * Ordinarily, in addition to functioning as GPIOs, Geode GPIOs can
359 	 * be mapped to regular interrupts *or* Geode-specific Power
360 	 * Management Events (PMEs) - events that bring the system out of
361 	 * suspend. In this case, we want both of those things - the system
362 	 * wakeup, *and* the ability to get an interrupt when an event occurs.
363 	 *
364 	 * To achieve this, we map the GPIO to a PME, and then we use one
365 	 * of the many generic knobs on the CS5535 PIC to additionally map the
366 	 * PME to the regular SCI interrupt line.
367 	 */
368 	cs5535_gpio_set(OLPC_GPIO_ECSCI, GPIO_EVENTS_ENABLE);
369 
370 	/* Set the SCI to cause a PME event on group 7 */
371 	cs5535_gpio_setup_event(OLPC_GPIO_ECSCI, 7, 1);
372 
373 	/* And have group 7 also fire the SCI interrupt */
374 	cs5535_pic_unreqz_select_high(7, sci_irq);
375 
376 	return 0;
377 }
378 
379 static void free_ec_sci(void)
380 {
381 	gpio_free(OLPC_GPIO_ECSCI);
382 }
383 
384 static int __devinit setup_lid_events(void)
385 {
386 	int r;
387 
388 	r = gpio_request(OLPC_GPIO_LID, "OLPC-LID");
389 	if (r)
390 		return r;
391 
392 	gpio_direction_input(OLPC_GPIO_LID);
393 
394 	cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_INPUT_INVERT);
395 	lid_inverted = 0;
396 
397 	/* Clear edge detection and event enable for now */
398 	cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
399 	cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_EN);
400 	cs5535_gpio_clear(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_EN);
401 	cs5535_gpio_set(OLPC_GPIO_LID, GPIO_NEGATIVE_EDGE_STS);
402 	cs5535_gpio_set(OLPC_GPIO_LID, GPIO_POSITIVE_EDGE_STS);
403 
404 	/* Set the LID to cause an PME event on group 6 */
405 	cs5535_gpio_setup_event(OLPC_GPIO_LID, 6, 1);
406 
407 	/* Set PME group 6 to fire the SCI interrupt */
408 	cs5535_gpio_set_irq(6, sci_irq);
409 
410 	/* Enable the event */
411 	cs5535_gpio_set(OLPC_GPIO_LID, GPIO_EVENTS_ENABLE);
412 
413 	return 0;
414 }
415 
416 static void free_lid_events(void)
417 {
418 	gpio_free(OLPC_GPIO_LID);
419 }
420 
421 static int __devinit setup_power_button(struct platform_device *pdev)
422 {
423 	int r;
424 
425 	power_button_idev = input_allocate_device();
426 	if (!power_button_idev)
427 		return -ENOMEM;
428 
429 	power_button_idev->name = "Power Button";
430 	power_button_idev->phys = DRV_NAME "/input0";
431 	set_bit(EV_KEY, power_button_idev->evbit);
432 	set_bit(KEY_POWER, power_button_idev->keybit);
433 
434 	power_button_idev->dev.parent = &pdev->dev;
435 	device_init_wakeup(&power_button_idev->dev, 1);
436 
437 	r = input_register_device(power_button_idev);
438 	if (r) {
439 		dev_err(&pdev->dev, "failed to register power button: %d\n", r);
440 		input_free_device(power_button_idev);
441 	}
442 
443 	return r;
444 }
445 
446 static void free_power_button(void)
447 {
448 	input_unregister_device(power_button_idev);
449 	input_free_device(power_button_idev);
450 }
451 
452 static int __devinit setup_ebook_switch(struct platform_device *pdev)
453 {
454 	int r;
455 
456 	ebook_switch_idev = input_allocate_device();
457 	if (!ebook_switch_idev)
458 		return -ENOMEM;
459 
460 	ebook_switch_idev->name = "EBook Switch";
461 	ebook_switch_idev->phys = DRV_NAME "/input1";
462 	set_bit(EV_SW, ebook_switch_idev->evbit);
463 	set_bit(SW_TABLET_MODE, ebook_switch_idev->swbit);
464 
465 	ebook_switch_idev->dev.parent = &pdev->dev;
466 	device_set_wakeup_capable(&ebook_switch_idev->dev, true);
467 
468 	r = input_register_device(ebook_switch_idev);
469 	if (r) {
470 		dev_err(&pdev->dev, "failed to register ebook switch: %d\n", r);
471 		input_free_device(ebook_switch_idev);
472 	}
473 
474 	return r;
475 }
476 
477 static void free_ebook_switch(void)
478 {
479 	input_unregister_device(ebook_switch_idev);
480 	input_free_device(ebook_switch_idev);
481 }
482 
483 static int __devinit setup_lid_switch(struct platform_device *pdev)
484 {
485 	int r;
486 
487 	lid_switch_idev = input_allocate_device();
488 	if (!lid_switch_idev)
489 		return -ENOMEM;
490 
491 	lid_switch_idev->name = "Lid Switch";
492 	lid_switch_idev->phys = DRV_NAME "/input2";
493 	set_bit(EV_SW, lid_switch_idev->evbit);
494 	set_bit(SW_LID, lid_switch_idev->swbit);
495 
496 	lid_switch_idev->dev.parent = &pdev->dev;
497 	device_set_wakeup_capable(&lid_switch_idev->dev, true);
498 
499 	r = input_register_device(lid_switch_idev);
500 	if (r) {
501 		dev_err(&pdev->dev, "failed to register lid switch: %d\n", r);
502 		goto err_register;
503 	}
504 
505 	r = device_create_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
506 	if (r) {
507 		dev_err(&pdev->dev, "failed to create wake mode attr: %d\n", r);
508 		goto err_create_attr;
509 	}
510 
511 	return 0;
512 
513 err_create_attr:
514 	input_unregister_device(lid_switch_idev);
515 err_register:
516 	input_free_device(lid_switch_idev);
517 	return r;
518 }
519 
520 static void free_lid_switch(void)
521 {
522 	device_remove_file(&lid_switch_idev->dev, &dev_attr_lid_wake_mode);
523 	input_unregister_device(lid_switch_idev);
524 	input_free_device(lid_switch_idev);
525 }
526 
527 static int __devinit xo1_sci_probe(struct platform_device *pdev)
528 {
529 	struct resource *res;
530 	int r;
531 
532 	/* don't run on non-XOs */
533 	if (!machine_is_olpc())
534 		return -ENODEV;
535 
536 	r = mfd_cell_enable(pdev);
537 	if (r)
538 		return r;
539 
540 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
541 	if (!res) {
542 		dev_err(&pdev->dev, "can't fetch device resource info\n");
543 		return -EIO;
544 	}
545 	acpi_base = res->start;
546 
547 	r = setup_power_button(pdev);
548 	if (r)
549 		return r;
550 
551 	r = setup_ebook_switch(pdev);
552 	if (r)
553 		goto err_ebook;
554 
555 	r = setup_lid_switch(pdev);
556 	if (r)
557 		goto err_lid;
558 
559 	r = setup_lid_events();
560 	if (r)
561 		goto err_lidevt;
562 
563 	r = setup_ec_sci();
564 	if (r)
565 		goto err_ecsci;
566 
567 	/* Enable PME generation for EC-generated events */
568 	outl(CS5536_GPIOM6_PME_EN | CS5536_GPIOM7_PME_EN,
569 		acpi_base + CS5536_PM_GPE0_EN);
570 
571 	/* Clear pending events */
572 	outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
573 	process_sci_queue(false);
574 
575 	/* Initial sync */
576 	send_ebook_state();
577 	detect_lid_state();
578 	send_lid_state();
579 
580 	r = setup_sci_interrupt(pdev);
581 	if (r)
582 		goto err_sci;
583 
584 	/* Enable all EC events */
585 	olpc_ec_mask_write(EC_SCI_SRC_ALL);
586 
587 	return r;
588 
589 err_sci:
590 	free_ec_sci();
591 err_ecsci:
592 	free_lid_events();
593 err_lidevt:
594 	free_lid_switch();
595 err_lid:
596 	free_ebook_switch();
597 err_ebook:
598 	free_power_button();
599 	return r;
600 }
601 
602 static int __devexit xo1_sci_remove(struct platform_device *pdev)
603 {
604 	mfd_cell_disable(pdev);
605 	free_irq(sci_irq, pdev);
606 	cancel_work_sync(&sci_work);
607 	free_ec_sci();
608 	free_lid_events();
609 	free_lid_switch();
610 	free_ebook_switch();
611 	free_power_button();
612 	acpi_base = 0;
613 	return 0;
614 }
615 
616 static struct platform_driver xo1_sci_driver = {
617 	.driver = {
618 		.name = "olpc-xo1-sci-acpi",
619 	},
620 	.probe = xo1_sci_probe,
621 	.remove = __devexit_p(xo1_sci_remove),
622 	.suspend = xo1_sci_suspend,
623 	.resume = xo1_sci_resume,
624 };
625 
626 static int __init xo1_sci_init(void)
627 {
628 	return platform_driver_register(&xo1_sci_driver);
629 }
630 arch_initcall(xo1_sci_init);
631