i2c-opal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 = be64_to_cpu(msg.params[1]);
  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 0:
  86. return 0;
  87. case 1:
  88. req.type = (msgs[0].flags & I2C_M_RD) ?
  89. OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
  90. req.addr = cpu_to_be16(msgs[0].addr);
  91. req.size = cpu_to_be32(msgs[0].len);
  92. req.buffer_ra = cpu_to_be64(__pa(msgs[0].buf));
  93. break;
  94. case 2:
  95. req.type = (msgs[1].flags & I2C_M_RD) ?
  96. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  97. req.addr = cpu_to_be16(msgs[0].addr);
  98. req.subaddr_sz = msgs[0].len;
  99. for (i = 0; i < msgs[0].len; i++)
  100. req.subaddr = (req.subaddr << 8) | msgs[0].buf[i];
  101. req.subaddr = cpu_to_be32(req.subaddr);
  102. req.size = cpu_to_be32(msgs[1].len);
  103. req.buffer_ra = cpu_to_be64(__pa(msgs[1].buf));
  104. break;
  105. default:
  106. return -EOPNOTSUPP;
  107. }
  108. rc = i2c_opal_send_request(opal_id, &req);
  109. if (rc)
  110. return rc;
  111. return num;
  112. }
  113. static int i2c_opal_smbus_xfer(struct i2c_adapter *adap, u16 addr,
  114. unsigned short flags, char read_write,
  115. u8 command, int size, union i2c_smbus_data *data)
  116. {
  117. unsigned long opal_id = (unsigned long)adap->algo_data;
  118. struct opal_i2c_request req;
  119. u8 local[2];
  120. int rc;
  121. memset(&req, 0, sizeof(req));
  122. req.addr = cpu_to_be16(addr);
  123. switch (size) {
  124. case I2C_SMBUS_BYTE:
  125. req.buffer_ra = cpu_to_be64(__pa(&data->byte));
  126. req.size = cpu_to_be32(1);
  127. /* Fall through */
  128. case I2C_SMBUS_QUICK:
  129. req.type = (read_write == I2C_SMBUS_READ) ?
  130. OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
  131. break;
  132. case I2C_SMBUS_BYTE_DATA:
  133. req.buffer_ra = cpu_to_be64(__pa(&data->byte));
  134. req.size = cpu_to_be32(1);
  135. req.subaddr = cpu_to_be32(command);
  136. req.subaddr_sz = 1;
  137. req.type = (read_write == I2C_SMBUS_READ) ?
  138. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  139. break;
  140. case I2C_SMBUS_WORD_DATA:
  141. if (!read_write) {
  142. local[0] = data->word & 0xff;
  143. local[1] = (data->word >> 8) & 0xff;
  144. }
  145. req.buffer_ra = cpu_to_be64(__pa(local));
  146. req.size = cpu_to_be32(2);
  147. req.subaddr = cpu_to_be32(command);
  148. req.subaddr_sz = 1;
  149. req.type = (read_write == I2C_SMBUS_READ) ?
  150. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  151. break;
  152. case I2C_SMBUS_I2C_BLOCK_DATA:
  153. req.buffer_ra = cpu_to_be64(__pa(&data->block[1]));
  154. req.size = cpu_to_be32(data->block[0]);
  155. req.subaddr = cpu_to_be32(command);
  156. req.subaddr_sz = 1;
  157. req.type = (read_write == I2C_SMBUS_READ) ?
  158. OPAL_I2C_SM_READ : OPAL_I2C_SM_WRITE;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. rc = i2c_opal_send_request(opal_id, &req);
  164. if (!rc && read_write && size == I2C_SMBUS_WORD_DATA) {
  165. data->word = ((u16)local[1]) << 8;
  166. data->word |= local[0];
  167. }
  168. return rc;
  169. }
  170. static u32 i2c_opal_func(struct i2c_adapter *adapter)
  171. {
  172. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  173. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  174. I2C_FUNC_SMBUS_I2C_BLOCK;
  175. }
  176. static const struct i2c_algorithm i2c_opal_algo = {
  177. .master_xfer = i2c_opal_master_xfer,
  178. .smbus_xfer = i2c_opal_smbus_xfer,
  179. .functionality = i2c_opal_func,
  180. };
  181. /*
  182. * For two messages, we basically support simple smbus transactions of a
  183. * write-then-anything.
  184. */
  185. static struct i2c_adapter_quirks i2c_opal_quirks = {
  186. .flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
  187. .max_comb_1st_msg_len = 4,
  188. };
  189. static int i2c_opal_probe(struct platform_device *pdev)
  190. {
  191. struct i2c_adapter *adapter;
  192. const char *pname;
  193. u32 opal_id;
  194. int rc;
  195. if (!pdev->dev.of_node)
  196. return -ENODEV;
  197. rc = of_property_read_u32(pdev->dev.of_node, "ibm,opal-id", &opal_id);
  198. if (rc) {
  199. dev_err(&pdev->dev, "Missing ibm,opal-id property !\n");
  200. return -EIO;
  201. }
  202. adapter = devm_kzalloc(&pdev->dev, sizeof(*adapter), GFP_KERNEL);
  203. if (!adapter)
  204. return -ENOMEM;
  205. adapter->algo = &i2c_opal_algo;
  206. adapter->algo_data = (void *)(unsigned long)opal_id;
  207. adapter->quirks = &i2c_opal_quirks;
  208. adapter->dev.parent = &pdev->dev;
  209. adapter->dev.of_node = of_node_get(pdev->dev.of_node);
  210. pname = of_get_property(pdev->dev.of_node, "ibm,port-name", NULL);
  211. if (pname)
  212. strlcpy(adapter->name, pname, sizeof(adapter->name));
  213. else
  214. strlcpy(adapter->name, "opal", sizeof(adapter->name));
  215. platform_set_drvdata(pdev, adapter);
  216. rc = i2c_add_adapter(adapter);
  217. if (rc)
  218. dev_err(&pdev->dev, "Failed to register the i2c adapter\n");
  219. return rc;
  220. }
  221. static int i2c_opal_remove(struct platform_device *pdev)
  222. {
  223. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  224. i2c_del_adapter(adapter);
  225. return 0;
  226. }
  227. static const struct of_device_id i2c_opal_of_match[] = {
  228. {
  229. .compatible = "ibm,opal-i2c",
  230. },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(of, i2c_opal_of_match);
  234. static struct platform_driver i2c_opal_driver = {
  235. .probe = i2c_opal_probe,
  236. .remove = i2c_opal_remove,
  237. .driver = {
  238. .name = "i2c-opal",
  239. .of_match_table = i2c_opal_of_match,
  240. },
  241. };
  242. static int __init i2c_opal_init(void)
  243. {
  244. if (!firmware_has_feature(FW_FEATURE_OPAL))
  245. return -ENODEV;
  246. return platform_driver_register(&i2c_opal_driver);
  247. }
  248. module_init(i2c_opal_init);
  249. static void __exit i2c_opal_exit(void)
  250. {
  251. return platform_driver_unregister(&i2c_opal_driver);
  252. }
  253. module_exit(i2c_opal_exit);
  254. MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
  255. MODULE_DESCRIPTION("IBM OPAL I2C driver");
  256. MODULE_LICENSE("GPL");