Lines Matching +full:i2c +full:- +full:transfer +full:- +full:timeout +full:- +full:us
1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C bus driver for Amlogic Meson SoCs
11 #include <linux/i2c.h>
21 /* Meson I2C register map */
68 * struct meson_i2c - Meson I2C device private data
70 * @adap: I2C adapter instance
74 * @msg: Pointer to the current I2C message
76 * @last: Flag set for the last message in the transfer
77 * @count: Number of bytes to be sent/received in current transfer
81 * @done: Completion used to wait for transfer termination
108 void (*set_clk_div)(struct meson_i2c *i2c, unsigned int freq);
111 static void meson_i2c_set_mask(struct meson_i2c *i2c, int reg, u32 mask, in meson_i2c_set_mask() argument
116 data = readl(i2c->regs + reg); in meson_i2c_set_mask()
119 writel(data, i2c->regs + reg); in meson_i2c_set_mask()
122 static void meson_i2c_reset_tokens(struct meson_i2c *i2c) in meson_i2c_reset_tokens() argument
124 i2c->tokens[0] = 0; in meson_i2c_reset_tokens()
125 i2c->tokens[1] = 0; in meson_i2c_reset_tokens()
126 i2c->num_tokens = 0; in meson_i2c_reset_tokens()
129 static void meson_i2c_add_token(struct meson_i2c *i2c, int token) in meson_i2c_add_token() argument
131 if (i2c->num_tokens < 8) in meson_i2c_add_token()
132 i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4); in meson_i2c_add_token()
134 i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4); in meson_i2c_add_token()
136 i2c->num_tokens++; in meson_i2c_add_token()
139 static void meson_gxbb_axg_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq) in meson_gxbb_axg_i2c_set_clk_div() argument
141 unsigned long clk_rate = clk_get_rate(i2c->clk); in meson_gxbb_axg_i2c_set_clk_div()
144 /* According to I2C-BUS Spec 2.1, in FAST-MODE, the minimum LOW period is 1.3uS, and in meson_gxbb_axg_i2c_set_clk_div()
145 * minimum HIGH is least 0.6us. in meson_gxbb_axg_i2c_set_clk_div()
146 * For 400000 freq, the period is 2.5us. To keep within the specs, give 40% of period to in meson_gxbb_axg_i2c_set_clk_div()
147 * HIGH and 60% to LOW. This means HIGH at 1.0us and LOW 1.5us. in meson_gxbb_axg_i2c_set_clk_div()
148 * The same applies for Fast-mode plus, where LOW is 0.5us and HIGH is 0.26us. in meson_gxbb_axg_i2c_set_clk_div()
154 div_h = DIV_ROUND_UP(div_h, 2) - FILTER_DELAY; in meson_gxbb_axg_i2c_set_clk_div()
156 div_h = DIV_ROUND_UP(clk_rate * 2, freq * 5) - FILTER_DELAY; in meson_gxbb_axg_i2c_set_clk_div()
162 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson_gxbb_axg_i2c_set_clk_div()
166 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson_gxbb_axg_i2c_set_clk_div()
170 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK, in meson_gxbb_axg_i2c_set_clk_div()
173 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK, in meson_gxbb_axg_i2c_set_clk_div()
177 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_MASK, in meson_gxbb_axg_i2c_set_clk_div()
181 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, REG_SLV_SCL_LOW_EN); in meson_gxbb_axg_i2c_set_clk_div()
183 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, divh %u, divl %u\n", __func__, in meson_gxbb_axg_i2c_set_clk_div()
187 static void meson6_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq) in meson6_i2c_set_clk_div() argument
189 unsigned long clk_rate = clk_get_rate(i2c->clk); in meson6_i2c_set_clk_div()
193 div -= FILTER_DELAY; in meson6_i2c_set_clk_div()
198 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson6_i2c_set_clk_div()
202 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK, in meson6_i2c_set_clk_div()
205 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK, in meson6_i2c_set_clk_div()
209 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0); in meson6_i2c_set_clk_div()
211 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__, in meson6_i2c_set_clk_div()
215 static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len) in meson_i2c_get_data() argument
220 rdata0 = readl(i2c->regs + REG_TOK_RDATA0); in meson_i2c_get_data()
221 rdata1 = readl(i2c->regs + REG_TOK_RDATA1); in meson_i2c_get_data()
223 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__, in meson_i2c_get_data()
230 *buf++ = (rdata1 >> (i - 4) * 8) & 0xff; in meson_i2c_get_data()
233 static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len) in meson_i2c_put_data() argument
242 wdata1 |= *buf++ << ((i - 4) * 8); in meson_i2c_put_data()
244 writel(wdata0, i2c->regs + REG_TOK_WDATA0); in meson_i2c_put_data()
245 writel(wdata1, i2c->regs + REG_TOK_WDATA1); in meson_i2c_put_data()
247 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__, in meson_i2c_put_data()
251 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c) in meson_i2c_prepare_xfer() argument
253 bool write = !(i2c->msg->flags & I2C_M_RD); in meson_i2c_prepare_xfer()
256 i2c->count = min(i2c->msg->len - i2c->pos, 8); in meson_i2c_prepare_xfer()
258 for (i = 0; i < i2c->count - 1; i++) in meson_i2c_prepare_xfer()
259 meson_i2c_add_token(i2c, TOKEN_DATA); in meson_i2c_prepare_xfer()
261 if (i2c->count) { in meson_i2c_prepare_xfer()
262 if (write || i2c->pos + i2c->count < i2c->msg->len) in meson_i2c_prepare_xfer()
263 meson_i2c_add_token(i2c, TOKEN_DATA); in meson_i2c_prepare_xfer()
265 meson_i2c_add_token(i2c, TOKEN_DATA_LAST); in meson_i2c_prepare_xfer()
269 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count); in meson_i2c_prepare_xfer()
271 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len) in meson_i2c_prepare_xfer()
272 meson_i2c_add_token(i2c, TOKEN_STOP); in meson_i2c_prepare_xfer()
274 writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0); in meson_i2c_prepare_xfer()
275 writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1); in meson_i2c_prepare_xfer()
278 static void meson_i2c_transfer_complete(struct meson_i2c *i2c, u32 ctrl) in meson_i2c_transfer_complete() argument
284 * I2C controller automatically generates a STOP in meson_i2c_transfer_complete()
287 dev_dbg(i2c->dev, "error bit set\n"); in meson_i2c_transfer_complete()
288 i2c->error = -ENXIO; in meson_i2c_transfer_complete()
289 i2c->state = STATE_IDLE; in meson_i2c_transfer_complete()
291 if (i2c->state == STATE_READ && i2c->count) in meson_i2c_transfer_complete()
292 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos, in meson_i2c_transfer_complete()
293 i2c->count); in meson_i2c_transfer_complete()
295 i2c->pos += i2c->count; in meson_i2c_transfer_complete()
297 if (i2c->pos >= i2c->msg->len) in meson_i2c_transfer_complete()
298 i2c->state = STATE_IDLE; in meson_i2c_transfer_complete()
304 struct meson_i2c *i2c = dev_id; in meson_i2c_irq() local
307 spin_lock(&i2c->lock); in meson_i2c_irq()
309 meson_i2c_reset_tokens(i2c); in meson_i2c_irq()
310 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_irq()
311 ctrl = readl(i2c->regs + REG_CTRL); in meson_i2c_irq()
313 dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n", in meson_i2c_irq()
314 i2c->state, i2c->pos, i2c->count, ctrl); in meson_i2c_irq()
316 if (i2c->state == STATE_IDLE) { in meson_i2c_irq()
317 spin_unlock(&i2c->lock); in meson_i2c_irq()
321 meson_i2c_transfer_complete(i2c, ctrl); in meson_i2c_irq()
323 if (i2c->state == STATE_IDLE) { in meson_i2c_irq()
324 complete(&i2c->done); in meson_i2c_irq()
329 meson_i2c_prepare_xfer(i2c); in meson_i2c_irq()
330 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START); in meson_i2c_irq()
332 spin_unlock(&i2c->lock); in meson_i2c_irq()
337 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg) in meson_i2c_do_start() argument
341 token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ : in meson_i2c_do_start()
345 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR_MASK, in meson_i2c_do_start()
346 FIELD_PREP(REG_SLV_ADDR_MASK, msg->addr << 1)); in meson_i2c_do_start()
348 meson_i2c_add_token(i2c, TOKEN_START); in meson_i2c_do_start()
349 meson_i2c_add_token(i2c, token); in meson_i2c_do_start()
352 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg, in meson_i2c_xfer_msg() argument
359 i2c->msg = msg; in meson_i2c_xfer_msg()
360 i2c->last = last; in meson_i2c_xfer_msg()
361 i2c->pos = 0; in meson_i2c_xfer_msg()
362 i2c->count = 0; in meson_i2c_xfer_msg()
363 i2c->error = 0; in meson_i2c_xfer_msg()
365 meson_i2c_reset_tokens(i2c); in meson_i2c_xfer_msg()
367 flags = (msg->flags & I2C_M_IGNORE_NAK) ? REG_CTRL_ACK_IGNORE : 0; in meson_i2c_xfer_msg()
368 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags); in meson_i2c_xfer_msg()
370 if (!(msg->flags & I2C_M_NOSTART)) in meson_i2c_xfer_msg()
371 meson_i2c_do_start(i2c, msg); in meson_i2c_xfer_msg()
373 i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; in meson_i2c_xfer_msg()
374 meson_i2c_prepare_xfer(i2c); in meson_i2c_xfer_msg()
377 reinit_completion(&i2c->done); in meson_i2c_xfer_msg()
379 /* Start the transfer */ in meson_i2c_xfer_msg()
380 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START); in meson_i2c_xfer_msg()
383 ret = readl_poll_timeout_atomic(i2c->regs + REG_CTRL, ctrl, in meson_i2c_xfer_msg()
388 time_left = wait_for_completion_timeout(&i2c->done, time_left); in meson_i2c_xfer_msg()
391 ret = -ETIMEDOUT; in meson_i2c_xfer_msg()
395 * Protect access to i2c struct and registers from interrupt in meson_i2c_xfer_msg()
396 * handlers triggered by a transfer terminated after the in meson_i2c_xfer_msg()
397 * timeout period in meson_i2c_xfer_msg()
399 spin_lock_irqsave(&i2c->lock, flags); in meson_i2c_xfer_msg()
402 meson_i2c_transfer_complete(i2c, ctrl); in meson_i2c_xfer_msg()
405 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_xfer_msg()
408 i2c->state = STATE_IDLE; in meson_i2c_xfer_msg()
410 if (i2c->error) in meson_i2c_xfer_msg()
411 ret = i2c->error; in meson_i2c_xfer_msg()
413 spin_unlock_irqrestore(&i2c->lock, flags); in meson_i2c_xfer_msg()
421 struct meson_i2c *i2c = adap->algo_data; in meson_i2c_xfer_messages() local
425 ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1, atomic); in meson_i2c_xfer_messages()
458 struct device_node *np = pdev->dev.of_node; in meson_i2c_probe()
459 struct meson_i2c *i2c; in meson_i2c_probe() local
463 i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL); in meson_i2c_probe()
464 if (!i2c) in meson_i2c_probe()
465 return -ENOMEM; in meson_i2c_probe()
467 i2c_parse_fw_timings(&pdev->dev, &timings, true); in meson_i2c_probe()
469 i2c->dev = &pdev->dev; in meson_i2c_probe()
470 platform_set_drvdata(pdev, i2c); in meson_i2c_probe()
472 spin_lock_init(&i2c->lock); in meson_i2c_probe()
473 init_completion(&i2c->done); in meson_i2c_probe()
475 i2c->data = (const struct meson_i2c_data *) in meson_i2c_probe()
476 of_device_get_match_data(&pdev->dev); in meson_i2c_probe()
478 i2c->clk = devm_clk_get(&pdev->dev, NULL); in meson_i2c_probe()
479 if (IS_ERR(i2c->clk)) { in meson_i2c_probe()
480 dev_err(&pdev->dev, "can't get device clock\n"); in meson_i2c_probe()
481 return PTR_ERR(i2c->clk); in meson_i2c_probe()
484 i2c->regs = devm_platform_ioremap_resource(pdev, 0); in meson_i2c_probe()
485 if (IS_ERR(i2c->regs)) in meson_i2c_probe()
486 return PTR_ERR(i2c->regs); in meson_i2c_probe()
492 ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c); in meson_i2c_probe()
494 dev_err(&pdev->dev, "can't request IRQ\n"); in meson_i2c_probe()
498 ret = clk_prepare_enable(i2c->clk); in meson_i2c_probe()
500 dev_err(&pdev->dev, "can't prepare clock\n"); in meson_i2c_probe()
504 strscpy(i2c->adap.name, "Meson I2C adapter", in meson_i2c_probe()
505 sizeof(i2c->adap.name)); in meson_i2c_probe()
506 i2c->adap.owner = THIS_MODULE; in meson_i2c_probe()
507 i2c->adap.algo = &meson_i2c_algorithm; in meson_i2c_probe()
508 i2c->adap.dev.parent = &pdev->dev; in meson_i2c_probe()
509 i2c->adap.dev.of_node = np; in meson_i2c_probe()
510 i2c->adap.algo_data = i2c; in meson_i2c_probe()
513 * A transfer is triggered when START bit changes from 0 to 1. in meson_i2c_probe()
516 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_probe()
519 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, in meson_i2c_probe()
522 if (!i2c->data->set_clk_div) { in meson_i2c_probe()
523 clk_disable_unprepare(i2c->clk); in meson_i2c_probe()
524 return -EINVAL; in meson_i2c_probe()
526 i2c->data->set_clk_div(i2c, timings.bus_freq_hz); in meson_i2c_probe()
528 ret = i2c_add_adapter(&i2c->adap); in meson_i2c_probe()
530 clk_disable_unprepare(i2c->clk); in meson_i2c_probe()
539 struct meson_i2c *i2c = platform_get_drvdata(pdev); in meson_i2c_remove() local
541 i2c_del_adapter(&i2c->adap); in meson_i2c_remove()
542 clk_disable_unprepare(i2c->clk); in meson_i2c_remove()
558 { .compatible = "amlogic,meson6-i2c", .data = &i2c_meson6_data },
559 { .compatible = "amlogic,meson-gxbb-i2c", .data = &i2c_gxbb_data },
560 { .compatible = "amlogic,meson-axg-i2c", .data = &i2c_axg_data },
570 .name = "meson-i2c",
577 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");