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 *
  93. 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,
  386. struct i2c_msg *msgs, int num)
  387. {
  388. unsigned long timeout;
  389. int ret;
  390. if (!(readl(i2c->regs + S3C2410_IICCON) & S3C2410_IICCON_IRQEN))
  391. return -EIO;
  392. ret = s3c24xx_i2c_set_master(i2c);
  393. if (ret != 0) {
  394. dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
  395. ret = -EAGAIN;
  396. goto out;
  397. }
  398. spin_lock_irq(&i2c->lock);
  399. i2c->msg = msgs;
  400. i2c->msg_num = num;
  401. i2c->msg_ptr = 0;
  402. i2c->msg_idx = 0;
  403. i2c->state = STATE_START;
  404. s3c24xx_i2c_enable_irq(i2c);
  405. s3c24xx_i2c_message_start(i2c, msgs);
  406. spin_unlock_irq(&i2c->lock);
  407. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  408. ret = i2c->msg_idx;
  409. /* having these next two as dev_err() makes life very
  410. * noisy when doing an i2cdetect */
  411. if (timeout == 0)
  412. dev_dbg(i2c->dev, "timeout\n");
  413. else if (ret != num)
  414. dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
  415. /* ensure the stop has been through the bus */
  416. msleep(1);
  417. out:
  418. return ret;
  419. }
  420. /* s3c24xx_i2c_xfer
  421. *
  422. * first port of call from the i2c bus code when an message needs
  423. * transferring across the i2c bus.
  424. */
  425. static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
  426. struct i2c_msg *msgs, int num)
  427. {
  428. struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
  429. int retry;
  430. int ret;
  431. for (retry = 0; retry < adap->retries; retry++) {
  432. ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
  433. if (ret != -EAGAIN)
  434. return ret;
  435. dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
  436. udelay(100);
  437. }
  438. return -EREMOTEIO;
  439. }
  440. /* declare our i2c functionality */
  441. static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
  442. {
  443. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  444. }
  445. /* i2c bus registration info */
  446. static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
  447. .master_xfer = s3c24xx_i2c_xfer,
  448. .functionality = s3c24xx_i2c_func,
  449. };
  450. static struct s3c24xx_i2c s3c24xx_i2c = {
  451. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock),
  452. .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
  453. .tx_setup = 50,
  454. .adap = {
  455. .name = "s3c2410-i2c",
  456. .owner = THIS_MODULE,
  457. .algo = &s3c24xx_i2c_algorithm,
  458. .retries = 2,
  459. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  460. },
  461. };
  462. /* s3c24xx_i2c_calcdivisor
  463. *
  464. * return the divisor settings for a given frequency
  465. */
  466. static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
  467. unsigned int *div1, unsigned int *divs)
  468. {
  469. unsigned int calc_divs = clkin / wanted;
  470. unsigned int calc_div1;
  471. if (calc_divs > (16*16))
  472. calc_div1 = 512;
  473. else
  474. calc_div1 = 16;
  475. calc_divs += calc_div1-1;
  476. calc_divs /= calc_div1;
  477. if (calc_divs == 0)
  478. calc_divs = 1;
  479. if (calc_divs > 17)
  480. calc_divs = 17;
  481. *divs = calc_divs;
  482. *div1 = calc_div1;
  483. return clkin / (calc_divs * calc_div1);
  484. }
  485. /* freq_acceptable
  486. *
  487. * test wether a frequency is within the acceptable range of error
  488. */
  489. static inline int freq_acceptable(unsigned int freq, unsigned int wanted)
  490. {
  491. int diff = freq - wanted;
  492. return diff >= -2 && diff <= 2;
  493. }
  494. /* s3c24xx_i2c_clockrate
  495. *
  496. * work out a divisor for the user requested frequency setting,
  497. * either by the requested frequency, or scanning the acceptable
  498. * range of frequencies until something is found
  499. */
  500. static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
  501. {
  502. struct s3c2410_platform_i2c *pdata;
  503. unsigned long clkin = clk_get_rate(i2c->clk);
  504. unsigned int divs, div1;
  505. u32 iiccon;
  506. int freq;
  507. int start, end;
  508. i2c->clkrate = clkin;
  509. pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent);
  510. clkin /= 1000; /* clkin now in KHz */
  511. dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n",
  512. pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq);
  513. if (pdata->bus_freq != 0) {
  514. freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000,
  515. &div1, &divs);
  516. if (freq_acceptable(freq, pdata->bus_freq/1000))
  517. goto found;
  518. }
  519. /* ok, we may have to search for something suitable... */
  520. start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq;
  521. end = pdata->min_freq;
  522. start /= 1000;
  523. end /= 1000;
  524. /* search loop... */
  525. for (; start > end; start--) {
  526. freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs);
  527. if (freq_acceptable(freq, start))
  528. goto found;
  529. }
  530. /* cannot find frequency spec */
  531. return -EINVAL;
  532. found:
  533. *got = freq;
  534. iiccon = readl(i2c->regs + S3C2410_IICCON);
  535. iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
  536. iiccon |= (divs-1);
  537. if (div1 == 512)
  538. iiccon |= S3C2410_IICCON_TXDIV_512;
  539. writel(iiccon, i2c->regs + S3C2410_IICCON);
  540. return 0;
  541. }
  542. #ifdef CONFIG_CPU_FREQ
  543. #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
  544. static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
  545. unsigned long val, void *data)
  546. {
  547. struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
  548. unsigned long flags;
  549. unsigned int got;
  550. int delta_f;
  551. int ret;
  552. delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
  553. /* if we're post-change and the input clock has slowed down
  554. * or at pre-change and the clock is about to speed up, then
  555. * adjust our clock rate. <0 is slow, >0 speedup.
  556. */
  557. if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
  558. (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
  559. spin_lock_irqsave(&i2c->lock, flags);
  560. ret = s3c24xx_i2c_clockrate(i2c, &got);
  561. spin_unlock_irqrestore(&i2c->lock, flags);
  562. if (ret < 0)
  563. dev_err(i2c->dev, "cannot find frequency\n");
  564. else
  565. dev_info(i2c->dev, "setting freq %d\n", got);
  566. }
  567. return 0;
  568. }
  569. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  570. {
  571. i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
  572. return cpufreq_register_notifier(&i2c->freq_transition,
  573. CPUFREQ_TRANSITION_NOTIFIER);
  574. }
  575. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  576. {
  577. cpufreq_unregister_notifier(&i2c->freq_transition,
  578. CPUFREQ_TRANSITION_NOTIFIER);
  579. }
  580. #else
  581. static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
  582. {
  583. return 0;
  584. }
  585. static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
  586. {
  587. }
  588. #endif
  589. /* s3c24xx_i2c_init
  590. *
  591. * initialise the controller, set the IO lines and frequency
  592. */
  593. static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
  594. {
  595. unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
  596. struct s3c2410_platform_i2c *pdata;
  597. unsigned int freq;
  598. /* get the plafrom data */
  599. pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent);
  600. /* inititalise the gpio */
  601. s3c2410_gpio_cfgpin(S3C2410_GPE15, S3C2410_GPE15_IICSDA);
  602. s3c2410_gpio_cfgpin(S3C2410_GPE14, S3C2410_GPE14_IICSCL);
  603. /* write slave address */
  604. writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
  605. dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
  606. writel(iicon, i2c->regs + S3C2410_IICCON);
  607. /* we need to work out the divisors for the clock... */
  608. if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
  609. writel(0, i2c->regs + S3C2410_IICCON);
  610. dev_err(i2c->dev, "cannot meet bus frequency required\n");
  611. return -EINVAL;
  612. }
  613. /* todo - check that the i2c lines aren't being dragged anywhere */
  614. dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
  615. dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
  616. /* check for s3c2440 i2c controller */
  617. if (s3c24xx_i2c_is2440(i2c)) {
  618. dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay);
  619. writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC);
  620. }
  621. return 0;
  622. }
  623. /* s3c24xx_i2c_probe
  624. *
  625. * called by the bus driver when a suitable device is found
  626. */
  627. static int s3c24xx_i2c_probe(struct platform_device *pdev)
  628. {
  629. struct s3c24xx_i2c *i2c = &s3c24xx_i2c;
  630. struct s3c2410_platform_i2c *pdata;
  631. struct resource *res;
  632. int ret;
  633. pdata = s3c24xx_i2c_get_platformdata(&pdev->dev);
  634. /* find the clock and enable it */
  635. i2c->dev = &pdev->dev;
  636. i2c->clk = clk_get(&pdev->dev, "i2c");
  637. if (IS_ERR(i2c->clk)) {
  638. dev_err(&pdev->dev, "cannot get clock\n");
  639. ret = -ENOENT;
  640. goto err_noclk;
  641. }
  642. dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
  643. clk_enable(i2c->clk);
  644. /* map the registers */
  645. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  646. if (res == NULL) {
  647. dev_err(&pdev->dev, "cannot find IO resource\n");
  648. ret = -ENOENT;
  649. goto err_clk;
  650. }
  651. i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1,
  652. pdev->name);
  653. if (i2c->ioarea == NULL) {
  654. dev_err(&pdev->dev, "cannot request IO\n");
  655. ret = -ENXIO;
  656. goto err_clk;
  657. }
  658. i2c->regs = ioremap(res->start, (res->end-res->start)+1);
  659. if (i2c->regs == NULL) {
  660. dev_err(&pdev->dev, "cannot map IO\n");
  661. ret = -ENXIO;
  662. goto err_ioarea;
  663. }
  664. dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
  665. i2c->regs, i2c->ioarea, res);
  666. /* setup info block for the i2c core */
  667. i2c->adap.algo_data = i2c;
  668. i2c->adap.dev.parent = &pdev->dev;
  669. /* initialise the i2c controller */
  670. ret = s3c24xx_i2c_init(i2c);
  671. if (ret != 0)
  672. goto err_iomap;
  673. /* find the IRQ for this unit (note, this relies on the init call to
  674. * ensure no current IRQs pending
  675. */
  676. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  677. if (res == NULL) {
  678. dev_err(&pdev->dev, "cannot find IRQ\n");
  679. ret = -ENOENT;
  680. goto err_iomap;
  681. }
  682. ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED,
  683. pdev->name, i2c);
  684. if (ret != 0) {
  685. dev_err(&pdev->dev, "cannot claim IRQ\n");
  686. goto err_iomap;
  687. }
  688. i2c->irq = res;
  689. dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res,
  690. (unsigned long)res->start);
  691. ret = s3c24xx_i2c_register_cpufreq(i2c);
  692. if (ret < 0) {
  693. dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
  694. goto err_irq;
  695. }
  696. /* Note, previous versions of the driver used i2c_add_adapter()
  697. * to add the bus at any number. We now pass the bus number via
  698. * the platform data, so if unset it will now default to always
  699. * being bus 0.
  700. */
  701. i2c->adap.nr = pdata->bus_num;
  702. ret = i2c_add_numbered_adapter(&i2c->adap);
  703. if (ret < 0) {
  704. dev_err(&pdev->dev, "failed to add bus to i2c core\n");
  705. goto err_cpufreq;
  706. }
  707. platform_set_drvdata(pdev, i2c);
  708. dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id);
  709. return 0;
  710. err_cpufreq:
  711. s3c24xx_i2c_deregister_cpufreq(i2c);
  712. err_irq:
  713. free_irq(i2c->irq->start, i2c);
  714. err_iomap:
  715. iounmap(i2c->regs);
  716. err_ioarea:
  717. release_resource(i2c->ioarea);
  718. kfree(i2c->ioarea);
  719. err_clk:
  720. clk_disable(i2c->clk);
  721. clk_put(i2c->clk);
  722. err_noclk:
  723. return ret;
  724. }
  725. /* s3c24xx_i2c_remove
  726. *
  727. * called when device is removed from the bus
  728. */
  729. static int s3c24xx_i2c_remove(struct platform_device *pdev)
  730. {
  731. struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
  732. s3c24xx_i2c_deregister_cpufreq(i2c);
  733. i2c_del_adapter(&i2c->adap);
  734. free_irq(i2c->irq->start, i2c);
  735. clk_disable(i2c->clk);
  736. clk_put(i2c->clk);
  737. iounmap(i2c->regs);
  738. release_resource(i2c->ioarea);
  739. kfree(i2c->ioarea);
  740. return 0;
  741. }
  742. #ifdef CONFIG_PM
  743. static int s3c24xx_i2c_resume(struct platform_device *dev)
  744. {
  745. struct s3c24xx_i2c *i2c = platform_get_drvdata(dev);
  746. if (i2c != NULL)
  747. s3c24xx_i2c_init(i2c);
  748. return 0;
  749. }
  750. #else
  751. #define s3c24xx_i2c_resume NULL
  752. #endif
  753. /* device driver for platform bus bits */
  754. static struct platform_driver s3c2410_i2c_driver = {
  755. .probe = s3c24xx_i2c_probe,
  756. .remove = s3c24xx_i2c_remove,
  757. .resume = s3c24xx_i2c_resume,
  758. .driver = {
  759. .owner = THIS_MODULE,
  760. .name = "s3c2410-i2c",
  761. },
  762. };
  763. static struct platform_driver s3c2440_i2c_driver = {
  764. .probe = s3c24xx_i2c_probe,
  765. .remove = s3c24xx_i2c_remove,
  766. .resume = s3c24xx_i2c_resume,
  767. .driver = {
  768. .owner = THIS_MODULE,
  769. .name = "s3c2440-i2c",
  770. },
  771. };
  772. static int __init i2c_adap_s3c_init(void)
  773. {
  774. int ret;
  775. ret = platform_driver_register(&s3c2410_i2c_driver);
  776. if (ret == 0) {
  777. ret = platform_driver_register(&s3c2440_i2c_driver);
  778. if (ret)
  779. platform_driver_unregister(&s3c2410_i2c_driver);
  780. }
  781. return ret;
  782. }
  783. static void __exit i2c_adap_s3c_exit(void)
  784. {
  785. platform_driver_unregister(&s3c2410_i2c_driver);
  786. platform_driver_unregister(&s3c2440_i2c_driver);
  787. }
  788. module_init(i2c_adap_s3c_init);
  789. module_exit(i2c_adap_s3c_exit);
  790. MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
  791. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  792. MODULE_LICENSE("GPL");
  793. MODULE_ALIAS("platform:s3c2410-i2c");
  794. MODULE_ALIAS("platform:s3c2440-i2c");