i2c-sh_mobile.c 28 KB

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