i2c-ibm_iic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * drivers/i2c/busses/i2c-ibm_iic.c
  3. *
  4. * Support for the IIC peripheral on IBM PPC 4xx
  5. *
  6. * Copyright (c) 2003, 2004 Zultys Technologies.
  7. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  8. *
  9. * Copyright (c) 2008 PIKA Technologies
  10. * Sean MacLennan <smaclennan@pikatech.com>
  11. *
  12. * Based on original work by
  13. * Ian DaSilva <idasilva@mvista.com>
  14. * Armin Kuster <akuster@mvista.com>
  15. * Matt Porter <mporter@mvista.com>
  16. *
  17. * Copyright 2000-2003 MontaVista Software Inc.
  18. *
  19. * Original driver version was highly leveraged from i2c-elektor.c
  20. *
  21. * Copyright 1995-97 Simon G. Vogl
  22. * 1998-99 Hans Berglund
  23. *
  24. * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
  25. * and even Frodo Looijaard <frodol@dds.nl>
  26. *
  27. * This program is free software; you can redistribute it and/or modify it
  28. * under the terms of the GNU General Public License as published by the
  29. * Free Software Foundation; either version 2 of the License, or (at your
  30. * option) any later version.
  31. *
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/ioport.h>
  36. #include <linux/delay.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <linux/interrupt.h>
  40. #include <asm/irq.h>
  41. #include <linux/io.h>
  42. #include <linux/i2c.h>
  43. #include <linux/of_platform.h>
  44. #include "i2c-ibm_iic.h"
  45. #define DRIVER_VERSION "2.2"
  46. MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
  47. MODULE_LICENSE("GPL");
  48. static bool iic_force_poll;
  49. module_param(iic_force_poll, bool, 0);
  50. MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
  51. static bool iic_force_fast;
  52. module_param(iic_force_fast, bool, 0);
  53. MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
  54. #define DBG_LEVEL 0
  55. #ifdef DBG
  56. #undef DBG
  57. #endif
  58. #ifdef DBG2
  59. #undef DBG2
  60. #endif
  61. #if DBG_LEVEL > 0
  62. # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
  63. #else
  64. # define DBG(f,x...) ((void)0)
  65. #endif
  66. #if DBG_LEVEL > 1
  67. # define DBG2(f,x...) DBG(f, ##x)
  68. #else
  69. # define DBG2(f,x...) ((void)0)
  70. #endif
  71. #if DBG_LEVEL > 2
  72. static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
  73. {
  74. volatile struct iic_regs __iomem *iic = dev->vaddr;
  75. printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
  76. printk(KERN_DEBUG
  77. " cntl = 0x%02x, mdcntl = 0x%02x\n"
  78. " sts = 0x%02x, extsts = 0x%02x\n"
  79. " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
  80. " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
  81. in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
  82. in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
  83. in_8(&iic->xtcntlss), in_8(&iic->directcntl));
  84. }
  85. # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
  86. #else
  87. # define DUMP_REGS(h,dev) ((void)0)
  88. #endif
  89. /* Bus timings (in ns) for bit-banging */
  90. static struct i2c_timings {
  91. unsigned int hd_sta;
  92. unsigned int su_sto;
  93. unsigned int low;
  94. unsigned int high;
  95. unsigned int buf;
  96. } timings [] = {
  97. /* Standard mode (100 KHz) */
  98. {
  99. .hd_sta = 4000,
  100. .su_sto = 4000,
  101. .low = 4700,
  102. .high = 4000,
  103. .buf = 4700,
  104. },
  105. /* Fast mode (400 KHz) */
  106. {
  107. .hd_sta = 600,
  108. .su_sto = 600,
  109. .low = 1300,
  110. .high = 600,
  111. .buf = 1300,
  112. }};
  113. /* Enable/disable interrupt generation */
  114. static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
  115. {
  116. out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
  117. }
  118. /*
  119. * Initialize IIC interface.
  120. */
  121. static void iic_dev_init(struct ibm_iic_private* dev)
  122. {
  123. volatile struct iic_regs __iomem *iic = dev->vaddr;
  124. DBG("%d: init\n", dev->idx);
  125. /* Clear master address */
  126. out_8(&iic->lmadr, 0);
  127. out_8(&iic->hmadr, 0);
  128. /* Clear slave address */
  129. out_8(&iic->lsadr, 0);
  130. out_8(&iic->hsadr, 0);
  131. /* Clear status & extended status */
  132. out_8(&iic->sts, STS_SCMP | STS_IRQA);
  133. out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
  134. | EXTSTS_ICT | EXTSTS_XFRA);
  135. /* Set clock divider */
  136. out_8(&iic->clkdiv, dev->clckdiv);
  137. /* Clear transfer count */
  138. out_8(&iic->xfrcnt, 0);
  139. /* Clear extended control and status */
  140. out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
  141. | XTCNTLSS_SWS);
  142. /* Clear control register */
  143. out_8(&iic->cntl, 0);
  144. /* Enable interrupts if possible */
  145. iic_interrupt_mode(dev, dev->irq >= 0);
  146. /* Set mode control */
  147. out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
  148. | (dev->fast_mode ? MDCNTL_FSM : 0));
  149. DUMP_REGS("iic_init", dev);
  150. }
  151. /*
  152. * Reset IIC interface
  153. */
  154. static void iic_dev_reset(struct ibm_iic_private* dev)
  155. {
  156. volatile struct iic_regs __iomem *iic = dev->vaddr;
  157. int i;
  158. u8 dc;
  159. DBG("%d: soft reset\n", dev->idx);
  160. DUMP_REGS("reset", dev);
  161. /* Place chip in the reset state */
  162. out_8(&iic->xtcntlss, XTCNTLSS_SRST);
  163. /* Check if bus is free */
  164. dc = in_8(&iic->directcntl);
  165. if (!DIRCTNL_FREE(dc)){
  166. DBG("%d: trying to regain bus control\n", dev->idx);
  167. /* Try to set bus free state */
  168. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  169. /* Wait until we regain bus control */
  170. for (i = 0; i < 100; ++i){
  171. dc = in_8(&iic->directcntl);
  172. if (DIRCTNL_FREE(dc))
  173. break;
  174. /* Toggle SCL line */
  175. dc ^= DIRCNTL_SCC;
  176. out_8(&iic->directcntl, dc);
  177. udelay(10);
  178. dc ^= DIRCNTL_SCC;
  179. out_8(&iic->directcntl, dc);
  180. /* be nice */
  181. cond_resched();
  182. }
  183. }
  184. /* Remove reset */
  185. out_8(&iic->xtcntlss, 0);
  186. /* Reinitialize interface */
  187. iic_dev_init(dev);
  188. }
  189. /*
  190. * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
  191. */
  192. /* Wait for SCL and/or SDA to be high */
  193. static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
  194. {
  195. unsigned long x = jiffies + HZ / 28 + 2;
  196. while ((in_8(&iic->directcntl) & mask) != mask){
  197. if (unlikely(time_after(jiffies, x)))
  198. return -1;
  199. cond_resched();
  200. }
  201. return 0;
  202. }
  203. static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
  204. {
  205. volatile struct iic_regs __iomem *iic = dev->vaddr;
  206. const struct i2c_timings* t = &timings[dev->fast_mode ? 1 : 0];
  207. u8 mask, v, sda;
  208. int i, res;
  209. /* Only 7-bit addresses are supported */
  210. if (unlikely(p->flags & I2C_M_TEN)){
  211. DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
  212. dev->idx);
  213. return -EINVAL;
  214. }
  215. DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
  216. /* Reset IIC interface */
  217. out_8(&iic->xtcntlss, XTCNTLSS_SRST);
  218. /* Wait for bus to become free */
  219. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  220. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
  221. goto err;
  222. ndelay(t->buf);
  223. /* START */
  224. out_8(&iic->directcntl, DIRCNTL_SCC);
  225. sda = 0;
  226. ndelay(t->hd_sta);
  227. /* Send address */
  228. v = (u8)((p->addr << 1) | ((p->flags & I2C_M_RD) ? 1 : 0));
  229. for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
  230. out_8(&iic->directcntl, sda);
  231. ndelay(t->low / 2);
  232. sda = (v & mask) ? DIRCNTL_SDAC : 0;
  233. out_8(&iic->directcntl, sda);
  234. ndelay(t->low / 2);
  235. out_8(&iic->directcntl, DIRCNTL_SCC | sda);
  236. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  237. goto err;
  238. ndelay(t->high);
  239. }
  240. /* ACK */
  241. out_8(&iic->directcntl, sda);
  242. ndelay(t->low / 2);
  243. out_8(&iic->directcntl, DIRCNTL_SDAC);
  244. ndelay(t->low / 2);
  245. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  246. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  247. goto err;
  248. res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
  249. ndelay(t->high);
  250. /* STOP */
  251. out_8(&iic->directcntl, 0);
  252. ndelay(t->low);
  253. out_8(&iic->directcntl, DIRCNTL_SCC);
  254. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  255. goto err;
  256. ndelay(t->su_sto);
  257. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  258. ndelay(t->buf);
  259. DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
  260. out:
  261. /* Remove reset */
  262. out_8(&iic->xtcntlss, 0);
  263. /* Reinitialize interface */
  264. iic_dev_init(dev);
  265. return res;
  266. err:
  267. DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
  268. res = -EREMOTEIO;
  269. goto out;
  270. }
  271. /*
  272. * IIC interrupt handler
  273. */
  274. static irqreturn_t iic_handler(int irq, void *dev_id)
  275. {
  276. struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
  277. volatile struct iic_regs __iomem *iic = dev->vaddr;
  278. DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
  279. dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
  280. /* Acknowledge IRQ and wakeup iic_wait_for_tc */
  281. out_8(&iic->sts, STS_IRQA | STS_SCMP);
  282. wake_up_interruptible(&dev->wq);
  283. return IRQ_HANDLED;
  284. }
  285. /*
  286. * Get master transfer result and clear errors if any.
  287. * Returns the number of actually transferred bytes or error (<0)
  288. */
  289. static int iic_xfer_result(struct ibm_iic_private* dev)
  290. {
  291. volatile struct iic_regs __iomem *iic = dev->vaddr;
  292. if (unlikely(in_8(&iic->sts) & STS_ERR)){
  293. DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
  294. in_8(&iic->extsts));
  295. /* Clear errors and possible pending IRQs */
  296. out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
  297. EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
  298. /* Flush master data buffer */
  299. out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
  300. /* Is bus free?
  301. * If error happened during combined xfer
  302. * IIC interface is usually stuck in some strange
  303. * state, the only way out - soft reset.
  304. */
  305. if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  306. DBG("%d: bus is stuck, resetting\n", dev->idx);
  307. iic_dev_reset(dev);
  308. }
  309. return -EREMOTEIO;
  310. }
  311. else
  312. return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
  313. }
  314. /*
  315. * Try to abort active transfer.
  316. */
  317. static void iic_abort_xfer(struct ibm_iic_private* dev)
  318. {
  319. volatile struct iic_regs __iomem *iic = dev->vaddr;
  320. unsigned long x;
  321. DBG("%d: iic_abort_xfer\n", dev->idx);
  322. out_8(&iic->cntl, CNTL_HMT);
  323. /*
  324. * Wait for the abort command to complete.
  325. * It's not worth to be optimized, just poll (timeout >= 1 tick)
  326. */
  327. x = jiffies + 2;
  328. while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  329. if (time_after(jiffies, x)){
  330. DBG("%d: abort timeout, resetting...\n", dev->idx);
  331. iic_dev_reset(dev);
  332. return;
  333. }
  334. schedule();
  335. }
  336. /* Just to clear errors */
  337. iic_xfer_result(dev);
  338. }
  339. /*
  340. * Wait for master transfer to complete.
  341. * It puts current process to sleep until we get interrupt or timeout expires.
  342. * Returns the number of transferred bytes or error (<0)
  343. */
  344. static int iic_wait_for_tc(struct ibm_iic_private* dev){
  345. volatile struct iic_regs __iomem *iic = dev->vaddr;
  346. int ret = 0;
  347. if (dev->irq >= 0){
  348. /* Interrupt mode */
  349. ret = wait_event_interruptible_timeout(dev->wq,
  350. !(in_8(&iic->sts) & STS_PT), dev->adap.timeout);
  351. if (unlikely(ret < 0))
  352. DBG("%d: wait interrupted\n", dev->idx);
  353. else if (unlikely(in_8(&iic->sts) & STS_PT)){
  354. DBG("%d: wait timeout\n", dev->idx);
  355. ret = -ETIMEDOUT;
  356. }
  357. }
  358. else {
  359. /* Polling mode */
  360. unsigned long x = jiffies + dev->adap.timeout;
  361. while (in_8(&iic->sts) & STS_PT){
  362. if (unlikely(time_after(jiffies, x))){
  363. DBG("%d: poll timeout\n", dev->idx);
  364. ret = -ETIMEDOUT;
  365. break;
  366. }
  367. if (unlikely(signal_pending(current))){
  368. DBG("%d: poll interrupted\n", dev->idx);
  369. ret = -ERESTARTSYS;
  370. break;
  371. }
  372. schedule();
  373. }
  374. }
  375. if (unlikely(ret < 0))
  376. iic_abort_xfer(dev);
  377. else
  378. ret = iic_xfer_result(dev);
  379. DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
  380. return ret;
  381. }
  382. /*
  383. * Low level master transfer routine
  384. */
  385. static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
  386. int combined_xfer)
  387. {
  388. volatile struct iic_regs __iomem *iic = dev->vaddr;
  389. char* buf = pm->buf;
  390. int i, j, loops, ret = 0;
  391. int len = pm->len;
  392. u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
  393. if (pm->flags & I2C_M_RD)
  394. cntl |= CNTL_RW;
  395. loops = (len + 3) / 4;
  396. for (i = 0; i < loops; ++i, len -= 4){
  397. int count = len > 4 ? 4 : len;
  398. u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
  399. if (!(cntl & CNTL_RW))
  400. for (j = 0; j < count; ++j)
  401. out_8((void __iomem *)&iic->mdbuf, *buf++);
  402. if (i < loops - 1)
  403. cmd |= CNTL_CHT;
  404. else if (combined_xfer)
  405. cmd |= CNTL_RPST;
  406. DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
  407. /* Start transfer */
  408. out_8(&iic->cntl, cmd);
  409. /* Wait for completion */
  410. ret = iic_wait_for_tc(dev);
  411. if (unlikely(ret < 0))
  412. break;
  413. else if (unlikely(ret != count)){
  414. DBG("%d: xfer_bytes, requested %d, transferred %d\n",
  415. dev->idx, count, ret);
  416. /* If it's not a last part of xfer, abort it */
  417. if (combined_xfer || (i < loops - 1))
  418. iic_abort_xfer(dev);
  419. ret = -EREMOTEIO;
  420. break;
  421. }
  422. if (cntl & CNTL_RW)
  423. for (j = 0; j < count; ++j)
  424. *buf++ = in_8((void __iomem *)&iic->mdbuf);
  425. }
  426. return ret > 0 ? 0 : ret;
  427. }
  428. /*
  429. * Set target slave address for master transfer
  430. */
  431. static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
  432. {
  433. volatile struct iic_regs __iomem *iic = dev->vaddr;
  434. u16 addr = msg->addr;
  435. DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
  436. addr, msg->flags & I2C_M_TEN ? 10 : 7);
  437. if (msg->flags & I2C_M_TEN){
  438. out_8(&iic->cntl, CNTL_AMD);
  439. out_8(&iic->lmadr, addr);
  440. out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
  441. }
  442. else {
  443. out_8(&iic->cntl, 0);
  444. out_8(&iic->lmadr, addr << 1);
  445. }
  446. }
  447. static inline int iic_invalid_address(const struct i2c_msg* p)
  448. {
  449. return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
  450. }
  451. static inline int iic_address_neq(const struct i2c_msg* p1,
  452. const struct i2c_msg* p2)
  453. {
  454. return (p1->addr != p2->addr)
  455. || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
  456. }
  457. /*
  458. * Generic master transfer entrypoint.
  459. * Returns the number of processed messages or error (<0)
  460. */
  461. static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  462. {
  463. struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
  464. volatile struct iic_regs __iomem *iic = dev->vaddr;
  465. int i, ret = 0;
  466. DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
  467. if (!num)
  468. return 0;
  469. /* Check the sanity of the passed messages.
  470. * Uhh, generic i2c layer is more suitable place for such code...
  471. */
  472. if (unlikely(iic_invalid_address(&msgs[0]))){
  473. DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
  474. msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
  475. return -EINVAL;
  476. }
  477. for (i = 0; i < num; ++i){
  478. if (unlikely(msgs[i].len <= 0)){
  479. if (num == 1 && !msgs[0].len){
  480. /* Special case for I2C_SMBUS_QUICK emulation.
  481. * IBM IIC doesn't support 0-length transactions
  482. * so we have to emulate them using bit-banging.
  483. */
  484. return iic_smbus_quick(dev, &msgs[0]);
  485. }
  486. DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
  487. msgs[i].len, i);
  488. return -EINVAL;
  489. }
  490. if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
  491. DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
  492. return -EINVAL;
  493. }
  494. }
  495. /* Check bus state */
  496. if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
  497. DBG("%d: iic_xfer, bus is not free\n", dev->idx);
  498. /* Usually it means something serious has happened.
  499. * We *cannot* have unfinished previous transfer
  500. * so it doesn't make any sense to try to stop it.
  501. * Probably we were not able to recover from the
  502. * previous error.
  503. * The only *reasonable* thing I can think of here
  504. * is soft reset. --ebs
  505. */
  506. iic_dev_reset(dev);
  507. if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  508. DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
  509. return -EREMOTEIO;
  510. }
  511. }
  512. else {
  513. /* Flush master data buffer (just in case) */
  514. out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
  515. }
  516. /* Load slave address */
  517. iic_address(dev, &msgs[0]);
  518. /* Do real transfer */
  519. for (i = 0; i < num && !ret; ++i)
  520. ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
  521. return ret < 0 ? ret : num;
  522. }
  523. static u32 iic_func(struct i2c_adapter *adap)
  524. {
  525. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  526. }
  527. static const struct i2c_algorithm iic_algo = {
  528. .master_xfer = iic_xfer,
  529. .functionality = iic_func
  530. };
  531. /*
  532. * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
  533. */
  534. static inline u8 iic_clckdiv(unsigned int opb)
  535. {
  536. /* Compatibility kludge, should go away after all cards
  537. * are fixed to fill correct value for opbfreq.
  538. * Previous driver version used hardcoded divider value 4,
  539. * it corresponds to OPB frequency from the range (40, 50] MHz
  540. */
  541. if (!opb){
  542. printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
  543. " fix your board specific setup\n");
  544. opb = 50000000;
  545. }
  546. /* Convert to MHz */
  547. opb /= 1000000;
  548. if (opb < 20 || opb > 150){
  549. printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
  550. opb);
  551. opb = opb < 20 ? 20 : 150;
  552. }
  553. return (u8)((opb + 9) / 10 - 1);
  554. }
  555. static int iic_request_irq(struct platform_device *ofdev,
  556. struct ibm_iic_private *dev)
  557. {
  558. struct device_node *np = ofdev->dev.of_node;
  559. int irq;
  560. if (iic_force_poll)
  561. return 0;
  562. irq = irq_of_parse_and_map(np, 0);
  563. if (!irq) {
  564. dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
  565. return 0;
  566. }
  567. /* Disable interrupts until we finish initialization, assumes
  568. * level-sensitive IRQ setup...
  569. */
  570. iic_interrupt_mode(dev, 0);
  571. if (request_irq(irq, iic_handler, 0, "IBM IIC", dev)) {
  572. dev_err(&ofdev->dev, "request_irq %d failed\n", irq);
  573. /* Fallback to the polling mode */
  574. return 0;
  575. }
  576. return irq;
  577. }
  578. /*
  579. * Register single IIC interface
  580. */
  581. static int iic_probe(struct platform_device *ofdev)
  582. {
  583. struct device_node *np = ofdev->dev.of_node;
  584. struct ibm_iic_private *dev;
  585. struct i2c_adapter *adap;
  586. const u32 *freq;
  587. int ret;
  588. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  589. if (!dev) {
  590. dev_err(&ofdev->dev, "failed to allocate device data\n");
  591. return -ENOMEM;
  592. }
  593. platform_set_drvdata(ofdev, dev);
  594. dev->vaddr = of_iomap(np, 0);
  595. if (dev->vaddr == NULL) {
  596. dev_err(&ofdev->dev, "failed to iomap device\n");
  597. ret = -ENXIO;
  598. goto error_cleanup;
  599. }
  600. init_waitqueue_head(&dev->wq);
  601. dev->irq = iic_request_irq(ofdev, dev);
  602. if (!dev->irq)
  603. dev_warn(&ofdev->dev, "using polling mode\n");
  604. /* Board specific settings */
  605. if (iic_force_fast || of_get_property(np, "fast-mode", NULL))
  606. dev->fast_mode = 1;
  607. freq = of_get_property(np, "clock-frequency", NULL);
  608. if (freq == NULL) {
  609. freq = of_get_property(np->parent, "clock-frequency", NULL);
  610. if (freq == NULL) {
  611. dev_err(&ofdev->dev, "Unable to get bus frequency\n");
  612. ret = -EINVAL;
  613. goto error_cleanup;
  614. }
  615. }
  616. dev->clckdiv = iic_clckdiv(*freq);
  617. dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv);
  618. /* Initialize IIC interface */
  619. iic_dev_init(dev);
  620. /* Register it with i2c layer */
  621. adap = &dev->adap;
  622. adap->dev.parent = &ofdev->dev;
  623. adap->dev.of_node = of_node_get(np);
  624. strlcpy(adap->name, "IBM IIC", sizeof(adap->name));
  625. i2c_set_adapdata(adap, dev);
  626. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  627. adap->algo = &iic_algo;
  628. adap->timeout = HZ;
  629. ret = i2c_add_adapter(adap);
  630. if (ret < 0) {
  631. dev_err(&ofdev->dev, "failed to register i2c adapter\n");
  632. goto error_cleanup;
  633. }
  634. dev_info(&ofdev->dev, "using %s mode\n",
  635. dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
  636. return 0;
  637. error_cleanup:
  638. if (dev->irq) {
  639. iic_interrupt_mode(dev, 0);
  640. free_irq(dev->irq, dev);
  641. }
  642. if (dev->vaddr)
  643. iounmap(dev->vaddr);
  644. kfree(dev);
  645. return ret;
  646. }
  647. /*
  648. * Cleanup initialized IIC interface
  649. */
  650. static int iic_remove(struct platform_device *ofdev)
  651. {
  652. struct ibm_iic_private *dev = platform_get_drvdata(ofdev);
  653. i2c_del_adapter(&dev->adap);
  654. if (dev->irq) {
  655. iic_interrupt_mode(dev, 0);
  656. free_irq(dev->irq, dev);
  657. }
  658. iounmap(dev->vaddr);
  659. kfree(dev);
  660. return 0;
  661. }
  662. static const struct of_device_id ibm_iic_match[] = {
  663. { .compatible = "ibm,iic", },
  664. {}
  665. };
  666. static struct platform_driver ibm_iic_driver = {
  667. .driver = {
  668. .name = "ibm-iic",
  669. .owner = THIS_MODULE,
  670. .of_match_table = ibm_iic_match,
  671. },
  672. .probe = iic_probe,
  673. .remove = iic_remove,
  674. };
  675. module_platform_driver(ibm_iic_driver);