i2c-st.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. /* From I2C Specifications v0.5 */
  191. static struct st_i2c_timings i2c_timings[] = {
  192. [I2C_MODE_STANDARD] = {
  193. .rate = 100000,
  194. .rep_start_hold = 4000,
  195. .rep_start_setup = 4700,
  196. .start_hold = 4000,
  197. .data_setup_time = 250,
  198. .stop_setup_time = 4000,
  199. .bus_free_time = 4700,
  200. },
  201. [I2C_MODE_FAST] = {
  202. .rate = 400000,
  203. .rep_start_hold = 600,
  204. .rep_start_setup = 600,
  205. .start_hold = 600,
  206. .data_setup_time = 100,
  207. .stop_setup_time = 600,
  208. .bus_free_time = 1300,
  209. },
  210. };
  211. static void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev)
  212. {
  213. int count, i;
  214. /*
  215. * Counter only counts up to 7 but fifo size is 8...
  216. * When fifo is full, counter is 0 and RIR bit of status register is
  217. * set
  218. */
  219. if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR)
  220. count = SSC_RXFIFO_SIZE;
  221. else
  222. count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) &
  223. SSC_RX_FSTAT_STATUS;
  224. for (i = 0; i < count; i++)
  225. readl_relaxed(i2c_dev->base + SSC_RBUF);
  226. }
  227. static void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev)
  228. {
  229. /*
  230. * FIFO needs to be emptied before reseting the IP,
  231. * else the controller raises a BUSY error.
  232. */
  233. st_i2c_flush_rx_fifo(i2c_dev);
  234. st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
  235. st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR);
  236. }
  237. /**
  238. * st_i2c_hw_config() - Prepare SSC block, calculate and apply tuning timings
  239. * @i2c_dev: Controller's private data
  240. */
  241. static void st_i2c_hw_config(struct st_i2c_dev *i2c_dev)
  242. {
  243. unsigned long rate;
  244. u32 val, ns_per_clk;
  245. struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode];
  246. st_i2c_soft_reset(i2c_dev);
  247. val = SSC_CLR_REPSTRT | SSC_CLR_NACK | SSC_CLR_SSCARBL |
  248. SSC_CLR_SSCAAS | SSC_CLR_SSCSTOP;
  249. writel_relaxed(val, i2c_dev->base + SSC_CLR);
  250. /* SSC Control register setup */
  251. val = SSC_CTL_PO | SSC_CTL_PH | SSC_CTL_HB | SSC_CTL_DATA_WIDTH_9;
  252. writel_relaxed(val, i2c_dev->base + SSC_CTL);
  253. rate = clk_get_rate(i2c_dev->clk);
  254. ns_per_clk = 1000000000 / rate;
  255. /* Baudrate */
  256. val = rate / (2 * t->rate);
  257. writel_relaxed(val, i2c_dev->base + SSC_BRG);
  258. /* Pre-scaler baudrate */
  259. writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG);
  260. /* Enable I2C mode */
  261. writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C);
  262. /* Repeated start hold time */
  263. val = t->rep_start_hold / ns_per_clk;
  264. writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD);
  265. /* Repeated start set up time */
  266. val = t->rep_start_setup / ns_per_clk;
  267. writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP);
  268. /* Start hold time */
  269. val = t->start_hold / ns_per_clk;
  270. writel_relaxed(val, i2c_dev->base + SSC_START_HOLD);
  271. /* Data set up time */
  272. val = t->data_setup_time / ns_per_clk;
  273. writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP);
  274. /* Stop set up time */
  275. val = t->stop_setup_time / ns_per_clk;
  276. writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP);
  277. /* Bus free time */
  278. val = t->bus_free_time / ns_per_clk;
  279. writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE);
  280. /* Prescalers set up */
  281. val = rate / 10000000;
  282. writel_relaxed(val, i2c_dev->base + SSC_PRSCALER);
  283. writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT);
  284. /* Noise suppression witdh */
  285. val = i2c_dev->scl_min_width_us * rate / 100000000;
  286. writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH);
  287. /* Noise suppression max output data delay width */
  288. val = i2c_dev->sda_min_width_us * rate / 100000000;
  289. writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT);
  290. }
  291. static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
  292. {
  293. u32 sta;
  294. int i;
  295. for (i = 0; i < 10; i++) {
  296. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  297. if (!(sta & SSC_STA_BUSY))
  298. return 0;
  299. usleep_range(2000, 4000);
  300. }
  301. dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta);
  302. return -EBUSY;
  303. }
  304. /**
  305. * st_i2c_write_tx_fifo() - Write a byte in the Tx FIFO
  306. * @i2c_dev: Controller's private data
  307. * @byte: Data to write in the Tx FIFO
  308. */
  309. static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte)
  310. {
  311. u16 tbuf = byte << 1;
  312. writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF);
  313. }
  314. /**
  315. * st_i2c_wr_fill_tx_fifo() - Fill the Tx FIFO in write mode
  316. * @i2c_dev: Controller's private data
  317. *
  318. * This functions fills the Tx FIFO with I2C transfert buffer when
  319. * in write mode.
  320. */
  321. static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
  322. {
  323. struct st_i2c_client *c = &i2c_dev->client;
  324. u32 tx_fstat, sta;
  325. int i;
  326. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  327. if (sta & SSC_STA_TX_FIFO_FULL)
  328. return;
  329. tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
  330. tx_fstat &= SSC_TX_FSTAT_STATUS;
  331. if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
  332. i = c->count;
  333. else
  334. i = SSC_TXFIFO_SIZE - tx_fstat;
  335. for (; i > 0; i--, c->count--, c->buf++)
  336. st_i2c_write_tx_fifo(i2c_dev, *c->buf);
  337. }
  338. /**
  339. * st_i2c_rd_fill_tx_fifo() - Fill the Tx FIFO in read mode
  340. * @i2c_dev: Controller's private data
  341. *
  342. * This functions fills the Tx FIFO with fixed pattern when
  343. * in read mode to trigger clock.
  344. */
  345. static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
  346. {
  347. struct st_i2c_client *c = &i2c_dev->client;
  348. u32 tx_fstat, sta;
  349. int i;
  350. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  351. if (sta & SSC_STA_TX_FIFO_FULL)
  352. return;
  353. tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
  354. tx_fstat &= SSC_TX_FSTAT_STATUS;
  355. if (max < (SSC_TXFIFO_SIZE - tx_fstat))
  356. i = max;
  357. else
  358. i = SSC_TXFIFO_SIZE - tx_fstat;
  359. for (; i > 0; i--, c->xfered++)
  360. st_i2c_write_tx_fifo(i2c_dev, 0xff);
  361. }
  362. static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev)
  363. {
  364. struct st_i2c_client *c = &i2c_dev->client;
  365. u32 i, sta;
  366. u16 rbuf;
  367. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  368. if (sta & SSC_STA_RIR) {
  369. i = SSC_RXFIFO_SIZE;
  370. } else {
  371. i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT);
  372. i &= SSC_RX_FSTAT_STATUS;
  373. }
  374. for (; (i > 0) && (c->count > 0); i--, c->count--) {
  375. rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1;
  376. *c->buf++ = (u8)rbuf & 0xff;
  377. }
  378. if (i) {
  379. dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i);
  380. st_i2c_flush_rx_fifo(i2c_dev);
  381. }
  382. }
  383. /**
  384. * st_i2c_terminate_xfer() - Send either STOP or REPSTART condition
  385. * @i2c_dev: Controller's private data
  386. */
  387. static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev)
  388. {
  389. struct st_i2c_client *c = &i2c_dev->client;
  390. st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN);
  391. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
  392. if (c->stop) {
  393. st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN);
  394. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  395. } else {
  396. st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN);
  397. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG);
  398. }
  399. }
  400. /**
  401. * st_i2c_handle_write() - Handle FIFO empty interrupt in case of write
  402. * @i2c_dev: Controller's private data
  403. */
  404. static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev)
  405. {
  406. struct st_i2c_client *c = &i2c_dev->client;
  407. st_i2c_flush_rx_fifo(i2c_dev);
  408. if (!c->count)
  409. /* End of xfer, send stop or repstart */
  410. st_i2c_terminate_xfer(i2c_dev);
  411. else
  412. st_i2c_wr_fill_tx_fifo(i2c_dev);
  413. }
  414. /**
  415. * st_i2c_handle_write() - Handle FIFO enmpty interrupt in case of read
  416. * @i2c_dev: Controller's private data
  417. */
  418. static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev)
  419. {
  420. struct st_i2c_client *c = &i2c_dev->client;
  421. u32 ien;
  422. /* Trash the address read back */
  423. if (!c->xfered) {
  424. readl_relaxed(i2c_dev->base + SSC_RBUF);
  425. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB);
  426. } else {
  427. st_i2c_read_rx_fifo(i2c_dev);
  428. }
  429. if (!c->count) {
  430. /* End of xfer, send stop or repstart */
  431. st_i2c_terminate_xfer(i2c_dev);
  432. } else if (c->count == 1) {
  433. /* Penultimate byte to xfer, disable ACK gen. */
  434. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG);
  435. /* Last received byte is to be handled by NACK interrupt */
  436. ien = SSC_IEN_NACKEN | SSC_IEN_ARBLEN;
  437. writel_relaxed(ien, i2c_dev->base + SSC_IEN);
  438. st_i2c_rd_fill_tx_fifo(i2c_dev, c->count);
  439. } else {
  440. st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1);
  441. }
  442. }
  443. /**
  444. * st_i2c_isr() - Interrupt routine
  445. * @irq: interrupt number
  446. * @data: Controller's private data
  447. */
  448. static irqreturn_t st_i2c_isr_thread(int irq, void *data)
  449. {
  450. struct st_i2c_dev *i2c_dev = data;
  451. struct st_i2c_client *c = &i2c_dev->client;
  452. u32 sta, ien;
  453. int it;
  454. ien = readl_relaxed(i2c_dev->base + SSC_IEN);
  455. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  456. /* Use __fls() to check error bits first */
  457. it = __fls(sta & ien);
  458. if (it < 0) {
  459. dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n",
  460. sta, ien);
  461. return IRQ_NONE;
  462. }
  463. switch (1 << it) {
  464. case SSC_STA_TE:
  465. if (c->addr & I2C_M_RD)
  466. st_i2c_handle_read(i2c_dev);
  467. else
  468. st_i2c_handle_write(i2c_dev);
  469. break;
  470. case SSC_STA_STOP:
  471. case SSC_STA_REPSTRT:
  472. writel_relaxed(0, i2c_dev->base + SSC_IEN);
  473. complete(&i2c_dev->complete);
  474. break;
  475. case SSC_STA_NACK:
  476. writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR);
  477. /* Last received byte handled by NACK interrupt */
  478. if ((c->addr & I2C_M_RD) && (c->count == 1) && (c->xfered)) {
  479. st_i2c_handle_read(i2c_dev);
  480. break;
  481. }
  482. it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
  483. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  484. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  485. c->result = -EIO;
  486. break;
  487. case SSC_STA_ARBL:
  488. writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR);
  489. it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
  490. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  491. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  492. c->result = -EAGAIN;
  493. break;
  494. default:
  495. dev_err(i2c_dev->dev,
  496. "it %d unhandled (sta=0x%04x)\n", it, sta);
  497. }
  498. /*
  499. * Read IEN register to ensure interrupt mask write is effective
  500. * before re-enabling interrupt at GIC level, and thus avoid spurious
  501. * interrupts.
  502. */
  503. readl(i2c_dev->base + SSC_IEN);
  504. return IRQ_HANDLED;
  505. }
  506. /**
  507. * st_i2c_xfer_msg() - Transfer a single I2C message
  508. * @i2c_dev: Controller's private data
  509. * @msg: I2C message to transfer
  510. * @is_first: first message of the sequence
  511. * @is_last: last message of the sequence
  512. */
  513. static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg,
  514. bool is_first, bool is_last)
  515. {
  516. struct st_i2c_client *c = &i2c_dev->client;
  517. u32 ctl, i2c, it;
  518. unsigned long timeout;
  519. int ret;
  520. c->addr = (u8)(msg->addr << 1);
  521. c->addr |= (msg->flags & I2C_M_RD);
  522. c->buf = msg->buf;
  523. c->count = msg->len;
  524. c->xfered = 0;
  525. c->result = 0;
  526. c->stop = is_last;
  527. reinit_completion(&i2c_dev->complete);
  528. ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
  529. st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
  530. i2c = SSC_I2C_TXENB;
  531. if (c->addr & I2C_M_RD)
  532. i2c |= SSC_I2C_ACKG;
  533. st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c);
  534. /* Write slave address */
  535. st_i2c_write_tx_fifo(i2c_dev, c->addr);
  536. /* Pre-fill Tx fifo with data in case of write */
  537. if (!(c->addr & I2C_M_RD))
  538. st_i2c_wr_fill_tx_fifo(i2c_dev);
  539. it = SSC_IEN_NACKEN | SSC_IEN_TEEN | SSC_IEN_ARBLEN;
  540. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  541. if (is_first) {
  542. ret = st_i2c_wait_free_bus(i2c_dev);
  543. if (ret)
  544. return ret;
  545. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
  546. }
  547. timeout = wait_for_completion_timeout(&i2c_dev->complete,
  548. i2c_dev->adap.timeout);
  549. ret = c->result;
  550. if (!timeout) {
  551. dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n",
  552. c->addr);
  553. ret = -ETIMEDOUT;
  554. }
  555. i2c = SSC_I2C_STOPG | SSC_I2C_REPSTRTG;
  556. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c);
  557. writel_relaxed(SSC_CLR_SSCSTOP | SSC_CLR_REPSTRT,
  558. i2c_dev->base + SSC_CLR);
  559. return ret;
  560. }
  561. /**
  562. * st_i2c_xfer() - Transfer a single I2C message
  563. * @i2c_adap: Adapter pointer to the controller
  564. * @msgs: Pointer to data to be written.
  565. * @num: Number of messages to be executed
  566. */
  567. static int st_i2c_xfer(struct i2c_adapter *i2c_adap,
  568. struct i2c_msg msgs[], int num)
  569. {
  570. struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
  571. int ret, i;
  572. i2c_dev->busy = true;
  573. ret = clk_prepare_enable(i2c_dev->clk);
  574. if (ret) {
  575. dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
  576. return ret;
  577. }
  578. pinctrl_pm_select_default_state(i2c_dev->dev);
  579. st_i2c_hw_config(i2c_dev);
  580. for (i = 0; (i < num) && !ret; i++)
  581. ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1);
  582. pinctrl_pm_select_idle_state(i2c_dev->dev);
  583. clk_disable_unprepare(i2c_dev->clk);
  584. i2c_dev->busy = false;
  585. return (ret < 0) ? ret : i;
  586. }
  587. #ifdef CONFIG_PM_SLEEP
  588. static int st_i2c_suspend(struct device *dev)
  589. {
  590. struct platform_device *pdev =
  591. container_of(dev, struct platform_device, dev);
  592. struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  593. if (i2c_dev->busy)
  594. return -EBUSY;
  595. pinctrl_pm_select_sleep_state(dev);
  596. return 0;
  597. }
  598. static int st_i2c_resume(struct device *dev)
  599. {
  600. pinctrl_pm_select_default_state(dev);
  601. /* Go in idle state if available */
  602. pinctrl_pm_select_idle_state(dev);
  603. return 0;
  604. }
  605. static SIMPLE_DEV_PM_OPS(st_i2c_pm, st_i2c_suspend, st_i2c_resume);
  606. #define ST_I2C_PM (&st_i2c_pm)
  607. #else
  608. #define ST_I2C_PM NULL
  609. #endif
  610. static u32 st_i2c_func(struct i2c_adapter *adap)
  611. {
  612. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  613. }
  614. static struct i2c_algorithm st_i2c_algo = {
  615. .master_xfer = st_i2c_xfer,
  616. .functionality = st_i2c_func,
  617. };
  618. static int st_i2c_of_get_deglitch(struct device_node *np,
  619. struct st_i2c_dev *i2c_dev)
  620. {
  621. int ret;
  622. ret = of_property_read_u32(np, "st,i2c-min-scl-pulse-width-us",
  623. &i2c_dev->scl_min_width_us);
  624. if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
  625. dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n");
  626. return ret;
  627. }
  628. ret = of_property_read_u32(np, "st,i2c-min-sda-pulse-width-us",
  629. &i2c_dev->sda_min_width_us);
  630. if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
  631. dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n");
  632. return ret;
  633. }
  634. return 0;
  635. }
  636. static int st_i2c_probe(struct platform_device *pdev)
  637. {
  638. struct device_node *np = pdev->dev.of_node;
  639. struct st_i2c_dev *i2c_dev;
  640. struct resource *res;
  641. u32 clk_rate;
  642. struct i2c_adapter *adap;
  643. int ret;
  644. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  645. if (!i2c_dev)
  646. return -ENOMEM;
  647. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  648. i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
  649. if (IS_ERR(i2c_dev->base))
  650. return PTR_ERR(i2c_dev->base);
  651. i2c_dev->irq = irq_of_parse_and_map(np, 0);
  652. if (!i2c_dev->irq) {
  653. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  654. return -EINVAL;
  655. }
  656. i2c_dev->clk = of_clk_get_by_name(np, "ssc");
  657. if (IS_ERR(i2c_dev->clk)) {
  658. dev_err(&pdev->dev, "Unable to request clock\n");
  659. return PTR_ERR(i2c_dev->clk);
  660. }
  661. i2c_dev->mode = I2C_MODE_STANDARD;
  662. ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
  663. if ((!ret) && (clk_rate == 400000))
  664. i2c_dev->mode = I2C_MODE_FAST;
  665. i2c_dev->dev = &pdev->dev;
  666. ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq,
  667. NULL, st_i2c_isr_thread,
  668. IRQF_ONESHOT, pdev->name, i2c_dev);
  669. if (ret) {
  670. dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
  671. return ret;
  672. }
  673. pinctrl_pm_select_default_state(i2c_dev->dev);
  674. /* In case idle state available, select it */
  675. pinctrl_pm_select_idle_state(i2c_dev->dev);
  676. ret = st_i2c_of_get_deglitch(np, i2c_dev);
  677. if (ret)
  678. return ret;
  679. adap = &i2c_dev->adap;
  680. i2c_set_adapdata(adap, i2c_dev);
  681. snprintf(adap->name, sizeof(adap->name), "ST I2C(0x%x)", res->start);
  682. adap->owner = THIS_MODULE;
  683. adap->timeout = 2 * HZ;
  684. adap->retries = 0;
  685. adap->algo = &st_i2c_algo;
  686. adap->dev.parent = &pdev->dev;
  687. adap->dev.of_node = pdev->dev.of_node;
  688. init_completion(&i2c_dev->complete);
  689. ret = i2c_add_adapter(adap);
  690. if (ret) {
  691. dev_err(&pdev->dev, "Failed to add adapter\n");
  692. return ret;
  693. }
  694. platform_set_drvdata(pdev, i2c_dev);
  695. dev_info(i2c_dev->dev, "%s initialized\n", adap->name);
  696. return 0;
  697. }
  698. static int st_i2c_remove(struct platform_device *pdev)
  699. {
  700. struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  701. i2c_del_adapter(&i2c_dev->adap);
  702. return 0;
  703. }
  704. static const struct of_device_id st_i2c_match[] = {
  705. { .compatible = "st,comms-ssc-i2c", },
  706. { .compatible = "st,comms-ssc4-i2c", },
  707. {},
  708. };
  709. MODULE_DEVICE_TABLE(of, st_i2c_match);
  710. static struct platform_driver st_i2c_driver = {
  711. .driver = {
  712. .name = "st-i2c",
  713. .owner = THIS_MODULE,
  714. .of_match_table = st_i2c_match,
  715. .pm = ST_I2C_PM,
  716. },
  717. .probe = st_i2c_probe,
  718. .remove = st_i2c_remove,
  719. };
  720. module_platform_driver(st_i2c_driver);
  721. MODULE_AUTHOR("Maxime Coquelin <maxime.coquelin@st.com>");
  722. MODULE_DESCRIPTION("STMicroelectronics I2C driver");
  723. MODULE_LICENSE("GPL v2");