i2c-pnx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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_DEFAULT 10 /* msec */
  26. #define I2C_PNX_SPEED_KHZ_DEFAULT 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_drsie = 0x00000010,
  47. mcntrl_rffie = 0x00000020,
  48. mcntrl_daie = 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. /* 'Asking' is done asynchronously, e.g. dummy TX of several
  249. * bytes is done before the first actual RX arrives in FIFO.
  250. * Therefore, ordered bytes (via TX) are counted separately.
  251. */
  252. if (alg_data->mif.order) {
  253. dev_dbg(&alg_data->adapter.dev,
  254. "%s(): Write dummy data to fill Rx-fifo...\n",
  255. __func__);
  256. if (alg_data->mif.order == 1) {
  257. /* Last byte, do not acknowledge next rcv. */
  258. val |= stop_bit;
  259. /*
  260. * Enable interrupt RFDAIE (data in Rx fifo),
  261. * and disable DRMIE (need data for Tx)
  262. */
  263. ctl = ioread32(I2C_REG_CTL(alg_data));
  264. ctl |= mcntrl_rffie | mcntrl_daie;
  265. ctl &= ~mcntrl_drmie;
  266. iowrite32(ctl, I2C_REG_CTL(alg_data));
  267. }
  268. /*
  269. * Now we'll 'ask' for data:
  270. * For each byte we want to receive, we must
  271. * write a (dummy) byte to the Tx-FIFO.
  272. */
  273. iowrite32(val, I2C_REG_TX(alg_data));
  274. alg_data->mif.order--;
  275. }
  276. return 0;
  277. }
  278. /* Handle data. */
  279. if (alg_data->mif.len > 0) {
  280. val = ioread32(I2C_REG_RX(alg_data));
  281. *alg_data->mif.buf++ = (u8) (val & 0xff);
  282. dev_dbg(&alg_data->adapter.dev, "%s(): rcv 0x%x [%d]\n",
  283. __func__, val, alg_data->mif.len);
  284. alg_data->mif.len--;
  285. if (alg_data->mif.len == 0) {
  286. if (alg_data->last)
  287. /* Wait until the STOP is seen. */
  288. if (wait_timeout(alg_data))
  289. dev_err(&alg_data->adapter.dev,
  290. "The bus is still active after timeout\n");
  291. /* Disable master interrupts */
  292. ctl = ioread32(I2C_REG_CTL(alg_data));
  293. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  294. mcntrl_drmie | mcntrl_daie);
  295. iowrite32(ctl, I2C_REG_CTL(alg_data));
  296. /* Kill timer. */
  297. del_timer_sync(&alg_data->mif.timer);
  298. complete(&alg_data->mif.complete);
  299. }
  300. }
  301. dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
  302. __func__, ioread32(I2C_REG_STS(alg_data)));
  303. return 0;
  304. }
  305. static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id)
  306. {
  307. struct i2c_pnx_algo_data *alg_data = dev_id;
  308. u32 stat, ctl;
  309. dev_dbg(&alg_data->adapter.dev,
  310. "%s(): mstat = %x mctrl = %x, mode = %d\n",
  311. __func__,
  312. ioread32(I2C_REG_STS(alg_data)),
  313. ioread32(I2C_REG_CTL(alg_data)),
  314. alg_data->mif.mode);
  315. stat = ioread32(I2C_REG_STS(alg_data));
  316. /* let's see what kind of event this is */
  317. if (stat & mstatus_afi) {
  318. /* We lost arbitration in the midst of a transfer */
  319. alg_data->mif.ret = -EIO;
  320. /* Disable master interrupts. */
  321. ctl = ioread32(I2C_REG_CTL(alg_data));
  322. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  323. mcntrl_drmie);
  324. iowrite32(ctl, I2C_REG_CTL(alg_data));
  325. /* Stop timer, to prevent timeout. */
  326. del_timer_sync(&alg_data->mif.timer);
  327. complete(&alg_data->mif.complete);
  328. } else if (stat & mstatus_nai) {
  329. /* Slave did not acknowledge, generate a STOP */
  330. dev_dbg(&alg_data->adapter.dev,
  331. "%s(): Slave did not acknowledge, generating a STOP.\n",
  332. __func__);
  333. i2c_pnx_stop(alg_data);
  334. /* Disable master interrupts. */
  335. ctl = ioread32(I2C_REG_CTL(alg_data));
  336. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie |
  337. mcntrl_drmie);
  338. iowrite32(ctl, I2C_REG_CTL(alg_data));
  339. /* Our return value. */
  340. alg_data->mif.ret = -EIO;
  341. /* Stop timer, to prevent timeout. */
  342. del_timer_sync(&alg_data->mif.timer);
  343. complete(&alg_data->mif.complete);
  344. } else {
  345. /*
  346. * Two options:
  347. * - Master Tx needs data.
  348. * - There is data in the Rx-fifo
  349. * The latter is only the case if we have requested for data,
  350. * via a dummy write. (See 'i2c_pnx_master_rcv'.)
  351. * We therefore check, as a sanity check, whether that interrupt
  352. * has been enabled.
  353. */
  354. if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) {
  355. if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
  356. i2c_pnx_master_xmit(alg_data);
  357. } else if (alg_data->mif.mode == I2C_SMBUS_READ) {
  358. i2c_pnx_master_rcv(alg_data);
  359. }
  360. }
  361. }
  362. /* Clear TDI and AFI bits */
  363. stat = ioread32(I2C_REG_STS(alg_data));
  364. iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
  365. dev_dbg(&alg_data->adapter.dev,
  366. "%s(): exiting, stat = %x ctrl = %x.\n",
  367. __func__, ioread32(I2C_REG_STS(alg_data)),
  368. ioread32(I2C_REG_CTL(alg_data)));
  369. return IRQ_HANDLED;
  370. }
  371. static void i2c_pnx_timeout(unsigned long data)
  372. {
  373. struct i2c_pnx_algo_data *alg_data = (struct i2c_pnx_algo_data *)data;
  374. u32 ctl;
  375. dev_err(&alg_data->adapter.dev,
  376. "Master timed out. stat = %04x, cntrl = %04x. Resetting master...\n",
  377. ioread32(I2C_REG_STS(alg_data)),
  378. ioread32(I2C_REG_CTL(alg_data)));
  379. /* Reset master and disable interrupts */
  380. ctl = ioread32(I2C_REG_CTL(alg_data));
  381. ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie);
  382. iowrite32(ctl, I2C_REG_CTL(alg_data));
  383. ctl |= mcntrl_reset;
  384. iowrite32(ctl, I2C_REG_CTL(alg_data));
  385. wait_reset(alg_data);
  386. alg_data->mif.ret = -EIO;
  387. complete(&alg_data->mif.complete);
  388. }
  389. static inline void bus_reset_if_active(struct i2c_pnx_algo_data *alg_data)
  390. {
  391. u32 stat;
  392. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
  393. dev_err(&alg_data->adapter.dev,
  394. "%s: Bus is still active after xfer. Reset it...\n",
  395. alg_data->adapter.name);
  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_rfe) || !(stat & mstatus_tfe)) {
  400. /* If there is data in the fifo's after transfer,
  401. * flush fifo's by reset.
  402. */
  403. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  404. I2C_REG_CTL(alg_data));
  405. wait_reset(alg_data);
  406. } else if (stat & mstatus_nai) {
  407. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
  408. I2C_REG_CTL(alg_data));
  409. wait_reset(alg_data);
  410. }
  411. }
  412. /**
  413. * i2c_pnx_xfer - generic transfer entry point
  414. * @adap: pointer to I2C adapter structure
  415. * @msgs: array of messages
  416. * @num: number of messages
  417. *
  418. * Initiates the transfer
  419. */
  420. static int
  421. i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  422. {
  423. struct i2c_msg *pmsg;
  424. int rc = 0, completed = 0, i;
  425. struct i2c_pnx_algo_data *alg_data = adap->algo_data;
  426. u32 stat = ioread32(I2C_REG_STS(alg_data));
  427. dev_dbg(&alg_data->adapter.dev,
  428. "%s(): entering: %d messages, stat = %04x.\n",
  429. __func__, num, ioread32(I2C_REG_STS(alg_data)));
  430. bus_reset_if_active(alg_data);
  431. /* Process transactions in a loop. */
  432. for (i = 0; rc >= 0 && i < num; i++) {
  433. u8 addr;
  434. pmsg = &msgs[i];
  435. addr = pmsg->addr;
  436. if (pmsg->flags & I2C_M_TEN) {
  437. dev_err(&alg_data->adapter.dev,
  438. "%s: 10 bits addr not supported!\n",
  439. alg_data->adapter.name);
  440. rc = -EINVAL;
  441. break;
  442. }
  443. alg_data->mif.buf = pmsg->buf;
  444. alg_data->mif.len = pmsg->len;
  445. alg_data->mif.order = pmsg->len;
  446. alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ?
  447. I2C_SMBUS_READ : I2C_SMBUS_WRITE;
  448. alg_data->mif.ret = 0;
  449. alg_data->last = (i == num - 1);
  450. dev_dbg(&alg_data->adapter.dev, "%s(): mode %d, %d bytes\n",
  451. __func__, alg_data->mif.mode, alg_data->mif.len);
  452. i2c_pnx_arm_timer(alg_data);
  453. /* initialize the completion var */
  454. init_completion(&alg_data->mif.complete);
  455. /* Enable master interrupt */
  456. iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
  457. mcntrl_naie | mcntrl_drmie,
  458. I2C_REG_CTL(alg_data));
  459. /* Put start-code and slave-address on the bus. */
  460. rc = i2c_pnx_start(addr, alg_data);
  461. if (rc < 0)
  462. break;
  463. /* Wait for completion */
  464. wait_for_completion(&alg_data->mif.complete);
  465. if (!(rc = alg_data->mif.ret))
  466. completed++;
  467. dev_dbg(&alg_data->adapter.dev,
  468. "%s(): Complete, return code = %d.\n",
  469. __func__, rc);
  470. /* Clear TDI and AFI bits in case they are set. */
  471. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) {
  472. dev_dbg(&alg_data->adapter.dev,
  473. "%s: TDI still set... clearing now.\n",
  474. alg_data->adapter.name);
  475. iowrite32(stat, I2C_REG_STS(alg_data));
  476. }
  477. if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) {
  478. dev_dbg(&alg_data->adapter.dev,
  479. "%s: AFI still set... clearing now.\n",
  480. alg_data->adapter.name);
  481. iowrite32(stat, I2C_REG_STS(alg_data));
  482. }
  483. }
  484. bus_reset_if_active(alg_data);
  485. /* Cleanup to be sure... */
  486. alg_data->mif.buf = NULL;
  487. alg_data->mif.len = 0;
  488. alg_data->mif.order = 0;
  489. dev_dbg(&alg_data->adapter.dev, "%s(): exiting, stat = %x\n",
  490. __func__, ioread32(I2C_REG_STS(alg_data)));
  491. if (completed != num)
  492. return ((rc < 0) ? rc : -EREMOTEIO);
  493. return num;
  494. }
  495. static u32 i2c_pnx_func(struct i2c_adapter *adapter)
  496. {
  497. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  498. }
  499. static struct i2c_algorithm pnx_algorithm = {
  500. .master_xfer = i2c_pnx_xfer,
  501. .functionality = i2c_pnx_func,
  502. };
  503. #ifdef CONFIG_PM_SLEEP
  504. static int i2c_pnx_controller_suspend(struct device *dev)
  505. {
  506. struct i2c_pnx_algo_data *alg_data = dev_get_drvdata(dev);
  507. clk_disable(alg_data->clk);
  508. return 0;
  509. }
  510. static int i2c_pnx_controller_resume(struct device *dev)
  511. {
  512. struct i2c_pnx_algo_data *alg_data = dev_get_drvdata(dev);
  513. return clk_enable(alg_data->clk);
  514. }
  515. static SIMPLE_DEV_PM_OPS(i2c_pnx_pm,
  516. i2c_pnx_controller_suspend, i2c_pnx_controller_resume);
  517. #define PNX_I2C_PM (&i2c_pnx_pm)
  518. #else
  519. #define PNX_I2C_PM NULL
  520. #endif
  521. static int i2c_pnx_probe(struct platform_device *pdev)
  522. {
  523. unsigned long tmp;
  524. int ret = 0;
  525. struct i2c_pnx_algo_data *alg_data;
  526. unsigned long freq;
  527. struct resource *res;
  528. u32 speed = I2C_PNX_SPEED_KHZ_DEFAULT * 1000;
  529. alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL);
  530. if (!alg_data) {
  531. ret = -ENOMEM;
  532. goto err_kzalloc;
  533. }
  534. platform_set_drvdata(pdev, alg_data);
  535. alg_data->adapter.dev.parent = &pdev->dev;
  536. alg_data->adapter.algo = &pnx_algorithm;
  537. alg_data->adapter.algo_data = alg_data;
  538. alg_data->adapter.nr = pdev->id;
  539. alg_data->timeout = I2C_PNX_TIMEOUT_DEFAULT;
  540. #ifdef CONFIG_OF
  541. alg_data->adapter.dev.of_node = of_node_get(pdev->dev.of_node);
  542. if (pdev->dev.of_node) {
  543. of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  544. &speed);
  545. /*
  546. * At this point, it is planned to add an OF timeout property.
  547. * As soon as there is a consensus about how to call and handle
  548. * this, sth. like the following can be put here:
  549. *
  550. * of_property_read_u32(pdev->dev.of_node, "timeout",
  551. * &alg_data->timeout);
  552. */
  553. }
  554. #endif
  555. alg_data->clk = clk_get(&pdev->dev, NULL);
  556. if (IS_ERR(alg_data->clk)) {
  557. ret = PTR_ERR(alg_data->clk);
  558. goto out_drvdata;
  559. }
  560. init_timer(&alg_data->mif.timer);
  561. alg_data->mif.timer.function = i2c_pnx_timeout;
  562. alg_data->mif.timer.data = (unsigned long)alg_data;
  563. snprintf(alg_data->adapter.name, sizeof(alg_data->adapter.name),
  564. "%s", pdev->name);
  565. /* Register I/O resource */
  566. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  567. if (!res) {
  568. dev_err(&pdev->dev, "Unable to get mem resource.\n");
  569. ret = -EBUSY;
  570. goto out_clkget;
  571. }
  572. if (!request_mem_region(res->start, I2C_PNX_REGION_SIZE,
  573. pdev->name)) {
  574. dev_err(&pdev->dev,
  575. "I/O region 0x%08x for I2C already in use.\n",
  576. res->start);
  577. ret = -ENOMEM;
  578. goto out_clkget;
  579. }
  580. alg_data->base = res->start;
  581. alg_data->ioaddr = ioremap(res->start, I2C_PNX_REGION_SIZE);
  582. if (!alg_data->ioaddr) {
  583. dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n");
  584. ret = -ENOMEM;
  585. goto out_release;
  586. }
  587. ret = clk_enable(alg_data->clk);
  588. if (ret)
  589. goto out_unmap;
  590. freq = clk_get_rate(alg_data->clk);
  591. /*
  592. * Clock Divisor High This value is the number of system clocks
  593. * the serial clock (SCL) will be high.
  594. * For example, if the system clock period is 50 ns and the maximum
  595. * desired serial period is 10000 ns (100 kHz), then CLKHI would be
  596. * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value
  597. * programmed into CLKHI will vary from this slightly due to
  598. * variations in the output pad's rise and fall times as well as
  599. * the deglitching filter length.
  600. */
  601. tmp = (freq / speed) / 2 - 2;
  602. if (tmp > 0x3FF)
  603. tmp = 0x3FF;
  604. iowrite32(tmp, I2C_REG_CKH(alg_data));
  605. iowrite32(tmp, I2C_REG_CKL(alg_data));
  606. iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data));
  607. if (wait_reset(alg_data)) {
  608. ret = -ENODEV;
  609. goto out_clock;
  610. }
  611. init_completion(&alg_data->mif.complete);
  612. alg_data->irq = platform_get_irq(pdev, 0);
  613. if (alg_data->irq < 0) {
  614. dev_err(&pdev->dev, "Failed to get IRQ from platform resource\n");
  615. ret = alg_data->irq;
  616. goto out_clock;
  617. }
  618. ret = request_irq(alg_data->irq, i2c_pnx_interrupt,
  619. 0, pdev->name, alg_data);
  620. if (ret)
  621. goto out_clock;
  622. /* Register this adapter with the I2C subsystem */
  623. ret = i2c_add_numbered_adapter(&alg_data->adapter);
  624. if (ret < 0) {
  625. dev_err(&pdev->dev, "I2C: Failed to add bus\n");
  626. goto out_irq;
  627. }
  628. dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n",
  629. alg_data->adapter.name, res->start, alg_data->irq);
  630. return 0;
  631. out_irq:
  632. free_irq(alg_data->irq, alg_data);
  633. out_clock:
  634. clk_disable(alg_data->clk);
  635. out_unmap:
  636. iounmap(alg_data->ioaddr);
  637. out_release:
  638. release_mem_region(res->start, I2C_PNX_REGION_SIZE);
  639. out_clkget:
  640. clk_put(alg_data->clk);
  641. out_drvdata:
  642. kfree(alg_data);
  643. err_kzalloc:
  644. return ret;
  645. }
  646. static int i2c_pnx_remove(struct platform_device *pdev)
  647. {
  648. struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev);
  649. free_irq(alg_data->irq, alg_data);
  650. i2c_del_adapter(&alg_data->adapter);
  651. clk_disable(alg_data->clk);
  652. iounmap(alg_data->ioaddr);
  653. release_mem_region(alg_data->base, I2C_PNX_REGION_SIZE);
  654. clk_put(alg_data->clk);
  655. kfree(alg_data);
  656. return 0;
  657. }
  658. #ifdef CONFIG_OF
  659. static const struct of_device_id i2c_pnx_of_match[] = {
  660. { .compatible = "nxp,pnx-i2c" },
  661. { },
  662. };
  663. MODULE_DEVICE_TABLE(of, i2c_pnx_of_match);
  664. #endif
  665. static struct platform_driver i2c_pnx_driver = {
  666. .driver = {
  667. .name = "pnx-i2c",
  668. .owner = THIS_MODULE,
  669. .of_match_table = of_match_ptr(i2c_pnx_of_match),
  670. .pm = PNX_I2C_PM,
  671. },
  672. .probe = i2c_pnx_probe,
  673. .remove = i2c_pnx_remove,
  674. };
  675. static int __init i2c_adap_pnx_init(void)
  676. {
  677. return platform_driver_register(&i2c_pnx_driver);
  678. }
  679. static void __exit i2c_adap_pnx_exit(void)
  680. {
  681. platform_driver_unregister(&i2c_pnx_driver);
  682. }
  683. MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>");
  684. MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses");
  685. MODULE_LICENSE("GPL");
  686. MODULE_ALIAS("platform:pnx-i2c");
  687. /* We need to make sure I2C is initialized before USB */
  688. subsys_initcall(i2c_adap_pnx_init);
  689. module_exit(i2c_adap_pnx_exit);