1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * X-Gene SLIMpro I2C Driver
5 * Copyright (c) 2014, Applied Micro Circuits Corporation
6 * Author: Feng Kan <fkan@apm.com>
7 * Author: Hieu Le <hnle@apm.com>
9 * This driver provides support for X-Gene SLIMpro I2C device access
10 * using the APM X-Gene SLIMpro mailbox driver.
13 #include <linux/acpi.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
18 #include <linux/mailbox_client.h>
19 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/version.h>
24 #define MAILBOX_OP_TIMEOUT 1000 /* Operation time out in ms */
25 #define MAILBOX_I2C_INDEX 0
26 #define SLIMPRO_IIC_BUS 1 /* Use I2C bus 1 only */
28 #define SMBUS_CMD_LEN 1
33 #define SLIMPRO_IIC_I2C_PROTOCOL 0
34 #define SLIMPRO_IIC_SMB_PROTOCOL 1
36 #define SLIMPRO_IIC_READ 0
37 #define SLIMPRO_IIC_WRITE 1
39 #define IIC_SMB_WITHOUT_DATA_LEN 0
40 #define IIC_SMB_WITH_DATA_LEN 1
42 #define SLIMPRO_DEBUG_MSG 0
43 #define SLIMPRO_MSG_TYPE_SHIFT 28
44 #define SLIMPRO_DBG_SUBTYPE_I2C1READ 4
45 #define SLIMPRO_DBGMSG_TYPE_SHIFT 24
46 #define SLIMPRO_DBGMSG_TYPE_MASK 0x0F000000U
47 #define SLIMPRO_IIC_DEV_SHIFT 23
48 #define SLIMPRO_IIC_DEV_MASK 0x00800000U
49 #define SLIMPRO_IIC_DEVID_SHIFT 13
50 #define SLIMPRO_IIC_DEVID_MASK 0x007FE000U
51 #define SLIMPRO_IIC_RW_SHIFT 12
52 #define SLIMPRO_IIC_RW_MASK 0x00001000U
53 #define SLIMPRO_IIC_PROTO_SHIFT 11
54 #define SLIMPRO_IIC_PROTO_MASK 0x00000800U
55 #define SLIMPRO_IIC_ADDRLEN_SHIFT 8
56 #define SLIMPRO_IIC_ADDRLEN_MASK 0x00000700U
57 #define SLIMPRO_IIC_DATALEN_SHIFT 0
58 #define SLIMPRO_IIC_DATALEN_MASK 0x000000FFU
61 * SLIMpro I2C message encode
63 * dev - Controller number (0-based)
64 * chip - I2C chip address
65 * op - SLIMPRO_IIC_READ or SLIMPRO_IIC_WRITE
66 * proto - SLIMPRO_IIC_SMB_PROTOCOL or SLIMPRO_IIC_I2C_PROTOCOL
67 * addrlen - Length of the address field
68 * datalen - Length of the data field
70 #define SLIMPRO_IIC_ENCODE_MSG(dev, chip, op, proto, addrlen, datalen) \
71 ((SLIMPRO_DEBUG_MSG << SLIMPRO_MSG_TYPE_SHIFT) | \
72 ((SLIMPRO_DBG_SUBTYPE_I2C1READ << SLIMPRO_DBGMSG_TYPE_SHIFT) & \
73 SLIMPRO_DBGMSG_TYPE_MASK) | \
74 ((dev << SLIMPRO_IIC_DEV_SHIFT) & SLIMPRO_IIC_DEV_MASK) | \
75 ((chip << SLIMPRO_IIC_DEVID_SHIFT) & SLIMPRO_IIC_DEVID_MASK) | \
76 ((op << SLIMPRO_IIC_RW_SHIFT) & SLIMPRO_IIC_RW_MASK) | \
77 ((proto << SLIMPRO_IIC_PROTO_SHIFT) & SLIMPRO_IIC_PROTO_MASK) | \
78 ((addrlen << SLIMPRO_IIC_ADDRLEN_SHIFT) & SLIMPRO_IIC_ADDRLEN_MASK) | \
79 ((datalen << SLIMPRO_IIC_DATALEN_SHIFT) & SLIMPRO_IIC_DATALEN_MASK))
81 #define SLIMPRO_MSG_TYPE(v) (((v) & 0xF0000000) >> 28)
84 * Encode for upper address for block data
86 #define SLIMPRO_IIC_ENCODE_FLAG_BUFADDR 0x80000000
87 #define SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(a) ((u32) (((a) << 30) \
89 #define SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(a) ((u32) (((a) >> 12) \
91 #define SLIMPRO_IIC_ENCODE_ADDR(a) ((a) & 0x000FFFFF)
93 #define SLIMPRO_IIC_MSG_DWORD_COUNT 3
95 /* PCC related defines */
96 #define PCC_SIGNATURE 0x50424300
97 #define PCC_STS_CMD_COMPLETE BIT(0)
98 #define PCC_STS_SCI_DOORBELL BIT(1)
99 #define PCC_STS_ERR BIT(2)
100 #define PCC_STS_PLAT_NOTIFY BIT(3)
101 #define PCC_CMD_GENERATE_DB_INT BIT(15)
103 struct slimpro_i2c_dev {
104 struct i2c_adapter adapter;
106 struct mbox_chan *mbox_chan;
107 struct mbox_client mbox_client;
109 struct completion rd_complete;
110 u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* dma_buffer[0] is used for length */
112 phys_addr_t comm_base_addr;
116 #define to_slimpro_i2c_dev(cl) \
117 container_of(cl, struct slimpro_i2c_dev, mbox_client)
119 enum slimpro_i2c_version {
120 XGENE_SLIMPRO_I2C_V1 = 0,
121 XGENE_SLIMPRO_I2C_V2 = 1,
125 * This function tests and clears a bitmask then returns its old value
127 static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask)
131 val = le16_to_cpu(READ_ONCE(*addr));
134 WRITE_ONCE(*addr, cpu_to_le16(val));
139 static void slimpro_i2c_rx_cb(struct mbox_client *cl, void *mssg)
141 struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
144 * Response message format:
145 * mssg[0] is the return code of the operation
146 * mssg[1] is the first data word
147 * mssg[2] is NOT used
150 *ctx->resp_msg = ((u32 *)mssg)[1];
152 if (ctx->mbox_client.tx_block)
153 complete(&ctx->rd_complete);
156 static void slimpro_i2c_pcc_rx_cb(struct mbox_client *cl, void *msg)
158 struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
159 struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
161 /* Check if platform sends interrupt */
162 if (!xgene_word_tst_and_clr(&generic_comm_base->status,
163 PCC_STS_SCI_DOORBELL))
166 if (xgene_word_tst_and_clr(&generic_comm_base->status,
167 PCC_STS_CMD_COMPLETE)) {
168 msg = generic_comm_base + 1;
170 /* Response message msg[1] contains the return value. */
172 *ctx->resp_msg = ((u32 *)msg)[1];
174 complete(&ctx->rd_complete);
178 static void slimpro_i2c_pcc_tx_prepare(struct slimpro_i2c_dev *ctx, u32 *msg)
180 struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
181 u32 *ptr = (void *)(generic_comm_base + 1);
185 WRITE_ONCE(generic_comm_base->signature,
186 cpu_to_le32(PCC_SIGNATURE | ctx->mbox_idx));
188 WRITE_ONCE(generic_comm_base->command,
189 cpu_to_le16(SLIMPRO_MSG_TYPE(msg[0]) | PCC_CMD_GENERATE_DB_INT));
191 status = le16_to_cpu(READ_ONCE(generic_comm_base->status));
192 status &= ~PCC_STS_CMD_COMPLETE;
193 WRITE_ONCE(generic_comm_base->status, cpu_to_le16(status));
195 /* Copy the message to the PCC comm space */
196 for (i = 0; i < SLIMPRO_IIC_MSG_DWORD_COUNT; i++)
197 WRITE_ONCE(ptr[i], cpu_to_le32(msg[i]));
200 static int start_i2c_msg_xfer(struct slimpro_i2c_dev *ctx)
202 if (ctx->mbox_client.tx_block || !acpi_disabled) {
203 if (!wait_for_completion_timeout(&ctx->rd_complete,
204 msecs_to_jiffies(MAILBOX_OP_TIMEOUT)))
208 /* Check of invalid data or no device */
209 if (*ctx->resp_msg == 0xffffffff)
215 static int slimpro_i2c_send_msg(struct slimpro_i2c_dev *ctx,
221 ctx->resp_msg = data;
223 if (!acpi_disabled) {
224 reinit_completion(&ctx->rd_complete);
225 slimpro_i2c_pcc_tx_prepare(ctx, msg);
228 rc = mbox_send_message(ctx->mbox_chan, msg);
232 rc = start_i2c_msg_xfer(ctx);
236 mbox_chan_txdone(ctx->mbox_chan, 0);
238 ctx->resp_msg = NULL;
243 static int slimpro_i2c_rd(struct slimpro_i2c_dev *ctx, u32 chip,
244 u32 addr, u32 addrlen, u32 protocol,
245 u32 readlen, u32 *data)
249 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip,
250 SLIMPRO_IIC_READ, protocol, addrlen, readlen);
251 msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr);
254 return slimpro_i2c_send_msg(ctx, msg, data);
257 static int slimpro_i2c_wr(struct slimpro_i2c_dev *ctx, u32 chip,
258 u32 addr, u32 addrlen, u32 protocol, u32 writelen,
263 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip,
264 SLIMPRO_IIC_WRITE, protocol, addrlen, writelen);
265 msg[1] = SLIMPRO_IIC_ENCODE_ADDR(addr);
268 return slimpro_i2c_send_msg(ctx, msg, msg);
271 static int slimpro_i2c_blkrd(struct slimpro_i2c_dev *ctx, u32 chip, u32 addr,
272 u32 addrlen, u32 protocol, u32 readlen,
273 u32 with_data_len, void *data)
279 paddr = dma_map_single(ctx->dev, ctx->dma_buffer, readlen, DMA_FROM_DEVICE);
280 if (dma_mapping_error(ctx->dev, paddr)) {
281 dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n",
286 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_READ,
287 protocol, addrlen, readlen);
288 msg[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR |
289 SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(with_data_len) |
290 SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) |
291 SLIMPRO_IIC_ENCODE_ADDR(addr);
294 rc = slimpro_i2c_send_msg(ctx, msg, msg);
296 /* Copy to destination */
297 memcpy(data, ctx->dma_buffer, readlen);
299 dma_unmap_single(ctx->dev, paddr, readlen, DMA_FROM_DEVICE);
303 static int slimpro_i2c_blkwr(struct slimpro_i2c_dev *ctx, u32 chip,
304 u32 addr, u32 addrlen, u32 protocol, u32 writelen,
311 if (writelen > I2C_SMBUS_BLOCK_MAX)
314 memcpy(ctx->dma_buffer, data, writelen);
315 paddr = dma_map_single(ctx->dev, ctx->dma_buffer, writelen,
317 if (dma_mapping_error(ctx->dev, paddr)) {
318 dev_err(&ctx->adapter.dev, "Error in mapping dma buffer %p\n",
323 msg[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS, chip, SLIMPRO_IIC_WRITE,
324 protocol, addrlen, writelen);
325 msg[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR |
326 SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr) |
327 SLIMPRO_IIC_ENCODE_ADDR(addr);
330 if (ctx->mbox_client.tx_block)
331 reinit_completion(&ctx->rd_complete);
333 rc = slimpro_i2c_send_msg(ctx, msg, msg);
335 dma_unmap_single(ctx->dev, paddr, writelen, DMA_TO_DEVICE);
339 static int xgene_slimpro_i2c_xfer(struct i2c_adapter *adap, u16 addr,
340 unsigned short flags, char read_write,
341 u8 command, int size,
342 union i2c_smbus_data *data)
344 struct slimpro_i2c_dev *ctx = i2c_get_adapdata(adap);
345 int ret = -EOPNOTSUPP;
350 if (read_write == I2C_SMBUS_READ) {
351 ret = slimpro_i2c_rd(ctx, addr, 0, 0,
352 SLIMPRO_IIC_SMB_PROTOCOL,
356 ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
357 SLIMPRO_IIC_SMB_PROTOCOL,
361 case I2C_SMBUS_BYTE_DATA:
362 if (read_write == I2C_SMBUS_READ) {
363 ret = slimpro_i2c_rd(ctx, addr, command, SMBUS_CMD_LEN,
364 SLIMPRO_IIC_SMB_PROTOCOL,
369 ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
370 SLIMPRO_IIC_SMB_PROTOCOL,
374 case I2C_SMBUS_WORD_DATA:
375 if (read_write == I2C_SMBUS_READ) {
376 ret = slimpro_i2c_rd(ctx, addr, command, SMBUS_CMD_LEN,
377 SLIMPRO_IIC_SMB_PROTOCOL,
382 ret = slimpro_i2c_wr(ctx, addr, command, SMBUS_CMD_LEN,
383 SLIMPRO_IIC_SMB_PROTOCOL,
387 case I2C_SMBUS_BLOCK_DATA:
388 if (read_write == I2C_SMBUS_READ) {
389 ret = slimpro_i2c_blkrd(ctx, addr, command,
391 SLIMPRO_IIC_SMB_PROTOCOL,
392 I2C_SMBUS_BLOCK_MAX + 1,
393 IIC_SMB_WITH_DATA_LEN,
397 ret = slimpro_i2c_blkwr(ctx, addr, command,
399 SLIMPRO_IIC_SMB_PROTOCOL,
404 case I2C_SMBUS_I2C_BLOCK_DATA:
405 if (read_write == I2C_SMBUS_READ) {
406 ret = slimpro_i2c_blkrd(ctx, addr,
409 SLIMPRO_IIC_I2C_PROTOCOL,
411 IIC_SMB_WITHOUT_DATA_LEN,
414 ret = slimpro_i2c_blkwr(ctx, addr, command,
416 SLIMPRO_IIC_I2C_PROTOCOL,
428 * Return list of supported functionality.
430 static u32 xgene_slimpro_i2c_func(struct i2c_adapter *adapter)
432 return I2C_FUNC_SMBUS_BYTE |
433 I2C_FUNC_SMBUS_BYTE_DATA |
434 I2C_FUNC_SMBUS_WORD_DATA |
435 I2C_FUNC_SMBUS_BLOCK_DATA |
436 I2C_FUNC_SMBUS_I2C_BLOCK;
439 static const struct i2c_algorithm xgene_slimpro_i2c_algorithm = {
440 .smbus_xfer = xgene_slimpro_i2c_xfer,
441 .functionality = xgene_slimpro_i2c_func,
444 static int xgene_slimpro_i2c_probe(struct platform_device *pdev)
446 struct slimpro_i2c_dev *ctx;
447 struct i2c_adapter *adapter;
448 struct mbox_client *cl;
451 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
455 ctx->dev = &pdev->dev;
456 platform_set_drvdata(pdev, ctx);
457 cl = &ctx->mbox_client;
459 /* Request mailbox channel */
460 cl->dev = &pdev->dev;
461 init_completion(&ctx->rd_complete);
462 cl->tx_tout = MAILBOX_OP_TIMEOUT;
463 cl->knows_txdone = false;
466 cl->rx_callback = slimpro_i2c_rx_cb;
467 ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX);
468 if (IS_ERR(ctx->mbox_chan)) {
469 dev_err(&pdev->dev, "i2c mailbox channel request failed\n");
470 return PTR_ERR(ctx->mbox_chan);
473 struct acpi_pcct_hw_reduced *cppc_ss;
474 const struct acpi_device_id *acpi_id;
475 int version = XGENE_SLIMPRO_I2C_V1;
477 acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
482 version = (int)acpi_id->driver_data;
484 if (device_property_read_u32(&pdev->dev, "pcc-channel",
486 ctx->mbox_idx = MAILBOX_I2C_INDEX;
488 cl->tx_block = false;
489 cl->rx_callback = slimpro_i2c_pcc_rx_cb;
490 ctx->mbox_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx);
491 if (IS_ERR(ctx->mbox_chan)) {
492 dev_err(&pdev->dev, "PCC mailbox channel request failed\n");
493 return PTR_ERR(ctx->mbox_chan);
497 * The PCC mailbox controller driver should
498 * have parsed the PCCT (global table of all
499 * PCC channels) and stored pointers to the
500 * subspace communication region in con_priv.
502 cppc_ss = ctx->mbox_chan->con_priv;
504 dev_err(&pdev->dev, "PPC subspace not found\n");
509 if (!ctx->mbox_chan->mbox->txdone_irq) {
510 dev_err(&pdev->dev, "PCC IRQ not supported\n");
516 * This is the shared communication region
517 * for the OS and Platform to communicate over.
519 ctx->comm_base_addr = cppc_ss->base_address;
520 if (ctx->comm_base_addr) {
521 if (version == XGENE_SLIMPRO_I2C_V2)
522 ctx->pcc_comm_addr = memremap(
527 ctx->pcc_comm_addr = memremap(
532 dev_err(&pdev->dev, "Failed to get PCC comm region\n");
537 if (!ctx->pcc_comm_addr) {
539 "Failed to ioremap PCC comm region\n");
544 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
546 dev_warn(&pdev->dev, "Unable to set dma mask\n");
548 /* Setup I2C adapter */
549 adapter = &ctx->adapter;
550 snprintf(adapter->name, sizeof(adapter->name), "MAILBOX I2C");
551 adapter->algo = &xgene_slimpro_i2c_algorithm;
552 adapter->class = I2C_CLASS_HWMON;
553 adapter->dev.parent = &pdev->dev;
554 adapter->dev.of_node = pdev->dev.of_node;
555 ACPI_COMPANION_SET(&adapter->dev, ACPI_COMPANION(&pdev->dev));
556 i2c_set_adapdata(adapter, ctx);
557 rc = i2c_add_adapter(adapter);
561 dev_info(&pdev->dev, "Mailbox I2C Adapter registered\n");
566 mbox_free_channel(ctx->mbox_chan);
568 pcc_mbox_free_channel(ctx->mbox_chan);
573 static int xgene_slimpro_i2c_remove(struct platform_device *pdev)
575 struct slimpro_i2c_dev *ctx = platform_get_drvdata(pdev);
577 i2c_del_adapter(&ctx->adapter);
580 mbox_free_channel(ctx->mbox_chan);
582 pcc_mbox_free_channel(ctx->mbox_chan);
587 static const struct of_device_id xgene_slimpro_i2c_dt_ids[] = {
588 {.compatible = "apm,xgene-slimpro-i2c" },
591 MODULE_DEVICE_TABLE(of, xgene_slimpro_i2c_dt_ids);
594 static const struct acpi_device_id xgene_slimpro_i2c_acpi_ids[] = {
595 {"APMC0D40", XGENE_SLIMPRO_I2C_V1},
596 {"APMC0D8B", XGENE_SLIMPRO_I2C_V2},
599 MODULE_DEVICE_TABLE(acpi, xgene_slimpro_i2c_acpi_ids);
602 static struct platform_driver xgene_slimpro_i2c_driver = {
603 .probe = xgene_slimpro_i2c_probe,
604 .remove = xgene_slimpro_i2c_remove,
606 .name = "xgene-slimpro-i2c",
607 .of_match_table = of_match_ptr(xgene_slimpro_i2c_dt_ids),
608 .acpi_match_table = ACPI_PTR(xgene_slimpro_i2c_acpi_ids)
612 module_platform_driver(xgene_slimpro_i2c_driver);
614 MODULE_DESCRIPTION("APM X-Gene SLIMpro I2C driver");
615 MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
616 MODULE_AUTHOR("Hieu Le <hnle@apm.com>");
617 MODULE_LICENSE("GPL");