i2c-sh_mobile.c 28 KB

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