ams-i2c.c 6.2 KB

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