i2c-s3c2410.c 23 KB

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