i2c-pxa.c 31 KB

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