]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/platform/olpc/olpc-xo15-sci.c
Merge branch 'akpm'
[karo-tx-linux.git] / arch / x86 / platform / olpc / olpc-xo15-sci.c
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
26 static unsigned long xo15_sci_gpe;
27 static 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  */
40 static 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         status = acpi_evaluate_object(NULL, "\\_SB.PCI0.LID.LIDW", &arg_list,
51                                       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         return 0;
59 }
60
61 static ssize_t lid_wake_on_close_show(struct kobject *s,
62                                       struct kobj_attribute *attr, char *buf)
63 {
64         return sprintf(buf, "%u\n", lid_wake_on_close);
65 }
66
67 static ssize_t lid_wake_on_close_store(struct kobject *s,
68                                        struct kobj_attribute *attr,
69                                        const char *buf, size_t n)
70 {
71         unsigned int val;
72
73         if (sscanf(buf, "%u", &val) != 1)
74                 return -EINVAL;
75
76         set_lid_wake_behavior(!!val);
77         return n;
78 }
79
80 static struct kobj_attribute lid_wake_on_close_attr =
81         __ATTR(lid_wake_on_close, 0644,
82                lid_wake_on_close_show, lid_wake_on_close_store);
83
84 static void battery_status_changed(void)
85 {
86         struct power_supply *psy = power_supply_get_by_name("olpc-battery");
87
88         if (psy) {
89                 power_supply_changed(psy);
90                 put_device(psy->dev);
91         }
92 }
93
94 static void ac_status_changed(void)
95 {
96         struct power_supply *psy = power_supply_get_by_name("olpc-ac");
97
98         if (psy) {
99                 power_supply_changed(psy);
100                 put_device(psy->dev);
101         }
102 }
103
104 static void process_sci_queue(void)
105 {
106         u16 data;
107         int r;
108
109         do {
110                 r = olpc_ec_sci_query(&data);
111                 if (r || !data)
112                         break;
113
114                 pr_debug(PFX "SCI 0x%x received\n", data);
115
116                 switch (data) {
117                 case EC_SCI_SRC_BATERR:
118                 case EC_SCI_SRC_BATSOC:
119                 case EC_SCI_SRC_BATTERY:
120                 case EC_SCI_SRC_BATCRIT:
121                         battery_status_changed();
122                         break;
123                 case EC_SCI_SRC_ACPWR:
124                         ac_status_changed();
125                         break;
126                 }
127         } while (data);
128
129         if (r)
130                 pr_err(PFX "Failed to clear SCI queue");
131 }
132
133 static void process_sci_queue_work(struct work_struct *work)
134 {
135         process_sci_queue();
136 }
137
138 static DECLARE_WORK(sci_work, process_sci_queue_work);
139
140 static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
141 {
142         schedule_work(&sci_work);
143         return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
144 }
145
146 static int xo15_sci_add(struct acpi_device *device)
147 {
148         unsigned long long tmp;
149         acpi_status status;
150         int r;
151
152         if (!device)
153                 return -EINVAL;
154
155         strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
156         strcpy(acpi_device_class(device), XO15_SCI_CLASS);
157
158         /* Get GPE bit assignment (EC events). */
159         status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
160         if (ACPI_FAILURE(status))
161                 return -EINVAL;
162
163         xo15_sci_gpe = tmp;
164         status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
165                                           ACPI_GPE_EDGE_TRIGGERED,
166                                           xo15_sci_gpe_handler, device);
167         if (ACPI_FAILURE(status))
168                 return -ENODEV;
169
170         dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
171
172         r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
173         if (r)
174                 goto err_sysfs;
175
176         /* Flush queue, and enable all SCI events */
177         process_sci_queue();
178         olpc_ec_mask_write(EC_SCI_SRC_ALL);
179
180         acpi_enable_gpe(NULL, xo15_sci_gpe);
181
182         /* Enable wake-on-EC */
183         if (device->wakeup.flags.valid)
184                 device_init_wakeup(&device->dev, true);
185
186         return 0;
187
188 err_sysfs:
189         acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
190         cancel_work_sync(&sci_work);
191         return r;
192 }
193
194 static int xo15_sci_remove(struct acpi_device *device, int type)
195 {
196         acpi_disable_gpe(NULL, xo15_sci_gpe);
197         acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
198         cancel_work_sync(&sci_work);
199         sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
200         return 0;
201 }
202
203 static int xo15_sci_resume(struct acpi_device *device)
204 {
205         /* Enable all EC events */
206         olpc_ec_mask_write(EC_SCI_SRC_ALL);
207
208         /* Power/battery status might have changed */
209         battery_status_changed();
210         ac_status_changed();
211
212         return 0;
213 }
214
215 static const struct acpi_device_id xo15_sci_device_ids[] = {
216         {"XO15EC", 0},
217         {"", 0},
218 };
219
220 static struct acpi_driver xo15_sci_drv = {
221         .name = DRV_NAME,
222         .class = XO15_SCI_CLASS,
223         .ids = xo15_sci_device_ids,
224         .ops = {
225                 .add = xo15_sci_add,
226                 .remove = xo15_sci_remove,
227                 .resume = xo15_sci_resume,
228         },
229 };
230
231 static int __init xo15_sci_init(void)
232 {
233         return acpi_bus_register_driver(&xo15_sci_drv);
234 }
235 device_initcall(xo15_sci_init);