i2c-powermac.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 (num != 1)
  137. return -EINVAL;
  138. if (msgs->flags & I2C_M_TEN)
  139. return -EINVAL;
  140. read = (msgs->flags & I2C_M_RD) != 0;
  141. addrdir = (msgs->addr << 1) | read;
  142. if (msgs->flags & I2C_M_REV_DIR_ADDR)
  143. addrdir ^= 1;
  144. rc = pmac_i2c_open(bus, 0);
  145. if (rc)
  146. return rc;
  147. rc = pmac_i2c_setmode(bus, pmac_i2c_mode_std);
  148. if (rc)
  149. goto bail;
  150. rc = pmac_i2c_xfer(bus, addrdir, 0, 0, msgs->buf, msgs->len);
  151. bail:
  152. pmac_i2c_close(bus);
  153. return rc < 0 ? rc : msgs->len;
  154. }
  155. static u32 i2c_powermac_func(struct i2c_adapter * adapter)
  156. {
  157. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  158. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  159. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_I2C;
  160. }
  161. /* For now, we only handle smbus */
  162. static struct i2c_algorithm i2c_powermac_algorithm = {
  163. .smbus_xfer = i2c_powermac_smbus_xfer,
  164. .master_xfer = i2c_powermac_master_xfer,
  165. .functionality = i2c_powermac_func,
  166. };
  167. static int i2c_powermac_remove(struct device *dev)
  168. {
  169. struct i2c_adapter *adapter = dev_get_drvdata(dev);
  170. struct pmac_i2c_bus *bus = i2c_get_adapdata(adapter);
  171. int rc;
  172. rc = i2c_del_adapter(adapter);
  173. pmac_i2c_detach_adapter(bus, adapter);
  174. i2c_set_adapdata(adapter, NULL);
  175. /* We aren't that prepared to deal with this... */
  176. if (rc)
  177. printk("i2c-powermac.c: Failed to remove bus %s !\n",
  178. adapter->name);
  179. dev_set_drvdata(dev, NULL);
  180. kfree(adapter);
  181. return 0;
  182. }
  183. static int i2c_powermac_probe(struct device *dev)
  184. {
  185. struct pmac_i2c_bus *bus = dev->platform_data;
  186. struct device_node *parent = NULL;
  187. struct i2c_adapter *adapter;
  188. char name[32], *basename;
  189. int rc;
  190. if (bus == NULL)
  191. return -EINVAL;
  192. /* Ok, now we need to make up a name for the interface that will
  193. * match what we used to do in the past, that is basically the
  194. * controller's parent device node for keywest. PMU didn't have a
  195. * naming convention and SMU has a different one
  196. */
  197. switch(pmac_i2c_get_type(bus)) {
  198. case pmac_i2c_bus_keywest:
  199. parent = of_get_parent(pmac_i2c_get_controller(bus));
  200. if (parent == NULL)
  201. return -EINVAL;
  202. basename = parent->name;
  203. break;
  204. case pmac_i2c_bus_pmu:
  205. basename = "pmu";
  206. break;
  207. case pmac_i2c_bus_smu:
  208. /* This is not what we used to do but I'm fixing drivers at
  209. * the same time as this change
  210. */
  211. basename = "smu";
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. snprintf(name, 32, "%s %d", basename, pmac_i2c_get_channel(bus));
  217. of_node_put(parent);
  218. adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  219. if (adapter == NULL) {
  220. printk(KERN_ERR "i2c-powermac: can't allocate inteface !\n");
  221. return -ENOMEM;
  222. }
  223. dev_set_drvdata(dev, adapter);
  224. strcpy(adapter->name, name);
  225. adapter->algo = &i2c_powermac_algorithm;
  226. i2c_set_adapdata(adapter, bus);
  227. adapter->dev.parent = dev;
  228. pmac_i2c_attach_adapter(bus, adapter);
  229. rc = i2c_add_adapter(adapter);
  230. if (rc) {
  231. printk(KERN_ERR "i2c-powermac: Adapter %s registration "
  232. "failed\n", name);
  233. i2c_set_adapdata(adapter, NULL);
  234. pmac_i2c_detach_adapter(bus, adapter);
  235. }
  236. printk(KERN_INFO "PowerMac i2c bus %s registered\n", name);
  237. return rc;
  238. }
  239. static struct device_driver i2c_powermac_driver = {
  240. .name = "i2c-powermac",
  241. .bus = &platform_bus_type,
  242. .probe = i2c_powermac_probe,
  243. .remove = i2c_powermac_remove,
  244. };
  245. static int __init i2c_powermac_init(void)
  246. {
  247. driver_register(&i2c_powermac_driver);
  248. return 0;
  249. }
  250. static void __exit i2c_powermac_cleanup(void)
  251. {
  252. driver_unregister(&i2c_powermac_driver);
  253. }
  254. module_init(i2c_powermac_init);
  255. module_exit(i2c_powermac_cleanup);