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