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