ams-core.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Apple Motion Sensor driver
  3. *
  4. * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
  5. * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/of_platform.h>
  26. #include <asm/pmac_pfunc.h>
  27. #include "ams.h"
  28. /* There is only one motion sensor per machine */
  29. struct ams ams_info;
  30. static unsigned int verbose;
  31. module_param(verbose, bool, 0644);
  32. MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
  33. /* Call with ams_info.lock held! */
  34. void ams_sensors(s8 *x, s8 *y, s8 *z)
  35. {
  36. u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
  37. if (orient & 0x80)
  38. /* X and Y swapped */
  39. ams_info.get_xyz(y, x, z);
  40. else
  41. ams_info.get_xyz(x, y, z);
  42. if (orient & 0x04)
  43. *z = ~(*z);
  44. if (orient & 0x02)
  45. *y = ~(*y);
  46. if (orient & 0x01)
  47. *x = ~(*x);
  48. }
  49. static ssize_t ams_show_current(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. s8 x, y, z;
  53. mutex_lock(&ams_info.lock);
  54. ams_sensors(&x, &y, &z);
  55. mutex_unlock(&ams_info.lock);
  56. return snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);
  57. }
  58. static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
  59. static void ams_handle_irq(void *data)
  60. {
  61. enum ams_irq irq = *((enum ams_irq *)data);
  62. spin_lock(&ams_info.irq_lock);
  63. ams_info.worker_irqs |= irq;
  64. schedule_work(&ams_info.worker);
  65. spin_unlock(&ams_info.irq_lock);
  66. }
  67. static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
  68. static struct pmf_irq_client ams_freefall_client = {
  69. .owner = THIS_MODULE,
  70. .handler = ams_handle_irq,
  71. .data = &ams_freefall_irq_data,
  72. };
  73. static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
  74. static struct pmf_irq_client ams_shock_client = {
  75. .owner = THIS_MODULE,
  76. .handler = ams_handle_irq,
  77. .data = &ams_shock_irq_data,
  78. };
  79. /* Once hard disk parking is implemented in the kernel, this function can
  80. * trigger it.
  81. */
  82. static void ams_worker(struct work_struct *work)
  83. {
  84. unsigned long flags;
  85. u8 irqs_to_clear;
  86. mutex_lock(&ams_info.lock);
  87. spin_lock_irqsave(&ams_info.irq_lock, flags);
  88. irqs_to_clear = ams_info.worker_irqs;
  89. if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
  90. if (verbose)
  91. printk(KERN_INFO "ams: freefall detected!\n");
  92. ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
  93. }
  94. if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
  95. if (verbose)
  96. printk(KERN_INFO "ams: shock detected!\n");
  97. ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
  98. }
  99. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  100. ams_info.clear_irq(irqs_to_clear);
  101. mutex_unlock(&ams_info.lock);
  102. }
  103. /* Call with ams_info.lock held! */
  104. int ams_sensor_attach(void)
  105. {
  106. int result;
  107. const u32 *prop;
  108. /* Get orientation */
  109. prop = of_get_property(ams_info.of_node, "orientation", NULL);
  110. if (!prop)
  111. return -ENODEV;
  112. ams_info.orient1 = *prop;
  113. ams_info.orient2 = *(prop + 1);
  114. /* Register freefall interrupt handler */
  115. result = pmf_register_irq_client(ams_info.of_node,
  116. "accel-int-1",
  117. &ams_freefall_client);
  118. if (result < 0)
  119. return -ENODEV;
  120. /* Reset saved irqs */
  121. ams_info.worker_irqs = 0;
  122. /* Register shock interrupt handler */
  123. result = pmf_register_irq_client(ams_info.of_node,
  124. "accel-int-2",
  125. &ams_shock_client);
  126. if (result < 0)
  127. goto release_freefall;
  128. /* Create device */
  129. ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
  130. if (!ams_info.of_dev) {
  131. result = -ENODEV;
  132. goto release_shock;
  133. }
  134. /* Create attributes */
  135. result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
  136. if (result)
  137. goto release_of;
  138. ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
  139. /* Init input device */
  140. result = ams_input_init();
  141. if (result)
  142. goto release_device_file;
  143. return result;
  144. release_device_file:
  145. device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  146. release_of:
  147. of_device_unregister(ams_info.of_dev);
  148. release_shock:
  149. pmf_unregister_irq_client(&ams_shock_client);
  150. release_freefall:
  151. pmf_unregister_irq_client(&ams_freefall_client);
  152. return result;
  153. }
  154. int __init ams_init(void)
  155. {
  156. struct device_node *np;
  157. spin_lock_init(&ams_info.irq_lock);
  158. mutex_init(&ams_info.lock);
  159. INIT_WORK(&ams_info.worker, ams_worker);
  160. #ifdef CONFIG_SENSORS_AMS_I2C
  161. np = of_find_node_by_name(NULL, "accelerometer");
  162. if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
  163. /* Found I2C motion sensor */
  164. return ams_i2c_init(np);
  165. #endif
  166. #ifdef CONFIG_SENSORS_AMS_PMU
  167. np = of_find_node_by_name(NULL, "sms");
  168. if (np && of_device_is_compatible(np, "sms"))
  169. /* Found PMU motion sensor */
  170. return ams_pmu_init(np);
  171. #endif
  172. return -ENODEV;
  173. }
  174. void ams_sensor_detach(void)
  175. {
  176. /* Remove input device */
  177. ams_input_exit();
  178. /* Remove attributes */
  179. device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  180. /* Flush interrupt worker
  181. *
  182. * We do this after ams_info.exit(), because an interrupt might
  183. * have arrived before disabling them.
  184. */
  185. flush_work_sync(&ams_info.worker);
  186. /* Remove device */
  187. of_device_unregister(ams_info.of_dev);
  188. /* Remove handler */
  189. pmf_unregister_irq_client(&ams_shock_client);
  190. pmf_unregister_irq_client(&ams_freefall_client);
  191. }
  192. static void __exit ams_exit(void)
  193. {
  194. /* Shut down implementation */
  195. ams_info.exit();
  196. }
  197. MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
  198. MODULE_DESCRIPTION("Apple Motion Sensor driver");
  199. MODULE_LICENSE("GPL");
  200. module_init(ams_init);
  201. module_exit(ams_exit);