1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dell-smo8800.c - Dell Latitude ACPI SMO88XX freefall sensor driver
5 * Copyright (C) 2012 Sonal Santan <sonal.santan@gmail.com>
6 * Copyright (C) 2014 Pali Rohár <pali@kernel.org>
8 * This is loosely based on lis3lv02d driver.
11 #define DRIVER_NAME "smo8800"
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/miscdevice.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/uaccess.h>
22 struct smo8800_device {
23 u32 irq; /* acpi device irq */
24 atomic_t counter; /* count after last read */
25 struct miscdevice miscdev; /* for /dev/freefall */
26 unsigned long misc_opened; /* whether the device is open */
27 wait_queue_head_t misc_wait; /* Wait queue for the misc dev */
28 struct device *dev; /* acpi device */
31 static irqreturn_t smo8800_interrupt_quick(int irq, void *data)
33 struct smo8800_device *smo8800 = data;
35 atomic_inc(&smo8800->counter);
36 wake_up_interruptible(&smo8800->misc_wait);
37 return IRQ_WAKE_THREAD;
40 static irqreturn_t smo8800_interrupt_thread(int irq, void *data)
42 struct smo8800_device *smo8800 = data;
44 dev_info(smo8800->dev, "detected free fall\n");
48 static ssize_t smo8800_misc_read(struct file *file, char __user *buf,
49 size_t count, loff_t *pos)
51 struct smo8800_device *smo8800 = container_of(file->private_data,
52 struct smo8800_device, miscdev);
55 unsigned char byte_data;
61 atomic_set(&smo8800->counter, 0);
62 retval = wait_event_interruptible(smo8800->misc_wait,
63 (data = atomic_xchg(&smo8800->counter, 0)));
75 if (put_user(byte_data, buf))
81 static int smo8800_misc_open(struct inode *inode, struct file *file)
83 struct smo8800_device *smo8800 = container_of(file->private_data,
84 struct smo8800_device, miscdev);
86 if (test_and_set_bit(0, &smo8800->misc_opened))
87 return -EBUSY; /* already open */
89 atomic_set(&smo8800->counter, 0);
93 static int smo8800_misc_release(struct inode *inode, struct file *file)
95 struct smo8800_device *smo8800 = container_of(file->private_data,
96 struct smo8800_device, miscdev);
98 clear_bit(0, &smo8800->misc_opened); /* release the device */
102 static const struct file_operations smo8800_misc_fops = {
103 .owner = THIS_MODULE,
104 .read = smo8800_misc_read,
105 .open = smo8800_misc_open,
106 .release = smo8800_misc_release,
109 static int smo8800_probe(struct platform_device *device)
112 struct smo8800_device *smo8800;
114 smo8800 = devm_kzalloc(&device->dev, sizeof(*smo8800), GFP_KERNEL);
116 dev_err(&device->dev, "failed to allocate device data\n");
120 smo8800->dev = &device->dev;
121 smo8800->miscdev.minor = MISC_DYNAMIC_MINOR;
122 smo8800->miscdev.name = "freefall";
123 smo8800->miscdev.fops = &smo8800_misc_fops;
125 init_waitqueue_head(&smo8800->misc_wait);
127 err = misc_register(&smo8800->miscdev);
129 dev_err(&device->dev, "failed to register misc dev: %d\n", err);
133 platform_set_drvdata(device, smo8800);
135 err = platform_get_irq(device, 0);
140 err = request_threaded_irq(smo8800->irq, smo8800_interrupt_quick,
141 smo8800_interrupt_thread,
142 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
143 DRIVER_NAME, smo8800);
145 dev_err(&device->dev,
146 "failed to request thread for IRQ %d: %d\n",
151 dev_dbg(&device->dev, "device /dev/freefall registered with IRQ %d\n",
156 misc_deregister(&smo8800->miscdev);
160 static int smo8800_remove(struct platform_device *device)
162 struct smo8800_device *smo8800 = platform_get_drvdata(device);
164 free_irq(smo8800->irq, smo8800);
165 misc_deregister(&smo8800->miscdev);
166 dev_dbg(&device->dev, "device /dev/freefall unregistered\n");
170 /* NOTE: Keep this list in sync with drivers/i2c/busses/i2c-i801.c */
171 static const struct acpi_device_id smo8800_ids[] = {
182 MODULE_DEVICE_TABLE(acpi, smo8800_ids);
184 static struct platform_driver smo8800_driver = {
185 .probe = smo8800_probe,
186 .remove = smo8800_remove,
189 .acpi_match_table = smo8800_ids,
192 module_platform_driver(smo8800_driver);
194 MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
195 MODULE_LICENSE("GPL");
196 MODULE_AUTHOR("Sonal Santan, Pali Rohár");