i2c-sh_mobile.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * SuperH Mobile I2C Controller
  3. *
  4. * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
  5. *
  6. * Copyright (C) 2008 Magnus Damm
  7. *
  8. * Portions of the code based on out-of-tree driver i2c-sh7343.c
  9. * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/err.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c/i2c-sh_mobile.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/of_device.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/slab.h>
  36. /* Transmit operation: */
  37. /* */
  38. /* 0 byte transmit */
  39. /* BUS: S A8 ACK P(*) */
  40. /* IRQ: DTE WAIT */
  41. /* ICIC: */
  42. /* ICCR: 0x94 0x90 */
  43. /* ICDR: A8 */
  44. /* */
  45. /* 1 byte transmit */
  46. /* BUS: S A8 ACK D8(1) ACK P(*) */
  47. /* IRQ: DTE WAIT WAIT */
  48. /* ICIC: -DTE */
  49. /* ICCR: 0x94 0x90 */
  50. /* ICDR: A8 D8(1) */
  51. /* */
  52. /* 2 byte transmit */
  53. /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P(*) */
  54. /* IRQ: DTE WAIT WAIT WAIT */
  55. /* ICIC: -DTE */
  56. /* ICCR: 0x94 0x90 */
  57. /* ICDR: A8 D8(1) D8(2) */
  58. /* */
  59. /* 3 bytes or more, +---------+ gets repeated */
  60. /* */
  61. /* */
  62. /* Receive operation: */
  63. /* */
  64. /* 0 byte receive - not supported since slave may hold SDA low */
  65. /* */
  66. /* 1 byte receive [TX] | [RX] */
  67. /* BUS: S A8 ACK | D8(1) ACK P(*) */
  68. /* IRQ: DTE WAIT | WAIT DTE */
  69. /* ICIC: -DTE | +DTE */
  70. /* ICCR: 0x94 0x81 | 0xc0 */
  71. /* ICDR: A8 | D8(1) */
  72. /* */
  73. /* 2 byte receive [TX]| [RX] */
  74. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P(*) */
  75. /* IRQ: DTE WAIT | WAIT WAIT DTE */
  76. /* ICIC: -DTE | +DTE */
  77. /* ICCR: 0x94 0x81 | 0xc0 */
  78. /* ICDR: A8 | D8(1) D8(2) */
  79. /* */
  80. /* 3 byte receive [TX] | [RX] (*) */
  81. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
  82. /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
  83. /* ICIC: -DTE | +DTE */
  84. /* ICCR: 0x94 0x81 | 0xc0 */
  85. /* ICDR: A8 | D8(1) D8(2) D8(3) */
  86. /* */
  87. /* 4 bytes or more, this part is repeated +---------+ */
  88. /* */
  89. /* */
  90. /* Interrupt order and BUSY flag */
  91. /* ___ _ */
  92. /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
  93. /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
  94. /* */
  95. /* S D7 D6 D5 D4 D3 D2 D1 D0 P(*) */
  96. /* ___ */
  97. /* WAIT IRQ ________________________________/ \___________ */
  98. /* TACK IRQ ____________________________________/ \_______ */
  99. /* DTE IRQ __________________________________________/ \_ */
  100. /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  101. /* _______________________________________________ */
  102. /* BUSY __/ \_ */
  103. /* */
  104. /* (*) The STOP condition is only sent by the master at the end of the last */
  105. /* I2C message or if the I2C_M_STOP flag is set. Similarly, the BUSY bit is */
  106. /* only cleared after the STOP condition, so, between messages we have to */
  107. /* poll for the DTE bit. */
  108. /* */
  109. enum sh_mobile_i2c_op {
  110. OP_START = 0,
  111. OP_TX_FIRST,
  112. OP_TX,
  113. OP_TX_STOP,
  114. OP_TX_STOP_DATA,
  115. OP_TX_TO_RX,
  116. OP_RX,
  117. OP_RX_STOP,
  118. OP_RX_STOP_DATA,
  119. };
  120. struct sh_mobile_i2c_data {
  121. struct device *dev;
  122. void __iomem *reg;
  123. struct i2c_adapter adap;
  124. unsigned long bus_speed;
  125. unsigned int clks_per_count;
  126. struct clk *clk;
  127. u_int8_t icic;
  128. u_int8_t flags;
  129. u_int16_t iccl;
  130. u_int16_t icch;
  131. spinlock_t lock;
  132. wait_queue_head_t wait;
  133. struct i2c_msg *msg;
  134. int pos;
  135. int sr;
  136. bool send_stop;
  137. bool stop_after_dma;
  138. struct resource *res;
  139. struct dma_chan *dma_tx;
  140. struct dma_chan *dma_rx;
  141. struct scatterlist sg;
  142. enum dma_data_direction dma_direction;
  143. };
  144. struct sh_mobile_dt_config {
  145. int clks_per_count;
  146. void (*setup)(struct sh_mobile_i2c_data *pd);
  147. };
  148. #define IIC_FLAG_HAS_ICIC67 (1 << 0)
  149. #define STANDARD_MODE 100000
  150. #define FAST_MODE 400000
  151. /* Register offsets */
  152. #define ICDR 0x00
  153. #define ICCR 0x04
  154. #define ICSR 0x08
  155. #define ICIC 0x0c
  156. #define ICCL 0x10
  157. #define ICCH 0x14
  158. #define ICSTART 0x70
  159. /* Register bits */
  160. #define ICCR_ICE 0x80
  161. #define ICCR_RACK 0x40
  162. #define ICCR_TRS 0x10
  163. #define ICCR_BBSY 0x04
  164. #define ICCR_SCP 0x01
  165. #define ICSR_SCLM 0x80
  166. #define ICSR_SDAM 0x40
  167. #define SW_DONE 0x20
  168. #define ICSR_BUSY 0x10
  169. #define ICSR_AL 0x08
  170. #define ICSR_TACK 0x04
  171. #define ICSR_WAIT 0x02
  172. #define ICSR_DTE 0x01
  173. #define ICIC_ICCLB8 0x80
  174. #define ICIC_ICCHB8 0x40
  175. #define ICIC_TDMAE 0x20
  176. #define ICIC_RDMAE 0x10
  177. #define ICIC_ALE 0x08
  178. #define ICIC_TACKE 0x04
  179. #define ICIC_WAITE 0x02
  180. #define ICIC_DTEE 0x01
  181. #define ICSTART_ICSTART 0x10
  182. static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
  183. {
  184. if (offs == ICIC)
  185. data |= pd->icic;
  186. iowrite8(data, pd->reg + offs);
  187. }
  188. static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
  189. {
  190. return ioread8(pd->reg + offs);
  191. }
  192. static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
  193. unsigned char set, unsigned char clr)
  194. {
  195. iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
  196. }
  197. static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf)
  198. {
  199. /*
  200. * Conditional expression:
  201. * ICCL >= COUNT_CLK * (tLOW + tf)
  202. *
  203. * SH-Mobile IIC hardware starts counting the LOW period of
  204. * the SCL signal (tLOW) as soon as it pulls the SCL line.
  205. * In order to meet the tLOW timing spec, we need to take into
  206. * account the fall time of SCL signal (tf). Default tf value
  207. * should be 0.3 us, for safety.
  208. */
  209. return (((count_khz * (tLOW + tf)) + 5000) / 10000);
  210. }
  211. static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf)
  212. {
  213. /*
  214. * Conditional expression:
  215. * ICCH >= COUNT_CLK * (tHIGH + tf)
  216. *
  217. * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
  218. * and can ignore it. SH-Mobile IIC controller starts counting
  219. * the HIGH period of the SCL signal (tHIGH) after the SCL input
  220. * voltage increases at VIH.
  221. *
  222. * Afterward it turned out calculating ICCH using only tHIGH spec
  223. * will result in violation of the tHD;STA timing spec. We need
  224. * to take into account the fall time of SDA signal (tf) at START
  225. * condition, in order to meet both tHIGH and tHD;STA specs.
  226. */
  227. return (((count_khz * (tHIGH + tf)) + 5000) / 10000);
  228. }
  229. static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
  230. {
  231. unsigned long i2c_clk_khz;
  232. u32 tHIGH, tLOW, tf;
  233. uint16_t max_val;
  234. /* Get clock rate after clock is enabled */
  235. clk_prepare_enable(pd->clk);
  236. i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
  237. clk_disable_unprepare(pd->clk);
  238. i2c_clk_khz /= pd->clks_per_count;
  239. if (pd->bus_speed == STANDARD_MODE) {
  240. tLOW = 47; /* tLOW = 4.7 us */
  241. tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
  242. tf = 3; /* tf = 0.3 us */
  243. } else if (pd->bus_speed == FAST_MODE) {
  244. tLOW = 13; /* tLOW = 1.3 us */
  245. tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
  246. tf = 3; /* tf = 0.3 us */
  247. } else {
  248. dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
  249. pd->bus_speed);
  250. return -EINVAL;
  251. }
  252. pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf);
  253. pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf);
  254. max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff;
  255. if (pd->iccl > max_val || pd->icch > max_val) {
  256. dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n",
  257. pd->iccl, pd->icch);
  258. return -EINVAL;
  259. }
  260. /* one more bit of ICCL in ICIC */
  261. if (pd->iccl & 0x100)
  262. pd->icic |= ICIC_ICCLB8;
  263. else
  264. pd->icic &= ~ICIC_ICCLB8;
  265. /* one more bit of ICCH in ICIC */
  266. if (pd->icch & 0x100)
  267. pd->icic |= ICIC_ICCHB8;
  268. else
  269. pd->icic &= ~ICIC_ICCHB8;
  270. dev_dbg(pd->dev, "timing values: L/H=0x%x/0x%x\n", pd->iccl, pd->icch);
  271. return 0;
  272. }
  273. static void activate_ch(struct sh_mobile_i2c_data *pd)
  274. {
  275. /* Wake up device and enable clock */
  276. pm_runtime_get_sync(pd->dev);
  277. clk_prepare_enable(pd->clk);
  278. /* Enable channel and configure rx ack */
  279. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  280. /* Mask all interrupts */
  281. iic_wr(pd, ICIC, 0);
  282. /* Set the clock */
  283. iic_wr(pd, ICCL, pd->iccl & 0xff);
  284. iic_wr(pd, ICCH, pd->icch & 0xff);
  285. }
  286. static void deactivate_ch(struct sh_mobile_i2c_data *pd)
  287. {
  288. /* Clear/disable interrupts */
  289. iic_wr(pd, ICSR, 0);
  290. iic_wr(pd, ICIC, 0);
  291. /* Disable channel */
  292. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  293. /* Disable clock and mark device as idle */
  294. clk_disable_unprepare(pd->clk);
  295. pm_runtime_put_sync(pd->dev);
  296. }
  297. static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
  298. enum sh_mobile_i2c_op op, unsigned char data)
  299. {
  300. unsigned char ret = 0;
  301. unsigned long flags;
  302. dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
  303. spin_lock_irqsave(&pd->lock, flags);
  304. switch (op) {
  305. case OP_START: /* issue start and trigger DTE interrupt */
  306. iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY);
  307. break;
  308. case OP_TX_FIRST: /* disable DTE interrupt and write data */
  309. iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  310. iic_wr(pd, ICDR, data);
  311. break;
  312. case OP_TX: /* write data */
  313. iic_wr(pd, ICDR, data);
  314. break;
  315. case OP_TX_STOP_DATA: /* write data and issue a stop afterwards */
  316. iic_wr(pd, ICDR, data);
  317. /* fallthrough */
  318. case OP_TX_STOP: /* issue a stop */
  319. iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS
  320. : ICCR_ICE | ICCR_TRS | ICCR_BBSY);
  321. break;
  322. case OP_TX_TO_RX: /* select read mode */
  323. iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP);
  324. break;
  325. case OP_RX: /* just read data */
  326. ret = iic_rd(pd, ICDR);
  327. break;
  328. case OP_RX_STOP: /* enable DTE interrupt, issue stop */
  329. iic_wr(pd, ICIC,
  330. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  331. iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
  332. break;
  333. case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
  334. iic_wr(pd, ICIC,
  335. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  336. ret = iic_rd(pd, ICDR);
  337. iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
  338. break;
  339. }
  340. spin_unlock_irqrestore(&pd->lock, flags);
  341. dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
  342. return ret;
  343. }
  344. static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
  345. {
  346. return pd->pos == -1;
  347. }
  348. static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
  349. {
  350. return pd->pos == pd->msg->len - 1;
  351. }
  352. static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
  353. unsigned char *buf)
  354. {
  355. switch (pd->pos) {
  356. case -1:
  357. *buf = i2c_8bit_addr_from_msg(pd->msg);
  358. break;
  359. default:
  360. *buf = pd->msg->buf[pd->pos];
  361. }
  362. }
  363. static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
  364. {
  365. unsigned char data;
  366. if (pd->pos == pd->msg->len) {
  367. /* Send stop if we haven't yet (DMA case) */
  368. if (pd->send_stop && pd->stop_after_dma)
  369. i2c_op(pd, OP_TX_STOP, 0);
  370. return 1;
  371. }
  372. sh_mobile_i2c_get_data(pd, &data);
  373. if (sh_mobile_i2c_is_last_byte(pd))
  374. i2c_op(pd, OP_TX_STOP_DATA, data);
  375. else if (sh_mobile_i2c_is_first_byte(pd))
  376. i2c_op(pd, OP_TX_FIRST, data);
  377. else
  378. i2c_op(pd, OP_TX, data);
  379. pd->pos++;
  380. return 0;
  381. }
  382. static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
  383. {
  384. unsigned char data;
  385. int real_pos;
  386. do {
  387. if (pd->pos <= -1) {
  388. sh_mobile_i2c_get_data(pd, &data);
  389. if (sh_mobile_i2c_is_first_byte(pd))
  390. i2c_op(pd, OP_TX_FIRST, data);
  391. else
  392. i2c_op(pd, OP_TX, data);
  393. break;
  394. }
  395. if (pd->pos == 0) {
  396. i2c_op(pd, OP_TX_TO_RX, 0);
  397. break;
  398. }
  399. real_pos = pd->pos - 2;
  400. if (pd->pos == pd->msg->len) {
  401. if (pd->stop_after_dma) {
  402. /* Simulate PIO end condition after DMA transfer */
  403. i2c_op(pd, OP_RX_STOP, 0);
  404. pd->pos++;
  405. break;
  406. }
  407. if (real_pos < 0) {
  408. i2c_op(pd, OP_RX_STOP, 0);
  409. break;
  410. }
  411. data = i2c_op(pd, OP_RX_STOP_DATA, 0);
  412. } else
  413. data = i2c_op(pd, OP_RX, 0);
  414. if (real_pos >= 0)
  415. pd->msg->buf[real_pos] = data;
  416. } while (0);
  417. pd->pos++;
  418. return pd->pos == (pd->msg->len + 2);
  419. }
  420. static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
  421. {
  422. struct sh_mobile_i2c_data *pd = dev_id;
  423. unsigned char sr;
  424. int wakeup = 0;
  425. sr = iic_rd(pd, ICSR);
  426. pd->sr |= sr; /* remember state */
  427. dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
  428. (pd->msg->flags & I2C_M_RD) ? "read" : "write",
  429. pd->pos, pd->msg->len);
  430. /* Kick off TxDMA after preface was done */
  431. if (pd->dma_direction == DMA_TO_DEVICE && pd->pos == 0)
  432. iic_set_clr(pd, ICIC, ICIC_TDMAE, 0);
  433. else if (sr & (ICSR_AL | ICSR_TACK))
  434. /* don't interrupt transaction - continue to issue stop */
  435. iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
  436. else if (pd->msg->flags & I2C_M_RD)
  437. wakeup = sh_mobile_i2c_isr_rx(pd);
  438. else
  439. wakeup = sh_mobile_i2c_isr_tx(pd);
  440. /* Kick off RxDMA after preface was done */
  441. if (pd->dma_direction == DMA_FROM_DEVICE && pd->pos == 1)
  442. iic_set_clr(pd, ICIC, ICIC_RDMAE, 0);
  443. if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
  444. iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
  445. if (wakeup) {
  446. pd->sr |= SW_DONE;
  447. wake_up(&pd->wait);
  448. }
  449. /* defeat write posting to avoid spurious WAIT interrupts */
  450. iic_rd(pd, ICSR);
  451. return IRQ_HANDLED;
  452. }
  453. static void sh_mobile_i2c_dma_unmap(struct sh_mobile_i2c_data *pd)
  454. {
  455. struct dma_chan *chan = pd->dma_direction == DMA_FROM_DEVICE
  456. ? pd->dma_rx : pd->dma_tx;
  457. dma_unmap_single(chan->device->dev, sg_dma_address(&pd->sg),
  458. pd->msg->len, pd->dma_direction);
  459. pd->dma_direction = DMA_NONE;
  460. }
  461. static void sh_mobile_i2c_cleanup_dma(struct sh_mobile_i2c_data *pd)
  462. {
  463. if (pd->dma_direction == DMA_NONE)
  464. return;
  465. else if (pd->dma_direction == DMA_FROM_DEVICE)
  466. dmaengine_terminate_all(pd->dma_rx);
  467. else if (pd->dma_direction == DMA_TO_DEVICE)
  468. dmaengine_terminate_all(pd->dma_tx);
  469. sh_mobile_i2c_dma_unmap(pd);
  470. }
  471. static void sh_mobile_i2c_dma_callback(void *data)
  472. {
  473. struct sh_mobile_i2c_data *pd = data;
  474. sh_mobile_i2c_dma_unmap(pd);
  475. pd->pos = pd->msg->len;
  476. pd->stop_after_dma = true;
  477. iic_set_clr(pd, ICIC, 0, ICIC_TDMAE | ICIC_RDMAE);
  478. }
  479. static struct dma_chan *sh_mobile_i2c_request_dma_chan(struct device *dev,
  480. enum dma_transfer_direction dir, dma_addr_t port_addr)
  481. {
  482. struct dma_chan *chan;
  483. struct dma_slave_config cfg;
  484. char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
  485. int ret;
  486. chan = dma_request_slave_channel_reason(dev, chan_name);
  487. if (IS_ERR(chan)) {
  488. ret = PTR_ERR(chan);
  489. dev_dbg(dev, "request_channel failed for %s (%d)\n", chan_name, ret);
  490. return chan;
  491. }
  492. memset(&cfg, 0, sizeof(cfg));
  493. cfg.direction = dir;
  494. if (dir == DMA_MEM_TO_DEV) {
  495. cfg.dst_addr = port_addr;
  496. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  497. } else {
  498. cfg.src_addr = port_addr;
  499. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  500. }
  501. ret = dmaengine_slave_config(chan, &cfg);
  502. if (ret) {
  503. dev_dbg(dev, "slave_config failed for %s (%d)\n", chan_name, ret);
  504. dma_release_channel(chan);
  505. return ERR_PTR(ret);
  506. }
  507. dev_dbg(dev, "got DMA channel for %s\n", chan_name);
  508. return chan;
  509. }
  510. static void sh_mobile_i2c_xfer_dma(struct sh_mobile_i2c_data *pd)
  511. {
  512. bool read = pd->msg->flags & I2C_M_RD;
  513. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  514. struct dma_chan *chan = read ? pd->dma_rx : pd->dma_tx;
  515. struct dma_async_tx_descriptor *txdesc;
  516. dma_addr_t dma_addr;
  517. dma_cookie_t cookie;
  518. if (PTR_ERR(chan) == -EPROBE_DEFER) {
  519. if (read)
  520. chan = pd->dma_rx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_DEV_TO_MEM,
  521. pd->res->start + ICDR);
  522. else
  523. chan = pd->dma_tx = sh_mobile_i2c_request_dma_chan(pd->dev, DMA_MEM_TO_DEV,
  524. pd->res->start + ICDR);
  525. }
  526. if (IS_ERR(chan))
  527. return;
  528. dma_addr = dma_map_single(chan->device->dev, pd->msg->buf, pd->msg->len, dir);
  529. if (dma_mapping_error(chan->device->dev, dma_addr)) {
  530. dev_dbg(pd->dev, "dma map failed, using PIO\n");
  531. return;
  532. }
  533. sg_dma_len(&pd->sg) = pd->msg->len;
  534. sg_dma_address(&pd->sg) = dma_addr;
  535. pd->dma_direction = dir;
  536. txdesc = dmaengine_prep_slave_sg(chan, &pd->sg, 1,
  537. read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
  538. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  539. if (!txdesc) {
  540. dev_dbg(pd->dev, "dma prep slave sg failed, using PIO\n");
  541. sh_mobile_i2c_cleanup_dma(pd);
  542. return;
  543. }
  544. txdesc->callback = sh_mobile_i2c_dma_callback;
  545. txdesc->callback_param = pd;
  546. cookie = dmaengine_submit(txdesc);
  547. if (dma_submit_error(cookie)) {
  548. dev_dbg(pd->dev, "submitting dma failed, using PIO\n");
  549. sh_mobile_i2c_cleanup_dma(pd);
  550. return;
  551. }
  552. dma_async_issue_pending(chan);
  553. }
  554. static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
  555. bool do_init)
  556. {
  557. if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
  558. dev_err(pd->dev, "Unsupported zero length i2c read\n");
  559. return -EOPNOTSUPP;
  560. }
  561. if (do_init) {
  562. /* Initialize channel registers */
  563. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  564. /* Enable channel and configure rx ack */
  565. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  566. /* Set the clock */
  567. iic_wr(pd, ICCL, pd->iccl & 0xff);
  568. iic_wr(pd, ICCH, pd->icch & 0xff);
  569. }
  570. pd->msg = usr_msg;
  571. pd->pos = -1;
  572. pd->sr = 0;
  573. if (pd->msg->len > 8)
  574. sh_mobile_i2c_xfer_dma(pd);
  575. /* Enable all interrupts to begin with */
  576. iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  577. return 0;
  578. }
  579. static int poll_dte(struct sh_mobile_i2c_data *pd)
  580. {
  581. int i;
  582. for (i = 1000; i; i--) {
  583. u_int8_t val = iic_rd(pd, ICSR);
  584. if (val & ICSR_DTE)
  585. break;
  586. if (val & ICSR_TACK)
  587. return -ENXIO;
  588. udelay(10);
  589. }
  590. return i ? 0 : -ETIMEDOUT;
  591. }
  592. static int poll_busy(struct sh_mobile_i2c_data *pd)
  593. {
  594. int i;
  595. for (i = 1000; i; i--) {
  596. u_int8_t val = iic_rd(pd, ICSR);
  597. dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
  598. /* the interrupt handler may wake us up before the
  599. * transfer is finished, so poll the hardware
  600. * until we're done.
  601. */
  602. if (!(val & ICSR_BUSY)) {
  603. /* handle missing acknowledge and arbitration lost */
  604. val |= pd->sr;
  605. if (val & ICSR_TACK)
  606. return -ENXIO;
  607. if (val & ICSR_AL)
  608. return -EAGAIN;
  609. break;
  610. }
  611. udelay(10);
  612. }
  613. return i ? 0 : -ETIMEDOUT;
  614. }
  615. static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
  616. struct i2c_msg *msgs,
  617. int num)
  618. {
  619. struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
  620. struct i2c_msg *msg;
  621. int err = 0;
  622. int i;
  623. long timeout;
  624. activate_ch(pd);
  625. /* Process all messages */
  626. for (i = 0; i < num; i++) {
  627. bool do_start = pd->send_stop || !i;
  628. msg = &msgs[i];
  629. pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP;
  630. pd->stop_after_dma = false;
  631. err = start_ch(pd, msg, do_start);
  632. if (err)
  633. break;
  634. if (do_start)
  635. i2c_op(pd, OP_START, 0);
  636. /* The interrupt handler takes care of the rest... */
  637. timeout = wait_event_timeout(pd->wait,
  638. pd->sr & (ICSR_TACK | SW_DONE),
  639. adapter->timeout);
  640. if (!timeout) {
  641. dev_err(pd->dev, "Transfer request timed out\n");
  642. if (pd->dma_direction != DMA_NONE)
  643. sh_mobile_i2c_cleanup_dma(pd);
  644. err = -ETIMEDOUT;
  645. break;
  646. }
  647. if (pd->send_stop)
  648. err = poll_busy(pd);
  649. else
  650. err = poll_dte(pd);
  651. if (err < 0)
  652. break;
  653. }
  654. deactivate_ch(pd);
  655. if (!err)
  656. err = num;
  657. return err;
  658. }
  659. static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
  660. {
  661. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  662. }
  663. static struct i2c_algorithm sh_mobile_i2c_algorithm = {
  664. .functionality = sh_mobile_i2c_func,
  665. .master_xfer = sh_mobile_i2c_xfer,
  666. };
  667. /*
  668. * r8a7740 chip has lasting errata on I2C I/O pad reset.
  669. * this is work-around for it.
  670. */
  671. static void sh_mobile_i2c_r8a7740_workaround(struct sh_mobile_i2c_data *pd)
  672. {
  673. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  674. iic_rd(pd, ICCR); /* dummy read */
  675. iic_set_clr(pd, ICSTART, ICSTART_ICSTART, 0);
  676. iic_rd(pd, ICSTART); /* dummy read */
  677. udelay(10);
  678. iic_wr(pd, ICCR, ICCR_SCP);
  679. iic_wr(pd, ICSTART, 0);
  680. udelay(10);
  681. iic_wr(pd, ICCR, ICCR_TRS);
  682. udelay(10);
  683. iic_wr(pd, ICCR, 0);
  684. udelay(10);
  685. iic_wr(pd, ICCR, ICCR_TRS);
  686. udelay(10);
  687. }
  688. static const struct sh_mobile_dt_config default_dt_config = {
  689. .clks_per_count = 1,
  690. };
  691. static const struct sh_mobile_dt_config fast_clock_dt_config = {
  692. .clks_per_count = 2,
  693. };
  694. static const struct sh_mobile_dt_config r8a7740_dt_config = {
  695. .clks_per_count = 1,
  696. .setup = sh_mobile_i2c_r8a7740_workaround,
  697. };
  698. static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
  699. { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
  700. { .compatible = "renesas,iic-r8a73a4", .data = &fast_clock_dt_config },
  701. { .compatible = "renesas,iic-r8a7740", .data = &r8a7740_dt_config },
  702. { .compatible = "renesas,iic-r8a7790", .data = &fast_clock_dt_config },
  703. { .compatible = "renesas,iic-r8a7791", .data = &fast_clock_dt_config },
  704. { .compatible = "renesas,iic-r8a7792", .data = &fast_clock_dt_config },
  705. { .compatible = "renesas,iic-r8a7793", .data = &fast_clock_dt_config },
  706. { .compatible = "renesas,iic-r8a7794", .data = &fast_clock_dt_config },
  707. { .compatible = "renesas,iic-r8a7795", .data = &fast_clock_dt_config },
  708. { .compatible = "renesas,iic-sh73a0", .data = &fast_clock_dt_config },
  709. {},
  710. };
  711. MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
  712. static void sh_mobile_i2c_release_dma(struct sh_mobile_i2c_data *pd)
  713. {
  714. if (!IS_ERR(pd->dma_tx)) {
  715. dma_release_channel(pd->dma_tx);
  716. pd->dma_tx = ERR_PTR(-EPROBE_DEFER);
  717. }
  718. if (!IS_ERR(pd->dma_rx)) {
  719. dma_release_channel(pd->dma_rx);
  720. pd->dma_rx = ERR_PTR(-EPROBE_DEFER);
  721. }
  722. }
  723. static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, struct sh_mobile_i2c_data *pd)
  724. {
  725. struct resource *res;
  726. resource_size_t n;
  727. int k = 0, ret;
  728. while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
  729. for (n = res->start; n <= res->end; n++) {
  730. ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr,
  731. 0, dev_name(&dev->dev), pd);
  732. if (ret) {
  733. dev_err(&dev->dev, "cannot request IRQ %pa\n", &n);
  734. return ret;
  735. }
  736. }
  737. k++;
  738. }
  739. return k > 0 ? 0 : -ENOENT;
  740. }
  741. static int sh_mobile_i2c_probe(struct platform_device *dev)
  742. {
  743. struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev);
  744. struct sh_mobile_i2c_data *pd;
  745. struct i2c_adapter *adap;
  746. struct resource *res;
  747. int ret;
  748. u32 bus_speed;
  749. pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
  750. if (!pd)
  751. return -ENOMEM;
  752. pd->clk = devm_clk_get(&dev->dev, NULL);
  753. if (IS_ERR(pd->clk)) {
  754. dev_err(&dev->dev, "cannot get clock\n");
  755. return PTR_ERR(pd->clk);
  756. }
  757. ret = sh_mobile_i2c_hook_irqs(dev, pd);
  758. if (ret)
  759. return ret;
  760. pd->dev = &dev->dev;
  761. platform_set_drvdata(dev, pd);
  762. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  763. pd->res = res;
  764. pd->reg = devm_ioremap_resource(&dev->dev, res);
  765. if (IS_ERR(pd->reg))
  766. return PTR_ERR(pd->reg);
  767. /* Use platform data bus speed or STANDARD_MODE */
  768. ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed);
  769. pd->bus_speed = ret ? STANDARD_MODE : bus_speed;
  770. pd->clks_per_count = 1;
  771. if (dev->dev.of_node) {
  772. const struct of_device_id *match;
  773. match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev);
  774. if (match) {
  775. const struct sh_mobile_dt_config *config;
  776. config = match->data;
  777. pd->clks_per_count = config->clks_per_count;
  778. if (config->setup)
  779. config->setup(pd);
  780. }
  781. } else {
  782. if (pdata && pdata->bus_speed)
  783. pd->bus_speed = pdata->bus_speed;
  784. if (pdata && pdata->clks_per_count)
  785. pd->clks_per_count = pdata->clks_per_count;
  786. }
  787. /* The IIC blocks on SH-Mobile ARM processors
  788. * come with two new bits in ICIC.
  789. */
  790. if (resource_size(res) > 0x17)
  791. pd->flags |= IIC_FLAG_HAS_ICIC67;
  792. ret = sh_mobile_i2c_init(pd);
  793. if (ret)
  794. return ret;
  795. /* Init DMA */
  796. sg_init_table(&pd->sg, 1);
  797. pd->dma_direction = DMA_NONE;
  798. pd->dma_rx = pd->dma_tx = ERR_PTR(-EPROBE_DEFER);
  799. /* Enable Runtime PM for this device.
  800. *
  801. * Also tell the Runtime PM core to ignore children
  802. * for this device since it is valid for us to suspend
  803. * this I2C master driver even though the slave devices
  804. * on the I2C bus may not be suspended.
  805. *
  806. * The state of the I2C hardware bus is unaffected by
  807. * the Runtime PM state.
  808. */
  809. pm_suspend_ignore_children(&dev->dev, true);
  810. pm_runtime_enable(&dev->dev);
  811. /* setup the private data */
  812. adap = &pd->adap;
  813. i2c_set_adapdata(adap, pd);
  814. adap->owner = THIS_MODULE;
  815. adap->algo = &sh_mobile_i2c_algorithm;
  816. adap->dev.parent = &dev->dev;
  817. adap->retries = 5;
  818. adap->nr = dev->id;
  819. adap->dev.of_node = dev->dev.of_node;
  820. strlcpy(adap->name, dev->name, sizeof(adap->name));
  821. spin_lock_init(&pd->lock);
  822. init_waitqueue_head(&pd->wait);
  823. ret = i2c_add_numbered_adapter(adap);
  824. if (ret < 0) {
  825. sh_mobile_i2c_release_dma(pd);
  826. return ret;
  827. }
  828. dev_info(&dev->dev, "I2C adapter %d, bus speed %lu Hz\n", adap->nr, pd->bus_speed);
  829. return 0;
  830. }
  831. static int sh_mobile_i2c_remove(struct platform_device *dev)
  832. {
  833. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  834. i2c_del_adapter(&pd->adap);
  835. sh_mobile_i2c_release_dma(pd);
  836. pm_runtime_disable(&dev->dev);
  837. return 0;
  838. }
  839. static int sh_mobile_i2c_runtime_nop(struct device *dev)
  840. {
  841. /* Runtime PM callback shared between ->runtime_suspend()
  842. * and ->runtime_resume(). Simply returns success.
  843. *
  844. * This driver re-initializes all registers after
  845. * pm_runtime_get_sync() anyway so there is no need
  846. * to save and restore registers here.
  847. */
  848. return 0;
  849. }
  850. static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
  851. .runtime_suspend = sh_mobile_i2c_runtime_nop,
  852. .runtime_resume = sh_mobile_i2c_runtime_nop,
  853. };
  854. static struct platform_driver sh_mobile_i2c_driver = {
  855. .driver = {
  856. .name = "i2c-sh_mobile",
  857. .pm = &sh_mobile_i2c_dev_pm_ops,
  858. .of_match_table = sh_mobile_i2c_dt_ids,
  859. },
  860. .probe = sh_mobile_i2c_probe,
  861. .remove = sh_mobile_i2c_remove,
  862. };
  863. static int __init sh_mobile_i2c_adap_init(void)
  864. {
  865. return platform_driver_register(&sh_mobile_i2c_driver);
  866. }
  867. subsys_initcall(sh_mobile_i2c_adap_init);
  868. static void __exit sh_mobile_i2c_adap_exit(void)
  869. {
  870. platform_driver_unregister(&sh_mobile_i2c_driver);
  871. }
  872. module_exit(sh_mobile_i2c_adap_exit);
  873. MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
  874. MODULE_AUTHOR("Magnus Damm and Wolfram Sang");
  875. MODULE_LICENSE("GPL v2");
  876. MODULE_ALIAS("platform:i2c-sh_mobile");