blob: 181e69211e4f785b1c47a81595f0854fa431a65b [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>
25
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/hardware.h>
Kevin Wellsa7d73d82009-11-12 00:25:52 +010027#include <mach/i2c.h>
Vitaly Wool41561f22006-12-10 21:21:29 +010028#include <asm/irq.h>
29#include <asm/uaccess.h>
30
31#define I2C_PNX_TIMEOUT 10 /* msec */
32#define I2C_PNX_SPEED_KHZ 100
33#define I2C_PNX_REGION_SIZE 0x100
Vitaly Wool41561f22006-12-10 21:21:29 +010034
35static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
36{
37 while (timeout > 0 &&
38 (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
39 mdelay(1);
40 timeout--;
41 }
42 return (timeout <= 0);
43}
44
45static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
46{
47 while (timeout > 0 &&
48 (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
49 mdelay(1);
50 timeout--;
51 }
52 return (timeout <= 0);
53}
54
Russell King81d67242009-11-21 12:40:00 +000055static inline void i2c_pnx_arm_timer(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +010056{
Russell King81d67242009-11-21 12:40:00 +000057 struct timer_list *timer = &alg_data->mif.timer;
Vitaly Wool41561f22006-12-10 21:21:29 +010058 int expires = I2C_PNX_TIMEOUT / (1000 / HZ);
59
Kevin Wellsb2f125b2009-11-12 00:28:13 +010060 if (expires <= 1)
61 expires = 2;
62
Vitaly Wool41561f22006-12-10 21:21:29 +010063 del_timer_sync(timer);
64
Russell King81d67242009-11-21 12:40:00 +000065 dev_dbg(&alg_data->adapter.dev, "Timer armed at %lu plus %u jiffies.\n",
Vitaly Wool41561f22006-12-10 21:21:29 +010066 jiffies, expires);
67
68 timer->expires = jiffies + expires;
Russell King81d67242009-11-21 12:40:00 +000069 timer->data = (unsigned long)&alg_data;
Vitaly Wool41561f22006-12-10 21:21:29 +010070
71 add_timer(timer);
72}
73
74/**
75 * i2c_pnx_start - start a device
76 * @slave_addr: slave address
77 * @adap: pointer to adapter structure
78 *
79 * Generate a START signal in the desired mode.
80 */
Russell King81d67242009-11-21 12:40:00 +000081static int i2c_pnx_start(unsigned char slave_addr,
82 struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +010083{
Russell King81d67242009-11-21 12:40:00 +000084 dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +010085 slave_addr, alg_data->mif.mode);
86
87 /* Check for 7 bit slave addresses only */
88 if (slave_addr & ~0x7f) {
Russell King81d67242009-11-21 12:40:00 +000089 dev_err(&alg_data->adapter.dev, "%s: Invalid slave address %x. "
Vitaly Wool41561f22006-12-10 21:21:29 +010090 "Only 7-bit addresses are supported\n",
Russell King81d67242009-11-21 12:40:00 +000091 alg_data->adapter.name, slave_addr);
Vitaly Wool41561f22006-12-10 21:21:29 +010092 return -EINVAL;
93 }
94
95 /* First, make sure bus is idle */
96 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
97 /* Somebody else is monopolizing the bus */
Russell King81d67242009-11-21 12:40:00 +000098 dev_err(&alg_data->adapter.dev, "%s: Bus busy. Slave addr = %02x, "
Vitaly Wool41561f22006-12-10 21:21:29 +010099 "cntrl = %x, stat = %x\n",
Russell King81d67242009-11-21 12:40:00 +0000100 alg_data->adapter.name, slave_addr,
Vitaly Wool41561f22006-12-10 21:21:29 +0100101 ioread32(I2C_REG_CTL(alg_data)),
102 ioread32(I2C_REG_STS(alg_data)));
103 return -EBUSY;
104 } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
105 /* Sorry, we lost the bus */
Russell King81d67242009-11-21 12:40:00 +0000106 dev_err(&alg_data->adapter.dev, "%s: Arbitration failure. "
107 "Slave addr = %02x\n", alg_data->adapter.name, slave_addr);
Vitaly Wool41561f22006-12-10 21:21:29 +0100108 return -EIO;
109 }
110
111 /*
112 * OK, I2C is enabled and we have the bus.
113 * Clear the current TDI and AFI status flags.
114 */
115 iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
116 I2C_REG_STS(alg_data));
117
Russell King81d67242009-11-21 12:40:00 +0000118 dev_dbg(&alg_data->adapter.dev, "%s(): sending %#x\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100119 (slave_addr << 1) | start_bit | alg_data->mif.mode);
120
121 /* Write the slave address, START bit and R/W bit */
122 iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode,
123 I2C_REG_TX(alg_data));
124
Russell King81d67242009-11-21 12:40:00 +0000125 dev_dbg(&alg_data->adapter.dev, "%s(): exit\n", __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100126
127 return 0;
128}
129
130/**
131 * i2c_pnx_stop - stop a device
132 * @adap: pointer to I2C adapter structure
133 *
134 * Generate a STOP signal to terminate the master transaction.
135 */
Russell King81d67242009-11-21 12:40:00 +0000136static void i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100137{
Vitaly Wool41561f22006-12-10 21:21:29 +0100138 /* Only 1 msec max timeout due to interrupt context */
139 long timeout = 1000;
140
Russell King81d67242009-11-21 12:40:00 +0000141 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200142 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100143
144 /* Write a STOP bit to TX FIFO */
145 iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
146
147 /* Wait until the STOP is seen. */
148 while (timeout > 0 &&
149 (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) {
150 /* may be called from interrupt context */
151 udelay(1);
152 timeout--;
153 }
154
Russell King81d67242009-11-21 12:40:00 +0000155 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200156 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100157}
158
159/**
160 * i2c_pnx_master_xmit - transmit data to slave
161 * @adap: pointer to I2C adapter structure
162 *
163 * Sends one byte of data to the slave
164 */
Russell King81d67242009-11-21 12:40:00 +0000165static int i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100166{
Vitaly Wool41561f22006-12-10 21:21:29 +0100167 u32 val;
168
Russell King81d67242009-11-21 12:40:00 +0000169 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200170 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100171
172 if (alg_data->mif.len > 0) {
173 /* We still have something to talk about... */
174 val = *alg_data->mif.buf++;
175
176 if (alg_data->mif.len == 1) {
177 val |= stop_bit;
178 if (!alg_data->last)
179 val |= start_bit;
180 }
181
182 alg_data->mif.len--;
183 iowrite32(val, I2C_REG_TX(alg_data));
184
Russell King81d67242009-11-21 12:40:00 +0000185 dev_dbg(&alg_data->adapter.dev, "%s(): xmit %#x [%d]\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100186 val, alg_data->mif.len + 1);
187
188 if (alg_data->mif.len == 0) {
189 if (alg_data->last) {
190 /* Wait until the STOP is seen. */
191 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
Russell King81d67242009-11-21 12:40:00 +0000192 dev_err(&alg_data->adapter.dev, "The bus is still "
Vitaly Wool41561f22006-12-10 21:21:29 +0100193 "active after timeout\n");
194 }
195 /* Disable master interrupts */
196 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
197 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
198 I2C_REG_CTL(alg_data));
199
200 del_timer_sync(&alg_data->mif.timer);
201
Russell King81d67242009-11-21 12:40:00 +0000202 dev_dbg(&alg_data->adapter.dev, "%s(): Waking up xfer routine.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200203 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100204
205 complete(&alg_data->mif.complete);
206 }
207 } else if (alg_data->mif.len == 0) {
208 /* zero-sized transfer */
Russell King81d67242009-11-21 12:40:00 +0000209 i2c_pnx_stop(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100210
211 /* Disable master interrupts. */
212 iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
213 ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
214 I2C_REG_CTL(alg_data));
215
216 /* Stop timer. */
217 del_timer_sync(&alg_data->mif.timer);
Russell King81d67242009-11-21 12:40:00 +0000218 dev_dbg(&alg_data->adapter.dev, "%s(): Waking up xfer routine after "
Harvey Harrison08882d22008-04-22 22:16:47 +0200219 "zero-xfer.\n", __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100220
221 complete(&alg_data->mif.complete);
222 }
223
Russell King81d67242009-11-21 12:40:00 +0000224 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200225 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100226
227 return 0;
228}
229
230/**
231 * i2c_pnx_master_rcv - receive data from slave
232 * @adap: pointer to I2C adapter structure
233 *
234 * Reads one byte data from the slave
235 */
Russell King81d67242009-11-21 12:40:00 +0000236static int i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100237{
Vitaly Wool41561f22006-12-10 21:21:29 +0100238 unsigned int val = 0;
239 u32 ctl = 0;
240
Russell King81d67242009-11-21 12:40:00 +0000241 dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200242 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100243
244 /* Check, whether there is already data,
245 * or we didn't 'ask' for it yet.
246 */
247 if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
Russell King81d67242009-11-21 12:40:00 +0000248 dev_dbg(&alg_data->adapter.dev, "%s(): Write dummy data to fill "
Harvey Harrison08882d22008-04-22 22:16:47 +0200249 "Rx-fifo...\n", __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100250
251 if (alg_data->mif.len == 1) {
252 /* Last byte, do not acknowledge next rcv. */
253 val |= stop_bit;
254 if (!alg_data->last)
255 val |= start_bit;
256
257 /*
258 * Enable interrupt RFDAIE (data in Rx fifo),
259 * and disable DRMIE (need data for Tx)
260 */
261 ctl = ioread32(I2C_REG_CTL(alg_data));
262 ctl |= mcntrl_rffie | mcntrl_daie;
263 ctl &= ~mcntrl_drmie;
264 iowrite32(ctl, I2C_REG_CTL(alg_data));
265 }
266
267 /*
268 * Now we'll 'ask' for data:
269 * For each byte we want to receive, we must
270 * write a (dummy) byte to the Tx-FIFO.
271 */
272 iowrite32(val, I2C_REG_TX(alg_data));
273
274 return 0;
275 }
276
277 /* Handle data. */
278 if (alg_data->mif.len > 0) {
279 val = ioread32(I2C_REG_RX(alg_data));
280 *alg_data->mif.buf++ = (u8) (val & 0xff);
Russell King81d67242009-11-21 12:40:00 +0000281 dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n", __func__, val,
Vitaly Wool41561f22006-12-10 21:21:29 +0100282 alg_data->mif.len);
283
284 alg_data->mif.len--;
285 if (alg_data->mif.len == 0) {
286 if (alg_data->last)
287 /* Wait until the STOP is seen. */
288 if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
Russell King81d67242009-11-21 12:40:00 +0000289 dev_err(&alg_data->adapter.dev, "The bus is still "
Vitaly Wool41561f22006-12-10 21:21:29 +0100290 "active after timeout\n");
291
292 /* Disable master interrupts */
293 ctl = ioread32(I2C_REG_CTL(alg_data));
294 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
295 mcntrl_drmie | mcntrl_daie);
296 iowrite32(ctl, I2C_REG_CTL(alg_data));
297
298 /* Kill timer. */
299 del_timer_sync(&alg_data->mif.timer);
300 complete(&alg_data->mif.complete);
301 }
302 }
303
Russell King81d67242009-11-21 12:40:00 +0000304 dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200305 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100306
307 return 0;
308}
309
Vitaly Wool6c566fb2007-01-04 13:07:03 +0100310static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
Vitaly Wool41561f22006-12-10 21:21:29 +0100311{
Russell King81d67242009-11-21 12:40:00 +0000312 struct i2c_pnx_algo_data *alg_data = dev_id;
Vitaly Wool41561f22006-12-10 21:21:29 +0100313 u32 stat, ctl;
Vitaly Wool41561f22006-12-10 21:21:29 +0100314
Russell King81d67242009-11-21 12:40:00 +0000315 dev_dbg(&alg_data->adapter.dev, "%s(): mstat = %x mctrl = %x, mode = %d\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200316 __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100317 ioread32(I2C_REG_STS(alg_data)),
318 ioread32(I2C_REG_CTL(alg_data)),
319 alg_data->mif.mode);
320 stat = ioread32(I2C_REG_STS(alg_data));
321
322 /* let's see what kind of event this is */
323 if (stat & mstatus_afi) {
324 /* We lost arbitration in the midst of a transfer */
325 alg_data->mif.ret = -EIO;
326
327 /* Disable master interrupts. */
328 ctl = ioread32(I2C_REG_CTL(alg_data));
329 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
330 mcntrl_drmie);
331 iowrite32(ctl, I2C_REG_CTL(alg_data));
332
333 /* Stop timer, to prevent timeout. */
334 del_timer_sync(&alg_data->mif.timer);
335 complete(&alg_data->mif.complete);
336 } else if (stat & mstatus_nai) {
337 /* Slave did not acknowledge, generate a STOP */
Russell King81d67242009-11-21 12:40:00 +0000338 dev_dbg(&alg_data->adapter.dev, "%s(): "
Vitaly Wool41561f22006-12-10 21:21:29 +0100339 "Slave did not acknowledge, generating a STOP.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200340 __func__);
Russell King81d67242009-11-21 12:40:00 +0000341 i2c_pnx_stop(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100342
343 /* Disable master interrupts. */
344 ctl = ioread32(I2C_REG_CTL(alg_data));
345 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
346 mcntrl_drmie);
347 iowrite32(ctl, I2C_REG_CTL(alg_data));
348
349 /* Our return value. */
350 alg_data->mif.ret = -EIO;
351
352 /* Stop timer, to prevent timeout. */
353 del_timer_sync(&alg_data->mif.timer);
354 complete(&alg_data->mif.complete);
355 } else {
356 /*
357 * Two options:
358 * - Master Tx needs data.
359 * - There is data in the Rx-fifo
360 * The latter is only the case if we have requested for data,
361 * via a dummy write. (See 'i2c_pnx_master_rcv'.)
362 * We therefore check, as a sanity check, whether that interrupt
363 * has been enabled.
364 */
365 if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
366 if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
Russell King81d67242009-11-21 12:40:00 +0000367 i2c_pnx_master_xmit(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100368 } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
Russell King81d67242009-11-21 12:40:00 +0000369 i2c_pnx_master_rcv(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100370 }
371 }
372 }
373
374 /* Clear TDI and AFI bits */
375 stat = ioread32(I2C_REG_STS(alg_data));
376 iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
377
Russell King81d67242009-11-21 12:40:00 +0000378 dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x ctrl = %x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200379 __func__, ioread32(I2C_REG_STS(alg_data)),
Vitaly Wool41561f22006-12-10 21:21:29 +0100380 ioread32(I2C_REG_CTL(alg_data)));
381
382 return IRQ_HANDLED;
383}
384
385static void i2c_pnx_timeout(unsigned long data)
386{
Russell King81d67242009-11-21 12:40:00 +0000387 struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100388 u32 ctl;
389
Russell King81d67242009-11-21 12:40:00 +0000390 dev_err(&alg_data->adapter.dev, "Master timed out. stat = %04x, cntrl = %04x. "
Vitaly Wool41561f22006-12-10 21:21:29 +0100391 "Resetting master...\n",
392 ioread32(I2C_REG_STS(alg_data)),
393 ioread32(I2C_REG_CTL(alg_data)));
394
395 /* Reset master and disable interrupts */
396 ctl = ioread32(I2C_REG_CTL(alg_data));
397 ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
398 iowrite32(ctl, I2C_REG_CTL(alg_data));
399
400 ctl |= mcntrl_reset;
401 iowrite32(ctl, I2C_REG_CTL(alg_data));
402 wait_reset(I2C_PNX_TIMEOUT, alg_data);
403 alg_data->mif.ret = -EIO;
404 complete(&alg_data->mif.complete);
405}
406
Russell King81d67242009-11-21 12:40:00 +0000407static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
Vitaly Wool41561f22006-12-10 21:21:29 +0100408{
Vitaly Wool41561f22006-12-10 21:21:29 +0100409 u32 stat;
410
411 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
Russell King81d67242009-11-21 12:40:00 +0000412 dev_err(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100413 "%s: Bus is still active after xfer. Reset it...\n",
Russell King81d67242009-11-21 12:40:00 +0000414 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100415 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
416 I2C_REG_CTL(alg_data));
417 wait_reset(I2C_PNX_TIMEOUT, alg_data);
418 } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
419 /* If there is data in the fifo's after transfer,
420 * flush fifo's by reset.
421 */
422 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
423 I2C_REG_CTL(alg_data));
424 wait_reset(I2C_PNX_TIMEOUT, alg_data);
425 } else if (stat & mstatus_nai) {
426 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
427 I2C_REG_CTL(alg_data));
428 wait_reset(I2C_PNX_TIMEOUT, alg_data);
429 }
430}
431
432/**
433 * i2c_pnx_xfer - generic transfer entry point
434 * @adap: pointer to I2C adapter structure
435 * @msgs: array of messages
436 * @num: number of messages
437 *
438 * Initiates the transfer
439 */
440static int
441i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
442{
443 struct i2c_msg *pmsg;
444 int rc = 0, completed = 0, i;
445 struct i2c_pnx_algo_data *alg_data = adap->algo_data;
446 u32 stat = ioread32(I2C_REG_STS(alg_data));
447
Russell King81d67242009-11-21 12:40:00 +0000448 dev_dbg(&alg_data->adapter.dev, "%s(): entering: %d messages, stat = %04x.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200449 __func__, num, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100450
Russell King81d67242009-11-21 12:40:00 +0000451 bus_reset_if_active(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100452
453 /* Process transactions in a loop. */
454 for (i = 0; rc >= 0 && i < num; i++) {
455 u8 addr;
456
457 pmsg = &msgs[i];
458 addr = pmsg->addr;
459
460 if (pmsg->flags & I2C_M_TEN) {
Russell King81d67242009-11-21 12:40:00 +0000461 dev_err(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100462 "%s: 10 bits addr not supported!\n",
Russell King81d67242009-11-21 12:40:00 +0000463 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100464 rc = -EINVAL;
465 break;
466 }
467
468 alg_data->mif.buf = pmsg->buf;
469 alg_data->mif.len = pmsg->len;
470 alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
471 I2C_SMBUS_READ : I2C_SMBUS_WRITE;
472 alg_data->mif.ret = 0;
473 alg_data->last = (i == num - 1);
474
Russell King81d67242009-11-21 12:40:00 +0000475 dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n", __func__,
Vitaly Wool41561f22006-12-10 21:21:29 +0100476 alg_data->mif.mode,
477 alg_data->mif.len);
478
Russell King81d67242009-11-21 12:40:00 +0000479 i2c_pnx_arm_timer(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100480
481 /* initialize the completion var */
482 init_completion(&alg_data->mif.complete);
483
484 /* Enable master interrupt */
485 iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
486 mcntrl_naie | mcntrl_drmie,
487 I2C_REG_CTL(alg_data));
488
489 /* Put start-code and slave-address on the bus. */
Russell King81d67242009-11-21 12:40:00 +0000490 rc = i2c_pnx_start(addr, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100491 if (rc < 0)
492 break;
493
494 /* Wait for completion */
495 wait_for_completion(&alg_data->mif.complete);
496
497 if (!(rc = alg_data->mif.ret))
498 completed++;
Russell King81d67242009-11-21 12:40:00 +0000499 dev_dbg(&alg_data->adapter.dev, "%s(): Complete, return code = %d.\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200500 __func__, rc);
Vitaly Wool41561f22006-12-10 21:21:29 +0100501
502 /* Clear TDI and AFI bits in case they are set. */
503 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
Russell King81d67242009-11-21 12:40:00 +0000504 dev_dbg(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100505 "%s: TDI still set... clearing now.\n",
Russell King81d67242009-11-21 12:40:00 +0000506 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100507 iowrite32(stat, I2C_REG_STS(alg_data));
508 }
509 if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
Russell King81d67242009-11-21 12:40:00 +0000510 dev_dbg(&alg_data->adapter.dev,
Vitaly Wool41561f22006-12-10 21:21:29 +0100511 "%s: AFI still set... clearing now.\n",
Russell King81d67242009-11-21 12:40:00 +0000512 alg_data->adapter.name);
Vitaly Wool41561f22006-12-10 21:21:29 +0100513 iowrite32(stat, I2C_REG_STS(alg_data));
514 }
515 }
516
Russell King81d67242009-11-21 12:40:00 +0000517 bus_reset_if_active(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100518
519 /* Cleanup to be sure... */
520 alg_data->mif.buf = NULL;
521 alg_data->mif.len = 0;
522
Russell King81d67242009-11-21 12:40:00 +0000523 dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200524 __func__, ioread32(I2C_REG_STS(alg_data)));
Vitaly Wool41561f22006-12-10 21:21:29 +0100525
526 if (completed != num)
527 return ((rc < 0) ? rc : -EREMOTEIO);
528
529 return num;
530}
531
532static u32 i2c_pnx_func(struct i2c_adapter *adapter)
533{
534 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
535}
536
537static struct i2c_algorithm pnx_algorithm = {
538 .master_xfer = i2c_pnx_xfer,
539 .functionality = i2c_pnx_func,
540};
541
Russell Kinga0dcf192009-11-20 10:50:34 +0000542#ifdef CONFIG_PM
Vitaly Wool41561f22006-12-10 21:21:29 +0100543static int i2c_pnx_controller_suspend(struct platform_device *pdev,
544 pm_message_t state)
545{
Russell King9d7f7362009-11-21 12:25:27 +0000546 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
Russell King0321cb82009-11-20 11:12:26 +0000547
Russell Kingebdbbf22009-11-20 11:44:46 +0000548 /* FIXME: shouldn't this be clk_disable? */
549 clk_enable(alg_data->clk);
Russell King0321cb82009-11-20 11:12:26 +0000550
551 return 0;
Vitaly Wool41561f22006-12-10 21:21:29 +0100552}
553
554static int i2c_pnx_controller_resume(struct platform_device *pdev)
555{
Russell King9d7f7362009-11-21 12:25:27 +0000556 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
Russell King0321cb82009-11-20 11:12:26 +0000557
Russell Kingebdbbf22009-11-20 11:44:46 +0000558 return clk_enable(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100559}
Russell Kinga0dcf192009-11-20 10:50:34 +0000560#else
561#define i2c_pnx_controller_suspend NULL
562#define i2c_pnx_controller_resume NULL
563#endif
Vitaly Wool41561f22006-12-10 21:21:29 +0100564
565static int __devinit i2c_pnx_probe(struct platform_device *pdev)
566{
567 unsigned long tmp;
568 int ret = 0;
569 struct i2c_pnx_algo_data *alg_data;
Russell King6fff3da2009-11-20 12:46:07 +0000570 unsigned long freq;
Vitaly Wool41561f22006-12-10 21:21:29 +0100571 struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data;
572
Russell King9d7f7362009-11-21 12:25:27 +0000573 if (!i2c_pnx || !i2c_pnx->name) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100574 dev_err(&pdev->dev, "%s: no platform data supplied\n",
Harvey Harrison08882d22008-04-22 22:16:47 +0200575 __func__);
Vitaly Wool41561f22006-12-10 21:21:29 +0100576 ret = -EINVAL;
577 goto out;
578 }
579
Russell King44c5d732009-11-21 12:10:54 +0000580 alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
581 if (!alg_data) {
582 ret = -ENOMEM;
583 goto err_kzalloc;
584 }
585
Russell King9d7f7362009-11-21 12:25:27 +0000586 platform_set_drvdata(pdev, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100587
Russell King9d7f7362009-11-21 12:25:27 +0000588 strlcpy(alg_data->adapter.name, i2c_pnx->name,
589 sizeof(alg_data->adapter.name));
590 alg_data->adapter.dev.parent = &pdev->dev;
591 alg_data->adapter.algo = &pnx_algorithm;
592 alg_data->adapter.algo_data = alg_data;
593 alg_data->adapter.nr = pdev->id;
594 alg_data->i2c_pnx = i2c_pnx;
Russell King0321cb82009-11-20 11:12:26 +0000595
596 alg_data->clk = clk_get(&pdev->dev, NULL);
597 if (IS_ERR(alg_data->clk)) {
598 ret = PTR_ERR(alg_data->clk);
599 goto out_drvdata;
600 }
601
Vitaly Wool41561f22006-12-10 21:21:29 +0100602 init_timer(&alg_data->mif.timer);
603 alg_data->mif.timer.function = i2c_pnx_timeout;
Russell King81d67242009-11-21 12:40:00 +0000604 alg_data->mif.timer.data = (unsigned long)alg_data;
Vitaly Wool41561f22006-12-10 21:21:29 +0100605
606 /* Register I/O resource */
Russell King44c5d732009-11-21 12:10:54 +0000607 if (!request_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE,
Julia Lawall449d2c72009-09-18 22:45:51 +0200608 pdev->name)) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100609 dev_err(&pdev->dev,
610 "I/O region 0x%08x for I2C already in use.\n",
Russell King44c5d732009-11-21 12:10:54 +0000611 i2c_pnx->base);
Vitaly Wool41561f22006-12-10 21:21:29 +0100612 ret = -ENODEV;
Russell King0321cb82009-11-20 11:12:26 +0000613 goto out_clkget;
Vitaly Wool41561f22006-12-10 21:21:29 +0100614 }
615
Russell King44c5d732009-11-21 12:10:54 +0000616 alg_data->ioaddr = ioremap(i2c_pnx->base, I2C_PNX_REGION_SIZE);
Russell King88d968b2009-11-21 11:58:36 +0000617 if (!alg_data->ioaddr) {
Vitaly Wool41561f22006-12-10 21:21:29 +0100618 dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
619 ret = -ENOMEM;
620 goto out_release;
621 }
622
Russell Kingebdbbf22009-11-20 11:44:46 +0000623 ret = clk_enable(alg_data->clk);
624 if (ret)
625 goto out_unmap;
Vitaly Wool41561f22006-12-10 21:21:29 +0100626
Russell King6fff3da2009-11-20 12:46:07 +0000627 freq = clk_get_rate(alg_data->clk);
628
Vitaly Wool41561f22006-12-10 21:21:29 +0100629 /*
630 * Clock Divisor High This value is the number of system clocks
631 * the serial clock (SCL) will be high.
632 * For example, if the system clock period is 50 ns and the maximum
633 * desired serial period is 10000 ns (100 kHz), then CLKHI would be
634 * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
635 * programmed into CLKHI will vary from this slightly due to
636 * variations in the output pad's rise and fall times as well as
637 * the deglitching filter length.
638 */
639
Russell King6fff3da2009-11-20 12:46:07 +0000640 tmp = ((freq / 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2;
Vitaly Wool41561f22006-12-10 21:21:29 +0100641 iowrite32(tmp, I2C_REG_CKH(alg_data));
642 iowrite32(tmp, I2C_REG_CKL(alg_data));
643
644 iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
645 if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
646 ret = -ENODEV;
Russell Kingebdbbf22009-11-20 11:44:46 +0000647 goto out_clock;
Vitaly Wool41561f22006-12-10 21:21:29 +0100648 }
649 init_completion(&alg_data->mif.complete);
650
Russell King44c5d732009-11-21 12:10:54 +0000651 ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt,
Russell King81d67242009-11-21 12:40:00 +0000652 0, pdev->name, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100653 if (ret)
654 goto out_clock;
655
656 /* Register this adapter with the I2C subsystem */
Russell King9d7f7362009-11-21 12:25:27 +0000657 ret = i2c_add_numbered_adapter(&alg_data->adapter);
Vitaly Wool41561f22006-12-10 21:21:29 +0100658 if (ret < 0) {
659 dev_err(&pdev->dev, "I2C: Failed to add bus\n");
660 goto out_irq;
661 }
662
663 dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
Russell King9d7f7362009-11-21 12:25:27 +0000664 alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq);
Vitaly Wool41561f22006-12-10 21:21:29 +0100665
666 return 0;
667
668out_irq:
Russell King81d67242009-11-21 12:40:00 +0000669 free_irq(i2c_pnx->irq, alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100670out_clock:
Russell Kingebdbbf22009-11-20 11:44:46 +0000671 clk_disable(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100672out_unmap:
Russell King88d968b2009-11-21 11:58:36 +0000673 iounmap(alg_data->ioaddr);
Vitaly Wool41561f22006-12-10 21:21:29 +0100674out_release:
Russell King44c5d732009-11-21 12:10:54 +0000675 release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
Russell King0321cb82009-11-20 11:12:26 +0000676out_clkget:
677 clk_put(alg_data->clk);
Vitaly Wool41561f22006-12-10 21:21:29 +0100678out_drvdata:
Russell King44c5d732009-11-21 12:10:54 +0000679 kfree(alg_data);
680err_kzalloc:
Vitaly Wool41561f22006-12-10 21:21:29 +0100681 platform_set_drvdata(pdev, NULL);
682out:
683 return ret;
684}
685
686static int __devexit i2c_pnx_remove(struct platform_device *pdev)
687{
Russell King9d7f7362009-11-21 12:25:27 +0000688 struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
689 struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx;
Vitaly Wool41561f22006-12-10 21:21:29 +0100690
Russell King81d67242009-11-21 12:40:00 +0000691 free_irq(i2c_pnx->irq, alg_data);
Russell King9d7f7362009-11-21 12:25:27 +0000692 i2c_del_adapter(&alg_data->adapter);
Russell Kingebdbbf22009-11-20 11:44:46 +0000693 clk_disable(alg_data->clk);
Russell King88d968b2009-11-21 11:58:36 +0000694 iounmap(alg_data->ioaddr);
Russell King44c5d732009-11-21 12:10:54 +0000695 release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE);
Russell King0321cb82009-11-20 11:12:26 +0000696 clk_put(alg_data->clk);
Russell King44c5d732009-11-21 12:10:54 +0000697 kfree(alg_data);
Vitaly Wool41561f22006-12-10 21:21:29 +0100698 platform_set_drvdata(pdev, NULL);
699
700 return 0;
701}
702
703static struct platform_driver i2c_pnx_driver = {
704 .driver = {
705 .name = "pnx-i2c",
706 .owner = THIS_MODULE,
707 },
708 .probe = i2c_pnx_probe,
709 .remove = __devexit_p(i2c_pnx_remove),
710 .suspend = i2c_pnx_controller_suspend,
711 .resume = i2c_pnx_controller_resume,
712};
713
714static int __init i2c_adap_pnx_init(void)
715{
716 return platform_driver_register(&i2c_pnx_driver);
717}
718
719static void __exit i2c_adap_pnx_exit(void)
720{
721 platform_driver_unregister(&i2c_pnx_driver);
722}
723
724MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
725MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
726MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200727MODULE_ALIAS("platform:pnx-i2c");
Vitaly Wool41561f22006-12-10 21:21:29 +0100728
Vitaly Wool41561f22006-12-10 21:21:29 +0100729/* We need to make sure I2C is initialized before USB */
730subsys_initcall(i2c_adap_pnx_init);
Vitaly Wool41561f22006-12-10 21:21:29 +0100731module_exit(i2c_adap_pnx_exit);