i2c-stm32f4.c 24 KB

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