1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2013 Broadcom Corporation
4 #include <linux/device.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/sched.h>
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
13 #include <linux/slab.h>
15 /* Hardware register offsets and field defintions */
16 #define CS_OFFSET 0x00000020
17 #define CS_ACK_SHIFT 3
18 #define CS_ACK_MASK 0x00000008
19 #define CS_ACK_CMD_GEN_START 0x00000000
20 #define CS_ACK_CMD_GEN_RESTART 0x00000001
21 #define CS_CMD_SHIFT 1
22 #define CS_CMD_CMD_NO_ACTION 0x00000000
23 #define CS_CMD_CMD_START_RESTART 0x00000001
24 #define CS_CMD_CMD_STOP 0x00000002
26 #define CS_EN_CMD_ENABLE_BSC 0x00000001
28 #define TIM_OFFSET 0x00000024
29 #define TIM_PRESCALE_SHIFT 6
31 #define TIM_NO_DIV_SHIFT 2
32 #define TIM_DIV_SHIFT 0
34 #define DAT_OFFSET 0x00000028
36 #define TOUT_OFFSET 0x0000002c
38 #define TXFCR_OFFSET 0x0000003c
39 #define TXFCR_FIFO_FLUSH_MASK 0x00000080
40 #define TXFCR_FIFO_EN_MASK 0x00000040
42 #define IER_OFFSET 0x00000044
43 #define IER_READ_COMPLETE_INT_MASK 0x00000010
44 #define IER_I2C_INT_EN_MASK 0x00000008
45 #define IER_FIFO_INT_EN_MASK 0x00000002
46 #define IER_NOACK_EN_MASK 0x00000001
48 #define ISR_OFFSET 0x00000048
49 #define ISR_RESERVED_MASK 0xffffff60
50 #define ISR_CMDBUSY_MASK 0x00000080
51 #define ISR_READ_COMPLETE_MASK 0x00000010
52 #define ISR_SES_DONE_MASK 0x00000008
53 #define ISR_ERR_MASK 0x00000004
54 #define ISR_TXFIFOEMPTY_MASK 0x00000002
55 #define ISR_NOACK_MASK 0x00000001
57 #define CLKEN_OFFSET 0x0000004C
58 #define CLKEN_AUTOSENSE_OFF_MASK 0x00000080
59 #define CLKEN_M_SHIFT 4
60 #define CLKEN_N_SHIFT 1
61 #define CLKEN_CLKEN_MASK 0x00000001
63 #define FIFO_STATUS_OFFSET 0x00000054
64 #define FIFO_STATUS_RXFIFO_EMPTY_MASK 0x00000004
65 #define FIFO_STATUS_TXFIFO_EMPTY_MASK 0x00000010
67 #define HSTIM_OFFSET 0x00000058
68 #define HSTIM_HS_MODE_MASK 0x00008000
69 #define HSTIM_HS_HOLD_SHIFT 10
70 #define HSTIM_HS_HIGH_PHASE_SHIFT 5
71 #define HSTIM_HS_SETUP_SHIFT 0
73 #define PADCTL_OFFSET 0x0000005c
74 #define PADCTL_PAD_OUT_EN_MASK 0x00000004
76 #define RXFCR_OFFSET 0x00000068
77 #define RXFCR_NACK_EN_SHIFT 7
78 #define RXFCR_READ_COUNT_SHIFT 0
79 #define RXFIFORDOUT_OFFSET 0x0000006c
81 /* Locally used constants */
82 #define MAX_RX_FIFO_SIZE 64U /* bytes */
83 #define MAX_TX_FIFO_SIZE 64U /* bytes */
85 #define STD_EXT_CLK_FREQ 13000000UL
86 #define HS_EXT_CLK_FREQ 104000000UL
88 #define CONTROLLER_CODE 0x08 /* Controller codes are 0000_1xxxb */
90 #define I2C_TIMEOUT 100 /* msecs */
92 /* Operations that can be commanded to the controller */
100 enum bus_speed_index
{
106 enum hs_bus_speed_index
{
110 /* Internal divider settings for standard mode, fast mode and fast mode plus */
111 struct bus_speed_cfg
{
112 uint8_t time_m
; /* Number of cycles for setup time */
113 uint8_t time_n
; /* Number of cycles for hold time */
114 uint8_t prescale
; /* Prescale divider */
115 uint8_t time_p
; /* Timing coefficient */
116 uint8_t no_div
; /* Disable clock divider */
117 uint8_t time_div
; /* Post-prescale divider */
120 /* Internal divider settings for high-speed mode */
121 struct hs_bus_speed_cfg
{
122 uint8_t hs_hold
; /* Number of clock cycles SCL stays low until
123 the end of bit period */
124 uint8_t hs_high_phase
; /* Number of clock cycles SCL stays high
126 uint8_t hs_setup
; /* Number of clock cycles SCL stays low
128 uint8_t prescale
; /* Prescale divider */
129 uint8_t time_p
; /* Timing coefficient */
130 uint8_t no_div
; /* Disable clock divider */
131 uint8_t time_div
; /* Post-prescale divider */
134 static const struct bus_speed_cfg std_cfg_table
[] = {
135 [BCM_SPD_100K
] = {0x01, 0x01, 0x03, 0x06, 0x00, 0x02},
136 [BCM_SPD_400K
] = {0x05, 0x01, 0x03, 0x05, 0x01, 0x02},
137 [BCM_SPD_1MHZ
] = {0x01, 0x01, 0x03, 0x01, 0x01, 0x03},
140 static const struct hs_bus_speed_cfg hs_cfg_table
[] = {
141 [BCM_SPD_3P4MHZ
] = {0x01, 0x08, 0x14, 0x00, 0x06, 0x01, 0x00},
144 struct bcm_kona_i2c_dev
{
145 struct device
*device
;
149 struct clk
*external_clk
;
151 struct i2c_adapter adapter
;
153 struct completion done
;
155 const struct bus_speed_cfg
*std_cfg
;
156 const struct hs_bus_speed_cfg
*hs_cfg
;
159 static void bcm_kona_i2c_send_cmd_to_ctrl(struct bcm_kona_i2c_dev
*dev
,
160 enum bcm_kona_cmd_t cmd
)
162 dev_dbg(dev
->device
, "%s, %d\n", __func__
, cmd
);
165 case BCM_CMD_NOACTION
:
166 writel((CS_CMD_CMD_NO_ACTION
<< CS_CMD_SHIFT
) |
167 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
168 dev
->base
+ CS_OFFSET
);
172 writel((CS_ACK_CMD_GEN_START
<< CS_ACK_SHIFT
) |
173 (CS_CMD_CMD_START_RESTART
<< CS_CMD_SHIFT
) |
174 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
175 dev
->base
+ CS_OFFSET
);
178 case BCM_CMD_RESTART
:
179 writel((CS_ACK_CMD_GEN_RESTART
<< CS_ACK_SHIFT
) |
180 (CS_CMD_CMD_START_RESTART
<< CS_CMD_SHIFT
) |
181 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
182 dev
->base
+ CS_OFFSET
);
186 writel((CS_CMD_CMD_STOP
<< CS_CMD_SHIFT
) |
187 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
188 dev
->base
+ CS_OFFSET
);
192 dev_err(dev
->device
, "Unknown command %d\n", cmd
);
196 static void bcm_kona_i2c_enable_clock(struct bcm_kona_i2c_dev
*dev
)
198 writel(readl(dev
->base
+ CLKEN_OFFSET
) | CLKEN_CLKEN_MASK
,
199 dev
->base
+ CLKEN_OFFSET
);
202 static void bcm_kona_i2c_disable_clock(struct bcm_kona_i2c_dev
*dev
)
204 writel(readl(dev
->base
+ CLKEN_OFFSET
) & ~CLKEN_CLKEN_MASK
,
205 dev
->base
+ CLKEN_OFFSET
);
208 static irqreturn_t
bcm_kona_i2c_isr(int irq
, void *devid
)
210 struct bcm_kona_i2c_dev
*dev
= devid
;
211 uint32_t status
= readl(dev
->base
+ ISR_OFFSET
);
213 if ((status
& ~ISR_RESERVED_MASK
) == 0)
216 /* Must flush the TX FIFO when NAK detected */
217 if (status
& ISR_NOACK_MASK
)
218 writel(TXFCR_FIFO_FLUSH_MASK
| TXFCR_FIFO_EN_MASK
,
219 dev
->base
+ TXFCR_OFFSET
);
221 writel(status
& ~ISR_RESERVED_MASK
, dev
->base
+ ISR_OFFSET
);
222 complete(&dev
->done
);
227 /* Wait for ISR_CMDBUSY_MASK to go low before writing to CS, DAT, or RCD */
228 static int bcm_kona_i2c_wait_if_busy(struct bcm_kona_i2c_dev
*dev
)
230 unsigned long timeout
= jiffies
+ msecs_to_jiffies(I2C_TIMEOUT
);
232 while (readl(dev
->base
+ ISR_OFFSET
) & ISR_CMDBUSY_MASK
)
233 if (time_after(jiffies
, timeout
)) {
234 dev_err(dev
->device
, "CMDBUSY timeout\n");
241 /* Send command to I2C bus */
242 static int bcm_kona_send_i2c_cmd(struct bcm_kona_i2c_dev
*dev
,
243 enum bcm_kona_cmd_t cmd
)
246 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
248 /* Make sure the hardware is ready */
249 rc
= bcm_kona_i2c_wait_if_busy(dev
);
253 /* Unmask the session done interrupt */
254 writel(IER_I2C_INT_EN_MASK
, dev
->base
+ IER_OFFSET
);
256 /* Mark as incomplete before sending the command */
257 reinit_completion(&dev
->done
);
259 /* Send the command */
260 bcm_kona_i2c_send_cmd_to_ctrl(dev
, cmd
);
262 /* Wait for transaction to finish or timeout */
263 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
265 /* Mask all interrupts */
266 writel(0, dev
->base
+ IER_OFFSET
);
269 dev_err(dev
->device
, "controller timed out\n");
274 bcm_kona_i2c_send_cmd_to_ctrl(dev
, BCM_CMD_NOACTION
);
279 /* Read a single RX FIFO worth of data from the i2c bus */
280 static int bcm_kona_i2c_read_fifo_single(struct bcm_kona_i2c_dev
*dev
,
281 uint8_t *buf
, unsigned int len
,
282 unsigned int last_byte_nak
)
284 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
286 /* Mark as incomplete before starting the RX FIFO */
287 reinit_completion(&dev
->done
);
289 /* Unmask the read complete interrupt */
290 writel(IER_READ_COMPLETE_INT_MASK
, dev
->base
+ IER_OFFSET
);
292 /* Start the RX FIFO */
293 writel((last_byte_nak
<< RXFCR_NACK_EN_SHIFT
) |
294 (len
<< RXFCR_READ_COUNT_SHIFT
),
295 dev
->base
+ RXFCR_OFFSET
);
297 /* Wait for FIFO read to complete */
298 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
300 /* Mask all interrupts */
301 writel(0, dev
->base
+ IER_OFFSET
);
304 dev_err(dev
->device
, "RX FIFO time out\n");
308 /* Read data from FIFO */
309 for (; len
> 0; len
--, buf
++)
310 *buf
= readl(dev
->base
+ RXFIFORDOUT_OFFSET
);
315 /* Read any amount of data using the RX FIFO from the i2c bus */
316 static int bcm_kona_i2c_read_fifo(struct bcm_kona_i2c_dev
*dev
,
319 unsigned int bytes_to_read
= MAX_RX_FIFO_SIZE
;
320 unsigned int last_byte_nak
= 0;
321 unsigned int bytes_read
= 0;
324 uint8_t *tmp_buf
= msg
->buf
;
326 while (bytes_read
< msg
->len
) {
327 if (msg
->len
- bytes_read
<= MAX_RX_FIFO_SIZE
) {
328 last_byte_nak
= 1; /* NAK last byte of transfer */
329 bytes_to_read
= msg
->len
- bytes_read
;
332 rc
= bcm_kona_i2c_read_fifo_single(dev
, tmp_buf
, bytes_to_read
,
337 bytes_read
+= bytes_to_read
;
338 tmp_buf
+= bytes_to_read
;
344 /* Write a single byte of data to the i2c bus */
345 static int bcm_kona_i2c_write_byte(struct bcm_kona_i2c_dev
*dev
, uint8_t data
,
346 unsigned int nak_expected
)
349 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
350 unsigned int nak_received
;
352 /* Make sure the hardware is ready */
353 rc
= bcm_kona_i2c_wait_if_busy(dev
);
357 /* Clear pending session done interrupt */
358 writel(ISR_SES_DONE_MASK
, dev
->base
+ ISR_OFFSET
);
360 /* Unmask the session done interrupt */
361 writel(IER_I2C_INT_EN_MASK
, dev
->base
+ IER_OFFSET
);
363 /* Mark as incomplete before sending the data */
364 reinit_completion(&dev
->done
);
366 /* Send one byte of data */
367 writel(data
, dev
->base
+ DAT_OFFSET
);
369 /* Wait for byte to be written */
370 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
372 /* Mask all interrupts */
373 writel(0, dev
->base
+ IER_OFFSET
);
376 dev_dbg(dev
->device
, "controller timed out\n");
380 nak_received
= readl(dev
->base
+ CS_OFFSET
) & CS_ACK_MASK
? 1 : 0;
382 if (nak_received
^ nak_expected
) {
383 dev_dbg(dev
->device
, "unexpected NAK/ACK\n");
390 /* Write a single TX FIFO worth of data to the i2c bus */
391 static int bcm_kona_i2c_write_fifo_single(struct bcm_kona_i2c_dev
*dev
,
392 uint8_t *buf
, unsigned int len
)
395 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
396 unsigned int fifo_status
;
398 /* Mark as incomplete before sending data to the TX FIFO */
399 reinit_completion(&dev
->done
);
401 /* Unmask the fifo empty and nak interrupt */
402 writel(IER_FIFO_INT_EN_MASK
| IER_NOACK_EN_MASK
,
403 dev
->base
+ IER_OFFSET
);
405 /* Disable IRQ to load a FIFO worth of data without interruption */
406 disable_irq(dev
->irq
);
408 /* Write data into FIFO */
409 for (k
= 0; k
< len
; k
++)
410 writel(buf
[k
], (dev
->base
+ DAT_OFFSET
));
412 /* Enable IRQ now that data has been loaded */
413 enable_irq(dev
->irq
);
415 /* Wait for FIFO to empty */
417 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
418 fifo_status
= readl(dev
->base
+ FIFO_STATUS_OFFSET
);
419 } while (time_left
&& !(fifo_status
& FIFO_STATUS_TXFIFO_EMPTY_MASK
));
421 /* Mask all interrupts */
422 writel(0, dev
->base
+ IER_OFFSET
);
424 /* Check if there was a NAK */
425 if (readl(dev
->base
+ CS_OFFSET
) & CS_ACK_MASK
) {
426 dev_err(dev
->device
, "unexpected NAK\n");
430 /* Check if a timeout occured */
432 dev_err(dev
->device
, "completion timed out\n");
440 /* Write any amount of data using TX FIFO to the i2c bus */
441 static int bcm_kona_i2c_write_fifo(struct bcm_kona_i2c_dev
*dev
,
444 unsigned int bytes_to_write
= MAX_TX_FIFO_SIZE
;
445 unsigned int bytes_written
= 0;
448 uint8_t *tmp_buf
= msg
->buf
;
450 while (bytes_written
< msg
->len
) {
451 if (msg
->len
- bytes_written
<= MAX_TX_FIFO_SIZE
)
452 bytes_to_write
= msg
->len
- bytes_written
;
454 rc
= bcm_kona_i2c_write_fifo_single(dev
, tmp_buf
,
459 bytes_written
+= bytes_to_write
;
460 tmp_buf
+= bytes_to_write
;
466 /* Send i2c address */
467 static int bcm_kona_i2c_do_addr(struct bcm_kona_i2c_dev
*dev
,
472 if (msg
->flags
& I2C_M_TEN
) {
473 /* First byte is 11110XX0 where XX is upper 2 bits */
474 addr
= 0xF0 | ((msg
->addr
& 0x300) >> 7);
475 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
478 /* Second byte is the remaining 8 bits */
479 addr
= msg
->addr
& 0xFF;
480 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
483 if (msg
->flags
& I2C_M_RD
) {
484 /* For read, send restart command */
485 if (bcm_kona_send_i2c_cmd(dev
, BCM_CMD_RESTART
) < 0)
488 /* Then re-send the first byte with the read bit set */
489 addr
= 0xF0 | ((msg
->addr
& 0x300) >> 7) | 0x01;
490 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
494 addr
= i2c_8bit_addr_from_msg(msg
);
496 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
503 static void bcm_kona_i2c_enable_autosense(struct bcm_kona_i2c_dev
*dev
)
505 writel(readl(dev
->base
+ CLKEN_OFFSET
) & ~CLKEN_AUTOSENSE_OFF_MASK
,
506 dev
->base
+ CLKEN_OFFSET
);
509 static void bcm_kona_i2c_config_timing(struct bcm_kona_i2c_dev
*dev
)
511 writel(readl(dev
->base
+ HSTIM_OFFSET
) & ~HSTIM_HS_MODE_MASK
,
512 dev
->base
+ HSTIM_OFFSET
);
514 writel((dev
->std_cfg
->prescale
<< TIM_PRESCALE_SHIFT
) |
515 (dev
->std_cfg
->time_p
<< TIM_P_SHIFT
) |
516 (dev
->std_cfg
->no_div
<< TIM_NO_DIV_SHIFT
) |
517 (dev
->std_cfg
->time_div
<< TIM_DIV_SHIFT
),
518 dev
->base
+ TIM_OFFSET
);
520 writel((dev
->std_cfg
->time_m
<< CLKEN_M_SHIFT
) |
521 (dev
->std_cfg
->time_n
<< CLKEN_N_SHIFT
) |
523 dev
->base
+ CLKEN_OFFSET
);
526 static void bcm_kona_i2c_config_timing_hs(struct bcm_kona_i2c_dev
*dev
)
528 writel((dev
->hs_cfg
->prescale
<< TIM_PRESCALE_SHIFT
) |
529 (dev
->hs_cfg
->time_p
<< TIM_P_SHIFT
) |
530 (dev
->hs_cfg
->no_div
<< TIM_NO_DIV_SHIFT
) |
531 (dev
->hs_cfg
->time_div
<< TIM_DIV_SHIFT
),
532 dev
->base
+ TIM_OFFSET
);
534 writel((dev
->hs_cfg
->hs_hold
<< HSTIM_HS_HOLD_SHIFT
) |
535 (dev
->hs_cfg
->hs_high_phase
<< HSTIM_HS_HIGH_PHASE_SHIFT
) |
536 (dev
->hs_cfg
->hs_setup
<< HSTIM_HS_SETUP_SHIFT
),
537 dev
->base
+ HSTIM_OFFSET
);
539 writel(readl(dev
->base
+ HSTIM_OFFSET
) | HSTIM_HS_MODE_MASK
,
540 dev
->base
+ HSTIM_OFFSET
);
543 static int bcm_kona_i2c_switch_to_hs(struct bcm_kona_i2c_dev
*dev
)
547 /* Send controller code at standard speed */
548 rc
= bcm_kona_i2c_write_byte(dev
, CONTROLLER_CODE
, 1);
550 pr_err("High speed handshake failed\n");
554 /* Configure external clock to higher frequency */
555 rc
= clk_set_rate(dev
->external_clk
, HS_EXT_CLK_FREQ
);
557 dev_err(dev
->device
, "%s: clk_set_rate returned %d\n",
562 /* Reconfigure internal dividers */
563 bcm_kona_i2c_config_timing_hs(dev
);
565 /* Send a restart command */
566 rc
= bcm_kona_send_i2c_cmd(dev
, BCM_CMD_RESTART
);
568 dev_err(dev
->device
, "High speed restart command failed\n");
573 static int bcm_kona_i2c_switch_to_std(struct bcm_kona_i2c_dev
*dev
)
577 /* Reconfigure internal dividers */
578 bcm_kona_i2c_config_timing(dev
);
580 /* Configure external clock to lower frequency */
581 rc
= clk_set_rate(dev
->external_clk
, STD_EXT_CLK_FREQ
);
583 dev_err(dev
->device
, "%s: clk_set_rate returned %d\n",
590 static int bcm_kona_i2c_xfer(struct i2c_adapter
*adapter
,
591 struct i2c_msg msgs
[], int num
)
593 struct bcm_kona_i2c_dev
*dev
= i2c_get_adapdata(adapter
);
594 struct i2c_msg
*pmsg
;
598 rc
= clk_prepare_enable(dev
->external_clk
);
600 dev_err(dev
->device
, "%s: peri clock enable failed. err %d\n",
605 /* Enable pad output */
606 writel(0, dev
->base
+ PADCTL_OFFSET
);
608 /* Enable internal clocks */
609 bcm_kona_i2c_enable_clock(dev
);
611 /* Send start command */
612 rc
= bcm_kona_send_i2c_cmd(dev
, BCM_CMD_START
);
614 dev_err(dev
->device
, "Start command failed rc = %d\n", rc
);
615 goto xfer_disable_pad
;
618 /* Switch to high speed if applicable */
620 rc
= bcm_kona_i2c_switch_to_hs(dev
);
625 /* Loop through all messages */
626 for (i
= 0; i
< num
; i
++) {
629 /* Send restart for subsequent messages */
630 if ((i
!= 0) && ((pmsg
->flags
& I2C_M_NOSTART
) == 0)) {
631 rc
= bcm_kona_send_i2c_cmd(dev
, BCM_CMD_RESTART
);
634 "restart cmd failed rc = %d\n", rc
);
639 /* Send target address */
640 if (!(pmsg
->flags
& I2C_M_NOSTART
)) {
641 rc
= bcm_kona_i2c_do_addr(dev
, pmsg
);
644 "NAK from addr %2.2x msg#%d rc = %d\n",
650 /* Perform data transfer */
651 if (pmsg
->flags
& I2C_M_RD
) {
652 rc
= bcm_kona_i2c_read_fifo(dev
, pmsg
);
654 dev_err(dev
->device
, "read failure\n");
658 rc
= bcm_kona_i2c_write_fifo(dev
, pmsg
);
660 dev_err(dev
->device
, "write failure");
669 /* Send a STOP command */
670 bcm_kona_send_i2c_cmd(dev
, BCM_CMD_STOP
);
672 /* Return from high speed if applicable */
674 int hs_rc
= bcm_kona_i2c_switch_to_std(dev
);
681 /* Disable pad output */
682 writel(PADCTL_PAD_OUT_EN_MASK
, dev
->base
+ PADCTL_OFFSET
);
684 /* Stop internal clock */
685 bcm_kona_i2c_disable_clock(dev
);
687 clk_disable_unprepare(dev
->external_clk
);
692 static uint32_t bcm_kona_i2c_functionality(struct i2c_adapter
*adap
)
694 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_10BIT_ADDR
|
698 static const struct i2c_algorithm bcm_algo
= {
699 .xfer
= bcm_kona_i2c_xfer
,
700 .functionality
= bcm_kona_i2c_functionality
,
703 static int bcm_kona_i2c_assign_bus_speed(struct bcm_kona_i2c_dev
*dev
)
705 unsigned int bus_speed
;
706 int ret
= of_property_read_u32(dev
->device
->of_node
, "clock-frequency",
709 dev_err(dev
->device
, "missing clock-frequency property\n");
714 case I2C_MAX_STANDARD_MODE_FREQ
:
715 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_100K
];
717 case I2C_MAX_FAST_MODE_FREQ
:
718 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_400K
];
720 case I2C_MAX_FAST_MODE_PLUS_FREQ
:
721 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_1MHZ
];
723 case I2C_MAX_HIGH_SPEED_MODE_FREQ
:
724 /* Send controller code at 100k */
725 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_100K
];
726 dev
->hs_cfg
= &hs_cfg_table
[BCM_SPD_3P4MHZ
];
729 pr_err("%d hz bus speed not supported\n", bus_speed
);
730 pr_err("Valid speeds are 100khz, 400khz, 1mhz, and 3.4mhz\n");
737 static int bcm_kona_i2c_probe(struct platform_device
*pdev
)
740 struct bcm_kona_i2c_dev
*dev
;
741 struct i2c_adapter
*adap
;
743 /* Allocate memory for private data structure */
744 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
748 platform_set_drvdata(pdev
, dev
);
749 dev
->device
= &pdev
->dev
;
750 init_completion(&dev
->done
);
752 /* Map hardware registers */
753 dev
->base
= devm_platform_ioremap_resource(pdev
, 0);
754 if (IS_ERR(dev
->base
))
755 return PTR_ERR(dev
->base
);
757 /* Get and enable external clock */
758 dev
->external_clk
= devm_clk_get(dev
->device
, NULL
);
759 if (IS_ERR(dev
->external_clk
)) {
760 dev_err(dev
->device
, "couldn't get clock\n");
764 rc
= clk_set_rate(dev
->external_clk
, STD_EXT_CLK_FREQ
);
766 dev_err(dev
->device
, "%s: clk_set_rate returned %d\n",
771 rc
= clk_prepare_enable(dev
->external_clk
);
773 dev_err(dev
->device
, "couldn't enable clock\n");
777 /* Parse bus speed */
778 rc
= bcm_kona_i2c_assign_bus_speed(dev
);
780 goto probe_disable_clk
;
782 /* Enable internal clocks */
783 bcm_kona_i2c_enable_clock(dev
);
785 /* Configure internal dividers */
786 bcm_kona_i2c_config_timing(dev
);
788 /* Disable timeout */
789 writel(0, dev
->base
+ TOUT_OFFSET
);
791 /* Enable autosense */
792 bcm_kona_i2c_enable_autosense(dev
);
795 writel(TXFCR_FIFO_FLUSH_MASK
| TXFCR_FIFO_EN_MASK
,
796 dev
->base
+ TXFCR_OFFSET
);
798 /* Mask all interrupts */
799 writel(0, dev
->base
+ IER_OFFSET
);
801 /* Clear all pending interrupts */
802 writel(ISR_CMDBUSY_MASK
|
803 ISR_READ_COMPLETE_MASK
|
806 ISR_TXFIFOEMPTY_MASK
|
808 dev
->base
+ ISR_OFFSET
);
810 /* Get the interrupt number */
811 dev
->irq
= platform_get_irq(pdev
, 0);
814 goto probe_disable_clk
;
817 /* register the ISR handler */
818 rc
= devm_request_irq(&pdev
->dev
, dev
->irq
, bcm_kona_i2c_isr
,
819 IRQF_SHARED
, pdev
->name
, dev
);
821 dev_err(dev
->device
, "failed to request irq %i\n", dev
->irq
);
822 goto probe_disable_clk
;
825 /* Enable the controller but leave it idle */
826 bcm_kona_i2c_send_cmd_to_ctrl(dev
, BCM_CMD_NOACTION
);
828 /* Disable pad output */
829 writel(PADCTL_PAD_OUT_EN_MASK
, dev
->base
+ PADCTL_OFFSET
);
831 /* Disable internal clock */
832 bcm_kona_i2c_disable_clock(dev
);
834 /* Disable external clock */
835 clk_disable_unprepare(dev
->external_clk
);
837 /* Add the i2c adapter */
838 adap
= &dev
->adapter
;
839 i2c_set_adapdata(adap
, dev
);
840 adap
->owner
= THIS_MODULE
;
841 strscpy(adap
->name
, "Broadcom I2C adapter", sizeof(adap
->name
));
842 adap
->algo
= &bcm_algo
;
843 adap
->dev
.parent
= &pdev
->dev
;
844 adap
->dev
.of_node
= pdev
->dev
.of_node
;
846 rc
= i2c_add_adapter(adap
);
850 dev_info(dev
->device
, "device registered successfully\n");
855 bcm_kona_i2c_disable_clock(dev
);
856 clk_disable_unprepare(dev
->external_clk
);
861 static void bcm_kona_i2c_remove(struct platform_device
*pdev
)
863 struct bcm_kona_i2c_dev
*dev
= platform_get_drvdata(pdev
);
865 i2c_del_adapter(&dev
->adapter
);
868 static const struct of_device_id bcm_kona_i2c_of_match
[] = {
869 {.compatible
= "brcm,kona-i2c",},
872 MODULE_DEVICE_TABLE(of
, bcm_kona_i2c_of_match
);
874 static struct platform_driver bcm_kona_i2c_driver
= {
876 .name
= "bcm-kona-i2c",
877 .of_match_table
= bcm_kona_i2c_of_match
,
879 .probe
= bcm_kona_i2c_probe
,
880 .remove_new
= bcm_kona_i2c_remove
,
882 module_platform_driver(bcm_kona_i2c_driver
);
884 MODULE_AUTHOR("Tim Kryger <tkryger@broadcom.com>");
885 MODULE_DESCRIPTION("Broadcom Kona I2C Driver");
886 MODULE_LICENSE("GPL v2");