2 * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
3 * (http://www.opencores.org/projects.cgi/web/i2c/overview).
5 * Peter Korsgaard <jacmet@sunsite.dk>
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/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 inline u8
oc_getreg_8(struct ocores_i2c
*i2c
, int reg
)
97 return ioread8(i2c
->base
+ (reg
<< i2c
->reg_shift
));
100 static inline u8
oc_getreg_16(struct ocores_i2c
*i2c
, int reg
)
102 return ioread16(i2c
->base
+ (reg
<< i2c
->reg_shift
));
105 static inline u8
oc_getreg_32(struct ocores_i2c
*i2c
, int reg
)
107 return ioread32(i2c
->base
+ (reg
<< i2c
->reg_shift
));
110 static inline void oc_setreg(struct ocores_i2c
*i2c
, int reg
, u8 value
)
112 i2c
->setreg(i2c
, reg
, value
);
115 static inline u8
oc_getreg(struct ocores_i2c
*i2c
, int reg
)
117 return i2c
->getreg(i2c
, reg
);
120 static void ocores_process(struct ocores_i2c
*i2c
)
122 struct i2c_msg
*msg
= i2c
->msg
;
123 u8 stat
= oc_getreg(i2c
, OCI2C_STATUS
);
125 if ((i2c
->state
== STATE_DONE
) || (i2c
->state
== STATE_ERROR
)) {
126 /* stop has been sent */
127 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_IACK
);
133 if (stat
& OCI2C_STAT_ARBLOST
) {
134 i2c
->state
= STATE_ERROR
;
135 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_STOP
);
139 if ((i2c
->state
== STATE_START
) || (i2c
->state
== STATE_WRITE
)) {
141 (msg
->flags
& I2C_M_RD
) ? STATE_READ
: STATE_WRITE
;
143 if (stat
& OCI2C_STAT_NACK
) {
144 i2c
->state
= STATE_ERROR
;
145 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_STOP
);
149 msg
->buf
[i2c
->pos
++] = oc_getreg(i2c
, OCI2C_DATA
);
152 if (i2c
->pos
== msg
->len
) {
158 if (i2c
->nmsgs
) { /* end? */
160 if (!(msg
->flags
& I2C_M_NOSTART
)) {
161 u8 addr
= (msg
->addr
<< 1);
163 if (msg
->flags
& I2C_M_RD
)
166 i2c
->state
= STATE_START
;
168 oc_setreg(i2c
, OCI2C_DATA
, addr
);
169 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_START
);
172 i2c
->state
= (msg
->flags
& I2C_M_RD
)
173 ? STATE_READ
: STATE_WRITE
;
175 i2c
->state
= STATE_DONE
;
176 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_STOP
);
181 if (i2c
->state
== STATE_READ
) {
182 oc_setreg(i2c
, OCI2C_CMD
, i2c
->pos
== (msg
->len
-1) ?
183 OCI2C_CMD_READ_NACK
: OCI2C_CMD_READ_ACK
);
185 oc_setreg(i2c
, OCI2C_DATA
, msg
->buf
[i2c
->pos
++]);
186 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_WRITE
);
190 static irqreturn_t
ocores_isr(int irq
, void *dev_id
)
192 struct ocores_i2c
*i2c
= dev_id
;
199 static int ocores_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
201 struct ocores_i2c
*i2c
= i2c_get_adapdata(adap
);
206 i2c
->state
= STATE_START
;
208 oc_setreg(i2c
, OCI2C_DATA
,
209 (i2c
->msg
->addr
<< 1) |
210 ((i2c
->msg
->flags
& I2C_M_RD
) ? 1:0));
212 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_START
);
214 if (wait_event_timeout(i2c
->wait
, (i2c
->state
== STATE_ERROR
) ||
215 (i2c
->state
== STATE_DONE
), HZ
))
216 return (i2c
->state
== STATE_DONE
) ? num
: -EIO
;
221 static int ocores_init(struct device
*dev
, struct ocores_i2c
*i2c
)
225 u8 ctrl
= oc_getreg(i2c
, OCI2C_CONTROL
);
227 /* make sure the device is disabled */
228 oc_setreg(i2c
, OCI2C_CONTROL
, ctrl
& ~(OCI2C_CTRL_EN
|OCI2C_CTRL_IEN
));
230 prescale
= (i2c
->ip_clock_khz
/ (5 * i2c
->bus_clock_khz
)) - 1;
231 prescale
= clamp(prescale
, 0, 0xffff);
233 diff
= i2c
->ip_clock_khz
/ (5 * (prescale
+ 1)) - i2c
->bus_clock_khz
;
234 if (abs(diff
) > i2c
->bus_clock_khz
/ 10) {
236 "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
237 i2c
->ip_clock_khz
, i2c
->bus_clock_khz
);
241 oc_setreg(i2c
, OCI2C_PRELOW
, prescale
& 0xff);
242 oc_setreg(i2c
, OCI2C_PREHIGH
, prescale
>> 8);
244 /* Init the device */
245 oc_setreg(i2c
, OCI2C_CMD
, OCI2C_CMD_IACK
);
246 oc_setreg(i2c
, OCI2C_CONTROL
, ctrl
| OCI2C_CTRL_IEN
| OCI2C_CTRL_EN
);
252 static u32
ocores_func(struct i2c_adapter
*adap
)
254 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
257 static const struct i2c_algorithm ocores_algorithm
= {
258 .master_xfer
= ocores_xfer
,
259 .functionality
= ocores_func
,
262 static struct i2c_adapter ocores_adapter
= {
263 .owner
= THIS_MODULE
,
264 .name
= "i2c-ocores",
265 .class = I2C_CLASS_DEPRECATED
,
266 .algo
= &ocores_algorithm
,
269 static const struct of_device_id ocores_i2c_match
[] = {
271 .compatible
= "opencores,i2c-ocores",
272 .data
= (void *)TYPE_OCORES
,
275 .compatible
= "aeroflexgaisler,i2cmst",
276 .data
= (void *)TYPE_GRLIB
,
280 MODULE_DEVICE_TABLE(of
, ocores_i2c_match
);
283 /* Read and write functions for the GRLIB port of the controller. Registers are
284 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
285 * register. The subsequent registers has their offset decreased accordingly. */
286 static u8
oc_getreg_grlib(struct ocores_i2c
*i2c
, int reg
)
290 if (reg
!= OCI2C_PRELOW
)
292 rd
= ioread32be(i2c
->base
+ (rreg
<< i2c
->reg_shift
));
293 if (reg
== OCI2C_PREHIGH
)
294 return (u8
)(rd
>> 8);
299 static void oc_setreg_grlib(struct ocores_i2c
*i2c
, int reg
, u8 value
)
303 if (reg
!= OCI2C_PRELOW
)
305 if (reg
== OCI2C_PRELOW
|| reg
== OCI2C_PREHIGH
) {
306 curr
= ioread32be(i2c
->base
+ (rreg
<< i2c
->reg_shift
));
307 if (reg
== OCI2C_PRELOW
)
308 wr
= (curr
& 0xff00) | value
;
310 wr
= (((u32
)value
) << 8) | (curr
& 0xff);
314 iowrite32be(wr
, i2c
->base
+ (rreg
<< i2c
->reg_shift
));
317 static int ocores_i2c_of_probe(struct platform_device
*pdev
,
318 struct ocores_i2c
*i2c
)
320 struct device_node
*np
= pdev
->dev
.of_node
;
321 const struct of_device_id
*match
;
324 bool clock_frequency_present
;
326 if (of_property_read_u32(np
, "reg-shift", &i2c
->reg_shift
)) {
327 /* no 'reg-shift', check for deprecated 'regstep' */
328 if (!of_property_read_u32(np
, "regstep", &val
)) {
329 if (!is_power_of_2(val
)) {
330 dev_err(&pdev
->dev
, "invalid regstep %d\n",
334 i2c
->reg_shift
= ilog2(val
);
336 "regstep property deprecated, use reg-shift\n");
340 clock_frequency_present
= !of_property_read_u32(np
, "clock-frequency",
342 i2c
->bus_clock_khz
= 100;
344 i2c
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
346 if (!IS_ERR(i2c
->clk
)) {
347 int ret
= clk_prepare_enable(i2c
->clk
);
351 "clk_prepare_enable failed: %d\n", ret
);
354 i2c
->ip_clock_khz
= clk_get_rate(i2c
->clk
) / 1000;
355 if (clock_frequency_present
)
356 i2c
->bus_clock_khz
= clock_frequency
/ 1000;
359 if (i2c
->ip_clock_khz
== 0) {
360 if (of_property_read_u32(np
, "opencores,ip-clock-frequency",
362 if (!clock_frequency_present
) {
364 "Missing required parameter 'opencores,ip-clock-frequency'\n");
367 i2c
->ip_clock_khz
= clock_frequency
/ 1000;
369 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
371 i2c
->ip_clock_khz
= val
/ 1000;
372 if (clock_frequency_present
)
373 i2c
->bus_clock_khz
= clock_frequency
/ 1000;
377 of_property_read_u32(pdev
->dev
.of_node
, "reg-io-width",
380 match
= of_match_node(ocores_i2c_match
, pdev
->dev
.of_node
);
381 if (match
&& (long)match
->data
== TYPE_GRLIB
) {
382 dev_dbg(&pdev
->dev
, "GRLIB variant of i2c-ocores\n");
383 i2c
->setreg
= oc_setreg_grlib
;
384 i2c
->getreg
= oc_getreg_grlib
;
390 #define ocores_i2c_of_probe(pdev,i2c) -ENODEV
393 static int ocores_i2c_probe(struct platform_device
*pdev
)
395 struct ocores_i2c
*i2c
;
396 struct ocores_i2c_platform_data
*pdata
;
397 struct resource
*res
;
402 irq
= platform_get_irq(pdev
, 0);
406 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
410 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
411 i2c
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
412 if (IS_ERR(i2c
->base
))
413 return PTR_ERR(i2c
->base
);
415 pdata
= dev_get_platdata(&pdev
->dev
);
417 i2c
->reg_shift
= pdata
->reg_shift
;
418 i2c
->reg_io_width
= pdata
->reg_io_width
;
419 i2c
->ip_clock_khz
= pdata
->clock_khz
;
420 i2c
->bus_clock_khz
= 100;
422 ret
= ocores_i2c_of_probe(pdev
, i2c
);
427 if (i2c
->reg_io_width
== 0)
428 i2c
->reg_io_width
= 1; /* Set to default value */
430 if (!i2c
->setreg
|| !i2c
->getreg
) {
431 switch (i2c
->reg_io_width
) {
433 i2c
->setreg
= oc_setreg_8
;
434 i2c
->getreg
= oc_getreg_8
;
438 i2c
->setreg
= oc_setreg_16
;
439 i2c
->getreg
= oc_getreg_16
;
443 i2c
->setreg
= oc_setreg_32
;
444 i2c
->getreg
= oc_getreg_32
;
448 dev_err(&pdev
->dev
, "Unsupported I/O width (%d)\n",
454 ret
= ocores_init(&pdev
->dev
, i2c
);
458 init_waitqueue_head(&i2c
->wait
);
459 ret
= devm_request_irq(&pdev
->dev
, irq
, ocores_isr
, 0,
462 dev_err(&pdev
->dev
, "Cannot claim IRQ\n");
466 /* hook up driver to tree */
467 platform_set_drvdata(pdev
, i2c
);
468 i2c
->adap
= ocores_adapter
;
469 i2c_set_adapdata(&i2c
->adap
, i2c
);
470 i2c
->adap
.dev
.parent
= &pdev
->dev
;
471 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
473 /* add i2c adapter to i2c tree */
474 ret
= i2c_add_adapter(&i2c
->adap
);
476 dev_err(&pdev
->dev
, "Failed to add adapter\n");
480 /* add in known devices to the bus */
482 for (i
= 0; i
< pdata
->num_devices
; i
++)
483 i2c_new_device(&i2c
->adap
, pdata
->devices
+ i
);
489 static int ocores_i2c_remove(struct platform_device
*pdev
)
491 struct ocores_i2c
*i2c
= platform_get_drvdata(pdev
);
493 /* disable i2c logic */
494 oc_setreg(i2c
, OCI2C_CONTROL
, oc_getreg(i2c
, OCI2C_CONTROL
)
495 & ~(OCI2C_CTRL_EN
|OCI2C_CTRL_IEN
));
497 /* remove adapter & data */
498 i2c_del_adapter(&i2c
->adap
);
500 if (!IS_ERR(i2c
->clk
))
501 clk_disable_unprepare(i2c
->clk
);
506 #ifdef CONFIG_PM_SLEEP
507 static int ocores_i2c_suspend(struct device
*dev
)
509 struct ocores_i2c
*i2c
= dev_get_drvdata(dev
);
510 u8 ctrl
= oc_getreg(i2c
, OCI2C_CONTROL
);
512 /* make sure the device is disabled */
513 oc_setreg(i2c
, OCI2C_CONTROL
, ctrl
& ~(OCI2C_CTRL_EN
|OCI2C_CTRL_IEN
));
515 if (!IS_ERR(i2c
->clk
))
516 clk_disable_unprepare(i2c
->clk
);
520 static int ocores_i2c_resume(struct device
*dev
)
522 struct ocores_i2c
*i2c
= dev_get_drvdata(dev
);
524 if (!IS_ERR(i2c
->clk
)) {
526 int ret
= clk_prepare_enable(i2c
->clk
);
530 "clk_prepare_enable failed: %d\n", ret
);
533 rate
= clk_get_rate(i2c
->clk
) / 1000;
535 i2c
->ip_clock_khz
= rate
;
537 return ocores_init(dev
, i2c
);
540 static SIMPLE_DEV_PM_OPS(ocores_i2c_pm
, ocores_i2c_suspend
, ocores_i2c_resume
);
541 #define OCORES_I2C_PM (&ocores_i2c_pm)
543 #define OCORES_I2C_PM NULL
546 static struct platform_driver ocores_i2c_driver
= {
547 .probe
= ocores_i2c_probe
,
548 .remove
= ocores_i2c_remove
,
550 .name
= "ocores-i2c",
551 .of_match_table
= ocores_i2c_match
,
556 module_platform_driver(ocores_i2c_driver
);
558 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
559 MODULE_DESCRIPTION("OpenCores I2C bus driver");
560 MODULE_LICENSE("GPL");
561 MODULE_ALIAS("platform:ocores-i2c");