i2c-rk3x.c 35 KB

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