i2c-pxa.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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. (!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
  734. (isr & ISR_ACKNAK)))) {
  735. int ret = BUS_ERROR;
  736. /*
  737. * I2C bus error - either the device NAK'd us, or
  738. * something more serious happened. If we were NAK'd
  739. * on the initial address phase, we can retry.
  740. */
  741. if (isr & ISR_ACKNAK) {
  742. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  743. ret = I2C_RETRY;
  744. else
  745. ret = XFER_NAKED;
  746. }
  747. i2c_pxa_master_complete(i2c, ret);
  748. } else if (isr & ISR_RWM) {
  749. /*
  750. * Read mode. We have just sent the address byte, and
  751. * now we must initiate the transfer.
  752. */
  753. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  754. i2c->msg_idx == i2c->msg_num - 1)
  755. icr |= ICR_STOP | ICR_ACKNAK;
  756. icr |= ICR_ALDIE | ICR_TB;
  757. } else if (i2c->msg_ptr < i2c->msg->len) {
  758. /*
  759. * Write mode. Write the next data byte.
  760. */
  761. writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
  762. icr |= ICR_ALDIE | ICR_TB;
  763. /*
  764. * If this is the last byte of the last message or last byte
  765. * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
  766. */
  767. if ((i2c->msg_ptr == i2c->msg->len) &&
  768. ((i2c->msg->flags & I2C_M_STOP) ||
  769. (i2c->msg_idx == i2c->msg_num - 1)))
  770. icr |= ICR_STOP;
  771. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  772. /*
  773. * Next segment of the message.
  774. */
  775. i2c->msg_ptr = 0;
  776. i2c->msg_idx ++;
  777. i2c->msg++;
  778. /*
  779. * If we aren't doing a repeated start and address,
  780. * go back and try to send the next byte. Note that
  781. * we do not support switching the R/W direction here.
  782. */
  783. if (i2c->msg->flags & I2C_M_NOSTART)
  784. goto again;
  785. /*
  786. * Write the next address.
  787. */
  788. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  789. /*
  790. * And trigger a repeated start, and send the byte.
  791. */
  792. icr &= ~ICR_ALDIE;
  793. icr |= ICR_START | ICR_TB;
  794. } else {
  795. if (i2c->msg->len == 0) {
  796. /*
  797. * Device probes have a message length of zero
  798. * and need the bus to be reset before it can
  799. * be used again.
  800. */
  801. i2c_pxa_reset(i2c);
  802. }
  803. i2c_pxa_master_complete(i2c, 0);
  804. }
  805. i2c->icrlog[i2c->irqlogidx-1] = icr;
  806. writel(icr, _ICR(i2c));
  807. show_state(i2c);
  808. }
  809. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  810. {
  811. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  812. /*
  813. * Read the byte.
  814. */
  815. i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
  816. if (i2c->msg_ptr < i2c->msg->len) {
  817. /*
  818. * If this is the last byte of the last
  819. * message, send a STOP.
  820. */
  821. if (i2c->msg_ptr == i2c->msg->len - 1)
  822. icr |= ICR_STOP | ICR_ACKNAK;
  823. icr |= ICR_ALDIE | ICR_TB;
  824. } else {
  825. i2c_pxa_master_complete(i2c, 0);
  826. }
  827. i2c->icrlog[i2c->irqlogidx-1] = icr;
  828. writel(icr, _ICR(i2c));
  829. }
  830. #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
  831. ISR_SAD | ISR_BED)
  832. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  833. {
  834. struct pxa_i2c *i2c = dev_id;
  835. u32 isr = readl(_ISR(i2c));
  836. if (!(isr & VALID_INT_SOURCE))
  837. return IRQ_NONE;
  838. if (i2c_debug > 2 && 0) {
  839. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  840. __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
  841. decode_ISR(isr);
  842. }
  843. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  844. i2c->isrlog[i2c->irqlogidx++] = isr;
  845. show_state(i2c);
  846. /*
  847. * Always clear all pending IRQs.
  848. */
  849. writel(isr & VALID_INT_SOURCE, _ISR(i2c));
  850. if (isr & ISR_SAD)
  851. i2c_pxa_slave_start(i2c, isr);
  852. if (isr & ISR_SSD)
  853. i2c_pxa_slave_stop(i2c);
  854. if (i2c_pxa_is_slavemode(i2c)) {
  855. if (isr & ISR_ITE)
  856. i2c_pxa_slave_txempty(i2c, isr);
  857. if (isr & ISR_IRF)
  858. i2c_pxa_slave_rxfull(i2c, isr);
  859. } else if (i2c->msg && (!i2c->highmode_enter)) {
  860. if (isr & ISR_ITE)
  861. i2c_pxa_irq_txempty(i2c, isr);
  862. if (isr & ISR_IRF)
  863. i2c_pxa_irq_rxfull(i2c, isr);
  864. } else if ((isr & ISR_ITE) && i2c->highmode_enter) {
  865. i2c->highmode_enter = false;
  866. wake_up(&i2c->wait);
  867. } else {
  868. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  869. }
  870. return IRQ_HANDLED;
  871. }
  872. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  873. {
  874. struct pxa_i2c *i2c = adap->algo_data;
  875. int ret, i;
  876. for (i = adap->retries; i >= 0; i--) {
  877. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  878. if (ret != I2C_RETRY)
  879. goto out;
  880. if (i2c_debug)
  881. dev_dbg(&adap->dev, "Retrying transmission\n");
  882. udelay(100);
  883. }
  884. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  885. ret = -EREMOTEIO;
  886. out:
  887. i2c_pxa_set_slave(i2c, ret);
  888. return ret;
  889. }
  890. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  891. {
  892. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  893. I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
  894. }
  895. static const struct i2c_algorithm i2c_pxa_algorithm = {
  896. .master_xfer = i2c_pxa_xfer,
  897. .functionality = i2c_pxa_functionality,
  898. };
  899. static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
  900. .master_xfer = i2c_pxa_pio_xfer,
  901. .functionality = i2c_pxa_functionality,
  902. };
  903. static const struct of_device_id i2c_pxa_dt_ids[] = {
  904. { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
  905. { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
  906. { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA2XX },
  907. {}
  908. };
  909. MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
  910. static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
  911. enum pxa_i2c_types *i2c_types)
  912. {
  913. struct device_node *np = pdev->dev.of_node;
  914. const struct of_device_id *of_id =
  915. of_match_device(i2c_pxa_dt_ids, &pdev->dev);
  916. if (!of_id)
  917. return 1;
  918. /* For device tree we always use the dynamic or alias-assigned ID */
  919. i2c->adap.nr = -1;
  920. if (of_get_property(np, "mrvl,i2c-polling", NULL))
  921. i2c->use_pio = 1;
  922. if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
  923. i2c->fast_mode = 1;
  924. *i2c_types = (u32)(of_id->data);
  925. return 0;
  926. }
  927. static int i2c_pxa_probe_pdata(struct platform_device *pdev,
  928. struct pxa_i2c *i2c,
  929. enum pxa_i2c_types *i2c_types)
  930. {
  931. struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev);
  932. const struct platform_device_id *id = platform_get_device_id(pdev);
  933. *i2c_types = id->driver_data;
  934. if (plat) {
  935. i2c->use_pio = plat->use_pio;
  936. i2c->fast_mode = plat->fast_mode;
  937. i2c->high_mode = plat->high_mode;
  938. i2c->master_code = plat->master_code;
  939. if (!i2c->master_code)
  940. i2c->master_code = 0xe;
  941. i2c->rate = plat->rate;
  942. }
  943. return 0;
  944. }
  945. static int i2c_pxa_probe(struct platform_device *dev)
  946. {
  947. struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev);
  948. enum pxa_i2c_types i2c_type;
  949. struct pxa_i2c *i2c;
  950. struct resource *res = NULL;
  951. int ret, irq;
  952. i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
  953. if (!i2c) {
  954. ret = -ENOMEM;
  955. goto emalloc;
  956. }
  957. /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
  958. i2c->adap.nr = dev->id;
  959. ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
  960. if (ret > 0)
  961. ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
  962. if (ret < 0)
  963. goto eclk;
  964. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  965. irq = platform_get_irq(dev, 0);
  966. if (res == NULL || irq < 0) {
  967. ret = -ENODEV;
  968. goto eclk;
  969. }
  970. if (!request_mem_region(res->start, resource_size(res), res->name)) {
  971. ret = -ENOMEM;
  972. goto eclk;
  973. }
  974. i2c->adap.owner = THIS_MODULE;
  975. i2c->adap.retries = 5;
  976. spin_lock_init(&i2c->lock);
  977. init_waitqueue_head(&i2c->wait);
  978. strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
  979. i2c->clk = clk_get(&dev->dev, NULL);
  980. if (IS_ERR(i2c->clk)) {
  981. ret = PTR_ERR(i2c->clk);
  982. goto eclk;
  983. }
  984. i2c->reg_base = ioremap(res->start, resource_size(res));
  985. if (!i2c->reg_base) {
  986. ret = -EIO;
  987. goto eremap;
  988. }
  989. i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
  990. i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
  991. i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
  992. i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
  993. if (i2c_type != REGS_CE4100)
  994. i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
  995. i2c->iobase = res->start;
  996. i2c->iosize = resource_size(res);
  997. i2c->irq = irq;
  998. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  999. i2c->highmode_enter = false;
  1000. if (plat) {
  1001. #ifdef CONFIG_I2C_PXA_SLAVE
  1002. i2c->slave_addr = plat->slave_addr;
  1003. i2c->slave = plat->slave;
  1004. #endif
  1005. i2c->adap.class = plat->class;
  1006. }
  1007. if (i2c->high_mode) {
  1008. if (i2c->rate) {
  1009. clk_set_rate(i2c->clk, i2c->rate);
  1010. pr_info("i2c: <%s> set rate to %ld\n",
  1011. i2c->adap.name, clk_get_rate(i2c->clk));
  1012. } else
  1013. pr_warn("i2c: <%s> clock rate not set\n",
  1014. i2c->adap.name);
  1015. }
  1016. clk_prepare_enable(i2c->clk);
  1017. if (i2c->use_pio) {
  1018. i2c->adap.algo = &i2c_pxa_pio_algorithm;
  1019. } else {
  1020. i2c->adap.algo = &i2c_pxa_algorithm;
  1021. ret = request_irq(irq, i2c_pxa_handler, IRQF_SHARED,
  1022. dev_name(&dev->dev), i2c);
  1023. if (ret)
  1024. goto ereqirq;
  1025. }
  1026. i2c_pxa_reset(i2c);
  1027. i2c->adap.algo_data = i2c;
  1028. i2c->adap.dev.parent = &dev->dev;
  1029. #ifdef CONFIG_OF
  1030. i2c->adap.dev.of_node = dev->dev.of_node;
  1031. #endif
  1032. ret = i2c_add_numbered_adapter(&i2c->adap);
  1033. if (ret < 0) {
  1034. printk(KERN_INFO "I2C: Failed to add bus\n");
  1035. goto eadapt;
  1036. }
  1037. platform_set_drvdata(dev, i2c);
  1038. #ifdef CONFIG_I2C_PXA_SLAVE
  1039. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  1040. dev_name(&i2c->adap.dev), i2c->slave_addr);
  1041. #else
  1042. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  1043. dev_name(&i2c->adap.dev));
  1044. #endif
  1045. return 0;
  1046. eadapt:
  1047. if (!i2c->use_pio)
  1048. free_irq(irq, i2c);
  1049. ereqirq:
  1050. clk_disable_unprepare(i2c->clk);
  1051. iounmap(i2c->reg_base);
  1052. eremap:
  1053. clk_put(i2c->clk);
  1054. eclk:
  1055. kfree(i2c);
  1056. emalloc:
  1057. release_mem_region(res->start, resource_size(res));
  1058. return ret;
  1059. }
  1060. static int i2c_pxa_remove(struct platform_device *dev)
  1061. {
  1062. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  1063. i2c_del_adapter(&i2c->adap);
  1064. if (!i2c->use_pio)
  1065. free_irq(i2c->irq, i2c);
  1066. clk_disable_unprepare(i2c->clk);
  1067. clk_put(i2c->clk);
  1068. iounmap(i2c->reg_base);
  1069. release_mem_region(i2c->iobase, i2c->iosize);
  1070. kfree(i2c);
  1071. return 0;
  1072. }
  1073. #ifdef CONFIG_PM
  1074. static int i2c_pxa_suspend_noirq(struct device *dev)
  1075. {
  1076. struct platform_device *pdev = to_platform_device(dev);
  1077. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1078. clk_disable(i2c->clk);
  1079. return 0;
  1080. }
  1081. static int i2c_pxa_resume_noirq(struct device *dev)
  1082. {
  1083. struct platform_device *pdev = to_platform_device(dev);
  1084. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1085. clk_enable(i2c->clk);
  1086. i2c_pxa_reset(i2c);
  1087. return 0;
  1088. }
  1089. static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
  1090. .suspend_noirq = i2c_pxa_suspend_noirq,
  1091. .resume_noirq = i2c_pxa_resume_noirq,
  1092. };
  1093. #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
  1094. #else
  1095. #define I2C_PXA_DEV_PM_OPS NULL
  1096. #endif
  1097. static struct platform_driver i2c_pxa_driver = {
  1098. .probe = i2c_pxa_probe,
  1099. .remove = i2c_pxa_remove,
  1100. .driver = {
  1101. .name = "pxa2xx-i2c",
  1102. .pm = I2C_PXA_DEV_PM_OPS,
  1103. .of_match_table = i2c_pxa_dt_ids,
  1104. },
  1105. .id_table = i2c_pxa_id_table,
  1106. };
  1107. static int __init i2c_adap_pxa_init(void)
  1108. {
  1109. return platform_driver_register(&i2c_pxa_driver);
  1110. }
  1111. static void __exit i2c_adap_pxa_exit(void)
  1112. {
  1113. platform_driver_unregister(&i2c_pxa_driver);
  1114. }
  1115. MODULE_LICENSE("GPL");
  1116. MODULE_ALIAS("platform:pxa2xx-i2c");
  1117. subsys_initcall(i2c_adap_pxa_init);
  1118. module_exit(i2c_adap_pxa_exit);