i2c-pnx.c 22 KB

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