i2c-sh_mobile.c 20 KB

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