xref: /openbmc/linux/drivers/i2c/busses/i2c-stm32f4.c (revision 961e026a06da0db68295bb012a34c5bcb8388d7c)
162817fc8SM'boumba Cedric Madianga /*
262817fc8SM'boumba Cedric Madianga  * Driver for STMicroelectronics STM32 I2C controller
362817fc8SM'boumba Cedric Madianga  *
462817fc8SM'boumba Cedric Madianga  * This I2C controller is described in the STM32F429/439 Soc reference manual.
562817fc8SM'boumba Cedric Madianga  * Please see below a link to the documentation:
662817fc8SM'boumba Cedric Madianga  * http://www.st.com/resource/en/reference_manual/DM00031020.pdf
762817fc8SM'boumba Cedric Madianga  *
862817fc8SM'boumba Cedric Madianga  * Copyright (C) M'boumba Cedric Madianga 2016
962817fc8SM'boumba Cedric Madianga  * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
1062817fc8SM'boumba Cedric Madianga  *
1162817fc8SM'boumba Cedric Madianga  * This driver is based on i2c-st.c
1262817fc8SM'boumba Cedric Madianga  *
1362817fc8SM'boumba Cedric Madianga  * License terms:  GNU General Public License (GPL), version 2
1462817fc8SM'boumba Cedric Madianga  */
1562817fc8SM'boumba Cedric Madianga 
1662817fc8SM'boumba Cedric Madianga #include <linux/clk.h>
1762817fc8SM'boumba Cedric Madianga #include <linux/delay.h>
1862817fc8SM'boumba Cedric Madianga #include <linux/err.h>
1962817fc8SM'boumba Cedric Madianga #include <linux/i2c.h>
2062817fc8SM'boumba Cedric Madianga #include <linux/interrupt.h>
2162817fc8SM'boumba Cedric Madianga #include <linux/io.h>
2262817fc8SM'boumba Cedric Madianga #include <linux/iopoll.h>
2362817fc8SM'boumba Cedric Madianga #include <linux/module.h>
2462817fc8SM'boumba Cedric Madianga #include <linux/of_address.h>
2562817fc8SM'boumba Cedric Madianga #include <linux/of_irq.h>
2662817fc8SM'boumba Cedric Madianga #include <linux/of.h>
2762817fc8SM'boumba Cedric Madianga #include <linux/platform_device.h>
2862817fc8SM'boumba Cedric Madianga #include <linux/reset.h>
2962817fc8SM'boumba Cedric Madianga 
3062817fc8SM'boumba Cedric Madianga /* STM32F4 I2C offset registers */
3162817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1			0x00
3262817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2			0x04
3362817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_DR			0x10
3462817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1			0x14
3562817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR2			0x18
3662817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR			0x1C
3762817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_TRISE		0x20
3862817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_FLTR		0x24
3962817fc8SM'boumba Cedric Madianga 
4062817fc8SM'boumba Cedric Madianga /* STM32F4 I2C control 1*/
4162817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_POS		BIT(11)
4262817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_ACK		BIT(10)
4362817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_STOP		BIT(9)
4462817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_START		BIT(8)
4562817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_PE		BIT(0)
4662817fc8SM'boumba Cedric Madianga 
4762817fc8SM'boumba Cedric Madianga /* STM32F4 I2C control 2 */
4862817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_FREQ_MASK	GENMASK(5, 0)
4962817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_FREQ(n)		((n) & STM32F4_I2C_CR2_FREQ_MASK)
5062817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_ITBUFEN		BIT(10)
5162817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_ITEVTEN		BIT(9)
5262817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_ITERREN		BIT(8)
5362817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_IRQ_MASK	(STM32F4_I2C_CR2_ITBUFEN | \
5462817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_CR2_ITEVTEN | \
5562817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_CR2_ITERREN)
5662817fc8SM'boumba Cedric Madianga 
5762817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Status 1 */
5862817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_AF		BIT(10)
5962817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ARLO		BIT(9)
6062817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_BERR		BIT(8)
6162817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_TXE		BIT(7)
6262817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_RXNE		BIT(6)
6362817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_BTF		BIT(2)
6462817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ADDR		BIT(1)
6562817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_SB		BIT(0)
6662817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ITEVTEN_MASK	(STM32F4_I2C_SR1_BTF | \
6762817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_SR1_ADDR | \
6862817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_SR1_SB)
6962817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ITBUFEN_MASK	(STM32F4_I2C_SR1_TXE | \
7062817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_SR1_RXNE)
7162817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ITERREN_MASK	(STM32F4_I2C_SR1_AF | \
7262817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_SR1_ARLO | \
7362817fc8SM'boumba Cedric Madianga 					 STM32F4_I2C_SR1_BERR)
7462817fc8SM'boumba Cedric Madianga 
7562817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Status 2 */
7662817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR2_BUSY		BIT(1)
7762817fc8SM'boumba Cedric Madianga 
7862817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Control Clock */
7962817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_CCR_MASK	GENMASK(11, 0)
8062817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_CCR(n)		((n) & STM32F4_I2C_CCR_CCR_MASK)
8162817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_FS		BIT(15)
8262817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_DUTY		BIT(14)
8362817fc8SM'boumba Cedric Madianga 
8462817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Trise */
8562817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_TRISE_VALUE_MASK	GENMASK(5, 0)
8662817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_TRISE_VALUE(n)	((n) & STM32F4_I2C_TRISE_VALUE_MASK)
8762817fc8SM'boumba Cedric Madianga 
8862817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_MIN_STANDARD_FREQ	2U
8962817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_MIN_FAST_FREQ	6U
9062817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_MAX_FREQ		46U
9162817fc8SM'boumba Cedric Madianga #define HZ_TO_MHZ			1000000
9262817fc8SM'boumba Cedric Madianga 
9362817fc8SM'boumba Cedric Madianga enum stm32f4_i2c_speed {
9462817fc8SM'boumba Cedric Madianga 	STM32F4_I2C_SPEED_STANDARD, /* 100 kHz */
9562817fc8SM'boumba Cedric Madianga 	STM32F4_I2C_SPEED_FAST, /* 400 kHz */
9662817fc8SM'boumba Cedric Madianga 	STM32F4_I2C_SPEED_END,
9762817fc8SM'boumba Cedric Madianga };
9862817fc8SM'boumba Cedric Madianga 
9962817fc8SM'boumba Cedric Madianga /**
10062817fc8SM'boumba Cedric Madianga  * struct stm32f4_i2c_msg - client specific data
10162817fc8SM'boumba Cedric Madianga  * @addr: 8-bit slave addr, including r/w bit
10262817fc8SM'boumba Cedric Madianga  * @count: number of bytes to be transferred
10362817fc8SM'boumba Cedric Madianga  * @buf: data buffer
10462817fc8SM'boumba Cedric Madianga  * @result: result of the transfer
10562817fc8SM'boumba Cedric Madianga  * @stop: last I2C msg to be sent, i.e. STOP to be generated
10662817fc8SM'boumba Cedric Madianga  */
10762817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg {
10862817fc8SM'boumba Cedric Madianga 	u8 addr;
10962817fc8SM'boumba Cedric Madianga 	u32 count;
11062817fc8SM'boumba Cedric Madianga 	u8 *buf;
11162817fc8SM'boumba Cedric Madianga 	int result;
11262817fc8SM'boumba Cedric Madianga 	bool stop;
11362817fc8SM'boumba Cedric Madianga };
11462817fc8SM'boumba Cedric Madianga 
11562817fc8SM'boumba Cedric Madianga /**
11662817fc8SM'boumba Cedric Madianga  * struct stm32f4_i2c_dev - private data of the controller
11762817fc8SM'boumba Cedric Madianga  * @adap: I2C adapter for this controller
11862817fc8SM'boumba Cedric Madianga  * @dev: device for this controller
11962817fc8SM'boumba Cedric Madianga  * @base: virtual memory area
12062817fc8SM'boumba Cedric Madianga  * @complete: completion of I2C message
12162817fc8SM'boumba Cedric Madianga  * @clk: hw i2c clock
12262817fc8SM'boumba Cedric Madianga  * @speed: I2C clock frequency of the controller. Standard or Fast are supported
12362817fc8SM'boumba Cedric Madianga  * @parent_rate: I2C clock parent rate in MHz
12462817fc8SM'boumba Cedric Madianga  * @msg: I2C transfer information
12562817fc8SM'boumba Cedric Madianga  */
12662817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev {
12762817fc8SM'boumba Cedric Madianga 	struct i2c_adapter adap;
12862817fc8SM'boumba Cedric Madianga 	struct device *dev;
12962817fc8SM'boumba Cedric Madianga 	void __iomem *base;
13062817fc8SM'boumba Cedric Madianga 	struct completion complete;
13162817fc8SM'boumba Cedric Madianga 	struct clk *clk;
13262817fc8SM'boumba Cedric Madianga 	int speed;
13362817fc8SM'boumba Cedric Madianga 	int parent_rate;
13462817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg msg;
13562817fc8SM'boumba Cedric Madianga };
13662817fc8SM'boumba Cedric Madianga 
13762817fc8SM'boumba Cedric Madianga static inline void stm32f4_i2c_set_bits(void __iomem *reg, u32 mask)
13862817fc8SM'boumba Cedric Madianga {
13962817fc8SM'boumba Cedric Madianga 	writel_relaxed(readl_relaxed(reg) | mask, reg);
14062817fc8SM'boumba Cedric Madianga }
14162817fc8SM'boumba Cedric Madianga 
14262817fc8SM'boumba Cedric Madianga static inline void stm32f4_i2c_clr_bits(void __iomem *reg, u32 mask)
14362817fc8SM'boumba Cedric Madianga {
14462817fc8SM'boumba Cedric Madianga 	writel_relaxed(readl_relaxed(reg) & ~mask, reg);
14562817fc8SM'boumba Cedric Madianga }
14662817fc8SM'boumba Cedric Madianga 
14762817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_disable_irq(struct stm32f4_i2c_dev *i2c_dev)
14862817fc8SM'boumba Cedric Madianga {
14962817fc8SM'boumba Cedric Madianga 	void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
15062817fc8SM'boumba Cedric Madianga 
15162817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_IRQ_MASK);
15262817fc8SM'boumba Cedric Madianga }
15362817fc8SM'boumba Cedric Madianga 
15462817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_set_periph_clk_freq(struct stm32f4_i2c_dev *i2c_dev)
15562817fc8SM'boumba Cedric Madianga {
15662817fc8SM'boumba Cedric Madianga 	u32 freq;
15762817fc8SM'boumba Cedric Madianga 	u32 cr2 = 0;
15862817fc8SM'boumba Cedric Madianga 
15962817fc8SM'boumba Cedric Madianga 	i2c_dev->parent_rate = clk_get_rate(i2c_dev->clk);
16062817fc8SM'boumba Cedric Madianga 	freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ);
16162817fc8SM'boumba Cedric Madianga 
16262817fc8SM'boumba Cedric Madianga 	if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) {
16362817fc8SM'boumba Cedric Madianga 		/*
16462817fc8SM'boumba Cedric Madianga 		 * To reach 100 kHz, the parent clk frequency should be between
16562817fc8SM'boumba Cedric Madianga 		 * a minimum value of 2 MHz and a maximum value of 46 MHz due
16662817fc8SM'boumba Cedric Madianga 		 * to hardware limitation
16762817fc8SM'boumba Cedric Madianga 		 */
16862817fc8SM'boumba Cedric Madianga 		if (freq < STM32F4_I2C_MIN_STANDARD_FREQ ||
16962817fc8SM'boumba Cedric Madianga 		    freq > STM32F4_I2C_MAX_FREQ) {
17062817fc8SM'boumba Cedric Madianga 			dev_err(i2c_dev->dev,
17162817fc8SM'boumba Cedric Madianga 				"bad parent clk freq for standard mode\n");
17262817fc8SM'boumba Cedric Madianga 			return -EINVAL;
17362817fc8SM'boumba Cedric Madianga 		}
17462817fc8SM'boumba Cedric Madianga 	} else {
17562817fc8SM'boumba Cedric Madianga 		/*
17662817fc8SM'boumba Cedric Madianga 		 * To be as close as possible to 400 kHz, the parent clk
17762817fc8SM'boumba Cedric Madianga 		 * frequency should be between a minimum value of 6 MHz and a
17862817fc8SM'boumba Cedric Madianga 		 * maximum value of 46 MHz due to hardware limitation
17962817fc8SM'boumba Cedric Madianga 		 */
18062817fc8SM'boumba Cedric Madianga 		if (freq < STM32F4_I2C_MIN_FAST_FREQ ||
18162817fc8SM'boumba Cedric Madianga 		    freq > STM32F4_I2C_MAX_FREQ) {
18262817fc8SM'boumba Cedric Madianga 			dev_err(i2c_dev->dev,
18362817fc8SM'boumba Cedric Madianga 				"bad parent clk freq for fast mode\n");
18462817fc8SM'boumba Cedric Madianga 			return -EINVAL;
18562817fc8SM'boumba Cedric Madianga 		}
18662817fc8SM'boumba Cedric Madianga 	}
18762817fc8SM'boumba Cedric Madianga 
18862817fc8SM'boumba Cedric Madianga 	cr2 |= STM32F4_I2C_CR2_FREQ(freq);
18962817fc8SM'boumba Cedric Madianga 	writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2);
19062817fc8SM'boumba Cedric Madianga 
19162817fc8SM'boumba Cedric Madianga 	return 0;
19262817fc8SM'boumba Cedric Madianga }
19362817fc8SM'boumba Cedric Madianga 
19462817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_set_rise_time(struct stm32f4_i2c_dev *i2c_dev)
19562817fc8SM'boumba Cedric Madianga {
19662817fc8SM'boumba Cedric Madianga 	u32 freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ);
19762817fc8SM'boumba Cedric Madianga 	u32 trise;
19862817fc8SM'boumba Cedric Madianga 
19962817fc8SM'boumba Cedric Madianga 	/*
20062817fc8SM'boumba Cedric Madianga 	 * These bits must be programmed with the maximum SCL rise time given in
20162817fc8SM'boumba Cedric Madianga 	 * the I2C bus specification, incremented by 1.
20262817fc8SM'boumba Cedric Madianga 	 *
20362817fc8SM'boumba Cedric Madianga 	 * In standard mode, the maximum allowed SCL rise time is 1000 ns.
20462817fc8SM'boumba Cedric Madianga 	 * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to
20562817fc8SM'boumba Cedric Madianga 	 * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be
20662817fc8SM'boumba Cedric Madianga 	 * programmed with 0x9. (1000 ns / 125 ns + 1)
20762817fc8SM'boumba Cedric Madianga 	 * So, for I2C standard mode TRISE = FREQ[5:0] + 1
20862817fc8SM'boumba Cedric Madianga 	 *
20962817fc8SM'boumba Cedric Madianga 	 * In fast mode, the maximum allowed SCL rise time is 300 ns.
21062817fc8SM'boumba Cedric Madianga 	 * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to
21162817fc8SM'boumba Cedric Madianga 	 * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be
21262817fc8SM'boumba Cedric Madianga 	 * programmed with 0x3. (300 ns / 125 ns + 1)
21362817fc8SM'boumba Cedric Madianga 	 * So, for I2C fast mode TRISE = FREQ[5:0] * 300 / 1000 + 1
21462817fc8SM'boumba Cedric Madianga 	 *
21562817fc8SM'boumba Cedric Madianga 	 * Function stm32f4_i2c_set_periph_clk_freq made sure that parent rate
21662817fc8SM'boumba Cedric Madianga 	 * is not higher than 46 MHz . As a result trise is at most 4 bits wide
21762817fc8SM'boumba Cedric Madianga 	 * and so fits into the TRISE bits [5:0].
21862817fc8SM'boumba Cedric Madianga 	 */
21962817fc8SM'boumba Cedric Madianga 	if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD)
22062817fc8SM'boumba Cedric Madianga 		trise = freq + 1;
22162817fc8SM'boumba Cedric Madianga 	else
22262817fc8SM'boumba Cedric Madianga 		trise = freq * 3 / 10 + 1;
22362817fc8SM'boumba Cedric Madianga 
22462817fc8SM'boumba Cedric Madianga 	writel_relaxed(STM32F4_I2C_TRISE_VALUE(trise),
22562817fc8SM'boumba Cedric Madianga 		       i2c_dev->base + STM32F4_I2C_TRISE);
22662817fc8SM'boumba Cedric Madianga }
22762817fc8SM'boumba Cedric Madianga 
22862817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_set_speed_mode(struct stm32f4_i2c_dev *i2c_dev)
22962817fc8SM'boumba Cedric Madianga {
23062817fc8SM'boumba Cedric Madianga 	u32 val;
23162817fc8SM'boumba Cedric Madianga 	u32 ccr = 0;
23262817fc8SM'boumba Cedric Madianga 
23362817fc8SM'boumba Cedric Madianga 	if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) {
23462817fc8SM'boumba Cedric Madianga 		/*
23562817fc8SM'boumba Cedric Madianga 		 * In standard mode:
23662817fc8SM'boumba Cedric Madianga 		 * t_scl_high = t_scl_low = CCR * I2C parent clk period
23762817fc8SM'boumba Cedric Madianga 		 * So to reach 100 kHz, we have:
23862817fc8SM'boumba Cedric Madianga 		 * CCR = I2C parent rate / 100 kHz >> 1
23962817fc8SM'boumba Cedric Madianga 		 *
24062817fc8SM'boumba Cedric Madianga 		 * For example with parent rate = 2 MHz:
24162817fc8SM'boumba Cedric Madianga 		 * CCR = 2000000 / (100000 << 1) = 10
24262817fc8SM'boumba Cedric Madianga 		 * t_scl_high = t_scl_low = 10 * (1 / 2000000) = 5000 ns
24362817fc8SM'boumba Cedric Madianga 		 * t_scl_high + t_scl_low = 10000 ns so 100 kHz is reached
24462817fc8SM'boumba Cedric Madianga 		 *
24562817fc8SM'boumba Cedric Madianga 		 * Function stm32f4_i2c_set_periph_clk_freq made sure that
24662817fc8SM'boumba Cedric Madianga 		 * parent rate is not higher than 46 MHz . As a result val
24762817fc8SM'boumba Cedric Madianga 		 * is at most 8 bits wide and so fits into the CCR bits [11:0].
24862817fc8SM'boumba Cedric Madianga 		 */
24962817fc8SM'boumba Cedric Madianga 		val = i2c_dev->parent_rate / (100000 << 1);
25062817fc8SM'boumba Cedric Madianga 	} else {
25162817fc8SM'boumba Cedric Madianga 		/*
25262817fc8SM'boumba Cedric Madianga 		 * In fast mode, we compute CCR with duty = 0 as with low
25362817fc8SM'boumba Cedric Madianga 		 * frequencies we are not able to reach 400 kHz.
25462817fc8SM'boumba Cedric Madianga 		 * In that case:
25562817fc8SM'boumba Cedric Madianga 		 * t_scl_high = CCR * I2C parent clk period
25662817fc8SM'boumba Cedric Madianga 		 * t_scl_low = 2 * CCR * I2C parent clk period
25762817fc8SM'boumba Cedric Madianga 		 * So, CCR = I2C parent rate / (400 kHz * 3)
25862817fc8SM'boumba Cedric Madianga 		 *
25962817fc8SM'boumba Cedric Madianga 		 * For example with parent rate = 6 MHz:
26062817fc8SM'boumba Cedric Madianga 		 * CCR = 6000000 / (400000 * 3) = 5
26162817fc8SM'boumba Cedric Madianga 		 * t_scl_high = 5 * (1 / 6000000) = 833 ns > 600 ns
26262817fc8SM'boumba Cedric Madianga 		 * t_scl_low = 2 * 5 * (1 / 6000000) = 1667 ns > 1300 ns
26362817fc8SM'boumba Cedric Madianga 		 * t_scl_high + t_scl_low = 2500 ns so 400 kHz is reached
26462817fc8SM'boumba Cedric Madianga 		 *
26562817fc8SM'boumba Cedric Madianga 		 * Function stm32f4_i2c_set_periph_clk_freq made sure that
26662817fc8SM'boumba Cedric Madianga 		 * parent rate is not higher than 46 MHz . As a result val
26762817fc8SM'boumba Cedric Madianga 		 * is at most 6 bits wide and so fits into the CCR bits [11:0].
26862817fc8SM'boumba Cedric Madianga 		 */
26962817fc8SM'boumba Cedric Madianga 		val = DIV_ROUND_UP(i2c_dev->parent_rate, 400000 * 3);
27062817fc8SM'boumba Cedric Madianga 
27162817fc8SM'boumba Cedric Madianga 		/* Select Fast mode */
27262817fc8SM'boumba Cedric Madianga 		ccr |= STM32F4_I2C_CCR_FS;
27362817fc8SM'boumba Cedric Madianga 	}
27462817fc8SM'boumba Cedric Madianga 
27562817fc8SM'boumba Cedric Madianga 	ccr |= STM32F4_I2C_CCR_CCR(val);
27662817fc8SM'boumba Cedric Madianga 	writel_relaxed(ccr, i2c_dev->base + STM32F4_I2C_CCR);
27762817fc8SM'boumba Cedric Madianga }
27862817fc8SM'boumba Cedric Madianga 
27962817fc8SM'boumba Cedric Madianga /**
28062817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_hw_config() - Prepare I2C block
28162817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
28262817fc8SM'boumba Cedric Madianga  */
28362817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_hw_config(struct stm32f4_i2c_dev *i2c_dev)
28462817fc8SM'boumba Cedric Madianga {
28562817fc8SM'boumba Cedric Madianga 	int ret;
28662817fc8SM'boumba Cedric Madianga 
28762817fc8SM'boumba Cedric Madianga 	ret = stm32f4_i2c_set_periph_clk_freq(i2c_dev);
28862817fc8SM'boumba Cedric Madianga 	if (ret)
28962817fc8SM'boumba Cedric Madianga 		return ret;
29062817fc8SM'boumba Cedric Madianga 
29162817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_set_rise_time(i2c_dev);
29262817fc8SM'boumba Cedric Madianga 
29362817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_set_speed_mode(i2c_dev);
29462817fc8SM'boumba Cedric Madianga 
29562817fc8SM'boumba Cedric Madianga 	/* Enable I2C */
29662817fc8SM'boumba Cedric Madianga 	writel_relaxed(STM32F4_I2C_CR1_PE, i2c_dev->base + STM32F4_I2C_CR1);
29762817fc8SM'boumba Cedric Madianga 
29862817fc8SM'boumba Cedric Madianga 	return 0;
29962817fc8SM'boumba Cedric Madianga }
30062817fc8SM'boumba Cedric Madianga 
30162817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_wait_free_bus(struct stm32f4_i2c_dev *i2c_dev)
30262817fc8SM'boumba Cedric Madianga {
30362817fc8SM'boumba Cedric Madianga 	u32 status;
30462817fc8SM'boumba Cedric Madianga 	int ret;
30562817fc8SM'boumba Cedric Madianga 
30662817fc8SM'boumba Cedric Madianga 	ret = readl_relaxed_poll_timeout(i2c_dev->base + STM32F4_I2C_SR2,
30762817fc8SM'boumba Cedric Madianga 					 status,
30862817fc8SM'boumba Cedric Madianga 					 !(status & STM32F4_I2C_SR2_BUSY),
30962817fc8SM'boumba Cedric Madianga 					 10, 1000);
31062817fc8SM'boumba Cedric Madianga 	if (ret) {
31162817fc8SM'boumba Cedric Madianga 		dev_dbg(i2c_dev->dev, "bus not free\n");
31262817fc8SM'boumba Cedric Madianga 		ret = -EBUSY;
31362817fc8SM'boumba Cedric Madianga 	}
31462817fc8SM'boumba Cedric Madianga 
31562817fc8SM'boumba Cedric Madianga 	return ret;
31662817fc8SM'boumba Cedric Madianga }
31762817fc8SM'boumba Cedric Madianga 
31862817fc8SM'boumba Cedric Madianga /**
31962817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_write_ byte() - Write a byte in the data register
32062817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
32162817fc8SM'boumba Cedric Madianga  * @byte: Data to write in the register
32262817fc8SM'boumba Cedric Madianga  */
32362817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_write_byte(struct stm32f4_i2c_dev *i2c_dev, u8 byte)
32462817fc8SM'boumba Cedric Madianga {
32562817fc8SM'boumba Cedric Madianga 	writel_relaxed(byte, i2c_dev->base + STM32F4_I2C_DR);
32662817fc8SM'boumba Cedric Madianga }
32762817fc8SM'boumba Cedric Madianga 
32862817fc8SM'boumba Cedric Madianga /**
32962817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_write_msg() - Fill the data register in write mode
33062817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
33162817fc8SM'boumba Cedric Madianga  *
33262817fc8SM'boumba Cedric Madianga  * This function fills the data register with I2C transfer buffer
33362817fc8SM'boumba Cedric Madianga  */
33462817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_write_msg(struct stm32f4_i2c_dev *i2c_dev)
33562817fc8SM'boumba Cedric Madianga {
33662817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
33762817fc8SM'boumba Cedric Madianga 
33862817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_write_byte(i2c_dev, *msg->buf++);
33962817fc8SM'boumba Cedric Madianga 	msg->count--;
34062817fc8SM'boumba Cedric Madianga }
34162817fc8SM'boumba Cedric Madianga 
34262817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_read_msg(struct stm32f4_i2c_dev *i2c_dev)
34362817fc8SM'boumba Cedric Madianga {
34462817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
34562817fc8SM'boumba Cedric Madianga 	u32 rbuf;
34662817fc8SM'boumba Cedric Madianga 
34762817fc8SM'boumba Cedric Madianga 	rbuf = readl_relaxed(i2c_dev->base + STM32F4_I2C_DR);
34862817fc8SM'boumba Cedric Madianga 	*msg->buf++ = rbuf;
34962817fc8SM'boumba Cedric Madianga 	msg->count--;
35062817fc8SM'boumba Cedric Madianga }
35162817fc8SM'boumba Cedric Madianga 
35262817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_terminate_xfer(struct stm32f4_i2c_dev *i2c_dev)
35362817fc8SM'boumba Cedric Madianga {
35462817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
35562817fc8SM'boumba Cedric Madianga 	void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
35662817fc8SM'boumba Cedric Madianga 
35762817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_disable_irq(i2c_dev);
35862817fc8SM'boumba Cedric Madianga 
35962817fc8SM'boumba Cedric Madianga 	reg = i2c_dev->base + STM32F4_I2C_CR1;
36062817fc8SM'boumba Cedric Madianga 	if (msg->stop)
36162817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
36262817fc8SM'boumba Cedric Madianga 	else
36362817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
36462817fc8SM'boumba Cedric Madianga 
36562817fc8SM'boumba Cedric Madianga 	complete(&i2c_dev->complete);
36662817fc8SM'boumba Cedric Madianga }
36762817fc8SM'boumba Cedric Madianga 
36862817fc8SM'boumba Cedric Madianga /**
36962817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_handle_write() - Handle FIFO empty interrupt in case of write
37062817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
37162817fc8SM'boumba Cedric Madianga  */
37262817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_write(struct stm32f4_i2c_dev *i2c_dev)
37362817fc8SM'boumba Cedric Madianga {
37462817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
37562817fc8SM'boumba Cedric Madianga 	void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
37662817fc8SM'boumba Cedric Madianga 
37762817fc8SM'boumba Cedric Madianga 	if (msg->count) {
37862817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_write_msg(i2c_dev);
37962817fc8SM'boumba Cedric Madianga 		if (!msg->count) {
38062817fc8SM'boumba Cedric Madianga 			/*
38162817fc8SM'boumba Cedric Madianga 			 * Disable buffer interrupts for RX not empty and TX
38262817fc8SM'boumba Cedric Madianga 			 * empty events
38362817fc8SM'boumba Cedric Madianga 			 */
38462817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN);
38562817fc8SM'boumba Cedric Madianga 		}
38662817fc8SM'boumba Cedric Madianga 	} else {
38762817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_terminate_xfer(i2c_dev);
38862817fc8SM'boumba Cedric Madianga 	}
38962817fc8SM'boumba Cedric Madianga }
39062817fc8SM'boumba Cedric Madianga 
39162817fc8SM'boumba Cedric Madianga /**
39262817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_handle_read() - Handle FIFO empty interrupt in case of read
39362817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
39462817fc8SM'boumba Cedric Madianga  *
39562817fc8SM'boumba Cedric Madianga  * This function is called when a new data is received in data register
39662817fc8SM'boumba Cedric Madianga  */
39762817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_read(struct stm32f4_i2c_dev *i2c_dev)
39862817fc8SM'boumba Cedric Madianga {
39962817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
40062817fc8SM'boumba Cedric Madianga 	void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2;
40162817fc8SM'boumba Cedric Madianga 
40262817fc8SM'boumba Cedric Madianga 	switch (msg->count) {
40362817fc8SM'boumba Cedric Madianga 	case 1:
40462817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_disable_irq(i2c_dev);
40562817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_read_msg(i2c_dev);
40662817fc8SM'boumba Cedric Madianga 		complete(&i2c_dev->complete);
40762817fc8SM'boumba Cedric Madianga 		break;
40862817fc8SM'boumba Cedric Madianga 	/*
40962817fc8SM'boumba Cedric Madianga 	 * For 2-byte reception, 3-byte reception and for Data N-2, N-1 and N
41062817fc8SM'boumba Cedric Madianga 	 * for N-byte reception with N > 3, we do not have to read the data
41162817fc8SM'boumba Cedric Madianga 	 * register when RX not empty event occurs as we have to wait for byte
41262817fc8SM'boumba Cedric Madianga 	 * transferred finished event before reading data.
41362817fc8SM'boumba Cedric Madianga 	 * So, here we just disable buffer interrupt in order to avoid another
41462817fc8SM'boumba Cedric Madianga 	 * system preemption due to RX not empty event.
41562817fc8SM'boumba Cedric Madianga 	 */
41662817fc8SM'boumba Cedric Madianga 	case 2:
41762817fc8SM'boumba Cedric Madianga 	case 3:
41862817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN);
41962817fc8SM'boumba Cedric Madianga 		break;
42062817fc8SM'boumba Cedric Madianga 	/*
42162817fc8SM'boumba Cedric Madianga 	 * For N byte reception with N > 3 we directly read data register
42262817fc8SM'boumba Cedric Madianga 	 * until N-2 data.
42362817fc8SM'boumba Cedric Madianga 	 */
42462817fc8SM'boumba Cedric Madianga 	default:
42562817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_read_msg(i2c_dev);
42662817fc8SM'boumba Cedric Madianga 	}
42762817fc8SM'boumba Cedric Madianga }
42862817fc8SM'boumba Cedric Madianga 
42962817fc8SM'boumba Cedric Madianga /**
43062817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_handle_rx_done() - Handle byte transfer finished interrupt
43162817fc8SM'boumba Cedric Madianga  * in case of read
43262817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
43362817fc8SM'boumba Cedric Madianga  *
43462817fc8SM'boumba Cedric Madianga  * This function is called when a new data is received in the shift register
43562817fc8SM'boumba Cedric Madianga  * but data register has not been read yet.
43662817fc8SM'boumba Cedric Madianga  */
43762817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_rx_done(struct stm32f4_i2c_dev *i2c_dev)
43862817fc8SM'boumba Cedric Madianga {
43962817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
44062817fc8SM'boumba Cedric Madianga 	void __iomem *reg;
44162817fc8SM'boumba Cedric Madianga 	u32 mask;
44262817fc8SM'boumba Cedric Madianga 	int i;
44362817fc8SM'boumba Cedric Madianga 
44462817fc8SM'boumba Cedric Madianga 	switch (msg->count) {
44562817fc8SM'boumba Cedric Madianga 	case 2:
44662817fc8SM'boumba Cedric Madianga 		/*
44762817fc8SM'boumba Cedric Madianga 		 * In order to correctly send the Stop or Repeated Start
44862817fc8SM'boumba Cedric Madianga 		 * condition on the I2C bus, the STOP/START bit has to be set
44962817fc8SM'boumba Cedric Madianga 		 * before reading the last two bytes (data N-1 and N).
45062817fc8SM'boumba Cedric Madianga 		 * After that, we could read the last two bytes, disable
45162817fc8SM'boumba Cedric Madianga 		 * remaining interrupts and notify the end of xfer to the
45262817fc8SM'boumba Cedric Madianga 		 * client
45362817fc8SM'boumba Cedric Madianga 		 */
45462817fc8SM'boumba Cedric Madianga 		reg = i2c_dev->base + STM32F4_I2C_CR1;
45562817fc8SM'boumba Cedric Madianga 		if (msg->stop)
45662817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
45762817fc8SM'boumba Cedric Madianga 		else
45862817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
45962817fc8SM'boumba Cedric Madianga 
46062817fc8SM'boumba Cedric Madianga 		for (i = 2; i > 0; i--)
46162817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_read_msg(i2c_dev);
46262817fc8SM'boumba Cedric Madianga 
46362817fc8SM'boumba Cedric Madianga 		reg = i2c_dev->base + STM32F4_I2C_CR2;
46462817fc8SM'boumba Cedric Madianga 		mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN;
46562817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_clr_bits(reg, mask);
46662817fc8SM'boumba Cedric Madianga 
46762817fc8SM'boumba Cedric Madianga 		complete(&i2c_dev->complete);
46862817fc8SM'boumba Cedric Madianga 		break;
46962817fc8SM'boumba Cedric Madianga 	case 3:
47062817fc8SM'boumba Cedric Madianga 		/*
47162817fc8SM'boumba Cedric Madianga 		 * In order to correctly generate the NACK pulse after the last
47262817fc8SM'boumba Cedric Madianga 		 * received data byte, we have to enable NACK before reading N-2
47362817fc8SM'boumba Cedric Madianga 		 * data
47462817fc8SM'boumba Cedric Madianga 		 */
47562817fc8SM'boumba Cedric Madianga 		reg = i2c_dev->base + STM32F4_I2C_CR1;
47662817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR1_ACK);
47762817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_read_msg(i2c_dev);
47862817fc8SM'boumba Cedric Madianga 		break;
47962817fc8SM'boumba Cedric Madianga 	default:
48062817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_read_msg(i2c_dev);
48162817fc8SM'boumba Cedric Madianga 	}
48262817fc8SM'boumba Cedric Madianga }
48362817fc8SM'boumba Cedric Madianga 
48462817fc8SM'boumba Cedric Madianga /**
48562817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_handle_rx_addr() - Handle address matched interrupt in case of
48662817fc8SM'boumba Cedric Madianga  * master receiver
48762817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
48862817fc8SM'boumba Cedric Madianga  */
48962817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_rx_addr(struct stm32f4_i2c_dev *i2c_dev)
49062817fc8SM'boumba Cedric Madianga {
49162817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
49262817fc8SM'boumba Cedric Madianga 	u32 cr1;
49362817fc8SM'boumba Cedric Madianga 
49462817fc8SM'boumba Cedric Madianga 	switch (msg->count) {
49562817fc8SM'boumba Cedric Madianga 	case 0:
49662817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_terminate_xfer(i2c_dev);
49762817fc8SM'boumba Cedric Madianga 
49862817fc8SM'boumba Cedric Madianga 		/* Clear ADDR flag */
49962817fc8SM'boumba Cedric Madianga 		readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
50062817fc8SM'boumba Cedric Madianga 		break;
50162817fc8SM'boumba Cedric Madianga 	case 1:
50262817fc8SM'boumba Cedric Madianga 		/*
50362817fc8SM'boumba Cedric Madianga 		 * Single byte reception:
50462817fc8SM'boumba Cedric Madianga 		 * Enable NACK and reset POS (Acknowledge position).
50562817fc8SM'boumba Cedric Madianga 		 * Then, clear ADDR flag and set STOP or RepSTART.
50662817fc8SM'boumba Cedric Madianga 		 * In that way, the NACK and STOP or RepStart pulses will be
50762817fc8SM'boumba Cedric Madianga 		 * sent as soon as the byte will be received in shift register
50862817fc8SM'boumba Cedric Madianga 		 */
50962817fc8SM'boumba Cedric Madianga 		cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
51062817fc8SM'boumba Cedric Madianga 		cr1 &= ~(STM32F4_I2C_CR1_ACK | STM32F4_I2C_CR1_POS);
51162817fc8SM'boumba Cedric Madianga 		writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
51262817fc8SM'boumba Cedric Madianga 
51362817fc8SM'boumba Cedric Madianga 		readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
51462817fc8SM'boumba Cedric Madianga 
51562817fc8SM'boumba Cedric Madianga 		if (msg->stop)
51662817fc8SM'boumba Cedric Madianga 			cr1 |= STM32F4_I2C_CR1_STOP;
51762817fc8SM'boumba Cedric Madianga 		else
51862817fc8SM'boumba Cedric Madianga 			cr1 |= STM32F4_I2C_CR1_START;
51962817fc8SM'boumba Cedric Madianga 		writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
52062817fc8SM'boumba Cedric Madianga 		break;
52162817fc8SM'boumba Cedric Madianga 	case 2:
52262817fc8SM'boumba Cedric Madianga 		/*
52362817fc8SM'boumba Cedric Madianga 		 * 2-byte reception:
52462817fc8SM'boumba Cedric Madianga 		 * Enable NACK, set POS (NACK position) and clear ADDR flag.
52562817fc8SM'boumba Cedric Madianga 		 * In that way, NACK will be sent for the next byte which will
52662817fc8SM'boumba Cedric Madianga 		 * be received in the shift register instead of the current
52762817fc8SM'boumba Cedric Madianga 		 * one.
52862817fc8SM'boumba Cedric Madianga 		 */
52962817fc8SM'boumba Cedric Madianga 		cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
53062817fc8SM'boumba Cedric Madianga 		cr1 &= ~STM32F4_I2C_CR1_ACK;
53162817fc8SM'boumba Cedric Madianga 		cr1 |= STM32F4_I2C_CR1_POS;
53262817fc8SM'boumba Cedric Madianga 		writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
53362817fc8SM'boumba Cedric Madianga 
53462817fc8SM'boumba Cedric Madianga 		readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
53562817fc8SM'boumba Cedric Madianga 		break;
53662817fc8SM'boumba Cedric Madianga 
53762817fc8SM'boumba Cedric Madianga 	default:
53862817fc8SM'boumba Cedric Madianga 		/*
53962817fc8SM'boumba Cedric Madianga 		 * N-byte reception:
54062817fc8SM'boumba Cedric Madianga 		 * Enable ACK, reset POS (ACK postion) and clear ADDR flag.
54162817fc8SM'boumba Cedric Madianga 		 * In that way, ACK will be sent as soon as the current byte
54262817fc8SM'boumba Cedric Madianga 		 * will be received in the shift register
54362817fc8SM'boumba Cedric Madianga 		 */
54462817fc8SM'boumba Cedric Madianga 		cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1);
54562817fc8SM'boumba Cedric Madianga 		cr1 |= STM32F4_I2C_CR1_ACK;
54662817fc8SM'boumba Cedric Madianga 		cr1 &= ~STM32F4_I2C_CR1_POS;
54762817fc8SM'boumba Cedric Madianga 		writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1);
54862817fc8SM'boumba Cedric Madianga 
54962817fc8SM'boumba Cedric Madianga 		readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
55062817fc8SM'boumba Cedric Madianga 		break;
55162817fc8SM'boumba Cedric Madianga 	}
55262817fc8SM'boumba Cedric Madianga }
55362817fc8SM'boumba Cedric Madianga 
55462817fc8SM'boumba Cedric Madianga /**
55562817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_isr_event() - Interrupt routine for I2C bus event
55662817fc8SM'boumba Cedric Madianga  * @irq: interrupt number
55762817fc8SM'boumba Cedric Madianga  * @data: Controller's private data
55862817fc8SM'boumba Cedric Madianga  */
55962817fc8SM'boumba Cedric Madianga static irqreturn_t stm32f4_i2c_isr_event(int irq, void *data)
56062817fc8SM'boumba Cedric Madianga {
56162817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_dev *i2c_dev = data;
56262817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
56362817fc8SM'boumba Cedric Madianga 	u32 possible_status = STM32F4_I2C_SR1_ITEVTEN_MASK;
56462817fc8SM'boumba Cedric Madianga 	u32 status, ien, event, cr2;
56562817fc8SM'boumba Cedric Madianga 
56662817fc8SM'boumba Cedric Madianga 	cr2 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR2);
56762817fc8SM'boumba Cedric Madianga 	ien = cr2 & STM32F4_I2C_CR2_IRQ_MASK;
56862817fc8SM'boumba Cedric Madianga 
56962817fc8SM'boumba Cedric Madianga 	/* Update possible_status if buffer interrupt is enabled */
57062817fc8SM'boumba Cedric Madianga 	if (ien & STM32F4_I2C_CR2_ITBUFEN)
57162817fc8SM'boumba Cedric Madianga 		possible_status |= STM32F4_I2C_SR1_ITBUFEN_MASK;
57262817fc8SM'boumba Cedric Madianga 
57362817fc8SM'boumba Cedric Madianga 	status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1);
57462817fc8SM'boumba Cedric Madianga 	event = status & possible_status;
57562817fc8SM'boumba Cedric Madianga 	if (!event) {
57662817fc8SM'boumba Cedric Madianga 		dev_dbg(i2c_dev->dev,
57762817fc8SM'boumba Cedric Madianga 			"spurious evt irq (status=0x%08x, ien=0x%08x)\n",
57862817fc8SM'boumba Cedric Madianga 			status, ien);
57962817fc8SM'boumba Cedric Madianga 		return IRQ_NONE;
58062817fc8SM'boumba Cedric Madianga 	}
58162817fc8SM'boumba Cedric Madianga 
58262817fc8SM'boumba Cedric Madianga 	/* Start condition generated */
58362817fc8SM'boumba Cedric Madianga 	if (event & STM32F4_I2C_SR1_SB)
58462817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_write_byte(i2c_dev, msg->addr);
58562817fc8SM'boumba Cedric Madianga 
58662817fc8SM'boumba Cedric Madianga 	/* I2C Address sent */
58762817fc8SM'boumba Cedric Madianga 	if (event & STM32F4_I2C_SR1_ADDR) {
58862817fc8SM'boumba Cedric Madianga 		if (msg->addr & I2C_M_RD)
58962817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_handle_rx_addr(i2c_dev);
59062817fc8SM'boumba Cedric Madianga 		else
59162817fc8SM'boumba Cedric Madianga 			readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2);
59262817fc8SM'boumba Cedric Madianga 
59362817fc8SM'boumba Cedric Madianga 		/*
59462817fc8SM'boumba Cedric Madianga 		 * Enable buffer interrupts for RX not empty and TX empty
59562817fc8SM'boumba Cedric Madianga 		 * events
59662817fc8SM'boumba Cedric Madianga 		 */
59762817fc8SM'boumba Cedric Madianga 		cr2 |= STM32F4_I2C_CR2_ITBUFEN;
59862817fc8SM'boumba Cedric Madianga 		writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2);
59962817fc8SM'boumba Cedric Madianga 	}
60062817fc8SM'boumba Cedric Madianga 
60162817fc8SM'boumba Cedric Madianga 	/* TX empty */
60262817fc8SM'boumba Cedric Madianga 	if ((event & STM32F4_I2C_SR1_TXE) && !(msg->addr & I2C_M_RD))
60362817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_handle_write(i2c_dev);
60462817fc8SM'boumba Cedric Madianga 
60562817fc8SM'boumba Cedric Madianga 	/* RX not empty */
60662817fc8SM'boumba Cedric Madianga 	if ((event & STM32F4_I2C_SR1_RXNE) && (msg->addr & I2C_M_RD))
60762817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_handle_read(i2c_dev);
60862817fc8SM'boumba Cedric Madianga 
60962817fc8SM'boumba Cedric Madianga 	/*
61062817fc8SM'boumba Cedric Madianga 	 * The BTF (Byte Transfer finished) event occurs when:
61162817fc8SM'boumba Cedric Madianga 	 * - in reception : a new byte is received in the shift register
61262817fc8SM'boumba Cedric Madianga 	 * but the previous byte has not been read yet from data register
61362817fc8SM'boumba Cedric Madianga 	 * - in transmission: a new byte should be sent but the data register
61462817fc8SM'boumba Cedric Madianga 	 * has not been written yet
61562817fc8SM'boumba Cedric Madianga 	 */
61662817fc8SM'boumba Cedric Madianga 	if (event & STM32F4_I2C_SR1_BTF) {
61762817fc8SM'boumba Cedric Madianga 		if (msg->addr & I2C_M_RD)
61862817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_handle_rx_done(i2c_dev);
61962817fc8SM'boumba Cedric Madianga 		else
62062817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_handle_write(i2c_dev);
62162817fc8SM'boumba Cedric Madianga 	}
62262817fc8SM'boumba Cedric Madianga 
62362817fc8SM'boumba Cedric Madianga 	return IRQ_HANDLED;
62462817fc8SM'boumba Cedric Madianga }
62562817fc8SM'boumba Cedric Madianga 
62662817fc8SM'boumba Cedric Madianga /**
62762817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_isr_error() - Interrupt routine for I2C bus error
62862817fc8SM'boumba Cedric Madianga  * @irq: interrupt number
62962817fc8SM'boumba Cedric Madianga  * @data: Controller's private data
63062817fc8SM'boumba Cedric Madianga  */
63162817fc8SM'boumba Cedric Madianga static irqreturn_t stm32f4_i2c_isr_error(int irq, void *data)
63262817fc8SM'boumba Cedric Madianga {
63362817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_dev *i2c_dev = data;
63462817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *msg = &i2c_dev->msg;
63562817fc8SM'boumba Cedric Madianga 	void __iomem *reg;
63662817fc8SM'boumba Cedric Madianga 	u32 status;
63762817fc8SM'boumba Cedric Madianga 
63862817fc8SM'boumba Cedric Madianga 	status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1);
63962817fc8SM'boumba Cedric Madianga 
64062817fc8SM'boumba Cedric Madianga 	/* Arbitration lost */
64162817fc8SM'boumba Cedric Madianga 	if (status & STM32F4_I2C_SR1_ARLO) {
64262817fc8SM'boumba Cedric Madianga 		status &= ~STM32F4_I2C_SR1_ARLO;
64362817fc8SM'boumba Cedric Madianga 		writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
64462817fc8SM'boumba Cedric Madianga 		msg->result = -EAGAIN;
64562817fc8SM'boumba Cedric Madianga 	}
64662817fc8SM'boumba Cedric Madianga 
64762817fc8SM'boumba Cedric Madianga 	/*
64862817fc8SM'boumba Cedric Madianga 	 * Acknowledge failure:
64962817fc8SM'boumba Cedric Madianga 	 * In master transmitter mode a Stop must be generated by software
65062817fc8SM'boumba Cedric Madianga 	 */
65162817fc8SM'boumba Cedric Madianga 	if (status & STM32F4_I2C_SR1_AF) {
65262817fc8SM'boumba Cedric Madianga 		if (!(msg->addr & I2C_M_RD)) {
65362817fc8SM'boumba Cedric Madianga 			reg = i2c_dev->base + STM32F4_I2C_CR1;
65462817fc8SM'boumba Cedric Madianga 			stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP);
65562817fc8SM'boumba Cedric Madianga 		}
65662817fc8SM'boumba Cedric Madianga 		status &= ~STM32F4_I2C_SR1_AF;
65762817fc8SM'boumba Cedric Madianga 		writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
65862817fc8SM'boumba Cedric Madianga 		msg->result = -EIO;
65962817fc8SM'boumba Cedric Madianga 	}
66062817fc8SM'boumba Cedric Madianga 
66162817fc8SM'boumba Cedric Madianga 	/* Bus error */
66262817fc8SM'boumba Cedric Madianga 	if (status & STM32F4_I2C_SR1_BERR) {
66362817fc8SM'boumba Cedric Madianga 		status &= ~STM32F4_I2C_SR1_BERR;
66462817fc8SM'boumba Cedric Madianga 		writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1);
66562817fc8SM'boumba Cedric Madianga 		msg->result = -EIO;
66662817fc8SM'boumba Cedric Madianga 	}
66762817fc8SM'boumba Cedric Madianga 
66862817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_disable_irq(i2c_dev);
66962817fc8SM'boumba Cedric Madianga 	complete(&i2c_dev->complete);
67062817fc8SM'boumba Cedric Madianga 
67162817fc8SM'boumba Cedric Madianga 	return IRQ_HANDLED;
67262817fc8SM'boumba Cedric Madianga }
67362817fc8SM'boumba Cedric Madianga 
67462817fc8SM'boumba Cedric Madianga /**
67562817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_xfer_msg() - Transfer a single I2C message
67662817fc8SM'boumba Cedric Madianga  * @i2c_dev: Controller's private data
67762817fc8SM'boumba Cedric Madianga  * @msg: I2C message to transfer
67862817fc8SM'boumba Cedric Madianga  * @is_first: first message of the sequence
67962817fc8SM'boumba Cedric Madianga  * @is_last: last message of the sequence
68062817fc8SM'boumba Cedric Madianga  */
68162817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_xfer_msg(struct stm32f4_i2c_dev *i2c_dev,
68262817fc8SM'boumba Cedric Madianga 				struct i2c_msg *msg, bool is_first,
68362817fc8SM'boumba Cedric Madianga 				bool is_last)
68462817fc8SM'boumba Cedric Madianga {
68562817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_msg *f4_msg = &i2c_dev->msg;
68662817fc8SM'boumba Cedric Madianga 	void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR1;
68762817fc8SM'boumba Cedric Madianga 	unsigned long timeout;
68862817fc8SM'boumba Cedric Madianga 	u32 mask;
68962817fc8SM'boumba Cedric Madianga 	int ret;
69062817fc8SM'boumba Cedric Madianga 
69162817fc8SM'boumba Cedric Madianga 	f4_msg->addr = i2c_8bit_addr_from_msg(msg);
69262817fc8SM'boumba Cedric Madianga 	f4_msg->buf = msg->buf;
69362817fc8SM'boumba Cedric Madianga 	f4_msg->count = msg->len;
69462817fc8SM'boumba Cedric Madianga 	f4_msg->result = 0;
69562817fc8SM'boumba Cedric Madianga 	f4_msg->stop = is_last;
69662817fc8SM'boumba Cedric Madianga 
69762817fc8SM'boumba Cedric Madianga 	reinit_completion(&i2c_dev->complete);
69862817fc8SM'boumba Cedric Madianga 
69962817fc8SM'boumba Cedric Madianga 	/* Enable events and errors interrupts */
70062817fc8SM'boumba Cedric Madianga 	mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN;
70162817fc8SM'boumba Cedric Madianga 	stm32f4_i2c_set_bits(i2c_dev->base + STM32F4_I2C_CR2, mask);
70262817fc8SM'boumba Cedric Madianga 
70362817fc8SM'boumba Cedric Madianga 	if (is_first) {
70462817fc8SM'boumba Cedric Madianga 		ret = stm32f4_i2c_wait_free_bus(i2c_dev);
70562817fc8SM'boumba Cedric Madianga 		if (ret)
70662817fc8SM'boumba Cedric Madianga 			return ret;
70762817fc8SM'boumba Cedric Madianga 
70862817fc8SM'boumba Cedric Madianga 		/* START generation */
70962817fc8SM'boumba Cedric Madianga 		stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START);
71062817fc8SM'boumba Cedric Madianga 	}
71162817fc8SM'boumba Cedric Madianga 
71262817fc8SM'boumba Cedric Madianga 	timeout = wait_for_completion_timeout(&i2c_dev->complete,
71362817fc8SM'boumba Cedric Madianga 					      i2c_dev->adap.timeout);
71462817fc8SM'boumba Cedric Madianga 	ret = f4_msg->result;
71562817fc8SM'boumba Cedric Madianga 
71662817fc8SM'boumba Cedric Madianga 	if (!timeout)
71762817fc8SM'boumba Cedric Madianga 		ret = -ETIMEDOUT;
71862817fc8SM'boumba Cedric Madianga 
71962817fc8SM'boumba Cedric Madianga 	return ret;
72062817fc8SM'boumba Cedric Madianga }
72162817fc8SM'boumba Cedric Madianga 
72262817fc8SM'boumba Cedric Madianga /**
72362817fc8SM'boumba Cedric Madianga  * stm32f4_i2c_xfer() - Transfer combined I2C message
72462817fc8SM'boumba Cedric Madianga  * @i2c_adap: Adapter pointer to the controller
72562817fc8SM'boumba Cedric Madianga  * @msgs: Pointer to data to be written.
72662817fc8SM'boumba Cedric Madianga  * @num: Number of messages to be executed
72762817fc8SM'boumba Cedric Madianga  */
72862817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[],
72962817fc8SM'boumba Cedric Madianga 			    int num)
73062817fc8SM'boumba Cedric Madianga {
73162817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap);
73262817fc8SM'boumba Cedric Madianga 	int ret, i;
73362817fc8SM'boumba Cedric Madianga 
73462817fc8SM'boumba Cedric Madianga 	ret = clk_enable(i2c_dev->clk);
73562817fc8SM'boumba Cedric Madianga 	if (ret) {
73662817fc8SM'boumba Cedric Madianga 		dev_err(i2c_dev->dev, "Failed to enable clock\n");
73762817fc8SM'boumba Cedric Madianga 		return ret;
73862817fc8SM'boumba Cedric Madianga 	}
73962817fc8SM'boumba Cedric Madianga 
74062817fc8SM'boumba Cedric Madianga 	for (i = 0; i < num && !ret; i++)
74162817fc8SM'boumba Cedric Madianga 		ret = stm32f4_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0,
74262817fc8SM'boumba Cedric Madianga 					   i == num - 1);
74362817fc8SM'boumba Cedric Madianga 
74462817fc8SM'boumba Cedric Madianga 	clk_disable(i2c_dev->clk);
74562817fc8SM'boumba Cedric Madianga 
74662817fc8SM'boumba Cedric Madianga 	return (ret < 0) ? ret : num;
74762817fc8SM'boumba Cedric Madianga }
74862817fc8SM'boumba Cedric Madianga 
74962817fc8SM'boumba Cedric Madianga static u32 stm32f4_i2c_func(struct i2c_adapter *adap)
75062817fc8SM'boumba Cedric Madianga {
75162817fc8SM'boumba Cedric Madianga 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
75262817fc8SM'boumba Cedric Madianga }
75362817fc8SM'boumba Cedric Madianga 
7548dc0f8c7SGustavo A. R. Silva static const struct i2c_algorithm stm32f4_i2c_algo = {
75562817fc8SM'boumba Cedric Madianga 	.master_xfer = stm32f4_i2c_xfer,
75662817fc8SM'boumba Cedric Madianga 	.functionality = stm32f4_i2c_func,
75762817fc8SM'boumba Cedric Madianga };
75862817fc8SM'boumba Cedric Madianga 
75962817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_probe(struct platform_device *pdev)
76062817fc8SM'boumba Cedric Madianga {
76162817fc8SM'boumba Cedric Madianga 	struct device_node *np = pdev->dev.of_node;
76262817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_dev *i2c_dev;
76362817fc8SM'boumba Cedric Madianga 	struct resource *res;
76462817fc8SM'boumba Cedric Madianga 	u32 irq_event, irq_error, clk_rate;
76562817fc8SM'boumba Cedric Madianga 	struct i2c_adapter *adap;
76662817fc8SM'boumba Cedric Madianga 	struct reset_control *rst;
76762817fc8SM'boumba Cedric Madianga 	int ret;
76862817fc8SM'boumba Cedric Madianga 
76962817fc8SM'boumba Cedric Madianga 	i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
77062817fc8SM'boumba Cedric Madianga 	if (!i2c_dev)
77162817fc8SM'boumba Cedric Madianga 		return -ENOMEM;
77262817fc8SM'boumba Cedric Madianga 
77362817fc8SM'boumba Cedric Madianga 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
77462817fc8SM'boumba Cedric Madianga 	i2c_dev->base = devm_ioremap_resource(&pdev->dev, res);
77562817fc8SM'boumba Cedric Madianga 	if (IS_ERR(i2c_dev->base))
77662817fc8SM'boumba Cedric Madianga 		return PTR_ERR(i2c_dev->base);
77762817fc8SM'boumba Cedric Madianga 
77862817fc8SM'boumba Cedric Madianga 	irq_event = irq_of_parse_and_map(np, 0);
77962817fc8SM'boumba Cedric Madianga 	if (!irq_event) {
78062817fc8SM'boumba Cedric Madianga 		dev_err(&pdev->dev, "IRQ event missing or invalid\n");
78162817fc8SM'boumba Cedric Madianga 		return -EINVAL;
78262817fc8SM'boumba Cedric Madianga 	}
78362817fc8SM'boumba Cedric Madianga 
78462817fc8SM'boumba Cedric Madianga 	irq_error = irq_of_parse_and_map(np, 1);
78562817fc8SM'boumba Cedric Madianga 	if (!irq_error) {
78662817fc8SM'boumba Cedric Madianga 		dev_err(&pdev->dev, "IRQ error missing or invalid\n");
78762817fc8SM'boumba Cedric Madianga 		return -EINVAL;
78862817fc8SM'boumba Cedric Madianga 	}
78962817fc8SM'boumba Cedric Madianga 
79062817fc8SM'boumba Cedric Madianga 	i2c_dev->clk = devm_clk_get(&pdev->dev, NULL);
79162817fc8SM'boumba Cedric Madianga 	if (IS_ERR(i2c_dev->clk)) {
79262817fc8SM'boumba Cedric Madianga 		dev_err(&pdev->dev, "Error: Missing controller clock\n");
79362817fc8SM'boumba Cedric Madianga 		return PTR_ERR(i2c_dev->clk);
79462817fc8SM'boumba Cedric Madianga 	}
79562817fc8SM'boumba Cedric Madianga 	ret = clk_prepare_enable(i2c_dev->clk);
79662817fc8SM'boumba Cedric Madianga 	if (ret) {
79762817fc8SM'boumba Cedric Madianga 		dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n");
79862817fc8SM'boumba Cedric Madianga 		return ret;
79962817fc8SM'boumba Cedric Madianga 	}
80062817fc8SM'boumba Cedric Madianga 
801*961e026aSPhilipp Zabel 	rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
80262817fc8SM'boumba Cedric Madianga 	if (IS_ERR(rst)) {
80362817fc8SM'boumba Cedric Madianga 		dev_err(&pdev->dev, "Error: Missing controller reset\n");
80462817fc8SM'boumba Cedric Madianga 		ret = PTR_ERR(rst);
80562817fc8SM'boumba Cedric Madianga 		goto clk_free;
80662817fc8SM'boumba Cedric Madianga 	}
80762817fc8SM'boumba Cedric Madianga 	reset_control_assert(rst);
80862817fc8SM'boumba Cedric Madianga 	udelay(2);
80962817fc8SM'boumba Cedric Madianga 	reset_control_deassert(rst);
81062817fc8SM'boumba Cedric Madianga 
81162817fc8SM'boumba Cedric Madianga 	i2c_dev->speed = STM32F4_I2C_SPEED_STANDARD;
81262817fc8SM'boumba Cedric Madianga 	ret = of_property_read_u32(np, "clock-frequency", &clk_rate);
81362817fc8SM'boumba Cedric Madianga 	if (!ret && clk_rate >= 400000)
81462817fc8SM'boumba Cedric Madianga 		i2c_dev->speed = STM32F4_I2C_SPEED_FAST;
81562817fc8SM'boumba Cedric Madianga 
81662817fc8SM'boumba Cedric Madianga 	i2c_dev->dev = &pdev->dev;
81762817fc8SM'boumba Cedric Madianga 
81862817fc8SM'boumba Cedric Madianga 	ret = devm_request_irq(&pdev->dev, irq_event, stm32f4_i2c_isr_event, 0,
81962817fc8SM'boumba Cedric Madianga 			       pdev->name, i2c_dev);
82062817fc8SM'boumba Cedric Madianga 	if (ret) {
82162817fc8SM'boumba Cedric Madianga 		dev_err(&pdev->dev, "Failed to request irq event %i\n",
82262817fc8SM'boumba Cedric Madianga 			irq_event);
82362817fc8SM'boumba Cedric Madianga 		goto clk_free;
82462817fc8SM'boumba Cedric Madianga 	}
82562817fc8SM'boumba Cedric Madianga 
82662817fc8SM'boumba Cedric Madianga 	ret = devm_request_irq(&pdev->dev, irq_error, stm32f4_i2c_isr_error, 0,
82762817fc8SM'boumba Cedric Madianga 			       pdev->name, i2c_dev);
82862817fc8SM'boumba Cedric Madianga 	if (ret) {
82962817fc8SM'boumba Cedric Madianga 		dev_err(&pdev->dev, "Failed to request irq error %i\n",
83062817fc8SM'boumba Cedric Madianga 			irq_error);
83162817fc8SM'boumba Cedric Madianga 		goto clk_free;
83262817fc8SM'boumba Cedric Madianga 	}
83362817fc8SM'boumba Cedric Madianga 
83462817fc8SM'boumba Cedric Madianga 	ret = stm32f4_i2c_hw_config(i2c_dev);
83562817fc8SM'boumba Cedric Madianga 	if (ret)
83662817fc8SM'boumba Cedric Madianga 		goto clk_free;
83762817fc8SM'boumba Cedric Madianga 
83862817fc8SM'boumba Cedric Madianga 	adap = &i2c_dev->adap;
83962817fc8SM'boumba Cedric Madianga 	i2c_set_adapdata(adap, i2c_dev);
84062817fc8SM'boumba Cedric Madianga 	snprintf(adap->name, sizeof(adap->name), "STM32 I2C(%pa)", &res->start);
84162817fc8SM'boumba Cedric Madianga 	adap->owner = THIS_MODULE;
84262817fc8SM'boumba Cedric Madianga 	adap->timeout = 2 * HZ;
84362817fc8SM'boumba Cedric Madianga 	adap->retries = 0;
84462817fc8SM'boumba Cedric Madianga 	adap->algo = &stm32f4_i2c_algo;
84562817fc8SM'boumba Cedric Madianga 	adap->dev.parent = &pdev->dev;
84662817fc8SM'boumba Cedric Madianga 	adap->dev.of_node = pdev->dev.of_node;
84762817fc8SM'boumba Cedric Madianga 
84862817fc8SM'boumba Cedric Madianga 	init_completion(&i2c_dev->complete);
84962817fc8SM'boumba Cedric Madianga 
85062817fc8SM'boumba Cedric Madianga 	ret = i2c_add_adapter(adap);
85162817fc8SM'boumba Cedric Madianga 	if (ret)
85262817fc8SM'boumba Cedric Madianga 		goto clk_free;
85362817fc8SM'boumba Cedric Madianga 
85462817fc8SM'boumba Cedric Madianga 	platform_set_drvdata(pdev, i2c_dev);
85562817fc8SM'boumba Cedric Madianga 
85662817fc8SM'boumba Cedric Madianga 	clk_disable(i2c_dev->clk);
85762817fc8SM'boumba Cedric Madianga 
85862817fc8SM'boumba Cedric Madianga 	dev_info(i2c_dev->dev, "STM32F4 I2C driver registered\n");
85962817fc8SM'boumba Cedric Madianga 
86062817fc8SM'boumba Cedric Madianga 	return 0;
86162817fc8SM'boumba Cedric Madianga 
86262817fc8SM'boumba Cedric Madianga clk_free:
86362817fc8SM'boumba Cedric Madianga 	clk_disable_unprepare(i2c_dev->clk);
86462817fc8SM'boumba Cedric Madianga 	return ret;
86562817fc8SM'boumba Cedric Madianga }
86662817fc8SM'boumba Cedric Madianga 
86762817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_remove(struct platform_device *pdev)
86862817fc8SM'boumba Cedric Madianga {
86962817fc8SM'boumba Cedric Madianga 	struct stm32f4_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
87062817fc8SM'boumba Cedric Madianga 
87162817fc8SM'boumba Cedric Madianga 	i2c_del_adapter(&i2c_dev->adap);
87262817fc8SM'boumba Cedric Madianga 
87362817fc8SM'boumba Cedric Madianga 	clk_unprepare(i2c_dev->clk);
87462817fc8SM'boumba Cedric Madianga 
87562817fc8SM'boumba Cedric Madianga 	return 0;
87662817fc8SM'boumba Cedric Madianga }
87762817fc8SM'boumba Cedric Madianga 
87862817fc8SM'boumba Cedric Madianga static const struct of_device_id stm32f4_i2c_match[] = {
87962817fc8SM'boumba Cedric Madianga 	{ .compatible = "st,stm32f4-i2c", },
88062817fc8SM'boumba Cedric Madianga 	{},
88162817fc8SM'boumba Cedric Madianga };
88262817fc8SM'boumba Cedric Madianga MODULE_DEVICE_TABLE(of, stm32f4_i2c_match);
88362817fc8SM'boumba Cedric Madianga 
88462817fc8SM'boumba Cedric Madianga static struct platform_driver stm32f4_i2c_driver = {
88562817fc8SM'boumba Cedric Madianga 	.driver = {
88662817fc8SM'boumba Cedric Madianga 		.name = "stm32f4-i2c",
88762817fc8SM'boumba Cedric Madianga 		.of_match_table = stm32f4_i2c_match,
88862817fc8SM'boumba Cedric Madianga 	},
88962817fc8SM'boumba Cedric Madianga 	.probe = stm32f4_i2c_probe,
89062817fc8SM'boumba Cedric Madianga 	.remove = stm32f4_i2c_remove,
89162817fc8SM'boumba Cedric Madianga };
89262817fc8SM'boumba Cedric Madianga 
89362817fc8SM'boumba Cedric Madianga module_platform_driver(stm32f4_i2c_driver);
89462817fc8SM'boumba Cedric Madianga 
89562817fc8SM'boumba Cedric Madianga MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
89662817fc8SM'boumba Cedric Madianga MODULE_DESCRIPTION("STMicroelectronics STM32F4 I2C driver");
89762817fc8SM'boumba Cedric Madianga MODULE_LICENSE("GPL v2");
898