[NETLINK]: Remove bogus BUG_ON
[linux-2.6/mini2440.git] / drivers / hwmon / ams / ams-core.c
blobf5ebad5614126d966cfc7e637a6522fda5bf705d
1 /*
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>
30 #include "ams.h"
32 /* There is only one motion sensor per machine */
33 struct ams ams_info;
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;
44 if (orient & 0x80)
45 /* X and Y swapped */
46 ams_info.get_xyz(y, x, z);
47 else
48 ams_info.get_xyz(x, y, z);
50 if (orient & 0x04)
51 *z = ~(*z);
52 if (orient & 0x02)
53 *y = ~(*y);
54 if (orient & 0x01)
55 *x = ~(*x);
58 static ssize_t ams_show_current(struct device *dev,
59 struct device_attribute *attr, char *buf)
61 s8 x, y, z;
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 = {
86 .owner = THIS_MODULE,
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 = {
93 .owner = THIS_MODULE,
94 .handler = ams_handle_irq,
95 .data = &ams_shock_irq_data,
98 /* Once hard disk parking is implemented in the kernel, this function can
99 * trigger it.
101 static void ams_worker(struct work_struct *work)
103 mutex_lock(&ams_info.lock);
105 if (ams_info.has_device) {
106 unsigned long flags;
108 spin_lock_irqsave(&ams_info.irq_lock, flags);
110 if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
111 if (verbose)
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) {
123 if (verbose)
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)
143 int result;
144 const u32 *prop;
146 /* Get orientation */
147 prop = get_property(ams_info.of_node, "orientation", NULL);
148 if (!prop)
149 return -ENODEV;
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,
155 "accel-int-1",
156 &ams_freefall_client);
157 if (result < 0)
158 return -ENODEV;
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,
165 "accel-int-2",
166 &ams_shock_client);
167 if (result < 0)
168 goto release_freefall;
170 /* Create device */
171 ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
172 if (!ams_info.of_dev) {
173 result = -ENODEV;
174 goto release_shock;
177 /* Create attributes */
178 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
179 if (result)
180 goto release_of;
182 ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
184 /* Init input device */
185 result = ams_input_init();
186 if (result)
187 goto release_device_file;
189 return result;
190 release_device_file:
191 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
192 release_of:
193 of_device_unregister(ams_info.of_dev);
194 release_shock:
195 pmf_unregister_irq_client(&ams_shock_client);
196 release_freefall:
197 pmf_unregister_irq_client(&ams_freefall_client);
198 return result;
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 && device_is_compatible(np, "AAPL,accelerometer_1"))
212 /* Found I2C motion sensor */
213 return ams_i2c_init(np);
214 #endif
216 #ifdef CONFIG_SENSORS_AMS_PMU
217 np = of_find_node_by_name(NULL, "sms");
218 if (np && device_is_compatible(np, "sms"))
219 /* Found PMU motion sensor */
220 return ams_pmu_init(np);
221 #endif
223 printk(KERN_ERR "ams: No motion sensor found.\n");
225 return -ENODEV;
228 void ams_exit(void)
230 mutex_lock(&ams_info.lock);
232 if (ams_info.has_device) {
233 /* Remove input device */
234 ams_input_exit();
236 /* Shut down implementation */
237 ams_info.exit();
239 /* Flush interrupt worker
241 * We do this after ams_info.exit(), because an interrupt might
242 * have arrived before disabling them.
244 flush_scheduled_work();
246 /* Remove attributes */
247 device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
249 /* Remove device */
250 of_device_unregister(ams_info.of_dev);
252 /* Remove handler */
253 pmf_unregister_irq_client(&ams_shock_client);
254 pmf_unregister_irq_client(&ams_freefall_client);
257 mutex_unlock(&ams_info.lock);
260 MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
261 MODULE_DESCRIPTION("Apple Motion Sensor driver");
262 MODULE_LICENSE("GPL");
264 module_init(ams_init);
265 module_exit(ams_exit);