blob: b36e21f9fd5315140f8c67372a495672a9b77e58 [file] [log] [blame]
Vitaly Wool41561f22006-12-10 21:21:29 +01001/*
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>
Kevin Wellsa7d73d82009-11-12 00:25:52 +010022#include <linux/io.h>
Russell King0321cb82009-11-20 11:12:26 +000023#include <linux/err.h>
24#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Roland Stiggeb41a2162012-04-22 11:59:47 +020026#include <linux/of_i2c.h>
Russell King0321cb82009-11-20 11:12:26 +000027
Roland Stiggeb41a2162012-04-22 11:59:47 +020028#define I2C_PNX_TIMEOUT_DEFAULT 10 /* msec */
29#define I2C_PNX_SPEED_KHZ_DEFAULT 100
30#define I2C_PNX_REGION_SIZE 0x100
Vitaly Wool41561f22006-12-10 21:21:29 +010031
Roland Stiggebe460382012-04-22 11:59:47 +020032enum {
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,
Roland Stiggeb3aafe82012-08-08 09:42:31 +020051 mcntrl_drsie = 0x00000010,
52 mcntrl_rffie = 0x00000020,
53 mcntrl_daie = 0x00000040,
Roland Stiggebe460382012-04-22 11:59:47 +020054 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
Roland Stiggeb41a2162012-04-22 11:59:47 +020079static inline int wait_timeout(struct i2c_pnx_algo_data *data)
Vitaly Wool41561f22006-12-10 21:21:29 +010080{
Roland Stiggeb41a2162012-04-22 11:59:47 +020081 long timeout = data->timeout;
Vitaly Wool41561f22006-12-10 21:21:29 +010082 while (timeout > 0 &&
83 (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
84 mdelay(1);
85 timeout--;
86 }
87 return (timeout <= 0);
88}
89
Roland Stiggeb41a2162012-04-22 11:59:47 +020090static inline int wait_reset(struct i2c_pnx_algo_data *data)
Vitaly Wool41561f22006-12-10 21:21:29 +010091{
Roland Stiggeb41a2162012-04-22 11:59:47 +020092 long timeout = data->timeout;
Vitaly Wool41561f22006-12-10 21:21:29 +010093 while (timeout > 0 &&
94 (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
95 mdelay(1);
96 timeout--;
97 }
98 return (timeout <= 0);
99}
100
Russell King81d67242009-11-21 12:40:00 +0000101static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100102{
Russell King81d67242009-11-21 12:40:00 +0000103 struct timer_list *timer = &alg_data->mif.timer;
Roland Stiggeb41a2162012-04-22 11:59:47 +0200104 unsigned long expires = msecs_to_jiffies(alg_data->timeout);
Vitaly Wool41561f22006-12-10 21:21:29 +0100105
Kevin Wellsb2f125b2009-11-12 00:28:13 +0100106 if (expires <= 1)
107 expires = 2;
108
Vitaly Wool41561f22006-12-10 21:21:29 +0100109 del_timer_sync(timer);
110
Russell Kingeed18b52009-11-21 12:58:13 +0000111 dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %lu jiffies.\n",
Vitaly Wool41561f22006-12-10 21:21:29 +0100112 jiffies, expires);
113
114 timer->expires = jiffies + expires;
Wolfram Sang9ddabb02011-04-29 15:30:02 +0200115 timer->data = (unsigned long)alg_data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100116
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 */
Russell King81d67242009-11-21 12:40:00 +0000127static int i2c_pnx_start(unsigned char slave_addr,
128 struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100129{
Russell King81d67242009-11-21 12:40:00 +0000130 dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100131 slave_addr, alg_data->mif.mode);
132
133 /* Check for 7 bit slave addresses only */
134 if (slave_addr & ~0x7f) {
Russell King4be53db2009-11-21 12:46:31 +0000135 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);
Vitaly Wool41561f22006-12-10 21:21:29 +0100138 return -EINVAL;
139 }
140
141 /* First, make sure bus is idle */
Roland Stiggeb41a2162012-04-22 11:59:47 +0200142 if (wait_timeout(alg_data)) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100143 /* Somebody else is monopolizing the bus */
Russell King4be53db2009-11-21 12:46:31 +0000144 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)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100149 return -EBUSY;
150 } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
151 /* Sorry, we lost the bus */
Russell King4be53db2009-11-21 12:46:31 +0000152 dev_err(&alg_data->adapter.dev,
153 "%s: Arbitration failure. Slave addr = %02x\n",
154 alg_data->adapter.name, slave_addr);
Vitaly Wool41561f22006-12-10 21:21:29 +0100155 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
Russell King81d67242009-11-21 12:40:00 +0000165 dev_dbg(&alg_data->adapter.dev, "%s(): sending %#x\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100166 (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
Russell King81d67242009-11-21 12:40:00 +0000172 dev_dbg(&alg_data->adapter.dev, "%s(): exit\n", __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100173
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 */
Russell King81d67242009-11-21 12:40:00 +0000183static void i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100184{
Vitaly Wool41561f22006-12-10 21:21:29 +0100185 /* Only 1 msec max timeout due to interrupt context */
186 long timeout = 1000;
187
Russell King81d67242009-11-21 12:40:00 +0000188 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200189 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100190
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
Russell King81d67242009-11-21 12:40:00 +0000202 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200203 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100204}
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 */
Russell King81d67242009-11-21 12:40:00 +0000212static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100213{
Vitaly Wool41561f22006-12-10 21:21:29 +0100214 u32 val;
215
Russell King81d67242009-11-21 12:40:00 +0000216 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200217 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100218
219 if (alg_data->mif.len > 0) {
220 /* We still have something to talk about... */
221 val = *alg_data->mif.buf++;
222
Kevin Wells28ad3322010-03-16 15:55:37 -0700223 if (alg_data->mif.len == 1)
224 val |= stop_bit;
225
Vitaly Wool41561f22006-12-10 21:21:29 +0100226 alg_data->mif.len--;
227 iowrite32(val, I2C_REG_TX(alg_data));
228
Russell King4be53db2009-11-21 12:46:31 +0000229 dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n",
230 __func__, val, alg_data->mif.len + 1);
Vitaly Wool41561f22006-12-10 21:21:29 +0100231
232 if (alg_data->mif.len == 0) {
233 if (alg_data->last) {
234 /* Wait until the STOP is seen. */
Roland Stiggeb41a2162012-04-22 11:59:47 +0200235 if (wait_timeout(alg_data))
Russell King4be53db2009-11-21 12:46:31 +0000236 dev_err(&alg_data->adapter.dev,
237 "The bus is still active after timeout\n");
Vitaly Wool41561f22006-12-10 21:21:29 +0100238 }
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
Russell King4be53db2009-11-21 12:46:31 +0000246 dev_dbg(&alg_data->adapter.dev,
247 "%s(): Waking up xfer routine.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200248 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100249
250 complete(&alg_data->mif.complete);
251 }
252 } else if (alg_data->mif.len == 0) {
253 /* zero-sized transfer */
Russell King81d67242009-11-21 12:40:00 +0000254 i2c_pnx_stop(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100255
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);
Russell King4be53db2009-11-21 12:46:31 +0000263 dev_dbg(&alg_data->adapter.dev,
264 "%s(): Waking up xfer routine after zero-xfer.\n",
265 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100266
267 complete(&alg_data->mif.complete);
268 }
269
Russell King81d67242009-11-21 12:40:00 +0000270 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200271 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100272
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 */
Russell King81d67242009-11-21 12:40:00 +0000282static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100283{
Vitaly Wool41561f22006-12-10 21:21:29 +0100284 unsigned int val = 0;
285 u32 ctl = 0;
286
Russell King81d67242009-11-21 12:40:00 +0000287 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200288 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100289
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) {
Russell King4be53db2009-11-21 12:46:31 +0000294 dev_dbg(&alg_data->adapter.dev,
295 "%s(): Write dummy data to fill Rx-fifo...\n",
296 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100297
298 if (alg_data->mif.len == 1) {
Kevin Wells28ad3322010-03-16 15:55:37 -0700299 /* Last byte, do not acknowledge next rcv. */
300 val |= stop_bit;
301
Vitaly Wool41561f22006-12-10 21:21:29 +0100302 /*
303 * Enable interrupt RFDAIE (data in Rx fifo),
304 * and disable DRMIE (need data for Tx)
305 */
306 ctl = ioread32(I2C_REG_CTL(alg_data));
307 ctl |= mcntrl_rffie | mcntrl_daie;
308 ctl &= ~mcntrl_drmie;
309 iowrite32(ctl, I2C_REG_CTL(alg_data));
310 }
311
312 /*
313 * Now we'll 'ask' for data:
314 * For each byte we want to receive, we must
315 * write a (dummy) byte to the Tx-FIFO.
316 */
317 iowrite32(val, I2C_REG_TX(alg_data));
318
319 return 0;
320 }
321
322 /* Handle data. */
323 if (alg_data->mif.len > 0) {
324 val = ioread32(I2C_REG_RX(alg_data));
325 *alg_data->mif.buf++ = (u8) (val & 0xff);
Russell King4be53db2009-11-21 12:46:31 +0000326 dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n",
327 __func__, val, alg_data->mif.len);
Vitaly Wool41561f22006-12-10 21:21:29 +0100328
329 alg_data->mif.len--;
330 if (alg_data->mif.len == 0) {
331 if (alg_data->last)
332 /* Wait until the STOP is seen. */
Roland Stiggeb41a2162012-04-22 11:59:47 +0200333 if (wait_timeout(alg_data))
Russell King4be53db2009-11-21 12:46:31 +0000334 dev_err(&alg_data->adapter.dev,
335 "The bus is still active after timeout\n");
Vitaly Wool41561f22006-12-10 21:21:29 +0100336
337 /* Disable master interrupts */
338 ctl = ioread32(I2C_REG_CTL(alg_data));
339 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
340 mcntrl_drmie | mcntrl_daie);
341 iowrite32(ctl, I2C_REG_CTL(alg_data));
342
343 /* Kill timer. */
344 del_timer_sync(&alg_data->mif.timer);
345 complete(&alg_data->mif.complete);
346 }
347 }
348
Russell King81d67242009-11-21 12:40:00 +0000349 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200350 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100351
352 return 0;
353}
354
Vitaly Wool6c566fb72007-01-04 13:07:03 +0100355static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
Vitaly Wool41561f22006-12-10 21:21:29 +0100356{
Russell King81d67242009-11-21 12:40:00 +0000357 struct i2c_pnx_algo_data *alg_data = dev_id;
Vitaly Wool41561f22006-12-10 21:21:29 +0100358 u32 stat, ctl;
Vitaly Wool41561f22006-12-10 21:21:29 +0100359
Russell King4be53db2009-11-21 12:46:31 +0000360 dev_dbg(&alg_data->adapter.dev,
361 "%s(): mstat = %x mctrl = %x, mode = %d\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200362 __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100363 ioread32(I2C_REG_STS(alg_data)),
364 ioread32(I2C_REG_CTL(alg_data)),
365 alg_data->mif.mode);
366 stat = ioread32(I2C_REG_STS(alg_data));
367
368 /* let's see what kind of event this is */
369 if (stat & mstatus_afi) {
370 /* We lost arbitration in the midst of a transfer */
371 alg_data->mif.ret = -EIO;
372
373 /* Disable master interrupts. */
374 ctl = ioread32(I2C_REG_CTL(alg_data));
375 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
376 mcntrl_drmie);
377 iowrite32(ctl, I2C_REG_CTL(alg_data));
378
379 /* Stop timer, to prevent timeout. */
380 del_timer_sync(&alg_data->mif.timer);
381 complete(&alg_data->mif.complete);
382 } else if (stat & mstatus_nai) {
383 /* Slave did not acknowledge, generate a STOP */
Russell King4be53db2009-11-21 12:46:31 +0000384 dev_dbg(&alg_data->adapter.dev,
385 "%s(): Slave did not acknowledge, generating a STOP.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200386 __func__);
Russell King81d67242009-11-21 12:40:00 +0000387 i2c_pnx_stop(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100388
389 /* Disable master interrupts. */
390 ctl = ioread32(I2C_REG_CTL(alg_data));
391 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
392 mcntrl_drmie);
393 iowrite32(ctl, I2C_REG_CTL(alg_data));
394
395 /* Our return value. */
396 alg_data->mif.ret = -EIO;
397
398 /* Stop timer, to prevent timeout. */
399 del_timer_sync(&alg_data->mif.timer);
400 complete(&alg_data->mif.complete);
401 } else {
402 /*
403 * Two options:
404 * - Master Tx needs data.
405 * - There is data in the Rx-fifo
406 * The latter is only the case if we have requested for data,
407 * via a dummy write. (See 'i2c_pnx_master_rcv'.)
408 * We therefore check, as a sanity check, whether that interrupt
409 * has been enabled.
410 */
411 if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
412 if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
Russell King81d67242009-11-21 12:40:00 +0000413 i2c_pnx_master_xmit(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100414 } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
Russell King81d67242009-11-21 12:40:00 +0000415 i2c_pnx_master_rcv(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100416 }
417 }
418 }
419
420 /* Clear TDI and AFI bits */
421 stat = ioread32(I2C_REG_STS(alg_data));
422 iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
423
Russell King4be53db2009-11-21 12:46:31 +0000424 dev_dbg(&alg_data->adapter.dev,
425 "%s(): exiting, stat = %x ctrl = %x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200426 __func__, ioread32(I2C_REG_STS(alg_data)),
Vitaly Wool41561f22006-12-10 21:21:29 +0100427 ioread32(I2C_REG_CTL(alg_data)));
428
429 return IRQ_HANDLED;
430}
431
432static void i2c_pnx_timeout(unsigned long data)
433{
Russell King81d67242009-11-21 12:40:00 +0000434 struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100435 u32 ctl;
436
Russell King4be53db2009-11-21 12:46:31 +0000437 dev_err(&alg_data->adapter.dev,
438 "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
439 ioread32(I2C_REG_STS(alg_data)),
440 ioread32(I2C_REG_CTL(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100441
442 /* Reset master and disable interrupts */
443 ctl = ioread32(I2C_REG_CTL(alg_data));
444 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
445 iowrite32(ctl, I2C_REG_CTL(alg_data));
446
447 ctl |= mcntrl_reset;
448 iowrite32(ctl, I2C_REG_CTL(alg_data));
Roland Stiggeb41a2162012-04-22 11:59:47 +0200449 wait_reset(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100450 alg_data->mif.ret = -EIO;
451 complete(&alg_data->mif.complete);
452}
453
Russell King81d67242009-11-21 12:40:00 +0000454static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100455{
Vitaly Wool41561f22006-12-10 21:21:29 +0100456 u32 stat;
457
458 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
Russell King81d67242009-11-21 12:40:00 +0000459 dev_err(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100460 "%s: Bus is still active after xfer. Reset it...\n",
Russell King4be53db2009-11-21 12:46:31 +0000461 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100462 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
463 I2C_REG_CTL(alg_data));
Roland Stiggeb41a2162012-04-22 11:59:47 +0200464 wait_reset(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100465 } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
466 /* If there is data in the fifo's after transfer,
467 * flush fifo's by reset.
468 */
469 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
470 I2C_REG_CTL(alg_data));
Roland Stiggeb41a2162012-04-22 11:59:47 +0200471 wait_reset(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100472 } else if (stat & mstatus_nai) {
473 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
474 I2C_REG_CTL(alg_data));
Roland Stiggeb41a2162012-04-22 11:59:47 +0200475 wait_reset(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100476 }
477}
478
479/**
480 * i2c_pnx_xfer - generic transfer entry point
481 * @adap: pointer to I2C adapter structure
482 * @msgs: array of messages
483 * @num: number of messages
484 *
485 * Initiates the transfer
486 */
487static int
488i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
489{
490 struct i2c_msg *pmsg;
491 int rc = 0, completed = 0, i;
492 struct i2c_pnx_algo_data *alg_data = adap->algo_data;
493 u32 stat = ioread32(I2C_REG_STS(alg_data));
494
Russell King4be53db2009-11-21 12:46:31 +0000495 dev_dbg(&alg_data->adapter.dev,
496 "%s(): entering: %d messages, stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200497 __func__, num, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100498
Russell King81d67242009-11-21 12:40:00 +0000499 bus_reset_if_active(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100500
501 /* Process transactions in a loop. */
502 for (i = 0; rc >= 0 && i < num; i++) {
503 u8 addr;
504
505 pmsg = &msgs[i];
506 addr = pmsg->addr;
507
508 if (pmsg->flags & I2C_M_TEN) {
Russell King81d67242009-11-21 12:40:00 +0000509 dev_err(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100510 "%s: 10 bits addr not supported!\n",
Russell King81d67242009-11-21 12:40:00 +0000511 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100512 rc = -EINVAL;
513 break;
514 }
515
516 alg_data->mif.buf = pmsg->buf;
517 alg_data->mif.len = pmsg->len;
518 alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
519 I2C_SMBUS_READ : I2C_SMBUS_WRITE;
520 alg_data->mif.ret = 0;
521 alg_data->last = (i == num - 1);
522
Russell King4be53db2009-11-21 12:46:31 +0000523 dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n",
524 __func__, alg_data->mif.mode, alg_data->mif.len);
Vitaly Wool41561f22006-12-10 21:21:29 +0100525
Russell King81d67242009-11-21 12:40:00 +0000526 i2c_pnx_arm_timer(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100527
528 /* initialize the completion var */
529 init_completion(&alg_data->mif.complete);
530
531 /* Enable master interrupt */
532 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
533 mcntrl_naie | mcntrl_drmie,
534 I2C_REG_CTL(alg_data));
535
536 /* Put start-code and slave-address on the bus. */
Russell King81d67242009-11-21 12:40:00 +0000537 rc = i2c_pnx_start(addr, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100538 if (rc < 0)
539 break;
540
541 /* Wait for completion */
542 wait_for_completion(&alg_data->mif.complete);
543
544 if (!(rc = alg_data->mif.ret))
545 completed++;
Russell King4be53db2009-11-21 12:46:31 +0000546 dev_dbg(&alg_data->adapter.dev,
547 "%s(): Complete, return code = %d.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200548 __func__, rc);
Vitaly Wool41561f22006-12-10 21:21:29 +0100549
550 /* Clear TDI and AFI bits in case they are set. */
551 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
Russell King81d67242009-11-21 12:40:00 +0000552 dev_dbg(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100553 "%s: TDI still set... clearing now.\n",
Russell King81d67242009-11-21 12:40:00 +0000554 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100555 iowrite32(stat, I2C_REG_STS(alg_data));
556 }
557 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
Russell King81d67242009-11-21 12:40:00 +0000558 dev_dbg(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100559 "%s: AFI still set... clearing now.\n",
Russell King81d67242009-11-21 12:40:00 +0000560 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100561 iowrite32(stat, I2C_REG_STS(alg_data));
562 }
563 }
564
Russell King81d67242009-11-21 12:40:00 +0000565 bus_reset_if_active(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100566
567 /* Cleanup to be sure... */
568 alg_data->mif.buf = NULL;
569 alg_data->mif.len = 0;
570
Russell King81d67242009-11-21 12:40:00 +0000571 dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200572 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100573
574 if (completed != num)
575 return ((rc < 0) ? rc : -EREMOTEIO);
576
577 return num;
578}
579
580static u32 i2c_pnx_func(struct i2c_adapter *adapter)
581{
582 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
583}
584
585static struct i2c_algorithm pnx_algorithm = {
586 .master_xfer = i2c_pnx_xfer,
587 .functionality = i2c_pnx_func,
588};
589
Russell Kinga0dcf192009-11-20 10:50:34 +0000590#ifdef CONFIG_PM
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200591static int i2c_pnx_controller_suspend(struct device *dev)
Vitaly Wool41561f22006-12-10 21:21:29 +0100592{
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200593 struct i2c_pnx_algo_data *alg_data = dev_get_drvdata(dev);
Russell King0321cb82009-11-20 11:12:26 +0000594
Roland Stiggec4cea7f2012-04-22 11:59:47 +0200595 clk_disable(alg_data->clk);
Russell King0321cb82009-11-20 11:12:26 +0000596
597 return 0;
Vitaly Wool41561f22006-12-10 21:21:29 +0100598}
599
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200600static int i2c_pnx_controller_resume(struct device *dev)
Vitaly Wool41561f22006-12-10 21:21:29 +0100601{
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200602 struct i2c_pnx_algo_data *alg_data = dev_get_drvdata(dev);
Russell King0321cb82009-11-20 11:12:26 +0000603
Russell Kingebdbbf22009-11-20 11:44:46 +0000604 return clk_enable(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100605}
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200606
607static SIMPLE_DEV_PM_OPS(i2c_pnx_pm,
608 i2c_pnx_controller_suspend, i2c_pnx_controller_resume);
609#define PNX_I2C_PM (&i2c_pnx_pm)
Russell Kinga0dcf192009-11-20 10:50:34 +0000610#else
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200611#define PNX_I2C_PM NULL
Russell Kinga0dcf192009-11-20 10:50:34 +0000612#endif
Vitaly Wool41561f22006-12-10 21:21:29 +0100613
614static int __devinit i2c_pnx_probe(struct platform_device *pdev)
615{
616 unsigned long tmp;
617 int ret = 0;
618 struct i2c_pnx_algo_data *alg_data;
Russell King6fff3da2009-11-20 12:46:07 +0000619 unsigned long freq;
Roland Stigge1451ba32012-04-22 11:59:47 +0200620 struct resource *res;
Roland Stiggeb41a2162012-04-22 11:59:47 +0200621 u32 speed = I2C_PNX_SPEED_KHZ_DEFAULT * 1000;
Vitaly Wool41561f22006-12-10 21:21:29 +0100622
Russell King44c5d732009-11-21 12:10:54 +0000623 alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
624 if (!alg_data) {
625 ret = -ENOMEM;
626 goto err_kzalloc;
627 }
628
Russell King9d7f7362009-11-21 12:25:27 +0000629 platform_set_drvdata(pdev, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100630
Russell King9d7f7362009-11-21 12:25:27 +0000631 alg_data->adapter.dev.parent = &pdev->dev;
632 alg_data->adapter.algo = &pnx_algorithm;
633 alg_data->adapter.algo_data = alg_data;
634 alg_data->adapter.nr = pdev->id;
Russell King0321cb82009-11-20 11:12:26 +0000635
Roland Stiggeb41a2162012-04-22 11:59:47 +0200636 alg_data->timeout = I2C_PNX_TIMEOUT_DEFAULT;
637#ifdef CONFIG_OF
638 alg_data->adapter.dev.of_node = of_node_get(pdev->dev.of_node);
639 if (pdev->dev.of_node) {
640 of_property_read_u32(pdev->dev.of_node, "clock-frequency",
641 &speed);
642 /*
643 * At this point, it is planned to add an OF timeout property.
644 * As soon as there is a consensus about how to call and handle
645 * this, sth. like the following can be put here:
646 *
647 * of_property_read_u32(pdev->dev.of_node, "timeout",
648 * &alg_data->timeout);
649 */
650 }
651#endif
Russell King0321cb82009-11-20 11:12:26 +0000652 alg_data->clk = clk_get(&pdev->dev, NULL);
653 if (IS_ERR(alg_data->clk)) {
654 ret = PTR_ERR(alg_data->clk);
655 goto out_drvdata;
656 }
657
Vitaly Wool41561f22006-12-10 21:21:29 +0100658 init_timer(&alg_data->mif.timer);
659 alg_data->mif.timer.function = i2c_pnx_timeout;
Russell King81d67242009-11-21 12:40:00 +0000660 alg_data->mif.timer.data = (unsigned long)alg_data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100661
Roland Stigge1451ba32012-04-22 11:59:47 +0200662 snprintf(alg_data->adapter.name, sizeof(alg_data->adapter.name),
663 "%s", pdev->name);
664
Vitaly Wool41561f22006-12-10 21:21:29 +0100665 /* Register I/O resource */
Roland Stigge1451ba32012-04-22 11:59:47 +0200666 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
667 if (!res) {
668 dev_err(&pdev->dev, "Unable to get mem resource.\n");
669 ret = -EBUSY;
670 goto out_clkget;
671 }
672 if (!request_mem_region(res->start, I2C_PNX_REGION_SIZE,
Julia Lawall449d2c72009-09-18 22:45:51 +0200673 pdev->name)) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100674 dev_err(&pdev->dev,
675 "I/O region 0x%08x for I2C already in use.\n",
Roland Stigge1451ba32012-04-22 11:59:47 +0200676 res->start);
Roland Stiggeb41a2162012-04-22 11:59:47 +0200677 ret = -ENOMEM;
Russell King0321cb82009-11-20 11:12:26 +0000678 goto out_clkget;
Vitaly Wool41561f22006-12-10 21:21:29 +0100679 }
680
Roland Stigge1451ba32012-04-22 11:59:47 +0200681 alg_data->base = res->start;
682 alg_data->ioaddr = ioremap(res->start, I2C_PNX_REGION_SIZE);
Russell King88d968b2009-11-21 11:58:36 +0000683 if (!alg_data->ioaddr) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100684 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
685 ret = -ENOMEM;
686 goto out_release;
687 }
688
Russell Kingebdbbf22009-11-20 11:44:46 +0000689 ret = clk_enable(alg_data->clk);
690 if (ret)
691 goto out_unmap;
Vitaly Wool41561f22006-12-10 21:21:29 +0100692
Russell King6fff3da2009-11-20 12:46:07 +0000693 freq = clk_get_rate(alg_data->clk);
694
Vitaly Wool41561f22006-12-10 21:21:29 +0100695 /*
696 * Clock Divisor High This value is the number of system clocks
697 * the serial clock (SCL) will be high.
698 * For example, if the system clock period is 50 ns and the maximum
699 * desired serial period is 10000 ns (100 kHz), then CLKHI would be
700 * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
701 * programmed into CLKHI will vary from this slightly due to
702 * variations in the output pad's rise and fall times as well as
703 * the deglitching filter length.
704 */
705
Roland Stiggeb41a2162012-04-22 11:59:47 +0200706 tmp = (freq / speed) / 2 - 2;
Kevin Wellsbe80dba2010-03-16 15:55:36 -0700707 if (tmp > 0x3FF)
708 tmp = 0x3FF;
Vitaly Wool41561f22006-12-10 21:21:29 +0100709 iowrite32(tmp, I2C_REG_CKH(alg_data));
710 iowrite32(tmp, I2C_REG_CKL(alg_data));
711
712 iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
Roland Stiggeb41a2162012-04-22 11:59:47 +0200713 if (wait_reset(alg_data)) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100714 ret = -ENODEV;
Russell Kingebdbbf22009-11-20 11:44:46 +0000715 goto out_clock;
Vitaly Wool41561f22006-12-10 21:21:29 +0100716 }
717 init_completion(&alg_data->mif.complete);
718
Roland Stigge1451ba32012-04-22 11:59:47 +0200719 alg_data->irq = platform_get_irq(pdev, 0);
720 if (alg_data->irq < 0) {
721 dev_err(&pdev->dev, "Failed to get IRQ from platform resource\n");
722 goto out_irq;
723 }
724 ret = request_irq(alg_data->irq, i2c_pnx_interrupt,
Russell King81d67242009-11-21 12:40:00 +0000725 0, pdev->name, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100726 if (ret)
727 goto out_clock;
728
729 /* Register this adapter with the I2C subsystem */
Russell King9d7f7362009-11-21 12:25:27 +0000730 ret = i2c_add_numbered_adapter(&alg_data->adapter);
Vitaly Wool41561f22006-12-10 21:21:29 +0100731 if (ret < 0) {
732 dev_err(&pdev->dev, "I2C: Failed to add bus\n");
733 goto out_irq;
734 }
735
Roland Stiggeb41a2162012-04-22 11:59:47 +0200736 of_i2c_register_devices(&alg_data->adapter);
737
Vitaly Wool41561f22006-12-10 21:21:29 +0100738 dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
Roland Stigge1451ba32012-04-22 11:59:47 +0200739 alg_data->adapter.name, res->start, alg_data->irq);
Vitaly Wool41561f22006-12-10 21:21:29 +0100740
741 return 0;
742
743out_irq:
Roland Stigge1451ba32012-04-22 11:59:47 +0200744 free_irq(alg_data->irq, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100745out_clock:
Russell Kingebdbbf22009-11-20 11:44:46 +0000746 clk_disable(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100747out_unmap:
Russell King88d968b2009-11-21 11:58:36 +0000748 iounmap(alg_data->ioaddr);
Vitaly Wool41561f22006-12-10 21:21:29 +0100749out_release:
Roland Stigge1451ba32012-04-22 11:59:47 +0200750 release_mem_region(res->start, I2C_PNX_REGION_SIZE);
Russell King0321cb82009-11-20 11:12:26 +0000751out_clkget:
752 clk_put(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100753out_drvdata:
Russell King44c5d732009-11-21 12:10:54 +0000754 kfree(alg_data);
755err_kzalloc:
Vitaly Wool41561f22006-12-10 21:21:29 +0100756 platform_set_drvdata(pdev, NULL);
Vitaly Wool41561f22006-12-10 21:21:29 +0100757 return ret;
758}
759
760static int __devexit i2c_pnx_remove(struct platform_device *pdev)
761{
Russell King9d7f7362009-11-21 12:25:27 +0000762 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
Vitaly Wool41561f22006-12-10 21:21:29 +0100763
Roland Stigge1451ba32012-04-22 11:59:47 +0200764 free_irq(alg_data->irq, alg_data);
Russell King9d7f7362009-11-21 12:25:27 +0000765 i2c_del_adapter(&alg_data->adapter);
Russell Kingebdbbf22009-11-20 11:44:46 +0000766 clk_disable(alg_data->clk);
Russell King88d968b2009-11-21 11:58:36 +0000767 iounmap(alg_data->ioaddr);
Roland Stigge1451ba32012-04-22 11:59:47 +0200768 release_mem_region(alg_data->base, I2C_PNX_REGION_SIZE);
Russell King0321cb82009-11-20 11:12:26 +0000769 clk_put(alg_data->clk);
Russell King44c5d732009-11-21 12:10:54 +0000770 kfree(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100771 platform_set_drvdata(pdev, NULL);
772
773 return 0;
774}
775
Roland Stiggeb41a2162012-04-22 11:59:47 +0200776#ifdef CONFIG_OF
777static const struct of_device_id i2c_pnx_of_match[] = {
778 { .compatible = "nxp,pnx-i2c" },
779 { },
780};
781MODULE_DEVICE_TABLE(of, i2c_pnx_of_match);
782#endif
783
Vitaly Wool41561f22006-12-10 21:21:29 +0100784static struct platform_driver i2c_pnx_driver = {
785 .driver = {
786 .name = "pnx-i2c",
787 .owner = THIS_MODULE,
Roland Stiggeb41a2162012-04-22 11:59:47 +0200788 .of_match_table = of_match_ptr(i2c_pnx_of_match),
Rafael J. Wysocki783414b2012-07-11 21:25:17 +0200789 .pm = PNX_I2C_PM,
Vitaly Wool41561f22006-12-10 21:21:29 +0100790 },
791 .probe = i2c_pnx_probe,
792 .remove = __devexit_p(i2c_pnx_remove),
Vitaly Wool41561f22006-12-10 21:21:29 +0100793};
794
795static int __init i2c_adap_pnx_init(void)
796{
797 return platform_driver_register(&i2c_pnx_driver);
798}
799
800static void __exit i2c_adap_pnx_exit(void)
801{
802 platform_driver_unregister(&i2c_pnx_driver);
803}
804
805MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
806MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
807MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200808MODULE_ALIAS("platform:pnx-i2c");
Vitaly Wool41561f22006-12-10 21:21:29 +0100809
Vitaly Wool41561f22006-12-10 21:21:29 +0100810/* We need to make sure I2C is initialized before USB */
811subsys_initcall(i2c_adap_pnx_init);
Vitaly Wool41561f22006-12-10 21:21:29 +0100812module_exit(i2c_adap_pnx_exit);