i2c-pnx.c 21 KB

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