i2c-sh_mobile.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. /* Transmit operation: */
  35. /* */
  36. /* 0 byte transmit */
  37. /* BUS: S A8 ACK P */
  38. /* IRQ: DTE WAIT */
  39. /* ICIC: */
  40. /* ICCR: 0x94 0x90 */
  41. /* ICDR: A8 */
  42. /* */
  43. /* 1 byte transmit */
  44. /* BUS: S A8 ACK D8(1) ACK P */
  45. /* IRQ: DTE WAIT WAIT */
  46. /* ICIC: -DTE */
  47. /* ICCR: 0x94 0x90 */
  48. /* ICDR: A8 D8(1) */
  49. /* */
  50. /* 2 byte transmit */
  51. /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P */
  52. /* IRQ: DTE WAIT WAIT WAIT */
  53. /* ICIC: -DTE */
  54. /* ICCR: 0x94 0x90 */
  55. /* ICDR: A8 D8(1) D8(2) */
  56. /* */
  57. /* 3 bytes or more, +---------+ gets repeated */
  58. /* */
  59. /* */
  60. /* Receive operation: */
  61. /* */
  62. /* 0 byte receive - not supported since slave may hold SDA low */
  63. /* */
  64. /* 1 byte receive [TX] | [RX] */
  65. /* BUS: S A8 ACK | D8(1) ACK P */
  66. /* IRQ: DTE WAIT | WAIT DTE */
  67. /* ICIC: -DTE | +DTE */
  68. /* ICCR: 0x94 0x81 | 0xc0 */
  69. /* ICDR: A8 | D8(1) */
  70. /* */
  71. /* 2 byte receive [TX]| [RX] */
  72. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P */
  73. /* IRQ: DTE WAIT | WAIT WAIT DTE */
  74. /* ICIC: -DTE | +DTE */
  75. /* ICCR: 0x94 0x81 | 0xc0 */
  76. /* ICDR: A8 | D8(1) D8(2) */
  77. /* */
  78. /* 3 byte receive [TX] | [RX] */
  79. /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */
  80. /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */
  81. /* ICIC: -DTE | +DTE */
  82. /* ICCR: 0x94 0x81 | 0xc0 */
  83. /* ICDR: A8 | D8(1) D8(2) D8(3) */
  84. /* */
  85. /* 4 bytes or more, this part is repeated +---------+ */
  86. /* */
  87. /* */
  88. /* Interrupt order and BUSY flag */
  89. /* ___ _ */
  90. /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */
  91. /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */
  92. /* */
  93. /* S D7 D6 D5 D4 D3 D2 D1 D0 P */
  94. /* ___ */
  95. /* WAIT IRQ ________________________________/ \___________ */
  96. /* TACK IRQ ____________________________________/ \_______ */
  97. /* DTE IRQ __________________________________________/ \_ */
  98. /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
  99. /* _______________________________________________ */
  100. /* BUSY __/ \_ */
  101. /* */
  102. enum sh_mobile_i2c_op {
  103. OP_START = 0,
  104. OP_TX_FIRST,
  105. OP_TX,
  106. OP_TX_STOP,
  107. OP_TX_TO_RX,
  108. OP_RX,
  109. OP_RX_STOP,
  110. OP_RX_STOP_DATA,
  111. };
  112. struct sh_mobile_i2c_data {
  113. struct device *dev;
  114. void __iomem *reg;
  115. struct i2c_adapter adap;
  116. struct clk *clk;
  117. u_int8_t iccl;
  118. u_int8_t icch;
  119. spinlock_t lock;
  120. wait_queue_head_t wait;
  121. struct i2c_msg *msg;
  122. int pos;
  123. int sr;
  124. };
  125. #define NORMAL_SPEED 100000 /* FAST_SPEED 400000 */
  126. /* Register offsets */
  127. #define ICDR 0x00
  128. #define ICCR 0x04
  129. #define ICSR 0x08
  130. #define ICIC 0x0c
  131. #define ICCL 0x10
  132. #define ICCH 0x14
  133. /* Register bits */
  134. #define ICCR_ICE 0x80
  135. #define ICCR_RACK 0x40
  136. #define ICCR_TRS 0x10
  137. #define ICCR_BBSY 0x04
  138. #define ICCR_SCP 0x01
  139. #define ICSR_SCLM 0x80
  140. #define ICSR_SDAM 0x40
  141. #define SW_DONE 0x20
  142. #define ICSR_BUSY 0x10
  143. #define ICSR_AL 0x08
  144. #define ICSR_TACK 0x04
  145. #define ICSR_WAIT 0x02
  146. #define ICSR_DTE 0x01
  147. #define ICIC_ALE 0x08
  148. #define ICIC_TACKE 0x04
  149. #define ICIC_WAITE 0x02
  150. #define ICIC_DTEE 0x01
  151. static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
  152. {
  153. iowrite8(data, pd->reg + offs);
  154. }
  155. static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
  156. {
  157. return ioread8(pd->reg + offs);
  158. }
  159. static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
  160. unsigned char set, unsigned char clr)
  161. {
  162. iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
  163. }
  164. static void activate_ch(struct sh_mobile_i2c_data *pd)
  165. {
  166. unsigned long i2c_clk;
  167. u_int32_t num;
  168. u_int32_t denom;
  169. u_int32_t tmp;
  170. /* Wake up device and enable clock */
  171. pm_runtime_get_sync(pd->dev);
  172. clk_enable(pd->clk);
  173. /* Get clock rate after clock is enabled */
  174. i2c_clk = clk_get_rate(pd->clk);
  175. /* Calculate the value for iccl. From the data sheet:
  176. * iccl = (p clock / transfer rate) * (L / (L + H))
  177. * where L and H are the SCL low/high ratio (5/4 in this case).
  178. * We also round off the result.
  179. */
  180. num = i2c_clk * 5;
  181. denom = NORMAL_SPEED * 9;
  182. tmp = num * 10 / denom;
  183. if (tmp % 10 >= 5)
  184. pd->iccl = (u_int8_t)((num/denom) + 1);
  185. else
  186. pd->iccl = (u_int8_t)(num/denom);
  187. /* Calculate the value for icch. From the data sheet:
  188. icch = (p clock / transfer rate) * (H / (L + H)) */
  189. num = i2c_clk * 4;
  190. tmp = num * 10 / denom;
  191. if (tmp % 10 >= 5)
  192. pd->icch = (u_int8_t)((num/denom) + 1);
  193. else
  194. pd->icch = (u_int8_t)(num/denom);
  195. /* Enable channel and configure rx ack */
  196. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  197. /* Mask all interrupts */
  198. iic_wr(pd, ICIC, 0);
  199. /* Set the clock */
  200. iic_wr(pd, ICCL, pd->iccl);
  201. iic_wr(pd, ICCH, pd->icch);
  202. }
  203. static void deactivate_ch(struct sh_mobile_i2c_data *pd)
  204. {
  205. /* Clear/disable interrupts */
  206. iic_wr(pd, ICSR, 0);
  207. iic_wr(pd, ICIC, 0);
  208. /* Disable channel */
  209. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  210. /* Disable clock and mark device as idle */
  211. clk_disable(pd->clk);
  212. pm_runtime_put_sync(pd->dev);
  213. }
  214. static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
  215. enum sh_mobile_i2c_op op, unsigned char data)
  216. {
  217. unsigned char ret = 0;
  218. unsigned long flags;
  219. dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
  220. spin_lock_irqsave(&pd->lock, flags);
  221. switch (op) {
  222. case OP_START: /* issue start and trigger DTE interrupt */
  223. iic_wr(pd, ICCR, 0x94);
  224. break;
  225. case OP_TX_FIRST: /* disable DTE interrupt and write data */
  226. iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  227. iic_wr(pd, ICDR, data);
  228. break;
  229. case OP_TX: /* write data */
  230. iic_wr(pd, ICDR, data);
  231. break;
  232. case OP_TX_STOP: /* write data and issue a stop afterwards */
  233. iic_wr(pd, ICDR, data);
  234. iic_wr(pd, ICCR, 0x90);
  235. break;
  236. case OP_TX_TO_RX: /* select read mode */
  237. iic_wr(pd, ICCR, 0x81);
  238. break;
  239. case OP_RX: /* just read data */
  240. ret = iic_rd(pd, ICDR);
  241. break;
  242. case OP_RX_STOP: /* enable DTE interrupt, issue stop */
  243. iic_wr(pd, ICIC,
  244. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  245. iic_wr(pd, ICCR, 0xc0);
  246. break;
  247. case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */
  248. iic_wr(pd, ICIC,
  249. ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  250. ret = iic_rd(pd, ICDR);
  251. iic_wr(pd, ICCR, 0xc0);
  252. break;
  253. }
  254. spin_unlock_irqrestore(&pd->lock, flags);
  255. dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
  256. return ret;
  257. }
  258. static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
  259. {
  260. if (pd->pos == -1)
  261. return 1;
  262. return 0;
  263. }
  264. static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
  265. {
  266. if (pd->pos == (pd->msg->len - 1))
  267. return 1;
  268. return 0;
  269. }
  270. static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
  271. unsigned char *buf)
  272. {
  273. switch (pd->pos) {
  274. case -1:
  275. *buf = (pd->msg->addr & 0x7f) << 1;
  276. *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
  277. break;
  278. default:
  279. *buf = pd->msg->buf[pd->pos];
  280. }
  281. }
  282. static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
  283. {
  284. unsigned char data;
  285. if (pd->pos == pd->msg->len)
  286. return 1;
  287. sh_mobile_i2c_get_data(pd, &data);
  288. if (sh_mobile_i2c_is_last_byte(pd))
  289. i2c_op(pd, OP_TX_STOP, data);
  290. else if (sh_mobile_i2c_is_first_byte(pd))
  291. i2c_op(pd, OP_TX_FIRST, data);
  292. else
  293. i2c_op(pd, OP_TX, data);
  294. pd->pos++;
  295. return 0;
  296. }
  297. static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
  298. {
  299. unsigned char data;
  300. int real_pos;
  301. do {
  302. if (pd->pos <= -1) {
  303. sh_mobile_i2c_get_data(pd, &data);
  304. if (sh_mobile_i2c_is_first_byte(pd))
  305. i2c_op(pd, OP_TX_FIRST, data);
  306. else
  307. i2c_op(pd, OP_TX, data);
  308. break;
  309. }
  310. if (pd->pos == 0) {
  311. i2c_op(pd, OP_TX_TO_RX, 0);
  312. break;
  313. }
  314. real_pos = pd->pos - 2;
  315. if (pd->pos == pd->msg->len) {
  316. if (real_pos < 0) {
  317. i2c_op(pd, OP_RX_STOP, 0);
  318. break;
  319. }
  320. data = i2c_op(pd, OP_RX_STOP_DATA, 0);
  321. } else
  322. data = i2c_op(pd, OP_RX, 0);
  323. if (real_pos >= 0)
  324. pd->msg->buf[real_pos] = data;
  325. } while (0);
  326. pd->pos++;
  327. return pd->pos == (pd->msg->len + 2);
  328. }
  329. static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
  330. {
  331. struct platform_device *dev = dev_id;
  332. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  333. unsigned char sr;
  334. int wakeup;
  335. sr = iic_rd(pd, ICSR);
  336. pd->sr |= sr; /* remember state */
  337. dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
  338. (pd->msg->flags & I2C_M_RD) ? "read" : "write",
  339. pd->pos, pd->msg->len);
  340. if (sr & (ICSR_AL | ICSR_TACK)) {
  341. /* don't interrupt transaction - continue to issue stop */
  342. iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
  343. wakeup = 0;
  344. } else if (pd->msg->flags & I2C_M_RD)
  345. wakeup = sh_mobile_i2c_isr_rx(pd);
  346. else
  347. wakeup = sh_mobile_i2c_isr_tx(pd);
  348. if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */
  349. iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
  350. if (wakeup) {
  351. pd->sr |= SW_DONE;
  352. wake_up(&pd->wait);
  353. }
  354. return IRQ_HANDLED;
  355. }
  356. static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg)
  357. {
  358. if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) {
  359. dev_err(pd->dev, "Unsupported zero length i2c read\n");
  360. return -EIO;
  361. }
  362. /* Initialize channel registers */
  363. iic_set_clr(pd, ICCR, 0, ICCR_ICE);
  364. /* Enable channel and configure rx ack */
  365. iic_set_clr(pd, ICCR, ICCR_ICE, 0);
  366. /* Set the clock */
  367. iic_wr(pd, ICCL, pd->iccl);
  368. iic_wr(pd, ICCH, pd->icch);
  369. pd->msg = usr_msg;
  370. pd->pos = -1;
  371. pd->sr = 0;
  372. /* Enable all interrupts to begin with */
  373. iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
  374. return 0;
  375. }
  376. static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter,
  377. struct i2c_msg *msgs,
  378. int num)
  379. {
  380. struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
  381. struct i2c_msg *msg;
  382. int err = 0;
  383. u_int8_t val;
  384. int i, k, retry_count;
  385. activate_ch(pd);
  386. /* Process all messages */
  387. for (i = 0; i < num; i++) {
  388. msg = &msgs[i];
  389. err = start_ch(pd, msg);
  390. if (err)
  391. break;
  392. i2c_op(pd, OP_START, 0);
  393. /* The interrupt handler takes care of the rest... */
  394. k = wait_event_timeout(pd->wait,
  395. pd->sr & (ICSR_TACK | SW_DONE),
  396. 5 * HZ);
  397. if (!k)
  398. dev_err(pd->dev, "Transfer request timed out\n");
  399. retry_count = 1000;
  400. again:
  401. val = iic_rd(pd, ICSR);
  402. dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
  403. /* the interrupt handler may wake us up before the
  404. * transfer is finished, so poll the hardware
  405. * until we're done.
  406. */
  407. if (val & ICSR_BUSY) {
  408. udelay(10);
  409. if (retry_count--)
  410. goto again;
  411. err = -EIO;
  412. dev_err(pd->dev, "Polling timed out\n");
  413. break;
  414. }
  415. /* handle missing acknowledge and arbitration lost */
  416. if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) {
  417. err = -EIO;
  418. break;
  419. }
  420. }
  421. deactivate_ch(pd);
  422. if (!err)
  423. err = num;
  424. return err;
  425. }
  426. static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter)
  427. {
  428. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  429. }
  430. static struct i2c_algorithm sh_mobile_i2c_algorithm = {
  431. .functionality = sh_mobile_i2c_func,
  432. .master_xfer = sh_mobile_i2c_xfer,
  433. };
  434. static int sh_mobile_i2c_hook_irqs(struct platform_device *dev, int hook)
  435. {
  436. struct resource *res;
  437. int ret = -ENXIO;
  438. int q, m;
  439. int k = 0;
  440. int n = 0;
  441. while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) {
  442. for (n = res->start; hook && n <= res->end; n++) {
  443. if (request_irq(n, sh_mobile_i2c_isr, IRQF_DISABLED,
  444. dev_name(&dev->dev), dev))
  445. goto rollback;
  446. }
  447. k++;
  448. }
  449. if (hook)
  450. return k > 0 ? 0 : -ENOENT;
  451. k--;
  452. ret = 0;
  453. rollback:
  454. for (q = k; k >= 0; k--) {
  455. for (m = n; m >= res->start; m--)
  456. free_irq(m, dev);
  457. res = platform_get_resource(dev, IORESOURCE_IRQ, k - 1);
  458. m = res->end;
  459. }
  460. return ret;
  461. }
  462. static int sh_mobile_i2c_probe(struct platform_device *dev)
  463. {
  464. struct sh_mobile_i2c_data *pd;
  465. struct i2c_adapter *adap;
  466. struct resource *res;
  467. char clk_name[8];
  468. int size;
  469. int ret;
  470. pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
  471. if (pd == NULL) {
  472. dev_err(&dev->dev, "cannot allocate private data\n");
  473. return -ENOMEM;
  474. }
  475. snprintf(clk_name, sizeof(clk_name), "i2c%d", dev->id);
  476. pd->clk = clk_get(&dev->dev, clk_name);
  477. if (IS_ERR(pd->clk)) {
  478. dev_err(&dev->dev, "cannot get clock \"%s\"\n", clk_name);
  479. ret = PTR_ERR(pd->clk);
  480. goto err;
  481. }
  482. ret = sh_mobile_i2c_hook_irqs(dev, 1);
  483. if (ret) {
  484. dev_err(&dev->dev, "cannot request IRQ\n");
  485. goto err_clk;
  486. }
  487. pd->dev = &dev->dev;
  488. platform_set_drvdata(dev, pd);
  489. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  490. if (res == NULL) {
  491. dev_err(&dev->dev, "cannot find IO resource\n");
  492. ret = -ENOENT;
  493. goto err_irq;
  494. }
  495. size = resource_size(res);
  496. pd->reg = ioremap(res->start, size);
  497. if (pd->reg == NULL) {
  498. dev_err(&dev->dev, "cannot map IO\n");
  499. ret = -ENXIO;
  500. goto err_irq;
  501. }
  502. /* Enable Runtime PM for this device.
  503. *
  504. * Also tell the Runtime PM core to ignore children
  505. * for this device since it is valid for us to suspend
  506. * this I2C master driver even though the slave devices
  507. * on the I2C bus may not be suspended.
  508. *
  509. * The state of the I2C hardware bus is unaffected by
  510. * the Runtime PM state.
  511. */
  512. pm_suspend_ignore_children(&dev->dev, true);
  513. pm_runtime_enable(&dev->dev);
  514. /* setup the private data */
  515. adap = &pd->adap;
  516. i2c_set_adapdata(adap, pd);
  517. adap->owner = THIS_MODULE;
  518. adap->algo = &sh_mobile_i2c_algorithm;
  519. adap->dev.parent = &dev->dev;
  520. adap->retries = 5;
  521. adap->nr = dev->id;
  522. strlcpy(adap->name, dev->name, sizeof(adap->name));
  523. spin_lock_init(&pd->lock);
  524. init_waitqueue_head(&pd->wait);
  525. ret = i2c_add_numbered_adapter(adap);
  526. if (ret < 0) {
  527. dev_err(&dev->dev, "cannot add numbered adapter\n");
  528. goto err_all;
  529. }
  530. return 0;
  531. err_all:
  532. iounmap(pd->reg);
  533. err_irq:
  534. sh_mobile_i2c_hook_irqs(dev, 0);
  535. err_clk:
  536. clk_put(pd->clk);
  537. err:
  538. kfree(pd);
  539. return ret;
  540. }
  541. static int sh_mobile_i2c_remove(struct platform_device *dev)
  542. {
  543. struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
  544. i2c_del_adapter(&pd->adap);
  545. iounmap(pd->reg);
  546. sh_mobile_i2c_hook_irqs(dev, 0);
  547. clk_put(pd->clk);
  548. pm_runtime_disable(&dev->dev);
  549. kfree(pd);
  550. return 0;
  551. }
  552. static int sh_mobile_i2c_runtime_nop(struct device *dev)
  553. {
  554. /* Runtime PM callback shared between ->runtime_suspend()
  555. * and ->runtime_resume(). Simply returns success.
  556. *
  557. * This driver re-initializes all registers after
  558. * pm_runtime_get_sync() anyway so there is no need
  559. * to save and restore registers here.
  560. */
  561. return 0;
  562. }
  563. static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = {
  564. .runtime_suspend = sh_mobile_i2c_runtime_nop,
  565. .runtime_resume = sh_mobile_i2c_runtime_nop,
  566. };
  567. static struct platform_driver sh_mobile_i2c_driver = {
  568. .driver = {
  569. .name = "i2c-sh_mobile",
  570. .owner = THIS_MODULE,
  571. .pm = &sh_mobile_i2c_dev_pm_ops,
  572. },
  573. .probe = sh_mobile_i2c_probe,
  574. .remove = sh_mobile_i2c_remove,
  575. };
  576. static int __init sh_mobile_i2c_adap_init(void)
  577. {
  578. return platform_driver_register(&sh_mobile_i2c_driver);
  579. }
  580. static void __exit sh_mobile_i2c_adap_exit(void)
  581. {
  582. platform_driver_unregister(&sh_mobile_i2c_driver);
  583. }
  584. subsys_initcall(sh_mobile_i2c_adap_init);
  585. module_exit(sh_mobile_i2c_adap_exit);
  586. MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver");
  587. MODULE_AUTHOR("Magnus Damm");
  588. MODULE_LICENSE("GPL v2");