i2c-pxa.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. unsigned int req_slave_addr;
  127. struct i2c_adapter adap;
  128. struct clk *clk;
  129. #ifdef CONFIG_I2C_PXA_SLAVE
  130. struct i2c_slave_client *slave;
  131. #endif
  132. unsigned int irqlogidx;
  133. u32 isrlog[32];
  134. u32 icrlog[32];
  135. void __iomem *reg_base;
  136. void __iomem *reg_ibmr;
  137. void __iomem *reg_idbr;
  138. void __iomem *reg_icr;
  139. void __iomem *reg_isr;
  140. void __iomem *reg_isar;
  141. unsigned long iobase;
  142. unsigned long iosize;
  143. int irq;
  144. unsigned int use_pio :1;
  145. unsigned int fast_mode :1;
  146. unsigned int high_mode:1;
  147. unsigned char master_code;
  148. unsigned long rate;
  149. bool highmode_enter;
  150. };
  151. #define _IBMR(i2c) ((i2c)->reg_ibmr)
  152. #define _IDBR(i2c) ((i2c)->reg_idbr)
  153. #define _ICR(i2c) ((i2c)->reg_icr)
  154. #define _ISR(i2c) ((i2c)->reg_isr)
  155. #define _ISAR(i2c) ((i2c)->reg_isar)
  156. /*
  157. * I2C Slave mode address
  158. */
  159. #define I2C_PXA_SLAVE_ADDR 0x1
  160. #ifdef DEBUG
  161. struct bits {
  162. u32 mask;
  163. const char *set;
  164. const char *unset;
  165. };
  166. #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
  167. static inline void
  168. decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
  169. {
  170. printk("%s %08x: ", prefix, val);
  171. while (num--) {
  172. const char *str = val & bits->mask ? bits->set : bits->unset;
  173. if (str)
  174. printk("%s ", str);
  175. bits++;
  176. }
  177. }
  178. static const struct bits isr_bits[] = {
  179. PXA_BIT(ISR_RWM, "RX", "TX"),
  180. PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
  181. PXA_BIT(ISR_UB, "Bsy", "Rdy"),
  182. PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
  183. PXA_BIT(ISR_SSD, "SlaveStop", NULL),
  184. PXA_BIT(ISR_ALD, "ALD", NULL),
  185. PXA_BIT(ISR_ITE, "TxEmpty", NULL),
  186. PXA_BIT(ISR_IRF, "RxFull", NULL),
  187. PXA_BIT(ISR_GCAD, "GenCall", NULL),
  188. PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
  189. PXA_BIT(ISR_BED, "BusErr", NULL),
  190. };
  191. static void decode_ISR(unsigned int val)
  192. {
  193. decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
  194. printk("\n");
  195. }
  196. static const struct bits icr_bits[] = {
  197. PXA_BIT(ICR_START, "START", NULL),
  198. PXA_BIT(ICR_STOP, "STOP", NULL),
  199. PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
  200. PXA_BIT(ICR_TB, "TB", NULL),
  201. PXA_BIT(ICR_MA, "MA", NULL),
  202. PXA_BIT(ICR_SCLE, "SCLE", "scle"),
  203. PXA_BIT(ICR_IUE, "IUE", "iue"),
  204. PXA_BIT(ICR_GCD, "GCD", NULL),
  205. PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
  206. PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
  207. PXA_BIT(ICR_BEIE, "BEIE", NULL),
  208. PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
  209. PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
  210. PXA_BIT(ICR_SADIE, "SADIE", NULL),
  211. PXA_BIT(ICR_UR, "UR", "ur"),
  212. };
  213. #ifdef CONFIG_I2C_PXA_SLAVE
  214. static void decode_ICR(unsigned int val)
  215. {
  216. decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
  217. printk("\n");
  218. }
  219. #endif
  220. static unsigned int i2c_debug = DEBUG;
  221. static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
  222. {
  223. dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
  224. readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  225. }
  226. #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
  227. static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
  228. {
  229. unsigned int i;
  230. struct device *dev = &i2c->adap.dev;
  231. dev_err(dev, "slave_0x%x error: %s\n",
  232. i2c->req_slave_addr >> 1, why);
  233. dev_err(dev, "msg_num: %d msg_idx: %d msg_ptr: %d\n",
  234. i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
  235. dev_err(dev, "IBMR: %08x IDBR: %08x ICR: %08x ISR: %08x\n",
  236. readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)),
  237. readl(_ISR(i2c)));
  238. dev_dbg(dev, "log: ");
  239. for (i = 0; i < i2c->irqlogidx; i++)
  240. pr_debug("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
  241. pr_debug("\n");
  242. }
  243. #else /* ifdef DEBUG */
  244. #define i2c_debug 0
  245. #define show_state(i2c) do { } while (0)
  246. #define decode_ISR(val) do { } while (0)
  247. #define decode_ICR(val) do { } while (0)
  248. #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
  249. #endif /* ifdef DEBUG / else */
  250. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
  251. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
  252. static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
  253. {
  254. return !(readl(_ICR(i2c)) & ICR_SCLE);
  255. }
  256. static void i2c_pxa_abort(struct pxa_i2c *i2c)
  257. {
  258. int i = 250;
  259. if (i2c_pxa_is_slavemode(i2c)) {
  260. dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
  261. return;
  262. }
  263. while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
  264. unsigned long icr = readl(_ICR(i2c));
  265. icr &= ~ICR_START;
  266. icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
  267. writel(icr, _ICR(i2c));
  268. show_state(i2c);
  269. mdelay(1);
  270. i --;
  271. }
  272. writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
  273. _ICR(i2c));
  274. }
  275. static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
  276. {
  277. int timeout = DEF_TIMEOUT;
  278. while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
  279. if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
  280. timeout += 4;
  281. msleep(2);
  282. show_state(i2c);
  283. }
  284. if (timeout < 0)
  285. show_state(i2c);
  286. return timeout < 0 ? I2C_RETRY : 0;
  287. }
  288. static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
  289. {
  290. unsigned long timeout = jiffies + HZ*4;
  291. while (time_before(jiffies, timeout)) {
  292. if (i2c_debug > 1)
  293. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  294. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  295. if (readl(_ISR(i2c)) & ISR_SAD) {
  296. if (i2c_debug > 0)
  297. dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
  298. goto out;
  299. }
  300. /* wait for unit and bus being not busy, and we also do a
  301. * quick check of the i2c lines themselves to ensure they've
  302. * gone high...
  303. */
  304. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
  305. if (i2c_debug > 0)
  306. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  307. return 1;
  308. }
  309. msleep(1);
  310. }
  311. if (i2c_debug > 0)
  312. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  313. out:
  314. return 0;
  315. }
  316. static int i2c_pxa_set_master(struct pxa_i2c *i2c)
  317. {
  318. if (i2c_debug)
  319. dev_dbg(&i2c->adap.dev, "setting to bus master\n");
  320. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
  321. dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
  322. if (!i2c_pxa_wait_master(i2c)) {
  323. dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
  324. return I2C_RETRY;
  325. }
  326. }
  327. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  328. return 0;
  329. }
  330. #ifdef CONFIG_I2C_PXA_SLAVE
  331. static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
  332. {
  333. unsigned long timeout = jiffies + HZ*1;
  334. /* wait for stop */
  335. show_state(i2c);
  336. while (time_before(jiffies, timeout)) {
  337. if (i2c_debug > 1)
  338. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  339. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  340. if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
  341. (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
  342. (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
  343. if (i2c_debug > 1)
  344. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  345. return 1;
  346. }
  347. msleep(1);
  348. }
  349. if (i2c_debug > 0)
  350. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  351. return 0;
  352. }
  353. /*
  354. * clear the hold on the bus, and take of anything else
  355. * that has been configured
  356. */
  357. static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
  358. {
  359. show_state(i2c);
  360. if (errcode < 0) {
  361. udelay(100); /* simple delay */
  362. } else {
  363. /* we need to wait for the stop condition to end */
  364. /* if we where in stop, then clear... */
  365. if (readl(_ICR(i2c)) & ICR_STOP) {
  366. udelay(100);
  367. writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
  368. }
  369. if (!i2c_pxa_wait_slave(i2c)) {
  370. dev_err(&i2c->adap.dev, "%s: wait timedout\n",
  371. __func__);
  372. return;
  373. }
  374. }
  375. writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
  376. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  377. if (i2c_debug) {
  378. dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
  379. decode_ICR(readl(_ICR(i2c)));
  380. }
  381. }
  382. #else
  383. #define i2c_pxa_set_slave(i2c, err) do { } while (0)
  384. #endif
  385. static void i2c_pxa_reset(struct pxa_i2c *i2c)
  386. {
  387. pr_debug("Resetting I2C Controller Unit\n");
  388. /* abort any transfer currently under way */
  389. i2c_pxa_abort(i2c);
  390. /* reset according to 9.8 */
  391. writel(ICR_UR, _ICR(i2c));
  392. writel(I2C_ISR_INIT, _ISR(i2c));
  393. writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
  394. if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
  395. writel(i2c->slave_addr, _ISAR(i2c));
  396. /* set control register values */
  397. writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c));
  398. writel(readl(_ICR(i2c)) | (i2c->high_mode ? ICR_HS : 0), _ICR(i2c));
  399. #ifdef CONFIG_I2C_PXA_SLAVE
  400. dev_info(&i2c->adap.dev, "Enabling slave mode\n");
  401. writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
  402. #endif
  403. i2c_pxa_set_slave(i2c, 0);
  404. /* enable unit */
  405. writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
  406. udelay(100);
  407. }
  408. #ifdef CONFIG_I2C_PXA_SLAVE
  409. /*
  410. * PXA I2C Slave mode
  411. */
  412. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  413. {
  414. if (isr & ISR_BED) {
  415. /* what should we do here? */
  416. } else {
  417. int ret = 0;
  418. if (i2c->slave != NULL)
  419. ret = i2c->slave->read(i2c->slave->data);
  420. writel(ret, _IDBR(i2c));
  421. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
  422. }
  423. }
  424. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  425. {
  426. unsigned int byte = readl(_IDBR(i2c));
  427. if (i2c->slave != NULL)
  428. i2c->slave->write(i2c->slave->data, byte);
  429. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  430. }
  431. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  432. {
  433. int timeout;
  434. if (i2c_debug > 0)
  435. dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
  436. (isr & ISR_RWM) ? 'r' : 't');
  437. if (i2c->slave != NULL)
  438. i2c->slave->event(i2c->slave->data,
  439. (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
  440. /*
  441. * slave could interrupt in the middle of us generating a
  442. * start condition... if this happens, we'd better back off
  443. * and stop holding the poor thing up
  444. */
  445. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  446. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  447. timeout = 0x10000;
  448. while (1) {
  449. if ((readl(_IBMR(i2c)) & 2) == 2)
  450. break;
  451. timeout--;
  452. if (timeout <= 0) {
  453. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  454. break;
  455. }
  456. }
  457. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  458. }
  459. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  460. {
  461. if (i2c_debug > 2)
  462. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
  463. if (i2c->slave != NULL)
  464. i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
  465. if (i2c_debug > 2)
  466. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
  467. /*
  468. * If we have a master-mode message waiting,
  469. * kick it off now that the slave has completed.
  470. */
  471. if (i2c->msg)
  472. i2c_pxa_master_complete(i2c, I2C_RETRY);
  473. }
  474. #else
  475. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  476. {
  477. if (isr & ISR_BED) {
  478. /* what should we do here? */
  479. } else {
  480. writel(0, _IDBR(i2c));
  481. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  482. }
  483. }
  484. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  485. {
  486. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  487. }
  488. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  489. {
  490. int timeout;
  491. /*
  492. * slave could interrupt in the middle of us generating a
  493. * start condition... if this happens, we'd better back off
  494. * and stop holding the poor thing up
  495. */
  496. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  497. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  498. timeout = 0x10000;
  499. while (1) {
  500. if ((readl(_IBMR(i2c)) & 2) == 2)
  501. break;
  502. timeout--;
  503. if (timeout <= 0) {
  504. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  505. break;
  506. }
  507. }
  508. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  509. }
  510. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  511. {
  512. if (i2c->msg)
  513. i2c_pxa_master_complete(i2c, I2C_RETRY);
  514. }
  515. #endif
  516. /*
  517. * PXA I2C Master mode
  518. */
  519. static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
  520. {
  521. unsigned int addr = (msg->addr & 0x7f) << 1;
  522. if (msg->flags & I2C_M_RD)
  523. addr |= 1;
  524. return addr;
  525. }
  526. static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
  527. {
  528. u32 icr;
  529. /*
  530. * Step 1: target slave address into IDBR
  531. */
  532. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  533. i2c->req_slave_addr = i2c_pxa_addr_byte(i2c->msg);
  534. /*
  535. * Step 2: initiate the write.
  536. */
  537. icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
  538. writel(icr | ICR_START | ICR_TB, _ICR(i2c));
  539. }
  540. static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
  541. {
  542. u32 icr;
  543. /*
  544. * Clear the STOP and ACK flags
  545. */
  546. icr = readl(_ICR(i2c));
  547. icr &= ~(ICR_STOP | ICR_ACKNAK);
  548. writel(icr, _ICR(i2c));
  549. }
  550. static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
  551. {
  552. /* make timeout the same as for interrupt based functions */
  553. long timeout = 2 * DEF_TIMEOUT;
  554. /*
  555. * Wait for the bus to become free.
  556. */
  557. while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
  558. udelay(1000);
  559. show_state(i2c);
  560. }
  561. if (timeout < 0) {
  562. show_state(i2c);
  563. dev_err(&i2c->adap.dev,
  564. "i2c_pxa: timeout waiting for bus free\n");
  565. return I2C_RETRY;
  566. }
  567. /*
  568. * Set master mode.
  569. */
  570. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  571. return 0;
  572. }
  573. /*
  574. * PXA I2C send master code
  575. * 1. Load master code to IDBR and send it.
  576. * Note for HS mode, set ICR [GPIOEN].
  577. * 2. Wait until win arbitration.
  578. */
  579. static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c)
  580. {
  581. u32 icr;
  582. long timeout;
  583. spin_lock_irq(&i2c->lock);
  584. i2c->highmode_enter = true;
  585. writel(i2c->master_code, _IDBR(i2c));
  586. icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
  587. icr |= ICR_GPIOEN | ICR_START | ICR_TB | ICR_ITEIE;
  588. writel(icr, _ICR(i2c));
  589. spin_unlock_irq(&i2c->lock);
  590. timeout = wait_event_timeout(i2c->wait,
  591. i2c->highmode_enter == false, HZ * 1);
  592. i2c->highmode_enter = false;
  593. return (timeout == 0) ? I2C_RETRY : 0;
  594. }
  595. static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
  596. struct i2c_msg *msg, int num)
  597. {
  598. unsigned long timeout = 500000; /* 5 seconds */
  599. int ret = 0;
  600. ret = i2c_pxa_pio_set_master(i2c);
  601. if (ret)
  602. goto out;
  603. i2c->msg = msg;
  604. i2c->msg_num = num;
  605. i2c->msg_idx = 0;
  606. i2c->msg_ptr = 0;
  607. i2c->irqlogidx = 0;
  608. i2c_pxa_start_message(i2c);
  609. while (i2c->msg_num > 0 && --timeout) {
  610. i2c_pxa_handler(0, i2c);
  611. udelay(10);
  612. }
  613. i2c_pxa_stop_message(i2c);
  614. /*
  615. * We place the return code in i2c->msg_idx.
  616. */
  617. ret = i2c->msg_idx;
  618. out:
  619. if (timeout == 0) {
  620. i2c_pxa_scream_blue_murder(i2c, "timeout");
  621. ret = I2C_RETRY;
  622. }
  623. return ret;
  624. }
  625. /*
  626. * We are protected by the adapter bus mutex.
  627. */
  628. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  629. {
  630. long timeout;
  631. int ret;
  632. /*
  633. * Wait for the bus to become free.
  634. */
  635. ret = i2c_pxa_wait_bus_not_busy(i2c);
  636. if (ret) {
  637. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  638. goto out;
  639. }
  640. /*
  641. * Set master mode.
  642. */
  643. ret = i2c_pxa_set_master(i2c);
  644. if (ret) {
  645. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  646. goto out;
  647. }
  648. if (i2c->high_mode) {
  649. ret = i2c_pxa_send_mastercode(i2c);
  650. if (ret) {
  651. dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
  652. goto out;
  653. }
  654. }
  655. spin_lock_irq(&i2c->lock);
  656. i2c->msg = msg;
  657. i2c->msg_num = num;
  658. i2c->msg_idx = 0;
  659. i2c->msg_ptr = 0;
  660. i2c->irqlogidx = 0;
  661. i2c_pxa_start_message(i2c);
  662. spin_unlock_irq(&i2c->lock);
  663. /*
  664. * The rest of the processing occurs in the interrupt handler.
  665. */
  666. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  667. i2c_pxa_stop_message(i2c);
  668. /*
  669. * We place the return code in i2c->msg_idx.
  670. */
  671. ret = i2c->msg_idx;
  672. if (!timeout && i2c->msg_num) {
  673. i2c_pxa_scream_blue_murder(i2c, "timeout");
  674. ret = I2C_RETRY;
  675. }
  676. out:
  677. return ret;
  678. }
  679. static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
  680. struct i2c_msg msgs[], int num)
  681. {
  682. struct pxa_i2c *i2c = adap->algo_data;
  683. int ret, i;
  684. /* If the I2C controller is disabled we need to reset it
  685. (probably due to a suspend/resume destroying state). We do
  686. this here as we can then avoid worrying about resuming the
  687. controller before its users. */
  688. if (!(readl(_ICR(i2c)) & ICR_IUE))
  689. i2c_pxa_reset(i2c);
  690. for (i = adap->retries; i >= 0; i--) {
  691. ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
  692. if (ret != I2C_RETRY)
  693. goto out;
  694. if (i2c_debug)
  695. dev_dbg(&adap->dev, "Retrying transmission\n");
  696. udelay(100);
  697. }
  698. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  699. ret = -EREMOTEIO;
  700. out:
  701. i2c_pxa_set_slave(i2c, ret);
  702. return ret;
  703. }
  704. /*
  705. * i2c_pxa_master_complete - complete the message and wake up.
  706. */
  707. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  708. {
  709. i2c->msg_ptr = 0;
  710. i2c->msg = NULL;
  711. i2c->msg_idx ++;
  712. i2c->msg_num = 0;
  713. if (ret)
  714. i2c->msg_idx = ret;
  715. if (!i2c->use_pio)
  716. wake_up(&i2c->wait);
  717. }
  718. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  719. {
  720. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  721. again:
  722. /*
  723. * If ISR_ALD is set, we lost arbitration.
  724. */
  725. if (isr & ISR_ALD) {
  726. /*
  727. * Do we need to do anything here? The PXA docs
  728. * are vague about what happens.
  729. */
  730. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  731. /*
  732. * We ignore this error. We seem to see spurious ALDs
  733. * for seemingly no reason. If we handle them as I think
  734. * they should, we end up causing an I2C error, which
  735. * is painful for some systems.
  736. */
  737. return; /* ignore */
  738. }
  739. if ((isr & ISR_BED) &&
  740. (!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
  741. (isr & ISR_ACKNAK)))) {
  742. int ret = BUS_ERROR;
  743. /*
  744. * I2C bus error - either the device NAK'd us, or
  745. * something more serious happened. If we were NAK'd
  746. * on the initial address phase, we can retry.
  747. */
  748. if (isr & ISR_ACKNAK) {
  749. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  750. ret = I2C_RETRY;
  751. else
  752. ret = XFER_NAKED;
  753. }
  754. i2c_pxa_master_complete(i2c, ret);
  755. } else if (isr & ISR_RWM) {
  756. /*
  757. * Read mode. We have just sent the address byte, and
  758. * now we must initiate the transfer.
  759. */
  760. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  761. i2c->msg_idx == i2c->msg_num - 1)
  762. icr |= ICR_STOP | ICR_ACKNAK;
  763. icr |= ICR_ALDIE | ICR_TB;
  764. } else if (i2c->msg_ptr < i2c->msg->len) {
  765. /*
  766. * Write mode. Write the next data byte.
  767. */
  768. writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
  769. icr |= ICR_ALDIE | ICR_TB;
  770. /*
  771. * If this is the last byte of the last message or last byte
  772. * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
  773. */
  774. if ((i2c->msg_ptr == i2c->msg->len) &&
  775. ((i2c->msg->flags & I2C_M_STOP) ||
  776. (i2c->msg_idx == i2c->msg_num - 1)))
  777. icr |= ICR_STOP;
  778. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  779. /*
  780. * Next segment of the message.
  781. */
  782. i2c->msg_ptr = 0;
  783. i2c->msg_idx ++;
  784. i2c->msg++;
  785. /*
  786. * If we aren't doing a repeated start and address,
  787. * go back and try to send the next byte. Note that
  788. * we do not support switching the R/W direction here.
  789. */
  790. if (i2c->msg->flags & I2C_M_NOSTART)
  791. goto again;
  792. /*
  793. * Write the next address.
  794. */
  795. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  796. i2c->req_slave_addr = i2c_pxa_addr_byte(i2c->msg);
  797. /*
  798. * And trigger a repeated start, and send the byte.
  799. */
  800. icr &= ~ICR_ALDIE;
  801. icr |= ICR_START | ICR_TB;
  802. } else {
  803. if (i2c->msg->len == 0) {
  804. /*
  805. * Device probes have a message length of zero
  806. * and need the bus to be reset before it can
  807. * be used again.
  808. */
  809. i2c_pxa_reset(i2c);
  810. }
  811. i2c_pxa_master_complete(i2c, 0);
  812. }
  813. i2c->icrlog[i2c->irqlogidx-1] = icr;
  814. writel(icr, _ICR(i2c));
  815. show_state(i2c);
  816. }
  817. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  818. {
  819. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  820. /*
  821. * Read the byte.
  822. */
  823. i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
  824. if (i2c->msg_ptr < i2c->msg->len) {
  825. /*
  826. * If this is the last byte of the last
  827. * message, send a STOP.
  828. */
  829. if (i2c->msg_ptr == i2c->msg->len - 1)
  830. icr |= ICR_STOP | ICR_ACKNAK;
  831. icr |= ICR_ALDIE | ICR_TB;
  832. } else {
  833. i2c_pxa_master_complete(i2c, 0);
  834. }
  835. i2c->icrlog[i2c->irqlogidx-1] = icr;
  836. writel(icr, _ICR(i2c));
  837. }
  838. #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
  839. ISR_SAD | ISR_BED)
  840. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  841. {
  842. struct pxa_i2c *i2c = dev_id;
  843. u32 isr = readl(_ISR(i2c));
  844. if (!(isr & VALID_INT_SOURCE))
  845. return IRQ_NONE;
  846. if (i2c_debug > 2 && 0) {
  847. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  848. __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
  849. decode_ISR(isr);
  850. }
  851. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  852. i2c->isrlog[i2c->irqlogidx++] = isr;
  853. show_state(i2c);
  854. /*
  855. * Always clear all pending IRQs.
  856. */
  857. writel(isr & VALID_INT_SOURCE, _ISR(i2c));
  858. if (isr & ISR_SAD)
  859. i2c_pxa_slave_start(i2c, isr);
  860. if (isr & ISR_SSD)
  861. i2c_pxa_slave_stop(i2c);
  862. if (i2c_pxa_is_slavemode(i2c)) {
  863. if (isr & ISR_ITE)
  864. i2c_pxa_slave_txempty(i2c, isr);
  865. if (isr & ISR_IRF)
  866. i2c_pxa_slave_rxfull(i2c, isr);
  867. } else if (i2c->msg && (!i2c->highmode_enter)) {
  868. if (isr & ISR_ITE)
  869. i2c_pxa_irq_txempty(i2c, isr);
  870. if (isr & ISR_IRF)
  871. i2c_pxa_irq_rxfull(i2c, isr);
  872. } else if ((isr & ISR_ITE) && i2c->highmode_enter) {
  873. i2c->highmode_enter = false;
  874. wake_up(&i2c->wait);
  875. } else {
  876. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  877. }
  878. return IRQ_HANDLED;
  879. }
  880. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  881. {
  882. struct pxa_i2c *i2c = adap->algo_data;
  883. int ret, i;
  884. for (i = adap->retries; i >= 0; i--) {
  885. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  886. if (ret != I2C_RETRY)
  887. goto out;
  888. if (i2c_debug)
  889. dev_dbg(&adap->dev, "Retrying transmission\n");
  890. udelay(100);
  891. }
  892. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  893. ret = -EREMOTEIO;
  894. out:
  895. i2c_pxa_set_slave(i2c, ret);
  896. return ret;
  897. }
  898. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  899. {
  900. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  901. I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
  902. }
  903. static const struct i2c_algorithm i2c_pxa_algorithm = {
  904. .master_xfer = i2c_pxa_xfer,
  905. .functionality = i2c_pxa_functionality,
  906. };
  907. static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
  908. .master_xfer = i2c_pxa_pio_xfer,
  909. .functionality = i2c_pxa_functionality,
  910. };
  911. static const struct of_device_id i2c_pxa_dt_ids[] = {
  912. { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
  913. { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
  914. { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA2XX },
  915. {}
  916. };
  917. MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
  918. static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
  919. enum pxa_i2c_types *i2c_types)
  920. {
  921. struct device_node *np = pdev->dev.of_node;
  922. const struct of_device_id *of_id =
  923. of_match_device(i2c_pxa_dt_ids, &pdev->dev);
  924. if (!of_id)
  925. return 1;
  926. /* For device tree we always use the dynamic or alias-assigned ID */
  927. i2c->adap.nr = -1;
  928. if (of_get_property(np, "mrvl,i2c-polling", NULL))
  929. i2c->use_pio = 1;
  930. if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
  931. i2c->fast_mode = 1;
  932. *i2c_types = (enum pxa_i2c_types)(of_id->data);
  933. return 0;
  934. }
  935. static int i2c_pxa_probe_pdata(struct platform_device *pdev,
  936. struct pxa_i2c *i2c,
  937. enum pxa_i2c_types *i2c_types)
  938. {
  939. struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev);
  940. const struct platform_device_id *id = platform_get_device_id(pdev);
  941. *i2c_types = id->driver_data;
  942. if (plat) {
  943. i2c->use_pio = plat->use_pio;
  944. i2c->fast_mode = plat->fast_mode;
  945. i2c->high_mode = plat->high_mode;
  946. i2c->master_code = plat->master_code;
  947. if (!i2c->master_code)
  948. i2c->master_code = 0xe;
  949. i2c->rate = plat->rate;
  950. }
  951. return 0;
  952. }
  953. static int i2c_pxa_probe(struct platform_device *dev)
  954. {
  955. struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev);
  956. enum pxa_i2c_types i2c_type;
  957. struct pxa_i2c *i2c;
  958. struct resource *res = NULL;
  959. int ret, irq;
  960. i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL);
  961. if (!i2c)
  962. return -ENOMEM;
  963. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  964. i2c->reg_base = devm_ioremap_resource(&dev->dev, res);
  965. if (IS_ERR(i2c->reg_base))
  966. return PTR_ERR(i2c->reg_base);
  967. irq = platform_get_irq(dev, 0);
  968. if (irq < 0) {
  969. dev_err(&dev->dev, "no irq resource: %d\n", irq);
  970. return irq;
  971. }
  972. /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
  973. i2c->adap.nr = dev->id;
  974. ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
  975. if (ret > 0)
  976. ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
  977. if (ret < 0)
  978. return ret;
  979. i2c->adap.owner = THIS_MODULE;
  980. i2c->adap.retries = 5;
  981. spin_lock_init(&i2c->lock);
  982. init_waitqueue_head(&i2c->wait);
  983. strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
  984. i2c->clk = devm_clk_get(&dev->dev, NULL);
  985. if (IS_ERR(i2c->clk)) {
  986. dev_err(&dev->dev, "failed to get the clk: %ld\n", PTR_ERR(i2c->clk));
  987. return PTR_ERR(i2c->clk);
  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 = devm_request_irq(&dev->dev, irq, i2c_pxa_handler,
  1022. IRQF_SHARED | IRQF_NO_SUSPEND,
  1023. dev_name(&dev->dev), i2c);
  1024. if (ret) {
  1025. dev_err(&dev->dev, "failed to request irq: %d\n", ret);
  1026. goto ereqirq;
  1027. }
  1028. }
  1029. i2c_pxa_reset(i2c);
  1030. i2c->adap.algo_data = i2c;
  1031. i2c->adap.dev.parent = &dev->dev;
  1032. #ifdef CONFIG_OF
  1033. i2c->adap.dev.of_node = dev->dev.of_node;
  1034. #endif
  1035. ret = i2c_add_numbered_adapter(&i2c->adap);
  1036. if (ret < 0) {
  1037. dev_err(&dev->dev, "failed to add bus: %d\n", ret);
  1038. goto ereqirq;
  1039. }
  1040. platform_set_drvdata(dev, i2c);
  1041. #ifdef CONFIG_I2C_PXA_SLAVE
  1042. dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n",
  1043. i2c->slave_addr);
  1044. #else
  1045. dev_info(&i2c->adap.dev, " PXA I2C adapter\n");
  1046. #endif
  1047. return 0;
  1048. ereqirq:
  1049. clk_disable_unprepare(i2c->clk);
  1050. return ret;
  1051. }
  1052. static int i2c_pxa_remove(struct platform_device *dev)
  1053. {
  1054. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  1055. i2c_del_adapter(&i2c->adap);
  1056. clk_disable_unprepare(i2c->clk);
  1057. return 0;
  1058. }
  1059. #ifdef CONFIG_PM
  1060. static int i2c_pxa_suspend_noirq(struct device *dev)
  1061. {
  1062. struct platform_device *pdev = to_platform_device(dev);
  1063. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1064. clk_disable(i2c->clk);
  1065. return 0;
  1066. }
  1067. static int i2c_pxa_resume_noirq(struct device *dev)
  1068. {
  1069. struct platform_device *pdev = to_platform_device(dev);
  1070. struct pxa_i2c *i2c = platform_get_drvdata(pdev);
  1071. clk_enable(i2c->clk);
  1072. i2c_pxa_reset(i2c);
  1073. return 0;
  1074. }
  1075. static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
  1076. .suspend_noirq = i2c_pxa_suspend_noirq,
  1077. .resume_noirq = i2c_pxa_resume_noirq,
  1078. };
  1079. #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
  1080. #else
  1081. #define I2C_PXA_DEV_PM_OPS NULL
  1082. #endif
  1083. static struct platform_driver i2c_pxa_driver = {
  1084. .probe = i2c_pxa_probe,
  1085. .remove = i2c_pxa_remove,
  1086. .driver = {
  1087. .name = "pxa2xx-i2c",
  1088. .pm = I2C_PXA_DEV_PM_OPS,
  1089. .of_match_table = i2c_pxa_dt_ids,
  1090. },
  1091. .id_table = i2c_pxa_id_table,
  1092. };
  1093. static int __init i2c_adap_pxa_init(void)
  1094. {
  1095. return platform_driver_register(&i2c_pxa_driver);
  1096. }
  1097. static void __exit i2c_adap_pxa_exit(void)
  1098. {
  1099. platform_driver_unregister(&i2c_pxa_driver);
  1100. }
  1101. MODULE_LICENSE("GPL");
  1102. MODULE_ALIAS("platform:pxa2xx-i2c");
  1103. subsys_initcall(i2c_adap_pxa_init);
  1104. module_exit(i2c_adap_pxa_exit);