i2c-ibm_iic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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/interrupt.h>
  39. #include <linux/sched/signal.h>
  40. #include <asm/irq.h>
  41. #include <linux/io.h>
  42. #include <linux/i2c.h>
  43. #include <linux/of_address.h>
  44. #include <linux/of_irq.h>
  45. #include <linux/of_platform.h>
  46. #include "i2c-ibm_iic.h"
  47. #define DRIVER_VERSION "2.2"
  48. MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
  49. MODULE_LICENSE("GPL");
  50. static bool iic_force_poll;
  51. module_param(iic_force_poll, bool, 0);
  52. MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
  53. static bool iic_force_fast;
  54. module_param(iic_force_fast, bool, 0);
  55. MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
  56. #define DBG_LEVEL 0
  57. #ifdef DBG
  58. #undef DBG
  59. #endif
  60. #ifdef DBG2
  61. #undef DBG2
  62. #endif
  63. #if DBG_LEVEL > 0
  64. # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
  65. #else
  66. # define DBG(f,x...) ((void)0)
  67. #endif
  68. #if DBG_LEVEL > 1
  69. # define DBG2(f,x...) DBG(f, ##x)
  70. #else
  71. # define DBG2(f,x...) ((void)0)
  72. #endif
  73. #if DBG_LEVEL > 2
  74. static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
  75. {
  76. volatile struct iic_regs __iomem *iic = dev->vaddr;
  77. printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
  78. printk(KERN_DEBUG
  79. " cntl = 0x%02x, mdcntl = 0x%02x\n"
  80. " sts = 0x%02x, extsts = 0x%02x\n"
  81. " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
  82. " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
  83. in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
  84. in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
  85. in_8(&iic->xtcntlss), in_8(&iic->directcntl));
  86. }
  87. # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
  88. #else
  89. # define DUMP_REGS(h,dev) ((void)0)
  90. #endif
  91. /* Bus timings (in ns) for bit-banging */
  92. static struct ibm_iic_timings {
  93. unsigned int hd_sta;
  94. unsigned int su_sto;
  95. unsigned int low;
  96. unsigned int high;
  97. unsigned int buf;
  98. } timings [] = {
  99. /* Standard mode (100 KHz) */
  100. {
  101. .hd_sta = 4000,
  102. .su_sto = 4000,
  103. .low = 4700,
  104. .high = 4000,
  105. .buf = 4700,
  106. },
  107. /* Fast mode (400 KHz) */
  108. {
  109. .hd_sta = 600,
  110. .su_sto = 600,
  111. .low = 1300,
  112. .high = 600,
  113. .buf = 1300,
  114. }};
  115. /* Enable/disable interrupt generation */
  116. static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
  117. {
  118. out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
  119. }
  120. /*
  121. * Initialize IIC interface.
  122. */
  123. static void iic_dev_init(struct ibm_iic_private* dev)
  124. {
  125. volatile struct iic_regs __iomem *iic = dev->vaddr;
  126. DBG("%d: init\n", dev->idx);
  127. /* Clear master address */
  128. out_8(&iic->lmadr, 0);
  129. out_8(&iic->hmadr, 0);
  130. /* Clear slave address */
  131. out_8(&iic->lsadr, 0);
  132. out_8(&iic->hsadr, 0);
  133. /* Clear status & extended status */
  134. out_8(&iic->sts, STS_SCMP | STS_IRQA);
  135. out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
  136. | EXTSTS_ICT | EXTSTS_XFRA);
  137. /* Set clock divider */
  138. out_8(&iic->clkdiv, dev->clckdiv);
  139. /* Clear transfer count */
  140. out_8(&iic->xfrcnt, 0);
  141. /* Clear extended control and status */
  142. out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
  143. | XTCNTLSS_SWS);
  144. /* Clear control register */
  145. out_8(&iic->cntl, 0);
  146. /* Enable interrupts if possible */
  147. iic_interrupt_mode(dev, dev->irq >= 0);
  148. /* Set mode control */
  149. out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
  150. | (dev->fast_mode ? MDCNTL_FSM : 0));
  151. DUMP_REGS("iic_init", dev);
  152. }
  153. /*
  154. * Reset IIC interface
  155. */
  156. static void iic_dev_reset(struct ibm_iic_private* dev)
  157. {
  158. volatile struct iic_regs __iomem *iic = dev->vaddr;
  159. int i;
  160. u8 dc;
  161. DBG("%d: soft reset\n", dev->idx);
  162. DUMP_REGS("reset", dev);
  163. /* Place chip in the reset state */
  164. out_8(&iic->xtcntlss, XTCNTLSS_SRST);
  165. /* Check if bus is free */
  166. dc = in_8(&iic->directcntl);
  167. if (!DIRCTNL_FREE(dc)){
  168. DBG("%d: trying to regain bus control\n", dev->idx);
  169. /* Try to set bus free state */
  170. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  171. /* Wait until we regain bus control */
  172. for (i = 0; i < 100; ++i){
  173. dc = in_8(&iic->directcntl);
  174. if (DIRCTNL_FREE(dc))
  175. break;
  176. /* Toggle SCL line */
  177. dc ^= DIRCNTL_SCC;
  178. out_8(&iic->directcntl, dc);
  179. udelay(10);
  180. dc ^= DIRCNTL_SCC;
  181. out_8(&iic->directcntl, dc);
  182. /* be nice */
  183. cond_resched();
  184. }
  185. }
  186. /* Remove reset */
  187. out_8(&iic->xtcntlss, 0);
  188. /* Reinitialize interface */
  189. iic_dev_init(dev);
  190. }
  191. /*
  192. * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
  193. */
  194. /* Wait for SCL and/or SDA to be high */
  195. static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
  196. {
  197. unsigned long x = jiffies + HZ / 28 + 2;
  198. while ((in_8(&iic->directcntl) & mask) != mask){
  199. if (unlikely(time_after(jiffies, x)))
  200. return -1;
  201. cond_resched();
  202. }
  203. return 0;
  204. }
  205. static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
  206. {
  207. volatile struct iic_regs __iomem *iic = dev->vaddr;
  208. const struct ibm_iic_timings *t = &timings[dev->fast_mode ? 1 : 0];
  209. u8 mask, v, sda;
  210. int i, res;
  211. /* Only 7-bit addresses are supported */
  212. if (unlikely(p->flags & I2C_M_TEN)){
  213. DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
  214. dev->idx);
  215. return -EINVAL;
  216. }
  217. DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
  218. /* Reset IIC interface */
  219. out_8(&iic->xtcntlss, XTCNTLSS_SRST);
  220. /* Wait for bus to become free */
  221. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  222. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
  223. goto err;
  224. ndelay(t->buf);
  225. /* START */
  226. out_8(&iic->directcntl, DIRCNTL_SCC);
  227. sda = 0;
  228. ndelay(t->hd_sta);
  229. /* Send address */
  230. v = i2c_8bit_addr_from_msg(p);
  231. for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
  232. out_8(&iic->directcntl, sda);
  233. ndelay(t->low / 2);
  234. sda = (v & mask) ? DIRCNTL_SDAC : 0;
  235. out_8(&iic->directcntl, sda);
  236. ndelay(t->low / 2);
  237. out_8(&iic->directcntl, DIRCNTL_SCC | sda);
  238. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  239. goto err;
  240. ndelay(t->high);
  241. }
  242. /* ACK */
  243. out_8(&iic->directcntl, sda);
  244. ndelay(t->low / 2);
  245. out_8(&iic->directcntl, DIRCNTL_SDAC);
  246. ndelay(t->low / 2);
  247. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  248. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  249. goto err;
  250. res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
  251. ndelay(t->high);
  252. /* STOP */
  253. out_8(&iic->directcntl, 0);
  254. ndelay(t->low);
  255. out_8(&iic->directcntl, DIRCNTL_SCC);
  256. if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
  257. goto err;
  258. ndelay(t->su_sto);
  259. out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
  260. ndelay(t->buf);
  261. DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
  262. out:
  263. /* Remove reset */
  264. out_8(&iic->xtcntlss, 0);
  265. /* Reinitialize interface */
  266. iic_dev_init(dev);
  267. return res;
  268. err:
  269. DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
  270. res = -EREMOTEIO;
  271. goto out;
  272. }
  273. /*
  274. * IIC interrupt handler
  275. */
  276. static irqreturn_t iic_handler(int irq, void *dev_id)
  277. {
  278. struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
  279. volatile struct iic_regs __iomem *iic = dev->vaddr;
  280. DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
  281. dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
  282. /* Acknowledge IRQ and wakeup iic_wait_for_tc */
  283. out_8(&iic->sts, STS_IRQA | STS_SCMP);
  284. wake_up_interruptible(&dev->wq);
  285. return IRQ_HANDLED;
  286. }
  287. /*
  288. * Get master transfer result and clear errors if any.
  289. * Returns the number of actually transferred bytes or error (<0)
  290. */
  291. static int iic_xfer_result(struct ibm_iic_private* dev)
  292. {
  293. volatile struct iic_regs __iomem *iic = dev->vaddr;
  294. if (unlikely(in_8(&iic->sts) & STS_ERR)){
  295. DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
  296. in_8(&iic->extsts));
  297. /* Clear errors and possible pending IRQs */
  298. out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
  299. EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
  300. /* Flush master data buffer */
  301. out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
  302. /* Is bus free?
  303. * If error happened during combined xfer
  304. * IIC interface is usually stuck in some strange
  305. * state, the only way out - soft reset.
  306. */
  307. if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  308. DBG("%d: bus is stuck, resetting\n", dev->idx);
  309. iic_dev_reset(dev);
  310. }
  311. return -EREMOTEIO;
  312. }
  313. else
  314. return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
  315. }
  316. /*
  317. * Try to abort active transfer.
  318. */
  319. static void iic_abort_xfer(struct ibm_iic_private* dev)
  320. {
  321. volatile struct iic_regs __iomem *iic = dev->vaddr;
  322. unsigned long x;
  323. DBG("%d: iic_abort_xfer\n", dev->idx);
  324. out_8(&iic->cntl, CNTL_HMT);
  325. /*
  326. * Wait for the abort command to complete.
  327. * It's not worth to be optimized, just poll (timeout >= 1 tick)
  328. */
  329. x = jiffies + 2;
  330. while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
  331. if (time_after(jiffies, x)){
  332. DBG("%d: abort timeout, resetting...\n", dev->idx);
  333. iic_dev_reset(dev);
  334. return;
  335. }
  336. schedule();
  337. }
  338. /* Just to clear errors */
  339. iic_xfer_result(dev);
  340. }
  341. /*
  342. * Wait for master transfer to complete.
  343. * It puts current process to sleep until we get interrupt or timeout expires.
  344. * Returns the number of transferred bytes or error (<0)
  345. */
  346. static int iic_wait_for_tc(struct ibm_iic_private* dev){
  347. volatile struct iic_regs __iomem *iic = dev->vaddr;
  348. int ret = 0;
  349. if (dev->irq >= 0){
  350. /* Interrupt mode */
  351. ret = wait_event_interruptible_timeout(dev->wq,
  352. !(in_8(&iic->sts) & STS_PT), dev->adap.timeout);
  353. if (unlikely(ret < 0))
  354. DBG("%d: wait interrupted\n", dev->idx);
  355. else if (unlikely(in_8(&iic->sts) & STS_PT)){
  356. DBG("%d: wait timeout\n", dev->idx);
  357. ret = -ETIMEDOUT;
  358. }
  359. }
  360. else {
  361. /* Polling mode */
  362. unsigned long x = jiffies + dev->adap.timeout;
  363. while (in_8(&iic->sts) & STS_PT){
  364. if (unlikely(time_after(jiffies, x))){
  365. DBG("%d: poll timeout\n", dev->idx);
  366. ret = -ETIMEDOUT;
  367. break;
  368. }
  369. if (unlikely(signal_pending(current))){
  370. DBG("%d: poll interrupted\n", dev->idx);
  371. ret = -ERESTARTSYS;
  372. break;
  373. }
  374. schedule();
  375. }
  376. }
  377. if (unlikely(ret < 0))
  378. iic_abort_xfer(dev);
  379. else
  380. ret = iic_xfer_result(dev);
  381. DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
  382. return ret;
  383. }
  384. /*
  385. * Low level master transfer routine
  386. */
  387. static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
  388. int combined_xfer)
  389. {
  390. volatile struct iic_regs __iomem *iic = dev->vaddr;
  391. char* buf = pm->buf;
  392. int i, j, loops, ret = 0;
  393. int len = pm->len;
  394. u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
  395. if (pm->flags & I2C_M_RD)
  396. cntl |= CNTL_RW;
  397. loops = (len + 3) / 4;
  398. for (i = 0; i < loops; ++i, len -= 4){
  399. int count = len > 4 ? 4 : len;
  400. u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
  401. if (!(cntl & CNTL_RW))
  402. for (j = 0; j < count; ++j)
  403. out_8((void __iomem *)&iic->mdbuf, *buf++);
  404. if (i < loops - 1)
  405. cmd |= CNTL_CHT;
  406. else if (combined_xfer)
  407. cmd |= CNTL_RPST;
  408. DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
  409. /* Start transfer */
  410. out_8(&iic->cntl, cmd);
  411. /* Wait for completion */
  412. ret = iic_wait_for_tc(dev);
  413. if (unlikely(ret < 0))
  414. break;
  415. else if (unlikely(ret != count)){
  416. DBG("%d: xfer_bytes, requested %d, transferred %d\n",
  417. dev->idx, count, ret);
  418. /* If it's not a last part of xfer, abort it */
  419. if (combined_xfer || (i < loops - 1))
  420. iic_abort_xfer(dev);
  421. ret = -EREMOTEIO;
  422. break;
  423. }
  424. if (cntl & CNTL_RW)
  425. for (j = 0; j < count; ++j)
  426. *buf++ = in_8((void __iomem *)&iic->mdbuf);
  427. }
  428. return ret > 0 ? 0 : ret;
  429. }
  430. /*
  431. * Set target slave address for master transfer
  432. */
  433. static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
  434. {
  435. volatile struct iic_regs __iomem *iic = dev->vaddr;
  436. u16 addr = msg->addr;
  437. DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
  438. addr, msg->flags & I2C_M_TEN ? 10 : 7);
  439. if (msg->flags & I2C_M_TEN){
  440. out_8(&iic->cntl, CNTL_AMD);
  441. out_8(&iic->lmadr, addr);
  442. out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
  443. }
  444. else {
  445. out_8(&iic->cntl, 0);
  446. out_8(&iic->lmadr, addr << 1);
  447. }
  448. }
  449. static inline int iic_invalid_address(const struct i2c_msg* p)
  450. {
  451. return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
  452. }
  453. static inline int iic_address_neq(const struct i2c_msg* p1,
  454. const struct i2c_msg* p2)
  455. {
  456. return (p1->addr != p2->addr)
  457. || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
  458. }
  459. /*
  460. * Generic master transfer entrypoint.
  461. * Returns the number of processed messages or error (<0)
  462. */
  463. static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  464. {
  465. struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
  466. volatile struct iic_regs __iomem *iic = dev->vaddr;
  467. int i, ret = 0;
  468. DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
  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. goto error_cleanup;
  632. dev_info(&ofdev->dev, "using %s mode\n",
  633. dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
  634. return 0;
  635. error_cleanup:
  636. if (dev->irq) {
  637. iic_interrupt_mode(dev, 0);
  638. free_irq(dev->irq, dev);
  639. }
  640. if (dev->vaddr)
  641. iounmap(dev->vaddr);
  642. kfree(dev);
  643. return ret;
  644. }
  645. /*
  646. * Cleanup initialized IIC interface
  647. */
  648. static int iic_remove(struct platform_device *ofdev)
  649. {
  650. struct ibm_iic_private *dev = platform_get_drvdata(ofdev);
  651. i2c_del_adapter(&dev->adap);
  652. if (dev->irq) {
  653. iic_interrupt_mode(dev, 0);
  654. free_irq(dev->irq, dev);
  655. }
  656. iounmap(dev->vaddr);
  657. kfree(dev);
  658. return 0;
  659. }
  660. static const struct of_device_id ibm_iic_match[] = {
  661. { .compatible = "ibm,iic", },
  662. {}
  663. };
  664. MODULE_DEVICE_TABLE(of, ibm_iic_match);
  665. static struct platform_driver ibm_iic_driver = {
  666. .driver = {
  667. .name = "ibm-iic",
  668. .of_match_table = ibm_iic_match,
  669. },
  670. .probe = iic_probe,
  671. .remove = iic_remove,
  672. };
  673. module_platform_driver(ibm_iic_driver);