i2c-sh_mobile.c 29 KB

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