.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * This driver implements I2C master functionality using the LSI API2C |
---|
3 | 4 | * controller. |
---|
.. | .. |
---|
5 | 6 | * NOTE: The controller has a limitation in that it can only do transfers of |
---|
6 | 7 | * maximum 255 bytes at a time. If a larger transfer is attempted, error code |
---|
7 | 8 | * (-EINVAL) is returned. |
---|
8 | | - * |
---|
9 | | - * This software is licensed under the terms of the GNU General Public |
---|
10 | | - * License version 2, as published by the Free Software Foundation, and |
---|
11 | | - * may be copied, distributed, and modified under those terms. |
---|
12 | 9 | */ |
---|
13 | 10 | #include <linux/clk.h> |
---|
14 | 11 | #include <linux/clkdev.h> |
---|
| 12 | +#include <linux/delay.h> |
---|
15 | 13 | #include <linux/err.h> |
---|
16 | 14 | #include <linux/i2c.h> |
---|
17 | 15 | #include <linux/init.h> |
---|
.. | .. |
---|
25 | 23 | #define I2C_XFER_TIMEOUT (msecs_to_jiffies(250)) |
---|
26 | 24 | #define I2C_STOP_TIMEOUT (msecs_to_jiffies(100)) |
---|
27 | 25 | #define FIFO_SIZE 8 |
---|
| 26 | +#define SEQ_LEN 2 |
---|
28 | 27 | |
---|
29 | 28 | #define GLOBAL_CONTROL 0x00 |
---|
30 | 29 | #define GLOBAL_MST_EN BIT(0) |
---|
.. | .. |
---|
51 | 50 | #define CMD_BUSY (1<<3) |
---|
52 | 51 | #define CMD_MANUAL (0x00 | CMD_BUSY) |
---|
53 | 52 | #define CMD_AUTO (0x01 | CMD_BUSY) |
---|
| 53 | +#define CMD_SEQUENCE (0x02 | CMD_BUSY) |
---|
54 | 54 | #define MST_RX_XFER 0x2c |
---|
55 | 55 | #define MST_TX_XFER 0x30 |
---|
56 | 56 | #define MST_ADDR_1 0x34 |
---|
.. | .. |
---|
77 | 77 | MST_STATUS_IP) |
---|
78 | 78 | #define MST_TX_BYTES_XFRD 0x50 |
---|
79 | 79 | #define MST_RX_BYTES_XFRD 0x54 |
---|
| 80 | +#define SLV_ADDR_DEC_CTL 0x58 |
---|
| 81 | +#define SLV_ADDR_DEC_GCE BIT(0) /* ACK to General Call Address from own master (loopback) */ |
---|
| 82 | +#define SLV_ADDR_DEC_OGCE BIT(1) /* ACK to General Call Address from external masters */ |
---|
| 83 | +#define SLV_ADDR_DEC_SA1E BIT(2) /* ACK to addr_1 enabled */ |
---|
| 84 | +#define SLV_ADDR_DEC_SA1M BIT(3) /* 10-bit addressing for addr_1 enabled */ |
---|
| 85 | +#define SLV_ADDR_DEC_SA2E BIT(4) /* ACK to addr_2 enabled */ |
---|
| 86 | +#define SLV_ADDR_DEC_SA2M BIT(5) /* 10-bit addressing for addr_2 enabled */ |
---|
| 87 | +#define SLV_ADDR_1 0x5c |
---|
| 88 | +#define SLV_ADDR_2 0x60 |
---|
| 89 | +#define SLV_RX_CTL 0x64 |
---|
| 90 | +#define SLV_RX_ACSA1 BIT(0) /* Generate ACK for writes to addr_1 */ |
---|
| 91 | +#define SLV_RX_ACSA2 BIT(1) /* Generate ACK for writes to addr_2 */ |
---|
| 92 | +#define SLV_RX_ACGCA BIT(2) /* ACK data phase transfers to General Call Address */ |
---|
| 93 | +#define SLV_DATA 0x68 |
---|
| 94 | +#define SLV_RX_FIFO 0x6c |
---|
| 95 | +#define SLV_FIFO_DV1 BIT(0) /* Data Valid for addr_1 */ |
---|
| 96 | +#define SLV_FIFO_DV2 BIT(1) /* Data Valid for addr_2 */ |
---|
| 97 | +#define SLV_FIFO_AS BIT(2) /* (N)ACK Sent */ |
---|
| 98 | +#define SLV_FIFO_TNAK BIT(3) /* Timeout NACK */ |
---|
| 99 | +#define SLV_FIFO_STRC BIT(4) /* First byte after start condition received */ |
---|
| 100 | +#define SLV_FIFO_RSC BIT(5) /* Repeated Start Condition */ |
---|
| 101 | +#define SLV_FIFO_STPC BIT(6) /* Stop Condition */ |
---|
| 102 | +#define SLV_FIFO_DV (SLV_FIFO_DV1 | SLV_FIFO_DV2) |
---|
| 103 | +#define SLV_INT_ENABLE 0x70 |
---|
| 104 | +#define SLV_INT_STATUS 0x74 |
---|
| 105 | +#define SLV_STATUS_RFH BIT(0) /* FIFO service */ |
---|
| 106 | +#define SLV_STATUS_WTC BIT(1) /* Write transfer complete */ |
---|
| 107 | +#define SLV_STATUS_SRS1 BIT(2) /* Slave read from addr 1 */ |
---|
| 108 | +#define SLV_STATUS_SRRS1 BIT(3) /* Repeated start from addr 1 */ |
---|
| 109 | +#define SLV_STATUS_SRND1 BIT(4) /* Read request not following start condition */ |
---|
| 110 | +#define SLV_STATUS_SRC1 BIT(5) /* Read canceled */ |
---|
| 111 | +#define SLV_STATUS_SRAT1 BIT(6) /* Slave Read timed out */ |
---|
| 112 | +#define SLV_STATUS_SRDRE1 BIT(7) /* Data written after timed out */ |
---|
| 113 | +#define SLV_READ_DUMMY 0x78 |
---|
80 | 114 | #define SCL_HIGH_PERIOD 0x80 |
---|
81 | 115 | #define SCL_LOW_PERIOD 0x84 |
---|
82 | 116 | #define SPIKE_FLTR_LEN 0x88 |
---|
.. | .. |
---|
87 | 121 | * axxia_i2c_dev - I2C device context |
---|
88 | 122 | * @base: pointer to register struct |
---|
89 | 123 | * @msg: pointer to current message |
---|
90 | | - * @msg_xfrd: number of bytes transferred in msg |
---|
| 124 | + * @msg_r: pointer to current read message (sequence transfer) |
---|
| 125 | + * @msg_xfrd: number of bytes transferred in tx_fifo |
---|
| 126 | + * @msg_xfrd_r: number of bytes transferred in rx_fifo |
---|
91 | 127 | * @msg_err: error code for completed message |
---|
92 | 128 | * @msg_complete: xfer completion object |
---|
93 | 129 | * @dev: device reference |
---|
94 | 130 | * @adapter: core i2c abstraction |
---|
95 | 131 | * @i2c_clk: clock reference for i2c input clock |
---|
96 | 132 | * @bus_clk_rate: current i2c bus clock rate |
---|
| 133 | + * @last: a flag indicating is this is last message in transfer |
---|
97 | 134 | */ |
---|
98 | 135 | struct axxia_i2c_dev { |
---|
99 | 136 | void __iomem *base; |
---|
100 | 137 | struct i2c_msg *msg; |
---|
| 138 | + struct i2c_msg *msg_r; |
---|
101 | 139 | size_t msg_xfrd; |
---|
| 140 | + size_t msg_xfrd_r; |
---|
102 | 141 | int msg_err; |
---|
103 | 142 | struct completion msg_complete; |
---|
104 | 143 | struct device *dev; |
---|
105 | 144 | struct i2c_adapter adapter; |
---|
106 | 145 | struct clk *i2c_clk; |
---|
107 | 146 | u32 bus_clk_rate; |
---|
| 147 | + bool last; |
---|
| 148 | + struct i2c_client *slave; |
---|
| 149 | + int irq; |
---|
108 | 150 | }; |
---|
109 | 151 | |
---|
110 | 152 | static void i2c_int_disable(struct axxia_i2c_dev *idev, u32 mask) |
---|
.. | .. |
---|
157 | 199 | /* Enable Master Mode */ |
---|
158 | 200 | writel(0x1, idev->base + GLOBAL_CONTROL); |
---|
159 | 201 | |
---|
160 | | - if (idev->bus_clk_rate <= 100000) { |
---|
| 202 | + if (idev->bus_clk_rate <= I2C_MAX_STANDARD_MODE_FREQ) { |
---|
161 | 203 | /* Standard mode SCL 50/50, tSU:DAT = 250 ns */ |
---|
162 | 204 | t_high = divisor * 1 / 2; |
---|
163 | 205 | t_low = divisor * 1 / 2; |
---|
.. | .. |
---|
227 | 269 | */ |
---|
228 | 270 | static int axxia_i2c_empty_rx_fifo(struct axxia_i2c_dev *idev) |
---|
229 | 271 | { |
---|
230 | | - struct i2c_msg *msg = idev->msg; |
---|
| 272 | + struct i2c_msg *msg = idev->msg_r; |
---|
231 | 273 | size_t rx_fifo_avail = readl(idev->base + MST_RX_FIFO); |
---|
232 | | - int bytes_to_transfer = min(rx_fifo_avail, msg->len - idev->msg_xfrd); |
---|
| 274 | + int bytes_to_transfer = min(rx_fifo_avail, msg->len - idev->msg_xfrd_r); |
---|
233 | 275 | |
---|
234 | 276 | while (bytes_to_transfer-- > 0) { |
---|
235 | 277 | int c = readl(idev->base + MST_DATA); |
---|
236 | 278 | |
---|
237 | | - if (idev->msg_xfrd == 0 && i2c_m_recv_len(msg)) { |
---|
| 279 | + if (idev->msg_xfrd_r == 0 && i2c_m_recv_len(msg)) { |
---|
238 | 280 | /* |
---|
239 | 281 | * Check length byte for SMBus block read |
---|
240 | 282 | */ |
---|
.. | .. |
---|
247 | 289 | msg->len = 1 + c; |
---|
248 | 290 | writel(msg->len, idev->base + MST_RX_XFER); |
---|
249 | 291 | } |
---|
250 | | - msg->buf[idev->msg_xfrd++] = c; |
---|
| 292 | + msg->buf[idev->msg_xfrd_r++] = c; |
---|
251 | 293 | } |
---|
252 | 294 | |
---|
253 | 295 | return 0; |
---|
.. | .. |
---|
270 | 312 | return ret; |
---|
271 | 313 | } |
---|
272 | 314 | |
---|
| 315 | +static void axxia_i2c_slv_fifo_event(struct axxia_i2c_dev *idev) |
---|
| 316 | +{ |
---|
| 317 | + u32 fifo_status = readl(idev->base + SLV_RX_FIFO); |
---|
| 318 | + u8 val; |
---|
| 319 | + |
---|
| 320 | + dev_dbg(idev->dev, "slave irq fifo_status=0x%x\n", fifo_status); |
---|
| 321 | + |
---|
| 322 | + if (fifo_status & SLV_FIFO_DV1) { |
---|
| 323 | + if (fifo_status & SLV_FIFO_STRC) |
---|
| 324 | + i2c_slave_event(idev->slave, |
---|
| 325 | + I2C_SLAVE_WRITE_REQUESTED, &val); |
---|
| 326 | + |
---|
| 327 | + val = readl(idev->base + SLV_DATA); |
---|
| 328 | + i2c_slave_event(idev->slave, I2C_SLAVE_WRITE_RECEIVED, &val); |
---|
| 329 | + } |
---|
| 330 | + if (fifo_status & SLV_FIFO_STPC) { |
---|
| 331 | + readl(idev->base + SLV_DATA); /* dummy read */ |
---|
| 332 | + i2c_slave_event(idev->slave, I2C_SLAVE_STOP, &val); |
---|
| 333 | + } |
---|
| 334 | + if (fifo_status & SLV_FIFO_RSC) |
---|
| 335 | + readl(idev->base + SLV_DATA); /* dummy read */ |
---|
| 336 | +} |
---|
| 337 | + |
---|
| 338 | +static irqreturn_t axxia_i2c_slv_isr(struct axxia_i2c_dev *idev) |
---|
| 339 | +{ |
---|
| 340 | + u32 status = readl(idev->base + SLV_INT_STATUS); |
---|
| 341 | + u8 val; |
---|
| 342 | + |
---|
| 343 | + dev_dbg(idev->dev, "slave irq status=0x%x\n", status); |
---|
| 344 | + |
---|
| 345 | + if (status & SLV_STATUS_RFH) |
---|
| 346 | + axxia_i2c_slv_fifo_event(idev); |
---|
| 347 | + if (status & SLV_STATUS_SRS1) { |
---|
| 348 | + i2c_slave_event(idev->slave, I2C_SLAVE_READ_REQUESTED, &val); |
---|
| 349 | + writel(val, idev->base + SLV_DATA); |
---|
| 350 | + } |
---|
| 351 | + if (status & SLV_STATUS_SRND1) { |
---|
| 352 | + i2c_slave_event(idev->slave, I2C_SLAVE_READ_PROCESSED, &val); |
---|
| 353 | + writel(val, idev->base + SLV_DATA); |
---|
| 354 | + } |
---|
| 355 | + if (status & SLV_STATUS_SRC1) |
---|
| 356 | + i2c_slave_event(idev->slave, I2C_SLAVE_STOP, &val); |
---|
| 357 | + |
---|
| 358 | + writel(INT_SLV, idev->base + INTERRUPT_STATUS); |
---|
| 359 | + return IRQ_HANDLED; |
---|
| 360 | +} |
---|
| 361 | + |
---|
273 | 362 | static irqreturn_t axxia_i2c_isr(int irq, void *_dev) |
---|
274 | 363 | { |
---|
275 | 364 | struct axxia_i2c_dev *idev = _dev; |
---|
| 365 | + irqreturn_t ret = IRQ_NONE; |
---|
276 | 366 | u32 status; |
---|
277 | 367 | |
---|
278 | | - if (!(readl(idev->base + INTERRUPT_STATUS) & INT_MST)) |
---|
279 | | - return IRQ_NONE; |
---|
| 368 | + status = readl(idev->base + INTERRUPT_STATUS); |
---|
| 369 | + |
---|
| 370 | + if (status & INT_SLV) |
---|
| 371 | + ret = axxia_i2c_slv_isr(idev); |
---|
| 372 | + if (!(status & INT_MST)) |
---|
| 373 | + return ret; |
---|
280 | 374 | |
---|
281 | 375 | /* Read interrupt status bits */ |
---|
282 | 376 | status = readl(idev->base + MST_INT_STATUS); |
---|
.. | .. |
---|
287 | 381 | } |
---|
288 | 382 | |
---|
289 | 383 | /* RX FIFO needs service? */ |
---|
290 | | - if (i2c_m_rd(idev->msg) && (status & MST_STATUS_RFL)) |
---|
| 384 | + if (i2c_m_rd(idev->msg_r) && (status & MST_STATUS_RFL)) |
---|
291 | 385 | axxia_i2c_empty_rx_fifo(idev); |
---|
292 | 386 | |
---|
293 | 387 | /* TX FIFO needs service? */ |
---|
.. | .. |
---|
317 | 411 | /* Stop completed */ |
---|
318 | 412 | i2c_int_disable(idev, ~MST_STATUS_TSS); |
---|
319 | 413 | complete(&idev->msg_complete); |
---|
320 | | - } else if (status & MST_STATUS_SNS) { |
---|
| 414 | + } else if (status & (MST_STATUS_SNS | MST_STATUS_SS)) { |
---|
321 | 415 | /* Transfer done */ |
---|
322 | | - i2c_int_disable(idev, ~MST_STATUS_TSS); |
---|
323 | | - if (i2c_m_rd(idev->msg) && idev->msg_xfrd < idev->msg->len) |
---|
| 416 | + int mask = idev->last ? ~0 : ~MST_STATUS_TSS; |
---|
| 417 | + |
---|
| 418 | + i2c_int_disable(idev, mask); |
---|
| 419 | + if (i2c_m_rd(idev->msg_r) && idev->msg_xfrd_r < idev->msg_r->len) |
---|
324 | 420 | axxia_i2c_empty_rx_fifo(idev); |
---|
325 | 421 | complete(&idev->msg_complete); |
---|
326 | 422 | } else if (status & MST_STATUS_TSS) { |
---|
.. | .. |
---|
337 | 433 | return IRQ_HANDLED; |
---|
338 | 434 | } |
---|
339 | 435 | |
---|
340 | | -static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg) |
---|
| 436 | +static void axxia_i2c_set_addr(struct axxia_i2c_dev *idev, struct i2c_msg *msg) |
---|
341 | 437 | { |
---|
342 | | - u32 int_mask = MST_STATUS_ERR | MST_STATUS_SNS; |
---|
343 | | - u32 rx_xfer, tx_xfer; |
---|
344 | 438 | u32 addr_1, addr_2; |
---|
345 | | - unsigned long time_left; |
---|
346 | | - unsigned int wt_value; |
---|
347 | | - |
---|
348 | | - idev->msg = msg; |
---|
349 | | - idev->msg_xfrd = 0; |
---|
350 | | - reinit_completion(&idev->msg_complete); |
---|
351 | 439 | |
---|
352 | 440 | if (i2c_m_ten(msg)) { |
---|
353 | 441 | /* 10-bit address |
---|
.. | .. |
---|
367 | 455 | addr_2 = 0; |
---|
368 | 456 | } |
---|
369 | 457 | |
---|
| 458 | + writel(addr_1, idev->base + MST_ADDR_1); |
---|
| 459 | + writel(addr_2, idev->base + MST_ADDR_2); |
---|
| 460 | +} |
---|
| 461 | + |
---|
| 462 | +/* The NAK interrupt will be sent _before_ issuing STOP command |
---|
| 463 | + * so the controller might still be busy processing it. No |
---|
| 464 | + * interrupt will be sent at the end so we have to poll for it |
---|
| 465 | + */ |
---|
| 466 | +static int axxia_i2c_handle_seq_nak(struct axxia_i2c_dev *idev) |
---|
| 467 | +{ |
---|
| 468 | + unsigned long timeout = jiffies + I2C_XFER_TIMEOUT; |
---|
| 469 | + |
---|
| 470 | + do { |
---|
| 471 | + if ((readl(idev->base + MST_COMMAND) & CMD_BUSY) == 0) |
---|
| 472 | + return 0; |
---|
| 473 | + usleep_range(1, 100); |
---|
| 474 | + } while (time_before(jiffies, timeout)); |
---|
| 475 | + |
---|
| 476 | + return -ETIMEDOUT; |
---|
| 477 | +} |
---|
| 478 | + |
---|
| 479 | +static int axxia_i2c_xfer_seq(struct axxia_i2c_dev *idev, struct i2c_msg msgs[]) |
---|
| 480 | +{ |
---|
| 481 | + u32 int_mask = MST_STATUS_ERR | MST_STATUS_SS | MST_STATUS_RFL; |
---|
| 482 | + u32 rlen = i2c_m_recv_len(&msgs[1]) ? I2C_SMBUS_BLOCK_MAX : msgs[1].len; |
---|
| 483 | + unsigned long time_left; |
---|
| 484 | + |
---|
| 485 | + axxia_i2c_set_addr(idev, &msgs[0]); |
---|
| 486 | + |
---|
| 487 | + writel(msgs[0].len, idev->base + MST_TX_XFER); |
---|
| 488 | + writel(rlen, idev->base + MST_RX_XFER); |
---|
| 489 | + |
---|
| 490 | + idev->msg = &msgs[0]; |
---|
| 491 | + idev->msg_r = &msgs[1]; |
---|
| 492 | + idev->msg_xfrd = 0; |
---|
| 493 | + idev->msg_xfrd_r = 0; |
---|
| 494 | + idev->last = true; |
---|
| 495 | + axxia_i2c_fill_tx_fifo(idev); |
---|
| 496 | + |
---|
| 497 | + writel(CMD_SEQUENCE, idev->base + MST_COMMAND); |
---|
| 498 | + |
---|
| 499 | + reinit_completion(&idev->msg_complete); |
---|
| 500 | + i2c_int_enable(idev, int_mask); |
---|
| 501 | + |
---|
| 502 | + time_left = wait_for_completion_timeout(&idev->msg_complete, |
---|
| 503 | + I2C_XFER_TIMEOUT); |
---|
| 504 | + |
---|
| 505 | + if (idev->msg_err == -ENXIO) { |
---|
| 506 | + if (axxia_i2c_handle_seq_nak(idev)) |
---|
| 507 | + axxia_i2c_init(idev); |
---|
| 508 | + } else if (readl(idev->base + MST_COMMAND) & CMD_BUSY) { |
---|
| 509 | + dev_warn(idev->dev, "busy after xfer\n"); |
---|
| 510 | + } |
---|
| 511 | + |
---|
| 512 | + if (time_left == 0) { |
---|
| 513 | + idev->msg_err = -ETIMEDOUT; |
---|
| 514 | + i2c_recover_bus(&idev->adapter); |
---|
| 515 | + axxia_i2c_init(idev); |
---|
| 516 | + } |
---|
| 517 | + |
---|
| 518 | + if (unlikely(idev->msg_err) && idev->msg_err != -ENXIO) |
---|
| 519 | + axxia_i2c_init(idev); |
---|
| 520 | + |
---|
| 521 | + return idev->msg_err; |
---|
| 522 | +} |
---|
| 523 | + |
---|
| 524 | +static int axxia_i2c_xfer_msg(struct axxia_i2c_dev *idev, struct i2c_msg *msg, |
---|
| 525 | + bool last) |
---|
| 526 | +{ |
---|
| 527 | + u32 int_mask = MST_STATUS_ERR; |
---|
| 528 | + u32 rx_xfer, tx_xfer; |
---|
| 529 | + unsigned long time_left; |
---|
| 530 | + unsigned int wt_value; |
---|
| 531 | + |
---|
| 532 | + idev->msg = msg; |
---|
| 533 | + idev->msg_r = msg; |
---|
| 534 | + idev->msg_xfrd = 0; |
---|
| 535 | + idev->msg_xfrd_r = 0; |
---|
| 536 | + idev->last = last; |
---|
| 537 | + reinit_completion(&idev->msg_complete); |
---|
| 538 | + |
---|
| 539 | + axxia_i2c_set_addr(idev, msg); |
---|
| 540 | + |
---|
370 | 541 | if (i2c_m_rd(msg)) { |
---|
371 | 542 | /* I2C read transfer */ |
---|
372 | 543 | rx_xfer = i2c_m_recv_len(msg) ? I2C_SMBUS_BLOCK_MAX : msg->len; |
---|
.. | .. |
---|
379 | 550 | |
---|
380 | 551 | writel(rx_xfer, idev->base + MST_RX_XFER); |
---|
381 | 552 | writel(tx_xfer, idev->base + MST_TX_XFER); |
---|
382 | | - writel(addr_1, idev->base + MST_ADDR_1); |
---|
383 | | - writel(addr_2, idev->base + MST_ADDR_2); |
---|
384 | 553 | |
---|
385 | 554 | if (i2c_m_rd(msg)) |
---|
386 | 555 | int_mask |= MST_STATUS_RFL; |
---|
.. | .. |
---|
394 | 563 | if (idev->msg_err) |
---|
395 | 564 | goto out; |
---|
396 | 565 | |
---|
397 | | - /* Start manual mode */ |
---|
398 | | - writel(CMD_MANUAL, idev->base + MST_COMMAND); |
---|
| 566 | + if (!last) { |
---|
| 567 | + writel(CMD_MANUAL, idev->base + MST_COMMAND); |
---|
| 568 | + int_mask |= MST_STATUS_SNS; |
---|
| 569 | + } else { |
---|
| 570 | + writel(CMD_AUTO, idev->base + MST_COMMAND); |
---|
| 571 | + int_mask |= MST_STATUS_SS; |
---|
| 572 | + } |
---|
399 | 573 | |
---|
400 | 574 | writel(WT_EN | wt_value, idev->base + WAIT_TIMER_CONTROL); |
---|
401 | 575 | |
---|
.. | .. |
---|
423 | 597 | return idev->msg_err; |
---|
424 | 598 | } |
---|
425 | 599 | |
---|
426 | | -static int axxia_i2c_stop(struct axxia_i2c_dev *idev) |
---|
| 600 | +/* This function checks if the msgs[] array contains messages compatible with |
---|
| 601 | + * Sequence mode of operation. This mode assumes there will be exactly one |
---|
| 602 | + * write of non-zero length followed by exactly one read of non-zero length, |
---|
| 603 | + * both targeted at the same client device. |
---|
| 604 | + */ |
---|
| 605 | +static bool axxia_i2c_sequence_ok(struct i2c_msg msgs[], int num) |
---|
427 | 606 | { |
---|
428 | | - u32 int_mask = MST_STATUS_ERR | MST_STATUS_SCC | MST_STATUS_TSS; |
---|
429 | | - unsigned long time_left; |
---|
430 | | - |
---|
431 | | - reinit_completion(&idev->msg_complete); |
---|
432 | | - |
---|
433 | | - /* Issue stop */ |
---|
434 | | - writel(0xb, idev->base + MST_COMMAND); |
---|
435 | | - i2c_int_enable(idev, int_mask); |
---|
436 | | - time_left = wait_for_completion_timeout(&idev->msg_complete, |
---|
437 | | - I2C_STOP_TIMEOUT); |
---|
438 | | - i2c_int_disable(idev, int_mask); |
---|
439 | | - if (time_left == 0) |
---|
440 | | - return -ETIMEDOUT; |
---|
441 | | - |
---|
442 | | - if (readl(idev->base + MST_COMMAND) & CMD_BUSY) |
---|
443 | | - dev_warn(idev->dev, "busy after stop\n"); |
---|
444 | | - |
---|
445 | | - return 0; |
---|
| 607 | + return num == SEQ_LEN && !i2c_m_rd(&msgs[0]) && i2c_m_rd(&msgs[1]) && |
---|
| 608 | + msgs[0].len > 0 && msgs[0].len <= FIFO_SIZE && |
---|
| 609 | + msgs[1].len > 0 && msgs[0].addr == msgs[1].addr; |
---|
446 | 610 | } |
---|
447 | 611 | |
---|
448 | 612 | static int |
---|
.. | .. |
---|
453 | 617 | int ret = 0; |
---|
454 | 618 | |
---|
455 | 619 | idev->msg_err = 0; |
---|
| 620 | + |
---|
| 621 | + if (axxia_i2c_sequence_ok(msgs, num)) { |
---|
| 622 | + ret = axxia_i2c_xfer_seq(idev, msgs); |
---|
| 623 | + return ret ? : SEQ_LEN; |
---|
| 624 | + } |
---|
| 625 | + |
---|
456 | 626 | i2c_int_enable(idev, MST_STATUS_TSS); |
---|
457 | 627 | |
---|
458 | 628 | for (i = 0; ret == 0 && i < num; ++i) |
---|
459 | | - ret = axxia_i2c_xfer_msg(idev, &msgs[i]); |
---|
460 | | - |
---|
461 | | - axxia_i2c_stop(idev); |
---|
| 629 | + ret = axxia_i2c_xfer_msg(idev, &msgs[i], i == (num - 1)); |
---|
462 | 630 | |
---|
463 | 631 | return ret ? : i; |
---|
464 | 632 | } |
---|
.. | .. |
---|
503 | 671 | return caps; |
---|
504 | 672 | } |
---|
505 | 673 | |
---|
| 674 | +static int axxia_i2c_reg_slave(struct i2c_client *slave) |
---|
| 675 | +{ |
---|
| 676 | + struct axxia_i2c_dev *idev = i2c_get_adapdata(slave->adapter); |
---|
| 677 | + u32 slv_int_mask = SLV_STATUS_RFH; |
---|
| 678 | + u32 dec_ctl; |
---|
| 679 | + |
---|
| 680 | + if (idev->slave) |
---|
| 681 | + return -EBUSY; |
---|
| 682 | + |
---|
| 683 | + idev->slave = slave; |
---|
| 684 | + |
---|
| 685 | + /* Enable slave mode as well */ |
---|
| 686 | + writel(GLOBAL_MST_EN | GLOBAL_SLV_EN, idev->base + GLOBAL_CONTROL); |
---|
| 687 | + writel(INT_MST | INT_SLV, idev->base + INTERRUPT_ENABLE); |
---|
| 688 | + |
---|
| 689 | + /* Set slave address */ |
---|
| 690 | + dec_ctl = SLV_ADDR_DEC_SA1E; |
---|
| 691 | + if (slave->flags & I2C_CLIENT_TEN) |
---|
| 692 | + dec_ctl |= SLV_ADDR_DEC_SA1M; |
---|
| 693 | + |
---|
| 694 | + writel(SLV_RX_ACSA1, idev->base + SLV_RX_CTL); |
---|
| 695 | + writel(dec_ctl, idev->base + SLV_ADDR_DEC_CTL); |
---|
| 696 | + writel(slave->addr, idev->base + SLV_ADDR_1); |
---|
| 697 | + |
---|
| 698 | + /* Enable interrupts */ |
---|
| 699 | + slv_int_mask |= SLV_STATUS_SRS1 | SLV_STATUS_SRRS1 | SLV_STATUS_SRND1; |
---|
| 700 | + slv_int_mask |= SLV_STATUS_SRC1; |
---|
| 701 | + writel(slv_int_mask, idev->base + SLV_INT_ENABLE); |
---|
| 702 | + |
---|
| 703 | + return 0; |
---|
| 704 | +} |
---|
| 705 | + |
---|
| 706 | +static int axxia_i2c_unreg_slave(struct i2c_client *slave) |
---|
| 707 | +{ |
---|
| 708 | + struct axxia_i2c_dev *idev = i2c_get_adapdata(slave->adapter); |
---|
| 709 | + |
---|
| 710 | + /* Disable slave mode */ |
---|
| 711 | + writel(GLOBAL_MST_EN, idev->base + GLOBAL_CONTROL); |
---|
| 712 | + writel(INT_MST, idev->base + INTERRUPT_ENABLE); |
---|
| 713 | + |
---|
| 714 | + synchronize_irq(idev->irq); |
---|
| 715 | + |
---|
| 716 | + idev->slave = NULL; |
---|
| 717 | + |
---|
| 718 | + return 0; |
---|
| 719 | +} |
---|
| 720 | + |
---|
506 | 721 | static const struct i2c_algorithm axxia_i2c_algo = { |
---|
507 | 722 | .master_xfer = axxia_i2c_xfer, |
---|
508 | 723 | .functionality = axxia_i2c_func, |
---|
| 724 | + .reg_slave = axxia_i2c_reg_slave, |
---|
| 725 | + .unreg_slave = axxia_i2c_unreg_slave, |
---|
509 | 726 | }; |
---|
510 | 727 | |
---|
511 | 728 | static const struct i2c_adapter_quirks axxia_i2c_quirks = { |
---|
.. | .. |
---|
517 | 734 | { |
---|
518 | 735 | struct device_node *np = pdev->dev.of_node; |
---|
519 | 736 | struct axxia_i2c_dev *idev = NULL; |
---|
520 | | - struct resource *res; |
---|
521 | 737 | void __iomem *base; |
---|
522 | | - int irq; |
---|
523 | 738 | int ret = 0; |
---|
524 | 739 | |
---|
525 | 740 | idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); |
---|
526 | 741 | if (!idev) |
---|
527 | 742 | return -ENOMEM; |
---|
528 | 743 | |
---|
529 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
---|
530 | | - base = devm_ioremap_resource(&pdev->dev, res); |
---|
| 744 | + base = devm_platform_ioremap_resource(pdev, 0); |
---|
531 | 745 | if (IS_ERR(base)) |
---|
532 | 746 | return PTR_ERR(base); |
---|
533 | 747 | |
---|
534 | | - irq = platform_get_irq(pdev, 0); |
---|
535 | | - if (irq < 0) { |
---|
536 | | - dev_err(&pdev->dev, "missing interrupt resource\n"); |
---|
537 | | - return irq; |
---|
538 | | - } |
---|
| 748 | + idev->irq = platform_get_irq(pdev, 0); |
---|
| 749 | + if (idev->irq < 0) |
---|
| 750 | + return idev->irq; |
---|
539 | 751 | |
---|
540 | 752 | idev->i2c_clk = devm_clk_get(&pdev->dev, "i2c"); |
---|
541 | 753 | if (IS_ERR(idev->i2c_clk)) { |
---|
.. | .. |
---|
549 | 761 | |
---|
550 | 762 | of_property_read_u32(np, "clock-frequency", &idev->bus_clk_rate); |
---|
551 | 763 | if (idev->bus_clk_rate == 0) |
---|
552 | | - idev->bus_clk_rate = 100000; /* default clock rate */ |
---|
| 764 | + idev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */ |
---|
553 | 765 | |
---|
554 | 766 | ret = clk_prepare_enable(idev->i2c_clk); |
---|
555 | 767 | if (ret) { |
---|
.. | .. |
---|
563 | 775 | goto error_disable_clk; |
---|
564 | 776 | } |
---|
565 | 777 | |
---|
566 | | - ret = devm_request_irq(&pdev->dev, irq, axxia_i2c_isr, 0, |
---|
| 778 | + ret = devm_request_irq(&pdev->dev, idev->irq, axxia_i2c_isr, 0, |
---|
567 | 779 | pdev->name, idev); |
---|
568 | 780 | if (ret) { |
---|
569 | | - dev_err(&pdev->dev, "failed to claim IRQ%d\n", irq); |
---|
| 781 | + dev_err(&pdev->dev, "failed to claim IRQ%d\n", idev->irq); |
---|
570 | 782 | goto error_disable_clk; |
---|
571 | 783 | } |
---|
572 | 784 | |
---|