/kernel/2.6.32_froyo_photon_nightly/drivers/i2c/busses/i2c-pnx.c

http://photon-android.googlecode.com/ · C · 710 lines · 470 code · 118 blank · 122 comment · 64 complexity · 572f92d5cc9e17686e7d104ac59451d2 MD5 · raw file

  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. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/ioport.h>
  15. #include <linux/delay.h>
  16. #include <linux/i2c.h>
  17. #include <linux/timer.h>
  18. #include <linux/completion.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/i2c-pnx.h>
  21. #include <linux/io.h>
  22. #include <mach/hardware.h>
  23. #include <mach/i2c.h>
  24. #include <asm/irq.h>
  25. #include <asm/uaccess.h>
  26. #define I2C_PNX_TIMEOUT 10 /* msec */
  27. #define I2C_PNX_SPEED_KHZ 100
  28. #define I2C_PNX_REGION_SIZE 0x100
  29. #define PNX_DEFAULT_FREQ 13 /* MHz */
  30. static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data)
  31. {
  32. while (timeout > 0 &&
  33. (ioread32(I2C_REG_STS(data)) & mstatus_active)) {
  34. mdelay(1);
  35. timeout--;
  36. }
  37. return (timeout <= 0);
  38. }
  39. static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data)
  40. {
  41. while (timeout > 0 &&
  42. (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) {
  43. mdelay(1);
  44. timeout--;
  45. }
  46. return (timeout <= 0);
  47. }
  48. static inline void i2c_pnx_arm_timer(struct i2c_adapter *adap)
  49. {
  50. struct i2c_pnx_algo_data *data = adap->algo_data;
  51. struct timer_list *timer = &data->mif.timer;
  52. int expires = I2C_PNX_TIMEOUT / (1000 / HZ);
  53. if (expires <= 1)
  54. expires = 2;
  55. del_timer_sync(timer);
  56. dev_dbg(&adap->dev, "Timer armed at %lu plus %u jiffies.\n",
  57. jiffies, expires);
  58. timer->expires = jiffies + expires;
  59. timer->data = (unsigned long)adap;
  60. add_timer(timer);
  61. }
  62. /**
  63. * i2c_pnx_start - start a device
  64. * @slave_addr: slave address
  65. * @adap: pointer to adapter structure
  66. *
  67. * Generate a START signal in the desired mode.
  68. */
  69. static int i2c_pnx_start(unsigned char slave_addr, struct i2c_adapter *adap)
  70. {
  71. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  72. dev_dbg(&adap->dev, "%s(): addr 0x%x mode %d\n", __func__,
  73. slave_addr, alg_data->mif.mode);
  74. /* Check for 7 bit slave addresses only */
  75. if (slave_addr & ~0x7f) {
  76. dev_err(&adap->dev, "%s: Invalid slave address %x. "
  77. "Only 7-bit addresses are supported\n",
  78. adap->name, slave_addr);
  79. return -EINVAL;
  80. }
  81. /* First, make sure bus is idle */
  82. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
  83. /* Somebody else is monopolizing the bus */
  84. dev_err(&adap->dev, "%s: Bus busy. Slave addr = %02x, "
  85. "cntrl = %x, stat = %x\n",
  86. adap->name, slave_addr,
  87. ioread32(I2C_REG_CTL(alg_data)),
  88. ioread32(I2C_REG_STS(alg_data)));
  89. return -EBUSY;
  90. } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) {
  91. /* Sorry, we lost the bus */
  92. dev_err(&adap->dev, "%s: Arbitration failure. "
  93. "Slave addr = %02x\n", adap->name, slave_addr);
  94. return -EIO;
  95. }
  96. /*
  97. * OK, I2C is enabled and we have the bus.
  98. * Clear the current TDI and AFI status flags.
  99. */
  100. iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
  101. I2C_REG_STS(alg_data));
  102. dev_dbg(&adap->dev, "%s(): sending %#x\n", __func__,
  103. (slave_addr << 1) | start_bit | alg_data->mif.mode);
  104. /* Write the slave address, START bit and R/W bit */
  105. iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode,
  106. I2C_REG_TX(alg_data));
  107. dev_dbg(&adap->dev, "%s(): exit\n", __func__);
  108. return 0;
  109. }
  110. /**
  111. * i2c_pnx_stop - stop a device
  112. * @adap: pointer to I2C adapter structure
  113. *
  114. * Generate a STOP signal to terminate the master transaction.
  115. */
  116. static void i2c_pnx_stop(struct i2c_adapter *adap)
  117. {
  118. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  119. /* Only 1 msec max timeout due to interrupt context */
  120. long timeout = 1000;
  121. dev_dbg(&adap->dev, "%s(): entering: stat = %04x.\n",
  122. __func__, ioread32(I2C_REG_STS(alg_data)));
  123. /* Write a STOP bit to TX FIFO */
  124. iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
  125. /* Wait until the STOP is seen. */
  126. while (timeout > 0 &&
  127. (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) {
  128. /* may be called from interrupt context */
  129. udelay(1);
  130. timeout--;
  131. }
  132. dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n",
  133. __func__, ioread32(I2C_REG_STS(alg_data)));
  134. }
  135. /**
  136. * i2c_pnx_master_xmit - transmit data to slave
  137. * @adap: pointer to I2C adapter structure
  138. *
  139. * Sends one byte of data to the slave
  140. */
  141. static int i2c_pnx_master_xmit(struct i2c_adapter *adap)
  142. {
  143. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  144. u32 val;
  145. dev_dbg(&adap->dev, "%s(): entering: stat = %04x.\n",
  146. __func__, ioread32(I2C_REG_STS(alg_data)));
  147. if (alg_data->mif.len > 0) {
  148. /* We still have something to talk about... */
  149. val = *alg_data->mif.buf++;
  150. if (alg_data->mif.len == 1) {
  151. val |= stop_bit;
  152. if (!alg_data->last)
  153. val |= start_bit;
  154. }
  155. alg_data->mif.len--;
  156. iowrite32(val, I2C_REG_TX(alg_data));
  157. dev_dbg(&adap->dev, "%s(): xmit %#x [%d]\n", __func__,
  158. val, alg_data->mif.len + 1);
  159. if (alg_data->mif.len == 0) {
  160. if (alg_data->last) {
  161. /* Wait until the STOP is seen. */
  162. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
  163. dev_err(&adap->dev, "The bus is still "
  164. "active after timeout\n");
  165. }
  166. /* Disable master interrupts */
  167. iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
  168. ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
  169. I2C_REG_CTL(alg_data));
  170. del_timer_sync(&alg_data->mif.timer);
  171. dev_dbg(&adap->dev, "%s(): Waking up xfer routine.\n",
  172. __func__);
  173. complete(&alg_data->mif.complete);
  174. }
  175. } else if (alg_data->mif.len == 0) {
  176. /* zero-sized transfer */
  177. i2c_pnx_stop(adap);
  178. /* Disable master interrupts. */
  179. iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
  180. ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
  181. I2C_REG_CTL(alg_data));
  182. /* Stop timer. */
  183. del_timer_sync(&alg_data->mif.timer);
  184. dev_dbg(&adap->dev, "%s(): Waking up xfer routine after "
  185. "zero-xfer.\n", __func__);
  186. complete(&alg_data->mif.complete);
  187. }
  188. dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n",
  189. __func__, ioread32(I2C_REG_STS(alg_data)));
  190. return 0;
  191. }
  192. /**
  193. * i2c_pnx_master_rcv - receive data from slave
  194. * @adap: pointer to I2C adapter structure
  195. *
  196. * Reads one byte data from the slave
  197. */
  198. static int i2c_pnx_master_rcv(struct i2c_adapter *adap)
  199. {
  200. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  201. unsigned int val = 0;
  202. u32 ctl = 0;
  203. dev_dbg(&adap->dev, "%s(): entering: stat = %04x.\n",
  204. __func__, ioread32(I2C_REG_STS(alg_data)));
  205. /* Check, whether there is already data,
  206. * or we didn't 'ask' for it yet.
  207. */
  208. if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) {
  209. dev_dbg(&adap->dev, "%s(): Write dummy data to fill "
  210. "Rx-fifo...\n", __func__);
  211. if (alg_data->mif.len == 1) {
  212. /* Last byte, do not acknowledge next rcv. */
  213. val |= stop_bit;
  214. if (!alg_data->last)
  215. val |= start_bit;
  216. /*
  217. * Enable interrupt RFDAIE (data in Rx fifo),
  218. * and disable DRMIE (need data for Tx)
  219. */
  220. ctl = ioread32(I2C_REG_CTL(alg_data));
  221. ctl |= mcntrl_rffie | mcntrl_daie;
  222. ctl &= ~mcntrl_drmie;
  223. iowrite32(ctl, I2C_REG_CTL(alg_data));
  224. }
  225. /*
  226. * Now we'll 'ask' for data:
  227. * For each byte we want to receive, we must
  228. * write a (dummy) byte to the Tx-FIFO.
  229. */
  230. iowrite32(val, I2C_REG_TX(alg_data));
  231. return 0;
  232. }
  233. /* Handle data. */
  234. if (alg_data->mif.len > 0) {
  235. val = ioread32(I2C_REG_RX(alg_data));
  236. *alg_data->mif.buf++ = (u8) (val & 0xff);
  237. dev_dbg(&adap->dev, "%s(): rcv 0x%x [%d]\n", __func__, val,
  238. alg_data->mif.len);
  239. alg_data->mif.len--;
  240. if (alg_data->mif.len == 0) {
  241. if (alg_data->last)
  242. /* Wait until the STOP is seen. */
  243. if (wait_timeout(I2C_PNX_TIMEOUT, alg_data))
  244. dev_err(&adap->dev, "The bus is still "
  245. "active after timeout\n");
  246. /* Disable master interrupts */
  247. ctl = ioread32(I2C_REG_CTL(alg_data));
  248. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  249. mcntrl_drmie | mcntrl_daie);
  250. iowrite32(ctl, I2C_REG_CTL(alg_data));
  251. /* Kill timer. */
  252. del_timer_sync(&alg_data->mif.timer);
  253. complete(&alg_data->mif.complete);
  254. }
  255. }
  256. dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n",
  257. __func__, ioread32(I2C_REG_STS(alg_data)));
  258. return 0;
  259. }
  260. static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
  261. {
  262. u32 stat, ctl;
  263. struct i2c_adapter *adap = dev_id;
  264. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  265. dev_dbg(&adap->dev, "%s(): mstat = %x mctrl = %x, mode = %d\n",
  266. __func__,
  267. ioread32(I2C_REG_STS(alg_data)),
  268. ioread32(I2C_REG_CTL(alg_data)),
  269. alg_data->mif.mode);
  270. stat = ioread32(I2C_REG_STS(alg_data));
  271. /* let's see what kind of event this is */
  272. if (stat & mstatus_afi) {
  273. /* We lost arbitration in the midst of a transfer */
  274. alg_data->mif.ret = -EIO;
  275. /* Disable master interrupts. */
  276. ctl = ioread32(I2C_REG_CTL(alg_data));
  277. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  278. mcntrl_drmie);
  279. iowrite32(ctl, I2C_REG_CTL(alg_data));
  280. /* Stop timer, to prevent timeout. */
  281. del_timer_sync(&alg_data->mif.timer);
  282. complete(&alg_data->mif.complete);
  283. } else if (stat & mstatus_nai) {
  284. /* Slave did not acknowledge, generate a STOP */
  285. dev_dbg(&adap->dev, "%s(): "
  286. "Slave did not acknowledge, generating a STOP.\n",
  287. __func__);
  288. i2c_pnx_stop(adap);
  289. /* Disable master interrupts. */
  290. ctl = ioread32(I2C_REG_CTL(alg_data));
  291. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  292. mcntrl_drmie);
  293. iowrite32(ctl, I2C_REG_CTL(alg_data));
  294. /* Our return value. */
  295. alg_data->mif.ret = -EIO;
  296. /* Stop timer, to prevent timeout. */
  297. del_timer_sync(&alg_data->mif.timer);
  298. complete(&alg_data->mif.complete);
  299. } else {
  300. /*
  301. * Two options:
  302. * - Master Tx needs data.
  303. * - There is data in the Rx-fifo
  304. * The latter is only the case if we have requested for data,
  305. * via a dummy write. (See 'i2c_pnx_master_rcv'.)
  306. * We therefore check, as a sanity check, whether that interrupt
  307. * has been enabled.
  308. */
  309. if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
  310. if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
  311. i2c_pnx_master_xmit(adap);
  312. } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
  313. i2c_pnx_master_rcv(adap);
  314. }
  315. }
  316. }
  317. /* Clear TDI and AFI bits */
  318. stat = ioread32(I2C_REG_STS(alg_data));
  319. iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
  320. dev_dbg(&adap->dev, "%s(): exiting, stat = %x ctrl = %x.\n",
  321. __func__, ioread32(I2C_REG_STS(alg_data)),
  322. ioread32(I2C_REG_CTL(alg_data)));
  323. return IRQ_HANDLED;
  324. }
  325. static void i2c_pnx_timeout(unsigned long data)
  326. {
  327. struct i2c_adapter *adap = (struct i2c_adapter *)data;
  328. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  329. u32 ctl;
  330. dev_err(&adap->dev, "Master timed out. stat = %04x, cntrl = %04x. "
  331. "Resetting master...\n",
  332. ioread32(I2C_REG_STS(alg_data)),
  333. ioread32(I2C_REG_CTL(alg_data)));
  334. /* Reset master and disable interrupts */
  335. ctl = ioread32(I2C_REG_CTL(alg_data));
  336. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
  337. iowrite32(ctl, I2C_REG_CTL(alg_data));
  338. ctl |= mcntrl_reset;
  339. iowrite32(ctl, I2C_REG_CTL(alg_data));
  340. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  341. alg_data->mif.ret = -EIO;
  342. complete(&alg_data->mif.complete);
  343. }
  344. static inline void bus_reset_if_active(struct i2c_adapter *adap)
  345. {
  346. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  347. u32 stat;
  348. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
  349. dev_err(&adap->dev,
  350. "%s: Bus is still active after xfer. Reset it...\n",
  351. adap->name);
  352. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  353. I2C_REG_CTL(alg_data));
  354. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  355. } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
  356. /* If there is data in the fifo's after transfer,
  357. * flush fifo's by reset.
  358. */
  359. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  360. I2C_REG_CTL(alg_data));
  361. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  362. } else if (stat & mstatus_nai) {
  363. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  364. I2C_REG_CTL(alg_data));
  365. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  366. }
  367. }
  368. /**
  369. * i2c_pnx_xfer - generic transfer entry point
  370. * @adap: pointer to I2C adapter structure
  371. * @msgs: array of messages
  372. * @num: number of messages
  373. *
  374. * Initiates the transfer
  375. */
  376. static int
  377. i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  378. {
  379. struct i2c_msg *pmsg;
  380. int rc = 0, completed = 0, i;
  381. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  382. u32 stat = ioread32(I2C_REG_STS(alg_data));
  383. dev_dbg(&adap->dev, "%s(): entering: %d messages, stat = %04x.\n",
  384. __func__, num, ioread32(I2C_REG_STS(alg_data)));
  385. bus_reset_if_active(adap);
  386. /* Process transactions in a loop. */
  387. for (i = 0; rc >= 0 && i < num; i++) {
  388. u8 addr;
  389. pmsg = &msgs[i];
  390. addr = pmsg->addr;
  391. if (pmsg->flags & I2C_M_TEN) {
  392. dev_err(&adap->dev,
  393. "%s: 10 bits addr not supported!\n",
  394. adap->name);
  395. rc = -EINVAL;
  396. break;
  397. }
  398. alg_data->mif.buf = pmsg->buf;
  399. alg_data->mif.len = pmsg->len;
  400. alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
  401. I2C_SMBUS_READ : I2C_SMBUS_WRITE;
  402. alg_data->mif.ret = 0;
  403. alg_data->last = (i == num - 1);
  404. dev_dbg(&adap->dev, "%s(): mode %d, %d bytes\n", __func__,
  405. alg_data->mif.mode,
  406. alg_data->mif.len);
  407. i2c_pnx_arm_timer(adap);
  408. /* initialize the completion var */
  409. init_completion(&alg_data->mif.complete);
  410. /* Enable master interrupt */
  411. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
  412. mcntrl_naie | mcntrl_drmie,
  413. I2C_REG_CTL(alg_data));
  414. /* Put start-code and slave-address on the bus. */
  415. rc = i2c_pnx_start(addr, adap);
  416. if (rc < 0)
  417. break;
  418. /* Wait for completion */
  419. wait_for_completion(&alg_data->mif.complete);
  420. if (!(rc = alg_data->mif.ret))
  421. completed++;
  422. dev_dbg(&adap->dev, "%s(): Complete, return code = %d.\n",
  423. __func__, rc);
  424. /* Clear TDI and AFI bits in case they are set. */
  425. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
  426. dev_dbg(&adap->dev,
  427. "%s: TDI still set... clearing now.\n",
  428. adap->name);
  429. iowrite32(stat, I2C_REG_STS(alg_data));
  430. }
  431. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
  432. dev_dbg(&adap->dev,
  433. "%s: AFI still set... clearing now.\n",
  434. adap->name);
  435. iowrite32(stat, I2C_REG_STS(alg_data));
  436. }
  437. }
  438. bus_reset_if_active(adap);
  439. /* Cleanup to be sure... */
  440. alg_data->mif.buf = NULL;
  441. alg_data->mif.len = 0;
  442. dev_dbg(&adap->dev, "%s(): exiting, stat = %x\n",
  443. __func__, ioread32(I2C_REG_STS(alg_data)));
  444. if (completed != num)
  445. return ((rc < 0) ? rc : -EREMOTEIO);
  446. return num;
  447. }
  448. static u32 i2c_pnx_func(struct i2c_adapter *adapter)
  449. {
  450. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  451. }
  452. static struct i2c_algorithm pnx_algorithm = {
  453. .master_xfer = i2c_pnx_xfer,
  454. .functionality = i2c_pnx_func,
  455. };
  456. static int i2c_pnx_controller_suspend(struct platform_device *pdev,
  457. pm_message_t state)
  458. {
  459. struct i2c_pnx_data *i2c_pnx = platform_get_drvdata(pdev);
  460. return i2c_pnx->suspend(pdev, state);
  461. }
  462. static int i2c_pnx_controller_resume(struct platform_device *pdev)
  463. {
  464. struct i2c_pnx_data *i2c_pnx = platform_get_drvdata(pdev);
  465. return i2c_pnx->resume(pdev);
  466. }
  467. static int __devinit i2c_pnx_probe(struct platform_device *pdev)
  468. {
  469. unsigned long tmp;
  470. int ret = 0;
  471. struct i2c_pnx_algo_data *alg_data;
  472. int freq_mhz;
  473. struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data;
  474. if (!i2c_pnx || !i2c_pnx->adapter) {
  475. dev_err(&pdev->dev, "%s: no platform data supplied\n",
  476. __func__);
  477. ret = -EINVAL;
  478. goto out;
  479. }
  480. platform_set_drvdata(pdev, i2c_pnx);
  481. if (i2c_pnx->calculate_input_freq)
  482. freq_mhz = i2c_pnx->calculate_input_freq(pdev);
  483. else {
  484. freq_mhz = PNX_DEFAULT_FREQ;
  485. dev_info(&pdev->dev, "Setting bus frequency to default value: "
  486. "%d MHz\n", freq_mhz);
  487. }
  488. i2c_pnx->adapter->algo = &pnx_algorithm;
  489. alg_data = i2c_pnx->adapter->algo_data;
  490. init_timer(&alg_data->mif.timer);
  491. alg_data->mif.timer.function = i2c_pnx_timeout;
  492. alg_data->mif.timer.data = (unsigned long)i2c_pnx->adapter;
  493. /* Register I/O resource */
  494. if (!request_mem_region(alg_data->base, I2C_PNX_REGION_SIZE,
  495. pdev->name)) {
  496. dev_err(&pdev->dev,
  497. "I/O region 0x%08x for I2C already in use.\n",
  498. alg_data->base);
  499. ret = -ENODEV;
  500. goto out_drvdata;
  501. }
  502. if (!(alg_data->ioaddr =
  503. (u32)ioremap(alg_data->base, I2C_PNX_REGION_SIZE))) {
  504. dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
  505. ret = -ENOMEM;
  506. goto out_release;
  507. }
  508. i2c_pnx->set_clock_run(pdev);
  509. /*
  510. * Clock Divisor High This value is the number of system clocks
  511. * the serial clock (SCL) will be high.
  512. * For example, if the system clock period is 50 ns and the maximum
  513. * desired serial period is 10000 ns (100 kHz), then CLKHI would be
  514. * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
  515. * programmed into CLKHI will vary from this slightly due to
  516. * variations in the output pad's rise and fall times as well as
  517. * the deglitching filter length.
  518. */
  519. tmp = ((freq_mhz * 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2;
  520. iowrite32(tmp, I2C_REG_CKH(alg_data));
  521. iowrite32(tmp, I2C_REG_CKL(alg_data));
  522. iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
  523. if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
  524. ret = -ENODEV;
  525. goto out_unmap;
  526. }
  527. init_completion(&alg_data->mif.complete);
  528. ret = request_irq(alg_data->irq, i2c_pnx_interrupt,
  529. 0, pdev->name, i2c_pnx->adapter);
  530. if (ret)
  531. goto out_clock;
  532. /* Register this adapter with the I2C subsystem */
  533. i2c_pnx->adapter->dev.parent = &pdev->dev;
  534. ret = i2c_add_adapter(i2c_pnx->adapter);
  535. if (ret < 0) {
  536. dev_err(&pdev->dev, "I2C: Failed to add bus\n");
  537. goto out_irq;
  538. }
  539. dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
  540. i2c_pnx->adapter->name, alg_data->base, alg_data->irq);
  541. return 0;
  542. out_irq:
  543. free_irq(alg_data->irq, i2c_pnx->adapter);
  544. out_clock:
  545. i2c_pnx->set_clock_stop(pdev);
  546. out_unmap:
  547. iounmap((void *)alg_data->ioaddr);
  548. out_release:
  549. release_mem_region(alg_data->base, I2C_PNX_REGION_SIZE);
  550. out_drvdata:
  551. platform_set_drvdata(pdev, NULL);
  552. out:
  553. return ret;
  554. }
  555. static int __devexit i2c_pnx_remove(struct platform_device *pdev)
  556. {
  557. struct i2c_pnx_data *i2c_pnx = platform_get_drvdata(pdev);
  558. struct i2c_adapter *adap = i2c_pnx->adapter;
  559. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  560. free_irq(alg_data->irq, i2c_pnx->adapter);
  561. i2c_del_adapter(adap);
  562. i2c_pnx->set_clock_stop(pdev);
  563. iounmap((void *)alg_data->ioaddr);
  564. release_mem_region(alg_data->base, I2C_PNX_REGION_SIZE);
  565. platform_set_drvdata(pdev, NULL);
  566. return 0;
  567. }
  568. static struct platform_driver i2c_pnx_driver = {
  569. .driver = {
  570. .name = "pnx-i2c",
  571. .owner = THIS_MODULE,
  572. },
  573. .probe = i2c_pnx_probe,
  574. .remove = __devexit_p(i2c_pnx_remove),
  575. .suspend = i2c_pnx_controller_suspend,
  576. .resume = i2c_pnx_controller_resume,
  577. };
  578. static int __init i2c_adap_pnx_init(void)
  579. {
  580. return platform_driver_register(&i2c_pnx_driver);
  581. }
  582. static void __exit i2c_adap_pnx_exit(void)
  583. {
  584. platform_driver_unregister(&i2c_pnx_driver);
  585. }
  586. MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
  587. MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
  588. MODULE_LICENSE("GPL");
  589. MODULE_ALIAS("platform:pnx-i2c");
  590. /* We need to make sure I2C is initialized before USB */
  591. subsys_initcall(i2c_adap_pnx_init);
  592. module_exit(i2c_adap_pnx_exit);