ams-i2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Apple Motion Sensor driver (I2C variant)
  4. *
  5. * Copyright (C) 2005 Stelian Pop ([email protected])
  6. * Copyright (C) 2006 Michael Hanselmann ([email protected])
  7. *
  8. * Clean room implementation based on the reverse engineered Mac OS X driver by
  9. * Johannes Berg <[email protected]>, documentation available at
  10. * http://johannes.sipsolutions.net/PowerBook/Apple_Motion_Sensor_Specification
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include "ams.h"
  18. /* AMS registers */
  19. #define AMS_COMMAND 0x00 /* command register */
  20. #define AMS_STATUS 0x01 /* status register */
  21. #define AMS_CTRL1 0x02 /* read control 1 (number of values) */
  22. #define AMS_CTRL2 0x03 /* read control 2 (offset?) */
  23. #define AMS_CTRL3 0x04 /* read control 3 (size of each value?) */
  24. #define AMS_DATA1 0x05 /* read data 1 */
  25. #define AMS_DATA2 0x06 /* read data 2 */
  26. #define AMS_DATA3 0x07 /* read data 3 */
  27. #define AMS_DATA4 0x08 /* read data 4 */
  28. #define AMS_DATAX 0x20 /* data X */
  29. #define AMS_DATAY 0x21 /* data Y */
  30. #define AMS_DATAZ 0x22 /* data Z */
  31. #define AMS_FREEFALL 0x24 /* freefall int control */
  32. #define AMS_SHOCK 0x25 /* shock int control */
  33. #define AMS_SENSLOW 0x26 /* sensitivity low limit */
  34. #define AMS_SENSHIGH 0x27 /* sensitivity high limit */
  35. #define AMS_CTRLX 0x28 /* control X */
  36. #define AMS_CTRLY 0x29 /* control Y */
  37. #define AMS_CTRLZ 0x2A /* control Z */
  38. #define AMS_UNKNOWN1 0x2B /* unknown 1 */
  39. #define AMS_UNKNOWN2 0x2C /* unknown 2 */
  40. #define AMS_UNKNOWN3 0x2D /* unknown 3 */
  41. #define AMS_VENDOR 0x2E /* vendor */
  42. /* AMS commands - use with the AMS_COMMAND register */
  43. enum ams_i2c_cmd {
  44. AMS_CMD_NOOP = 0,
  45. AMS_CMD_VERSION,
  46. AMS_CMD_READMEM,
  47. AMS_CMD_WRITEMEM,
  48. AMS_CMD_ERASEMEM,
  49. AMS_CMD_READEE,
  50. AMS_CMD_WRITEEE,
  51. AMS_CMD_RESET,
  52. AMS_CMD_START,
  53. };
  54. static int ams_i2c_probe(struct i2c_client *client,
  55. const struct i2c_device_id *id);
  56. static void ams_i2c_remove(struct i2c_client *client);
  57. static const struct i2c_device_id ams_id[] = {
  58. { "MAC,accelerometer_1", 0 },
  59. { }
  60. };
  61. MODULE_DEVICE_TABLE(i2c, ams_id);
  62. static struct i2c_driver ams_i2c_driver = {
  63. .driver = {
  64. .name = "ams",
  65. },
  66. .probe = ams_i2c_probe,
  67. .remove = ams_i2c_remove,
  68. .id_table = ams_id,
  69. };
  70. static s32 ams_i2c_read(u8 reg)
  71. {
  72. return i2c_smbus_read_byte_data(ams_info.i2c_client, reg);
  73. }
  74. static int ams_i2c_write(u8 reg, u8 value)
  75. {
  76. return i2c_smbus_write_byte_data(ams_info.i2c_client, reg, value);
  77. }
  78. static int ams_i2c_cmd(enum ams_i2c_cmd cmd)
  79. {
  80. s32 result;
  81. int count = 3;
  82. ams_i2c_write(AMS_COMMAND, cmd);
  83. msleep(5);
  84. while (count--) {
  85. result = ams_i2c_read(AMS_COMMAND);
  86. if (result == 0 || result & 0x80)
  87. return 0;
  88. schedule_timeout_uninterruptible(HZ / 20);
  89. }
  90. return -1;
  91. }
  92. static void ams_i2c_set_irq(enum ams_irq reg, char enable)
  93. {
  94. if (reg & AMS_IRQ_FREEFALL) {
  95. u8 val = ams_i2c_read(AMS_CTRLX);
  96. if (enable)
  97. val |= 0x80;
  98. else
  99. val &= ~0x80;
  100. ams_i2c_write(AMS_CTRLX, val);
  101. }
  102. if (reg & AMS_IRQ_SHOCK) {
  103. u8 val = ams_i2c_read(AMS_CTRLY);
  104. if (enable)
  105. val |= 0x80;
  106. else
  107. val &= ~0x80;
  108. ams_i2c_write(AMS_CTRLY, val);
  109. }
  110. if (reg & AMS_IRQ_GLOBAL) {
  111. u8 val = ams_i2c_read(AMS_CTRLZ);
  112. if (enable)
  113. val |= 0x80;
  114. else
  115. val &= ~0x80;
  116. ams_i2c_write(AMS_CTRLZ, val);
  117. }
  118. }
  119. static void ams_i2c_clear_irq(enum ams_irq reg)
  120. {
  121. if (reg & AMS_IRQ_FREEFALL)
  122. ams_i2c_write(AMS_FREEFALL, 0);
  123. if (reg & AMS_IRQ_SHOCK)
  124. ams_i2c_write(AMS_SHOCK, 0);
  125. }
  126. static u8 ams_i2c_get_vendor(void)
  127. {
  128. return ams_i2c_read(AMS_VENDOR);
  129. }
  130. static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z)
  131. {
  132. *x = ams_i2c_read(AMS_DATAX);
  133. *y = ams_i2c_read(AMS_DATAY);
  134. *z = ams_i2c_read(AMS_DATAZ);
  135. }
  136. static int ams_i2c_probe(struct i2c_client *client,
  137. const struct i2c_device_id *id)
  138. {
  139. int vmaj, vmin;
  140. int result;
  141. /* There can be only one */
  142. if (unlikely(ams_info.has_device))
  143. return -ENODEV;
  144. ams_info.i2c_client = client;
  145. if (ams_i2c_cmd(AMS_CMD_RESET)) {
  146. printk(KERN_INFO "ams: Failed to reset the device\n");
  147. return -ENODEV;
  148. }
  149. if (ams_i2c_cmd(AMS_CMD_START)) {
  150. printk(KERN_INFO "ams: Failed to start the device\n");
  151. return -ENODEV;
  152. }
  153. /* get version/vendor information */
  154. ams_i2c_write(AMS_CTRL1, 0x02);
  155. ams_i2c_write(AMS_CTRL2, 0x85);
  156. ams_i2c_write(AMS_CTRL3, 0x01);
  157. ams_i2c_cmd(AMS_CMD_READMEM);
  158. vmaj = ams_i2c_read(AMS_DATA1);
  159. vmin = ams_i2c_read(AMS_DATA2);
  160. if (vmaj != 1 || vmin != 52) {
  161. printk(KERN_INFO "ams: Incorrect device version (%d.%d)\n",
  162. vmaj, vmin);
  163. return -ENODEV;
  164. }
  165. ams_i2c_cmd(AMS_CMD_VERSION);
  166. vmaj = ams_i2c_read(AMS_DATA1);
  167. vmin = ams_i2c_read(AMS_DATA2);
  168. if (vmaj != 0 || vmin != 1) {
  169. printk(KERN_INFO "ams: Incorrect firmware version (%d.%d)\n",
  170. vmaj, vmin);
  171. return -ENODEV;
  172. }
  173. /* Disable interrupts */
  174. ams_i2c_set_irq(AMS_IRQ_ALL, 0);
  175. result = ams_sensor_attach();
  176. if (result < 0)
  177. return result;
  178. /* Set default values */
  179. ams_i2c_write(AMS_SENSLOW, 0x15);
  180. ams_i2c_write(AMS_SENSHIGH, 0x60);
  181. ams_i2c_write(AMS_CTRLX, 0x08);
  182. ams_i2c_write(AMS_CTRLY, 0x0F);
  183. ams_i2c_write(AMS_CTRLZ, 0x4F);
  184. ams_i2c_write(AMS_UNKNOWN1, 0x14);
  185. /* Clear interrupts */
  186. ams_i2c_clear_irq(AMS_IRQ_ALL);
  187. ams_info.has_device = 1;
  188. /* Enable interrupts */
  189. ams_i2c_set_irq(AMS_IRQ_ALL, 1);
  190. printk(KERN_INFO "ams: Found I2C based motion sensor\n");
  191. return 0;
  192. }
  193. static void ams_i2c_remove(struct i2c_client *client)
  194. {
  195. if (ams_info.has_device) {
  196. ams_sensor_detach();
  197. /* Disable interrupts */
  198. ams_i2c_set_irq(AMS_IRQ_ALL, 0);
  199. /* Clear interrupts */
  200. ams_i2c_clear_irq(AMS_IRQ_ALL);
  201. printk(KERN_INFO "ams: Unloading\n");
  202. ams_info.has_device = 0;
  203. }
  204. }
  205. static void ams_i2c_exit(void)
  206. {
  207. i2c_del_driver(&ams_i2c_driver);
  208. }
  209. int __init ams_i2c_init(struct device_node *np)
  210. {
  211. /* Set implementation stuff */
  212. ams_info.of_node = np;
  213. ams_info.exit = ams_i2c_exit;
  214. ams_info.get_vendor = ams_i2c_get_vendor;
  215. ams_info.get_xyz = ams_i2c_get_xyz;
  216. ams_info.clear_irq = ams_i2c_clear_irq;
  217. ams_info.bustype = BUS_I2C;
  218. return i2c_add_driver(&ams_i2c_driver);
  219. }