Lines Matching +full:fast +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0
56 #define SYNQUACER_I2C_CCR_FM BIT(6) // Speed Mode Select
68 /* STANDARD MODE frequency */
70 DIV_ROUND_UP(DIV_ROUND_UP((rate), I2C_MAX_STANDARD_MODE_FREQ) - 2, 2)
71 /* FAST MODE frequency */
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) \
120 #define SYNQUACER_I2C_SPEED_FM 400 // Fast Mode
121 #define SYNQUACER_I2C_SPEED_SM 100 // Standard Mode
150 return i2c->msg_idx >= (i2c->msg_num - 1); in is_lastmsg()
155 return i2c->msg_ptr == (i2c->msg->len - 1); in is_msglast()
160 return i2c->msg_ptr >= i2c->msg->len; in is_msgend()
171 bit_count += msgs->len; in calc_timeout_ms()
183 writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_stop()
185 i2c->state = STATE_IDLE; in synquacer_i2c_stop()
187 i2c->msg_ptr = 0; in synquacer_i2c_stop()
188 i2c->msg = NULL; in synquacer_i2c_stop()
189 i2c->msg_idx++; in synquacer_i2c_stop()
190 i2c->msg_num = 0; in synquacer_i2c_stop()
192 i2c->msg_idx = ret; in synquacer_i2c_stop()
194 complete(&i2c->completion); in synquacer_i2c_stop()
200 u32 rt = i2c->pclkrate; in synquacer_i2c_hw_init()
203 writeb(0, i2c->base + SYNQUACER_I2C_REG_ADR); in synquacer_i2c_hw_init()
206 writeb(SYNQUACER_I2C_BUS_CLK_FR(i2c->pclkrate), in synquacer_i2c_hw_init()
207 i2c->base + SYNQUACER_I2C_REG_FSR); in synquacer_i2c_hw_init()
209 switch (i2c->speed_khz) { in synquacer_i2c_hw_init()
211 if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) { in synquacer_i2c_hw_init()
219 /* Set Clock and enable, Set fast mode */ in synquacer_i2c_hw_init()
222 i2c->base + SYNQUACER_I2C_REG_CCR); in synquacer_i2c_hw_init()
223 writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR); in synquacer_i2c_hw_init()
226 if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) { in synquacer_i2c_hw_init()
234 /* Set Clock and enable, Set standard mode */ in synquacer_i2c_hw_init()
236 i2c->base + SYNQUACER_I2C_REG_CCR); in synquacer_i2c_hw_init()
237 writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR); in synquacer_i2c_hw_init()
244 writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_hw_init()
245 writeb(0, i2c->base + SYNQUACER_I2C_REG_BC2R); in synquacer_i2c_hw_init()
251 writeb(0, i2c->base + SYNQUACER_I2C_REG_CCR); in synquacer_i2c_hw_reset()
252 writeb(0, i2c->base + SYNQUACER_I2C_REG_CSR); in synquacer_i2c_hw_reset()
254 WAIT_PCLK(100, i2c->pclkrate); in synquacer_i2c_hw_reset()
262 writeb(i2c_8bit_addr_from_msg(pmsg), i2c->base + SYNQUACER_I2C_REG_DAR); in synquacer_i2c_master_start()
264 dev_dbg(i2c->dev, "slave:0x%02x\n", pmsg->addr); in synquacer_i2c_master_start()
267 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_master_start()
268 bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
269 dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); in synquacer_i2c_master_start()
273 dev_dbg(i2c->dev, "bus is busy"); in synquacer_i2c_master_start()
274 return -EBUSY; in synquacer_i2c_master_start()
278 dev_dbg(i2c->dev, "Continuous Start"); in synquacer_i2c_master_start()
280 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
283 dev_dbg(i2c->dev, "not in master mode"); in synquacer_i2c_master_start()
284 return -EAGAIN; in synquacer_i2c_master_start()
286 dev_dbg(i2c->dev, "Start Condition"); in synquacer_i2c_master_start()
290 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
293 WAIT_PCLK(10, i2c->pclkrate); in synquacer_i2c_master_start()
296 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_master_start()
297 bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_master_start()
298 dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); in synquacer_i2c_master_start()
302 dev_dbg(i2c->dev, "arbitration lost\n"); in synquacer_i2c_master_start()
303 return -EAGAIN; in synquacer_i2c_master_start()
317 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_doxfer()
319 dev_err(i2c->dev, "cannot get bus (bus busy)\n"); in synquacer_i2c_doxfer()
320 return -EBUSY; in synquacer_i2c_doxfer()
323 reinit_completion(&i2c->completion); in synquacer_i2c_doxfer()
325 i2c->msg = msgs; in synquacer_i2c_doxfer()
326 i2c->msg_num = num; in synquacer_i2c_doxfer()
327 i2c->msg_ptr = 0; in synquacer_i2c_doxfer()
328 i2c->msg_idx = 0; in synquacer_i2c_doxfer()
329 i2c->state = STATE_START; in synquacer_i2c_doxfer()
331 ret = synquacer_i2c_master_start(i2c, i2c->msg); in synquacer_i2c_doxfer()
333 dev_dbg(i2c->dev, "Address failed: (%d)\n", ret); in synquacer_i2c_doxfer()
337 time_left = wait_for_completion_timeout(&i2c->completion, in synquacer_i2c_doxfer()
338 msecs_to_jiffies(i2c->timeout_ms)); in synquacer_i2c_doxfer()
340 dev_dbg(i2c->dev, "timeout\n"); in synquacer_i2c_doxfer()
341 return -EAGAIN; in synquacer_i2c_doxfer()
344 ret = i2c->msg_idx; in synquacer_i2c_doxfer()
346 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); in synquacer_i2c_doxfer()
347 return -EAGAIN; in synquacer_i2c_doxfer()
351 udelay(DIV_ROUND_UP(2 * 1000, i2c->speed_khz)); in synquacer_i2c_doxfer()
364 bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
365 bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR); in synquacer_i2c_isr()
366 dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr); in synquacer_i2c_isr()
369 dev_err(i2c->dev, "bus error\n"); in synquacer_i2c_isr()
370 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
375 dev_dbg(i2c->dev, "arbitration lost\n"); in synquacer_i2c_isr()
376 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
380 switch (i2c->state) { in synquacer_i2c_isr()
383 dev_dbg(i2c->dev, "ack was not received\n"); in synquacer_i2c_isr()
384 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
388 if (i2c->msg->flags & I2C_M_RD) in synquacer_i2c_isr()
389 i2c->state = STATE_READ; in synquacer_i2c_isr()
391 i2c->state = STATE_WRITE; in synquacer_i2c_isr()
393 if (is_lastmsg(i2c) && i2c->msg->len == 0) { in synquacer_i2c_isr()
398 if (i2c->state == STATE_READ) in synquacer_i2c_isr()
404 dev_dbg(i2c->dev, "WRITE: No Ack\n"); in synquacer_i2c_isr()
405 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
410 writeb(i2c->msg->buf[i2c->msg_ptr++], in synquacer_i2c_isr()
411 i2c->base + SYNQUACER_I2C_REG_DAR); in synquacer_i2c_isr()
417 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
424 dev_dbg(i2c->dev, "WRITE: Next Message\n"); in synquacer_i2c_isr()
426 i2c->msg_ptr = 0; in synquacer_i2c_isr()
427 i2c->msg_idx++; in synquacer_i2c_isr()
428 i2c->msg++; in synquacer_i2c_isr()
431 ret = synquacer_i2c_master_start(i2c, i2c->msg); in synquacer_i2c_isr()
433 dev_dbg(i2c->dev, "restart error (%d)\n", ret); in synquacer_i2c_isr()
434 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
437 i2c->state = STATE_START; in synquacer_i2c_isr()
441 byte = readb(i2c->base + SYNQUACER_I2C_REG_DAR); in synquacer_i2c_isr()
443 i2c->msg->buf[i2c->msg_ptr++] = byte; in synquacer_i2c_isr()
445 dev_dbg(i2c->dev, "address:0x%02x. ignore it.\n", byte); in synquacer_i2c_isr()
452 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
460 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
465 dev_dbg(i2c->dev, "READ: Send Stop\n"); in synquacer_i2c_isr()
469 dev_dbg(i2c->dev, "READ: Next Transfer\n"); in synquacer_i2c_isr()
471 i2c->msg_ptr = 0; in synquacer_i2c_isr()
472 i2c->msg_idx++; in synquacer_i2c_isr()
473 i2c->msg++; in synquacer_i2c_isr()
475 ret = synquacer_i2c_master_start(i2c, i2c->msg); in synquacer_i2c_isr()
477 dev_dbg(i2c->dev, "restart error (%d)\n", ret); in synquacer_i2c_isr()
478 synquacer_i2c_stop(i2c, -EAGAIN); in synquacer_i2c_isr()
480 i2c->state = STATE_START; in synquacer_i2c_isr()
484 dev_err(i2c->dev, "called in err STATE (%d)\n", i2c->state); in synquacer_i2c_isr()
489 WAIT_PCLK(10, i2c->pclkrate); in synquacer_i2c_isr()
501 i2c->timeout_ms = calc_timeout_ms(i2c, msgs, num); in synquacer_i2c_xfer()
503 dev_dbg(i2c->dev, "calculated timeout %d ms\n", i2c->timeout_ms); in synquacer_i2c_xfer()
505 for (retry = 0; retry <= adap->retries; retry++) { in synquacer_i2c_xfer()
507 if (ret != -EAGAIN) in synquacer_i2c_xfer()
510 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); in synquacer_i2c_xfer()
514 return -EIO; in synquacer_i2c_xfer()
529 .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 pclk = devm_clk_get_optional_enabled(&pdev->dev, "pclk"); in synquacer_i2c_probe()
555 return dev_err_probe(&pdev->dev, PTR_ERR(pclk), in synquacer_i2c_probe()
559 i2c->pclkrate = clk_get_rate(pclk); in synquacer_i2c_probe()
561 if (i2c->pclkrate < SYNQUACER_I2C_MIN_CLK_RATE || in synquacer_i2c_probe()
562 i2c->pclkrate > SYNQUACER_I2C_MAX_CLK_RATE) in synquacer_i2c_probe()
563 return dev_err_probe(&pdev->dev, -EINVAL, in synquacer_i2c_probe()
565 i2c->pclkrate); in synquacer_i2c_probe()
567 i2c->base = devm_platform_ioremap_resource(pdev, 0); in synquacer_i2c_probe()
568 if (IS_ERR(i2c->base)) in synquacer_i2c_probe()
569 return PTR_ERR(i2c->base); in synquacer_i2c_probe()
571 i2c->irq = platform_get_irq(pdev, 0); in synquacer_i2c_probe()
572 if (i2c->irq < 0) in synquacer_i2c_probe()
573 return i2c->irq; in synquacer_i2c_probe()
575 ret = devm_request_irq(&pdev->dev, i2c->irq, synquacer_i2c_isr, in synquacer_i2c_probe()
576 0, dev_name(&pdev->dev), i2c); in synquacer_i2c_probe()
578 return dev_err_probe(&pdev->dev, ret, "cannot claim IRQ %d\n", i2c->irq); in synquacer_i2c_probe()
580 i2c->state = STATE_IDLE; in synquacer_i2c_probe()
581 i2c->dev = &pdev->dev; in synquacer_i2c_probe()
582 i2c->adapter = synquacer_i2c_ops; in synquacer_i2c_probe()
583 i2c_set_adapdata(&i2c->adapter, i2c); in synquacer_i2c_probe()
584 i2c->adapter.dev.parent = &pdev->dev; in synquacer_i2c_probe()
585 i2c->adapter.dev.of_node = pdev->dev.of_node; in synquacer_i2c_probe()
586 ACPI_COMPANION_SET(&i2c->adapter.dev, ACPI_COMPANION(&pdev->dev)); in synquacer_i2c_probe()
587 i2c->adapter.nr = pdev->id; in synquacer_i2c_probe()
588 init_completion(&i2c->completion); in synquacer_i2c_probe()
591 i2c->speed_khz = SYNQUACER_I2C_SPEED_SM; in synquacer_i2c_probe()
593 i2c->speed_khz = SYNQUACER_I2C_SPEED_FM; in synquacer_i2c_probe()
597 ret = i2c_add_numbered_adapter(&i2c->adapter); in synquacer_i2c_probe()
599 return dev_err_probe(&pdev->dev, ret, "failed to add bus to i2c core\n"); in synquacer_i2c_probe()
603 dev_info(&pdev->dev, "%s: synquacer_i2c adapter\n", in synquacer_i2c_probe()
604 dev_name(&i2c->adapter.dev)); in synquacer_i2c_probe()
613 i2c_del_adapter(&i2c->adapter); in synquacer_i2c_remove()
617 { .compatible = "socionext,synquacer-i2c" },