116216333SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2bd5f47ecSJean Delvare /*
3bd5f47ecSJean Delvare * Apple Motion Sensor driver
4bd5f47ecSJean Delvare *
5bd5f47ecSJean Delvare * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
6bd5f47ecSJean Delvare * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
7bd5f47ecSJean Delvare */
8bd5f47ecSJean Delvare
9bd5f47ecSJean Delvare #include <linux/module.h>
10bd5f47ecSJean Delvare #include <linux/types.h>
11bd5f47ecSJean Delvare #include <linux/errno.h>
12bd5f47ecSJean Delvare #include <linux/init.h>
13bd5f47ecSJean Delvare #include <linux/of_platform.h>
14bd5f47ecSJean Delvare #include <asm/pmac_pfunc.h>
15bd5f47ecSJean Delvare
16bd5f47ecSJean Delvare #include "ams.h"
17bd5f47ecSJean Delvare
18bd5f47ecSJean Delvare /* There is only one motion sensor per machine */
19bd5f47ecSJean Delvare struct ams ams_info;
20bd5f47ecSJean Delvare
2190ab5ee9SRusty Russell static bool verbose;
22bd5f47ecSJean Delvare module_param(verbose, bool, 0644);
23bd5f47ecSJean Delvare MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
24bd5f47ecSJean Delvare
25bd5f47ecSJean Delvare /* Call with ams_info.lock held! */
ams_sensors(s8 * x,s8 * y,s8 * z)26bd5f47ecSJean Delvare void ams_sensors(s8 *x, s8 *y, s8 *z)
27bd5f47ecSJean Delvare {
28bd5f47ecSJean Delvare u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
29bd5f47ecSJean Delvare
30bd5f47ecSJean Delvare if (orient & 0x80)
31bd5f47ecSJean Delvare /* X and Y swapped */
32bd5f47ecSJean Delvare ams_info.get_xyz(y, x, z);
33bd5f47ecSJean Delvare else
34bd5f47ecSJean Delvare ams_info.get_xyz(x, y, z);
35bd5f47ecSJean Delvare
36bd5f47ecSJean Delvare if (orient & 0x04)
37bd5f47ecSJean Delvare *z = ~(*z);
38bd5f47ecSJean Delvare if (orient & 0x02)
39bd5f47ecSJean Delvare *y = ~(*y);
40bd5f47ecSJean Delvare if (orient & 0x01)
41bd5f47ecSJean Delvare *x = ~(*x);
42bd5f47ecSJean Delvare }
43bd5f47ecSJean Delvare
ams_show_current(struct device * dev,struct device_attribute * attr,char * buf)44bd5f47ecSJean Delvare static ssize_t ams_show_current(struct device *dev,
45bd5f47ecSJean Delvare struct device_attribute *attr, char *buf)
46bd5f47ecSJean Delvare {
47bd5f47ecSJean Delvare s8 x, y, z;
48bd5f47ecSJean Delvare
49bd5f47ecSJean Delvare mutex_lock(&ams_info.lock);
50bd5f47ecSJean Delvare ams_sensors(&x, &y, &z);
51bd5f47ecSJean Delvare mutex_unlock(&ams_info.lock);
52bd5f47ecSJean Delvare
53cc463998SQing Wang return sysfs_emit(buf, "%d %d %d\n", x, y, z);
54bd5f47ecSJean Delvare }
55bd5f47ecSJean Delvare
56bd5f47ecSJean Delvare static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
57bd5f47ecSJean Delvare
ams_handle_irq(void * data)58bd5f47ecSJean Delvare static void ams_handle_irq(void *data)
59bd5f47ecSJean Delvare {
60bd5f47ecSJean Delvare enum ams_irq irq = *((enum ams_irq *)data);
61bd5f47ecSJean Delvare
62bd5f47ecSJean Delvare spin_lock(&ams_info.irq_lock);
63bd5f47ecSJean Delvare
64bd5f47ecSJean Delvare ams_info.worker_irqs |= irq;
65bd5f47ecSJean Delvare schedule_work(&ams_info.worker);
66bd5f47ecSJean Delvare
67bd5f47ecSJean Delvare spin_unlock(&ams_info.irq_lock);
68bd5f47ecSJean Delvare }
69bd5f47ecSJean Delvare
70bd5f47ecSJean Delvare static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
71bd5f47ecSJean Delvare static struct pmf_irq_client ams_freefall_client = {
72bd5f47ecSJean Delvare .owner = THIS_MODULE,
73bd5f47ecSJean Delvare .handler = ams_handle_irq,
74bd5f47ecSJean Delvare .data = &ams_freefall_irq_data,
75bd5f47ecSJean Delvare };
76bd5f47ecSJean Delvare
77bd5f47ecSJean Delvare static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
78bd5f47ecSJean Delvare static struct pmf_irq_client ams_shock_client = {
79bd5f47ecSJean Delvare .owner = THIS_MODULE,
80bd5f47ecSJean Delvare .handler = ams_handle_irq,
81bd5f47ecSJean Delvare .data = &ams_shock_irq_data,
82bd5f47ecSJean Delvare };
83bd5f47ecSJean Delvare
84bd5f47ecSJean Delvare /* Once hard disk parking is implemented in the kernel, this function can
85bd5f47ecSJean Delvare * trigger it.
86bd5f47ecSJean Delvare */
ams_worker(struct work_struct * work)87bd5f47ecSJean Delvare static void ams_worker(struct work_struct *work)
88bd5f47ecSJean Delvare {
89bd5f47ecSJean Delvare unsigned long flags;
90bd5f47ecSJean Delvare u8 irqs_to_clear;
91bd5f47ecSJean Delvare
92bd5f47ecSJean Delvare mutex_lock(&ams_info.lock);
93bd5f47ecSJean Delvare
94bd5f47ecSJean Delvare spin_lock_irqsave(&ams_info.irq_lock, flags);
95bd5f47ecSJean Delvare irqs_to_clear = ams_info.worker_irqs;
96bd5f47ecSJean Delvare
97bd5f47ecSJean Delvare if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
98bd5f47ecSJean Delvare if (verbose)
99bd5f47ecSJean Delvare printk(KERN_INFO "ams: freefall detected!\n");
100bd5f47ecSJean Delvare
101bd5f47ecSJean Delvare ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
102bd5f47ecSJean Delvare }
103bd5f47ecSJean Delvare
104bd5f47ecSJean Delvare if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
105bd5f47ecSJean Delvare if (verbose)
106bd5f47ecSJean Delvare printk(KERN_INFO "ams: shock detected!\n");
107bd5f47ecSJean Delvare
108bd5f47ecSJean Delvare ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
109bd5f47ecSJean Delvare }
110bd5f47ecSJean Delvare
111bd5f47ecSJean Delvare spin_unlock_irqrestore(&ams_info.irq_lock, flags);
112bd5f47ecSJean Delvare
113bd5f47ecSJean Delvare ams_info.clear_irq(irqs_to_clear);
114bd5f47ecSJean Delvare
115bd5f47ecSJean Delvare mutex_unlock(&ams_info.lock);
116bd5f47ecSJean Delvare }
117bd5f47ecSJean Delvare
118bd5f47ecSJean Delvare /* Call with ams_info.lock held! */
ams_sensor_attach(void)119bd5f47ecSJean Delvare int ams_sensor_attach(void)
120bd5f47ecSJean Delvare {
121bd5f47ecSJean Delvare int result;
122bd5f47ecSJean Delvare const u32 *prop;
123bd5f47ecSJean Delvare
124bd5f47ecSJean Delvare /* Get orientation */
125bd5f47ecSJean Delvare prop = of_get_property(ams_info.of_node, "orientation", NULL);
126bd5f47ecSJean Delvare if (!prop)
127bd5f47ecSJean Delvare return -ENODEV;
128bd5f47ecSJean Delvare ams_info.orient1 = *prop;
129bd5f47ecSJean Delvare ams_info.orient2 = *(prop + 1);
130bd5f47ecSJean Delvare
131bd5f47ecSJean Delvare /* Register freefall interrupt handler */
132bd5f47ecSJean Delvare result = pmf_register_irq_client(ams_info.of_node,
133bd5f47ecSJean Delvare "accel-int-1",
134bd5f47ecSJean Delvare &ams_freefall_client);
135bd5f47ecSJean Delvare if (result < 0)
136bd5f47ecSJean Delvare return -ENODEV;
137bd5f47ecSJean Delvare
138bd5f47ecSJean Delvare /* Reset saved irqs */
139bd5f47ecSJean Delvare ams_info.worker_irqs = 0;
140bd5f47ecSJean Delvare
141bd5f47ecSJean Delvare /* Register shock interrupt handler */
142bd5f47ecSJean Delvare result = pmf_register_irq_client(ams_info.of_node,
143bd5f47ecSJean Delvare "accel-int-2",
144bd5f47ecSJean Delvare &ams_shock_client);
145bd5f47ecSJean Delvare if (result < 0)
146bd5f47ecSJean Delvare goto release_freefall;
147bd5f47ecSJean Delvare
148bd5f47ecSJean Delvare /* Create device */
149bd5f47ecSJean Delvare ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
150bd5f47ecSJean Delvare if (!ams_info.of_dev) {
151bd5f47ecSJean Delvare result = -ENODEV;
152bd5f47ecSJean Delvare goto release_shock;
153bd5f47ecSJean Delvare }
154bd5f47ecSJean Delvare
155bd5f47ecSJean Delvare /* Create attributes */
156bd5f47ecSJean Delvare result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
157bd5f47ecSJean Delvare if (result)
158bd5f47ecSJean Delvare goto release_of;
159bd5f47ecSJean Delvare
160bd5f47ecSJean Delvare ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
161bd5f47ecSJean Delvare
162bd5f47ecSJean Delvare /* Init input device */
163bd5f47ecSJean Delvare result = ams_input_init();
164bd5f47ecSJean Delvare if (result)
165bd5f47ecSJean Delvare goto release_device_file;
166bd5f47ecSJean Delvare
167bd5f47ecSJean Delvare return result;
168bd5f47ecSJean Delvare release_device_file:
169bd5f47ecSJean Delvare device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
170bd5f47ecSJean Delvare release_of:
171bd5f47ecSJean Delvare of_device_unregister(ams_info.of_dev);
172bd5f47ecSJean Delvare release_shock:
173bd5f47ecSJean Delvare pmf_unregister_irq_client(&ams_shock_client);
174bd5f47ecSJean Delvare release_freefall:
175bd5f47ecSJean Delvare pmf_unregister_irq_client(&ams_freefall_client);
176bd5f47ecSJean Delvare return result;
177bd5f47ecSJean Delvare }
178bd5f47ecSJean Delvare
ams_init(void)179*cd50430cSArnd Bergmann static int __init ams_init(void)
180bd5f47ecSJean Delvare {
181bd5f47ecSJean Delvare struct device_node *np;
182bd5f47ecSJean Delvare
183bd5f47ecSJean Delvare spin_lock_init(&ams_info.irq_lock);
184bd5f47ecSJean Delvare mutex_init(&ams_info.lock);
185bd5f47ecSJean Delvare INIT_WORK(&ams_info.worker, ams_worker);
186bd5f47ecSJean Delvare
187bd5f47ecSJean Delvare #ifdef CONFIG_SENSORS_AMS_I2C
188bd5f47ecSJean Delvare np = of_find_node_by_name(NULL, "accelerometer");
189bd5f47ecSJean Delvare if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
190bd5f47ecSJean Delvare /* Found I2C motion sensor */
191bd5f47ecSJean Delvare return ams_i2c_init(np);
192bd5f47ecSJean Delvare #endif
193bd5f47ecSJean Delvare
194bd5f47ecSJean Delvare #ifdef CONFIG_SENSORS_AMS_PMU
195bd5f47ecSJean Delvare np = of_find_node_by_name(NULL, "sms");
196bd5f47ecSJean Delvare if (np && of_device_is_compatible(np, "sms"))
197bd5f47ecSJean Delvare /* Found PMU motion sensor */
198bd5f47ecSJean Delvare return ams_pmu_init(np);
199bd5f47ecSJean Delvare #endif
200bd5f47ecSJean Delvare return -ENODEV;
201bd5f47ecSJean Delvare }
202bd5f47ecSJean Delvare
ams_sensor_detach(void)203bd5f47ecSJean Delvare void ams_sensor_detach(void)
204bd5f47ecSJean Delvare {
205bd5f47ecSJean Delvare /* Remove input device */
206bd5f47ecSJean Delvare ams_input_exit();
207bd5f47ecSJean Delvare
208bd5f47ecSJean Delvare /* Remove attributes */
209bd5f47ecSJean Delvare device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
210bd5f47ecSJean Delvare
211bd5f47ecSJean Delvare /* Flush interrupt worker
212bd5f47ecSJean Delvare *
213bd5f47ecSJean Delvare * We do this after ams_info.exit(), because an interrupt might
214bd5f47ecSJean Delvare * have arrived before disabling them.
215bd5f47ecSJean Delvare */
21643829731STejun Heo flush_work(&ams_info.worker);
217bd5f47ecSJean Delvare
218bd5f47ecSJean Delvare /* Remove device */
219bd5f47ecSJean Delvare of_device_unregister(ams_info.of_dev);
220bd5f47ecSJean Delvare
221bd5f47ecSJean Delvare /* Remove handler */
222bd5f47ecSJean Delvare pmf_unregister_irq_client(&ams_shock_client);
223bd5f47ecSJean Delvare pmf_unregister_irq_client(&ams_freefall_client);
224bd5f47ecSJean Delvare }
225bd5f47ecSJean Delvare
ams_exit(void)226bd5f47ecSJean Delvare static void __exit ams_exit(void)
227bd5f47ecSJean Delvare {
228bd5f47ecSJean Delvare /* Shut down implementation */
229bd5f47ecSJean Delvare ams_info.exit();
230bd5f47ecSJean Delvare }
231bd5f47ecSJean Delvare
232bd5f47ecSJean Delvare MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
233bd5f47ecSJean Delvare MODULE_DESCRIPTION("Apple Motion Sensor driver");
234bd5f47ecSJean Delvare MODULE_LICENSE("GPL");
235bd5f47ecSJean Delvare
236bd5f47ecSJean Delvare module_init(ams_init);
237bd5f47ecSJean Delvare module_exit(ams_exit);
238