2 * Copyright (C) 2013 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
23 #include <linux/slab.h>
25 /* Hardware register offsets and field defintions */
26 #define CS_OFFSET 0x00000020
27 #define CS_ACK_SHIFT 3
28 #define CS_ACK_MASK 0x00000008
29 #define CS_ACK_CMD_GEN_START 0x00000000
30 #define CS_ACK_CMD_GEN_RESTART 0x00000001
31 #define CS_CMD_SHIFT 1
32 #define CS_CMD_CMD_NO_ACTION 0x00000000
33 #define CS_CMD_CMD_START_RESTART 0x00000001
34 #define CS_CMD_CMD_STOP 0x00000002
36 #define CS_EN_CMD_ENABLE_BSC 0x00000001
38 #define TIM_OFFSET 0x00000024
39 #define TIM_PRESCALE_SHIFT 6
41 #define TIM_NO_DIV_SHIFT 2
42 #define TIM_DIV_SHIFT 0
44 #define DAT_OFFSET 0x00000028
46 #define TOUT_OFFSET 0x0000002c
48 #define TXFCR_OFFSET 0x0000003c
49 #define TXFCR_FIFO_FLUSH_MASK 0x00000080
50 #define TXFCR_FIFO_EN_MASK 0x00000040
52 #define IER_OFFSET 0x00000044
53 #define IER_READ_COMPLETE_INT_MASK 0x00000010
54 #define IER_I2C_INT_EN_MASK 0x00000008
55 #define IER_FIFO_INT_EN_MASK 0x00000002
56 #define IER_NOACK_EN_MASK 0x00000001
58 #define ISR_OFFSET 0x00000048
59 #define ISR_RESERVED_MASK 0xffffff60
60 #define ISR_CMDBUSY_MASK 0x00000080
61 #define ISR_READ_COMPLETE_MASK 0x00000010
62 #define ISR_SES_DONE_MASK 0x00000008
63 #define ISR_ERR_MASK 0x00000004
64 #define ISR_TXFIFOEMPTY_MASK 0x00000002
65 #define ISR_NOACK_MASK 0x00000001
67 #define CLKEN_OFFSET 0x0000004C
68 #define CLKEN_AUTOSENSE_OFF_MASK 0x00000080
69 #define CLKEN_M_SHIFT 4
70 #define CLKEN_N_SHIFT 1
71 #define CLKEN_CLKEN_MASK 0x00000001
73 #define FIFO_STATUS_OFFSET 0x00000054
74 #define FIFO_STATUS_RXFIFO_EMPTY_MASK 0x00000004
75 #define FIFO_STATUS_TXFIFO_EMPTY_MASK 0x00000010
77 #define HSTIM_OFFSET 0x00000058
78 #define HSTIM_HS_MODE_MASK 0x00008000
79 #define HSTIM_HS_HOLD_SHIFT 10
80 #define HSTIM_HS_HIGH_PHASE_SHIFT 5
81 #define HSTIM_HS_SETUP_SHIFT 0
83 #define PADCTL_OFFSET 0x0000005c
84 #define PADCTL_PAD_OUT_EN_MASK 0x00000004
86 #define RXFCR_OFFSET 0x00000068
87 #define RXFCR_NACK_EN_SHIFT 7
88 #define RXFCR_READ_COUNT_SHIFT 0
89 #define RXFIFORDOUT_OFFSET 0x0000006c
91 /* Locally used constants */
92 #define MAX_RX_FIFO_SIZE 64U /* bytes */
93 #define MAX_TX_FIFO_SIZE 64U /* bytes */
95 #define STD_EXT_CLK_FREQ 13000000UL
96 #define HS_EXT_CLK_FREQ 104000000UL
98 #define MASTERCODE 0x08 /* Mastercodes are 0000_1xxxb */
100 #define I2C_TIMEOUT 100 /* msecs */
102 /* Operations that can be commanded to the controller */
103 enum bcm_kona_cmd_t
{
104 BCM_CMD_NOACTION
= 0,
110 enum bus_speed_index
{
116 enum hs_bus_speed_index
{
120 /* Internal divider settings for standard mode, fast mode and fast mode plus */
121 struct bus_speed_cfg
{
122 uint8_t time_m
; /* Number of cycles for setup time */
123 uint8_t time_n
; /* Number of cycles for hold time */
124 uint8_t prescale
; /* Prescale divider */
125 uint8_t time_p
; /* Timing coefficient */
126 uint8_t no_div
; /* Disable clock divider */
127 uint8_t time_div
; /* Post-prescale divider */
130 /* Internal divider settings for high-speed mode */
131 struct hs_bus_speed_cfg
{
132 uint8_t hs_hold
; /* Number of clock cycles SCL stays low until
133 the end of bit period */
134 uint8_t hs_high_phase
; /* Number of clock cycles SCL stays high
136 uint8_t hs_setup
; /* Number of clock cycles SCL stays low
138 uint8_t prescale
; /* Prescale divider */
139 uint8_t time_p
; /* Timing coefficient */
140 uint8_t no_div
; /* Disable clock divider */
141 uint8_t time_div
; /* Post-prescale divider */
144 static const struct bus_speed_cfg std_cfg_table
[] = {
145 [BCM_SPD_100K
] = {0x01, 0x01, 0x03, 0x06, 0x00, 0x02},
146 [BCM_SPD_400K
] = {0x05, 0x01, 0x03, 0x05, 0x01, 0x02},
147 [BCM_SPD_1MHZ
] = {0x01, 0x01, 0x03, 0x01, 0x01, 0x03},
150 static const struct hs_bus_speed_cfg hs_cfg_table
[] = {
151 [BCM_SPD_3P4MHZ
] = {0x01, 0x08, 0x14, 0x00, 0x06, 0x01, 0x00},
154 struct bcm_kona_i2c_dev
{
155 struct device
*device
;
159 struct clk
*external_clk
;
161 struct i2c_adapter adapter
;
163 struct completion done
;
165 const struct bus_speed_cfg
*std_cfg
;
166 const struct hs_bus_speed_cfg
*hs_cfg
;
169 static void bcm_kona_i2c_send_cmd_to_ctrl(struct bcm_kona_i2c_dev
*dev
,
170 enum bcm_kona_cmd_t cmd
)
172 dev_dbg(dev
->device
, "%s, %d\n", __func__
, cmd
);
175 case BCM_CMD_NOACTION
:
176 writel((CS_CMD_CMD_NO_ACTION
<< CS_CMD_SHIFT
) |
177 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
178 dev
->base
+ CS_OFFSET
);
182 writel((CS_ACK_CMD_GEN_START
<< CS_ACK_SHIFT
) |
183 (CS_CMD_CMD_START_RESTART
<< CS_CMD_SHIFT
) |
184 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
185 dev
->base
+ CS_OFFSET
);
188 case BCM_CMD_RESTART
:
189 writel((CS_ACK_CMD_GEN_RESTART
<< CS_ACK_SHIFT
) |
190 (CS_CMD_CMD_START_RESTART
<< CS_CMD_SHIFT
) |
191 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
192 dev
->base
+ CS_OFFSET
);
196 writel((CS_CMD_CMD_STOP
<< CS_CMD_SHIFT
) |
197 (CS_EN_CMD_ENABLE_BSC
<< CS_EN_SHIFT
),
198 dev
->base
+ CS_OFFSET
);
202 dev_err(dev
->device
, "Unknown command %d\n", cmd
);
206 static void bcm_kona_i2c_enable_clock(struct bcm_kona_i2c_dev
*dev
)
208 writel(readl(dev
->base
+ CLKEN_OFFSET
) | CLKEN_CLKEN_MASK
,
209 dev
->base
+ CLKEN_OFFSET
);
212 static void bcm_kona_i2c_disable_clock(struct bcm_kona_i2c_dev
*dev
)
214 writel(readl(dev
->base
+ CLKEN_OFFSET
) & ~CLKEN_CLKEN_MASK
,
215 dev
->base
+ CLKEN_OFFSET
);
218 static irqreturn_t
bcm_kona_i2c_isr(int irq
, void *devid
)
220 struct bcm_kona_i2c_dev
*dev
= devid
;
221 uint32_t status
= readl(dev
->base
+ ISR_OFFSET
);
223 if ((status
& ~ISR_RESERVED_MASK
) == 0)
226 /* Must flush the TX FIFO when NAK detected */
227 if (status
& ISR_NOACK_MASK
)
228 writel(TXFCR_FIFO_FLUSH_MASK
| TXFCR_FIFO_EN_MASK
,
229 dev
->base
+ TXFCR_OFFSET
);
231 writel(status
& ~ISR_RESERVED_MASK
, dev
->base
+ ISR_OFFSET
);
232 complete_all(&dev
->done
);
237 /* Wait for ISR_CMDBUSY_MASK to go low before writing to CS, DAT, or RCD */
238 static int bcm_kona_i2c_wait_if_busy(struct bcm_kona_i2c_dev
*dev
)
240 unsigned long timeout
= jiffies
+ msecs_to_jiffies(I2C_TIMEOUT
);
242 while (readl(dev
->base
+ ISR_OFFSET
) & ISR_CMDBUSY_MASK
)
243 if (time_after(jiffies
, timeout
)) {
244 dev_err(dev
->device
, "CMDBUSY timeout\n");
251 /* Send command to I2C bus */
252 static int bcm_kona_send_i2c_cmd(struct bcm_kona_i2c_dev
*dev
,
253 enum bcm_kona_cmd_t cmd
)
256 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
258 /* Make sure the hardware is ready */
259 rc
= bcm_kona_i2c_wait_if_busy(dev
);
263 /* Unmask the session done interrupt */
264 writel(IER_I2C_INT_EN_MASK
, dev
->base
+ IER_OFFSET
);
266 /* Mark as incomplete before sending the command */
267 reinit_completion(&dev
->done
);
269 /* Send the command */
270 bcm_kona_i2c_send_cmd_to_ctrl(dev
, cmd
);
272 /* Wait for transaction to finish or timeout */
273 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
275 /* Mask all interrupts */
276 writel(0, dev
->base
+ IER_OFFSET
);
279 dev_err(dev
->device
, "controller timed out\n");
284 bcm_kona_i2c_send_cmd_to_ctrl(dev
, BCM_CMD_NOACTION
);
289 /* Read a single RX FIFO worth of data from the i2c bus */
290 static int bcm_kona_i2c_read_fifo_single(struct bcm_kona_i2c_dev
*dev
,
291 uint8_t *buf
, unsigned int len
,
292 unsigned int last_byte_nak
)
294 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
296 /* Mark as incomplete before starting the RX FIFO */
297 reinit_completion(&dev
->done
);
299 /* Unmask the read complete interrupt */
300 writel(IER_READ_COMPLETE_INT_MASK
, dev
->base
+ IER_OFFSET
);
302 /* Start the RX FIFO */
303 writel((last_byte_nak
<< RXFCR_NACK_EN_SHIFT
) |
304 (len
<< RXFCR_READ_COUNT_SHIFT
),
305 dev
->base
+ RXFCR_OFFSET
);
307 /* Wait for FIFO read to complete */
308 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
310 /* Mask all interrupts */
311 writel(0, dev
->base
+ IER_OFFSET
);
314 dev_err(dev
->device
, "RX FIFO time out\n");
318 /* Read data from FIFO */
319 for (; len
> 0; len
--, buf
++)
320 *buf
= readl(dev
->base
+ RXFIFORDOUT_OFFSET
);
325 /* Read any amount of data using the RX FIFO from the i2c bus */
326 static int bcm_kona_i2c_read_fifo(struct bcm_kona_i2c_dev
*dev
,
329 unsigned int bytes_to_read
= MAX_RX_FIFO_SIZE
;
330 unsigned int last_byte_nak
= 0;
331 unsigned int bytes_read
= 0;
334 uint8_t *tmp_buf
= msg
->buf
;
336 while (bytes_read
< msg
->len
) {
337 if (msg
->len
- bytes_read
<= MAX_RX_FIFO_SIZE
) {
338 last_byte_nak
= 1; /* NAK last byte of transfer */
339 bytes_to_read
= msg
->len
- bytes_read
;
342 rc
= bcm_kona_i2c_read_fifo_single(dev
, tmp_buf
, bytes_to_read
,
347 bytes_read
+= bytes_to_read
;
348 tmp_buf
+= bytes_to_read
;
354 /* Write a single byte of data to the i2c bus */
355 static int bcm_kona_i2c_write_byte(struct bcm_kona_i2c_dev
*dev
, uint8_t data
,
356 unsigned int nak_expected
)
359 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
360 unsigned int nak_received
;
362 /* Make sure the hardware is ready */
363 rc
= bcm_kona_i2c_wait_if_busy(dev
);
367 /* Clear pending session done interrupt */
368 writel(ISR_SES_DONE_MASK
, dev
->base
+ ISR_OFFSET
);
370 /* Unmask the session done interrupt */
371 writel(IER_I2C_INT_EN_MASK
, dev
->base
+ IER_OFFSET
);
373 /* Mark as incomplete before sending the data */
374 reinit_completion(&dev
->done
);
376 /* Send one byte of data */
377 writel(data
, dev
->base
+ DAT_OFFSET
);
379 /* Wait for byte to be written */
380 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
382 /* Mask all interrupts */
383 writel(0, dev
->base
+ IER_OFFSET
);
386 dev_dbg(dev
->device
, "controller timed out\n");
390 nak_received
= readl(dev
->base
+ CS_OFFSET
) & CS_ACK_MASK
? 1 : 0;
392 if (nak_received
^ nak_expected
) {
393 dev_dbg(dev
->device
, "unexpected NAK/ACK\n");
400 /* Write a single TX FIFO worth of data to the i2c bus */
401 static int bcm_kona_i2c_write_fifo_single(struct bcm_kona_i2c_dev
*dev
,
402 uint8_t *buf
, unsigned int len
)
405 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT
);
406 unsigned int fifo_status
;
408 /* Mark as incomplete before sending data to the TX FIFO */
409 reinit_completion(&dev
->done
);
411 /* Unmask the fifo empty and nak interrupt */
412 writel(IER_FIFO_INT_EN_MASK
| IER_NOACK_EN_MASK
,
413 dev
->base
+ IER_OFFSET
);
415 /* Disable IRQ to load a FIFO worth of data without interruption */
416 disable_irq(dev
->irq
);
418 /* Write data into FIFO */
419 for (k
= 0; k
< len
; k
++)
420 writel(buf
[k
], (dev
->base
+ DAT_OFFSET
));
422 /* Enable IRQ now that data has been loaded */
423 enable_irq(dev
->irq
);
425 /* Wait for FIFO to empty */
427 time_left
= wait_for_completion_timeout(&dev
->done
, time_left
);
428 fifo_status
= readl(dev
->base
+ FIFO_STATUS_OFFSET
);
429 } while (time_left
&& !(fifo_status
& FIFO_STATUS_TXFIFO_EMPTY_MASK
));
431 /* Mask all interrupts */
432 writel(0, dev
->base
+ IER_OFFSET
);
434 /* Check if there was a NAK */
435 if (readl(dev
->base
+ CS_OFFSET
) & CS_ACK_MASK
) {
436 dev_err(dev
->device
, "unexpected NAK\n");
440 /* Check if a timeout occured */
442 dev_err(dev
->device
, "completion timed out\n");
450 /* Write any amount of data using TX FIFO to the i2c bus */
451 static int bcm_kona_i2c_write_fifo(struct bcm_kona_i2c_dev
*dev
,
454 unsigned int bytes_to_write
= MAX_TX_FIFO_SIZE
;
455 unsigned int bytes_written
= 0;
458 uint8_t *tmp_buf
= msg
->buf
;
460 while (bytes_written
< msg
->len
) {
461 if (msg
->len
- bytes_written
<= MAX_TX_FIFO_SIZE
)
462 bytes_to_write
= msg
->len
- bytes_written
;
464 rc
= bcm_kona_i2c_write_fifo_single(dev
, tmp_buf
,
469 bytes_written
+= bytes_to_write
;
470 tmp_buf
+= bytes_to_write
;
476 /* Send i2c address */
477 static int bcm_kona_i2c_do_addr(struct bcm_kona_i2c_dev
*dev
,
482 if (msg
->flags
& I2C_M_TEN
) {
483 /* First byte is 11110XX0 where XX is upper 2 bits */
484 addr
= 0xF0 | ((msg
->addr
& 0x300) >> 7);
485 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
488 /* Second byte is the remaining 8 bits */
489 addr
= msg
->addr
& 0xFF;
490 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
493 if (msg
->flags
& I2C_M_RD
) {
494 /* For read, send restart command */
495 if (bcm_kona_send_i2c_cmd(dev
, BCM_CMD_RESTART
) < 0)
498 /* Then re-send the first byte with the read bit set */
499 addr
= 0xF0 | ((msg
->addr
& 0x300) >> 7) | 0x01;
500 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
504 addr
= msg
->addr
<< 1;
506 if (msg
->flags
& I2C_M_RD
)
509 if (bcm_kona_i2c_write_byte(dev
, addr
, 0) < 0)
516 static void bcm_kona_i2c_enable_autosense(struct bcm_kona_i2c_dev
*dev
)
518 writel(readl(dev
->base
+ CLKEN_OFFSET
) & ~CLKEN_AUTOSENSE_OFF_MASK
,
519 dev
->base
+ CLKEN_OFFSET
);
522 static void bcm_kona_i2c_config_timing(struct bcm_kona_i2c_dev
*dev
)
524 writel(readl(dev
->base
+ HSTIM_OFFSET
) & ~HSTIM_HS_MODE_MASK
,
525 dev
->base
+ HSTIM_OFFSET
);
527 writel((dev
->std_cfg
->prescale
<< TIM_PRESCALE_SHIFT
) |
528 (dev
->std_cfg
->time_p
<< TIM_P_SHIFT
) |
529 (dev
->std_cfg
->no_div
<< TIM_NO_DIV_SHIFT
) |
530 (dev
->std_cfg
->time_div
<< TIM_DIV_SHIFT
),
531 dev
->base
+ TIM_OFFSET
);
533 writel((dev
->std_cfg
->time_m
<< CLKEN_M_SHIFT
) |
534 (dev
->std_cfg
->time_n
<< CLKEN_N_SHIFT
) |
536 dev
->base
+ CLKEN_OFFSET
);
539 static void bcm_kona_i2c_config_timing_hs(struct bcm_kona_i2c_dev
*dev
)
541 writel((dev
->hs_cfg
->prescale
<< TIM_PRESCALE_SHIFT
) |
542 (dev
->hs_cfg
->time_p
<< TIM_P_SHIFT
) |
543 (dev
->hs_cfg
->no_div
<< TIM_NO_DIV_SHIFT
) |
544 (dev
->hs_cfg
->time_div
<< TIM_DIV_SHIFT
),
545 dev
->base
+ TIM_OFFSET
);
547 writel((dev
->hs_cfg
->hs_hold
<< HSTIM_HS_HOLD_SHIFT
) |
548 (dev
->hs_cfg
->hs_high_phase
<< HSTIM_HS_HIGH_PHASE_SHIFT
) |
549 (dev
->hs_cfg
->hs_setup
<< HSTIM_HS_SETUP_SHIFT
),
550 dev
->base
+ HSTIM_OFFSET
);
552 writel(readl(dev
->base
+ HSTIM_OFFSET
) | HSTIM_HS_MODE_MASK
,
553 dev
->base
+ HSTIM_OFFSET
);
556 static int bcm_kona_i2c_switch_to_hs(struct bcm_kona_i2c_dev
*dev
)
560 /* Send mastercode at standard speed */
561 rc
= bcm_kona_i2c_write_byte(dev
, MASTERCODE
, 1);
563 pr_err("High speed handshake failed\n");
567 /* Configure external clock to higher frequency */
568 rc
= clk_set_rate(dev
->external_clk
, HS_EXT_CLK_FREQ
);
570 dev_err(dev
->device
, "%s: clk_set_rate returned %d\n",
575 /* Reconfigure internal dividers */
576 bcm_kona_i2c_config_timing_hs(dev
);
578 /* Send a restart command */
579 rc
= bcm_kona_send_i2c_cmd(dev
, BCM_CMD_RESTART
);
581 dev_err(dev
->device
, "High speed restart command failed\n");
586 static int bcm_kona_i2c_switch_to_std(struct bcm_kona_i2c_dev
*dev
)
590 /* Reconfigure internal dividers */
591 bcm_kona_i2c_config_timing(dev
);
593 /* Configure external clock to lower frequency */
594 rc
= clk_set_rate(dev
->external_clk
, STD_EXT_CLK_FREQ
);
596 dev_err(dev
->device
, "%s: clk_set_rate returned %d\n",
603 /* Master transfer function */
604 static int bcm_kona_i2c_xfer(struct i2c_adapter
*adapter
,
605 struct i2c_msg msgs
[], int num
)
607 struct bcm_kona_i2c_dev
*dev
= i2c_get_adapdata(adapter
);
608 struct i2c_msg
*pmsg
;
612 rc
= clk_prepare_enable(dev
->external_clk
);
614 dev_err(dev
->device
, "%s: peri clock enable failed. err %d\n",
619 /* Enable pad output */
620 writel(0, dev
->base
+ PADCTL_OFFSET
);
622 /* Enable internal clocks */
623 bcm_kona_i2c_enable_clock(dev
);
625 /* Send start command */
626 rc
= bcm_kona_send_i2c_cmd(dev
, BCM_CMD_START
);
628 dev_err(dev
->device
, "Start command failed rc = %d\n", rc
);
629 goto xfer_disable_pad
;
632 /* Switch to high speed if applicable */
634 rc
= bcm_kona_i2c_switch_to_hs(dev
);
639 /* Loop through all messages */
640 for (i
= 0; i
< num
; i
++) {
643 /* Send restart for subsequent messages */
644 if ((i
!= 0) && ((pmsg
->flags
& I2C_M_NOSTART
) == 0)) {
645 rc
= bcm_kona_send_i2c_cmd(dev
, BCM_CMD_RESTART
);
648 "restart cmd failed rc = %d\n", rc
);
653 /* Send slave address */
654 if (!(pmsg
->flags
& I2C_M_NOSTART
)) {
655 rc
= bcm_kona_i2c_do_addr(dev
, pmsg
);
658 "NAK from addr %2.2x msg#%d rc = %d\n",
664 /* Perform data transfer */
665 if (pmsg
->flags
& I2C_M_RD
) {
666 rc
= bcm_kona_i2c_read_fifo(dev
, pmsg
);
668 dev_err(dev
->device
, "read failure\n");
672 rc
= bcm_kona_i2c_write_fifo(dev
, pmsg
);
674 dev_err(dev
->device
, "write failure");
683 /* Send a STOP command */
684 bcm_kona_send_i2c_cmd(dev
, BCM_CMD_STOP
);
686 /* Return from high speed if applicable */
688 int hs_rc
= bcm_kona_i2c_switch_to_std(dev
);
695 /* Disable pad output */
696 writel(PADCTL_PAD_OUT_EN_MASK
, dev
->base
+ PADCTL_OFFSET
);
698 /* Stop internal clock */
699 bcm_kona_i2c_disable_clock(dev
);
701 clk_disable_unprepare(dev
->external_clk
);
706 static uint32_t bcm_kona_i2c_functionality(struct i2c_adapter
*adap
)
708 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_10BIT_ADDR
|
712 static const struct i2c_algorithm bcm_algo
= {
713 .master_xfer
= bcm_kona_i2c_xfer
,
714 .functionality
= bcm_kona_i2c_functionality
,
717 static int bcm_kona_i2c_assign_bus_speed(struct bcm_kona_i2c_dev
*dev
)
719 unsigned int bus_speed
;
720 int ret
= of_property_read_u32(dev
->device
->of_node
, "clock-frequency",
723 dev_err(dev
->device
, "missing clock-frequency property\n");
729 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_100K
];
732 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_400K
];
735 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_1MHZ
];
738 /* Send mastercode at 100k */
739 dev
->std_cfg
= &std_cfg_table
[BCM_SPD_100K
];
740 dev
->hs_cfg
= &hs_cfg_table
[BCM_SPD_3P4MHZ
];
743 pr_err("%d hz bus speed not supported\n", bus_speed
);
744 pr_err("Valid speeds are 100khz, 400khz, 1mhz, and 3.4mhz\n");
751 static int bcm_kona_i2c_probe(struct platform_device
*pdev
)
754 struct bcm_kona_i2c_dev
*dev
;
755 struct i2c_adapter
*adap
;
756 struct resource
*iomem
;
758 /* Allocate memory for private data structure */
759 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
763 platform_set_drvdata(pdev
, dev
);
764 dev
->device
= &pdev
->dev
;
765 init_completion(&dev
->done
);
767 /* Map hardware registers */
768 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
769 dev
->base
= devm_ioremap_resource(dev
->device
, iomem
);
770 if (IS_ERR(dev
->base
))
773 /* Get and enable external clock */
774 dev
->external_clk
= devm_clk_get(dev
->device
, NULL
);
775 if (IS_ERR(dev
->external_clk
)) {
776 dev_err(dev
->device
, "couldn't get clock\n");
780 rc
= clk_set_rate(dev
->external_clk
, STD_EXT_CLK_FREQ
);
782 dev_err(dev
->device
, "%s: clk_set_rate returned %d\n",
787 rc
= clk_prepare_enable(dev
->external_clk
);
789 dev_err(dev
->device
, "couldn't enable clock\n");
793 /* Parse bus speed */
794 rc
= bcm_kona_i2c_assign_bus_speed(dev
);
796 goto probe_disable_clk
;
798 /* Enable internal clocks */
799 bcm_kona_i2c_enable_clock(dev
);
801 /* Configure internal dividers */
802 bcm_kona_i2c_config_timing(dev
);
804 /* Disable timeout */
805 writel(0, dev
->base
+ TOUT_OFFSET
);
807 /* Enable autosense */
808 bcm_kona_i2c_enable_autosense(dev
);
811 writel(TXFCR_FIFO_FLUSH_MASK
| TXFCR_FIFO_EN_MASK
,
812 dev
->base
+ TXFCR_OFFSET
);
814 /* Mask all interrupts */
815 writel(0, dev
->base
+ IER_OFFSET
);
817 /* Clear all pending interrupts */
818 writel(ISR_CMDBUSY_MASK
|
819 ISR_READ_COMPLETE_MASK
|
822 ISR_TXFIFOEMPTY_MASK
|
824 dev
->base
+ ISR_OFFSET
);
826 /* Get the interrupt number */
827 dev
->irq
= platform_get_irq(pdev
, 0);
829 dev_err(dev
->device
, "no irq resource\n");
831 goto probe_disable_clk
;
834 /* register the ISR handler */
835 rc
= devm_request_irq(&pdev
->dev
, dev
->irq
, bcm_kona_i2c_isr
,
836 IRQF_SHARED
, pdev
->name
, dev
);
838 dev_err(dev
->device
, "failed to request irq %i\n", dev
->irq
);
839 goto probe_disable_clk
;
842 /* Enable the controller but leave it idle */
843 bcm_kona_i2c_send_cmd_to_ctrl(dev
, BCM_CMD_NOACTION
);
845 /* Disable pad output */
846 writel(PADCTL_PAD_OUT_EN_MASK
, dev
->base
+ PADCTL_OFFSET
);
848 /* Disable internal clock */
849 bcm_kona_i2c_disable_clock(dev
);
851 /* Disable external clock */
852 clk_disable_unprepare(dev
->external_clk
);
854 /* Add the i2c adapter */
855 adap
= &dev
->adapter
;
856 i2c_set_adapdata(adap
, dev
);
857 adap
->owner
= THIS_MODULE
;
858 strlcpy(adap
->name
, "Broadcom I2C adapter", sizeof(adap
->name
));
859 adap
->algo
= &bcm_algo
;
860 adap
->dev
.parent
= &pdev
->dev
;
861 adap
->dev
.of_node
= pdev
->dev
.of_node
;
863 rc
= i2c_add_adapter(adap
);
865 dev_err(dev
->device
, "failed to add adapter\n");
869 dev_info(dev
->device
, "device registered successfully\n");
874 bcm_kona_i2c_disable_clock(dev
);
875 clk_disable_unprepare(dev
->external_clk
);
880 static int bcm_kona_i2c_remove(struct platform_device
*pdev
)
882 struct bcm_kona_i2c_dev
*dev
= platform_get_drvdata(pdev
);
884 i2c_del_adapter(&dev
->adapter
);
889 static const struct of_device_id bcm_kona_i2c_of_match
[] = {
890 {.compatible
= "brcm,kona-i2c",},
893 MODULE_DEVICE_TABLE(of
, bcm_kona_i2c_of_match
);
895 static struct platform_driver bcm_kona_i2c_driver
= {
897 .name
= "bcm-kona-i2c",
898 .of_match_table
= bcm_kona_i2c_of_match
,
900 .probe
= bcm_kona_i2c_probe
,
901 .remove
= bcm_kona_i2c_remove
,
903 module_platform_driver(bcm_kona_i2c_driver
);
905 MODULE_AUTHOR("Tim Kryger <tkryger@broadcom.com>");
906 MODULE_DESCRIPTION("Broadcom Kona I2C Driver");
907 MODULE_LICENSE("GPL v2");