i2c-pxa.c 30 KB

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