i2c-rk3x.c 26 KB

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