2 * I2C bus driver for Amlogic Meson SoCs
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/bitfield.h>
12 #include <linux/clk.h>
13 #include <linux/completion.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/types.h>
24 /* Meson I2C register map */
26 #define REG_SLAVE_ADDR 0x04
27 #define REG_TOK_LIST0 0x08
28 #define REG_TOK_LIST1 0x0c
29 #define REG_TOK_WDATA0 0x10
30 #define REG_TOK_WDATA1 0x14
31 #define REG_TOK_RDATA0 0x18
32 #define REG_TOK_RDATA1 0x1c
34 /* Control register fields */
35 #define REG_CTRL_START BIT(0)
36 #define REG_CTRL_ACK_IGNORE BIT(1)
37 #define REG_CTRL_STATUS BIT(2)
38 #define REG_CTRL_ERROR BIT(3)
39 #define REG_CTRL_CLKDIV GENMASK(21, 12)
40 #define REG_CTRL_CLKDIVEXT GENMASK(29, 28)
42 #define REG_SLV_ADDR GENMASK(7, 0)
43 #define REG_SLV_SDA_FILTER GENMASK(10, 8)
44 #define REG_SLV_SCL_FILTER GENMASK(13, 11)
45 #define REG_SLV_SCL_LOW GENMASK(27, 16)
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,
67 struct meson_i2c_data {
68 unsigned char div_factor;
72 * struct meson_i2c - Meson I2C device private data
74 * @adap: I2C adapter instance
75 * @dev: Pointer to device structure
76 * @regs: Base address of the device memory mapped registers
77 * @clk: Pointer to clock structure
78 * @msg: Pointer to the current I2C message
79 * @state: Current state in the driver state machine
80 * @last: Flag set for the last message in the transfer
81 * @count: Number of bytes to be sent/received in current transfer
82 * @pos: Current position in the send/receive buffer
83 * @error: Flag set when an error is received
84 * @lock: To avoid race conditions between irq handler and xfer code
85 * @done: Completion used to wait for transfer termination
86 * @tokens: Sequence of tokens to be written to the device
87 * @num_tokens: Number of tokens
88 * @data: Pointer to the controlller's platform data
91 struct i2c_adapter adap;
104 struct completion done;
108 const struct meson_i2c_data *data;
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_i2c_set_clk_div(struct meson_i2c *i2c, unsigned int freq)
141 unsigned long clk_rate = clk_get_rate(i2c->clk);
144 div = DIV_ROUND_UP(clk_rate, freq);
146 div = DIV_ROUND_UP(div, i2c->data->div_factor);
148 /* clock divider has 12 bits */
149 if (div > GENMASK(11, 0)) {
150 dev_err(i2c->dev, "requested bus frequency too low\n");
151 div = GENMASK(11, 0);
154 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIV,
155 FIELD_PREP(REG_CTRL_CLKDIV, div & GENMASK(9, 0)));
157 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_CLKDIVEXT,
158 FIELD_PREP(REG_CTRL_CLKDIVEXT, div >> 10));
160 /* Disable HIGH/LOW mode */
161 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_SCL_LOW_EN, 0);
163 dev_dbg(i2c->dev, "%s: clk %lu, freq %u, div %u\n", __func__,
164 clk_rate, freq, div);
167 static void meson_i2c_get_data(struct meson_i2c *i2c, char *buf, int len)
172 rdata0 = readl(i2c->regs + REG_TOK_RDATA0);
173 rdata1 = readl(i2c->regs + REG_TOK_RDATA1);
175 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
176 rdata0, rdata1, len);
178 for (i = 0; i < min(4, len); i++)
179 *buf++ = (rdata0 >> i * 8) & 0xff;
181 for (i = 4; i < min(8, len); i++)
182 *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
185 static void meson_i2c_put_data(struct meson_i2c *i2c, char *buf, int len)
187 u32 wdata0 = 0, wdata1 = 0;
190 for (i = 0; i < min(4, len); i++)
191 wdata0 |= *buf++ << (i * 8);
193 for (i = 4; i < min(8, len); i++)
194 wdata1 |= *buf++ << ((i - 4) * 8);
196 writel(wdata0, i2c->regs + REG_TOK_WDATA0);
197 writel(wdata1, i2c->regs + REG_TOK_WDATA1);
199 dev_dbg(i2c->dev, "%s: data %08x %08x len %d\n", __func__,
200 wdata0, wdata1, len);
203 static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
205 bool write = !(i2c->msg->flags & I2C_M_RD);
208 i2c->count = min(i2c->msg->len - i2c->pos, 8);
210 for (i = 0; i < i2c->count - 1; i++)
211 meson_i2c_add_token(i2c, TOKEN_DATA);
214 if (write || i2c->pos + i2c->count < i2c->msg->len)
215 meson_i2c_add_token(i2c, TOKEN_DATA);
217 meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
221 meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
223 if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
224 meson_i2c_add_token(i2c, TOKEN_STOP);
226 writel(i2c->tokens[0], i2c->regs + REG_TOK_LIST0);
227 writel(i2c->tokens[1], i2c->regs + REG_TOK_LIST1);
230 static irqreturn_t meson_i2c_irq(int irqno, void *dev_id)
232 struct meson_i2c *i2c = dev_id;
235 spin_lock(&i2c->lock);
237 meson_i2c_reset_tokens(i2c);
238 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
239 ctrl = readl(i2c->regs + REG_CTRL);
241 dev_dbg(i2c->dev, "irq: state %d, pos %d, count %d, ctrl %08x\n",
242 i2c->state, i2c->pos, i2c->count, ctrl);
244 if (i2c->state == STATE_IDLE) {
245 spin_unlock(&i2c->lock);
249 if (ctrl & REG_CTRL_ERROR) {
251 * The bit is set when the IGNORE_NAK bit is cleared
252 * and the device didn't respond. In this case, the
253 * I2C controller automatically generates a STOP
256 dev_dbg(i2c->dev, "error bit set\n");
258 i2c->state = STATE_IDLE;
259 complete(&i2c->done);
263 if (i2c->state == STATE_READ && i2c->count)
264 meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
266 i2c->pos += i2c->count;
268 if (i2c->pos >= i2c->msg->len) {
269 i2c->state = STATE_IDLE;
270 complete(&i2c->done);
274 /* Restart the processing */
275 meson_i2c_prepare_xfer(i2c);
276 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
278 spin_unlock(&i2c->lock);
283 static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
287 token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
288 TOKEN_SLAVE_ADDR_WRITE;
291 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR, REG_SLV_ADDR,
292 FIELD_PREP(REG_SLV_ADDR, msg->addr << 1));
294 meson_i2c_add_token(i2c, TOKEN_START);
295 meson_i2c_add_token(i2c, token);
298 static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
301 unsigned long time_left, flags;
310 meson_i2c_reset_tokens(i2c);
312 flags = (msg->flags & I2C_M_IGNORE_NAK) ? REG_CTRL_ACK_IGNORE : 0;
313 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_ACK_IGNORE, flags);
315 if (!(msg->flags & I2C_M_NOSTART))
316 meson_i2c_do_start(i2c, msg);
318 i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
319 meson_i2c_prepare_xfer(i2c);
320 reinit_completion(&i2c->done);
322 /* Start the transfer */
323 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, REG_CTRL_START);
325 time_left = msecs_to_jiffies(I2C_TIMEOUT_MS);
326 time_left = wait_for_completion_timeout(&i2c->done, time_left);
329 * Protect access to i2c struct and registers from interrupt
330 * handlers triggered by a transfer terminated after the
333 spin_lock_irqsave(&i2c->lock, flags);
335 /* Abort any active operation */
336 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
339 i2c->state = STATE_IDLE;
346 spin_unlock_irqrestore(&i2c->lock, flags);
351 static int meson_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
354 struct meson_i2c *i2c = adap->algo_data;
357 clk_enable(i2c->clk);
359 for (i = 0; i < num; i++) {
360 ret = meson_i2c_xfer_msg(i2c, msgs + i, i == num - 1);
365 clk_disable(i2c->clk);
370 static u32 meson_i2c_func(struct i2c_adapter *adap)
372 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
375 static const struct i2c_algorithm meson_i2c_algorithm = {
376 .master_xfer = meson_i2c_xfer,
377 .functionality = meson_i2c_func,
380 static int meson_i2c_probe(struct platform_device *pdev)
382 struct device_node *np = pdev->dev.of_node;
383 struct meson_i2c *i2c;
384 struct resource *mem;
385 struct i2c_timings timings;
388 i2c = devm_kzalloc(&pdev->dev, sizeof(struct meson_i2c), GFP_KERNEL);
392 i2c_parse_fw_timings(&pdev->dev, &timings, true);
394 i2c->dev = &pdev->dev;
395 platform_set_drvdata(pdev, i2c);
397 spin_lock_init(&i2c->lock);
398 init_completion(&i2c->done);
400 i2c->data = (const struct meson_i2c_data *)
401 of_device_get_match_data(&pdev->dev);
403 i2c->clk = devm_clk_get(&pdev->dev, NULL);
404 if (IS_ERR(i2c->clk)) {
405 dev_err(&pdev->dev, "can't get device clock\n");
406 return PTR_ERR(i2c->clk);
409 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
410 i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
411 if (IS_ERR(i2c->regs))
412 return PTR_ERR(i2c->regs);
414 irq = platform_get_irq(pdev, 0);
416 dev_err(&pdev->dev, "can't find IRQ\n");
420 ret = devm_request_irq(&pdev->dev, irq, meson_i2c_irq, 0, NULL, i2c);
422 dev_err(&pdev->dev, "can't request IRQ\n");
426 ret = clk_prepare(i2c->clk);
428 dev_err(&pdev->dev, "can't prepare clock\n");
432 strlcpy(i2c->adap.name, "Meson I2C adapter",
433 sizeof(i2c->adap.name));
434 i2c->adap.owner = THIS_MODULE;
435 i2c->adap.algo = &meson_i2c_algorithm;
436 i2c->adap.dev.parent = &pdev->dev;
437 i2c->adap.dev.of_node = np;
438 i2c->adap.algo_data = i2c;
441 * A transfer is triggered when START bit changes from 0 to 1.
442 * Ensure that the bit is set to 0 after probe
444 meson_i2c_set_mask(i2c, REG_CTRL, REG_CTRL_START, 0);
446 ret = i2c_add_adapter(&i2c->adap);
448 clk_unprepare(i2c->clk);
452 /* Disable filtering */
453 meson_i2c_set_mask(i2c, REG_SLAVE_ADDR,
454 REG_SLV_SDA_FILTER | REG_SLV_SCL_FILTER, 0);
456 meson_i2c_set_clk_div(i2c, timings.bus_freq_hz);
461 static int meson_i2c_remove(struct platform_device *pdev)
463 struct meson_i2c *i2c = platform_get_drvdata(pdev);
465 i2c_del_adapter(&i2c->adap);
466 clk_unprepare(i2c->clk);
471 static const struct meson_i2c_data i2c_meson6_data = {
475 static const struct meson_i2c_data i2c_gxbb_data = {
479 static const struct meson_i2c_data i2c_axg_data = {
483 static const struct of_device_id meson_i2c_match[] = {
484 { .compatible = "amlogic,meson6-i2c", .data = &i2c_meson6_data },
485 { .compatible = "amlogic,meson-gxbb-i2c", .data = &i2c_gxbb_data },
486 { .compatible = "amlogic,meson-axg-i2c", .data = &i2c_axg_data },
490 MODULE_DEVICE_TABLE(of, meson_i2c_match);
492 static struct platform_driver meson_i2c_driver = {
493 .probe = meson_i2c_probe,
494 .remove = meson_i2c_remove,
497 .of_match_table = meson_i2c_match,
501 module_platform_driver(meson_i2c_driver);
503 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");
504 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
505 MODULE_LICENSE("GPL v2");