ams-i2c.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_attach(struct i2c_adapter *adapter);
  59. static int ams_i2c_detach(struct i2c_adapter *adapter);
  60. static struct i2c_driver ams_i2c_driver = {
  61. .driver = {
  62. .name = "ams",
  63. .owner = THIS_MODULE,
  64. },
  65. .attach_adapter = ams_i2c_attach,
  66. .detach_adapter = ams_i2c_detach,
  67. };
  68. static s32 ams_i2c_read(u8 reg)
  69. {
  70. return i2c_smbus_read_byte_data(&ams_info.i2c_client, reg);
  71. }
  72. static int ams_i2c_write(u8 reg, u8 value)
  73. {
  74. return i2c_smbus_write_byte_data(&ams_info.i2c_client, reg, value);
  75. }
  76. static int ams_i2c_cmd(enum ams_i2c_cmd cmd)
  77. {
  78. s32 result;
  79. int count = 3;
  80. ams_i2c_write(AMS_COMMAND, cmd);
  81. msleep(5);
  82. while (count--) {
  83. result = ams_i2c_read(AMS_COMMAND);
  84. if (result == 0 || result & 0x80)
  85. return 0;
  86. schedule_timeout_uninterruptible(HZ / 20);
  87. }
  88. return -1;
  89. }
  90. static void ams_i2c_set_irq(enum ams_irq reg, char enable)
  91. {
  92. if (reg & AMS_IRQ_FREEFALL) {
  93. u8 val = ams_i2c_read(AMS_CTRLX);
  94. if (enable)
  95. val |= 0x80;
  96. else
  97. val &= ~0x80;
  98. ams_i2c_write(AMS_CTRLX, val);
  99. }
  100. if (reg & AMS_IRQ_SHOCK) {
  101. u8 val = ams_i2c_read(AMS_CTRLY);
  102. if (enable)
  103. val |= 0x80;
  104. else
  105. val &= ~0x80;
  106. ams_i2c_write(AMS_CTRLY, val);
  107. }
  108. if (reg & AMS_IRQ_GLOBAL) {
  109. u8 val = ams_i2c_read(AMS_CTRLZ);
  110. if (enable)
  111. val |= 0x80;
  112. else
  113. val &= ~0x80;
  114. ams_i2c_write(AMS_CTRLZ, val);
  115. }
  116. }
  117. static void ams_i2c_clear_irq(enum ams_irq reg)
  118. {
  119. if (reg & AMS_IRQ_FREEFALL)
  120. ams_i2c_write(AMS_FREEFALL, 0);
  121. if (reg & AMS_IRQ_SHOCK)
  122. ams_i2c_write(AMS_SHOCK, 0);
  123. }
  124. static u8 ams_i2c_get_vendor(void)
  125. {
  126. return ams_i2c_read(AMS_VENDOR);
  127. }
  128. static void ams_i2c_get_xyz(s8 *x, s8 *y, s8 *z)
  129. {
  130. *x = ams_i2c_read(AMS_DATAX);
  131. *y = ams_i2c_read(AMS_DATAY);
  132. *z = ams_i2c_read(AMS_DATAZ);
  133. }
  134. static int ams_i2c_attach(struct i2c_adapter *adapter)
  135. {
  136. unsigned long bus;
  137. int vmaj, vmin;
  138. int result;
  139. /* There can be only one */
  140. if (unlikely(ams_info.has_device))
  141. return -ENODEV;
  142. if (strncmp(adapter->name, "uni-n", 5))
  143. return -ENODEV;
  144. bus = simple_strtoul(adapter->name + 6, NULL, 10);
  145. if (bus != ams_info.i2c_bus)
  146. return -ENODEV;
  147. ams_info.i2c_client.addr = ams_info.i2c_address;
  148. ams_info.i2c_client.adapter = adapter;
  149. ams_info.i2c_client.driver = &ams_i2c_driver;
  150. strcpy(ams_info.i2c_client.name, "Apple Motion Sensor");
  151. if (ams_i2c_cmd(AMS_CMD_RESET)) {
  152. printk(KERN_INFO "ams: Failed to reset the device\n");
  153. return -ENODEV;
  154. }
  155. if (ams_i2c_cmd(AMS_CMD_START)) {
  156. printk(KERN_INFO "ams: Failed to start the device\n");
  157. return -ENODEV;
  158. }
  159. /* get version/vendor information */
  160. ams_i2c_write(AMS_CTRL1, 0x02);
  161. ams_i2c_write(AMS_CTRL2, 0x85);
  162. ams_i2c_write(AMS_CTRL3, 0x01);
  163. ams_i2c_cmd(AMS_CMD_READMEM);
  164. vmaj = ams_i2c_read(AMS_DATA1);
  165. vmin = ams_i2c_read(AMS_DATA2);
  166. if (vmaj != 1 || vmin != 52) {
  167. printk(KERN_INFO "ams: Incorrect device version (%d.%d)\n",
  168. vmaj, vmin);
  169. return -ENODEV;
  170. }
  171. ams_i2c_cmd(AMS_CMD_VERSION);
  172. vmaj = ams_i2c_read(AMS_DATA1);
  173. vmin = ams_i2c_read(AMS_DATA2);
  174. if (vmaj != 0 || vmin != 1) {
  175. printk(KERN_INFO "ams: Incorrect firmware version (%d.%d)\n",
  176. vmaj, vmin);
  177. return -ENODEV;
  178. }
  179. /* Disable interrupts */
  180. ams_i2c_set_irq(AMS_IRQ_ALL, 0);
  181. result = ams_sensor_attach();
  182. if (result < 0)
  183. return result;
  184. /* Set default values */
  185. ams_i2c_write(AMS_SENSLOW, 0x15);
  186. ams_i2c_write(AMS_SENSHIGH, 0x60);
  187. ams_i2c_write(AMS_CTRLX, 0x08);
  188. ams_i2c_write(AMS_CTRLY, 0x0F);
  189. ams_i2c_write(AMS_CTRLZ, 0x4F);
  190. ams_i2c_write(AMS_UNKNOWN1, 0x14);
  191. /* Clear interrupts */
  192. ams_i2c_clear_irq(AMS_IRQ_ALL);
  193. ams_info.has_device = 1;
  194. /* Enable interrupts */
  195. ams_i2c_set_irq(AMS_IRQ_ALL, 1);
  196. printk(KERN_INFO "ams: Found I2C based motion sensor\n");
  197. return 0;
  198. }
  199. static int ams_i2c_detach(struct i2c_adapter *adapter)
  200. {
  201. if (ams_info.has_device) {
  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. char *tmp_bus;
  218. int result;
  219. const u32 *prop;
  220. mutex_lock(&ams_info.lock);
  221. /* Set implementation stuff */
  222. ams_info.of_node = np;
  223. ams_info.exit = ams_i2c_exit;
  224. ams_info.get_vendor = ams_i2c_get_vendor;
  225. ams_info.get_xyz = ams_i2c_get_xyz;
  226. ams_info.clear_irq = ams_i2c_clear_irq;
  227. ams_info.bustype = BUS_I2C;
  228. /* look for bus either using "reg" or by path */
  229. prop = of_get_property(ams_info.of_node, "reg", NULL);
  230. if (!prop) {
  231. result = -ENODEV;
  232. goto exit;
  233. }
  234. tmp_bus = strstr(ams_info.of_node->full_name, "/i2c-bus@");
  235. if (tmp_bus)
  236. ams_info.i2c_bus = *(tmp_bus + 9) - '0';
  237. else
  238. ams_info.i2c_bus = ((*prop) >> 8) & 0x0f;
  239. ams_info.i2c_address = ((*prop) & 0xff) >> 1;
  240. result = i2c_add_driver(&ams_i2c_driver);
  241. exit:
  242. mutex_unlock(&ams_info.lock);
  243. return result;
  244. }