i2c-sh_mobile.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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/i2c/i2c-sh_mobile.h>
  35. /* Transmit operation: */
  36. /* */
  37. /* 0 byte transmit */
  38. /* BUS: S A8 ACK P(*) */
  39. /* IRQ: DTE WAIT */
  40. /* ICIC: */
  41. /* ICCR: 0x94 0x90 */
  42. /* ICDR: A8 */
  43. /* */
  44. /* 1 byte transmit */
  45. /* BUS: S A8 ACK D8(1) ACK P(*) */
  46. /* IRQ: DTE WAIT WAIT */
  47. /* ICIC: -DTE */
  48. /* ICCR: 0x94 0x90 */
  49. /* ICDR: A8 D8(1) */
  50. /* */
  51. /* 2 byte transmit */
  52. /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P(*) */
  53. /* IRQ: DTE WAIT WAIT WAIT */
  54. /* ICIC: -DTE */
  55. /* ICCR: 0x94 0x90 */
  56. /* ICDR: A8 D8(1) D8(2) */
  57. /* */
  58. /* 3 bytes or more, +---------+ gets repeated */
  59. /* */
  60. /* */
  61. /* Receive operation: */
  62. /* */
  63. /* 0 byte receive - not supported since slave may hold SDA low */
  64. /* */
  65. /* 1 byte receive [TX] | [RX] */
  66. /* BUS: S A8 ACK | D8(1) ACK P(*) */
  67. /* IRQ: DTE WAIT | WAIT DTE */
  68. /* ICIC: -DTE | +DTE */
  69. /* ICCR: 0x94 0x81 | 0xc0 */
  70. /* ICDR: A8 | D8(1) */
  71. /* */
  72. /* 2 byte receive [TX]| [RX] */
  73. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P(*) */
  74. /* IRQ: DTE WAIT | WAIT WAIT DTE */
  75. /* ICIC: -DTE | +DTE */
  76. /* ICCR: 0x94 0x81 | 0xc0 */
  77. /* ICDR: A8 | D8(1) D8(2) */
  78. /* */
  79. /* 3 byte receive [TX] | [RX] (*) */
  80. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
  81. /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
  82. /* ICIC: -DTE | +DTE */
  83. /* ICCR: 0x94 0x81 | 0xc0 */
  84. /* ICDR: A8 | D8(1) D8(2) D8(3) */
  85. /* */
  86. /* 4 bytes or more, this part is repeated +---------+ */
  87. /* */
  88. /* */
  89. /* Interrupt order and BUSY flag */
  90. /* ___ _ */
  91. /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
  92. /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
  93. /* */
  94. /* S D7 D6 D5 D4 D3 D2 D1 D0 P(*) */
  95. /* ___ */
  96. /* WAIT IRQ ________________________________/ \___________ */
  97. /* TACK IRQ ____________________________________/ \_______ */
  98. /* DTE IRQ __________________________________________/ \_ */
  99. /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  100. /* _______________________________________________ */
  101. /* BUSY __/ \_ */
  102. /* */
  103. /* (*) The STOP condition is only sent by the master at the end of the last */
  104. /* I2C message or if the I2C_M_STOP flag is set. Similarly, the BUSY bit is */
  105. /* only cleared after the STOP condition, so, between messages we have to */
  106. /* poll for the DTE bit. */
  107. /* */
  108. enum sh_mobile_i2c_op {
  109. OP_START = 0,
  110. OP_TX_FIRST,
  111. OP_TX,
  112. OP_TX_STOP,
  113. OP_TX_TO_RX,
  114. OP_RX,
  115. OP_RX_STOP,
  116. OP_RX_STOP_DATA,
  117. };
  118. struct sh_mobile_i2c_data {
  119. struct device *dev;
  120. void __iomem *reg;
  121. struct i2c_adapter adap;
  122. unsigned long bus_speed;
  123. unsigned int clks_per_count;
  124. struct clk *clk;
  125. u_int8_t icic;
  126. u_int8_t flags;
  127. u_int16_t iccl;
  128. u_int16_t icch;
  129. spinlock_t lock;
  130. wait_queue_head_t wait;
  131. struct i2c_msg *msg;
  132. int pos;
  133. int sr;
  134. bool send_stop;
  135. };
  136. #define IIC_FLAG_HAS_ICIC67 (1 << 0)
  137. #define STANDARD_MODE 100000
  138. #define FAST_MODE 400000
  139. /* Register offsets */
  140. #define ICDR 0x00
  141. #define ICCR 0x04
  142. #define ICSR 0x08
  143. #define ICIC 0x0c
  144. #define ICCL 0x10
  145. #define ICCH 0x14
  146. /* Register bits */
  147. #define ICCR_ICE 0x80
  148. #define ICCR_RACK 0x40
  149. #define ICCR_TRS 0x10
  150. #define ICCR_BBSY 0x04
  151. #define ICCR_SCP 0x01
  152. #define ICSR_SCLM 0x80
  153. #define ICSR_SDAM 0x40
  154. #define SW_DONE 0x20
  155. #define ICSR_BUSY 0x10
  156. #define ICSR_AL 0x08
  157. #define ICSR_TACK 0x04
  158. #define ICSR_WAIT 0x02
  159. #define ICSR_DTE 0x01
  160. #define ICIC_ICCLB8 0x80
  161. #define ICIC_ICCHB8 0x40
  162. #define ICIC_ALE 0x08
  163. #define ICIC_TACKE 0x04
  164. #define ICIC_WAITE 0x02
  165. #define ICIC_DTEE 0x01
  166. static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
  167. {
  168. if (offs == ICIC)
  169. data |= pd->icic;
  170. iowrite8(data, pd->reg + offs);
  171. }
  172. static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
  173. {
  174. return ioread8(pd->reg + offs);
  175. }
  176. static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
  177. unsigned char set, unsigned char clr)
  178. {
  179. iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
  180. }
  181. static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf, int offset)
  182. {
  183. /*
  184. * Conditional expression:
  185. * ICCL >= COUNT_CLK * (tLOW + tf)
  186. *
  187. * SH-Mobile IIC hardware starts counting the LOW period of
  188. * the SCL signal (tLOW) as soon as it pulls the SCL line.
  189. * In order to meet the tLOW timing spec, we need to take into
  190. * account the fall time of SCL signal (tf). Default tf value
  191. * should be 0.3 us, for safety.
  192. */
  193. return (((count_khz * (tLOW + tf)) + 5000) / 10000) + offset;
  194. }
  195. static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf, int offset)
  196. {
  197. /*
  198. * Conditional expression:
  199. * ICCH >= COUNT_CLK * (tHIGH + tf)
  200. *
  201. * SH-Mobile IIC hardware is aware of SCL transition period 'tr',
  202. * and can ignore it. SH-Mobile IIC controller starts counting
  203. * the HIGH period of the SCL signal (tHIGH) after the SCL input
  204. * voltage increases at VIH.
  205. *
  206. * Afterward it turned out calculating ICCH using only tHIGH spec
  207. * will result in violation of the tHD;STA timing spec. We need
  208. * to take into account the fall time of SDA signal (tf) at START
  209. * condition, in order to meet both tHIGH and tHD;STA specs.
  210. */
  211. return (((count_khz * (tHIGH + tf)) + 5000) / 10000) + offset;
  212. }
  213. static void sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd)
  214. {
  215. unsigned long i2c_clk_khz;
  216. u32 tHIGH, tLOW, tf;
  217. int offset;
  218. /* Get clock rate after clock is enabled */
  219. clk_enable(pd->clk);
  220. i2c_clk_khz = clk_get_rate(pd->clk) / 1000;
  221. i2c_clk_khz /= pd->clks_per_count;
  222. if (pd->bus_speed == STANDARD_MODE) {
  223. tLOW = 47; /* tLOW = 4.7 us */
  224. tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */
  225. tf = 3; /* tf = 0.3 us */
  226. offset = 0; /* No offset */
  227. } else if (pd->bus_speed == FAST_MODE) {
  228. tLOW = 13; /* tLOW = 1.3 us */
  229. tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */
  230. tf = 3; /* tf = 0.3 us */
  231. offset = 0; /* No offset */
  232. } else {
  233. dev_err(pd->dev, "unrecognized bus speed %lu Hz\n",
  234. pd->bus_speed);
  235. goto out;
  236. }
  237. pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf, offset);
  238. /* one more bit of ICCL in ICIC */
  239. if ((pd->iccl > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
  240. pd->icic |= ICIC_ICCLB8;
  241. else
  242. pd->icic &= ~ICIC_ICCLB8;
  243. pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf, offset);
  244. /* one more bit of ICCH in ICIC */
  245. if ((pd->icch > 0xff) && (pd->flags & IIC_FLAG_HAS_ICIC67))
  246. pd->icic |= ICIC_ICCHB8;
  247. else
  248. pd->icic &= ~ICIC_ICCHB8;
  249. out:
  250. clk_disable(pd->clk);
  251. }
  252. static void activate_ch(struct sh_mobile_i2c_data *pd)
  253. {
  254. /* Wake up device and enable clock */
  255. pm_runtime_get_sync(pd->dev);
  256. clk_enable(pd->clk);
  257. /* Enable channel and configure rx ack */
  258. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  259. /* Mask all interrupts */
  260. iic_wr(pd, ICIC, 0);
  261. /* Set the clock */
  262. iic_wr(pd, ICCL, pd->iccl & 0xff);
  263. iic_wr(pd, ICCH, pd->icch & 0xff);
  264. }
  265. static void deactivate_ch(struct sh_mobile_i2c_data *pd)
  266. {
  267. /* Clear/disable interrupts */
  268. iic_wr(pd, ICSR, 0);
  269. iic_wr(pd, ICIC, 0);
  270. /* Disable channel */
  271. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  272. /* Disable clock and mark device as idle */
  273. clk_disable(pd->clk);
  274. pm_runtime_put_sync(pd->dev);
  275. }
  276. static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
  277. enum sh_mobile_i2c_op op, unsigned char data)
  278. {
  279. unsigned char ret = 0;
  280. unsigned long flags;
  281. dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
  282. spin_lock_irqsave(&pd->lock, flags);
  283. switch (op) {
  284. case OP_START: /* issue start and trigger DTE interrupt */
  285. iic_wr(pd, ICCR, 0x94);
  286. break;
  287. case OP_TX_FIRST: /* disable DTE interrupt and write data */
  288. iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  289. iic_wr(pd, ICDR, data);
  290. break;
  291. case OP_TX: /* write data */
  292. iic_wr(pd, ICDR, data);
  293. break;
  294. case OP_TX_STOP: /* write data and issue a stop afterwards */
  295. iic_wr(pd, ICDR, data);
  296. iic_wr(pd, ICCR, pd->send_stop ? 0x90 : 0x94);
  297. break;
  298. case OP_TX_TO_RX: /* select read mode */
  299. iic_wr(pd, ICCR, 0x81);
  300. break;
  301. case OP_RX: /* just read data */
  302. ret = iic_rd(pd, ICDR);
  303. break;
  304. case OP_RX_STOP: /* enable DTE interrupt, issue stop */
  305. iic_wr(pd, ICIC,
  306. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  307. iic_wr(pd, ICCR, 0xc0);
  308. break;
  309. case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
  310. iic_wr(pd, ICIC,
  311. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  312. ret = iic_rd(pd, ICDR);
  313. iic_wr(pd, ICCR, 0xc0);
  314. break;
  315. }
  316. spin_unlock_irqrestore(&pd->lock, flags);
  317. dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
  318. return ret;
  319. }
  320. static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
  321. {
  322. return pd->pos == -1;
  323. }
  324. static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
  325. {
  326. return pd->pos == pd->msg->len - 1;
  327. }
  328. static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
  329. unsigned char *buf)
  330. {
  331. switch (pd->pos) {
  332. case -1:
  333. *buf = (pd->msg->addr & 0x7f) << 1;
  334. *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
  335. break;
  336. default:
  337. *buf = pd->msg->buf[pd->pos];
  338. }
  339. }
  340. static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
  341. {
  342. unsigned char data;
  343. if (pd->pos == pd->msg->len)
  344. return 1;
  345. sh_mobile_i2c_get_data(pd, &data);
  346. if (sh_mobile_i2c_is_last_byte(pd))
  347. i2c_op(pd, OP_TX_STOP, data);
  348. else if (sh_mobile_i2c_is_first_byte(pd))
  349. i2c_op(pd, OP_TX_FIRST, data);
  350. else
  351. i2c_op(pd, OP_TX, data);
  352. pd->pos++;
  353. return 0;
  354. }
  355. static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
  356. {
  357. unsigned char data;
  358. int real_pos;
  359. do {
  360. if (pd->pos <= -1) {
  361. sh_mobile_i2c_get_data(pd, &data);
  362. if (sh_mobile_i2c_is_first_byte(pd))
  363. i2c_op(pd, OP_TX_FIRST, data);
  364. else
  365. i2c_op(pd, OP_TX, data);
  366. break;
  367. }
  368. if (pd->pos == 0) {
  369. i2c_op(pd, OP_TX_TO_RX, 0);
  370. break;
  371. }
  372. real_pos = pd->pos - 2;
  373. if (pd->pos == pd->msg->len) {
  374. if (real_pos < 0) {
  375. i2c_op(pd, OP_RX_STOP, 0);
  376. break;
  377. }
  378. data = i2c_op(pd, OP_RX_STOP_DATA, 0);
  379. } else
  380. data = i2c_op(pd, OP_RX, 0);
  381. if (real_pos >= 0)
  382. pd->msg->buf[real_pos] = data;
  383. } while (0);
  384. pd->pos++;
  385. return pd->pos == (pd->msg->len + 2);
  386. }
  387. static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
  388. {
  389. struct platform_device *dev = dev_id;
  390. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  391. unsigned char sr;
  392. int wakeup;
  393. sr = iic_rd(pd, ICSR);
  394. pd->sr |= sr; /* remember state */
  395. dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
  396. (pd->msg->flags & I2C_M_RD) ? "read" : "write",
  397. pd->pos, pd->msg->len);
  398. if (sr & (ICSR_AL | ICSR_TACK)) {
  399. /* don't interrupt transaction - continue to issue stop */
  400. iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
  401. wakeup = 0;
  402. } else if (pd->msg->flags & I2C_M_RD)
  403. wakeup = sh_mobile_i2c_isr_rx(pd);
  404. else
  405. wakeup = sh_mobile_i2c_isr_tx(pd);
  406. if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
  407. iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
  408. if (wakeup) {
  409. pd->sr |= SW_DONE;
  410. wake_up(&pd->wait);
  411. }
  412. /* defeat write posting to avoid spurious WAIT interrupts */
  413. iic_rd(pd, ICSR);
  414. return IRQ_HANDLED;
  415. }
  416. static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg,
  417. bool do_init)
  418. {
  419. if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
  420. dev_err(pd->dev, "Unsupported zero length i2c read\n");
  421. return -EIO;
  422. }
  423. if (do_init) {
  424. /* Initialize channel registers */
  425. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  426. /* Enable channel and configure rx ack */
  427. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  428. /* Set the clock */
  429. iic_wr(pd, ICCL, pd->iccl & 0xff);
  430. iic_wr(pd, ICCH, pd->icch & 0xff);
  431. }
  432. pd->msg = usr_msg;
  433. pd->pos = -1;
  434. pd->sr = 0;
  435. /* Enable all interrupts to begin with */
  436. iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  437. return 0;
  438. }
  439. static int poll_dte(struct sh_mobile_i2c_data *pd)
  440. {
  441. int i;
  442. for (i = 1000; i; i--) {
  443. u_int8_t val = iic_rd(pd, ICSR);
  444. if (val & ICSR_DTE)
  445. break;
  446. if (val & ICSR_TACK)
  447. return -EIO;
  448. udelay(10);
  449. }
  450. if (!i) {
  451. dev_warn(pd->dev, "Timeout polling for DTE!\n");
  452. return -ETIMEDOUT;
  453. }
  454. return 0;
  455. }
  456. static int poll_busy(struct sh_mobile_i2c_data *pd)
  457. {
  458. int i;
  459. for (i = 1000; i; i--) {
  460. u_int8_t val = iic_rd(pd, ICSR);
  461. dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
  462. /* the interrupt handler may wake us up before the
  463. * transfer is finished, so poll the hardware
  464. * until we're done.
  465. */
  466. if (!(val & ICSR_BUSY)) {
  467. /* handle missing acknowledge and arbitration lost */
  468. if ((val | pd->sr) & (ICSR_TACK | ICSR_AL))
  469. return -EIO;
  470. break;
  471. }
  472. udelay(10);
  473. }
  474. if (!i) {
  475. dev_err(pd->dev, "Polling timed out\n");
  476. return -ETIMEDOUT;
  477. }
  478. return 0;
  479. }
  480. static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
  481. struct i2c_msg *msgs,
  482. int num)
  483. {
  484. struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
  485. struct i2c_msg *msg;
  486. int err = 0;
  487. int i, k;
  488. activate_ch(pd);
  489. /* Process all messages */
  490. for (i = 0; i < num; i++) {
  491. bool do_start = pd->send_stop || !i;
  492. msg = &msgs[i];
  493. pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP;
  494. err = start_ch(pd, msg, do_start);
  495. if (err)
  496. break;
  497. if (do_start)
  498. i2c_op(pd, OP_START, 0);
  499. /* The interrupt handler takes care of the rest... */
  500. k = wait_event_timeout(pd->wait,
  501. pd->sr & (ICSR_TACK | SW_DONE),
  502. 5 * HZ);
  503. if (!k) {
  504. dev_err(pd->dev, "Transfer request timed out\n");
  505. err = -ETIMEDOUT;
  506. break;
  507. }
  508. if (pd->send_stop)
  509. err = poll_busy(pd);
  510. else
  511. err = poll_dte(pd);
  512. if (err < 0)
  513. break;
  514. }
  515. deactivate_ch(pd);
  516. if (!err)
  517. err = num;
  518. return err;
  519. }
  520. static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
  521. {
  522. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  523. }
  524. static struct i2c_algorithm sh_mobile_i2c_algorithm = {
  525. .functionality = sh_mobile_i2c_func,
  526. .master_xfer = sh_mobile_i2c_xfer,
  527. };
  528. static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook)
  529. {
  530. struct resource *res;
  531. int ret = -ENXIO;
  532. int n, k = 0;
  533. while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
  534. for (n = res->start; hook && n <= res->end; n++) {
  535. if (request_irq(n, sh_mobile_i2c_isr, 0,
  536. dev_name(&dev->dev), dev)) {
  537. for (n--; n >= res->start; n--)
  538. free_irq(n, dev);
  539. goto rollback;
  540. }
  541. }
  542. k++;
  543. }
  544. if (hook)
  545. return k > 0 ? 0 : -ENOENT;
  546. ret = 0;
  547. rollback:
  548. k--;
  549. while (k >= 0) {
  550. res = platform_get_resource(dev, IORESOURCE_IRQ, k);
  551. for (n = res->start; n <= res->end; n++)
  552. free_irq(n, dev);
  553. k--;
  554. }
  555. return ret;
  556. }
  557. static int sh_mobile_i2c_probe(struct platform_device *dev)
  558. {
  559. struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev);
  560. struct sh_mobile_i2c_data *pd;
  561. struct i2c_adapter *adap;
  562. struct resource *res;
  563. int size;
  564. int ret;
  565. pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
  566. if (pd == NULL) {
  567. dev_err(&dev->dev, "cannot allocate private data\n");
  568. return -ENOMEM;
  569. }
  570. pd->clk = clk_get(&dev->dev, NULL);
  571. if (IS_ERR(pd->clk)) {
  572. dev_err(&dev->dev, "cannot get clock\n");
  573. ret = PTR_ERR(pd->clk);
  574. goto err;
  575. }
  576. ret = sh_mobile_i2c_hook_irqs(dev, 1);
  577. if (ret) {
  578. dev_err(&dev->dev, "cannot request IRQ\n");
  579. goto err_clk;
  580. }
  581. pd->dev = &dev->dev;
  582. platform_set_drvdata(dev, pd);
  583. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  584. if (res == NULL) {
  585. dev_err(&dev->dev, "cannot find IO resource\n");
  586. ret = -ENOENT;
  587. goto err_irq;
  588. }
  589. size = resource_size(res);
  590. pd->reg = ioremap(res->start, size);
  591. if (pd->reg == NULL) {
  592. dev_err(&dev->dev, "cannot map IO\n");
  593. ret = -ENXIO;
  594. goto err_irq;
  595. }
  596. /* Use platform data bus speed or STANDARD_MODE */
  597. pd->bus_speed = STANDARD_MODE;
  598. if (pdata && pdata->bus_speed)
  599. pd->bus_speed = pdata->bus_speed;
  600. pd->clks_per_count = 1;
  601. if (pdata && pdata->clks_per_count)
  602. pd->clks_per_count = pdata->clks_per_count;
  603. /* The IIC blocks on SH-Mobile ARM processors
  604. * come with two new bits in ICIC.
  605. */
  606. if (size > 0x17)
  607. pd->flags |= IIC_FLAG_HAS_ICIC67;
  608. sh_mobile_i2c_init(pd);
  609. /* Enable Runtime PM for this device.
  610. *
  611. * Also tell the Runtime PM core to ignore children
  612. * for this device since it is valid for us to suspend
  613. * this I2C master driver even though the slave devices
  614. * on the I2C bus may not be suspended.
  615. *
  616. * The state of the I2C hardware bus is unaffected by
  617. * the Runtime PM state.
  618. */
  619. pm_suspend_ignore_children(&dev->dev, true);
  620. pm_runtime_enable(&dev->dev);
  621. /* setup the private data */
  622. adap = &pd->adap;
  623. i2c_set_adapdata(adap, pd);
  624. adap->owner = THIS_MODULE;
  625. adap->algo = &sh_mobile_i2c_algorithm;
  626. adap->dev.parent = &dev->dev;
  627. adap->retries = 5;
  628. adap->nr = dev->id;
  629. adap->dev.of_node = dev->dev.of_node;
  630. strlcpy(adap->name, dev->name, sizeof(adap->name));
  631. spin_lock_init(&pd->lock);
  632. init_waitqueue_head(&pd->wait);
  633. ret = i2c_add_numbered_adapter(adap);
  634. if (ret < 0) {
  635. dev_err(&dev->dev, "cannot add numbered adapter\n");
  636. goto err_all;
  637. }
  638. dev_info(&dev->dev,
  639. "I2C adapter %d with bus speed %lu Hz (L/H=%x/%x)\n",
  640. adap->nr, pd->bus_speed, pd->iccl, pd->icch);
  641. return 0;
  642. err_all:
  643. iounmap(pd->reg);
  644. err_irq:
  645. sh_mobile_i2c_hook_irqs(dev, 0);
  646. err_clk:
  647. clk_put(pd->clk);
  648. err:
  649. kfree(pd);
  650. return ret;
  651. }
  652. static int sh_mobile_i2c_remove(struct platform_device *dev)
  653. {
  654. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  655. i2c_del_adapter(&pd->adap);
  656. iounmap(pd->reg);
  657. sh_mobile_i2c_hook_irqs(dev, 0);
  658. clk_put(pd->clk);
  659. pm_runtime_disable(&dev->dev);
  660. kfree(pd);
  661. return 0;
  662. }
  663. static int sh_mobile_i2c_runtime_nop(struct device *dev)
  664. {
  665. /* Runtime PM callback shared between ->runtime_suspend()
  666. * and ->runtime_resume(). Simply returns success.
  667. *
  668. * This driver re-initializes all registers after
  669. * pm_runtime_get_sync() anyway so there is no need
  670. * to save and restore registers here.
  671. */
  672. return 0;
  673. }
  674. static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
  675. .runtime_suspend = sh_mobile_i2c_runtime_nop,
  676. .runtime_resume = sh_mobile_i2c_runtime_nop,
  677. };
  678. static const struct of_device_id sh_mobile_i2c_dt_ids[] = {
  679. { .compatible = "renesas,rmobile-iic", },
  680. {},
  681. };
  682. MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids);
  683. static struct platform_driver sh_mobile_i2c_driver = {
  684. .driver = {
  685. .name = "i2c-sh_mobile",
  686. .owner = THIS_MODULE,
  687. .pm = &sh_mobile_i2c_dev_pm_ops,
  688. .of_match_table = sh_mobile_i2c_dt_ids,
  689. },
  690. .probe = sh_mobile_i2c_probe,
  691. .remove = sh_mobile_i2c_remove,
  692. };
  693. static int __init sh_mobile_i2c_adap_init(void)
  694. {
  695. return platform_driver_register(&sh_mobile_i2c_driver);
  696. }
  697. static void __exit sh_mobile_i2c_adap_exit(void)
  698. {
  699. platform_driver_unregister(&sh_mobile_i2c_driver);
  700. }
  701. subsys_initcall(sh_mobile_i2c_adap_init);
  702. module_exit(sh_mobile_i2c_adap_exit);
  703. MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
  704. MODULE_AUTHOR("Magnus Damm");
  705. MODULE_LICENSE("GPL v2");
  706. MODULE_ALIAS("platform:i2c-sh_mobile");