i2c-sh_mobile.c 22 KB

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