i2c-rk3x.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for I2C adapter in Rockchip RK3xxx SoC
  4. *
  5. * Max Schwarz <max.schwarz@online.de>
  6. * based on the patches by Rockchip Inc.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/clk.h>
  21. #include <linux/wait.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/regmap.h>
  24. #include <linux/math64.h>
  25. /* Register Map */
  26. #define REG_CON 0x00 /* control register */
  27. #define REG_CLKDIV 0x04 /* clock divisor register */
  28. #define REG_MRXADDR 0x08 /* target address for REGISTER_TX */
  29. #define REG_MRXRADDR 0x0c /* target register address for REGISTER_TX */
  30. #define REG_MTXCNT 0x10 /* number of bytes to be transmitted */
  31. #define REG_MRXCNT 0x14 /* number of bytes to be received */
  32. #define REG_IEN 0x18 /* interrupt enable */
  33. #define REG_IPD 0x1c /* interrupt pending */
  34. #define REG_FCNT 0x20 /* finished count */
  35. /* Data buffer offsets */
  36. #define TXBUFFER_BASE 0x100
  37. #define RXBUFFER_BASE 0x200
  38. /* REG_CON bits */
  39. #define REG_CON_EN BIT(0)
  40. enum {
  41. REG_CON_MOD_TX = 0, /* transmit data */
  42. REG_CON_MOD_REGISTER_TX, /* select register and restart */
  43. REG_CON_MOD_RX, /* receive data */
  44. REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes
  45. * register addr */
  46. };
  47. #define REG_CON_MOD(mod) ((mod) << 1)
  48. #define REG_CON_MOD_MASK (BIT(1) | BIT(2))
  49. #define REG_CON_START BIT(3)
  50. #define REG_CON_STOP BIT(4)
  51. #define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */
  52. #define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */
  53. #define REG_CON_TUNING_MASK GENMASK_ULL(15, 8)
  54. #define REG_CON_SDA_CFG(cfg) ((cfg) << 8)
  55. #define REG_CON_STA_CFG(cfg) ((cfg) << 12)
  56. #define REG_CON_STO_CFG(cfg) ((cfg) << 14)
  57. /* REG_MRXADDR bits */
  58. #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */
  59. /* REG_IEN/REG_IPD bits */
  60. #define REG_INT_BTF BIT(0) /* a byte was transmitted */
  61. #define REG_INT_BRF BIT(1) /* a byte was received */
  62. #define REG_INT_MBTF BIT(2) /* controller data transmit finished */
  63. #define REG_INT_MBRF BIT(3) /* controller data receive finished */
  64. #define REG_INT_START BIT(4) /* START condition generated */
  65. #define REG_INT_STOP BIT(5) /* STOP condition generated */
  66. #define REG_INT_NAKRCV BIT(6) /* NACK received */
  67. #define REG_INT_ALL 0x7f
  68. /* Constants */
  69. #define WAIT_TIMEOUT 1000 /* ms */
  70. #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */
  71. /**
  72. * struct i2c_spec_values - I2C specification values for various modes
  73. * @min_hold_start_ns: min hold time (repeated) START condition
  74. * @min_low_ns: min LOW period of the SCL clock
  75. * @min_high_ns: min HIGH period of the SCL cloc
  76. * @min_setup_start_ns: min set-up time for a repeated START conditio
  77. * @max_data_hold_ns: max data hold time
  78. * @min_data_setup_ns: min data set-up time
  79. * @min_setup_stop_ns: min set-up time for STOP condition
  80. * @min_hold_buffer_ns: min bus free time between a STOP and
  81. * START condition
  82. */
  83. struct i2c_spec_values {
  84. unsigned long min_hold_start_ns;
  85. unsigned long min_low_ns;
  86. unsigned long min_high_ns;
  87. unsigned long min_setup_start_ns;
  88. unsigned long max_data_hold_ns;
  89. unsigned long min_data_setup_ns;
  90. unsigned long min_setup_stop_ns;
  91. unsigned long min_hold_buffer_ns;
  92. };
  93. static const struct i2c_spec_values standard_mode_spec = {
  94. .min_hold_start_ns = 4000,
  95. .min_low_ns = 4700,
  96. .min_high_ns = 4000,
  97. .min_setup_start_ns = 4700,
  98. .max_data_hold_ns = 3450,
  99. .min_data_setup_ns = 250,
  100. .min_setup_stop_ns = 4000,
  101. .min_hold_buffer_ns = 4700,
  102. };
  103. static const struct i2c_spec_values fast_mode_spec = {
  104. .min_hold_start_ns = 600,
  105. .min_low_ns = 1300,
  106. .min_high_ns = 600,
  107. .min_setup_start_ns = 600,
  108. .max_data_hold_ns = 900,
  109. .min_data_setup_ns = 100,
  110. .min_setup_stop_ns = 600,
  111. .min_hold_buffer_ns = 1300,
  112. };
  113. static const struct i2c_spec_values fast_mode_plus_spec = {
  114. .min_hold_start_ns = 260,
  115. .min_low_ns = 500,
  116. .min_high_ns = 260,
  117. .min_setup_start_ns = 260,
  118. .max_data_hold_ns = 400,
  119. .min_data_setup_ns = 50,
  120. .min_setup_stop_ns = 260,
  121. .min_hold_buffer_ns = 500,
  122. };
  123. /**
  124. * struct rk3x_i2c_calced_timings - calculated V1 timings
  125. * @div_low: Divider output for low
  126. * @div_high: Divider output for high
  127. * @tuning: Used to adjust setup/hold data time,
  128. * setup/hold start time and setup stop time for
  129. * v1's calc_timings, the tuning should all be 0
  130. * for old hardware anyone using v0's calc_timings.
  131. */
  132. struct rk3x_i2c_calced_timings {
  133. unsigned long div_low;
  134. unsigned long div_high;
  135. unsigned int tuning;
  136. };
  137. enum rk3x_i2c_state {
  138. STATE_IDLE,
  139. STATE_START,
  140. STATE_READ,
  141. STATE_WRITE,
  142. STATE_STOP
  143. };
  144. /**
  145. * struct rk3x_i2c_soc_data - SOC-specific data
  146. * @grf_offset: offset inside the grf regmap for setting the i2c type
  147. * @calc_timings: Callback function for i2c timing information calculated
  148. */
  149. struct rk3x_i2c_soc_data {
  150. int grf_offset;
  151. int (*calc_timings)(unsigned long, struct i2c_timings *,
  152. struct rk3x_i2c_calced_timings *);
  153. };
  154. /**
  155. * struct rk3x_i2c - private data of the controller
  156. * @adap: corresponding I2C adapter
  157. * @dev: device for this controller
  158. * @soc_data: related soc data struct
  159. * @regs: virtual memory area
  160. * @clk: function clk for rk3399 or function & Bus clks for others
  161. * @pclk: Bus clk for rk3399
  162. * @clk_rate_nb: i2c clk rate change notify
  163. * @irq: irq number
  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 target 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. const 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. int irq;
  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. * rk3x_i2c_start - Generate a START condition, which triggers a REG_INT_START interrupt.
  218. * @i2c: target controller data
  219. */
  220. static void rk3x_i2c_start(struct rk3x_i2c *i2c)
  221. {
  222. u32 val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
  223. i2c_writel(i2c, REG_INT_START, REG_IEN);
  224. /* enable adapter with correct mode, send START condition */
  225. val |= REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START;
  226. /* if we want to react to NACK, set ACTACK bit */
  227. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  228. val |= REG_CON_ACTACK;
  229. i2c_writel(i2c, val, REG_CON);
  230. }
  231. /**
  232. * rk3x_i2c_stop - Generate a STOP condition, which triggers a REG_INT_STOP interrupt.
  233. * @i2c: target controller data
  234. * @error: Error code to return in rk3x_i2c_xfer
  235. */
  236. static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error)
  237. {
  238. unsigned int ctrl;
  239. i2c->processed = 0;
  240. i2c->msg = NULL;
  241. i2c->error = error;
  242. if (i2c->is_last_msg) {
  243. /* Enable stop interrupt */
  244. i2c_writel(i2c, REG_INT_STOP, REG_IEN);
  245. i2c->state = STATE_STOP;
  246. ctrl = i2c_readl(i2c, REG_CON);
  247. ctrl |= REG_CON_STOP;
  248. i2c_writel(i2c, ctrl, REG_CON);
  249. } else {
  250. /* Signal rk3x_i2c_xfer to start the next message. */
  251. i2c->busy = false;
  252. i2c->state = STATE_IDLE;
  253. /*
  254. * The HW is actually not capable of REPEATED START. But we can
  255. * get the intended effect by resetting its internal state
  256. * and issuing an ordinary START.
  257. */
  258. ctrl = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
  259. i2c_writel(i2c, ctrl, REG_CON);
  260. /* signal that we are finished with the current msg */
  261. wake_up(&i2c->wait);
  262. }
  263. }
  264. /**
  265. * rk3x_i2c_prepare_read - Setup a read according to i2c->msg
  266. * @i2c: target controller data
  267. */
  268. static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c)
  269. {
  270. unsigned int len = i2c->msg->len - i2c->processed;
  271. u32 con;
  272. con = i2c_readl(i2c, REG_CON);
  273. /*
  274. * The hw can read up to 32 bytes at a time. If we need more than one
  275. * chunk, send an ACK after the last byte of the current chunk.
  276. */
  277. if (len > 32) {
  278. len = 32;
  279. con &= ~REG_CON_LASTACK;
  280. } else {
  281. con |= REG_CON_LASTACK;
  282. }
  283. /* make sure we are in plain RX mode if we read a second chunk */
  284. if (i2c->processed != 0) {
  285. con &= ~REG_CON_MOD_MASK;
  286. con |= REG_CON_MOD(REG_CON_MOD_RX);
  287. }
  288. i2c_writel(i2c, con, REG_CON);
  289. i2c_writel(i2c, len, REG_MRXCNT);
  290. }
  291. /**
  292. * rk3x_i2c_fill_transmit_buf - Fill the transmit buffer with data from i2c->msg
  293. * @i2c: target controller data
  294. */
  295. static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c)
  296. {
  297. unsigned int i, j;
  298. u32 cnt = 0;
  299. u32 val;
  300. u8 byte;
  301. for (i = 0; i < 8; ++i) {
  302. val = 0;
  303. for (j = 0; j < 4; ++j) {
  304. if ((i2c->processed == i2c->msg->len) && (cnt != 0))
  305. break;
  306. if (i2c->processed == 0 && cnt == 0)
  307. byte = (i2c->addr & 0x7f) << 1;
  308. else
  309. byte = i2c->msg->buf[i2c->processed++];
  310. val |= byte << (j * 8);
  311. cnt++;
  312. }
  313. i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i);
  314. if (i2c->processed == i2c->msg->len)
  315. break;
  316. }
  317. i2c_writel(i2c, cnt, REG_MTXCNT);
  318. }
  319. /* IRQ handlers for individual states */
  320. static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd)
  321. {
  322. if (!(ipd & REG_INT_START)) {
  323. rk3x_i2c_stop(i2c, -EIO);
  324. dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd);
  325. rk3x_i2c_clean_ipd(i2c);
  326. return;
  327. }
  328. /* ack interrupt */
  329. i2c_writel(i2c, REG_INT_START, REG_IPD);
  330. /* disable start bit */
  331. i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON);
  332. /* enable appropriate interrupts and transition */
  333. if (i2c->mode == REG_CON_MOD_TX) {
  334. i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN);
  335. i2c->state = STATE_WRITE;
  336. rk3x_i2c_fill_transmit_buf(i2c);
  337. } else {
  338. /* in any other case, we are going to be reading. */
  339. i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN);
  340. i2c->state = STATE_READ;
  341. rk3x_i2c_prepare_read(i2c);
  342. }
  343. }
  344. static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd)
  345. {
  346. if (!(ipd & REG_INT_MBTF)) {
  347. rk3x_i2c_stop(i2c, -EIO);
  348. dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd);
  349. rk3x_i2c_clean_ipd(i2c);
  350. return;
  351. }
  352. /* ack interrupt */
  353. i2c_writel(i2c, REG_INT_MBTF, REG_IPD);
  354. /* are we finished? */
  355. if (i2c->processed == i2c->msg->len)
  356. rk3x_i2c_stop(i2c, i2c->error);
  357. else
  358. rk3x_i2c_fill_transmit_buf(i2c);
  359. }
  360. static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd)
  361. {
  362. unsigned int i;
  363. unsigned int len = i2c->msg->len - i2c->processed;
  364. u32 val;
  365. u8 byte;
  366. /* we only care for MBRF here. */
  367. if (!(ipd & REG_INT_MBRF))
  368. return;
  369. /* ack interrupt (read also produces a spurious START flag, clear it too) */
  370. i2c_writel(i2c, REG_INT_MBRF | REG_INT_START, REG_IPD);
  371. /* Can only handle a maximum of 32 bytes at a time */
  372. if (len > 32)
  373. len = 32;
  374. /* read the data from receive buffer */
  375. for (i = 0; i < len; ++i) {
  376. if (i % 4 == 0)
  377. val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4);
  378. byte = (val >> ((i % 4) * 8)) & 0xff;
  379. i2c->msg->buf[i2c->processed++] = byte;
  380. }
  381. /* are we finished? */
  382. if (i2c->processed == i2c->msg->len)
  383. rk3x_i2c_stop(i2c, i2c->error);
  384. else
  385. rk3x_i2c_prepare_read(i2c);
  386. }
  387. static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd)
  388. {
  389. unsigned int con;
  390. if (!(ipd & REG_INT_STOP)) {
  391. rk3x_i2c_stop(i2c, -EIO);
  392. dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd);
  393. rk3x_i2c_clean_ipd(i2c);
  394. return;
  395. }
  396. /* ack interrupt */
  397. i2c_writel(i2c, REG_INT_STOP, REG_IPD);
  398. /* disable STOP bit */
  399. con = i2c_readl(i2c, REG_CON);
  400. con &= ~REG_CON_STOP;
  401. i2c_writel(i2c, con, REG_CON);
  402. i2c->busy = false;
  403. i2c->state = STATE_IDLE;
  404. /* signal rk3x_i2c_xfer that we are finished */
  405. wake_up(&i2c->wait);
  406. }
  407. static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id)
  408. {
  409. struct rk3x_i2c *i2c = dev_id;
  410. unsigned int ipd;
  411. spin_lock(&i2c->lock);
  412. ipd = i2c_readl(i2c, REG_IPD);
  413. if (i2c->state == STATE_IDLE) {
  414. dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd);
  415. rk3x_i2c_clean_ipd(i2c);
  416. goto out;
  417. }
  418. dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd);
  419. /* Clean interrupt bits we don't care about */
  420. ipd &= ~(REG_INT_BRF | REG_INT_BTF);
  421. if (ipd & REG_INT_NAKRCV) {
  422. /*
  423. * We got a NACK in the last operation. Depending on whether
  424. * IGNORE_NAK is set, we have to stop the operation and report
  425. * an error.
  426. */
  427. i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD);
  428. ipd &= ~REG_INT_NAKRCV;
  429. if (!(i2c->msg->flags & I2C_M_IGNORE_NAK))
  430. rk3x_i2c_stop(i2c, -ENXIO);
  431. }
  432. /* is there anything left to handle? */
  433. if ((ipd & REG_INT_ALL) == 0)
  434. goto out;
  435. switch (i2c->state) {
  436. case STATE_START:
  437. rk3x_i2c_handle_start(i2c, ipd);
  438. break;
  439. case STATE_WRITE:
  440. rk3x_i2c_handle_write(i2c, ipd);
  441. break;
  442. case STATE_READ:
  443. rk3x_i2c_handle_read(i2c, ipd);
  444. break;
  445. case STATE_STOP:
  446. rk3x_i2c_handle_stop(i2c, ipd);
  447. break;
  448. case STATE_IDLE:
  449. break;
  450. }
  451. out:
  452. spin_unlock(&i2c->lock);
  453. return IRQ_HANDLED;
  454. }
  455. /**
  456. * rk3x_i2c_get_spec - Get timing values of I2C specification
  457. * @speed: Desired SCL frequency
  458. *
  459. * Return: Matched i2c_spec_values.
  460. */
  461. static const struct i2c_spec_values *rk3x_i2c_get_spec(unsigned int speed)
  462. {
  463. if (speed <= I2C_MAX_STANDARD_MODE_FREQ)
  464. return &standard_mode_spec;
  465. else if (speed <= I2C_MAX_FAST_MODE_FREQ)
  466. return &fast_mode_spec;
  467. else
  468. return &fast_mode_plus_spec;
  469. }
  470. /**
  471. * rk3x_i2c_v0_calc_timings - Calculate divider values for desired SCL frequency
  472. * @clk_rate: I2C input clock rate
  473. * @t: Known I2C timing information
  474. * @t_calc: Caculated rk3x private timings that would be written into regs
  475. *
  476. * Return: %0 on success, -%EINVAL if the goal SCL rate is too slow. In that case
  477. * a best-effort divider value is returned in divs. If the target rate is
  478. * too high, we silently use the highest possible rate.
  479. */
  480. static int rk3x_i2c_v0_calc_timings(unsigned long clk_rate,
  481. struct i2c_timings *t,
  482. struct rk3x_i2c_calced_timings *t_calc)
  483. {
  484. unsigned long min_low_ns, min_high_ns;
  485. unsigned long max_low_ns, min_total_ns;
  486. unsigned long clk_rate_khz, scl_rate_khz;
  487. unsigned long min_low_div, min_high_div;
  488. unsigned long max_low_div;
  489. unsigned long min_div_for_hold, min_total_div;
  490. unsigned long extra_div, extra_low_div, ideal_low_div;
  491. unsigned long data_hold_buffer_ns = 50;
  492. const struct i2c_spec_values *spec;
  493. int ret = 0;
  494. /* Only support standard-mode and fast-mode */
  495. if (WARN_ON(t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ))
  496. t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
  497. /* prevent scl_rate_khz from becoming 0 */
  498. if (WARN_ON(t->bus_freq_hz < 1000))
  499. t->bus_freq_hz = 1000;
  500. /*
  501. * min_low_ns: The minimum number of ns we need to hold low to
  502. * meet I2C specification, should include fall time.
  503. * min_high_ns: The minimum number of ns we need to hold high to
  504. * meet I2C specification, should include rise time.
  505. * max_low_ns: The maximum number of ns we can hold low to meet
  506. * I2C specification.
  507. *
  508. * Note: max_low_ns should be (maximum data hold time * 2 - buffer)
  509. * This is because the i2c host on Rockchip holds the data line
  510. * for half the low time.
  511. */
  512. spec = rk3x_i2c_get_spec(t->bus_freq_hz);
  513. min_high_ns = t->scl_rise_ns + spec->min_high_ns;
  514. /*
  515. * Timings for repeated start:
  516. * - controller appears to drop SDA at .875x (7/8) programmed clk high.
  517. * - controller appears to keep SCL high for 2x programmed clk high.
  518. *
  519. * We need to account for those rules in picking our "high" time so
  520. * we meet tSU;STA and tHD;STA times.
  521. */
  522. min_high_ns = max(min_high_ns, DIV_ROUND_UP(
  523. (t->scl_rise_ns + spec->min_setup_start_ns) * 1000, 875));
  524. min_high_ns = max(min_high_ns, DIV_ROUND_UP(
  525. (t->scl_rise_ns + spec->min_setup_start_ns + t->sda_fall_ns +
  526. spec->min_high_ns), 2));
  527. min_low_ns = t->scl_fall_ns + spec->min_low_ns;
  528. max_low_ns = spec->max_data_hold_ns * 2 - data_hold_buffer_ns;
  529. min_total_ns = min_low_ns + min_high_ns;
  530. /* Adjust to avoid overflow */
  531. clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000);
  532. scl_rate_khz = t->bus_freq_hz / 1000;
  533. /*
  534. * We need the total div to be >= this number
  535. * so we don't clock too fast.
  536. */
  537. min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8);
  538. /* These are the min dividers needed for min hold times. */
  539. min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000);
  540. min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000);
  541. min_div_for_hold = (min_low_div + min_high_div);
  542. /*
  543. * This is the maximum divider so we don't go over the maximum.
  544. * We don't round up here (we round down) since this is a maximum.
  545. */
  546. max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000);
  547. if (min_low_div > max_low_div) {
  548. WARN_ONCE(true,
  549. "Conflicting, min_low_div %lu, max_low_div %lu\n",
  550. min_low_div, max_low_div);
  551. max_low_div = min_low_div;
  552. }
  553. if (min_div_for_hold > min_total_div) {
  554. /*
  555. * Time needed to meet hold requirements is important.
  556. * Just use that.
  557. */
  558. t_calc->div_low = min_low_div;
  559. t_calc->div_high = min_high_div;
  560. } else {
  561. /*
  562. * We've got to distribute some time among the low and high
  563. * so we don't run too fast.
  564. */
  565. extra_div = min_total_div - min_div_for_hold;
  566. /*
  567. * We'll try to split things up perfectly evenly,
  568. * biasing slightly towards having a higher div
  569. * for low (spend more time low).
  570. */
  571. ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns,
  572. scl_rate_khz * 8 * min_total_ns);
  573. /* Don't allow it to go over the maximum */
  574. if (ideal_low_div > max_low_div)
  575. ideal_low_div = max_low_div;
  576. /*
  577. * Handle when the ideal low div is going to take up
  578. * more than we have.
  579. */
  580. if (ideal_low_div > min_low_div + extra_div)
  581. ideal_low_div = min_low_div + extra_div;
  582. /* Give low the "ideal" and give high whatever extra is left */
  583. extra_low_div = ideal_low_div - min_low_div;
  584. t_calc->div_low = ideal_low_div;
  585. t_calc->div_high = min_high_div + (extra_div - extra_low_div);
  586. }
  587. /*
  588. * Adjust to the fact that the hardware has an implicit "+1".
  589. * NOTE: Above calculations always produce div_low > 0 and div_high > 0.
  590. */
  591. t_calc->div_low--;
  592. t_calc->div_high--;
  593. /* Give the tuning value 0, that would not update con register */
  594. t_calc->tuning = 0;
  595. /* Maximum divider supported by hw is 0xffff */
  596. if (t_calc->div_low > 0xffff) {
  597. t_calc->div_low = 0xffff;
  598. ret = -EINVAL;
  599. }
  600. if (t_calc->div_high > 0xffff) {
  601. t_calc->div_high = 0xffff;
  602. ret = -EINVAL;
  603. }
  604. return ret;
  605. }
  606. /**
  607. * rk3x_i2c_v1_calc_timings - Calculate timing values for desired SCL frequency
  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. * Return: %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 > I2C_MAX_FAST_MODE_PLUS_FREQ))
  651. t->bus_freq_hz = I2C_MAX_FAST_MODE_PLUS_FREQ;
  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. * rk3x_i2c_setup - Setup I2C registers for an I2C operation specified by msgs, num.
  824. * @i2c: target controller data
  825. * @msgs: I2C msgs to process
  826. * @num: Number of msgs
  827. *
  828. * Must be called with i2c->lock held.
  829. *
  830. * Return: 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 target address and the target 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 target 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_wait_xfer_poll(struct rk3x_i2c *i2c)
  889. {
  890. ktime_t timeout = ktime_add_ms(ktime_get(), WAIT_TIMEOUT);
  891. while (READ_ONCE(i2c->busy) &&
  892. ktime_compare(ktime_get(), timeout) < 0) {
  893. udelay(5);
  894. rk3x_i2c_irq(0, i2c);
  895. }
  896. return !i2c->busy;
  897. }
  898. static int rk3x_i2c_xfer_common(struct i2c_adapter *adap,
  899. struct i2c_msg *msgs, int num, bool polling)
  900. {
  901. struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data;
  902. unsigned long flags;
  903. long time_left;
  904. u32 val;
  905. int ret = 0;
  906. int i;
  907. spin_lock_irqsave(&i2c->lock, flags);
  908. clk_enable(i2c->clk);
  909. clk_enable(i2c->pclk);
  910. i2c->is_last_msg = false;
  911. /*
  912. * Process msgs. We can handle more than one message at once (see
  913. * rk3x_i2c_setup()).
  914. */
  915. for (i = 0; i < num; i += ret) {
  916. ret = rk3x_i2c_setup(i2c, msgs + i, num - i);
  917. if (ret < 0) {
  918. dev_err(i2c->dev, "rk3x_i2c_setup() failed\n");
  919. break;
  920. }
  921. if (i + ret >= num)
  922. i2c->is_last_msg = true;
  923. spin_unlock_irqrestore(&i2c->lock, flags);
  924. if (!polling) {
  925. rk3x_i2c_start(i2c);
  926. time_left = wait_event_timeout(i2c->wait, !i2c->busy,
  927. msecs_to_jiffies(WAIT_TIMEOUT));
  928. } else {
  929. disable_irq(i2c->irq);
  930. rk3x_i2c_start(i2c);
  931. time_left = rk3x_i2c_wait_xfer_poll(i2c);
  932. enable_irq(i2c->irq);
  933. }
  934. spin_lock_irqsave(&i2c->lock, flags);
  935. if (time_left == 0) {
  936. /* Force a STOP condition without interrupt */
  937. i2c_writel(i2c, 0, REG_IEN);
  938. val = i2c_readl(i2c, REG_CON) & REG_CON_TUNING_MASK;
  939. val |= REG_CON_EN | REG_CON_STOP;
  940. i2c_writel(i2c, val, REG_CON);
  941. i2c->state = STATE_IDLE;
  942. ret = -ETIMEDOUT;
  943. break;
  944. }
  945. if (i2c->error) {
  946. ret = i2c->error;
  947. break;
  948. }
  949. }
  950. clk_disable(i2c->pclk);
  951. clk_disable(i2c->clk);
  952. spin_unlock_irqrestore(&i2c->lock, flags);
  953. return ret < 0 ? ret : num;
  954. }
  955. static int rk3x_i2c_xfer(struct i2c_adapter *adap,
  956. struct i2c_msg *msgs, int num)
  957. {
  958. return rk3x_i2c_xfer_common(adap, msgs, num, false);
  959. }
  960. static int rk3x_i2c_xfer_polling(struct i2c_adapter *adap,
  961. struct i2c_msg *msgs, int num)
  962. {
  963. return rk3x_i2c_xfer_common(adap, msgs, num, true);
  964. }
  965. static __maybe_unused int rk3x_i2c_resume(struct device *dev)
  966. {
  967. struct rk3x_i2c *i2c = dev_get_drvdata(dev);
  968. rk3x_i2c_adapt_div(i2c, clk_get_rate(i2c->clk));
  969. return 0;
  970. }
  971. static u32 rk3x_i2c_func(struct i2c_adapter *adap)
  972. {
  973. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
  974. }
  975. static const struct i2c_algorithm rk3x_i2c_algorithm = {
  976. .xfer = rk3x_i2c_xfer,
  977. .xfer_atomic = rk3x_i2c_xfer_polling,
  978. .functionality = rk3x_i2c_func,
  979. };
  980. static const struct rk3x_i2c_soc_data rv1108_soc_data = {
  981. .grf_offset = -1,
  982. .calc_timings = rk3x_i2c_v1_calc_timings,
  983. };
  984. static const struct rk3x_i2c_soc_data rv1126_soc_data = {
  985. .grf_offset = 0x118,
  986. .calc_timings = rk3x_i2c_v1_calc_timings,
  987. };
  988. static const struct rk3x_i2c_soc_data rk3066_soc_data = {
  989. .grf_offset = 0x154,
  990. .calc_timings = rk3x_i2c_v0_calc_timings,
  991. };
  992. static const struct rk3x_i2c_soc_data rk3188_soc_data = {
  993. .grf_offset = 0x0a4,
  994. .calc_timings = rk3x_i2c_v0_calc_timings,
  995. };
  996. static const struct rk3x_i2c_soc_data rk3228_soc_data = {
  997. .grf_offset = -1,
  998. .calc_timings = rk3x_i2c_v0_calc_timings,
  999. };
  1000. static const struct rk3x_i2c_soc_data rk3288_soc_data = {
  1001. .grf_offset = -1,
  1002. .calc_timings = rk3x_i2c_v0_calc_timings,
  1003. };
  1004. static const struct rk3x_i2c_soc_data rk3399_soc_data = {
  1005. .grf_offset = -1,
  1006. .calc_timings = rk3x_i2c_v1_calc_timings,
  1007. };
  1008. static const struct of_device_id rk3x_i2c_match[] = {
  1009. {
  1010. .compatible = "rockchip,rv1108-i2c",
  1011. .data = &rv1108_soc_data
  1012. },
  1013. {
  1014. .compatible = "rockchip,rv1126-i2c",
  1015. .data = &rv1126_soc_data
  1016. },
  1017. {
  1018. .compatible = "rockchip,rk3066-i2c",
  1019. .data = &rk3066_soc_data
  1020. },
  1021. {
  1022. .compatible = "rockchip,rk3188-i2c",
  1023. .data = &rk3188_soc_data
  1024. },
  1025. {
  1026. .compatible = "rockchip,rk3228-i2c",
  1027. .data = &rk3228_soc_data
  1028. },
  1029. {
  1030. .compatible = "rockchip,rk3288-i2c",
  1031. .data = &rk3288_soc_data
  1032. },
  1033. {
  1034. .compatible = "rockchip,rk3399-i2c",
  1035. .data = &rk3399_soc_data
  1036. },
  1037. {},
  1038. };
  1039. MODULE_DEVICE_TABLE(of, rk3x_i2c_match);
  1040. static int rk3x_i2c_probe(struct platform_device *pdev)
  1041. {
  1042. struct device_node *np = pdev->dev.of_node;
  1043. const struct of_device_id *match;
  1044. struct rk3x_i2c *i2c;
  1045. int ret = 0;
  1046. int bus_nr;
  1047. u32 value;
  1048. int irq;
  1049. unsigned long clk_rate;
  1050. i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL);
  1051. if (!i2c)
  1052. return -ENOMEM;
  1053. match = of_match_node(rk3x_i2c_match, np);
  1054. i2c->soc_data = match->data;
  1055. /* use common interface to get I2C timing properties */
  1056. i2c_parse_fw_timings(&pdev->dev, &i2c->t, true);
  1057. strscpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name));
  1058. i2c->adap.owner = THIS_MODULE;
  1059. i2c->adap.algo = &rk3x_i2c_algorithm;
  1060. i2c->adap.retries = 3;
  1061. i2c->adap.dev.of_node = np;
  1062. i2c->adap.algo_data = i2c;
  1063. i2c->adap.dev.parent = &pdev->dev;
  1064. i2c->dev = &pdev->dev;
  1065. spin_lock_init(&i2c->lock);
  1066. init_waitqueue_head(&i2c->wait);
  1067. i2c->regs = devm_platform_ioremap_resource(pdev, 0);
  1068. if (IS_ERR(i2c->regs))
  1069. return PTR_ERR(i2c->regs);
  1070. /* Try to set the I2C adapter number from dt */
  1071. bus_nr = of_alias_get_id(np, "i2c");
  1072. /*
  1073. * Switch to new interface if the SoC also offers the old one.
  1074. * The control bit is located in the GRF register space.
  1075. */
  1076. if (i2c->soc_data->grf_offset >= 0) {
  1077. struct regmap *grf;
  1078. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  1079. if (IS_ERR(grf)) {
  1080. dev_err(&pdev->dev,
  1081. "rk3x-i2c needs 'rockchip,grf' property\n");
  1082. return PTR_ERR(grf);
  1083. }
  1084. if (bus_nr < 0) {
  1085. dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias");
  1086. return -EINVAL;
  1087. }
  1088. /* rv1126 i2c2 uses non-sequential write mask 20, value 4 */
  1089. if (i2c->soc_data == &rv1126_soc_data && bus_nr == 2)
  1090. value = BIT(20) | BIT(4);
  1091. else
  1092. /* 27+i: write mask, 11+i: value */
  1093. value = BIT(27 + bus_nr) | BIT(11 + bus_nr);
  1094. ret = regmap_write(grf, i2c->soc_data->grf_offset, value);
  1095. if (ret != 0) {
  1096. dev_err(i2c->dev, "Could not write to GRF: %d\n", ret);
  1097. return ret;
  1098. }
  1099. }
  1100. /* IRQ setup */
  1101. irq = platform_get_irq(pdev, 0);
  1102. if (irq < 0)
  1103. return irq;
  1104. ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq,
  1105. 0, dev_name(&pdev->dev), i2c);
  1106. if (ret < 0) {
  1107. dev_err(&pdev->dev, "cannot request IRQ\n");
  1108. return ret;
  1109. }
  1110. i2c->irq = irq;
  1111. platform_set_drvdata(pdev, i2c);
  1112. if (i2c->soc_data->calc_timings == rk3x_i2c_v0_calc_timings) {
  1113. /* Only one clock to use for bus clock and peripheral clock */
  1114. i2c->clk = devm_clk_get(&pdev->dev, NULL);
  1115. i2c->pclk = i2c->clk;
  1116. } else {
  1117. i2c->clk = devm_clk_get(&pdev->dev, "i2c");
  1118. i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
  1119. }
  1120. if (IS_ERR(i2c->clk))
  1121. return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk),
  1122. "Can't get bus clk\n");
  1123. if (IS_ERR(i2c->pclk))
  1124. return dev_err_probe(&pdev->dev, PTR_ERR(i2c->pclk),
  1125. "Can't get periph clk\n");
  1126. ret = clk_prepare(i2c->clk);
  1127. if (ret < 0) {
  1128. dev_err(&pdev->dev, "Can't prepare bus clk: %d\n", ret);
  1129. return ret;
  1130. }
  1131. ret = clk_prepare(i2c->pclk);
  1132. if (ret < 0) {
  1133. dev_err(&pdev->dev, "Can't prepare periph clock: %d\n", ret);
  1134. goto err_clk;
  1135. }
  1136. i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb;
  1137. ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb);
  1138. if (ret != 0) {
  1139. dev_err(&pdev->dev, "Unable to register clock notifier\n");
  1140. goto err_pclk;
  1141. }
  1142. ret = clk_enable(i2c->clk);
  1143. if (ret < 0) {
  1144. dev_err(&pdev->dev, "Can't enable bus clk: %d\n", ret);
  1145. goto err_clk_notifier;
  1146. }
  1147. clk_rate = clk_get_rate(i2c->clk);
  1148. rk3x_i2c_adapt_div(i2c, clk_rate);
  1149. clk_disable(i2c->clk);
  1150. ret = i2c_add_adapter(&i2c->adap);
  1151. if (ret < 0)
  1152. goto err_clk_notifier;
  1153. return 0;
  1154. err_clk_notifier:
  1155. clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
  1156. err_pclk:
  1157. clk_unprepare(i2c->pclk);
  1158. err_clk:
  1159. clk_unprepare(i2c->clk);
  1160. return ret;
  1161. }
  1162. static void rk3x_i2c_remove(struct platform_device *pdev)
  1163. {
  1164. struct rk3x_i2c *i2c = platform_get_drvdata(pdev);
  1165. i2c_del_adapter(&i2c->adap);
  1166. clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb);
  1167. clk_unprepare(i2c->pclk);
  1168. clk_unprepare(i2c->clk);
  1169. }
  1170. static SIMPLE_DEV_PM_OPS(rk3x_i2c_pm_ops, NULL, rk3x_i2c_resume);
  1171. static struct platform_driver rk3x_i2c_driver = {
  1172. .probe = rk3x_i2c_probe,
  1173. .remove_new = rk3x_i2c_remove,
  1174. .driver = {
  1175. .name = "rk3x-i2c",
  1176. .of_match_table = rk3x_i2c_match,
  1177. .pm = &rk3x_i2c_pm_ops,
  1178. },
  1179. };
  1180. module_platform_driver(rk3x_i2c_driver);
  1181. MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver");
  1182. MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>");
  1183. MODULE_LICENSE("GPL v2");