Lines Matching +full:num +full:- +full:transfer +full:- +full:bits
1 // SPDX-License-Identifier: GPL-2.0
36 #define SYNQUACER_I2C_BSR_FBT BIT(0) // First Byte Transfer
39 #define SYNQUACER_I2C_BSR_TRX BIT(3) // Transfer/Receive
70 DIV_ROUND_UP(DIV_ROUND_UP((rate), I2C_MAX_STANDARD_MODE_FREQ) - 2, 2)
73 DIV_ROUND_UP((DIV_ROUND_UP((rate), I2C_MAX_FAST_MODE_FREQ) - 2) * 2, 3)
76 /* calculate the value of CS bits in CCR register on standard mode */
78 ((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 65) \
81 /* calculate the value of CS bits in CSR register on standard mode */
84 /* calculate the value of CS bits in CCR register on fast mode */
86 ((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) \
89 /* calculate the value of CS bits in CSR register on fast mode */
93 /* calculate the value of CS bits in CCR register on standard mode */
95 ((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 1) \
98 /* calculate the value of CS bits in CSR register on standard mode */
100 (((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 1) >> 5) \
103 /* calculate the value of CS bits in CCR register on fast mode */
105 ((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) \
108 /* calculate the value of CS bits in CSR register on fast mode */
110 (((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) >> 5) \
151 return i2c->msg_idx >= (i2c->msg_num - 1); in is_lastmsg()
156 return i2c->msg_ptr == (i2c->msg->len - 1); in is_msglast()
161 return i2c->msg_ptr >= i2c->msg->len; in is_msgend()
166 int num) in calc_timeout_ms() argument
171 for (i = 0; i < num; i++, msgs++) in calc_timeout_ms()
172 bit_count += msgs->len; in calc_timeout_ms()
174 return DIV_ROUND_UP((bit_count * 9 + num * 10) * 3, 200) + 10; in calc_timeout_ms()
184 writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_stop()
186 i2c->state = STATE_IDLE; in synquacer_i2c_stop()
188 i2c->msg_ptr = 0; in synquacer_i2c_stop()
189 i2c->msg = NULL; in synquacer_i2c_stop()
190 i2c->msg_idx++; in synquacer_i2c_stop()
191 i2c->msg_num = 0; in synquacer_i2c_stop()
193 i2c->msg_idx = ret; in synquacer_i2c_stop()
195 complete(&i2c->completion); in synquacer_i2c_stop()
201 u32 rt = i2c->pclkrate; in synquacer_i2c_hw_init()
204 writeb(0, i2c->base + SYNQUACER_I2C_REG_ADR); in synquacer_i2c_hw_init()
207 writeb(SYNQUACER_I2C_BUS_CLK_FR(i2c->pclkrate), in synquacer_i2c_hw_init()
208 i2c->base + SYNQUACER_I2C_REG_FSR); in synquacer_i2c_hw_init()
210 switch (i2c->speed_khz) { in synquacer_i2c_hw_init()
212 if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) { in synquacer_i2c_hw_init()
223 i2c->base + SYNQUACER_I2C_REG_CCR); in synquacer_i2c_hw_init()
224 writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR); in synquacer_i2c_hw_init()
227 if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) { in synquacer_i2c_hw_init()
237 i2c->base + SYNQUACER_I2C_REG_CCR); in synquacer_i2c_hw_init()
238 writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR); in synquacer_i2c_hw_init()
245 writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_hw_init()
246 writeb(0, i2c->base + SYNQUACER_I2C_REG_BC2R); in synquacer_i2c_hw_init()
252 writeb(0, i2c->base + SYNQUACER_I2C_REG_CCR); in synquacer_i2c_hw_reset()
253 writeb(0, i2c->base + SYNQUACER_I2C_REG_CSR); in synquacer_i2c_hw_reset()
255 WAIT_PCLK(100, i2c->pclkrate); in synquacer_i2c_hw_reset()
263 writeb(i2c_8bit_addr_from_msg(pmsg), i2c->base + SYNQUACER_I2C_REG_DAR); in synquacer_i2c_master_start()
265 dev_dbg(i2c->dev, "slave:0x%02x\n", pmsg->addr); in synquacer_i2c_master_start()
268 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_master_start()
269 bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
270 dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); in synquacer_i2c_master_start()
274 dev_dbg(i2c->dev, "bus is busy"); in synquacer_i2c_master_start()
275 return -EBUSY; in synquacer_i2c_master_start()
279 dev_dbg(i2c->dev, "Continuous Start"); in synquacer_i2c_master_start()
281 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
284 dev_dbg(i2c->dev, "not in master mode"); in synquacer_i2c_master_start()
285 return -EAGAIN; in synquacer_i2c_master_start()
287 dev_dbg(i2c->dev, "Start Condition"); in synquacer_i2c_master_start()
291 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
294 WAIT_PCLK(10, i2c->pclkrate); in synquacer_i2c_master_start()
297 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_master_start()
298 bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
299 dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); in synquacer_i2c_master_start()
303 dev_dbg(i2c->dev, "arbitration lost\n"); in synquacer_i2c_master_start()
304 return -EAGAIN; in synquacer_i2c_master_start()
311 struct i2c_msg *msgs, int num) in synquacer_i2c_doxfer() argument
318 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_doxfer()
320 dev_err(i2c->dev, "cannot get bus (bus busy)\n"); in synquacer_i2c_doxfer()
321 return -EBUSY; in synquacer_i2c_doxfer()
324 reinit_completion(&i2c->completion); in synquacer_i2c_doxfer()
326 i2c->msg = msgs; in synquacer_i2c_doxfer()
327 i2c->msg_num = num; in synquacer_i2c_doxfer()
328 i2c->msg_ptr = 0; in synquacer_i2c_doxfer()
329 i2c->msg_idx = 0; in synquacer_i2c_doxfer()
330 i2c->state = STATE_START; in synquacer_i2c_doxfer()
332 ret = synquacer_i2c_master_start(i2c, i2c->msg); in synquacer_i2c_doxfer()
334 dev_dbg(i2c->dev, "Address failed: (%d)\n", ret); in synquacer_i2c_doxfer()
338 timeout = wait_for_completion_timeout(&i2c->completion, in synquacer_i2c_doxfer()
339 msecs_to_jiffies(i2c->timeout_ms)); in synquacer_i2c_doxfer()
341 dev_dbg(i2c->dev, "timeout\n"); in synquacer_i2c_doxfer()
342 return -EAGAIN; in synquacer_i2c_doxfer()
345 ret = i2c->msg_idx; in synquacer_i2c_doxfer()
346 if (ret != num) { in synquacer_i2c_doxfer()
347 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); in synquacer_i2c_doxfer()
348 return -EAGAIN; in synquacer_i2c_doxfer()
352 udelay(DIV_ROUND_UP(2 * 1000, i2c->speed_khz)); in synquacer_i2c_doxfer()
365 bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
366 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_isr()
367 dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); in synquacer_i2c_isr()
370 dev_err(i2c->dev, "bus error\n"); in synquacer_i2c_isr()
371 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
376 dev_dbg(i2c->dev, "arbitration lost\n"); in synquacer_i2c_isr()
377 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
381 switch (i2c->state) { in synquacer_i2c_isr()
384 dev_dbg(i2c->dev, "ack was not received\n"); in synquacer_i2c_isr()
385 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
389 if (i2c->msg->flags & I2C_M_RD) in synquacer_i2c_isr()
390 i2c->state = STATE_READ; in synquacer_i2c_isr()
392 i2c->state = STATE_WRITE; in synquacer_i2c_isr()
394 if (is_lastmsg(i2c) && i2c->msg->len == 0) { in synquacer_i2c_isr()
399 if (i2c->state == STATE_READ) in synquacer_i2c_isr()
405 dev_dbg(i2c->dev, "WRITE: No Ack\n"); in synquacer_i2c_isr()
406 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
411 writeb(i2c->msg->buf[i2c->msg_ptr++], in synquacer_i2c_isr()
412 i2c->base + SYNQUACER_I2C_REG_DAR); in synquacer_i2c_isr()
418 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
425 dev_dbg(i2c->dev, "WRITE: Next Message\n"); in synquacer_i2c_isr()
427 i2c->msg_ptr = 0; in synquacer_i2c_isr()
428 i2c->msg_idx++; in synquacer_i2c_isr()
429 i2c->msg++; in synquacer_i2c_isr()
432 ret = synquacer_i2c_master_start(i2c, i2c->msg); in synquacer_i2c_isr()
434 dev_dbg(i2c->dev, "restart error (%d)\n", ret); in synquacer_i2c_isr()
435 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
438 i2c->state = STATE_START; in synquacer_i2c_isr()
442 byte = readb(i2c->base + SYNQUACER_I2C_REG_DAR); in synquacer_i2c_isr()
444 i2c->msg->buf[i2c->msg_ptr++] = byte; in synquacer_i2c_isr()
446 dev_dbg(i2c->dev, "address:0x%02x. ignore it.\n", byte); in synquacer_i2c_isr()
453 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
461 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
466 dev_dbg(i2c->dev, "READ: Send Stop\n"); in synquacer_i2c_isr()
470 dev_dbg(i2c->dev, "READ: Next Transfer\n"); in synquacer_i2c_isr()
472 i2c->msg_ptr = 0; in synquacer_i2c_isr()
473 i2c->msg_idx++; in synquacer_i2c_isr()
474 i2c->msg++; in synquacer_i2c_isr()
476 ret = synquacer_i2c_master_start(i2c, i2c->msg); in synquacer_i2c_isr()
478 dev_dbg(i2c->dev, "restart error (%d)\n", ret); in synquacer_i2c_isr()
479 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
481 i2c->state = STATE_START; in synquacer_i2c_isr()
485 dev_err(i2c->dev, "called in err STATE (%d)\n", i2c->state); in synquacer_i2c_isr()
490 WAIT_PCLK(10, i2c->pclkrate); in synquacer_i2c_isr()
495 int num) in synquacer_i2c_xfer() argument
502 i2c->timeout_ms = calc_timeout_ms(i2c, msgs, num); in synquacer_i2c_xfer()
504 dev_dbg(i2c->dev, "calculated timeout %d ms\n", i2c->timeout_ms); in synquacer_i2c_xfer()
506 for (retry = 0; retry <= adap->retries; retry++) { in synquacer_i2c_xfer()
507 ret = synquacer_i2c_doxfer(i2c, msgs, num); in synquacer_i2c_xfer()
508 if (ret != -EAGAIN) in synquacer_i2c_xfer()
511 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); in synquacer_i2c_xfer()
515 return -EIO; in synquacer_i2c_xfer()
530 .name = "synquacer_i2c-adapter",
541 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in synquacer_i2c_probe()
543 return -ENOMEM; in synquacer_i2c_probe()
545 bus_speed = i2c_acpi_find_bus_speed(&pdev->dev); in synquacer_i2c_probe()
547 device_property_read_u32(&pdev->dev, "clock-frequency", in synquacer_i2c_probe()
550 device_property_read_u32(&pdev->dev, "socionext,pclk-rate", in synquacer_i2c_probe()
551 &i2c->pclkrate); in synquacer_i2c_probe()
553 i2c->pclk = devm_clk_get(&pdev->dev, "pclk"); in synquacer_i2c_probe()
554 if (PTR_ERR(i2c->pclk) == -EPROBE_DEFER) in synquacer_i2c_probe()
555 return -EPROBE_DEFER; in synquacer_i2c_probe()
556 if (!IS_ERR_OR_NULL(i2c->pclk)) { in synquacer_i2c_probe()
557 dev_dbg(&pdev->dev, "clock source %p\n", i2c->pclk); in synquacer_i2c_probe()
559 ret = clk_prepare_enable(i2c->pclk); in synquacer_i2c_probe()
561 dev_err(&pdev->dev, "failed to enable clock (%d)\n", in synquacer_i2c_probe()
565 i2c->pclkrate = clk_get_rate(i2c->pclk); in synquacer_i2c_probe()
568 if (i2c->pclkrate < SYNQUACER_I2C_MIN_CLK_RATE || in synquacer_i2c_probe()
569 i2c->pclkrate > SYNQUACER_I2C_MAX_CLK_RATE) { in synquacer_i2c_probe()
570 dev_err(&pdev->dev, "PCLK missing or out of range (%d)\n", in synquacer_i2c_probe()
571 i2c->pclkrate); in synquacer_i2c_probe()
572 return -EINVAL; in synquacer_i2c_probe()
575 i2c->base = devm_platform_ioremap_resource(pdev, 0); in synquacer_i2c_probe()
576 if (IS_ERR(i2c->base)) in synquacer_i2c_probe()
577 return PTR_ERR(i2c->base); in synquacer_i2c_probe()
579 i2c->irq = platform_get_irq(pdev, 0); in synquacer_i2c_probe()
580 if (i2c->irq < 0) in synquacer_i2c_probe()
581 return i2c->irq; in synquacer_i2c_probe()
583 ret = devm_request_irq(&pdev->dev, i2c->irq, synquacer_i2c_isr, in synquacer_i2c_probe()
584 0, dev_name(&pdev->dev), i2c); in synquacer_i2c_probe()
586 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); in synquacer_i2c_probe()
590 i2c->state = STATE_IDLE; in synquacer_i2c_probe()
591 i2c->dev = &pdev->dev; in synquacer_i2c_probe()
592 i2c->adapter = synquacer_i2c_ops; in synquacer_i2c_probe()
593 i2c_set_adapdata(&i2c->adapter, i2c); in synquacer_i2c_probe()
594 i2c->adapter.dev.parent = &pdev->dev; in synquacer_i2c_probe()
595 i2c->adapter.dev.of_node = pdev->dev.of_node; in synquacer_i2c_probe()
596 ACPI_COMPANION_SET(&i2c->adapter.dev, ACPI_COMPANION(&pdev->dev)); in synquacer_i2c_probe()
597 i2c->adapter.nr = pdev->id; in synquacer_i2c_probe()
598 init_completion(&i2c->completion); in synquacer_i2c_probe()
601 i2c->speed_khz = SYNQUACER_I2C_SPEED_SM; in synquacer_i2c_probe()
603 i2c->speed_khz = SYNQUACER_I2C_SPEED_FM; in synquacer_i2c_probe()
607 ret = i2c_add_numbered_adapter(&i2c->adapter); in synquacer_i2c_probe()
609 dev_err(&pdev->dev, "failed to add bus to i2c core\n"); in synquacer_i2c_probe()
615 dev_info(&pdev->dev, "%s: synquacer_i2c adapter\n", in synquacer_i2c_probe()
616 dev_name(&i2c->adapter.dev)); in synquacer_i2c_probe()
625 i2c_del_adapter(&i2c->adapter); in synquacer_i2c_remove()
626 if (!IS_ERR(i2c->pclk)) in synquacer_i2c_remove()
627 clk_disable_unprepare(i2c->pclk); in synquacer_i2c_remove()
633 { .compatible = "socionext,synquacer-i2c" },