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

1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/i2c.h>
25 /* I2C register address definitions */
35 /* I2C register bit definitions */
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) \
113 /* min I2C clock frequency 14M */
115 /* max I2C clock frequency 200M */
117 /* I2C clock frequency 18M */
120 #define SYNQUACER_I2C_SPEED_FM 400 // Fast Mode
121 #define SYNQUACER_I2C_SPEED_SM 100 // Standard Mode
149 static inline int is_lastmsg(struct synquacer_i2c *i2c) in is_lastmsg() argument
151 return i2c->msg_idx >= (i2c->msg_num - 1); in is_lastmsg()
154 static inline int is_msglast(struct synquacer_i2c *i2c) in is_msglast() argument
156 return i2c->msg_ptr == (i2c->msg->len - 1); in is_msglast()
159 static inline int is_msgend(struct synquacer_i2c *i2c) in is_msgend() argument
161 return i2c->msg_ptr >= i2c->msg->len; in is_msgend()
164 static inline unsigned long calc_timeout_ms(struct synquacer_i2c *i2c, in calc_timeout_ms() argument
172 bit_count += msgs->len; in calc_timeout_ms()
177 static void synquacer_i2c_stop(struct synquacer_i2c *i2c, int ret) in synquacer_i2c_stop() argument
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()
198 static void synquacer_i2c_hw_init(struct synquacer_i2c *i2c) in synquacer_i2c_hw_init() argument
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()
220 /* Set Clock and enable, Set fast mode */ 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()
235 /* Set Clock and enable, Set standard mode */ 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()
249 static void synquacer_i2c_hw_reset(struct synquacer_i2c *i2c) in synquacer_i2c_hw_reset() argument
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()
258 static int synquacer_i2c_master_start(struct synquacer_i2c *i2c, in synquacer_i2c_master_start() argument
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()
310 static int synquacer_i2c_doxfer(struct synquacer_i2c *i2c, in synquacer_i2c_doxfer() argument
317 synquacer_i2c_hw_init(i2c); in synquacer_i2c_doxfer()
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()
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()
359 struct synquacer_i2c *i2c = dev_id; in synquacer_i2c_isr() local
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()
395 synquacer_i2c_stop(i2c, 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()
410 if (!is_msgend(i2c)) { 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()
421 if (is_lastmsg(i2c)) { in synquacer_i2c_isr()
422 synquacer_i2c_stop(i2c, 0); 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()
449 if (is_msglast(i2c)) { in synquacer_i2c_isr()
453 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
456 if (!is_msgend(i2c)) { in synquacer_i2c_isr()
461 i2c->base + SYNQUACER_I2C_REG_BCR); in synquacer_i2c_isr()
464 if (is_lastmsg(i2c)) { in synquacer_i2c_isr()
466 dev_dbg(i2c->dev, "READ: Send Stop\n"); in synquacer_i2c_isr()
467 synquacer_i2c_stop(i2c, 0); 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()
497 struct synquacer_i2c *i2c; in synquacer_i2c_xfer() local
501 i2c = i2c_get_adapdata(adap); in synquacer_i2c_xfer()
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()
513 synquacer_i2c_hw_reset(i2c); in synquacer_i2c_xfer()
515 return -EIO; in synquacer_i2c_xfer()
530 .name = "synquacer_i2c-adapter",
537 struct synquacer_i2c *i2c; in synquacer_i2c_probe() local
541 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); in synquacer_i2c_probe()
542 if (!i2c) 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 -ENODEV; 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()
605 synquacer_i2c_hw_init(i2c); 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()
613 platform_set_drvdata(pdev, i2c); 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()
623 struct synquacer_i2c *i2c = platform_get_drvdata(pdev); in synquacer_i2c_remove() local
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" },
658 MODULE_DESCRIPTION("Socionext SynQuacer I2C Driver");