blob: 399beb1638d1aa9acf009c1c05aeb9cf1bc79af2 [file] [log] [blame]
Stelian Popdcb69dd2006-12-12 18:18:30 +01001/*
2 * Apple Motion Sensor driver
3 *
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
6 *
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.
11 *
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.
16 *
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.
20 */
21
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/errno.h>
25#include <linux/init.h>
Stephen Rothwell7587cb22008-05-23 16:25:50 +100026#include <linux/of_platform.h>
Stelian Popdcb69dd2006-12-12 18:18:30 +010027#include <asm/pmac_pfunc.h>
Stelian Popdcb69dd2006-12-12 18:18:30 +010028
29#include "ams.h"
30
31/* There is only one motion sensor per machine */
32struct ams ams_info;
33
34static unsigned int verbose;
35module_param(verbose, bool, 0644);
36MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
37
38/* Call with ams_info.lock held! */
39void ams_sensors(s8 *x, s8 *y, s8 *z)
40{
41 u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
42
43 if (orient & 0x80)
44 /* X and Y swapped */
45 ams_info.get_xyz(y, x, z);
46 else
47 ams_info.get_xyz(x, y, z);
48
49 if (orient & 0x04)
50 *z = ~(*z);
51 if (orient & 0x02)
52 *y = ~(*y);
53 if (orient & 0x01)
54 *x = ~(*x);
55}
56
57static ssize_t ams_show_current(struct device *dev,
58 struct device_attribute *attr, char *buf)
59{
60 s8 x, y, z;
61
62 mutex_lock(&ams_info.lock);
63 ams_sensors(&x, &y, &z);
64 mutex_unlock(&ams_info.lock);
65
66 return snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);
67}
68
69static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
70
71static void ams_handle_irq(void *data)
72{
73 enum ams_irq irq = *((enum ams_irq *)data);
74
75 spin_lock(&ams_info.irq_lock);
76
77 ams_info.worker_irqs |= irq;
78 schedule_work(&ams_info.worker);
79
80 spin_unlock(&ams_info.irq_lock);
81}
82
83static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
84static struct pmf_irq_client ams_freefall_client = {
85 .owner = THIS_MODULE,
86 .handler = ams_handle_irq,
87 .data = &ams_freefall_irq_data,
88};
89
90static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
91static struct pmf_irq_client ams_shock_client = {
92 .owner = THIS_MODULE,
93 .handler = ams_handle_irq,
94 .data = &ams_shock_irq_data,
95};
96
97/* Once hard disk parking is implemented in the kernel, this function can
98 * trigger it.
99 */
100static void ams_worker(struct work_struct *work)
101{
Dmitry Torokhovdbee3562008-10-17 17:51:13 +0200102 unsigned long flags;
103 u8 irqs_to_clear;
104
Stelian Popdcb69dd2006-12-12 18:18:30 +0100105 mutex_lock(&ams_info.lock);
106
Dmitry Torokhovdbee3562008-10-17 17:51:13 +0200107 spin_lock_irqsave(&ams_info.irq_lock, flags);
108 irqs_to_clear = ams_info.worker_irqs;
Stelian Popdcb69dd2006-12-12 18:18:30 +0100109
Dmitry Torokhovdbee3562008-10-17 17:51:13 +0200110 if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
111 if (verbose)
112 printk(KERN_INFO "ams: freefall detected!\n");
Stelian Popdcb69dd2006-12-12 18:18:30 +0100113
Dmitry Torokhovdbee3562008-10-17 17:51:13 +0200114 ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
Stelian Popdcb69dd2006-12-12 18:18:30 +0100115 }
116
Dmitry Torokhovdbee3562008-10-17 17:51:13 +0200117 if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
118 if (verbose)
119 printk(KERN_INFO "ams: shock detected!\n");
120
121 ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
122 }
123
124 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
125
126 ams_info.clear_irq(irqs_to_clear);
127
Stelian Popdcb69dd2006-12-12 18:18:30 +0100128 mutex_unlock(&ams_info.lock);
129}
130
131/* Call with ams_info.lock held! */
132int ams_sensor_attach(void)
133{
134 int result;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000135 const u32 *prop;
Stelian Popdcb69dd2006-12-12 18:18:30 +0100136
137 /* Get orientation */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000138 prop = of_get_property(ams_info.of_node, "orientation", NULL);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100139 if (!prop)
140 return -ENODEV;
141 ams_info.orient1 = *prop;
142 ams_info.orient2 = *(prop + 1);
143
144 /* Register freefall interrupt handler */
145 result = pmf_register_irq_client(ams_info.of_node,
146 "accel-int-1",
147 &ams_freefall_client);
148 if (result < 0)
149 return -ENODEV;
150
151 /* Reset saved irqs */
152 ams_info.worker_irqs = 0;
153
154 /* Register shock interrupt handler */
155 result = pmf_register_irq_client(ams_info.of_node,
156 "accel-int-2",
157 &ams_shock_client);
158 if (result < 0)
159 goto release_freefall;
160
161 /* Create device */
162 ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
163 if (!ams_info.of_dev) {
164 result = -ENODEV;
165 goto release_shock;
166 }
167
168 /* Create attributes */
169 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
170 if (result)
171 goto release_of;
172
173 ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
174
175 /* Init input device */
176 result = ams_input_init();
177 if (result)
178 goto release_device_file;
179
180 return result;
181release_device_file:
182 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
183release_of:
184 of_device_unregister(ams_info.of_dev);
185release_shock:
186 pmf_unregister_irq_client(&ams_shock_client);
187release_freefall:
188 pmf_unregister_irq_client(&ams_freefall_client);
189 return result;
190}
191
192int __init ams_init(void)
193{
194 struct device_node *np;
195
196 spin_lock_init(&ams_info.irq_lock);
197 mutex_init(&ams_info.lock);
198 INIT_WORK(&ams_info.worker, ams_worker);
199
200#ifdef CONFIG_SENSORS_AMS_I2C
201 np = of_find_node_by_name(NULL, "accelerometer");
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000202 if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
Stelian Popdcb69dd2006-12-12 18:18:30 +0100203 /* Found I2C motion sensor */
204 return ams_i2c_init(np);
205#endif
206
207#ifdef CONFIG_SENSORS_AMS_PMU
208 np = of_find_node_by_name(NULL, "sms");
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000209 if (np && of_device_is_compatible(np, "sms"))
Stelian Popdcb69dd2006-12-12 18:18:30 +0100210 /* Found PMU motion sensor */
211 return ams_pmu_init(np);
212#endif
Stelian Popdcb69dd2006-12-12 18:18:30 +0100213 return -ENODEV;
214}
215
Jean Delvare98ceb752010-01-31 04:03:23 +0000216void ams_sensor_detach(void)
Stelian Popdcb69dd2006-12-12 18:18:30 +0100217{
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200218 /* Remove input device */
219 ams_input_exit();
Stelian Popdcb69dd2006-12-12 18:18:30 +0100220
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200221 /* Remove attributes */
222 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100223
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200224 /* Flush interrupt worker
225 *
226 * We do this after ams_info.exit(), because an interrupt might
227 * have arrived before disabling them.
228 */
Tejun Heod9c612e2010-12-24 15:59:07 +0100229 flush_work_sync(&ams_info.worker);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100230
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200231 /* Remove device */
232 of_device_unregister(ams_info.of_dev);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100233
Dmitry Torokhovee4cd322008-10-17 17:51:12 +0200234 /* Remove handler */
235 pmf_unregister_irq_client(&ams_shock_client);
236 pmf_unregister_irq_client(&ams_freefall_client);
Stelian Popdcb69dd2006-12-12 18:18:30 +0100237}
238
Jean Delvare98ceb752010-01-31 04:03:23 +0000239static void __exit ams_exit(void)
240{
241 /* Shut down implementation */
242 ams_info.exit();
243}
244
Stelian Popdcb69dd2006-12-12 18:18:30 +0100245MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
246MODULE_DESCRIPTION("Apple Motion Sensor driver");
247MODULE_LICENSE("GPL");
248
249module_init(ams_init);
250module_exit(ams_exit);