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 	if (data < 255)
71 		byte_data = data;
72 	else
73 		byte_data = 255;
74 
75 	if (put_user(byte_data, buf))
76 		retval = -EFAULT;
77 
78 	return retval;
79 }
80 
81 static int smo8800_misc_open(struct inode *inode, struct file *file)
82 {
83 	struct smo8800_device *smo8800 = container_of(file->private_data,
84 					 struct smo8800_device, miscdev);
85 
86 	if (test_and_set_bit(0, &smo8800->misc_opened))
87 		return -EBUSY; /* already open */
88 
89 	atomic_set(&smo8800->counter, 0);
90 	return 0;
91 }
92 
93 static int smo8800_misc_release(struct inode *inode, struct file *file)
94 {
95 	struct smo8800_device *smo8800 = container_of(file->private_data,
96 					 struct smo8800_device, miscdev);
97 
98 	clear_bit(0, &smo8800->misc_opened); /* release the device */
99 	return 0;
100 }
101 
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,
107 };
108 
109 static int smo8800_probe(struct platform_device *device)
110 {
111 	int err;
112 	struct smo8800_device *smo8800;
113 
114 	smo8800 = devm_kzalloc(&device->dev, sizeof(*smo8800), GFP_KERNEL);
115 	if (!smo8800) {
116 		dev_err(&device->dev, "failed to allocate device data\n");
117 		return -ENOMEM;
118 	}
119 
120 	smo8800->dev = &device->dev;
121 	smo8800->miscdev.minor = MISC_DYNAMIC_MINOR;
122 	smo8800->miscdev.name = "freefall";
123 	smo8800->miscdev.fops = &smo8800_misc_fops;
124 
125 	init_waitqueue_head(&smo8800->misc_wait);
126 
127 	err = misc_register(&smo8800->miscdev);
128 	if (err) {
129 		dev_err(&device->dev, "failed to register misc dev: %d\n", err);
130 		return err;
131 	}
132 
133 	platform_set_drvdata(device, smo8800);
134 
135 	err = platform_get_irq(device, 0);
136 	if (err < 0)
137 		goto error;
138 	smo8800->irq = err;
139 
140 	err = request_threaded_irq(smo8800->irq, smo8800_interrupt_quick,
141 				   smo8800_interrupt_thread,
142 				   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
143 				   DRIVER_NAME, smo8800);
144 	if (err) {
145 		dev_err(&device->dev,
146 			"failed to request thread for IRQ %d: %d\n",
147 			smo8800->irq, err);
148 		goto error;
149 	}
150 
151 	dev_dbg(&device->dev, "device /dev/freefall registered with IRQ %d\n",
152 		 smo8800->irq);
153 	return 0;
154 
155 error:
156 	misc_deregister(&smo8800->miscdev);
157 	return err;
158 }
159 
160 static int smo8800_remove(struct platform_device *device)
161 {
162 	struct smo8800_device *smo8800 = platform_get_drvdata(device);
163 
164 	free_irq(smo8800->irq, smo8800);
165 	misc_deregister(&smo8800->miscdev);
166 	dev_dbg(&device->dev, "device /dev/freefall unregistered\n");
167 	return 0;
168 }
169 
170 /* NOTE: Keep this list in sync with drivers/i2c/busses/i2c-i801.c */
171 static const struct acpi_device_id smo8800_ids[] = {
172 	{ "SMO8800", 0 },
173 	{ "SMO8801", 0 },
174 	{ "SMO8810", 0 },
175 	{ "SMO8811", 0 },
176 	{ "SMO8820", 0 },
177 	{ "SMO8821", 0 },
178 	{ "SMO8830", 0 },
179 	{ "SMO8831", 0 },
180 	{ "", 0 },
181 };
182 MODULE_DEVICE_TABLE(acpi, smo8800_ids);
183 
184 static struct platform_driver smo8800_driver = {
185 	.probe = smo8800_probe,
186 	.remove = smo8800_remove,
187 	.driver = {
188 		.name = DRIVER_NAME,
189 		.acpi_match_table = smo8800_ids,
190 	},
191 };
192 module_platform_driver(smo8800_driver);
193 
194 MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
195 MODULE_LICENSE("GPL");
196 MODULE_AUTHOR("Sonal Santan, Pali Rohár");
197