dell-smo8800.c 4.7 KB

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