i2c-pxa.c 32 KB

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