i2c-pxa.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. if (!of_id)
  875. return 1;
  876. /* For device tree we always use the dynamic or alias-assigned ID */
  877. i2c->adap.nr = -1;
  878. if (of_get_property(np, "mrvl,i2c-polling", NULL))
  879. i2c->use_pio = 1;
  880. if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
  881. i2c->fast_mode = 1;
  882. *i2c_types = (u32)(of_id->data);
  883. return 0;
  884. }
  885. static int i2c_pxa_probe_pdata(struct platform_device *pdev,
  886. struct pxa_i2c *i2c,
  887. enum pxa_i2c_types *i2c_types)
  888. {
  889. struct i2c_pxa_platform_data *plat = pdev->dev.platform_data;
  890. const struct platform_device_id *id = platform_get_device_id(pdev);
  891. *i2c_types = id->driver_data;
  892. if (plat) {
  893. i2c->use_pio = plat->use_pio;
  894. i2c->fast_mode = plat->fast_mode;
  895. }
  896. return 0;
  897. }
  898. static int i2c_pxa_probe(struct platform_device *dev)
  899. {
  900. struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
  901. enum pxa_i2c_types i2c_type;
  902. struct pxa_i2c *i2c;
  903. struct resource *res = NULL;
  904. int ret, irq;
  905. i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
  906. if (!i2c) {
  907. ret = -ENOMEM;
  908. goto emalloc;
  909. }
  910. /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
  911. i2c->adap.nr = dev->id;
  912. ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
  913. if (ret > 0)
  914. ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
  915. if (ret < 0)
  916. goto eclk;
  917. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  918. irq = platform_get_irq(dev, 0);
  919. if (res == NULL || irq < 0) {
  920. ret = -ENODEV;
  921. goto eclk;
  922. }
  923. if (!request_mem_region(res->start, resource_size(res), res->name)) {
  924. ret = -ENOMEM;
  925. goto eclk;
  926. }
  927. i2c->adap.owner = THIS_MODULE;
  928. i2c->adap.retries = 5;
  929. spin_lock_init(&i2c->lock);
  930. init_waitqueue_head(&i2c->wait);
  931. strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
  932. i2c->clk = clk_get(&dev->dev, NULL);
  933. if (IS_ERR(i2c->clk)) {
  934. ret = PTR_ERR(i2c->clk);
  935. goto eclk;
  936. }
  937. i2c->reg_base = ioremap(res->start, resource_size(res));
  938. if (!i2c->reg_base) {
  939. ret = -EIO;
  940. goto eremap;
  941. }
  942. i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
  943. i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
  944. i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
  945. i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
  946. if (i2c_type != REGS_CE4100)
  947. i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
  948. i2c->iobase = res->start;
  949. i2c->iosize = resource_size(res);
  950. i2c->irq = irq;
  951. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  952. if (plat) {
  953. #ifdef CONFIG_I2C_PXA_SLAVE
  954. i2c->slave_addr = plat->slave_addr;
  955. i2c->slave = plat->slave;
  956. #endif
  957. i2c->adap.class = plat->class;
  958. }
  959. clk_enable(i2c->clk);
  960. if (i2c->use_pio) {
  961. i2c->adap.algo = &i2c_pxa_pio_algorithm;
  962. } else {
  963. i2c->adap.algo = &i2c_pxa_algorithm;
  964. ret = request_irq(irq, i2c_pxa_handler, IRQF_SHARED,
  965. dev_name(&dev->dev), i2c);
  966. if (ret)
  967. goto ereqirq;
  968. }
  969. i2c_pxa_reset(i2c);
  970. i2c->adap.algo_data = i2c;
  971. i2c->adap.dev.parent = &dev->dev;
  972. #ifdef CONFIG_OF
  973. i2c->adap.dev.of_node = dev->dev.of_node;
  974. #endif
  975. ret = i2c_add_numbered_adapter(&i2c->adap);
  976. if (ret < 0) {
  977. printk(KERN_INFO "I2C: Failed to add bus\n");
  978. goto eadapt;
  979. }
  980. of_i2c_register_devices(&i2c->adap);
  981. platform_set_drvdata(dev, i2c);
  982. #ifdef CONFIG_I2C_PXA_SLAVE
  983. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  984. dev_name(&i2c->adap.dev), i2c->slave_addr);
  985. #else
  986. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  987. dev_name(&i2c->adap.dev));
  988. #endif
  989. return 0;
  990. eadapt:
  991. if (!i2c->use_pio)
  992. free_irq(irq, i2c);
  993. ereqirq:
  994. clk_disable(i2c->clk);
  995. iounmap(i2c->reg_base);
  996. eremap:
  997. clk_put(i2c->clk);
  998. eclk:
  999. kfree(i2c);
  1000. emalloc:
  1001. release_mem_region(res->start, resource_size(res));
  1002. return ret;
  1003. }
  1004. static int i2c_pxa_remove(struct platform_device *dev)
  1005. {
  1006. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  1007. i2c_del_adapter(&i2c->adap);
  1008. if (!i2c->use_pio)
  1009. free_irq(i2c->irq, i2c);
  1010. clk_disable(i2c->clk);
  1011. clk_put(i2c->clk);
  1012. iounmap(i2c->reg_base);
  1013. release_mem_region(i2c->iobase, i2c->iosize);
  1014. kfree(i2c);
  1015. return 0;
  1016. }
  1017. #ifdef CONFIG_PM
  1018. static int i2c_pxa_suspend_noirq(struct device *dev)
  1019. {
  1020. struct platform_device *pdev = to_platform_device(dev);
  1021. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1022. clk_disable(i2c->clk);
  1023. return 0;
  1024. }
  1025. static int i2c_pxa_resume_noirq(struct device *dev)
  1026. {
  1027. struct platform_device *pdev = to_platform_device(dev);
  1028. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1029. clk_enable(i2c->clk);
  1030. i2c_pxa_reset(i2c);
  1031. return 0;
  1032. }
  1033. static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
  1034. .suspend_noirq = i2c_pxa_suspend_noirq,
  1035. .resume_noirq = i2c_pxa_resume_noirq,
  1036. };
  1037. #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
  1038. #else
  1039. #define I2C_PXA_DEV_PM_OPS NULL
  1040. #endif
  1041. static struct platform_driver i2c_pxa_driver = {
  1042. .probe = i2c_pxa_probe,
  1043. .remove = i2c_pxa_remove,
  1044. .driver = {
  1045. .name = "pxa2xx-i2c",
  1046. .owner = THIS_MODULE,
  1047. .pm = I2C_PXA_DEV_PM_OPS,
  1048. .of_match_table = i2c_pxa_dt_ids,
  1049. },
  1050. .id_table = i2c_pxa_id_table,
  1051. };
  1052. static int __init i2c_adap_pxa_init(void)
  1053. {
  1054. return platform_driver_register(&i2c_pxa_driver);
  1055. }
  1056. static void __exit i2c_adap_pxa_exit(void)
  1057. {
  1058. platform_driver_unregister(&i2c_pxa_driver);
  1059. }
  1060. MODULE_LICENSE("GPL");
  1061. MODULE_ALIAS("platform:pxa2xx-i2c");
  1062. subsys_initcall(i2c_adap_pxa_init);
  1063. module_exit(i2c_adap_pxa_exit);