2 * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
3 * (https://opencores.org/project/i2c/overview)
5 * Peter Korsgaard <peter@korsgaard.com>
7 * Support for the GRLIB port of the controller by
8 * Andreas Larsson <andreas@gaisler.com>
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
12 * kind, whether express or implied.
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/platform_device.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/wait.h>
24 #include <linux/platform_data/i2c-ocores.h>
25 #include <linux/slab.h>
27 #include <linux/log2.h>
33 wait_queue_head_t wait
;
34 struct i2c_adapter adap
;
38 int state
; /* see STATE_ */
42 void (*setreg
)(struct ocores_i2c
*i2c
, int reg
, u8 value
);
43 u8 (*getreg
)(struct ocores_i2c
*i2c
, int reg
);
47 #define OCI2C_PRELOW 0
48 #define OCI2C_PREHIGH 1
49 #define OCI2C_CONTROL 2
51 #define OCI2C_CMD 4 /* write only */
52 #define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
54 #define OCI2C_CTRL_IEN 0x40
55 #define OCI2C_CTRL_EN 0x80
57 #define OCI2C_CMD_START 0x91
58 #define OCI2C_CMD_STOP 0x41
59 #define OCI2C_CMD_READ 0x21
60 #define OCI2C_CMD_WRITE 0x11
61 #define OCI2C_CMD_READ_ACK 0x21
62 #define OCI2C_CMD_READ_NACK 0x29
63 #define OCI2C_CMD_IACK 0x01
65 #define OCI2C_STAT_IF 0x01
66 #define OCI2C_STAT_TIP 0x02
67 #define OCI2C_STAT_ARBLOST 0x20
68 #define OCI2C_STAT_BUSY 0x40
69 #define OCI2C_STAT_NACK 0x80
80 static void oc_setreg_8(struct ocores_i2c
*i2c
, int reg
, u8 value
)
82 iowrite8(value
, i2c
->base
+ (reg
<< i2c
->reg_shift
));
85 static void oc_setreg_16(struct ocores_i2c
*i2c
, int reg
, u8 value
)
87 iowrite16(value
, i2c
->base
+ (reg
<< i2c
->reg_shift
));
90 static void oc_setreg_32(struct ocores_i2c
*i2c
, int reg
, u8 value
)
92 iowrite32(value
, i2c
->base
+ (reg
<< i2c
->reg_shift
));
95 static void oc_setreg_16be(struct ocores_i2c
*i2c
, int reg
, u8 value
)
97 iowrite16be(value
, i2c
->base
+ (reg
<< i2c
->reg_shift
));
100 static void oc_setreg_32be(struct ocores_i2c
*i2c
, int reg
, u8 value
)
102 iowrite32be(value
, i2c
->base
+ (reg
<< i2c
->reg_shift
));
105 static inline u8
oc_getreg_8(struct ocores_i2c
*i2c
, int reg
)
107 return ioread8(i2c
->base
+ (reg
<< i2c
->reg_shift
));
110 static inline u8
oc_getreg_16(struct ocores_i2c
*i2c
, int reg
)
112 return ioread16(i2c
->base
+ (reg
<< i2c
->reg_shift
));
115 static inline u8
oc_getreg_32(struct ocores_i2c
*i2c
, int reg
)
117 return ioread32(i2c
->base
+ (reg
<< i2c
->reg_shift
));
120 static inline u8
oc_getreg_16be(struct ocores_i2c
*i2c
, int reg
)
122 return ioread16be(i2c
->base
+ (reg
<< i2c
->reg_shift
));
125 static inline u8
oc_getreg_32be(struct ocores_i2c
*i2c
, int reg
)
127 return ioread32be(i2c
->base
+ (reg
<< i2c
->reg_shift
));
130 static inline void oc_setreg(struct ocores_i2c
*i2c
, int reg
, u8 value
)
132 i2c
->setreg(i2c
, reg
, value
);
135 static inline u8
oc_getreg(struct ocores_i2c
*i2c
, int reg
)
137 return i2c
->getreg(i2c
, reg
);
140 static void ocores_process(struct ocores_i2c
*i2c
)
142 struct i2c_msg
*msg
= i2c
->msg
;
143 u8 stat
= oc_getreg(i2c
, OCI2C_STATUS
);
145 if ((i2c
->state
== STATE_DONE
) || (i2c
->state
== STATE_ERROR
)) {
146 /* stop has been sent */
147 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_IACK
);
153 if (stat
& OCI2C_STAT_ARBLOST
) {
154 i2c
->state
= STATE_ERROR
;
155 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_STOP
);
159 if ((i2c
->state
== STATE_START
) || (i2c
->state
== STATE_WRITE
)) {
161 (msg
->flags
& I2C_M_RD
) ? STATE_READ
: STATE_WRITE
;
163 if (stat
& OCI2C_STAT_NACK
) {
164 i2c
->state
= STATE_ERROR
;
165 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_STOP
);
169 msg
->buf
[i2c
->pos
++] = oc_getreg(i2c
, OCI2C_DATA
);
172 if (i2c
->pos
== msg
->len
) {
178 if (i2c
->nmsgs
) { /* end? */
180 if (!(msg
->flags
& I2C_M_NOSTART
)) {
181 u8 addr
= i2c_8bit_addr_from_msg(msg
);
183 i2c
->state
= STATE_START
;
185 oc_setreg(i2c
, OCI2C_DATA
, addr
);
186 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_START
);
189 i2c
->state
= (msg
->flags
& I2C_M_RD
)
190 ? STATE_READ
: STATE_WRITE
;
192 i2c
->state
= STATE_DONE
;
193 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_STOP
);
198 if (i2c
->state
== STATE_READ
) {
199 oc_setreg(i2c
, OCI2C_CMD
, i2c
->pos
== (msg
->len
-1) ?
200 OCI2C_CMD_READ_NACK
: OCI2C_CMD_READ_ACK
);
202 oc_setreg(i2c
, OCI2C_DATA
, msg
->buf
[i2c
->pos
++]);
203 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_WRITE
);
207 static irqreturn_t
ocores_isr(int irq
, void *dev_id
)
209 struct ocores_i2c
*i2c
= dev_id
;
216 static int ocores_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
218 struct ocores_i2c
*i2c
= i2c_get_adapdata(adap
);
223 i2c
->state
= STATE_START
;
225 oc_setreg(i2c
, OCI2C_DATA
, i2c_8bit_addr_from_msg(i2c
->msg
));
226 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_START
);
228 if (wait_event_timeout(i2c
->wait
, (i2c
->state
== STATE_ERROR
) ||
229 (i2c
->state
== STATE_DONE
), HZ
))
230 return (i2c
->state
== STATE_DONE
) ? num
: -EIO
;
235 static int ocores_init(struct device
*dev
, struct ocores_i2c
*i2c
)
239 u8 ctrl
= oc_getreg(i2c
, OCI2C_CONTROL
);
241 /* make sure the device is disabled */
242 oc_setreg(i2c
, OCI2C_CONTROL
, ctrl
& ~(OCI2C_CTRL_EN
|OCI2C_CTRL_IEN
));
244 prescale
= (i2c
->ip_clock_khz
/ (5 * i2c
->bus_clock_khz
)) - 1;
245 prescale
= clamp(prescale
, 0, 0xffff);
247 diff
= i2c
->ip_clock_khz
/ (5 * (prescale
+ 1)) - i2c
->bus_clock_khz
;
248 if (abs(diff
) > i2c
->bus_clock_khz
/ 10) {
250 "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
251 i2c
->ip_clock_khz
, i2c
->bus_clock_khz
);
255 oc_setreg(i2c
, OCI2C_PRELOW
, prescale
& 0xff);
256 oc_setreg(i2c
, OCI2C_PREHIGH
, prescale
>> 8);
258 /* Init the device */
259 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_IACK
);
260 oc_setreg(i2c
, OCI2C_CONTROL
, ctrl
| OCI2C_CTRL_IEN
| OCI2C_CTRL_EN
);
266 static u32
ocores_func(struct i2c_adapter
*adap
)
268 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
271 static const struct i2c_algorithm ocores_algorithm
= {
272 .master_xfer
= ocores_xfer
,
273 .functionality
= ocores_func
,
276 static const struct i2c_adapter ocores_adapter
= {
277 .owner
= THIS_MODULE
,
278 .name
= "i2c-ocores",
279 .class = I2C_CLASS_DEPRECATED
,
280 .algo
= &ocores_algorithm
,
283 static const struct of_device_id ocores_i2c_match
[] = {
285 .compatible
= "opencores,i2c-ocores",
286 .data
= (void *)TYPE_OCORES
,
289 .compatible
= "aeroflexgaisler,i2cmst",
290 .data
= (void *)TYPE_GRLIB
,
294 MODULE_DEVICE_TABLE(of
, ocores_i2c_match
);
297 /* Read and write functions for the GRLIB port of the controller. Registers are
298 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
299 * register. The subsequent registers has their offset decreased accordingly. */
300 static u8
oc_getreg_grlib(struct ocores_i2c
*i2c
, int reg
)
304 if (reg
!= OCI2C_PRELOW
)
306 rd
= ioread32be(i2c
->base
+ (rreg
<< i2c
->reg_shift
));
307 if (reg
== OCI2C_PREHIGH
)
308 return (u8
)(rd
>> 8);
313 static void oc_setreg_grlib(struct ocores_i2c
*i2c
, int reg
, u8 value
)
317 if (reg
!= OCI2C_PRELOW
)
319 if (reg
== OCI2C_PRELOW
|| reg
== OCI2C_PREHIGH
) {
320 curr
= ioread32be(i2c
->base
+ (rreg
<< i2c
->reg_shift
));
321 if (reg
== OCI2C_PRELOW
)
322 wr
= (curr
& 0xff00) | value
;
324 wr
= (((u32
)value
) << 8) | (curr
& 0xff);
328 iowrite32be(wr
, i2c
->base
+ (rreg
<< i2c
->reg_shift
));
331 static int ocores_i2c_of_probe(struct platform_device
*pdev
,
332 struct ocores_i2c
*i2c
)
334 struct device_node
*np
= pdev
->dev
.of_node
;
335 const struct of_device_id
*match
;
338 bool clock_frequency_present
;
340 if (of_property_read_u32(np
, "reg-shift", &i2c
->reg_shift
)) {
341 /* no 'reg-shift', check for deprecated 'regstep' */
342 if (!of_property_read_u32(np
, "regstep", &val
)) {
343 if (!is_power_of_2(val
)) {
344 dev_err(&pdev
->dev
, "invalid regstep %d\n",
348 i2c
->reg_shift
= ilog2(val
);
350 "regstep property deprecated, use reg-shift\n");
354 clock_frequency_present
= !of_property_read_u32(np
, "clock-frequency",
356 i2c
->bus_clock_khz
= 100;
358 i2c
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
360 if (!IS_ERR(i2c
->clk
)) {
361 int ret
= clk_prepare_enable(i2c
->clk
);
365 "clk_prepare_enable failed: %d\n", ret
);
368 i2c
->ip_clock_khz
= clk_get_rate(i2c
->clk
) / 1000;
369 if (clock_frequency_present
)
370 i2c
->bus_clock_khz
= clock_frequency
/ 1000;
373 if (i2c
->ip_clock_khz
== 0) {
374 if (of_property_read_u32(np
, "opencores,ip-clock-frequency",
376 if (!clock_frequency_present
) {
378 "Missing required parameter 'opencores,ip-clock-frequency'\n");
379 clk_disable_unprepare(i2c
->clk
);
382 i2c
->ip_clock_khz
= clock_frequency
/ 1000;
384 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
386 i2c
->ip_clock_khz
= val
/ 1000;
387 if (clock_frequency_present
)
388 i2c
->bus_clock_khz
= clock_frequency
/ 1000;
392 of_property_read_u32(pdev
->dev
.of_node
, "reg-io-width",
395 match
= of_match_node(ocores_i2c_match
, pdev
->dev
.of_node
);
396 if (match
&& (long)match
->data
== TYPE_GRLIB
) {
397 dev_dbg(&pdev
->dev
, "GRLIB variant of i2c-ocores\n");
398 i2c
->setreg
= oc_setreg_grlib
;
399 i2c
->getreg
= oc_getreg_grlib
;
405 #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
408 static int ocores_i2c_probe(struct platform_device
*pdev
)
410 struct ocores_i2c
*i2c
;
411 struct ocores_i2c_platform_data
*pdata
;
412 struct resource
*res
;
417 irq
= platform_get_irq(pdev
, 0);
421 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
425 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
426 i2c
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
427 if (IS_ERR(i2c
->base
))
428 return PTR_ERR(i2c
->base
);
430 pdata
= dev_get_platdata(&pdev
->dev
);
432 i2c
->reg_shift
= pdata
->reg_shift
;
433 i2c
->reg_io_width
= pdata
->reg_io_width
;
434 i2c
->ip_clock_khz
= pdata
->clock_khz
;
435 i2c
->bus_clock_khz
= 100;
437 ret
= ocores_i2c_of_probe(pdev
, i2c
);
442 if (i2c
->reg_io_width
== 0)
443 i2c
->reg_io_width
= 1; /* Set to default value */
445 if (!i2c
->setreg
|| !i2c
->getreg
) {
446 bool be
= pdata
? pdata
->big_endian
:
447 of_device_is_big_endian(pdev
->dev
.of_node
);
449 switch (i2c
->reg_io_width
) {
451 i2c
->setreg
= oc_setreg_8
;
452 i2c
->getreg
= oc_getreg_8
;
456 i2c
->setreg
= be
? oc_setreg_16be
: oc_setreg_16
;
457 i2c
->getreg
= be
? oc_getreg_16be
: oc_getreg_16
;
461 i2c
->setreg
= be
? oc_setreg_32be
: oc_setreg_32
;
462 i2c
->getreg
= be
? oc_getreg_32be
: oc_getreg_32
;
466 dev_err(&pdev
->dev
, "Unsupported I/O width (%d)\n",
473 ret
= ocores_init(&pdev
->dev
, i2c
);
477 init_waitqueue_head(&i2c
->wait
);
478 ret
= devm_request_irq(&pdev
->dev
, irq
, ocores_isr
, 0,
481 dev_err(&pdev
->dev
, "Cannot claim IRQ\n");
485 /* hook up driver to tree */
486 platform_set_drvdata(pdev
, i2c
);
487 i2c
->adap
= ocores_adapter
;
488 i2c_set_adapdata(&i2c
->adap
, i2c
);
489 i2c
->adap
.dev
.parent
= &pdev
->dev
;
490 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
492 /* add i2c adapter to i2c tree */
493 ret
= i2c_add_adapter(&i2c
->adap
);
497 /* add in known devices to the bus */
499 for (i
= 0; i
< pdata
->num_devices
; i
++)
500 i2c_new_device(&i2c
->adap
, pdata
->devices
+ i
);
506 clk_disable_unprepare(i2c
->clk
);
510 static int ocores_i2c_remove(struct platform_device
*pdev
)
512 struct ocores_i2c
*i2c
= platform_get_drvdata(pdev
);
514 /* disable i2c logic */
515 oc_setreg(i2c
, OCI2C_CONTROL
, oc_getreg(i2c
, OCI2C_CONTROL
)
516 & ~(OCI2C_CTRL_EN
|OCI2C_CTRL_IEN
));
518 /* remove adapter & data */
519 i2c_del_adapter(&i2c
->adap
);
521 if (!IS_ERR(i2c
->clk
))
522 clk_disable_unprepare(i2c
->clk
);
527 #ifdef CONFIG_PM_SLEEP
528 static int ocores_i2c_suspend(struct device
*dev
)
530 struct ocores_i2c
*i2c
= dev_get_drvdata(dev
);
531 u8 ctrl
= oc_getreg(i2c
, OCI2C_CONTROL
);
533 /* make sure the device is disabled */
534 oc_setreg(i2c
, OCI2C_CONTROL
, ctrl
& ~(OCI2C_CTRL_EN
|OCI2C_CTRL_IEN
));
536 if (!IS_ERR(i2c
->clk
))
537 clk_disable_unprepare(i2c
->clk
);
541 static int ocores_i2c_resume(struct device
*dev
)
543 struct ocores_i2c
*i2c
= dev_get_drvdata(dev
);
545 if (!IS_ERR(i2c
->clk
)) {
547 int ret
= clk_prepare_enable(i2c
->clk
);
551 "clk_prepare_enable failed: %d\n", ret
);
554 rate
= clk_get_rate(i2c
->clk
) / 1000;
556 i2c
->ip_clock_khz
= rate
;
558 return ocores_init(dev
, i2c
);
561 static SIMPLE_DEV_PM_OPS(ocores_i2c_pm
, ocores_i2c_suspend
, ocores_i2c_resume
);
562 #define OCORES_I2C_PM (&ocores_i2c_pm)
564 #define OCORES_I2C_PM NULL
567 static struct platform_driver ocores_i2c_driver
= {
568 .probe
= ocores_i2c_probe
,
569 .remove
= ocores_i2c_remove
,
571 .name
= "ocores-i2c",
572 .of_match_table
= ocores_i2c_match
,
577 module_platform_driver(ocores_i2c_driver
);
579 MODULE_AUTHOR("Peter Korsgaard <peter@korsgaard.com>");
580 MODULE_DESCRIPTION("OpenCores I2C bus driver");
581 MODULE_LICENSE("GPL");
582 MODULE_ALIAS("platform:ocores-i2c");