ams-core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/module.h>
  26. #include <asm/pmac_pfunc.h>
  27. #include <asm/of_platform.h>
  28. #include "ams.h"
  29. /* There is only one motion sensor per machine */
  30. struct ams ams_info;
  31. static unsigned int verbose;
  32. module_param(verbose, bool, 0644);
  33. MODULE_PARM_DESC(verbose, "Show free falls and shocks in kernel output");
  34. /* Call with ams_info.lock held! */
  35. void ams_sensors(s8 *x, s8 *y, s8 *z)
  36. {
  37. u32 orient = ams_info.vflag? ams_info.orient1 : ams_info.orient2;
  38. if (orient & 0x80)
  39. /* X and Y swapped */
  40. ams_info.get_xyz(y, x, z);
  41. else
  42. ams_info.get_xyz(x, y, z);
  43. if (orient & 0x04)
  44. *z = ~(*z);
  45. if (orient & 0x02)
  46. *y = ~(*y);
  47. if (orient & 0x01)
  48. *x = ~(*x);
  49. }
  50. static ssize_t ams_show_current(struct device *dev,
  51. struct device_attribute *attr, char *buf)
  52. {
  53. s8 x, y, z;
  54. mutex_lock(&ams_info.lock);
  55. ams_sensors(&x, &y, &z);
  56. mutex_unlock(&ams_info.lock);
  57. return snprintf(buf, PAGE_SIZE, "%d %d %d\n", x, y, z);
  58. }
  59. static DEVICE_ATTR(current, S_IRUGO, ams_show_current, NULL);
  60. static void ams_handle_irq(void *data)
  61. {
  62. enum ams_irq irq = *((enum ams_irq *)data);
  63. spin_lock(&ams_info.irq_lock);
  64. ams_info.worker_irqs |= irq;
  65. schedule_work(&ams_info.worker);
  66. spin_unlock(&ams_info.irq_lock);
  67. }
  68. static enum ams_irq ams_freefall_irq_data = AMS_IRQ_FREEFALL;
  69. static struct pmf_irq_client ams_freefall_client = {
  70. .owner = THIS_MODULE,
  71. .handler = ams_handle_irq,
  72. .data = &ams_freefall_irq_data,
  73. };
  74. static enum ams_irq ams_shock_irq_data = AMS_IRQ_SHOCK;
  75. static struct pmf_irq_client ams_shock_client = {
  76. .owner = THIS_MODULE,
  77. .handler = ams_handle_irq,
  78. .data = &ams_shock_irq_data,
  79. };
  80. /* Once hard disk parking is implemented in the kernel, this function can
  81. * trigger it.
  82. */
  83. static void ams_worker(struct work_struct *work)
  84. {
  85. mutex_lock(&ams_info.lock);
  86. if (ams_info.has_device) {
  87. unsigned long flags;
  88. spin_lock_irqsave(&ams_info.irq_lock, flags);
  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. /* we must call this with interrupts enabled */
  94. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  95. ams_info.clear_irq(AMS_IRQ_FREEFALL);
  96. spin_lock_irqsave(&ams_info.irq_lock, flags);
  97. }
  98. if (ams_info.worker_irqs & AMS_IRQ_SHOCK) {
  99. if (verbose)
  100. printk(KERN_INFO "ams: shock detected!\n");
  101. ams_info.worker_irqs &= ~AMS_IRQ_SHOCK;
  102. /* we must call this with interrupts enabled */
  103. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  104. ams_info.clear_irq(AMS_IRQ_SHOCK);
  105. spin_lock_irqsave(&ams_info.irq_lock, flags);
  106. }
  107. spin_unlock_irqrestore(&ams_info.irq_lock, flags);
  108. }
  109. mutex_unlock(&ams_info.lock);
  110. }
  111. /* Call with ams_info.lock held! */
  112. int ams_sensor_attach(void)
  113. {
  114. int result;
  115. const u32 *prop;
  116. /* Get orientation */
  117. prop = of_get_property(ams_info.of_node, "orientation", NULL);
  118. if (!prop)
  119. return -ENODEV;
  120. ams_info.orient1 = *prop;
  121. ams_info.orient2 = *(prop + 1);
  122. /* Register freefall interrupt handler */
  123. result = pmf_register_irq_client(ams_info.of_node,
  124. "accel-int-1",
  125. &ams_freefall_client);
  126. if (result < 0)
  127. return -ENODEV;
  128. /* Reset saved irqs */
  129. ams_info.worker_irqs = 0;
  130. /* Register shock interrupt handler */
  131. result = pmf_register_irq_client(ams_info.of_node,
  132. "accel-int-2",
  133. &ams_shock_client);
  134. if (result < 0)
  135. goto release_freefall;
  136. /* Create device */
  137. ams_info.of_dev = of_platform_device_create(ams_info.of_node, "ams", NULL);
  138. if (!ams_info.of_dev) {
  139. result = -ENODEV;
  140. goto release_shock;
  141. }
  142. /* Create attributes */
  143. result = device_create_file(&ams_info.of_dev->dev, &dev_attr_current);
  144. if (result)
  145. goto release_of;
  146. ams_info.vflag = !!(ams_info.get_vendor() & 0x10);
  147. /* Init input device */
  148. result = ams_input_init();
  149. if (result)
  150. goto release_device_file;
  151. return result;
  152. release_device_file:
  153. device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  154. release_of:
  155. of_device_unregister(ams_info.of_dev);
  156. release_shock:
  157. pmf_unregister_irq_client(&ams_shock_client);
  158. release_freefall:
  159. pmf_unregister_irq_client(&ams_freefall_client);
  160. return result;
  161. }
  162. int __init ams_init(void)
  163. {
  164. struct device_node *np;
  165. spin_lock_init(&ams_info.irq_lock);
  166. mutex_init(&ams_info.lock);
  167. INIT_WORK(&ams_info.worker, ams_worker);
  168. #ifdef CONFIG_SENSORS_AMS_I2C
  169. np = of_find_node_by_name(NULL, "accelerometer");
  170. if (np && of_device_is_compatible(np, "AAPL,accelerometer_1"))
  171. /* Found I2C motion sensor */
  172. return ams_i2c_init(np);
  173. #endif
  174. #ifdef CONFIG_SENSORS_AMS_PMU
  175. np = of_find_node_by_name(NULL, "sms");
  176. if (np && of_device_is_compatible(np, "sms"))
  177. /* Found PMU motion sensor */
  178. return ams_pmu_init(np);
  179. #endif
  180. return -ENODEV;
  181. }
  182. void ams_exit(void)
  183. {
  184. mutex_lock(&ams_info.lock);
  185. if (ams_info.has_device) {
  186. /* Remove input device */
  187. ams_input_exit();
  188. /* Shut down implementation */
  189. ams_info.exit();
  190. /* Flush interrupt worker
  191. *
  192. * We do this after ams_info.exit(), because an interrupt might
  193. * have arrived before disabling them.
  194. */
  195. flush_scheduled_work();
  196. /* Remove attributes */
  197. device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
  198. /* Remove device */
  199. of_device_unregister(ams_info.of_dev);
  200. /* Remove handler */
  201. pmf_unregister_irq_client(&ams_shock_client);
  202. pmf_unregister_irq_client(&ams_freefall_client);
  203. }
  204. mutex_unlock(&ams_info.lock);
  205. }
  206. MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");
  207. MODULE_DESCRIPTION("Apple Motion Sensor driver");
  208. MODULE_LICENSE("GPL");
  209. module_init(ams_init);
  210. module_exit(ams_exit);