1 // SPDX-License-Identifier: GPL-2.0
3 * I2C driver for the Renesas EMEV2 SoC
5 * Copyright (C) 2015 Wolfram Sang <wsa@sang-engineering.com>
6 * Copyright 2013 Codethink Ltd.
7 * Copyright 2010-2015 Renesas Electronics Corporation
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
24 #define I2C_OFS_IICACT0 0x00 /* start */
25 #define I2C_OFS_IIC0 0x04 /* shift */
26 #define I2C_OFS_IICC0 0x08 /* control */
27 #define I2C_OFS_SVA0 0x0c /* slave address */
28 #define I2C_OFS_IICCL0 0x10 /* clock select */
29 #define I2C_OFS_IICX0 0x14 /* extension */
30 #define I2C_OFS_IICS0 0x18 /* status */
31 #define I2C_OFS_IICSE0 0x1c /* status For emulation */
32 #define I2C_OFS_IICF0 0x20 /* IIC flag */
34 /* I2C IICACT0 Masks */
35 #define I2C_BIT_IICE0 0x0001
38 #define I2C_BIT_LREL0 0x0040
39 #define I2C_BIT_WREL0 0x0020
40 #define I2C_BIT_SPIE0 0x0010
41 #define I2C_BIT_WTIM0 0x0008
42 #define I2C_BIT_ACKE0 0x0004
43 #define I2C_BIT_STT0 0x0002
44 #define I2C_BIT_SPT0 0x0001
46 /* I2C IICCL0 Masks */
47 #define I2C_BIT_SMC0 0x0008
48 #define I2C_BIT_DFC0 0x0004
50 /* I2C IICSE0 Masks */
51 #define I2C_BIT_MSTS0 0x0080
52 #define I2C_BIT_ALD0 0x0040
53 #define I2C_BIT_EXC0 0x0020
54 #define I2C_BIT_COI0 0x0010
55 #define I2C_BIT_TRC0 0x0008
56 #define I2C_BIT_ACKD0 0x0004
57 #define I2C_BIT_STD0 0x0002
58 #define I2C_BIT_SPD0 0x0001
61 #define I2C_BIT_STCF 0x0080
62 #define I2C_BIT_IICBSY 0x0040
63 #define I2C_BIT_STCEN 0x0002
64 #define I2C_BIT_IICRSV 0x0001
66 struct em_i2c_device
{
68 struct i2c_adapter adap
;
69 struct completion msg_done
;
71 struct i2c_client
*slave
;
75 static inline void em_clear_set_bit(struct em_i2c_device
*priv
, u8 clear
, u8 set
, u8 reg
)
77 writeb((readb(priv
->base
+ reg
) & ~clear
) | set
, priv
->base
+ reg
);
80 static int em_i2c_wait_for_event(struct em_i2c_device
*priv
)
82 unsigned long time_left
;
85 reinit_completion(&priv
->msg_done
);
87 time_left
= wait_for_completion_timeout(&priv
->msg_done
, priv
->adap
.timeout
);
92 status
= readb(priv
->base
+ I2C_OFS_IICSE0
);
93 return status
& I2C_BIT_ALD0
? -EAGAIN
: status
;
96 static void em_i2c_stop(struct em_i2c_device
*priv
)
98 /* Send Stop condition */
99 em_clear_set_bit(priv
, 0, I2C_BIT_SPT0
| I2C_BIT_SPIE0
, I2C_OFS_IICC0
);
101 /* Wait for stop condition */
102 em_i2c_wait_for_event(priv
);
105 static void em_i2c_reset(struct i2c_adapter
*adap
)
107 struct em_i2c_device
*priv
= i2c_get_adapdata(adap
);
111 if (readb(priv
->base
+ I2C_OFS_IICACT0
) & I2C_BIT_IICE0
) {
112 /* Disable I2C operation */
113 writeb(0, priv
->base
+ I2C_OFS_IICACT0
);
116 while (readb(priv
->base
+ I2C_OFS_IICACT0
) == 1 && retr
)
121 /* Transfer mode set */
122 writeb(I2C_BIT_DFC0
, priv
->base
+ I2C_OFS_IICCL0
);
124 /* Can Issue start without detecting a stop, Reservation disabled. */
125 writeb(I2C_BIT_STCEN
| I2C_BIT_IICRSV
, priv
->base
+ I2C_OFS_IICF0
);
127 /* I2C enable, 9 bit interrupt mode */
128 writeb(I2C_BIT_WTIM0
, priv
->base
+ I2C_OFS_IICC0
);
130 /* Enable I2C operation */
131 writeb(I2C_BIT_IICE0
, priv
->base
+ I2C_OFS_IICACT0
);
134 while (readb(priv
->base
+ I2C_OFS_IICACT0
) == 0 && retr
)
139 static int __em_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msg
,
142 struct em_i2c_device
*priv
= i2c_get_adapdata(adap
);
143 int count
, status
, read
= !!(msg
->flags
& I2C_M_RD
);
145 /* Send start condition */
146 em_clear_set_bit(priv
, 0, I2C_BIT_ACKE0
| I2C_BIT_WTIM0
, I2C_OFS_IICC0
);
147 em_clear_set_bit(priv
, 0, I2C_BIT_STT0
, I2C_OFS_IICC0
);
149 /* Send slave address and R/W type */
150 writeb(i2c_8bit_addr_from_msg(msg
), priv
->base
+ I2C_OFS_IIC0
);
152 /* Wait for transaction */
153 status
= em_i2c_wait_for_event(priv
);
157 /* Received NACK (result of setting slave address and R/W) */
158 if (!(status
& I2C_BIT_ACKD0
)) {
163 /* Extra setup for read transactions */
165 /* 8 bit interrupt mode */
166 em_clear_set_bit(priv
, I2C_BIT_WTIM0
, I2C_BIT_ACKE0
, I2C_OFS_IICC0
);
167 em_clear_set_bit(priv
, I2C_BIT_WTIM0
, I2C_BIT_WREL0
, I2C_OFS_IICC0
);
169 /* Wait for transaction */
170 status
= em_i2c_wait_for_event(priv
);
175 /* Send / receive data */
176 for (count
= 0; count
< msg
->len
; count
++) {
177 if (read
) { /* Read transaction */
178 msg
->buf
[count
] = readb(priv
->base
+ I2C_OFS_IIC0
);
179 em_clear_set_bit(priv
, 0, I2C_BIT_WREL0
, I2C_OFS_IICC0
);
181 } else { /* Write transaction */
183 if (!(status
& I2C_BIT_ACKD0
)) {
189 writeb(msg
->buf
[count
], priv
->base
+ I2C_OFS_IIC0
);
192 /* Wait for R/W transaction */
193 status
= em_i2c_wait_for_event(priv
);
206 return status
< 0 ? status
: -ENXIO
;
209 static int em_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
212 struct em_i2c_device
*priv
= i2c_get_adapdata(adap
);
215 if (readb(priv
->base
+ I2C_OFS_IICF0
) & I2C_BIT_IICBSY
)
218 for (i
= 0; i
< num
; i
++) {
219 ret
= __em_i2c_xfer(adap
, &msgs
[i
], (i
== (num
- 1)));
224 /* I2C transfer completed */
228 static bool em_i2c_slave_irq(struct em_i2c_device
*priv
)
231 enum i2c_slave_event event
;
237 status
= readb(priv
->base
+ I2C_OFS_IICSE0
);
239 /* Extension code, do not participate */
240 if (status
& I2C_BIT_EXC0
) {
241 em_clear_set_bit(priv
, 0, I2C_BIT_LREL0
, I2C_OFS_IICC0
);
245 /* Stop detected, we don't know if it's for slave or master */
246 if (status
& I2C_BIT_SPD0
) {
247 /* Notify slave device */
248 i2c_slave_event(priv
->slave
, I2C_SLAVE_STOP
, &value
);
249 /* Pretend we did not handle the interrupt */
253 /* Only handle interrupts addressed to us */
254 if (!(status
& I2C_BIT_COI0
))
257 /* Enable stop interrupts */
258 em_clear_set_bit(priv
, 0, I2C_BIT_SPIE0
, I2C_OFS_IICC0
);
260 /* Transmission or Reception */
261 if (status
& I2C_BIT_TRC0
) {
262 if (status
& I2C_BIT_ACKD0
) {
263 /* 9 bit interrupt mode */
264 em_clear_set_bit(priv
, 0, I2C_BIT_WTIM0
, I2C_OFS_IICC0
);
267 event
= status
& I2C_BIT_STD0
?
268 I2C_SLAVE_READ_REQUESTED
:
269 I2C_SLAVE_READ_PROCESSED
;
270 i2c_slave_event(priv
->slave
, event
, &value
);
271 writeb(value
, priv
->base
+ I2C_OFS_IIC0
);
273 /* NACK, stop transmitting */
274 em_clear_set_bit(priv
, 0, I2C_BIT_LREL0
, I2C_OFS_IICC0
);
277 /* 8 bit interrupt mode */
278 em_clear_set_bit(priv
, I2C_BIT_WTIM0
, I2C_BIT_ACKE0
,
280 em_clear_set_bit(priv
, I2C_BIT_WTIM0
, I2C_BIT_WREL0
,
283 if (status
& I2C_BIT_STD0
) {
284 i2c_slave_event(priv
->slave
, I2C_SLAVE_WRITE_REQUESTED
,
288 value
= readb(priv
->base
+ I2C_OFS_IIC0
);
289 ret
= i2c_slave_event(priv
->slave
,
290 I2C_SLAVE_WRITE_RECEIVED
, &value
);
292 em_clear_set_bit(priv
, I2C_BIT_ACKE0
, 0,
300 static irqreturn_t
em_i2c_irq_handler(int this_irq
, void *dev_id
)
302 struct em_i2c_device
*priv
= dev_id
;
304 if (em_i2c_slave_irq(priv
))
307 complete(&priv
->msg_done
);
312 static u32
em_i2c_func(struct i2c_adapter
*adap
)
314 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_SLAVE
;
317 static int em_i2c_reg_slave(struct i2c_client
*slave
)
319 struct em_i2c_device
*priv
= i2c_get_adapdata(slave
->adapter
);
324 if (slave
->flags
& I2C_CLIENT_TEN
)
325 return -EAFNOSUPPORT
;
329 /* Set slave address */
330 writeb(slave
->addr
<< 1, priv
->base
+ I2C_OFS_SVA0
);
335 static int em_i2c_unreg_slave(struct i2c_client
*slave
)
337 struct em_i2c_device
*priv
= i2c_get_adapdata(slave
->adapter
);
339 WARN_ON(!priv
->slave
);
341 writeb(0, priv
->base
+ I2C_OFS_SVA0
);
344 * Wait for interrupt to finish. New slave irqs cannot happen because we
345 * cleared the slave address and, thus, only extension codes will be
346 * detected which do not use the slave ptr.
348 synchronize_irq(priv
->irq
);
354 static const struct i2c_algorithm em_i2c_algo
= {
355 .master_xfer
= em_i2c_xfer
,
356 .functionality
= em_i2c_func
,
357 .reg_slave
= em_i2c_reg_slave
,
358 .unreg_slave
= em_i2c_unreg_slave
,
361 static int em_i2c_probe(struct platform_device
*pdev
)
363 struct em_i2c_device
*priv
;
366 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
370 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
371 if (IS_ERR(priv
->base
))
372 return PTR_ERR(priv
->base
);
374 strlcpy(priv
->adap
.name
, "EMEV2 I2C", sizeof(priv
->adap
.name
));
376 priv
->sclk
= devm_clk_get(&pdev
->dev
, "sclk");
377 if (IS_ERR(priv
->sclk
))
378 return PTR_ERR(priv
->sclk
);
380 ret
= clk_prepare_enable(priv
->sclk
);
384 priv
->adap
.timeout
= msecs_to_jiffies(100);
385 priv
->adap
.retries
= 5;
386 priv
->adap
.dev
.parent
= &pdev
->dev
;
387 priv
->adap
.algo
= &em_i2c_algo
;
388 priv
->adap
.owner
= THIS_MODULE
;
389 priv
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
391 init_completion(&priv
->msg_done
);
393 platform_set_drvdata(pdev
, priv
);
394 i2c_set_adapdata(&priv
->adap
, priv
);
396 em_i2c_reset(&priv
->adap
);
398 priv
->irq
= platform_get_irq(pdev
, 0);
399 ret
= devm_request_irq(&pdev
->dev
, priv
->irq
, em_i2c_irq_handler
, 0,
404 ret
= i2c_add_adapter(&priv
->adap
);
409 dev_info(&pdev
->dev
, "Added i2c controller %d, irq %d\n", priv
->adap
.nr
,
415 clk_disable_unprepare(priv
->sclk
);
419 static int em_i2c_remove(struct platform_device
*dev
)
421 struct em_i2c_device
*priv
= platform_get_drvdata(dev
);
423 i2c_del_adapter(&priv
->adap
);
424 clk_disable_unprepare(priv
->sclk
);
429 static const struct of_device_id em_i2c_ids
[] = {
430 { .compatible
= "renesas,iic-emev2", },
434 static struct platform_driver em_i2c_driver
= {
435 .probe
= em_i2c_probe
,
436 .remove
= em_i2c_remove
,
439 .of_match_table
= em_i2c_ids
,
442 module_platform_driver(em_i2c_driver
);
444 MODULE_DESCRIPTION("EMEV2 I2C bus driver");
445 MODULE_AUTHOR("Ian Molton and Wolfram Sang <wsa@sang-engineering.com>");
446 MODULE_LICENSE("GPL v2");
447 MODULE_DEVICE_TABLE(of
, em_i2c_ids
);