i2c-pnx.c 19 KB

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