i2c-sh_mobile.c 19 KB

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