i2c-pnx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 <mach/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", __func__,
  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", __func__,
  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", __func__);
  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. __func__, 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. __func__, 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. __func__, 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", __func__,
  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. __func__);
  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", __func__);
  182. complete(&alg_data->mif.complete);
  183. }
  184. dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n",
  185. __func__, 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. __func__, 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", __func__);
  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", __func__, 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. __func__, ioread32(I2C_REG_STS(alg_data)));
  254. return 0;
  255. }
  256. static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
  257. {
  258. u32 stat, ctl;
  259. struct i2c_adapter *adap = dev_id;
  260. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  261. dev_dbg(&adap->dev, "%s(): mstat = %x mctrl = %x, mode = %d\n",
  262. __func__,
  263. ioread32(I2C_REG_STS(alg_data)),
  264. ioread32(I2C_REG_CTL(alg_data)),
  265. alg_data->mif.mode);
  266. stat = ioread32(I2C_REG_STS(alg_data));
  267. /* let's see what kind of event this is */
  268. if (stat & mstatus_afi) {
  269. /* We lost arbitration in the midst of a transfer */
  270. alg_data->mif.ret = -EIO;
  271. /* Disable master interrupts. */
  272. ctl = ioread32(I2C_REG_CTL(alg_data));
  273. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  274. mcntrl_drmie);
  275. iowrite32(ctl, I2C_REG_CTL(alg_data));
  276. /* Stop timer, to prevent timeout. */
  277. del_timer_sync(&alg_data->mif.timer);
  278. complete(&alg_data->mif.complete);
  279. } else if (stat & mstatus_nai) {
  280. /* Slave did not acknowledge, generate a STOP */
  281. dev_dbg(&adap->dev, "%s(): "
  282. "Slave did not acknowledge, generating a STOP.\n",
  283. __func__);
  284. i2c_pnx_stop(adap);
  285. /* Disable master interrupts. */
  286. ctl = ioread32(I2C_REG_CTL(alg_data));
  287. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  288. mcntrl_drmie);
  289. iowrite32(ctl, I2C_REG_CTL(alg_data));
  290. /* Our return value. */
  291. alg_data->mif.ret = -EIO;
  292. /* Stop timer, to prevent timeout. */
  293. del_timer_sync(&alg_data->mif.timer);
  294. complete(&alg_data->mif.complete);
  295. } else {
  296. /*
  297. * Two options:
  298. * - Master Tx needs data.
  299. * - There is data in the Rx-fifo
  300. * The latter is only the case if we have requested for data,
  301. * via a dummy write. (See 'i2c_pnx_master_rcv'.)
  302. * We therefore check, as a sanity check, whether that interrupt
  303. * has been enabled.
  304. */
  305. if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
  306. if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
  307. i2c_pnx_master_xmit(adap);
  308. } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
  309. i2c_pnx_master_rcv(adap);
  310. }
  311. }
  312. }
  313. /* Clear TDI and AFI bits */
  314. stat = ioread32(I2C_REG_STS(alg_data));
  315. iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
  316. dev_dbg(&adap->dev, "%s(): exiting, stat = %x ctrl = %x.\n",
  317. __func__, ioread32(I2C_REG_STS(alg_data)),
  318. ioread32(I2C_REG_CTL(alg_data)));
  319. return IRQ_HANDLED;
  320. }
  321. static void i2c_pnx_timeout(unsigned long data)
  322. {
  323. struct i2c_adapter *adap = (struct i2c_adapter *)data;
  324. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  325. u32 ctl;
  326. dev_err(&adap->dev, "Master timed out. stat = %04x, cntrl = %04x. "
  327. "Resetting master...\n",
  328. ioread32(I2C_REG_STS(alg_data)),
  329. ioread32(I2C_REG_CTL(alg_data)));
  330. /* Reset master and disable interrupts */
  331. ctl = ioread32(I2C_REG_CTL(alg_data));
  332. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
  333. iowrite32(ctl, I2C_REG_CTL(alg_data));
  334. ctl |= mcntrl_reset;
  335. iowrite32(ctl, I2C_REG_CTL(alg_data));
  336. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  337. alg_data->mif.ret = -EIO;
  338. complete(&alg_data->mif.complete);
  339. }
  340. static inline void bus_reset_if_active(struct i2c_adapter *adap)
  341. {
  342. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  343. u32 stat;
  344. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
  345. dev_err(&adap->dev,
  346. "%s: Bus is still active after xfer. Reset it...\n",
  347. adap->name);
  348. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  349. I2C_REG_CTL(alg_data));
  350. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  351. } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) {
  352. /* If there is data in the fifo's after transfer,
  353. * flush fifo's by reset.
  354. */
  355. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  356. I2C_REG_CTL(alg_data));
  357. wait_reset(I2C_PNX_TIMEOUT, alg_data);
  358. } else if (stat & mstatus_nai) {
  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. }
  363. }
  364. /**
  365. * i2c_pnx_xfer - generic transfer entry point
  366. * @adap: pointer to I2C adapter structure
  367. * @msgs: array of messages
  368. * @num: number of messages
  369. *
  370. * Initiates the transfer
  371. */
  372. static int
  373. i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  374. {
  375. struct i2c_msg *pmsg;
  376. int rc = 0, completed = 0, i;
  377. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  378. u32 stat = ioread32(I2C_REG_STS(alg_data));
  379. dev_dbg(&adap->dev, "%s(): entering: %d messages, stat = %04x.\n",
  380. __func__, num, ioread32(I2C_REG_STS(alg_data)));
  381. bus_reset_if_active(adap);
  382. /* Process transactions in a loop. */
  383. for (i = 0; rc >= 0 && i < num; i++) {
  384. u8 addr;
  385. pmsg = &msgs[i];
  386. addr = pmsg->addr;
  387. if (pmsg->flags & I2C_M_TEN) {
  388. dev_err(&adap->dev,
  389. "%s: 10 bits addr not supported!\n",
  390. adap->name);
  391. rc = -EINVAL;
  392. break;
  393. }
  394. alg_data->mif.buf = pmsg->buf;
  395. alg_data->mif.len = pmsg->len;
  396. alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
  397. I2C_SMBUS_READ : I2C_SMBUS_WRITE;
  398. alg_data->mif.ret = 0;
  399. alg_data->last = (i == num - 1);
  400. dev_dbg(&adap->dev, "%s(): mode %d, %d bytes\n", __func__,
  401. alg_data->mif.mode,
  402. alg_data->mif.len);
  403. i2c_pnx_arm_timer(adap);
  404. /* initialize the completion var */
  405. init_completion(&alg_data->mif.complete);
  406. /* Enable master interrupt */
  407. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
  408. mcntrl_naie | mcntrl_drmie,
  409. I2C_REG_CTL(alg_data));
  410. /* Put start-code and slave-address on the bus. */
  411. rc = i2c_pnx_start(addr, adap);
  412. if (rc < 0)
  413. break;
  414. /* Wait for completion */
  415. wait_for_completion(&alg_data->mif.complete);
  416. if (!(rc = alg_data->mif.ret))
  417. completed++;
  418. dev_dbg(&adap->dev, "%s(): Complete, return code = %d.\n",
  419. __func__, rc);
  420. /* Clear TDI and AFI bits in case they are set. */
  421. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
  422. dev_dbg(&adap->dev,
  423. "%s: TDI still set... clearing now.\n",
  424. adap->name);
  425. iowrite32(stat, I2C_REG_STS(alg_data));
  426. }
  427. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
  428. dev_dbg(&adap->dev,
  429. "%s: AFI still set... clearing now.\n",
  430. adap->name);
  431. iowrite32(stat, I2C_REG_STS(alg_data));
  432. }
  433. }
  434. bus_reset_if_active(adap);
  435. /* Cleanup to be sure... */
  436. alg_data->mif.buf = NULL;
  437. alg_data->mif.len = 0;
  438. dev_dbg(&adap->dev, "%s(): exiting, stat = %x\n",
  439. __func__, ioread32(I2C_REG_STS(alg_data)));
  440. if (completed != num)
  441. return ((rc < 0) ? rc : -EREMOTEIO);
  442. return num;
  443. }
  444. static u32 i2c_pnx_func(struct i2c_adapter *adapter)
  445. {
  446. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  447. }
  448. static struct i2c_algorithm pnx_algorithm = {
  449. .master_xfer = i2c_pnx_xfer,
  450. .functionality = i2c_pnx_func,
  451. };
  452. static int i2c_pnx_controller_suspend(struct platform_device *pdev,
  453. pm_message_t state)
  454. {
  455. struct i2c_pnx_data *i2c_pnx = platform_get_drvdata(pdev);
  456. return i2c_pnx->suspend(pdev, state);
  457. }
  458. static int i2c_pnx_controller_resume(struct platform_device *pdev)
  459. {
  460. struct i2c_pnx_data *i2c_pnx = platform_get_drvdata(pdev);
  461. return i2c_pnx->resume(pdev);
  462. }
  463. static int __devinit i2c_pnx_probe(struct platform_device *pdev)
  464. {
  465. unsigned long tmp;
  466. int ret = 0;
  467. struct i2c_pnx_algo_data *alg_data;
  468. int freq_mhz;
  469. struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data;
  470. if (!i2c_pnx || !i2c_pnx->adapter) {
  471. dev_err(&pdev->dev, "%s: no platform data supplied\n",
  472. __func__);
  473. ret = -EINVAL;
  474. goto out;
  475. }
  476. platform_set_drvdata(pdev, i2c_pnx);
  477. if (i2c_pnx->calculate_input_freq)
  478. freq_mhz = i2c_pnx->calculate_input_freq(pdev);
  479. else {
  480. freq_mhz = PNX_DEFAULT_FREQ;
  481. dev_info(&pdev->dev, "Setting bus frequency to default value: "
  482. "%d MHz\n", freq_mhz);
  483. }
  484. i2c_pnx->adapter->algo = &pnx_algorithm;
  485. alg_data = i2c_pnx->adapter->algo_data;
  486. init_timer(&alg_data->mif.timer);
  487. alg_data->mif.timer.function = i2c_pnx_timeout;
  488. alg_data->mif.timer.data = (unsigned long)i2c_pnx->adapter;
  489. /* Register I/O resource */
  490. if (!request_region(alg_data->base, I2C_PNX_REGION_SIZE, pdev->name)) {
  491. dev_err(&pdev->dev,
  492. "I/O region 0x%08x for I2C already in use.\n",
  493. alg_data->base);
  494. ret = -ENODEV;
  495. goto out_drvdata;
  496. }
  497. if (!(alg_data->ioaddr =
  498. (u32)ioremap(alg_data->base, I2C_PNX_REGION_SIZE))) {
  499. dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
  500. ret = -ENOMEM;
  501. goto out_release;
  502. }
  503. i2c_pnx->set_clock_run(pdev);
  504. /*
  505. * Clock Divisor High This value is the number of system clocks
  506. * the serial clock (SCL) will be high.
  507. * For example, if the system clock period is 50 ns and the maximum
  508. * desired serial period is 10000 ns (100 kHz), then CLKHI would be
  509. * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
  510. * programmed into CLKHI will vary from this slightly due to
  511. * variations in the output pad's rise and fall times as well as
  512. * the deglitching filter length.
  513. */
  514. tmp = ((freq_mhz * 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2;
  515. iowrite32(tmp, I2C_REG_CKH(alg_data));
  516. iowrite32(tmp, I2C_REG_CKL(alg_data));
  517. iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
  518. if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) {
  519. ret = -ENODEV;
  520. goto out_unmap;
  521. }
  522. init_completion(&alg_data->mif.complete);
  523. ret = request_irq(alg_data->irq, i2c_pnx_interrupt,
  524. 0, pdev->name, i2c_pnx->adapter);
  525. if (ret)
  526. goto out_clock;
  527. /* Register this adapter with the I2C subsystem */
  528. i2c_pnx->adapter->dev.parent = &pdev->dev;
  529. ret = i2c_add_adapter(i2c_pnx->adapter);
  530. if (ret < 0) {
  531. dev_err(&pdev->dev, "I2C: Failed to add bus\n");
  532. goto out_irq;
  533. }
  534. dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
  535. i2c_pnx->adapter->name, alg_data->base, alg_data->irq);
  536. return 0;
  537. out_irq:
  538. free_irq(alg_data->irq, alg_data);
  539. out_clock:
  540. i2c_pnx->set_clock_stop(pdev);
  541. out_unmap:
  542. iounmap((void *)alg_data->ioaddr);
  543. out_release:
  544. release_region(alg_data->base, I2C_PNX_REGION_SIZE);
  545. out_drvdata:
  546. platform_set_drvdata(pdev, NULL);
  547. out:
  548. return ret;
  549. }
  550. static int __devexit i2c_pnx_remove(struct platform_device *pdev)
  551. {
  552. struct i2c_pnx_data *i2c_pnx = platform_get_drvdata(pdev);
  553. struct i2c_adapter *adap = i2c_pnx->adapter;
  554. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  555. free_irq(alg_data->irq, alg_data);
  556. i2c_del_adapter(adap);
  557. i2c_pnx->set_clock_stop(pdev);
  558. iounmap((void *)alg_data->ioaddr);
  559. release_region(alg_data->base, I2C_PNX_REGION_SIZE);
  560. platform_set_drvdata(pdev, NULL);
  561. return 0;
  562. }
  563. static struct platform_driver i2c_pnx_driver = {
  564. .driver = {
  565. .name = "pnx-i2c",
  566. .owner = THIS_MODULE,
  567. },
  568. .probe = i2c_pnx_probe,
  569. .remove = __devexit_p(i2c_pnx_remove),
  570. .suspend = i2c_pnx_controller_suspend,
  571. .resume = i2c_pnx_controller_resume,
  572. };
  573. static int __init i2c_adap_pnx_init(void)
  574. {
  575. return platform_driver_register(&i2c_pnx_driver);
  576. }
  577. static void __exit i2c_adap_pnx_exit(void)
  578. {
  579. platform_driver_unregister(&i2c_pnx_driver);
  580. }
  581. MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
  582. MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
  583. MODULE_LICENSE("GPL");
  584. MODULE_ALIAS("platform:pnx-i2c");
  585. /* We need to make sure I2C is initialized before USB */
  586. subsys_initcall(i2c_adap_pnx_init);
  587. module_exit(i2c_adap_pnx_exit);