1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for STMicroelectronics STM32F7 I2C controller
5 * This I2C controller is described in the STM32F75xxx and STM32F74xxx Soc
7 * Please see below a link to the documentation:
8 * http://www.st.com/resource/en/reference_manual/dm00124865.pdf
10 * Copyright (C) M'boumba Cedric Madianga 2017
11 * Copyright (C) STMicroelectronics 2017
12 * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
14 * This driver is based on i2c-stm32f4.c
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
23 #include <linux/iopoll.h>
24 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_platform.h>
28 #include <linux/platform_device.h>
29 #include <linux/reset.h>
30 #include <linux/slab.h>
32 #include "i2c-stm32.h"
34 /* STM32F7 I2C registers */
35 #define STM32F7_I2C_CR1 0x00
36 #define STM32F7_I2C_CR2 0x04
37 #define STM32F7_I2C_OAR1 0x08
38 #define STM32F7_I2C_OAR2 0x0C
39 #define STM32F7_I2C_PECR 0x20
40 #define STM32F7_I2C_TIMINGR 0x10
41 #define STM32F7_I2C_ISR 0x18
42 #define STM32F7_I2C_ICR 0x1C
43 #define STM32F7_I2C_RXDR 0x24
44 #define STM32F7_I2C_TXDR 0x28
46 /* STM32F7 I2C control 1 */
47 #define STM32F7_I2C_CR1_PECEN BIT(23)
48 #define STM32F7_I2C_CR1_SBC BIT(16)
49 #define STM32F7_I2C_CR1_RXDMAEN BIT(15)
50 #define STM32F7_I2C_CR1_TXDMAEN BIT(14)
51 #define STM32F7_I2C_CR1_ANFOFF BIT(12)
52 #define STM32F7_I2C_CR1_ERRIE BIT(7)
53 #define STM32F7_I2C_CR1_TCIE BIT(6)
54 #define STM32F7_I2C_CR1_STOPIE BIT(5)
55 #define STM32F7_I2C_CR1_NACKIE BIT(4)
56 #define STM32F7_I2C_CR1_ADDRIE BIT(3)
57 #define STM32F7_I2C_CR1_RXIE BIT(2)
58 #define STM32F7_I2C_CR1_TXIE BIT(1)
59 #define STM32F7_I2C_CR1_PE BIT(0)
60 #define STM32F7_I2C_ALL_IRQ_MASK (STM32F7_I2C_CR1_ERRIE \
61 | STM32F7_I2C_CR1_TCIE \
62 | STM32F7_I2C_CR1_STOPIE \
63 | STM32F7_I2C_CR1_NACKIE \
64 | STM32F7_I2C_CR1_RXIE \
65 | STM32F7_I2C_CR1_TXIE)
66 #define STM32F7_I2C_XFER_IRQ_MASK (STM32F7_I2C_CR1_TCIE \
67 | STM32F7_I2C_CR1_STOPIE \
68 | STM32F7_I2C_CR1_NACKIE \
69 | STM32F7_I2C_CR1_RXIE \
70 | STM32F7_I2C_CR1_TXIE)
72 /* STM32F7 I2C control 2 */
73 #define STM32F7_I2C_CR2_PECBYTE BIT(26)
74 #define STM32F7_I2C_CR2_RELOAD BIT(24)
75 #define STM32F7_I2C_CR2_NBYTES_MASK GENMASK(23, 16)
76 #define STM32F7_I2C_CR2_NBYTES(n) (((n) & 0xff) << 16)
77 #define STM32F7_I2C_CR2_NACK BIT(15)
78 #define STM32F7_I2C_CR2_STOP BIT(14)
79 #define STM32F7_I2C_CR2_START BIT(13)
80 #define STM32F7_I2C_CR2_HEAD10R BIT(12)
81 #define STM32F7_I2C_CR2_ADD10 BIT(11)
82 #define STM32F7_I2C_CR2_RD_WRN BIT(10)
83 #define STM32F7_I2C_CR2_SADD10_MASK GENMASK(9, 0)
84 #define STM32F7_I2C_CR2_SADD10(n) (((n) & \
85 STM32F7_I2C_CR2_SADD10_MASK))
86 #define STM32F7_I2C_CR2_SADD7_MASK GENMASK(7, 1)
87 #define STM32F7_I2C_CR2_SADD7(n) (((n) & 0x7f) << 1)
89 /* STM32F7 I2C Own Address 1 */
90 #define STM32F7_I2C_OAR1_OA1EN BIT(15)
91 #define STM32F7_I2C_OAR1_OA1MODE BIT(10)
92 #define STM32F7_I2C_OAR1_OA1_10_MASK GENMASK(9, 0)
93 #define STM32F7_I2C_OAR1_OA1_10(n) (((n) & \
94 STM32F7_I2C_OAR1_OA1_10_MASK))
95 #define STM32F7_I2C_OAR1_OA1_7_MASK GENMASK(7, 1)
96 #define STM32F7_I2C_OAR1_OA1_7(n) (((n) & 0x7f) << 1)
97 #define STM32F7_I2C_OAR1_MASK (STM32F7_I2C_OAR1_OA1_7_MASK \
98 | STM32F7_I2C_OAR1_OA1_10_MASK \
99 | STM32F7_I2C_OAR1_OA1EN \
100 | STM32F7_I2C_OAR1_OA1MODE)
102 /* STM32F7 I2C Own Address 2 */
103 #define STM32F7_I2C_OAR2_OA2EN BIT(15)
104 #define STM32F7_I2C_OAR2_OA2MSK_MASK GENMASK(10, 8)
105 #define STM32F7_I2C_OAR2_OA2MSK(n) (((n) & 0x7) << 8)
106 #define STM32F7_I2C_OAR2_OA2_7_MASK GENMASK(7, 1)
107 #define STM32F7_I2C_OAR2_OA2_7(n) (((n) & 0x7f) << 1)
108 #define STM32F7_I2C_OAR2_MASK (STM32F7_I2C_OAR2_OA2MSK_MASK \
109 | STM32F7_I2C_OAR2_OA2_7_MASK \
110 | STM32F7_I2C_OAR2_OA2EN)
112 /* STM32F7 I2C Interrupt Status */
113 #define STM32F7_I2C_ISR_ADDCODE_MASK GENMASK(23, 17)
114 #define STM32F7_I2C_ISR_ADDCODE_GET(n) \
115 (((n) & STM32F7_I2C_ISR_ADDCODE_MASK) >> 17)
116 #define STM32F7_I2C_ISR_DIR BIT(16)
117 #define STM32F7_I2C_ISR_BUSY BIT(15)
118 #define STM32F7_I2C_ISR_PECERR BIT(11)
119 #define STM32F7_I2C_ISR_ARLO BIT(9)
120 #define STM32F7_I2C_ISR_BERR BIT(8)
121 #define STM32F7_I2C_ISR_TCR BIT(7)
122 #define STM32F7_I2C_ISR_TC BIT(6)
123 #define STM32F7_I2C_ISR_STOPF BIT(5)
124 #define STM32F7_I2C_ISR_NACKF BIT(4)
125 #define STM32F7_I2C_ISR_ADDR BIT(3)
126 #define STM32F7_I2C_ISR_RXNE BIT(2)
127 #define STM32F7_I2C_ISR_TXIS BIT(1)
128 #define STM32F7_I2C_ISR_TXE BIT(0)
130 /* STM32F7 I2C Interrupt Clear */
131 #define STM32F7_I2C_ICR_PECCF BIT(11)
132 #define STM32F7_I2C_ICR_ARLOCF BIT(9)
133 #define STM32F7_I2C_ICR_BERRCF BIT(8)
134 #define STM32F7_I2C_ICR_STOPCF BIT(5)
135 #define STM32F7_I2C_ICR_NACKCF BIT(4)
136 #define STM32F7_I2C_ICR_ADDRCF BIT(3)
138 /* STM32F7 I2C Timing */
139 #define STM32F7_I2C_TIMINGR_PRESC(n) (((n) & 0xf) << 28)
140 #define STM32F7_I2C_TIMINGR_SCLDEL(n) (((n) & 0xf) << 20)
141 #define STM32F7_I2C_TIMINGR_SDADEL(n) (((n) & 0xf) << 16)
142 #define STM32F7_I2C_TIMINGR_SCLH(n) (((n) & 0xff) << 8)
143 #define STM32F7_I2C_TIMINGR_SCLL(n) ((n) & 0xff)
145 #define STM32F7_I2C_MAX_LEN 0xff
146 #define STM32F7_I2C_DMA_LEN_MIN 0x16
147 #define STM32F7_I2C_MAX_SLAVE 0x2
149 #define STM32F7_I2C_DNF_DEFAULT 0
150 #define STM32F7_I2C_DNF_MAX 16
152 #define STM32F7_I2C_ANALOG_FILTER_ENABLE 1
153 #define STM32F7_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */
154 #define STM32F7_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */
156 #define STM32F7_I2C_RISE_TIME_DEFAULT 25 /* ns */
157 #define STM32F7_I2C_FALL_TIME_DEFAULT 10 /* ns */
159 #define STM32F7_PRESC_MAX BIT(4)
160 #define STM32F7_SCLDEL_MAX BIT(4)
161 #define STM32F7_SDADEL_MAX BIT(4)
162 #define STM32F7_SCLH_MAX BIT(8)
163 #define STM32F7_SCLL_MAX BIT(8)
166 * struct stm32f7_i2c_spec - private i2c specification timing
167 * @rate: I2C bus speed (Hz)
168 * @rate_min: 80% of I2C bus speed (Hz)
169 * @rate_max: 100% of I2C bus speed (Hz)
170 * @fall_max: Max fall time of both SDA and SCL signals (ns)
171 * @rise_max: Max rise time of both SDA and SCL signals (ns)
172 * @hddat_min: Min data hold time (ns)
173 * @vddat_max: Max data valid time (ns)
174 * @sudat_min: Min data setup time (ns)
175 * @l_min: Min low period of the SCL clock (ns)
176 * @h_min: Min high period of the SCL clock (ns)
178 struct stm32f7_i2c_spec
{
192 * struct stm32f7_i2c_setup - private I2C timing setup parameters
193 * @speed: I2C speed mode (standard, Fast Plus)
194 * @speed_freq: I2C speed frequency (Hz)
195 * @clock_src: I2C clock source frequency (Hz)
196 * @rise_time: Rise time (ns)
197 * @fall_time: Fall time (ns)
198 * @dnf: Digital filter coefficient (0-16)
199 * @analog_filter: Analog filter delay (On/Off)
201 struct stm32f7_i2c_setup
{
202 enum stm32_i2c_speed speed
;
212 * struct stm32f7_i2c_timings - private I2C output parameters
214 * @presc: Prescaler value
215 * @scldel: Data setup time
216 * @sdadel: Data hold time
217 * @sclh: SCL high period (master mode)
218 * @scll: SCL low period (master mode)
220 struct stm32f7_i2c_timings
{
221 struct list_head node
;
230 * struct stm32f7_i2c_msg - client specific data
231 * @addr: 8-bit or 10-bit slave addr, including r/w bit
232 * @count: number of bytes to be transferred
234 * @result: result of the transfer
235 * @stop: last I2C msg to be sent, i.e. STOP to be generated
236 * @smbus: boolean to know if the I2C IP is used in SMBus mode
237 * @size: type of SMBus protocol
238 * @read_write: direction of SMBus protocol
239 * SMBus block read and SMBus block write - block read process call protocols
240 * @smbus_buf: buffer to be used for SMBus protocol transfer. It will
241 * contain a maximum of 32 bytes of data + byte command + byte count + PEC
242 * This buffer has to be 32-bit aligned to be compliant with memory address
243 * register in DMA mode.
245 struct stm32f7_i2c_msg
{
254 u8 smbus_buf
[I2C_SMBUS_BLOCK_MAX
+ 3] __aligned(4);
258 * struct stm32f7_i2c_dev - private data of the controller
259 * @adap: I2C adapter for this controller
260 * @dev: device for this controller
261 * @base: virtual memory area
262 * @complete: completion of I2C message
264 * @speed: I2C clock frequency of the controller. Standard, Fast or Fast+
265 * @msg: Pointer to data to be written
266 * @msg_num: number of I2C messages to be executed
267 * @msg_id: message identifiant
268 * @f7_msg: customized i2c msg for driver usage
269 * @setup: I2C timing input setup
270 * @timing: I2C computed timings
271 * @slave: list of slave devices registered on the I2C bus
272 * @slave_running: slave device currently used
273 * @slave_dir: transfer direction for the current slave device
274 * @master_mode: boolean to know in which mode the I2C is running (master or
277 * @use_dma: boolean to know if dma is used in the current transfer
279 struct stm32f7_i2c_dev
{
280 struct i2c_adapter adap
;
283 struct completion complete
;
287 unsigned int msg_num
;
289 struct stm32f7_i2c_msg f7_msg
;
290 struct stm32f7_i2c_setup setup
;
291 struct stm32f7_i2c_timings timing
;
292 struct i2c_client
*slave
[STM32F7_I2C_MAX_SLAVE
];
293 struct i2c_client
*slave_running
;
296 struct stm32_i2c_dma
*dma
;
301 * All these values are coming from I2C Specification, Version 6.0, 4th of
304 * Table10. Characteristics of the SDA and SCL bus lines for Standard, Fast,
305 * and Fast-mode Plus I2C-bus devices
307 static struct stm32f7_i2c_spec i2c_specs
[] = {
308 [STM32_I2C_SPEED_STANDARD
] = {
320 [STM32_I2C_SPEED_FAST
] = {
332 [STM32_I2C_SPEED_FAST_PLUS
] = {
346 static const struct stm32f7_i2c_setup stm32f7_setup
= {
347 .rise_time
= STM32F7_I2C_RISE_TIME_DEFAULT
,
348 .fall_time
= STM32F7_I2C_FALL_TIME_DEFAULT
,
349 .dnf
= STM32F7_I2C_DNF_DEFAULT
,
350 .analog_filter
= STM32F7_I2C_ANALOG_FILTER_ENABLE
,
353 static inline void stm32f7_i2c_set_bits(void __iomem
*reg
, u32 mask
)
355 writel_relaxed(readl_relaxed(reg
) | mask
, reg
);
358 static inline void stm32f7_i2c_clr_bits(void __iomem
*reg
, u32 mask
)
360 writel_relaxed(readl_relaxed(reg
) & ~mask
, reg
);
363 static void stm32f7_i2c_disable_irq(struct stm32f7_i2c_dev
*i2c_dev
, u32 mask
)
365 stm32f7_i2c_clr_bits(i2c_dev
->base
+ STM32F7_I2C_CR1
, mask
);
368 static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev
*i2c_dev
,
369 struct stm32f7_i2c_setup
*setup
,
370 struct stm32f7_i2c_timings
*output
)
372 u32 p_prev
= STM32F7_PRESC_MAX
;
373 u32 i2cclk
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
,
375 u32 i2cbus
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
,
377 u32 clk_error_prev
= i2cbus
;
379 u32 af_delay_min
, af_delay_max
;
381 u32 clk_min
, clk_max
;
382 int sdadel_min
, sdadel_max
;
384 struct stm32f7_i2c_timings
*v
, *_v
, *s
;
385 struct list_head solutions
;
389 if (setup
->speed
>= STM32_I2C_SPEED_END
) {
390 dev_err(i2c_dev
->dev
, "speed out of bound {%d/%d}\n",
391 setup
->speed
, STM32_I2C_SPEED_END
- 1);
395 if ((setup
->rise_time
> i2c_specs
[setup
->speed
].rise_max
) ||
396 (setup
->fall_time
> i2c_specs
[setup
->speed
].fall_max
)) {
397 dev_err(i2c_dev
->dev
,
398 "timings out of bound Rise{%d>%d}/Fall{%d>%d}\n",
399 setup
->rise_time
, i2c_specs
[setup
->speed
].rise_max
,
400 setup
->fall_time
, i2c_specs
[setup
->speed
].fall_max
);
404 if (setup
->dnf
> STM32F7_I2C_DNF_MAX
) {
405 dev_err(i2c_dev
->dev
,
406 "DNF out of bound %d/%d\n",
407 setup
->dnf
, STM32F7_I2C_DNF_MAX
);
411 if (setup
->speed_freq
> i2c_specs
[setup
->speed
].rate
) {
412 dev_err(i2c_dev
->dev
, "ERROR: Freq {%d/%d}\n",
413 setup
->speed_freq
, i2c_specs
[setup
->speed
].rate
);
417 /* Analog and Digital Filters */
419 (setup
->analog_filter
?
420 STM32F7_I2C_ANALOG_FILTER_DELAY_MIN
: 0);
422 (setup
->analog_filter
?
423 STM32F7_I2C_ANALOG_FILTER_DELAY_MAX
: 0);
424 dnf_delay
= setup
->dnf
* i2cclk
;
426 sdadel_min
= i2c_specs
[setup
->speed
].hddat_min
+ setup
->fall_time
-
427 af_delay_min
- (setup
->dnf
+ 3) * i2cclk
;
429 sdadel_max
= i2c_specs
[setup
->speed
].vddat_max
- setup
->rise_time
-
430 af_delay_max
- (setup
->dnf
+ 4) * i2cclk
;
432 scldel_min
= setup
->rise_time
+ i2c_specs
[setup
->speed
].sudat_min
;
439 dev_dbg(i2c_dev
->dev
, "SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n",
440 sdadel_min
, sdadel_max
, scldel_min
);
442 INIT_LIST_HEAD(&solutions
);
443 /* Compute possible values for PRESC, SCLDEL and SDADEL */
444 for (p
= 0; p
< STM32F7_PRESC_MAX
; p
++) {
445 for (l
= 0; l
< STM32F7_SCLDEL_MAX
; l
++) {
446 u32 scldel
= (l
+ 1) * (p
+ 1) * i2cclk
;
448 if (scldel
< scldel_min
)
451 for (a
= 0; a
< STM32F7_SDADEL_MAX
; a
++) {
452 u32 sdadel
= (a
* (p
+ 1) + 1) * i2cclk
;
454 if (((sdadel
>= sdadel_min
) &&
455 (sdadel
<= sdadel_max
)) &&
457 v
= kmalloc(sizeof(*v
), GFP_KERNEL
);
468 list_add_tail(&v
->node
,
475 if (list_empty(&solutions
)) {
476 dev_err(i2c_dev
->dev
, "no Prescaler solution\n");
481 tsync
= af_delay_min
+ dnf_delay
+ (2 * i2cclk
);
483 clk_max
= NSEC_PER_SEC
/ i2c_specs
[setup
->speed
].rate_min
;
484 clk_min
= NSEC_PER_SEC
/ i2c_specs
[setup
->speed
].rate_max
;
487 * Among Prescaler possibilities discovered above figures out SCL Low
488 * and High Period. Provided:
489 * - SCL Low Period has to be higher than SCL Clock Low Period
490 * defined by I2C Specification. I2C Clock has to be lower than
491 * (SCL Low Period - Analog/Digital filters) / 4.
492 * - SCL High Period has to be lower than SCL Clock High Period
493 * defined by I2C Specification
494 * - I2C Clock has to be lower than SCL High Period
496 list_for_each_entry(v
, &solutions
, node
) {
497 u32 prescaler
= (v
->presc
+ 1) * i2cclk
;
499 for (l
= 0; l
< STM32F7_SCLL_MAX
; l
++) {
500 u32 tscl_l
= (l
+ 1) * prescaler
+ tsync
;
502 if ((tscl_l
< i2c_specs
[setup
->speed
].l_min
) ||
504 ((tscl_l
- af_delay_min
- dnf_delay
) / 4))) {
508 for (h
= 0; h
< STM32F7_SCLH_MAX
; h
++) {
509 u32 tscl_h
= (h
+ 1) * prescaler
+ tsync
;
510 u32 tscl
= tscl_l
+ tscl_h
+
511 setup
->rise_time
+ setup
->fall_time
;
513 if ((tscl
>= clk_min
) && (tscl
<= clk_max
) &&
514 (tscl_h
>= i2c_specs
[setup
->speed
].h_min
) &&
516 int clk_error
= tscl
- i2cbus
;
519 clk_error
= -clk_error
;
521 if (clk_error
< clk_error_prev
) {
522 clk_error_prev
= clk_error
;
533 dev_err(i2c_dev
->dev
, "no solution at all\n");
538 output
->presc
= s
->presc
;
539 output
->scldel
= s
->scldel
;
540 output
->sdadel
= s
->sdadel
;
541 output
->scll
= s
->scll
;
542 output
->sclh
= s
->sclh
;
544 dev_dbg(i2c_dev
->dev
,
545 "Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n",
547 output
->scldel
, output
->sdadel
,
548 output
->scll
, output
->sclh
);
551 /* Release list and memory */
552 list_for_each_entry_safe(v
, _v
, &solutions
, node
) {
560 static int stm32f7_i2c_setup_timing(struct stm32f7_i2c_dev
*i2c_dev
,
561 struct stm32f7_i2c_setup
*setup
)
565 setup
->speed
= i2c_dev
->speed
;
566 setup
->speed_freq
= i2c_specs
[setup
->speed
].rate
;
567 setup
->clock_src
= clk_get_rate(i2c_dev
->clk
);
569 if (!setup
->clock_src
) {
570 dev_err(i2c_dev
->dev
, "clock rate is 0\n");
575 ret
= stm32f7_i2c_compute_timing(i2c_dev
, setup
,
578 dev_err(i2c_dev
->dev
,
579 "failed to compute I2C timings.\n");
580 if (i2c_dev
->speed
> STM32_I2C_SPEED_STANDARD
) {
582 setup
->speed
= i2c_dev
->speed
;
584 i2c_specs
[setup
->speed
].rate
;
585 dev_warn(i2c_dev
->dev
,
586 "downgrade I2C Speed Freq to (%i)\n",
587 i2c_specs
[setup
->speed
].rate
);
595 dev_err(i2c_dev
->dev
, "Impossible to compute I2C timings.\n");
599 dev_dbg(i2c_dev
->dev
, "I2C Speed(%i), Freq(%i), Clk Source(%i)\n",
600 setup
->speed
, setup
->speed_freq
, setup
->clock_src
);
601 dev_dbg(i2c_dev
->dev
, "I2C Rise(%i) and Fall(%i) Time\n",
602 setup
->rise_time
, setup
->fall_time
);
603 dev_dbg(i2c_dev
->dev
, "I2C Analog Filter(%s), DNF(%i)\n",
604 (setup
->analog_filter
? "On" : "Off"), setup
->dnf
);
609 static void stm32f7_i2c_disable_dma_req(struct stm32f7_i2c_dev
*i2c_dev
)
611 void __iomem
*base
= i2c_dev
->base
;
612 u32 mask
= STM32F7_I2C_CR1_RXDMAEN
| STM32F7_I2C_CR1_TXDMAEN
;
614 stm32f7_i2c_clr_bits(base
+ STM32F7_I2C_CR1
, mask
);
617 static void stm32f7_i2c_dma_callback(void *arg
)
619 struct stm32f7_i2c_dev
*i2c_dev
= (struct stm32f7_i2c_dev
*)arg
;
620 struct stm32_i2c_dma
*dma
= i2c_dev
->dma
;
621 struct device
*dev
= dma
->chan_using
->device
->dev
;
623 stm32f7_i2c_disable_dma_req(i2c_dev
);
624 dma_unmap_single(dev
, dma
->dma_buf
, dma
->dma_len
, dma
->dma_data_dir
);
625 complete(&dma
->dma_complete
);
628 static void stm32f7_i2c_hw_config(struct stm32f7_i2c_dev
*i2c_dev
)
630 struct stm32f7_i2c_timings
*t
= &i2c_dev
->timing
;
633 /* Timing settings */
634 timing
|= STM32F7_I2C_TIMINGR_PRESC(t
->presc
);
635 timing
|= STM32F7_I2C_TIMINGR_SCLDEL(t
->scldel
);
636 timing
|= STM32F7_I2C_TIMINGR_SDADEL(t
->sdadel
);
637 timing
|= STM32F7_I2C_TIMINGR_SCLH(t
->sclh
);
638 timing
|= STM32F7_I2C_TIMINGR_SCLL(t
->scll
);
639 writel_relaxed(timing
, i2c_dev
->base
+ STM32F7_I2C_TIMINGR
);
642 if (i2c_dev
->setup
.analog_filter
)
643 stm32f7_i2c_clr_bits(i2c_dev
->base
+ STM32F7_I2C_CR1
,
644 STM32F7_I2C_CR1_ANFOFF
);
646 stm32f7_i2c_set_bits(i2c_dev
->base
+ STM32F7_I2C_CR1
,
647 STM32F7_I2C_CR1_ANFOFF
);
648 stm32f7_i2c_set_bits(i2c_dev
->base
+ STM32F7_I2C_CR1
,
652 static void stm32f7_i2c_write_tx_data(struct stm32f7_i2c_dev
*i2c_dev
)
654 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
655 void __iomem
*base
= i2c_dev
->base
;
658 writeb_relaxed(*f7_msg
->buf
++, base
+ STM32F7_I2C_TXDR
);
663 static void stm32f7_i2c_read_rx_data(struct stm32f7_i2c_dev
*i2c_dev
)
665 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
666 void __iomem
*base
= i2c_dev
->base
;
669 *f7_msg
->buf
++ = readb_relaxed(base
+ STM32F7_I2C_RXDR
);
672 /* Flush RX buffer has no data is expected */
673 readb_relaxed(base
+ STM32F7_I2C_RXDR
);
677 static void stm32f7_i2c_reload(struct stm32f7_i2c_dev
*i2c_dev
)
679 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
682 if (i2c_dev
->use_dma
)
683 f7_msg
->count
-= STM32F7_I2C_MAX_LEN
;
685 cr2
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_CR2
);
687 cr2
&= ~STM32F7_I2C_CR2_NBYTES_MASK
;
688 if (f7_msg
->count
> STM32F7_I2C_MAX_LEN
) {
689 cr2
|= STM32F7_I2C_CR2_NBYTES(STM32F7_I2C_MAX_LEN
);
691 cr2
&= ~STM32F7_I2C_CR2_RELOAD
;
692 cr2
|= STM32F7_I2C_CR2_NBYTES(f7_msg
->count
);
695 writel_relaxed(cr2
, i2c_dev
->base
+ STM32F7_I2C_CR2
);
698 static void stm32f7_i2c_smbus_reload(struct stm32f7_i2c_dev
*i2c_dev
)
700 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
705 * For I2C_SMBUS_BLOCK_DATA && I2C_SMBUS_BLOCK_PROC_CALL, the first
706 * data received inform us how many data will follow.
708 stm32f7_i2c_read_rx_data(i2c_dev
);
711 * Update NBYTES with the value read to continue the transfer
713 val
= f7_msg
->buf
- sizeof(u8
);
714 f7_msg
->count
= *val
;
715 cr2
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_CR2
);
716 cr2
&= ~(STM32F7_I2C_CR2_NBYTES_MASK
| STM32F7_I2C_CR2_RELOAD
);
717 cr2
|= STM32F7_I2C_CR2_NBYTES(f7_msg
->count
);
718 writel_relaxed(cr2
, i2c_dev
->base
+ STM32F7_I2C_CR2
);
721 static int stm32f7_i2c_release_bus(struct i2c_adapter
*i2c_adap
)
723 struct stm32f7_i2c_dev
*i2c_dev
= i2c_get_adapdata(i2c_adap
);
725 dev_info(i2c_dev
->dev
, "Trying to recover bus\n");
727 stm32f7_i2c_clr_bits(i2c_dev
->base
+ STM32F7_I2C_CR1
,
730 stm32f7_i2c_hw_config(i2c_dev
);
735 static int stm32f7_i2c_wait_free_bus(struct stm32f7_i2c_dev
*i2c_dev
)
740 ret
= readl_relaxed_poll_timeout(i2c_dev
->base
+ STM32F7_I2C_ISR
,
742 !(status
& STM32F7_I2C_ISR_BUSY
),
747 dev_info(i2c_dev
->dev
, "bus busy\n");
749 ret
= stm32f7_i2c_release_bus(&i2c_dev
->adap
);
751 dev_err(i2c_dev
->dev
, "Failed to recover the bus (%d)\n", ret
);
758 static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev
*i2c_dev
,
761 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
762 void __iomem
*base
= i2c_dev
->base
;
766 f7_msg
->addr
= msg
->addr
;
767 f7_msg
->buf
= msg
->buf
;
768 f7_msg
->count
= msg
->len
;
770 f7_msg
->stop
= (i2c_dev
->msg_id
>= i2c_dev
->msg_num
- 1);
772 reinit_completion(&i2c_dev
->complete
);
774 cr1
= readl_relaxed(base
+ STM32F7_I2C_CR1
);
775 cr2
= readl_relaxed(base
+ STM32F7_I2C_CR2
);
777 /* Set transfer direction */
778 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
779 if (msg
->flags
& I2C_M_RD
)
780 cr2
|= STM32F7_I2C_CR2_RD_WRN
;
782 /* Set slave address */
783 cr2
&= ~(STM32F7_I2C_CR2_HEAD10R
| STM32F7_I2C_CR2_ADD10
);
784 if (msg
->flags
& I2C_M_TEN
) {
785 cr2
&= ~STM32F7_I2C_CR2_SADD10_MASK
;
786 cr2
|= STM32F7_I2C_CR2_SADD10(f7_msg
->addr
);
787 cr2
|= STM32F7_I2C_CR2_ADD10
;
789 cr2
&= ~STM32F7_I2C_CR2_SADD7_MASK
;
790 cr2
|= STM32F7_I2C_CR2_SADD7(f7_msg
->addr
);
793 /* Set nb bytes to transfer and reload if needed */
794 cr2
&= ~(STM32F7_I2C_CR2_NBYTES_MASK
| STM32F7_I2C_CR2_RELOAD
);
795 if (f7_msg
->count
> STM32F7_I2C_MAX_LEN
) {
796 cr2
|= STM32F7_I2C_CR2_NBYTES(STM32F7_I2C_MAX_LEN
);
797 cr2
|= STM32F7_I2C_CR2_RELOAD
;
799 cr2
|= STM32F7_I2C_CR2_NBYTES(f7_msg
->count
);
802 /* Enable NACK, STOP, error and transfer complete interrupts */
803 cr1
|= STM32F7_I2C_CR1_ERRIE
| STM32F7_I2C_CR1_TCIE
|
804 STM32F7_I2C_CR1_STOPIE
| STM32F7_I2C_CR1_NACKIE
;
806 /* Clear DMA req and TX/RX interrupt */
807 cr1
&= ~(STM32F7_I2C_CR1_RXIE
| STM32F7_I2C_CR1_TXIE
|
808 STM32F7_I2C_CR1_RXDMAEN
| STM32F7_I2C_CR1_TXDMAEN
);
810 /* Configure DMA or enable RX/TX interrupt */
811 i2c_dev
->use_dma
= false;
812 if (i2c_dev
->dma
&& f7_msg
->count
>= STM32F7_I2C_DMA_LEN_MIN
) {
813 ret
= stm32_i2c_prep_dma_xfer(i2c_dev
->dev
, i2c_dev
->dma
,
814 msg
->flags
& I2C_M_RD
,
815 f7_msg
->count
, f7_msg
->buf
,
816 stm32f7_i2c_dma_callback
,
819 i2c_dev
->use_dma
= true;
821 dev_warn(i2c_dev
->dev
, "can't use DMA\n");
824 if (!i2c_dev
->use_dma
) {
825 if (msg
->flags
& I2C_M_RD
)
826 cr1
|= STM32F7_I2C_CR1_RXIE
;
828 cr1
|= STM32F7_I2C_CR1_TXIE
;
830 if (msg
->flags
& I2C_M_RD
)
831 cr1
|= STM32F7_I2C_CR1_RXDMAEN
;
833 cr1
|= STM32F7_I2C_CR1_TXDMAEN
;
836 /* Configure Start/Repeated Start */
837 cr2
|= STM32F7_I2C_CR2_START
;
839 i2c_dev
->master_mode
= true;
841 /* Write configurations registers */
842 writel_relaxed(cr1
, base
+ STM32F7_I2C_CR1
);
843 writel_relaxed(cr2
, base
+ STM32F7_I2C_CR2
);
846 static int stm32f7_i2c_smbus_xfer_msg(struct stm32f7_i2c_dev
*i2c_dev
,
847 unsigned short flags
, u8 command
,
848 union i2c_smbus_data
*data
)
850 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
851 struct device
*dev
= i2c_dev
->dev
;
852 void __iomem
*base
= i2c_dev
->base
;
857 reinit_completion(&i2c_dev
->complete
);
859 cr2
= readl_relaxed(base
+ STM32F7_I2C_CR2
);
860 cr1
= readl_relaxed(base
+ STM32F7_I2C_CR1
);
862 /* Set transfer direction */
863 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
864 if (f7_msg
->read_write
)
865 cr2
|= STM32F7_I2C_CR2_RD_WRN
;
867 /* Set slave address */
868 cr2
&= ~(STM32F7_I2C_CR2_ADD10
| STM32F7_I2C_CR2_SADD7_MASK
);
869 cr2
|= STM32F7_I2C_CR2_SADD7(f7_msg
->addr
);
871 f7_msg
->smbus_buf
[0] = command
;
872 switch (f7_msg
->size
) {
873 case I2C_SMBUS_QUICK
:
881 case I2C_SMBUS_BYTE_DATA
:
882 if (f7_msg
->read_write
) {
883 f7_msg
->stop
= false;
885 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
889 f7_msg
->smbus_buf
[1] = data
->byte
;
892 case I2C_SMBUS_WORD_DATA
:
893 if (f7_msg
->read_write
) {
894 f7_msg
->stop
= false;
896 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
900 f7_msg
->smbus_buf
[1] = data
->word
& 0xff;
901 f7_msg
->smbus_buf
[2] = data
->word
>> 8;
904 case I2C_SMBUS_BLOCK_DATA
:
905 if (f7_msg
->read_write
) {
906 f7_msg
->stop
= false;
908 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
911 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
||
913 dev_err(dev
, "Invalid block write size %d\n",
917 f7_msg
->count
= data
->block
[0] + 2;
918 for (i
= 1; i
< f7_msg
->count
; i
++)
919 f7_msg
->smbus_buf
[i
] = data
->block
[i
- 1];
922 case I2C_SMBUS_PROC_CALL
:
923 f7_msg
->stop
= false;
925 f7_msg
->smbus_buf
[1] = data
->word
& 0xff;
926 f7_msg
->smbus_buf
[2] = data
->word
>> 8;
927 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
928 f7_msg
->read_write
= I2C_SMBUS_READ
;
930 case I2C_SMBUS_BLOCK_PROC_CALL
:
931 f7_msg
->stop
= false;
932 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
- 1) {
933 dev_err(dev
, "Invalid block write size %d\n",
937 f7_msg
->count
= data
->block
[0] + 2;
938 for (i
= 1; i
< f7_msg
->count
; i
++)
939 f7_msg
->smbus_buf
[i
] = data
->block
[i
- 1];
940 cr2
&= ~STM32F7_I2C_CR2_RD_WRN
;
941 f7_msg
->read_write
= I2C_SMBUS_READ
;
944 dev_err(dev
, "Unsupported smbus protocol %d\n", f7_msg
->size
);
948 f7_msg
->buf
= f7_msg
->smbus_buf
;
951 if ((flags
& I2C_CLIENT_PEC
) && f7_msg
->size
!= I2C_SMBUS_QUICK
) {
952 cr1
|= STM32F7_I2C_CR1_PECEN
;
953 cr2
|= STM32F7_I2C_CR2_PECBYTE
;
954 if (!f7_msg
->read_write
)
957 cr1
&= ~STM32F7_I2C_CR1_PECEN
;
958 cr2
&= ~STM32F7_I2C_CR2_PECBYTE
;
961 /* Set number of bytes to be transferred */
962 cr2
&= ~(STM32F7_I2C_CR2_NBYTES_MASK
| STM32F7_I2C_CR2_RELOAD
);
963 cr2
|= STM32F7_I2C_CR2_NBYTES(f7_msg
->count
);
965 /* Enable NACK, STOP, error and transfer complete interrupts */
966 cr1
|= STM32F7_I2C_CR1_ERRIE
| STM32F7_I2C_CR1_TCIE
|
967 STM32F7_I2C_CR1_STOPIE
| STM32F7_I2C_CR1_NACKIE
;
969 /* Clear DMA req and TX/RX interrupt */
970 cr1
&= ~(STM32F7_I2C_CR1_RXIE
| STM32F7_I2C_CR1_TXIE
|
971 STM32F7_I2C_CR1_RXDMAEN
| STM32F7_I2C_CR1_TXDMAEN
);
973 /* Configure DMA or enable RX/TX interrupt */
974 i2c_dev
->use_dma
= false;
975 if (i2c_dev
->dma
&& f7_msg
->count
>= STM32F7_I2C_DMA_LEN_MIN
) {
976 ret
= stm32_i2c_prep_dma_xfer(i2c_dev
->dev
, i2c_dev
->dma
,
977 cr2
& STM32F7_I2C_CR2_RD_WRN
,
978 f7_msg
->count
, f7_msg
->buf
,
979 stm32f7_i2c_dma_callback
,
982 i2c_dev
->use_dma
= true;
984 dev_warn(i2c_dev
->dev
, "can't use DMA\n");
987 if (!i2c_dev
->use_dma
) {
988 if (cr2
& STM32F7_I2C_CR2_RD_WRN
)
989 cr1
|= STM32F7_I2C_CR1_RXIE
;
991 cr1
|= STM32F7_I2C_CR1_TXIE
;
993 if (cr2
& STM32F7_I2C_CR2_RD_WRN
)
994 cr1
|= STM32F7_I2C_CR1_RXDMAEN
;
996 cr1
|= STM32F7_I2C_CR1_TXDMAEN
;
1000 cr2
|= STM32F7_I2C_CR2_START
;
1002 i2c_dev
->master_mode
= true;
1004 /* Write configurations registers */
1005 writel_relaxed(cr1
, base
+ STM32F7_I2C_CR1
);
1006 writel_relaxed(cr2
, base
+ STM32F7_I2C_CR2
);
1011 static void stm32f7_i2c_smbus_rep_start(struct stm32f7_i2c_dev
*i2c_dev
)
1013 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1014 void __iomem
*base
= i2c_dev
->base
;
1018 cr2
= readl_relaxed(base
+ STM32F7_I2C_CR2
);
1019 cr1
= readl_relaxed(base
+ STM32F7_I2C_CR1
);
1021 /* Set transfer direction */
1022 cr2
|= STM32F7_I2C_CR2_RD_WRN
;
1024 switch (f7_msg
->size
) {
1025 case I2C_SMBUS_BYTE_DATA
:
1028 case I2C_SMBUS_WORD_DATA
:
1029 case I2C_SMBUS_PROC_CALL
:
1032 case I2C_SMBUS_BLOCK_DATA
:
1033 case I2C_SMBUS_BLOCK_PROC_CALL
:
1035 cr2
|= STM32F7_I2C_CR2_RELOAD
;
1039 f7_msg
->buf
= f7_msg
->smbus_buf
;
1040 f7_msg
->stop
= true;
1042 /* Add one byte for PEC if needed */
1043 if (cr1
& STM32F7_I2C_CR1_PECEN
)
1046 /* Set number of bytes to be transferred */
1047 cr2
&= ~(STM32F7_I2C_CR2_NBYTES_MASK
);
1048 cr2
|= STM32F7_I2C_CR2_NBYTES(f7_msg
->count
);
1051 * Configure RX/TX interrupt:
1053 cr1
&= ~(STM32F7_I2C_CR1_RXIE
| STM32F7_I2C_CR1_TXIE
);
1054 cr1
|= STM32F7_I2C_CR1_RXIE
;
1057 * Configure DMA or enable RX/TX interrupt:
1058 * For I2C_SMBUS_BLOCK_DATA and I2C_SMBUS_BLOCK_PROC_CALL we don't use
1059 * dma as we don't know in advance how many data will be received
1061 cr1
&= ~(STM32F7_I2C_CR1_RXIE
| STM32F7_I2C_CR1_TXIE
|
1062 STM32F7_I2C_CR1_RXDMAEN
| STM32F7_I2C_CR1_TXDMAEN
);
1064 i2c_dev
->use_dma
= false;
1065 if (i2c_dev
->dma
&& f7_msg
->count
>= STM32F7_I2C_DMA_LEN_MIN
&&
1066 f7_msg
->size
!= I2C_SMBUS_BLOCK_DATA
&&
1067 f7_msg
->size
!= I2C_SMBUS_BLOCK_PROC_CALL
) {
1068 ret
= stm32_i2c_prep_dma_xfer(i2c_dev
->dev
, i2c_dev
->dma
,
1069 cr2
& STM32F7_I2C_CR2_RD_WRN
,
1070 f7_msg
->count
, f7_msg
->buf
,
1071 stm32f7_i2c_dma_callback
,
1075 i2c_dev
->use_dma
= true;
1077 dev_warn(i2c_dev
->dev
, "can't use DMA\n");
1080 if (!i2c_dev
->use_dma
)
1081 cr1
|= STM32F7_I2C_CR1_RXIE
;
1083 cr1
|= STM32F7_I2C_CR1_RXDMAEN
;
1085 /* Configure Repeated Start */
1086 cr2
|= STM32F7_I2C_CR2_START
;
1088 /* Write configurations registers */
1089 writel_relaxed(cr1
, base
+ STM32F7_I2C_CR1
);
1090 writel_relaxed(cr2
, base
+ STM32F7_I2C_CR2
);
1093 static int stm32f7_i2c_smbus_check_pec(struct stm32f7_i2c_dev
*i2c_dev
)
1095 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1096 u8 count
, internal_pec
, received_pec
;
1098 internal_pec
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_PECR
);
1100 switch (f7_msg
->size
) {
1101 case I2C_SMBUS_BYTE
:
1102 case I2C_SMBUS_BYTE_DATA
:
1103 received_pec
= f7_msg
->smbus_buf
[1];
1105 case I2C_SMBUS_WORD_DATA
:
1106 case I2C_SMBUS_PROC_CALL
:
1107 received_pec
= f7_msg
->smbus_buf
[2];
1109 case I2C_SMBUS_BLOCK_DATA
:
1110 case I2C_SMBUS_BLOCK_PROC_CALL
:
1111 count
= f7_msg
->smbus_buf
[0];
1112 received_pec
= f7_msg
->smbus_buf
[count
];
1115 dev_err(i2c_dev
->dev
, "Unsupported smbus protocol for PEC\n");
1119 if (internal_pec
!= received_pec
) {
1120 dev_err(i2c_dev
->dev
, "Bad PEC 0x%02x vs. 0x%02x\n",
1121 internal_pec
, received_pec
);
1128 static bool stm32f7_i2c_is_addr_match(struct i2c_client
*slave
, u32 addcode
)
1135 if (slave
->flags
& I2C_CLIENT_TEN
) {
1137 * For 10-bit addr, addcode = 11110XY with
1138 * X = Bit 9 of slave address
1139 * Y = Bit 8 of slave address
1141 addr
= slave
->addr
>> 8;
1143 if (addr
== addcode
)
1146 addr
= slave
->addr
& 0x7f;
1147 if (addr
== addcode
)
1154 static void stm32f7_i2c_slave_start(struct stm32f7_i2c_dev
*i2c_dev
)
1156 struct i2c_client
*slave
= i2c_dev
->slave_running
;
1157 void __iomem
*base
= i2c_dev
->base
;
1161 if (i2c_dev
->slave_dir
) {
1162 /* Notify i2c slave that new read transfer is starting */
1163 i2c_slave_event(slave
, I2C_SLAVE_READ_REQUESTED
, &value
);
1166 * Disable slave TX config in case of I2C combined message
1167 * (I2C Write followed by I2C Read)
1169 mask
= STM32F7_I2C_CR2_RELOAD
;
1170 stm32f7_i2c_clr_bits(base
+ STM32F7_I2C_CR2
, mask
);
1171 mask
= STM32F7_I2C_CR1_SBC
| STM32F7_I2C_CR1_RXIE
|
1172 STM32F7_I2C_CR1_TCIE
;
1173 stm32f7_i2c_clr_bits(base
+ STM32F7_I2C_CR1
, mask
);
1175 /* Enable TX empty, STOP, NACK interrupts */
1176 mask
= STM32F7_I2C_CR1_STOPIE
| STM32F7_I2C_CR1_NACKIE
|
1177 STM32F7_I2C_CR1_TXIE
;
1178 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_CR1
, mask
);
1180 /* Write 1st data byte */
1181 writel_relaxed(value
, base
+ STM32F7_I2C_TXDR
);
1183 /* Notify i2c slave that new write transfer is starting */
1184 i2c_slave_event(slave
, I2C_SLAVE_WRITE_REQUESTED
, &value
);
1186 /* Set reload mode to be able to ACK/NACK each received byte */
1187 mask
= STM32F7_I2C_CR2_RELOAD
;
1188 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_CR2
, mask
);
1191 * Set STOP, NACK, RX empty and transfer complete interrupts.*
1192 * Set Slave Byte Control to be able to ACK/NACK each data
1195 mask
= STM32F7_I2C_CR1_STOPIE
| STM32F7_I2C_CR1_NACKIE
|
1196 STM32F7_I2C_CR1_SBC
| STM32F7_I2C_CR1_RXIE
|
1197 STM32F7_I2C_CR1_TCIE
;
1198 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_CR1
, mask
);
1202 static void stm32f7_i2c_slave_addr(struct stm32f7_i2c_dev
*i2c_dev
)
1204 void __iomem
*base
= i2c_dev
->base
;
1205 u32 isr
, addcode
, dir
, mask
;
1208 isr
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_ISR
);
1209 addcode
= STM32F7_I2C_ISR_ADDCODE_GET(isr
);
1210 dir
= isr
& STM32F7_I2C_ISR_DIR
;
1212 for (i
= 0; i
< STM32F7_I2C_MAX_SLAVE
; i
++) {
1213 if (stm32f7_i2c_is_addr_match(i2c_dev
->slave
[i
], addcode
)) {
1214 i2c_dev
->slave_running
= i2c_dev
->slave
[i
];
1215 i2c_dev
->slave_dir
= dir
;
1217 /* Start I2C slave processing */
1218 stm32f7_i2c_slave_start(i2c_dev
);
1220 /* Clear ADDR flag */
1221 mask
= STM32F7_I2C_ICR_ADDRCF
;
1222 writel_relaxed(mask
, base
+ STM32F7_I2C_ICR
);
1228 static int stm32f7_i2c_get_slave_id(struct stm32f7_i2c_dev
*i2c_dev
,
1229 struct i2c_client
*slave
, int *id
)
1233 for (i
= 0; i
< STM32F7_I2C_MAX_SLAVE
; i
++) {
1234 if (i2c_dev
->slave
[i
] == slave
) {
1240 dev_err(i2c_dev
->dev
, "Slave 0x%x not registered\n", slave
->addr
);
1245 static int stm32f7_i2c_get_free_slave_id(struct stm32f7_i2c_dev
*i2c_dev
,
1246 struct i2c_client
*slave
, int *id
)
1248 struct device
*dev
= i2c_dev
->dev
;
1252 * slave[0] supports 7-bit and 10-bit slave address
1253 * slave[1] supports 7-bit slave address only
1255 for (i
= STM32F7_I2C_MAX_SLAVE
- 1; i
>= 0; i
--) {
1256 if (i
== 1 && (slave
->flags
& I2C_CLIENT_TEN
))
1258 if (!i2c_dev
->slave
[i
]) {
1264 dev_err(dev
, "Slave 0x%x could not be registered\n", slave
->addr
);
1269 static bool stm32f7_i2c_is_slave_registered(struct stm32f7_i2c_dev
*i2c_dev
)
1273 for (i
= 0; i
< STM32F7_I2C_MAX_SLAVE
; i
++) {
1274 if (i2c_dev
->slave
[i
])
1281 static bool stm32f7_i2c_is_slave_busy(struct stm32f7_i2c_dev
*i2c_dev
)
1286 for (i
= 0; i
< STM32F7_I2C_MAX_SLAVE
; i
++) {
1287 if (i2c_dev
->slave
[i
])
1294 static irqreturn_t
stm32f7_i2c_slave_isr_event(struct stm32f7_i2c_dev
*i2c_dev
)
1296 void __iomem
*base
= i2c_dev
->base
;
1297 u32 cr2
, status
, mask
;
1301 status
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_ISR
);
1303 /* Slave transmitter mode */
1304 if (status
& STM32F7_I2C_ISR_TXIS
) {
1305 i2c_slave_event(i2c_dev
->slave_running
,
1306 I2C_SLAVE_READ_PROCESSED
,
1309 /* Write data byte */
1310 writel_relaxed(val
, base
+ STM32F7_I2C_TXDR
);
1313 /* Transfer Complete Reload for Slave receiver mode */
1314 if (status
& STM32F7_I2C_ISR_TCR
|| status
& STM32F7_I2C_ISR_RXNE
) {
1316 * Read data byte then set NBYTES to receive next byte or NACK
1317 * the current received byte
1319 val
= readb_relaxed(i2c_dev
->base
+ STM32F7_I2C_RXDR
);
1320 ret
= i2c_slave_event(i2c_dev
->slave_running
,
1321 I2C_SLAVE_WRITE_RECEIVED
,
1324 cr2
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_CR2
);
1325 cr2
|= STM32F7_I2C_CR2_NBYTES(1);
1326 writel_relaxed(cr2
, i2c_dev
->base
+ STM32F7_I2C_CR2
);
1328 mask
= STM32F7_I2C_CR2_NACK
;
1329 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_CR2
, mask
);
1334 if (status
& STM32F7_I2C_ISR_NACKF
) {
1335 dev_dbg(i2c_dev
->dev
, "<%s>: Receive NACK\n", __func__
);
1336 writel_relaxed(STM32F7_I2C_ICR_NACKCF
, base
+ STM32F7_I2C_ICR
);
1340 if (status
& STM32F7_I2C_ISR_STOPF
) {
1341 /* Disable interrupts */
1342 stm32f7_i2c_disable_irq(i2c_dev
, STM32F7_I2C_XFER_IRQ_MASK
);
1344 if (i2c_dev
->slave_dir
) {
1346 * Flush TX buffer in order to not used the byte in
1347 * TXDR for the next transfer
1349 mask
= STM32F7_I2C_ISR_TXE
;
1350 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_ISR
, mask
);
1353 /* Clear STOP flag */
1354 writel_relaxed(STM32F7_I2C_ICR_STOPCF
, base
+ STM32F7_I2C_ICR
);
1356 /* Notify i2c slave that a STOP flag has been detected */
1357 i2c_slave_event(i2c_dev
->slave_running
, I2C_SLAVE_STOP
, &val
);
1359 i2c_dev
->slave_running
= NULL
;
1362 /* Address match received */
1363 if (status
& STM32F7_I2C_ISR_ADDR
)
1364 stm32f7_i2c_slave_addr(i2c_dev
);
1369 static irqreturn_t
stm32f7_i2c_isr_event(int irq
, void *data
)
1371 struct stm32f7_i2c_dev
*i2c_dev
= data
;
1372 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1373 void __iomem
*base
= i2c_dev
->base
;
1375 int ret
= IRQ_HANDLED
;
1377 /* Check if the interrupt if for a slave device */
1378 if (!i2c_dev
->master_mode
) {
1379 ret
= stm32f7_i2c_slave_isr_event(i2c_dev
);
1383 status
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_ISR
);
1386 if (status
& STM32F7_I2C_ISR_TXIS
)
1387 stm32f7_i2c_write_tx_data(i2c_dev
);
1390 if (status
& STM32F7_I2C_ISR_RXNE
)
1391 stm32f7_i2c_read_rx_data(i2c_dev
);
1394 if (status
& STM32F7_I2C_ISR_NACKF
) {
1395 dev_dbg(i2c_dev
->dev
, "<%s>: Receive NACK\n", __func__
);
1396 writel_relaxed(STM32F7_I2C_ICR_NACKCF
, base
+ STM32F7_I2C_ICR
);
1397 f7_msg
->result
= -ENXIO
;
1400 /* STOP detection flag */
1401 if (status
& STM32F7_I2C_ISR_STOPF
) {
1402 /* Disable interrupts */
1403 if (stm32f7_i2c_is_slave_registered(i2c_dev
))
1404 mask
= STM32F7_I2C_XFER_IRQ_MASK
;
1406 mask
= STM32F7_I2C_ALL_IRQ_MASK
;
1407 stm32f7_i2c_disable_irq(i2c_dev
, mask
);
1409 /* Clear STOP flag */
1410 writel_relaxed(STM32F7_I2C_ICR_STOPCF
, base
+ STM32F7_I2C_ICR
);
1412 if (i2c_dev
->use_dma
) {
1413 ret
= IRQ_WAKE_THREAD
;
1415 i2c_dev
->master_mode
= false;
1416 complete(&i2c_dev
->complete
);
1420 /* Transfer complete */
1421 if (status
& STM32F7_I2C_ISR_TC
) {
1423 mask
= STM32F7_I2C_CR2_STOP
;
1424 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_CR2
, mask
);
1425 } else if (i2c_dev
->use_dma
) {
1426 ret
= IRQ_WAKE_THREAD
;
1427 } else if (f7_msg
->smbus
) {
1428 stm32f7_i2c_smbus_rep_start(i2c_dev
);
1432 stm32f7_i2c_xfer_msg(i2c_dev
, i2c_dev
->msg
);
1436 if (status
& STM32F7_I2C_ISR_TCR
) {
1438 stm32f7_i2c_smbus_reload(i2c_dev
);
1440 stm32f7_i2c_reload(i2c_dev
);
1446 static irqreturn_t
stm32f7_i2c_isr_event_thread(int irq
, void *data
)
1448 struct stm32f7_i2c_dev
*i2c_dev
= data
;
1449 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1450 struct stm32_i2c_dma
*dma
= i2c_dev
->dma
;
1455 * Wait for dma transfer completion before sending next message or
1456 * notity the end of xfer to the client
1458 ret
= wait_for_completion_timeout(&i2c_dev
->dma
->dma_complete
, HZ
);
1460 dev_dbg(i2c_dev
->dev
, "<%s>: Timed out\n", __func__
);
1461 stm32f7_i2c_disable_dma_req(i2c_dev
);
1462 dmaengine_terminate_all(dma
->chan_using
);
1463 f7_msg
->result
= -ETIMEDOUT
;
1466 status
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_ISR
);
1468 if (status
& STM32F7_I2C_ISR_TC
) {
1469 if (f7_msg
->smbus
) {
1470 stm32f7_i2c_smbus_rep_start(i2c_dev
);
1474 stm32f7_i2c_xfer_msg(i2c_dev
, i2c_dev
->msg
);
1477 i2c_dev
->master_mode
= false;
1478 complete(&i2c_dev
->complete
);
1484 static irqreturn_t
stm32f7_i2c_isr_error(int irq
, void *data
)
1486 struct stm32f7_i2c_dev
*i2c_dev
= data
;
1487 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1488 void __iomem
*base
= i2c_dev
->base
;
1489 struct device
*dev
= i2c_dev
->dev
;
1490 struct stm32_i2c_dma
*dma
= i2c_dev
->dma
;
1493 status
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_ISR
);
1496 if (status
& STM32F7_I2C_ISR_BERR
) {
1497 dev_err(dev
, "<%s>: Bus error\n", __func__
);
1498 writel_relaxed(STM32F7_I2C_ICR_BERRCF
, base
+ STM32F7_I2C_ICR
);
1499 stm32f7_i2c_release_bus(&i2c_dev
->adap
);
1500 f7_msg
->result
= -EIO
;
1503 /* Arbitration loss */
1504 if (status
& STM32F7_I2C_ISR_ARLO
) {
1505 dev_dbg(dev
, "<%s>: Arbitration loss\n", __func__
);
1506 writel_relaxed(STM32F7_I2C_ICR_ARLOCF
, base
+ STM32F7_I2C_ICR
);
1507 f7_msg
->result
= -EAGAIN
;
1510 if (status
& STM32F7_I2C_ISR_PECERR
) {
1511 dev_err(dev
, "<%s>: PEC error in reception\n", __func__
);
1512 writel_relaxed(STM32F7_I2C_ICR_PECCF
, base
+ STM32F7_I2C_ICR
);
1513 f7_msg
->result
= -EINVAL
;
1516 if (!i2c_dev
->slave_running
) {
1518 /* Disable interrupts */
1519 if (stm32f7_i2c_is_slave_registered(i2c_dev
))
1520 mask
= STM32F7_I2C_XFER_IRQ_MASK
;
1522 mask
= STM32F7_I2C_ALL_IRQ_MASK
;
1523 stm32f7_i2c_disable_irq(i2c_dev
, mask
);
1527 if (i2c_dev
->use_dma
) {
1528 stm32f7_i2c_disable_dma_req(i2c_dev
);
1529 dmaengine_terminate_all(dma
->chan_using
);
1532 i2c_dev
->master_mode
= false;
1533 complete(&i2c_dev
->complete
);
1538 static int stm32f7_i2c_xfer(struct i2c_adapter
*i2c_adap
,
1539 struct i2c_msg msgs
[], int num
)
1541 struct stm32f7_i2c_dev
*i2c_dev
= i2c_get_adapdata(i2c_adap
);
1542 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1543 struct stm32_i2c_dma
*dma
= i2c_dev
->dma
;
1544 unsigned long time_left
;
1547 i2c_dev
->msg
= msgs
;
1548 i2c_dev
->msg_num
= num
;
1549 i2c_dev
->msg_id
= 0;
1550 f7_msg
->smbus
= false;
1552 ret
= clk_enable(i2c_dev
->clk
);
1554 dev_err(i2c_dev
->dev
, "Failed to enable clock\n");
1558 ret
= stm32f7_i2c_wait_free_bus(i2c_dev
);
1562 stm32f7_i2c_xfer_msg(i2c_dev
, msgs
);
1564 time_left
= wait_for_completion_timeout(&i2c_dev
->complete
,
1565 i2c_dev
->adap
.timeout
);
1566 ret
= f7_msg
->result
;
1569 dev_dbg(i2c_dev
->dev
, "Access to slave 0x%x timed out\n",
1570 i2c_dev
->msg
->addr
);
1571 if (i2c_dev
->use_dma
)
1572 dmaengine_terminate_all(dma
->chan_using
);
1577 clk_disable(i2c_dev
->clk
);
1579 return (ret
< 0) ? ret
: num
;
1582 static int stm32f7_i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
1583 unsigned short flags
, char read_write
,
1584 u8 command
, int size
,
1585 union i2c_smbus_data
*data
)
1587 struct stm32f7_i2c_dev
*i2c_dev
= i2c_get_adapdata(adapter
);
1588 struct stm32f7_i2c_msg
*f7_msg
= &i2c_dev
->f7_msg
;
1589 struct stm32_i2c_dma
*dma
= i2c_dev
->dma
;
1590 struct device
*dev
= i2c_dev
->dev
;
1591 unsigned long timeout
;
1594 f7_msg
->addr
= addr
;
1595 f7_msg
->size
= size
;
1596 f7_msg
->read_write
= read_write
;
1597 f7_msg
->smbus
= true;
1599 ret
= clk_enable(i2c_dev
->clk
);
1601 dev_err(i2c_dev
->dev
, "Failed to enable clock\n");
1605 ret
= stm32f7_i2c_wait_free_bus(i2c_dev
);
1609 ret
= stm32f7_i2c_smbus_xfer_msg(i2c_dev
, flags
, command
, data
);
1613 timeout
= wait_for_completion_timeout(&i2c_dev
->complete
,
1614 i2c_dev
->adap
.timeout
);
1615 ret
= f7_msg
->result
;
1620 dev_dbg(dev
, "Access to slave 0x%x timed out\n", f7_msg
->addr
);
1621 if (i2c_dev
->use_dma
)
1622 dmaengine_terminate_all(dma
->chan_using
);
1628 if ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
&& read_write
) {
1629 ret
= stm32f7_i2c_smbus_check_pec(i2c_dev
);
1634 if (read_write
&& size
!= I2C_SMBUS_QUICK
) {
1636 case I2C_SMBUS_BYTE
:
1637 case I2C_SMBUS_BYTE_DATA
:
1638 data
->byte
= f7_msg
->smbus_buf
[0];
1640 case I2C_SMBUS_WORD_DATA
:
1641 case I2C_SMBUS_PROC_CALL
:
1642 data
->word
= f7_msg
->smbus_buf
[0] |
1643 (f7_msg
->smbus_buf
[1] << 8);
1645 case I2C_SMBUS_BLOCK_DATA
:
1646 case I2C_SMBUS_BLOCK_PROC_CALL
:
1647 for (i
= 0; i
<= f7_msg
->smbus_buf
[0]; i
++)
1648 data
->block
[i
] = f7_msg
->smbus_buf
[i
];
1651 dev_err(dev
, "Unsupported smbus transaction\n");
1657 clk_disable(i2c_dev
->clk
);
1661 static int stm32f7_i2c_reg_slave(struct i2c_client
*slave
)
1663 struct stm32f7_i2c_dev
*i2c_dev
= i2c_get_adapdata(slave
->adapter
);
1664 void __iomem
*base
= i2c_dev
->base
;
1665 struct device
*dev
= i2c_dev
->dev
;
1666 u32 oar1
, oar2
, mask
;
1669 if (slave
->flags
& I2C_CLIENT_PEC
) {
1670 dev_err(dev
, "SMBus PEC not supported in slave mode\n");
1674 if (stm32f7_i2c_is_slave_busy(i2c_dev
)) {
1675 dev_err(dev
, "Too much slave registered\n");
1679 ret
= stm32f7_i2c_get_free_slave_id(i2c_dev
, slave
, &id
);
1683 if (!(stm32f7_i2c_is_slave_registered(i2c_dev
))) {
1684 ret
= clk_enable(i2c_dev
->clk
);
1686 dev_err(dev
, "Failed to enable clock\n");
1692 /* Configure Own Address 1 */
1693 oar1
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_OAR1
);
1694 oar1
&= ~STM32F7_I2C_OAR1_MASK
;
1695 if (slave
->flags
& I2C_CLIENT_TEN
) {
1696 oar1
|= STM32F7_I2C_OAR1_OA1_10(slave
->addr
);
1697 oar1
|= STM32F7_I2C_OAR1_OA1MODE
;
1699 oar1
|= STM32F7_I2C_OAR1_OA1_7(slave
->addr
);
1701 oar1
|= STM32F7_I2C_OAR1_OA1EN
;
1702 i2c_dev
->slave
[id
] = slave
;
1703 writel_relaxed(oar1
, i2c_dev
->base
+ STM32F7_I2C_OAR1
);
1704 } else if (id
== 1) {
1705 /* Configure Own Address 2 */
1706 oar2
= readl_relaxed(i2c_dev
->base
+ STM32F7_I2C_OAR2
);
1707 oar2
&= ~STM32F7_I2C_OAR2_MASK
;
1708 if (slave
->flags
& I2C_CLIENT_TEN
) {
1713 oar2
|= STM32F7_I2C_OAR2_OA2_7(slave
->addr
);
1714 oar2
|= STM32F7_I2C_OAR2_OA2EN
;
1715 i2c_dev
->slave
[id
] = slave
;
1716 writel_relaxed(oar2
, i2c_dev
->base
+ STM32F7_I2C_OAR2
);
1723 stm32f7_i2c_clr_bits(base
+ STM32F7_I2C_CR2
, STM32F7_I2C_CR2_NACK
);
1725 /* Enable Address match interrupt, error interrupt and enable I2C */
1726 mask
= STM32F7_I2C_CR1_ADDRIE
| STM32F7_I2C_CR1_ERRIE
|
1728 stm32f7_i2c_set_bits(base
+ STM32F7_I2C_CR1
, mask
);
1733 if (!(stm32f7_i2c_is_slave_registered(i2c_dev
)))
1734 clk_disable(i2c_dev
->clk
);
1739 static int stm32f7_i2c_unreg_slave(struct i2c_client
*slave
)
1741 struct stm32f7_i2c_dev
*i2c_dev
= i2c_get_adapdata(slave
->adapter
);
1742 void __iomem
*base
= i2c_dev
->base
;
1746 ret
= stm32f7_i2c_get_slave_id(i2c_dev
, slave
, &id
);
1750 WARN_ON(!i2c_dev
->slave
[id
]);
1753 mask
= STM32F7_I2C_OAR1_OA1EN
;
1754 stm32f7_i2c_clr_bits(base
+ STM32F7_I2C_OAR1
, mask
);
1756 mask
= STM32F7_I2C_OAR2_OA2EN
;
1757 stm32f7_i2c_clr_bits(base
+ STM32F7_I2C_OAR2
, mask
);
1760 i2c_dev
->slave
[id
] = NULL
;
1762 if (!(stm32f7_i2c_is_slave_registered(i2c_dev
))) {
1763 stm32f7_i2c_disable_irq(i2c_dev
, STM32F7_I2C_ALL_IRQ_MASK
);
1764 clk_disable(i2c_dev
->clk
);
1770 static u32
stm32f7_i2c_func(struct i2c_adapter
*adap
)
1772 return I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
| I2C_FUNC_SLAVE
|
1773 I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
1774 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
1775 I2C_FUNC_SMBUS_BLOCK_DATA
| I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
1776 I2C_FUNC_SMBUS_PROC_CALL
| I2C_FUNC_SMBUS_PEC
;
1779 static struct i2c_algorithm stm32f7_i2c_algo
= {
1780 .master_xfer
= stm32f7_i2c_xfer
,
1781 .smbus_xfer
= stm32f7_i2c_smbus_xfer
,
1782 .functionality
= stm32f7_i2c_func
,
1783 .reg_slave
= stm32f7_i2c_reg_slave
,
1784 .unreg_slave
= stm32f7_i2c_unreg_slave
,
1787 static int stm32f7_i2c_probe(struct platform_device
*pdev
)
1789 struct stm32f7_i2c_dev
*i2c_dev
;
1790 const struct stm32f7_i2c_setup
*setup
;
1791 struct resource
*res
;
1792 u32 clk_rate
, rise_time
, fall_time
;
1793 struct i2c_adapter
*adap
;
1794 struct reset_control
*rst
;
1795 dma_addr_t phy_addr
;
1796 int irq_error
, irq_event
, ret
;
1798 i2c_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c_dev
), GFP_KERNEL
);
1802 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1803 i2c_dev
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1804 if (IS_ERR(i2c_dev
->base
))
1805 return PTR_ERR(i2c_dev
->base
);
1806 phy_addr
= (dma_addr_t
)res
->start
;
1808 irq_event
= platform_get_irq(pdev
, 0);
1809 if (irq_event
<= 0) {
1810 if (irq_event
!= -EPROBE_DEFER
)
1811 dev_err(&pdev
->dev
, "Failed to get IRQ event: %d\n",
1813 return irq_event
? : -ENOENT
;
1816 irq_error
= platform_get_irq(pdev
, 1);
1817 if (irq_error
<= 0) {
1818 if (irq_error
!= -EPROBE_DEFER
)
1819 dev_err(&pdev
->dev
, "Failed to get IRQ error: %d\n",
1821 return irq_error
? : -ENOENT
;
1824 i2c_dev
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1825 if (IS_ERR(i2c_dev
->clk
)) {
1826 dev_err(&pdev
->dev
, "Error: Missing controller clock\n");
1827 return PTR_ERR(i2c_dev
->clk
);
1829 ret
= clk_prepare_enable(i2c_dev
->clk
);
1831 dev_err(&pdev
->dev
, "Failed to prepare_enable clock\n");
1835 i2c_dev
->speed
= STM32_I2C_SPEED_STANDARD
;
1836 ret
= device_property_read_u32(&pdev
->dev
, "clock-frequency",
1838 if (!ret
&& clk_rate
>= 1000000)
1839 i2c_dev
->speed
= STM32_I2C_SPEED_FAST_PLUS
;
1840 else if (!ret
&& clk_rate
>= 400000)
1841 i2c_dev
->speed
= STM32_I2C_SPEED_FAST
;
1842 else if (!ret
&& clk_rate
>= 100000)
1843 i2c_dev
->speed
= STM32_I2C_SPEED_STANDARD
;
1845 rst
= devm_reset_control_get(&pdev
->dev
, NULL
);
1847 dev_err(&pdev
->dev
, "Error: Missing controller reset\n");
1851 reset_control_assert(rst
);
1853 reset_control_deassert(rst
);
1855 i2c_dev
->dev
= &pdev
->dev
;
1857 ret
= devm_request_threaded_irq(&pdev
->dev
, irq_event
,
1858 stm32f7_i2c_isr_event
,
1859 stm32f7_i2c_isr_event_thread
,
1861 pdev
->name
, i2c_dev
);
1863 dev_err(&pdev
->dev
, "Failed to request irq event %i\n",
1868 ret
= devm_request_irq(&pdev
->dev
, irq_error
, stm32f7_i2c_isr_error
, 0,
1869 pdev
->name
, i2c_dev
);
1871 dev_err(&pdev
->dev
, "Failed to request irq error %i\n",
1876 setup
= of_device_get_match_data(&pdev
->dev
);
1878 dev_err(&pdev
->dev
, "Can't get device data\n");
1882 i2c_dev
->setup
= *setup
;
1884 ret
= device_property_read_u32(i2c_dev
->dev
, "i2c-scl-rising-time-ns",
1887 i2c_dev
->setup
.rise_time
= rise_time
;
1889 ret
= device_property_read_u32(i2c_dev
->dev
, "i2c-scl-falling-time-ns",
1892 i2c_dev
->setup
.fall_time
= fall_time
;
1894 ret
= stm32f7_i2c_setup_timing(i2c_dev
, &i2c_dev
->setup
);
1898 stm32f7_i2c_hw_config(i2c_dev
);
1900 adap
= &i2c_dev
->adap
;
1901 i2c_set_adapdata(adap
, i2c_dev
);
1902 snprintf(adap
->name
, sizeof(adap
->name
), "STM32F7 I2C(%pa)",
1904 adap
->owner
= THIS_MODULE
;
1905 adap
->timeout
= 2 * HZ
;
1907 adap
->algo
= &stm32f7_i2c_algo
;
1908 adap
->dev
.parent
= &pdev
->dev
;
1909 adap
->dev
.of_node
= pdev
->dev
.of_node
;
1911 init_completion(&i2c_dev
->complete
);
1913 /* Init DMA config if supported */
1914 i2c_dev
->dma
= stm32_i2c_dma_request(i2c_dev
->dev
, phy_addr
,
1917 if (PTR_ERR(i2c_dev
->dma
) == -ENODEV
)
1918 i2c_dev
->dma
= NULL
;
1919 else if (IS_ERR(i2c_dev
->dma
)) {
1920 ret
= PTR_ERR(i2c_dev
->dma
);
1921 if (ret
!= -EPROBE_DEFER
)
1923 "Failed to request dma error %i\n", ret
);
1927 ret
= i2c_add_adapter(adap
);
1931 platform_set_drvdata(pdev
, i2c_dev
);
1933 clk_disable(i2c_dev
->clk
);
1935 dev_info(i2c_dev
->dev
, "STM32F7 I2C-%d bus adapter\n", adap
->nr
);
1940 clk_disable_unprepare(i2c_dev
->clk
);
1945 static int stm32f7_i2c_remove(struct platform_device
*pdev
)
1947 struct stm32f7_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
1950 stm32_i2c_dma_free(i2c_dev
->dma
);
1951 i2c_dev
->dma
= NULL
;
1954 i2c_del_adapter(&i2c_dev
->adap
);
1956 clk_unprepare(i2c_dev
->clk
);
1961 static const struct of_device_id stm32f7_i2c_match
[] = {
1962 { .compatible
= "st,stm32f7-i2c", .data
= &stm32f7_setup
},
1965 MODULE_DEVICE_TABLE(of
, stm32f7_i2c_match
);
1967 static struct platform_driver stm32f7_i2c_driver
= {
1969 .name
= "stm32f7-i2c",
1970 .of_match_table
= stm32f7_i2c_match
,
1972 .probe
= stm32f7_i2c_probe
,
1973 .remove
= stm32f7_i2c_remove
,
1976 module_platform_driver(stm32f7_i2c_driver
);
1978 MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
1979 MODULE_DESCRIPTION("STMicroelectronics STM32F7 I2C driver");
1980 MODULE_LICENSE("GPL v2");