i2c-pnx.c 21 KB

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