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.rohar@gmail.com>
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>
20 struct smo8800_device
{
21 u32 irq
; /* acpi device irq */
22 atomic_t counter
; /* count after last read */
23 struct miscdevice miscdev
; /* for /dev/freefall */
24 unsigned long misc_opened
; /* whether the device is open */
25 wait_queue_head_t misc_wait
; /* Wait queue for the misc dev */
26 struct device
*dev
; /* acpi device */
29 static irqreturn_t
smo8800_interrupt_quick(int irq
, void *data
)
31 struct smo8800_device
*smo8800
= data
;
33 atomic_inc(&smo8800
->counter
);
34 wake_up_interruptible(&smo8800
->misc_wait
);
35 return IRQ_WAKE_THREAD
;
38 static irqreturn_t
smo8800_interrupt_thread(int irq
, void *data
)
40 struct smo8800_device
*smo8800
= data
;
42 dev_info(smo8800
->dev
, "detected free fall\n");
46 static acpi_status
smo8800_get_resource(struct acpi_resource
*resource
,
49 struct acpi_resource_extended_irq
*irq
;
51 if (resource
->type
!= ACPI_RESOURCE_TYPE_EXTENDED_IRQ
)
54 irq
= &resource
->data
.extended_irq
;
55 if (!irq
|| !irq
->interrupt_count
)
58 *((u32
*)context
) = irq
->interrupts
[0];
59 return AE_CTRL_TERMINATE
;
62 static u32
smo8800_get_irq(struct acpi_device
*device
)
67 status
= acpi_walk_resources(device
->handle
, METHOD_NAME__CRS
,
68 smo8800_get_resource
, &irq
);
69 if (ACPI_FAILURE(status
)) {
70 dev_err(&device
->dev
, "acpi_walk_resources failed\n");
77 static ssize_t
smo8800_misc_read(struct file
*file
, char __user
*buf
,
78 size_t count
, loff_t
*pos
)
80 struct smo8800_device
*smo8800
= container_of(file
->private_data
,
81 struct smo8800_device
, miscdev
);
84 unsigned char byte_data
;
90 atomic_set(&smo8800
->counter
, 0);
91 retval
= wait_event_interruptible(smo8800
->misc_wait
,
92 (data
= atomic_xchg(&smo8800
->counter
, 0)));
104 if (put_user(byte_data
, buf
))
110 static int smo8800_misc_open(struct inode
*inode
, struct file
*file
)
112 struct smo8800_device
*smo8800
= container_of(file
->private_data
,
113 struct smo8800_device
, miscdev
);
115 if (test_and_set_bit(0, &smo8800
->misc_opened
))
116 return -EBUSY
; /* already open */
118 atomic_set(&smo8800
->counter
, 0);
122 static int smo8800_misc_release(struct inode
*inode
, struct file
*file
)
124 struct smo8800_device
*smo8800
= container_of(file
->private_data
,
125 struct smo8800_device
, miscdev
);
127 clear_bit(0, &smo8800
->misc_opened
); /* release the device */
131 static const struct file_operations smo8800_misc_fops
= {
132 .owner
= THIS_MODULE
,
133 .read
= smo8800_misc_read
,
134 .open
= smo8800_misc_open
,
135 .release
= smo8800_misc_release
,
138 static int smo8800_add(struct acpi_device
*device
)
141 struct smo8800_device
*smo8800
;
143 smo8800
= devm_kzalloc(&device
->dev
, sizeof(*smo8800
), GFP_KERNEL
);
145 dev_err(&device
->dev
, "failed to allocate device data\n");
149 smo8800
->dev
= &device
->dev
;
150 smo8800
->miscdev
.minor
= MISC_DYNAMIC_MINOR
;
151 smo8800
->miscdev
.name
= "freefall";
152 smo8800
->miscdev
.fops
= &smo8800_misc_fops
;
154 init_waitqueue_head(&smo8800
->misc_wait
);
156 err
= misc_register(&smo8800
->miscdev
);
158 dev_err(&device
->dev
, "failed to register misc dev: %d\n", err
);
162 device
->driver_data
= smo8800
;
164 smo8800
->irq
= smo8800_get_irq(device
);
166 dev_err(&device
->dev
, "failed to obtain IRQ\n");
171 err
= request_threaded_irq(smo8800
->irq
, smo8800_interrupt_quick
,
172 smo8800_interrupt_thread
,
173 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
174 DRIVER_NAME
, smo8800
);
176 dev_err(&device
->dev
,
177 "failed to request thread for IRQ %d: %d\n",
182 dev_dbg(&device
->dev
, "device /dev/freefall registered with IRQ %d\n",
187 misc_deregister(&smo8800
->miscdev
);
191 static int smo8800_remove(struct acpi_device
*device
)
193 struct smo8800_device
*smo8800
= device
->driver_data
;
195 free_irq(smo8800
->irq
, smo8800
);
196 misc_deregister(&smo8800
->miscdev
);
197 dev_dbg(&device
->dev
, "device /dev/freefall unregistered\n");
201 /* NOTE: Keep this list in sync with drivers/i2c/busses/i2c-i801.c */
202 static const struct acpi_device_id smo8800_ids
[] = {
214 MODULE_DEVICE_TABLE(acpi
, smo8800_ids
);
216 static struct acpi_driver smo8800_driver
= {
222 .remove
= smo8800_remove
,
224 .owner
= THIS_MODULE
,
227 module_acpi_driver(smo8800_driver
);
229 MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
230 MODULE_LICENSE("GPL");
231 MODULE_AUTHOR("Sonal Santan, Pali Rohár");