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