i2c-xgene-slimpro.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * X-Gene SLIMpro I2C Driver
  3. *
  4. * Copyright (c) 2014, Applied Micro Circuits Corporation
  5. * Author: Feng Kan <fkan@apm.com>
  6. * Author: Hieu Le <hnle@apm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * This driver provides support for X-Gene SLIMpro I2C device access
  22. * using the APM X-Gene SLIMpro mailbox driver.
  23. *
  24. */
  25. #include <linux/acpi.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/i2c.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/mailbox_client.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/version.h>
  34. #define MAILBOX_OP_TIMEOUT 1000 /* Operation time out in ms */
  35. #define MAILBOX_I2C_INDEX 0
  36. #define SLIMPRO_IIC_BUS 1 /* Use I2C bus 1 only */
  37. #define SMBUS_CMD_LEN 1
  38. #define BYTE_DATA 1
  39. #define WORD_DATA 2
  40. #define BLOCK_DATA 3
  41. #define SLIMPRO_IIC_I2C_PROTOCOL 0
  42. #define SLIMPRO_IIC_SMB_PROTOCOL 1
  43. #define SLIMPRO_IIC_READ 0
  44. #define SLIMPRO_IIC_WRITE 1
  45. #define IIC_SMB_WITHOUT_DATA_LEN 0
  46. #define IIC_SMB_WITH_DATA_LEN 1
  47. #define SLIMPRO_DEBUG_MSG 0
  48. #define SLIMPRO_MSG_TYPE_SHIFT 28
  49. #define SLIMPRO_DBG_SUBTYPE_I2C1READ 4
  50. #define SLIMPRO_DBGMSG_TYPE_SHIFT 24
  51. #define SLIMPRO_DBGMSG_TYPE_MASK 0x0F000000U
  52. #define SLIMPRO_IIC_DEV_SHIFT 23
  53. #define SLIMPRO_IIC_DEV_MASK 0x00800000U
  54. #define SLIMPRO_IIC_DEVID_SHIFT 13
  55. #define SLIMPRO_IIC_DEVID_MASK 0x007FE000U
  56. #define SLIMPRO_IIC_RW_SHIFT 12
  57. #define SLIMPRO_IIC_RW_MASK 0x00001000U
  58. #define SLIMPRO_IIC_PROTO_SHIFT 11
  59. #define SLIMPRO_IIC_PROTO_MASK 0x00000800U
  60. #define SLIMPRO_IIC_ADDRLEN_SHIFT 8
  61. #define SLIMPRO_IIC_ADDRLEN_MASK 0x00000700U
  62. #define SLIMPRO_IIC_DATALEN_SHIFT 0
  63. #define SLIMPRO_IIC_DATALEN_MASK 0x000000FFU
  64. /*
  65. * SLIMpro I2C message encode
  66. *
  67. * dev - Controller number (0-based)
  68. * chip - I2C chip address
  69. * op - SLIMPRO_IIC_READ or SLIMPRO_IIC_WRITE
  70. * proto - SLIMPRO_IIC_SMB_PROTOCOL or SLIMPRO_IIC_I2C_PROTOCOL
  71. * addrlen - Length of the address field
  72. * datalen - Length of the data field
  73. */
  74. #define SLIMPRO_IIC_ENCODE_MSG(dev, chip, op, proto, addrlen, datalen) \
  75. ((SLIMPRO_DEBUG_MSG << SLIMPRO_MSG_TYPE_SHIFT) | \
  76. ((SLIMPRO_DBG_SUBTYPE_I2C1READ << SLIMPRO_DBGMSG_TYPE_SHIFT) & \
  77. SLIMPRO_DBGMSG_TYPE_MASK) | \
  78. ((dev << SLIMPRO_IIC_DEV_SHIFT) & SLIMPRO_IIC_DEV_MASK) | \
  79. ((chip << SLIMPRO_IIC_DEVID_SHIFT) & SLIMPRO_IIC_DEVID_MASK) | \
  80. ((op << SLIMPRO_IIC_RW_SHIFT) & SLIMPRO_IIC_RW_MASK) | \
  81. ((proto << SLIMPRO_IIC_PROTO_SHIFT) & SLIMPRO_IIC_PROTO_MASK) | \
  82. ((addrlen << SLIMPRO_IIC_ADDRLEN_SHIFT) & SLIMPRO_IIC_ADDRLEN_MASK) | \
  83. ((datalen << SLIMPRO_IIC_DATALEN_SHIFT) & SLIMPRO_IIC_DATALEN_MASK))
  84. /*
  85. * Encode for upper address for block data
  86. */
  87. #define SLIMPRO_IIC_ENCODE_FLAG_BUFADDR 0x80000000
  88. #define SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(a) ((u32) (((a) << 30) \
  89. & 0x40000000))
  90. #define SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(a) ((u32) (((a) >> 12) \
  91. & 0x3FF00000))
  92. #define SLIMPRO_IIC_ENCODE_ADDR(a) ((a) & 0x000FFFFF)
  93. struct slimpro_i2c_dev {
  94. struct i2c_adapter adapter;
  95. struct device *dev;
  96. struct mbox_chan *mbox_chan;
  97. struct mbox_client mbox_client;
  98. struct completion rd_complete;
  99. u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* dma_buffer[0] is used for length */
  100. u32 *resp_msg;
  101. };
  102. #define to_slimpro_i2c_dev(cl) \
  103. container_of(cl, struct slimpro_i2c_dev, mbox_client)
  104. static void slimpro_i2c_rx_cb(struct mbox_client *cl, void *mssg)
  105. {
  106. struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
  107. /*
  108. * Response message format:
  109. * mssg[0] is the return code of the operation
  110. * mssg[1] is the first data word
  111. * mssg[2] is NOT used
  112. */
  113. if (ctx->resp_msg)
  114. *ctx->resp_msg = ((u32 *)mssg)[1];
  115. if (ctx->mbox_client.tx_block)
  116. complete(&ctx->rd_complete);
  117. }
  118. static int start_i2c_msg_xfer(struct slimpro_i2c_dev *ctx)
  119. {
  120. if (ctx->mbox_client.tx_block) {
  121. if (!wait_for_completion_timeout(&ctx->rd_complete,
  122. msecs_to_jiffies(MAILBOX_OP_TIMEOUT)))
  123. return -ETIMEDOUT;
  124. }
  125. /* Check of invalid data or no device */
  126. if (*ctx->resp_msg == 0xffffffff)
  127. return -ENODEV;
  128. return 0;
  129. }
  130. static int slimpro_i2c_rd(struct slimpro_i2c_dev *ctx, u32 chip,
  131. u32 addr, u32 addrlen, u32 protocol,
  132. u32 readlen, u32 *data)
  133. {
  134. u32 msg[3];
  135. int rc;
  136. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip,
  137. SLIMPRO_IIC_READ, protocol, addrlen, readlen);
  138. msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr);
  139. msg[2] = 0;
  140. ctx->resp_msg = data;
  141. rc = mbox_send_message(ctx->mbox_chan, &msg);
  142. if (rc < 0)
  143. goto err;
  144. rc = start_i2c_msg_xfer(ctx);
  145. err:
  146. ctx->resp_msg = NULL;
  147. return rc;
  148. }
  149. static int slimpro_i2c_wr(struct slimpro_i2c_dev *ctx, u32 chip,
  150. u32 addr, u32 addrlen, u32 protocol, u32 writelen,
  151. u32 data)
  152. {
  153. u32 msg[3];
  154. int rc;
  155. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip,
  156. SLIMPRO_IIC_WRITE, protocol, addrlen, writelen);
  157. msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr);
  158. msg[2] = data;
  159. ctx->resp_msg = msg;
  160. rc = mbox_send_message(ctx->mbox_chan, &msg);
  161. if (rc < 0)
  162. goto err;
  163. rc = start_i2c_msg_xfer(ctx);
  164. err:
  165. ctx->resp_msg = NULL;
  166. return rc;
  167. }
  168. static int slimpro_i2c_blkrd(struct slimpro_i2c_dev *ctx, u32 chip, u32 addr,
  169. u32 addrlen, u32 protocol, u32 readlen,
  170. u32 with_data_len, void *data)
  171. {
  172. dma_addr_t paddr;
  173. u32 msg[3];
  174. int rc;
  175. paddr = dma_map_single(ctx->dev, ctx->dma_buffer, readlen, DMA_FROM_DEVICE);
  176. if (dma_mapping_error(ctx->dev, paddr)) {
  177. dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n",
  178. ctx->dma_buffer);
  179. rc = -ENOMEM;
  180. goto err;
  181. }
  182. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_READ,
  183. protocol, addrlen, readlen);
  184. msg[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR |
  185. SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(with_data_len) |
  186. SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) |
  187. SLIMPRO_IIC_ENCODE_ADDR(addr);
  188. msg[2] = (u32)paddr;
  189. ctx->resp_msg = msg;
  190. rc = mbox_send_message(ctx->mbox_chan, &msg);
  191. if (rc < 0)
  192. goto err_unmap;
  193. rc = start_i2c_msg_xfer(ctx);
  194. /* Copy to destination */
  195. memcpy(data, ctx->dma_buffer, readlen);
  196. err_unmap:
  197. dma_unmap_single(ctx->dev, paddr, readlen, DMA_FROM_DEVICE);
  198. err:
  199. ctx->resp_msg = NULL;
  200. return rc;
  201. }
  202. static int slimpro_i2c_blkwr(struct slimpro_i2c_dev *ctx, u32 chip,
  203. u32 addr, u32 addrlen, u32 protocol, u32 writelen,
  204. void *data)
  205. {
  206. dma_addr_t paddr;
  207. u32 msg[3];
  208. int rc;
  209. memcpy(ctx->dma_buffer, data, writelen);
  210. paddr = dma_map_single(ctx->dev, ctx->dma_buffer, writelen,
  211. DMA_TO_DEVICE);
  212. if (dma_mapping_error(ctx->dev, paddr)) {
  213. dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n",
  214. ctx->dma_buffer);
  215. rc = -ENOMEM;
  216. goto err;
  217. }
  218. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_WRITE,
  219. protocol, addrlen, writelen);
  220. msg[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR |
  221. SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) |
  222. SLIMPRO_IIC_ENCODE_ADDR(addr);
  223. msg[2] = (u32)paddr;
  224. ctx->resp_msg = msg;
  225. if (ctx->mbox_client.tx_block)
  226. reinit_completion(&ctx->rd_complete);
  227. rc = mbox_send_message(ctx->mbox_chan, &msg);
  228. if (rc < 0)
  229. goto err_unmap;
  230. rc = start_i2c_msg_xfer(ctx);
  231. err_unmap:
  232. dma_unmap_single(ctx->dev, paddr, writelen, DMA_TO_DEVICE);
  233. err:
  234. ctx->resp_msg = NULL;
  235. return rc;
  236. }
  237. static int xgene_slimpro_i2c_xfer(struct i2c_adapter *adap, u16 addr,
  238. unsigned short flags, char read_write,
  239. u8 command, int size,
  240. union i2c_smbus_data *data)
  241. {
  242. struct slimpro_i2c_dev *ctx = i2c_get_adapdata(adap);
  243. int ret = -EOPNOTSUPP;
  244. u32 val;
  245. switch (size) {
  246. case I2C_SMBUS_BYTE:
  247. if (read_write == I2C_SMBUS_READ) {
  248. ret = slimpro_i2c_rd(ctx, addr, 0, 0,
  249. SLIMPRO_IIC_SMB_PROTOCOL,
  250. BYTE_DATA, &val);
  251. data->byte = val;
  252. } else {
  253. ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
  254. SLIMPRO_IIC_SMB_PROTOCOL,
  255. 0, 0);
  256. }
  257. break;
  258. case I2C_SMBUS_BYTE_DATA:
  259. if (read_write == I2C_SMBUS_READ) {
  260. ret = slimpro_i2c_rd(ctx, addr, command, SMBUS_CMD_LEN,
  261. SLIMPRO_IIC_SMB_PROTOCOL,
  262. BYTE_DATA, &val);
  263. data->byte = val;
  264. } else {
  265. val = data->byte;
  266. ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
  267. SLIMPRO_IIC_SMB_PROTOCOL,
  268. BYTE_DATA, val);
  269. }
  270. break;
  271. case I2C_SMBUS_WORD_DATA:
  272. if (read_write == I2C_SMBUS_READ) {
  273. ret = slimpro_i2c_rd(ctx, addr, command, SMBUS_CMD_LEN,
  274. SLIMPRO_IIC_SMB_PROTOCOL,
  275. WORD_DATA, &val);
  276. data->word = val;
  277. } else {
  278. val = data->word;
  279. ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
  280. SLIMPRO_IIC_SMB_PROTOCOL,
  281. WORD_DATA, val);
  282. }
  283. break;
  284. case I2C_SMBUS_BLOCK_DATA:
  285. if (read_write == I2C_SMBUS_READ) {
  286. ret = slimpro_i2c_blkrd(ctx, addr, command,
  287. SMBUS_CMD_LEN,
  288. SLIMPRO_IIC_SMB_PROTOCOL,
  289. I2C_SMBUS_BLOCK_MAX + 1,
  290. IIC_SMB_WITH_DATA_LEN,
  291. &data->block[0]);
  292. } else {
  293. ret = slimpro_i2c_blkwr(ctx, addr, command,
  294. SMBUS_CMD_LEN,
  295. SLIMPRO_IIC_SMB_PROTOCOL,
  296. data->block[0] + 1,
  297. &data->block[0]);
  298. }
  299. break;
  300. case I2C_SMBUS_I2C_BLOCK_DATA:
  301. if (read_write == I2C_SMBUS_READ) {
  302. ret = slimpro_i2c_blkrd(ctx, addr,
  303. command,
  304. SMBUS_CMD_LEN,
  305. SLIMPRO_IIC_I2C_PROTOCOL,
  306. I2C_SMBUS_BLOCK_MAX,
  307. IIC_SMB_WITHOUT_DATA_LEN,
  308. &data->block[1]);
  309. } else {
  310. ret = slimpro_i2c_blkwr(ctx, addr, command,
  311. SMBUS_CMD_LEN,
  312. SLIMPRO_IIC_I2C_PROTOCOL,
  313. data->block[0],
  314. &data->block[1]);
  315. }
  316. break;
  317. default:
  318. break;
  319. }
  320. return ret;
  321. }
  322. /*
  323. * Return list of supported functionality.
  324. */
  325. static u32 xgene_slimpro_i2c_func(struct i2c_adapter *adapter)
  326. {
  327. return I2C_FUNC_SMBUS_BYTE |
  328. I2C_FUNC_SMBUS_BYTE_DATA |
  329. I2C_FUNC_SMBUS_WORD_DATA |
  330. I2C_FUNC_SMBUS_BLOCK_DATA |
  331. I2C_FUNC_SMBUS_I2C_BLOCK;
  332. }
  333. static struct i2c_algorithm xgene_slimpro_i2c_algorithm = {
  334. .smbus_xfer = xgene_slimpro_i2c_xfer,
  335. .functionality = xgene_slimpro_i2c_func,
  336. };
  337. static int xgene_slimpro_i2c_probe(struct platform_device *pdev)
  338. {
  339. struct slimpro_i2c_dev *ctx;
  340. struct i2c_adapter *adapter;
  341. struct mbox_client *cl;
  342. int rc;
  343. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  344. if (!ctx)
  345. return -ENOMEM;
  346. ctx->dev = &pdev->dev;
  347. platform_set_drvdata(pdev, ctx);
  348. cl = &ctx->mbox_client;
  349. /* Request mailbox channel */
  350. cl->dev = &pdev->dev;
  351. cl->rx_callback = slimpro_i2c_rx_cb;
  352. cl->tx_block = true;
  353. init_completion(&ctx->rd_complete);
  354. cl->tx_tout = MAILBOX_OP_TIMEOUT;
  355. cl->knows_txdone = false;
  356. ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX);
  357. if (IS_ERR(ctx->mbox_chan)) {
  358. dev_err(&pdev->dev, "i2c mailbox channel request failed\n");
  359. return PTR_ERR(ctx->mbox_chan);
  360. }
  361. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  362. if (rc)
  363. dev_warn(&pdev->dev, "Unable to set dma mask\n");
  364. /* Setup I2C adapter */
  365. adapter = &ctx->adapter;
  366. snprintf(adapter->name, sizeof(adapter->name), "MAILBOX I2C");
  367. adapter->algo = &xgene_slimpro_i2c_algorithm;
  368. adapter->class = I2C_CLASS_HWMON;
  369. adapter->dev.parent = &pdev->dev;
  370. i2c_set_adapdata(adapter, ctx);
  371. rc = i2c_add_adapter(adapter);
  372. if (rc) {
  373. dev_err(&pdev->dev, "Adapter registeration failed\n");
  374. mbox_free_channel(ctx->mbox_chan);
  375. return rc;
  376. }
  377. dev_info(&pdev->dev, "Mailbox I2C Adapter registered\n");
  378. return 0;
  379. }
  380. static int xgene_slimpro_i2c_remove(struct platform_device *pdev)
  381. {
  382. struct slimpro_i2c_dev *ctx = platform_get_drvdata(pdev);
  383. i2c_del_adapter(&ctx->adapter);
  384. mbox_free_channel(ctx->mbox_chan);
  385. return 0;
  386. }
  387. static const struct of_device_id xgene_slimpro_i2c_dt_ids[] = {
  388. {.compatible = "apm,xgene-slimpro-i2c" },
  389. {},
  390. };
  391. MODULE_DEVICE_TABLE(of, xgene_slimpro_i2c_dt_ids);
  392. #ifdef CONFIG_ACPI
  393. static const struct acpi_device_id xgene_slimpro_i2c_acpi_ids[] = {
  394. {"APMC0D40", 0},
  395. {}
  396. };
  397. MODULE_DEVICE_TABLE(acpi, xgene_slimpro_i2c_acpi_ids);
  398. #endif
  399. static struct platform_driver xgene_slimpro_i2c_driver = {
  400. .probe = xgene_slimpro_i2c_probe,
  401. .remove = xgene_slimpro_i2c_remove,
  402. .driver = {
  403. .name = "xgene-slimpro-i2c",
  404. .of_match_table = of_match_ptr(xgene_slimpro_i2c_dt_ids),
  405. .acpi_match_table = ACPI_PTR(xgene_slimpro_i2c_acpi_ids)
  406. },
  407. };
  408. module_platform_driver(xgene_slimpro_i2c_driver);
  409. MODULE_DESCRIPTION("APM X-Gene SLIMpro I2C driver");
  410. MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
  411. MODULE_AUTHOR("Hieu Le <hnle@apm.com>");
  412. MODULE_LICENSE("GPL");