i2c-s3c2410.c 23 KB

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