i2c-s3c2410.c 26 KB

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