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