i2c-pnx.c 19 KB

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