i2c-pxa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * i2c_adap_pxa.c
  3. *
  4. * I2C adapter for the PXA I2C bus access.
  5. *
  6. * Copyright (C) 2002 Intrinsyc Software Inc.
  7. * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * History:
  14. * Apr 2002: Initial version [CS]
  15. * Jun 2002: Properly seperated algo/adap [FB]
  16. * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
  17. * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
  18. * Sep 2004: Major rework to ensure efficient bus handling [RMK]
  19. * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
  20. * Feb 2005: Rework slave mode handling [RMK]
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/i2c.h>
  25. #include <linux/i2c-id.h>
  26. #include <linux/init.h>
  27. #include <linux/time.h>
  28. #include <linux/sched.h>
  29. #include <linux/delay.h>
  30. #include <linux/errno.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/i2c-pxa.h>
  33. #include <linux/platform_device.h>
  34. #include <asm/hardware.h>
  35. #include <asm/irq.h>
  36. #include <asm/arch/i2c.h>
  37. #include <asm/arch/pxa-regs.h>
  38. struct pxa_i2c {
  39. spinlock_t lock;
  40. wait_queue_head_t wait;
  41. struct i2c_msg *msg;
  42. unsigned int msg_num;
  43. unsigned int msg_idx;
  44. unsigned int msg_ptr;
  45. unsigned int slave_addr;
  46. struct i2c_adapter adap;
  47. #ifdef CONFIG_I2C_PXA_SLAVE
  48. struct i2c_slave_client *slave;
  49. #endif
  50. unsigned int irqlogidx;
  51. u32 isrlog[32];
  52. u32 icrlog[32];
  53. };
  54. /*
  55. * I2C Slave mode address
  56. */
  57. #define I2C_PXA_SLAVE_ADDR 0x1
  58. #ifdef DEBUG
  59. struct bits {
  60. u32 mask;
  61. const char *set;
  62. const char *unset;
  63. };
  64. #define BIT(m, s, u) { .mask = m, .set = s, .unset = u }
  65. static inline void
  66. decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
  67. {
  68. printk("%s %08x: ", prefix, val);
  69. while (num--) {
  70. const char *str = val & bits->mask ? bits->set : bits->unset;
  71. if (str)
  72. printk("%s ", str);
  73. bits++;
  74. }
  75. }
  76. static const struct bits isr_bits[] = {
  77. BIT(ISR_RWM, "RX", "TX"),
  78. BIT(ISR_ACKNAK, "NAK", "ACK"),
  79. BIT(ISR_UB, "Bsy", "Rdy"),
  80. BIT(ISR_IBB, "BusBsy", "BusRdy"),
  81. BIT(ISR_SSD, "SlaveStop", NULL),
  82. BIT(ISR_ALD, "ALD", NULL),
  83. BIT(ISR_ITE, "TxEmpty", NULL),
  84. BIT(ISR_IRF, "RxFull", NULL),
  85. BIT(ISR_GCAD, "GenCall", NULL),
  86. BIT(ISR_SAD, "SlaveAddr", NULL),
  87. BIT(ISR_BED, "BusErr", NULL),
  88. };
  89. static void decode_ISR(unsigned int val)
  90. {
  91. decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
  92. printk("\n");
  93. }
  94. static const struct bits icr_bits[] = {
  95. BIT(ICR_START, "START", NULL),
  96. BIT(ICR_STOP, "STOP", NULL),
  97. BIT(ICR_ACKNAK, "ACKNAK", NULL),
  98. BIT(ICR_TB, "TB", NULL),
  99. BIT(ICR_MA, "MA", NULL),
  100. BIT(ICR_SCLE, "SCLE", "scle"),
  101. BIT(ICR_IUE, "IUE", "iue"),
  102. BIT(ICR_GCD, "GCD", NULL),
  103. BIT(ICR_ITEIE, "ITEIE", NULL),
  104. BIT(ICR_IRFIE, "IRFIE", NULL),
  105. BIT(ICR_BEIE, "BEIE", NULL),
  106. BIT(ICR_SSDIE, "SSDIE", NULL),
  107. BIT(ICR_ALDIE, "ALDIE", NULL),
  108. BIT(ICR_SADIE, "SADIE", NULL),
  109. BIT(ICR_UR, "UR", "ur"),
  110. };
  111. static void decode_ICR(unsigned int val)
  112. {
  113. decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
  114. printk("\n");
  115. }
  116. static unsigned int i2c_debug = DEBUG;
  117. static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
  118. {
  119. dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno, ISR, ICR, IBMR);
  120. }
  121. #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __FUNCTION__)
  122. #else
  123. #define i2c_debug 0
  124. #define show_state(i2c) do { } while (0)
  125. #define decode_ISR(val) do { } while (0)
  126. #define decode_ICR(val) do { } while (0)
  127. #endif
  128. #define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
  129. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
  130. static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
  131. {
  132. unsigned int i;
  133. printk("i2c: error: %s\n", why);
  134. printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
  135. i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
  136. printk("i2c: ICR: %08x ISR: %08x\n"
  137. "i2c: log: ", ICR, ISR);
  138. for (i = 0; i < i2c->irqlogidx; i++)
  139. printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
  140. printk("\n");
  141. }
  142. static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
  143. {
  144. return !(ICR & ICR_SCLE);
  145. }
  146. static void i2c_pxa_abort(struct pxa_i2c *i2c)
  147. {
  148. unsigned long timeout = jiffies + HZ/4;
  149. if (i2c_pxa_is_slavemode(i2c)) {
  150. dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
  151. return;
  152. }
  153. while (time_before(jiffies, timeout) && (IBMR & 0x1) == 0) {
  154. unsigned long icr = ICR;
  155. icr &= ~ICR_START;
  156. icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
  157. ICR = icr;
  158. show_state(i2c);
  159. msleep(1);
  160. }
  161. ICR &= ~(ICR_MA | ICR_START | ICR_STOP);
  162. }
  163. static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
  164. {
  165. int timeout = DEF_TIMEOUT;
  166. while (timeout-- && ISR & (ISR_IBB | ISR_UB)) {
  167. if ((ISR & ISR_SAD) != 0)
  168. timeout += 4;
  169. msleep(2);
  170. show_state(i2c);
  171. }
  172. if (timeout <= 0)
  173. show_state(i2c);
  174. return timeout <= 0 ? I2C_RETRY : 0;
  175. }
  176. static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
  177. {
  178. unsigned long timeout = jiffies + HZ*4;
  179. while (time_before(jiffies, timeout)) {
  180. if (i2c_debug > 1)
  181. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  182. __func__, (long)jiffies, ISR, ICR, IBMR);
  183. if (ISR & ISR_SAD) {
  184. if (i2c_debug > 0)
  185. dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
  186. goto out;
  187. }
  188. /* wait for unit and bus being not busy, and we also do a
  189. * quick check of the i2c lines themselves to ensure they've
  190. * gone high...
  191. */
  192. if ((ISR & (ISR_UB | ISR_IBB)) == 0 && IBMR == 3) {
  193. if (i2c_debug > 0)
  194. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  195. return 1;
  196. }
  197. msleep(1);
  198. }
  199. if (i2c_debug > 0)
  200. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  201. out:
  202. return 0;
  203. }
  204. static int i2c_pxa_set_master(struct pxa_i2c *i2c)
  205. {
  206. if (i2c_debug)
  207. dev_dbg(&i2c->adap.dev, "setting to bus master\n");
  208. if ((ISR & (ISR_UB | ISR_IBB)) != 0) {
  209. dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
  210. if (!i2c_pxa_wait_master(i2c)) {
  211. dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
  212. return I2C_RETRY;
  213. }
  214. }
  215. ICR |= ICR_SCLE;
  216. return 0;
  217. }
  218. #ifdef CONFIG_I2C_PXA_SLAVE
  219. static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
  220. {
  221. unsigned long timeout = jiffies + HZ*1;
  222. /* wait for stop */
  223. show_state(i2c);
  224. while (time_before(jiffies, timeout)) {
  225. if (i2c_debug > 1)
  226. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  227. __func__, (long)jiffies, ISR, ICR, IBMR);
  228. if ((ISR & (ISR_UB|ISR_IBB)) == 0 ||
  229. (ISR & ISR_SAD) != 0 ||
  230. (ICR & ICR_SCLE) == 0) {
  231. if (i2c_debug > 1)
  232. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  233. return 1;
  234. }
  235. msleep(1);
  236. }
  237. if (i2c_debug > 0)
  238. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  239. return 0;
  240. }
  241. /*
  242. * clear the hold on the bus, and take of anything else
  243. * that has been configured
  244. */
  245. static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
  246. {
  247. show_state(i2c);
  248. if (errcode < 0) {
  249. udelay(100); /* simple delay */
  250. } else {
  251. /* we need to wait for the stop condition to end */
  252. /* if we where in stop, then clear... */
  253. if (ICR & ICR_STOP) {
  254. udelay(100);
  255. ICR &= ~ICR_STOP;
  256. }
  257. if (!i2c_pxa_wait_slave(i2c)) {
  258. dev_err(&i2c->adap.dev, "%s: wait timedout\n",
  259. __func__);
  260. return;
  261. }
  262. }
  263. ICR &= ~(ICR_STOP|ICR_ACKNAK|ICR_MA);
  264. ICR &= ~ICR_SCLE;
  265. if (i2c_debug) {
  266. dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", ICR, ISR);
  267. decode_ICR(ICR);
  268. }
  269. }
  270. #else
  271. #define i2c_pxa_set_slave(i2c, err) do { } while (0)
  272. #endif
  273. static void i2c_pxa_reset(struct pxa_i2c *i2c)
  274. {
  275. pr_debug("Resetting I2C Controller Unit\n");
  276. /* abort any transfer currently under way */
  277. i2c_pxa_abort(i2c);
  278. /* reset according to 9.8 */
  279. ICR = ICR_UR;
  280. ISR = I2C_ISR_INIT;
  281. ICR &= ~ICR_UR;
  282. ISAR = i2c->slave_addr;
  283. /* set control register values */
  284. ICR = I2C_ICR_INIT;
  285. #ifdef CONFIG_I2C_PXA_SLAVE
  286. dev_info(&i2c->adap.dev, "Enabling slave mode\n");
  287. ICR |= ICR_SADIE | ICR_ALDIE | ICR_SSDIE;
  288. #endif
  289. i2c_pxa_set_slave(i2c, 0);
  290. /* enable unit */
  291. ICR |= ICR_IUE;
  292. udelay(100);
  293. }
  294. #ifdef CONFIG_I2C_PXA_SLAVE
  295. /*
  296. * PXA I2C Slave mode
  297. */
  298. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  299. {
  300. if (isr & ISR_BED) {
  301. /* what should we do here? */
  302. } else {
  303. int ret = 0;
  304. if (i2c->slave != NULL)
  305. ret = i2c->slave->read(i2c->slave->data);
  306. IDBR = ret;
  307. ICR |= ICR_TB; /* allow next byte */
  308. }
  309. }
  310. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  311. {
  312. unsigned int byte = IDBR;
  313. if (i2c->slave != NULL)
  314. i2c->slave->write(i2c->slave->data, byte);
  315. ICR |= ICR_TB;
  316. }
  317. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  318. {
  319. int timeout;
  320. if (i2c_debug > 0)
  321. dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
  322. (isr & ISR_RWM) ? 'r' : 't');
  323. if (i2c->slave != NULL)
  324. i2c->slave->event(i2c->slave->data,
  325. (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
  326. /*
  327. * slave could interrupt in the middle of us generating a
  328. * start condition... if this happens, we'd better back off
  329. * and stop holding the poor thing up
  330. */
  331. ICR &= ~(ICR_START|ICR_STOP);
  332. ICR |= ICR_TB;
  333. timeout = 0x10000;
  334. while (1) {
  335. if ((IBMR & 2) == 2)
  336. break;
  337. timeout--;
  338. if (timeout <= 0) {
  339. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  340. break;
  341. }
  342. }
  343. ICR &= ~ICR_SCLE;
  344. }
  345. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  346. {
  347. if (i2c_debug > 2)
  348. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
  349. if (i2c->slave != NULL)
  350. i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
  351. if (i2c_debug > 2)
  352. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
  353. /*
  354. * If we have a master-mode message waiting,
  355. * kick it off now that the slave has completed.
  356. */
  357. if (i2c->msg)
  358. i2c_pxa_master_complete(i2c, I2C_RETRY);
  359. }
  360. #else
  361. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  362. {
  363. if (isr & ISR_BED) {
  364. /* what should we do here? */
  365. } else {
  366. IDBR = 0;
  367. ICR |= ICR_TB;
  368. }
  369. }
  370. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  371. {
  372. ICR |= ICR_TB | ICR_ACKNAK;
  373. }
  374. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  375. {
  376. int timeout;
  377. /*
  378. * slave could interrupt in the middle of us generating a
  379. * start condition... if this happens, we'd better back off
  380. * and stop holding the poor thing up
  381. */
  382. ICR &= ~(ICR_START|ICR_STOP);
  383. ICR |= ICR_TB | ICR_ACKNAK;
  384. timeout = 0x10000;
  385. while (1) {
  386. if ((IBMR & 2) == 2)
  387. break;
  388. timeout--;
  389. if (timeout <= 0) {
  390. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  391. break;
  392. }
  393. }
  394. ICR &= ~ICR_SCLE;
  395. }
  396. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  397. {
  398. if (i2c->msg)
  399. i2c_pxa_master_complete(i2c, I2C_RETRY);
  400. }
  401. #endif
  402. /*
  403. * PXA I2C Master mode
  404. */
  405. static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
  406. {
  407. unsigned int addr = (msg->addr & 0x7f) << 1;
  408. if (msg->flags & I2C_M_RD)
  409. addr |= 1;
  410. return addr;
  411. }
  412. static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
  413. {
  414. u32 icr;
  415. /*
  416. * Step 1: target slave address into IDBR
  417. */
  418. IDBR = i2c_pxa_addr_byte(i2c->msg);
  419. /*
  420. * Step 2: initiate the write.
  421. */
  422. icr = ICR & ~(ICR_STOP | ICR_ALDIE);
  423. ICR = icr | ICR_START | ICR_TB;
  424. }
  425. /*
  426. * We are protected by the adapter bus mutex.
  427. */
  428. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  429. {
  430. long timeout;
  431. int ret;
  432. /*
  433. * Wait for the bus to become free.
  434. */
  435. ret = i2c_pxa_wait_bus_not_busy(i2c);
  436. if (ret) {
  437. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  438. goto out;
  439. }
  440. /*
  441. * Set master mode.
  442. */
  443. ret = i2c_pxa_set_master(i2c);
  444. if (ret) {
  445. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  446. goto out;
  447. }
  448. spin_lock_irq(&i2c->lock);
  449. i2c->msg = msg;
  450. i2c->msg_num = num;
  451. i2c->msg_idx = 0;
  452. i2c->msg_ptr = 0;
  453. i2c->irqlogidx = 0;
  454. i2c_pxa_start_message(i2c);
  455. spin_unlock_irq(&i2c->lock);
  456. /*
  457. * The rest of the processing occurs in the interrupt handler.
  458. */
  459. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  460. /*
  461. * We place the return code in i2c->msg_idx.
  462. */
  463. ret = i2c->msg_idx;
  464. if (timeout == 0)
  465. i2c_pxa_scream_blue_murder(i2c, "timeout");
  466. out:
  467. return ret;
  468. }
  469. /*
  470. * i2c_pxa_master_complete - complete the message and wake up.
  471. */
  472. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  473. {
  474. i2c->msg_ptr = 0;
  475. i2c->msg = NULL;
  476. i2c->msg_idx ++;
  477. i2c->msg_num = 0;
  478. if (ret)
  479. i2c->msg_idx = ret;
  480. wake_up(&i2c->wait);
  481. }
  482. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  483. {
  484. u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  485. again:
  486. /*
  487. * If ISR_ALD is set, we lost arbitration.
  488. */
  489. if (isr & ISR_ALD) {
  490. /*
  491. * Do we need to do anything here? The PXA docs
  492. * are vague about what happens.
  493. */
  494. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  495. /*
  496. * We ignore this error. We seem to see spurious ALDs
  497. * for seemingly no reason. If we handle them as I think
  498. * they should, we end up causing an I2C error, which
  499. * is painful for some systems.
  500. */
  501. return; /* ignore */
  502. }
  503. if (isr & ISR_BED) {
  504. int ret = BUS_ERROR;
  505. /*
  506. * I2C bus error - either the device NAK'd us, or
  507. * something more serious happened. If we were NAK'd
  508. * on the initial address phase, we can retry.
  509. */
  510. if (isr & ISR_ACKNAK) {
  511. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  512. ret = I2C_RETRY;
  513. else
  514. ret = XFER_NAKED;
  515. }
  516. i2c_pxa_master_complete(i2c, ret);
  517. } else if (isr & ISR_RWM) {
  518. /*
  519. * Read mode. We have just sent the address byte, and
  520. * now we must initiate the transfer.
  521. */
  522. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  523. i2c->msg_idx == i2c->msg_num - 1)
  524. icr |= ICR_STOP | ICR_ACKNAK;
  525. icr |= ICR_ALDIE | ICR_TB;
  526. } else if (i2c->msg_ptr < i2c->msg->len) {
  527. /*
  528. * Write mode. Write the next data byte.
  529. */
  530. IDBR = i2c->msg->buf[i2c->msg_ptr++];
  531. icr |= ICR_ALDIE | ICR_TB;
  532. /*
  533. * If this is the last byte of the last message, send
  534. * a STOP.
  535. */
  536. if (i2c->msg_ptr == i2c->msg->len &&
  537. i2c->msg_idx == i2c->msg_num - 1)
  538. icr |= ICR_STOP;
  539. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  540. /*
  541. * Next segment of the message.
  542. */
  543. i2c->msg_ptr = 0;
  544. i2c->msg_idx ++;
  545. i2c->msg++;
  546. /*
  547. * If we aren't doing a repeated start and address,
  548. * go back and try to send the next byte. Note that
  549. * we do not support switching the R/W direction here.
  550. */
  551. if (i2c->msg->flags & I2C_M_NOSTART)
  552. goto again;
  553. /*
  554. * Write the next address.
  555. */
  556. IDBR = i2c_pxa_addr_byte(i2c->msg);
  557. /*
  558. * And trigger a repeated start, and send the byte.
  559. */
  560. icr &= ~ICR_ALDIE;
  561. icr |= ICR_START | ICR_TB;
  562. } else {
  563. if (i2c->msg->len == 0) {
  564. /*
  565. * Device probes have a message length of zero
  566. * and need the bus to be reset before it can
  567. * be used again.
  568. */
  569. i2c_pxa_reset(i2c);
  570. }
  571. i2c_pxa_master_complete(i2c, 0);
  572. }
  573. i2c->icrlog[i2c->irqlogidx-1] = icr;
  574. ICR = icr;
  575. show_state(i2c);
  576. }
  577. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  578. {
  579. u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  580. /*
  581. * Read the byte.
  582. */
  583. i2c->msg->buf[i2c->msg_ptr++] = IDBR;
  584. if (i2c->msg_ptr < i2c->msg->len) {
  585. /*
  586. * If this is the last byte of the last
  587. * message, send a STOP.
  588. */
  589. if (i2c->msg_ptr == i2c->msg->len - 1)
  590. icr |= ICR_STOP | ICR_ACKNAK;
  591. icr |= ICR_ALDIE | ICR_TB;
  592. } else {
  593. i2c_pxa_master_complete(i2c, 0);
  594. }
  595. i2c->icrlog[i2c->irqlogidx-1] = icr;
  596. ICR = icr;
  597. }
  598. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  599. {
  600. struct pxa_i2c *i2c = dev_id;
  601. u32 isr = ISR;
  602. if (i2c_debug > 2 && 0) {
  603. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  604. __func__, isr, ICR, IBMR);
  605. decode_ISR(isr);
  606. }
  607. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  608. i2c->isrlog[i2c->irqlogidx++] = isr;
  609. show_state(i2c);
  610. /*
  611. * Always clear all pending IRQs.
  612. */
  613. ISR = isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED);
  614. if (isr & ISR_SAD)
  615. i2c_pxa_slave_start(i2c, isr);
  616. if (isr & ISR_SSD)
  617. i2c_pxa_slave_stop(i2c);
  618. if (i2c_pxa_is_slavemode(i2c)) {
  619. if (isr & ISR_ITE)
  620. i2c_pxa_slave_txempty(i2c, isr);
  621. if (isr & ISR_IRF)
  622. i2c_pxa_slave_rxfull(i2c, isr);
  623. } else if (i2c->msg) {
  624. if (isr & ISR_ITE)
  625. i2c_pxa_irq_txempty(i2c, isr);
  626. if (isr & ISR_IRF)
  627. i2c_pxa_irq_rxfull(i2c, isr);
  628. } else {
  629. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  630. }
  631. return IRQ_HANDLED;
  632. }
  633. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  634. {
  635. struct pxa_i2c *i2c = adap->algo_data;
  636. int ret, i;
  637. /* If the I2C controller is disabled we need to reset it (probably due
  638. to a suspend/resume destroying state). We do this here as we can then
  639. avoid worrying about resuming the controller before its users. */
  640. if (!(ICR & ICR_IUE))
  641. i2c_pxa_reset(i2c);
  642. for (i = adap->retries; i >= 0; i--) {
  643. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  644. if (ret != I2C_RETRY)
  645. goto out;
  646. if (i2c_debug)
  647. dev_dbg(&adap->dev, "Retrying transmission\n");
  648. udelay(100);
  649. }
  650. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  651. ret = -EREMOTEIO;
  652. out:
  653. i2c_pxa_set_slave(i2c, ret);
  654. return ret;
  655. }
  656. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  657. {
  658. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  659. }
  660. static const struct i2c_algorithm i2c_pxa_algorithm = {
  661. .master_xfer = i2c_pxa_xfer,
  662. .functionality = i2c_pxa_functionality,
  663. };
  664. static struct pxa_i2c i2c_pxa = {
  665. .lock = SPIN_LOCK_UNLOCKED,
  666. .wait = __WAIT_QUEUE_HEAD_INITIALIZER(i2c_pxa.wait),
  667. .adap = {
  668. .owner = THIS_MODULE,
  669. .algo = &i2c_pxa_algorithm,
  670. .name = "pxa2xx-i2c",
  671. .retries = 5,
  672. },
  673. };
  674. static int i2c_pxa_probe(struct platform_device *dev)
  675. {
  676. struct pxa_i2c *i2c = &i2c_pxa;
  677. #ifdef CONFIG_I2C_PXA_SLAVE
  678. struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
  679. #endif
  680. int ret;
  681. #ifdef CONFIG_PXA27x
  682. pxa_gpio_mode(GPIO117_I2CSCL_MD);
  683. pxa_gpio_mode(GPIO118_I2CSDA_MD);
  684. udelay(100);
  685. #endif
  686. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  687. #ifdef CONFIG_I2C_PXA_SLAVE
  688. if (plat) {
  689. i2c->slave_addr = plat->slave_addr;
  690. i2c->slave = plat->slave;
  691. }
  692. #endif
  693. pxa_set_cken(CKEN14_I2C, 1);
  694. ret = request_irq(IRQ_I2C, i2c_pxa_handler, IRQF_DISABLED,
  695. "pxa2xx-i2c", i2c);
  696. if (ret)
  697. goto out;
  698. i2c_pxa_reset(i2c);
  699. i2c->adap.algo_data = i2c;
  700. i2c->adap.dev.parent = &dev->dev;
  701. ret = i2c_add_adapter(&i2c->adap);
  702. if (ret < 0) {
  703. printk(KERN_INFO "I2C: Failed to add bus\n");
  704. goto err_irq;
  705. }
  706. platform_set_drvdata(dev, i2c);
  707. #ifdef CONFIG_I2C_PXA_SLAVE
  708. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  709. i2c->adap.dev.bus_id, i2c->slave_addr);
  710. #else
  711. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  712. i2c->adap.dev.bus_id);
  713. #endif
  714. return 0;
  715. err_irq:
  716. free_irq(IRQ_I2C, i2c);
  717. out:
  718. return ret;
  719. }
  720. static int i2c_pxa_remove(struct platform_device *dev)
  721. {
  722. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  723. platform_set_drvdata(dev, NULL);
  724. i2c_del_adapter(&i2c->adap);
  725. free_irq(IRQ_I2C, i2c);
  726. pxa_set_cken(CKEN14_I2C, 0);
  727. return 0;
  728. }
  729. static struct platform_driver i2c_pxa_driver = {
  730. .probe = i2c_pxa_probe,
  731. .remove = i2c_pxa_remove,
  732. .driver = {
  733. .name = "pxa2xx-i2c",
  734. },
  735. };
  736. static int __init i2c_adap_pxa_init(void)
  737. {
  738. return platform_driver_register(&i2c_pxa_driver);
  739. }
  740. static void i2c_adap_pxa_exit(void)
  741. {
  742. return platform_driver_unregister(&i2c_pxa_driver);
  743. }
  744. MODULE_LICENSE("GPL");
  745. module_init(i2c_adap_pxa_init);
  746. module_exit(i2c_adap_pxa_exit);