i2c-rk3x.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. #define REG_CON_TUNING_MASK GENMASK_ULL(15, 8)
  56. #define REG_CON_SDA_CFG(cfg) ((cfg) << 8)
  57. #define REG_CON_STA_CFG(cfg) ((cfg) << 12)
  58. #define REG_CON_STO_CFG(cfg) ((cfg) << 14)
  59. /* REG_MRXADDR bits */
  60. #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
  61. /* REG_IEN/REG_IPD bits */
  62. #define REG_INT_BTF BIT(0) /* a byte was transmitted */
  63. #define REG_INT_BRF BIT(1) /* a byte was received */
  64. #define REG_INT_MBTF BIT(2) /* master data transmit finished */
  65. #define REG_INT_MBRF BIT(3) /* master data receive finished */
  66. #define REG_INT_START BIT(4) /* START condition generated */
  67. #define REG_INT_STOP BIT(5) /* STOP condition generated */
  68. #define REG_INT_NAKRCV BIT(6) /* NACK received */
  69. #define REG_INT_ALL 0x7f
  70. /* Constants */
  71. #define WAIT_TIMEOUT 1000 /* ms */
  72. #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
  73. /**
  74. * struct i2c_spec_values:
  75. * @min_hold_start_ns: min hold time (repeated) START condition
  76. * @min_low_ns: min LOW period of the SCL clock
  77. * @min_high_ns: min HIGH period of the SCL cloc
  78. * @min_setup_start_ns: min set-up time for a repeated START conditio
  79. * @max_data_hold_ns: max data hold time
  80. * @min_data_setup_ns: min data set-up time
  81. * @min_setup_stop_ns: min set-up time for STOP condition
  82. * @min_hold_buffer_ns: min bus free time between a STOP and
  83. * START condition
  84. */
  85. struct i2c_spec_values {
  86. unsigned long min_hold_start_ns;
  87. unsigned long min_low_ns;
  88. unsigned long min_high_ns;
  89. unsigned long min_setup_start_ns;
  90. unsigned long max_data_hold_ns;
  91. unsigned long min_data_setup_ns;
  92. unsigned long min_setup_stop_ns;
  93. unsigned long min_hold_buffer_ns;
  94. };
  95. static const struct i2c_spec_values standard_mode_spec = {
  96. .min_hold_start_ns = 4000,
  97. .min_low_ns = 4700,
  98. .min_high_ns = 4000,
  99. .min_setup_start_ns = 4700,
  100. .max_data_hold_ns = 3450,
  101. .min_data_setup_ns = 250,
  102. .min_setup_stop_ns = 4000,
  103. .min_hold_buffer_ns = 4700,
  104. };
  105. static const struct i2c_spec_values fast_mode_spec = {
  106. .min_hold_start_ns = 600,
  107. .min_low_ns = 1300,
  108. .min_high_ns = 600,
  109. .min_setup_start_ns = 600,
  110. .max_data_hold_ns = 900,
  111. .min_data_setup_ns = 100,
  112. .min_setup_stop_ns = 600,
  113. .min_hold_buffer_ns = 1300,
  114. };
  115. static const struct i2c_spec_values fast_mode_plus_spec = {
  116. .min_hold_start_ns = 260,
  117. .min_low_ns = 500,
  118. .min_high_ns = 260,
  119. .min_setup_start_ns = 260,
  120. .max_data_hold_ns = 400,
  121. .min_data_setup_ns = 50,
  122. .min_setup_stop_ns = 260,
  123. .min_hold_buffer_ns = 500,
  124. };
  125. /**
  126. * struct rk3x_i2c_calced_timings:
  127. * @div_low: Divider output for low
  128. * @div_high: Divider output for high
  129. * @tuning: Used to adjust setup/hold data time,
  130. * setup/hold start time and setup stop time for
  131. * v1's calc_timings, the tuning should all be 0
  132. * for old hardware anyone using v0's calc_timings.
  133. */
  134. struct rk3x_i2c_calced_timings {
  135. unsigned long div_low;
  136. unsigned long div_high;
  137. unsigned int tuning;
  138. };
  139. enum rk3x_i2c_state {
  140. STATE_IDLE,
  141. STATE_START,
  142. STATE_READ,
  143. STATE_WRITE,
  144. STATE_STOP
  145. };
  146. /**
  147. * struct rk3x_i2c_soc_data:
  148. * @grf_offset: offset inside the grf regmap for setting the i2c type
  149. * @calc_timings: Callback function for i2c timing information calculated
  150. */
  151. struct rk3x_i2c_soc_data {
  152. int grf_offset;
  153. int (*calc_timings)(unsigned long, struct i2c_timings *,
  154. struct rk3x_i2c_calced_timings *);
  155. };
  156. /**
  157. * struct rk3x_i2c - private data of the controller
  158. * @adap: corresponding I2C adapter
  159. * @dev: device for this controller
  160. * @soc_data: related soc data struct
  161. * @regs: virtual memory area
  162. * @clk: function clk for rk3399 or function & Bus clks for others
  163. * @pclk: Bus clk for rk3399
  164. * @clk_rate_nb: i2c clk rate change notify
  165. * @t: I2C known timing information
  166. * @lock: spinlock for the i2c bus
  167. * @wait: the waitqueue to wait for i2c transfer
  168. * @busy: the condition for the event to wait for
  169. * @msg: current i2c message
  170. * @addr: addr of i2c slave device
  171. * @mode: mode of i2c transfer
  172. * @is_last_msg: flag determines whether it is the last msg in this transfer
  173. * @state: state of i2c transfer
  174. * @processed: byte length which has been send or received
  175. * @error: error code for i2c transfer
  176. */
  177. struct rk3x_i2c {
  178. struct i2c_adapter adap;
  179. struct device *dev;
  180. const struct rk3x_i2c_soc_data *soc_data;
  181. /* Hardware resources */
  182. void __iomem *regs;
  183. struct clk *clk;
  184. struct clk *pclk;
  185. struct notifier_block clk_rate_nb;
  186. /* Settings */
  187. struct i2c_timings t;
  188. /* Synchronization & notification */
  189. spinlock_t lock;
  190. wait_queue_head_t wait;
  191. bool busy;
  192. /* Current message */
  193. struct i2c_msg *msg;
  194. u8 addr;
  195. unsigned int mode;
  196. bool is_last_msg;
  197. /* I2C state machine */
  198. enum rk3x_i2c_state state;
  199. unsigned int processed;
  200. int error;
  201. };
  202. static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value,
  203. unsigned int offset)
  204. {
  205. writel(value, i2c->regs + offset);
  206. }
  207. static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset)
  208. {
  209. return readl(i2c->regs + offset);
  210. }
  211. /* Reset all interrupt pending bits */
  212. static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c)
  213. {
  214. i2c_writel(i2c, REG_INT_ALL, REG_IPD);
  215. }
  216. /**
  217. * Generate a START condition, which triggers a REG_INT_START interrupt.
  218. */
  219. static void rk3x_i2c_start(struct rk3x_i2c *i2c)
  220. {
  221. u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
  222. i2c_writel(i2c, REG_INT_START, REG_IEN);
  223. /* enable adapter with correct mode, send START condition */
  224. val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
  225. /* if we want to react to NACK, set ACTACK bit */
  226. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  227. val |= REG_CON_ACTACK;
  228. i2c_writel(i2c, val, REG_CON);
  229. }
  230. /**
  231. * Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
  232. *
  233. * @error: Error code to return in rk3x_i2c_xfer
  234. */
  235. static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
  236. {
  237. unsigned int ctrl;
  238. i2c->processed = 0;
  239. i2c->msg = NULL;
  240. i2c->error = error;
  241. if (i2c->is_last_msg) {
  242. /* Enable stop interrupt */
  243. i2c_writel(i2c, REG_INT_STOP, REG_IEN);
  244. i2c->state = STATE_STOP;
  245. ctrl = i2c_readl(i2c, REG_CON);
  246. ctrl |= REG_CON_STOP;
  247. i2c_writel(i2c, ctrl, REG_CON);
  248. } else {
  249. /* Signal rk3x_i2c_xfer to start the next message. */
  250. i2c->busy = false;
  251. i2c->state = STATE_IDLE;
  252. /*
  253. * The HW is actually not capable of REPEATED START. But we can
  254. * get the intended effect by resetting its internal state
  255. * and issuing an ordinary START.
  256. */
  257. ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
  258. i2c_writel(i2c, ctrl, REG_CON);
  259. /* signal that we are finished with the current msg */
  260. wake_up(&i2c->wait);
  261. }
  262. }
  263. /**
  264. * Setup a read according to i2c->msg
  265. */
  266. static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
  267. {
  268. unsigned int len = i2c->msg->len - i2c->processed;
  269. u32 con;
  270. con = i2c_readl(i2c, REG_CON);
  271. /*
  272. * The hw can read up to 32 bytes at a time. If we need more than one
  273. * chunk, send an ACK after the last byte of the current chunk.
  274. */
  275. if (len > 32) {
  276. len = 32;
  277. con &= ~REG_CON_LASTACK;
  278. } else {
  279. con |= REG_CON_LASTACK;
  280. }
  281. /* make sure we are in plain RX mode if we read a second chunk */
  282. if (i2c->processed != 0) {
  283. con &= ~REG_CON_MOD_MASK;
  284. con |= REG_CON_MOD(REG_CON_MOD_RX);
  285. }
  286. i2c_writel(i2c, con, REG_CON);
  287. i2c_writel(i2c, len, REG_MRXCNT);
  288. }
  289. /**
  290. * Fill the transmit buffer with data from i2c->msg
  291. */
  292. static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
  293. {
  294. unsigned int i, j;
  295. u32 cnt = 0;
  296. u32 val;
  297. u8 byte;
  298. for (i = 0; i < 8; ++i) {
  299. val = 0;
  300. for (j = 0; j < 4; ++j) {
  301. if ((i2c->processed == i2c->msg->len) && (cnt != 0))
  302. break;
  303. if (i2c->processed == 0 && cnt == 0)
  304. byte = (i2c->addr & 0x7f) << 1;
  305. else
  306. byte = i2c->msg->buf[i2c->processed++];
  307. val |= byte << (j * 8);
  308. cnt++;
  309. }
  310. i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
  311. if (i2c->processed == i2c->msg->len)
  312. break;
  313. }
  314. i2c_writel(i2c, cnt, REG_MTXCNT);
  315. }
  316. /* IRQ handlers for individual states */
  317. static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
  318. {
  319. if (!(ipd & REG_INT_START)) {
  320. rk3x_i2c_stop(i2c, -EIO);
  321. dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
  322. rk3x_i2c_clean_ipd(i2c);
  323. return;
  324. }
  325. /* ack interrupt */
  326. i2c_writel(i2c, REG_INT_START, REG_IPD);
  327. /* disable start bit */
  328. i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
  329. /* enable appropriate interrupts and transition */
  330. if (i2c->mode == REG_CON_MOD_TX) {
  331. i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
  332. i2c->state = STATE_WRITE;
  333. rk3x_i2c_fill_transmit_buf(i2c);
  334. } else {
  335. /* in any other case, we are going to be reading. */
  336. i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
  337. i2c->state = STATE_READ;
  338. rk3x_i2c_prepare_read(i2c);
  339. }
  340. }
  341. static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
  342. {
  343. if (!(ipd & REG_INT_MBTF)) {
  344. rk3x_i2c_stop(i2c, -EIO);
  345. dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
  346. rk3x_i2c_clean_ipd(i2c);
  347. return;
  348. }
  349. /* ack interrupt */
  350. i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
  351. /* are we finished? */
  352. if (i2c->processed == i2c->msg->len)
  353. rk3x_i2c_stop(i2c, i2c->error);
  354. else
  355. rk3x_i2c_fill_transmit_buf(i2c);
  356. }
  357. static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
  358. {
  359. unsigned int i;
  360. unsigned int len = i2c->msg->len - i2c->processed;
  361. u32 uninitialized_var(val);
  362. u8 byte;
  363. /* we only care for MBRF here. */
  364. if (!(ipd & REG_INT_MBRF))
  365. return;
  366. /* ack interrupt */
  367. i2c_writel(i2c, REG_INT_MBRF, REG_IPD);
  368. /* Can only handle a maximum of 32 bytes at a time */
  369. if (len > 32)
  370. len = 32;
  371. /* read the data from receive buffer */
  372. for (i = 0; i < len; ++i) {
  373. if (i % 4 == 0)
  374. val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
  375. byte = (val >> ((i % 4) * 8)) & 0xff;
  376. i2c->msg->buf[i2c->processed++] = byte;
  377. }
  378. /* are we finished? */
  379. if (i2c->processed == i2c->msg->len)
  380. rk3x_i2c_stop(i2c, i2c->error);
  381. else
  382. rk3x_i2c_prepare_read(i2c);
  383. }
  384. static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
  385. {
  386. unsigned int con;
  387. if (!(ipd & REG_INT_STOP)) {
  388. rk3x_i2c_stop(i2c, -EIO);
  389. dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
  390. rk3x_i2c_clean_ipd(i2c);
  391. return;
  392. }
  393. /* ack interrupt */
  394. i2c_writel(i2c, REG_INT_STOP, REG_IPD);
  395. /* disable STOP bit */
  396. con = i2c_readl(i2c, REG_CON);
  397. con &= ~REG_CON_STOP;
  398. i2c_writel(i2c, con, REG_CON);
  399. i2c->busy = false;
  400. i2c->state = STATE_IDLE;
  401. /* signal rk3x_i2c_xfer that we are finished */
  402. wake_up(&i2c->wait);
  403. }
  404. static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
  405. {
  406. struct rk3x_i2c *i2c = dev_id;
  407. unsigned int ipd;
  408. spin_lock(&i2c->lock);
  409. ipd = i2c_readl(i2c, REG_IPD);
  410. if (i2c->state == STATE_IDLE) {
  411. dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
  412. rk3x_i2c_clean_ipd(i2c);
  413. goto out;
  414. }
  415. dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
  416. /* Clean interrupt bits we don't care about */
  417. ipd &= ~(REG_INT_BRF | REG_INT_BTF);
  418. if (ipd & REG_INT_NAKRCV) {
  419. /*
  420. * We got a NACK in the last operation. Depending on whether
  421. * IGNORE_NAK is set, we have to stop the operation and report
  422. * an error.
  423. */
  424. i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
  425. ipd &= ~REG_INT_NAKRCV;
  426. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  427. rk3x_i2c_stop(i2c, -ENXIO);
  428. }
  429. /* is there anything left to handle? */
  430. if ((ipd & REG_INT_ALL) == 0)
  431. goto out;
  432. switch (i2c->state) {
  433. case STATE_START:
  434. rk3x_i2c_handle_start(i2c, ipd);
  435. break;
  436. case STATE_WRITE:
  437. rk3x_i2c_handle_write(i2c, ipd);
  438. break;
  439. case STATE_READ:
  440. rk3x_i2c_handle_read(i2c, ipd);
  441. break;
  442. case STATE_STOP:
  443. rk3x_i2c_handle_stop(i2c, ipd);
  444. break;
  445. case STATE_IDLE:
  446. break;
  447. }
  448. out:
  449. spin_unlock(&i2c->lock);
  450. return IRQ_HANDLED;
  451. }
  452. /**
  453. * Get timing values of I2C specification
  454. *
  455. * @speed: Desired SCL frequency
  456. *
  457. * Returns: Matched i2c spec values.
  458. */
  459. static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed)
  460. {
  461. if (speed <= 100000)
  462. return &standard_mode_spec;
  463. else if (speed <= 400000)
  464. return &fast_mode_spec;
  465. else
  466. return &fast_mode_plus_spec;
  467. }
  468. /**
  469. * Calculate divider values for desired SCL frequency
  470. *
  471. * @clk_rate: I2C input clock rate
  472. * @t: Known I2C timing information
  473. * @t_calc: Caculated rk3x private timings that would be written into regs
  474. *
  475. * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
  476. * a best-effort divider value is returned in divs. If the target rate is
  477. * too high, we silently use the highest possible rate.
  478. */
  479. static int rk3x_i2c_v0_calc_timings(unsigned long clk_rate,
  480. struct i2c_timings *t,
  481. struct rk3x_i2c_calced_timings *t_calc)
  482. {
  483. unsigned long min_low_ns, min_high_ns;
  484. unsigned long max_low_ns, min_total_ns;
  485. unsigned long clk_rate_khz, scl_rate_khz;
  486. unsigned long min_low_div, min_high_div;
  487. unsigned long max_low_div;
  488. unsigned long min_div_for_hold, min_total_div;
  489. unsigned long extra_div, extra_low_div, ideal_low_div;
  490. unsigned long data_hold_buffer_ns = 50;
  491. const struct i2c_spec_values *spec;
  492. int ret = 0;
  493. /* Only support standard-mode and fast-mode */
  494. if (WARN_ON(t->bus_freq_hz > 400000))
  495. t->bus_freq_hz = 400000;
  496. /* prevent scl_rate_khz from becoming 0 */
  497. if (WARN_ON(t->bus_freq_hz < 1000))
  498. t->bus_freq_hz = 1000;
  499. /*
  500. * min_low_ns: The minimum number of ns we need to hold low to
  501. * meet I2C specification, should include fall time.
  502. * min_high_ns: The minimum number of ns we need to hold high to
  503. * meet I2C specification, should include rise time.
  504. * max_low_ns: The maximum number of ns we can hold low to meet
  505. * I2C specification.
  506. *
  507. * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
  508. * This is because the i2c host on Rockchip holds the data line
  509. * for half the low time.
  510. */
  511. spec = rk3x_i2c_get_spec(t->bus_freq_hz);
  512. min_high_ns = t->scl_rise_ns + spec->min_high_ns;
  513. /*
  514. * Timings for repeated start:
  515. * - controller appears to drop SDA at .875x (7/8) programmed clk high.
  516. * - controller appears to keep SCL high for 2x programmed clk high.
  517. *
  518. * We need to account for those rules in picking our "high" time so
  519. * we meet tSU;STA and tHD;STA times.
  520. */
  521. min_high_ns = max(min_high_ns, DIV_ROUND_UP(
  522. (t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875));
  523. min_high_ns = max(min_high_ns, DIV_ROUND_UP(
  524. (t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns +
  525. spec->min_high_ns), 2));
  526. min_low_ns = t->scl_fall_ns + spec->min_low_ns;
  527. max_low_ns = spec->max_data_hold_ns * 2 - data_hold_buffer_ns;
  528. min_total_ns = min_low_ns + min_high_ns;
  529. /* Adjust to avoid overflow */
  530. clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
  531. scl_rate_khz = t->bus_freq_hz / 1000;
  532. /*
  533. * We need the total div to be >= this number
  534. * so we don't clock too fast.
  535. */
  536. min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
  537. /* These are the min dividers needed for min hold times. */
  538. min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
  539. min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
  540. min_div_for_hold = (min_low_div + min_high_div);
  541. /*
  542. * This is the maximum divider so we don't go over the maximum.
  543. * We don't round up here (we round down) since this is a maximum.
  544. */
  545. max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
  546. if (min_low_div > max_low_div) {
  547. WARN_ONCE(true,
  548. "Conflicting, min_low_div %lu, max_low_div %lu\n",
  549. min_low_div, max_low_div);
  550. max_low_div = min_low_div;
  551. }
  552. if (min_div_for_hold > min_total_div) {
  553. /*
  554. * Time needed to meet hold requirements is important.
  555. * Just use that.
  556. */
  557. t_calc->div_low = min_low_div;
  558. t_calc->div_high = min_high_div;
  559. } else {
  560. /*
  561. * We've got to distribute some time among the low and high
  562. * so we don't run too fast.
  563. */
  564. extra_div = min_total_div - min_div_for_hold;
  565. /*
  566. * We'll try to split things up perfectly evenly,
  567. * biasing slightly towards having a higher div
  568. * for low (spend more time low).
  569. */
  570. ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
  571. scl_rate_khz * 8 * min_total_ns);
  572. /* Don't allow it to go over the maximum */
  573. if (ideal_low_div > max_low_div)
  574. ideal_low_div = max_low_div;
  575. /*
  576. * Handle when the ideal low div is going to take up
  577. * more than we have.
  578. */
  579. if (ideal_low_div > min_low_div + extra_div)
  580. ideal_low_div = min_low_div + extra_div;
  581. /* Give low the "ideal" and give high whatever extra is left */
  582. extra_low_div = ideal_low_div - min_low_div;
  583. t_calc->div_low = ideal_low_div;
  584. t_calc->div_high = min_high_div + (extra_div - extra_low_div);
  585. }
  586. /*
  587. * Adjust to the fact that the hardware has an implicit "+1".
  588. * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
  589. */
  590. t_calc->div_low--;
  591. t_calc->div_high--;
  592. /* Give the tuning value 0, that would not update con register */
  593. t_calc->tuning = 0;
  594. /* Maximum divider supported by hw is 0xffff */
  595. if (t_calc->div_low > 0xffff) {
  596. t_calc->div_low = 0xffff;
  597. ret = -EINVAL;
  598. }
  599. if (t_calc->div_high > 0xffff) {
  600. t_calc->div_high = 0xffff;
  601. ret = -EINVAL;
  602. }
  603. return ret;
  604. }
  605. /**
  606. * Calculate timing values for desired SCL frequency
  607. *
  608. * @clk_rate: I2C input clock rate
  609. * @t: Known I2C timing information
  610. * @t_calc: Caculated rk3x private timings that would be written into regs
  611. *
  612. * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case
  613. * a best-effort divider value is returned in divs. If the target rate is
  614. * too high, we silently use the highest possible rate.
  615. * The following formulas are v1's method to calculate timings.
  616. *
  617. * l = divl + 1;
  618. * h = divh + 1;
  619. * s = sda_update_config + 1;
  620. * u = start_setup_config + 1;
  621. * p = stop_setup_config + 1;
  622. * T = Tclk_i2c;
  623. *
  624. * tHigh = 8 * h * T;
  625. * tLow = 8 * l * T;
  626. *
  627. * tHD;sda = (l * s + 1) * T;
  628. * tSU;sda = [(8 - s) * l + 1] * T;
  629. * tI2C = 8 * (l + h) * T;
  630. *
  631. * tSU;sta = (8h * u + 1) * T;
  632. * tHD;sta = [8h * (u + 1) - 1] * T;
  633. * tSU;sto = (8h * p + 1) * T;
  634. */
  635. static int rk3x_i2c_v1_calc_timings(unsigned long clk_rate,
  636. struct i2c_timings *t,
  637. struct rk3x_i2c_calced_timings *t_calc)
  638. {
  639. unsigned long min_low_ns, min_high_ns;
  640. unsigned long min_setup_start_ns, min_setup_data_ns;
  641. unsigned long min_setup_stop_ns, max_hold_data_ns;
  642. unsigned long clk_rate_khz, scl_rate_khz;
  643. unsigned long min_low_div, min_high_div;
  644. unsigned long min_div_for_hold, min_total_div;
  645. unsigned long extra_div, extra_low_div;
  646. unsigned long sda_update_cfg, stp_sta_cfg, stp_sto_cfg;
  647. const struct i2c_spec_values *spec;
  648. int ret = 0;
  649. /* Support standard-mode, fast-mode and fast-mode plus */
  650. if (WARN_ON(t->bus_freq_hz > 1000000))
  651. t->bus_freq_hz = 1000000;
  652. /* prevent scl_rate_khz from becoming 0 */
  653. if (WARN_ON(t->bus_freq_hz < 1000))
  654. t->bus_freq_hz = 1000;
  655. /*
  656. * min_low_ns: The minimum number of ns we need to hold low to
  657. * meet I2C specification, should include fall time.
  658. * min_high_ns: The minimum number of ns we need to hold high to
  659. * meet I2C specification, should include rise time.
  660. */
  661. spec = rk3x_i2c_get_spec(t->bus_freq_hz);
  662. /* calculate min-divh and min-divl */
  663. clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
  664. scl_rate_khz = t->bus_freq_hz / 1000;
  665. min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
  666. min_high_ns = t->scl_rise_ns + spec->min_high_ns;
  667. min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
  668. min_low_ns = t->scl_fall_ns + spec->min_low_ns;
  669. min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
  670. /*
  671. * Final divh and divl must be greater than 0, otherwise the
  672. * hardware would not output the i2c clk.
  673. */
  674. min_high_div = (min_high_div < 1) ? 2 : min_high_div;
  675. min_low_div = (min_low_div < 1) ? 2 : min_low_div;
  676. /* These are the min dividers needed for min hold times. */
  677. min_div_for_hold = (min_low_div + min_high_div);
  678. /*
  679. * This is the maximum divider so we don't go over the maximum.
  680. * We don't round up here (we round down) since this is a maximum.
  681. */
  682. if (min_div_for_hold >= min_total_div) {
  683. /*
  684. * Time needed to meet hold requirements is important.
  685. * Just use that.
  686. */
  687. t_calc->div_low = min_low_div;
  688. t_calc->div_high = min_high_div;
  689. } else {
  690. /*
  691. * We've got to distribute some time among the low and high
  692. * so we don't run too fast.
  693. * We'll try to split things up by the scale of min_low_div and
  694. * min_high_div, biasing slightly towards having a higher div
  695. * for low (spend more time low).
  696. */
  697. extra_div = min_total_div - min_div_for_hold;
  698. extra_low_div = DIV_ROUND_UP(min_low_div * extra_div,
  699. min_div_for_hold);
  700. t_calc->div_low = min_low_div + extra_low_div;
  701. t_calc->div_high = min_high_div + (extra_div - extra_low_div);
  702. }
  703. /*
  704. * calculate sda data hold count by the rules, data_upd_st:3
  705. * is a appropriate value to reduce calculated times.
  706. */
  707. for (sda_update_cfg = 3; sda_update_cfg > 0; sda_update_cfg--) {
  708. max_hold_data_ns = DIV_ROUND_UP((sda_update_cfg
  709. * (t_calc->div_low) + 1)
  710. * 1000000, clk_rate_khz);
  711. min_setup_data_ns = DIV_ROUND_UP(((8 - sda_update_cfg)
  712. * (t_calc->div_low) + 1)
  713. * 1000000, clk_rate_khz);
  714. if ((max_hold_data_ns < spec->max_data_hold_ns) &&
  715. (min_setup_data_ns > spec->min_data_setup_ns))
  716. break;
  717. }
  718. /* calculate setup start config */
  719. min_setup_start_ns = t->scl_rise_ns + spec->min_setup_start_ns;
  720. stp_sta_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_start_ns
  721. - 1000000, 8 * 1000000 * (t_calc->div_high));
  722. /* calculate setup stop config */
  723. min_setup_stop_ns = t->scl_rise_ns + spec->min_setup_stop_ns;
  724. stp_sto_cfg = DIV_ROUND_UP(clk_rate_khz * min_setup_stop_ns
  725. - 1000000, 8 * 1000000 * (t_calc->div_high));
  726. t_calc->tuning = REG_CON_SDA_CFG(--sda_update_cfg) |
  727. REG_CON_STA_CFG(--stp_sta_cfg) |
  728. REG_CON_STO_CFG(--stp_sto_cfg);
  729. t_calc->div_low--;
  730. t_calc->div_high--;
  731. /* Maximum divider supported by hw is 0xffff */
  732. if (t_calc->div_low > 0xffff) {
  733. t_calc->div_low = 0xffff;
  734. ret = -EINVAL;
  735. }
  736. if (t_calc->div_high > 0xffff) {
  737. t_calc->div_high = 0xffff;
  738. ret = -EINVAL;
  739. }
  740. return ret;
  741. }
  742. static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate)
  743. {
  744. struct i2c_timings *t = &i2c->t;
  745. struct rk3x_i2c_calced_timings calc;
  746. u64 t_low_ns, t_high_ns;
  747. unsigned long flags;
  748. u32 val;
  749. int ret;
  750. ret = i2c->soc_data->calc_timings(clk_rate, t, &calc);
  751. WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz);
  752. clk_enable(i2c->pclk);
  753. spin_lock_irqsave(&i2c->lock, flags);
  754. val = i2c_readl(i2c, REG_CON);
  755. val &= ~REG_CON_TUNING_MASK;
  756. val |= calc.tuning;
  757. i2c_writel(i2c, val, REG_CON);
  758. i2c_writel(i2c, (calc.div_high << 16) | (calc.div_low & 0xffff),
  759. REG_CLKDIV);
  760. spin_unlock_irqrestore(&i2c->lock, flags);
  761. clk_disable(i2c->pclk);
  762. t_low_ns = div_u64(((u64)calc.div_low + 1) * 8 * 1000000000, clk_rate);
  763. t_high_ns = div_u64(((u64)calc.div_high + 1) * 8 * 1000000000,
  764. clk_rate);
  765. dev_dbg(i2c->dev,
  766. "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n",
  767. clk_rate / 1000,
  768. 1000000000 / t->bus_freq_hz,
  769. t_low_ns, t_high_ns);
  770. }
  771. /**
  772. * rk3x_i2c_clk_notifier_cb - Clock rate change callback
  773. * @nb: Pointer to notifier block
  774. * @event: Notification reason
  775. * @data: Pointer to notification data object
  776. *
  777. * The callback checks whether a valid bus frequency can be generated after the
  778. * change. If so, the change is acknowledged, otherwise the change is aborted.
  779. * New dividers are written to the HW in the pre- or post change notification
  780. * depending on the scaling direction.
  781. *
  782. * Code adapted from i2c-cadence.c.
  783. *
  784. * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
  785. * to acknowledge the change, NOTIFY_DONE if the notification is
  786. * considered irrelevant.
  787. */
  788. static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
  789. event, void *data)
  790. {
  791. struct clk_notifier_data *ndata = data;
  792. struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb);
  793. struct rk3x_i2c_calced_timings calc;
  794. switch (event) {
  795. case PRE_RATE_CHANGE:
  796. /*
  797. * Try the calculation (but don't store the result) ahead of
  798. * time to see if we need to block the clock change. Timings
  799. * shouldn't actually take effect until rk3x_i2c_adapt_div().
  800. */
  801. if (i2c->soc_data->calc_timings(ndata->new_rate, &i2c->t,
  802. &calc) != 0)
  803. return NOTIFY_STOP;
  804. /* scale up */
  805. if (ndata->new_rate > ndata->old_rate)
  806. rk3x_i2c_adapt_div(i2c, ndata->new_rate);
  807. return NOTIFY_OK;
  808. case POST_RATE_CHANGE:
  809. /* scale down */
  810. if (ndata->new_rate < ndata->old_rate)
  811. rk3x_i2c_adapt_div(i2c, ndata->new_rate);
  812. return NOTIFY_OK;
  813. case ABORT_RATE_CHANGE:
  814. /* scale up */
  815. if (ndata->new_rate > ndata->old_rate)
  816. rk3x_i2c_adapt_div(i2c, ndata->old_rate);
  817. return NOTIFY_OK;
  818. default:
  819. return NOTIFY_DONE;
  820. }
  821. }
  822. /**
  823. * Setup I2C registers for an I2C operation specified by msgs, num.
  824. *
  825. * Must be called with i2c->lock held.
  826. *
  827. * @msgs: I2C msgs to process
  828. * @num: Number of msgs
  829. *
  830. * returns: Number of I2C msgs processed or negative in case of error
  831. */
  832. static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num)
  833. {
  834. u32 addr = (msgs[0].addr & 0x7f) << 1;
  835. int ret = 0;
  836. /*
  837. * The I2C adapter can issue a small (len < 4) write packet before
  838. * reading. This speeds up SMBus-style register reads.
  839. * The MRXADDR/MRXRADDR hold the slave address and the slave register
  840. * address in this case.
  841. */
  842. if (num >= 2 && msgs[0].len < 4 &&
  843. !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) {
  844. u32 reg_addr = 0;
  845. int i;
  846. dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n",
  847. addr >> 1);
  848. /* Fill MRXRADDR with the register address(es) */
  849. for (i = 0; i < msgs[0].len; ++i) {
  850. reg_addr |= msgs[0].buf[i] << (i * 8);
  851. reg_addr |= REG_MRXADDR_VALID(i);
  852. }
  853. /* msgs[0] is handled by hw. */
  854. i2c->msg = &msgs[1];
  855. i2c->mode = REG_CON_MOD_REGISTER_TX;
  856. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR);
  857. i2c_writel(i2c, reg_addr, REG_MRXRADDR);
  858. ret = 2;
  859. } else {
  860. /*
  861. * We'll have to do it the boring way and process the msgs
  862. * one-by-one.
  863. */
  864. if (msgs[0].flags & I2C_M_RD) {
  865. addr |= 1; /* set read bit */
  866. /*
  867. * We have to transmit the slave addr first. Use
  868. * MOD_REGISTER_TX for that purpose.
  869. */
  870. i2c->mode = REG_CON_MOD_REGISTER_TX;
  871. i2c_writel(i2c, addr | REG_MRXADDR_VALID(0),
  872. REG_MRXADDR);
  873. i2c_writel(i2c, 0, REG_MRXRADDR);
  874. } else {
  875. i2c->mode = REG_CON_MOD_TX;
  876. }
  877. i2c->msg = &msgs[0];
  878. ret = 1;
  879. }
  880. i2c->addr = msgs[0].addr;
  881. i2c->busy = true;
  882. i2c->state = STATE_START;
  883. i2c->processed = 0;
  884. i2c->error = 0;
  885. rk3x_i2c_clean_ipd(i2c);
  886. return ret;
  887. }
  888. static int rk3x_i2c_xfer(struct i2c_adapter *adap,
  889. struct i2c_msg *msgs, int num)
  890. {
  891. struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
  892. unsigned long timeout, flags;
  893. u32 val;
  894. int ret = 0;
  895. int i;
  896. spin_lock_irqsave(&i2c->lock, flags);
  897. clk_enable(i2c->clk);
  898. clk_enable(i2c->pclk);
  899. i2c->is_last_msg = false;
  900. /*
  901. * Process msgs. We can handle more than one message at once (see
  902. * rk3x_i2c_setup()).
  903. */
  904. for (i = 0; i < num; i += ret) {
  905. ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
  906. if (ret < 0) {
  907. dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
  908. break;
  909. }
  910. if (i + ret >= num)
  911. i2c->is_last_msg = true;
  912. spin_unlock_irqrestore(&i2c->lock, flags);
  913. rk3x_i2c_start(i2c);
  914. timeout = wait_event_timeout(i2c->wait, !i2c->busy,
  915. msecs_to_jiffies(WAIT_TIMEOUT));
  916. spin_lock_irqsave(&i2c->lock, flags);
  917. if (timeout == 0) {
  918. dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n",
  919. i2c_readl(i2c, REG_IPD), i2c->state);
  920. /* Force a STOP condition without interrupt */
  921. i2c_writel(i2c, 0, REG_IEN);
  922. val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
  923. val |= REG_CON_EN | REG_CON_STOP;
  924. i2c_writel(i2c, val, REG_CON);
  925. i2c->state = STATE_IDLE;
  926. ret = -ETIMEDOUT;
  927. break;
  928. }
  929. if (i2c->error) {
  930. ret = i2c->error;
  931. break;
  932. }
  933. }
  934. clk_disable(i2c->pclk);
  935. clk_disable(i2c->clk);
  936. spin_unlock_irqrestore(&i2c->lock, flags);
  937. return ret < 0 ? ret : num;
  938. }
  939. static __maybe_unused int rk3x_i2c_resume(struct device *dev)
  940. {
  941. struct rk3x_i2c *i2c = dev_get_drvdata(dev);
  942. rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk));
  943. return 0;
  944. }
  945. static u32 rk3x_i2c_func(struct i2c_adapter *adap)
  946. {
  947. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  948. }
  949. static const struct i2c_algorithm rk3x_i2c_algorithm = {
  950. .master_xfer = rk3x_i2c_xfer,
  951. .functionality = rk3x_i2c_func,
  952. };
  953. static const struct rk3x_i2c_soc_data rv1108_soc_data = {
  954. .grf_offset = -1,
  955. .calc_timings = rk3x_i2c_v1_calc_timings,
  956. };
  957. static const struct rk3x_i2c_soc_data rk3066_soc_data = {
  958. .grf_offset = 0x154,
  959. .calc_timings = rk3x_i2c_v0_calc_timings,
  960. };
  961. static const struct rk3x_i2c_soc_data rk3188_soc_data = {
  962. .grf_offset = 0x0a4,
  963. .calc_timings = rk3x_i2c_v0_calc_timings,
  964. };
  965. static const struct rk3x_i2c_soc_data rk3228_soc_data = {
  966. .grf_offset = -1,
  967. .calc_timings = rk3x_i2c_v0_calc_timings,
  968. };
  969. static const struct rk3x_i2c_soc_data rk3288_soc_data = {
  970. .grf_offset = -1,
  971. .calc_timings = rk3x_i2c_v0_calc_timings,
  972. };
  973. static const struct rk3x_i2c_soc_data rk3399_soc_data = {
  974. .grf_offset = -1,
  975. .calc_timings = rk3x_i2c_v1_calc_timings,
  976. };
  977. static const struct of_device_id rk3x_i2c_match[] = {
  978. {
  979. .compatible = "rockchip,rv1108-i2c",
  980. .data = &rv1108_soc_data
  981. },
  982. {
  983. .compatible = "rockchip,rk3066-i2c",
  984. .data = &rk3066_soc_data
  985. },
  986. {
  987. .compatible = "rockchip,rk3188-i2c",
  988. .data = &rk3188_soc_data
  989. },
  990. {
  991. .compatible = "rockchip,rk3228-i2c",
  992. .data = &rk3228_soc_data
  993. },
  994. {
  995. .compatible = "rockchip,rk3288-i2c",
  996. .data = &rk3288_soc_data
  997. },
  998. {
  999. .compatible = "rockchip,rk3399-i2c",
  1000. .data = &rk3399_soc_data
  1001. },
  1002. {},
  1003. };
  1004. MODULE_DEVICE_TABLE(of, rk3x_i2c_match);
  1005. static int rk3x_i2c_probe(struct platform_device *pdev)
  1006. {
  1007. struct device_node *np = pdev->dev.of_node;
  1008. const struct of_device_id *match;
  1009. struct rk3x_i2c *i2c;
  1010. struct resource *mem;
  1011. int ret = 0;
  1012. int bus_nr;
  1013. u32 value;
  1014. int irq;
  1015. unsigned long clk_rate;
  1016. i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
  1017. if (!i2c)
  1018. return -ENOMEM;
  1019. match = of_match_node(rk3x_i2c_match, np);
  1020. i2c->soc_data = match->data;
  1021. /* use common interface to get I2C timing properties */
  1022. i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
  1023. strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
  1024. i2c->adap.owner = THIS_MODULE;
  1025. i2c->adap.algo = &rk3x_i2c_algorithm;
  1026. i2c->adap.retries = 3;
  1027. i2c->adap.dev.of_node = np;
  1028. i2c->adap.algo_data = i2c;
  1029. i2c->adap.dev.parent = &pdev->dev;
  1030. i2c->dev = &pdev->dev;
  1031. spin_lock_init(&i2c->lock);
  1032. init_waitqueue_head(&i2c->wait);
  1033. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1034. i2c->regs = devm_ioremap_resource(&pdev->dev, mem);
  1035. if (IS_ERR(i2c->regs))
  1036. return PTR_ERR(i2c->regs);
  1037. /* Try to set the I2C adapter number from dt */
  1038. bus_nr = of_alias_get_id(np, "i2c");
  1039. /*
  1040. * Switch to new interface if the SoC also offers the old one.
  1041. * The control bit is located in the GRF register space.
  1042. */
  1043. if (i2c->soc_data->grf_offset >= 0) {
  1044. struct regmap *grf;
  1045. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  1046. if (IS_ERR(grf)) {
  1047. dev_err(&pdev->dev,
  1048. "rk3x-i2c needs 'rockchip,grf' property\n");
  1049. return PTR_ERR(grf);
  1050. }
  1051. if (bus_nr < 0) {
  1052. dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
  1053. return -EINVAL;
  1054. }
  1055. /* 27+i: write mask, 11+i: value */
  1056. value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
  1057. ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
  1058. if (ret != 0) {
  1059. dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
  1060. return ret;
  1061. }
  1062. }
  1063. /* IRQ setup */
  1064. irq = platform_get_irq(pdev, 0);
  1065. if (irq < 0) {
  1066. dev_err(&pdev->dev, "cannot find rk3x IRQ\n");
  1067. return irq;
  1068. }
  1069. ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
  1070. 0, dev_name(&pdev->dev), i2c);
  1071. if (ret < 0) {
  1072. dev_err(&pdev->dev, "cannot request IRQ\n");
  1073. return ret;
  1074. }
  1075. platform_set_drvdata(pdev, i2c);
  1076. if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) {
  1077. /* Only one clock to use for bus clock and peripheral clock */
  1078. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  1079. i2c->pclk = i2c->clk;
  1080. } else {
  1081. i2c->clk = devm_clk_get(&pdev->dev, "i2c");
  1082. i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
  1083. }
  1084. if (IS_ERR(i2c->clk)) {
  1085. ret = PTR_ERR(i2c->clk);
  1086. if (ret != -EPROBE_DEFER)
  1087. dev_err(&pdev->dev, "Can't get bus clk: %d\n", ret);
  1088. return ret;
  1089. }
  1090. if (IS_ERR(i2c->pclk)) {
  1091. ret = PTR_ERR(i2c->pclk);
  1092. if (ret != -EPROBE_DEFER)
  1093. dev_err(&pdev->dev, "Can't get periph clk: %d\n", ret);
  1094. return ret;
  1095. }
  1096. ret = clk_prepare(i2c->clk);
  1097. if (ret < 0) {
  1098. dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
  1099. return ret;
  1100. }
  1101. ret = clk_prepare(i2c->pclk);
  1102. if (ret < 0) {
  1103. dev_err(&pdev->dev, "Can't prepare periph clock: %d\n", ret);
  1104. goto err_clk;
  1105. }
  1106. i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
  1107. ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
  1108. if (ret != 0) {
  1109. dev_err(&pdev->dev, "Unable to register clock notifier\n");
  1110. goto err_pclk;
  1111. }
  1112. clk_rate = clk_get_rate(i2c->clk);
  1113. rk3x_i2c_adapt_div(i2c, clk_rate);
  1114. ret = i2c_add_adapter(&i2c->adap);
  1115. if (ret < 0)
  1116. goto err_clk_notifier;
  1117. return 0;
  1118. err_clk_notifier:
  1119. clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
  1120. err_pclk:
  1121. clk_unprepare(i2c->pclk);
  1122. err_clk:
  1123. clk_unprepare(i2c->clk);
  1124. return ret;
  1125. }
  1126. static int rk3x_i2c_remove(struct platform_device *pdev)
  1127. {
  1128. struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
  1129. i2c_del_adapter(&i2c->adap);
  1130. clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
  1131. clk_unprepare(i2c->pclk);
  1132. clk_unprepare(i2c->clk);
  1133. return 0;
  1134. }
  1135. static SIMPLE_DEV_PM_OPS(rk3x_i2c_pm_ops, NULL, rk3x_i2c_resume);
  1136. static struct platform_driver rk3x_i2c_driver = {
  1137. .probe = rk3x_i2c_probe,
  1138. .remove = rk3x_i2c_remove,
  1139. .driver = {
  1140. .name = "rk3x-i2c",
  1141. .of_match_table = rk3x_i2c_match,
  1142. .pm = &rk3x_i2c_pm_ops,
  1143. },
  1144. };
  1145. module_platform_driver(rk3x_i2c_driver);
  1146. MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
  1147. MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
  1148. MODULE_LICENSE("GPL v2");