Merge branch 'locks' of git://linux-nfs.org/~bfields/linux
[linux-2.6] / drivers / hwmon / ams / ams-core.c
1 /*
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>
26 #include <asm/pmac_pfunc.h>
27 #include <asm/of_platform.h>
28
29 #include "ams.h"
30
31 /* There is only one motion sensor per machine */
32 struct ams ams_info;
33
34 static unsigned int verbose;
35 module_param(verbose, bool, 0644);
36 MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
37
38 /* Call with ams_info.lock held! */
39 void 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
57 static 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
69 static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
70
71 static 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
83 static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
84 static struct pmf_irq_client ams_freefall_client = {
85         .owner = THIS_MODULE,
86         .handler = ams_handle_irq,
87         .data = &ams_freefall_irq_data,
88 };
89
90 static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
91 static 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  */
100 static void ams_worker(struct work_struct *work)
101 {
102         mutex_lock(&ams_info.lock);
103
104         if (ams_info.has_device) {
105                 unsigned long flags;
106
107                 spin_lock_irqsave(&ams_info.irq_lock, flags);
108
109                 if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
110                         if (verbose)
111                                 printk(KERN_INFO "ams: freefall detected!\n");
112
113                         ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
114
115                         /* we must call this with interrupts enabled */
116                         spin_unlock_irqrestore(&ams_info.irq_lock, flags);
117                         ams_info.clear_irq(AMS_IRQ_FREEFALL);
118                         spin_lock_irqsave(&ams_info.irq_lock, flags);
119                 }
120
121                 if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
122                         if (verbose)
123                                 printk(KERN_INFO "ams: shock detected!\n");
124
125                         ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
126
127                         /* we must call this with interrupts enabled */
128                         spin_unlock_irqrestore(&ams_info.irq_lock, flags);
129                         ams_info.clear_irq(AMS_IRQ_SHOCK);
130                         spin_lock_irqsave(&ams_info.irq_lock, flags);
131                 }
132
133                 spin_unlock_irqrestore(&ams_info.irq_lock, flags);
134         }
135
136         mutex_unlock(&ams_info.lock);
137 }
138
139 /* Call with ams_info.lock held! */
140 int ams_sensor_attach(void)
141 {
142         int result;
143         const u32 *prop;
144
145         /* Get orientation */
146         prop = of_get_property(ams_info.of_node, "orientation", NULL);
147         if (!prop)
148                 return -ENODEV;
149         ams_info.orient1 = *prop;
150         ams_info.orient2 = *(prop + 1);
151
152         /* Register freefall interrupt handler */
153         result = pmf_register_irq_client(ams_info.of_node,
154                         "accel-int-1",
155                         &ams_freefall_client);
156         if (result < 0)
157                 return -ENODEV;
158
159         /* Reset saved irqs */
160         ams_info.worker_irqs = 0;
161
162         /* Register shock interrupt handler */
163         result = pmf_register_irq_client(ams_info.of_node,
164                         "accel-int-2",
165                         &ams_shock_client);
166         if (result < 0)
167                 goto release_freefall;
168
169         /* Create device */
170         ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
171         if (!ams_info.of_dev) {
172                 result = -ENODEV;
173                 goto release_shock;
174         }
175
176         /* Create attributes */
177         result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
178         if (result)
179                 goto release_of;
180
181         ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
182
183         /* Init input device */
184         result = ams_input_init();
185         if (result)
186                 goto release_device_file;
187
188         return result;
189 release_device_file:
190         device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
191 release_of:
192         of_device_unregister(ams_info.of_dev);
193 release_shock:
194         pmf_unregister_irq_client(&ams_shock_client);
195 release_freefall:
196         pmf_unregister_irq_client(&ams_freefall_client);
197         return result;
198 }
199
200 int __init ams_init(void)
201 {
202         struct device_node *np;
203
204         spin_lock_init(&ams_info.irq_lock);
205         mutex_init(&ams_info.lock);
206         INIT_WORK(&ams_info.worker, ams_worker);
207
208 #ifdef CONFIG_SENSORS_AMS_I2C
209         np = of_find_node_by_name(NULL, "accelerometer");
210         if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
211                 /* Found I2C motion sensor */
212                 return ams_i2c_init(np);
213 #endif
214
215 #ifdef CONFIG_SENSORS_AMS_PMU
216         np = of_find_node_by_name(NULL, "sms");
217         if (np && of_device_is_compatible(np, "sms"))
218                 /* Found PMU motion sensor */
219                 return ams_pmu_init(np);
220 #endif
221         return -ENODEV;
222 }
223
224 void ams_exit(void)
225 {
226         mutex_lock(&ams_info.lock);
227
228         if (ams_info.has_device) {
229                 /* Remove input device */
230                 ams_input_exit();
231
232                 /* Shut down implementation */
233                 ams_info.exit();
234
235                 /* Flush interrupt worker
236                  *
237                  * We do this after ams_info.exit(), because an interrupt might
238                  * have arrived before disabling them.
239                  */
240                 flush_scheduled_work();
241
242                 /* Remove attributes */
243                 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
244
245                 /* Remove device */
246                 of_device_unregister(ams_info.of_dev);
247
248                 /* Remove handler */
249                 pmf_unregister_irq_client(&ams_shock_client);
250                 pmf_unregister_irq_client(&ams_freefall_client);
251         }
252
253         mutex_unlock(&ams_info.lock);
254 }
255
256 MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
257 MODULE_DESCRIPTION("Apple Motion Sensor driver");
258 MODULE_LICENSE("GPL");
259
260 module_init(ams_init);
261 module_exit(ams_exit);