Lines Matching full:i2c

3  * I2C bus driver for Amlogic Meson SoCs
11 #include <linux/i2c.h>
22 /* Meson I2C register map */
69 * struct meson_i2c - Meson I2C device private data
71 * @adap: I2C adapter instance
75 * @msg: Pointer to the current I2C message
109 void (*set_clk_div)(struct meson_i2c *i2c, unsigned int freq);
112 static void meson_i2c_set_mask(struct meson_i2c *i2c, int reg, u32 mask, in meson_i2c_set_mask() argument
117 data = readl(i2c->regs + reg); in meson_i2c_set_mask()
120 writel(data, i2c->regs + reg); in meson_i2c_set_mask()
123 static void meson_i2c_reset_tokens(struct meson_i2c *i2c) in meson_i2c_reset_tokens() argument
125 i2c->tokens[0] = 0; in meson_i2c_reset_tokens()
126 i2c->tokens[1] = 0; in meson_i2c_reset_tokens()
127 i2c->num_tokens = 0; in meson_i2c_reset_tokens()
130 static void meson_i2c_add_token(struct meson_i2c *i2c, int token) in meson_i2c_add_token() argument
132 if (i2c->num_tokens < 8) in meson_i2c_add_token()
133 i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4); in meson_i2c_add_token()
135 i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4); in meson_i2c_add_token()
137 i2c->num_tokens++; in meson_i2c_add_token()
140 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
142 unsigned long clk_rate = clk_get_rate(i2c->clk); in meson_gxbb_axg_i2c_set_clk_div()
145 /* 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()
163 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson_gxbb_axg_i2c_set_clk_div()
167 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson_gxbb_axg_i2c_set_clk_div()
171 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK, in meson_gxbb_axg_i2c_set_clk_div()
174 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK, in meson_gxbb_axg_i2c_set_clk_div()
178 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_MASK, in meson_gxbb_axg_i2c_set_clk_div()
182 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()
184 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, divh %u, divl %u\n", __func__, in meson_gxbb_axg_i2c_set_clk_div()
188 static void meson6_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq) in meson6_i2c_set_clk_div() argument
190 unsigned long clk_rate = clk_get_rate(i2c->clk); in meson6_i2c_set_clk_div()
199 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson6_i2c_set_clk_div()
203 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK, in meson6_i2c_set_clk_div()
206 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK, in meson6_i2c_set_clk_div()
210 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0); in meson6_i2c_set_clk_div()
212 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__, in meson6_i2c_set_clk_div()
216 static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len) in meson_i2c_get_data() argument
221 rdata0 = readl(i2c->regs + REG_TOK_RDATA0); in meson_i2c_get_data()
222 rdata1 = readl(i2c->regs + REG_TOK_RDATA1); in meson_i2c_get_data()
224 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__, in meson_i2c_get_data()
234 static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len) in meson_i2c_put_data() argument
245 writel(wdata0, i2c->regs + REG_TOK_WDATA0); in meson_i2c_put_data()
246 writel(wdata1, i2c->regs + REG_TOK_WDATA1); in meson_i2c_put_data()
248 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__, in meson_i2c_put_data()
252 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c) in meson_i2c_prepare_xfer() argument
254 bool write = !(i2c->msg->flags & I2C_M_RD); in meson_i2c_prepare_xfer()
257 i2c->count = min(i2c->msg->len - i2c->pos, 8); in meson_i2c_prepare_xfer()
259 for (i = 0; i < i2c->count - 1; i++) in meson_i2c_prepare_xfer()
260 meson_i2c_add_token(i2c, TOKEN_DATA); in meson_i2c_prepare_xfer()
262 if (i2c->count) { in meson_i2c_prepare_xfer()
263 if (write || i2c->pos + i2c->count < i2c->msg->len) in meson_i2c_prepare_xfer()
264 meson_i2c_add_token(i2c, TOKEN_DATA); in meson_i2c_prepare_xfer()
266 meson_i2c_add_token(i2c, TOKEN_DATA_LAST); in meson_i2c_prepare_xfer()
270 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count); in meson_i2c_prepare_xfer()
272 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len) in meson_i2c_prepare_xfer()
273 meson_i2c_add_token(i2c, TOKEN_STOP); in meson_i2c_prepare_xfer()
275 writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0); in meson_i2c_prepare_xfer()
276 writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1); in meson_i2c_prepare_xfer()
279 static void meson_i2c_transfer_complete(struct meson_i2c *i2c, u32 ctrl) in meson_i2c_transfer_complete() argument
285 * I2C controller automatically generates a STOP in meson_i2c_transfer_complete()
288 dev_dbg(i2c->dev, "error bit set\n"); in meson_i2c_transfer_complete()
289 i2c->error = -ENXIO; in meson_i2c_transfer_complete()
290 i2c->state = STATE_IDLE; in meson_i2c_transfer_complete()
292 if (i2c->state == STATE_READ && i2c->count) in meson_i2c_transfer_complete()
293 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos, in meson_i2c_transfer_complete()
294 i2c->count); in meson_i2c_transfer_complete()
296 i2c->pos += i2c->count; in meson_i2c_transfer_complete()
298 if (i2c->pos >= i2c->msg->len) in meson_i2c_transfer_complete()
299 i2c->state = STATE_IDLE; in meson_i2c_transfer_complete()
305 struct meson_i2c *i2c = dev_id; in meson_i2c_irq() local
308 spin_lock(&i2c->lock); in meson_i2c_irq()
310 meson_i2c_reset_tokens(i2c); in meson_i2c_irq()
311 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_irq()
312 ctrl = readl(i2c->regs + REG_CTRL); in meson_i2c_irq()
314 dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n", in meson_i2c_irq()
315 i2c->state, i2c->pos, i2c->count, ctrl); in meson_i2c_irq()
317 if (i2c->state == STATE_IDLE) { in meson_i2c_irq()
318 spin_unlock(&i2c->lock); in meson_i2c_irq()
322 meson_i2c_transfer_complete(i2c, ctrl); in meson_i2c_irq()
324 if (i2c->state == STATE_IDLE) { in meson_i2c_irq()
325 complete(&i2c->done); in meson_i2c_irq()
330 meson_i2c_prepare_xfer(i2c); in meson_i2c_irq()
331 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START); in meson_i2c_irq()
333 spin_unlock(&i2c->lock); in meson_i2c_irq()
338 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg) in meson_i2c_do_start() argument
346 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR_MASK, in meson_i2c_do_start()
349 meson_i2c_add_token(i2c, TOKEN_START); in meson_i2c_do_start()
350 meson_i2c_add_token(i2c, token); in meson_i2c_do_start()
353 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg, in meson_i2c_xfer_msg() argument
360 i2c->msg = msg; in meson_i2c_xfer_msg()
361 i2c->last = last; in meson_i2c_xfer_msg()
362 i2c->pos = 0; in meson_i2c_xfer_msg()
363 i2c->count = 0; in meson_i2c_xfer_msg()
364 i2c->error = 0; in meson_i2c_xfer_msg()
366 meson_i2c_reset_tokens(i2c); in meson_i2c_xfer_msg()
369 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags); in meson_i2c_xfer_msg()
372 meson_i2c_do_start(i2c, msg); in meson_i2c_xfer_msg()
374 i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; in meson_i2c_xfer_msg()
375 meson_i2c_prepare_xfer(i2c); in meson_i2c_xfer_msg()
378 reinit_completion(&i2c->done); in meson_i2c_xfer_msg()
381 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START); in meson_i2c_xfer_msg()
384 ret = readl_poll_timeout_atomic(i2c->regs + REG_CTRL, ctrl, in meson_i2c_xfer_msg()
389 time_left = wait_for_completion_timeout(&i2c->done, time_left); in meson_i2c_xfer_msg()
396 * Protect access to i2c struct and registers from interrupt in meson_i2c_xfer_msg()
400 spin_lock_irqsave(&i2c->lock, flags); in meson_i2c_xfer_msg()
403 meson_i2c_transfer_complete(i2c, ctrl); in meson_i2c_xfer_msg()
406 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_xfer_msg()
409 i2c->state = STATE_IDLE; in meson_i2c_xfer_msg()
411 if (i2c->error) in meson_i2c_xfer_msg()
412 ret = i2c->error; in meson_i2c_xfer_msg()
414 spin_unlock_irqrestore(&i2c->lock, flags); in meson_i2c_xfer_msg()
422 struct meson_i2c *i2c = adap->algo_data; in meson_i2c_xfer_messages() local
426 ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1, atomic); in meson_i2c_xfer_messages()
460 struct meson_i2c *i2c; in meson_i2c_probe() local
464 i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL); in meson_i2c_probe()
465 if (!i2c) in meson_i2c_probe()
470 i2c->dev = &pdev->dev; in meson_i2c_probe()
471 platform_set_drvdata(pdev, i2c); in meson_i2c_probe()
473 spin_lock_init(&i2c->lock); in meson_i2c_probe()
474 init_completion(&i2c->done); in meson_i2c_probe()
476 i2c->data = (const struct meson_i2c_data *) in meson_i2c_probe()
479 i2c->clk = devm_clk_get(&pdev->dev, NULL); in meson_i2c_probe()
480 if (IS_ERR(i2c->clk)) { in meson_i2c_probe()
482 return PTR_ERR(i2c->clk); in meson_i2c_probe()
485 i2c->regs = devm_platform_ioremap_resource(pdev, 0); in meson_i2c_probe()
486 if (IS_ERR(i2c->regs)) in meson_i2c_probe()
487 return PTR_ERR(i2c->regs); in meson_i2c_probe()
493 ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c); in meson_i2c_probe()
499 ret = clk_prepare_enable(i2c->clk); in meson_i2c_probe()
505 strscpy(i2c->adap.name, "Meson I2C adapter", in meson_i2c_probe()
506 sizeof(i2c->adap.name)); in meson_i2c_probe()
507 i2c->adap.owner = THIS_MODULE; in meson_i2c_probe()
508 i2c->adap.algo = &meson_i2c_algorithm; in meson_i2c_probe()
509 i2c->adap.dev.parent = &pdev->dev; in meson_i2c_probe()
510 i2c->adap.dev.of_node = np; in meson_i2c_probe()
511 i2c->adap.algo_data = i2c; in meson_i2c_probe()
517 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_probe()
520 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, in meson_i2c_probe()
523 if (!i2c->data->set_clk_div) { in meson_i2c_probe()
524 clk_disable_unprepare(i2c->clk); in meson_i2c_probe()
527 i2c->data->set_clk_div(i2c, timings.bus_freq_hz); in meson_i2c_probe()
529 ret = i2c_add_adapter(&i2c->adap); in meson_i2c_probe()
531 clk_disable_unprepare(i2c->clk); in meson_i2c_probe()
540 struct meson_i2c *i2c = platform_get_drvdata(pdev); in meson_i2c_remove() local
542 i2c_del_adapter(&i2c->adap); in meson_i2c_remove()
543 clk_disable_unprepare(i2c->clk); in meson_i2c_remove()
561 { .compatible = "amlogic,meson6-i2c", .data = &i2c_meson6_data },
562 { .compatible = "amlogic,meson-gxbb-i2c", .data = &i2c_gxbb_data },
563 { .compatible = "amlogic,meson-axg-i2c", .data = &i2c_axg_data },
573 .name = "meson-i2c",
580 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");