i2c-pxa.c 22 KB

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