2 * Driver for STMicroelectronics STM32 I2C controller
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
8 * Copyright (C) M'boumba Cedric Madianga 2016
9 * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
11 * This driver is based on i2c-st.c
13 * License terms: GNU General Public License (GPL), version 2
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>
22 #include <linux/iopoll.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
27 #include <linux/platform_device.h>
28 #include <linux/reset.h>
30 #include "i2c-stm32.h"
32 /* STM32F4 I2C offset registers */
33 #define STM32F4_I2C_CR1 0x00
34 #define STM32F4_I2C_CR2 0x04
35 #define STM32F4_I2C_DR 0x10
36 #define STM32F4_I2C_SR1 0x14
37 #define STM32F4_I2C_SR2 0x18
38 #define STM32F4_I2C_CCR 0x1C
39 #define STM32F4_I2C_TRISE 0x20
40 #define STM32F4_I2C_FLTR 0x24
42 /* STM32F4 I2C control 1*/
43 #define STM32F4_I2C_CR1_POS BIT(11)
44 #define STM32F4_I2C_CR1_ACK BIT(10)
45 #define STM32F4_I2C_CR1_STOP BIT(9)
46 #define STM32F4_I2C_CR1_START BIT(8)
47 #define STM32F4_I2C_CR1_PE BIT(0)
49 /* STM32F4 I2C control 2 */
50 #define STM32F4_I2C_CR2_FREQ_MASK GENMASK(5, 0)
51 #define STM32F4_I2C_CR2_FREQ(n) ((n) & STM32F4_I2C_CR2_FREQ_MASK)
52 #define STM32F4_I2C_CR2_ITBUFEN BIT(10)
53 #define STM32F4_I2C_CR2_ITEVTEN BIT(9)
54 #define STM32F4_I2C_CR2_ITERREN BIT(8)
55 #define STM32F4_I2C_CR2_IRQ_MASK (STM32F4_I2C_CR2_ITBUFEN | \
56 STM32F4_I2C_CR2_ITEVTEN | \
57 STM32F4_I2C_CR2_ITERREN)
59 /* STM32F4 I2C Status 1 */
60 #define STM32F4_I2C_SR1_AF BIT(10)
61 #define STM32F4_I2C_SR1_ARLO BIT(9)
62 #define STM32F4_I2C_SR1_BERR BIT(8)
63 #define STM32F4_I2C_SR1_TXE BIT(7)
64 #define STM32F4_I2C_SR1_RXNE BIT(6)
65 #define STM32F4_I2C_SR1_BTF BIT(2)
66 #define STM32F4_I2C_SR1_ADDR BIT(1)
67 #define STM32F4_I2C_SR1_SB BIT(0)
68 #define STM32F4_I2C_SR1_ITEVTEN_MASK (STM32F4_I2C_SR1_BTF | \
69 STM32F4_I2C_SR1_ADDR | \
71 #define STM32F4_I2C_SR1_ITBUFEN_MASK (STM32F4_I2C_SR1_TXE | \
73 #define STM32F4_I2C_SR1_ITERREN_MASK (STM32F4_I2C_SR1_AF | \
74 STM32F4_I2C_SR1_ARLO | \
77 /* STM32F4 I2C Status 2 */
78 #define STM32F4_I2C_SR2_BUSY BIT(1)
80 /* STM32F4 I2C Control Clock */
81 #define STM32F4_I2C_CCR_CCR_MASK GENMASK(11, 0)
82 #define STM32F4_I2C_CCR_CCR(n) ((n) & STM32F4_I2C_CCR_CCR_MASK)
83 #define STM32F4_I2C_CCR_FS BIT(15)
84 #define STM32F4_I2C_CCR_DUTY BIT(14)
86 /* STM32F4 I2C Trise */
87 #define STM32F4_I2C_TRISE_VALUE_MASK GENMASK(5, 0)
88 #define STM32F4_I2C_TRISE_VALUE(n) ((n) & STM32F4_I2C_TRISE_VALUE_MASK)
90 #define STM32F4_I2C_MIN_STANDARD_FREQ 2U
91 #define STM32F4_I2C_MIN_FAST_FREQ 6U
92 #define STM32F4_I2C_MAX_FREQ 46U
93 #define HZ_TO_MHZ 1000000
96 * struct stm32f4_i2c_msg - client specific data
97 * @addr: 8-bit slave addr, including r/w bit
98 * @count: number of bytes to be transferred
100 * @result: result of the transfer
101 * @stop: last I2C msg to be sent, i.e. STOP to be generated
103 struct stm32f4_i2c_msg {
112 * struct stm32f4_i2c_dev - private data of the controller
113 * @adap: I2C adapter for this controller
114 * @dev: device for this controller
115 * @base: virtual memory area
116 * @complete: completion of I2C message
118 * @speed: I2C clock frequency of the controller. Standard or Fast are supported
119 * @parent_rate: I2C clock parent rate in MHz
120 * @msg: I2C transfer information
122 struct stm32f4_i2c_dev {
123 struct i2c_adapter adap;
126 struct completion complete;
130 struct stm32f4_i2c_msg msg;
133 static inline void stm32f4_i2c_set_bits(void __iomem *reg, u32 mask)
135 writel_relaxed(readl_relaxed(reg) | mask, reg);
138 static inline void stm32f4_i2c_clr_bits(void __iomem *reg, u32 mask)
140 writel_relaxed(readl_relaxed(reg) & ~mask, reg);
143 static void stm32f4_i2c_disable_irq(struct stm32f4_i2c_dev *i2c_dev)
145 void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
147 stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_IRQ_MASK);
150 static int stm32f4_i2c_set_periph_clk_freq(struct stm32f4_i2c_dev *i2c_dev)
155 i2c_dev->parent_rate = clk_get_rate(i2c_dev->clk);
156 freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ);
158 if (i2c_dev->speed == STM32_I2C_SPEED_STANDARD) {
160 * To reach 100 kHz, the parent clk frequency should be between
161 * a minimum value of 2 MHz and a maximum value of 46 MHz due
162 * to hardware limitation
164 if (freq < STM32F4_I2C_MIN_STANDARD_FREQ ||
165 freq > STM32F4_I2C_MAX_FREQ) {
166 dev_err(i2c_dev->dev,
167 "bad parent clk freq for standard mode\n");
172 * To be as close as possible to 400 kHz, the parent clk
173 * frequency should be between a minimum value of 6 MHz and a
174 * maximum value of 46 MHz due to hardware limitation
176 if (freq < STM32F4_I2C_MIN_FAST_FREQ ||
177 freq > STM32F4_I2C_MAX_FREQ) {
178 dev_err(i2c_dev->dev,
179 "bad parent clk freq for fast mode\n");
184 cr2 |= STM32F4_I2C_CR2_FREQ(freq);
185 writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2);
190 static void stm32f4_i2c_set_rise_time(struct stm32f4_i2c_dev *i2c_dev)
192 u32 freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ);
196 * These bits must be programmed with the maximum SCL rise time given in
197 * the I2C bus specification, incremented by 1.
199 * In standard mode, the maximum allowed SCL rise time is 1000 ns.
200 * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to
201 * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be
202 * programmed with 0x9. (1000 ns / 125 ns + 1)
203 * So, for I2C standard mode TRISE = FREQ[5:0] + 1
205 * In fast mode, the maximum allowed SCL rise time is 300 ns.
206 * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to
207 * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be
208 * programmed with 0x3. (300 ns / 125 ns + 1)
209 * So, for I2C fast mode TRISE = FREQ[5:0] * 300 / 1000 + 1
211 * Function stm32f4_i2c_set_periph_clk_freq made sure that parent rate
212 * is not higher than 46 MHz . As a result trise is at most 4 bits wide
213 * and so fits into the TRISE bits [5:0].
215 if (i2c_dev->speed == STM32_I2C_SPEED_STANDARD)
218 trise = freq * 3 / 10 + 1;
220 writel_relaxed(STM32F4_I2C_TRISE_VALUE(trise),
221 i2c_dev->base + STM32F4_I2C_TRISE);
224 static void stm32f4_i2c_set_speed_mode(struct stm32f4_i2c_dev *i2c_dev)
229 if (i2c_dev->speed == STM32_I2C_SPEED_STANDARD) {
232 * t_scl_high = t_scl_low = CCR * I2C parent clk period
233 * So to reach 100 kHz, we have:
234 * CCR = I2C parent rate / 100 kHz >> 1
236 * For example with parent rate = 2 MHz:
237 * CCR = 2000000 / (100000 << 1) = 10
238 * t_scl_high = t_scl_low = 10 * (1 / 2000000) = 5000 ns
239 * t_scl_high + t_scl_low = 10000 ns so 100 kHz is reached
241 * Function stm32f4_i2c_set_periph_clk_freq made sure that
242 * parent rate is not higher than 46 MHz . As a result val
243 * is at most 8 bits wide and so fits into the CCR bits [11:0].
245 val = i2c_dev->parent_rate / (100000 << 1);
248 * In fast mode, we compute CCR with duty = 0 as with low
249 * frequencies we are not able to reach 400 kHz.
251 * t_scl_high = CCR * I2C parent clk period
252 * t_scl_low = 2 * CCR * I2C parent clk period
253 * So, CCR = I2C parent rate / (400 kHz * 3)
255 * For example with parent rate = 6 MHz:
256 * CCR = 6000000 / (400000 * 3) = 5
257 * t_scl_high = 5 * (1 / 6000000) = 833 ns > 600 ns
258 * t_scl_low = 2 * 5 * (1 / 6000000) = 1667 ns > 1300 ns
259 * t_scl_high + t_scl_low = 2500 ns so 400 kHz is reached
261 * Function stm32f4_i2c_set_periph_clk_freq made sure that
262 * parent rate is not higher than 46 MHz . As a result val
263 * is at most 6 bits wide and so fits into the CCR bits [11:0].
265 val = DIV_ROUND_UP(i2c_dev->parent_rate, 400000 * 3);
267 /* Select Fast mode */
268 ccr |= STM32F4_I2C_CCR_FS;
271 ccr |= STM32F4_I2C_CCR_CCR(val);
272 writel_relaxed(ccr, i2c_dev->base + STM32F4_I2C_CCR);
276 * stm32f4_i2c_hw_config() - Prepare I2C block
277 * @i2c_dev: Controller's private data
279 static int stm32f4_i2c_hw_config(struct stm32f4_i2c_dev *i2c_dev)
283 ret = stm32f4_i2c_set_periph_clk_freq(i2c_dev);
287 stm32f4_i2c_set_rise_time(i2c_dev);
289 stm32f4_i2c_set_speed_mode(i2c_dev);
292 writel_relaxed(STM32F4_I2C_CR1_PE, i2c_dev->base + STM32F4_I2C_CR1);
297 static int stm32f4_i2c_wait_free_bus(struct stm32f4_i2c_dev *i2c_dev)
302 ret = readl_relaxed_poll_timeout(i2c_dev->base + STM32F4_I2C_SR2,
304 !(status & STM32F4_I2C_SR2_BUSY),
307 dev_dbg(i2c_dev->dev, "bus not free\n");
315 * stm32f4_i2c_write_ byte() - Write a byte in the data register
316 * @i2c_dev: Controller's private data
317 * @byte: Data to write in the register
319 static void stm32f4_i2c_write_byte(struct stm32f4_i2c_dev *i2c_dev, u8 byte)
321 writel_relaxed(byte, i2c_dev->base + STM32F4_I2C_DR);
325 * stm32f4_i2c_write_msg() - Fill the data register in write mode
326 * @i2c_dev: Controller's private data
328 * This function fills the data register with I2C transfer buffer
330 static void stm32f4_i2c_write_msg(struct stm32f4_i2c_dev *i2c_dev)
332 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
334 stm32f4_i2c_write_byte(i2c_dev, *msg->buf++);
338 static void stm32f4_i2c_read_msg(struct stm32f4_i2c_dev *i2c_dev)
340 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
343 rbuf = readl_relaxed(i2c_dev->base + STM32F4_I2C_DR);
348 static void stm32f4_i2c_terminate_xfer(struct stm32f4_i2c_dev *i2c_dev)
350 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
351 void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
353 stm32f4_i2c_disable_irq(i2c_dev);
355 reg = i2c_dev->base + STM32F4_I2C_CR1;
357 stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
359 stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
361 complete(&i2c_dev->complete);
365 * stm32f4_i2c_handle_write() - Handle FIFO empty interrupt in case of write
366 * @i2c_dev: Controller's private data
368 static void stm32f4_i2c_handle_write(struct stm32f4_i2c_dev *i2c_dev)
370 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
371 void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
374 stm32f4_i2c_write_msg(i2c_dev);
377 * Disable buffer interrupts for RX not empty and TX
380 stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN);
383 stm32f4_i2c_terminate_xfer(i2c_dev);
388 * stm32f4_i2c_handle_read() - Handle FIFO empty interrupt in case of read
389 * @i2c_dev: Controller's private data
391 * This function is called when a new data is received in data register
393 static void stm32f4_i2c_handle_read(struct stm32f4_i2c_dev *i2c_dev)
395 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
396 void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
398 switch (msg->count) {
400 stm32f4_i2c_disable_irq(i2c_dev);
401 stm32f4_i2c_read_msg(i2c_dev);
402 complete(&i2c_dev->complete);
405 * For 2-byte reception, 3-byte reception and for Data N-2, N-1 and N
406 * for N-byte reception with N > 3, we do not have to read the data
407 * register when RX not empty event occurs as we have to wait for byte
408 * transferred finished event before reading data.
409 * So, here we just disable buffer interrupt in order to avoid another
410 * system preemption due to RX not empty event.
414 stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN);
417 * For N byte reception with N > 3 we directly read data register
421 stm32f4_i2c_read_msg(i2c_dev);
426 * stm32f4_i2c_handle_rx_done() - Handle byte transfer finished interrupt
428 * @i2c_dev: Controller's private data
430 * This function is called when a new data is received in the shift register
431 * but data register has not been read yet.
433 static void stm32f4_i2c_handle_rx_done(struct stm32f4_i2c_dev *i2c_dev)
435 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
440 switch (msg->count) {
443 * In order to correctly send the Stop or Repeated Start
444 * condition on the I2C bus, the STOP/START bit has to be set
445 * before reading the last two bytes (data N-1 and N).
446 * After that, we could read the last two bytes, disable
447 * remaining interrupts and notify the end of xfer to the
450 reg = i2c_dev->base + STM32F4_I2C_CR1;
452 stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
454 stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
456 for (i = 2; i > 0; i--)
457 stm32f4_i2c_read_msg(i2c_dev);
459 reg = i2c_dev->base + STM32F4_I2C_CR2;
460 mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN;
461 stm32f4_i2c_clr_bits(reg, mask);
463 complete(&i2c_dev->complete);
467 * In order to correctly generate the NACK pulse after the last
468 * received data byte, we have to enable NACK before reading N-2
471 reg = i2c_dev->base + STM32F4_I2C_CR1;
472 stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR1_ACK);
473 stm32f4_i2c_read_msg(i2c_dev);
476 stm32f4_i2c_read_msg(i2c_dev);
481 * stm32f4_i2c_handle_rx_addr() - Handle address matched interrupt in case of
483 * @i2c_dev: Controller's private data
485 static void stm32f4_i2c_handle_rx_addr(struct stm32f4_i2c_dev *i2c_dev)
487 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
490 switch (msg->count) {
492 stm32f4_i2c_terminate_xfer(i2c_dev);
494 /* Clear ADDR flag */
495 readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
499 * Single byte reception:
500 * Enable NACK and reset POS (Acknowledge position).
501 * Then, clear ADDR flag and set STOP or RepSTART.
502 * In that way, the NACK and STOP or RepStart pulses will be
503 * sent as soon as the byte will be received in shift register
505 cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
506 cr1 &= ~(STM32F4_I2C_CR1_ACK | STM32F4_I2C_CR1_POS);
507 writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
509 readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
512 cr1 |= STM32F4_I2C_CR1_STOP;
514 cr1 |= STM32F4_I2C_CR1_START;
515 writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
520 * Enable NACK, set POS (NACK position) and clear ADDR flag.
521 * In that way, NACK will be sent for the next byte which will
522 * be received in the shift register instead of the current
525 cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
526 cr1 &= ~STM32F4_I2C_CR1_ACK;
527 cr1 |= STM32F4_I2C_CR1_POS;
528 writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
530 readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
536 * Enable ACK, reset POS (ACK postion) and clear ADDR flag.
537 * In that way, ACK will be sent as soon as the current byte
538 * will be received in the shift register
540 cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
541 cr1 |= STM32F4_I2C_CR1_ACK;
542 cr1 &= ~STM32F4_I2C_CR1_POS;
543 writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
545 readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
551 * stm32f4_i2c_isr_event() - Interrupt routine for I2C bus event
552 * @irq: interrupt number
553 * @data: Controller's private data
555 static irqreturn_t stm32f4_i2c_isr_event(int irq, void *data)
557 struct stm32f4_i2c_dev *i2c_dev = data;
558 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
559 u32 possible_status = STM32F4_I2C_SR1_ITEVTEN_MASK;
560 u32 status, ien, event, cr2;
562 cr2 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR2);
563 ien = cr2 & STM32F4_I2C_CR2_IRQ_MASK;
565 /* Update possible_status if buffer interrupt is enabled */
566 if (ien & STM32F4_I2C_CR2_ITBUFEN)
567 possible_status |= STM32F4_I2C_SR1_ITBUFEN_MASK;
569 status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1);
570 event = status & possible_status;
572 dev_dbg(i2c_dev->dev,
573 "spurious evt irq (status=0x%08x, ien=0x%08x)\n",
578 /* Start condition generated */
579 if (event & STM32F4_I2C_SR1_SB)
580 stm32f4_i2c_write_byte(i2c_dev, msg->addr);
582 /* I2C Address sent */
583 if (event & STM32F4_I2C_SR1_ADDR) {
584 if (msg->addr & I2C_M_RD)
585 stm32f4_i2c_handle_rx_addr(i2c_dev);
587 readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
590 * Enable buffer interrupts for RX not empty and TX empty
593 cr2 |= STM32F4_I2C_CR2_ITBUFEN;
594 writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2);
598 if ((event & STM32F4_I2C_SR1_TXE) && !(msg->addr & I2C_M_RD))
599 stm32f4_i2c_handle_write(i2c_dev);
602 if ((event & STM32F4_I2C_SR1_RXNE) && (msg->addr & I2C_M_RD))
603 stm32f4_i2c_handle_read(i2c_dev);
606 * The BTF (Byte Transfer finished) event occurs when:
607 * - in reception : a new byte is received in the shift register
608 * but the previous byte has not been read yet from data register
609 * - in transmission: a new byte should be sent but the data register
610 * has not been written yet
612 if (event & STM32F4_I2C_SR1_BTF) {
613 if (msg->addr & I2C_M_RD)
614 stm32f4_i2c_handle_rx_done(i2c_dev);
616 stm32f4_i2c_handle_write(i2c_dev);
623 * stm32f4_i2c_isr_error() - Interrupt routine for I2C bus error
624 * @irq: interrupt number
625 * @data: Controller's private data
627 static irqreturn_t stm32f4_i2c_isr_error(int irq, void *data)
629 struct stm32f4_i2c_dev *i2c_dev = data;
630 struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
634 status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1);
636 /* Arbitration lost */
637 if (status & STM32F4_I2C_SR1_ARLO) {
638 status &= ~STM32F4_I2C_SR1_ARLO;
639 writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
640 msg->result = -EAGAIN;
644 * Acknowledge failure:
645 * In master transmitter mode a Stop must be generated by software
647 if (status & STM32F4_I2C_SR1_AF) {
648 if (!(msg->addr & I2C_M_RD)) {
649 reg = i2c_dev->base + STM32F4_I2C_CR1;
650 stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
652 status &= ~STM32F4_I2C_SR1_AF;
653 writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
658 if (status & STM32F4_I2C_SR1_BERR) {
659 status &= ~STM32F4_I2C_SR1_BERR;
660 writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
664 stm32f4_i2c_disable_irq(i2c_dev);
665 complete(&i2c_dev->complete);
671 * stm32f4_i2c_xfer_msg() - Transfer a single I2C message
672 * @i2c_dev: Controller's private data
673 * @msg: I2C message to transfer
674 * @is_first: first message of the sequence
675 * @is_last: last message of the sequence
677 static int stm32f4_i2c_xfer_msg(struct stm32f4_i2c_dev *i2c_dev,
678 struct i2c_msg *msg, bool is_first,
681 struct stm32f4_i2c_msg *f4_msg = &i2c_dev->msg;
682 void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR1;
683 unsigned long timeout;
687 f4_msg->addr = i2c_8bit_addr_from_msg(msg);
688 f4_msg->buf = msg->buf;
689 f4_msg->count = msg->len;
691 f4_msg->stop = is_last;
693 reinit_completion(&i2c_dev->complete);
695 /* Enable events and errors interrupts */
696 mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN;
697 stm32f4_i2c_set_bits(i2c_dev->base + STM32F4_I2C_CR2, mask);
700 ret = stm32f4_i2c_wait_free_bus(i2c_dev);
704 /* START generation */
705 stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
708 timeout = wait_for_completion_timeout(&i2c_dev->complete,
709 i2c_dev->adap.timeout);
710 ret = f4_msg->result;
719 * stm32f4_i2c_xfer() - Transfer combined I2C message
720 * @i2c_adap: Adapter pointer to the controller
721 * @msgs: Pointer to data to be written.
722 * @num: Number of messages to be executed
724 static int stm32f4_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[],
727 struct stm32f4_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
730 ret = clk_enable(i2c_dev->clk);
732 dev_err(i2c_dev->dev, "Failed to enable clock\n");
736 for (i = 0; i < num && !ret; i++)
737 ret = stm32f4_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0,
740 clk_disable(i2c_dev->clk);
742 return (ret < 0) ? ret : num;
745 static u32 stm32f4_i2c_func(struct i2c_adapter *adap)
747 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
750 static const struct i2c_algorithm stm32f4_i2c_algo = {
751 .master_xfer = stm32f4_i2c_xfer,
752 .functionality = stm32f4_i2c_func,
755 static int stm32f4_i2c_probe(struct platform_device *pdev)
757 struct device_node *np = pdev->dev.of_node;
758 struct stm32f4_i2c_dev *i2c_dev;
759 struct resource *res;
760 u32 irq_event, irq_error, clk_rate;
761 struct i2c_adapter *adap;
762 struct reset_control *rst;
765 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
769 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
770 i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
771 if (IS_ERR(i2c_dev->base))
772 return PTR_ERR(i2c_dev->base);
774 irq_event = irq_of_parse_and_map(np, 0);
776 dev_err(&pdev->dev, "IRQ event missing or invalid\n");
780 irq_error = irq_of_parse_and_map(np, 1);
782 dev_err(&pdev->dev, "IRQ error missing or invalid\n");
786 i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
787 if (IS_ERR(i2c_dev->clk)) {
788 dev_err(&pdev->dev, "Error: Missing controller clock\n");
789 return PTR_ERR(i2c_dev->clk);
791 ret = clk_prepare_enable(i2c_dev->clk);
793 dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
797 rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
799 dev_err(&pdev->dev, "Error: Missing controller reset\n");
803 reset_control_assert(rst);
805 reset_control_deassert(rst);
807 i2c_dev->speed = STM32_I2C_SPEED_STANDARD;
808 ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
809 if (!ret && clk_rate >= 400000)
810 i2c_dev->speed = STM32_I2C_SPEED_FAST;
812 i2c_dev->dev = &pdev->dev;
814 ret = devm_request_irq(&pdev->dev, irq_event, stm32f4_i2c_isr_event, 0,
815 pdev->name, i2c_dev);
817 dev_err(&pdev->dev, "Failed to request irq event %i\n",
822 ret = devm_request_irq(&pdev->dev, irq_error, stm32f4_i2c_isr_error, 0,
823 pdev->name, i2c_dev);
825 dev_err(&pdev->dev, "Failed to request irq error %i\n",
830 ret = stm32f4_i2c_hw_config(i2c_dev);
834 adap = &i2c_dev->adap;
835 i2c_set_adapdata(adap, i2c_dev);
836 snprintf(adap->name, sizeof(adap->name), "STM32 I2C(%pa)", &res->start);
837 adap->owner = THIS_MODULE;
838 adap->timeout = 2 * HZ;
840 adap->algo = &stm32f4_i2c_algo;
841 adap->dev.parent = &pdev->dev;
842 adap->dev.of_node = pdev->dev.of_node;
844 init_completion(&i2c_dev->complete);
846 ret = i2c_add_adapter(adap);
850 platform_set_drvdata(pdev, i2c_dev);
852 clk_disable(i2c_dev->clk);
854 dev_info(i2c_dev->dev, "STM32F4 I2C driver registered\n");
859 clk_disable_unprepare(i2c_dev->clk);
863 static int stm32f4_i2c_remove(struct platform_device *pdev)
865 struct stm32f4_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
867 i2c_del_adapter(&i2c_dev->adap);
869 clk_unprepare(i2c_dev->clk);
874 static const struct of_device_id stm32f4_i2c_match[] = {
875 { .compatible = "st,stm32f4-i2c", },
878 MODULE_DEVICE_TABLE(of, stm32f4_i2c_match);
880 static struct platform_driver stm32f4_i2c_driver = {
882 .name = "stm32f4-i2c",
883 .of_match_table = stm32f4_i2c_match,
885 .probe = stm32f4_i2c_probe,
886 .remove = stm32f4_i2c_remove,
889 module_platform_driver(stm32f4_i2c_driver);
891 MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
892 MODULE_DESCRIPTION("STMicroelectronics STM32F4 I2C driver");
893 MODULE_LICENSE("GPL v2");