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