Linux Audio

Check our new training course

Loading...
v3.5.6
  1/*
  2 * Support for OLPC XO-1.5 System Control Interrupts (SCI)
  3 *
  4 * Copyright (C) 2009-2010 One Laptop per Child
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 */
 11
 12#include <linux/device.h>
 13#include <linux/slab.h>
 14#include <linux/workqueue.h>
 15#include <linux/power_supply.h>
 16
 17#include <acpi/acpi_bus.h>
 18#include <acpi/acpi_drivers.h>
 19#include <asm/olpc.h>
 20
 21#define DRV_NAME			"olpc-xo15-sci"
 22#define PFX				DRV_NAME ": "
 23#define XO15_SCI_CLASS			DRV_NAME
 24#define XO15_SCI_DEVICE_NAME		"OLPC XO-1.5 SCI"
 25
 26static unsigned long			xo15_sci_gpe;
 27static bool				lid_wake_on_close;
 28
 29/*
 30 * The normal ACPI LID wakeup behavior is wake-on-open, but not
 31 * wake-on-close. This is implemented as standard by the XO-1.5 DSDT.
 32 *
 33 * We provide here a sysfs attribute that will additionally enable
 34 * wake-on-close behavior. This is useful (e.g.) when we oportunistically
 35 * suspend with the display running; if the lid is then closed, we want to
 36 * wake up to turn the display off.
 37 *
 38 * This is controlled through a custom method in the XO-1.5 DSDT.
 39 */
 40static int set_lid_wake_behavior(bool wake_on_close)
 41{
 42	struct acpi_object_list arg_list;
 43	union acpi_object arg;
 44	acpi_status status;
 45
 46	arg_list.count		= 1;
 47	arg_list.pointer	= &arg;
 48	arg.type		= ACPI_TYPE_INTEGER;
 49	arg.integer.value	= wake_on_close;
 50
 51	status = acpi_evaluate_object(NULL, "\\_SB.PCI0.LID.LIDW", &arg_list, NULL);
 52	if (ACPI_FAILURE(status)) {
 53		pr_warning(PFX "failed to set lid behavior\n");
 54		return 1;
 55	}
 56
 57	lid_wake_on_close = wake_on_close;
 58
 59	return 0;
 60}
 61
 62static ssize_t
 63lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)
 64{
 65	return sprintf(buf, "%u\n", lid_wake_on_close);
 66}
 67
 68static ssize_t lid_wake_on_close_store(struct kobject *s,
 69				       struct kobj_attribute *attr,
 70				       const char *buf, size_t n)
 71{
 72	unsigned int val;
 73
 74	if (sscanf(buf, "%u", &val) != 1)
 75		return -EINVAL;
 76
 77	set_lid_wake_behavior(!!val);
 78
 79	return n;
 80}
 81
 82static struct kobj_attribute lid_wake_on_close_attr =
 83	__ATTR(lid_wake_on_close, 0644,
 84	       lid_wake_on_close_show,
 85	       lid_wake_on_close_store);
 86
 87static void battery_status_changed(void)
 88{
 89	struct power_supply *psy = power_supply_get_by_name("olpc-battery");
 90
 91	if (psy) {
 92		power_supply_changed(psy);
 93		put_device(psy->dev);
 94	}
 95}
 96
 97static void ac_status_changed(void)
 98{
 99	struct power_supply *psy = power_supply_get_by_name("olpc-ac");
100
101	if (psy) {
102		power_supply_changed(psy);
103		put_device(psy->dev);
104	}
105}
106
107static void process_sci_queue(void)
108{
109	u16 data;
110	int r;
111
112	do {
113		r = olpc_ec_sci_query(&data);
114		if (r || !data)
115			break;
116
117		pr_debug(PFX "SCI 0x%x received\n", data);
118
119		switch (data) {
120		case EC_SCI_SRC_BATERR:
121		case EC_SCI_SRC_BATSOC:
122		case EC_SCI_SRC_BATTERY:
123		case EC_SCI_SRC_BATCRIT:
124			battery_status_changed();
125			break;
126		case EC_SCI_SRC_ACPWR:
127			ac_status_changed();
128			break;
129		}
130	} while (data);
131
132	if (r)
133		pr_err(PFX "Failed to clear SCI queue");
134}
135
136static void process_sci_queue_work(struct work_struct *work)
137{
138	process_sci_queue();
139}
140
141static DECLARE_WORK(sci_work, process_sci_queue_work);
142
143static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
144{
145	schedule_work(&sci_work);
146	return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
147}
148
149static int xo15_sci_add(struct acpi_device *device)
150{
151	unsigned long long tmp;
152	acpi_status status;
153	int r;
154
155	if (!device)
156		return -EINVAL;
157
158	strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
159	strcpy(acpi_device_class(device), XO15_SCI_CLASS);
160
161	/* Get GPE bit assignment (EC events). */
162	status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
163	if (ACPI_FAILURE(status))
164		return -EINVAL;
165
166	xo15_sci_gpe = tmp;
167	status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
168					  ACPI_GPE_EDGE_TRIGGERED,
169					  xo15_sci_gpe_handler, device);
170	if (ACPI_FAILURE(status))
171		return -ENODEV;
172
173	dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
174
175	r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
176	if (r)
177		goto err_sysfs;
178
179	/* Flush queue, and enable all SCI events */
180	process_sci_queue();
181	olpc_ec_mask_write(EC_SCI_SRC_ALL);
182
183	acpi_enable_gpe(NULL, xo15_sci_gpe);
184
185	/* Enable wake-on-EC */
186	if (device->wakeup.flags.valid)
187		device_init_wakeup(&device->dev, true);
188
189	return 0;
190
191err_sysfs:
192	acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
193	cancel_work_sync(&sci_work);
194	return r;
195}
196
197static int xo15_sci_remove(struct acpi_device *device, int type)
198{
199	acpi_disable_gpe(NULL, xo15_sci_gpe);
200	acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
201	cancel_work_sync(&sci_work);
202	sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
203	return 0;
204}
205
206static int xo15_sci_resume(struct acpi_device *device)
207{
208	/* Enable all EC events */
209	olpc_ec_mask_write(EC_SCI_SRC_ALL);
210
211	/* Power/battery status might have changed */
212	battery_status_changed();
213	ac_status_changed();
214
215	return 0;
216}
217
218static const struct acpi_device_id xo15_sci_device_ids[] = {
219	{"XO15EC", 0},
220	{"", 0},
221};
222
223static struct acpi_driver xo15_sci_drv = {
224	.name = DRV_NAME,
225	.class = XO15_SCI_CLASS,
226	.ids = xo15_sci_device_ids,
227	.ops = {
228		.add = xo15_sci_add,
229		.remove = xo15_sci_remove,
230		.resume = xo15_sci_resume,
231	},
232};
233
234static int __init xo15_sci_init(void)
235{
236	return acpi_bus_register_driver(&xo15_sci_drv);
237}
238device_initcall(xo15_sci_init);
v3.1
  1/*
  2 * Support for OLPC XO-1.5 System Control Interrupts (SCI)
  3 *
  4 * Copyright (C) 2009-2010 One Laptop per Child
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 */
 11
 12#include <linux/device.h>
 13#include <linux/slab.h>
 14#include <linux/workqueue.h>
 15#include <linux/power_supply.h>
 16
 17#include <acpi/acpi_bus.h>
 18#include <acpi/acpi_drivers.h>
 19#include <asm/olpc.h>
 20
 21#define DRV_NAME			"olpc-xo15-sci"
 22#define PFX				DRV_NAME ": "
 23#define XO15_SCI_CLASS			DRV_NAME
 24#define XO15_SCI_DEVICE_NAME		"OLPC XO-1.5 SCI"
 25
 26static unsigned long xo15_sci_gpe;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 27
 28static void battery_status_changed(void)
 29{
 30	struct power_supply *psy = power_supply_get_by_name("olpc-battery");
 31
 32	if (psy) {
 33		power_supply_changed(psy);
 34		put_device(psy->dev);
 35	}
 36}
 37
 38static void ac_status_changed(void)
 39{
 40	struct power_supply *psy = power_supply_get_by_name("olpc-ac");
 41
 42	if (psy) {
 43		power_supply_changed(psy);
 44		put_device(psy->dev);
 45	}
 46}
 47
 48static void process_sci_queue(void)
 49{
 50	u16 data;
 51	int r;
 52
 53	do {
 54		r = olpc_ec_sci_query(&data);
 55		if (r || !data)
 56			break;
 57
 58		pr_debug(PFX "SCI 0x%x received\n", data);
 59
 60		switch (data) {
 61		case EC_SCI_SRC_BATERR:
 62		case EC_SCI_SRC_BATSOC:
 63		case EC_SCI_SRC_BATTERY:
 64		case EC_SCI_SRC_BATCRIT:
 65			battery_status_changed();
 66			break;
 67		case EC_SCI_SRC_ACPWR:
 68			ac_status_changed();
 69			break;
 70		}
 71	} while (data);
 72
 73	if (r)
 74		pr_err(PFX "Failed to clear SCI queue");
 75}
 76
 77static void process_sci_queue_work(struct work_struct *work)
 78{
 79	process_sci_queue();
 80}
 81
 82static DECLARE_WORK(sci_work, process_sci_queue_work);
 83
 84static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
 85{
 86	schedule_work(&sci_work);
 87	return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
 88}
 89
 90static int xo15_sci_add(struct acpi_device *device)
 91{
 92	unsigned long long tmp;
 93	acpi_status status;
 
 94
 95	if (!device)
 96		return -EINVAL;
 97
 98	strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
 99	strcpy(acpi_device_class(device), XO15_SCI_CLASS);
100
101	/* Get GPE bit assignment (EC events). */
102	status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
103	if (ACPI_FAILURE(status))
104		return -EINVAL;
105
106	xo15_sci_gpe = tmp;
107	status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
108					  ACPI_GPE_EDGE_TRIGGERED,
109					  xo15_sci_gpe_handler, device);
110	if (ACPI_FAILURE(status))
111		return -ENODEV;
112
113	dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
114
 
 
 
 
115	/* Flush queue, and enable all SCI events */
116	process_sci_queue();
117	olpc_ec_mask_write(EC_SCI_SRC_ALL);
118
119	acpi_enable_gpe(NULL, xo15_sci_gpe);
120
121	/* Enable wake-on-EC */
122	if (device->wakeup.flags.valid)
123		device_init_wakeup(&device->dev, true);
124
125	return 0;
 
 
 
 
 
126}
127
128static int xo15_sci_remove(struct acpi_device *device, int type)
129{
130	acpi_disable_gpe(NULL, xo15_sci_gpe);
131	acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
132	cancel_work_sync(&sci_work);
 
133	return 0;
134}
135
136static int xo15_sci_resume(struct acpi_device *device)
137{
138	/* Enable all EC events */
139	olpc_ec_mask_write(EC_SCI_SRC_ALL);
140
141	/* Power/battery status might have changed */
142	battery_status_changed();
143	ac_status_changed();
144
145	return 0;
146}
147
148static const struct acpi_device_id xo15_sci_device_ids[] = {
149	{"XO15EC", 0},
150	{"", 0},
151};
152
153static struct acpi_driver xo15_sci_drv = {
154	.name = DRV_NAME,
155	.class = XO15_SCI_CLASS,
156	.ids = xo15_sci_device_ids,
157	.ops = {
158		.add = xo15_sci_add,
159		.remove = xo15_sci_remove,
160		.resume = xo15_sci_resume,
161	},
162};
163
164static int __init xo15_sci_init(void)
165{
166	return acpi_bus_register_driver(&xo15_sci_drv);
167}
168device_initcall(xo15_sci_init);