Merge branch 'timer/cleanup' into late/mvebu2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / i2c / busses / i2c-pnx.c
CommitLineData
41561f28
VW
1/*
2 * Provides I2C support for Philips PNX010x/PNX4008 boards.
3 *
4 * Authors: Dennis Kovalev <dkovalev@ru.mvista.com>
5 * Vitaly Wool <vwool@ru.mvista.com>
6 *
7 * 2004-2006 (c) MontaVista Software, Inc. This file is licensed under
8 * the terms of the GNU General Public License version 2. This program
9 * is licensed "as is" without any warranty of any kind, whether express
10 * or implied.
11 */
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/ioport.h>
16#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/timer.h>
19#include <linux/completion.h>
20#include <linux/platform_device.h>
21#include <linux/i2c-pnx.h>
a7d73d8c 22#include <linux/io.h>
0321cb83
RK
23#include <linux/err.h>
24#include <linux/clk.h>
5a0e3ad6 25#include <linux/slab.h>
b41a216d 26#include <linux/of_i2c.h>
0321cb83 27
b41a216d
RS
28#define I2C_PNX_TIMEOUT_DEFAULT 10 /* msec */
29#define I2C_PNX_SPEED_KHZ_DEFAULT 100
30#define I2C_PNX_REGION_SIZE 0x100
41561f28 31
be460385
RS
32enum {
33 mstatus_tdi = 0x00000001,
34 mstatus_afi = 0x00000002,
35 mstatus_nai = 0x00000004,
36 mstatus_drmi = 0x00000008,
37 mstatus_active = 0x00000020,
38 mstatus_scl = 0x00000040,
39 mstatus_sda = 0x00000080,
40 mstatus_rff = 0x00000100,
41 mstatus_rfe = 0x00000200,
42 mstatus_tff = 0x00000400,
43 mstatus_tfe = 0x00000800,
44};
45
46enum {
47 mcntrl_tdie = 0x00000001,
48 mcntrl_afie = 0x00000002,
49 mcntrl_naie = 0x00000004,
50 mcntrl_drmie = 0x00000008,
b3aafe80
RS
51 mcntrl_drsie = 0x00000010,
52 mcntrl_rffie = 0x00000020,
53 mcntrl_daie = 0x00000040,
be460385
RS
54 mcntrl_tffie = 0x00000080,
55 mcntrl_reset = 0x00000100,
56 mcntrl_cdbmode = 0x00000400,
57};
58
59enum {
60 rw_bit = 1 << 0,
61 start_bit = 1 << 8,
62 stop_bit = 1 << 9,
63};
64
65#define I2C_REG_RX(a) ((a)->ioaddr) /* Rx FIFO reg (RO) */
66#define I2C_REG_TX(a) ((a)->ioaddr) /* Tx FIFO reg (WO) */
67#define I2C_REG_STS(a) ((a)->ioaddr + 0x04) /* Status reg (RO) */
68#define I2C_REG_CTL(a) ((a)->ioaddr + 0x08) /* Ctl reg */
69#define I2C_REG_CKL(a) ((a)->ioaddr + 0x0c) /* Clock divider low */
70#define I2C_REG_CKH(a) ((a)->ioaddr + 0x10) /* Clock divider high */
71#define I2C_REG_ADR(a) ((a)->ioaddr + 0x14) /* I2C address */
72#define I2C_REG_RFL(a) ((a)->ioaddr + 0x18) /* Rx FIFO level (RO) */
73#define I2C_REG_TFL(a) ((a)->ioaddr + 0x1c) /* Tx FIFO level (RO) */
74#define I2C_REG_RXB(a) ((a)->ioaddr + 0x20) /* Num of bytes Rx-ed (RO) */
75#define I2C_REG_TXB(a) ((a)->ioaddr + 0x24) /* Num of bytes Tx-ed (RO) */
76#define I2C_REG_TXS(a) ((a)->ioaddr + 0x28) /* Tx slave FIFO (RO) */
77#define I2C_REG_STFL(a) ((a)->ioaddr + 0x2c) /* Tx slave FIFO level (RO) */
78
b41a216d 79static inline int wait_timeout(struct i2c_pnx_algo_data *data)
41561f28 80{
b41a216d 81 long timeout = data->timeout;
41561f28
VW
82 while (timeout > 0 &&
83 (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
84 mdelay(1);
85 timeout--;
86 }
87 return (timeout <= 0);
88}
89
b41a216d 90static inline int wait_reset(struct i2c_pnx_algo_data *data)
41561f28 91{
b41a216d 92 long timeout = data->timeout;
41561f28
VW
93 while (timeout > 0 &&
94 (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
95 mdelay(1);
96 timeout--;
97 }
98 return (timeout <= 0);
99}
100
81d6724a 101static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
41561f28 102{
81d6724a 103 struct timer_list *timer = &alg_data->mif.timer;
b41a216d 104 unsigned long expires = msecs_to_jiffies(alg_data->timeout);
41561f28 105
b2f125bc
KW
106 if (expires <= 1)
107 expires = 2;
108
41561f28
VW
109 del_timer_sync(timer);
110
eed18b5f 111 dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %lu jiffies.\n",
41561f28
VW
112 jiffies, expires);
113
114 timer->expires = jiffies + expires;
9ddabb05 115 timer->data = (unsigned long)alg_data;
41561f28
VW
116
117 add_timer(timer);
118}
119
120/**
121 * i2c_pnx_start - start a device
122 * @slave_addr: slave address
123 * @adap: pointer to adapter structure
124 *
125 * Generate a START signal in the desired mode.
126 */
81d6724a
RK
127static int i2c_pnx_start(unsigned char slave_addr,
128 struct i2c_pnx_algo_data *alg_data)
41561f28 129{
81d6724a 130 dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
41561f28
VW
131 slave_addr, alg_data->mif.mode);
132
133 /* Check for 7 bit slave addresses only */
134 if (slave_addr & ~0x7f) {
4be53dbe
RK
135 dev_err(&alg_data->adapter.dev,
136 "%s: Invalid slave address %x. Only 7-bit addresses are supported\n",
137 alg_data->adapter.name, slave_addr);
41561f28
VW
138 return -EINVAL;
139 }
140
141 /* First, make sure bus is idle */
b41a216d 142 if (wait_timeout(alg_data)) {
41561f28 143 /* Somebody else is monopolizing the bus */
4be53dbe
RK
144 dev_err(&alg_data->adapter.dev,
145 "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
146 alg_data->adapter.name, slave_addr,
147 ioread32(I2C_REG_CTL(alg_data)),
148 ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
149 return -EBUSY;
150 } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
151 /* Sorry, we lost the bus */
4be53dbe
RK
152 dev_err(&alg_data->adapter.dev,
153 "%s: Arbitration failure. Slave addr = %02x\n",
154 alg_data->adapter.name, slave_addr);
41561f28
VW
155 return -EIO;
156 }
157
158 /*
159 * OK, I2C is enabled and we have the bus.
160 * Clear the current TDI and AFI status flags.
161 */
162 iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
163 I2C_REG_STS(alg_data));
164
81d6724a 165 dev_dbg(&alg_data->adapter.dev, "%s(): sending %#x\n", __func__,
41561f28
VW
166 (slave_addr << 1) | start_bit | alg_data->mif.mode);
167
168 /* Write the slave address, START bit and R/W bit */
169 iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode,
170 I2C_REG_TX(alg_data));
171
81d6724a 172 dev_dbg(&alg_data->adapter.dev, "%s(): exit\n", __func__);
41561f28
VW
173
174 return 0;
175}
176
177/**
178 * i2c_pnx_stop - stop a device
179 * @adap: pointer to I2C adapter structure
180 *
181 * Generate a STOP signal to terminate the master transaction.
182 */
81d6724a 183static void i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
41561f28 184{
41561f28
VW
185 /* Only 1 msec max timeout due to interrupt context */
186 long timeout = 1000;
187
81d6724a 188 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
08882d20 189 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
190
191 /* Write a STOP bit to TX FIFO */
192 iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
193
194 /* Wait until the STOP is seen. */
195 while (timeout > 0 &&
196 (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) {
197 /* may be called from interrupt context */
198 udelay(1);
199 timeout--;
200 }
201
81d6724a 202 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
08882d20 203 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
204}
205
206/**
207 * i2c_pnx_master_xmit - transmit data to slave
208 * @adap: pointer to I2C adapter structure
209 *
210 * Sends one byte of data to the slave
211 */
81d6724a 212static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
41561f28 213{
41561f28
VW
214 u32 val;
215
81d6724a 216 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
08882d20 217 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
218
219 if (alg_data->mif.len > 0) {
220 /* We still have something to talk about... */
221 val = *alg_data->mif.buf++;
222
28ad3321
KW
223 if (alg_data->mif.len == 1)
224 val |= stop_bit;
225
41561f28
VW
226 alg_data->mif.len--;
227 iowrite32(val, I2C_REG_TX(alg_data));
228
4be53dbe
RK
229 dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n",
230 __func__, val, alg_data->mif.len + 1);
41561f28
VW
231
232 if (alg_data->mif.len == 0) {
233 if (alg_data->last) {
234 /* Wait until the STOP is seen. */
b41a216d 235 if (wait_timeout(alg_data))
4be53dbe
RK
236 dev_err(&alg_data->adapter.dev,
237 "The bus is still active after timeout\n");
41561f28
VW
238 }
239 /* Disable master interrupts */
240 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
241 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
242 I2C_REG_CTL(alg_data));
243
244 del_timer_sync(&alg_data->mif.timer);
245
4be53dbe
RK
246 dev_dbg(&alg_data->adapter.dev,
247 "%s(): Waking up xfer routine.\n",
08882d20 248 __func__);
41561f28
VW
249
250 complete(&alg_data->mif.complete);
251 }
252 } else if (alg_data->mif.len == 0) {
253 /* zero-sized transfer */
81d6724a 254 i2c_pnx_stop(alg_data);
41561f28
VW
255
256 /* Disable master interrupts. */
257 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
258 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
259 I2C_REG_CTL(alg_data));
260
261 /* Stop timer. */
262 del_timer_sync(&alg_data->mif.timer);
4be53dbe
RK
263 dev_dbg(&alg_data->adapter.dev,
264 "%s(): Waking up xfer routine after zero-xfer.\n",
265 __func__);
41561f28
VW
266
267 complete(&alg_data->mif.complete);
268 }
269
81d6724a 270 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
08882d20 271 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
272
273 return 0;
274}
275
276/**
277 * i2c_pnx_master_rcv - receive data from slave
278 * @adap: pointer to I2C adapter structure
279 *
280 * Reads one byte data from the slave
281 */
81d6724a 282static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
41561f28 283{
41561f28
VW
284 unsigned int val = 0;
285 u32 ctl = 0;
286
81d6724a 287 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
08882d20 288 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
289
290 /* Check, whether there is already data,
291 * or we didn't 'ask' for it yet.
292 */
293 if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
c076ada4
RS
294 /* 'Asking' is done asynchronously, e.g. dummy TX of several
295 * bytes is done before the first actual RX arrives in FIFO.
296 * Therefore, ordered bytes (via TX) are counted separately.
297 */
298 if (alg_data->mif.order) {
299 dev_dbg(&alg_data->adapter.dev,
300 "%s(): Write dummy data to fill Rx-fifo...\n",
301 __func__);
41561f28 302
c076ada4
RS
303 if (alg_data->mif.order == 1) {
304 /* Last byte, do not acknowledge next rcv. */
305 val |= stop_bit;
306
307 /*
308 * Enable interrupt RFDAIE (data in Rx fifo),
309 * and disable DRMIE (need data for Tx)
310 */
311 ctl = ioread32(I2C_REG_CTL(alg_data));
312 ctl |= mcntrl_rffie | mcntrl_daie;
313 ctl &= ~mcntrl_drmie;
314 iowrite32(ctl, I2C_REG_CTL(alg_data));
315 }
28ad3321 316
41561f28 317 /*
c076ada4
RS
318 * Now we'll 'ask' for data:
319 * For each byte we want to receive, we must
320 * write a (dummy) byte to the Tx-FIFO.
41561f28 321 */
c076ada4
RS
322 iowrite32(val, I2C_REG_TX(alg_data));
323 alg_data->mif.order--;
41561f28 324 }
41561f28
VW
325 return 0;
326 }
327
328 /* Handle data. */
329 if (alg_data->mif.len > 0) {
330 val = ioread32(I2C_REG_RX(alg_data));
331 *alg_data->mif.buf++ = (u8) (val & 0xff);
4be53dbe
RK
332 dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n",
333 __func__, val, alg_data->mif.len);
41561f28
VW
334
335 alg_data->mif.len--;
336 if (alg_data->mif.len == 0) {
337 if (alg_data->last)
338 /* Wait until the STOP is seen. */
b41a216d 339 if (wait_timeout(alg_data))
4be53dbe
RK
340 dev_err(&alg_data->adapter.dev,
341 "The bus is still active after timeout\n");
41561f28
VW
342
343 /* Disable master interrupts */
344 ctl = ioread32(I2C_REG_CTL(alg_data));
345 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
346 mcntrl_drmie | mcntrl_daie);
347 iowrite32(ctl, I2C_REG_CTL(alg_data));
348
349 /* Kill timer. */
350 del_timer_sync(&alg_data->mif.timer);
351 complete(&alg_data->mif.complete);
352 }
353 }
354
81d6724a 355 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
08882d20 356 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
357
358 return 0;
359}
360
6c566fb7 361static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
41561f28 362{
81d6724a 363 struct i2c_pnx_algo_data *alg_data = dev_id;
41561f28 364 u32 stat, ctl;
41561f28 365
4be53dbe
RK
366 dev_dbg(&alg_data->adapter.dev,
367 "%s(): mstat = %x mctrl = %x, mode = %d\n",
08882d20 368 __func__,
41561f28
VW
369 ioread32(I2C_REG_STS(alg_data)),
370 ioread32(I2C_REG_CTL(alg_data)),
371 alg_data->mif.mode);
372 stat = ioread32(I2C_REG_STS(alg_data));
373
374 /* let's see what kind of event this is */
375 if (stat & mstatus_afi) {
376 /* We lost arbitration in the midst of a transfer */
377 alg_data->mif.ret = -EIO;
378
379 /* Disable master interrupts. */
380 ctl = ioread32(I2C_REG_CTL(alg_data));
381 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
382 mcntrl_drmie);
383 iowrite32(ctl, I2C_REG_CTL(alg_data));
384
385 /* Stop timer, to prevent timeout. */
386 del_timer_sync(&alg_data->mif.timer);
387 complete(&alg_data->mif.complete);
388 } else if (stat & mstatus_nai) {
389 /* Slave did not acknowledge, generate a STOP */
4be53dbe
RK
390 dev_dbg(&alg_data->adapter.dev,
391 "%s(): Slave did not acknowledge, generating a STOP.\n",
08882d20 392 __func__);
81d6724a 393 i2c_pnx_stop(alg_data);
41561f28
VW
394
395 /* Disable master interrupts. */
396 ctl = ioread32(I2C_REG_CTL(alg_data));
397 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
398 mcntrl_drmie);
399 iowrite32(ctl, I2C_REG_CTL(alg_data));
400
401 /* Our return value. */
402 alg_data->mif.ret = -EIO;
403
404 /* Stop timer, to prevent timeout. */
405 del_timer_sync(&alg_data->mif.timer);
406 complete(&alg_data->mif.complete);
407 } else {
408 /*
409 * Two options:
410 * - Master Tx needs data.
411 * - There is data in the Rx-fifo
412 * The latter is only the case if we have requested for data,
413 * via a dummy write. (See 'i2c_pnx_master_rcv'.)
414 * We therefore check, as a sanity check, whether that interrupt
415 * has been enabled.
416 */
417 if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
418 if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
81d6724a 419 i2c_pnx_master_xmit(alg_data);
41561f28 420 } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
81d6724a 421 i2c_pnx_master_rcv(alg_data);
41561f28
VW
422 }
423 }
424 }
425
426 /* Clear TDI and AFI bits */
427 stat = ioread32(I2C_REG_STS(alg_data));
428 iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
429
4be53dbe
RK
430 dev_dbg(&alg_data->adapter.dev,
431 "%s(): exiting, stat = %x ctrl = %x.\n",
08882d20 432 __func__, ioread32(I2C_REG_STS(alg_data)),
41561f28
VW
433 ioread32(I2C_REG_CTL(alg_data)));
434
435 return IRQ_HANDLED;
436}
437
438static void i2c_pnx_timeout(unsigned long data)
439{
81d6724a 440 struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
41561f28
VW
441 u32 ctl;
442
4be53dbe
RK
443 dev_err(&alg_data->adapter.dev,
444 "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
445 ioread32(I2C_REG_STS(alg_data)),
446 ioread32(I2C_REG_CTL(alg_data)));
41561f28
VW
447
448 /* Reset master and disable interrupts */
449 ctl = ioread32(I2C_REG_CTL(alg_data));
450 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
451 iowrite32(ctl, I2C_REG_CTL(alg_data));
452
453 ctl |= mcntrl_reset;
454 iowrite32(ctl, I2C_REG_CTL(alg_data));
b41a216d 455 wait_reset(alg_data);
41561f28
VW
456 alg_data->mif.ret = -EIO;
457 complete(&alg_data->mif.complete);
458}
459
81d6724a 460static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
41561f28 461{
41561f28
VW
462 u32 stat;
463
464 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
81d6724a 465 dev_err(&alg_data->adapter.dev,
41561f28 466 "%s: Bus is still active after xfer. Reset it...\n",
4be53dbe 467 alg_data->adapter.name);
41561f28
VW
468 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
469 I2C_REG_CTL(alg_data));
b41a216d 470 wait_reset(alg_data);
41561f28
VW
471 } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
472 /* If there is data in the fifo's after transfer,
473 * flush fifo's by reset.
474 */
475 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
476 I2C_REG_CTL(alg_data));
b41a216d 477 wait_reset(alg_data);
41561f28
VW
478 } else if (stat & mstatus_nai) {
479 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
480 I2C_REG_CTL(alg_data));
b41a216d 481 wait_reset(alg_data);
41561f28
VW
482 }
483}
484
485/**
486 * i2c_pnx_xfer - generic transfer entry point
487 * @adap: pointer to I2C adapter structure
488 * @msgs: array of messages
489 * @num: number of messages
490 *
491 * Initiates the transfer
492 */
493static int
494i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
495{
496 struct i2c_msg *pmsg;
497 int rc = 0, completed = 0, i;
498 struct i2c_pnx_algo_data *alg_data = adap->algo_data;
499 u32 stat = ioread32(I2C_REG_STS(alg_data));
500
4be53dbe
RK
501 dev_dbg(&alg_data->adapter.dev,
502 "%s(): entering: %d messages, stat = %04x.\n",
08882d20 503 __func__, num, ioread32(I2C_REG_STS(alg_data)));
41561f28 504
81d6724a 505 bus_reset_if_active(alg_data);
41561f28
VW
506
507 /* Process transactions in a loop. */
508 for (i = 0; rc >= 0 && i < num; i++) {
509 u8 addr;
510
511 pmsg = &msgs[i];
512 addr = pmsg->addr;
513
514 if (pmsg->flags & I2C_M_TEN) {
81d6724a 515 dev_err(&alg_data->adapter.dev,
41561f28 516 "%s: 10 bits addr not supported!\n",
81d6724a 517 alg_data->adapter.name);
41561f28
VW
518 rc = -EINVAL;
519 break;
520 }
521
522 alg_data->mif.buf = pmsg->buf;
523 alg_data->mif.len = pmsg->len;
c076ada4 524 alg_data->mif.order = pmsg->len;
41561f28
VW
525 alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
526 I2C_SMBUS_READ : I2C_SMBUS_WRITE;
527 alg_data->mif.ret = 0;
528 alg_data->last = (i == num - 1);
529
4be53dbe
RK
530 dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n",
531 __func__, alg_data->mif.mode, alg_data->mif.len);
41561f28 532
81d6724a 533 i2c_pnx_arm_timer(alg_data);
41561f28
VW
534
535 /* initialize the completion var */
536 init_completion(&alg_data->mif.complete);
537
538 /* Enable master interrupt */
539 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
540 mcntrl_naie | mcntrl_drmie,
541 I2C_REG_CTL(alg_data));
542
543 /* Put start-code and slave-address on the bus. */
81d6724a 544 rc = i2c_pnx_start(addr, alg_data);
41561f28
VW
545 if (rc < 0)
546 break;
547
548 /* Wait for completion */
549 wait_for_completion(&alg_data->mif.complete);
550
551 if (!(rc = alg_data->mif.ret))
552 completed++;
4be53dbe
RK
553 dev_dbg(&alg_data->adapter.dev,
554 "%s(): Complete, return code = %d.\n",
08882d20 555 __func__, rc);
41561f28
VW
556
557 /* Clear TDI and AFI bits in case they are set. */
558 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
81d6724a 559 dev_dbg(&alg_data->adapter.dev,
41561f28 560 "%s: TDI still set... clearing now.\n",
81d6724a 561 alg_data->adapter.name);
41561f28
VW
562 iowrite32(stat, I2C_REG_STS(alg_data));
563 }
564 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
81d6724a 565 dev_dbg(&alg_data->adapter.dev,
41561f28 566 "%s: AFI still set... clearing now.\n",
81d6724a 567 alg_data->adapter.name);
41561f28
VW
568 iowrite32(stat, I2C_REG_STS(alg_data));
569 }
570 }
571
81d6724a 572 bus_reset_if_active(alg_data);
41561f28
VW
573
574 /* Cleanup to be sure... */
575 alg_data->mif.buf = NULL;
576 alg_data->mif.len = 0;
c076ada4 577 alg_data->mif.order = 0;
41561f28 578
81d6724a 579 dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
08882d20 580 __func__, ioread32(I2C_REG_STS(alg_data)));
41561f28
VW
581
582 if (completed != num)
583 return ((rc < 0) ? rc : -EREMOTEIO);
584
585 return num;
586}
587
588static u32 i2c_pnx_func(struct i2c_adapter *adapter)
589{
590 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
591}
592
593static struct i2c_algorithm pnx_algorithm = {
594 .master_xfer = i2c_pnx_xfer,
595 .functionality = i2c_pnx_func,
596};
597
a0dcf19f 598#ifdef CONFIG_PM
783414ba 599static int i2c_pnx_controller_suspend(struct device *dev)
41561f28 600{
783414ba 601 struct i2c_pnx_algo_data *alg_data = dev_get_drvdata(dev);
0321cb83 602
c4cea7fc 603 clk_disable(alg_data->clk);
0321cb83
RK
604
605 return 0;
41561f28
VW
606}
607
783414ba 608static int i2c_pnx_controller_resume(struct device *dev)
41561f28 609{
783414ba 610 struct i2c_pnx_algo_data *alg_data = dev_get_drvdata(dev);
0321cb83 611
ebdbbf20 612 return clk_enable(alg_data->clk);
41561f28 613}
783414ba
RW
614
615static SIMPLE_DEV_PM_OPS(i2c_pnx_pm,
616 i2c_pnx_controller_suspend, i2c_pnx_controller_resume);
617#define PNX_I2C_PM (&i2c_pnx_pm)
a0dcf19f 618#else
783414ba 619#define PNX_I2C_PM NULL
a0dcf19f 620#endif
41561f28 621
0b255e92 622static int i2c_pnx_probe(struct platform_device *pdev)
41561f28
VW
623{
624 unsigned long tmp;
625 int ret = 0;
626 struct i2c_pnx_algo_data *alg_data;
6fff3da9 627 unsigned long freq;
1451ba3a 628 struct resource *res;
b41a216d 629 u32 speed = I2C_PNX_SPEED_KHZ_DEFAULT * 1000;
41561f28 630
44c5d739
RK
631 alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
632 if (!alg_data) {
633 ret = -ENOMEM;
634 goto err_kzalloc;
635 }
636
9d7f7363 637 platform_set_drvdata(pdev, alg_data);
41561f28 638
9d7f7363
RK
639 alg_data->adapter.dev.parent = &pdev->dev;
640 alg_data->adapter.algo = &pnx_algorithm;
641 alg_data->adapter.algo_data = alg_data;
642 alg_data->adapter.nr = pdev->id;
0321cb83 643
b41a216d
RS
644 alg_data->timeout = I2C_PNX_TIMEOUT_DEFAULT;
645#ifdef CONFIG_OF
646 alg_data->adapter.dev.of_node = of_node_get(pdev->dev.of_node);
647 if (pdev->dev.of_node) {
648 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
649 &speed);
650 /*
651 * At this point, it is planned to add an OF timeout property.
652 * As soon as there is a consensus about how to call and handle
653 * this, sth. like the following can be put here:
654 *
655 * of_property_read_u32(pdev->dev.of_node, "timeout",
656 * &alg_data->timeout);
657 */
658 }
659#endif
0321cb83
RK
660 alg_data->clk = clk_get(&pdev->dev, NULL);
661 if (IS_ERR(alg_data->clk)) {
662 ret = PTR_ERR(alg_data->clk);
663 goto out_drvdata;
664 }
665
41561f28
VW
666 init_timer(&alg_data->mif.timer);
667 alg_data->mif.timer.function = i2c_pnx_timeout;
81d6724a 668 alg_data->mif.timer.data = (unsigned long)alg_data;
41561f28 669
1451ba3a
RS
670 snprintf(alg_data->adapter.name, sizeof(alg_data->adapter.name),
671 "%s", pdev->name);
672
41561f28 673 /* Register I/O resource */
1451ba3a
RS
674 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
675 if (!res) {
676 dev_err(&pdev->dev, "Unable to get mem resource.\n");
677 ret = -EBUSY;
678 goto out_clkget;
679 }
680 if (!request_mem_region(res->start, I2C_PNX_REGION_SIZE,
449d2c75 681 pdev->name)) {
41561f28
VW
682 dev_err(&pdev->dev,
683 "I/O region 0x%08x for I2C already in use.\n",
1451ba3a 684 res->start);
b41a216d 685 ret = -ENOMEM;
0321cb83 686 goto out_clkget;
41561f28
VW
687 }
688
1451ba3a
RS
689 alg_data->base = res->start;
690 alg_data->ioaddr = ioremap(res->start, I2C_PNX_REGION_SIZE);
88d968b2 691 if (!alg_data->ioaddr) {
41561f28
VW
692 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
693 ret = -ENOMEM;
694 goto out_release;
695 }
696
ebdbbf20
RK
697 ret = clk_enable(alg_data->clk);
698 if (ret)
699 goto out_unmap;
41561f28 700
6fff3da9
RK
701 freq = clk_get_rate(alg_data->clk);
702
41561f28
VW
703 /*
704 * Clock Divisor High This value is the number of system clocks
705 * the serial clock (SCL) will be high.
706 * For example, if the system clock period is 50 ns and the maximum
707 * desired serial period is 10000 ns (100 kHz), then CLKHI would be
708 * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
709 * programmed into CLKHI will vary from this slightly due to
710 * variations in the output pad's rise and fall times as well as
711 * the deglitching filter length.
712 */
713
b41a216d 714 tmp = (freq / speed) / 2 - 2;
be80dbaa
KW
715 if (tmp > 0x3FF)
716 tmp = 0x3FF;
41561f28
VW
717 iowrite32(tmp, I2C_REG_CKH(alg_data));
718 iowrite32(tmp, I2C_REG_CKL(alg_data));
719
720 iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
b41a216d 721 if (wait_reset(alg_data)) {
41561f28 722 ret = -ENODEV;
ebdbbf20 723 goto out_clock;
41561f28
VW
724 }
725 init_completion(&alg_data->mif.complete);
726
1451ba3a
RS
727 alg_data->irq = platform_get_irq(pdev, 0);
728 if (alg_data->irq < 0) {
729 dev_err(&pdev->dev, "Failed to get IRQ from platform resource\n");
730 goto out_irq;
731 }
732 ret = request_irq(alg_data->irq, i2c_pnx_interrupt,
81d6724a 733 0, pdev->name, alg_data);
41561f28
VW
734 if (ret)
735 goto out_clock;
736
737 /* Register this adapter with the I2C subsystem */
9d7f7363 738 ret = i2c_add_numbered_adapter(&alg_data->adapter);
41561f28
VW
739 if (ret < 0) {
740 dev_err(&pdev->dev, "I2C: Failed to add bus\n");
741 goto out_irq;
742 }
743
b41a216d
RS
744 of_i2c_register_devices(&alg_data->adapter);
745
41561f28 746 dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
1451ba3a 747 alg_data->adapter.name, res->start, alg_data->irq);
41561f28
VW
748
749 return 0;
750
751out_irq:
1451ba3a 752 free_irq(alg_data->irq, alg_data);
41561f28 753out_clock:
ebdbbf20 754 clk_disable(alg_data->clk);
41561f28 755out_unmap:
88d968b2 756 iounmap(alg_data->ioaddr);
41561f28 757out_release:
1451ba3a 758 release_mem_region(res->start, I2C_PNX_REGION_SIZE);
0321cb83
RK
759out_clkget:
760 clk_put(alg_data->clk);
41561f28 761out_drvdata:
44c5d739
RK
762 kfree(alg_data);
763err_kzalloc:
41561f28 764 platform_set_drvdata(pdev, NULL);
41561f28
VW
765 return ret;
766}
767
0b255e92 768static int i2c_pnx_remove(struct platform_device *pdev)
41561f28 769{
9d7f7363 770 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
41561f28 771
1451ba3a 772 free_irq(alg_data->irq, alg_data);
9d7f7363 773 i2c_del_adapter(&alg_data->adapter);
ebdbbf20 774 clk_disable(alg_data->clk);
88d968b2 775 iounmap(alg_data->ioaddr);
1451ba3a 776 release_mem_region(alg_data->base, I2C_PNX_REGION_SIZE);
0321cb83 777 clk_put(alg_data->clk);
44c5d739 778 kfree(alg_data);
41561f28
VW
779 platform_set_drvdata(pdev, NULL);
780
781 return 0;
782}
783
b41a216d
RS
784#ifdef CONFIG_OF
785static const struct of_device_id i2c_pnx_of_match[] = {
786 { .compatible = "nxp,pnx-i2c" },
787 { },
788};
789MODULE_DEVICE_TABLE(of, i2c_pnx_of_match);
790#endif
791
41561f28
VW
792static struct platform_driver i2c_pnx_driver = {
793 .driver = {
794 .name = "pnx-i2c",
795 .owner = THIS_MODULE,
b41a216d 796 .of_match_table = of_match_ptr(i2c_pnx_of_match),
783414ba 797 .pm = PNX_I2C_PM,
41561f28
VW
798 },
799 .probe = i2c_pnx_probe,
0b255e92 800 .remove = i2c_pnx_remove,
41561f28
VW
801};
802
803static int __init i2c_adap_pnx_init(void)
804{
805 return platform_driver_register(&i2c_pnx_driver);
806}
807
808static void __exit i2c_adap_pnx_exit(void)
809{
810 platform_driver_unregister(&i2c_pnx_driver);
811}
812
813MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
814MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
815MODULE_LICENSE("GPL");
add8eda7 816MODULE_ALIAS("platform:pnx-i2c");
41561f28 817
41561f28
VW
818/* We need to make sure I2C is initialized before USB */
819subsys_initcall(i2c_adap_pnx_init);
41561f28 820module_exit(i2c_adap_pnx_exit);