Lines Matching full:i2c
3 * I2C bus driver for Amlogic Meson SoCs
11 #include <linux/i2c.h>
22 /* Meson I2C register map */
70 * struct meson_i2c - Meson I2C device private data
72 * @adap: I2C adapter instance
76 * @msg: Pointer to the current I2C message
109 static void meson_i2c_set_mask(struct meson_i2c *i2c, int reg, u32 mask, in meson_i2c_set_mask() argument
114 data = readl(i2c->regs + reg); in meson_i2c_set_mask()
117 writel(data, i2c->regs + reg); in meson_i2c_set_mask()
120 static void meson_i2c_reset_tokens(struct meson_i2c *i2c) in meson_i2c_reset_tokens() argument
122 i2c->tokens[0] = 0; in meson_i2c_reset_tokens()
123 i2c->tokens[1] = 0; in meson_i2c_reset_tokens()
124 i2c->num_tokens = 0; in meson_i2c_reset_tokens()
127 static void meson_i2c_add_token(struct meson_i2c *i2c, int token) in meson_i2c_add_token() argument
129 if (i2c->num_tokens < 8) in meson_i2c_add_token()
130 i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4); in meson_i2c_add_token()
132 i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4); in meson_i2c_add_token()
134 i2c->num_tokens++; in meson_i2c_add_token()
137 static void meson_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq) in meson_i2c_set_clk_div() argument
139 unsigned long clk_rate = clk_get_rate(i2c->clk); in meson_i2c_set_clk_div()
144 div = DIV_ROUND_UP(div, i2c->data->div_factor); in meson_i2c_set_clk_div()
148 dev_err(i2c->dev, "requested bus frequency too low\n"); in meson_i2c_set_clk_div()
152 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV, in meson_i2c_set_clk_div()
155 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT, in meson_i2c_set_clk_div()
159 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0); in meson_i2c_set_clk_div()
161 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__, in meson_i2c_set_clk_div()
165 static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len) in meson_i2c_get_data() argument
170 rdata0 = readl(i2c->regs + REG_TOK_RDATA0); in meson_i2c_get_data()
171 rdata1 = readl(i2c->regs + REG_TOK_RDATA1); in meson_i2c_get_data()
173 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__, in meson_i2c_get_data()
183 static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len) in meson_i2c_put_data() argument
194 writel(wdata0, i2c->regs + REG_TOK_WDATA0); in meson_i2c_put_data()
195 writel(wdata1, i2c->regs + REG_TOK_WDATA1); in meson_i2c_put_data()
197 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__, in meson_i2c_put_data()
201 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c) in meson_i2c_prepare_xfer() argument
203 bool write = !(i2c->msg->flags & I2C_M_RD); in meson_i2c_prepare_xfer()
206 i2c->count = min(i2c->msg->len - i2c->pos, 8); in meson_i2c_prepare_xfer()
208 for (i = 0; i < i2c->count - 1; i++) in meson_i2c_prepare_xfer()
209 meson_i2c_add_token(i2c, TOKEN_DATA); in meson_i2c_prepare_xfer()
211 if (i2c->count) { in meson_i2c_prepare_xfer()
212 if (write || i2c->pos + i2c->count < i2c->msg->len) in meson_i2c_prepare_xfer()
213 meson_i2c_add_token(i2c, TOKEN_DATA); in meson_i2c_prepare_xfer()
215 meson_i2c_add_token(i2c, TOKEN_DATA_LAST); in meson_i2c_prepare_xfer()
219 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count); in meson_i2c_prepare_xfer()
221 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len) in meson_i2c_prepare_xfer()
222 meson_i2c_add_token(i2c, TOKEN_STOP); in meson_i2c_prepare_xfer()
224 writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0); in meson_i2c_prepare_xfer()
225 writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1); in meson_i2c_prepare_xfer()
228 static void meson_i2c_transfer_complete(struct meson_i2c *i2c, u32 ctrl) in meson_i2c_transfer_complete() argument
234 * I2C controller automatically generates a STOP in meson_i2c_transfer_complete()
237 dev_dbg(i2c->dev, "error bit set\n"); in meson_i2c_transfer_complete()
238 i2c->error = -ENXIO; in meson_i2c_transfer_complete()
239 i2c->state = STATE_IDLE; in meson_i2c_transfer_complete()
241 if (i2c->state == STATE_READ && i2c->count) in meson_i2c_transfer_complete()
242 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos, in meson_i2c_transfer_complete()
243 i2c->count); in meson_i2c_transfer_complete()
245 i2c->pos += i2c->count; in meson_i2c_transfer_complete()
247 if (i2c->pos >= i2c->msg->len) in meson_i2c_transfer_complete()
248 i2c->state = STATE_IDLE; in meson_i2c_transfer_complete()
254 struct meson_i2c *i2c = dev_id; in meson_i2c_irq() local
257 spin_lock(&i2c->lock); in meson_i2c_irq()
259 meson_i2c_reset_tokens(i2c); in meson_i2c_irq()
260 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_irq()
261 ctrl = readl(i2c->regs + REG_CTRL); in meson_i2c_irq()
263 dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n", in meson_i2c_irq()
264 i2c->state, i2c->pos, i2c->count, ctrl); in meson_i2c_irq()
266 if (i2c->state == STATE_IDLE) { in meson_i2c_irq()
267 spin_unlock(&i2c->lock); in meson_i2c_irq()
271 meson_i2c_transfer_complete(i2c, ctrl); in meson_i2c_irq()
273 if (i2c->state == STATE_IDLE) { in meson_i2c_irq()
274 complete(&i2c->done); in meson_i2c_irq()
279 meson_i2c_prepare_xfer(i2c); in meson_i2c_irq()
280 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START); in meson_i2c_irq()
282 spin_unlock(&i2c->lock); in meson_i2c_irq()
287 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg) in meson_i2c_do_start() argument
295 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR, in meson_i2c_do_start()
298 meson_i2c_add_token(i2c, TOKEN_START); in meson_i2c_do_start()
299 meson_i2c_add_token(i2c, token); in meson_i2c_do_start()
302 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg, in meson_i2c_xfer_msg() argument
309 i2c->msg = msg; in meson_i2c_xfer_msg()
310 i2c->last = last; in meson_i2c_xfer_msg()
311 i2c->pos = 0; in meson_i2c_xfer_msg()
312 i2c->count = 0; in meson_i2c_xfer_msg()
313 i2c->error = 0; in meson_i2c_xfer_msg()
315 meson_i2c_reset_tokens(i2c); in meson_i2c_xfer_msg()
318 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags); in meson_i2c_xfer_msg()
321 meson_i2c_do_start(i2c, msg); in meson_i2c_xfer_msg()
323 i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; in meson_i2c_xfer_msg()
324 meson_i2c_prepare_xfer(i2c); in meson_i2c_xfer_msg()
327 reinit_completion(&i2c->done); in meson_i2c_xfer_msg()
330 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START); in meson_i2c_xfer_msg()
333 ret = readl_poll_timeout_atomic(i2c->regs + REG_CTRL, ctrl, in meson_i2c_xfer_msg()
338 time_left = wait_for_completion_timeout(&i2c->done, time_left); in meson_i2c_xfer_msg()
345 * Protect access to i2c struct and registers from interrupt in meson_i2c_xfer_msg()
349 spin_lock_irqsave(&i2c->lock, flags); in meson_i2c_xfer_msg()
352 meson_i2c_transfer_complete(i2c, ctrl); in meson_i2c_xfer_msg()
355 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_xfer_msg()
358 i2c->state = STATE_IDLE; in meson_i2c_xfer_msg()
360 if (i2c->error) in meson_i2c_xfer_msg()
361 ret = i2c->error; in meson_i2c_xfer_msg()
363 spin_unlock_irqrestore(&i2c->lock, flags); in meson_i2c_xfer_msg()
371 struct meson_i2c *i2c = adap->algo_data; in meson_i2c_xfer_messages() local
375 ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1, atomic); in meson_i2c_xfer_messages()
409 struct meson_i2c *i2c; in meson_i2c_probe() local
413 i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL); in meson_i2c_probe()
414 if (!i2c) in meson_i2c_probe()
419 i2c->dev = &pdev->dev; in meson_i2c_probe()
420 platform_set_drvdata(pdev, i2c); in meson_i2c_probe()
422 spin_lock_init(&i2c->lock); in meson_i2c_probe()
423 init_completion(&i2c->done); in meson_i2c_probe()
425 i2c->data = (const struct meson_i2c_data *) in meson_i2c_probe()
428 i2c->clk = devm_clk_get(&pdev->dev, NULL); in meson_i2c_probe()
429 if (IS_ERR(i2c->clk)) { in meson_i2c_probe()
431 return PTR_ERR(i2c->clk); in meson_i2c_probe()
434 i2c->regs = devm_platform_ioremap_resource(pdev, 0); in meson_i2c_probe()
435 if (IS_ERR(i2c->regs)) in meson_i2c_probe()
436 return PTR_ERR(i2c->regs); in meson_i2c_probe()
442 ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c); in meson_i2c_probe()
448 ret = clk_prepare_enable(i2c->clk); in meson_i2c_probe()
454 strlcpy(i2c->adap.name, "Meson I2C adapter", in meson_i2c_probe()
455 sizeof(i2c->adap.name)); in meson_i2c_probe()
456 i2c->adap.owner = THIS_MODULE; in meson_i2c_probe()
457 i2c->adap.algo = &meson_i2c_algorithm; in meson_i2c_probe()
458 i2c->adap.dev.parent = &pdev->dev; in meson_i2c_probe()
459 i2c->adap.dev.of_node = np; in meson_i2c_probe()
460 i2c->adap.algo_data = i2c; in meson_i2c_probe()
466 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0); in meson_i2c_probe()
468 ret = i2c_add_adapter(&i2c->adap); in meson_i2c_probe()
470 clk_disable_unprepare(i2c->clk); in meson_i2c_probe()
475 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, in meson_i2c_probe()
478 meson_i2c_set_clk_div(i2c, timings.bus_freq_hz); in meson_i2c_probe()
485 struct meson_i2c *i2c = platform_get_drvdata(pdev); in meson_i2c_remove() local
487 i2c_del_adapter(&i2c->adap); in meson_i2c_remove()
488 clk_disable_unprepare(i2c->clk); in meson_i2c_remove()
506 { .compatible = "amlogic,meson6-i2c", .data = &i2c_meson6_data },
507 { .compatible = "amlogic,meson-gxbb-i2c", .data = &i2c_gxbb_data },
508 { .compatible = "amlogic,meson-axg-i2c", .data = &i2c_axg_data },
518 .name = "meson-i2c",
525 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");