i2c-pnx.c 19 KB

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