i2c-pnx.c 19 KB

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