ams-pmu.c 4.2 KB

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