ams-pmu.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Apple Motion Sensor driver (PMU variant)
  3. *
  4. * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/adb.h>
  16. #include <linux/pmu.h>
  17. #include "ams.h"
  18. /* Attitude */
  19. #define AMS_X 0x00
  20. #define AMS_Y 0x01
  21. #define AMS_Z 0x02
  22. /* Not exactly known, maybe chip vendor */
  23. #define AMS_VENDOR 0x03
  24. /* Freefall registers */
  25. #define AMS_FF_CLEAR 0x04
  26. #define AMS_FF_ENABLE 0x05
  27. #define AMS_FF_LOW_LIMIT 0x06
  28. #define AMS_FF_DEBOUNCE 0x07
  29. /* Shock registers */
  30. #define AMS_SHOCK_CLEAR 0x08
  31. #define AMS_SHOCK_ENABLE 0x09
  32. #define AMS_SHOCK_HIGH_LIMIT 0x0a
  33. #define AMS_SHOCK_DEBOUNCE 0x0b
  34. /* Global interrupt and power control register */
  35. #define AMS_CONTROL 0x0c
  36. static u8 ams_pmu_cmd;
  37. static void ams_pmu_req_complete(struct adb_request *req)
  38. {
  39. complete((struct completion *)req->arg);
  40. }
  41. /* Only call this function from task context */
  42. static void ams_pmu_set_register(u8 reg, u8 value)
  43. {
  44. static struct adb_request req;
  45. DECLARE_COMPLETION(req_complete);
  46. req.arg = &req_complete;
  47. if (pmu_request(&req, ams_pmu_req_complete, 4, ams_pmu_cmd, 0x00, reg, value))
  48. return;
  49. wait_for_completion(&req_complete);
  50. }
  51. /* Only call this function from task context */
  52. static u8 ams_pmu_get_register(u8 reg)
  53. {
  54. static struct adb_request req;
  55. DECLARE_COMPLETION(req_complete);
  56. req.arg = &req_complete;
  57. if (pmu_request(&req, ams_pmu_req_complete, 3, ams_pmu_cmd, 0x01, reg))
  58. return 0;
  59. wait_for_completion(&req_complete);
  60. if (req.reply_len > 0)
  61. return req.reply[0];
  62. else
  63. return 0;
  64. }
  65. /* Enables or disables the specified interrupts */
  66. static void ams_pmu_set_irq(enum ams_irq reg, char enable)
  67. {
  68. if (reg & AMS_IRQ_FREEFALL) {
  69. u8 val = ams_pmu_get_register(AMS_FF_ENABLE);
  70. if (enable)
  71. val |= 0x80;
  72. else
  73. val &= ~0x80;
  74. ams_pmu_set_register(AMS_FF_ENABLE, val);
  75. }
  76. if (reg & AMS_IRQ_SHOCK) {
  77. u8 val = ams_pmu_get_register(AMS_SHOCK_ENABLE);
  78. if (enable)
  79. val |= 0x80;
  80. else
  81. val &= ~0x80;
  82. ams_pmu_set_register(AMS_SHOCK_ENABLE, val);
  83. }
  84. if (reg & AMS_IRQ_GLOBAL) {
  85. u8 val = ams_pmu_get_register(AMS_CONTROL);
  86. if (enable)
  87. val |= 0x80;
  88. else
  89. val &= ~0x80;
  90. ams_pmu_set_register(AMS_CONTROL, val);
  91. }
  92. }
  93. static void ams_pmu_clear_irq(enum ams_irq reg)
  94. {
  95. if (reg & AMS_IRQ_FREEFALL)
  96. ams_pmu_set_register(AMS_FF_CLEAR, 0x00);
  97. if (reg & AMS_IRQ_SHOCK)
  98. ams_pmu_set_register(AMS_SHOCK_CLEAR, 0x00);
  99. }
  100. static u8 ams_pmu_get_vendor(void)
  101. {
  102. return ams_pmu_get_register(AMS_VENDOR);
  103. }
  104. static void ams_pmu_get_xyz(s8 *x, s8 *y, s8 *z)
  105. {
  106. *x = ams_pmu_get_register(AMS_X);
  107. *y = ams_pmu_get_register(AMS_Y);
  108. *z = ams_pmu_get_register(AMS_Z);
  109. }
  110. static void ams_pmu_exit(void)
  111. {
  112. ams_sensor_detach();
  113. /* Disable interrupts */
  114. ams_pmu_set_irq(AMS_IRQ_ALL, 0);
  115. /* Clear interrupts */
  116. ams_pmu_clear_irq(AMS_IRQ_ALL);
  117. ams_info.has_device = 0;
  118. printk(KERN_INFO "ams: Unloading\n");
  119. }
  120. int __init ams_pmu_init(struct device_node *np)
  121. {
  122. const u32 *prop;
  123. int result;
  124. /* Set implementation stuff */
  125. ams_info.of_node = np;
  126. ams_info.exit = ams_pmu_exit;
  127. ams_info.get_vendor = ams_pmu_get_vendor;
  128. ams_info.get_xyz = ams_pmu_get_xyz;
  129. ams_info.clear_irq = ams_pmu_clear_irq;
  130. ams_info.bustype = BUS_HOST;
  131. /* Get PMU command, should be 0x4e, but we can never know */
  132. prop = of_get_property(ams_info.of_node, "reg", NULL);
  133. if (!prop)
  134. return -ENODEV;
  135. ams_pmu_cmd = ((*prop) >> 8) & 0xff;
  136. /* Disable interrupts */
  137. ams_pmu_set_irq(AMS_IRQ_ALL, 0);
  138. /* Clear interrupts */
  139. ams_pmu_clear_irq(AMS_IRQ_ALL);
  140. result = ams_sensor_attach();
  141. if (result < 0)
  142. return result;
  143. /* Set default values */
  144. ams_pmu_set_register(AMS_FF_LOW_LIMIT, 0x15);
  145. ams_pmu_set_register(AMS_FF_ENABLE, 0x08);
  146. ams_pmu_set_register(AMS_FF_DEBOUNCE, 0x14);
  147. ams_pmu_set_register(AMS_SHOCK_HIGH_LIMIT, 0x60);
  148. ams_pmu_set_register(AMS_SHOCK_ENABLE, 0x0f);
  149. ams_pmu_set_register(AMS_SHOCK_DEBOUNCE, 0x14);
  150. ams_pmu_set_register(AMS_CONTROL, 0x4f);
  151. /* Clear interrupts */
  152. ams_pmu_clear_irq(AMS_IRQ_ALL);
  153. ams_info.has_device = 1;
  154. /* Enable interrupts */
  155. ams_pmu_set_irq(AMS_IRQ_ALL, 1);
  156. printk(KERN_INFO "ams: Found PMU based motion sensor\n");
  157. return 0;
  158. }