ams-i2c.c 6.3 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. .owner = THIS_MODULE,
  70. },
  71. .probe = ams_i2c_probe,
  72. .remove = ams_i2c_remove,
  73. .id_table = ams_id,
  74. };
  75. static s32 ams_i2c_read(u8 reg)
  76. {
  77. return i2c_smbus_read_byte_data(ams_info.i2c_client, reg);
  78. }
  79. static int ams_i2c_write(u8 reg, u8 value)
  80. {
  81. return i2c_smbus_write_byte_data(ams_info.i2c_client, reg, value);
  82. }
  83. static int ams_i2c_cmd(enum ams_i2c_cmd cmd)
  84. {
  85. s32 result;
  86. int count = 3;
  87. ams_i2c_write(AMS_COMMAND, cmd);
  88. msleep(5);
  89. while (count--) {
  90. result = ams_i2c_read(AMS_COMMAND);
  91. if (result == 0 || result & 0x80)
  92. return 0;
  93. schedule_timeout_uninterruptible(HZ / 20);
  94. }
  95. return -1;
  96. }
  97. static void ams_i2c_set_irq(enum ams_irq reg, char enable)
  98. {
  99. if (reg & AMS_IRQ_FREEFALL) {
  100. u8 val = ams_i2c_read(AMS_CTRLX);
  101. if (enable)
  102. val |= 0x80;
  103. else
  104. val &= ~0x80;
  105. ams_i2c_write(AMS_CTRLX, val);
  106. }
  107. if (reg & AMS_IRQ_SHOCK) {
  108. u8 val = ams_i2c_read(AMS_CTRLY);
  109. if (enable)
  110. val |= 0x80;
  111. else
  112. val &= ~0x80;
  113. ams_i2c_write(AMS_CTRLY, val);
  114. }
  115. if (reg & AMS_IRQ_GLOBAL) {
  116. u8 val = ams_i2c_read(AMS_CTRLZ);
  117. if (enable)
  118. val |= 0x80;
  119. else
  120. val &= ~0x80;
  121. ams_i2c_write(AMS_CTRLZ, val);
  122. }
  123. }
  124. static void ams_i2c_clear_irq(enum ams_irq reg)
  125. {
  126. if (reg & AMS_IRQ_FREEFALL)
  127. ams_i2c_write(AMS_FREEFALL, 0);
  128. if (reg & AMS_IRQ_SHOCK)
  129. ams_i2c_write(AMS_SHOCK, 0);
  130. }
  131. static u8 ams_i2c_get_vendor(void)
  132. {
  133. return ams_i2c_read(AMS_VENDOR);
  134. }
  135. static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z)
  136. {
  137. *x = ams_i2c_read(AMS_DATAX);
  138. *y = ams_i2c_read(AMS_DATAY);
  139. *z = ams_i2c_read(AMS_DATAZ);
  140. }
  141. static int ams_i2c_probe(struct i2c_client *client,
  142. const struct i2c_device_id *id)
  143. {
  144. int vmaj, vmin;
  145. int result;
  146. /* There can be only one */
  147. if (unlikely(ams_info.has_device))
  148. return -ENODEV;
  149. ams_info.i2c_client = client;
  150. if (ams_i2c_cmd(AMS_CMD_RESET)) {
  151. printk(KERN_INFO "ams: Failed to reset the device\n");
  152. return -ENODEV;
  153. }
  154. if (ams_i2c_cmd(AMS_CMD_START)) {
  155. printk(KERN_INFO "ams: Failed to start the device\n");
  156. return -ENODEV;
  157. }
  158. /* get version/vendor information */
  159. ams_i2c_write(AMS_CTRL1, 0x02);
  160. ams_i2c_write(AMS_CTRL2, 0x85);
  161. ams_i2c_write(AMS_CTRL3, 0x01);
  162. ams_i2c_cmd(AMS_CMD_READMEM);
  163. vmaj = ams_i2c_read(AMS_DATA1);
  164. vmin = ams_i2c_read(AMS_DATA2);
  165. if (vmaj != 1 || vmin != 52) {
  166. printk(KERN_INFO "ams: Incorrect device version (%d.%d)\n",
  167. vmaj, vmin);
  168. return -ENODEV;
  169. }
  170. ams_i2c_cmd(AMS_CMD_VERSION);
  171. vmaj = ams_i2c_read(AMS_DATA1);
  172. vmin = ams_i2c_read(AMS_DATA2);
  173. if (vmaj != 0 || vmin != 1) {
  174. printk(KERN_INFO "ams: Incorrect firmware version (%d.%d)\n",
  175. vmaj, vmin);
  176. return -ENODEV;
  177. }
  178. /* Disable interrupts */
  179. ams_i2c_set_irq(AMS_IRQ_ALL, 0);
  180. result = ams_sensor_attach();
  181. if (result < 0)
  182. return result;
  183. /* Set default values */
  184. ams_i2c_write(AMS_SENSLOW, 0x15);
  185. ams_i2c_write(AMS_SENSHIGH, 0x60);
  186. ams_i2c_write(AMS_CTRLX, 0x08);
  187. ams_i2c_write(AMS_CTRLY, 0x0F);
  188. ams_i2c_write(AMS_CTRLZ, 0x4F);
  189. ams_i2c_write(AMS_UNKNOWN1, 0x14);
  190. /* Clear interrupts */
  191. ams_i2c_clear_irq(AMS_IRQ_ALL);
  192. ams_info.has_device = 1;
  193. /* Enable interrupts */
  194. ams_i2c_set_irq(AMS_IRQ_ALL, 1);
  195. printk(KERN_INFO "ams: Found I2C based motion sensor\n");
  196. return 0;
  197. }
  198. static int ams_i2c_remove(struct i2c_client *client)
  199. {
  200. if (ams_info.has_device) {
  201. ams_sensor_detach();
  202. /* Disable interrupts */
  203. ams_i2c_set_irq(AMS_IRQ_ALL, 0);
  204. /* Clear interrupts */
  205. ams_i2c_clear_irq(AMS_IRQ_ALL);
  206. printk(KERN_INFO "ams: Unloading\n");
  207. ams_info.has_device = 0;
  208. }
  209. return 0;
  210. }
  211. static void ams_i2c_exit(void)
  212. {
  213. i2c_del_driver(&ams_i2c_driver);
  214. }
  215. int __init ams_i2c_init(struct device_node *np)
  216. {
  217. int result;
  218. /* Set implementation stuff */
  219. ams_info.of_node = np;
  220. ams_info.exit = ams_i2c_exit;
  221. ams_info.get_vendor = ams_i2c_get_vendor;
  222. ams_info.get_xyz = ams_i2c_get_xyz;
  223. ams_info.clear_irq = ams_i2c_clear_irq;
  224. ams_info.bustype = BUS_I2C;
  225. result = i2c_add_driver(&ams_i2c_driver);
  226. return result;
  227. }