1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  dell-smo8800.c - Dell Latitude ACPI SMO88XX freefall sensor driver
4  *
5  *  Copyright (C) 2012 Sonal Santan <sonal.santan@gmail.com>
6  *  Copyright (C) 2014 Pali Rohár <pali@kernel.org>
7  *
8  *  This is loosely based on lis3lv02d driver.
9  */
10 
11 #define DRIVER_NAME "smo8800"
12 
13 #include <linux/fs.h>
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>
21 
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 */
29 };
30 
31 static irqreturn_t smo8800_interrupt_quick(int irq, void *data)
32 {
33 	struct smo8800_device *smo8800 = data;
34 
35 	atomic_inc(&smo8800->counter);
36 	wake_up_interruptible(&smo8800->misc_wait);
37 	return IRQ_WAKE_THREAD;
38 }
39 
40 static irqreturn_t smo8800_interrupt_thread(int irq, void *data)
41 {
42 	struct smo8800_device *smo8800 = data;
43 
44 	dev_info(smo8800->dev, "detected free fall\n");
45 	return IRQ_HANDLED;
46 }
47 
48 static ssize_t smo8800_misc_read(struct file *file, char __user *buf,
49 				 size_t count, loff_t *pos)
50 {
51 	struct smo8800_device *smo8800 = container_of(file->private_data,
52 					 struct smo8800_device, miscdev);
53 
54 	u32 data = 0;
55 	unsigned char byte_data;
56 	ssize_t retval = 1;
57 
58 	if (count < 1)
59 		return -EINVAL;
60 
61 	atomic_set(&smo8800->counter, 0);
62 	retval = wait_event_interruptible(smo8800->misc_wait,
63 				(data = atomic_xchg(&smo8800->counter, 0)));
64 
65 	if (retval)
66 		return retval;
67 
68 	retval = 1;
69 
70 	byte_data = min_t(u32, data, 255);
71 
72 	if (put_user(byte_data, buf))
73 		retval = -EFAULT;
74 
75 	return retval;
76 }
77 
78 static int smo8800_misc_open(struct inode *inode, struct file *file)
79 {
80 	struct smo8800_device *smo8800 = container_of(file->private_data,
81 					 struct smo8800_device, miscdev);
82 
83 	if (test_and_set_bit(0, &smo8800->misc_opened))
84 		return -EBUSY; /* already open */
85 
86 	atomic_set(&smo8800->counter, 0);
87 	return 0;
88 }
89 
90 static int smo8800_misc_release(struct inode *inode, struct file *file)
91 {
92 	struct smo8800_device *smo8800 = container_of(file->private_data,
93 					 struct smo8800_device, miscdev);
94 
95 	clear_bit(0, &smo8800->misc_opened); /* release the device */
96 	return 0;
97 }
98 
99 static const struct file_operations smo8800_misc_fops = {
100 	.owner = THIS_MODULE,
101 	.read = smo8800_misc_read,
102 	.open = smo8800_misc_open,
103 	.release = smo8800_misc_release,
104 };
105 
106 static int smo8800_probe(struct platform_device *device)
107 {
108 	int err;
109 	struct smo8800_device *smo8800;
110 
111 	smo8800 = devm_kzalloc(&device->dev, sizeof(*smo8800), GFP_KERNEL);
112 	if (!smo8800) {
113 		dev_err(&device->dev, "failed to allocate device data\n");
114 		return -ENOMEM;
115 	}
116 
117 	smo8800->dev = &device->dev;
118 	smo8800->miscdev.minor = MISC_DYNAMIC_MINOR;
119 	smo8800->miscdev.name = "freefall";
120 	smo8800->miscdev.fops = &smo8800_misc_fops;
121 
122 	init_waitqueue_head(&smo8800->misc_wait);
123 
124 	err = misc_register(&smo8800->miscdev);
125 	if (err) {
126 		dev_err(&device->dev, "failed to register misc dev: %d\n", err);
127 		return err;
128 	}
129 
130 	platform_set_drvdata(device, smo8800);
131 
132 	err = platform_get_irq(device, 0);
133 	if (err < 0)
134 		goto error;
135 	smo8800->irq = err;
136 
137 	err = request_threaded_irq(smo8800->irq, smo8800_interrupt_quick,
138 				   smo8800_interrupt_thread,
139 				   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
140 				   DRIVER_NAME, smo8800);
141 	if (err) {
142 		dev_err(&device->dev,
143 			"failed to request thread for IRQ %d: %d\n",
144 			smo8800->irq, err);
145 		goto error;
146 	}
147 
148 	dev_dbg(&device->dev, "device /dev/freefall registered with IRQ %d\n",
149 		 smo8800->irq);
150 	return 0;
151 
152 error:
153 	misc_deregister(&smo8800->miscdev);
154 	return err;
155 }
156 
157 static void smo8800_remove(struct platform_device *device)
158 {
159 	struct smo8800_device *smo8800 = platform_get_drvdata(device);
160 
161 	free_irq(smo8800->irq, smo8800);
162 	misc_deregister(&smo8800->miscdev);
163 	dev_dbg(&device->dev, "device /dev/freefall unregistered\n");
164 }
165 
166 /* NOTE: Keep this list in sync with drivers/i2c/busses/i2c-i801.c */
167 static const struct acpi_device_id smo8800_ids[] = {
168 	{ "SMO8800", 0 },
169 	{ "SMO8801", 0 },
170 	{ "SMO8810", 0 },
171 	{ "SMO8811", 0 },
172 	{ "SMO8820", 0 },
173 	{ "SMO8821", 0 },
174 	{ "SMO8830", 0 },
175 	{ "SMO8831", 0 },
176 	{ "", 0 },
177 };
178 MODULE_DEVICE_TABLE(acpi, smo8800_ids);
179 
180 static struct platform_driver smo8800_driver = {
181 	.probe = smo8800_probe,
182 	.remove_new = smo8800_remove,
183 	.driver = {
184 		.name = DRIVER_NAME,
185 		.acpi_match_table = smo8800_ids,
186 	},
187 };
188 module_platform_driver(smo8800_driver);
189 
190 MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
191 MODULE_LICENSE("GPL");
192 MODULE_AUTHOR("Sonal Santan, Pali Rohár");
193