i2c-xgene-slimpro.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 <acpi/pcc.h>
  26. #include <linux/acpi.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/i2c.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/io.h>
  31. #include <linux/mailbox_client.h>
  32. #include <linux/module.h>
  33. #include <linux/of.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/version.h>
  36. #define MAILBOX_OP_TIMEOUT 1000 /* Operation time out in ms */
  37. #define MAILBOX_I2C_INDEX 0
  38. #define SLIMPRO_IIC_BUS 1 /* Use I2C bus 1 only */
  39. #define SMBUS_CMD_LEN 1
  40. #define BYTE_DATA 1
  41. #define WORD_DATA 2
  42. #define BLOCK_DATA 3
  43. #define SLIMPRO_IIC_I2C_PROTOCOL 0
  44. #define SLIMPRO_IIC_SMB_PROTOCOL 1
  45. #define SLIMPRO_IIC_READ 0
  46. #define SLIMPRO_IIC_WRITE 1
  47. #define IIC_SMB_WITHOUT_DATA_LEN 0
  48. #define IIC_SMB_WITH_DATA_LEN 1
  49. #define SLIMPRO_DEBUG_MSG 0
  50. #define SLIMPRO_MSG_TYPE_SHIFT 28
  51. #define SLIMPRO_DBG_SUBTYPE_I2C1READ 4
  52. #define SLIMPRO_DBGMSG_TYPE_SHIFT 24
  53. #define SLIMPRO_DBGMSG_TYPE_MASK 0x0F000000U
  54. #define SLIMPRO_IIC_DEV_SHIFT 23
  55. #define SLIMPRO_IIC_DEV_MASK 0x00800000U
  56. #define SLIMPRO_IIC_DEVID_SHIFT 13
  57. #define SLIMPRO_IIC_DEVID_MASK 0x007FE000U
  58. #define SLIMPRO_IIC_RW_SHIFT 12
  59. #define SLIMPRO_IIC_RW_MASK 0x00001000U
  60. #define SLIMPRO_IIC_PROTO_SHIFT 11
  61. #define SLIMPRO_IIC_PROTO_MASK 0x00000800U
  62. #define SLIMPRO_IIC_ADDRLEN_SHIFT 8
  63. #define SLIMPRO_IIC_ADDRLEN_MASK 0x00000700U
  64. #define SLIMPRO_IIC_DATALEN_SHIFT 0
  65. #define SLIMPRO_IIC_DATALEN_MASK 0x000000FFU
  66. /*
  67. * SLIMpro I2C message encode
  68. *
  69. * dev - Controller number (0-based)
  70. * chip - I2C chip address
  71. * op - SLIMPRO_IIC_READ or SLIMPRO_IIC_WRITE
  72. * proto - SLIMPRO_IIC_SMB_PROTOCOL or SLIMPRO_IIC_I2C_PROTOCOL
  73. * addrlen - Length of the address field
  74. * datalen - Length of the data field
  75. */
  76. #define SLIMPRO_IIC_ENCODE_MSG(dev, chip, op, proto, addrlen, datalen) \
  77. ((SLIMPRO_DEBUG_MSG << SLIMPRO_MSG_TYPE_SHIFT) | \
  78. ((SLIMPRO_DBG_SUBTYPE_I2C1READ << SLIMPRO_DBGMSG_TYPE_SHIFT) & \
  79. SLIMPRO_DBGMSG_TYPE_MASK) | \
  80. ((dev << SLIMPRO_IIC_DEV_SHIFT) & SLIMPRO_IIC_DEV_MASK) | \
  81. ((chip << SLIMPRO_IIC_DEVID_SHIFT) & SLIMPRO_IIC_DEVID_MASK) | \
  82. ((op << SLIMPRO_IIC_RW_SHIFT) & SLIMPRO_IIC_RW_MASK) | \
  83. ((proto << SLIMPRO_IIC_PROTO_SHIFT) & SLIMPRO_IIC_PROTO_MASK) | \
  84. ((addrlen << SLIMPRO_IIC_ADDRLEN_SHIFT) & SLIMPRO_IIC_ADDRLEN_MASK) | \
  85. ((datalen << SLIMPRO_IIC_DATALEN_SHIFT) & SLIMPRO_IIC_DATALEN_MASK))
  86. #define SLIMPRO_MSG_TYPE(v) (((v) & 0xF0000000) >> 28)
  87. /*
  88. * Encode for upper address for block data
  89. */
  90. #define SLIMPRO_IIC_ENCODE_FLAG_BUFADDR 0x80000000
  91. #define SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(a) ((u32) (((a) << 30) \
  92. & 0x40000000))
  93. #define SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(a) ((u32) (((a) >> 12) \
  94. & 0x3FF00000))
  95. #define SLIMPRO_IIC_ENCODE_ADDR(a) ((a) & 0x000FFFFF)
  96. #define SLIMPRO_IIC_MSG_DWORD_COUNT 3
  97. /* PCC related defines */
  98. #define PCC_SIGNATURE 0x50424300
  99. #define PCC_STS_CMD_COMPLETE BIT(0)
  100. #define PCC_STS_SCI_DOORBELL BIT(1)
  101. #define PCC_STS_ERR BIT(2)
  102. #define PCC_STS_PLAT_NOTIFY BIT(3)
  103. #define PCC_CMD_GENERATE_DB_INT BIT(15)
  104. struct slimpro_i2c_dev {
  105. struct i2c_adapter adapter;
  106. struct device *dev;
  107. struct mbox_chan *mbox_chan;
  108. struct mbox_client mbox_client;
  109. int mbox_idx;
  110. struct completion rd_complete;
  111. u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* dma_buffer[0] is used for length */
  112. u32 *resp_msg;
  113. phys_addr_t comm_base_addr;
  114. void *pcc_comm_addr;
  115. };
  116. #define to_slimpro_i2c_dev(cl) \
  117. container_of(cl, struct slimpro_i2c_dev, mbox_client)
  118. enum slimpro_i2c_version {
  119. XGENE_SLIMPRO_I2C_V1 = 0,
  120. XGENE_SLIMPRO_I2C_V2 = 1,
  121. };
  122. /*
  123. * This function tests and clears a bitmask then returns its old value
  124. */
  125. static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask)
  126. {
  127. u16 ret, val;
  128. val = le16_to_cpu(READ_ONCE(*addr));
  129. ret = val & mask;
  130. val &= ~mask;
  131. WRITE_ONCE(*addr, cpu_to_le16(val));
  132. return ret;
  133. }
  134. static void slimpro_i2c_rx_cb(struct mbox_client *cl, void *mssg)
  135. {
  136. struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
  137. /*
  138. * Response message format:
  139. * mssg[0] is the return code of the operation
  140. * mssg[1] is the first data word
  141. * mssg[2] is NOT used
  142. */
  143. if (ctx->resp_msg)
  144. *ctx->resp_msg = ((u32 *)mssg)[1];
  145. if (ctx->mbox_client.tx_block)
  146. complete(&ctx->rd_complete);
  147. }
  148. static void slimpro_i2c_pcc_rx_cb(struct mbox_client *cl, void *msg)
  149. {
  150. struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
  151. struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
  152. /* Check if platform sends interrupt */
  153. if (!xgene_word_tst_and_clr(&generic_comm_base->status,
  154. PCC_STS_SCI_DOORBELL))
  155. return;
  156. if (xgene_word_tst_and_clr(&generic_comm_base->status,
  157. PCC_STS_CMD_COMPLETE)) {
  158. msg = generic_comm_base + 1;
  159. /* Response message msg[1] contains the return value. */
  160. if (ctx->resp_msg)
  161. *ctx->resp_msg = ((u32 *)msg)[1];
  162. complete(&ctx->rd_complete);
  163. }
  164. }
  165. static void slimpro_i2c_pcc_tx_prepare(struct slimpro_i2c_dev *ctx, u32 *msg)
  166. {
  167. struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
  168. u32 *ptr = (void *)(generic_comm_base + 1);
  169. u16 status;
  170. int i;
  171. WRITE_ONCE(generic_comm_base->signature,
  172. cpu_to_le32(PCC_SIGNATURE | ctx->mbox_idx));
  173. WRITE_ONCE(generic_comm_base->command,
  174. cpu_to_le16(SLIMPRO_MSG_TYPE(msg[0]) | PCC_CMD_GENERATE_DB_INT));
  175. status = le16_to_cpu(READ_ONCE(generic_comm_base->status));
  176. status &= ~PCC_STS_CMD_COMPLETE;
  177. WRITE_ONCE(generic_comm_base->status, cpu_to_le16(status));
  178. /* Copy the message to the PCC comm space */
  179. for (i = 0; i < SLIMPRO_IIC_MSG_DWORD_COUNT; i++)
  180. WRITE_ONCE(ptr[i], cpu_to_le32(msg[i]));
  181. }
  182. static int start_i2c_msg_xfer(struct slimpro_i2c_dev *ctx)
  183. {
  184. if (ctx->mbox_client.tx_block || !acpi_disabled) {
  185. if (!wait_for_completion_timeout(&ctx->rd_complete,
  186. msecs_to_jiffies(MAILBOX_OP_TIMEOUT)))
  187. return -ETIMEDOUT;
  188. }
  189. /* Check of invalid data or no device */
  190. if (*ctx->resp_msg == 0xffffffff)
  191. return -ENODEV;
  192. return 0;
  193. }
  194. static int slimpro_i2c_send_msg(struct slimpro_i2c_dev *ctx,
  195. u32 *msg,
  196. u32 *data)
  197. {
  198. int rc;
  199. ctx->resp_msg = data;
  200. if (!acpi_disabled) {
  201. reinit_completion(&ctx->rd_complete);
  202. slimpro_i2c_pcc_tx_prepare(ctx, msg);
  203. }
  204. rc = mbox_send_message(ctx->mbox_chan, msg);
  205. if (rc < 0)
  206. goto err;
  207. rc = start_i2c_msg_xfer(ctx);
  208. err:
  209. if (!acpi_disabled)
  210. mbox_chan_txdone(ctx->mbox_chan, 0);
  211. ctx->resp_msg = NULL;
  212. return rc;
  213. }
  214. static int slimpro_i2c_rd(struct slimpro_i2c_dev *ctx, u32 chip,
  215. u32 addr, u32 addrlen, u32 protocol,
  216. u32 readlen, u32 *data)
  217. {
  218. u32 msg[3];
  219. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip,
  220. SLIMPRO_IIC_READ, protocol, addrlen, readlen);
  221. msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr);
  222. msg[2] = 0;
  223. return slimpro_i2c_send_msg(ctx, msg, data);
  224. }
  225. static int slimpro_i2c_wr(struct slimpro_i2c_dev *ctx, u32 chip,
  226. u32 addr, u32 addrlen, u32 protocol, u32 writelen,
  227. u32 data)
  228. {
  229. u32 msg[3];
  230. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip,
  231. SLIMPRO_IIC_WRITE, protocol, addrlen, writelen);
  232. msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr);
  233. msg[2] = data;
  234. return slimpro_i2c_send_msg(ctx, msg, msg);
  235. }
  236. static int slimpro_i2c_blkrd(struct slimpro_i2c_dev *ctx, u32 chip, u32 addr,
  237. u32 addrlen, u32 protocol, u32 readlen,
  238. u32 with_data_len, void *data)
  239. {
  240. dma_addr_t paddr;
  241. u32 msg[3];
  242. int rc;
  243. paddr = dma_map_single(ctx->dev, ctx->dma_buffer, readlen, DMA_FROM_DEVICE);
  244. if (dma_mapping_error(ctx->dev, paddr)) {
  245. dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n",
  246. ctx->dma_buffer);
  247. return -ENOMEM;
  248. }
  249. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_READ,
  250. protocol, addrlen, readlen);
  251. msg[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR |
  252. SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(with_data_len) |
  253. SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) |
  254. SLIMPRO_IIC_ENCODE_ADDR(addr);
  255. msg[2] = (u32)paddr;
  256. rc = slimpro_i2c_send_msg(ctx, msg, msg);
  257. /* Copy to destination */
  258. memcpy(data, ctx->dma_buffer, readlen);
  259. dma_unmap_single(ctx->dev, paddr, readlen, DMA_FROM_DEVICE);
  260. return rc;
  261. }
  262. static int slimpro_i2c_blkwr(struct slimpro_i2c_dev *ctx, u32 chip,
  263. u32 addr, u32 addrlen, u32 protocol, u32 writelen,
  264. void *data)
  265. {
  266. dma_addr_t paddr;
  267. u32 msg[3];
  268. int rc;
  269. memcpy(ctx->dma_buffer, data, writelen);
  270. paddr = dma_map_single(ctx->dev, ctx->dma_buffer, writelen,
  271. DMA_TO_DEVICE);
  272. if (dma_mapping_error(ctx->dev, paddr)) {
  273. dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n",
  274. ctx->dma_buffer);
  275. return -ENOMEM;
  276. }
  277. msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_WRITE,
  278. protocol, addrlen, writelen);
  279. msg[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR |
  280. SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) |
  281. SLIMPRO_IIC_ENCODE_ADDR(addr);
  282. msg[2] = (u32)paddr;
  283. if (ctx->mbox_client.tx_block)
  284. reinit_completion(&ctx->rd_complete);
  285. rc = slimpro_i2c_send_msg(ctx, msg, msg);
  286. dma_unmap_single(ctx->dev, paddr, writelen, DMA_TO_DEVICE);
  287. return rc;
  288. }
  289. static int xgene_slimpro_i2c_xfer(struct i2c_adapter *adap, u16 addr,
  290. unsigned short flags, char read_write,
  291. u8 command, int size,
  292. union i2c_smbus_data *data)
  293. {
  294. struct slimpro_i2c_dev *ctx = i2c_get_adapdata(adap);
  295. int ret = -EOPNOTSUPP;
  296. u32 val;
  297. switch (size) {
  298. case I2C_SMBUS_BYTE:
  299. if (read_write == I2C_SMBUS_READ) {
  300. ret = slimpro_i2c_rd(ctx, addr, 0, 0,
  301. SLIMPRO_IIC_SMB_PROTOCOL,
  302. BYTE_DATA, &val);
  303. data->byte = val;
  304. } else {
  305. ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
  306. SLIMPRO_IIC_SMB_PROTOCOL,
  307. 0, 0);
  308. }
  309. break;
  310. case I2C_SMBUS_BYTE_DATA:
  311. if (read_write == I2C_SMBUS_READ) {
  312. ret = slimpro_i2c_rd(ctx, addr, command, SMBUS_CMD_LEN,
  313. SLIMPRO_IIC_SMB_PROTOCOL,
  314. BYTE_DATA, &val);
  315. data->byte = val;
  316. } else {
  317. val = data->byte;
  318. ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
  319. SLIMPRO_IIC_SMB_PROTOCOL,
  320. BYTE_DATA, val);
  321. }
  322. break;
  323. case I2C_SMBUS_WORD_DATA:
  324. if (read_write == I2C_SMBUS_READ) {
  325. ret = slimpro_i2c_rd(ctx, addr, command, SMBUS_CMD_LEN,
  326. SLIMPRO_IIC_SMB_PROTOCOL,
  327. WORD_DATA, &val);
  328. data->word = val;
  329. } else {
  330. val = data->word;
  331. ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
  332. SLIMPRO_IIC_SMB_PROTOCOL,
  333. WORD_DATA, val);
  334. }
  335. break;
  336. case I2C_SMBUS_BLOCK_DATA:
  337. if (read_write == I2C_SMBUS_READ) {
  338. ret = slimpro_i2c_blkrd(ctx, addr, command,
  339. SMBUS_CMD_LEN,
  340. SLIMPRO_IIC_SMB_PROTOCOL,
  341. I2C_SMBUS_BLOCK_MAX + 1,
  342. IIC_SMB_WITH_DATA_LEN,
  343. &data->block[0]);
  344. } else {
  345. ret = slimpro_i2c_blkwr(ctx, addr, command,
  346. SMBUS_CMD_LEN,
  347. SLIMPRO_IIC_SMB_PROTOCOL,
  348. data->block[0] + 1,
  349. &data->block[0]);
  350. }
  351. break;
  352. case I2C_SMBUS_I2C_BLOCK_DATA:
  353. if (read_write == I2C_SMBUS_READ) {
  354. ret = slimpro_i2c_blkrd(ctx, addr,
  355. command,
  356. SMBUS_CMD_LEN,
  357. SLIMPRO_IIC_I2C_PROTOCOL,
  358. I2C_SMBUS_BLOCK_MAX,
  359. IIC_SMB_WITHOUT_DATA_LEN,
  360. &data->block[1]);
  361. } else {
  362. ret = slimpro_i2c_blkwr(ctx, addr, command,
  363. SMBUS_CMD_LEN,
  364. SLIMPRO_IIC_I2C_PROTOCOL,
  365. data->block[0],
  366. &data->block[1]);
  367. }
  368. break;
  369. default:
  370. break;
  371. }
  372. return ret;
  373. }
  374. /*
  375. * Return list of supported functionality.
  376. */
  377. static u32 xgene_slimpro_i2c_func(struct i2c_adapter *adapter)
  378. {
  379. return I2C_FUNC_SMBUS_BYTE |
  380. I2C_FUNC_SMBUS_BYTE_DATA |
  381. I2C_FUNC_SMBUS_WORD_DATA |
  382. I2C_FUNC_SMBUS_BLOCK_DATA |
  383. I2C_FUNC_SMBUS_I2C_BLOCK;
  384. }
  385. static const struct i2c_algorithm xgene_slimpro_i2c_algorithm = {
  386. .smbus_xfer = xgene_slimpro_i2c_xfer,
  387. .functionality = xgene_slimpro_i2c_func,
  388. };
  389. static int xgene_slimpro_i2c_probe(struct platform_device *pdev)
  390. {
  391. struct slimpro_i2c_dev *ctx;
  392. struct i2c_adapter *adapter;
  393. struct mbox_client *cl;
  394. int rc;
  395. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  396. if (!ctx)
  397. return -ENOMEM;
  398. ctx->dev = &pdev->dev;
  399. platform_set_drvdata(pdev, ctx);
  400. cl = &ctx->mbox_client;
  401. /* Request mailbox channel */
  402. cl->dev = &pdev->dev;
  403. init_completion(&ctx->rd_complete);
  404. cl->tx_tout = MAILBOX_OP_TIMEOUT;
  405. cl->knows_txdone = false;
  406. if (acpi_disabled) {
  407. cl->tx_block = true;
  408. cl->rx_callback = slimpro_i2c_rx_cb;
  409. ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX);
  410. if (IS_ERR(ctx->mbox_chan)) {
  411. dev_err(&pdev->dev, "i2c mailbox channel request failed\n");
  412. return PTR_ERR(ctx->mbox_chan);
  413. }
  414. } else {
  415. struct acpi_pcct_hw_reduced *cppc_ss;
  416. const struct acpi_device_id *acpi_id;
  417. int version = XGENE_SLIMPRO_I2C_V1;
  418. acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  419. &pdev->dev);
  420. if (!acpi_id)
  421. return -EINVAL;
  422. version = (int)acpi_id->driver_data;
  423. if (device_property_read_u32(&pdev->dev, "pcc-channel",
  424. &ctx->mbox_idx))
  425. ctx->mbox_idx = MAILBOX_I2C_INDEX;
  426. cl->tx_block = false;
  427. cl->rx_callback = slimpro_i2c_pcc_rx_cb;
  428. ctx->mbox_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx);
  429. if (IS_ERR(ctx->mbox_chan)) {
  430. dev_err(&pdev->dev, "PCC mailbox channel request failed\n");
  431. return PTR_ERR(ctx->mbox_chan);
  432. }
  433. /*
  434. * The PCC mailbox controller driver should
  435. * have parsed the PCCT (global table of all
  436. * PCC channels) and stored pointers to the
  437. * subspace communication region in con_priv.
  438. */
  439. cppc_ss = ctx->mbox_chan->con_priv;
  440. if (!cppc_ss) {
  441. dev_err(&pdev->dev, "PPC subspace not found\n");
  442. rc = -ENOENT;
  443. goto mbox_err;
  444. }
  445. if (!ctx->mbox_chan->mbox->txdone_irq) {
  446. dev_err(&pdev->dev, "PCC IRQ not supported\n");
  447. rc = -ENOENT;
  448. goto mbox_err;
  449. }
  450. /*
  451. * This is the shared communication region
  452. * for the OS and Platform to communicate over.
  453. */
  454. ctx->comm_base_addr = cppc_ss->base_address;
  455. if (ctx->comm_base_addr) {
  456. if (version == XGENE_SLIMPRO_I2C_V2)
  457. ctx->pcc_comm_addr = memremap(
  458. ctx->comm_base_addr,
  459. cppc_ss->length,
  460. MEMREMAP_WT);
  461. else
  462. ctx->pcc_comm_addr = memremap(
  463. ctx->comm_base_addr,
  464. cppc_ss->length,
  465. MEMREMAP_WB);
  466. } else {
  467. dev_err(&pdev->dev, "Failed to get PCC comm region\n");
  468. rc = -ENOENT;
  469. goto mbox_err;
  470. }
  471. if (!ctx->pcc_comm_addr) {
  472. dev_err(&pdev->dev,
  473. "Failed to ioremap PCC comm region\n");
  474. rc = -ENOMEM;
  475. goto mbox_err;
  476. }
  477. }
  478. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  479. if (rc)
  480. dev_warn(&pdev->dev, "Unable to set dma mask\n");
  481. /* Setup I2C adapter */
  482. adapter = &ctx->adapter;
  483. snprintf(adapter->name, sizeof(adapter->name), "MAILBOX I2C");
  484. adapter->algo = &xgene_slimpro_i2c_algorithm;
  485. adapter->class = I2C_CLASS_HWMON;
  486. adapter->dev.parent = &pdev->dev;
  487. adapter->dev.of_node = pdev->dev.of_node;
  488. ACPI_COMPANION_SET(&adapter->dev, ACPI_COMPANION(&pdev->dev));
  489. i2c_set_adapdata(adapter, ctx);
  490. rc = i2c_add_adapter(adapter);
  491. if (rc)
  492. goto mbox_err;
  493. dev_info(&pdev->dev, "Mailbox I2C Adapter registered\n");
  494. return 0;
  495. mbox_err:
  496. if (acpi_disabled)
  497. mbox_free_channel(ctx->mbox_chan);
  498. else
  499. pcc_mbox_free_channel(ctx->mbox_chan);
  500. return rc;
  501. }
  502. static int xgene_slimpro_i2c_remove(struct platform_device *pdev)
  503. {
  504. struct slimpro_i2c_dev *ctx = platform_get_drvdata(pdev);
  505. i2c_del_adapter(&ctx->adapter);
  506. if (acpi_disabled)
  507. mbox_free_channel(ctx->mbox_chan);
  508. else
  509. pcc_mbox_free_channel(ctx->mbox_chan);
  510. return 0;
  511. }
  512. static const struct of_device_id xgene_slimpro_i2c_dt_ids[] = {
  513. {.compatible = "apm,xgene-slimpro-i2c" },
  514. {},
  515. };
  516. MODULE_DEVICE_TABLE(of, xgene_slimpro_i2c_dt_ids);
  517. #ifdef CONFIG_ACPI
  518. static const struct acpi_device_id xgene_slimpro_i2c_acpi_ids[] = {
  519. {"APMC0D40", XGENE_SLIMPRO_I2C_V1},
  520. {"APMC0D8B", XGENE_SLIMPRO_I2C_V2},
  521. {}
  522. };
  523. MODULE_DEVICE_TABLE(acpi, xgene_slimpro_i2c_acpi_ids);
  524. #endif
  525. static struct platform_driver xgene_slimpro_i2c_driver = {
  526. .probe = xgene_slimpro_i2c_probe,
  527. .remove = xgene_slimpro_i2c_remove,
  528. .driver = {
  529. .name = "xgene-slimpro-i2c",
  530. .of_match_table = of_match_ptr(xgene_slimpro_i2c_dt_ids),
  531. .acpi_match_table = ACPI_PTR(xgene_slimpro_i2c_acpi_ids)
  532. },
  533. };
  534. module_platform_driver(xgene_slimpro_i2c_driver);
  535. MODULE_DESCRIPTION("APM X-Gene SLIMpro I2C driver");
  536. MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
  537. MODULE_AUTHOR("Hieu Le <hnle@apm.com>");
  538. MODULE_LICENSE("GPL");