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/clk.h>
12 #include <linux/completion.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.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 ((BIT(10) - 1) << REG_CTRL_CLKDIV_SHIFT)
40 #define I2C_TIMEOUT_MS 500
41 #define DEFAULT_FREQ 100000
46 TOKEN_SLAVE_ADDR_WRITE
,
47 TOKEN_SLAVE_ADDR_READ
,
61 * struct meson_i2c - Meson I2C device private data
63 * @adap: I2C adapter instance
64 * @dev: Pointer to device structure
65 * @regs: Base address of the device memory mapped registers
66 * @clk: Pointer to clock structure
68 * @msg: Pointer to the current I2C message
69 * @state: Current state in the driver state machine
70 * @last: Flag set for the last message in the transfer
71 * @count: Number of bytes to be sent/received in current transfer
72 * @pos: Current position in the send/receive buffer
73 * @error: Flag set when an error is received
74 * @lock: To avoid race conditions between irq handler and xfer code
75 * @done: Completion used to wait for transfer termination
76 * @frequency: Operating frequency of I2C bus clock
77 * @tokens: Sequence of tokens to be written to the device
78 * @num_tokens: Number of tokens
81 struct i2c_adapter adap
;
95 struct completion done
;
96 unsigned int frequency
;
101 static void meson_i2c_set_mask(struct meson_i2c
*i2c
, int reg
, u32 mask
,
106 data
= readl(i2c
->regs
+ reg
);
109 writel(data
, i2c
->regs
+ reg
);
112 static void meson_i2c_reset_tokens(struct meson_i2c
*i2c
)
119 static void meson_i2c_add_token(struct meson_i2c
*i2c
, int token
)
121 if (i2c
->num_tokens
< 8)
122 i2c
->tokens
[0] |= (token
& 0xf) << (i2c
->num_tokens
* 4);
124 i2c
->tokens
[1] |= (token
& 0xf) << ((i2c
->num_tokens
% 8) * 4);
129 static void meson_i2c_write_tokens(struct meson_i2c
*i2c
)
131 writel(i2c
->tokens
[0], i2c
->regs
+ REG_TOK_LIST0
);
132 writel(i2c
->tokens
[1], i2c
->regs
+ REG_TOK_LIST1
);
135 static void meson_i2c_set_clk_div(struct meson_i2c
*i2c
)
137 unsigned long clk_rate
= clk_get_rate(i2c
->clk
);
140 div
= DIV_ROUND_UP(clk_rate
, i2c
->frequency
* 4);
141 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_CLKDIV_MASK
,
142 div
<< REG_CTRL_CLKDIV_SHIFT
);
144 dev_dbg(i2c
->dev
, "%s: clk %lu, freq %u, div %u\n", __func__
,
145 clk_rate
, i2c
->frequency
, div
);
148 static void meson_i2c_get_data(struct meson_i2c
*i2c
, char *buf
, int len
)
153 rdata0
= readl(i2c
->regs
+ REG_TOK_RDATA0
);
154 rdata1
= readl(i2c
->regs
+ REG_TOK_RDATA1
);
156 dev_dbg(i2c
->dev
, "%s: data %08x %08x len %d\n", __func__
,
157 rdata0
, rdata1
, len
);
159 for (i
= 0; i
< min_t(int, 4, len
); i
++)
160 *buf
++ = (rdata0
>> i
* 8) & 0xff;
162 for (i
= 4; i
< min_t(int, 8, len
); i
++)
163 *buf
++ = (rdata1
>> (i
- 4) * 8) & 0xff;
166 static void meson_i2c_put_data(struct meson_i2c
*i2c
, char *buf
, int len
)
168 u32 wdata0
= 0, wdata1
= 0;
171 for (i
= 0; i
< min_t(int, 4, len
); i
++)
172 wdata0
|= *buf
++ << (i
* 8);
174 for (i
= 4; i
< min_t(int, 8, len
); i
++)
175 wdata1
|= *buf
++ << ((i
- 4) * 8);
177 writel(wdata0
, i2c
->regs
+ REG_TOK_WDATA0
);
178 writel(wdata1
, i2c
->regs
+ REG_TOK_WDATA1
);
180 dev_dbg(i2c
->dev
, "%s: data %08x %08x len %d\n", __func__
,
181 wdata0
, wdata1
, len
);
184 static void meson_i2c_prepare_xfer(struct meson_i2c
*i2c
)
186 bool write
= !(i2c
->msg
->flags
& I2C_M_RD
);
189 i2c
->count
= min_t(int, i2c
->msg
->len
- i2c
->pos
, 8);
191 for (i
= 0; i
< i2c
->count
- 1; i
++)
192 meson_i2c_add_token(i2c
, TOKEN_DATA
);
195 if (write
|| i2c
->pos
+ i2c
->count
< i2c
->msg
->len
)
196 meson_i2c_add_token(i2c
, TOKEN_DATA
);
198 meson_i2c_add_token(i2c
, TOKEN_DATA_LAST
);
202 meson_i2c_put_data(i2c
, i2c
->msg
->buf
+ i2c
->pos
, i2c
->count
);
205 static void meson_i2c_stop(struct meson_i2c
*i2c
)
207 dev_dbg(i2c
->dev
, "%s: last %d\n", __func__
, i2c
->last
);
210 i2c
->state
= STATE_STOP
;
211 meson_i2c_add_token(i2c
, TOKEN_STOP
);
213 i2c
->state
= STATE_IDLE
;
214 complete_all(&i2c
->done
);
218 static irqreturn_t
meson_i2c_irq(int irqno
, void *dev_id
)
220 struct meson_i2c
*i2c
= dev_id
;
223 spin_lock(&i2c
->lock
);
225 meson_i2c_reset_tokens(i2c
);
226 ctrl
= readl(i2c
->regs
+ REG_CTRL
);
228 dev_dbg(i2c
->dev
, "irq: state %d, pos %d, count %d, ctrl %08x\n",
229 i2c
->state
, i2c
->pos
, i2c
->count
, ctrl
);
231 if (ctrl
& REG_CTRL_ERROR
&& i2c
->state
!= STATE_IDLE
) {
233 * The bit is set when the IGNORE_NAK bit is cleared
234 * and the device didn't respond. In this case, the
235 * I2C controller automatically generates a STOP
238 dev_dbg(i2c
->dev
, "error bit set\n");
240 i2c
->state
= STATE_IDLE
;
241 complete_all(&i2c
->done
);
245 switch (i2c
->state
) {
247 if (i2c
->count
> 0) {
248 meson_i2c_get_data(i2c
, i2c
->msg
->buf
+ i2c
->pos
,
250 i2c
->pos
+= i2c
->count
;
253 if (i2c
->pos
>= i2c
->msg
->len
) {
258 meson_i2c_prepare_xfer(i2c
);
261 i2c
->pos
+= i2c
->count
;
263 if (i2c
->pos
>= i2c
->msg
->len
) {
268 meson_i2c_prepare_xfer(i2c
);
271 i2c
->state
= STATE_IDLE
;
272 complete_all(&i2c
->done
);
279 if (i2c
->state
!= STATE_IDLE
) {
280 /* Restart the processing */
281 meson_i2c_write_tokens(i2c
);
282 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_START
, 0);
283 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_START
,
287 spin_unlock(&i2c
->lock
);
292 static void meson_i2c_do_start(struct meson_i2c
*i2c
, struct i2c_msg
*msg
)
296 token
= (msg
->flags
& I2C_M_RD
) ? TOKEN_SLAVE_ADDR_READ
:
297 TOKEN_SLAVE_ADDR_WRITE
;
299 writel(msg
->addr
<< 1, i2c
->regs
+ REG_SLAVE_ADDR
);
300 meson_i2c_add_token(i2c
, TOKEN_START
);
301 meson_i2c_add_token(i2c
, token
);
304 static int meson_i2c_xfer_msg(struct meson_i2c
*i2c
, struct i2c_msg
*msg
,
307 unsigned long time_left
, flags
;
316 meson_i2c_reset_tokens(i2c
);
318 flags
= (msg
->flags
& I2C_M_IGNORE_NAK
) ? REG_CTRL_ACK_IGNORE
: 0;
319 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_ACK_IGNORE
, flags
);
321 if (!(msg
->flags
& I2C_M_NOSTART
))
322 meson_i2c_do_start(i2c
, msg
);
324 i2c
->state
= (msg
->flags
& I2C_M_RD
) ? STATE_READ
: STATE_WRITE
;
325 meson_i2c_prepare_xfer(i2c
);
326 meson_i2c_write_tokens(i2c
);
327 reinit_completion(&i2c
->done
);
329 /* Start the transfer */
330 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_START
, REG_CTRL_START
);
332 time_left
= msecs_to_jiffies(I2C_TIMEOUT_MS
);
333 time_left
= wait_for_completion_timeout(&i2c
->done
, time_left
);
336 * Protect access to i2c struct and registers from interrupt
337 * handlers triggered by a transfer terminated after the
340 spin_lock_irqsave(&i2c
->lock
, flags
);
342 /* Abort any active operation */
343 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_START
, 0);
346 i2c
->state
= STATE_IDLE
;
353 spin_unlock_irqrestore(&i2c
->lock
, flags
);
358 static int meson_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
361 struct meson_i2c
*i2c
= adap
->algo_data
;
362 int i
, ret
= 0, count
= 0;
364 clk_enable(i2c
->clk
);
365 meson_i2c_set_clk_div(i2c
);
367 for (i
= 0; i
< num
; i
++) {
368 ret
= meson_i2c_xfer_msg(i2c
, msgs
+ i
, i
== num
- 1);
374 clk_disable(i2c
->clk
);
376 return ret
? ret
: count
;
379 static u32
meson_i2c_func(struct i2c_adapter
*adap
)
381 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
384 static const struct i2c_algorithm meson_i2c_algorithm
= {
385 .master_xfer
= meson_i2c_xfer
,
386 .functionality
= meson_i2c_func
,
389 static int meson_i2c_probe(struct platform_device
*pdev
)
391 struct device_node
*np
= pdev
->dev
.of_node
;
392 struct meson_i2c
*i2c
;
393 struct resource
*mem
;
396 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(struct meson_i2c
), GFP_KERNEL
);
400 if (of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
402 i2c
->frequency
= DEFAULT_FREQ
;
404 i2c
->dev
= &pdev
->dev
;
405 platform_set_drvdata(pdev
, i2c
);
407 spin_lock_init(&i2c
->lock
);
408 init_completion(&i2c
->done
);
410 i2c
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
411 if (IS_ERR(i2c
->clk
)) {
412 dev_err(&pdev
->dev
, "can't get device clock\n");
413 return PTR_ERR(i2c
->clk
);
416 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
417 i2c
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
418 if (IS_ERR(i2c
->regs
))
419 return PTR_ERR(i2c
->regs
);
421 i2c
->irq
= platform_get_irq(pdev
, 0);
423 dev_err(&pdev
->dev
, "can't find IRQ\n");
427 ret
= devm_request_irq(&pdev
->dev
, i2c
->irq
, meson_i2c_irq
,
428 0, dev_name(&pdev
->dev
), i2c
);
430 dev_err(&pdev
->dev
, "can't request IRQ\n");
434 ret
= clk_prepare(i2c
->clk
);
436 dev_err(&pdev
->dev
, "can't prepare clock\n");
440 strlcpy(i2c
->adap
.name
, "Meson I2C adapter",
441 sizeof(i2c
->adap
.name
));
442 i2c
->adap
.owner
= THIS_MODULE
;
443 i2c
->adap
.algo
= &meson_i2c_algorithm
;
444 i2c
->adap
.dev
.parent
= &pdev
->dev
;
445 i2c
->adap
.dev
.of_node
= np
;
446 i2c
->adap
.algo_data
= i2c
;
449 * A transfer is triggered when START bit changes from 0 to 1.
450 * Ensure that the bit is set to 0 after probe
452 meson_i2c_set_mask(i2c
, REG_CTRL
, REG_CTRL_START
, 0);
454 ret
= i2c_add_adapter(&i2c
->adap
);
456 dev_err(&pdev
->dev
, "can't register adapter\n");
457 clk_unprepare(i2c
->clk
);
464 static int meson_i2c_remove(struct platform_device
*pdev
)
466 struct meson_i2c
*i2c
= platform_get_drvdata(pdev
);
468 i2c_del_adapter(&i2c
->adap
);
469 clk_unprepare(i2c
->clk
);
474 static const struct of_device_id meson_i2c_match
[] = {
475 { .compatible
= "amlogic,meson6-i2c" },
478 MODULE_DEVICE_TABLE(of
, meson_i2c_match
);
480 static struct platform_driver meson_i2c_driver
= {
481 .probe
= meson_i2c_probe
,
482 .remove
= meson_i2c_remove
,
485 .of_match_table
= meson_i2c_match
,
489 module_platform_driver(meson_i2c_driver
);
491 MODULE_DESCRIPTION("Amlogic Meson I2C Bus driver");
492 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
493 MODULE_LICENSE("GPL v2");