i2c-sh_mobile.c 28 KB

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