i2c-rk3x.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Driver for I2C adapter in Rockchip RK3xxx SoC
  3. *
  4. * Max Schwarz <max.schwarz@online.de>
  5. * based on the patches by Rockchip Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/io.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/clk.h>
  23. #include <linux/wait.h>
  24. #include <linux/mfd/syscon.h>
  25. #include <linux/regmap.h>
  26. /* Register Map */
  27. #define REG_CON 0x00 /* control register */
  28. #define REG_CLKDIV 0x04 /* clock divisor register */
  29. #define REG_MRXADDR 0x08 /* slave address for REGISTER_TX */
  30. #define REG_MRXRADDR 0x0c /* slave register address for REGISTER_TX */
  31. #define REG_MTXCNT 0x10 /* number of bytes to be transmitted */
  32. #define REG_MRXCNT 0x14 /* number of bytes to be received */
  33. #define REG_IEN 0x18 /* interrupt enable */
  34. #define REG_IPD 0x1c /* interrupt pending */
  35. #define REG_FCNT 0x20 /* finished count */
  36. /* Data buffer offsets */
  37. #define TXBUFFER_BASE 0x100
  38. #define RXBUFFER_BASE 0x200
  39. /* REG_CON bits */
  40. #define REG_CON_EN BIT(0)
  41. enum {
  42. REG_CON_MOD_TX = 0, /* transmit data */
  43. REG_CON_MOD_REGISTER_TX, /* select register and restart */
  44. REG_CON_MOD_RX, /* receive data */
  45. REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes
  46. * register addr */
  47. };
  48. #define REG_CON_MOD(mod) ((mod) << 1)
  49. #define REG_CON_MOD_MASK (BIT(1) | BIT(2))
  50. #define REG_CON_START BIT(3)
  51. #define REG_CON_STOP BIT(4)
  52. #define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */
  53. #define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */
  54. /* REG_MRXADDR bits */
  55. #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
  56. /* REG_IEN/REG_IPD bits */
  57. #define REG_INT_BTF BIT(0) /* a byte was transmitted */
  58. #define REG_INT_BRF BIT(1) /* a byte was received */
  59. #define REG_INT_MBTF BIT(2) /* master data transmit finished */
  60. #define REG_INT_MBRF BIT(3) /* master data receive finished */
  61. #define REG_INT_START BIT(4) /* START condition generated */
  62. #define REG_INT_STOP BIT(5) /* STOP condition generated */
  63. #define REG_INT_NAKRCV BIT(6) /* NACK received */
  64. #define REG_INT_ALL 0x7f
  65. /* Constants */
  66. #define WAIT_TIMEOUT 200 /* ms */
  67. #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
  68. enum rk3x_i2c_state {
  69. STATE_IDLE,
  70. STATE_START,
  71. STATE_READ,
  72. STATE_WRITE,
  73. STATE_STOP
  74. };
  75. /**
  76. * @grf_offset: offset inside the grf regmap for setting the i2c type
  77. */
  78. struct rk3x_i2c_soc_data {
  79. int grf_offset;
  80. };
  81. struct rk3x_i2c {
  82. struct i2c_adapter adap;
  83. struct device *dev;
  84. struct rk3x_i2c_soc_data *soc_data;
  85. /* Hardware resources */
  86. void __iomem *regs;
  87. struct clk *clk;
  88. /* Settings */
  89. unsigned int scl_frequency;
  90. /* Synchronization & notification */
  91. spinlock_t lock;
  92. wait_queue_head_t wait;
  93. bool busy;
  94. /* Current message */
  95. struct i2c_msg *msg;
  96. u8 addr;
  97. unsigned int mode;
  98. bool is_last_msg;
  99. /* I2C state machine */
  100. enum rk3x_i2c_state state;
  101. unsigned int processed; /* sent/received bytes */
  102. int error;
  103. };
  104. static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
  105. unsigned int offset)
  106. {
  107. writel(value, i2c->regs + offset);
  108. }
  109. static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
  110. {
  111. return readl(i2c->regs + offset);
  112. }
  113. /* Reset all interrupt pending bits */
  114. static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
  115. {
  116. i2c_writel(i2c, REG_INT_ALL, REG_IPD);
  117. }
  118. /**
  119. * Generate a START condition, which triggers a REG_INT_START interrupt.
  120. */
  121. static void rk3x_i2c_start(struct rk3x_i2c *i2c)
  122. {
  123. u32 val;
  124. rk3x_i2c_clean_ipd(i2c);
  125. i2c_writel(i2c, REG_INT_START, REG_IEN);
  126. /* enable adapter with correct mode, send START condition */
  127. val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
  128. /* if we want to react to NACK, set ACTACK bit */
  129. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  130. val |= REG_CON_ACTACK;
  131. i2c_writel(i2c, val, REG_CON);
  132. }
  133. /**
  134. * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
  135. *
  136. * @error: Error code to return in rk3x_i2c_xfer
  137. */
  138. static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
  139. {
  140. unsigned int ctrl;
  141. i2c->processed = 0;
  142. i2c->msg = NULL;
  143. i2c->error = error;
  144. if (i2c->is_last_msg) {
  145. /* Enable stop interrupt */
  146. i2c_writel(i2c, REG_INT_STOP, REG_IEN);
  147. i2c->state = STATE_STOP;
  148. ctrl = i2c_readl(i2c, REG_CON);
  149. ctrl |= REG_CON_STOP;
  150. i2c_writel(i2c, ctrl, REG_CON);
  151. } else {
  152. /* Signal rk3x_i2c_xfer to start the next message. */
  153. i2c->busy = false;
  154. i2c->state = STATE_IDLE;
  155. /*
  156. * The HW is actually not capable of REPEATED START. But we can
  157. * get the intended effect by resetting its internal state
  158. * and issuing an ordinary START.
  159. */
  160. i2c_writel(i2c, 0, REG_CON);
  161. /* signal that we are finished with the current msg */
  162. wake_up(&i2c->wait);
  163. }
  164. }
  165. /**
  166. * Setup a read according to i2c->msg
  167. */
  168. static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
  169. {
  170. unsigned int len = i2c->msg->len - i2c->processed;
  171. u32 con;
  172. con = i2c_readl(i2c, REG_CON);
  173. /*
  174. * The hw can read up to 32 bytes at a time. If we need more than one
  175. * chunk, send an ACK after the last byte of the current chunk.
  176. */
  177. if (unlikely(len > 32)) {
  178. len = 32;
  179. con &= ~REG_CON_LASTACK;
  180. } else {
  181. con |= REG_CON_LASTACK;
  182. }
  183. /* make sure we are in plain RX mode if we read a second chunk */
  184. if (i2c->processed != 0) {
  185. con &= ~REG_CON_MOD_MASK;
  186. con |= REG_CON_MOD(REG_CON_MOD_RX);
  187. }
  188. i2c_writel(i2c, con, REG_CON);
  189. i2c_writel(i2c, len, REG_MRXCNT);
  190. }
  191. /**
  192. * Fill the transmit buffer with data from i2c->msg
  193. */
  194. static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
  195. {
  196. unsigned int i, j;
  197. u32 cnt = 0;
  198. u32 val;
  199. u8 byte;
  200. for (i = 0; i < 8; ++i) {
  201. val = 0;
  202. for (j = 0; j < 4; ++j) {
  203. if (i2c->processed == i2c->msg->len)
  204. break;
  205. if (i2c->processed == 0 && cnt == 0)
  206. byte = (i2c->addr & 0x7f) << 1;
  207. else
  208. byte = i2c->msg->buf[i2c->processed++];
  209. val |= byte << (j * 8);
  210. cnt++;
  211. }
  212. i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
  213. if (i2c->processed == i2c->msg->len)
  214. break;
  215. }
  216. i2c_writel(i2c, cnt, REG_MTXCNT);
  217. }
  218. /* IRQ handlers for individual states */
  219. static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
  220. {
  221. if (!(ipd & REG_INT_START)) {
  222. rk3x_i2c_stop(i2c, -EIO);
  223. dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
  224. rk3x_i2c_clean_ipd(i2c);
  225. return;
  226. }
  227. /* ack interrupt */
  228. i2c_writel(i2c, REG_INT_START, REG_IPD);
  229. /* disable start bit */
  230. i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
  231. /* enable appropriate interrupts and transition */
  232. if (i2c->mode == REG_CON_MOD_TX) {
  233. i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
  234. i2c->state = STATE_WRITE;
  235. rk3x_i2c_fill_transmit_buf(i2c);
  236. } else {
  237. /* in any other case, we are going to be reading. */
  238. i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
  239. i2c->state = STATE_READ;
  240. rk3x_i2c_prepare_read(i2c);
  241. }
  242. }
  243. static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
  244. {
  245. if (!(ipd & REG_INT_MBTF)) {
  246. rk3x_i2c_stop(i2c, -EIO);
  247. dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
  248. rk3x_i2c_clean_ipd(i2c);
  249. return;
  250. }
  251. /* ack interrupt */
  252. i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
  253. /* are we finished? */
  254. if (i2c->processed == i2c->msg->len)
  255. rk3x_i2c_stop(i2c, i2c->error);
  256. else
  257. rk3x_i2c_fill_transmit_buf(i2c);
  258. }
  259. static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
  260. {
  261. unsigned int i;
  262. unsigned int len = i2c->msg->len - i2c->processed;
  263. u32 uninitialized_var(val);
  264. u8 byte;
  265. /* we only care for MBRF here. */
  266. if (!(ipd & REG_INT_MBRF))
  267. return;
  268. /* ack interrupt */
  269. i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
  270. /* Can only handle a maximum of 32 bytes at a time */
  271. if (len > 32)
  272. len = 32;
  273. /* read the data from receive buffer */
  274. for (i = 0; i < len; ++i) {
  275. if (i % 4 == 0)
  276. val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
  277. byte = (val >> ((i % 4) * 8)) & 0xff;
  278. i2c->msg->buf[i2c->processed++] = byte;
  279. }
  280. /* are we finished? */
  281. if (i2c->processed == i2c->msg->len)
  282. rk3x_i2c_stop(i2c, i2c->error);
  283. else
  284. rk3x_i2c_prepare_read(i2c);
  285. }
  286. static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
  287. {
  288. unsigned int con;
  289. if (!(ipd & REG_INT_STOP)) {
  290. rk3x_i2c_stop(i2c, -EIO);
  291. dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
  292. rk3x_i2c_clean_ipd(i2c);
  293. return;
  294. }
  295. /* ack interrupt */
  296. i2c_writel(i2c, REG_INT_STOP, REG_IPD);
  297. /* disable STOP bit */
  298. con = i2c_readl(i2c, REG_CON);
  299. con &= ~REG_CON_STOP;
  300. i2c_writel(i2c, con, REG_CON);
  301. i2c->busy = false;
  302. i2c->state = STATE_IDLE;
  303. /* signal rk3x_i2c_xfer that we are finished */
  304. wake_up(&i2c->wait);
  305. }
  306. static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
  307. {
  308. struct rk3x_i2c *i2c = dev_id;
  309. unsigned int ipd;
  310. spin_lock(&i2c->lock);
  311. ipd = i2c_readl(i2c, REG_IPD);
  312. if (i2c->state == STATE_IDLE) {
  313. dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
  314. rk3x_i2c_clean_ipd(i2c);
  315. goto out;
  316. }
  317. dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
  318. /* Clean interrupt bits we don't care about */
  319. ipd &= ~(REG_INT_BRF | REG_INT_BTF);
  320. if (ipd & REG_INT_NAKRCV) {
  321. /*
  322. * We got a NACK in the last operation. Depending on whether
  323. * IGNORE_NAK is set, we have to stop the operation and report
  324. * an error.
  325. */
  326. i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
  327. ipd &= ~REG_INT_NAKRCV;
  328. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  329. rk3x_i2c_stop(i2c, -ENXIO);
  330. }
  331. /* is there anything left to handle? */
  332. if (unlikely((ipd & REG_INT_ALL) == 0))
  333. goto out;
  334. switch (i2c->state) {
  335. case STATE_START:
  336. rk3x_i2c_handle_start(i2c, ipd);
  337. break;
  338. case STATE_WRITE:
  339. rk3x_i2c_handle_write(i2c, ipd);
  340. break;
  341. case STATE_READ:
  342. rk3x_i2c_handle_read(i2c, ipd);
  343. break;
  344. case STATE_STOP:
  345. rk3x_i2c_handle_stop(i2c, ipd);
  346. break;
  347. case STATE_IDLE:
  348. break;
  349. }
  350. out:
  351. spin_unlock(&i2c->lock);
  352. return IRQ_HANDLED;
  353. }
  354. static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
  355. {
  356. unsigned long i2c_rate = clk_get_rate(i2c->clk);
  357. unsigned int div;
  358. /* SCL rate = (clk rate) / (8 * DIV) */
  359. div = DIV_ROUND_UP(i2c_rate, scl_rate * 8);
  360. /* The lower and upper half of the CLKDIV reg describe the length of
  361. * SCL low & high periods. */
  362. div = DIV_ROUND_UP(div, 2);
  363. i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
  364. }
  365. /**
  366. * Setup I2C registers for an I2C operation specified by msgs, num.
  367. *
  368. * Must be called with i2c->lock held.
  369. *
  370. * @msgs: I2C msgs to process
  371. * @num: Number of msgs
  372. *
  373. * returns: Number of I2C msgs processed or negative in case of error
  374. */
  375. static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
  376. {
  377. u32 addr = (msgs[0].addr & 0x7f) << 1;
  378. int ret = 0;
  379. /*
  380. * The I2C adapter can issue a small (len < 4) write packet before
  381. * reading. This speeds up SMBus-style register reads.
  382. * The MRXADDR/MRXRADDR hold the slave address and the slave register
  383. * address in this case.
  384. */
  385. if (num >= 2 && msgs[0].len < 4 &&
  386. !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
  387. u32 reg_addr = 0;
  388. int i;
  389. dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
  390. addr >> 1);
  391. /* Fill MRXRADDR with the register address(es) */
  392. for (i = 0; i < msgs[0].len; ++i) {
  393. reg_addr |= msgs[0].buf[i] << (i * 8);
  394. reg_addr |= REG_MRXADDR_VALID(i);
  395. }
  396. /* msgs[0] is handled by hw. */
  397. i2c->msg = &msgs[1];
  398. i2c->mode = REG_CON_MOD_REGISTER_TX;
  399. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
  400. i2c_writel(i2c, reg_addr, REG_MRXRADDR);
  401. ret = 2;
  402. } else {
  403. /*
  404. * We'll have to do it the boring way and process the msgs
  405. * one-by-one.
  406. */
  407. if (msgs[0].flags & I2C_M_RD) {
  408. addr |= 1; /* set read bit */
  409. /*
  410. * We have to transmit the slave addr first. Use
  411. * MOD_REGISTER_TX for that purpose.
  412. */
  413. i2c->mode = REG_CON_MOD_REGISTER_TX;
  414. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
  415. REG_MRXADDR);
  416. i2c_writel(i2c, 0, REG_MRXRADDR);
  417. } else {
  418. i2c->mode = REG_CON_MOD_TX;
  419. }
  420. i2c->msg = &msgs[0];
  421. ret = 1;
  422. }
  423. i2c->addr = msgs[0].addr;
  424. i2c->busy = true;
  425. i2c->state = STATE_START;
  426. i2c->processed = 0;
  427. i2c->error = 0;
  428. rk3x_i2c_clean_ipd(i2c);
  429. return ret;
  430. }
  431. static int rk3x_i2c_xfer(struct i2c_adapter *adap,
  432. struct i2c_msg *msgs, int num)
  433. {
  434. struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
  435. unsigned long timeout, flags;
  436. int ret = 0;
  437. int i;
  438. spin_lock_irqsave(&i2c->lock, flags);
  439. clk_enable(i2c->clk);
  440. /* The clock rate might have changed, so setup the divider again */
  441. rk3x_i2c_set_scl_rate(i2c, i2c->scl_frequency);
  442. i2c->is_last_msg = false;
  443. /*
  444. * Process msgs. We can handle more than one message at once (see
  445. * rk3x_i2c_setup()).
  446. */
  447. for (i = 0; i < num; i += ret) {
  448. ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
  449. if (ret < 0) {
  450. dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
  451. break;
  452. }
  453. if (i + ret >= num)
  454. i2c->is_last_msg = true;
  455. spin_unlock_irqrestore(&i2c->lock, flags);
  456. rk3x_i2c_start(i2c);
  457. timeout = wait_event_timeout(i2c->wait, !i2c->busy,
  458. msecs_to_jiffies(WAIT_TIMEOUT));
  459. spin_lock_irqsave(&i2c->lock, flags);
  460. if (timeout == 0) {
  461. dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
  462. i2c_readl(i2c, REG_IPD), i2c->state);
  463. /* Force a STOP condition without interrupt */
  464. i2c_writel(i2c, 0, REG_IEN);
  465. i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
  466. i2c->state = STATE_IDLE;
  467. ret = -ETIMEDOUT;
  468. break;
  469. }
  470. if (i2c->error) {
  471. ret = i2c->error;
  472. break;
  473. }
  474. }
  475. clk_disable(i2c->clk);
  476. spin_unlock_irqrestore(&i2c->lock, flags);
  477. return ret;
  478. }
  479. static u32 rk3x_i2c_func(struct i2c_adapter *adap)
  480. {
  481. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  482. }
  483. static const struct i2c_algorithm rk3x_i2c_algorithm = {
  484. .master_xfer = rk3x_i2c_xfer,
  485. .functionality = rk3x_i2c_func,
  486. };
  487. static struct rk3x_i2c_soc_data soc_data[3] = {
  488. { .grf_offset = 0x154 }, /* rk3066 */
  489. { .grf_offset = 0x0a4 }, /* rk3188 */
  490. { .grf_offset = -1 }, /* no I2C switching needed */
  491. };
  492. static const struct of_device_id rk3x_i2c_match[] = {
  493. { .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] },
  494. { .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] },
  495. { .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] },
  496. {},
  497. };
  498. static int rk3x_i2c_probe(struct platform_device *pdev)
  499. {
  500. struct device_node *np = pdev->dev.of_node;
  501. const struct of_device_id *match;
  502. struct rk3x_i2c *i2c;
  503. struct resource *mem;
  504. int ret = 0;
  505. int bus_nr;
  506. u32 value;
  507. int irq;
  508. i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
  509. if (!i2c)
  510. return -ENOMEM;
  511. match = of_match_node(rk3x_i2c_match, np);
  512. i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
  513. if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  514. &i2c->scl_frequency)) {
  515. dev_info(&pdev->dev, "using default SCL frequency: %d\n",
  516. DEFAULT_SCL_RATE);
  517. i2c->scl_frequency = DEFAULT_SCL_RATE;
  518. }
  519. if (i2c->scl_frequency == 0 || i2c->scl_frequency > 400 * 1000) {
  520. dev_warn(&pdev->dev, "invalid SCL frequency specified.\n");
  521. dev_warn(&pdev->dev, "using default SCL frequency: %d\n",
  522. DEFAULT_SCL_RATE);
  523. i2c->scl_frequency = DEFAULT_SCL_RATE;
  524. }
  525. strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
  526. i2c->adap.owner = THIS_MODULE;
  527. i2c->adap.algo = &rk3x_i2c_algorithm;
  528. i2c->adap.retries = 3;
  529. i2c->adap.dev.of_node = np;
  530. i2c->adap.algo_data = i2c;
  531. i2c->adap.dev.parent = &pdev->dev;
  532. i2c->dev = &pdev->dev;
  533. spin_lock_init(&i2c->lock);
  534. init_waitqueue_head(&i2c->wait);
  535. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  536. if (IS_ERR(i2c->clk)) {
  537. dev_err(&pdev->dev, "cannot get clock\n");
  538. return PTR_ERR(i2c->clk);
  539. }
  540. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  541. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  542. if (IS_ERR(i2c->regs))
  543. return PTR_ERR(i2c->regs);
  544. /* Try to set the I2C adapter number from dt */
  545. bus_nr = of_alias_get_id(np, "i2c");
  546. /*
  547. * Switch to new interface if the SoC also offers the old one.
  548. * The control bit is located in the GRF register space.
  549. */
  550. if (i2c->soc_data->grf_offset >= 0) {
  551. struct regmap *grf;
  552. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  553. if (IS_ERR(grf)) {
  554. dev_err(&pdev->dev,
  555. "rk3x-i2c needs 'rockchip,grf' property\n");
  556. return PTR_ERR(grf);
  557. }
  558. if (bus_nr < 0) {
  559. dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
  560. return -EINVAL;
  561. }
  562. /* 27+i: write mask, 11+i: value */
  563. value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
  564. ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
  565. if (ret != 0) {
  566. dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
  567. return ret;
  568. }
  569. }
  570. /* IRQ setup */
  571. irq = platform_get_irq(pdev, 0);
  572. if (irq < 0) {
  573. dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
  574. return irq;
  575. }
  576. ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
  577. 0, dev_name(&pdev->dev), i2c);
  578. if (ret < 0) {
  579. dev_err(&pdev->dev, "cannot request IRQ\n");
  580. return ret;
  581. }
  582. platform_set_drvdata(pdev, i2c);
  583. ret = clk_prepare(i2c->clk);
  584. if (ret < 0) {
  585. dev_err(&pdev->dev, "Could not prepare clock\n");
  586. return ret;
  587. }
  588. ret = i2c_add_adapter(&i2c->adap);
  589. if (ret < 0) {
  590. dev_err(&pdev->dev, "Could not register adapter\n");
  591. goto err_clk;
  592. }
  593. dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
  594. return 0;
  595. err_clk:
  596. clk_unprepare(i2c->clk);
  597. return ret;
  598. }
  599. static int rk3x_i2c_remove(struct platform_device *pdev)
  600. {
  601. struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
  602. i2c_del_adapter(&i2c->adap);
  603. clk_unprepare(i2c->clk);
  604. return 0;
  605. }
  606. static struct platform_driver rk3x_i2c_driver = {
  607. .probe = rk3x_i2c_probe,
  608. .remove = rk3x_i2c_remove,
  609. .driver = {
  610. .owner = THIS_MODULE,
  611. .name = "rk3x-i2c",
  612. .of_match_table = rk3x_i2c_match,
  613. },
  614. };
  615. module_platform_driver(rk3x_i2c_driver);
  616. MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
  617. MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
  618. MODULE_LICENSE("GPL v2");