i2c-st.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 STMicroelectronics
  4. *
  5. * I2C master mode controller driver, used in STMicroelectronics devices.
  6. *
  7. * Author: Maxime Coquelin <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of.h>
  19. #include <linux/pinctrl/consumer.h>
  20. #include <linux/platform_device.h>
  21. /* SSC registers */
  22. #define SSC_BRG 0x000
  23. #define SSC_TBUF 0x004
  24. #define SSC_RBUF 0x008
  25. #define SSC_CTL 0x00C
  26. #define SSC_IEN 0x010
  27. #define SSC_STA 0x014
  28. #define SSC_I2C 0x018
  29. #define SSC_SLAD 0x01C
  30. #define SSC_REP_START_HOLD 0x020
  31. #define SSC_START_HOLD 0x024
  32. #define SSC_REP_START_SETUP 0x028
  33. #define SSC_DATA_SETUP 0x02C
  34. #define SSC_STOP_SETUP 0x030
  35. #define SSC_BUS_FREE 0x034
  36. #define SSC_TX_FSTAT 0x038
  37. #define SSC_RX_FSTAT 0x03C
  38. #define SSC_PRE_SCALER_BRG 0x040
  39. #define SSC_CLR 0x080
  40. #define SSC_NOISE_SUPP_WIDTH 0x100
  41. #define SSC_PRSCALER 0x104
  42. #define SSC_NOISE_SUPP_WIDTH_DATAOUT 0x108
  43. #define SSC_PRSCALER_DATAOUT 0x10c
  44. /* SSC Control */
  45. #define SSC_CTL_DATA_WIDTH_9 0x8
  46. #define SSC_CTL_DATA_WIDTH_MSK 0xf
  47. #define SSC_CTL_BM 0xf
  48. #define SSC_CTL_HB BIT(4)
  49. #define SSC_CTL_PH BIT(5)
  50. #define SSC_CTL_PO BIT(6)
  51. #define SSC_CTL_SR BIT(7)
  52. #define SSC_CTL_MS BIT(8)
  53. #define SSC_CTL_EN BIT(9)
  54. #define SSC_CTL_LPB BIT(10)
  55. #define SSC_CTL_EN_TX_FIFO BIT(11)
  56. #define SSC_CTL_EN_RX_FIFO BIT(12)
  57. #define SSC_CTL_EN_CLST_RX BIT(13)
  58. /* SSC Interrupt Enable */
  59. #define SSC_IEN_RIEN BIT(0)
  60. #define SSC_IEN_TIEN BIT(1)
  61. #define SSC_IEN_TEEN BIT(2)
  62. #define SSC_IEN_REEN BIT(3)
  63. #define SSC_IEN_PEEN BIT(4)
  64. #define SSC_IEN_AASEN BIT(6)
  65. #define SSC_IEN_STOPEN BIT(7)
  66. #define SSC_IEN_ARBLEN BIT(8)
  67. #define SSC_IEN_NACKEN BIT(10)
  68. #define SSC_IEN_REPSTRTEN BIT(11)
  69. #define SSC_IEN_TX_FIFO_HALF BIT(12)
  70. #define SSC_IEN_RX_FIFO_HALF_FULL BIT(14)
  71. /* SSC Status */
  72. #define SSC_STA_RIR BIT(0)
  73. #define SSC_STA_TIR BIT(1)
  74. #define SSC_STA_TE BIT(2)
  75. #define SSC_STA_RE BIT(3)
  76. #define SSC_STA_PE BIT(4)
  77. #define SSC_STA_CLST BIT(5)
  78. #define SSC_STA_AAS BIT(6)
  79. #define SSC_STA_STOP BIT(7)
  80. #define SSC_STA_ARBL BIT(8)
  81. #define SSC_STA_BUSY BIT(9)
  82. #define SSC_STA_NACK BIT(10)
  83. #define SSC_STA_REPSTRT BIT(11)
  84. #define SSC_STA_TX_FIFO_HALF BIT(12)
  85. #define SSC_STA_TX_FIFO_FULL BIT(13)
  86. #define SSC_STA_RX_FIFO_HALF BIT(14)
  87. /* SSC I2C Control */
  88. #define SSC_I2C_I2CM BIT(0)
  89. #define SSC_I2C_STRTG BIT(1)
  90. #define SSC_I2C_STOPG BIT(2)
  91. #define SSC_I2C_ACKG BIT(3)
  92. #define SSC_I2C_AD10 BIT(4)
  93. #define SSC_I2C_TXENB BIT(5)
  94. #define SSC_I2C_REPSTRTG BIT(11)
  95. #define SSC_I2C_SLAVE_DISABLE BIT(12)
  96. /* SSC Tx FIFO Status */
  97. #define SSC_TX_FSTAT_STATUS 0x07
  98. /* SSC Rx FIFO Status */
  99. #define SSC_RX_FSTAT_STATUS 0x07
  100. /* SSC Clear bit operation */
  101. #define SSC_CLR_SSCAAS BIT(6)
  102. #define SSC_CLR_SSCSTOP BIT(7)
  103. #define SSC_CLR_SSCARBL BIT(8)
  104. #define SSC_CLR_NACK BIT(10)
  105. #define SSC_CLR_REPSTRT BIT(11)
  106. /* SSC Clock Prescaler */
  107. #define SSC_PRSC_VALUE 0x0f
  108. #define SSC_TXFIFO_SIZE 0x8
  109. #define SSC_RXFIFO_SIZE 0x8
  110. enum st_i2c_mode {
  111. I2C_MODE_STANDARD,
  112. I2C_MODE_FAST,
  113. I2C_MODE_END,
  114. };
  115. /**
  116. * struct st_i2c_timings - per-Mode tuning parameters
  117. * @rate: I2C bus rate
  118. * @rep_start_hold: I2C repeated start hold time requirement
  119. * @rep_start_setup: I2C repeated start set up time requirement
  120. * @start_hold: I2C start hold time requirement
  121. * @data_setup_time: I2C data set up time requirement
  122. * @stop_setup_time: I2C stop set up time requirement
  123. * @bus_free_time: I2C bus free time requirement
  124. * @sda_pulse_min_limit: I2C SDA pulse mini width limit
  125. */
  126. struct st_i2c_timings {
  127. u32 rate;
  128. u32 rep_start_hold;
  129. u32 rep_start_setup;
  130. u32 start_hold;
  131. u32 data_setup_time;
  132. u32 stop_setup_time;
  133. u32 bus_free_time;
  134. u32 sda_pulse_min_limit;
  135. };
  136. /**
  137. * struct st_i2c_client - client specific data
  138. * @addr: 8-bit slave addr, including r/w bit
  139. * @count: number of bytes to be transfered
  140. * @xfered: number of bytes already transferred
  141. * @buf: data buffer
  142. * @result: result of the transfer
  143. * @stop: last I2C msg to be sent, i.e. STOP to be generated
  144. */
  145. struct st_i2c_client {
  146. u8 addr;
  147. u32 count;
  148. u32 xfered;
  149. u8 *buf;
  150. int result;
  151. bool stop;
  152. };
  153. /**
  154. * struct st_i2c_dev - private data of the controller
  155. * @adap: I2C adapter for this controller
  156. * @dev: device for this controller
  157. * @base: virtual memory area
  158. * @complete: completion of I2C message
  159. * @irq: interrupt line for th controller
  160. * @clk: hw ssc block clock
  161. * @mode: I2C mode of the controller. Standard or Fast only supported
  162. * @scl_min_width_us: SCL line minimum pulse width in us
  163. * @sda_min_width_us: SDA line minimum pulse width in us
  164. * @client: I2C transfert information
  165. * @busy: I2C transfer on-going
  166. */
  167. struct st_i2c_dev {
  168. struct i2c_adapter adap;
  169. struct device *dev;
  170. void __iomem *base;
  171. struct completion complete;
  172. int irq;
  173. struct clk *clk;
  174. int mode;
  175. u32 scl_min_width_us;
  176. u32 sda_min_width_us;
  177. struct st_i2c_client client;
  178. bool busy;
  179. };
  180. static inline void st_i2c_set_bits(void __iomem *reg, u32 mask)
  181. {
  182. writel_relaxed(readl_relaxed(reg) | mask, reg);
  183. }
  184. static inline void st_i2c_clr_bits(void __iomem *reg, u32 mask)
  185. {
  186. writel_relaxed(readl_relaxed(reg) & ~mask, reg);
  187. }
  188. /*
  189. * From I2C Specifications v0.5.
  190. *
  191. * All the values below have +10% margin added to be
  192. * compatible with some out-of-spec devices,
  193. * like HDMI link of the Toshiba 19AV600 TV.
  194. */
  195. static struct st_i2c_timings i2c_timings[] = {
  196. [I2C_MODE_STANDARD] = {
  197. .rate = I2C_MAX_STANDARD_MODE_FREQ,
  198. .rep_start_hold = 4400,
  199. .rep_start_setup = 5170,
  200. .start_hold = 4400,
  201. .data_setup_time = 275,
  202. .stop_setup_time = 4400,
  203. .bus_free_time = 5170,
  204. },
  205. [I2C_MODE_FAST] = {
  206. .rate = I2C_MAX_FAST_MODE_FREQ,
  207. .rep_start_hold = 660,
  208. .rep_start_setup = 660,
  209. .start_hold = 660,
  210. .data_setup_time = 110,
  211. .stop_setup_time = 660,
  212. .bus_free_time = 1430,
  213. },
  214. };
  215. static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev)
  216. {
  217. int count, i;
  218. /*
  219. * Counter only counts up to 7 but fifo size is 8...
  220. * When fifo is full, counter is 0 and RIR bit of status register is
  221. * set
  222. */
  223. if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR)
  224. count = SSC_RXFIFO_SIZE;
  225. else
  226. count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) &
  227. SSC_RX_FSTAT_STATUS;
  228. for (i = 0; i < count; i++)
  229. readl_relaxed(i2c_dev->base + SSC_RBUF);
  230. }
  231. static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev)
  232. {
  233. /*
  234. * FIFO needs to be emptied before reseting the IP,
  235. * else the controller raises a BUSY error.
  236. */
  237. st_i2c_flush_rx_fifo(i2c_dev);
  238. st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
  239. st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
  240. }
  241. /**
  242. * st_i2c_hw_config() - Prepare SSC block, calculate and apply tuning timings
  243. * @i2c_dev: Controller's private data
  244. */
  245. static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
  246. {
  247. unsigned long rate;
  248. u32 val, ns_per_clk;
  249. struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode];
  250. st_i2c_soft_reset(i2c_dev);
  251. val = SSC_CLR_REPSTRT | SSC_CLR_NACK | SSC_CLR_SSCARBL |
  252. SSC_CLR_SSCAAS | SSC_CLR_SSCSTOP;
  253. writel_relaxed(val, i2c_dev->base + SSC_CLR);
  254. /* SSC Control register setup */
  255. val = SSC_CTL_PO | SSC_CTL_PH | SSC_CTL_HB | SSC_CTL_DATA_WIDTH_9;
  256. writel_relaxed(val, i2c_dev->base + SSC_CTL);
  257. rate = clk_get_rate(i2c_dev->clk);
  258. ns_per_clk = 1000000000 / rate;
  259. /* Baudrate */
  260. val = rate / (2 * t->rate);
  261. writel_relaxed(val, i2c_dev->base + SSC_BRG);
  262. /* Pre-scaler baudrate */
  263. writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG);
  264. /* Enable I2C mode */
  265. writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C);
  266. /* Repeated start hold time */
  267. val = t->rep_start_hold / ns_per_clk;
  268. writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD);
  269. /* Repeated start set up time */
  270. val = t->rep_start_setup / ns_per_clk;
  271. writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP);
  272. /* Start hold time */
  273. val = t->start_hold / ns_per_clk;
  274. writel_relaxed(val, i2c_dev->base + SSC_START_HOLD);
  275. /* Data set up time */
  276. val = t->data_setup_time / ns_per_clk;
  277. writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP);
  278. /* Stop set up time */
  279. val = t->stop_setup_time / ns_per_clk;
  280. writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP);
  281. /* Bus free time */
  282. val = t->bus_free_time / ns_per_clk;
  283. writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE);
  284. /* Prescalers set up */
  285. val = rate / 10000000;
  286. writel_relaxed(val, i2c_dev->base + SSC_PRSCALER);
  287. writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT);
  288. /* Noise suppression witdh */
  289. val = i2c_dev->scl_min_width_us * rate / 100000000;
  290. writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH);
  291. /* Noise suppression max output data delay width */
  292. val = i2c_dev->sda_min_width_us * rate / 100000000;
  293. writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT);
  294. }
  295. static int st_i2c_recover_bus(struct i2c_adapter *i2c_adap)
  296. {
  297. struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
  298. u32 ctl;
  299. dev_dbg(i2c_dev->dev, "Trying to recover bus\n");
  300. /*
  301. * SSP IP is dual role SPI/I2C to generate 9 clock pulses
  302. * we switch to SPI node, 9 bit words and write a 0. This
  303. * has been validate with a oscilloscope and is easier
  304. * than switching to GPIO mode.
  305. */
  306. /* Disable interrupts */
  307. writel_relaxed(0, i2c_dev->base + SSC_IEN);
  308. st_i2c_hw_config(i2c_dev);
  309. ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
  310. st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
  311. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
  312. usleep_range(8000, 10000);
  313. writel_relaxed(0, i2c_dev->base + SSC_TBUF);
  314. usleep_range(2000, 4000);
  315. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
  316. return 0;
  317. }
  318. static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
  319. {
  320. u32 sta;
  321. int i, ret;
  322. for (i = 0; i < 10; i++) {
  323. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  324. if (!(sta & SSC_STA_BUSY))
  325. return 0;
  326. usleep_range(2000, 4000);
  327. }
  328. dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta);
  329. ret = i2c_recover_bus(&i2c_dev->adap);
  330. if (ret) {
  331. dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret);
  332. return ret;
  333. }
  334. return -EBUSY;
  335. }
  336. /**
  337. * st_i2c_write_tx_fifo() - Write a byte in the Tx FIFO
  338. * @i2c_dev: Controller's private data
  339. * @byte: Data to write in the Tx FIFO
  340. */
  341. static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte)
  342. {
  343. u16 tbuf = byte << 1;
  344. writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF);
  345. }
  346. /**
  347. * st_i2c_wr_fill_tx_fifo() - Fill the Tx FIFO in write mode
  348. * @i2c_dev: Controller's private data
  349. *
  350. * This functions fills the Tx FIFO with I2C transfert buffer when
  351. * in write mode.
  352. */
  353. static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
  354. {
  355. struct st_i2c_client *c = &i2c_dev->client;
  356. u32 tx_fstat, sta;
  357. int i;
  358. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  359. if (sta & SSC_STA_TX_FIFO_FULL)
  360. return;
  361. tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
  362. tx_fstat &= SSC_TX_FSTAT_STATUS;
  363. if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
  364. i = c->count;
  365. else
  366. i = SSC_TXFIFO_SIZE - tx_fstat;
  367. for (; i > 0; i--, c->count--, c->buf++)
  368. st_i2c_write_tx_fifo(i2c_dev, *c->buf);
  369. }
  370. /**
  371. * st_i2c_rd_fill_tx_fifo() - Fill the Tx FIFO in read mode
  372. * @i2c_dev: Controller's private data
  373. * @max: Maximum amount of data to fill into the Tx FIFO
  374. *
  375. * This functions fills the Tx FIFO with fixed pattern when
  376. * in read mode to trigger clock.
  377. */
  378. static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
  379. {
  380. struct st_i2c_client *c = &i2c_dev->client;
  381. u32 tx_fstat, sta;
  382. int i;
  383. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  384. if (sta & SSC_STA_TX_FIFO_FULL)
  385. return;
  386. tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
  387. tx_fstat &= SSC_TX_FSTAT_STATUS;
  388. if (max < (SSC_TXFIFO_SIZE - tx_fstat))
  389. i = max;
  390. else
  391. i = SSC_TXFIFO_SIZE - tx_fstat;
  392. for (; i > 0; i--, c->xfered++)
  393. st_i2c_write_tx_fifo(i2c_dev, 0xff);
  394. }
  395. static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev)
  396. {
  397. struct st_i2c_client *c = &i2c_dev->client;
  398. u32 i, sta;
  399. u16 rbuf;
  400. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  401. if (sta & SSC_STA_RIR) {
  402. i = SSC_RXFIFO_SIZE;
  403. } else {
  404. i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT);
  405. i &= SSC_RX_FSTAT_STATUS;
  406. }
  407. for (; (i > 0) && (c->count > 0); i--, c->count--) {
  408. rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1;
  409. *c->buf++ = (u8)rbuf & 0xff;
  410. }
  411. if (i) {
  412. dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i);
  413. st_i2c_flush_rx_fifo(i2c_dev);
  414. }
  415. }
  416. /**
  417. * st_i2c_terminate_xfer() - Send either STOP or REPSTART condition
  418. * @i2c_dev: Controller's private data
  419. */
  420. static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev)
  421. {
  422. struct st_i2c_client *c = &i2c_dev->client;
  423. st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN);
  424. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
  425. if (c->stop) {
  426. st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN);
  427. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  428. } else {
  429. st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN);
  430. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG);
  431. }
  432. }
  433. /**
  434. * st_i2c_handle_write() - Handle FIFO empty interrupt in case of write
  435. * @i2c_dev: Controller's private data
  436. */
  437. static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev)
  438. {
  439. struct st_i2c_client *c = &i2c_dev->client;
  440. st_i2c_flush_rx_fifo(i2c_dev);
  441. if (!c->count)
  442. /* End of xfer, send stop or repstart */
  443. st_i2c_terminate_xfer(i2c_dev);
  444. else
  445. st_i2c_wr_fill_tx_fifo(i2c_dev);
  446. }
  447. /**
  448. * st_i2c_handle_read() - Handle FIFO empty interrupt in case of read
  449. * @i2c_dev: Controller's private data
  450. */
  451. static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev)
  452. {
  453. struct st_i2c_client *c = &i2c_dev->client;
  454. u32 ien;
  455. /* Trash the address read back */
  456. if (!c->xfered) {
  457. readl_relaxed(i2c_dev->base + SSC_RBUF);
  458. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB);
  459. } else {
  460. st_i2c_read_rx_fifo(i2c_dev);
  461. }
  462. if (!c->count) {
  463. /* End of xfer, send stop or repstart */
  464. st_i2c_terminate_xfer(i2c_dev);
  465. } else if (c->count == 1) {
  466. /* Penultimate byte to xfer, disable ACK gen. */
  467. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG);
  468. /* Last received byte is to be handled by NACK interrupt */
  469. ien = SSC_IEN_NACKEN | SSC_IEN_ARBLEN;
  470. writel_relaxed(ien, i2c_dev->base + SSC_IEN);
  471. st_i2c_rd_fill_tx_fifo(i2c_dev, c->count);
  472. } else {
  473. st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1);
  474. }
  475. }
  476. /**
  477. * st_i2c_isr_thread() - Interrupt routine
  478. * @irq: interrupt number
  479. * @data: Controller's private data
  480. */
  481. static irqreturn_t st_i2c_isr_thread(int irq, void *data)
  482. {
  483. struct st_i2c_dev *i2c_dev = data;
  484. struct st_i2c_client *c = &i2c_dev->client;
  485. u32 sta, ien;
  486. int it;
  487. ien = readl_relaxed(i2c_dev->base + SSC_IEN);
  488. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  489. /* Use __fls() to check error bits first */
  490. it = __fls(sta & ien);
  491. if (it < 0) {
  492. dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n",
  493. sta, ien);
  494. return IRQ_NONE;
  495. }
  496. switch (1 << it) {
  497. case SSC_STA_TE:
  498. if (c->addr & I2C_M_RD)
  499. st_i2c_handle_read(i2c_dev);
  500. else
  501. st_i2c_handle_write(i2c_dev);
  502. break;
  503. case SSC_STA_STOP:
  504. case SSC_STA_REPSTRT:
  505. writel_relaxed(0, i2c_dev->base + SSC_IEN);
  506. complete(&i2c_dev->complete);
  507. break;
  508. case SSC_STA_NACK:
  509. writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR);
  510. /* Last received byte handled by NACK interrupt */
  511. if ((c->addr & I2C_M_RD) && (c->count == 1) && (c->xfered)) {
  512. st_i2c_handle_read(i2c_dev);
  513. break;
  514. }
  515. it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
  516. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  517. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  518. c->result = -EIO;
  519. break;
  520. case SSC_STA_ARBL:
  521. writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR);
  522. it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
  523. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  524. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  525. c->result = -EAGAIN;
  526. break;
  527. default:
  528. dev_err(i2c_dev->dev,
  529. "it %d unhandled (sta=0x%04x)\n", it, sta);
  530. }
  531. /*
  532. * Read IEN register to ensure interrupt mask write is effective
  533. * before re-enabling interrupt at GIC level, and thus avoid spurious
  534. * interrupts.
  535. */
  536. readl(i2c_dev->base + SSC_IEN);
  537. return IRQ_HANDLED;
  538. }
  539. /**
  540. * st_i2c_xfer_msg() - Transfer a single I2C message
  541. * @i2c_dev: Controller's private data
  542. * @msg: I2C message to transfer
  543. * @is_first: first message of the sequence
  544. * @is_last: last message of the sequence
  545. */
  546. static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg,
  547. bool is_first, bool is_last)
  548. {
  549. struct st_i2c_client *c = &i2c_dev->client;
  550. u32 ctl, i2c, it;
  551. unsigned long timeout;
  552. int ret;
  553. c->addr = i2c_8bit_addr_from_msg(msg);
  554. c->buf = msg->buf;
  555. c->count = msg->len;
  556. c->xfered = 0;
  557. c->result = 0;
  558. c->stop = is_last;
  559. reinit_completion(&i2c_dev->complete);
  560. ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
  561. st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
  562. i2c = SSC_I2C_TXENB;
  563. if (c->addr & I2C_M_RD)
  564. i2c |= SSC_I2C_ACKG;
  565. st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c);
  566. /* Write slave address */
  567. st_i2c_write_tx_fifo(i2c_dev, c->addr);
  568. /* Pre-fill Tx fifo with data in case of write */
  569. if (!(c->addr & I2C_M_RD))
  570. st_i2c_wr_fill_tx_fifo(i2c_dev);
  571. it = SSC_IEN_NACKEN | SSC_IEN_TEEN | SSC_IEN_ARBLEN;
  572. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  573. if (is_first) {
  574. ret = st_i2c_wait_free_bus(i2c_dev);
  575. if (ret)
  576. return ret;
  577. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
  578. }
  579. timeout = wait_for_completion_timeout(&i2c_dev->complete,
  580. i2c_dev->adap.timeout);
  581. ret = c->result;
  582. if (!timeout) {
  583. dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n",
  584. c->addr);
  585. ret = -ETIMEDOUT;
  586. }
  587. i2c = SSC_I2C_STOPG | SSC_I2C_REPSTRTG;
  588. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c);
  589. writel_relaxed(SSC_CLR_SSCSTOP | SSC_CLR_REPSTRT,
  590. i2c_dev->base + SSC_CLR);
  591. return ret;
  592. }
  593. /**
  594. * st_i2c_xfer() - Transfer a single I2C message
  595. * @i2c_adap: Adapter pointer to the controller
  596. * @msgs: Pointer to data to be written.
  597. * @num: Number of messages to be executed
  598. */
  599. static int st_i2c_xfer(struct i2c_adapter *i2c_adap,
  600. struct i2c_msg msgs[], int num)
  601. {
  602. struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
  603. int ret, i;
  604. i2c_dev->busy = true;
  605. ret = clk_prepare_enable(i2c_dev->clk);
  606. if (ret) {
  607. dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
  608. return ret;
  609. }
  610. pinctrl_pm_select_default_state(i2c_dev->dev);
  611. st_i2c_hw_config(i2c_dev);
  612. for (i = 0; (i < num) && !ret; i++)
  613. ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1);
  614. pinctrl_pm_select_idle_state(i2c_dev->dev);
  615. clk_disable_unprepare(i2c_dev->clk);
  616. i2c_dev->busy = false;
  617. return (ret < 0) ? ret : i;
  618. }
  619. #ifdef CONFIG_PM_SLEEP
  620. static int st_i2c_suspend(struct device *dev)
  621. {
  622. struct st_i2c_dev *i2c_dev = dev_get_drvdata(dev);
  623. if (i2c_dev->busy)
  624. return -EBUSY;
  625. pinctrl_pm_select_sleep_state(dev);
  626. return 0;
  627. }
  628. static int st_i2c_resume(struct device *dev)
  629. {
  630. pinctrl_pm_select_default_state(dev);
  631. /* Go in idle state if available */
  632. pinctrl_pm_select_idle_state(dev);
  633. return 0;
  634. }
  635. static SIMPLE_DEV_PM_OPS(st_i2c_pm, st_i2c_suspend, st_i2c_resume);
  636. #define ST_I2C_PM (&st_i2c_pm)
  637. #else
  638. #define ST_I2C_PM NULL
  639. #endif
  640. static u32 st_i2c_func(struct i2c_adapter *adap)
  641. {
  642. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  643. }
  644. static const struct i2c_algorithm st_i2c_algo = {
  645. .master_xfer = st_i2c_xfer,
  646. .functionality = st_i2c_func,
  647. };
  648. static struct i2c_bus_recovery_info st_i2c_recovery_info = {
  649. .recover_bus = st_i2c_recover_bus,
  650. };
  651. static int st_i2c_of_get_deglitch(struct device_node *np,
  652. struct st_i2c_dev *i2c_dev)
  653. {
  654. int ret;
  655. ret = of_property_read_u32(np, "st,i2c-min-scl-pulse-width-us",
  656. &i2c_dev->scl_min_width_us);
  657. if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
  658. dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n");
  659. return ret;
  660. }
  661. ret = of_property_read_u32(np, "st,i2c-min-sda-pulse-width-us",
  662. &i2c_dev->sda_min_width_us);
  663. if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
  664. dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n");
  665. return ret;
  666. }
  667. return 0;
  668. }
  669. static int st_i2c_probe(struct platform_device *pdev)
  670. {
  671. struct device_node *np = pdev->dev.of_node;
  672. struct st_i2c_dev *i2c_dev;
  673. struct resource *res;
  674. u32 clk_rate;
  675. struct i2c_adapter *adap;
  676. int ret;
  677. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  678. if (!i2c_dev)
  679. return -ENOMEM;
  680. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  681. i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
  682. if (IS_ERR(i2c_dev->base))
  683. return PTR_ERR(i2c_dev->base);
  684. i2c_dev->irq = irq_of_parse_and_map(np, 0);
  685. if (!i2c_dev->irq) {
  686. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  687. return -EINVAL;
  688. }
  689. i2c_dev->clk = of_clk_get_by_name(np, "ssc");
  690. if (IS_ERR(i2c_dev->clk)) {
  691. dev_err(&pdev->dev, "Unable to request clock\n");
  692. return PTR_ERR(i2c_dev->clk);
  693. }
  694. i2c_dev->mode = I2C_MODE_STANDARD;
  695. ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
  696. if (!ret && (clk_rate == I2C_MAX_FAST_MODE_FREQ))
  697. i2c_dev->mode = I2C_MODE_FAST;
  698. i2c_dev->dev = &pdev->dev;
  699. ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq,
  700. NULL, st_i2c_isr_thread,
  701. IRQF_ONESHOT, pdev->name, i2c_dev);
  702. if (ret) {
  703. dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
  704. return ret;
  705. }
  706. pinctrl_pm_select_default_state(i2c_dev->dev);
  707. /* In case idle state available, select it */
  708. pinctrl_pm_select_idle_state(i2c_dev->dev);
  709. ret = st_i2c_of_get_deglitch(np, i2c_dev);
  710. if (ret)
  711. return ret;
  712. adap = &i2c_dev->adap;
  713. i2c_set_adapdata(adap, i2c_dev);
  714. snprintf(adap->name, sizeof(adap->name), "ST I2C(%pa)", &res->start);
  715. adap->owner = THIS_MODULE;
  716. adap->timeout = 2 * HZ;
  717. adap->retries = 0;
  718. adap->algo = &st_i2c_algo;
  719. adap->bus_recovery_info = &st_i2c_recovery_info;
  720. adap->dev.parent = &pdev->dev;
  721. adap->dev.of_node = pdev->dev.of_node;
  722. init_completion(&i2c_dev->complete);
  723. ret = i2c_add_adapter(adap);
  724. if (ret)
  725. return ret;
  726. platform_set_drvdata(pdev, i2c_dev);
  727. dev_info(i2c_dev->dev, "%s initialized\n", adap->name);
  728. return 0;
  729. }
  730. static int st_i2c_remove(struct platform_device *pdev)
  731. {
  732. struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  733. i2c_del_adapter(&i2c_dev->adap);
  734. return 0;
  735. }
  736. static const struct of_device_id st_i2c_match[] = {
  737. { .compatible = "st,comms-ssc-i2c", },
  738. { .compatible = "st,comms-ssc4-i2c", },
  739. {},
  740. };
  741. MODULE_DEVICE_TABLE(of, st_i2c_match);
  742. static struct platform_driver st_i2c_driver = {
  743. .driver = {
  744. .name = "st-i2c",
  745. .of_match_table = st_i2c_match,
  746. .pm = ST_I2C_PM,
  747. },
  748. .probe = st_i2c_probe,
  749. .remove = st_i2c_remove,
  750. };
  751. module_platform_driver(st_i2c_driver);
  752. MODULE_AUTHOR("Maxime Coquelin <[email protected]>");
  753. MODULE_DESCRIPTION("STMicroelectronics I2C driver");
  754. MODULE_LICENSE("GPL v2");