i2c-s3c2410.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /* linux/drivers/i2c/busses/i2c-s3c2410.c
  2. *
  3. * Copyright (C) 2004,2005 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 I2C Controller
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  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/i2c.h>
  25. #include <linux/i2c-id.h>
  26. #include <linux/init.h>
  27. #include <linux/time.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/errno.h>
  31. #include <linux/err.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/clk.h>
  34. #include <linux/cpufreq.h>
  35. #include <asm/irq.h>
  36. #include <asm/io.h>
  37. #include <asm/plat-s3c/regs-iic.h>
  38. #include <asm/plat-s3c/iic.h>
  39. /* i2c controller state */
  40. enum s3c24xx_i2c_state {
  41. STATE_IDLE,
  42. STATE_START,
  43. STATE_READ,
  44. STATE_WRITE,
  45. STATE_STOP
  46. };
  47. struct s3c24xx_i2c {
  48. spinlock_t lock;
  49. wait_queue_head_t wait;
  50. struct i2c_msg *msg;
  51. unsigned int msg_num;
  52. unsigned int msg_idx;
  53. unsigned int msg_ptr;
  54. unsigned int tx_setup;
  55. enum s3c24xx_i2c_state state;
  56. unsigned long clkrate;
  57. void __iomem *regs;
  58. struct clk *clk;
  59. struct device *dev;
  60. struct resource *irq;
  61. struct resource *ioarea;
  62. struct i2c_adapter adap;
  63. #ifdef CONFIG_CPU_FREQ
  64. struct notifier_block freq_transition;
  65. #endif
  66. };
  67. /* default platform data removed, dev should always carry data. */
  68. /* s3c24xx_i2c_is2440()
  69. *
  70. * return true is this is an s3c2440
  71. */
  72. static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
  73. {
  74. struct platform_device *pdev = to_platform_device(i2c->dev);
  75. return !strcmp(pdev->name, "s3c2440-i2c");
  76. }
  77. /* s3c24xx_i2c_master_complete
  78. *
  79. * complete the message and wake up the caller, using the given return code,
  80. * or zero to mean ok.
  81. */
  82. static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
  83. {
  84. dev_dbg(i2c->dev, "master_complete %d\n", ret);
  85. i2c->msg_ptr = 0;
  86. i2c->msg = NULL;
  87. i2c->msg_idx++;
  88. i2c->msg_num = 0;
  89. if (ret)
  90. i2c->msg_idx = ret;
  91. wake_up(&i2c->wait);
  92. }
  93. static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
  94. {
  95. unsigned long tmp;
  96. tmp = readl(i2c->regs + S3C2410_IICCON);
  97. writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
  98. }
  99. static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
  100. {
  101. unsigned long tmp;
  102. tmp = readl(i2c->regs + S3C2410_IICCON);
  103. writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
  104. }
  105. /* irq enable/disable functions */
  106. static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
  107. {
  108. unsigned long tmp;
  109. tmp = readl(i2c->regs + S3C2410_IICCON);
  110. writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
  111. }
  112. static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
  113. {
  114. unsigned long tmp;
  115. tmp = readl(i2c->regs + S3C2410_IICCON);
  116. writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
  117. }
  118. /* s3c24xx_i2c_message_start
  119. *
  120. * put the start of a message onto the bus
  121. */
  122. static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
  123. struct i2c_msg *msg)
  124. {
  125. unsigned int addr = (msg->addr & 0x7f) << 1;
  126. unsigned long stat;
  127. unsigned long iiccon;
  128. stat = 0;
  129. stat |= S3C2410_IICSTAT_TXRXEN;
  130. if (msg->flags & I2C_M_RD) {
  131. stat |= S3C2410_IICSTAT_MASTER_RX;
  132. addr |= 1;
  133. } else
  134. stat |= S3C2410_IICSTAT_MASTER_TX;
  135. if (msg->flags & I2C_M_REV_DIR_ADDR)
  136. addr ^= 1;
  137. /* todo - check for wether ack wanted or not */
  138. s3c24xx_i2c_enable_ack(i2c);
  139. iiccon = readl(i2c->regs + S3C2410_IICCON);
  140. writel(stat, i2c->regs + S3C2410_IICSTAT);
  141. dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
  142. writeb(addr, i2c->regs + S3C2410_IICDS);
  143. /* delay here to ensure the data byte has gotten onto the bus
  144. * before the transaction is started */
  145. ndelay(i2c->tx_setup);
  146. dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
  147. writel(iiccon, i2c->regs + S3C2410_IICCON);
  148. stat |= S3C2410_IICSTAT_START;
  149. writel(stat, i2c->regs + S3C2410_IICSTAT);
  150. }
  151. static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
  152. {
  153. unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  154. dev_dbg(i2c->dev, "STOP\n");
  155. /* stop the transfer */
  156. iicstat &= ~S3C2410_IICSTAT_START;
  157. writel(iicstat, i2c->regs + S3C2410_IICSTAT);
  158. i2c->state = STATE_STOP;
  159. s3c24xx_i2c_master_complete(i2c, ret);
  160. s3c24xx_i2c_disable_irq(i2c);
  161. }
  162. /* helper functions to determine the current state in the set of
  163. * messages we are sending */
  164. /* is_lastmsg()
  165. *
  166. * returns TRUE if the current message is the last in the set
  167. */
  168. static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
  169. {
  170. return i2c->msg_idx >= (i2c->msg_num - 1);
  171. }
  172. /* is_msglast
  173. *
  174. * returns TRUE if we this is the last byte in the current message
  175. */
  176. static inline int is_msglast(struct s3c24xx_i2c *i2c)
  177. {
  178. return i2c->msg_ptr == i2c->msg->len-1;
  179. }
  180. /* is_msgend
  181. *
  182. * returns TRUE if we reached the end of the current message
  183. */
  184. static inline int is_msgend(struct s3c24xx_i2c *i2c)
  185. {
  186. return i2c->msg_ptr >= i2c->msg->len;
  187. }
  188. /* i2s_s3c_irq_nextbyte
  189. *
  190. * process an interrupt and work out what to do
  191. */
  192. static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
  193. {
  194. unsigned long tmp;
  195. unsigned char byte;
  196. int ret = 0;
  197. switch (i2c->state) {
  198. case STATE_IDLE:
  199. dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
  200. goto out;
  201. break;
  202. case STATE_STOP:
  203. dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
  204. s3c24xx_i2c_disable_irq(i2c);
  205. goto out_ack;
  206. case STATE_START:
  207. /* last thing we did was send a start condition on the
  208. * bus, or started a new i2c message
  209. */
  210. if (iicstat & S3C2410_IICSTAT_LASTBIT &&
  211. !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  212. /* ack was not received... */
  213. dev_dbg(i2c->dev, "ack was not received\n");
  214. s3c24xx_i2c_stop(i2c, -ENXIO);
  215. goto out_ack;
  216. }
  217. if (i2c->msg->flags & I2C_M_RD)
  218. i2c->state = STATE_READ;
  219. else
  220. i2c->state = STATE_WRITE;
  221. /* terminate the transfer if there is nothing to do
  222. * as this is used by the i2c probe to find devices. */
  223. if (is_lastmsg(i2c) && i2c->msg->len == 0) {
  224. s3c24xx_i2c_stop(i2c, 0);
  225. goto out_ack;
  226. }
  227. if (i2c->state == STATE_READ)
  228. goto prepare_read;
  229. /* fall through to the write state, as we will need to
  230. * send a byte as well */
  231. case STATE_WRITE:
  232. /* we are writing data to the device... check for the
  233. * end of the message, and if so, work out what to do
  234. */
  235. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
  236. if (iicstat & S3C2410_IICSTAT_LASTBIT) {
  237. dev_dbg(i2c->dev, "WRITE: No Ack\n");
  238. s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
  239. goto out_ack;
  240. }
  241. }
  242. retry_write:
  243. if (!is_msgend(i2c)) {
  244. byte = i2c->msg->buf[i2c->msg_ptr++];
  245. writeb(byte, i2c->regs + S3C2410_IICDS);
  246. /* delay after writing the byte to allow the
  247. * data setup time on the bus, as writing the
  248. * data to the register causes the first bit
  249. * to appear on SDA, and SCL will change as
  250. * soon as the interrupt is acknowledged */
  251. ndelay(i2c->tx_setup);
  252. } else if (!is_lastmsg(i2c)) {
  253. /* we need to go to the next i2c message */
  254. dev_dbg(i2c->dev, "WRITE: Next Message\n");
  255. i2c->msg_ptr = 0;
  256. i2c->msg_idx++;
  257. i2c->msg++;
  258. /* check to see if we need to do another message */
  259. if (i2c->msg->flags & I2C_M_NOSTART) {
  260. if (i2c->msg->flags & I2C_M_RD) {
  261. /* cannot do this, the controller
  262. * forces us to send a new START
  263. * when we change direction */
  264. s3c24xx_i2c_stop(i2c, -EINVAL);
  265. }
  266. goto retry_write;
  267. } else {
  268. /* send the new start */
  269. s3c24xx_i2c_message_start(i2c, i2c->msg);
  270. i2c->state = STATE_START;
  271. }
  272. } else {
  273. /* send stop */
  274. s3c24xx_i2c_stop(i2c, 0);
  275. }
  276. break;
  277. case STATE_READ:
  278. /* we have a byte of data in the data register, do
  279. * something with it, and then work out wether we are
  280. * going to do any more read/write
  281. */
  282. byte = readb(i2c->regs + S3C2410_IICDS);
  283. i2c->msg->buf[i2c->msg_ptr++] = byte;
  284. prepare_read:
  285. if (is_msglast(i2c)) {
  286. /* last byte of buffer */
  287. if (is_lastmsg(i2c))
  288. s3c24xx_i2c_disable_ack(i2c);
  289. } else if (is_msgend(i2c)) {
  290. /* ok, we've read the entire buffer, see if there
  291. * is anything else we need to do */
  292. if (is_lastmsg(i2c)) {
  293. /* last message, send stop and complete */
  294. dev_dbg(i2c->dev, "READ: Send Stop\n");
  295. s3c24xx_i2c_stop(i2c, 0);
  296. } else {
  297. /* go to the next transfer */
  298. dev_dbg(i2c->dev, "READ: Next Transfer\n");
  299. i2c->msg_ptr = 0;
  300. i2c->msg_idx++;
  301. i2c->msg++;
  302. }
  303. }
  304. break;
  305. }
  306. /* acknowlegde the IRQ and get back on with the work */
  307. out_ack:
  308. tmp = readl(i2c->regs + S3C2410_IICCON);
  309. tmp &= ~S3C2410_IICCON_IRQPEND;
  310. writel(tmp, i2c->regs + S3C2410_IICCON);
  311. out:
  312. return ret;
  313. }
  314. /* s3c24xx_i2c_irq
  315. *
  316. * top level IRQ servicing routine
  317. */
  318. static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
  319. {
  320. struct s3c24xx_i2c *i2c = dev_id;
  321. unsigned long status;
  322. unsigned long tmp;
  323. status = readl(i2c->regs + S3C2410_IICSTAT);
  324. if (status & S3C2410_IICSTAT_ARBITR) {
  325. /* deal with arbitration loss */
  326. dev_err(i2c->dev, "deal with arbitration loss\n");
  327. }
  328. if (i2c->state == STATE_IDLE) {
  329. dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
  330. tmp = readl(i2c->regs + S3C2410_IICCON);
  331. tmp &= ~S3C2410_IICCON_IRQPEND;
  332. writel(tmp, i2c->regs + S3C2410_IICCON);
  333. goto out;
  334. }
  335. /* pretty much this leaves us with the fact that we've
  336. * transmitted or received whatever byte we last sent */
  337. i2s_s3c_irq_nextbyte(i2c, status);
  338. out:
  339. return IRQ_HANDLED;
  340. }
  341. /* s3c24xx_i2c_set_master
  342. *
  343. * get the i2c bus for a master transaction
  344. */
  345. static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
  346. {
  347. unsigned long iicstat;
  348. int timeout = 400;
  349. while (timeout-- > 0) {
  350. iicstat = readl(i2c->regs + S3C2410_IICSTAT);
  351. if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
  352. return 0;
  353. msleep(1);
  354. }
  355. return -ETIMEDOUT;
  356. }
  357. /* s3c24xx_i2c_doxfer
  358. *
  359. * this starts an i2c transfer
  360. */
  361. static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
  362. struct i2c_msg *msgs, int num)
  363. {
  364. unsigned long timeout;
  365. int ret;
  366. if (!(readl(i2c->regs + S3C2410_IICCON) & S3C2410_IICCON_IRQEN))
  367. return -EIO;
  368. ret = s3c24xx_i2c_set_master(i2c);
  369. if (ret != 0) {
  370. dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
  371. ret = -EAGAIN;
  372. goto out;
  373. }
  374. spin_lock_irq(&i2c->lock);
  375. i2c->msg = msgs;
  376. i2c->msg_num = num;
  377. i2c->msg_ptr = 0;
  378. i2c->msg_idx = 0;
  379. i2c->state = STATE_START;
  380. s3c24xx_i2c_enable_irq(i2c);
  381. s3c24xx_i2c_message_start(i2c, msgs);
  382. spin_unlock_irq(&i2c->lock);
  383. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  384. ret = i2c->msg_idx;
  385. /* having these next two as dev_err() makes life very
  386. * noisy when doing an i2cdetect */
  387. if (timeout == 0)
  388. dev_dbg(i2c->dev, "timeout\n");
  389. else if (ret != num)
  390. dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
  391. /* ensure the stop has been through the bus */
  392. msleep(1);
  393. out:
  394. return ret;
  395. }
  396. /* s3c24xx_i2c_xfer
  397. *
  398. * first port of call from the i2c bus code when an message needs
  399. * transferring across the i2c bus.
  400. */
  401. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
  402. struct i2c_msg *msgs, int num)
  403. {
  404. struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  405. int retry;
  406. int ret;
  407. for (retry = 0; retry < adap->retries; retry++) {
  408. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  409. if (ret != -EAGAIN)
  410. return ret;
  411. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  412. udelay(100);
  413. }
  414. return -EREMOTEIO;
  415. }
  416. /* declare our i2c functionality */
  417. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  418. {
  419. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  420. }
  421. /* i2c bus registration info */
  422. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  423. .master_xfer = s3c24xx_i2c_xfer,
  424. .functionality = s3c24xx_i2c_func,
  425. };
  426. static struct s3c24xx_i2c s3c24xx_i2c = {
  427. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock),
  428. .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
  429. .tx_setup = 50,
  430. .adap = {
  431. .name = "s3c2410-i2c",
  432. .owner = THIS_MODULE,
  433. .algo = &s3c24xx_i2c_algorithm,
  434. .retries = 2,
  435. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  436. },
  437. };
  438. /* s3c24xx_i2c_calcdivisor
  439. *
  440. * return the divisor settings for a given frequency
  441. */
  442. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  443. unsigned int *div1, unsigned int *divs)
  444. {
  445. unsigned int calc_divs = clkin / wanted;
  446. unsigned int calc_div1;
  447. if (calc_divs > (16*16))
  448. calc_div1 = 512;
  449. else
  450. calc_div1 = 16;
  451. calc_divs += calc_div1-1;
  452. calc_divs /= calc_div1;
  453. if (calc_divs == 0)
  454. calc_divs = 1;
  455. if (calc_divs > 17)
  456. calc_divs = 17;
  457. *divs = calc_divs;
  458. *div1 = calc_div1;
  459. return clkin / (calc_divs * calc_div1);
  460. }
  461. /* freq_acceptable
  462. *
  463. * test wether a frequency is within the acceptable range of error
  464. */
  465. static inline int freq_acceptable(unsigned int freq, unsigned int wanted)
  466. {
  467. int diff = freq - wanted;
  468. return diff >= -2 && diff <= 2;
  469. }
  470. /* s3c24xx_i2c_clockrate
  471. *
  472. * work out a divisor for the user requested frequency setting,
  473. * either by the requested frequency, or scanning the acceptable
  474. * range of frequencies until something is found
  475. */
  476. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  477. {
  478. struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data;
  479. unsigned long clkin = clk_get_rate(i2c->clk);
  480. unsigned int divs, div1;
  481. u32 iiccon;
  482. int freq;
  483. int start, end;
  484. i2c->clkrate = clkin;
  485. clkin /= 1000; /* clkin now in KHz */
  486. dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n",
  487. pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq);
  488. if (pdata->bus_freq != 0) {
  489. freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000,
  490. &div1, &divs);
  491. if (freq_acceptable(freq, pdata->bus_freq/1000))
  492. goto found;
  493. }
  494. /* ok, we may have to search for something suitable... */
  495. start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;
  496. end = pdata->min_freq;
  497. start /= 1000;
  498. end /= 1000;
  499. /* search loop... */
  500. for (; start > end; start--) {
  501. freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);
  502. if (freq_acceptable(freq, start))
  503. goto found;
  504. }
  505. /* cannot find frequency spec */
  506. return -EINVAL;
  507. found:
  508. *got = freq;
  509. iiccon = readl(i2c->regs + S3C2410_IICCON);
  510. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  511. iiccon |= (divs-1);
  512. if (div1 == 512)
  513. iiccon |= S3C2410_IICCON_TXDIV_512;
  514. writel(iiccon, i2c->regs + S3C2410_IICCON);
  515. return 0;
  516. }
  517. #ifdef CONFIG_CPU_FREQ
  518. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  519. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  520. unsigned long val, void *data)
  521. {
  522. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  523. unsigned long flags;
  524. unsigned int got;
  525. int delta_f;
  526. int ret;
  527. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  528. /* if we're post-change and the input clock has slowed down
  529. * or at pre-change and the clock is about to speed up, then
  530. * adjust our clock rate. <0 is slow, >0 speedup.
  531. */
  532. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  533. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  534. spin_lock_irqsave(&i2c->lock, flags);
  535. ret = s3c24xx_i2c_clockrate(i2c, &got);
  536. spin_unlock_irqrestore(&i2c->lock, flags);
  537. if (ret < 0)
  538. dev_err(i2c->dev, "cannot find frequency\n");
  539. else
  540. dev_info(i2c->dev, "setting freq %d\n", got);
  541. }
  542. return 0;
  543. }
  544. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  545. {
  546. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  547. return cpufreq_register_notifier(&i2c->freq_transition,
  548. CPUFREQ_TRANSITION_NOTIFIER);
  549. }
  550. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  551. {
  552. cpufreq_unregister_notifier(&i2c->freq_transition,
  553. CPUFREQ_TRANSITION_NOTIFIER);
  554. }
  555. #else
  556. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  557. {
  558. return 0;
  559. }
  560. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  561. {
  562. }
  563. #endif
  564. /* s3c24xx_i2c_init
  565. *
  566. * initialise the controller, set the IO lines and frequency
  567. */
  568. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  569. {
  570. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  571. struct s3c2410_platform_i2c *pdata;
  572. unsigned int freq;
  573. /* get the plafrom data */
  574. pdata = i2c->dev->platform_data;
  575. /* inititalise the gpio */
  576. if (pdata->cfg_gpio)
  577. pdata->cfg_gpio(to_platform_device(i2c->dev));
  578. /* write slave address */
  579. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  580. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  581. writel(iicon, i2c->regs + S3C2410_IICCON);
  582. /* we need to work out the divisors for the clock... */
  583. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  584. writel(0, i2c->regs + S3C2410_IICCON);
  585. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  586. return -EINVAL;
  587. }
  588. /* todo - check that the i2c lines aren't being dragged anywhere */
  589. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  590. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  591. /* check for s3c2440 i2c controller */
  592. if (s3c24xx_i2c_is2440(i2c)) {
  593. dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay);
  594. writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC);
  595. }
  596. return 0;
  597. }
  598. /* s3c24xx_i2c_probe
  599. *
  600. * called by the bus driver when a suitable device is found
  601. */
  602. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  603. {
  604. struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
  605. struct s3c2410_platform_i2c *pdata;
  606. struct resource *res;
  607. int ret;
  608. pdata = pdev->dev.platform_data;
  609. if (!pdata) {
  610. dev_err(&pdev->dev, "no platform data\n");
  611. return -EINVAL;
  612. }
  613. /* find the clock and enable it */
  614. i2c->dev = &pdev->dev;
  615. i2c->clk = clk_get(&pdev->dev, "i2c");
  616. if (IS_ERR(i2c->clk)) {
  617. dev_err(&pdev->dev, "cannot get clock\n");
  618. ret = -ENOENT;
  619. goto err_noclk;
  620. }
  621. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  622. clk_enable(i2c->clk);
  623. /* map the registers */
  624. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  625. if (res == NULL) {
  626. dev_err(&pdev->dev, "cannot find IO resource\n");
  627. ret = -ENOENT;
  628. goto err_clk;
  629. }
  630. i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
  631. pdev->name);
  632. if (i2c->ioarea == NULL) {
  633. dev_err(&pdev->dev, "cannot request IO\n");
  634. ret = -ENXIO;
  635. goto err_clk;
  636. }
  637. i2c->regs = ioremap(res->start, (res->end-res->start)+1);
  638. if (i2c->regs == NULL) {
  639. dev_err(&pdev->dev, "cannot map IO\n");
  640. ret = -ENXIO;
  641. goto err_ioarea;
  642. }
  643. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  644. i2c->regs, i2c->ioarea, res);
  645. /* setup info block for the i2c core */
  646. i2c->adap.algo_data = i2c;
  647. i2c->adap.dev.parent = &pdev->dev;
  648. /* initialise the i2c controller */
  649. ret = s3c24xx_i2c_init(i2c);
  650. if (ret != 0)
  651. goto err_iomap;
  652. /* find the IRQ for this unit (note, this relies on the init call to
  653. * ensure no current IRQs pending
  654. */
  655. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  656. if (res == NULL) {
  657. dev_err(&pdev->dev, "cannot find IRQ\n");
  658. ret = -ENOENT;
  659. goto err_iomap;
  660. }
  661. ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,
  662. pdev->name, i2c);
  663. if (ret != 0) {
  664. dev_err(&pdev->dev, "cannot claim IRQ\n");
  665. goto err_iomap;
  666. }
  667. i2c->irq = res;
  668. dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res,
  669. (unsigned long)res->start);
  670. ret = s3c24xx_i2c_register_cpufreq(i2c);
  671. if (ret < 0) {
  672. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  673. goto err_irq;
  674. }
  675. /* Note, previous versions of the driver used i2c_add_adapter()
  676. * to add the bus at any number. We now pass the bus number via
  677. * the platform data, so if unset it will now default to always
  678. * being bus 0.
  679. */
  680. i2c->adap.nr = pdata->bus_num;
  681. ret = i2c_add_numbered_adapter(&i2c->adap);
  682. if (ret < 0) {
  683. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  684. goto err_cpufreq;
  685. }
  686. platform_set_drvdata(pdev, i2c);
  687. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id);
  688. return 0;
  689. err_cpufreq:
  690. s3c24xx_i2c_deregister_cpufreq(i2c);
  691. err_irq:
  692. free_irq(i2c->irq->start, i2c);
  693. err_iomap:
  694. iounmap(i2c->regs);
  695. err_ioarea:
  696. release_resource(i2c->ioarea);
  697. kfree(i2c->ioarea);
  698. err_clk:
  699. clk_disable(i2c->clk);
  700. clk_put(i2c->clk);
  701. err_noclk:
  702. return ret;
  703. }
  704. /* s3c24xx_i2c_remove
  705. *
  706. * called when device is removed from the bus
  707. */
  708. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  709. {
  710. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  711. s3c24xx_i2c_deregister_cpufreq(i2c);
  712. i2c_del_adapter(&i2c->adap);
  713. free_irq(i2c->irq->start, i2c);
  714. clk_disable(i2c->clk);
  715. clk_put(i2c->clk);
  716. iounmap(i2c->regs);
  717. release_resource(i2c->ioarea);
  718. kfree(i2c->ioarea);
  719. return 0;
  720. }
  721. #ifdef CONFIG_PM
  722. static int s3c24xx_i2c_resume(struct platform_device *dev)
  723. {
  724. struct s3c24xx_i2c *i2c = platform_get_drvdata(dev);
  725. if (i2c != NULL)
  726. s3c24xx_i2c_init(i2c);
  727. return 0;
  728. }
  729. #else
  730. #define s3c24xx_i2c_resume NULL
  731. #endif
  732. /* device driver for platform bus bits */
  733. static struct platform_driver s3c2410_i2c_driver = {
  734. .probe = s3c24xx_i2c_probe,
  735. .remove = s3c24xx_i2c_remove,
  736. .resume = s3c24xx_i2c_resume,
  737. .driver = {
  738. .owner = THIS_MODULE,
  739. .name = "s3c2410-i2c",
  740. },
  741. };
  742. static struct platform_driver s3c2440_i2c_driver = {
  743. .probe = s3c24xx_i2c_probe,
  744. .remove = s3c24xx_i2c_remove,
  745. .resume = s3c24xx_i2c_resume,
  746. .driver = {
  747. .owner = THIS_MODULE,
  748. .name = "s3c2440-i2c",
  749. },
  750. };
  751. static int __init i2c_adap_s3c_init(void)
  752. {
  753. int ret;
  754. ret = platform_driver_register(&s3c2410_i2c_driver);
  755. if (ret == 0) {
  756. ret = platform_driver_register(&s3c2440_i2c_driver);
  757. if (ret)
  758. platform_driver_unregister(&s3c2410_i2c_driver);
  759. }
  760. return ret;
  761. }
  762. static void __exit i2c_adap_s3c_exit(void)
  763. {
  764. platform_driver_unregister(&s3c2410_i2c_driver);
  765. platform_driver_unregister(&s3c2440_i2c_driver);
  766. }
  767. module_init(i2c_adap_s3c_init);
  768. module_exit(i2c_adap_s3c_exit);
  769. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  770. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  771. MODULE_LICENSE("GPL");
  772. MODULE_ALIAS("platform:s3c2410-i2c");
  773. MODULE_ALIAS("platform:s3c2440-i2c");