i2c-powermac.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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("i2c-powermac.c: Failed to remove bus %s !\n",
  174. adapter->name);
  175. platform_set_drvdata(dev, NULL);
  176. kfree(adapter);
  177. return 0;
  178. }
  179. static int __devinit i2c_powermac_probe(struct platform_device *dev)
  180. {
  181. struct pmac_i2c_bus *bus = dev->dev.platform_data;
  182. struct device_node *parent = NULL;
  183. struct i2c_adapter *adapter;
  184. char name[32];
  185. const char *basename;
  186. int rc;
  187. if (bus == NULL)
  188. return -EINVAL;
  189. /* Ok, now we need to make up a name for the interface that will
  190. * match what we used to do in the past, that is basically the
  191. * controller's parent device node for keywest. PMU didn't have a
  192. * naming convention and SMU has a different one
  193. */
  194. switch(pmac_i2c_get_type(bus)) {
  195. case pmac_i2c_bus_keywest:
  196. parent = of_get_parent(pmac_i2c_get_controller(bus));
  197. if (parent == NULL)
  198. return -EINVAL;
  199. basename = parent->name;
  200. break;
  201. case pmac_i2c_bus_pmu:
  202. basename = "pmu";
  203. break;
  204. case pmac_i2c_bus_smu:
  205. /* This is not what we used to do but I'm fixing drivers at
  206. * the same time as this change
  207. */
  208. basename = "smu";
  209. break;
  210. default:
  211. return -EINVAL;
  212. }
  213. snprintf(name, 32, "%s %d", basename, pmac_i2c_get_channel(bus));
  214. of_node_put(parent);
  215. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  216. if (adapter == NULL) {
  217. printk(KERN_ERR "i2c-powermac: can't allocate inteface !\n");
  218. return -ENOMEM;
  219. }
  220. platform_set_drvdata(dev, adapter);
  221. strcpy(adapter->name, name);
  222. adapter->algo = &i2c_powermac_algorithm;
  223. i2c_set_adapdata(adapter, bus);
  224. adapter->dev.parent = &dev->dev;
  225. pmac_i2c_attach_adapter(bus, adapter);
  226. rc = i2c_add_adapter(adapter);
  227. if (rc) {
  228. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  229. "failed\n", name);
  230. i2c_set_adapdata(adapter, NULL);
  231. pmac_i2c_detach_adapter(bus, adapter);
  232. }
  233. printk(KERN_INFO "PowerMac i2c bus %s registered\n", name);
  234. return rc;
  235. }
  236. /* work with hotplug and coldplug */
  237. MODULE_ALIAS("platform:i2c-powermac");
  238. static struct platform_driver i2c_powermac_driver = {
  239. .probe = i2c_powermac_probe,
  240. .remove = __devexit_p(i2c_powermac_remove),
  241. .driver = {
  242. .name = "i2c-powermac",
  243. .bus = &platform_bus_type,
  244. },
  245. };
  246. static int __init i2c_powermac_init(void)
  247. {
  248. platform_driver_register(&i2c_powermac_driver);
  249. return 0;
  250. }
  251. static void __exit i2c_powermac_cleanup(void)
  252. {
  253. platform_driver_unregister(&i2c_powermac_driver);
  254. }
  255. module_init(i2c_powermac_init);
  256. module_exit(i2c_powermac_cleanup);