i2c-powermac.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/completion.h>
  23. #include <linux/device.h>
  24. #include <linux/platform_device.h>
  25. #include <asm/prom.h>
  26. #include <asm/pmac_low_i2c.h>
  27. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  28. MODULE_DESCRIPTION("I2C driver for Apple PowerMac");
  29. MODULE_LICENSE("GPL");
  30. /*
  31. * SMBUS-type transfer entrypoint
  32. */
  33. static s32 i2c_powermac_smbus_xfer( struct i2c_adapter* adap,
  34. u16 addr,
  35. unsigned short flags,
  36. char read_write,
  37. u8 command,
  38. int size,
  39. union i2c_smbus_data* data)
  40. {
  41. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  42. int rc = 0;
  43. int read = (read_write == I2C_SMBUS_READ);
  44. int addrdir = (addr << 1) | read;
  45. u8 local[2];
  46. rc = pmac_i2c_open(bus, 0);
  47. if (rc)
  48. return rc;
  49. switch (size) {
  50. case I2C_SMBUS_QUICK:
  51. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  52. if (rc)
  53. goto bail;
  54. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, NULL, 0);
  55. break;
  56. case I2C_SMBUS_BYTE:
  57. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  58. if (rc)
  59. goto bail;
  60. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, &data->byte, 1);
  61. break;
  62. case I2C_SMBUS_BYTE_DATA:
  63. rc = pmac_i2c_setmode(bus, read ?
  64. pmac_i2c_mode_combined :
  65. pmac_i2c_mode_stdsub);
  66. if (rc)
  67. goto bail;
  68. rc = pmac_i2c_xfer(bus, addrdir, 1, command, &data->byte, 1);
  69. break;
  70. case I2C_SMBUS_WORD_DATA:
  71. rc = pmac_i2c_setmode(bus, read ?
  72. pmac_i2c_mode_combined :
  73. pmac_i2c_mode_stdsub);
  74. if (rc)
  75. goto bail;
  76. if (!read) {
  77. local[0] = data->word & 0xff;
  78. local[1] = (data->word >> 8) & 0xff;
  79. }
  80. rc = pmac_i2c_xfer(bus, addrdir, 1, command, local, 2);
  81. if (rc == 0 && read) {
  82. data->word = ((u16)local[1]) << 8;
  83. data->word |= local[0];
  84. }
  85. break;
  86. /* Note that these are broken vs. the expected smbus API where
  87. * on reads, the lenght is actually returned from the function,
  88. * but I think the current API makes no sense and I don't want
  89. * any driver that I haven't verified for correctness to go
  90. * anywhere near a pmac i2c bus anyway ...
  91. *
  92. * I'm also not completely sure what kind of phases to do between
  93. * the actual command and the data (what I am _supposed_ to do that
  94. * is). For now, I assume writes are a single stream and reads have
  95. * a repeat start/addr phase (but not stop in between)
  96. */
  97. case I2C_SMBUS_BLOCK_DATA:
  98. rc = pmac_i2c_setmode(bus, read ?
  99. pmac_i2c_mode_combined :
  100. pmac_i2c_mode_stdsub);
  101. if (rc)
  102. goto bail;
  103. rc = pmac_i2c_xfer(bus, addrdir, 1, command, data->block,
  104. data->block[0] + 1);
  105. break;
  106. case I2C_SMBUS_I2C_BLOCK_DATA:
  107. rc = pmac_i2c_setmode(bus, read ?
  108. pmac_i2c_mode_combined :
  109. pmac_i2c_mode_stdsub);
  110. if (rc)
  111. goto bail;
  112. rc = pmac_i2c_xfer(bus, addrdir, 1, command,
  113. read ? data->block : &data->block[1],
  114. data->block[0]);
  115. break;
  116. default:
  117. rc = -EINVAL;
  118. }
  119. bail:
  120. pmac_i2c_close(bus);
  121. return rc;
  122. }
  123. /*
  124. * Generic i2c master transfer entrypoint. This driver only support single
  125. * messages (for "lame i2c" transfers). Anything else should use the smbus
  126. * entry point
  127. */
  128. static int i2c_powermac_master_xfer( struct i2c_adapter *adap,
  129. struct i2c_msg *msgs,
  130. int num)
  131. {
  132. struct pmac_i2c_bus *bus = i2c_get_adapdata(adap);
  133. int rc = 0;
  134. int read;
  135. int addrdir;
  136. if (msgs->flags & I2C_M_TEN)
  137. return -EINVAL;
  138. read = (msgs->flags & I2C_M_RD) != 0;
  139. addrdir = (msgs->addr << 1) | read;
  140. if (msgs->flags & I2C_M_REV_DIR_ADDR)
  141. addrdir ^= 1;
  142. rc = pmac_i2c_open(bus, 0);
  143. if (rc)
  144. return rc;
  145. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  146. if (rc)
  147. goto bail;
  148. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  149. bail:
  150. pmac_i2c_close(bus);
  151. return rc < 0 ? rc : 1;
  152. }
  153. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  154. {
  155. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  156. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  157. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  158. }
  159. /* For now, we only handle smbus */
  160. static const struct i2c_algorithm i2c_powermac_algorithm = {
  161. .smbus_xfer = i2c_powermac_smbus_xfer,
  162. .master_xfer = i2c_powermac_master_xfer,
  163. .functionality = i2c_powermac_func,
  164. };
  165. static int i2c_powermac_remove(struct platform_device *dev)
  166. {
  167. struct i2c_adapter *adapter = platform_get_drvdata(dev);
  168. struct pmac_i2c_bus *bus = i2c_get_adapdata(adapter);
  169. int rc;
  170. rc = i2c_del_adapter(adapter);
  171. pmac_i2c_detach_adapter(bus, adapter);
  172. i2c_set_adapdata(adapter, NULL);
  173. /* We aren't that prepared to deal with this... */
  174. if (rc)
  175. printk("i2c-powermac.c: Failed to remove bus %s !\n",
  176. adapter->name);
  177. platform_set_drvdata(dev, NULL);
  178. kfree(adapter);
  179. return 0;
  180. }
  181. static int __devexit i2c_powermac_probe(struct platform_device *dev)
  182. {
  183. struct pmac_i2c_bus *bus = dev->dev.platform_data;
  184. struct device_node *parent = NULL;
  185. struct i2c_adapter *adapter;
  186. char name[32];
  187. const char *basename;
  188. int rc;
  189. if (bus == NULL)
  190. return -EINVAL;
  191. /* Ok, now we need to make up a name for the interface that will
  192. * match what we used to do in the past, that is basically the
  193. * controller's parent device node for keywest. PMU didn't have a
  194. * naming convention and SMU has a different one
  195. */
  196. switch(pmac_i2c_get_type(bus)) {
  197. case pmac_i2c_bus_keywest:
  198. parent = of_get_parent(pmac_i2c_get_controller(bus));
  199. if (parent == NULL)
  200. return -EINVAL;
  201. basename = parent->name;
  202. break;
  203. case pmac_i2c_bus_pmu:
  204. basename = "pmu";
  205. break;
  206. case pmac_i2c_bus_smu:
  207. /* This is not what we used to do but I'm fixing drivers at
  208. * the same time as this change
  209. */
  210. basename = "smu";
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. snprintf(name, 32, "%s %d", basename, pmac_i2c_get_channel(bus));
  216. of_node_put(parent);
  217. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  218. if (adapter == NULL) {
  219. printk(KERN_ERR "i2c-powermac: can't allocate inteface !\n");
  220. return -ENOMEM;
  221. }
  222. platform_set_drvdata(dev, adapter);
  223. strcpy(adapter->name, name);
  224. adapter->algo = &i2c_powermac_algorithm;
  225. i2c_set_adapdata(adapter, bus);
  226. adapter->dev.parent = &dev->dev;
  227. pmac_i2c_attach_adapter(bus, adapter);
  228. rc = i2c_add_adapter(adapter);
  229. if (rc) {
  230. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  231. "failed\n", name);
  232. i2c_set_adapdata(adapter, NULL);
  233. pmac_i2c_detach_adapter(bus, adapter);
  234. }
  235. printk(KERN_INFO "PowerMac i2c bus %s registered\n", name);
  236. return rc;
  237. }
  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);