i2c-pxa.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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 separated 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/init.h>
  26. #include <linux/time.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/errno.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/i2c-pxa.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_i2c.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/err.h>
  37. #include <linux/clk.h>
  38. #include <linux/slab.h>
  39. #include <linux/io.h>
  40. #include <linux/i2c/pxa-i2c.h>
  41. #include <asm/irq.h>
  42. struct pxa_reg_layout {
  43. u32 ibmr;
  44. u32 idbr;
  45. u32 icr;
  46. u32 isr;
  47. u32 isar;
  48. };
  49. enum pxa_i2c_types {
  50. REGS_PXA2XX,
  51. REGS_PXA3XX,
  52. REGS_CE4100,
  53. };
  54. /*
  55. * I2C registers definitions
  56. */
  57. static struct pxa_reg_layout pxa_reg_layout[] = {
  58. [REGS_PXA2XX] = {
  59. .ibmr = 0x00,
  60. .idbr = 0x08,
  61. .icr = 0x10,
  62. .isr = 0x18,
  63. .isar = 0x20,
  64. },
  65. [REGS_PXA3XX] = {
  66. .ibmr = 0x00,
  67. .idbr = 0x04,
  68. .icr = 0x08,
  69. .isr = 0x0c,
  70. .isar = 0x10,
  71. },
  72. [REGS_CE4100] = {
  73. .ibmr = 0x14,
  74. .idbr = 0x0c,
  75. .icr = 0x00,
  76. .isr = 0x04,
  77. /* no isar register */
  78. },
  79. };
  80. static const struct platform_device_id i2c_pxa_id_table[] = {
  81. { "pxa2xx-i2c", REGS_PXA2XX },
  82. { "pxa3xx-pwri2c", REGS_PXA3XX },
  83. { "ce4100-i2c", REGS_CE4100 },
  84. { },
  85. };
  86. MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);
  87. /*
  88. * I2C bit definitions
  89. */
  90. #define ICR_START (1 << 0) /* start bit */
  91. #define ICR_STOP (1 << 1) /* stop bit */
  92. #define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */
  93. #define ICR_TB (1 << 3) /* transfer byte bit */
  94. #define ICR_MA (1 << 4) /* master abort */
  95. #define ICR_SCLE (1 << 5) /* master clock enable */
  96. #define ICR_IUE (1 << 6) /* unit enable */
  97. #define ICR_GCD (1 << 7) /* general call disable */
  98. #define ICR_ITEIE (1 << 8) /* enable tx interrupts */
  99. #define ICR_IRFIE (1 << 9) /* enable rx interrupts */
  100. #define ICR_BEIE (1 << 10) /* enable bus error ints */
  101. #define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */
  102. #define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */
  103. #define ICR_SADIE (1 << 13) /* slave address detected int enable */
  104. #define ICR_UR (1 << 14) /* unit reset */
  105. #define ICR_FM (1 << 15) /* fast mode */
  106. #define ISR_RWM (1 << 0) /* read/write mode */
  107. #define ISR_ACKNAK (1 << 1) /* ack/nak status */
  108. #define ISR_UB (1 << 2) /* unit busy */
  109. #define ISR_IBB (1 << 3) /* bus busy */
  110. #define ISR_SSD (1 << 4) /* slave stop detected */
  111. #define ISR_ALD (1 << 5) /* arbitration loss detected */
  112. #define ISR_ITE (1 << 6) /* tx buffer empty */
  113. #define ISR_IRF (1 << 7) /* rx buffer full */
  114. #define ISR_GCAD (1 << 8) /* general call address detected */
  115. #define ISR_SAD (1 << 9) /* slave address detected */
  116. #define ISR_BED (1 << 10) /* bus error no ACK/NAK */
  117. struct pxa_i2c {
  118. spinlock_t lock;
  119. wait_queue_head_t wait;
  120. struct i2c_msg *msg;
  121. unsigned int msg_num;
  122. unsigned int msg_idx;
  123. unsigned int msg_ptr;
  124. unsigned int slave_addr;
  125. struct i2c_adapter adap;
  126. struct clk *clk;
  127. #ifdef CONFIG_I2C_PXA_SLAVE
  128. struct i2c_slave_client *slave;
  129. #endif
  130. unsigned int irqlogidx;
  131. u32 isrlog[32];
  132. u32 icrlog[32];
  133. void __iomem *reg_base;
  134. void __iomem *reg_ibmr;
  135. void __iomem *reg_idbr;
  136. void __iomem *reg_icr;
  137. void __iomem *reg_isr;
  138. void __iomem *reg_isar;
  139. unsigned long iobase;
  140. unsigned long iosize;
  141. int irq;
  142. unsigned int use_pio :1;
  143. unsigned int fast_mode :1;
  144. };
  145. #define _IBMR(i2c) ((i2c)->reg_ibmr)
  146. #define _IDBR(i2c) ((i2c)->reg_idbr)
  147. #define _ICR(i2c) ((i2c)->reg_icr)
  148. #define _ISR(i2c) ((i2c)->reg_isr)
  149. #define _ISAR(i2c) ((i2c)->reg_isar)
  150. /*
  151. * I2C Slave mode address
  152. */
  153. #define I2C_PXA_SLAVE_ADDR 0x1
  154. #ifdef DEBUG
  155. struct bits {
  156. u32 mask;
  157. const char *set;
  158. const char *unset;
  159. };
  160. #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
  161. static inline void
  162. decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
  163. {
  164. printk("%s %08x: ", prefix, val);
  165. while (num--) {
  166. const char *str = val & bits->mask ? bits->set : bits->unset;
  167. if (str)
  168. printk("%s ", str);
  169. bits++;
  170. }
  171. }
  172. static const struct bits isr_bits[] = {
  173. PXA_BIT(ISR_RWM, "RX", "TX"),
  174. PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
  175. PXA_BIT(ISR_UB, "Bsy", "Rdy"),
  176. PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
  177. PXA_BIT(ISR_SSD, "SlaveStop", NULL),
  178. PXA_BIT(ISR_ALD, "ALD", NULL),
  179. PXA_BIT(ISR_ITE, "TxEmpty", NULL),
  180. PXA_BIT(ISR_IRF, "RxFull", NULL),
  181. PXA_BIT(ISR_GCAD, "GenCall", NULL),
  182. PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
  183. PXA_BIT(ISR_BED, "BusErr", NULL),
  184. };
  185. static void decode_ISR(unsigned int val)
  186. {
  187. decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
  188. printk("\n");
  189. }
  190. static const struct bits icr_bits[] = {
  191. PXA_BIT(ICR_START, "START", NULL),
  192. PXA_BIT(ICR_STOP, "STOP", NULL),
  193. PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
  194. PXA_BIT(ICR_TB, "TB", NULL),
  195. PXA_BIT(ICR_MA, "MA", NULL),
  196. PXA_BIT(ICR_SCLE, "SCLE", "scle"),
  197. PXA_BIT(ICR_IUE, "IUE", "iue"),
  198. PXA_BIT(ICR_GCD, "GCD", NULL),
  199. PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
  200. PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
  201. PXA_BIT(ICR_BEIE, "BEIE", NULL),
  202. PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
  203. PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
  204. PXA_BIT(ICR_SADIE, "SADIE", NULL),
  205. PXA_BIT(ICR_UR, "UR", "ur"),
  206. };
  207. #ifdef CONFIG_I2C_PXA_SLAVE
  208. static void decode_ICR(unsigned int val)
  209. {
  210. decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
  211. printk("\n");
  212. }
  213. #endif
  214. static unsigned int i2c_debug = DEBUG;
  215. static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
  216. {
  217. dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
  218. readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  219. }
  220. #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
  221. static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
  222. {
  223. unsigned int i;
  224. printk(KERN_ERR "i2c: error: %s\n", why);
  225. printk(KERN_ERR "i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
  226. i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
  227. printk(KERN_ERR "i2c: ICR: %08x ISR: %08x\n",
  228. readl(_ICR(i2c)), readl(_ISR(i2c)));
  229. printk(KERN_DEBUG "i2c: log: ");
  230. for (i = 0; i < i2c->irqlogidx; i++)
  231. printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
  232. printk("\n");
  233. }
  234. #else /* ifdef DEBUG */
  235. #define i2c_debug 0
  236. #define show_state(i2c) do { } while (0)
  237. #define decode_ISR(val) do { } while (0)
  238. #define decode_ICR(val) do { } while (0)
  239. #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
  240. #endif /* ifdef DEBUG / else */
  241. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
  242. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
  243. static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
  244. {
  245. return !(readl(_ICR(i2c)) & ICR_SCLE);
  246. }
  247. static void i2c_pxa_abort(struct pxa_i2c *i2c)
  248. {
  249. int i = 250;
  250. if (i2c_pxa_is_slavemode(i2c)) {
  251. dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
  252. return;
  253. }
  254. while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
  255. unsigned long icr = readl(_ICR(i2c));
  256. icr &= ~ICR_START;
  257. icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
  258. writel(icr, _ICR(i2c));
  259. show_state(i2c);
  260. mdelay(1);
  261. i --;
  262. }
  263. writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
  264. _ICR(i2c));
  265. }
  266. static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
  267. {
  268. int timeout = DEF_TIMEOUT;
  269. while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
  270. if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
  271. timeout += 4;
  272. msleep(2);
  273. show_state(i2c);
  274. }
  275. if (timeout < 0)
  276. show_state(i2c);
  277. return timeout < 0 ? I2C_RETRY : 0;
  278. }
  279. static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
  280. {
  281. unsigned long timeout = jiffies + HZ*4;
  282. while (time_before(jiffies, timeout)) {
  283. if (i2c_debug > 1)
  284. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  285. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  286. if (readl(_ISR(i2c)) & ISR_SAD) {
  287. if (i2c_debug > 0)
  288. dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
  289. goto out;
  290. }
  291. /* wait for unit and bus being not busy, and we also do a
  292. * quick check of the i2c lines themselves to ensure they've
  293. * gone high...
  294. */
  295. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
  296. if (i2c_debug > 0)
  297. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  298. return 1;
  299. }
  300. msleep(1);
  301. }
  302. if (i2c_debug > 0)
  303. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  304. out:
  305. return 0;
  306. }
  307. static int i2c_pxa_set_master(struct pxa_i2c *i2c)
  308. {
  309. if (i2c_debug)
  310. dev_dbg(&i2c->adap.dev, "setting to bus master\n");
  311. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
  312. dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
  313. if (!i2c_pxa_wait_master(i2c)) {
  314. dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
  315. return I2C_RETRY;
  316. }
  317. }
  318. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  319. return 0;
  320. }
  321. #ifdef CONFIG_I2C_PXA_SLAVE
  322. static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
  323. {
  324. unsigned long timeout = jiffies + HZ*1;
  325. /* wait for stop */
  326. show_state(i2c);
  327. while (time_before(jiffies, timeout)) {
  328. if (i2c_debug > 1)
  329. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  330. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  331. if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
  332. (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
  333. (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
  334. if (i2c_debug > 1)
  335. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  336. return 1;
  337. }
  338. msleep(1);
  339. }
  340. if (i2c_debug > 0)
  341. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  342. return 0;
  343. }
  344. /*
  345. * clear the hold on the bus, and take of anything else
  346. * that has been configured
  347. */
  348. static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
  349. {
  350. show_state(i2c);
  351. if (errcode < 0) {
  352. udelay(100); /* simple delay */
  353. } else {
  354. /* we need to wait for the stop condition to end */
  355. /* if we where in stop, then clear... */
  356. if (readl(_ICR(i2c)) & ICR_STOP) {
  357. udelay(100);
  358. writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
  359. }
  360. if (!i2c_pxa_wait_slave(i2c)) {
  361. dev_err(&i2c->adap.dev, "%s: wait timedout\n",
  362. __func__);
  363. return;
  364. }
  365. }
  366. writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
  367. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  368. if (i2c_debug) {
  369. dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
  370. decode_ICR(readl(_ICR(i2c)));
  371. }
  372. }
  373. #else
  374. #define i2c_pxa_set_slave(i2c, err) do { } while (0)
  375. #endif
  376. static void i2c_pxa_reset(struct pxa_i2c *i2c)
  377. {
  378. pr_debug("Resetting I2C Controller Unit\n");
  379. /* abort any transfer currently under way */
  380. i2c_pxa_abort(i2c);
  381. /* reset according to 9.8 */
  382. writel(ICR_UR, _ICR(i2c));
  383. writel(I2C_ISR_INIT, _ISR(i2c));
  384. writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
  385. if (i2c->reg_isar)
  386. writel(i2c->slave_addr, _ISAR(i2c));
  387. /* set control register values */
  388. writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c));
  389. #ifdef CONFIG_I2C_PXA_SLAVE
  390. dev_info(&i2c->adap.dev, "Enabling slave mode\n");
  391. writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
  392. #endif
  393. i2c_pxa_set_slave(i2c, 0);
  394. /* enable unit */
  395. writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
  396. udelay(100);
  397. }
  398. #ifdef CONFIG_I2C_PXA_SLAVE
  399. /*
  400. * PXA I2C Slave mode
  401. */
  402. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  403. {
  404. if (isr & ISR_BED) {
  405. /* what should we do here? */
  406. } else {
  407. int ret = 0;
  408. if (i2c->slave != NULL)
  409. ret = i2c->slave->read(i2c->slave->data);
  410. writel(ret, _IDBR(i2c));
  411. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
  412. }
  413. }
  414. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  415. {
  416. unsigned int byte = readl(_IDBR(i2c));
  417. if (i2c->slave != NULL)
  418. i2c->slave->write(i2c->slave->data, byte);
  419. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  420. }
  421. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  422. {
  423. int timeout;
  424. if (i2c_debug > 0)
  425. dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
  426. (isr & ISR_RWM) ? 'r' : 't');
  427. if (i2c->slave != NULL)
  428. i2c->slave->event(i2c->slave->data,
  429. (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
  430. /*
  431. * slave could interrupt in the middle of us generating a
  432. * start condition... if this happens, we'd better back off
  433. * and stop holding the poor thing up
  434. */
  435. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  436. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  437. timeout = 0x10000;
  438. while (1) {
  439. if ((readl(_IBMR(i2c)) & 2) == 2)
  440. break;
  441. timeout--;
  442. if (timeout <= 0) {
  443. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  444. break;
  445. }
  446. }
  447. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  448. }
  449. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  450. {
  451. if (i2c_debug > 2)
  452. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
  453. if (i2c->slave != NULL)
  454. i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
  455. if (i2c_debug > 2)
  456. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
  457. /*
  458. * If we have a master-mode message waiting,
  459. * kick it off now that the slave has completed.
  460. */
  461. if (i2c->msg)
  462. i2c_pxa_master_complete(i2c, I2C_RETRY);
  463. }
  464. #else
  465. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  466. {
  467. if (isr & ISR_BED) {
  468. /* what should we do here? */
  469. } else {
  470. writel(0, _IDBR(i2c));
  471. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  472. }
  473. }
  474. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  475. {
  476. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  477. }
  478. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  479. {
  480. int timeout;
  481. /*
  482. * slave could interrupt in the middle of us generating a
  483. * start condition... if this happens, we'd better back off
  484. * and stop holding the poor thing up
  485. */
  486. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  487. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  488. timeout = 0x10000;
  489. while (1) {
  490. if ((readl(_IBMR(i2c)) & 2) == 2)
  491. break;
  492. timeout--;
  493. if (timeout <= 0) {
  494. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  495. break;
  496. }
  497. }
  498. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  499. }
  500. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  501. {
  502. if (i2c->msg)
  503. i2c_pxa_master_complete(i2c, I2C_RETRY);
  504. }
  505. #endif
  506. /*
  507. * PXA I2C Master mode
  508. */
  509. static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
  510. {
  511. unsigned int addr = (msg->addr & 0x7f) << 1;
  512. if (msg->flags & I2C_M_RD)
  513. addr |= 1;
  514. return addr;
  515. }
  516. static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
  517. {
  518. u32 icr;
  519. /*
  520. * Step 1: target slave address into IDBR
  521. */
  522. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  523. /*
  524. * Step 2: initiate the write.
  525. */
  526. icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
  527. writel(icr | ICR_START | ICR_TB, _ICR(i2c));
  528. }
  529. static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
  530. {
  531. u32 icr;
  532. /*
  533. * Clear the STOP and ACK flags
  534. */
  535. icr = readl(_ICR(i2c));
  536. icr &= ~(ICR_STOP | ICR_ACKNAK);
  537. writel(icr, _ICR(i2c));
  538. }
  539. static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
  540. {
  541. /* make timeout the same as for interrupt based functions */
  542. long timeout = 2 * DEF_TIMEOUT;
  543. /*
  544. * Wait for the bus to become free.
  545. */
  546. while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
  547. udelay(1000);
  548. show_state(i2c);
  549. }
  550. if (timeout < 0) {
  551. show_state(i2c);
  552. dev_err(&i2c->adap.dev,
  553. "i2c_pxa: timeout waiting for bus free\n");
  554. return I2C_RETRY;
  555. }
  556. /*
  557. * Set master mode.
  558. */
  559. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  560. return 0;
  561. }
  562. static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
  563. struct i2c_msg *msg, int num)
  564. {
  565. unsigned long timeout = 500000; /* 5 seconds */
  566. int ret = 0;
  567. ret = i2c_pxa_pio_set_master(i2c);
  568. if (ret)
  569. goto out;
  570. i2c->msg = msg;
  571. i2c->msg_num = num;
  572. i2c->msg_idx = 0;
  573. i2c->msg_ptr = 0;
  574. i2c->irqlogidx = 0;
  575. i2c_pxa_start_message(i2c);
  576. while (i2c->msg_num > 0 && --timeout) {
  577. i2c_pxa_handler(0, i2c);
  578. udelay(10);
  579. }
  580. i2c_pxa_stop_message(i2c);
  581. /*
  582. * We place the return code in i2c->msg_idx.
  583. */
  584. ret = i2c->msg_idx;
  585. out:
  586. if (timeout == 0)
  587. i2c_pxa_scream_blue_murder(i2c, "timeout");
  588. return ret;
  589. }
  590. /*
  591. * We are protected by the adapter bus mutex.
  592. */
  593. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  594. {
  595. long timeout;
  596. int ret;
  597. /*
  598. * Wait for the bus to become free.
  599. */
  600. ret = i2c_pxa_wait_bus_not_busy(i2c);
  601. if (ret) {
  602. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  603. goto out;
  604. }
  605. /*
  606. * Set master mode.
  607. */
  608. ret = i2c_pxa_set_master(i2c);
  609. if (ret) {
  610. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  611. goto out;
  612. }
  613. spin_lock_irq(&i2c->lock);
  614. i2c->msg = msg;
  615. i2c->msg_num = num;
  616. i2c->msg_idx = 0;
  617. i2c->msg_ptr = 0;
  618. i2c->irqlogidx = 0;
  619. i2c_pxa_start_message(i2c);
  620. spin_unlock_irq(&i2c->lock);
  621. /*
  622. * The rest of the processing occurs in the interrupt handler.
  623. */
  624. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  625. i2c_pxa_stop_message(i2c);
  626. /*
  627. * We place the return code in i2c->msg_idx.
  628. */
  629. ret = i2c->msg_idx;
  630. if (!timeout && i2c->msg_num) {
  631. i2c_pxa_scream_blue_murder(i2c, "timeout");
  632. ret = I2C_RETRY;
  633. }
  634. out:
  635. return ret;
  636. }
  637. static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
  638. struct i2c_msg msgs[], int num)
  639. {
  640. struct pxa_i2c *i2c = adap->algo_data;
  641. int ret, i;
  642. /* If the I2C controller is disabled we need to reset it
  643. (probably due to a suspend/resume destroying state). We do
  644. this here as we can then avoid worrying about resuming the
  645. controller before its users. */
  646. if (!(readl(_ICR(i2c)) & ICR_IUE))
  647. i2c_pxa_reset(i2c);
  648. for (i = adap->retries; i >= 0; i--) {
  649. ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
  650. if (ret != I2C_RETRY)
  651. goto out;
  652. if (i2c_debug)
  653. dev_dbg(&adap->dev, "Retrying transmission\n");
  654. udelay(100);
  655. }
  656. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  657. ret = -EREMOTEIO;
  658. out:
  659. i2c_pxa_set_slave(i2c, ret);
  660. return ret;
  661. }
  662. /*
  663. * i2c_pxa_master_complete - complete the message and wake up.
  664. */
  665. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  666. {
  667. i2c->msg_ptr = 0;
  668. i2c->msg = NULL;
  669. i2c->msg_idx ++;
  670. i2c->msg_num = 0;
  671. if (ret)
  672. i2c->msg_idx = ret;
  673. if (!i2c->use_pio)
  674. wake_up(&i2c->wait);
  675. }
  676. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  677. {
  678. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  679. again:
  680. /*
  681. * If ISR_ALD is set, we lost arbitration.
  682. */
  683. if (isr & ISR_ALD) {
  684. /*
  685. * Do we need to do anything here? The PXA docs
  686. * are vague about what happens.
  687. */
  688. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  689. /*
  690. * We ignore this error. We seem to see spurious ALDs
  691. * for seemingly no reason. If we handle them as I think
  692. * they should, we end up causing an I2C error, which
  693. * is painful for some systems.
  694. */
  695. return; /* ignore */
  696. }
  697. if (isr & ISR_BED) {
  698. int ret = BUS_ERROR;
  699. /*
  700. * I2C bus error - either the device NAK'd us, or
  701. * something more serious happened. If we were NAK'd
  702. * on the initial address phase, we can retry.
  703. */
  704. if (isr & ISR_ACKNAK) {
  705. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  706. ret = I2C_RETRY;
  707. else
  708. ret = XFER_NAKED;
  709. }
  710. i2c_pxa_master_complete(i2c, ret);
  711. } else if (isr & ISR_RWM) {
  712. /*
  713. * Read mode. We have just sent the address byte, and
  714. * now we must initiate the transfer.
  715. */
  716. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  717. i2c->msg_idx == i2c->msg_num - 1)
  718. icr |= ICR_STOP | ICR_ACKNAK;
  719. icr |= ICR_ALDIE | ICR_TB;
  720. } else if (i2c->msg_ptr < i2c->msg->len) {
  721. /*
  722. * Write mode. Write the next data byte.
  723. */
  724. writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
  725. icr |= ICR_ALDIE | ICR_TB;
  726. /*
  727. * If this is the last byte of the last message, send
  728. * a STOP.
  729. */
  730. if (i2c->msg_ptr == i2c->msg->len &&
  731. i2c->msg_idx == i2c->msg_num - 1)
  732. icr |= ICR_STOP;
  733. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  734. /*
  735. * Next segment of the message.
  736. */
  737. i2c->msg_ptr = 0;
  738. i2c->msg_idx ++;
  739. i2c->msg++;
  740. /*
  741. * If we aren't doing a repeated start and address,
  742. * go back and try to send the next byte. Note that
  743. * we do not support switching the R/W direction here.
  744. */
  745. if (i2c->msg->flags & I2C_M_NOSTART)
  746. goto again;
  747. /*
  748. * Write the next address.
  749. */
  750. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  751. /*
  752. * And trigger a repeated start, and send the byte.
  753. */
  754. icr &= ~ICR_ALDIE;
  755. icr |= ICR_START | ICR_TB;
  756. } else {
  757. if (i2c->msg->len == 0) {
  758. /*
  759. * Device probes have a message length of zero
  760. * and need the bus to be reset before it can
  761. * be used again.
  762. */
  763. i2c_pxa_reset(i2c);
  764. }
  765. i2c_pxa_master_complete(i2c, 0);
  766. }
  767. i2c->icrlog[i2c->irqlogidx-1] = icr;
  768. writel(icr, _ICR(i2c));
  769. show_state(i2c);
  770. }
  771. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  772. {
  773. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  774. /*
  775. * Read the byte.
  776. */
  777. i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
  778. if (i2c->msg_ptr < i2c->msg->len) {
  779. /*
  780. * If this is the last byte of the last
  781. * message, send a STOP.
  782. */
  783. if (i2c->msg_ptr == i2c->msg->len - 1)
  784. icr |= ICR_STOP | ICR_ACKNAK;
  785. icr |= ICR_ALDIE | ICR_TB;
  786. } else {
  787. i2c_pxa_master_complete(i2c, 0);
  788. }
  789. i2c->icrlog[i2c->irqlogidx-1] = icr;
  790. writel(icr, _ICR(i2c));
  791. }
  792. #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
  793. ISR_SAD | ISR_BED)
  794. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  795. {
  796. struct pxa_i2c *i2c = dev_id;
  797. u32 isr = readl(_ISR(i2c));
  798. if (!(isr & VALID_INT_SOURCE))
  799. return IRQ_NONE;
  800. if (i2c_debug > 2 && 0) {
  801. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  802. __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
  803. decode_ISR(isr);
  804. }
  805. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  806. i2c->isrlog[i2c->irqlogidx++] = isr;
  807. show_state(i2c);
  808. /*
  809. * Always clear all pending IRQs.
  810. */
  811. writel(isr & VALID_INT_SOURCE, _ISR(i2c));
  812. if (isr & ISR_SAD)
  813. i2c_pxa_slave_start(i2c, isr);
  814. if (isr & ISR_SSD)
  815. i2c_pxa_slave_stop(i2c);
  816. if (i2c_pxa_is_slavemode(i2c)) {
  817. if (isr & ISR_ITE)
  818. i2c_pxa_slave_txempty(i2c, isr);
  819. if (isr & ISR_IRF)
  820. i2c_pxa_slave_rxfull(i2c, isr);
  821. } else if (i2c->msg) {
  822. if (isr & ISR_ITE)
  823. i2c_pxa_irq_txempty(i2c, isr);
  824. if (isr & ISR_IRF)
  825. i2c_pxa_irq_rxfull(i2c, isr);
  826. } else {
  827. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  828. }
  829. return IRQ_HANDLED;
  830. }
  831. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  832. {
  833. struct pxa_i2c *i2c = adap->algo_data;
  834. int ret, i;
  835. for (i = adap->retries; i >= 0; i--) {
  836. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  837. if (ret != I2C_RETRY)
  838. goto out;
  839. if (i2c_debug)
  840. dev_dbg(&adap->dev, "Retrying transmission\n");
  841. udelay(100);
  842. }
  843. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  844. ret = -EREMOTEIO;
  845. out:
  846. i2c_pxa_set_slave(i2c, ret);
  847. return ret;
  848. }
  849. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  850. {
  851. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  852. }
  853. static const struct i2c_algorithm i2c_pxa_algorithm = {
  854. .master_xfer = i2c_pxa_xfer,
  855. .functionality = i2c_pxa_functionality,
  856. };
  857. static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
  858. .master_xfer = i2c_pxa_pio_xfer,
  859. .functionality = i2c_pxa_functionality,
  860. };
  861. static struct of_device_id i2c_pxa_dt_ids[] = {
  862. { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
  863. { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
  864. { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA2XX },
  865. {}
  866. };
  867. MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
  868. static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
  869. enum pxa_i2c_types *i2c_types)
  870. {
  871. struct device_node *np = pdev->dev.of_node;
  872. const struct of_device_id *of_id =
  873. of_match_device(i2c_pxa_dt_ids, &pdev->dev);
  874. int ret;
  875. if (!of_id)
  876. return 1;
  877. ret = of_alias_get_id(np, "i2c");
  878. if (ret < 0) {
  879. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  880. return ret;
  881. }
  882. pdev->id = ret;
  883. if (of_get_property(np, "mrvl,i2c-polling", NULL))
  884. i2c->use_pio = 1;
  885. if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
  886. i2c->fast_mode = 1;
  887. *i2c_types = (u32)(of_id->data);
  888. return 0;
  889. }
  890. static int i2c_pxa_probe_pdata(struct platform_device *pdev,
  891. struct pxa_i2c *i2c,
  892. enum pxa_i2c_types *i2c_types)
  893. {
  894. struct i2c_pxa_platform_data *plat = pdev->dev.platform_data;
  895. const struct platform_device_id *id = platform_get_device_id(pdev);
  896. *i2c_types = id->driver_data;
  897. if (plat) {
  898. i2c->use_pio = plat->use_pio;
  899. i2c->fast_mode = plat->fast_mode;
  900. }
  901. return 0;
  902. }
  903. static int i2c_pxa_probe(struct platform_device *dev)
  904. {
  905. struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
  906. enum pxa_i2c_types i2c_type;
  907. struct pxa_i2c *i2c;
  908. struct resource *res = NULL;
  909. int ret, irq;
  910. i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
  911. if (!i2c) {
  912. ret = -ENOMEM;
  913. goto emalloc;
  914. }
  915. ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
  916. if (ret > 0)
  917. ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
  918. if (ret < 0)
  919. goto eclk;
  920. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  921. irq = platform_get_irq(dev, 0);
  922. if (res == NULL || irq < 0) {
  923. ret = -ENODEV;
  924. goto eclk;
  925. }
  926. if (!request_mem_region(res->start, resource_size(res), res->name)) {
  927. ret = -ENOMEM;
  928. goto eclk;
  929. }
  930. i2c->adap.owner = THIS_MODULE;
  931. i2c->adap.retries = 5;
  932. spin_lock_init(&i2c->lock);
  933. init_waitqueue_head(&i2c->wait);
  934. i2c->adap.nr = dev->id;
  935. snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
  936. i2c->adap.nr);
  937. i2c->clk = clk_get(&dev->dev, NULL);
  938. if (IS_ERR(i2c->clk)) {
  939. ret = PTR_ERR(i2c->clk);
  940. goto eclk;
  941. }
  942. i2c->reg_base = ioremap(res->start, resource_size(res));
  943. if (!i2c->reg_base) {
  944. ret = -EIO;
  945. goto eremap;
  946. }
  947. i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
  948. i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
  949. i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
  950. i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
  951. if (i2c_type != REGS_CE4100)
  952. i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
  953. i2c->iobase = res->start;
  954. i2c->iosize = resource_size(res);
  955. i2c->irq = irq;
  956. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  957. if (plat) {
  958. #ifdef CONFIG_I2C_PXA_SLAVE
  959. i2c->slave_addr = plat->slave_addr;
  960. i2c->slave = plat->slave;
  961. #endif
  962. i2c->adap.class = plat->class;
  963. }
  964. clk_enable(i2c->clk);
  965. if (i2c->use_pio) {
  966. i2c->adap.algo = &i2c_pxa_pio_algorithm;
  967. } else {
  968. i2c->adap.algo = &i2c_pxa_algorithm;
  969. ret = request_irq(irq, i2c_pxa_handler, IRQF_SHARED,
  970. i2c->adap.name, i2c);
  971. if (ret)
  972. goto ereqirq;
  973. }
  974. i2c_pxa_reset(i2c);
  975. i2c->adap.algo_data = i2c;
  976. i2c->adap.dev.parent = &dev->dev;
  977. #ifdef CONFIG_OF
  978. i2c->adap.dev.of_node = dev->dev.of_node;
  979. #endif
  980. ret = i2c_add_numbered_adapter(&i2c->adap);
  981. if (ret < 0) {
  982. printk(KERN_INFO "I2C: Failed to add bus\n");
  983. goto eadapt;
  984. }
  985. of_i2c_register_devices(&i2c->adap);
  986. platform_set_drvdata(dev, i2c);
  987. #ifdef CONFIG_I2C_PXA_SLAVE
  988. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  989. dev_name(&i2c->adap.dev), i2c->slave_addr);
  990. #else
  991. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  992. dev_name(&i2c->adap.dev));
  993. #endif
  994. return 0;
  995. eadapt:
  996. if (!i2c->use_pio)
  997. free_irq(irq, i2c);
  998. ereqirq:
  999. clk_disable(i2c->clk);
  1000. iounmap(i2c->reg_base);
  1001. eremap:
  1002. clk_put(i2c->clk);
  1003. eclk:
  1004. kfree(i2c);
  1005. emalloc:
  1006. release_mem_region(res->start, resource_size(res));
  1007. return ret;
  1008. }
  1009. static int i2c_pxa_remove(struct platform_device *dev)
  1010. {
  1011. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  1012. i2c_del_adapter(&i2c->adap);
  1013. if (!i2c->use_pio)
  1014. free_irq(i2c->irq, i2c);
  1015. clk_disable(i2c->clk);
  1016. clk_put(i2c->clk);
  1017. iounmap(i2c->reg_base);
  1018. release_mem_region(i2c->iobase, i2c->iosize);
  1019. kfree(i2c);
  1020. return 0;
  1021. }
  1022. #ifdef CONFIG_PM
  1023. static int i2c_pxa_suspend_noirq(struct device *dev)
  1024. {
  1025. struct platform_device *pdev = to_platform_device(dev);
  1026. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1027. clk_disable(i2c->clk);
  1028. return 0;
  1029. }
  1030. static int i2c_pxa_resume_noirq(struct device *dev)
  1031. {
  1032. struct platform_device *pdev = to_platform_device(dev);
  1033. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1034. clk_enable(i2c->clk);
  1035. i2c_pxa_reset(i2c);
  1036. return 0;
  1037. }
  1038. static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
  1039. .suspend_noirq = i2c_pxa_suspend_noirq,
  1040. .resume_noirq = i2c_pxa_resume_noirq,
  1041. };
  1042. #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
  1043. #else
  1044. #define I2C_PXA_DEV_PM_OPS NULL
  1045. #endif
  1046. static struct platform_driver i2c_pxa_driver = {
  1047. .probe = i2c_pxa_probe,
  1048. .remove = i2c_pxa_remove,
  1049. .driver = {
  1050. .name = "pxa2xx-i2c",
  1051. .owner = THIS_MODULE,
  1052. .pm = I2C_PXA_DEV_PM_OPS,
  1053. .of_match_table = i2c_pxa_dt_ids,
  1054. },
  1055. .id_table = i2c_pxa_id_table,
  1056. };
  1057. static int __init i2c_adap_pxa_init(void)
  1058. {
  1059. return platform_driver_register(&i2c_pxa_driver);
  1060. }
  1061. static void __exit i2c_adap_pxa_exit(void)
  1062. {
  1063. platform_driver_unregister(&i2c_pxa_driver);
  1064. }
  1065. MODULE_LICENSE("GPL");
  1066. MODULE_ALIAS("platform:pxa2xx-i2c");
  1067. subsys_initcall(i2c_adap_pxa_init);
  1068. module_exit(i2c_adap_pxa_exit);