dell-smo8800.c 5.4 KB

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