i2c-rk3x.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. /* read the data from receive buffer */
  271. for (i = 0; i < len; ++i) {
  272. if (i % 4 == 0)
  273. val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
  274. byte = (val >> ((i % 4) * 8)) & 0xff;
  275. i2c->msg->buf[i2c->processed++] = byte;
  276. }
  277. /* are we finished? */
  278. if (i2c->processed == i2c->msg->len)
  279. rk3x_i2c_stop(i2c, i2c->error);
  280. else
  281. rk3x_i2c_prepare_read(i2c);
  282. }
  283. static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
  284. {
  285. unsigned int con;
  286. if (!(ipd & REG_INT_STOP)) {
  287. rk3x_i2c_stop(i2c, -EIO);
  288. dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
  289. rk3x_i2c_clean_ipd(i2c);
  290. return;
  291. }
  292. /* ack interrupt */
  293. i2c_writel(i2c, REG_INT_STOP, REG_IPD);
  294. /* disable STOP bit */
  295. con = i2c_readl(i2c, REG_CON);
  296. con &= ~REG_CON_STOP;
  297. i2c_writel(i2c, con, REG_CON);
  298. i2c->busy = false;
  299. i2c->state = STATE_IDLE;
  300. /* signal rk3x_i2c_xfer that we are finished */
  301. wake_up(&i2c->wait);
  302. }
  303. static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
  304. {
  305. struct rk3x_i2c *i2c = dev_id;
  306. unsigned int ipd;
  307. spin_lock(&i2c->lock);
  308. ipd = i2c_readl(i2c, REG_IPD);
  309. if (i2c->state == STATE_IDLE) {
  310. dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
  311. rk3x_i2c_clean_ipd(i2c);
  312. goto out;
  313. }
  314. dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
  315. /* Clean interrupt bits we don't care about */
  316. ipd &= ~(REG_INT_BRF | REG_INT_BTF);
  317. if (ipd & REG_INT_NAKRCV) {
  318. /*
  319. * We got a NACK in the last operation. Depending on whether
  320. * IGNORE_NAK is set, we have to stop the operation and report
  321. * an error.
  322. */
  323. i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
  324. ipd &= ~REG_INT_NAKRCV;
  325. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  326. rk3x_i2c_stop(i2c, -ENXIO);
  327. }
  328. /* is there anything left to handle? */
  329. if (unlikely(ipd == 0))
  330. goto out;
  331. switch (i2c->state) {
  332. case STATE_START:
  333. rk3x_i2c_handle_start(i2c, ipd);
  334. break;
  335. case STATE_WRITE:
  336. rk3x_i2c_handle_write(i2c, ipd);
  337. break;
  338. case STATE_READ:
  339. rk3x_i2c_handle_read(i2c, ipd);
  340. break;
  341. case STATE_STOP:
  342. rk3x_i2c_handle_stop(i2c, ipd);
  343. break;
  344. case STATE_IDLE:
  345. break;
  346. }
  347. out:
  348. spin_unlock(&i2c->lock);
  349. return IRQ_HANDLED;
  350. }
  351. static void rk3x_i2c_set_scl_rate(struct rk3x_i2c *i2c, unsigned long scl_rate)
  352. {
  353. unsigned long i2c_rate = clk_get_rate(i2c->clk);
  354. unsigned int div;
  355. /* SCL rate = (clk rate) / (8 * DIV) */
  356. div = DIV_ROUND_UP(i2c_rate, scl_rate * 8);
  357. /* The lower and upper half of the CLKDIV reg describe the length of
  358. * SCL low & high periods. */
  359. div = DIV_ROUND_UP(div, 2);
  360. i2c_writel(i2c, (div << 16) | (div & 0xffff), REG_CLKDIV);
  361. }
  362. /**
  363. * Setup I2C registers for an I2C operation specified by msgs, num.
  364. *
  365. * Must be called with i2c->lock held.
  366. *
  367. * @msgs: I2C msgs to process
  368. * @num: Number of msgs
  369. *
  370. * returns: Number of I2C msgs processed or negative in case of error
  371. */
  372. static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
  373. {
  374. u32 addr = (msgs[0].addr & 0x7f) << 1;
  375. int ret = 0;
  376. /*
  377. * The I2C adapter can issue a small (len < 4) write packet before
  378. * reading. This speeds up SMBus-style register reads.
  379. * The MRXADDR/MRXRADDR hold the slave address and the slave register
  380. * address in this case.
  381. */
  382. if (num >= 2 && msgs[0].len < 4 &&
  383. !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
  384. u32 reg_addr = 0;
  385. int i;
  386. dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
  387. addr >> 1);
  388. /* Fill MRXRADDR with the register address(es) */
  389. for (i = 0; i < msgs[0].len; ++i) {
  390. reg_addr |= msgs[0].buf[i] << (i * 8);
  391. reg_addr |= REG_MRXADDR_VALID(i);
  392. }
  393. /* msgs[0] is handled by hw. */
  394. i2c->msg = &msgs[1];
  395. i2c->mode = REG_CON_MOD_REGISTER_TX;
  396. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
  397. i2c_writel(i2c, reg_addr, REG_MRXRADDR);
  398. ret = 2;
  399. } else {
  400. /*
  401. * We'll have to do it the boring way and process the msgs
  402. * one-by-one.
  403. */
  404. if (msgs[0].flags & I2C_M_RD) {
  405. addr |= 1; /* set read bit */
  406. /*
  407. * We have to transmit the slave addr first. Use
  408. * MOD_REGISTER_TX for that purpose.
  409. */
  410. i2c->mode = REG_CON_MOD_REGISTER_TX;
  411. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
  412. REG_MRXADDR);
  413. i2c_writel(i2c, 0, REG_MRXRADDR);
  414. } else {
  415. i2c->mode = REG_CON_MOD_TX;
  416. }
  417. i2c->msg = &msgs[0];
  418. ret = 1;
  419. }
  420. i2c->addr = msgs[0].addr;
  421. i2c->busy = true;
  422. i2c->state = STATE_START;
  423. i2c->processed = 0;
  424. i2c->error = 0;
  425. rk3x_i2c_clean_ipd(i2c);
  426. return ret;
  427. }
  428. static int rk3x_i2c_xfer(struct i2c_adapter *adap,
  429. struct i2c_msg *msgs, int num)
  430. {
  431. struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
  432. unsigned long timeout, flags;
  433. int ret = 0;
  434. int i;
  435. spin_lock_irqsave(&i2c->lock, flags);
  436. clk_enable(i2c->clk);
  437. /* The clock rate might have changed, so setup the divider again */
  438. rk3x_i2c_set_scl_rate(i2c, i2c->scl_frequency);
  439. i2c->is_last_msg = false;
  440. /*
  441. * Process msgs. We can handle more than one message at once (see
  442. * rk3x_i2c_setup()).
  443. */
  444. for (i = 0; i < num; i += ret) {
  445. ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
  446. if (ret < 0) {
  447. dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
  448. break;
  449. }
  450. if (i + ret >= num)
  451. i2c->is_last_msg = true;
  452. spin_unlock_irqrestore(&i2c->lock, flags);
  453. rk3x_i2c_start(i2c);
  454. timeout = wait_event_timeout(i2c->wait, !i2c->busy,
  455. msecs_to_jiffies(WAIT_TIMEOUT));
  456. spin_lock_irqsave(&i2c->lock, flags);
  457. if (timeout == 0) {
  458. dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
  459. i2c_readl(i2c, REG_IPD), i2c->state);
  460. /* Force a STOP condition without interrupt */
  461. i2c_writel(i2c, 0, REG_IEN);
  462. i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
  463. i2c->state = STATE_IDLE;
  464. ret = -ETIMEDOUT;
  465. break;
  466. }
  467. if (i2c->error) {
  468. ret = i2c->error;
  469. break;
  470. }
  471. }
  472. clk_disable(i2c->clk);
  473. spin_unlock_irqrestore(&i2c->lock, flags);
  474. return ret;
  475. }
  476. static u32 rk3x_i2c_func(struct i2c_adapter *adap)
  477. {
  478. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  479. }
  480. static const struct i2c_algorithm rk3x_i2c_algorithm = {
  481. .master_xfer = rk3x_i2c_xfer,
  482. .functionality = rk3x_i2c_func,
  483. };
  484. static struct rk3x_i2c_soc_data soc_data[3] = {
  485. { .grf_offset = 0x154 }, /* rk3066 */
  486. { .grf_offset = 0x0a4 }, /* rk3188 */
  487. { .grf_offset = -1 }, /* no I2C switching needed */
  488. };
  489. static const struct of_device_id rk3x_i2c_match[] = {
  490. { .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] },
  491. { .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] },
  492. { .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] },
  493. {},
  494. };
  495. static int rk3x_i2c_probe(struct platform_device *pdev)
  496. {
  497. struct device_node *np = pdev->dev.of_node;
  498. const struct of_device_id *match;
  499. struct rk3x_i2c *i2c;
  500. struct resource *mem;
  501. int ret = 0;
  502. int bus_nr;
  503. u32 value;
  504. int irq;
  505. i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
  506. if (!i2c)
  507. return -ENOMEM;
  508. match = of_match_node(rk3x_i2c_match, np);
  509. i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
  510. if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  511. &i2c->scl_frequency)) {
  512. dev_info(&pdev->dev, "using default SCL frequency: %d\n",
  513. DEFAULT_SCL_RATE);
  514. i2c->scl_frequency = DEFAULT_SCL_RATE;
  515. }
  516. if (i2c->scl_frequency == 0 || i2c->scl_frequency > 400 * 1000) {
  517. dev_warn(&pdev->dev, "invalid SCL frequency specified.\n");
  518. dev_warn(&pdev->dev, "using default SCL frequency: %d\n",
  519. DEFAULT_SCL_RATE);
  520. i2c->scl_frequency = DEFAULT_SCL_RATE;
  521. }
  522. strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
  523. i2c->adap.owner = THIS_MODULE;
  524. i2c->adap.algo = &rk3x_i2c_algorithm;
  525. i2c->adap.retries = 3;
  526. i2c->adap.dev.of_node = np;
  527. i2c->adap.algo_data = i2c;
  528. i2c->adap.dev.parent = &pdev->dev;
  529. i2c->dev = &pdev->dev;
  530. spin_lock_init(&i2c->lock);
  531. init_waitqueue_head(&i2c->wait);
  532. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  533. if (IS_ERR(i2c->clk)) {
  534. dev_err(&pdev->dev, "cannot get clock\n");
  535. return PTR_ERR(i2c->clk);
  536. }
  537. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  538. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  539. if (IS_ERR(i2c->regs))
  540. return PTR_ERR(i2c->regs);
  541. /* Try to set the I2C adapter number from dt */
  542. bus_nr = of_alias_get_id(np, "i2c");
  543. /*
  544. * Switch to new interface if the SoC also offers the old one.
  545. * The control bit is located in the GRF register space.
  546. */
  547. if (i2c->soc_data->grf_offset >= 0) {
  548. struct regmap *grf;
  549. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  550. if (IS_ERR(grf)) {
  551. dev_err(&pdev->dev,
  552. "rk3x-i2c needs 'rockchip,grf' property\n");
  553. return PTR_ERR(grf);
  554. }
  555. if (bus_nr < 0) {
  556. dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
  557. return -EINVAL;
  558. }
  559. /* 27+i: write mask, 11+i: value */
  560. value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
  561. ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
  562. if (ret != 0) {
  563. dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
  564. return ret;
  565. }
  566. }
  567. /* IRQ setup */
  568. irq = platform_get_irq(pdev, 0);
  569. if (irq < 0) {
  570. dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
  571. return irq;
  572. }
  573. ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
  574. 0, dev_name(&pdev->dev), i2c);
  575. if (ret < 0) {
  576. dev_err(&pdev->dev, "cannot request IRQ\n");
  577. return ret;
  578. }
  579. platform_set_drvdata(pdev, i2c);
  580. ret = clk_prepare(i2c->clk);
  581. if (ret < 0) {
  582. dev_err(&pdev->dev, "Could not prepare clock\n");
  583. return ret;
  584. }
  585. ret = i2c_add_adapter(&i2c->adap);
  586. if (ret < 0) {
  587. dev_err(&pdev->dev, "Could not register adapter\n");
  588. goto err_clk;
  589. }
  590. dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
  591. return 0;
  592. err_clk:
  593. clk_unprepare(i2c->clk);
  594. return ret;
  595. }
  596. static int rk3x_i2c_remove(struct platform_device *pdev)
  597. {
  598. struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
  599. i2c_del_adapter(&i2c->adap);
  600. clk_unprepare(i2c->clk);
  601. return 0;
  602. }
  603. static struct platform_driver rk3x_i2c_driver = {
  604. .probe = rk3x_i2c_probe,
  605. .remove = rk3x_i2c_remove,
  606. .driver = {
  607. .owner = THIS_MODULE,
  608. .name = "rk3x-i2c",
  609. .of_match_table = rk3x_i2c_match,
  610. },
  611. };
  612. module_platform_driver(rk3x_i2c_driver);
  613. MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
  614. MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
  615. MODULE_LICENSE("GPL v2");