i2c-rk3x.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. #include <linux/math64.h>
  27. /* Register Map */
  28. #define REG_CON 0x00 /* control register */
  29. #define REG_CLKDIV 0x04 /* clock divisor register */
  30. #define REG_MRXADDR 0x08 /* slave address for REGISTER_TX */
  31. #define REG_MRXRADDR 0x0c /* slave register address for REGISTER_TX */
  32. #define REG_MTXCNT 0x10 /* number of bytes to be transmitted */
  33. #define REG_MRXCNT 0x14 /* number of bytes to be received */
  34. #define REG_IEN 0x18 /* interrupt enable */
  35. #define REG_IPD 0x1c /* interrupt pending */
  36. #define REG_FCNT 0x20 /* finished count */
  37. /* Data buffer offsets */
  38. #define TXBUFFER_BASE 0x100
  39. #define RXBUFFER_BASE 0x200
  40. /* REG_CON bits */
  41. #define REG_CON_EN BIT(0)
  42. enum {
  43. REG_CON_MOD_TX = 0, /* transmit data */
  44. REG_CON_MOD_REGISTER_TX, /* select register and restart */
  45. REG_CON_MOD_RX, /* receive data */
  46. REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes
  47. * register addr */
  48. };
  49. #define REG_CON_MOD(mod) ((mod) << 1)
  50. #define REG_CON_MOD_MASK (BIT(1) | BIT(2))
  51. #define REG_CON_START BIT(3)
  52. #define REG_CON_STOP BIT(4)
  53. #define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */
  54. #define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */
  55. /* REG_MRXADDR bits */
  56. #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
  57. /* REG_IEN/REG_IPD bits */
  58. #define REG_INT_BTF BIT(0) /* a byte was transmitted */
  59. #define REG_INT_BRF BIT(1) /* a byte was received */
  60. #define REG_INT_MBTF BIT(2) /* master data transmit finished */
  61. #define REG_INT_MBRF BIT(3) /* master data receive finished */
  62. #define REG_INT_START BIT(4) /* START condition generated */
  63. #define REG_INT_STOP BIT(5) /* STOP condition generated */
  64. #define REG_INT_NAKRCV BIT(6) /* NACK received */
  65. #define REG_INT_ALL 0x7f
  66. /* Constants */
  67. #define WAIT_TIMEOUT 200 /* ms */
  68. #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
  69. enum rk3x_i2c_state {
  70. STATE_IDLE,
  71. STATE_START,
  72. STATE_READ,
  73. STATE_WRITE,
  74. STATE_STOP
  75. };
  76. /**
  77. * @grf_offset: offset inside the grf regmap for setting the i2c type
  78. */
  79. struct rk3x_i2c_soc_data {
  80. int grf_offset;
  81. };
  82. struct rk3x_i2c {
  83. struct i2c_adapter adap;
  84. struct device *dev;
  85. struct rk3x_i2c_soc_data *soc_data;
  86. /* Hardware resources */
  87. void __iomem *regs;
  88. struct clk *clk;
  89. struct notifier_block clk_rate_nb;
  90. /* Settings */
  91. unsigned int scl_frequency;
  92. /* Synchronization & notification */
  93. spinlock_t lock;
  94. wait_queue_head_t wait;
  95. bool busy;
  96. /* Current message */
  97. struct i2c_msg *msg;
  98. u8 addr;
  99. unsigned int mode;
  100. bool is_last_msg;
  101. /* I2C state machine */
  102. enum rk3x_i2c_state state;
  103. unsigned int processed; /* sent/received bytes */
  104. int error;
  105. };
  106. static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
  107. unsigned int offset)
  108. {
  109. writel(value, i2c->regs + offset);
  110. }
  111. static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
  112. {
  113. return readl(i2c->regs + offset);
  114. }
  115. /* Reset all interrupt pending bits */
  116. static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
  117. {
  118. i2c_writel(i2c, REG_INT_ALL, REG_IPD);
  119. }
  120. /**
  121. * Generate a START condition, which triggers a REG_INT_START interrupt.
  122. */
  123. static void rk3x_i2c_start(struct rk3x_i2c *i2c)
  124. {
  125. u32 val;
  126. rk3x_i2c_clean_ipd(i2c);
  127. i2c_writel(i2c, REG_INT_START, REG_IEN);
  128. /* enable adapter with correct mode, send START condition */
  129. val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
  130. /* if we want to react to NACK, set ACTACK bit */
  131. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  132. val |= REG_CON_ACTACK;
  133. i2c_writel(i2c, val, REG_CON);
  134. }
  135. /**
  136. * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
  137. *
  138. * @error: Error code to return in rk3x_i2c_xfer
  139. */
  140. static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
  141. {
  142. unsigned int ctrl;
  143. i2c->processed = 0;
  144. i2c->msg = NULL;
  145. i2c->error = error;
  146. if (i2c->is_last_msg) {
  147. /* Enable stop interrupt */
  148. i2c_writel(i2c, REG_INT_STOP, REG_IEN);
  149. i2c->state = STATE_STOP;
  150. ctrl = i2c_readl(i2c, REG_CON);
  151. ctrl |= REG_CON_STOP;
  152. i2c_writel(i2c, ctrl, REG_CON);
  153. } else {
  154. /* Signal rk3x_i2c_xfer to start the next message. */
  155. i2c->busy = false;
  156. i2c->state = STATE_IDLE;
  157. /*
  158. * The HW is actually not capable of REPEATED START. But we can
  159. * get the intended effect by resetting its internal state
  160. * and issuing an ordinary START.
  161. */
  162. i2c_writel(i2c, 0, REG_CON);
  163. /* signal that we are finished with the current msg */
  164. wake_up(&i2c->wait);
  165. }
  166. }
  167. /**
  168. * Setup a read according to i2c->msg
  169. */
  170. static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
  171. {
  172. unsigned int len = i2c->msg->len - i2c->processed;
  173. u32 con;
  174. con = i2c_readl(i2c, REG_CON);
  175. /*
  176. * The hw can read up to 32 bytes at a time. If we need more than one
  177. * chunk, send an ACK after the last byte of the current chunk.
  178. */
  179. if (len > 32) {
  180. len = 32;
  181. con &= ~REG_CON_LASTACK;
  182. } else {
  183. con |= REG_CON_LASTACK;
  184. }
  185. /* make sure we are in plain RX mode if we read a second chunk */
  186. if (i2c->processed != 0) {
  187. con &= ~REG_CON_MOD_MASK;
  188. con |= REG_CON_MOD(REG_CON_MOD_RX);
  189. }
  190. i2c_writel(i2c, con, REG_CON);
  191. i2c_writel(i2c, len, REG_MRXCNT);
  192. }
  193. /**
  194. * Fill the transmit buffer with data from i2c->msg
  195. */
  196. static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
  197. {
  198. unsigned int i, j;
  199. u32 cnt = 0;
  200. u32 val;
  201. u8 byte;
  202. for (i = 0; i < 8; ++i) {
  203. val = 0;
  204. for (j = 0; j < 4; ++j) {
  205. if ((i2c->processed == i2c->msg->len) && (cnt != 0))
  206. break;
  207. if (i2c->processed == 0 && cnt == 0)
  208. byte = (i2c->addr & 0x7f) << 1;
  209. else
  210. byte = i2c->msg->buf[i2c->processed++];
  211. val |= byte << (j * 8);
  212. cnt++;
  213. }
  214. i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
  215. if (i2c->processed == i2c->msg->len)
  216. break;
  217. }
  218. i2c_writel(i2c, cnt, REG_MTXCNT);
  219. }
  220. /* IRQ handlers for individual states */
  221. static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
  222. {
  223. if (!(ipd & REG_INT_START)) {
  224. rk3x_i2c_stop(i2c, -EIO);
  225. dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
  226. rk3x_i2c_clean_ipd(i2c);
  227. return;
  228. }
  229. /* ack interrupt */
  230. i2c_writel(i2c, REG_INT_START, REG_IPD);
  231. /* disable start bit */
  232. i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
  233. /* enable appropriate interrupts and transition */
  234. if (i2c->mode == REG_CON_MOD_TX) {
  235. i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
  236. i2c->state = STATE_WRITE;
  237. rk3x_i2c_fill_transmit_buf(i2c);
  238. } else {
  239. /* in any other case, we are going to be reading. */
  240. i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
  241. i2c->state = STATE_READ;
  242. rk3x_i2c_prepare_read(i2c);
  243. }
  244. }
  245. static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
  246. {
  247. if (!(ipd & REG_INT_MBTF)) {
  248. rk3x_i2c_stop(i2c, -EIO);
  249. dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
  250. rk3x_i2c_clean_ipd(i2c);
  251. return;
  252. }
  253. /* ack interrupt */
  254. i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
  255. /* are we finished? */
  256. if (i2c->processed == i2c->msg->len)
  257. rk3x_i2c_stop(i2c, i2c->error);
  258. else
  259. rk3x_i2c_fill_transmit_buf(i2c);
  260. }
  261. static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
  262. {
  263. unsigned int i;
  264. unsigned int len = i2c->msg->len - i2c->processed;
  265. u32 uninitialized_var(val);
  266. u8 byte;
  267. /* we only care for MBRF here. */
  268. if (!(ipd & REG_INT_MBRF))
  269. return;
  270. /* ack interrupt */
  271. i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
  272. /* Can only handle a maximum of 32 bytes at a time */
  273. if (len > 32)
  274. len = 32;
  275. /* read the data from receive buffer */
  276. for (i = 0; i < len; ++i) {
  277. if (i % 4 == 0)
  278. val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
  279. byte = (val >> ((i % 4) * 8)) & 0xff;
  280. i2c->msg->buf[i2c->processed++] = byte;
  281. }
  282. /* are we finished? */
  283. if (i2c->processed == i2c->msg->len)
  284. rk3x_i2c_stop(i2c, i2c->error);
  285. else
  286. rk3x_i2c_prepare_read(i2c);
  287. }
  288. static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
  289. {
  290. unsigned int con;
  291. if (!(ipd & REG_INT_STOP)) {
  292. rk3x_i2c_stop(i2c, -EIO);
  293. dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
  294. rk3x_i2c_clean_ipd(i2c);
  295. return;
  296. }
  297. /* ack interrupt */
  298. i2c_writel(i2c, REG_INT_STOP, REG_IPD);
  299. /* disable STOP bit */
  300. con = i2c_readl(i2c, REG_CON);
  301. con &= ~REG_CON_STOP;
  302. i2c_writel(i2c, con, REG_CON);
  303. i2c->busy = false;
  304. i2c->state = STATE_IDLE;
  305. /* signal rk3x_i2c_xfer that we are finished */
  306. wake_up(&i2c->wait);
  307. }
  308. static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
  309. {
  310. struct rk3x_i2c *i2c = dev_id;
  311. unsigned int ipd;
  312. spin_lock(&i2c->lock);
  313. ipd = i2c_readl(i2c, REG_IPD);
  314. if (i2c->state == STATE_IDLE) {
  315. dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
  316. rk3x_i2c_clean_ipd(i2c);
  317. goto out;
  318. }
  319. dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
  320. /* Clean interrupt bits we don't care about */
  321. ipd &= ~(REG_INT_BRF | REG_INT_BTF);
  322. if (ipd & REG_INT_NAKRCV) {
  323. /*
  324. * We got a NACK in the last operation. Depending on whether
  325. * IGNORE_NAK is set, we have to stop the operation and report
  326. * an error.
  327. */
  328. i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
  329. ipd &= ~REG_INT_NAKRCV;
  330. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  331. rk3x_i2c_stop(i2c, -ENXIO);
  332. }
  333. /* is there anything left to handle? */
  334. if ((ipd & REG_INT_ALL) == 0)
  335. goto out;
  336. switch (i2c->state) {
  337. case STATE_START:
  338. rk3x_i2c_handle_start(i2c, ipd);
  339. break;
  340. case STATE_WRITE:
  341. rk3x_i2c_handle_write(i2c, ipd);
  342. break;
  343. case STATE_READ:
  344. rk3x_i2c_handle_read(i2c, ipd);
  345. break;
  346. case STATE_STOP:
  347. rk3x_i2c_handle_stop(i2c, ipd);
  348. break;
  349. case STATE_IDLE:
  350. break;
  351. }
  352. out:
  353. spin_unlock(&i2c->lock);
  354. return IRQ_HANDLED;
  355. }
  356. /**
  357. * Calculate divider values for desired SCL frequency
  358. *
  359. * @clk_rate: I2C input clock rate
  360. * @scl_rate: Desired SCL rate
  361. * @div_low: Divider output for low
  362. * @div_high: Divider output for high
  363. *
  364. * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
  365. * a best-effort divider value is returned in divs. If the target rate is
  366. * too high, we silently use the highest possible rate.
  367. */
  368. static int rk3x_i2c_calc_divs(unsigned long clk_rate, unsigned long scl_rate,
  369. unsigned long *div_low, unsigned long *div_high)
  370. {
  371. unsigned long min_low_ns, min_high_ns;
  372. unsigned long max_data_hold_ns;
  373. unsigned long data_hold_buffer_ns;
  374. unsigned long max_low_ns, min_total_ns;
  375. unsigned long clk_rate_khz, scl_rate_khz;
  376. unsigned long min_low_div, min_high_div;
  377. unsigned long max_low_div;
  378. unsigned long min_div_for_hold, min_total_div;
  379. unsigned long extra_div, extra_low_div, ideal_low_div;
  380. int ret = 0;
  381. /* Only support standard-mode and fast-mode */
  382. if (WARN_ON(scl_rate > 400000))
  383. scl_rate = 400000;
  384. /* prevent scl_rate_khz from becoming 0 */
  385. if (WARN_ON(scl_rate < 1000))
  386. scl_rate = 1000;
  387. /*
  388. * min_low_ns: The minimum number of ns we need to hold low
  389. * to meet i2c spec
  390. * min_high_ns: The minimum number of ns we need to hold high
  391. * to meet i2c spec
  392. * max_low_ns: The maximum number of ns we can hold low
  393. * to meet i2c spec
  394. *
  395. * Note: max_low_ns should be (max data hold time * 2 - buffer)
  396. * This is because the i2c host on Rockchip holds the data line
  397. * for half the low time.
  398. */
  399. if (scl_rate <= 100000) {
  400. min_low_ns = 4700;
  401. min_high_ns = 4000;
  402. max_data_hold_ns = 3450;
  403. data_hold_buffer_ns = 50;
  404. } else {
  405. min_low_ns = 1300;
  406. min_high_ns = 600;
  407. max_data_hold_ns = 900;
  408. data_hold_buffer_ns = 50;
  409. }
  410. max_low_ns = max_data_hold_ns * 2 - data_hold_buffer_ns;
  411. min_total_ns = min_low_ns + min_high_ns;
  412. /* Adjust to avoid overflow */
  413. clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
  414. scl_rate_khz = scl_rate / 1000;
  415. /*
  416. * We need the total div to be >= this number
  417. * so we don't clock too fast.
  418. */
  419. min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
  420. /* These are the min dividers needed for min hold times. */
  421. min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
  422. min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
  423. min_div_for_hold = (min_low_div + min_high_div);
  424. /*
  425. * This is the maximum divider so we don't go over the max.
  426. * We don't round up here (we round down) since this is a max.
  427. */
  428. max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
  429. if (min_low_div > max_low_div) {
  430. WARN_ONCE(true,
  431. "Conflicting, min_low_div %lu, max_low_div %lu\n",
  432. min_low_div, max_low_div);
  433. max_low_div = min_low_div;
  434. }
  435. if (min_div_for_hold > min_total_div) {
  436. /*
  437. * Time needed to meet hold requirements is important.
  438. * Just use that.
  439. */
  440. *div_low = min_low_div;
  441. *div_high = min_high_div;
  442. } else {
  443. /*
  444. * We've got to distribute some time among the low and high
  445. * so we don't run too fast.
  446. */
  447. extra_div = min_total_div - min_div_for_hold;
  448. /*
  449. * We'll try to split things up perfectly evenly,
  450. * biasing slightly towards having a higher div
  451. * for low (spend more time low).
  452. */
  453. ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
  454. scl_rate_khz * 8 * min_total_ns);
  455. /* Don't allow it to go over the max */
  456. if (ideal_low_div > max_low_div)
  457. ideal_low_div = max_low_div;
  458. /*
  459. * Handle when the ideal low div is going to take up
  460. * more than we have.
  461. */
  462. if (ideal_low_div > min_low_div + extra_div)
  463. ideal_low_div = min_low_div + extra_div;
  464. /* Give low the "ideal" and give high whatever extra is left */
  465. extra_low_div = ideal_low_div - min_low_div;
  466. *div_low = ideal_low_div;
  467. *div_high = min_high_div + (extra_div - extra_low_div);
  468. }
  469. /*
  470. * Adjust to the fact that the hardware has an implicit "+1".
  471. * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
  472. */
  473. *div_low = *div_low - 1;
  474. *div_high = *div_high - 1;
  475. /* Maximum divider supported by hw is 0xffff */
  476. if (*div_low > 0xffff) {
  477. *div_low = 0xffff;
  478. ret = -EINVAL;
  479. }
  480. if (*div_high > 0xffff) {
  481. *div_high = 0xffff;
  482. ret = -EINVAL;
  483. }
  484. return ret;
  485. }
  486. static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
  487. {
  488. unsigned long div_low, div_high;
  489. u64 t_low_ns, t_high_ns;
  490. int ret;
  491. ret = rk3x_i2c_calc_divs(clk_rate, i2c->scl_frequency, &div_low,
  492. &div_high);
  493. WARN_ONCE(ret != 0, "Could not reach SCL freq %u", i2c->scl_frequency);
  494. clk_enable(i2c->clk);
  495. i2c_writel(i2c, (div_high << 16) | (div_low & 0xffff), REG_CLKDIV);
  496. clk_disable(i2c->clk);
  497. t_low_ns = div_u64(((u64)div_low + 1) * 8 * 1000000000, clk_rate);
  498. t_high_ns = div_u64(((u64)div_high + 1) * 8 * 1000000000, clk_rate);
  499. dev_dbg(i2c->dev,
  500. "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
  501. clk_rate / 1000,
  502. 1000000000 / i2c->scl_frequency,
  503. t_low_ns, t_high_ns);
  504. }
  505. /**
  506. * rk3x_i2c_clk_notifier_cb - Clock rate change callback
  507. * @nb: Pointer to notifier block
  508. * @event: Notification reason
  509. * @data: Pointer to notification data object
  510. *
  511. * The callback checks whether a valid bus frequency can be generated after the
  512. * change. If so, the change is acknowledged, otherwise the change is aborted.
  513. * New dividers are written to the HW in the pre- or post change notification
  514. * depending on the scaling direction.
  515. *
  516. * Code adapted from i2c-cadence.c.
  517. *
  518. * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
  519. * to acknowedge the change, NOTIFY_DONE if the notification is
  520. * considered irrelevant.
  521. */
  522. static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
  523. event, void *data)
  524. {
  525. struct clk_notifier_data *ndata = data;
  526. struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
  527. unsigned long div_low, div_high;
  528. switch (event) {
  529. case PRE_RATE_CHANGE:
  530. if (rk3x_i2c_calc_divs(ndata->new_rate, i2c->scl_frequency,
  531. &div_low, &div_high) != 0) {
  532. return NOTIFY_STOP;
  533. }
  534. /* scale up */
  535. if (ndata->new_rate > ndata->old_rate)
  536. rk3x_i2c_adapt_div(i2c, ndata->new_rate);
  537. return NOTIFY_OK;
  538. case POST_RATE_CHANGE:
  539. /* scale down */
  540. if (ndata->new_rate < ndata->old_rate)
  541. rk3x_i2c_adapt_div(i2c, ndata->new_rate);
  542. return NOTIFY_OK;
  543. case ABORT_RATE_CHANGE:
  544. /* scale up */
  545. if (ndata->new_rate > ndata->old_rate)
  546. rk3x_i2c_adapt_div(i2c, ndata->old_rate);
  547. return NOTIFY_OK;
  548. default:
  549. return NOTIFY_DONE;
  550. }
  551. }
  552. /**
  553. * Setup I2C registers for an I2C operation specified by msgs, num.
  554. *
  555. * Must be called with i2c->lock held.
  556. *
  557. * @msgs: I2C msgs to process
  558. * @num: Number of msgs
  559. *
  560. * returns: Number of I2C msgs processed or negative in case of error
  561. */
  562. static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
  563. {
  564. u32 addr = (msgs[0].addr & 0x7f) << 1;
  565. int ret = 0;
  566. /*
  567. * The I2C adapter can issue a small (len < 4) write packet before
  568. * reading. This speeds up SMBus-style register reads.
  569. * The MRXADDR/MRXRADDR hold the slave address and the slave register
  570. * address in this case.
  571. */
  572. if (num >= 2 && msgs[0].len < 4 &&
  573. !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
  574. u32 reg_addr = 0;
  575. int i;
  576. dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
  577. addr >> 1);
  578. /* Fill MRXRADDR with the register address(es) */
  579. for (i = 0; i < msgs[0].len; ++i) {
  580. reg_addr |= msgs[0].buf[i] << (i * 8);
  581. reg_addr |= REG_MRXADDR_VALID(i);
  582. }
  583. /* msgs[0] is handled by hw. */
  584. i2c->msg = &msgs[1];
  585. i2c->mode = REG_CON_MOD_REGISTER_TX;
  586. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
  587. i2c_writel(i2c, reg_addr, REG_MRXRADDR);
  588. ret = 2;
  589. } else {
  590. /*
  591. * We'll have to do it the boring way and process the msgs
  592. * one-by-one.
  593. */
  594. if (msgs[0].flags & I2C_M_RD) {
  595. addr |= 1; /* set read bit */
  596. /*
  597. * We have to transmit the slave addr first. Use
  598. * MOD_REGISTER_TX for that purpose.
  599. */
  600. i2c->mode = REG_CON_MOD_REGISTER_TX;
  601. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
  602. REG_MRXADDR);
  603. i2c_writel(i2c, 0, REG_MRXRADDR);
  604. } else {
  605. i2c->mode = REG_CON_MOD_TX;
  606. }
  607. i2c->msg = &msgs[0];
  608. ret = 1;
  609. }
  610. i2c->addr = msgs[0].addr;
  611. i2c->busy = true;
  612. i2c->state = STATE_START;
  613. i2c->processed = 0;
  614. i2c->error = 0;
  615. rk3x_i2c_clean_ipd(i2c);
  616. return ret;
  617. }
  618. static int rk3x_i2c_xfer(struct i2c_adapter *adap,
  619. struct i2c_msg *msgs, int num)
  620. {
  621. struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
  622. unsigned long timeout, flags;
  623. int ret = 0;
  624. int i;
  625. spin_lock_irqsave(&i2c->lock, flags);
  626. clk_enable(i2c->clk);
  627. i2c->is_last_msg = false;
  628. /*
  629. * Process msgs. We can handle more than one message at once (see
  630. * rk3x_i2c_setup()).
  631. */
  632. for (i = 0; i < num; i += ret) {
  633. ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
  634. if (ret < 0) {
  635. dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
  636. break;
  637. }
  638. if (i + ret >= num)
  639. i2c->is_last_msg = true;
  640. spin_unlock_irqrestore(&i2c->lock, flags);
  641. rk3x_i2c_start(i2c);
  642. timeout = wait_event_timeout(i2c->wait, !i2c->busy,
  643. msecs_to_jiffies(WAIT_TIMEOUT));
  644. spin_lock_irqsave(&i2c->lock, flags);
  645. if (timeout == 0) {
  646. dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
  647. i2c_readl(i2c, REG_IPD), i2c->state);
  648. /* Force a STOP condition without interrupt */
  649. i2c_writel(i2c, 0, REG_IEN);
  650. i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON);
  651. i2c->state = STATE_IDLE;
  652. ret = -ETIMEDOUT;
  653. break;
  654. }
  655. if (i2c->error) {
  656. ret = i2c->error;
  657. break;
  658. }
  659. }
  660. clk_disable(i2c->clk);
  661. spin_unlock_irqrestore(&i2c->lock, flags);
  662. return ret;
  663. }
  664. static u32 rk3x_i2c_func(struct i2c_adapter *adap)
  665. {
  666. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  667. }
  668. static const struct i2c_algorithm rk3x_i2c_algorithm = {
  669. .master_xfer = rk3x_i2c_xfer,
  670. .functionality = rk3x_i2c_func,
  671. };
  672. static struct rk3x_i2c_soc_data soc_data[3] = {
  673. { .grf_offset = 0x154 }, /* rk3066 */
  674. { .grf_offset = 0x0a4 }, /* rk3188 */
  675. { .grf_offset = -1 }, /* no I2C switching needed */
  676. };
  677. static const struct of_device_id rk3x_i2c_match[] = {
  678. { .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] },
  679. { .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] },
  680. { .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] },
  681. {},
  682. };
  683. static int rk3x_i2c_probe(struct platform_device *pdev)
  684. {
  685. struct device_node *np = pdev->dev.of_node;
  686. const struct of_device_id *match;
  687. struct rk3x_i2c *i2c;
  688. struct resource *mem;
  689. int ret = 0;
  690. int bus_nr;
  691. u32 value;
  692. int irq;
  693. unsigned long clk_rate;
  694. i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
  695. if (!i2c)
  696. return -ENOMEM;
  697. match = of_match_node(rk3x_i2c_match, np);
  698. i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data;
  699. if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  700. &i2c->scl_frequency)) {
  701. dev_info(&pdev->dev, "using default SCL frequency: %d\n",
  702. DEFAULT_SCL_RATE);
  703. i2c->scl_frequency = DEFAULT_SCL_RATE;
  704. }
  705. if (i2c->scl_frequency == 0 || i2c->scl_frequency > 400 * 1000) {
  706. dev_warn(&pdev->dev, "invalid SCL frequency specified.\n");
  707. dev_warn(&pdev->dev, "using default SCL frequency: %d\n",
  708. DEFAULT_SCL_RATE);
  709. i2c->scl_frequency = DEFAULT_SCL_RATE;
  710. }
  711. strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
  712. i2c->adap.owner = THIS_MODULE;
  713. i2c->adap.algo = &rk3x_i2c_algorithm;
  714. i2c->adap.retries = 3;
  715. i2c->adap.dev.of_node = np;
  716. i2c->adap.algo_data = i2c;
  717. i2c->adap.dev.parent = &pdev->dev;
  718. i2c->dev = &pdev->dev;
  719. spin_lock_init(&i2c->lock);
  720. init_waitqueue_head(&i2c->wait);
  721. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  722. if (IS_ERR(i2c->clk)) {
  723. dev_err(&pdev->dev, "cannot get clock\n");
  724. return PTR_ERR(i2c->clk);
  725. }
  726. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  727. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  728. if (IS_ERR(i2c->regs))
  729. return PTR_ERR(i2c->regs);
  730. /* Try to set the I2C adapter number from dt */
  731. bus_nr = of_alias_get_id(np, "i2c");
  732. /*
  733. * Switch to new interface if the SoC also offers the old one.
  734. * The control bit is located in the GRF register space.
  735. */
  736. if (i2c->soc_data->grf_offset >= 0) {
  737. struct regmap *grf;
  738. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  739. if (IS_ERR(grf)) {
  740. dev_err(&pdev->dev,
  741. "rk3x-i2c needs 'rockchip,grf' property\n");
  742. return PTR_ERR(grf);
  743. }
  744. if (bus_nr < 0) {
  745. dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
  746. return -EINVAL;
  747. }
  748. /* 27+i: write mask, 11+i: value */
  749. value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
  750. ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
  751. if (ret != 0) {
  752. dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
  753. return ret;
  754. }
  755. }
  756. /* IRQ setup */
  757. irq = platform_get_irq(pdev, 0);
  758. if (irq < 0) {
  759. dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
  760. return irq;
  761. }
  762. ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
  763. 0, dev_name(&pdev->dev), i2c);
  764. if (ret < 0) {
  765. dev_err(&pdev->dev, "cannot request IRQ\n");
  766. return ret;
  767. }
  768. platform_set_drvdata(pdev, i2c);
  769. ret = clk_prepare(i2c->clk);
  770. if (ret < 0) {
  771. dev_err(&pdev->dev, "Could not prepare clock\n");
  772. return ret;
  773. }
  774. i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
  775. ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
  776. if (ret != 0) {
  777. dev_err(&pdev->dev, "Unable to register clock notifier\n");
  778. goto err_clk;
  779. }
  780. clk_rate = clk_get_rate(i2c->clk);
  781. rk3x_i2c_adapt_div(i2c, clk_rate);
  782. ret = i2c_add_adapter(&i2c->adap);
  783. if (ret < 0) {
  784. dev_err(&pdev->dev, "Could not register adapter\n");
  785. goto err_clk_notifier;
  786. }
  787. dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs);
  788. return 0;
  789. err_clk_notifier:
  790. clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
  791. err_clk:
  792. clk_unprepare(i2c->clk);
  793. return ret;
  794. }
  795. static int rk3x_i2c_remove(struct platform_device *pdev)
  796. {
  797. struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
  798. i2c_del_adapter(&i2c->adap);
  799. clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
  800. clk_unprepare(i2c->clk);
  801. return 0;
  802. }
  803. static struct platform_driver rk3x_i2c_driver = {
  804. .probe = rk3x_i2c_probe,
  805. .remove = rk3x_i2c_remove,
  806. .driver = {
  807. .name = "rk3x-i2c",
  808. .of_match_table = rk3x_i2c_match,
  809. },
  810. };
  811. module_platform_driver(rk3x_i2c_driver);
  812. MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
  813. MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
  814. MODULE_LICENSE("GPL v2");