i2c-pxa.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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|ISR_SAD)) == ISR_SAD ||
  229. (ICR & ICR_SCLE) == 0) {
  230. if (i2c_debug > 1)
  231. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  232. return 1;
  233. }
  234. msleep(1);
  235. }
  236. if (i2c_debug > 0)
  237. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  238. return 0;
  239. }
  240. /*
  241. * clear the hold on the bus, and take of anything else
  242. * that has been configured
  243. */
  244. static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
  245. {
  246. show_state(i2c);
  247. if (errcode < 0) {
  248. udelay(100); /* simple delay */
  249. } else {
  250. /* we need to wait for the stop condition to end */
  251. /* if we where in stop, then clear... */
  252. if (ICR & ICR_STOP) {
  253. udelay(100);
  254. ICR &= ~ICR_STOP;
  255. }
  256. if (!i2c_pxa_wait_slave(i2c)) {
  257. dev_err(&i2c->adap.dev, "%s: wait timedout\n",
  258. __func__);
  259. return;
  260. }
  261. }
  262. ICR &= ~(ICR_STOP|ICR_ACKNAK|ICR_MA);
  263. ICR &= ~ICR_SCLE;
  264. if (i2c_debug) {
  265. dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", ICR, ISR);
  266. decode_ICR(ICR);
  267. }
  268. }
  269. #else
  270. #define i2c_pxa_set_slave(i2c, err) do { } while (0)
  271. #endif
  272. static void i2c_pxa_reset(struct pxa_i2c *i2c)
  273. {
  274. pr_debug("Resetting I2C Controller Unit\n");
  275. /* abort any transfer currently under way */
  276. i2c_pxa_abort(i2c);
  277. /* reset according to 9.8 */
  278. ICR = ICR_UR;
  279. ISR = I2C_ISR_INIT;
  280. ICR &= ~ICR_UR;
  281. ISAR = i2c->slave_addr;
  282. /* set control register values */
  283. ICR = I2C_ICR_INIT;
  284. #ifdef CONFIG_I2C_PXA_SLAVE
  285. dev_info(&i2c->adap.dev, "Enabling slave mode\n");
  286. ICR |= ICR_SADIE | ICR_ALDIE | ICR_SSDIE;
  287. #endif
  288. i2c_pxa_set_slave(i2c, 0);
  289. /* enable unit */
  290. ICR |= ICR_IUE;
  291. udelay(100);
  292. }
  293. #ifdef CONFIG_I2C_PXA_SLAVE
  294. /*
  295. * I2C EEPROM emulation.
  296. */
  297. static struct i2c_eeprom_emu eeprom = {
  298. .size = I2C_EEPROM_EMU_SIZE,
  299. .watch = LIST_HEAD_INIT(eeprom.watch),
  300. };
  301. struct i2c_eeprom_emu *i2c_pxa_get_eeprom(void)
  302. {
  303. return &eeprom;
  304. }
  305. int i2c_eeprom_emu_addwatcher(struct i2c_eeprom_emu *emu, void *data,
  306. unsigned int addr, unsigned int size,
  307. struct i2c_eeprom_emu_watcher *watcher)
  308. {
  309. struct i2c_eeprom_emu_watch *watch;
  310. unsigned long flags;
  311. if (addr + size > emu->size)
  312. return -EINVAL;
  313. watch = kmalloc(sizeof(struct i2c_eeprom_emu_watch), GFP_KERNEL);
  314. if (watch) {
  315. watch->start = addr;
  316. watch->end = addr + size - 1;
  317. watch->ops = watcher;
  318. watch->data = data;
  319. local_irq_save(flags);
  320. list_add(&watch->node, &emu->watch);
  321. local_irq_restore(flags);
  322. }
  323. return watch ? 0 : -ENOMEM;
  324. }
  325. void i2c_eeprom_emu_delwatcher(struct i2c_eeprom_emu *emu, void *data,
  326. struct i2c_eeprom_emu_watcher *watcher)
  327. {
  328. struct i2c_eeprom_emu_watch *watch, *n;
  329. unsigned long flags;
  330. list_for_each_entry_safe(watch, n, &emu->watch, node) {
  331. if (watch->ops == watcher && watch->data == data) {
  332. local_irq_save(flags);
  333. list_del(&watch->node);
  334. local_irq_restore(flags);
  335. kfree(watch);
  336. }
  337. }
  338. }
  339. static void i2c_eeprom_emu_event(void *ptr, i2c_slave_event_t event)
  340. {
  341. struct i2c_eeprom_emu *emu = ptr;
  342. eedbg(3, "i2c_eeprom_emu_event: %d\n", event);
  343. switch (event) {
  344. case I2C_SLAVE_EVENT_START_WRITE:
  345. emu->seen_start = 1;
  346. eedbg(2, "i2c_eeprom: write initiated\n");
  347. break;
  348. case I2C_SLAVE_EVENT_START_READ:
  349. emu->seen_start = 0;
  350. eedbg(2, "i2c_eeprom: read initiated\n");
  351. break;
  352. case I2C_SLAVE_EVENT_STOP:
  353. emu->seen_start = 0;
  354. eedbg(2, "i2c_eeprom: received stop\n");
  355. break;
  356. default:
  357. eedbg(0, "i2c_eeprom: unhandled event\n");
  358. break;
  359. }
  360. }
  361. static int i2c_eeprom_emu_read(void *ptr)
  362. {
  363. struct i2c_eeprom_emu *emu = ptr;
  364. int ret;
  365. ret = emu->bytes[emu->ptr];
  366. emu->ptr = (emu->ptr + 1) % emu->size;
  367. return ret;
  368. }
  369. static void i2c_eeprom_emu_write(void *ptr, unsigned int val)
  370. {
  371. struct i2c_eeprom_emu *emu = ptr;
  372. struct i2c_eeprom_emu_watch *watch;
  373. if (emu->seen_start != 0) {
  374. eedbg(2, "i2c_eeprom_emu_write: setting ptr %02x\n", val);
  375. emu->ptr = val;
  376. emu->seen_start = 0;
  377. return;
  378. }
  379. emu->bytes[emu->ptr] = val;
  380. eedbg(1, "i2c_eeprom_emu_write: ptr=0x%02x, val=0x%02x\n",
  381. emu->ptr, val);
  382. list_for_each_entry(watch, &emu->watch, node) {
  383. if (!watch->ops || !watch->ops->write)
  384. continue;
  385. if (watch->start <= emu->ptr && watch->end >= emu->ptr)
  386. watch->ops->write(watch->data, emu->ptr, val);
  387. }
  388. emu->ptr = (emu->ptr + 1) % emu->size;
  389. }
  390. struct i2c_slave_client eeprom_client = {
  391. .data = &eeprom,
  392. .event = i2c_eeprom_emu_event,
  393. .read = i2c_eeprom_emu_read,
  394. .write = i2c_eeprom_emu_write
  395. };
  396. /*
  397. * PXA I2C Slave mode
  398. */
  399. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  400. {
  401. if (isr & ISR_BED) {
  402. /* what should we do here? */
  403. } else {
  404. int ret = i2c->slave->read(i2c->slave->data);
  405. IDBR = ret;
  406. ICR |= ICR_TB; /* allow next byte */
  407. }
  408. }
  409. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  410. {
  411. unsigned int byte = IDBR;
  412. if (i2c->slave != NULL)
  413. i2c->slave->write(i2c->slave->data, byte);
  414. ICR |= ICR_TB;
  415. }
  416. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  417. {
  418. int timeout;
  419. if (i2c_debug > 0)
  420. dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
  421. (isr & ISR_RWM) ? 'r' : 't');
  422. if (i2c->slave != NULL)
  423. i2c->slave->event(i2c->slave->data,
  424. (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
  425. /*
  426. * slave could interrupt in the middle of us generating a
  427. * start condition... if this happens, we'd better back off
  428. * and stop holding the poor thing up
  429. */
  430. ICR &= ~(ICR_START|ICR_STOP);
  431. ICR |= ICR_TB;
  432. timeout = 0x10000;
  433. while (1) {
  434. if ((IBMR & 2) == 2)
  435. break;
  436. timeout--;
  437. if (timeout <= 0) {
  438. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  439. break;
  440. }
  441. }
  442. ICR &= ~ICR_SCLE;
  443. }
  444. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  445. {
  446. if (i2c_debug > 2)
  447. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
  448. if (i2c->slave != NULL)
  449. i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
  450. if (i2c_debug > 2)
  451. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
  452. /*
  453. * If we have a master-mode message waiting,
  454. * kick it off now that the slave has completed.
  455. */
  456. if (i2c->msg)
  457. i2c_pxa_master_complete(i2c, I2C_RETRY);
  458. }
  459. #else
  460. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  461. {
  462. if (isr & ISR_BED) {
  463. /* what should we do here? */
  464. } else {
  465. IDBR = 0;
  466. ICR |= ICR_TB;
  467. }
  468. }
  469. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  470. {
  471. ICR |= ICR_TB | ICR_ACKNAK;
  472. }
  473. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  474. {
  475. int timeout;
  476. /*
  477. * slave could interrupt in the middle of us generating a
  478. * start condition... if this happens, we'd better back off
  479. * and stop holding the poor thing up
  480. */
  481. ICR &= ~(ICR_START|ICR_STOP);
  482. ICR |= ICR_TB | ICR_ACKNAK;
  483. timeout = 0x10000;
  484. while (1) {
  485. if ((IBMR & 2) == 2)
  486. break;
  487. timeout--;
  488. if (timeout <= 0) {
  489. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  490. break;
  491. }
  492. }
  493. ICR &= ~ICR_SCLE;
  494. }
  495. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  496. {
  497. if (i2c->msg)
  498. i2c_pxa_master_complete(i2c, I2C_RETRY);
  499. }
  500. #endif
  501. /*
  502. * PXA I2C Master mode
  503. */
  504. static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
  505. {
  506. unsigned int addr = (msg->addr & 0x7f) << 1;
  507. if (msg->flags & I2C_M_RD)
  508. addr |= 1;
  509. return addr;
  510. }
  511. static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
  512. {
  513. u32 icr;
  514. /*
  515. * Step 1: target slave address into IDBR
  516. */
  517. IDBR = i2c_pxa_addr_byte(i2c->msg);
  518. /*
  519. * Step 2: initiate the write.
  520. */
  521. icr = ICR & ~(ICR_STOP | ICR_ALDIE);
  522. ICR = icr | ICR_START | ICR_TB;
  523. }
  524. /*
  525. * We are protected by the adapter bus semaphore.
  526. */
  527. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  528. {
  529. long timeout;
  530. int ret;
  531. /*
  532. * Wait for the bus to become free.
  533. */
  534. ret = i2c_pxa_wait_bus_not_busy(i2c);
  535. if (ret) {
  536. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  537. goto out;
  538. }
  539. /*
  540. * Set master mode.
  541. */
  542. ret = i2c_pxa_set_master(i2c);
  543. if (ret) {
  544. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  545. goto out;
  546. }
  547. spin_lock_irq(&i2c->lock);
  548. i2c->msg = msg;
  549. i2c->msg_num = num;
  550. i2c->msg_idx = 0;
  551. i2c->msg_ptr = 0;
  552. i2c->irqlogidx = 0;
  553. i2c_pxa_start_message(i2c);
  554. spin_unlock_irq(&i2c->lock);
  555. /*
  556. * The rest of the processing occurs in the interrupt handler.
  557. */
  558. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  559. /*
  560. * We place the return code in i2c->msg_idx.
  561. */
  562. ret = i2c->msg_idx;
  563. if (timeout == 0)
  564. i2c_pxa_scream_blue_murder(i2c, "timeout");
  565. out:
  566. return ret;
  567. }
  568. /*
  569. * i2c_pxa_master_complete - complete the message and wake up.
  570. */
  571. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  572. {
  573. i2c->msg_ptr = 0;
  574. i2c->msg = NULL;
  575. i2c->msg_idx ++;
  576. i2c->msg_num = 0;
  577. if (ret)
  578. i2c->msg_idx = ret;
  579. wake_up(&i2c->wait);
  580. }
  581. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  582. {
  583. u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  584. again:
  585. /*
  586. * If ISR_ALD is set, we lost arbitration.
  587. */
  588. if (isr & ISR_ALD) {
  589. /*
  590. * Do we need to do anything here? The PXA docs
  591. * are vague about what happens.
  592. */
  593. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  594. /*
  595. * We ignore this error. We seem to see spurious ALDs
  596. * for seemingly no reason. If we handle them as I think
  597. * they should, we end up causing an I2C error, which
  598. * is painful for some systems.
  599. */
  600. return; /* ignore */
  601. }
  602. if (isr & ISR_BED) {
  603. int ret = BUS_ERROR;
  604. /*
  605. * I2C bus error - either the device NAK'd us, or
  606. * something more serious happened. If we were NAK'd
  607. * on the initial address phase, we can retry.
  608. */
  609. if (isr & ISR_ACKNAK) {
  610. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  611. ret = I2C_RETRY;
  612. else
  613. ret = XFER_NAKED;
  614. }
  615. i2c_pxa_master_complete(i2c, ret);
  616. } else if (isr & ISR_RWM) {
  617. /*
  618. * Read mode. We have just sent the address byte, and
  619. * now we must initiate the transfer.
  620. */
  621. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  622. i2c->msg_idx == i2c->msg_num - 1)
  623. icr |= ICR_STOP | ICR_ACKNAK;
  624. icr |= ICR_ALDIE | ICR_TB;
  625. } else if (i2c->msg_ptr < i2c->msg->len) {
  626. /*
  627. * Write mode. Write the next data byte.
  628. */
  629. IDBR = i2c->msg->buf[i2c->msg_ptr++];
  630. icr |= ICR_ALDIE | ICR_TB;
  631. /*
  632. * If this is the last byte of the last message, send
  633. * a STOP.
  634. */
  635. if (i2c->msg_ptr == i2c->msg->len &&
  636. i2c->msg_idx == i2c->msg_num - 1)
  637. icr |= ICR_STOP;
  638. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  639. /*
  640. * Next segment of the message.
  641. */
  642. i2c->msg_ptr = 0;
  643. i2c->msg_idx ++;
  644. i2c->msg++;
  645. /*
  646. * If we aren't doing a repeated start and address,
  647. * go back and try to send the next byte. Note that
  648. * we do not support switching the R/W direction here.
  649. */
  650. if (i2c->msg->flags & I2C_M_NOSTART)
  651. goto again;
  652. /*
  653. * Write the next address.
  654. */
  655. IDBR = i2c_pxa_addr_byte(i2c->msg);
  656. /*
  657. * And trigger a repeated start, and send the byte.
  658. */
  659. icr &= ~ICR_ALDIE;
  660. icr |= ICR_START | ICR_TB;
  661. } else {
  662. if (i2c->msg->len == 0) {
  663. /*
  664. * Device probes have a message length of zero
  665. * and need the bus to be reset before it can
  666. * be used again.
  667. */
  668. i2c_pxa_reset(i2c);
  669. }
  670. i2c_pxa_master_complete(i2c, 0);
  671. }
  672. i2c->icrlog[i2c->irqlogidx-1] = icr;
  673. ICR = icr;
  674. show_state(i2c);
  675. }
  676. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  677. {
  678. u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  679. /*
  680. * Read the byte.
  681. */
  682. i2c->msg->buf[i2c->msg_ptr++] = IDBR;
  683. if (i2c->msg_ptr < i2c->msg->len) {
  684. /*
  685. * If this is the last byte of the last
  686. * message, send a STOP.
  687. */
  688. if (i2c->msg_ptr == i2c->msg->len - 1)
  689. icr |= ICR_STOP | ICR_ACKNAK;
  690. icr |= ICR_ALDIE | ICR_TB;
  691. } else {
  692. i2c_pxa_master_complete(i2c, 0);
  693. }
  694. i2c->icrlog[i2c->irqlogidx-1] = icr;
  695. ICR = icr;
  696. }
  697. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id, struct pt_regs *regs)
  698. {
  699. struct pxa_i2c *i2c = dev_id;
  700. u32 isr = ISR;
  701. if (i2c_debug > 2 && 0) {
  702. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  703. __func__, isr, ICR, IBMR);
  704. decode_ISR(isr);
  705. }
  706. if (i2c->irqlogidx < sizeof(i2c->isrlog)/sizeof(u32))
  707. i2c->isrlog[i2c->irqlogidx++] = isr;
  708. show_state(i2c);
  709. /*
  710. * Always clear all pending IRQs.
  711. */
  712. ISR = isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED);
  713. if (isr & ISR_SAD)
  714. i2c_pxa_slave_start(i2c, isr);
  715. if (isr & ISR_SSD)
  716. i2c_pxa_slave_stop(i2c);
  717. if (i2c_pxa_is_slavemode(i2c)) {
  718. if (isr & ISR_ITE)
  719. i2c_pxa_slave_txempty(i2c, isr);
  720. if (isr & ISR_IRF)
  721. i2c_pxa_slave_rxfull(i2c, isr);
  722. } else if (i2c->msg) {
  723. if (isr & ISR_ITE)
  724. i2c_pxa_irq_txempty(i2c, isr);
  725. if (isr & ISR_IRF)
  726. i2c_pxa_irq_rxfull(i2c, isr);
  727. } else {
  728. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  729. }
  730. return IRQ_HANDLED;
  731. }
  732. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  733. {
  734. struct pxa_i2c *i2c = adap->algo_data;
  735. int ret, i;
  736. for (i = adap->retries; i >= 0; i--) {
  737. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  738. if (ret != I2C_RETRY)
  739. goto out;
  740. if (i2c_debug)
  741. dev_dbg(&adap->dev, "Retrying transmission\n");
  742. udelay(100);
  743. }
  744. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  745. ret = -EREMOTEIO;
  746. out:
  747. i2c_pxa_set_slave(i2c, ret);
  748. return ret;
  749. }
  750. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  751. {
  752. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  753. }
  754. static struct i2c_algorithm i2c_pxa_algorithm = {
  755. .master_xfer = i2c_pxa_xfer,
  756. .functionality = i2c_pxa_functionality,
  757. };
  758. static struct pxa_i2c i2c_pxa = {
  759. .lock = SPIN_LOCK_UNLOCKED,
  760. .wait = __WAIT_QUEUE_HEAD_INITIALIZER(i2c_pxa.wait),
  761. .adap = {
  762. .owner = THIS_MODULE,
  763. .algo = &i2c_pxa_algorithm,
  764. .name = "pxa2xx-i2c",
  765. .retries = 5,
  766. },
  767. };
  768. static int i2c_pxa_probe(struct platform_device *dev)
  769. {
  770. struct pxa_i2c *i2c = &i2c_pxa;
  771. struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
  772. int ret;
  773. #ifdef CONFIG_PXA27x
  774. pxa_gpio_mode(GPIO117_I2CSCL_MD);
  775. pxa_gpio_mode(GPIO118_I2CSDA_MD);
  776. udelay(100);
  777. #endif
  778. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  779. #ifdef CONFIG_I2C_PXA_SLAVE
  780. i2c->slave = &eeprom_client;
  781. if (plat) {
  782. i2c->slave_addr = plat->slave_addr;
  783. if (plat->slave)
  784. i2c->slave = plat->slave;
  785. }
  786. #endif
  787. pxa_set_cken(CKEN14_I2C, 1);
  788. ret = request_irq(IRQ_I2C, i2c_pxa_handler, SA_INTERRUPT,
  789. "pxa2xx-i2c", i2c);
  790. if (ret)
  791. goto out;
  792. i2c_pxa_reset(i2c);
  793. i2c->adap.algo_data = i2c;
  794. i2c->adap.dev.parent = &dev->dev;
  795. ret = i2c_add_adapter(&i2c->adap);
  796. if (ret < 0) {
  797. printk(KERN_INFO "I2C: Failed to add bus\n");
  798. goto err_irq;
  799. }
  800. platform_set_drvdata(dev, i2c);
  801. #ifdef CONFIG_I2C_PXA_SLAVE
  802. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  803. i2c->adap.dev.bus_id, i2c->slave_addr);
  804. #else
  805. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  806. i2c->adap.dev.bus_id);
  807. #endif
  808. return 0;
  809. err_irq:
  810. free_irq(IRQ_I2C, i2c);
  811. out:
  812. return ret;
  813. }
  814. static int i2c_pxa_remove(struct platform_device *dev)
  815. {
  816. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  817. platform_set_drvdata(dev, NULL);
  818. i2c_del_adapter(&i2c->adap);
  819. free_irq(IRQ_I2C, i2c);
  820. pxa_set_cken(CKEN14_I2C, 0);
  821. return 0;
  822. }
  823. static struct platform_driver i2c_pxa_driver = {
  824. .probe = i2c_pxa_probe,
  825. .remove = i2c_pxa_remove,
  826. .driver = {
  827. .name = "pxa2xx-i2c",
  828. },
  829. };
  830. static int __init i2c_adap_pxa_init(void)
  831. {
  832. return platform_driver_register(&i2c_pxa_driver);
  833. }
  834. static void i2c_adap_pxa_exit(void)
  835. {
  836. return platform_driver_unregister(&i2c_pxa_driver);
  837. }
  838. module_init(i2c_adap_pxa_init);
  839. module_exit(i2c_adap_pxa_exit);