| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  |  * Support for power management features of the OLPC XO-1 laptop | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  |  * Copyright (C) 2010 Andres Salomon <dilinger@queued.net> | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  |  * Copyright (C) 2010 One Laptop per Child | 
					
						
							|  |  |  |  * Copyright (C) 2006 Red Hat, Inc. | 
					
						
							|  |  |  |  * Copyright (C) 2006 Advanced Micro Devices, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:09 +01:00
										 |  |  | #include <linux/cs5535.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | #include <linux/platform_device.h>
 | 
					
						
							| 
									
										
										
										
											2011-05-26 12:22:53 -04:00
										 |  |  | #include <linux/export.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | #include <linux/pm.h>
 | 
					
						
							| 
									
										
										
										
											2011-02-17 19:07:36 -08:00
										 |  |  | #include <linux/mfd/core.h>
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:11 +01:00
										 |  |  | #include <linux/suspend.h>
 | 
					
						
							| 
									
										
										
										
											2012-07-11 01:16:29 -07:00
										 |  |  | #include <linux/olpc-ec.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <asm/io.h>
 | 
					
						
							|  |  |  | #include <asm/olpc.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | #define DRV_NAME "olpc-xo1-pm"
 | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static unsigned long acpi_base; | 
					
						
							|  |  |  | static unsigned long pms_base; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:11 +01:00
										 |  |  | static u16 wakeup_mask = CS5536_PM_PWRBTN; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct { | 
					
						
							|  |  |  | 	unsigned long address; | 
					
						
							|  |  |  | 	unsigned short segment; | 
					
						
							|  |  |  | } ofw_bios_entry = { 0xF0000 + PAGE_OFFSET, __KERNEL_CS }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Set bits in the wakeup mask */ | 
					
						
							|  |  |  | void olpc_xo1_pm_wakeup_set(u16 value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	wakeup_mask |= value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_set); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Clear bits in the wakeup mask */ | 
					
						
							|  |  |  | void olpc_xo1_pm_wakeup_clear(u16 value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	wakeup_mask &= ~value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_clear); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int xo1_power_state_enter(suspend_state_t pm_state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned long saved_sci_mask; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Only STR is supported */ | 
					
						
							|  |  |  | 	if (pm_state != PM_SUSPEND_MEM) | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Save SCI mask (this gets lost since PM1_EN is used as a mask for | 
					
						
							|  |  |  | 	 * wakeup events, which is not necessarily the same event set) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	saved_sci_mask = inl(acpi_base + CS5536_PM1_STS); | 
					
						
							|  |  |  | 	saved_sci_mask &= 0xffff0000; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Save CPU state */ | 
					
						
							|  |  |  | 	do_olpc_suspend_lowlevel(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Resume path starts here */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Restore SCI mask (using dword access to CS5536_PM1_EN) */ | 
					
						
							|  |  |  | 	outl(saved_sci_mask, acpi_base + CS5536_PM1_STS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 00:44:37 +02:00
										 |  |  | asmlinkage __visible int xo1_do_sleep(u8 sleep_state) | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:11 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	void *pgd_addr = __va(read_cr3()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Program wakeup mask (using dword access to CS5536_PM1_EN) */ | 
					
						
							|  |  |  | 	outl(wakeup_mask << 16, acpi_base + CS5536_PM1_STS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	__asm__("movl %0,%%eax" : : "r" (pgd_addr)); | 
					
						
							|  |  |  | 	__asm__("call *(%%edi); cld" | 
					
						
							|  |  |  | 		: : "D" (&ofw_bios_entry)); | 
					
						
							|  |  |  | 	__asm__("movb $0x34, %al\n\t" | 
					
						
							|  |  |  | 		"outb %al, $0x70\n\t" | 
					
						
							|  |  |  | 		"movb $0x30, %al\n\t" | 
					
						
							|  |  |  | 		"outb %al, $0x71\n\t"); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | static void xo1_power_off(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	printk(KERN_INFO "OLPC XO-1 power off sequence...\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Enable all of these controls with 0 delay */ | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:09 +01:00
										 |  |  | 	outl(0x40000000, pms_base + CS5536_PM_SCLK); | 
					
						
							|  |  |  | 	outl(0x40000000, pms_base + CS5536_PM_IN_SLPCTL); | 
					
						
							|  |  |  | 	outl(0x40000000, pms_base + CS5536_PM_WKXD); | 
					
						
							|  |  |  | 	outl(0x40000000, pms_base + CS5536_PM_WKD); | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Clear status bits (possibly unnecessary) */ | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:09 +01:00
										 |  |  | 	outl(0x0002ffff, pms_base  + CS5536_PM_SSC); | 
					
						
							|  |  |  | 	outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS); | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Write SLP_EN bit to start the machinery */ | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:09 +01:00
										 |  |  | 	outl(0x00002000, acpi_base + CS5536_PM1_CNT); | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:11 +01:00
										 |  |  | static int xo1_power_state_valid(suspend_state_t pm_state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* suspend-to-RAM only */ | 
					
						
							|  |  |  | 	return pm_state == PM_SUSPEND_MEM; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct platform_suspend_ops xo1_suspend_ops = { | 
					
						
							|  |  |  | 	.valid = xo1_power_state_valid, | 
					
						
							|  |  |  | 	.enter = xo1_power_state_enter, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 14:02:53 -08:00
										 |  |  | static int xo1_pm_probe(struct platform_device *pdev) | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	struct resource *res; | 
					
						
							| 
									
										
										
										
											2011-02-17 19:07:36 -08:00
										 |  |  | 	int err; | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	/* don't run on non-XOs */ | 
					
						
							|  |  |  | 	if (!machine_is_olpc()) | 
					
						
							|  |  |  | 		return -ENODEV; | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-03 09:51:58 -08:00
										 |  |  | 	err = mfd_cell_enable(pdev); | 
					
						
							| 
									
										
										
										
											2011-02-17 19:07:36 -08:00
										 |  |  | 	if (err) | 
					
						
							|  |  |  | 		return err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	res = platform_get_resource(pdev, IORESOURCE_IO, 0); | 
					
						
							|  |  |  | 	if (!res) { | 
					
						
							|  |  |  | 		dev_err(&pdev->dev, "can't fetch device resource info\n"); | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-03-22 13:50:39 -07:00
										 |  |  | 	if (strcmp(pdev->name, "cs5535-pms") == 0) | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 		pms_base = res->start; | 
					
						
							| 
									
										
										
										
											2011-03-22 13:50:39 -07:00
										 |  |  | 	else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0) | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 		acpi_base = res->start; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* If we have both addresses, we can override the poweroff hook */ | 
					
						
							|  |  |  | 	if (pms_base && acpi_base) { | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:11 +01:00
										 |  |  | 		suspend_set_ops(&xo1_suspend_ops); | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 		pm_power_off = xo1_power_off; | 
					
						
							|  |  |  | 		printk(KERN_INFO "OLPC XO-1 support registered\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 14:02:53 -08:00
										 |  |  | static int xo1_pm_remove(struct platform_device *pdev) | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-03 09:51:58 -08:00
										 |  |  | 	mfd_cell_disable(pdev); | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-22 13:50:39 -07:00
										 |  |  | 	if (strcmp(pdev->name, "cs5535-pms") == 0) | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 		pms_base = 0; | 
					
						
							| 
									
										
										
										
											2011-03-22 13:50:39 -07:00
										 |  |  | 	else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0) | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 		acpi_base = 0; | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pm_power_off = NULL; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | static struct platform_driver cs5535_pms_driver = { | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	.driver = { | 
					
						
							| 
									
										
										
										
											2011-03-22 13:50:39 -07:00
										 |  |  | 		.name = "cs5535-pms", | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 		.owner = THIS_MODULE, | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | 	.probe = xo1_pm_probe, | 
					
						
							| 
									
										
										
										
											2012-12-21 14:02:53 -08:00
										 |  |  | 	.remove = xo1_pm_remove, | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | static struct platform_driver cs5535_acpi_driver = { | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 	.driver = { | 
					
						
							| 
									
										
										
										
											2011-03-22 13:50:39 -07:00
										 |  |  | 		.name = "olpc-xo1-pm-acpi", | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | 		.owner = THIS_MODULE, | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | 	.probe = xo1_pm_probe, | 
					
						
							| 
									
										
										
										
											2012-12-21 14:02:53 -08:00
										 |  |  | 	.remove = xo1_pm_remove, | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | static int __init xo1_pm_init(void) | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	int r; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | 	r = platform_driver_register(&cs5535_pms_driver); | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	if (r) | 
					
						
							|  |  |  | 		return r; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | 	r = platform_driver_register(&cs5535_acpi_driver); | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 	if (r) | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | 		platform_driver_unregister(&cs5535_pms_driver); | 
					
						
							| 
									
										
										
										
											2010-11-29 15:45:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return r; | 
					
						
							| 
									
										
										
										
											2010-10-10 10:40:32 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-06-25 17:34:10 +01:00
										 |  |  | arch_initcall(xo1_pm_init); |