Path: blob/master/drivers/macintosh/ams/ams-core.c
15115 views
/*1* Apple Motion Sensor driver2*3* Copyright (C) 2005 Stelian Pop ([email protected])4* Copyright (C) 2006 Michael Hanselmann ([email protected])5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.19*/2021#include <linux/module.h>22#include <linux/types.h>23#include <linux/errno.h>24#include <linux/init.h>25#include <linux/of_platform.h>26#include <asm/pmac_pfunc.h>2728#include "ams.h"2930/* There is only one motion sensor per machine */31struct ams ams_info;3233static unsigned int verbose;34module_param(verbose, bool, 0644);35MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");3637/* Call with ams_info.lock held! */38void ams_sensors(s8 *x, s8 *y, s8 *z)39{40u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;4142if (orient & 0x80)43/* X and Y swapped */44ams_info.get_xyz(y, x, z);45else46ams_info.get_xyz(x, y, z);4748if (orient & 0x04)49*z = ~(*z);50if (orient & 0x02)51*y = ~(*y);52if (orient & 0x01)53*x = ~(*x);54}5556static ssize_t ams_show_current(struct device *dev,57struct device_attribute *attr, char *buf)58{59s8 x, y, z;6061mutex_lock(&ams_info.lock);62ams_sensors(&x, &y, &z);63mutex_unlock(&ams_info.lock);6465return snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);66}6768static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);6970static void ams_handle_irq(void *data)71{72enum ams_irq irq = *((enum ams_irq *)data);7374spin_lock(&ams_info.irq_lock);7576ams_info.worker_irqs |= irq;77schedule_work(&ams_info.worker);7879spin_unlock(&ams_info.irq_lock);80}8182static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;83static struct pmf_irq_client ams_freefall_client = {84.owner = THIS_MODULE,85.handler = ams_handle_irq,86.data = &ams_freefall_irq_data,87};8889static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;90static struct pmf_irq_client ams_shock_client = {91.owner = THIS_MODULE,92.handler = ams_handle_irq,93.data = &ams_shock_irq_data,94};9596/* Once hard disk parking is implemented in the kernel, this function can97* trigger it.98*/99static void ams_worker(struct work_struct *work)100{101unsigned long flags;102u8 irqs_to_clear;103104mutex_lock(&ams_info.lock);105106spin_lock_irqsave(&ams_info.irq_lock, flags);107irqs_to_clear = ams_info.worker_irqs;108109if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {110if (verbose)111printk(KERN_INFO "ams: freefall detected!\n");112113ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;114}115116if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {117if (verbose)118printk(KERN_INFO "ams: shock detected!\n");119120ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;121}122123spin_unlock_irqrestore(&ams_info.irq_lock, flags);124125ams_info.clear_irq(irqs_to_clear);126127mutex_unlock(&ams_info.lock);128}129130/* Call with ams_info.lock held! */131int ams_sensor_attach(void)132{133int result;134const u32 *prop;135136/* Get orientation */137prop = of_get_property(ams_info.of_node, "orientation", NULL);138if (!prop)139return -ENODEV;140ams_info.orient1 = *prop;141ams_info.orient2 = *(prop + 1);142143/* Register freefall interrupt handler */144result = pmf_register_irq_client(ams_info.of_node,145"accel-int-1",146&ams_freefall_client);147if (result < 0)148return -ENODEV;149150/* Reset saved irqs */151ams_info.worker_irqs = 0;152153/* Register shock interrupt handler */154result = pmf_register_irq_client(ams_info.of_node,155"accel-int-2",156&ams_shock_client);157if (result < 0)158goto release_freefall;159160/* Create device */161ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);162if (!ams_info.of_dev) {163result = -ENODEV;164goto release_shock;165}166167/* Create attributes */168result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);169if (result)170goto release_of;171172ams_info.vflag = !!(ams_info.get_vendor() & 0x10);173174/* Init input device */175result = ams_input_init();176if (result)177goto release_device_file;178179return result;180release_device_file:181device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);182release_of:183of_device_unregister(ams_info.of_dev);184release_shock:185pmf_unregister_irq_client(&ams_shock_client);186release_freefall:187pmf_unregister_irq_client(&ams_freefall_client);188return result;189}190191int __init ams_init(void)192{193struct device_node *np;194195spin_lock_init(&ams_info.irq_lock);196mutex_init(&ams_info.lock);197INIT_WORK(&ams_info.worker, ams_worker);198199#ifdef CONFIG_SENSORS_AMS_I2C200np = of_find_node_by_name(NULL, "accelerometer");201if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))202/* Found I2C motion sensor */203return ams_i2c_init(np);204#endif205206#ifdef CONFIG_SENSORS_AMS_PMU207np = of_find_node_by_name(NULL, "sms");208if (np && of_device_is_compatible(np, "sms"))209/* Found PMU motion sensor */210return ams_pmu_init(np);211#endif212return -ENODEV;213}214215void ams_sensor_detach(void)216{217/* Remove input device */218ams_input_exit();219220/* Remove attributes */221device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);222223/* Flush interrupt worker224*225* We do this after ams_info.exit(), because an interrupt might226* have arrived before disabling them.227*/228flush_work_sync(&ams_info.worker);229230/* Remove device */231of_device_unregister(ams_info.of_dev);232233/* Remove handler */234pmf_unregister_irq_client(&ams_shock_client);235pmf_unregister_irq_client(&ams_freefall_client);236}237238static void __exit ams_exit(void)239{240/* Shut down implementation */241ams_info.exit();242}243244MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");245MODULE_DESCRIPTION("Apple Motion Sensor driver");246MODULE_LICENSE("GPL");247248module_init(ams_init);249module_exit(ams_exit);250251252