i2c-sh_mobile.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * SuperH Mobile I2C Controller
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Portions of the code based on out-of-tree driver i2c-sh7343.c
  7. * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/i2c.h>
  29. #include <linux/err.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/clk.h>
  32. #include <linux/io.h>
  33. #include <linux/slab.h>
  34. #include <linux/of_device.h>
  35. #include <linux/i2c/i2c-sh_mobile.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_TO_RX,
  115. OP_RX,
  116. OP_RX_STOP,
  117. OP_RX_STOP_DATA,
  118. };
  119. struct sh_mobile_i2c_data {
  120. struct device *dev;
  121. void __iomem *reg;
  122. struct i2c_adapter adap;
  123. unsigned long bus_speed;
  124. unsigned int clks_per_count;
  125. struct clk *clk;
  126. u_int8_t icic;
  127. u_int8_t flags;
  128. u_int16_t iccl;
  129. u_int16_t icch;
  130. spinlock_t lock;
  131. wait_queue_head_t wait;
  132. struct i2c_msg *msg;
  133. int pos;
  134. int sr;
  135. bool send_stop;
  136. };
  137. struct sh_mobile_dt_config {
  138. int clks_per_count;
  139. };
  140. #define IIC_FLAG_HAS_ICIC67 (1 << 0)
  141. #define STANDARD_MODE 100000
  142. #define FAST_MODE 400000
  143. /* Register offsets */
  144. #define ICDR 0x00
  145. #define ICCR 0x04
  146. #define ICSR 0x08
  147. #define ICIC 0x0c
  148. #define ICCL 0x10
  149. #define ICCH 0x14
  150. /* Register bits */
  151. #define ICCR_ICE 0x80
  152. #define ICCR_RACK 0x40
  153. #define ICCR_TRS 0x10
  154. #define ICCR_BBSY 0x04
  155. #define ICCR_SCP 0x01
  156. #define ICSR_SCLM 0x80
  157. #define ICSR_SDAM 0x40
  158. #define SW_DONE 0x20
  159. #define ICSR_BUSY 0x10
  160. #define ICSR_AL 0x08
  161. #define ICSR_TACK 0x04
  162. #define ICSR_WAIT 0x02
  163. #define ICSR_DTE 0x01
  164. #define ICIC_ICCLB8 0x80
  165. #define ICIC_ICCHB8 0x40
  166. #define ICIC_ALE 0x08
  167. #define ICIC_TACKE 0x04
  168. #define ICIC_WAITE 0x02
  169. #define ICIC_DTEE 0x01
  170. static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
  171. {
  172. if (offs == ICIC)
  173. data |= pd->icic;
  174. iowrite8(data, pd->reg + offs);
  175. }
  176. static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
  177. {
  178. return ioread8(pd->reg + offs);
  179. }
  180. static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
  181. unsigned char set, unsigned char clr)
  182. {
  183. iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
  184. }
  185. static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf)
  186. {
  187. /*
  188. * Conditional expression:
  189. * ICCL >= COUNT_CLK * (tLOW + tf)
  190. *
  191. * SH-Mobile IIC hardware starts counting the LOW period of
  192. * the SCL signal (tLOW) as soon as it pulls the SCL line.
  193. * In order to meet the tLOW timing spec, we need to take into
  194. * account the fall time of SCL signal (tf). Default tf value
  195. * should be 0.3 us, for safety.
  196. */
  197. return (((count_khz * (tLOW + tf)) + 5000) / 10000);
  198. }
  199. static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf)
  200. {
  201. /*
  202. * Conditional expression:
  203. * ICCH >= COUNT_CLK * (tHIGH + tf)
  204. *
  205. * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
  206. * and can ignore it. SH-Mobile IIC controller starts counting
  207. * the HIGH period of the SCL signal (tHIGH) after the SCL input
  208. * voltage increases at VIH.
  209. *
  210. * Afterward it turned out calculating ICCH using only tHIGH spec
  211. * will result in violation of the tHD;STA timing spec. We need
  212. * to take into account the fall time of SDA signal (tf) at START
  213. * condition, in order to meet both tHIGH and tHD;STA specs.
  214. */
  215. return (((count_khz * (tHIGH + tf)) + 5000) / 10000);
  216. }
  217. static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
  218. {
  219. unsigned long i2c_clk_khz;
  220. u32 tHIGH, tLOW, tf;
  221. uint16_t max_val;
  222. /* Get clock rate after clock is enabled */
  223. clk_prepare_enable(pd->clk);
  224. i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
  225. clk_disable_unprepare(pd->clk);
  226. i2c_clk_khz /= pd->clks_per_count;
  227. if (pd->bus_speed == STANDARD_MODE) {
  228. tLOW = 47; /* tLOW = 4.7 us */
  229. tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
  230. tf = 3; /* tf = 0.3 us */
  231. } else if (pd->bus_speed == FAST_MODE) {
  232. tLOW = 13; /* tLOW = 1.3 us */
  233. tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
  234. tf = 3; /* tf = 0.3 us */
  235. } else {
  236. dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
  237. pd->bus_speed);
  238. return -EINVAL;
  239. }
  240. pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf);
  241. pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf);
  242. max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff;
  243. if (pd->iccl > max_val || pd->icch > max_val) {
  244. dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n",
  245. pd->iccl, pd->icch);
  246. return -EINVAL;
  247. }
  248. /* one more bit of ICCL in ICIC */
  249. if (pd->iccl & 0x100)
  250. pd->icic |= ICIC_ICCLB8;
  251. else
  252. pd->icic &= ~ICIC_ICCLB8;
  253. /* one more bit of ICCH in ICIC */
  254. if (pd->icch & 0x100)
  255. pd->icic |= ICIC_ICCHB8;
  256. else
  257. pd->icic &= ~ICIC_ICCHB8;
  258. return 0;
  259. }
  260. static void activate_ch(struct sh_mobile_i2c_data *pd)
  261. {
  262. /* Wake up device and enable clock */
  263. pm_runtime_get_sync(pd->dev);
  264. clk_prepare_enable(pd->clk);
  265. /* Enable channel and configure rx ack */
  266. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  267. /* Mask all interrupts */
  268. iic_wr(pd, ICIC, 0);
  269. /* Set the clock */
  270. iic_wr(pd, ICCL, pd->iccl & 0xff);
  271. iic_wr(pd, ICCH, pd->icch & 0xff);
  272. }
  273. static void deactivate_ch(struct sh_mobile_i2c_data *pd)
  274. {
  275. /* Clear/disable interrupts */
  276. iic_wr(pd, ICSR, 0);
  277. iic_wr(pd, ICIC, 0);
  278. /* Disable channel */
  279. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  280. /* Disable clock and mark device as idle */
  281. clk_disable_unprepare(pd->clk);
  282. pm_runtime_put_sync(pd->dev);
  283. }
  284. static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
  285. enum sh_mobile_i2c_op op, unsigned char data)
  286. {
  287. unsigned char ret = 0;
  288. unsigned long flags;
  289. dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
  290. spin_lock_irqsave(&pd->lock, flags);
  291. switch (op) {
  292. case OP_START: /* issue start and trigger DTE interrupt */
  293. iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY);
  294. break;
  295. case OP_TX_FIRST: /* disable DTE interrupt and write data */
  296. iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  297. iic_wr(pd, ICDR, data);
  298. break;
  299. case OP_TX: /* write data */
  300. iic_wr(pd, ICDR, data);
  301. break;
  302. case OP_TX_STOP: /* write data and issue a stop afterwards */
  303. iic_wr(pd, ICDR, data);
  304. iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS
  305. : ICCR_ICE | ICCR_TRS | ICCR_BBSY);
  306. break;
  307. case OP_TX_TO_RX: /* select read mode */
  308. iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP);
  309. break;
  310. case OP_RX: /* just read data */
  311. ret = iic_rd(pd, ICDR);
  312. break;
  313. case OP_RX_STOP: /* enable DTE interrupt, issue stop */
  314. iic_wr(pd, ICIC,
  315. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  316. iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
  317. break;
  318. case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
  319. iic_wr(pd, ICIC,
  320. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  321. ret = iic_rd(pd, ICDR);
  322. iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK);
  323. break;
  324. }
  325. spin_unlock_irqrestore(&pd->lock, flags);
  326. dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
  327. return ret;
  328. }
  329. static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
  330. {
  331. return pd->pos == -1;
  332. }
  333. static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
  334. {
  335. return pd->pos == pd->msg->len - 1;
  336. }
  337. static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
  338. unsigned char *buf)
  339. {
  340. switch (pd->pos) {
  341. case -1:
  342. *buf = (pd->msg->addr & 0x7f) << 1;
  343. *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
  344. break;
  345. default:
  346. *buf = pd->msg->buf[pd->pos];
  347. }
  348. }
  349. static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
  350. {
  351. unsigned char data;
  352. if (pd->pos == pd->msg->len)
  353. return 1;
  354. sh_mobile_i2c_get_data(pd, &data);
  355. if (sh_mobile_i2c_is_last_byte(pd))
  356. i2c_op(pd, OP_TX_STOP, data);
  357. else if (sh_mobile_i2c_is_first_byte(pd))
  358. i2c_op(pd, OP_TX_FIRST, data);
  359. else
  360. i2c_op(pd, OP_TX, data);
  361. pd->pos++;
  362. return 0;
  363. }
  364. static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
  365. {
  366. unsigned char data;
  367. int real_pos;
  368. do {
  369. if (pd->pos <= -1) {
  370. sh_mobile_i2c_get_data(pd, &data);
  371. if (sh_mobile_i2c_is_first_byte(pd))
  372. i2c_op(pd, OP_TX_FIRST, data);
  373. else
  374. i2c_op(pd, OP_TX, data);
  375. break;
  376. }
  377. if (pd->pos == 0) {
  378. i2c_op(pd, OP_TX_TO_RX, 0);
  379. break;
  380. }
  381. real_pos = pd->pos - 2;
  382. if (pd->pos == pd->msg->len) {
  383. if (real_pos < 0) {
  384. i2c_op(pd, OP_RX_STOP, 0);
  385. break;
  386. }
  387. data = i2c_op(pd, OP_RX_STOP_DATA, 0);
  388. } else
  389. data = i2c_op(pd, OP_RX, 0);
  390. if (real_pos >= 0)
  391. pd->msg->buf[real_pos] = data;
  392. } while (0);
  393. pd->pos++;
  394. return pd->pos == (pd->msg->len + 2);
  395. }
  396. static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
  397. {
  398. struct platform_device *dev = dev_id;
  399. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  400. unsigned char sr;
  401. int wakeup;
  402. sr = iic_rd(pd, ICSR);
  403. pd->sr |= sr; /* remember state */
  404. dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
  405. (pd->msg->flags & I2C_M_RD) ? "read" : "write",
  406. pd->pos, pd->msg->len);
  407. if (sr & (ICSR_AL | ICSR_TACK)) {
  408. /* don't interrupt transaction - continue to issue stop */
  409. iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
  410. wakeup = 0;
  411. } else if (pd->msg->flags & I2C_M_RD)
  412. wakeup = sh_mobile_i2c_isr_rx(pd);
  413. else
  414. wakeup = sh_mobile_i2c_isr_tx(pd);
  415. if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
  416. iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
  417. if (wakeup) {
  418. pd->sr |= SW_DONE;
  419. wake_up(&pd->wait);
  420. }
  421. /* defeat write posting to avoid spurious WAIT interrupts */
  422. iic_rd(pd, ICSR);
  423. return IRQ_HANDLED;
  424. }
  425. static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
  426. bool do_init)
  427. {
  428. if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
  429. dev_err(pd->dev, "Unsupported zero length i2c read\n");
  430. return -EOPNOTSUPP;
  431. }
  432. if (do_init) {
  433. /* Initialize channel registers */
  434. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  435. /* Enable channel and configure rx ack */
  436. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  437. /* Set the clock */
  438. iic_wr(pd, ICCL, pd->iccl & 0xff);
  439. iic_wr(pd, ICCH, pd->icch & 0xff);
  440. }
  441. pd->msg = usr_msg;
  442. pd->pos = -1;
  443. pd->sr = 0;
  444. /* Enable all interrupts to begin with */
  445. iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  446. return 0;
  447. }
  448. static int poll_dte(struct sh_mobile_i2c_data *pd)
  449. {
  450. int i;
  451. for (i = 1000; i; i--) {
  452. u_int8_t val = iic_rd(pd, ICSR);
  453. if (val & ICSR_DTE)
  454. break;
  455. if (val & ICSR_TACK)
  456. return -ENXIO;
  457. udelay(10);
  458. }
  459. return i ? 0 : -ETIMEDOUT;
  460. }
  461. static int poll_busy(struct sh_mobile_i2c_data *pd)
  462. {
  463. int i;
  464. for (i = 1000; i; i--) {
  465. u_int8_t val = iic_rd(pd, ICSR);
  466. dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
  467. /* the interrupt handler may wake us up before the
  468. * transfer is finished, so poll the hardware
  469. * until we're done.
  470. */
  471. if (!(val & ICSR_BUSY)) {
  472. /* handle missing acknowledge and arbitration lost */
  473. val |= pd->sr;
  474. if (val & ICSR_TACK)
  475. return -ENXIO;
  476. if (val & ICSR_AL)
  477. return -EAGAIN;
  478. break;
  479. }
  480. udelay(10);
  481. }
  482. return i ? 0 : -ETIMEDOUT;
  483. }
  484. static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
  485. struct i2c_msg *msgs,
  486. int num)
  487. {
  488. struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
  489. struct i2c_msg *msg;
  490. int err = 0;
  491. int i, k;
  492. activate_ch(pd);
  493. /* Process all messages */
  494. for (i = 0; i < num; i++) {
  495. bool do_start = pd->send_stop || !i;
  496. msg = &msgs[i];
  497. pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP;
  498. err = start_ch(pd, msg, do_start);
  499. if (err)
  500. break;
  501. if (do_start)
  502. i2c_op(pd, OP_START, 0);
  503. /* The interrupt handler takes care of the rest... */
  504. k = wait_event_timeout(pd->wait,
  505. pd->sr & (ICSR_TACK | SW_DONE),
  506. 5 * HZ);
  507. if (!k) {
  508. dev_err(pd->dev, "Transfer request timed out\n");
  509. err = -ETIMEDOUT;
  510. break;
  511. }
  512. if (pd->send_stop)
  513. err = poll_busy(pd);
  514. else
  515. err = poll_dte(pd);
  516. if (err < 0)
  517. break;
  518. }
  519. deactivate_ch(pd);
  520. if (!err)
  521. err = num;
  522. return err;
  523. }
  524. static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
  525. {
  526. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  527. }
  528. static struct i2c_algorithm sh_mobile_i2c_algorithm = {
  529. .functionality = sh_mobile_i2c_func,
  530. .master_xfer = sh_mobile_i2c_xfer,
  531. };
  532. static const struct sh_mobile_dt_config default_dt_config = {
  533. .clks_per_count = 1,
  534. };
  535. static const struct sh_mobile_dt_config rcar_gen2_dt_config = {
  536. .clks_per_count = 2,
  537. };
  538. static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
  539. { .compatible = "renesas,rmobile-iic", .data = &default_dt_config },
  540. { .compatible = "renesas,iic-r8a7790", .data = &rcar_gen2_dt_config },
  541. { .compatible = "renesas,iic-r8a7791", .data = &rcar_gen2_dt_config },
  542. { .compatible = "renesas,iic-r8a7792", .data = &rcar_gen2_dt_config },
  543. { .compatible = "renesas,iic-r8a7793", .data = &rcar_gen2_dt_config },
  544. { .compatible = "renesas,iic-r8a7794", .data = &rcar_gen2_dt_config },
  545. {},
  546. };
  547. MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
  548. static int sh_mobile_i2c_hook_irqs(struct platform_device *dev)
  549. {
  550. struct resource *res;
  551. resource_size_t n;
  552. int k = 0, ret;
  553. while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
  554. for (n = res->start; n <= res->end; n++) {
  555. ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr,
  556. 0, dev_name(&dev->dev), dev);
  557. if (ret) {
  558. dev_err(&dev->dev, "cannot request IRQ %pa\n", &n);
  559. return ret;
  560. }
  561. }
  562. k++;
  563. }
  564. return k > 0 ? 0 : -ENOENT;
  565. }
  566. static int sh_mobile_i2c_probe(struct platform_device *dev)
  567. {
  568. struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev);
  569. struct sh_mobile_i2c_data *pd;
  570. struct i2c_adapter *adap;
  571. struct resource *res;
  572. int ret;
  573. u32 bus_speed;
  574. pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
  575. if (!pd)
  576. return -ENOMEM;
  577. pd->clk = devm_clk_get(&dev->dev, NULL);
  578. if (IS_ERR(pd->clk)) {
  579. dev_err(&dev->dev, "cannot get clock\n");
  580. return PTR_ERR(pd->clk);
  581. }
  582. ret = sh_mobile_i2c_hook_irqs(dev);
  583. if (ret)
  584. return ret;
  585. pd->dev = &dev->dev;
  586. platform_set_drvdata(dev, pd);
  587. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  588. pd->reg = devm_ioremap_resource(&dev->dev, res);
  589. if (IS_ERR(pd->reg))
  590. return PTR_ERR(pd->reg);
  591. /* Use platform data bus speed or STANDARD_MODE */
  592. ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed);
  593. pd->bus_speed = ret ? STANDARD_MODE : bus_speed;
  594. pd->clks_per_count = 1;
  595. if (dev->dev.of_node) {
  596. const struct of_device_id *match;
  597. match = of_match_device(sh_mobile_i2c_dt_ids, &dev->dev);
  598. if (match) {
  599. const struct sh_mobile_dt_config *config;
  600. config = match->data;
  601. pd->clks_per_count = config->clks_per_count;
  602. }
  603. } else {
  604. if (pdata && pdata->bus_speed)
  605. pd->bus_speed = pdata->bus_speed;
  606. if (pdata && pdata->clks_per_count)
  607. pd->clks_per_count = pdata->clks_per_count;
  608. }
  609. /* The IIC blocks on SH-Mobile ARM processors
  610. * come with two new bits in ICIC.
  611. */
  612. if (resource_size(res) > 0x17)
  613. pd->flags |= IIC_FLAG_HAS_ICIC67;
  614. ret = sh_mobile_i2c_init(pd);
  615. if (ret)
  616. return ret;
  617. /* Enable Runtime PM for this device.
  618. *
  619. * Also tell the Runtime PM core to ignore children
  620. * for this device since it is valid for us to suspend
  621. * this I2C master driver even though the slave devices
  622. * on the I2C bus may not be suspended.
  623. *
  624. * The state of the I2C hardware bus is unaffected by
  625. * the Runtime PM state.
  626. */
  627. pm_suspend_ignore_children(&dev->dev, true);
  628. pm_runtime_enable(&dev->dev);
  629. /* setup the private data */
  630. adap = &pd->adap;
  631. i2c_set_adapdata(adap, pd);
  632. adap->owner = THIS_MODULE;
  633. adap->algo = &sh_mobile_i2c_algorithm;
  634. adap->dev.parent = &dev->dev;
  635. adap->retries = 5;
  636. adap->nr = dev->id;
  637. adap->dev.of_node = dev->dev.of_node;
  638. strlcpy(adap->name, dev->name, sizeof(adap->name));
  639. spin_lock_init(&pd->lock);
  640. init_waitqueue_head(&pd->wait);
  641. ret = i2c_add_numbered_adapter(adap);
  642. if (ret < 0) {
  643. dev_err(&dev->dev, "cannot add numbered adapter\n");
  644. return ret;
  645. }
  646. dev_info(&dev->dev,
  647. "I2C adapter %d with bus speed %lu Hz (L/H=0x%x/0x%x)\n",
  648. adap->nr, pd->bus_speed, pd->iccl, pd->icch);
  649. return 0;
  650. }
  651. static int sh_mobile_i2c_remove(struct platform_device *dev)
  652. {
  653. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  654. i2c_del_adapter(&pd->adap);
  655. pm_runtime_disable(&dev->dev);
  656. return 0;
  657. }
  658. static int sh_mobile_i2c_runtime_nop(struct device *dev)
  659. {
  660. /* Runtime PM callback shared between ->runtime_suspend()
  661. * and ->runtime_resume(). Simply returns success.
  662. *
  663. * This driver re-initializes all registers after
  664. * pm_runtime_get_sync() anyway so there is no need
  665. * to save and restore registers here.
  666. */
  667. return 0;
  668. }
  669. static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
  670. .runtime_suspend = sh_mobile_i2c_runtime_nop,
  671. .runtime_resume = sh_mobile_i2c_runtime_nop,
  672. };
  673. static struct platform_driver sh_mobile_i2c_driver = {
  674. .driver = {
  675. .name = "i2c-sh_mobile",
  676. .owner = THIS_MODULE,
  677. .pm = &sh_mobile_i2c_dev_pm_ops,
  678. .of_match_table = sh_mobile_i2c_dt_ids,
  679. },
  680. .probe = sh_mobile_i2c_probe,
  681. .remove = sh_mobile_i2c_remove,
  682. };
  683. static int __init sh_mobile_i2c_adap_init(void)
  684. {
  685. return platform_driver_register(&sh_mobile_i2c_driver);
  686. }
  687. static void __exit sh_mobile_i2c_adap_exit(void)
  688. {
  689. platform_driver_unregister(&sh_mobile_i2c_driver);
  690. }
  691. subsys_initcall(sh_mobile_i2c_adap_init);
  692. module_exit(sh_mobile_i2c_adap_exit);
  693. MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
  694. MODULE_AUTHOR("Magnus Damm");
  695. MODULE_LICENSE("GPL v2");
  696. MODULE_ALIAS("platform:i2c-sh_mobile");