1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C bus driver for Amlogic Meson SoCs
5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
8 #include <linux/bitfield.h>
10 #include <linux/completion.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/types.h>
21 /* Meson I2C register map */
23 #define REG_SLAVE_ADDR 0x04
24 #define REG_TOK_LIST0 0x08
25 #define REG_TOK_LIST1 0x0c
26 #define REG_TOK_WDATA0 0x10
27 #define REG_TOK_WDATA1 0x14
28 #define REG_TOK_RDATA0 0x18
29 #define REG_TOK_RDATA1 0x1c
31 /* Control register fields */
32 #define REG_CTRL_START BIT(0)
33 #define REG_CTRL_ACK_IGNORE BIT(1)
34 #define REG_CTRL_STATUS BIT(2)
35 #define REG_CTRL_ERROR BIT(3)
36 #define REG_CTRL_CLKDIV_SHIFT 12
37 #define REG_CTRL_CLKDIV_MASK GENMASK(21, REG_CTRL_CLKDIV_SHIFT)
38 #define REG_CTRL_CLKDIVEXT_SHIFT 28
39 #define REG_CTRL_CLKDIVEXT_MASK GENMASK(29, REG_CTRL_CLKDIVEXT_SHIFT)
41 #define REG_SLV_ADDR_MASK GENMASK(7, 0)
42 #define REG_SLV_SDA_FILTER_MASK GENMASK(10, 8)
43 #define REG_SLV_SCL_FILTER_MASK GENMASK(13, 11)
44 #define REG_SLV_SCL_LOW_SHIFT 16
45 #define REG_SLV_SCL_LOW_MASK GENMASK(27, REG_SLV_SCL_LOW_SHIFT)
46 #define REG_SLV_SCL_LOW_EN BIT(28)
48 #define I2C_TIMEOUT_MS 500
49 #define FILTER_DELAY 15
54 TOKEN_SLAVE_ADDR_WRITE,
55 TOKEN_SLAVE_ADDR_READ,
68 * struct meson_i2c - Meson I2C device private data
70 * @adap: I2C adapter instance
71 * @dev: Pointer to device structure
72 * @regs: Base address of the device memory mapped registers
73 * @clk: Pointer to clock structure
74 * @msg: Pointer to the current I2C message
75 * @state: Current state in the driver state machine
76 * @last: Flag set for the last message in the transfer
77 * @count: Number of bytes to be sent/received in current transfer
78 * @pos: Current position in the send/receive buffer
79 * @error: Flag set when an error is received
80 * @lock: To avoid race conditions between irq handler and xfer code
81 * @done: Completion used to wait for transfer termination
82 * @tokens: Sequence of tokens to be written to the device
83 * @num_tokens: Number of tokens
84 * @data: Pointer to the controller's platform data
87 struct i2c_adapter adap;
100 struct completion done;
104 const struct meson_i2c_data *data;
107 struct meson_i2c_data {
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,
116 data = readl(i2c->regs + reg);
119 writel(data, i2c->regs + reg);
122 static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
129 static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
131 if (i2c->num_tokens < 8)
132 i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
134 i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
139 static void meson_gxbb_axg_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
141 unsigned long clk_rate = clk_get_rate(i2c->clk);
142 unsigned int div_h, div_l;
144 /* According to I2C-BUS Spec 2.1, in FAST-MODE, the minimum LOW period is 1.3uS, and
145 * minimum HIGH is least 0.6us.
146 * For 400000 freq, the period is 2.5us. To keep within the specs, give 40% of period to
147 * HIGH and 60% to LOW. This means HIGH at 1.0us and LOW 1.5us.
148 * The same applies for Fast-mode plus, where LOW is 0.5us and HIGH is 0.26us.
149 * Duty = H/(H + L) = 2/5
151 if (freq <= I2C_MAX_STANDARD_MODE_FREQ) {
152 div_h = DIV_ROUND_UP(clk_rate, freq);
153 div_l = DIV_ROUND_UP(div_h, 4);
154 div_h = DIV_ROUND_UP(div_h, 2) - FILTER_DELAY;
156 div_h = DIV_ROUND_UP(clk_rate * 2, freq * 5) - FILTER_DELAY;
157 div_l = DIV_ROUND_UP(clk_rate * 3, freq * 5 * 2);
160 /* clock divider has 12 bits */
161 if (div_h > GENMASK(11, 0)) {
162 dev_err(i2c->dev, "requested bus frequency too low\n");
163 div_h = GENMASK(11, 0);
165 if (div_l > GENMASK(11, 0)) {
166 dev_err(i2c->dev, "requested bus frequency too low\n");
167 div_l = GENMASK(11, 0);
170 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
171 FIELD_PREP(REG_CTRL_CLKDIV_MASK, div_h & GENMASK(9, 0)));
173 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK,
174 FIELD_PREP(REG_CTRL_CLKDIVEXT_MASK, div_h >> 10));
176 /* set SCL low delay */
177 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_MASK,
178 FIELD_PREP(REG_SLV_SCL_LOW_MASK, div_l));
180 /* Enable HIGH/LOW mode */
181 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, REG_SLV_SCL_LOW_EN);
183 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, divh %u, divl %u\n", __func__,
184 clk_rate, freq, div_h, div_l);
187 static void meson6_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
189 unsigned long clk_rate = clk_get_rate(i2c->clk);
192 div = DIV_ROUND_UP(clk_rate, freq);
194 div = DIV_ROUND_UP(div, 4);
196 /* clock divider has 12 bits */
197 if (div > GENMASK(11, 0)) {
198 dev_err(i2c->dev, "requested bus frequency too low\n");
199 div = GENMASK(11, 0);
202 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
203 FIELD_PREP(REG_CTRL_CLKDIV_MASK, div & GENMASK(9, 0)));
205 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK,
206 FIELD_PREP(REG_CTRL_CLKDIVEXT_MASK, div >> 10));
208 /* Disable HIGH/LOW mode */
209 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0);
211 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__,
212 clk_rate, freq, div);
215 static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
220 rdata0 = readl(i2c->regs + REG_TOK_RDATA0);
221 rdata1 = readl(i2c->regs + REG_TOK_RDATA1);
223 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
224 rdata0, rdata1, len);
226 for (i = 0; i < min(4, len); i++)
227 *buf++ = (rdata0 >> i * 8) & 0xff;
229 for (i = 4; i < min(8, len); i++)
230 *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
233 static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len)
235 u32 wdata0 = 0, wdata1 = 0;
238 for (i = 0; i < min(4, len); i++)
239 wdata0 |= *buf++ << (i * 8);
241 for (i = 4; i < min(8, len); i++)
242 wdata1 |= *buf++ << ((i - 4) * 8);
244 writel(wdata0, i2c->regs + REG_TOK_WDATA0);
245 writel(wdata1, i2c->regs + REG_TOK_WDATA1);
247 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
248 wdata0, wdata1, len);
251 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
253 bool write = !(i2c->msg->flags & I2C_M_RD);
256 i2c->count = min(i2c->msg->len - i2c->pos, 8);
258 for (i = 0; i < i2c->count - 1; i++)
259 meson_i2c_add_token(i2c, TOKEN_DATA);
262 if (write || i2c->pos + i2c->count < i2c->msg->len)
263 meson_i2c_add_token(i2c, TOKEN_DATA);
265 meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
269 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
271 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
272 meson_i2c_add_token(i2c, TOKEN_STOP);
274 writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0);
275 writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1);
278 static void meson_i2c_transfer_complete(struct meson_i2c *i2c, u32 ctrl)
280 if (ctrl & REG_CTRL_ERROR) {
282 * The bit is set when the IGNORE_NAK bit is cleared
283 * and the device didn't respond. In this case, the
284 * I2C controller automatically generates a STOP
287 dev_dbg(i2c->dev, "error bit set\n");
289 i2c->state = STATE_IDLE;
291 if (i2c->state == STATE_READ && i2c->count)
292 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
295 i2c->pos += i2c->count;
297 if (i2c->pos >= i2c->msg->len)
298 i2c->state = STATE_IDLE;
302 static irqreturn_t meson_i2c_irq(int irqno, void *dev_id)
304 struct meson_i2c *i2c = dev_id;
307 spin_lock(&i2c->lock);
309 meson_i2c_reset_tokens(i2c);
310 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
311 ctrl = readl(i2c->regs + REG_CTRL);
313 dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n",
314 i2c->state, i2c->pos, i2c->count, ctrl);
316 if (i2c->state == STATE_IDLE) {
317 spin_unlock(&i2c->lock);
321 meson_i2c_transfer_complete(i2c, ctrl);
323 if (i2c->state == STATE_IDLE) {
324 complete(&i2c->done);
328 /* Restart the processing */
329 meson_i2c_prepare_xfer(i2c);
330 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
332 spin_unlock(&i2c->lock);
337 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
341 token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
342 TOKEN_SLAVE_ADDR_WRITE;
345 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR_MASK,
346 FIELD_PREP(REG_SLV_ADDR_MASK, msg->addr << 1));
348 meson_i2c_add_token(i2c, TOKEN_START);
349 meson_i2c_add_token(i2c, token);
352 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
353 int last, bool atomic)
355 unsigned long time_left, flags;
365 meson_i2c_reset_tokens(i2c);
367 flags = (msg->flags & I2C_M_IGNORE_NAK) ? REG_CTRL_ACK_IGNORE : 0;
368 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags);
370 if (!(msg->flags & I2C_M_NOSTART))
371 meson_i2c_do_start(i2c, msg);
373 i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
374 meson_i2c_prepare_xfer(i2c);
377 reinit_completion(&i2c->done);
379 /* Start the transfer */
380 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
383 ret = readl_poll_timeout_atomic(i2c->regs + REG_CTRL, ctrl,
384 !(ctrl & REG_CTRL_STATUS),
385 10, I2C_TIMEOUT_MS * 1000);
387 time_left = msecs_to_jiffies(I2C_TIMEOUT_MS);
388 time_left = wait_for_completion_timeout(&i2c->done, time_left);
395 * Protect access to i2c struct and registers from interrupt
396 * handlers triggered by a transfer terminated after the
399 spin_lock_irqsave(&i2c->lock, flags);
402 meson_i2c_transfer_complete(i2c, ctrl);
404 /* Abort any active operation */
405 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
408 i2c->state = STATE_IDLE;
413 spin_unlock_irqrestore(&i2c->lock, flags);
418 static int meson_i2c_xfer_messages(struct i2c_adapter *adap,
419 struct i2c_msg *msgs, int num, bool atomic)
421 struct meson_i2c *i2c = adap->algo_data;
424 for (i = 0; i < num; i++) {
425 ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1, atomic);
433 static int meson_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
436 return meson_i2c_xfer_messages(adap, msgs, num, false);
439 static int meson_i2c_xfer_atomic(struct i2c_adapter *adap,
440 struct i2c_msg *msgs, int num)
442 return meson_i2c_xfer_messages(adap, msgs, num, true);
445 static u32 meson_i2c_func(struct i2c_adapter *adap)
447 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
450 static const struct i2c_algorithm meson_i2c_algorithm = {
451 .master_xfer = meson_i2c_xfer,
452 .master_xfer_atomic = meson_i2c_xfer_atomic,
453 .functionality = meson_i2c_func,
456 static int meson_i2c_probe(struct platform_device *pdev)
458 struct device_node *np = pdev->dev.of_node;
459 struct meson_i2c *i2c;
460 struct i2c_timings timings;
463 i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL);
467 i2c_parse_fw_timings(&pdev->dev, &timings, true);
469 i2c->dev = &pdev->dev;
470 platform_set_drvdata(pdev, i2c);
472 spin_lock_init(&i2c->lock);
473 init_completion(&i2c->done);
475 i2c->data = (const struct meson_i2c_data *)
476 of_device_get_match_data(&pdev->dev);
478 i2c->clk = devm_clk_get(&pdev->dev, NULL);
479 if (IS_ERR(i2c->clk)) {
480 dev_err(&pdev->dev, "can't get device clock\n");
481 return PTR_ERR(i2c->clk);
484 i2c->regs = devm_platform_ioremap_resource(pdev, 0);
485 if (IS_ERR(i2c->regs))
486 return PTR_ERR(i2c->regs);
488 irq = platform_get_irq(pdev, 0);
492 ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c);
494 dev_err(&pdev->dev, "can't request IRQ\n");
498 ret = clk_prepare_enable(i2c->clk);
500 dev_err(&pdev->dev, "can't prepare clock\n");
504 strscpy(i2c->adap.name, "Meson I2C adapter",
505 sizeof(i2c->adap.name));
506 i2c->adap.owner = THIS_MODULE;
507 i2c->adap.algo = &meson_i2c_algorithm;
508 i2c->adap.dev.parent = &pdev->dev;
509 i2c->adap.dev.of_node = np;
510 i2c->adap.algo_data = i2c;
513 * A transfer is triggered when START bit changes from 0 to 1.
514 * Ensure that the bit is set to 0 after probe
516 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
518 /* Disable filtering */
519 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR,
520 REG_SLV_SDA_FILTER_MASK | REG_SLV_SCL_FILTER_MASK, 0);
522 if (!i2c->data->set_clk_div) {
523 clk_disable_unprepare(i2c->clk);
526 i2c->data->set_clk_div(i2c, timings.bus_freq_hz);
528 ret = i2c_add_adapter(&i2c->adap);
530 clk_disable_unprepare(i2c->clk);
537 static void meson_i2c_remove(struct platform_device *pdev)
539 struct meson_i2c *i2c = platform_get_drvdata(pdev);
541 i2c_del_adapter(&i2c->adap);
542 clk_disable_unprepare(i2c->clk);
545 static const struct meson_i2c_data i2c_meson6_data = {
546 .set_clk_div = meson6_i2c_set_clk_div,
549 static const struct meson_i2c_data i2c_gxbb_data = {
550 .set_clk_div = meson_gxbb_axg_i2c_set_clk_div,
553 static const struct meson_i2c_data i2c_axg_data = {
554 .set_clk_div = meson_gxbb_axg_i2c_set_clk_div,
557 static const struct of_device_id meson_i2c_match[] = {
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 },
564 MODULE_DEVICE_TABLE(of, meson_i2c_match);
566 static struct platform_driver meson_i2c_driver = {
567 .probe = meson_i2c_probe,
568 .remove_new = meson_i2c_remove,
571 .of_match_table = meson_i2c_match,
575 module_platform_driver(meson_i2c_driver);
577 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");
578 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
579 MODULE_LICENSE("GPL v2");