i2c-pxa.c 22 KB

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