i2c-stm32f4.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Driver for STMicroelectronics STM32 I2C controller
  3. *
  4. * This I2C controller is described in the STM32F429/439 Soc reference manual.
  5. * Please see below a link to the documentation:
  6. * http://www.st.com/resource/en/reference_manual/DM00031020.pdf
  7. *
  8. * Copyright (C) M'boumba Cedric Madianga 2016
  9. * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
  10. *
  11. * This driver is based on i2c-st.c
  12. *
  13. * License terms: GNU General Public License (GPL), version 2
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/module.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/reset.h>
  28. /* STM32F4 I2C offset registers */
  29. #define STM32F4_I2C_CR1 0x00
  30. #define STM32F4_I2C_CR2 0x04
  31. #define STM32F4_I2C_DR 0x10
  32. #define STM32F4_I2C_SR1 0x14
  33. #define STM32F4_I2C_SR2 0x18
  34. #define STM32F4_I2C_CCR 0x1C
  35. #define STM32F4_I2C_TRISE 0x20
  36. #define STM32F4_I2C_FLTR 0x24
  37. /* STM32F4 I2C control 1*/
  38. #define STM32F4_I2C_CR1_POS BIT(11)
  39. #define STM32F4_I2C_CR1_ACK BIT(10)
  40. #define STM32F4_I2C_CR1_STOP BIT(9)
  41. #define STM32F4_I2C_CR1_START BIT(8)
  42. #define STM32F4_I2C_CR1_PE BIT(0)
  43. /* STM32F4 I2C control 2 */
  44. #define STM32F4_I2C_CR2_FREQ_MASK GENMASK(5, 0)
  45. #define STM32F4_I2C_CR2_FREQ(n) ((n) & STM32F4_I2C_CR2_FREQ_MASK)
  46. #define STM32F4_I2C_CR2_ITBUFEN BIT(10)
  47. #define STM32F4_I2C_CR2_ITEVTEN BIT(9)
  48. #define STM32F4_I2C_CR2_ITERREN BIT(8)
  49. #define STM32F4_I2C_CR2_IRQ_MASK (STM32F4_I2C_CR2_ITBUFEN | \
  50. STM32F4_I2C_CR2_ITEVTEN | \
  51. STM32F4_I2C_CR2_ITERREN)
  52. /* STM32F4 I2C Status 1 */
  53. #define STM32F4_I2C_SR1_AF BIT(10)
  54. #define STM32F4_I2C_SR1_ARLO BIT(9)
  55. #define STM32F4_I2C_SR1_BERR BIT(8)
  56. #define STM32F4_I2C_SR1_TXE BIT(7)
  57. #define STM32F4_I2C_SR1_RXNE BIT(6)
  58. #define STM32F4_I2C_SR1_BTF BIT(2)
  59. #define STM32F4_I2C_SR1_ADDR BIT(1)
  60. #define STM32F4_I2C_SR1_SB BIT(0)
  61. #define STM32F4_I2C_SR1_ITEVTEN_MASK (STM32F4_I2C_SR1_BTF | \
  62. STM32F4_I2C_SR1_ADDR | \
  63. STM32F4_I2C_SR1_SB)
  64. #define STM32F4_I2C_SR1_ITBUFEN_MASK (STM32F4_I2C_SR1_TXE | \
  65. STM32F4_I2C_SR1_RXNE)
  66. #define STM32F4_I2C_SR1_ITERREN_MASK (STM32F4_I2C_SR1_AF | \
  67. STM32F4_I2C_SR1_ARLO | \
  68. STM32F4_I2C_SR1_BERR)
  69. /* STM32F4 I2C Status 2 */
  70. #define STM32F4_I2C_SR2_BUSY BIT(1)
  71. /* STM32F4 I2C Control Clock */
  72. #define STM32F4_I2C_CCR_CCR_MASK GENMASK(11, 0)
  73. #define STM32F4_I2C_CCR_CCR(n) ((n) & STM32F4_I2C_CCR_CCR_MASK)
  74. #define STM32F4_I2C_CCR_FS BIT(15)
  75. #define STM32F4_I2C_CCR_DUTY BIT(14)
  76. /* STM32F4 I2C Trise */
  77. #define STM32F4_I2C_TRISE_VALUE_MASK GENMASK(5, 0)
  78. #define STM32F4_I2C_TRISE_VALUE(n) ((n) & STM32F4_I2C_TRISE_VALUE_MASK)
  79. #define STM32F4_I2C_MIN_STANDARD_FREQ 2U
  80. #define STM32F4_I2C_MIN_FAST_FREQ 6U
  81. #define STM32F4_I2C_MAX_FREQ 46U
  82. #define HZ_TO_MHZ 1000000
  83. enum stm32f4_i2c_speed {
  84. STM32F4_I2C_SPEED_STANDARD, /* 100 kHz */
  85. STM32F4_I2C_SPEED_FAST, /* 400 kHz */
  86. STM32F4_I2C_SPEED_END,
  87. };
  88. /**
  89. * struct stm32f4_i2c_msg - client specific data
  90. * @addr: 8-bit slave addr, including r/w bit
  91. * @count: number of bytes to be transferred
  92. * @buf: data buffer
  93. * @result: result of the transfer
  94. * @stop: last I2C msg to be sent, i.e. STOP to be generated
  95. */
  96. struct stm32f4_i2c_msg {
  97. u8 addr;
  98. u32 count;
  99. u8 *buf;
  100. int result;
  101. bool stop;
  102. };
  103. /**
  104. * struct stm32f4_i2c_dev - private data of the controller
  105. * @adap: I2C adapter for this controller
  106. * @dev: device for this controller
  107. * @base: virtual memory area
  108. * @complete: completion of I2C message
  109. * @clk: hw i2c clock
  110. * @speed: I2C clock frequency of the controller. Standard or Fast are supported
  111. * @parent_rate: I2C clock parent rate in MHz
  112. * @msg: I2C transfer information
  113. */
  114. struct stm32f4_i2c_dev {
  115. struct i2c_adapter adap;
  116. struct device *dev;
  117. void __iomem *base;
  118. struct completion complete;
  119. struct clk *clk;
  120. int speed;
  121. int parent_rate;
  122. struct stm32f4_i2c_msg msg;
  123. };
  124. static inline void stm32f4_i2c_set_bits(void __iomem *reg, u32 mask)
  125. {
  126. writel_relaxed(readl_relaxed(reg) | mask, reg);
  127. }
  128. static inline void stm32f4_i2c_clr_bits(void __iomem *reg, u32 mask)
  129. {
  130. writel_relaxed(readl_relaxed(reg) & ~mask, reg);
  131. }
  132. static void stm32f4_i2c_disable_irq(struct stm32f4_i2c_dev *i2c_dev)
  133. {
  134. void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
  135. stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_IRQ_MASK);
  136. }
  137. static int stm32f4_i2c_set_periph_clk_freq(struct stm32f4_i2c_dev *i2c_dev)
  138. {
  139. u32 freq;
  140. u32 cr2 = 0;
  141. i2c_dev->parent_rate = clk_get_rate(i2c_dev->clk);
  142. freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ);
  143. if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) {
  144. /*
  145. * To reach 100 kHz, the parent clk frequency should be between
  146. * a minimum value of 2 MHz and a maximum value of 46 MHz due
  147. * to hardware limitation
  148. */
  149. if (freq < STM32F4_I2C_MIN_STANDARD_FREQ ||
  150. freq > STM32F4_I2C_MAX_FREQ) {
  151. dev_err(i2c_dev->dev,
  152. "bad parent clk freq for standard mode\n");
  153. return -EINVAL;
  154. }
  155. } else {
  156. /*
  157. * To be as close as possible to 400 kHz, the parent clk
  158. * frequency should be between a minimum value of 6 MHz and a
  159. * maximum value of 46 MHz due to hardware limitation
  160. */
  161. if (freq < STM32F4_I2C_MIN_FAST_FREQ ||
  162. freq > STM32F4_I2C_MAX_FREQ) {
  163. dev_err(i2c_dev->dev,
  164. "bad parent clk freq for fast mode\n");
  165. return -EINVAL;
  166. }
  167. }
  168. cr2 |= STM32F4_I2C_CR2_FREQ(freq);
  169. writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2);
  170. return 0;
  171. }
  172. static void stm32f4_i2c_set_rise_time(struct stm32f4_i2c_dev *i2c_dev)
  173. {
  174. u32 freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ);
  175. u32 trise;
  176. /*
  177. * These bits must be programmed with the maximum SCL rise time given in
  178. * the I2C bus specification, incremented by 1.
  179. *
  180. * In standard mode, the maximum allowed SCL rise time is 1000 ns.
  181. * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to
  182. * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be
  183. * programmed with 0x9. (1000 ns / 125 ns + 1)
  184. * So, for I2C standard mode TRISE = FREQ[5:0] + 1
  185. *
  186. * In fast mode, the maximum allowed SCL rise time is 300 ns.
  187. * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to
  188. * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be
  189. * programmed with 0x3. (300 ns / 125 ns + 1)
  190. * So, for I2C fast mode TRISE = FREQ[5:0] * 300 / 1000 + 1
  191. *
  192. * Function stm32f4_i2c_set_periph_clk_freq made sure that parent rate
  193. * is not higher than 46 MHz . As a result trise is at most 4 bits wide
  194. * and so fits into the TRISE bits [5:0].
  195. */
  196. if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD)
  197. trise = freq + 1;
  198. else
  199. trise = freq * 3 / 10 + 1;
  200. writel_relaxed(STM32F4_I2C_TRISE_VALUE(trise),
  201. i2c_dev->base + STM32F4_I2C_TRISE);
  202. }
  203. static void stm32f4_i2c_set_speed_mode(struct stm32f4_i2c_dev *i2c_dev)
  204. {
  205. u32 val;
  206. u32 ccr = 0;
  207. if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) {
  208. /*
  209. * In standard mode:
  210. * t_scl_high = t_scl_low = CCR * I2C parent clk period
  211. * So to reach 100 kHz, we have:
  212. * CCR = I2C parent rate / 100 kHz >> 1
  213. *
  214. * For example with parent rate = 2 MHz:
  215. * CCR = 2000000 / (100000 << 1) = 10
  216. * t_scl_high = t_scl_low = 10 * (1 / 2000000) = 5000 ns
  217. * t_scl_high + t_scl_low = 10000 ns so 100 kHz is reached
  218. *
  219. * Function stm32f4_i2c_set_periph_clk_freq made sure that
  220. * parent rate is not higher than 46 MHz . As a result val
  221. * is at most 8 bits wide and so fits into the CCR bits [11:0].
  222. */
  223. val = i2c_dev->parent_rate / (100000 << 1);
  224. } else {
  225. /*
  226. * In fast mode, we compute CCR with duty = 0 as with low
  227. * frequencies we are not able to reach 400 kHz.
  228. * In that case:
  229. * t_scl_high = CCR * I2C parent clk period
  230. * t_scl_low = 2 * CCR * I2C parent clk period
  231. * So, CCR = I2C parent rate / (400 kHz * 3)
  232. *
  233. * For example with parent rate = 6 MHz:
  234. * CCR = 6000000 / (400000 * 3) = 5
  235. * t_scl_high = 5 * (1 / 6000000) = 833 ns > 600 ns
  236. * t_scl_low = 2 * 5 * (1 / 6000000) = 1667 ns > 1300 ns
  237. * t_scl_high + t_scl_low = 2500 ns so 400 kHz is reached
  238. *
  239. * Function stm32f4_i2c_set_periph_clk_freq made sure that
  240. * parent rate is not higher than 46 MHz . As a result val
  241. * is at most 6 bits wide and so fits into the CCR bits [11:0].
  242. */
  243. val = DIV_ROUND_UP(i2c_dev->parent_rate, 400000 * 3);
  244. /* Select Fast mode */
  245. ccr |= STM32F4_I2C_CCR_FS;
  246. }
  247. ccr |= STM32F4_I2C_CCR_CCR(val);
  248. writel_relaxed(ccr, i2c_dev->base + STM32F4_I2C_CCR);
  249. }
  250. /**
  251. * stm32f4_i2c_hw_config() - Prepare I2C block
  252. * @i2c_dev: Controller's private data
  253. */
  254. static int stm32f4_i2c_hw_config(struct stm32f4_i2c_dev *i2c_dev)
  255. {
  256. int ret;
  257. ret = stm32f4_i2c_set_periph_clk_freq(i2c_dev);
  258. if (ret)
  259. return ret;
  260. stm32f4_i2c_set_rise_time(i2c_dev);
  261. stm32f4_i2c_set_speed_mode(i2c_dev);
  262. /* Enable I2C */
  263. writel_relaxed(STM32F4_I2C_CR1_PE, i2c_dev->base + STM32F4_I2C_CR1);
  264. return 0;
  265. }
  266. static int stm32f4_i2c_wait_free_bus(struct stm32f4_i2c_dev *i2c_dev)
  267. {
  268. u32 status;
  269. int ret;
  270. ret = readl_relaxed_poll_timeout(i2c_dev->base + STM32F4_I2C_SR2,
  271. status,
  272. !(status & STM32F4_I2C_SR2_BUSY),
  273. 10, 1000);
  274. if (ret) {
  275. dev_dbg(i2c_dev->dev, "bus not free\n");
  276. ret = -EBUSY;
  277. }
  278. return ret;
  279. }
  280. /**
  281. * stm32f4_i2c_write_ byte() - Write a byte in the data register
  282. * @i2c_dev: Controller's private data
  283. * @byte: Data to write in the register
  284. */
  285. static void stm32f4_i2c_write_byte(struct stm32f4_i2c_dev *i2c_dev, u8 byte)
  286. {
  287. writel_relaxed(byte, i2c_dev->base + STM32F4_I2C_DR);
  288. }
  289. /**
  290. * stm32f4_i2c_write_msg() - Fill the data register in write mode
  291. * @i2c_dev: Controller's private data
  292. *
  293. * This function fills the data register with I2C transfer buffer
  294. */
  295. static void stm32f4_i2c_write_msg(struct stm32f4_i2c_dev *i2c_dev)
  296. {
  297. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  298. stm32f4_i2c_write_byte(i2c_dev, *msg->buf++);
  299. msg->count--;
  300. }
  301. static void stm32f4_i2c_read_msg(struct stm32f4_i2c_dev *i2c_dev)
  302. {
  303. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  304. u32 rbuf;
  305. rbuf = readl_relaxed(i2c_dev->base + STM32F4_I2C_DR);
  306. *msg->buf++ = rbuf;
  307. msg->count--;
  308. }
  309. static void stm32f4_i2c_terminate_xfer(struct stm32f4_i2c_dev *i2c_dev)
  310. {
  311. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  312. void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
  313. stm32f4_i2c_disable_irq(i2c_dev);
  314. reg = i2c_dev->base + STM32F4_I2C_CR1;
  315. if (msg->stop)
  316. stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
  317. else
  318. stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
  319. complete(&i2c_dev->complete);
  320. }
  321. /**
  322. * stm32f4_i2c_handle_write() - Handle FIFO empty interrupt in case of write
  323. * @i2c_dev: Controller's private data
  324. */
  325. static void stm32f4_i2c_handle_write(struct stm32f4_i2c_dev *i2c_dev)
  326. {
  327. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  328. void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
  329. if (msg->count) {
  330. stm32f4_i2c_write_msg(i2c_dev);
  331. if (!msg->count) {
  332. /*
  333. * Disable buffer interrupts for RX not empty and TX
  334. * empty events
  335. */
  336. stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN);
  337. }
  338. } else {
  339. stm32f4_i2c_terminate_xfer(i2c_dev);
  340. }
  341. }
  342. /**
  343. * stm32f4_i2c_handle_read() - Handle FIFO empty interrupt in case of read
  344. * @i2c_dev: Controller's private data
  345. *
  346. * This function is called when a new data is received in data register
  347. */
  348. static void stm32f4_i2c_handle_read(struct stm32f4_i2c_dev *i2c_dev)
  349. {
  350. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  351. void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
  352. switch (msg->count) {
  353. case 1:
  354. stm32f4_i2c_disable_irq(i2c_dev);
  355. stm32f4_i2c_read_msg(i2c_dev);
  356. complete(&i2c_dev->complete);
  357. break;
  358. /*
  359. * For 2-byte reception, 3-byte reception and for Data N-2, N-1 and N
  360. * for N-byte reception with N > 3, we do not have to read the data
  361. * register when RX not empty event occurs as we have to wait for byte
  362. * transferred finished event before reading data.
  363. * So, here we just disable buffer interrupt in order to avoid another
  364. * system preemption due to RX not empty event.
  365. */
  366. case 2:
  367. case 3:
  368. stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN);
  369. break;
  370. /*
  371. * For N byte reception with N > 3 we directly read data register
  372. * until N-2 data.
  373. */
  374. default:
  375. stm32f4_i2c_read_msg(i2c_dev);
  376. }
  377. }
  378. /**
  379. * stm32f4_i2c_handle_rx_done() - Handle byte transfer finished interrupt
  380. * in case of read
  381. * @i2c_dev: Controller's private data
  382. *
  383. * This function is called when a new data is received in the shift register
  384. * but data register has not been read yet.
  385. */
  386. static void stm32f4_i2c_handle_rx_done(struct stm32f4_i2c_dev *i2c_dev)
  387. {
  388. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  389. void __iomem *reg;
  390. u32 mask;
  391. int i;
  392. switch (msg->count) {
  393. case 2:
  394. /*
  395. * In order to correctly send the Stop or Repeated Start
  396. * condition on the I2C bus, the STOP/START bit has to be set
  397. * before reading the last two bytes (data N-1 and N).
  398. * After that, we could read the last two bytes, disable
  399. * remaining interrupts and notify the end of xfer to the
  400. * client
  401. */
  402. reg = i2c_dev->base + STM32F4_I2C_CR1;
  403. if (msg->stop)
  404. stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
  405. else
  406. stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
  407. for (i = 2; i > 0; i--)
  408. stm32f4_i2c_read_msg(i2c_dev);
  409. reg = i2c_dev->base + STM32F4_I2C_CR2;
  410. mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN;
  411. stm32f4_i2c_clr_bits(reg, mask);
  412. complete(&i2c_dev->complete);
  413. break;
  414. case 3:
  415. /*
  416. * In order to correctly generate the NACK pulse after the last
  417. * received data byte, we have to enable NACK before reading N-2
  418. * data
  419. */
  420. reg = i2c_dev->base + STM32F4_I2C_CR1;
  421. stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR1_ACK);
  422. stm32f4_i2c_read_msg(i2c_dev);
  423. break;
  424. default:
  425. stm32f4_i2c_read_msg(i2c_dev);
  426. }
  427. }
  428. /**
  429. * stm32f4_i2c_handle_rx_addr() - Handle address matched interrupt in case of
  430. * master receiver
  431. * @i2c_dev: Controller's private data
  432. */
  433. static void stm32f4_i2c_handle_rx_addr(struct stm32f4_i2c_dev *i2c_dev)
  434. {
  435. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  436. u32 cr1;
  437. switch (msg->count) {
  438. case 0:
  439. stm32f4_i2c_terminate_xfer(i2c_dev);
  440. /* Clear ADDR flag */
  441. readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
  442. break;
  443. case 1:
  444. /*
  445. * Single byte reception:
  446. * Enable NACK and reset POS (Acknowledge position).
  447. * Then, clear ADDR flag and set STOP or RepSTART.
  448. * In that way, the NACK and STOP or RepStart pulses will be
  449. * sent as soon as the byte will be received in shift register
  450. */
  451. cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
  452. cr1 &= ~(STM32F4_I2C_CR1_ACK | STM32F4_I2C_CR1_POS);
  453. writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
  454. readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
  455. if (msg->stop)
  456. cr1 |= STM32F4_I2C_CR1_STOP;
  457. else
  458. cr1 |= STM32F4_I2C_CR1_START;
  459. writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
  460. break;
  461. case 2:
  462. /*
  463. * 2-byte reception:
  464. * Enable NACK, set POS (NACK position) and clear ADDR flag.
  465. * In that way, NACK will be sent for the next byte which will
  466. * be received in the shift register instead of the current
  467. * one.
  468. */
  469. cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
  470. cr1 &= ~STM32F4_I2C_CR1_ACK;
  471. cr1 |= STM32F4_I2C_CR1_POS;
  472. writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
  473. readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
  474. break;
  475. default:
  476. /*
  477. * N-byte reception:
  478. * Enable ACK, reset POS (ACK postion) and clear ADDR flag.
  479. * In that way, ACK will be sent as soon as the current byte
  480. * will be received in the shift register
  481. */
  482. cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
  483. cr1 |= STM32F4_I2C_CR1_ACK;
  484. cr1 &= ~STM32F4_I2C_CR1_POS;
  485. writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
  486. readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
  487. break;
  488. }
  489. }
  490. /**
  491. * stm32f4_i2c_isr_event() - Interrupt routine for I2C bus event
  492. * @irq: interrupt number
  493. * @data: Controller's private data
  494. */
  495. static irqreturn_t stm32f4_i2c_isr_event(int irq, void *data)
  496. {
  497. struct stm32f4_i2c_dev *i2c_dev = data;
  498. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  499. u32 possible_status = STM32F4_I2C_SR1_ITEVTEN_MASK;
  500. u32 status, ien, event, cr2;
  501. cr2 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR2);
  502. ien = cr2 & STM32F4_I2C_CR2_IRQ_MASK;
  503. /* Update possible_status if buffer interrupt is enabled */
  504. if (ien & STM32F4_I2C_CR2_ITBUFEN)
  505. possible_status |= STM32F4_I2C_SR1_ITBUFEN_MASK;
  506. status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1);
  507. event = status & possible_status;
  508. if (!event) {
  509. dev_dbg(i2c_dev->dev,
  510. "spurious evt irq (status=0x%08x, ien=0x%08x)\n",
  511. status, ien);
  512. return IRQ_NONE;
  513. }
  514. /* Start condition generated */
  515. if (event & STM32F4_I2C_SR1_SB)
  516. stm32f4_i2c_write_byte(i2c_dev, msg->addr);
  517. /* I2C Address sent */
  518. if (event & STM32F4_I2C_SR1_ADDR) {
  519. if (msg->addr & I2C_M_RD)
  520. stm32f4_i2c_handle_rx_addr(i2c_dev);
  521. else
  522. readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
  523. /*
  524. * Enable buffer interrupts for RX not empty and TX empty
  525. * events
  526. */
  527. cr2 |= STM32F4_I2C_CR2_ITBUFEN;
  528. writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2);
  529. }
  530. /* TX empty */
  531. if ((event & STM32F4_I2C_SR1_TXE) && !(msg->addr & I2C_M_RD))
  532. stm32f4_i2c_handle_write(i2c_dev);
  533. /* RX not empty */
  534. if ((event & STM32F4_I2C_SR1_RXNE) && (msg->addr & I2C_M_RD))
  535. stm32f4_i2c_handle_read(i2c_dev);
  536. /*
  537. * The BTF (Byte Transfer finished) event occurs when:
  538. * - in reception : a new byte is received in the shift register
  539. * but the previous byte has not been read yet from data register
  540. * - in transmission: a new byte should be sent but the data register
  541. * has not been written yet
  542. */
  543. if (event & STM32F4_I2C_SR1_BTF) {
  544. if (msg->addr & I2C_M_RD)
  545. stm32f4_i2c_handle_rx_done(i2c_dev);
  546. else
  547. stm32f4_i2c_handle_write(i2c_dev);
  548. }
  549. return IRQ_HANDLED;
  550. }
  551. /**
  552. * stm32f4_i2c_isr_error() - Interrupt routine for I2C bus error
  553. * @irq: interrupt number
  554. * @data: Controller's private data
  555. */
  556. static irqreturn_t stm32f4_i2c_isr_error(int irq, void *data)
  557. {
  558. struct stm32f4_i2c_dev *i2c_dev = data;
  559. struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
  560. void __iomem *reg;
  561. u32 status;
  562. status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1);
  563. /* Arbitration lost */
  564. if (status & STM32F4_I2C_SR1_ARLO) {
  565. status &= ~STM32F4_I2C_SR1_ARLO;
  566. writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
  567. msg->result = -EAGAIN;
  568. }
  569. /*
  570. * Acknowledge failure:
  571. * In master transmitter mode a Stop must be generated by software
  572. */
  573. if (status & STM32F4_I2C_SR1_AF) {
  574. if (!(msg->addr & I2C_M_RD)) {
  575. reg = i2c_dev->base + STM32F4_I2C_CR1;
  576. stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
  577. }
  578. status &= ~STM32F4_I2C_SR1_AF;
  579. writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
  580. msg->result = -EIO;
  581. }
  582. /* Bus error */
  583. if (status & STM32F4_I2C_SR1_BERR) {
  584. status &= ~STM32F4_I2C_SR1_BERR;
  585. writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
  586. msg->result = -EIO;
  587. }
  588. stm32f4_i2c_disable_irq(i2c_dev);
  589. complete(&i2c_dev->complete);
  590. return IRQ_HANDLED;
  591. }
  592. /**
  593. * stm32f4_i2c_xfer_msg() - Transfer a single I2C message
  594. * @i2c_dev: Controller's private data
  595. * @msg: I2C message to transfer
  596. * @is_first: first message of the sequence
  597. * @is_last: last message of the sequence
  598. */
  599. static int stm32f4_i2c_xfer_msg(struct stm32f4_i2c_dev *i2c_dev,
  600. struct i2c_msg *msg, bool is_first,
  601. bool is_last)
  602. {
  603. struct stm32f4_i2c_msg *f4_msg = &i2c_dev->msg;
  604. void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR1;
  605. unsigned long timeout;
  606. u32 mask;
  607. int ret;
  608. f4_msg->addr = i2c_8bit_addr_from_msg(msg);
  609. f4_msg->buf = msg->buf;
  610. f4_msg->count = msg->len;
  611. f4_msg->result = 0;
  612. f4_msg->stop = is_last;
  613. reinit_completion(&i2c_dev->complete);
  614. /* Enable events and errors interrupts */
  615. mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN;
  616. stm32f4_i2c_set_bits(i2c_dev->base + STM32F4_I2C_CR2, mask);
  617. if (is_first) {
  618. ret = stm32f4_i2c_wait_free_bus(i2c_dev);
  619. if (ret)
  620. return ret;
  621. /* START generation */
  622. stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
  623. }
  624. timeout = wait_for_completion_timeout(&i2c_dev->complete,
  625. i2c_dev->adap.timeout);
  626. ret = f4_msg->result;
  627. if (!timeout)
  628. ret = -ETIMEDOUT;
  629. return ret;
  630. }
  631. /**
  632. * stm32f4_i2c_xfer() - Transfer combined I2C message
  633. * @i2c_adap: Adapter pointer to the controller
  634. * @msgs: Pointer to data to be written.
  635. * @num: Number of messages to be executed
  636. */
  637. static int stm32f4_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[],
  638. int num)
  639. {
  640. struct stm32f4_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
  641. int ret, i;
  642. ret = clk_enable(i2c_dev->clk);
  643. if (ret) {
  644. dev_err(i2c_dev->dev, "Failed to enable clock\n");
  645. return ret;
  646. }
  647. for (i = 0; i < num && !ret; i++)
  648. ret = stm32f4_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0,
  649. i == num - 1);
  650. clk_disable(i2c_dev->clk);
  651. return (ret < 0) ? ret : num;
  652. }
  653. static u32 stm32f4_i2c_func(struct i2c_adapter *adap)
  654. {
  655. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  656. }
  657. static struct i2c_algorithm stm32f4_i2c_algo = {
  658. .master_xfer = stm32f4_i2c_xfer,
  659. .functionality = stm32f4_i2c_func,
  660. };
  661. static int stm32f4_i2c_probe(struct platform_device *pdev)
  662. {
  663. struct device_node *np = pdev->dev.of_node;
  664. struct stm32f4_i2c_dev *i2c_dev;
  665. struct resource *res;
  666. u32 irq_event, irq_error, clk_rate;
  667. struct i2c_adapter *adap;
  668. struct reset_control *rst;
  669. int ret;
  670. i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
  671. if (!i2c_dev)
  672. return -ENOMEM;
  673. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  674. i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
  675. if (IS_ERR(i2c_dev->base))
  676. return PTR_ERR(i2c_dev->base);
  677. irq_event = irq_of_parse_and_map(np, 0);
  678. if (!irq_event) {
  679. dev_err(&pdev->dev, "IRQ event missing or invalid\n");
  680. return -EINVAL;
  681. }
  682. irq_error = irq_of_parse_and_map(np, 1);
  683. if (!irq_error) {
  684. dev_err(&pdev->dev, "IRQ error missing or invalid\n");
  685. return -EINVAL;
  686. }
  687. i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
  688. if (IS_ERR(i2c_dev->clk)) {
  689. dev_err(&pdev->dev, "Error: Missing controller clock\n");
  690. return PTR_ERR(i2c_dev->clk);
  691. }
  692. ret = clk_prepare_enable(i2c_dev->clk);
  693. if (ret) {
  694. dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
  695. return ret;
  696. }
  697. rst = devm_reset_control_get(&pdev->dev, NULL);
  698. if (IS_ERR(rst)) {
  699. dev_err(&pdev->dev, "Error: Missing controller reset\n");
  700. ret = PTR_ERR(rst);
  701. goto clk_free;
  702. }
  703. reset_control_assert(rst);
  704. udelay(2);
  705. reset_control_deassert(rst);
  706. i2c_dev->speed = STM32F4_I2C_SPEED_STANDARD;
  707. ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
  708. if (!ret && clk_rate >= 400000)
  709. i2c_dev->speed = STM32F4_I2C_SPEED_FAST;
  710. i2c_dev->dev = &pdev->dev;
  711. ret = devm_request_irq(&pdev->dev, irq_event, stm32f4_i2c_isr_event, 0,
  712. pdev->name, i2c_dev);
  713. if (ret) {
  714. dev_err(&pdev->dev, "Failed to request irq event %i\n",
  715. irq_event);
  716. goto clk_free;
  717. }
  718. ret = devm_request_irq(&pdev->dev, irq_error, stm32f4_i2c_isr_error, 0,
  719. pdev->name, i2c_dev);
  720. if (ret) {
  721. dev_err(&pdev->dev, "Failed to request irq error %i\n",
  722. irq_error);
  723. goto clk_free;
  724. }
  725. ret = stm32f4_i2c_hw_config(i2c_dev);
  726. if (ret)
  727. goto clk_free;
  728. adap = &i2c_dev->adap;
  729. i2c_set_adapdata(adap, i2c_dev);
  730. snprintf(adap->name, sizeof(adap->name), "STM32 I2C(%pa)", &res->start);
  731. adap->owner = THIS_MODULE;
  732. adap->timeout = 2 * HZ;
  733. adap->retries = 0;
  734. adap->algo = &stm32f4_i2c_algo;
  735. adap->dev.parent = &pdev->dev;
  736. adap->dev.of_node = pdev->dev.of_node;
  737. init_completion(&i2c_dev->complete);
  738. ret = i2c_add_adapter(adap);
  739. if (ret)
  740. goto clk_free;
  741. platform_set_drvdata(pdev, i2c_dev);
  742. clk_disable(i2c_dev->clk);
  743. dev_info(i2c_dev->dev, "STM32F4 I2C driver registered\n");
  744. return 0;
  745. clk_free:
  746. clk_disable_unprepare(i2c_dev->clk);
  747. return ret;
  748. }
  749. static int stm32f4_i2c_remove(struct platform_device *pdev)
  750. {
  751. struct stm32f4_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
  752. i2c_del_adapter(&i2c_dev->adap);
  753. clk_unprepare(i2c_dev->clk);
  754. return 0;
  755. }
  756. static const struct of_device_id stm32f4_i2c_match[] = {
  757. { .compatible = "st,stm32f4-i2c", },
  758. {},
  759. };
  760. MODULE_DEVICE_TABLE(of, stm32f4_i2c_match);
  761. static struct platform_driver stm32f4_i2c_driver = {
  762. .driver = {
  763. .name = "stm32f4-i2c",
  764. .of_match_table = stm32f4_i2c_match,
  765. },
  766. .probe = stm32f4_i2c_probe,
  767. .remove = stm32f4_i2c_remove,
  768. };
  769. module_platform_driver(stm32f4_i2c_driver);
  770. MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
  771. MODULE_DESCRIPTION("STMicroelectronics STM32F4 I2C driver");
  772. MODULE_LICENSE("GPL v2");