i2c-opal.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * IBM OPAL I2C driver
  3. * Copyright (C) 2014 IBM
  4. *
  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. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/i2c.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <asm/firmware.h>
  27. #include <asm/opal.h>
  28. static int i2c_opal_translate_error(int rc)
  29. {
  30. switch (rc) {
  31. case OPAL_NO_MEM:
  32. return -ENOMEM;
  33. case OPAL_PARAMETER:
  34. return -EINVAL;
  35. case OPAL_I2C_ARBT_LOST:
  36. return -EAGAIN;
  37. case OPAL_I2C_TIMEOUT:
  38. return -ETIMEDOUT;
  39. case OPAL_I2C_NACK_RCVD:
  40. return -ENXIO;
  41. case OPAL_I2C_STOP_ERR:
  42. return -EBUSY;
  43. default:
  44. return -EIO;
  45. }
  46. }
  47. static int i2c_opal_send_request(u32 bus_id, struct opal_i2c_request *req)
  48. {
  49. struct opal_msg msg;
  50. int token, rc;
  51. token = opal_async_get_token_interruptible();
  52. if (token < 0) {
  53. if (token != -ERESTARTSYS)
  54. pr_err("Failed to get the async token\n");
  55. return token;
  56. }
  57. rc = opal_i2c_request(token, bus_id, req);
  58. if (rc != OPAL_ASYNC_COMPLETION) {
  59. rc = i2c_opal_translate_error(rc);
  60. goto exit;
  61. }
  62. rc = opal_async_wait_response(token, &msg);
  63. if (rc)
  64. goto exit;
  65. rc = opal_get_async_rc(msg);
  66. if (rc != OPAL_SUCCESS) {
  67. rc = i2c_opal_translate_error(rc);
  68. goto exit;
  69. }
  70. exit:
  71. opal_async_release_token(token);
  72. return rc;
  73. }
  74. static int i2c_opal_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  75. int num)
  76. {
  77. unsigned long opal_id = (unsigned long)adap->algo_data;
  78. struct opal_i2c_request req;
  79. int rc, i;
  80. /* We only support fairly simple combinations here of one
  81. * or two messages
  82. */
  83. memset(&req, 0, sizeof(req));
  84. switch(num) {
  85. case 1:
  86. req.type = (msgs[0].flags & I2C_M_RD) ?
  87. OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
  88. req.addr = cpu_to_be16(msgs[0].addr);
  89. req.size = cpu_to_be32(msgs[0].len);
  90. req.buffer_ra = cpu_to_be64(__pa(msgs[0].buf));
  91. break;
  92. case 2:
  93. req.type = (msgs[1].flags & I2C_M_RD) ?
  94. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  95. req.addr = cpu_to_be16(msgs[0].addr);
  96. req.subaddr_sz = msgs[0].len;
  97. for (i = 0; i < msgs[0].len; i++)
  98. req.subaddr = (req.subaddr << 8) | msgs[0].buf[i];
  99. req.subaddr = cpu_to_be32(req.subaddr);
  100. req.size = cpu_to_be32(msgs[1].len);
  101. req.buffer_ra = cpu_to_be64(__pa(msgs[1].buf));
  102. break;
  103. }
  104. rc = i2c_opal_send_request(opal_id, &req);
  105. if (rc)
  106. return rc;
  107. return num;
  108. }
  109. static int i2c_opal_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  110. unsigned short flags, char read_write,
  111. u8 command, int size, union i2c_smbus_data *data)
  112. {
  113. unsigned long opal_id = (unsigned long)adap->algo_data;
  114. struct opal_i2c_request req;
  115. u8 local[2];
  116. int rc;
  117. memset(&req, 0, sizeof(req));
  118. req.addr = cpu_to_be16(addr);
  119. switch (size) {
  120. case I2C_SMBUS_BYTE:
  121. req.buffer_ra = cpu_to_be64(__pa(&data->byte));
  122. req.size = cpu_to_be32(1);
  123. /* Fall through */
  124. case I2C_SMBUS_QUICK:
  125. req.type = (read_write == I2C_SMBUS_READ) ?
  126. OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
  127. break;
  128. case I2C_SMBUS_BYTE_DATA:
  129. req.buffer_ra = cpu_to_be64(__pa(&data->byte));
  130. req.size = cpu_to_be32(1);
  131. req.subaddr = cpu_to_be32(command);
  132. req.subaddr_sz = 1;
  133. req.type = (read_write == I2C_SMBUS_READ) ?
  134. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  135. break;
  136. case I2C_SMBUS_WORD_DATA:
  137. if (!read_write) {
  138. local[0] = data->word & 0xff;
  139. local[1] = (data->word >> 8) & 0xff;
  140. }
  141. req.buffer_ra = cpu_to_be64(__pa(local));
  142. req.size = cpu_to_be32(2);
  143. req.subaddr = cpu_to_be32(command);
  144. req.subaddr_sz = 1;
  145. req.type = (read_write == I2C_SMBUS_READ) ?
  146. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  147. break;
  148. case I2C_SMBUS_I2C_BLOCK_DATA:
  149. req.buffer_ra = cpu_to_be64(__pa(&data->block[1]));
  150. req.size = cpu_to_be32(data->block[0]);
  151. req.subaddr = cpu_to_be32(command);
  152. req.subaddr_sz = 1;
  153. req.type = (read_write == I2C_SMBUS_READ) ?
  154. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. rc = i2c_opal_send_request(opal_id, &req);
  160. if (!rc && read_write && size == I2C_SMBUS_WORD_DATA) {
  161. data->word = ((u16)local[1]) << 8;
  162. data->word |= local[0];
  163. }
  164. return rc;
  165. }
  166. static u32 i2c_opal_func(struct i2c_adapter *adapter)
  167. {
  168. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  169. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  170. I2C_FUNC_SMBUS_I2C_BLOCK;
  171. }
  172. static const struct i2c_algorithm i2c_opal_algo = {
  173. .master_xfer = i2c_opal_master_xfer,
  174. .smbus_xfer = i2c_opal_smbus_xfer,
  175. .functionality = i2c_opal_func,
  176. };
  177. /*
  178. * For two messages, we basically support simple smbus transactions of a
  179. * write-then-anything.
  180. */
  181. static const struct i2c_adapter_quirks i2c_opal_quirks = {
  182. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
  183. .max_comb_1st_msg_len = 4,
  184. };
  185. static int i2c_opal_probe(struct platform_device *pdev)
  186. {
  187. struct i2c_adapter *adapter;
  188. const char *pname;
  189. u32 opal_id;
  190. int rc;
  191. if (!pdev->dev.of_node)
  192. return -ENODEV;
  193. rc = of_property_read_u32(pdev->dev.of_node, "ibm,opal-id", &opal_id);
  194. if (rc) {
  195. dev_err(&pdev->dev, "Missing ibm,opal-id property !\n");
  196. return -EIO;
  197. }
  198. adapter = devm_kzalloc(&pdev->dev, sizeof(*adapter), GFP_KERNEL);
  199. if (!adapter)
  200. return -ENOMEM;
  201. adapter->algo = &i2c_opal_algo;
  202. adapter->algo_data = (void *)(unsigned long)opal_id;
  203. adapter->quirks = &i2c_opal_quirks;
  204. adapter->dev.parent = &pdev->dev;
  205. adapter->dev.of_node = of_node_get(pdev->dev.of_node);
  206. pname = of_get_property(pdev->dev.of_node, "ibm,port-name", NULL);
  207. if (pname)
  208. strlcpy(adapter->name, pname, sizeof(adapter->name));
  209. else
  210. strlcpy(adapter->name, "opal", sizeof(adapter->name));
  211. platform_set_drvdata(pdev, adapter);
  212. rc = i2c_add_adapter(adapter);
  213. if (rc)
  214. dev_err(&pdev->dev, "Failed to register the i2c adapter\n");
  215. return rc;
  216. }
  217. static int i2c_opal_remove(struct platform_device *pdev)
  218. {
  219. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  220. i2c_del_adapter(adapter);
  221. return 0;
  222. }
  223. static const struct of_device_id i2c_opal_of_match[] = {
  224. {
  225. .compatible = "ibm,opal-i2c",
  226. },
  227. { }
  228. };
  229. MODULE_DEVICE_TABLE(of, i2c_opal_of_match);
  230. static struct platform_driver i2c_opal_driver = {
  231. .probe = i2c_opal_probe,
  232. .remove = i2c_opal_remove,
  233. .driver = {
  234. .name = "i2c-opal",
  235. .of_match_table = i2c_opal_of_match,
  236. },
  237. };
  238. static int __init i2c_opal_init(void)
  239. {
  240. if (!firmware_has_feature(FW_FEATURE_OPAL))
  241. return -ENODEV;
  242. return platform_driver_register(&i2c_opal_driver);
  243. }
  244. module_init(i2c_opal_init);
  245. static void __exit i2c_opal_exit(void)
  246. {
  247. return platform_driver_unregister(&i2c_opal_driver);
  248. }
  249. module_exit(i2c_opal_exit);
  250. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  251. MODULE_DESCRIPTION("IBM OPAL I2C driver");
  252. MODULE_LICENSE("GPL");