i2c-pxa.c 32 KB

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