i2c-pxa.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 __exit i2c_pxa_remove(struct platform_device *dev)
  1010. {
  1011. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  1012. platform_set_drvdata(dev, NULL);
  1013. i2c_del_adapter(&i2c->adap);
  1014. if (!i2c->use_pio)
  1015. free_irq(i2c->irq, i2c);
  1016. clk_disable(i2c->clk);
  1017. clk_put(i2c->clk);
  1018. iounmap(i2c->reg_base);
  1019. release_mem_region(i2c->iobase, i2c->iosize);
  1020. kfree(i2c);
  1021. return 0;
  1022. }
  1023. #ifdef CONFIG_PM
  1024. static int i2c_pxa_suspend_noirq(struct device *dev)
  1025. {
  1026. struct platform_device *pdev = to_platform_device(dev);
  1027. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1028. clk_disable(i2c->clk);
  1029. return 0;
  1030. }
  1031. static int i2c_pxa_resume_noirq(struct device *dev)
  1032. {
  1033. struct platform_device *pdev = to_platform_device(dev);
  1034. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1035. clk_enable(i2c->clk);
  1036. i2c_pxa_reset(i2c);
  1037. return 0;
  1038. }
  1039. static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
  1040. .suspend_noirq = i2c_pxa_suspend_noirq,
  1041. .resume_noirq = i2c_pxa_resume_noirq,
  1042. };
  1043. #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
  1044. #else
  1045. #define I2C_PXA_DEV_PM_OPS NULL
  1046. #endif
  1047. static struct platform_driver i2c_pxa_driver = {
  1048. .probe = i2c_pxa_probe,
  1049. .remove = __exit_p(i2c_pxa_remove),
  1050. .driver = {
  1051. .name = "pxa2xx-i2c",
  1052. .owner = THIS_MODULE,
  1053. .pm = I2C_PXA_DEV_PM_OPS,
  1054. .of_match_table = i2c_pxa_dt_ids,
  1055. },
  1056. .id_table = i2c_pxa_id_table,
  1057. };
  1058. static int __init i2c_adap_pxa_init(void)
  1059. {
  1060. return platform_driver_register(&i2c_pxa_driver);
  1061. }
  1062. static void __exit i2c_adap_pxa_exit(void)
  1063. {
  1064. platform_driver_unregister(&i2c_pxa_driver);
  1065. }
  1066. MODULE_LICENSE("GPL");
  1067. MODULE_ALIAS("platform:pxa2xx-i2c");
  1068. subsys_initcall(i2c_adap_pxa_init);
  1069. module_exit(i2c_adap_pxa_exit);