Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  dell-smo8800.c - Dell Latitude ACPI SMO88XX freefall sensor driver
0004  *
0005  *  Copyright (C) 2012 Sonal Santan <sonal.santan@gmail.com>
0006  *  Copyright (C) 2014 Pali Rohár <pali@kernel.org>
0007  *
0008  *  This is loosely based on lis3lv02d driver.
0009  */
0010 
0011 #define DRIVER_NAME "smo8800"
0012 
0013 #include <linux/fs.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/kernel.h>
0016 #include <linux/miscdevice.h>
0017 #include <linux/mod_devicetable.h>
0018 #include <linux/module.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/uaccess.h>
0021 
0022 struct smo8800_device {
0023     u32 irq;                     /* acpi device irq */
0024     atomic_t counter;            /* count after last read */
0025     struct miscdevice miscdev;   /* for /dev/freefall */
0026     unsigned long misc_opened;   /* whether the device is open */
0027     wait_queue_head_t misc_wait; /* Wait queue for the misc dev */
0028     struct device *dev;          /* acpi device */
0029 };
0030 
0031 static irqreturn_t smo8800_interrupt_quick(int irq, void *data)
0032 {
0033     struct smo8800_device *smo8800 = data;
0034 
0035     atomic_inc(&smo8800->counter);
0036     wake_up_interruptible(&smo8800->misc_wait);
0037     return IRQ_WAKE_THREAD;
0038 }
0039 
0040 static irqreturn_t smo8800_interrupt_thread(int irq, void *data)
0041 {
0042     struct smo8800_device *smo8800 = data;
0043 
0044     dev_info(smo8800->dev, "detected free fall\n");
0045     return IRQ_HANDLED;
0046 }
0047 
0048 static ssize_t smo8800_misc_read(struct file *file, char __user *buf,
0049                  size_t count, loff_t *pos)
0050 {
0051     struct smo8800_device *smo8800 = container_of(file->private_data,
0052                      struct smo8800_device, miscdev);
0053 
0054     u32 data = 0;
0055     unsigned char byte_data;
0056     ssize_t retval = 1;
0057 
0058     if (count < 1)
0059         return -EINVAL;
0060 
0061     atomic_set(&smo8800->counter, 0);
0062     retval = wait_event_interruptible(smo8800->misc_wait,
0063                 (data = atomic_xchg(&smo8800->counter, 0)));
0064 
0065     if (retval)
0066         return retval;
0067 
0068     retval = 1;
0069 
0070     if (data < 255)
0071         byte_data = data;
0072     else
0073         byte_data = 255;
0074 
0075     if (put_user(byte_data, buf))
0076         retval = -EFAULT;
0077 
0078     return retval;
0079 }
0080 
0081 static int smo8800_misc_open(struct inode *inode, struct file *file)
0082 {
0083     struct smo8800_device *smo8800 = container_of(file->private_data,
0084                      struct smo8800_device, miscdev);
0085 
0086     if (test_and_set_bit(0, &smo8800->misc_opened))
0087         return -EBUSY; /* already open */
0088 
0089     atomic_set(&smo8800->counter, 0);
0090     return 0;
0091 }
0092 
0093 static int smo8800_misc_release(struct inode *inode, struct file *file)
0094 {
0095     struct smo8800_device *smo8800 = container_of(file->private_data,
0096                      struct smo8800_device, miscdev);
0097 
0098     clear_bit(0, &smo8800->misc_opened); /* release the device */
0099     return 0;
0100 }
0101 
0102 static const struct file_operations smo8800_misc_fops = {
0103     .owner = THIS_MODULE,
0104     .read = smo8800_misc_read,
0105     .open = smo8800_misc_open,
0106     .release = smo8800_misc_release,
0107 };
0108 
0109 static int smo8800_probe(struct platform_device *device)
0110 {
0111     int err;
0112     struct smo8800_device *smo8800;
0113 
0114     smo8800 = devm_kzalloc(&device->dev, sizeof(*smo8800), GFP_KERNEL);
0115     if (!smo8800) {
0116         dev_err(&device->dev, "failed to allocate device data\n");
0117         return -ENOMEM;
0118     }
0119 
0120     smo8800->dev = &device->dev;
0121     smo8800->miscdev.minor = MISC_DYNAMIC_MINOR;
0122     smo8800->miscdev.name = "freefall";
0123     smo8800->miscdev.fops = &smo8800_misc_fops;
0124 
0125     init_waitqueue_head(&smo8800->misc_wait);
0126 
0127     err = misc_register(&smo8800->miscdev);
0128     if (err) {
0129         dev_err(&device->dev, "failed to register misc dev: %d\n", err);
0130         return err;
0131     }
0132 
0133     platform_set_drvdata(device, smo8800);
0134 
0135     err = platform_get_irq(device, 0);
0136     if (err < 0)
0137         goto error;
0138     smo8800->irq = err;
0139 
0140     err = request_threaded_irq(smo8800->irq, smo8800_interrupt_quick,
0141                    smo8800_interrupt_thread,
0142                    IRQF_TRIGGER_RISING | IRQF_ONESHOT,
0143                    DRIVER_NAME, smo8800);
0144     if (err) {
0145         dev_err(&device->dev,
0146             "failed to request thread for IRQ %d: %d\n",
0147             smo8800->irq, err);
0148         goto error;
0149     }
0150 
0151     dev_dbg(&device->dev, "device /dev/freefall registered with IRQ %d\n",
0152          smo8800->irq);
0153     return 0;
0154 
0155 error:
0156     misc_deregister(&smo8800->miscdev);
0157     return err;
0158 }
0159 
0160 static int smo8800_remove(struct platform_device *device)
0161 {
0162     struct smo8800_device *smo8800 = platform_get_drvdata(device);
0163 
0164     free_irq(smo8800->irq, smo8800);
0165     misc_deregister(&smo8800->miscdev);
0166     dev_dbg(&device->dev, "device /dev/freefall unregistered\n");
0167     return 0;
0168 }
0169 
0170 /* NOTE: Keep this list in sync with drivers/i2c/busses/i2c-i801.c */
0171 static const struct acpi_device_id smo8800_ids[] = {
0172     { "SMO8800", 0 },
0173     { "SMO8801", 0 },
0174     { "SMO8810", 0 },
0175     { "SMO8811", 0 },
0176     { "SMO8820", 0 },
0177     { "SMO8821", 0 },
0178     { "SMO8830", 0 },
0179     { "SMO8831", 0 },
0180     { "", 0 },
0181 };
0182 MODULE_DEVICE_TABLE(acpi, smo8800_ids);
0183 
0184 static struct platform_driver smo8800_driver = {
0185     .probe = smo8800_probe,
0186     .remove = smo8800_remove,
0187     .driver = {
0188         .name = DRIVER_NAME,
0189         .acpi_match_table = smo8800_ids,
0190     },
0191 };
0192 module_platform_driver(smo8800_driver);
0193 
0194 MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
0195 MODULE_LICENSE("GPL");
0196 MODULE_AUTHOR("Sonal Santan, Pali Rohár");