1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C bus driver for Amlogic Meson SoCs
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/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/types.h>
22 /* Meson I2C register map */
24 #define REG_SLAVE_ADDR 0x04
25 #define REG_TOK_LIST0 0x08
26 #define REG_TOK_LIST1 0x0c
27 #define REG_TOK_WDATA0 0x10
28 #define REG_TOK_WDATA1 0x14
29 #define REG_TOK_RDATA0 0x18
30 #define REG_TOK_RDATA1 0x1c
32 /* Control register fields */
33 #define REG_CTRL_START BIT(0)
34 #define REG_CTRL_ACK_IGNORE BIT(1)
35 #define REG_CTRL_STATUS BIT(2)
36 #define REG_CTRL_ERROR BIT(3)
37 #define REG_CTRL_CLKDIV_SHIFT 12
38 #define REG_CTRL_CLKDIV_MASK GENMASK(21, REG_CTRL_CLKDIV_SHIFT)
39 #define REG_CTRL_CLKDIVEXT_SHIFT 28
40 #define REG_CTRL_CLKDIVEXT_MASK GENMASK(29, REG_CTRL_CLKDIVEXT_SHIFT)
42 #define REG_SLV_ADDR_MASK GENMASK(7, 0)
43 #define REG_SLV_SDA_FILTER_MASK GENMASK(10, 8)
44 #define REG_SLV_SCL_FILTER_MASK GENMASK(13, 11)
45 #define REG_SLV_SCL_LOW_SHIFT 16
46 #define REG_SLV_SCL_LOW_MASK GENMASK(27, REG_SLV_SCL_LOW_SHIFT)
47 #define REG_SLV_SCL_LOW_EN BIT(28)
49 #define I2C_TIMEOUT_MS 500
50 #define FILTER_DELAY 15
55 TOKEN_SLAVE_ADDR_WRITE,
56 TOKEN_SLAVE_ADDR_READ,
69 * struct meson_i2c - Meson I2C device private data
71 * @adap: I2C adapter instance
72 * @dev: Pointer to device structure
73 * @regs: Base address of the device memory mapped registers
74 * @clk: Pointer to clock structure
75 * @msg: Pointer to the current I2C message
76 * @state: Current state in the driver state machine
77 * @last: Flag set for the last message in the transfer
78 * @count: Number of bytes to be sent/received in current transfer
79 * @pos: Current position in the send/receive buffer
80 * @error: Flag set when an error is received
81 * @lock: To avoid race conditions between irq handler and xfer code
82 * @done: Completion used to wait for transfer termination
83 * @tokens: Sequence of tokens to be written to the device
84 * @num_tokens: Number of tokens
85 * @data: Pointer to the controller's platform data
88 struct i2c_adapter adap;
101 struct completion done;
105 const struct meson_i2c_data *data;
108 struct meson_i2c_data {
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,
117 data = readl(i2c->regs + reg);
120 writel(data, i2c->regs + reg);
123 static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
130 static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
132 if (i2c->num_tokens < 8)
133 i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
135 i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
140 static void meson_gxbb_axg_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
142 unsigned long clk_rate = clk_get_rate(i2c->clk);
143 unsigned int div_h, div_l;
145 /* According to I2C-BUS Spec 2.1, in FAST-MODE, the minimum LOW period is 1.3uS, and
146 * minimum HIGH is least 0.6us.
147 * For 400000 freq, the period is 2.5us. To keep within the specs, give 40% of period to
148 * HIGH and 60% to LOW. This means HIGH at 1.0us and LOW 1.5us.
149 * The same applies for Fast-mode plus, where LOW is 0.5us and HIGH is 0.26us.
150 * Duty = H/(H + L) = 2/5
152 if (freq <= I2C_MAX_STANDARD_MODE_FREQ) {
153 div_h = DIV_ROUND_UP(clk_rate, freq);
154 div_l = DIV_ROUND_UP(div_h, 4);
155 div_h = DIV_ROUND_UP(div_h, 2) - FILTER_DELAY;
157 div_h = DIV_ROUND_UP(clk_rate * 2, freq * 5) - FILTER_DELAY;
158 div_l = DIV_ROUND_UP(clk_rate * 3, freq * 5 * 2);
161 /* clock divider has 12 bits */
162 if (div_h > GENMASK(11, 0)) {
163 dev_err(i2c->dev, "requested bus frequency too low\n");
164 div_h = GENMASK(11, 0);
166 if (div_l > GENMASK(11, 0)) {
167 dev_err(i2c->dev, "requested bus frequency too low\n");
168 div_l = GENMASK(11, 0);
171 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
172 FIELD_PREP(REG_CTRL_CLKDIV_MASK, div_h & GENMASK(9, 0)));
174 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK,
175 FIELD_PREP(REG_CTRL_CLKDIVEXT_MASK, div_h >> 10));
177 /* set SCL low delay */
178 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_MASK,
179 FIELD_PREP(REG_SLV_SCL_LOW_MASK, div_l));
181 /* Enable HIGH/LOW mode */
182 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, REG_SLV_SCL_LOW_EN);
184 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, divh %u, divl %u\n", __func__,
185 clk_rate, freq, div_h, div_l);
188 static void meson6_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
190 unsigned long clk_rate = clk_get_rate(i2c->clk);
193 div = DIV_ROUND_UP(clk_rate, freq);
195 div = DIV_ROUND_UP(div, 4);
197 /* clock divider has 12 bits */
198 if (div > GENMASK(11, 0)) {
199 dev_err(i2c->dev, "requested bus frequency too low\n");
200 div = GENMASK(11, 0);
203 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV_MASK,
204 FIELD_PREP(REG_CTRL_CLKDIV_MASK, div & GENMASK(9, 0)));
206 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT_MASK,
207 FIELD_PREP(REG_CTRL_CLKDIVEXT_MASK, div >> 10));
209 /* Disable HIGH/LOW mode */
210 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0);
212 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__,
213 clk_rate, freq, div);
216 static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
221 rdata0 = readl(i2c->regs + REG_TOK_RDATA0);
222 rdata1 = readl(i2c->regs + REG_TOK_RDATA1);
224 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
225 rdata0, rdata1, len);
227 for (i = 0; i < min(4, len); i++)
228 *buf++ = (rdata0 >> i * 8) & 0xff;
230 for (i = 4; i < min(8, len); i++)
231 *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
234 static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len)
236 u32 wdata0 = 0, wdata1 = 0;
239 for (i = 0; i < min(4, len); i++)
240 wdata0 |= *buf++ << (i * 8);
242 for (i = 4; i < min(8, len); i++)
243 wdata1 |= *buf++ << ((i - 4) * 8);
245 writel(wdata0, i2c->regs + REG_TOK_WDATA0);
246 writel(wdata1, i2c->regs + REG_TOK_WDATA1);
248 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
249 wdata0, wdata1, len);
252 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
254 bool write = !(i2c->msg->flags & I2C_M_RD);
257 i2c->count = min(i2c->msg->len - i2c->pos, 8);
259 for (i = 0; i < i2c->count - 1; i++)
260 meson_i2c_add_token(i2c, TOKEN_DATA);
263 if (write || i2c->pos + i2c->count < i2c->msg->len)
264 meson_i2c_add_token(i2c, TOKEN_DATA);
266 meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
270 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
272 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
273 meson_i2c_add_token(i2c, TOKEN_STOP);
275 writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0);
276 writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1);
279 static void meson_i2c_transfer_complete(struct meson_i2c *i2c, u32 ctrl)
281 if (ctrl & REG_CTRL_ERROR) {
283 * The bit is set when the IGNORE_NAK bit is cleared
284 * and the device didn't respond. In this case, the
285 * I2C controller automatically generates a STOP
288 dev_dbg(i2c->dev, "error bit set\n");
290 i2c->state = STATE_IDLE;
292 if (i2c->state == STATE_READ && i2c->count)
293 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
296 i2c->pos += i2c->count;
298 if (i2c->pos >= i2c->msg->len)
299 i2c->state = STATE_IDLE;
303 static irqreturn_t meson_i2c_irq(int irqno, void *dev_id)
305 struct meson_i2c *i2c = dev_id;
308 spin_lock(&i2c->lock);
310 meson_i2c_reset_tokens(i2c);
311 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
312 ctrl = readl(i2c->regs + REG_CTRL);
314 dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n",
315 i2c->state, i2c->pos, i2c->count, ctrl);
317 if (i2c->state == STATE_IDLE) {
318 spin_unlock(&i2c->lock);
322 meson_i2c_transfer_complete(i2c, ctrl);
324 if (i2c->state == STATE_IDLE) {
325 complete(&i2c->done);
329 /* Restart the processing */
330 meson_i2c_prepare_xfer(i2c);
331 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
333 spin_unlock(&i2c->lock);
338 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
342 token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
343 TOKEN_SLAVE_ADDR_WRITE;
346 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR_MASK,
347 FIELD_PREP(REG_SLV_ADDR_MASK, msg->addr << 1));
349 meson_i2c_add_token(i2c, TOKEN_START);
350 meson_i2c_add_token(i2c, token);
353 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
354 int last, bool atomic)
356 unsigned long time_left, flags;
366 meson_i2c_reset_tokens(i2c);
368 flags = (msg->flags & I2C_M_IGNORE_NAK) ? REG_CTRL_ACK_IGNORE : 0;
369 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags);
371 if (!(msg->flags & I2C_M_NOSTART))
372 meson_i2c_do_start(i2c, msg);
374 i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
375 meson_i2c_prepare_xfer(i2c);
378 reinit_completion(&i2c->done);
380 /* Start the transfer */
381 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
384 ret = readl_poll_timeout_atomic(i2c->regs + REG_CTRL, ctrl,
385 !(ctrl & REG_CTRL_STATUS),
386 10, I2C_TIMEOUT_MS * 1000);
388 time_left = msecs_to_jiffies(I2C_TIMEOUT_MS);
389 time_left = wait_for_completion_timeout(&i2c->done, time_left);
396 * Protect access to i2c struct and registers from interrupt
397 * handlers triggered by a transfer terminated after the
400 spin_lock_irqsave(&i2c->lock, flags);
403 meson_i2c_transfer_complete(i2c, ctrl);
405 /* Abort any active operation */
406 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
409 i2c->state = STATE_IDLE;
414 spin_unlock_irqrestore(&i2c->lock, flags);
419 static int meson_i2c_xfer_messages(struct i2c_adapter *adap,
420 struct i2c_msg *msgs, int num, bool atomic)
422 struct meson_i2c *i2c = adap->algo_data;
425 for (i = 0; i < num; i++) {
426 ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1, atomic);
434 static int meson_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
437 return meson_i2c_xfer_messages(adap, msgs, num, false);
440 static int meson_i2c_xfer_atomic(struct i2c_adapter *adap,
441 struct i2c_msg *msgs, int num)
443 return meson_i2c_xfer_messages(adap, msgs, num, true);
446 static u32 meson_i2c_func(struct i2c_adapter *adap)
448 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
451 static const struct i2c_algorithm meson_i2c_algorithm = {
452 .master_xfer = meson_i2c_xfer,
453 .master_xfer_atomic = meson_i2c_xfer_atomic,
454 .functionality = meson_i2c_func,
457 static int meson_i2c_probe(struct platform_device *pdev)
459 struct device_node *np = pdev->dev.of_node;
460 struct meson_i2c *i2c;
461 struct i2c_timings timings;
464 i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL);
468 i2c_parse_fw_timings(&pdev->dev, &timings, true);
470 i2c->dev = &pdev->dev;
471 platform_set_drvdata(pdev, i2c);
473 spin_lock_init(&i2c->lock);
474 init_completion(&i2c->done);
476 i2c->data = (const struct meson_i2c_data *)
477 of_device_get_match_data(&pdev->dev);
479 i2c->clk = devm_clk_get(&pdev->dev, NULL);
480 if (IS_ERR(i2c->clk)) {
481 dev_err(&pdev->dev, "can't get device clock\n");
482 return PTR_ERR(i2c->clk);
485 i2c->regs = devm_platform_ioremap_resource(pdev, 0);
486 if (IS_ERR(i2c->regs))
487 return PTR_ERR(i2c->regs);
489 irq = platform_get_irq(pdev, 0);
493 ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c);
495 dev_err(&pdev->dev, "can't request IRQ\n");
499 ret = clk_prepare_enable(i2c->clk);
501 dev_err(&pdev->dev, "can't prepare clock\n");
505 strscpy(i2c->adap.name, "Meson I2C adapter",
506 sizeof(i2c->adap.name));
507 i2c->adap.owner = THIS_MODULE;
508 i2c->adap.algo = &meson_i2c_algorithm;
509 i2c->adap.dev.parent = &pdev->dev;
510 i2c->adap.dev.of_node = np;
511 i2c->adap.algo_data = i2c;
514 * A transfer is triggered when START bit changes from 0 to 1.
515 * Ensure that the bit is set to 0 after probe
517 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
519 /* Disable filtering */
520 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR,
521 REG_SLV_SDA_FILTER_MASK | REG_SLV_SCL_FILTER_MASK, 0);
523 if (!i2c->data->set_clk_div) {
524 clk_disable_unprepare(i2c->clk);
527 i2c->data->set_clk_div(i2c, timings.bus_freq_hz);
529 ret = i2c_add_adapter(&i2c->adap);
531 clk_disable_unprepare(i2c->clk);
538 static int meson_i2c_remove(struct platform_device *pdev)
540 struct meson_i2c *i2c = platform_get_drvdata(pdev);
542 i2c_del_adapter(&i2c->adap);
543 clk_disable_unprepare(i2c->clk);
548 static const struct meson_i2c_data i2c_meson6_data = {
549 .set_clk_div = meson6_i2c_set_clk_div,
552 static const struct meson_i2c_data i2c_gxbb_data = {
553 .set_clk_div = meson_gxbb_axg_i2c_set_clk_div,
556 static const struct meson_i2c_data i2c_axg_data = {
557 .set_clk_div = meson_gxbb_axg_i2c_set_clk_div,
560 static const struct of_device_id meson_i2c_match[] = {
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 },
567 MODULE_DEVICE_TABLE(of, meson_i2c_match);
569 static struct platform_driver meson_i2c_driver = {
570 .probe = meson_i2c_probe,
571 .remove = meson_i2c_remove,
574 .of_match_table = meson_i2c_match,
578 module_platform_driver(meson_i2c_driver);
580 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");
582 MODULE_LICENSE("GPL v2");