Lines Matching +full:i2c +full:- +full:controller +full:- +full:id
2 * I2C bus driver for the Cadence I2C controller.
4 * Copyright (C) 2009 - 2014 Xilinx, Inc.
15 #include <linux/i2c.h>
23 /* Register offsets for the I2C device. */
26 #define CDNS_I2C_ADDR_OFFSET 0x08 /* I2C Address Register, RW */
27 #define CDNS_I2C_DATA_OFFSET 0x0C /* I2C Data Register, RW */
53 * I2C Address Register Bit mask definitions
55 * bits. A write access to this register always initiates a transfer if the I2C
58 #define CDNS_I2C_ADDR_MASK 0x000003FF /* I2C Address Mask */
61 * I2C Interrupt Registers Bit mask definitions
105 #define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
108 #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
110 #define DRIVER_NAME "cdns-i2c"
122 #define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
123 #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
126 * struct cdns_i2c - I2C device private data structure
129 * @membase: Base address of the I2C device
130 * @adap: I2C adapter instance
140 * @input_clk: Input clock to I2C controller
141 * @i2c_clk: Maximum I2C clock speed
176 * cdns_i2c_clear_bus_hold - Clear bus hold bit
177 * @id: Pointer to driver data struct
179 * Helper to clear the controller's bus hold bit.
181 static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id) in cdns_i2c_clear_bus_hold() argument
188 static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround) in cdns_is_holdquirk() argument
191 (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1)); in cdns_is_holdquirk()
195 * cdns_i2c_isr - Interrupt handler for the I2C device
196 * @irq: irq number for the I2C device
200 * the error interrupts of the I2C device.
209 struct cdns_i2c *id = ptr; in cdns_i2c_isr() local
228 if (id->recv_count > id->curr_recv_count) in cdns_i2c_isr()
231 hold_quirk = (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx; in cdns_i2c_isr()
234 if (id->p_recv_buf && in cdns_i2c_isr()
245 if ((id->recv_count < CDNS_I2C_FIFO_DEPTH) && in cdns_i2c_isr()
246 !id->bus_hold_flag) in cdns_i2c_isr()
247 cdns_i2c_clear_bus_hold(id); in cdns_i2c_isr()
249 *(id->p_recv_buf)++ = in cdns_i2c_isr()
251 id->recv_count--; in cdns_i2c_isr()
252 id->curr_recv_count--; in cdns_i2c_isr()
254 if (cdns_is_holdquirk(id, hold_quirk)) in cdns_i2c_isr()
259 * The controller sends NACK to the slave when transfer size in cdns_i2c_isr()
262 * maintain transfer size non-zero while performing a large in cdns_i2c_isr()
265 if (cdns_is_holdquirk(id, hold_quirk)) { in cdns_i2c_isr()
268 (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH)) in cdns_i2c_isr()
275 if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) > in cdns_i2c_isr()
279 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE + in cdns_i2c_isr()
282 cdns_i2c_writereg(id->recv_count - in cdns_i2c_isr()
285 id->curr_recv_count = id->recv_count; in cdns_i2c_isr()
287 } else if (id->recv_count && !hold_quirk && in cdns_i2c_isr()
288 !id->curr_recv_count) { in cdns_i2c_isr()
291 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK, in cdns_i2c_isr()
294 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) { in cdns_i2c_isr()
297 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE; in cdns_i2c_isr()
299 cdns_i2c_writereg(id->recv_count, in cdns_i2c_isr()
301 id->curr_recv_count = id->recv_count; in cdns_i2c_isr()
306 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) { in cdns_i2c_isr()
307 if (!id->bus_hold_flag) in cdns_i2c_isr()
308 cdns_i2c_clear_bus_hold(id); in cdns_i2c_isr()
316 if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) { in cdns_i2c_isr()
321 if (id->send_count) { in cdns_i2c_isr()
322 avail_bytes = CDNS_I2C_FIFO_DEPTH - in cdns_i2c_isr()
324 if (id->send_count > avail_bytes) in cdns_i2c_isr()
327 bytes_to_send = id->send_count; in cdns_i2c_isr()
329 while (bytes_to_send--) { in cdns_i2c_isr()
331 (*(id->p_send_buf)++), in cdns_i2c_isr()
333 id->send_count--; in cdns_i2c_isr()
343 if (!id->send_count && !id->bus_hold_flag) in cdns_i2c_isr()
344 cdns_i2c_clear_bus_hold(id); in cdns_i2c_isr()
350 id->err_status = isr_status & CDNS_I2C_IXR_ERR_INTR_MASK; in cdns_i2c_isr()
351 if (id->err_status) in cdns_i2c_isr()
355 complete(&id->xfer_done); in cdns_i2c_isr()
361 * cdns_i2c_mrecv - Prepare and start a master receive operation
362 * @id: pointer to the i2c device structure
364 static void cdns_i2c_mrecv(struct cdns_i2c *id) in cdns_i2c_mrecv() argument
369 id->p_recv_buf = id->p_msg->buf; in cdns_i2c_mrecv()
370 id->recv_count = id->p_msg->len; in cdns_i2c_mrecv()
372 /* Put the controller in master receive mode and clear the FIFO */ in cdns_i2c_mrecv()
376 if (id->p_msg->flags & I2C_M_RECV_LEN) in cdns_i2c_mrecv()
377 id->recv_count = I2C_SMBUS_BLOCK_MAX + 1; in cdns_i2c_mrecv()
379 id->curr_recv_count = id->recv_count; in cdns_i2c_mrecv()
385 if (id->recv_count > CDNS_I2C_FIFO_DEPTH) in cdns_i2c_mrecv()
400 if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) { in cdns_i2c_mrecv()
403 id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE; in cdns_i2c_mrecv()
405 cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET); in cdns_i2c_mrecv()
408 /* Set the slave address in address register - triggers operation */ in cdns_i2c_mrecv()
409 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK, in cdns_i2c_mrecv()
412 if (!id->bus_hold_flag && in cdns_i2c_mrecv()
413 ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) && in cdns_i2c_mrecv()
414 (id->recv_count <= CDNS_I2C_FIFO_DEPTH)) in cdns_i2c_mrecv()
415 cdns_i2c_clear_bus_hold(id); in cdns_i2c_mrecv()
420 * cdns_i2c_msend - Prepare and start a master send operation
421 * @id: pointer to the i2c device
423 static void cdns_i2c_msend(struct cdns_i2c *id) in cdns_i2c_msend() argument
430 id->p_recv_buf = NULL; in cdns_i2c_msend()
431 id->p_send_buf = id->p_msg->buf; in cdns_i2c_msend()
432 id->send_count = id->p_msg->len; in cdns_i2c_msend()
434 /* Set the controller in Master transmit mode and clear the FIFO. */ in cdns_i2c_msend()
443 if (id->send_count > CDNS_I2C_FIFO_DEPTH) in cdns_i2c_msend()
456 avail_bytes = CDNS_I2C_FIFO_DEPTH - in cdns_i2c_msend()
459 if (id->send_count > avail_bytes) in cdns_i2c_msend()
462 bytes_to_send = id->send_count; in cdns_i2c_msend()
464 while (bytes_to_send--) { in cdns_i2c_msend()
465 cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET); in cdns_i2c_msend()
466 id->send_count--; in cdns_i2c_msend()
473 if (!id->bus_hold_flag && !id->send_count) in cdns_i2c_msend()
474 cdns_i2c_clear_bus_hold(id); in cdns_i2c_msend()
475 /* Set the slave address in address register - triggers operation. */ in cdns_i2c_msend()
476 cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK, in cdns_i2c_msend()
483 * cdns_i2c_master_reset - Reset the interface
484 * @adap: pointer to the i2c adapter driver instance
491 struct cdns_i2c *id = adap->algo_data; in cdns_i2c_master_reset() local
511 static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg, in cdns_i2c_process_msg() argument
517 id->p_msg = msg; in cdns_i2c_process_msg()
518 id->err_status = 0; in cdns_i2c_process_msg()
519 reinit_completion(&id->xfer_done); in cdns_i2c_process_msg()
523 if (msg->flags & I2C_M_TEN) { in cdns_i2c_process_msg()
534 if (msg->flags & I2C_M_RD) in cdns_i2c_process_msg()
535 cdns_i2c_mrecv(id); in cdns_i2c_process_msg()
537 cdns_i2c_msend(id); in cdns_i2c_process_msg()
540 time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout); in cdns_i2c_process_msg()
543 dev_err(id->adap.dev.parent, in cdns_i2c_process_msg()
545 return -ETIMEDOUT; in cdns_i2c_process_msg()
552 if (id->err_status & CDNS_I2C_IXR_ARB_LOST) in cdns_i2c_process_msg()
553 return -EAGAIN; in cdns_i2c_process_msg()
559 * cdns_i2c_master_xfer - The main i2c transfer function
560 * @adap: pointer to the i2c adapter driver instance
561 * @msgs: pointer to the i2c message structure
573 struct cdns_i2c *id = adap->algo_data; in cdns_i2c_master_xfer() local
576 ret = pm_runtime_get_sync(id->dev); in cdns_i2c_master_xfer()
581 ret = -EAGAIN; in cdns_i2c_master_xfer()
585 hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT); in cdns_i2c_master_xfer()
592 * This controller does not give completion interrupt after a in cdns_i2c_master_xfer()
598 for (count = 0; (count < num - 1 && hold_quirk); count++) { in cdns_i2c_master_xfer()
600 dev_warn(adap->dev.parent, in cdns_i2c_master_xfer()
602 ret = -EOPNOTSUPP; in cdns_i2c_master_xfer()
606 id->bus_hold_flag = 1; in cdns_i2c_master_xfer()
611 id->bus_hold_flag = 0; in cdns_i2c_master_xfer()
616 if (count == (num - 1)) in cdns_i2c_master_xfer()
617 id->bus_hold_flag = 0; in cdns_i2c_master_xfer()
619 ret = cdns_i2c_process_msg(id, msgs, adap); in cdns_i2c_master_xfer()
624 if (id->err_status) { in cdns_i2c_master_xfer()
627 if (id->err_status & CDNS_I2C_IXR_NACK) { in cdns_i2c_master_xfer()
628 ret = -ENXIO; in cdns_i2c_master_xfer()
631 ret = -EIO; in cdns_i2c_master_xfer()
638 pm_runtime_mark_last_busy(id->dev); in cdns_i2c_master_xfer()
639 pm_runtime_put_autosuspend(id->dev); in cdns_i2c_master_xfer()
644 * cdns_i2c_func - Returns the supported features of the I2C driver
645 * @adap: pointer to the i2c adapter structure
662 * cdns_i2c_calc_divs - Calculate clock dividers
663 * @f: I2C clock frequency
668 * f is used as input and output variable. As input it is used as target I2C
669 * frequency. On function exit f holds the actually resulting I2C frequency.
688 return -EINVAL; in cdns_i2c_calc_divs()
690 last_error = -1; in cdns_i2c_calc_divs()
696 div_b--; in cdns_i2c_calc_divs()
703 current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) : in cdns_i2c_calc_divs()
704 (fscl - actual_fscl)); in cdns_i2c_calc_divs()
722 * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
723 * @clk_in: I2C clock input frequency in Hz
724 * @id: Pointer to the I2C device structure
737 static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id) in cdns_i2c_setclk() argument
742 unsigned long fscl = id->i2c_clk; in cdns_i2c_setclk()
758 * cdns_i2c_clk_notifier_cb - Clock rate change callback
766 * New dividers are written to the HW in the pre- or post change notification
777 struct cdns_i2c *id = to_cdns_i2c(nb); in cdns_i2c_clk_notifier_cb() local
779 if (pm_runtime_suspended(id->dev)) in cdns_i2c_clk_notifier_cb()
785 unsigned long input_clk = ndata->new_rate; in cdns_i2c_clk_notifier_cb()
786 unsigned long fscl = id->i2c_clk; in cdns_i2c_clk_notifier_cb()
792 dev_warn(id->adap.dev.parent, in cdns_i2c_clk_notifier_cb()
798 if (ndata->new_rate > ndata->old_rate) in cdns_i2c_clk_notifier_cb()
799 cdns_i2c_setclk(ndata->new_rate, id); in cdns_i2c_clk_notifier_cb()
804 id->input_clk = ndata->new_rate; in cdns_i2c_clk_notifier_cb()
806 if (ndata->new_rate < ndata->old_rate) in cdns_i2c_clk_notifier_cb()
807 cdns_i2c_setclk(ndata->new_rate, id); in cdns_i2c_clk_notifier_cb()
811 if (ndata->new_rate > ndata->old_rate) in cdns_i2c_clk_notifier_cb()
812 cdns_i2c_setclk(ndata->old_rate, id); in cdns_i2c_clk_notifier_cb()
820 * cdns_i2c_runtime_suspend - Runtime suspend method for the driver
831 clk_disable(xi2c->clk); in cdns_i2c_runtime_suspend()
837 * cdns_i2c_runtime_resume - Runtime resume
849 ret = clk_enable(xi2c->clk); in cdns_i2c_runtime_resume()
868 { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
869 { .compatible = "cdns,i2c-r1p14",},
875 * cdns_i2c_probe - Platform registration call
878 * This function does all the memory allocation and registration for the i2c
887 struct cdns_i2c *id; in cdns_i2c_probe() local
891 id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL); in cdns_i2c_probe()
892 if (!id) in cdns_i2c_probe()
893 return -ENOMEM; in cdns_i2c_probe()
895 id->dev = &pdev->dev; in cdns_i2c_probe()
896 platform_set_drvdata(pdev, id); in cdns_i2c_probe()
898 match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node); in cdns_i2c_probe()
899 if (match && match->data) { in cdns_i2c_probe()
900 const struct cdns_platform_data *data = match->data; in cdns_i2c_probe()
901 id->quirks = data->quirks; in cdns_i2c_probe()
905 id->membase = devm_ioremap_resource(&pdev->dev, r_mem); in cdns_i2c_probe()
906 if (IS_ERR(id->membase)) in cdns_i2c_probe()
907 return PTR_ERR(id->membase); in cdns_i2c_probe()
909 id->irq = platform_get_irq(pdev, 0); in cdns_i2c_probe()
911 id->adap.owner = THIS_MODULE; in cdns_i2c_probe()
912 id->adap.dev.of_node = pdev->dev.of_node; in cdns_i2c_probe()
913 id->adap.algo = &cdns_i2c_algo; in cdns_i2c_probe()
914 id->adap.timeout = CDNS_I2C_TIMEOUT; in cdns_i2c_probe()
915 id->adap.retries = 3; /* Default retry value. */ in cdns_i2c_probe()
916 id->adap.algo_data = id; in cdns_i2c_probe()
917 id->adap.dev.parent = &pdev->dev; in cdns_i2c_probe()
918 init_completion(&id->xfer_done); in cdns_i2c_probe()
919 snprintf(id->adap.name, sizeof(id->adap.name), in cdns_i2c_probe()
920 "Cadence I2C at %08lx", (unsigned long)r_mem->start); in cdns_i2c_probe()
922 id->clk = devm_clk_get(&pdev->dev, NULL); in cdns_i2c_probe()
923 if (IS_ERR(id->clk)) { in cdns_i2c_probe()
924 dev_err(&pdev->dev, "input clock not found.\n"); in cdns_i2c_probe()
925 return PTR_ERR(id->clk); in cdns_i2c_probe()
927 ret = clk_prepare_enable(id->clk); in cdns_i2c_probe()
929 dev_err(&pdev->dev, "Unable to enable clock.\n"); in cdns_i2c_probe()
931 pm_runtime_enable(id->dev); in cdns_i2c_probe()
932 pm_runtime_set_autosuspend_delay(id->dev, CNDS_I2C_PM_TIMEOUT); in cdns_i2c_probe()
933 pm_runtime_use_autosuspend(id->dev); in cdns_i2c_probe()
934 pm_runtime_set_active(id->dev); in cdns_i2c_probe()
936 id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb; in cdns_i2c_probe()
937 if (clk_notifier_register(id->clk, &id->clk_rate_change_nb)) in cdns_i2c_probe()
938 dev_warn(&pdev->dev, "Unable to register clock notifier.\n"); in cdns_i2c_probe()
939 id->input_clk = clk_get_rate(id->clk); in cdns_i2c_probe()
941 ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", in cdns_i2c_probe()
942 &id->i2c_clk); in cdns_i2c_probe()
943 if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX)) in cdns_i2c_probe()
944 id->i2c_clk = CDNS_I2C_SPEED_DEFAULT; in cdns_i2c_probe()
949 ret = cdns_i2c_setclk(id->input_clk, id); in cdns_i2c_probe()
951 dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk); in cdns_i2c_probe()
952 ret = -EINVAL; in cdns_i2c_probe()
956 ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0, in cdns_i2c_probe()
957 DRIVER_NAME, id); in cdns_i2c_probe()
959 dev_err(&pdev->dev, "cannot get irq %d\n", id->irq); in cdns_i2c_probe()
964 * Cadence I2C controller has a bug wherein it generates in cdns_i2c_probe()
972 ret = i2c_add_adapter(&id->adap); in cdns_i2c_probe()
976 dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n", in cdns_i2c_probe()
977 id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq); in cdns_i2c_probe()
982 clk_disable_unprepare(id->clk); in cdns_i2c_probe()
983 pm_runtime_set_suspended(&pdev->dev); in cdns_i2c_probe()
984 pm_runtime_disable(&pdev->dev); in cdns_i2c_probe()
989 * cdns_i2c_remove - Unregister the device after releasing the resources
998 struct cdns_i2c *id = platform_get_drvdata(pdev); in cdns_i2c_remove() local
1000 i2c_del_adapter(&id->adap); in cdns_i2c_remove()
1001 clk_notifier_unregister(id->clk, &id->clk_rate_change_nb); in cdns_i2c_remove()
1002 clk_disable_unprepare(id->clk); in cdns_i2c_remove()
1003 pm_runtime_disable(&pdev->dev); in cdns_i2c_remove()
1021 MODULE_DESCRIPTION("Cadence I2C bus driver");