i2c-pnx.c 21 KB

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