2 * Apple Motion Sensor driver
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <asm/pmac_pfunc.h>
28 #include <asm/of_platform.h>
32 /* There is only one motion sensor per machine */
35 static unsigned int verbose;
36 module_param(verbose, bool, 0644);
37 MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
39 /* Call with ams_info.lock held! */
40 void ams_sensors(s8 *x, s8 *y, s8 *z)
42 u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
46 ams_info.get_xyz(y, x, z);
48 ams_info.get_xyz(x, y, z);
58 static ssize_t ams_show_current(struct device *dev,
59 struct device_attribute *attr, char *buf)
63 mutex_lock(&ams_info.lock);
64 ams_sensors(&x, &y, &z);
65 mutex_unlock(&ams_info.lock);
67 return snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);
70 static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
72 static void ams_handle_irq(void *data)
74 enum ams_irq irq = *((enum ams_irq *)data);
76 spin_lock(&ams_info.irq_lock);
78 ams_info.worker_irqs |= irq;
79 schedule_work(&ams_info.worker);
81 spin_unlock(&ams_info.irq_lock);
84 static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
85 static struct pmf_irq_client ams_freefall_client = {
87 .handler = ams_handle_irq,
88 .data = &ams_freefall_irq_data,
91 static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
92 static struct pmf_irq_client ams_shock_client = {
94 .handler = ams_handle_irq,
95 .data = &ams_shock_irq_data,
98 /* Once hard disk parking is implemented in the kernel, this function can
101 static void ams_worker(struct work_struct *work)
103 mutex_lock(&ams_info.lock);
105 if (ams_info.has_device) {
108 spin_lock_irqsave(&ams_info.irq_lock, flags);
110 if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
112 printk(KERN_INFO "ams: freefall detected!\n");
114 ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
116 /* we must call this with interrupts enabled */
117 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
118 ams_info.clear_irq(AMS_IRQ_FREEFALL);
119 spin_lock_irqsave(&ams_info.irq_lock, flags);
122 if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
124 printk(KERN_INFO "ams: shock detected!\n");
126 ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
128 /* we must call this with interrupts enabled */
129 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
130 ams_info.clear_irq(AMS_IRQ_SHOCK);
131 spin_lock_irqsave(&ams_info.irq_lock, flags);
134 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
137 mutex_unlock(&ams_info.lock);
140 /* Call with ams_info.lock held! */
141 int ams_sensor_attach(void)
146 /* Get orientation */
147 prop = of_get_property(ams_info.of_node, "orientation", NULL);
150 ams_info.orient1 = *prop;
151 ams_info.orient2 = *(prop + 1);
153 /* Register freefall interrupt handler */
154 result = pmf_register_irq_client(ams_info.of_node,
156 &ams_freefall_client);
160 /* Reset saved irqs */
161 ams_info.worker_irqs = 0;
163 /* Register shock interrupt handler */
164 result = pmf_register_irq_client(ams_info.of_node,
168 goto release_freefall;
171 ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
172 if (!ams_info.of_dev) {
177 /* Create attributes */
178 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
182 ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
184 /* Init input device */
185 result = ams_input_init();
187 goto release_device_file;
191 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
193 of_device_unregister(ams_info.of_dev);
195 pmf_unregister_irq_client(&ams_shock_client);
197 pmf_unregister_irq_client(&ams_freefall_client);
201 int __init ams_init(void)
203 struct device_node *np;
205 spin_lock_init(&ams_info.irq_lock);
206 mutex_init(&ams_info.lock);
207 INIT_WORK(&ams_info.worker, ams_worker);
209 #ifdef CONFIG_SENSORS_AMS_I2C
210 np = of_find_node_by_name(NULL, "accelerometer");
211 if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
212 /* Found I2C motion sensor */
213 return ams_i2c_init(np);
216 #ifdef CONFIG_SENSORS_AMS_PMU
217 np = of_find_node_by_name(NULL, "sms");
218 if (np && of_device_is_compatible(np, "sms"))
219 /* Found PMU motion sensor */
220 return ams_pmu_init(np);
227 mutex_lock(&ams_info.lock);
229 if (ams_info.has_device) {
230 /* Remove input device */
233 /* Shut down implementation */
236 /* Flush interrupt worker
238 * We do this after ams_info.exit(), because an interrupt might
239 * have arrived before disabling them.
241 flush_scheduled_work();
243 /* Remove attributes */
244 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
247 of_device_unregister(ams_info.of_dev);
250 pmf_unregister_irq_client(&ams_shock_client);
251 pmf_unregister_irq_client(&ams_freefall_client);
254 mutex_unlock(&ams_info.lock);
257 MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
258 MODULE_DESCRIPTION("Apple Motion Sensor driver");
259 MODULE_LICENSE("GPL");
261 module_init(ams_init);
262 module_exit(ams_exit);