1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dell-smo8800.c - Dell Latitude ACPI SMO88XX freefall sensor driver
8 * This is loosely based on lis3lv02d driver.
11 #define DRIVER_NAME "smo8800"
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/acpi.h>
16 #include <linux/interrupt.h>
17 #include <linux/miscdevice.h>
18 #include <linux/uaccess.h>
21 struct smo8800_device {
22 u32 irq; /* acpi device irq */
23 atomic_t counter; /* count after last read */
24 struct miscdevice miscdev; /* for /dev/freefall */
25 unsigned long misc_opened; /* whether the device is open */
26 wait_queue_head_t misc_wait; /* Wait queue for the misc dev */
27 struct device *dev; /* acpi device */
30 static irqreturn_t smo8800_interrupt_quick(int irq, void *data)
32 struct smo8800_device *smo8800 = data;
34 atomic_inc(&smo8800->counter);
35 wake_up_interruptible(&smo8800->misc_wait);
36 return IRQ_WAKE_THREAD;
39 static irqreturn_t smo8800_interrupt_thread(int irq, void *data)
41 struct smo8800_device *smo8800 = data;
43 dev_info(smo8800->dev, "detected free fall\n");
47 static acpi_status smo8800_get_resource(struct acpi_resource *resource,
50 struct acpi_resource_extended_irq *irq;
52 if (resource->type != ACPI_RESOURCE_TYPE_EXTENDED_IRQ)
55 irq = &resource->data.extended_irq;
56 if (!irq || !irq->interrupt_count)
59 *((u32 *)context) = irq->interrupts[0];
60 return AE_CTRL_TERMINATE;
63 static u32 smo8800_get_irq(struct acpi_device *device)
68 status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
69 smo8800_get_resource, &irq);
70 if (ACPI_FAILURE(status)) {
71 dev_err(&device->dev, "acpi_walk_resources failed\n");
78 static ssize_t smo8800_misc_read(struct file *file, char __user *buf,
79 size_t count, loff_t *pos)
81 struct smo8800_device *smo8800 = container_of(file->private_data,
82 struct smo8800_device, miscdev);
85 unsigned char byte_data;
91 atomic_set(&smo8800->counter, 0);
92 retval = wait_event_interruptible(smo8800->misc_wait,
93 (data = atomic_xchg(&smo8800->counter, 0)));
105 if (put_user(byte_data, buf))
111 static int smo8800_misc_open(struct inode *inode, struct file *file)
113 struct smo8800_device *smo8800 = container_of(file->private_data,
114 struct smo8800_device, miscdev);
116 if (test_and_set_bit(0, &smo8800->misc_opened))
117 return -EBUSY; /* already open */
119 atomic_set(&smo8800->counter, 0);
123 static int smo8800_misc_release(struct inode *inode, struct file *file)
125 struct smo8800_device *smo8800 = container_of(file->private_data,
126 struct smo8800_device, miscdev);
128 clear_bit(0, &smo8800->misc_opened); /* release the device */
132 static const struct file_operations smo8800_misc_fops = {
133 .owner = THIS_MODULE,
134 .read = smo8800_misc_read,
135 .open = smo8800_misc_open,
136 .release = smo8800_misc_release,
139 static int smo8800_add(struct acpi_device *device)
142 struct smo8800_device *smo8800;
144 smo8800 = devm_kzalloc(&device->dev, sizeof(*smo8800), GFP_KERNEL);
146 dev_err(&device->dev, "failed to allocate device data\n");
150 smo8800->dev = &device->dev;
151 smo8800->miscdev.minor = MISC_DYNAMIC_MINOR;
152 smo8800->miscdev.name = "freefall";
153 smo8800->miscdev.fops = &smo8800_misc_fops;
155 init_waitqueue_head(&smo8800->misc_wait);
157 err = misc_register(&smo8800->miscdev);
159 dev_err(&device->dev, "failed to register misc dev: %d\n", err);
163 device->driver_data = smo8800;
165 smo8800->irq = smo8800_get_irq(device);
167 dev_err(&device->dev, "failed to obtain IRQ\n");
172 err = request_threaded_irq(smo8800->irq, smo8800_interrupt_quick,
173 smo8800_interrupt_thread,
174 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
175 DRIVER_NAME, smo8800);
177 dev_err(&device->dev,
178 "failed to request thread for IRQ %d: %d\n",
183 dev_dbg(&device->dev, "device /dev/freefall registered with IRQ %d\n",
188 misc_deregister(&smo8800->miscdev);
192 static int smo8800_remove(struct acpi_device *device)
194 struct smo8800_device *smo8800 = device->driver_data;
196 free_irq(smo8800->irq, smo8800);
197 misc_deregister(&smo8800->miscdev);
198 dev_dbg(&device->dev, "device /dev/freefall unregistered\n");
202 /* NOTE: Keep this list in sync with drivers/i2c/busses/i2c-i801.c */
203 static const struct acpi_device_id smo8800_ids[] = {
215 MODULE_DEVICE_TABLE(acpi, smo8800_ids);
217 static struct acpi_driver smo8800_driver = {
223 .remove = smo8800_remove,
225 .owner = THIS_MODULE,
228 module_acpi_driver(smo8800_driver);
230 MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
231 MODULE_LICENSE("GPL");
232 MODULE_AUTHOR("Sonal Santan, Pali Rohár");