i2c-pnx.c 22 KB

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