i2c-st.c 22 KB

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