ams-core.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. mutex_lock(&ams_info.lock);
  85. if (ams_info.has_device) {
  86. unsigned long flags;
  87. spin_lock_irqsave(&ams_info.irq_lock, flags);
  88. if (ams_info.worker_irqs & AMS_IRQ_FREEFALL) {
  89. if (verbose)
  90. printk(KERN_INFO "ams: freefall detected!\n");
  91. ams_info.worker_irqs &= ~AMS_IRQ_FREEFALL;
  92. /* we must call this with interrupts enabled */
  93. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  94. ams_info.clear_irq(AMS_IRQ_FREEFALL);
  95. spin_lock_irqsave(&ams_info.irq_lock, flags);
  96. }
  97. if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
  98. if (verbose)
  99. printk(KERN_INFO "ams: shock detected!\n");
  100. ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
  101. /* we must call this with interrupts enabled */
  102. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  103. ams_info.clear_irq(AMS_IRQ_SHOCK);
  104. spin_lock_irqsave(&ams_info.irq_lock, flags);
  105. }
  106. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  107. }
  108. mutex_unlock(&ams_info.lock);
  109. }
  110. /* Call with ams_info.lock held! */
  111. int ams_sensor_attach(void)
  112. {
  113. int result;
  114. const u32 *prop;
  115. /* Get orientation */
  116. prop = of_get_property(ams_info.of_node, "orientation", NULL);
  117. if (!prop)
  118. return -ENODEV;
  119. ams_info.orient1 = *prop;
  120. ams_info.orient2 = *(prop + 1);
  121. /* Register freefall interrupt handler */
  122. result = pmf_register_irq_client(ams_info.of_node,
  123. "accel-int-1",
  124. &ams_freefall_client);
  125. if (result < 0)
  126. return -ENODEV;
  127. /* Reset saved irqs */
  128. ams_info.worker_irqs = 0;
  129. /* Register shock interrupt handler */
  130. result = pmf_register_irq_client(ams_info.of_node,
  131. "accel-int-2",
  132. &ams_shock_client);
  133. if (result < 0)
  134. goto release_freefall;
  135. /* Create device */
  136. ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
  137. if (!ams_info.of_dev) {
  138. result = -ENODEV;
  139. goto release_shock;
  140. }
  141. /* Create attributes */
  142. result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
  143. if (result)
  144. goto release_of;
  145. ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
  146. /* Init input device */
  147. result = ams_input_init();
  148. if (result)
  149. goto release_device_file;
  150. return result;
  151. release_device_file:
  152. device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  153. release_of:
  154. of_device_unregister(ams_info.of_dev);
  155. release_shock:
  156. pmf_unregister_irq_client(&ams_shock_client);
  157. release_freefall:
  158. pmf_unregister_irq_client(&ams_freefall_client);
  159. return result;
  160. }
  161. int __init ams_init(void)
  162. {
  163. struct device_node *np;
  164. spin_lock_init(&ams_info.irq_lock);
  165. mutex_init(&ams_info.lock);
  166. INIT_WORK(&ams_info.worker, ams_worker);
  167. #ifdef CONFIG_SENSORS_AMS_I2C
  168. np = of_find_node_by_name(NULL, "accelerometer");
  169. if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
  170. /* Found I2C motion sensor */
  171. return ams_i2c_init(np);
  172. #endif
  173. #ifdef CONFIG_SENSORS_AMS_PMU
  174. np = of_find_node_by_name(NULL, "sms");
  175. if (np && of_device_is_compatible(np, "sms"))
  176. /* Found PMU motion sensor */
  177. return ams_pmu_init(np);
  178. #endif
  179. return -ENODEV;
  180. }
  181. void ams_exit(void)
  182. {
  183. mutex_lock(&ams_info.lock);
  184. if (ams_info.has_device) {
  185. /* Remove input device */
  186. ams_input_exit();
  187. /* Shut down implementation */
  188. ams_info.exit();
  189. /* Flush interrupt worker
  190. *
  191. * We do this after ams_info.exit(), because an interrupt might
  192. * have arrived before disabling them.
  193. */
  194. flush_scheduled_work();
  195. /* Remove attributes */
  196. device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  197. /* Remove device */
  198. of_device_unregister(ams_info.of_dev);
  199. /* Remove handler */
  200. pmf_unregister_irq_client(&ams_shock_client);
  201. pmf_unregister_irq_client(&ams_freefall_client);
  202. }
  203. mutex_unlock(&ams_info.lock);
  204. }
  205. MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
  206. MODULE_DESCRIPTION("Apple Motion Sensor driver");
  207. MODULE_LICENSE("GPL");
  208. module_init(ams_init);
  209. module_exit(ams_exit);