i2c-pmac-smu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <asm/prom.h>
  26. #include <asm/of_device.h>
  27. #include <asm/smu.h>
  28. static int probe;
  29. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  30. MODULE_DESCRIPTION("I2C driver for Apple's SMU");
  31. MODULE_LICENSE("GPL");
  32. module_param(probe, bool, 0);
  33. /* Physical interface */
  34. struct smu_iface
  35. {
  36. struct i2c_adapter adapter;
  37. struct completion complete;
  38. u32 busid;
  39. };
  40. static void smu_i2c_done(struct smu_i2c_cmd *cmd, void *misc)
  41. {
  42. struct smu_iface *iface = misc;
  43. complete(&iface->complete);
  44. }
  45. /*
  46. * SMBUS-type transfer entrypoint
  47. */
  48. static s32 smu_smbus_xfer( struct i2c_adapter* adap,
  49. u16 addr,
  50. unsigned short flags,
  51. char read_write,
  52. u8 command,
  53. int size,
  54. union i2c_smbus_data* data)
  55. {
  56. struct smu_iface *iface = i2c_get_adapdata(adap);
  57. struct smu_i2c_cmd cmd;
  58. int rc = 0;
  59. int read = (read_write == I2C_SMBUS_READ);
  60. cmd.info.bus = iface->busid;
  61. cmd.info.devaddr = (addr << 1) | (read ? 0x01 : 0x00);
  62. /* Prepare datas & select mode */
  63. switch (size) {
  64. case I2C_SMBUS_QUICK:
  65. cmd.info.type = SMU_I2C_TRANSFER_SIMPLE;
  66. cmd.info.datalen = 0;
  67. break;
  68. case I2C_SMBUS_BYTE:
  69. cmd.info.type = SMU_I2C_TRANSFER_SIMPLE;
  70. cmd.info.datalen = 1;
  71. if (!read)
  72. cmd.info.data[0] = data->byte;
  73. break;
  74. case I2C_SMBUS_BYTE_DATA:
  75. cmd.info.type = SMU_I2C_TRANSFER_STDSUB;
  76. cmd.info.datalen = 1;
  77. cmd.info.sublen = 1;
  78. cmd.info.subaddr[0] = command;
  79. cmd.info.subaddr[1] = 0;
  80. cmd.info.subaddr[2] = 0;
  81. if (!read)
  82. cmd.info.data[0] = data->byte;
  83. break;
  84. case I2C_SMBUS_WORD_DATA:
  85. cmd.info.type = SMU_I2C_TRANSFER_STDSUB;
  86. cmd.info.datalen = 2;
  87. cmd.info.sublen = 1;
  88. cmd.info.subaddr[0] = command;
  89. cmd.info.subaddr[1] = 0;
  90. cmd.info.subaddr[2] = 0;
  91. if (!read) {
  92. cmd.info.data[0] = data->byte & 0xff;
  93. cmd.info.data[1] = (data->byte >> 8) & 0xff;
  94. }
  95. break;
  96. /* Note that these are broken vs. the expected smbus API where
  97. * on reads, the lenght is actually returned from the function,
  98. * but I think the current API makes no sense and I don't want
  99. * any driver that I haven't verified for correctness to go
  100. * anywhere near a pmac i2c bus anyway ...
  101. */
  102. case I2C_SMBUS_BLOCK_DATA:
  103. cmd.info.type = SMU_I2C_TRANSFER_STDSUB;
  104. cmd.info.datalen = data->block[0] + 1;
  105. if (cmd.info.datalen > 6)
  106. return -EINVAL;
  107. if (!read)
  108. memcpy(cmd.info.data, data->block, cmd.info.datalen);
  109. cmd.info.sublen = 1;
  110. cmd.info.subaddr[0] = command;
  111. cmd.info.subaddr[1] = 0;
  112. cmd.info.subaddr[2] = 0;
  113. break;
  114. case I2C_SMBUS_I2C_BLOCK_DATA:
  115. cmd.info.type = SMU_I2C_TRANSFER_STDSUB;
  116. cmd.info.datalen = data->block[0];
  117. if (cmd.info.datalen > 7)
  118. return -EINVAL;
  119. if (!read)
  120. memcpy(cmd.info.data, &data->block[1],
  121. cmd.info.datalen);
  122. cmd.info.sublen = 1;
  123. cmd.info.subaddr[0] = command;
  124. cmd.info.subaddr[1] = 0;
  125. cmd.info.subaddr[2] = 0;
  126. break;
  127. default:
  128. return -EINVAL;
  129. }
  130. /* Turn a standardsub read into a combined mode access */
  131. if (read_write == I2C_SMBUS_READ &&
  132. cmd.info.type == SMU_I2C_TRANSFER_STDSUB)
  133. cmd.info.type = SMU_I2C_TRANSFER_COMBINED;
  134. /* Finish filling command and submit it */
  135. cmd.done = smu_i2c_done;
  136. cmd.misc = iface;
  137. rc = smu_queue_i2c(&cmd);
  138. if (rc < 0)
  139. return rc;
  140. wait_for_completion(&iface->complete);
  141. rc = cmd.status;
  142. if (!read || rc < 0)
  143. return rc;
  144. switch (size) {
  145. case I2C_SMBUS_BYTE:
  146. case I2C_SMBUS_BYTE_DATA:
  147. data->byte = cmd.info.data[0];
  148. break;
  149. case I2C_SMBUS_WORD_DATA:
  150. data->word = ((u16)cmd.info.data[1]) << 8;
  151. data->word |= cmd.info.data[0];
  152. break;
  153. /* Note that these are broken vs. the expected smbus API where
  154. * on reads, the lenght is actually returned from the function,
  155. * but I think the current API makes no sense and I don't want
  156. * any driver that I haven't verified for correctness to go
  157. * anywhere near a pmac i2c bus anyway ...
  158. */
  159. case I2C_SMBUS_BLOCK_DATA:
  160. case I2C_SMBUS_I2C_BLOCK_DATA:
  161. memcpy(&data->block[0], cmd.info.data, cmd.info.datalen);
  162. break;
  163. }
  164. return rc;
  165. }
  166. static u32
  167. smu_smbus_func(struct i2c_adapter * adapter)
  168. {
  169. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  170. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  171. I2C_FUNC_SMBUS_BLOCK_DATA;
  172. }
  173. /* For now, we only handle combined mode (smbus) */
  174. static struct i2c_algorithm smu_algorithm = {
  175. .smbus_xfer = smu_smbus_xfer,
  176. .functionality = smu_smbus_func,
  177. };
  178. static int create_iface(struct device_node *np, struct device *dev)
  179. {
  180. struct smu_iface* iface;
  181. u32 *reg, busid;
  182. int rc;
  183. reg = (u32 *)get_property(np, "reg", NULL);
  184. if (reg == NULL) {
  185. printk(KERN_ERR "i2c-pmac-smu: can't find bus number !\n");
  186. return -ENXIO;
  187. }
  188. busid = *reg;
  189. iface = kmalloc(sizeof(struct smu_iface), GFP_KERNEL);
  190. if (iface == NULL) {
  191. printk(KERN_ERR "i2c-pmac-smu: can't allocate inteface !\n");
  192. return -ENOMEM;
  193. }
  194. memset(iface, 0, sizeof(struct smu_iface));
  195. init_completion(&iface->complete);
  196. iface->busid = busid;
  197. dev_set_drvdata(dev, iface);
  198. sprintf(iface->adapter.name, "smu-i2c-%02x", busid);
  199. iface->adapter.algo = &smu_algorithm;
  200. iface->adapter.algo_data = NULL;
  201. iface->adapter.client_register = NULL;
  202. iface->adapter.client_unregister = NULL;
  203. i2c_set_adapdata(&iface->adapter, iface);
  204. iface->adapter.dev.parent = dev;
  205. rc = i2c_add_adapter(&iface->adapter);
  206. if (rc) {
  207. printk(KERN_ERR "i2c-pamc-smu.c: Adapter %s registration "
  208. "failed\n", iface->adapter.name);
  209. i2c_set_adapdata(&iface->adapter, NULL);
  210. }
  211. if (probe) {
  212. unsigned char addr;
  213. printk("Probe: ");
  214. for (addr = 0x00; addr <= 0x7f; addr++) {
  215. if (i2c_smbus_xfer(&iface->adapter,addr,
  216. 0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  217. printk("%02x ", addr);
  218. }
  219. printk("\n");
  220. }
  221. printk(KERN_INFO "SMU i2c bus %x registered\n", busid);
  222. return 0;
  223. }
  224. static int dispose_iface(struct device *dev)
  225. {
  226. struct smu_iface *iface = dev_get_drvdata(dev);
  227. int rc;
  228. rc = i2c_del_adapter(&iface->adapter);
  229. i2c_set_adapdata(&iface->adapter, NULL);
  230. /* We aren't that prepared to deal with this... */
  231. if (rc)
  232. printk("i2c-pmac-smu.c: Failed to remove bus %s !\n",
  233. iface->adapter.name);
  234. dev_set_drvdata(dev, NULL);
  235. kfree(iface);
  236. return 0;
  237. }
  238. static int create_iface_of_platform(struct of_device* dev,
  239. const struct of_device_id *match)
  240. {
  241. return create_iface(dev->node, &dev->dev);
  242. }
  243. static int dispose_iface_of_platform(struct of_device* dev)
  244. {
  245. return dispose_iface(&dev->dev);
  246. }
  247. static struct of_device_id i2c_smu_match[] =
  248. {
  249. {
  250. .compatible = "smu-i2c",
  251. },
  252. {},
  253. };
  254. static struct of_platform_driver i2c_smu_of_platform_driver =
  255. {
  256. .name = "i2c-smu",
  257. .match_table = i2c_smu_match,
  258. .probe = create_iface_of_platform,
  259. .remove = dispose_iface_of_platform
  260. };
  261. static int __init i2c_pmac_smu_init(void)
  262. {
  263. of_register_driver(&i2c_smu_of_platform_driver);
  264. return 0;
  265. }
  266. static void __exit i2c_pmac_smu_cleanup(void)
  267. {
  268. of_unregister_driver(&i2c_smu_of_platform_driver);
  269. }
  270. module_init(i2c_pmac_smu_init);
  271. module_exit(i2c_pmac_smu_cleanup);