dell-smo8800.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * dell-smo8800.c - Dell Latitude ACPI SMO88XX freefall sensor driver
  3. *
  4. * Copyright (C) 2012 Sonal Santan <sonal.santan@gmail.com>
  5. * Copyright (C) 2014 Pali Rohár <pali.rohar@gmail.com>
  6. *
  7. * This is loosely based on lis3lv02d driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define DRIVER_NAME "smo8800"
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/acpi.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/uaccess.h>
  26. struct smo8800_device {
  27. u32 irq; /* acpi device irq */
  28. atomic_t counter; /* count after last read */
  29. struct miscdevice miscdev; /* for /dev/freefall */
  30. unsigned long misc_opened; /* whether the device is open */
  31. wait_queue_head_t misc_wait; /* Wait queue for the misc dev */
  32. struct device *dev; /* acpi device */
  33. };
  34. static irqreturn_t smo8800_interrupt_quick(int irq, void *data)
  35. {
  36. struct smo8800_device *smo8800 = data;
  37. atomic_inc(&smo8800->counter);
  38. wake_up_interruptible(&smo8800->misc_wait);
  39. return IRQ_WAKE_THREAD;
  40. }
  41. static irqreturn_t smo8800_interrupt_thread(int irq, void *data)
  42. {
  43. struct smo8800_device *smo8800 = data;
  44. dev_info(smo8800->dev, "detected free fall\n");
  45. return IRQ_HANDLED;
  46. }
  47. static acpi_status smo8800_get_resource(struct acpi_resource *resource,
  48. void *context)
  49. {
  50. struct acpi_resource_extended_irq *irq;
  51. if (resource->type != ACPI_RESOURCE_TYPE_EXTENDED_IRQ)
  52. return AE_OK;
  53. irq = &resource->data.extended_irq;
  54. if (!irq || !irq->interrupt_count)
  55. return AE_OK;
  56. *((u32 *)context) = irq->interrupts[0];
  57. return AE_CTRL_TERMINATE;
  58. }
  59. static u32 smo8800_get_irq(struct acpi_device *device)
  60. {
  61. u32 irq = 0;
  62. acpi_status status;
  63. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  64. smo8800_get_resource, &irq);
  65. if (ACPI_FAILURE(status)) {
  66. dev_err(&device->dev, "acpi_walk_resources failed\n");
  67. return 0;
  68. }
  69. return irq;
  70. }
  71. static ssize_t smo8800_misc_read(struct file *file, char __user *buf,
  72. size_t count, loff_t *pos)
  73. {
  74. struct smo8800_device *smo8800 = container_of(file->private_data,
  75. struct smo8800_device, miscdev);
  76. u32 data = 0;
  77. unsigned char byte_data = 0;
  78. ssize_t retval = 1;
  79. if (count < 1)
  80. return -EINVAL;
  81. atomic_set(&smo8800->counter, 0);
  82. retval = wait_event_interruptible(smo8800->misc_wait,
  83. (data = atomic_xchg(&smo8800->counter, 0)));
  84. if (retval)
  85. return retval;
  86. byte_data = 1;
  87. retval = 1;
  88. if (data < 255)
  89. byte_data = data;
  90. else
  91. byte_data = 255;
  92. if (put_user(byte_data, buf))
  93. retval = -EFAULT;
  94. return retval;
  95. }
  96. static int smo8800_misc_open(struct inode *inode, struct file *file)
  97. {
  98. struct smo8800_device *smo8800 = container_of(file->private_data,
  99. struct smo8800_device, miscdev);
  100. if (test_and_set_bit(0, &smo8800->misc_opened))
  101. return -EBUSY; /* already open */
  102. atomic_set(&smo8800->counter, 0);
  103. return 0;
  104. }
  105. static int smo8800_misc_release(struct inode *inode, struct file *file)
  106. {
  107. struct smo8800_device *smo8800 = container_of(file->private_data,
  108. struct smo8800_device, miscdev);
  109. clear_bit(0, &smo8800->misc_opened); /* release the device */
  110. return 0;
  111. }
  112. static const struct file_operations smo8800_misc_fops = {
  113. .owner = THIS_MODULE,
  114. .read = smo8800_misc_read,
  115. .open = smo8800_misc_open,
  116. .release = smo8800_misc_release,
  117. };
  118. static int smo8800_add(struct acpi_device *device)
  119. {
  120. int err;
  121. struct smo8800_device *smo8800;
  122. smo8800 = devm_kzalloc(&device->dev, sizeof(*smo8800), GFP_KERNEL);
  123. if (!smo8800) {
  124. dev_err(&device->dev, "failed to allocate device data\n");
  125. return -ENOMEM;
  126. }
  127. smo8800->dev = &device->dev;
  128. smo8800->miscdev.minor = MISC_DYNAMIC_MINOR;
  129. smo8800->miscdev.name = "freefall";
  130. smo8800->miscdev.fops = &smo8800_misc_fops;
  131. init_waitqueue_head(&smo8800->misc_wait);
  132. err = misc_register(&smo8800->miscdev);
  133. if (err) {
  134. dev_err(&device->dev, "failed to register misc dev: %d\n", err);
  135. return err;
  136. }
  137. device->driver_data = smo8800;
  138. smo8800->irq = smo8800_get_irq(device);
  139. if (!smo8800->irq) {
  140. dev_err(&device->dev, "failed to obtain IRQ\n");
  141. err = -EINVAL;
  142. goto error;
  143. }
  144. err = request_threaded_irq(smo8800->irq, smo8800_interrupt_quick,
  145. smo8800_interrupt_thread,
  146. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  147. DRIVER_NAME, smo8800);
  148. if (err) {
  149. dev_err(&device->dev,
  150. "failed to request thread for IRQ %d: %d\n",
  151. smo8800->irq, err);
  152. goto error;
  153. }
  154. dev_dbg(&device->dev, "device /dev/freefall registered with IRQ %d\n",
  155. smo8800->irq);
  156. return 0;
  157. error:
  158. misc_deregister(&smo8800->miscdev);
  159. return err;
  160. }
  161. static int smo8800_remove(struct acpi_device *device)
  162. {
  163. struct smo8800_device *smo8800 = device->driver_data;
  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. static const struct acpi_device_id smo8800_ids[] = {
  170. { "SMO8800", 0 },
  171. { "SMO8801", 0 },
  172. { "SMO8810", 0 },
  173. { "SMO8811", 0 },
  174. { "SMO8820", 0 },
  175. { "SMO8821", 0 },
  176. { "SMO8830", 0 },
  177. { "SMO8831", 0 },
  178. { "", 0 },
  179. };
  180. MODULE_DEVICE_TABLE(acpi, smo8800_ids);
  181. static struct acpi_driver smo8800_driver = {
  182. .name = DRIVER_NAME,
  183. .class = "Latitude",
  184. .ids = smo8800_ids,
  185. .ops = {
  186. .add = smo8800_add,
  187. .remove = smo8800_remove,
  188. },
  189. .owner = THIS_MODULE,
  190. };
  191. module_acpi_driver(smo8800_driver);
  192. MODULE_DESCRIPTION("Dell Latitude freefall driver (ACPI SMO88XX)");
  193. MODULE_LICENSE("GPL");
  194. MODULE_AUTHOR("Sonal Santan, Pali Rohár");