i2c-st.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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_recover_bus(struct i2c_adapter *i2c_adap)
  299. {
  300. struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
  301. u32 ctl;
  302. dev_dbg(i2c_dev->dev, "Trying to recover bus\n");
  303. /*
  304. * SSP IP is dual role SPI/I2C to generate 9 clock pulses
  305. * we switch to SPI node, 9 bit words and write a 0. This
  306. * has been validate with a oscilloscope and is easier
  307. * than switching to GPIO mode.
  308. */
  309. /* Disable interrupts */
  310. writel_relaxed(0, i2c_dev->base + SSC_IEN);
  311. st_i2c_hw_config(i2c_dev);
  312. ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
  313. st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
  314. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
  315. usleep_range(8000, 10000);
  316. writel_relaxed(0, i2c_dev->base + SSC_TBUF);
  317. usleep_range(2000, 4000);
  318. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_I2CM);
  319. return 0;
  320. }
  321. static int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev)
  322. {
  323. u32 sta;
  324. int i, ret;
  325. for (i = 0; i < 10; i++) {
  326. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  327. if (!(sta & SSC_STA_BUSY))
  328. return 0;
  329. usleep_range(2000, 4000);
  330. }
  331. dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta);
  332. ret = i2c_recover_bus(&i2c_dev->adap);
  333. if (ret) {
  334. dev_err(i2c_dev->dev, "Failed to recover the bus (%d)\n", ret);
  335. return ret;
  336. }
  337. return -EBUSY;
  338. }
  339. /**
  340. * st_i2c_write_tx_fifo() - Write a byte in the Tx FIFO
  341. * @i2c_dev: Controller's private data
  342. * @byte: Data to write in the Tx FIFO
  343. */
  344. static inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte)
  345. {
  346. u16 tbuf = byte << 1;
  347. writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF);
  348. }
  349. /**
  350. * st_i2c_wr_fill_tx_fifo() - Fill the Tx FIFO in write mode
  351. * @i2c_dev: Controller's private data
  352. *
  353. * This functions fills the Tx FIFO with I2C transfert buffer when
  354. * in write mode.
  355. */
  356. static void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev)
  357. {
  358. struct st_i2c_client *c = &i2c_dev->client;
  359. u32 tx_fstat, sta;
  360. int i;
  361. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  362. if (sta & SSC_STA_TX_FIFO_FULL)
  363. return;
  364. tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
  365. tx_fstat &= SSC_TX_FSTAT_STATUS;
  366. if (c->count < (SSC_TXFIFO_SIZE - tx_fstat))
  367. i = c->count;
  368. else
  369. i = SSC_TXFIFO_SIZE - tx_fstat;
  370. for (; i > 0; i--, c->count--, c->buf++)
  371. st_i2c_write_tx_fifo(i2c_dev, *c->buf);
  372. }
  373. /**
  374. * st_i2c_rd_fill_tx_fifo() - Fill the Tx FIFO in read mode
  375. * @i2c_dev: Controller's private data
  376. *
  377. * This functions fills the Tx FIFO with fixed pattern when
  378. * in read mode to trigger clock.
  379. */
  380. static void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max)
  381. {
  382. struct st_i2c_client *c = &i2c_dev->client;
  383. u32 tx_fstat, sta;
  384. int i;
  385. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  386. if (sta & SSC_STA_TX_FIFO_FULL)
  387. return;
  388. tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT);
  389. tx_fstat &= SSC_TX_FSTAT_STATUS;
  390. if (max < (SSC_TXFIFO_SIZE - tx_fstat))
  391. i = max;
  392. else
  393. i = SSC_TXFIFO_SIZE - tx_fstat;
  394. for (; i > 0; i--, c->xfered++)
  395. st_i2c_write_tx_fifo(i2c_dev, 0xff);
  396. }
  397. static void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev)
  398. {
  399. struct st_i2c_client *c = &i2c_dev->client;
  400. u32 i, sta;
  401. u16 rbuf;
  402. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  403. if (sta & SSC_STA_RIR) {
  404. i = SSC_RXFIFO_SIZE;
  405. } else {
  406. i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT);
  407. i &= SSC_RX_FSTAT_STATUS;
  408. }
  409. for (; (i > 0) && (c->count > 0); i--, c->count--) {
  410. rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1;
  411. *c->buf++ = (u8)rbuf & 0xff;
  412. }
  413. if (i) {
  414. dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i);
  415. st_i2c_flush_rx_fifo(i2c_dev);
  416. }
  417. }
  418. /**
  419. * st_i2c_terminate_xfer() - Send either STOP or REPSTART condition
  420. * @i2c_dev: Controller's private data
  421. */
  422. static void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev)
  423. {
  424. struct st_i2c_client *c = &i2c_dev->client;
  425. st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN);
  426. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
  427. if (c->stop) {
  428. st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN);
  429. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  430. } else {
  431. st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN);
  432. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG);
  433. }
  434. }
  435. /**
  436. * st_i2c_handle_write() - Handle FIFO empty interrupt in case of write
  437. * @i2c_dev: Controller's private data
  438. */
  439. static void st_i2c_handle_write(struct st_i2c_dev *i2c_dev)
  440. {
  441. struct st_i2c_client *c = &i2c_dev->client;
  442. st_i2c_flush_rx_fifo(i2c_dev);
  443. if (!c->count)
  444. /* End of xfer, send stop or repstart */
  445. st_i2c_terminate_xfer(i2c_dev);
  446. else
  447. st_i2c_wr_fill_tx_fifo(i2c_dev);
  448. }
  449. /**
  450. * st_i2c_handle_write() - Handle FIFO enmpty interrupt in case of read
  451. * @i2c_dev: Controller's private data
  452. */
  453. static void st_i2c_handle_read(struct st_i2c_dev *i2c_dev)
  454. {
  455. struct st_i2c_client *c = &i2c_dev->client;
  456. u32 ien;
  457. /* Trash the address read back */
  458. if (!c->xfered) {
  459. readl_relaxed(i2c_dev->base + SSC_RBUF);
  460. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB);
  461. } else {
  462. st_i2c_read_rx_fifo(i2c_dev);
  463. }
  464. if (!c->count) {
  465. /* End of xfer, send stop or repstart */
  466. st_i2c_terminate_xfer(i2c_dev);
  467. } else if (c->count == 1) {
  468. /* Penultimate byte to xfer, disable ACK gen. */
  469. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG);
  470. /* Last received byte is to be handled by NACK interrupt */
  471. ien = SSC_IEN_NACKEN | SSC_IEN_ARBLEN;
  472. writel_relaxed(ien, i2c_dev->base + SSC_IEN);
  473. st_i2c_rd_fill_tx_fifo(i2c_dev, c->count);
  474. } else {
  475. st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1);
  476. }
  477. }
  478. /**
  479. * st_i2c_isr() - Interrupt routine
  480. * @irq: interrupt number
  481. * @data: Controller's private data
  482. */
  483. static irqreturn_t st_i2c_isr_thread(int irq, void *data)
  484. {
  485. struct st_i2c_dev *i2c_dev = data;
  486. struct st_i2c_client *c = &i2c_dev->client;
  487. u32 sta, ien;
  488. int it;
  489. ien = readl_relaxed(i2c_dev->base + SSC_IEN);
  490. sta = readl_relaxed(i2c_dev->base + SSC_STA);
  491. /* Use __fls() to check error bits first */
  492. it = __fls(sta & ien);
  493. if (it < 0) {
  494. dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n",
  495. sta, ien);
  496. return IRQ_NONE;
  497. }
  498. switch (1 << it) {
  499. case SSC_STA_TE:
  500. if (c->addr & I2C_M_RD)
  501. st_i2c_handle_read(i2c_dev);
  502. else
  503. st_i2c_handle_write(i2c_dev);
  504. break;
  505. case SSC_STA_STOP:
  506. case SSC_STA_REPSTRT:
  507. writel_relaxed(0, i2c_dev->base + SSC_IEN);
  508. complete(&i2c_dev->complete);
  509. break;
  510. case SSC_STA_NACK:
  511. writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR);
  512. /* Last received byte handled by NACK interrupt */
  513. if ((c->addr & I2C_M_RD) && (c->count == 1) && (c->xfered)) {
  514. st_i2c_handle_read(i2c_dev);
  515. break;
  516. }
  517. it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
  518. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  519. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  520. c->result = -EIO;
  521. break;
  522. case SSC_STA_ARBL:
  523. writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR);
  524. it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN;
  525. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  526. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG);
  527. c->result = -EAGAIN;
  528. break;
  529. default:
  530. dev_err(i2c_dev->dev,
  531. "it %d unhandled (sta=0x%04x)\n", it, sta);
  532. }
  533. /*
  534. * Read IEN register to ensure interrupt mask write is effective
  535. * before re-enabling interrupt at GIC level, and thus avoid spurious
  536. * interrupts.
  537. */
  538. readl(i2c_dev->base + SSC_IEN);
  539. return IRQ_HANDLED;
  540. }
  541. /**
  542. * st_i2c_xfer_msg() - Transfer a single I2C message
  543. * @i2c_dev: Controller's private data
  544. * @msg: I2C message to transfer
  545. * @is_first: first message of the sequence
  546. * @is_last: last message of the sequence
  547. */
  548. static int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg,
  549. bool is_first, bool is_last)
  550. {
  551. struct st_i2c_client *c = &i2c_dev->client;
  552. u32 ctl, i2c, it;
  553. unsigned long timeout;
  554. int ret;
  555. c->addr = i2c_8bit_addr_from_msg(msg);
  556. c->buf = msg->buf;
  557. c->count = msg->len;
  558. c->xfered = 0;
  559. c->result = 0;
  560. c->stop = is_last;
  561. reinit_completion(&i2c_dev->complete);
  562. ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO;
  563. st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl);
  564. i2c = SSC_I2C_TXENB;
  565. if (c->addr & I2C_M_RD)
  566. i2c |= SSC_I2C_ACKG;
  567. st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c);
  568. /* Write slave address */
  569. st_i2c_write_tx_fifo(i2c_dev, c->addr);
  570. /* Pre-fill Tx fifo with data in case of write */
  571. if (!(c->addr & I2C_M_RD))
  572. st_i2c_wr_fill_tx_fifo(i2c_dev);
  573. it = SSC_IEN_NACKEN | SSC_IEN_TEEN | SSC_IEN_ARBLEN;
  574. writel_relaxed(it, i2c_dev->base + SSC_IEN);
  575. if (is_first) {
  576. ret = st_i2c_wait_free_bus(i2c_dev);
  577. if (ret)
  578. return ret;
  579. st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG);
  580. }
  581. timeout = wait_for_completion_timeout(&i2c_dev->complete,
  582. i2c_dev->adap.timeout);
  583. ret = c->result;
  584. if (!timeout) {
  585. dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n",
  586. c->addr);
  587. ret = -ETIMEDOUT;
  588. }
  589. i2c = SSC_I2C_STOPG | SSC_I2C_REPSTRTG;
  590. st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c);
  591. writel_relaxed(SSC_CLR_SSCSTOP | SSC_CLR_REPSTRT,
  592. i2c_dev->base + SSC_CLR);
  593. return ret;
  594. }
  595. /**
  596. * st_i2c_xfer() - Transfer a single I2C message
  597. * @i2c_adap: Adapter pointer to the controller
  598. * @msgs: Pointer to data to be written.
  599. * @num: Number of messages to be executed
  600. */
  601. static int st_i2c_xfer(struct i2c_adapter *i2c_adap,
  602. struct i2c_msg msgs[], int num)
  603. {
  604. struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
  605. int ret, i;
  606. i2c_dev->busy = true;
  607. ret = clk_prepare_enable(i2c_dev->clk);
  608. if (ret) {
  609. dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
  610. return ret;
  611. }
  612. pinctrl_pm_select_default_state(i2c_dev->dev);
  613. st_i2c_hw_config(i2c_dev);
  614. for (i = 0; (i < num) && !ret; i++)
  615. ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1);
  616. pinctrl_pm_select_idle_state(i2c_dev->dev);
  617. clk_disable_unprepare(i2c_dev->clk);
  618. i2c_dev->busy = false;
  619. return (ret < 0) ? ret : i;
  620. }
  621. #ifdef CONFIG_PM_SLEEP
  622. static int st_i2c_suspend(struct device *dev)
  623. {
  624. struct platform_device *pdev = to_platform_device(dev);
  625. struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  626. if (i2c_dev->busy)
  627. return -EBUSY;
  628. pinctrl_pm_select_sleep_state(dev);
  629. return 0;
  630. }
  631. static int st_i2c_resume(struct device *dev)
  632. {
  633. pinctrl_pm_select_default_state(dev);
  634. /* Go in idle state if available */
  635. pinctrl_pm_select_idle_state(dev);
  636. return 0;
  637. }
  638. static SIMPLE_DEV_PM_OPS(st_i2c_pm, st_i2c_suspend, st_i2c_resume);
  639. #define ST_I2C_PM (&st_i2c_pm)
  640. #else
  641. #define ST_I2C_PM NULL
  642. #endif
  643. static u32 st_i2c_func(struct i2c_adapter *adap)
  644. {
  645. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  646. }
  647. static struct i2c_algorithm st_i2c_algo = {
  648. .master_xfer = st_i2c_xfer,
  649. .functionality = st_i2c_func,
  650. };
  651. static struct i2c_bus_recovery_info st_i2c_recovery_info = {
  652. .recover_bus = st_i2c_recover_bus,
  653. };
  654. static int st_i2c_of_get_deglitch(struct device_node *np,
  655. struct st_i2c_dev *i2c_dev)
  656. {
  657. int ret;
  658. ret = of_property_read_u32(np, "st,i2c-min-scl-pulse-width-us",
  659. &i2c_dev->scl_min_width_us);
  660. if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
  661. dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n");
  662. return ret;
  663. }
  664. ret = of_property_read_u32(np, "st,i2c-min-sda-pulse-width-us",
  665. &i2c_dev->sda_min_width_us);
  666. if ((ret == -ENODATA) || (ret == -EOVERFLOW)) {
  667. dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n");
  668. return ret;
  669. }
  670. return 0;
  671. }
  672. static int st_i2c_probe(struct platform_device *pdev)
  673. {
  674. struct device_node *np = pdev->dev.of_node;
  675. struct st_i2c_dev *i2c_dev;
  676. struct resource *res;
  677. u32 clk_rate;
  678. struct i2c_adapter *adap;
  679. int ret;
  680. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  681. if (!i2c_dev)
  682. return -ENOMEM;
  683. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  684. i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
  685. if (IS_ERR(i2c_dev->base))
  686. return PTR_ERR(i2c_dev->base);
  687. i2c_dev->irq = irq_of_parse_and_map(np, 0);
  688. if (!i2c_dev->irq) {
  689. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  690. return -EINVAL;
  691. }
  692. i2c_dev->clk = of_clk_get_by_name(np, "ssc");
  693. if (IS_ERR(i2c_dev->clk)) {
  694. dev_err(&pdev->dev, "Unable to request clock\n");
  695. return PTR_ERR(i2c_dev->clk);
  696. }
  697. i2c_dev->mode = I2C_MODE_STANDARD;
  698. ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
  699. if ((!ret) && (clk_rate == 400000))
  700. i2c_dev->mode = I2C_MODE_FAST;
  701. i2c_dev->dev = &pdev->dev;
  702. ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq,
  703. NULL, st_i2c_isr_thread,
  704. IRQF_ONESHOT, pdev->name, i2c_dev);
  705. if (ret) {
  706. dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
  707. return ret;
  708. }
  709. pinctrl_pm_select_default_state(i2c_dev->dev);
  710. /* In case idle state available, select it */
  711. pinctrl_pm_select_idle_state(i2c_dev->dev);
  712. ret = st_i2c_of_get_deglitch(np, i2c_dev);
  713. if (ret)
  714. return ret;
  715. adap = &i2c_dev->adap;
  716. i2c_set_adapdata(adap, i2c_dev);
  717. snprintf(adap->name, sizeof(adap->name), "ST I2C(%pa)", &res->start);
  718. adap->owner = THIS_MODULE;
  719. adap->timeout = 2 * HZ;
  720. adap->retries = 0;
  721. adap->algo = &st_i2c_algo;
  722. adap->bus_recovery_info = &st_i2c_recovery_info;
  723. adap->dev.parent = &pdev->dev;
  724. adap->dev.of_node = pdev->dev.of_node;
  725. init_completion(&i2c_dev->complete);
  726. ret = i2c_add_adapter(adap);
  727. if (ret)
  728. return ret;
  729. platform_set_drvdata(pdev, i2c_dev);
  730. dev_info(i2c_dev->dev, "%s initialized\n", adap->name);
  731. return 0;
  732. }
  733. static int st_i2c_remove(struct platform_device *pdev)
  734. {
  735. struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  736. i2c_del_adapter(&i2c_dev->adap);
  737. return 0;
  738. }
  739. static const struct of_device_id st_i2c_match[] = {
  740. { .compatible = "st,comms-ssc-i2c", },
  741. { .compatible = "st,comms-ssc4-i2c", },
  742. {},
  743. };
  744. MODULE_DEVICE_TABLE(of, st_i2c_match);
  745. static struct platform_driver st_i2c_driver = {
  746. .driver = {
  747. .name = "st-i2c",
  748. .of_match_table = st_i2c_match,
  749. .pm = ST_I2C_PM,
  750. },
  751. .probe = st_i2c_probe,
  752. .remove = st_i2c_remove,
  753. };
  754. module_platform_driver(st_i2c_driver);
  755. MODULE_AUTHOR("Maxime Coquelin <maxime.coquelin@st.com>");
  756. MODULE_DESCRIPTION("STMicroelectronics I2C driver");
  757. MODULE_LICENSE("GPL v2");