i2c-powermac.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. i2c Support for Apple SMU Controller
  3. Copyright (c) 2005 Benjamin Herrenschmidt, IBM Corp.
  4. <benh@kernel.crashing.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/i2c.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/prom.h>
  25. #include <asm/pmac_low_i2c.h>
  26. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  27. MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
  28. MODULE_LICENSE("GPL");
  29. /*
  30. * SMBUS-type transfer entrypoint
  31. */
  32. static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,
  33. u16 addr,
  34. unsigned short flags,
  35. char read_write,
  36. u8 command,
  37. int size,
  38. union i2c_smbus_data* data)
  39. {
  40. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  41. int rc = 0;
  42. int read = (read_write == I2C_SMBUS_READ);
  43. int addrdir = (addr << 1) | read;
  44. u8 local[2];
  45. rc = pmac_i2c_open(bus, 0);
  46. if (rc)
  47. return rc;
  48. switch (size) {
  49. case I2C_SMBUS_QUICK:
  50. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  51. if (rc)
  52. goto bail;
  53. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, NULL, 0);
  54. break;
  55. case I2C_SMBUS_BYTE:
  56. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  57. if (rc)
  58. goto bail;
  59. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, &data->byte, 1);
  60. break;
  61. case I2C_SMBUS_BYTE_DATA:
  62. rc = pmac_i2c_setmode(bus, read ?
  63. pmac_i2c_mode_combined :
  64. pmac_i2c_mode_stdsub);
  65. if (rc)
  66. goto bail;
  67. rc = pmac_i2c_xfer(bus, addrdir, 1, command, &data->byte, 1);
  68. break;
  69. case I2C_SMBUS_WORD_DATA:
  70. rc = pmac_i2c_setmode(bus, read ?
  71. pmac_i2c_mode_combined :
  72. pmac_i2c_mode_stdsub);
  73. if (rc)
  74. goto bail;
  75. if (!read) {
  76. local[0] = data->word & 0xff;
  77. local[1] = (data->word >> 8) & 0xff;
  78. }
  79. rc = pmac_i2c_xfer(bus, addrdir, 1, command, local, 2);
  80. if (rc == 0 && read) {
  81. data->word = ((u16)local[1]) << 8;
  82. data->word |= local[0];
  83. }
  84. break;
  85. /* Note that these are broken vs. the expected smbus API where
  86. * on reads, the length is actually returned from the function,
  87. * but I think the current API makes no sense and I don't want
  88. * any driver that I haven't verified for correctness to go
  89. * anywhere near a pmac i2c bus anyway ...
  90. *
  91. * I'm also not completely sure what kind of phases to do between
  92. * the actual command and the data (what I am _supposed_ to do that
  93. * is). For now, I assume writes are a single stream and reads have
  94. * a repeat start/addr phase (but not stop in between)
  95. */
  96. case I2C_SMBUS_BLOCK_DATA:
  97. rc = pmac_i2c_setmode(bus, read ?
  98. pmac_i2c_mode_combined :
  99. pmac_i2c_mode_stdsub);
  100. if (rc)
  101. goto bail;
  102. rc = pmac_i2c_xfer(bus, addrdir, 1, command, data->block,
  103. data->block[0] + 1);
  104. break;
  105. case I2C_SMBUS_I2C_BLOCK_DATA:
  106. rc = pmac_i2c_setmode(bus, read ?
  107. pmac_i2c_mode_combined :
  108. pmac_i2c_mode_stdsub);
  109. if (rc)
  110. goto bail;
  111. rc = pmac_i2c_xfer(bus, addrdir, 1, command,
  112. &data->block[1], data->block[0]);
  113. break;
  114. default:
  115. rc = -EINVAL;
  116. }
  117. bail:
  118. pmac_i2c_close(bus);
  119. return rc;
  120. }
  121. /*
  122. * Generic i2c master transfer entrypoint. This driver only support single
  123. * messages (for "lame i2c" transfers). Anything else should use the smbus
  124. * entry point
  125. */
  126. static int i2c_powermac_master_xfer( struct i2c_adapter *adap,
  127. struct i2c_msg *msgs,
  128. int num)
  129. {
  130. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  131. int rc = 0;
  132. int read;
  133. int addrdir;
  134. if (msgs->flags & I2C_M_TEN)
  135. return -EINVAL;
  136. read = (msgs->flags & I2C_M_RD) != 0;
  137. addrdir = (msgs->addr << 1) | read;
  138. if (msgs->flags & I2C_M_REV_DIR_ADDR)
  139. addrdir ^= 1;
  140. rc = pmac_i2c_open(bus, 0);
  141. if (rc)
  142. return rc;
  143. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  144. if (rc)
  145. goto bail;
  146. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  147. bail:
  148. pmac_i2c_close(bus);
  149. return rc < 0 ? rc : 1;
  150. }
  151. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  152. {
  153. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  154. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  155. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  156. }
  157. /* For now, we only handle smbus */
  158. static const struct i2c_algorithm i2c_powermac_algorithm = {
  159. .smbus_xfer = i2c_powermac_smbus_xfer,
  160. .master_xfer = i2c_powermac_master_xfer,
  161. .functionality = i2c_powermac_func,
  162. };
  163. static int __devexit i2c_powermac_remove(struct platform_device *dev)
  164. {
  165. struct i2c_adapter *adapter = platform_get_drvdata(dev);
  166. struct pmac_i2c_bus *bus = i2c_get_adapdata(adapter);
  167. int rc;
  168. rc = i2c_del_adapter(adapter);
  169. pmac_i2c_detach_adapter(bus, adapter);
  170. i2c_set_adapdata(adapter, NULL);
  171. /* We aren't that prepared to deal with this... */
  172. if (rc)
  173. printk(KERN_WARNING
  174. "i2c-powermac.c: Failed to remove bus %s !\n",
  175. adapter->name);
  176. platform_set_drvdata(dev, NULL);
  177. kfree(adapter);
  178. return 0;
  179. }
  180. static int __devinit i2c_powermac_probe(struct platform_device *dev)
  181. {
  182. struct pmac_i2c_bus *bus = dev->dev.platform_data;
  183. struct device_node *parent = NULL;
  184. struct i2c_adapter *adapter;
  185. char name[32];
  186. const char *basename;
  187. int rc;
  188. if (bus == NULL)
  189. return -EINVAL;
  190. /* Ok, now we need to make up a name for the interface that will
  191. * match what we used to do in the past, that is basically the
  192. * controller's parent device node for keywest. PMU didn't have a
  193. * naming convention and SMU has a different one
  194. */
  195. switch(pmac_i2c_get_type(bus)) {
  196. case pmac_i2c_bus_keywest:
  197. parent = of_get_parent(pmac_i2c_get_controller(bus));
  198. if (parent == NULL)
  199. return -EINVAL;
  200. basename = parent->name;
  201. break;
  202. case pmac_i2c_bus_pmu:
  203. basename = "pmu";
  204. break;
  205. case pmac_i2c_bus_smu:
  206. /* This is not what we used to do but I'm fixing drivers at
  207. * the same time as this change
  208. */
  209. basename = "smu";
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. snprintf(name, 32, "%s %d", basename, pmac_i2c_get_channel(bus));
  215. of_node_put(parent);
  216. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  217. if (adapter == NULL) {
  218. printk(KERN_ERR "i2c-powermac: can't allocate inteface !\n");
  219. return -ENOMEM;
  220. }
  221. platform_set_drvdata(dev, adapter);
  222. strcpy(adapter->name, name);
  223. adapter->algo = &i2c_powermac_algorithm;
  224. i2c_set_adapdata(adapter, bus);
  225. adapter->dev.parent = &dev->dev;
  226. pmac_i2c_attach_adapter(bus, adapter);
  227. rc = i2c_add_adapter(adapter);
  228. if (rc) {
  229. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  230. "failed\n", name);
  231. i2c_set_adapdata(adapter, NULL);
  232. pmac_i2c_detach_adapter(bus, adapter);
  233. }
  234. printk(KERN_INFO "PowerMac i2c bus %s registered\n", name);
  235. if (!strncmp(basename, "uni-n", 5)) {
  236. struct device_node *np;
  237. const u32 *prop;
  238. struct i2c_board_info info;
  239. /* Instantiate I2C motion sensor if present */
  240. np = of_find_node_by_name(NULL, "accelerometer");
  241. if (np && of_device_is_compatible(np, "AAPL,accelerometer_1") &&
  242. (prop = of_get_property(np, "reg", NULL))) {
  243. int i2c_bus;
  244. const char *tmp_bus;
  245. /* look for bus either using "reg" or by path */
  246. tmp_bus = strstr(np->full_name, "/i2c-bus@");
  247. if (tmp_bus)
  248. i2c_bus = *(tmp_bus + 9) - '0';
  249. else
  250. i2c_bus = ((*prop) >> 8) & 0x0f;
  251. if (pmac_i2c_get_channel(bus) == i2c_bus) {
  252. memset(&info, 0, sizeof(struct i2c_board_info));
  253. info.addr = ((*prop) & 0xff) >> 1;
  254. strlcpy(info.type, "ams", I2C_NAME_SIZE);
  255. i2c_new_device(adapter, &info);
  256. }
  257. }
  258. }
  259. return rc;
  260. }
  261. /* work with hotplug and coldplug */
  262. MODULE_ALIAS("platform:i2c-powermac");
  263. static struct platform_driver i2c_powermac_driver = {
  264. .probe = i2c_powermac_probe,
  265. .remove = __devexit_p(i2c_powermac_remove),
  266. .driver = {
  267. .name = "i2c-powermac",
  268. .bus = &platform_bus_type,
  269. },
  270. };
  271. static int __init i2c_powermac_init(void)
  272. {
  273. platform_driver_register(&i2c_powermac_driver);
  274. return 0;
  275. }
  276. static void __exit i2c_powermac_cleanup(void)
  277. {
  278. platform_driver_unregister(&i2c_powermac_driver);
  279. }
  280. module_init(i2c_powermac_init);
  281. module_exit(i2c_powermac_cleanup);