i2c-pxa.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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);