Lines Matching +full:i2c +full:- +full:arb

1 // SPDX-License-Identifier: GPL-2.0-only
5 * Based on the i2c-axxia.c driver.
10 #include <linux/i2c.h>
30 #define ALTR_I2C_ISER_ARB_EN BIT(3) /* Enable ARB LOST IRQ */
36 #define ALTR_I2C_ISR_ARB BIT(3) /* ARB LOST IRQ */
58 * struct altr_i2c_dev - I2C device context
65 * @adapter: core i2c abstraction
66 * @i2c_clk: clock reference for i2c input clock
67 * @bus_clk_rate: current i2c bus clock rate
96 int_en = readl(idev->base + ALTR_I2C_ISER); in altr_i2c_int_enable()
98 idev->isr_mask = int_en | mask; in altr_i2c_int_enable()
100 idev->isr_mask = int_en & ~mask; in altr_i2c_int_enable()
102 writel(idev->isr_mask, idev->base + ALTR_I2C_ISER); in altr_i2c_int_enable()
107 u32 int_en = readl(idev->base + ALTR_I2C_ISR); in altr_i2c_int_clear()
109 writel(int_en | mask, idev->base + ALTR_I2C_ISR); in altr_i2c_int_clear()
114 u32 tmp = readl(idev->base + ALTR_I2C_CTRL); in altr_i2c_core_disable()
116 writel(tmp & ~ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL); in altr_i2c_core_disable()
121 u32 tmp = readl(idev->base + ALTR_I2C_CTRL); in altr_i2c_core_enable()
123 writel(tmp | ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL); in altr_i2c_core_enable()
134 writel(ALTR_I2C_TFR_CMD_STO, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_stop()
139 u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate; in altr_i2c_init()
140 u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000; in altr_i2c_init()
145 if (idev->bus_clk_rate <= I2C_MAX_STANDARD_MODE_FREQ) { in altr_i2c_init()
156 writel(tmp, idev->base + ALTR_I2C_CTRL); in altr_i2c_init()
158 dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n", in altr_i2c_init()
159 idev->bus_clk_rate, clk_mhz, divisor); in altr_i2c_init()
165 writel(t_high, idev->base + ALTR_I2C_SCL_HIGH); in altr_i2c_init()
167 writel(t_low, idev->base + ALTR_I2C_SCL_LOW); in altr_i2c_init()
169 writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD); in altr_i2c_init()
176 * altr_i2c_transfer - On the last byte to be transmitted, send
182 if (idev->msg_len == 1) in altr_i2c_transfer()
184 if (idev->msg_len > 0) in altr_i2c_transfer()
185 writel(data, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_transfer()
189 * altr_i2c_empty_rx_fifo - Fetch data from RX FIFO until end of
194 size_t rx_fifo_avail = readl(idev->base + ALTR_I2C_RX_FIFO_LVL); in altr_i2c_empty_rx_fifo()
195 int bytes_to_transfer = min(rx_fifo_avail, idev->msg_len); in altr_i2c_empty_rx_fifo()
197 while (bytes_to_transfer-- > 0) { in altr_i2c_empty_rx_fifo()
198 *idev->buf++ = readl(idev->base + ALTR_I2C_RX_DATA); in altr_i2c_empty_rx_fifo()
199 idev->msg_len--; in altr_i2c_empty_rx_fifo()
205 * altr_i2c_fill_tx_fifo - Fill TX FIFO from current message buffer.
209 size_t tx_fifo_avail = idev->fifo_size - readl(idev->base + in altr_i2c_fill_tx_fifo()
211 int bytes_to_transfer = min(tx_fifo_avail, idev->msg_len); in altr_i2c_fill_tx_fifo()
212 int ret = idev->msg_len - bytes_to_transfer; in altr_i2c_fill_tx_fifo()
214 while (bytes_to_transfer-- > 0) { in altr_i2c_fill_tx_fifo()
215 altr_i2c_transfer(idev, *idev->buf++); in altr_i2c_fill_tx_fifo()
216 idev->msg_len--; in altr_i2c_fill_tx_fifo()
228 idev->isr_status = readl(idev->base + ALTR_I2C_ISR) & idev->isr_mask; in altr_i2c_isr_quick()
229 if (idev->isr_status) in altr_i2c_isr_quick()
240 u32 status = idev->isr_status; in altr_i2c_isr()
242 mutex_lock(&idev->isr_mutex); in altr_i2c_isr()
243 if (!idev->msg) { in altr_i2c_isr()
244 dev_warn(idev->dev, "unexpected interrupt\n"); in altr_i2c_isr()
248 read = (idev->msg->flags & I2C_M_RD) != 0; in altr_i2c_isr()
253 idev->msg_err = -EAGAIN; in altr_i2c_isr()
256 dev_dbg(idev->dev, "Could not get ACK\n"); in altr_i2c_isr()
257 idev->msg_err = -ENXIO; in altr_i2c_isr()
266 dev_err(idev->dev, "RX FIFO Overflow\n"); in altr_i2c_isr()
272 if (!idev->msg_len) in altr_i2c_isr()
277 if (idev->msg_len > 0) in altr_i2c_isr()
282 dev_warn(idev->dev, "Unexpected interrupt: 0x%x\n", status); in altr_i2c_isr()
288 ret = readl_poll_timeout_atomic(idev->base + ALTR_I2C_STATUS, in altr_i2c_isr()
293 dev_err(idev->dev, "message timeout\n"); in altr_i2c_isr()
296 complete(&idev->msg_complete); in altr_i2c_isr()
297 dev_dbg(idev->dev, "Message Complete\n"); in altr_i2c_isr()
300 mutex_unlock(&idev->isr_mutex); in altr_i2c_isr()
312 mutex_lock(&idev->isr_mutex); in altr_i2c_xfer_msg()
313 idev->msg = msg; in altr_i2c_xfer_msg()
314 idev->msg_len = msg->len; in altr_i2c_xfer_msg()
315 idev->buf = msg->buf; in altr_i2c_xfer_msg()
316 idev->msg_err = 0; in altr_i2c_xfer_msg()
317 reinit_completion(&idev->msg_complete); in altr_i2c_xfer_msg()
322 readl(idev->base + ALTR_I2C_RX_DATA); in altr_i2c_xfer_msg()
323 } while (readl(idev->base + ALTR_I2C_RX_FIFO_LVL)); in altr_i2c_xfer_msg()
325 writel(ALTR_I2C_TFR_CMD_STA | addr, idev->base + ALTR_I2C_TFR_CMD); in altr_i2c_xfer_msg()
327 if ((msg->flags & I2C_M_RD) != 0) { in altr_i2c_xfer_msg()
337 mutex_unlock(&idev->isr_mutex); in altr_i2c_xfer_msg()
339 time_left = wait_for_completion_timeout(&idev->msg_complete, in altr_i2c_xfer_msg()
341 mutex_lock(&idev->isr_mutex); in altr_i2c_xfer_msg()
344 value = readl(idev->base + ALTR_I2C_STATUS) & ALTR_I2C_STAT_CORE; in altr_i2c_xfer_msg()
346 dev_err(idev->dev, "Core Status not IDLE...\n"); in altr_i2c_xfer_msg()
349 idev->msg_err = -ETIMEDOUT; in altr_i2c_xfer_msg()
350 dev_dbg(idev->dev, "Transaction timed out.\n"); in altr_i2c_xfer_msg()
354 mutex_unlock(&idev->isr_mutex); in altr_i2c_xfer_msg()
356 return idev->msg_err; in altr_i2c_xfer_msg()
388 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); in altr_i2c_probe()
390 return -ENOMEM; in altr_i2c_probe()
392 idev->base = devm_platform_ioremap_resource(pdev, 0); in altr_i2c_probe()
393 if (IS_ERR(idev->base)) in altr_i2c_probe()
394 return PTR_ERR(idev->base); in altr_i2c_probe()
400 idev->i2c_clk = devm_clk_get(&pdev->dev, NULL); in altr_i2c_probe()
401 if (IS_ERR(idev->i2c_clk)) { in altr_i2c_probe()
402 dev_err(&pdev->dev, "missing clock\n"); in altr_i2c_probe()
403 return PTR_ERR(idev->i2c_clk); in altr_i2c_probe()
406 idev->dev = &pdev->dev; in altr_i2c_probe()
407 init_completion(&idev->msg_complete); in altr_i2c_probe()
408 mutex_init(&idev->isr_mutex); in altr_i2c_probe()
410 ret = device_property_read_u32(idev->dev, "fifo-size", in altr_i2c_probe()
411 &idev->fifo_size); in altr_i2c_probe()
413 dev_err(&pdev->dev, "FIFO size set to default of %d\n", in altr_i2c_probe()
415 idev->fifo_size = ALTR_I2C_DFLT_FIFO_SZ; in altr_i2c_probe()
418 ret = device_property_read_u32(idev->dev, "clock-frequency", in altr_i2c_probe()
419 &idev->bus_clk_rate); in altr_i2c_probe()
421 dev_err(&pdev->dev, "Default to 100kHz\n"); in altr_i2c_probe()
422 idev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */ in altr_i2c_probe()
425 if (idev->bus_clk_rate > I2C_MAX_FAST_MODE_FREQ) { in altr_i2c_probe()
426 dev_err(&pdev->dev, "invalid clock-frequency %d\n", in altr_i2c_probe()
427 idev->bus_clk_rate); in altr_i2c_probe()
428 return -EINVAL; in altr_i2c_probe()
431 ret = devm_request_threaded_irq(&pdev->dev, irq, altr_i2c_isr_quick, in altr_i2c_probe()
433 pdev->name, idev); in altr_i2c_probe()
435 dev_err(&pdev->dev, "failed to claim IRQ %d\n", irq); in altr_i2c_probe()
439 ret = clk_prepare_enable(idev->i2c_clk); in altr_i2c_probe()
441 dev_err(&pdev->dev, "failed to enable clock\n"); in altr_i2c_probe()
445 mutex_lock(&idev->isr_mutex); in altr_i2c_probe()
447 mutex_unlock(&idev->isr_mutex); in altr_i2c_probe()
449 i2c_set_adapdata(&idev->adapter, idev); in altr_i2c_probe()
450 strscpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name)); in altr_i2c_probe()
451 idev->adapter.owner = THIS_MODULE; in altr_i2c_probe()
452 idev->adapter.algo = &altr_i2c_algo; in altr_i2c_probe()
453 idev->adapter.dev.parent = &pdev->dev; in altr_i2c_probe()
454 idev->adapter.dev.of_node = pdev->dev.of_node; in altr_i2c_probe()
458 ret = i2c_add_adapter(&idev->adapter); in altr_i2c_probe()
460 clk_disable_unprepare(idev->i2c_clk); in altr_i2c_probe()
463 dev_info(&pdev->dev, "Altera SoftIP I2C Probe Complete\n"); in altr_i2c_probe()
472 clk_disable_unprepare(idev->i2c_clk); in altr_i2c_remove()
473 i2c_del_adapter(&idev->adapter); in altr_i2c_remove()
478 { .compatible = "altr,softip-i2c-v1.0" },
487 .name = "altera-i2c",
494 MODULE_DESCRIPTION("Altera Soft IP I2C bus driver");