i2c: ocores: Move system PM hooks to the NOIRQ phase
[linux-2.6-block.git] / drivers / i2c / busses / i2c-ocores.c
CommitLineData
2c7e4928 1// SPDX-License-Identifier: GPL-2.0
18f98b1e
PK
2/*
3 * i2c-ocores.c: I2C bus driver for OpenCores I2C controller
a0ccb6b5 4 * (https://opencores.org/project/i2c/overview)
18f98b1e 5 *
5d3a01a2 6 * Peter Korsgaard <peter@korsgaard.com>
18f98b1e 7 *
a000b8c1
AL
8 * Support for the GRLIB port of the controller by
9 * Andreas Larsson <andreas@gaisler.com>
18f98b1e
PK
10 */
11
e961a094 12#include <linux/clk.h>
69c8c0c0 13#include <linux/delay.h>
84dbf809 14#include <linux/err.h>
18f98b1e
PK
15#include <linux/kernel.h>
16#include <linux/module.h>
18f98b1e
PK
17#include <linux/errno.h>
18#include <linux/platform_device.h>
19#include <linux/i2c.h>
20#include <linux/interrupt.h>
21#include <linux/wait.h>
985ecf00 22#include <linux/platform_data/i2c-ocores.h>
5a0e3ad6 23#include <linux/slab.h>
21782180 24#include <linux/io.h>
8bb986a8 25#include <linux/log2.h>
e7663ef5 26#include <linux/spinlock.h>
69c8c0c0
FV
27#include <linux/jiffies.h>
28
088a8a7f
WS
29/*
30 * 'process_lock' exists because ocores_process() and ocores_process_timeout()
31 * can't run in parallel.
e7663ef5 32 */
18f98b1e
PK
33struct ocores_i2c {
34 void __iomem *base;
809445d4 35 int iobase;
8bb986a8 36 u32 reg_shift;
7326e38f 37 u32 reg_io_width;
c45d4ba8 38 unsigned long flags;
18f98b1e
PK
39 wait_queue_head_t wait;
40 struct i2c_adapter adap;
41 struct i2c_msg *msg;
42 int pos;
43 int nmsgs;
44 int state; /* see STATE_ */
e7663ef5 45 spinlock_t process_lock;
e961a094 46 struct clk *clk;
3a33a854
MF
47 int ip_clock_khz;
48 int bus_clock_khz;
a000b8c1
AL
49 void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
50 u8 (*getreg)(struct ocores_i2c *i2c, int reg);
18f98b1e
PK
51};
52
53/* registers */
54#define OCI2C_PRELOW 0
55#define OCI2C_PREHIGH 1
56#define OCI2C_CONTROL 2
57#define OCI2C_DATA 3
1ded969f
PK
58#define OCI2C_CMD 4 /* write only */
59#define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */
18f98b1e
PK
60
61#define OCI2C_CTRL_IEN 0x40
62#define OCI2C_CTRL_EN 0x80
63
64#define OCI2C_CMD_START 0x91
65#define OCI2C_CMD_STOP 0x41
66#define OCI2C_CMD_READ 0x21
67#define OCI2C_CMD_WRITE 0x11
68#define OCI2C_CMD_READ_ACK 0x21
69#define OCI2C_CMD_READ_NACK 0x29
70#define OCI2C_CMD_IACK 0x01
71
72#define OCI2C_STAT_IF 0x01
73#define OCI2C_STAT_TIP 0x02
74#define OCI2C_STAT_ARBLOST 0x20
75#define OCI2C_STAT_BUSY 0x40
76#define OCI2C_STAT_NACK 0x80
77
78#define STATE_DONE 0
79#define STATE_START 1
80#define STATE_WRITE 2
81#define STATE_READ 3
82#define STATE_ERROR 4
83
a000b8c1
AL
84#define TYPE_OCORES 0
85#define TYPE_GRLIB 1
86
c45d4ba8
SSK
87#define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
88
a000b8c1 89static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value)
18f98b1e 90{
a000b8c1
AL
91 iowrite8(value, i2c->base + (reg << i2c->reg_shift));
92}
93
94static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value)
95{
96 iowrite16(value, i2c->base + (reg << i2c->reg_shift));
97}
98
99static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value)
100{
101 iowrite32(value, i2c->base + (reg << i2c->reg_shift));
102}
103
b2991676
MF
104static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value)
105{
106 iowrite16be(value, i2c->base + (reg << i2c->reg_shift));
107}
108
109static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value)
110{
111 iowrite32be(value, i2c->base + (reg << i2c->reg_shift));
112}
113
a000b8c1
AL
114static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg)
115{
116 return ioread8(i2c->base + (reg << i2c->reg_shift));
117}
118
119static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg)
120{
121 return ioread16(i2c->base + (reg << i2c->reg_shift));
122}
123
124static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg)
125{
126 return ioread32(i2c->base + (reg << i2c->reg_shift));
127}
128
b2991676
MF
129static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg)
130{
131 return ioread16be(i2c->base + (reg << i2c->reg_shift));
132}
133
134static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg)
135{
136 return ioread32be(i2c->base + (reg << i2c->reg_shift));
137}
138
809445d4
AL
139static void oc_setreg_io_8(struct ocores_i2c *i2c, int reg, u8 value)
140{
141 outb(value, i2c->iobase + reg);
142}
143
144static inline u8 oc_getreg_io_8(struct ocores_i2c *i2c, int reg)
145{
146 return inb(i2c->iobase + reg);
147}
148
18f98b1e
PK
149static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value)
150{
a000b8c1 151 i2c->setreg(i2c, reg, value);
18f98b1e
PK
152}
153
154static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg)
155{
a000b8c1 156 return i2c->getreg(i2c, reg);
18f98b1e
PK
157}
158
2dc98346 159static void ocores_process(struct ocores_i2c *i2c, u8 stat)
18f98b1e
PK
160{
161 struct i2c_msg *msg = i2c->msg;
e7663ef5
FV
162 unsigned long flags;
163
164 /*
165 * If we spin here is because we are in timeout, so we are going
166 * to be in STATE_ERROR. See ocores_process_timeout()
167 */
168 spin_lock_irqsave(&i2c->process_lock, flags);
18f98b1e
PK
169
170 if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) {
171 /* stop has been sent */
172 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
173 wake_up(&i2c->wait);
e7663ef5 174 goto out;
18f98b1e
PK
175 }
176
177 /* error? */
178 if (stat & OCI2C_STAT_ARBLOST) {
179 i2c->state = STATE_ERROR;
180 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
e7663ef5 181 goto out;
18f98b1e
PK
182 }
183
184 if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) {
185 i2c->state =
186 (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE;
187
188 if (stat & OCI2C_STAT_NACK) {
189 i2c->state = STATE_ERROR;
190 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
e7663ef5 191 goto out;
18f98b1e 192 }
fac9c29f 193 } else {
18f98b1e 194 msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA);
fac9c29f 195 }
18f98b1e
PK
196
197 /* end of msg? */
198 if (i2c->pos == msg->len) {
199 i2c->nmsgs--;
200 i2c->msg++;
201 i2c->pos = 0;
202 msg = i2c->msg;
203
204 if (i2c->nmsgs) { /* end? */
205 /* send start? */
206 if (!(msg->flags & I2C_M_NOSTART)) {
380a295c 207 u8 addr = i2c_8bit_addr_from_msg(msg);
18f98b1e
PK
208
209 i2c->state = STATE_START;
210
211 oc_setreg(i2c, OCI2C_DATA, addr);
fac9c29f 212 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
e7663ef5 213 goto out;
fac9c29f
FV
214 }
215 i2c->state = (msg->flags & I2C_M_RD)
216 ? STATE_READ : STATE_WRITE;
18f98b1e
PK
217 } else {
218 i2c->state = STATE_DONE;
219 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
e7663ef5 220 goto out;
18f98b1e
PK
221 }
222 }
223
224 if (i2c->state == STATE_READ) {
225 oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ?
226 OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK);
227 } else {
228 oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]);
229 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE);
230 }
e7663ef5
FV
231
232out:
233 spin_unlock_irqrestore(&i2c->process_lock, flags);
18f98b1e
PK
234}
235
7d12e780 236static irqreturn_t ocores_isr(int irq, void *dev_id)
18f98b1e
PK
237{
238 struct ocores_i2c *i2c = dev_id;
2dc98346
FV
239 u8 stat = oc_getreg(i2c, OCI2C_STATUS);
240
c45d4ba8
SSK
241 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) {
242 if ((stat & OCI2C_STAT_IF) && !(stat & OCI2C_STAT_BUSY))
243 return IRQ_NONE;
244 } else if (!(stat & OCI2C_STAT_IF)) {
2dc98346 245 return IRQ_NONE;
c45d4ba8 246 }
2dc98346 247 ocores_process(i2c, stat);
18f98b1e
PK
248
249 return IRQ_HANDLED;
250}
251
e7663ef5 252/**
d4c73d41 253 * ocores_process_timeout() - Process timeout event
e7663ef5
FV
254 * @i2c: ocores I2C device instance
255 */
256static void ocores_process_timeout(struct ocores_i2c *i2c)
257{
258 unsigned long flags;
259
260 spin_lock_irqsave(&i2c->process_lock, flags);
261 i2c->state = STATE_ERROR;
262 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP);
263 spin_unlock_irqrestore(&i2c->process_lock, flags);
264}
265
69c8c0c0 266/**
d4c73d41 267 * ocores_wait() - Wait until something change in a given register
69c8c0c0
FV
268 * @i2c: ocores I2C device instance
269 * @reg: register to query
270 * @mask: bitmask to apply on register value
271 * @val: expected result
272 * @timeout: timeout in jiffies
273 *
274 * Timeout is necessary to avoid to stay here forever when the chip
275 * does not answer correctly.
276 *
277 * Return: 0 on success, -ETIMEDOUT on timeout
278 */
279static int ocores_wait(struct ocores_i2c *i2c,
280 int reg, u8 mask, u8 val,
281 const unsigned long timeout)
282{
283 unsigned long j;
284
285 j = jiffies + timeout;
286 while (1) {
287 u8 status = oc_getreg(i2c, reg);
288
289 if ((status & mask) == val)
290 break;
291
292 if (time_after(jiffies, j))
293 return -ETIMEDOUT;
294 }
295 return 0;
296}
297
298/**
d4c73d41 299 * ocores_poll_wait() - Wait until is possible to process some data
69c8c0c0
FV
300 * @i2c: ocores I2C device instance
301 *
302 * Used when the device is in polling mode (interrupts disabled).
303 *
304 * Return: 0 on success, -ETIMEDOUT on timeout
305 */
306static int ocores_poll_wait(struct ocores_i2c *i2c)
307{
308 u8 mask;
309 int err;
310
311 if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) {
312 /* transfer is over */
313 mask = OCI2C_STAT_BUSY;
314 } else {
315 /* on going transfer */
316 mask = OCI2C_STAT_TIP;
317 /*
318 * We wait for the data to be transferred (8bit),
319 * then we start polling on the ACK/NACK bit
320 */
321 udelay((8 * 1000) / i2c->bus_clock_khz);
322 }
323
324 /*
325 * once we are here we expect to get the expected result immediately
326 * so if after 1ms we timeout then something is broken.
327 */
328 err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1));
329 if (err)
330 dev_warn(i2c->adap.dev.parent,
331 "%s: STATUS timeout, bit 0x%x did not clear in 1ms\n",
332 __func__, mask);
333 return err;
334}
335
336/**
d4c73d41 337 * ocores_process_polling() - It handles an IRQ-less transfer
69c8c0c0
FV
338 * @i2c: ocores I2C device instance
339 *
340 * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same
341 * (only that IRQ are not produced). This means that we can re-use entirely
342 * ocores_isr(), we just add our polling code around it.
343 *
344 * It can run in atomic context
f8160d3b
GH
345 *
346 * Return: 0 on success, -ETIMEDOUT on timeout
69c8c0c0 347 */
f8160d3b 348static int ocores_process_polling(struct ocores_i2c *i2c)
69c8c0c0 349{
f8160d3b
GH
350 irqreturn_t ret;
351 int err = 0;
69c8c0c0 352
f8160d3b 353 while (1) {
69c8c0c0 354 err = ocores_poll_wait(i2c);
f8160d3b 355 if (err)
69c8c0c0 356 break; /* timeout */
69c8c0c0
FV
357
358 ret = ocores_isr(-1, i2c);
359 if (ret == IRQ_NONE)
360 break; /* all messages have been transferred */
c45d4ba8
SSK
361 else {
362 if (i2c->flags & OCORES_FLAG_BROKEN_IRQ)
363 if (i2c->state == STATE_DONE)
364 break;
365 }
69c8c0c0 366 }
f8160d3b
GH
367
368 return err;
69c8c0c0
FV
369}
370
371static int ocores_xfer_core(struct ocores_i2c *i2c,
372 struct i2c_msg *msgs, int num,
373 bool polling)
18f98b1e 374{
f8160d3b 375 int ret = 0;
69c8c0c0
FV
376 u8 ctrl;
377
378 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
379 if (polling)
380 oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN);
381 else
382 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN);
18f98b1e
PK
383
384 i2c->msg = msgs;
385 i2c->pos = 0;
386 i2c->nmsgs = num;
387 i2c->state = STATE_START;
388
30a64757 389 oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg));
18f98b1e
PK
390 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
391
69c8c0c0 392 if (polling) {
f8160d3b 393 ret = ocores_process_polling(i2c);
69c8c0c0 394 } else {
f8160d3b
GH
395 if (wait_event_timeout(i2c->wait,
396 (i2c->state == STATE_ERROR) ||
397 (i2c->state == STATE_DONE), HZ) == 0)
398 ret = -ETIMEDOUT;
399 }
400 if (ret) {
401 ocores_process_timeout(i2c);
402 return ret;
e7663ef5
FV
403 }
404
405 return (i2c->state == STATE_DONE) ? num : -EIO;
18f98b1e
PK
406}
407
69c8c0c0
FV
408static int ocores_xfer_polling(struct i2c_adapter *adap,
409 struct i2c_msg *msgs, int num)
410{
411 return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, true);
412}
413
414static int ocores_xfer(struct i2c_adapter *adap,
415 struct i2c_msg *msgs, int num)
416{
dd7dbf0e 417 return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, false);
69c8c0c0
FV
418}
419
3a33a854 420static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
18f98b1e
PK
421{
422 int prescale;
3a33a854 423 int diff;
18f98b1e
PK
424 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
425
426 /* make sure the device is disabled */
69c8c0c0
FV
427 ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
428 oc_setreg(i2c, OCI2C_CONTROL, ctrl);
18f98b1e 429
3a33a854
MF
430 prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
431 prescale = clamp(prescale, 0, 0xffff);
432
433 diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
434 if (abs(diff) > i2c->bus_clock_khz / 10) {
435 dev_err(dev,
436 "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
437 i2c->ip_clock_khz, i2c->bus_clock_khz);
438 return -EINVAL;
439 }
440
18f98b1e
PK
441 oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
442 oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
443
444 /* Init the device */
445 oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
69c8c0c0 446 oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN);
3a33a854
MF
447
448 return 0;
18f98b1e
PK
449}
450
451
452static u32 ocores_func(struct i2c_adapter *adap)
453{
454 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
455}
456
dd7dbf0e 457static struct i2c_algorithm ocores_algorithm = {
1ce97e07 458 .master_xfer = ocores_xfer,
3d11a12e 459 .master_xfer_atomic = ocores_xfer_polling,
1ce97e07 460 .functionality = ocores_func,
18f98b1e
PK
461};
462
329430cc 463static const struct i2c_adapter ocores_adapter = {
1ce97e07
WS
464 .owner = THIS_MODULE,
465 .name = "i2c-ocores",
466 .class = I2C_CLASS_DEPRECATED,
467 .algo = &ocores_algorithm,
18f98b1e
PK
468};
469
eae45e5d 470static const struct of_device_id ocores_i2c_match[] = {
a000b8c1
AL
471 {
472 .compatible = "opencores,i2c-ocores",
473 .data = (void *)TYPE_OCORES,
474 },
475 {
476 .compatible = "aeroflexgaisler,i2cmst",
477 .data = (void *)TYPE_GRLIB,
478 },
d9ce957d
SSK
479 {
480 .compatible = "sifive,fu540-c000-i2c",
d9ce957d
SSK
481 },
482 {
483 .compatible = "sifive,i2c0",
d9ce957d 484 },
a000b8c1
AL
485 {},
486};
487MODULE_DEVICE_TABLE(of, ocores_i2c_match);
488
049bb69d 489#ifdef CONFIG_OF
fac9c29f
FV
490/*
491 * Read and write functions for the GRLIB port of the controller. Registers are
c5d54744 492 * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one
fac9c29f
FV
493 * register. The subsequent registers have their offsets decreased accordingly.
494 */
c5d54744
AL
495static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg)
496{
497 u32 rd;
498 int rreg = reg;
fac9c29f 499
c5d54744
AL
500 if (reg != OCI2C_PRELOW)
501 rreg--;
502 rd = ioread32be(i2c->base + (rreg << i2c->reg_shift));
503 if (reg == OCI2C_PREHIGH)
504 return (u8)(rd >> 8);
505 else
506 return (u8)rd;
507}
508
509static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value)
510{
511 u32 curr, wr;
512 int rreg = reg;
fac9c29f 513
c5d54744
AL
514 if (reg != OCI2C_PRELOW)
515 rreg--;
516 if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) {
517 curr = ioread32be(i2c->base + (rreg << i2c->reg_shift));
518 if (reg == OCI2C_PRELOW)
519 wr = (curr & 0xff00) | value;
520 else
521 wr = (((u32)value) << 8) | (curr & 0xff);
522 } else {
523 wr = value;
524 }
525 iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift));
526}
527
9ae97a89
J
528static int ocores_i2c_of_probe(struct platform_device *pdev,
529 struct ocores_i2c *i2c)
049bb69d 530{
8bb986a8 531 struct device_node *np = pdev->dev.of_node;
a000b8c1 532 const struct of_device_id *match;
8bb986a8 533 u32 val;
3a33a854
MF
534 u32 clock_frequency;
535 bool clock_frequency_present;
8bb986a8
GR
536
537 if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
538 /* no 'reg-shift', check for deprecated 'regstep' */
539 if (!of_property_read_u32(np, "regstep", &val)) {
540 if (!is_power_of_2(val)) {
541 dev_err(&pdev->dev, "invalid regstep %d\n",
542 val);
543 return -EINVAL;
544 }
545 i2c->reg_shift = ilog2(val);
546 dev_warn(&pdev->dev,
547 "regstep property deprecated, use reg-shift\n");
548 }
049bb69d 549 }
049bb69d 550
3a33a854
MF
551 clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
552 &clock_frequency);
553 i2c->bus_clock_khz = 100;
554
9e1a1ee9
WZ
555 i2c->clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
556 if (IS_ERR(i2c->clk))
557 return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk),
558 "devm_clk_get_optional_enabled failed\n");
559
560 i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000;
561 if (clock_frequency_present)
562 i2c->bus_clock_khz = clock_frequency / 1000;
0d8fb599
WS
563 if (i2c->ip_clock_khz == 0) {
564 if (of_property_read_u32(np, "opencores,ip-clock-frequency",
565 &val)) {
566 if (!clock_frequency_present) {
567 dev_err(&pdev->dev,
568 "Missing required parameter 'opencores,ip-clock-frequency'\n");
569 return -ENODEV;
570 }
571 i2c->ip_clock_khz = clock_frequency / 1000;
572 dev_warn(&pdev->dev,
573 "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
574 } else {
575 i2c->ip_clock_khz = val / 1000;
576 if (clock_frequency_present)
577 i2c->bus_clock_khz = clock_frequency / 1000;
3a33a854 578 }
049bb69d 579 }
049bb69d 580
7326e38f
GR
581 of_property_read_u32(pdev->dev.of_node, "reg-io-width",
582 &i2c->reg_io_width);
a000b8c1
AL
583
584 match = of_match_node(ocores_i2c_match, pdev->dev.of_node);
6beaddf2 585 if (match && (long)match->data == TYPE_GRLIB) {
a000b8c1
AL
586 dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n");
587 i2c->setreg = oc_setreg_grlib;
588 i2c->getreg = oc_getreg_grlib;
589 }
590
049bb69d
JB
591 return 0;
592}
593#else
fac9c29f 594#define ocores_i2c_of_probe(pdev, i2c) -ENODEV
049bb69d 595#endif
18f98b1e 596
0b255e92 597static int ocores_i2c_probe(struct platform_device *pdev)
18f98b1e
PK
598{
599 struct ocores_i2c *i2c;
600 struct ocores_i2c_platform_data *pdata;
f5f35a92
AL
601 struct resource *res;
602 int irq;
18f98b1e 603 int ret;
dd14be4c 604 int i;
18f98b1e 605
47def5b8 606 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
18f98b1e
PK
607 if (!i2c)
608 return -ENOMEM;
609
e7663ef5
FV
610 spin_lock_init(&i2c->process_lock);
611
b7d12a86 612 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
809445d4
AL
613 if (res) {
614 i2c->base = devm_ioremap_resource(&pdev->dev, res);
615 if (IS_ERR(i2c->base))
616 return PTR_ERR(i2c->base);
617 } else {
618 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
619 if (!res)
620 return -EINVAL;
621 i2c->iobase = res->start;
622 if (!devm_request_region(&pdev->dev, res->start,
623 resource_size(res),
624 pdev->name)) {
625 dev_err(&pdev->dev, "Can't get I/O resource.\n");
626 return -EBUSY;
627 }
628 i2c->setreg = oc_setreg_io_8;
629 i2c->getreg = oc_getreg_io_8;
630 }
18f98b1e 631
6d4028c6 632 pdata = dev_get_platdata(&pdev->dev);
049bb69d 633 if (pdata) {
8bb986a8 634 i2c->reg_shift = pdata->reg_shift;
7326e38f 635 i2c->reg_io_width = pdata->reg_io_width;
3a33a854 636 i2c->ip_clock_khz = pdata->clock_khz;
237b5f66
AL
637 if (pdata->bus_khz)
638 i2c->bus_clock_khz = pdata->bus_khz;
639 else
640 i2c->bus_clock_khz = 100;
049bb69d
JB
641 } else {
642 ret = ocores_i2c_of_probe(pdev, i2c);
643 if (ret)
644 return ret;
645 }
646
7326e38f
GR
647 if (i2c->reg_io_width == 0)
648 i2c->reg_io_width = 1; /* Set to default value */
649
a000b8c1 650 if (!i2c->setreg || !i2c->getreg) {
b2991676
MF
651 bool be = pdata ? pdata->big_endian :
652 of_device_is_big_endian(pdev->dev.of_node);
653
a000b8c1
AL
654 switch (i2c->reg_io_width) {
655 case 1:
656 i2c->setreg = oc_setreg_8;
657 i2c->getreg = oc_getreg_8;
658 break;
659
660 case 2:
b2991676
MF
661 i2c->setreg = be ? oc_setreg_16be : oc_setreg_16;
662 i2c->getreg = be ? oc_getreg_16be : oc_getreg_16;
a000b8c1
AL
663 break;
664
665 case 4:
b2991676
MF
666 i2c->setreg = be ? oc_setreg_32be : oc_setreg_32;
667 i2c->getreg = be ? oc_getreg_32be : oc_getreg_32;
a000b8c1
AL
668 break;
669
670 default:
671 dev_err(&pdev->dev, "Unsupported I/O width (%d)\n",
672 i2c->reg_io_width);
9e1a1ee9 673 return -EINVAL;
a000b8c1
AL
674 }
675 }
676
69c8c0c0
FV
677 init_waitqueue_head(&i2c->wait);
678
dc4e10b6 679 irq = platform_get_irq_optional(pdev, 0);
eda03fa0
SSK
680 /*
681 * Since the SoC does have an interrupt, its DT has an interrupt
682 * property - But this should be bypassed as the IRQ logic in this
683 * SoC is broken.
684 */
685 if (of_device_is_compatible(pdev->dev.of_node,
686 "sifive,fu540-c000-i2c")) {
687 i2c->flags |= OCORES_FLAG_BROKEN_IRQ;
688 irq = -ENXIO;
689 }
690
69c8c0c0 691 if (irq == -ENXIO) {
dd7dbf0e 692 ocores_algorithm.master_xfer = ocores_xfer_polling;
69c8c0c0
FV
693 } else {
694 if (irq < 0)
695 return irq;
696 }
697
dd7dbf0e 698 if (ocores_algorithm.master_xfer != ocores_xfer_polling) {
ba919403
FV
699 ret = devm_request_any_context_irq(&pdev->dev, irq,
700 ocores_isr, 0,
701 pdev->name, i2c);
69c8c0c0
FV
702 if (ret) {
703 dev_err(&pdev->dev, "Cannot claim IRQ\n");
9e1a1ee9 704 return ret;
69c8c0c0
FV
705 }
706 }
707
3a33a854
MF
708 ret = ocores_init(&pdev->dev, i2c);
709 if (ret)
9e1a1ee9 710 return ret;
18f98b1e 711
18f98b1e
PK
712 /* hook up driver to tree */
713 platform_set_drvdata(pdev, i2c);
714 i2c->adap = ocores_adapter;
715 i2c_set_adapdata(&i2c->adap, i2c);
716 i2c->adap.dev.parent = &pdev->dev;
049bb69d 717 i2c->adap.dev.of_node = pdev->dev.of_node;
18f98b1e
PK
718
719 /* add i2c adapter to i2c tree */
720 ret = i2c_add_adapter(&i2c->adap);
ea734404 721 if (ret)
9e1a1ee9 722 return ret;
18f98b1e 723
dd14be4c 724 /* add in known devices to the bus */
049bb69d
JB
725 if (pdata) {
726 for (i = 0; i < pdata->num_devices; i++)
7de69dbf 727 i2c_new_client_device(&i2c->adap, pdata->devices + i);
049bb69d 728 }
dd14be4c 729
18f98b1e 730 return 0;
18f98b1e
PK
731}
732
e190a0c3 733static void ocores_i2c_remove(struct platform_device *pdev)
18f98b1e
PK
734{
735 struct ocores_i2c *i2c = platform_get_drvdata(pdev);
fac9c29f 736 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
18f98b1e
PK
737
738 /* disable i2c logic */
fac9c29f
FV
739 ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
740 oc_setreg(i2c, OCI2C_CONTROL, ctrl);
18f98b1e
PK
741
742 /* remove adapter & data */
743 i2c_del_adapter(&i2c->adap);
18f98b1e
PK
744}
745
84603c7c 746static int ocores_i2c_suspend(struct device *dev)
2373c180 747{
84603c7c 748 struct ocores_i2c *i2c = dev_get_drvdata(dev);
2373c180
ML
749 u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
750
751 /* make sure the device is disabled */
fac9c29f
FV
752 ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN);
753 oc_setreg(i2c, OCI2C_CONTROL, ctrl);
2373c180 754
9e1a1ee9 755 clk_disable_unprepare(i2c->clk);
2373c180
ML
756 return 0;
757}
758
84603c7c 759static int ocores_i2c_resume(struct device *dev)
2373c180 760{
84603c7c 761 struct ocores_i2c *i2c = dev_get_drvdata(dev);
9e1a1ee9
WZ
762 unsigned long rate;
763 int ret;
2373c180 764
9e1a1ee9
WZ
765 ret = clk_prepare_enable(i2c->clk);
766 if (ret)
767 return dev_err_probe(dev, ret, "clk_prepare_enable failed\n");
768 rate = clk_get_rate(i2c->clk) / 1000;
769 if (rate)
770 i2c->ip_clock_khz = rate;
3a33a854 771 return ocores_init(dev, i2c);
2373c180 772}
84603c7c 773
382561d1
SH
774static DEFINE_NOIRQ_DEV_PM_OPS(ocores_i2c_pm,
775 ocores_i2c_suspend, ocores_i2c_resume);
2373c180 776
18f98b1e 777static struct platform_driver ocores_i2c_driver = {
2373c180 778 .probe = ocores_i2c_probe,
e190a0c3 779 .remove_new = ocores_i2c_remove,
2373c180 780 .driver = {
18f98b1e 781 .name = "ocores-i2c",
c9e358df 782 .of_match_table = ocores_i2c_match,
0ad93449 783 .pm = pm_sleep_ptr(&ocores_i2c_pm),
18f98b1e
PK
784 },
785};
786
a3664b51 787module_platform_driver(ocores_i2c_driver);
18f98b1e 788
5d3a01a2 789MODULE_AUTHOR("Peter Korsgaard <peter@korsgaard.com>");
18f98b1e
PK
790MODULE_DESCRIPTION("OpenCores I2C bus driver");
791MODULE_LICENSE("GPL");
a3664b51 792MODULE_ALIAS("platform:ocores-i2c");