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