i2c-st.c 22 KB

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