2 * X-Gene SLIMpro I2C Driver
4 * Copyright (c) 2014, Applied Micro Circuits Corporation
5 * Author: Feng Kan <fkan@apm.com>
6 * Author: Hieu Le <hnle@apm.com>
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.
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.
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/>.
21 * This driver provides support for X-Gene SLIMpro I2C device access
22 * using the APM X-Gene SLIMpro mailbox driver.
26 #include <linux/acpi.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/i2c.h>
29 #include <linux/interrupt.h>
31 #include <linux/mailbox_client.h>
32 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/version.h>
37 #define MAILBOX_OP_TIMEOUT 1000 /* Operation time out in ms */
38 #define MAILBOX_I2C_INDEX 0
39 #define SLIMPRO_IIC_BUS 1 /* Use I2C bus 1 only */
41 #define SMBUS_CMD_LEN 1
46 #define SLIMPRO_IIC_I2C_PROTOCOL 0
47 #define SLIMPRO_IIC_SMB_PROTOCOL 1
49 #define SLIMPRO_IIC_READ 0
50 #define SLIMPRO_IIC_WRITE 1
52 #define IIC_SMB_WITHOUT_DATA_LEN 0
53 #define IIC_SMB_WITH_DATA_LEN 1
55 #define SLIMPRO_DEBUG_MSG 0
56 #define SLIMPRO_MSG_TYPE_SHIFT 28
57 #define SLIMPRO_DBG_SUBTYPE_I2C1READ 4
58 #define SLIMPRO_DBGMSG_TYPE_SHIFT 24
59 #define SLIMPRO_DBGMSG_TYPE_MASK 0x0F000000U
60 #define SLIMPRO_IIC_DEV_SHIFT 23
61 #define SLIMPRO_IIC_DEV_MASK 0x00800000U
62 #define SLIMPRO_IIC_DEVID_SHIFT 13
63 #define SLIMPRO_IIC_DEVID_MASK 0x007FE000U
64 #define SLIMPRO_IIC_RW_SHIFT 12
65 #define SLIMPRO_IIC_RW_MASK 0x00001000U
66 #define SLIMPRO_IIC_PROTO_SHIFT 11
67 #define SLIMPRO_IIC_PROTO_MASK 0x00000800U
68 #define SLIMPRO_IIC_ADDRLEN_SHIFT 8
69 #define SLIMPRO_IIC_ADDRLEN_MASK 0x00000700U
70 #define SLIMPRO_IIC_DATALEN_SHIFT 0
71 #define SLIMPRO_IIC_DATALEN_MASK 0x000000FFU
74 * SLIMpro I2C message encode
76 * dev - Controller number (0-based)
77 * chip - I2C chip address
78 * op - SLIMPRO_IIC_READ or SLIMPRO_IIC_WRITE
79 * proto - SLIMPRO_IIC_SMB_PROTOCOL or SLIMPRO_IIC_I2C_PROTOCOL
80 * addrlen - Length of the address field
81 * datalen - Length of the data field
83 #define SLIMPRO_IIC_ENCODE_MSG(dev, chip, op, proto, addrlen, datalen) \
84 ((SLIMPRO_DEBUG_MSG << SLIMPRO_MSG_TYPE_SHIFT) | \
85 ((SLIMPRO_DBG_SUBTYPE_I2C1READ << SLIMPRO_DBGMSG_TYPE_SHIFT) & \
86 SLIMPRO_DBGMSG_TYPE_MASK) | \
87 ((dev << SLIMPRO_IIC_DEV_SHIFT) & SLIMPRO_IIC_DEV_MASK) | \
88 ((chip << SLIMPRO_IIC_DEVID_SHIFT) & SLIMPRO_IIC_DEVID_MASK) | \
89 ((op << SLIMPRO_IIC_RW_SHIFT) & SLIMPRO_IIC_RW_MASK) | \
90 ((proto << SLIMPRO_IIC_PROTO_SHIFT) & SLIMPRO_IIC_PROTO_MASK) | \
91 ((addrlen << SLIMPRO_IIC_ADDRLEN_SHIFT) & SLIMPRO_IIC_ADDRLEN_MASK) | \
92 ((datalen << SLIMPRO_IIC_DATALEN_SHIFT) & SLIMPRO_IIC_DATALEN_MASK))
94 #define SLIMPRO_MSG_TYPE(v) (((v) & 0xF0000000) >> 28)
97 * Encode for upper address for block data
99 #define SLIMPRO_IIC_ENCODE_FLAG_BUFADDR 0x80000000
100 #define SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(a) ((u32) (((a) << 30) \
102 #define SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(a) ((u32) (((a) >> 12) \
104 #define SLIMPRO_IIC_ENCODE_ADDR(a) ((a) & 0x000FFFFF)
106 #define SLIMPRO_IIC_MSG_DWORD_COUNT 3
108 /* PCC related defines */
109 #define PCC_SIGNATURE 0x50424300
110 #define PCC_STS_CMD_COMPLETE BIT(0)
111 #define PCC_STS_SCI_DOORBELL BIT(1)
112 #define PCC_STS_ERR BIT(2)
113 #define PCC_STS_PLAT_NOTIFY BIT(3)
114 #define PCC_CMD_GENERATE_DB_INT BIT(15)
116 struct slimpro_i2c_dev
{
117 struct i2c_adapter adapter
;
119 struct mbox_chan
*mbox_chan
;
120 struct mbox_client mbox_client
;
122 struct completion rd_complete
;
123 u8 dma_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1]; /* dma_buffer[0] is used for length */
125 phys_addr_t comm_base_addr
;
129 #define to_slimpro_i2c_dev(cl) \
130 container_of(cl, struct slimpro_i2c_dev, mbox_client)
132 enum slimpro_i2c_version
{
133 XGENE_SLIMPRO_I2C_V1
= 0,
134 XGENE_SLIMPRO_I2C_V2
= 1,
138 * This function tests and clears a bitmask then returns its old value
140 static u16
xgene_word_tst_and_clr(u16
*addr
, u16 mask
)
144 val
= le16_to_cpu(READ_ONCE(*addr
));
147 WRITE_ONCE(*addr
, cpu_to_le16(val
));
152 static void slimpro_i2c_rx_cb(struct mbox_client
*cl
, void *mssg
)
154 struct slimpro_i2c_dev
*ctx
= to_slimpro_i2c_dev(cl
);
157 * Response message format:
158 * mssg[0] is the return code of the operation
159 * mssg[1] is the first data word
160 * mssg[2] is NOT used
163 *ctx
->resp_msg
= ((u32
*)mssg
)[1];
165 if (ctx
->mbox_client
.tx_block
)
166 complete(&ctx
->rd_complete
);
169 static void slimpro_i2c_pcc_rx_cb(struct mbox_client
*cl
, void *msg
)
171 struct slimpro_i2c_dev
*ctx
= to_slimpro_i2c_dev(cl
);
172 struct acpi_pcct_shared_memory
*generic_comm_base
= ctx
->pcc_comm_addr
;
174 /* Check if platform sends interrupt */
175 if (!xgene_word_tst_and_clr(&generic_comm_base
->status
,
176 PCC_STS_SCI_DOORBELL
))
179 if (xgene_word_tst_and_clr(&generic_comm_base
->status
,
180 PCC_STS_CMD_COMPLETE
)) {
181 msg
= generic_comm_base
+ 1;
183 /* Response message msg[1] contains the return value. */
185 *ctx
->resp_msg
= ((u32
*)msg
)[1];
187 complete(&ctx
->rd_complete
);
191 static void slimpro_i2c_pcc_tx_prepare(struct slimpro_i2c_dev
*ctx
, u32
*msg
)
193 struct acpi_pcct_shared_memory
*generic_comm_base
= ctx
->pcc_comm_addr
;
194 u32
*ptr
= (void *)(generic_comm_base
+ 1);
198 WRITE_ONCE(generic_comm_base
->signature
,
199 cpu_to_le32(PCC_SIGNATURE
| ctx
->mbox_idx
));
201 WRITE_ONCE(generic_comm_base
->command
,
202 cpu_to_le16(SLIMPRO_MSG_TYPE(msg
[0]) | PCC_CMD_GENERATE_DB_INT
));
204 status
= le16_to_cpu(READ_ONCE(generic_comm_base
->status
));
205 status
&= ~PCC_STS_CMD_COMPLETE
;
206 WRITE_ONCE(generic_comm_base
->status
, cpu_to_le16(status
));
208 /* Copy the message to the PCC comm space */
209 for (i
= 0; i
< SLIMPRO_IIC_MSG_DWORD_COUNT
; i
++)
210 WRITE_ONCE(ptr
[i
], cpu_to_le32(msg
[i
]));
213 static int start_i2c_msg_xfer(struct slimpro_i2c_dev
*ctx
)
215 if (ctx
->mbox_client
.tx_block
|| !acpi_disabled
) {
216 if (!wait_for_completion_timeout(&ctx
->rd_complete
,
217 msecs_to_jiffies(MAILBOX_OP_TIMEOUT
)))
221 /* Check of invalid data or no device */
222 if (*ctx
->resp_msg
== 0xffffffff)
228 static int slimpro_i2c_send_msg(struct slimpro_i2c_dev
*ctx
,
234 ctx
->resp_msg
= data
;
236 if (!acpi_disabled
) {
237 reinit_completion(&ctx
->rd_complete
);
238 slimpro_i2c_pcc_tx_prepare(ctx
, msg
);
241 rc
= mbox_send_message(ctx
->mbox_chan
, msg
);
245 rc
= start_i2c_msg_xfer(ctx
);
249 mbox_chan_txdone(ctx
->mbox_chan
, 0);
251 ctx
->resp_msg
= NULL
;
256 static int slimpro_i2c_rd(struct slimpro_i2c_dev
*ctx
, u32 chip
,
257 u32 addr
, u32 addrlen
, u32 protocol
,
258 u32 readlen
, u32
*data
)
262 msg
[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS
, chip
,
263 SLIMPRO_IIC_READ
, protocol
, addrlen
, readlen
);
264 msg
[1] = SLIMPRO_IIC_ENCODE_ADDR(addr
);
267 return slimpro_i2c_send_msg(ctx
, msg
, data
);
270 static int slimpro_i2c_wr(struct slimpro_i2c_dev
*ctx
, u32 chip
,
271 u32 addr
, u32 addrlen
, u32 protocol
, u32 writelen
,
276 msg
[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS
, chip
,
277 SLIMPRO_IIC_WRITE
, protocol
, addrlen
, writelen
);
278 msg
[1] = SLIMPRO_IIC_ENCODE_ADDR(addr
);
281 return slimpro_i2c_send_msg(ctx
, msg
, msg
);
284 static int slimpro_i2c_blkrd(struct slimpro_i2c_dev
*ctx
, u32 chip
, u32 addr
,
285 u32 addrlen
, u32 protocol
, u32 readlen
,
286 u32 with_data_len
, void *data
)
292 paddr
= dma_map_single(ctx
->dev
, ctx
->dma_buffer
, readlen
, DMA_FROM_DEVICE
);
293 if (dma_mapping_error(ctx
->dev
, paddr
)) {
294 dev_err(&ctx
->adapter
.dev
, "Error in mapping dma buffer %p\n",
299 msg
[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS
, chip
, SLIMPRO_IIC_READ
,
300 protocol
, addrlen
, readlen
);
301 msg
[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR
|
302 SLIMPRO_IIC_ENCODE_FLAG_WITH_DATA_LEN(with_data_len
) |
303 SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr
) |
304 SLIMPRO_IIC_ENCODE_ADDR(addr
);
307 rc
= slimpro_i2c_send_msg(ctx
, msg
, msg
);
309 /* Copy to destination */
310 memcpy(data
, ctx
->dma_buffer
, readlen
);
312 dma_unmap_single(ctx
->dev
, paddr
, readlen
, DMA_FROM_DEVICE
);
316 static int slimpro_i2c_blkwr(struct slimpro_i2c_dev
*ctx
, u32 chip
,
317 u32 addr
, u32 addrlen
, u32 protocol
, u32 writelen
,
324 memcpy(ctx
->dma_buffer
, data
, writelen
);
325 paddr
= dma_map_single(ctx
->dev
, ctx
->dma_buffer
, writelen
,
327 if (dma_mapping_error(ctx
->dev
, paddr
)) {
328 dev_err(&ctx
->adapter
.dev
, "Error in mapping dma buffer %p\n",
333 msg
[0] = SLIMPRO_IIC_ENCODE_MSG(SLIMPRO_IIC_BUS
, chip
, SLIMPRO_IIC_WRITE
,
334 protocol
, addrlen
, writelen
);
335 msg
[1] = SLIMPRO_IIC_ENCODE_FLAG_BUFADDR
|
336 SLIMPRO_IIC_ENCODE_UPPER_BUFADDR(paddr
) |
337 SLIMPRO_IIC_ENCODE_ADDR(addr
);
340 if (ctx
->mbox_client
.tx_block
)
341 reinit_completion(&ctx
->rd_complete
);
343 rc
= slimpro_i2c_send_msg(ctx
, msg
, msg
);
345 dma_unmap_single(ctx
->dev
, paddr
, writelen
, DMA_TO_DEVICE
);
349 static int xgene_slimpro_i2c_xfer(struct i2c_adapter
*adap
, u16 addr
,
350 unsigned short flags
, char read_write
,
351 u8 command
, int size
,
352 union i2c_smbus_data
*data
)
354 struct slimpro_i2c_dev
*ctx
= i2c_get_adapdata(adap
);
355 int ret
= -EOPNOTSUPP
;
360 if (read_write
== I2C_SMBUS_READ
) {
361 ret
= slimpro_i2c_rd(ctx
, addr
, 0, 0,
362 SLIMPRO_IIC_SMB_PROTOCOL
,
366 ret
= slimpro_i2c_wr(ctx
, addr
, command
, SMBUS_CMD_LEN
,
367 SLIMPRO_IIC_SMB_PROTOCOL
,
371 case I2C_SMBUS_BYTE_DATA
:
372 if (read_write
== I2C_SMBUS_READ
) {
373 ret
= slimpro_i2c_rd(ctx
, addr
, command
, SMBUS_CMD_LEN
,
374 SLIMPRO_IIC_SMB_PROTOCOL
,
379 ret
= slimpro_i2c_wr(ctx
, addr
, command
, SMBUS_CMD_LEN
,
380 SLIMPRO_IIC_SMB_PROTOCOL
,
384 case I2C_SMBUS_WORD_DATA
:
385 if (read_write
== I2C_SMBUS_READ
) {
386 ret
= slimpro_i2c_rd(ctx
, addr
, command
, SMBUS_CMD_LEN
,
387 SLIMPRO_IIC_SMB_PROTOCOL
,
392 ret
= slimpro_i2c_wr(ctx
, addr
, command
, SMBUS_CMD_LEN
,
393 SLIMPRO_IIC_SMB_PROTOCOL
,
397 case I2C_SMBUS_BLOCK_DATA
:
398 if (read_write
== I2C_SMBUS_READ
) {
399 ret
= slimpro_i2c_blkrd(ctx
, addr
, command
,
401 SLIMPRO_IIC_SMB_PROTOCOL
,
402 I2C_SMBUS_BLOCK_MAX
+ 1,
403 IIC_SMB_WITH_DATA_LEN
,
407 ret
= slimpro_i2c_blkwr(ctx
, addr
, command
,
409 SLIMPRO_IIC_SMB_PROTOCOL
,
414 case I2C_SMBUS_I2C_BLOCK_DATA
:
415 if (read_write
== I2C_SMBUS_READ
) {
416 ret
= slimpro_i2c_blkrd(ctx
, addr
,
419 SLIMPRO_IIC_I2C_PROTOCOL
,
421 IIC_SMB_WITHOUT_DATA_LEN
,
424 ret
= slimpro_i2c_blkwr(ctx
, addr
, command
,
426 SLIMPRO_IIC_I2C_PROTOCOL
,
438 * Return list of supported functionality.
440 static u32
xgene_slimpro_i2c_func(struct i2c_adapter
*adapter
)
442 return I2C_FUNC_SMBUS_BYTE
|
443 I2C_FUNC_SMBUS_BYTE_DATA
|
444 I2C_FUNC_SMBUS_WORD_DATA
|
445 I2C_FUNC_SMBUS_BLOCK_DATA
|
446 I2C_FUNC_SMBUS_I2C_BLOCK
;
449 static const struct i2c_algorithm xgene_slimpro_i2c_algorithm
= {
450 .smbus_xfer
= xgene_slimpro_i2c_xfer
,
451 .functionality
= xgene_slimpro_i2c_func
,
454 static int xgene_slimpro_i2c_probe(struct platform_device
*pdev
)
456 struct slimpro_i2c_dev
*ctx
;
457 struct i2c_adapter
*adapter
;
458 struct mbox_client
*cl
;
461 ctx
= devm_kzalloc(&pdev
->dev
, sizeof(*ctx
), GFP_KERNEL
);
465 ctx
->dev
= &pdev
->dev
;
466 platform_set_drvdata(pdev
, ctx
);
467 cl
= &ctx
->mbox_client
;
469 /* Request mailbox channel */
470 cl
->dev
= &pdev
->dev
;
471 init_completion(&ctx
->rd_complete
);
472 cl
->tx_tout
= MAILBOX_OP_TIMEOUT
;
473 cl
->knows_txdone
= false;
476 cl
->rx_callback
= slimpro_i2c_rx_cb
;
477 ctx
->mbox_chan
= mbox_request_channel(cl
, MAILBOX_I2C_INDEX
);
478 if (IS_ERR(ctx
->mbox_chan
)) {
479 dev_err(&pdev
->dev
, "i2c mailbox channel request failed\n");
480 return PTR_ERR(ctx
->mbox_chan
);
483 struct acpi_pcct_hw_reduced
*cppc_ss
;
484 const struct acpi_device_id
*acpi_id
;
485 int version
= XGENE_SLIMPRO_I2C_V1
;
487 acpi_id
= acpi_match_device(pdev
->dev
.driver
->acpi_match_table
,
492 version
= (int)acpi_id
->driver_data
;
494 if (device_property_read_u32(&pdev
->dev
, "pcc-channel",
496 ctx
->mbox_idx
= MAILBOX_I2C_INDEX
;
498 cl
->tx_block
= false;
499 cl
->rx_callback
= slimpro_i2c_pcc_rx_cb
;
500 ctx
->mbox_chan
= pcc_mbox_request_channel(cl
, ctx
->mbox_idx
);
501 if (IS_ERR(ctx
->mbox_chan
)) {
502 dev_err(&pdev
->dev
, "PCC mailbox channel request failed\n");
503 return PTR_ERR(ctx
->mbox_chan
);
507 * The PCC mailbox controller driver should
508 * have parsed the PCCT (global table of all
509 * PCC channels) and stored pointers to the
510 * subspace communication region in con_priv.
512 cppc_ss
= ctx
->mbox_chan
->con_priv
;
514 dev_err(&pdev
->dev
, "PPC subspace not found\n");
519 if (!ctx
->mbox_chan
->mbox
->txdone_irq
) {
520 dev_err(&pdev
->dev
, "PCC IRQ not supported\n");
526 * This is the shared communication region
527 * for the OS and Platform to communicate over.
529 ctx
->comm_base_addr
= cppc_ss
->base_address
;
530 if (ctx
->comm_base_addr
) {
531 if (version
== XGENE_SLIMPRO_I2C_V2
)
532 ctx
->pcc_comm_addr
= memremap(
537 ctx
->pcc_comm_addr
= memremap(
542 dev_err(&pdev
->dev
, "Failed to get PCC comm region\n");
547 if (!ctx
->pcc_comm_addr
) {
549 "Failed to ioremap PCC comm region\n");
554 rc
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
556 dev_warn(&pdev
->dev
, "Unable to set dma mask\n");
558 /* Setup I2C adapter */
559 adapter
= &ctx
->adapter
;
560 snprintf(adapter
->name
, sizeof(adapter
->name
), "MAILBOX I2C");
561 adapter
->algo
= &xgene_slimpro_i2c_algorithm
;
562 adapter
->class = I2C_CLASS_HWMON
;
563 adapter
->dev
.parent
= &pdev
->dev
;
564 adapter
->dev
.of_node
= pdev
->dev
.of_node
;
565 ACPI_COMPANION_SET(&adapter
->dev
, ACPI_COMPANION(&pdev
->dev
));
566 i2c_set_adapdata(adapter
, ctx
);
567 rc
= i2c_add_adapter(adapter
);
571 dev_info(&pdev
->dev
, "Mailbox I2C Adapter registered\n");
576 mbox_free_channel(ctx
->mbox_chan
);
578 pcc_mbox_free_channel(ctx
->mbox_chan
);
583 static int xgene_slimpro_i2c_remove(struct platform_device
*pdev
)
585 struct slimpro_i2c_dev
*ctx
= platform_get_drvdata(pdev
);
587 i2c_del_adapter(&ctx
->adapter
);
590 mbox_free_channel(ctx
->mbox_chan
);
592 pcc_mbox_free_channel(ctx
->mbox_chan
);
597 static const struct of_device_id xgene_slimpro_i2c_dt_ids
[] = {
598 {.compatible
= "apm,xgene-slimpro-i2c" },
601 MODULE_DEVICE_TABLE(of
, xgene_slimpro_i2c_dt_ids
);
604 static const struct acpi_device_id xgene_slimpro_i2c_acpi_ids
[] = {
605 {"APMC0D40", XGENE_SLIMPRO_I2C_V1
},
606 {"APMC0D8B", XGENE_SLIMPRO_I2C_V2
},
609 MODULE_DEVICE_TABLE(acpi
, xgene_slimpro_i2c_acpi_ids
);
612 static struct platform_driver xgene_slimpro_i2c_driver
= {
613 .probe
= xgene_slimpro_i2c_probe
,
614 .remove
= xgene_slimpro_i2c_remove
,
616 .name
= "xgene-slimpro-i2c",
617 .of_match_table
= of_match_ptr(xgene_slimpro_i2c_dt_ids
),
618 .acpi_match_table
= ACPI_PTR(xgene_slimpro_i2c_acpi_ids
)
622 module_platform_driver(xgene_slimpro_i2c_driver
);
624 MODULE_DESCRIPTION("APM X-Gene SLIMpro I2C driver");
625 MODULE_AUTHOR("Feng Kan <fkan@apm.com>");
626 MODULE_AUTHOR("Hieu Le <hnle@apm.com>");
627 MODULE_LICENSE("GPL");