i2c-sh_mobile.c 29 KB

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