1*62817fc8SM'boumba Cedric Madianga /* 2*62817fc8SM'boumba Cedric Madianga * Driver for STMicroelectronics STM32 I2C controller 3*62817fc8SM'boumba Cedric Madianga * 4*62817fc8SM'boumba Cedric Madianga * This I2C controller is described in the STM32F429/439 Soc reference manual. 5*62817fc8SM'boumba Cedric Madianga * Please see below a link to the documentation: 6*62817fc8SM'boumba Cedric Madianga * http://www.st.com/resource/en/reference_manual/DM00031020.pdf 7*62817fc8SM'boumba Cedric Madianga * 8*62817fc8SM'boumba Cedric Madianga * Copyright (C) M'boumba Cedric Madianga 2016 9*62817fc8SM'boumba Cedric Madianga * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com> 10*62817fc8SM'boumba Cedric Madianga * 11*62817fc8SM'boumba Cedric Madianga * This driver is based on i2c-st.c 12*62817fc8SM'boumba Cedric Madianga * 13*62817fc8SM'boumba Cedric Madianga * License terms: GNU General Public License (GPL), version 2 14*62817fc8SM'boumba Cedric Madianga */ 15*62817fc8SM'boumba Cedric Madianga 16*62817fc8SM'boumba Cedric Madianga #include <linux/clk.h> 17*62817fc8SM'boumba Cedric Madianga #include <linux/delay.h> 18*62817fc8SM'boumba Cedric Madianga #include <linux/err.h> 19*62817fc8SM'boumba Cedric Madianga #include <linux/i2c.h> 20*62817fc8SM'boumba Cedric Madianga #include <linux/interrupt.h> 21*62817fc8SM'boumba Cedric Madianga #include <linux/io.h> 22*62817fc8SM'boumba Cedric Madianga #include <linux/iopoll.h> 23*62817fc8SM'boumba Cedric Madianga #include <linux/module.h> 24*62817fc8SM'boumba Cedric Madianga #include <linux/of_address.h> 25*62817fc8SM'boumba Cedric Madianga #include <linux/of_irq.h> 26*62817fc8SM'boumba Cedric Madianga #include <linux/of.h> 27*62817fc8SM'boumba Cedric Madianga #include <linux/platform_device.h> 28*62817fc8SM'boumba Cedric Madianga #include <linux/reset.h> 29*62817fc8SM'boumba Cedric Madianga 30*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C offset registers */ 31*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1 0x00 32*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2 0x04 33*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_DR 0x10 34*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1 0x14 35*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR2 0x18 36*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR 0x1C 37*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_TRISE 0x20 38*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_FLTR 0x24 39*62817fc8SM'boumba Cedric Madianga 40*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C control 1*/ 41*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_POS BIT(11) 42*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_ACK BIT(10) 43*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_STOP BIT(9) 44*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_START BIT(8) 45*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR1_PE BIT(0) 46*62817fc8SM'boumba Cedric Madianga 47*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C control 2 */ 48*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_FREQ_MASK GENMASK(5, 0) 49*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_FREQ(n) ((n) & STM32F4_I2C_CR2_FREQ_MASK) 50*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_ITBUFEN BIT(10) 51*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_ITEVTEN BIT(9) 52*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_ITERREN BIT(8) 53*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CR2_IRQ_MASK (STM32F4_I2C_CR2_ITBUFEN | \ 54*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_CR2_ITEVTEN | \ 55*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_CR2_ITERREN) 56*62817fc8SM'boumba Cedric Madianga 57*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Status 1 */ 58*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_AF BIT(10) 59*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ARLO BIT(9) 60*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_BERR BIT(8) 61*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_TXE BIT(7) 62*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_RXNE BIT(6) 63*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_BTF BIT(2) 64*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ADDR BIT(1) 65*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_SB BIT(0) 66*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ITEVTEN_MASK (STM32F4_I2C_SR1_BTF | \ 67*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SR1_ADDR | \ 68*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SR1_SB) 69*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ITBUFEN_MASK (STM32F4_I2C_SR1_TXE | \ 70*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SR1_RXNE) 71*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR1_ITERREN_MASK (STM32F4_I2C_SR1_AF | \ 72*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SR1_ARLO | \ 73*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SR1_BERR) 74*62817fc8SM'boumba Cedric Madianga 75*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Status 2 */ 76*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_SR2_BUSY BIT(1) 77*62817fc8SM'boumba Cedric Madianga 78*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Control Clock */ 79*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_CCR_MASK GENMASK(11, 0) 80*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_CCR(n) ((n) & STM32F4_I2C_CCR_CCR_MASK) 81*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_FS BIT(15) 82*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_CCR_DUTY BIT(14) 83*62817fc8SM'boumba Cedric Madianga 84*62817fc8SM'boumba Cedric Madianga /* STM32F4 I2C Trise */ 85*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_TRISE_VALUE_MASK GENMASK(5, 0) 86*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_TRISE_VALUE(n) ((n) & STM32F4_I2C_TRISE_VALUE_MASK) 87*62817fc8SM'boumba Cedric Madianga 88*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_MIN_STANDARD_FREQ 2U 89*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_MIN_FAST_FREQ 6U 90*62817fc8SM'boumba Cedric Madianga #define STM32F4_I2C_MAX_FREQ 46U 91*62817fc8SM'boumba Cedric Madianga #define HZ_TO_MHZ 1000000 92*62817fc8SM'boumba Cedric Madianga 93*62817fc8SM'boumba Cedric Madianga enum stm32f4_i2c_speed { 94*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SPEED_STANDARD, /* 100 kHz */ 95*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SPEED_FAST, /* 400 kHz */ 96*62817fc8SM'boumba Cedric Madianga STM32F4_I2C_SPEED_END, 97*62817fc8SM'boumba Cedric Madianga }; 98*62817fc8SM'boumba Cedric Madianga 99*62817fc8SM'boumba Cedric Madianga /** 100*62817fc8SM'boumba Cedric Madianga * struct stm32f4_i2c_msg - client specific data 101*62817fc8SM'boumba Cedric Madianga * @addr: 8-bit slave addr, including r/w bit 102*62817fc8SM'boumba Cedric Madianga * @count: number of bytes to be transferred 103*62817fc8SM'boumba Cedric Madianga * @buf: data buffer 104*62817fc8SM'boumba Cedric Madianga * @result: result of the transfer 105*62817fc8SM'boumba Cedric Madianga * @stop: last I2C msg to be sent, i.e. STOP to be generated 106*62817fc8SM'boumba Cedric Madianga */ 107*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg { 108*62817fc8SM'boumba Cedric Madianga u8 addr; 109*62817fc8SM'boumba Cedric Madianga u32 count; 110*62817fc8SM'boumba Cedric Madianga u8 *buf; 111*62817fc8SM'boumba Cedric Madianga int result; 112*62817fc8SM'boumba Cedric Madianga bool stop; 113*62817fc8SM'boumba Cedric Madianga }; 114*62817fc8SM'boumba Cedric Madianga 115*62817fc8SM'boumba Cedric Madianga /** 116*62817fc8SM'boumba Cedric Madianga * struct stm32f4_i2c_dev - private data of the controller 117*62817fc8SM'boumba Cedric Madianga * @adap: I2C adapter for this controller 118*62817fc8SM'boumba Cedric Madianga * @dev: device for this controller 119*62817fc8SM'boumba Cedric Madianga * @base: virtual memory area 120*62817fc8SM'boumba Cedric Madianga * @complete: completion of I2C message 121*62817fc8SM'boumba Cedric Madianga * @clk: hw i2c clock 122*62817fc8SM'boumba Cedric Madianga * @speed: I2C clock frequency of the controller. Standard or Fast are supported 123*62817fc8SM'boumba Cedric Madianga * @parent_rate: I2C clock parent rate in MHz 124*62817fc8SM'boumba Cedric Madianga * @msg: I2C transfer information 125*62817fc8SM'boumba Cedric Madianga */ 126*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev { 127*62817fc8SM'boumba Cedric Madianga struct i2c_adapter adap; 128*62817fc8SM'boumba Cedric Madianga struct device *dev; 129*62817fc8SM'boumba Cedric Madianga void __iomem *base; 130*62817fc8SM'boumba Cedric Madianga struct completion complete; 131*62817fc8SM'boumba Cedric Madianga struct clk *clk; 132*62817fc8SM'boumba Cedric Madianga int speed; 133*62817fc8SM'boumba Cedric Madianga int parent_rate; 134*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg msg; 135*62817fc8SM'boumba Cedric Madianga }; 136*62817fc8SM'boumba Cedric Madianga 137*62817fc8SM'boumba Cedric Madianga static inline void stm32f4_i2c_set_bits(void __iomem *reg, u32 mask) 138*62817fc8SM'boumba Cedric Madianga { 139*62817fc8SM'boumba Cedric Madianga writel_relaxed(readl_relaxed(reg) | mask, reg); 140*62817fc8SM'boumba Cedric Madianga } 141*62817fc8SM'boumba Cedric Madianga 142*62817fc8SM'boumba Cedric Madianga static inline void stm32f4_i2c_clr_bits(void __iomem *reg, u32 mask) 143*62817fc8SM'boumba Cedric Madianga { 144*62817fc8SM'boumba Cedric Madianga writel_relaxed(readl_relaxed(reg) & ~mask, reg); 145*62817fc8SM'boumba Cedric Madianga } 146*62817fc8SM'boumba Cedric Madianga 147*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_disable_irq(struct stm32f4_i2c_dev *i2c_dev) 148*62817fc8SM'boumba Cedric Madianga { 149*62817fc8SM'boumba Cedric Madianga void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2; 150*62817fc8SM'boumba Cedric Madianga 151*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_IRQ_MASK); 152*62817fc8SM'boumba Cedric Madianga } 153*62817fc8SM'boumba Cedric Madianga 154*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_set_periph_clk_freq(struct stm32f4_i2c_dev *i2c_dev) 155*62817fc8SM'boumba Cedric Madianga { 156*62817fc8SM'boumba Cedric Madianga u32 freq; 157*62817fc8SM'boumba Cedric Madianga u32 cr2 = 0; 158*62817fc8SM'boumba Cedric Madianga 159*62817fc8SM'boumba Cedric Madianga i2c_dev->parent_rate = clk_get_rate(i2c_dev->clk); 160*62817fc8SM'boumba Cedric Madianga freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ); 161*62817fc8SM'boumba Cedric Madianga 162*62817fc8SM'boumba Cedric Madianga if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) { 163*62817fc8SM'boumba Cedric Madianga /* 164*62817fc8SM'boumba Cedric Madianga * To reach 100 kHz, the parent clk frequency should be between 165*62817fc8SM'boumba Cedric Madianga * a minimum value of 2 MHz and a maximum value of 46 MHz due 166*62817fc8SM'boumba Cedric Madianga * to hardware limitation 167*62817fc8SM'boumba Cedric Madianga */ 168*62817fc8SM'boumba Cedric Madianga if (freq < STM32F4_I2C_MIN_STANDARD_FREQ || 169*62817fc8SM'boumba Cedric Madianga freq > STM32F4_I2C_MAX_FREQ) { 170*62817fc8SM'boumba Cedric Madianga dev_err(i2c_dev->dev, 171*62817fc8SM'boumba Cedric Madianga "bad parent clk freq for standard mode\n"); 172*62817fc8SM'boumba Cedric Madianga return -EINVAL; 173*62817fc8SM'boumba Cedric Madianga } 174*62817fc8SM'boumba Cedric Madianga } else { 175*62817fc8SM'boumba Cedric Madianga /* 176*62817fc8SM'boumba Cedric Madianga * To be as close as possible to 400 kHz, the parent clk 177*62817fc8SM'boumba Cedric Madianga * frequency should be between a minimum value of 6 MHz and a 178*62817fc8SM'boumba Cedric Madianga * maximum value of 46 MHz due to hardware limitation 179*62817fc8SM'boumba Cedric Madianga */ 180*62817fc8SM'boumba Cedric Madianga if (freq < STM32F4_I2C_MIN_FAST_FREQ || 181*62817fc8SM'boumba Cedric Madianga freq > STM32F4_I2C_MAX_FREQ) { 182*62817fc8SM'boumba Cedric Madianga dev_err(i2c_dev->dev, 183*62817fc8SM'boumba Cedric Madianga "bad parent clk freq for fast mode\n"); 184*62817fc8SM'boumba Cedric Madianga return -EINVAL; 185*62817fc8SM'boumba Cedric Madianga } 186*62817fc8SM'boumba Cedric Madianga } 187*62817fc8SM'boumba Cedric Madianga 188*62817fc8SM'boumba Cedric Madianga cr2 |= STM32F4_I2C_CR2_FREQ(freq); 189*62817fc8SM'boumba Cedric Madianga writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2); 190*62817fc8SM'boumba Cedric Madianga 191*62817fc8SM'boumba Cedric Madianga return 0; 192*62817fc8SM'boumba Cedric Madianga } 193*62817fc8SM'boumba Cedric Madianga 194*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_set_rise_time(struct stm32f4_i2c_dev *i2c_dev) 195*62817fc8SM'boumba Cedric Madianga { 196*62817fc8SM'boumba Cedric Madianga u32 freq = DIV_ROUND_UP(i2c_dev->parent_rate, HZ_TO_MHZ); 197*62817fc8SM'boumba Cedric Madianga u32 trise; 198*62817fc8SM'boumba Cedric Madianga 199*62817fc8SM'boumba Cedric Madianga /* 200*62817fc8SM'boumba Cedric Madianga * These bits must be programmed with the maximum SCL rise time given in 201*62817fc8SM'boumba Cedric Madianga * the I2C bus specification, incremented by 1. 202*62817fc8SM'boumba Cedric Madianga * 203*62817fc8SM'boumba Cedric Madianga * In standard mode, the maximum allowed SCL rise time is 1000 ns. 204*62817fc8SM'boumba Cedric Madianga * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to 205*62817fc8SM'boumba Cedric Madianga * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be 206*62817fc8SM'boumba Cedric Madianga * programmed with 0x9. (1000 ns / 125 ns + 1) 207*62817fc8SM'boumba Cedric Madianga * So, for I2C standard mode TRISE = FREQ[5:0] + 1 208*62817fc8SM'boumba Cedric Madianga * 209*62817fc8SM'boumba Cedric Madianga * In fast mode, the maximum allowed SCL rise time is 300 ns. 210*62817fc8SM'boumba Cedric Madianga * If, in the I2C_CR2 register, the value of FREQ[5:0] bits is equal to 211*62817fc8SM'boumba Cedric Madianga * 0x08 so period = 125 ns therefore the TRISE[5:0] bits must be 212*62817fc8SM'boumba Cedric Madianga * programmed with 0x3. (300 ns / 125 ns + 1) 213*62817fc8SM'boumba Cedric Madianga * So, for I2C fast mode TRISE = FREQ[5:0] * 300 / 1000 + 1 214*62817fc8SM'boumba Cedric Madianga * 215*62817fc8SM'boumba Cedric Madianga * Function stm32f4_i2c_set_periph_clk_freq made sure that parent rate 216*62817fc8SM'boumba Cedric Madianga * is not higher than 46 MHz . As a result trise is at most 4 bits wide 217*62817fc8SM'boumba Cedric Madianga * and so fits into the TRISE bits [5:0]. 218*62817fc8SM'boumba Cedric Madianga */ 219*62817fc8SM'boumba Cedric Madianga if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) 220*62817fc8SM'boumba Cedric Madianga trise = freq + 1; 221*62817fc8SM'boumba Cedric Madianga else 222*62817fc8SM'boumba Cedric Madianga trise = freq * 3 / 10 + 1; 223*62817fc8SM'boumba Cedric Madianga 224*62817fc8SM'boumba Cedric Madianga writel_relaxed(STM32F4_I2C_TRISE_VALUE(trise), 225*62817fc8SM'boumba Cedric Madianga i2c_dev->base + STM32F4_I2C_TRISE); 226*62817fc8SM'boumba Cedric Madianga } 227*62817fc8SM'boumba Cedric Madianga 228*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_set_speed_mode(struct stm32f4_i2c_dev *i2c_dev) 229*62817fc8SM'boumba Cedric Madianga { 230*62817fc8SM'boumba Cedric Madianga u32 val; 231*62817fc8SM'boumba Cedric Madianga u32 ccr = 0; 232*62817fc8SM'boumba Cedric Madianga 233*62817fc8SM'boumba Cedric Madianga if (i2c_dev->speed == STM32F4_I2C_SPEED_STANDARD) { 234*62817fc8SM'boumba Cedric Madianga /* 235*62817fc8SM'boumba Cedric Madianga * In standard mode: 236*62817fc8SM'boumba Cedric Madianga * t_scl_high = t_scl_low = CCR * I2C parent clk period 237*62817fc8SM'boumba Cedric Madianga * So to reach 100 kHz, we have: 238*62817fc8SM'boumba Cedric Madianga * CCR = I2C parent rate / 100 kHz >> 1 239*62817fc8SM'boumba Cedric Madianga * 240*62817fc8SM'boumba Cedric Madianga * For example with parent rate = 2 MHz: 241*62817fc8SM'boumba Cedric Madianga * CCR = 2000000 / (100000 << 1) = 10 242*62817fc8SM'boumba Cedric Madianga * t_scl_high = t_scl_low = 10 * (1 / 2000000) = 5000 ns 243*62817fc8SM'boumba Cedric Madianga * t_scl_high + t_scl_low = 10000 ns so 100 kHz is reached 244*62817fc8SM'boumba Cedric Madianga * 245*62817fc8SM'boumba Cedric Madianga * Function stm32f4_i2c_set_periph_clk_freq made sure that 246*62817fc8SM'boumba Cedric Madianga * parent rate is not higher than 46 MHz . As a result val 247*62817fc8SM'boumba Cedric Madianga * is at most 8 bits wide and so fits into the CCR bits [11:0]. 248*62817fc8SM'boumba Cedric Madianga */ 249*62817fc8SM'boumba Cedric Madianga val = i2c_dev->parent_rate / (100000 << 1); 250*62817fc8SM'boumba Cedric Madianga } else { 251*62817fc8SM'boumba Cedric Madianga /* 252*62817fc8SM'boumba Cedric Madianga * In fast mode, we compute CCR with duty = 0 as with low 253*62817fc8SM'boumba Cedric Madianga * frequencies we are not able to reach 400 kHz. 254*62817fc8SM'boumba Cedric Madianga * In that case: 255*62817fc8SM'boumba Cedric Madianga * t_scl_high = CCR * I2C parent clk period 256*62817fc8SM'boumba Cedric Madianga * t_scl_low = 2 * CCR * I2C parent clk period 257*62817fc8SM'boumba Cedric Madianga * So, CCR = I2C parent rate / (400 kHz * 3) 258*62817fc8SM'boumba Cedric Madianga * 259*62817fc8SM'boumba Cedric Madianga * For example with parent rate = 6 MHz: 260*62817fc8SM'boumba Cedric Madianga * CCR = 6000000 / (400000 * 3) = 5 261*62817fc8SM'boumba Cedric Madianga * t_scl_high = 5 * (1 / 6000000) = 833 ns > 600 ns 262*62817fc8SM'boumba Cedric Madianga * t_scl_low = 2 * 5 * (1 / 6000000) = 1667 ns > 1300 ns 263*62817fc8SM'boumba Cedric Madianga * t_scl_high + t_scl_low = 2500 ns so 400 kHz is reached 264*62817fc8SM'boumba Cedric Madianga * 265*62817fc8SM'boumba Cedric Madianga * Function stm32f4_i2c_set_periph_clk_freq made sure that 266*62817fc8SM'boumba Cedric Madianga * parent rate is not higher than 46 MHz . As a result val 267*62817fc8SM'boumba Cedric Madianga * is at most 6 bits wide and so fits into the CCR bits [11:0]. 268*62817fc8SM'boumba Cedric Madianga */ 269*62817fc8SM'boumba Cedric Madianga val = DIV_ROUND_UP(i2c_dev->parent_rate, 400000 * 3); 270*62817fc8SM'boumba Cedric Madianga 271*62817fc8SM'boumba Cedric Madianga /* Select Fast mode */ 272*62817fc8SM'boumba Cedric Madianga ccr |= STM32F4_I2C_CCR_FS; 273*62817fc8SM'boumba Cedric Madianga } 274*62817fc8SM'boumba Cedric Madianga 275*62817fc8SM'boumba Cedric Madianga ccr |= STM32F4_I2C_CCR_CCR(val); 276*62817fc8SM'boumba Cedric Madianga writel_relaxed(ccr, i2c_dev->base + STM32F4_I2C_CCR); 277*62817fc8SM'boumba Cedric Madianga } 278*62817fc8SM'boumba Cedric Madianga 279*62817fc8SM'boumba Cedric Madianga /** 280*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_hw_config() - Prepare I2C block 281*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 282*62817fc8SM'boumba Cedric Madianga */ 283*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_hw_config(struct stm32f4_i2c_dev *i2c_dev) 284*62817fc8SM'boumba Cedric Madianga { 285*62817fc8SM'boumba Cedric Madianga int ret; 286*62817fc8SM'boumba Cedric Madianga 287*62817fc8SM'boumba Cedric Madianga ret = stm32f4_i2c_set_periph_clk_freq(i2c_dev); 288*62817fc8SM'boumba Cedric Madianga if (ret) 289*62817fc8SM'boumba Cedric Madianga return ret; 290*62817fc8SM'boumba Cedric Madianga 291*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_rise_time(i2c_dev); 292*62817fc8SM'boumba Cedric Madianga 293*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_speed_mode(i2c_dev); 294*62817fc8SM'boumba Cedric Madianga 295*62817fc8SM'boumba Cedric Madianga /* Enable I2C */ 296*62817fc8SM'boumba Cedric Madianga writel_relaxed(STM32F4_I2C_CR1_PE, i2c_dev->base + STM32F4_I2C_CR1); 297*62817fc8SM'boumba Cedric Madianga 298*62817fc8SM'boumba Cedric Madianga return 0; 299*62817fc8SM'boumba Cedric Madianga } 300*62817fc8SM'boumba Cedric Madianga 301*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_wait_free_bus(struct stm32f4_i2c_dev *i2c_dev) 302*62817fc8SM'boumba Cedric Madianga { 303*62817fc8SM'boumba Cedric Madianga u32 status; 304*62817fc8SM'boumba Cedric Madianga int ret; 305*62817fc8SM'boumba Cedric Madianga 306*62817fc8SM'boumba Cedric Madianga ret = readl_relaxed_poll_timeout(i2c_dev->base + STM32F4_I2C_SR2, 307*62817fc8SM'boumba Cedric Madianga status, 308*62817fc8SM'boumba Cedric Madianga !(status & STM32F4_I2C_SR2_BUSY), 309*62817fc8SM'boumba Cedric Madianga 10, 1000); 310*62817fc8SM'boumba Cedric Madianga if (ret) { 311*62817fc8SM'boumba Cedric Madianga dev_dbg(i2c_dev->dev, "bus not free\n"); 312*62817fc8SM'boumba Cedric Madianga ret = -EBUSY; 313*62817fc8SM'boumba Cedric Madianga } 314*62817fc8SM'boumba Cedric Madianga 315*62817fc8SM'boumba Cedric Madianga return ret; 316*62817fc8SM'boumba Cedric Madianga } 317*62817fc8SM'boumba Cedric Madianga 318*62817fc8SM'boumba Cedric Madianga /** 319*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_write_ byte() - Write a byte in the data register 320*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 321*62817fc8SM'boumba Cedric Madianga * @byte: Data to write in the register 322*62817fc8SM'boumba Cedric Madianga */ 323*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_write_byte(struct stm32f4_i2c_dev *i2c_dev, u8 byte) 324*62817fc8SM'boumba Cedric Madianga { 325*62817fc8SM'boumba Cedric Madianga writel_relaxed(byte, i2c_dev->base + STM32F4_I2C_DR); 326*62817fc8SM'boumba Cedric Madianga } 327*62817fc8SM'boumba Cedric Madianga 328*62817fc8SM'boumba Cedric Madianga /** 329*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_write_msg() - Fill the data register in write mode 330*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 331*62817fc8SM'boumba Cedric Madianga * 332*62817fc8SM'boumba Cedric Madianga * This function fills the data register with I2C transfer buffer 333*62817fc8SM'boumba Cedric Madianga */ 334*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_write_msg(struct stm32f4_i2c_dev *i2c_dev) 335*62817fc8SM'boumba Cedric Madianga { 336*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 337*62817fc8SM'boumba Cedric Madianga 338*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_write_byte(i2c_dev, *msg->buf++); 339*62817fc8SM'boumba Cedric Madianga msg->count--; 340*62817fc8SM'boumba Cedric Madianga } 341*62817fc8SM'boumba Cedric Madianga 342*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_read_msg(struct stm32f4_i2c_dev *i2c_dev) 343*62817fc8SM'boumba Cedric Madianga { 344*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 345*62817fc8SM'boumba Cedric Madianga u32 rbuf; 346*62817fc8SM'boumba Cedric Madianga 347*62817fc8SM'boumba Cedric Madianga rbuf = readl_relaxed(i2c_dev->base + STM32F4_I2C_DR); 348*62817fc8SM'boumba Cedric Madianga *msg->buf++ = rbuf; 349*62817fc8SM'boumba Cedric Madianga msg->count--; 350*62817fc8SM'boumba Cedric Madianga } 351*62817fc8SM'boumba Cedric Madianga 352*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_terminate_xfer(struct stm32f4_i2c_dev *i2c_dev) 353*62817fc8SM'boumba Cedric Madianga { 354*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 355*62817fc8SM'boumba Cedric Madianga void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2; 356*62817fc8SM'boumba Cedric Madianga 357*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_disable_irq(i2c_dev); 358*62817fc8SM'boumba Cedric Madianga 359*62817fc8SM'boumba Cedric Madianga reg = i2c_dev->base + STM32F4_I2C_CR1; 360*62817fc8SM'boumba Cedric Madianga if (msg->stop) 361*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP); 362*62817fc8SM'boumba Cedric Madianga else 363*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START); 364*62817fc8SM'boumba Cedric Madianga 365*62817fc8SM'boumba Cedric Madianga complete(&i2c_dev->complete); 366*62817fc8SM'boumba Cedric Madianga } 367*62817fc8SM'boumba Cedric Madianga 368*62817fc8SM'boumba Cedric Madianga /** 369*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_handle_write() - Handle FIFO empty interrupt in case of write 370*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 371*62817fc8SM'boumba Cedric Madianga */ 372*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_write(struct stm32f4_i2c_dev *i2c_dev) 373*62817fc8SM'boumba Cedric Madianga { 374*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 375*62817fc8SM'boumba Cedric Madianga void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2; 376*62817fc8SM'boumba Cedric Madianga 377*62817fc8SM'boumba Cedric Madianga if (msg->count) { 378*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_write_msg(i2c_dev); 379*62817fc8SM'boumba Cedric Madianga if (!msg->count) { 380*62817fc8SM'boumba Cedric Madianga /* 381*62817fc8SM'boumba Cedric Madianga * Disable buffer interrupts for RX not empty and TX 382*62817fc8SM'boumba Cedric Madianga * empty events 383*62817fc8SM'boumba Cedric Madianga */ 384*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN); 385*62817fc8SM'boumba Cedric Madianga } 386*62817fc8SM'boumba Cedric Madianga } else { 387*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_terminate_xfer(i2c_dev); 388*62817fc8SM'boumba Cedric Madianga } 389*62817fc8SM'boumba Cedric Madianga } 390*62817fc8SM'boumba Cedric Madianga 391*62817fc8SM'boumba Cedric Madianga /** 392*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_handle_read() - Handle FIFO empty interrupt in case of read 393*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 394*62817fc8SM'boumba Cedric Madianga * 395*62817fc8SM'boumba Cedric Madianga * This function is called when a new data is received in data register 396*62817fc8SM'boumba Cedric Madianga */ 397*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_read(struct stm32f4_i2c_dev *i2c_dev) 398*62817fc8SM'boumba Cedric Madianga { 399*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 400*62817fc8SM'boumba Cedric Madianga void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR2; 401*62817fc8SM'boumba Cedric Madianga 402*62817fc8SM'boumba Cedric Madianga switch (msg->count) { 403*62817fc8SM'boumba Cedric Madianga case 1: 404*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_disable_irq(i2c_dev); 405*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_read_msg(i2c_dev); 406*62817fc8SM'boumba Cedric Madianga complete(&i2c_dev->complete); 407*62817fc8SM'boumba Cedric Madianga break; 408*62817fc8SM'boumba Cedric Madianga /* 409*62817fc8SM'boumba Cedric Madianga * For 2-byte reception, 3-byte reception and for Data N-2, N-1 and N 410*62817fc8SM'boumba Cedric Madianga * for N-byte reception with N > 3, we do not have to read the data 411*62817fc8SM'boumba Cedric Madianga * register when RX not empty event occurs as we have to wait for byte 412*62817fc8SM'boumba Cedric Madianga * transferred finished event before reading data. 413*62817fc8SM'boumba Cedric Madianga * So, here we just disable buffer interrupt in order to avoid another 414*62817fc8SM'boumba Cedric Madianga * system preemption due to RX not empty event. 415*62817fc8SM'boumba Cedric Madianga */ 416*62817fc8SM'boumba Cedric Madianga case 2: 417*62817fc8SM'boumba Cedric Madianga case 3: 418*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR2_ITBUFEN); 419*62817fc8SM'boumba Cedric Madianga break; 420*62817fc8SM'boumba Cedric Madianga /* 421*62817fc8SM'boumba Cedric Madianga * For N byte reception with N > 3 we directly read data register 422*62817fc8SM'boumba Cedric Madianga * until N-2 data. 423*62817fc8SM'boumba Cedric Madianga */ 424*62817fc8SM'boumba Cedric Madianga default: 425*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_read_msg(i2c_dev); 426*62817fc8SM'boumba Cedric Madianga } 427*62817fc8SM'boumba Cedric Madianga } 428*62817fc8SM'boumba Cedric Madianga 429*62817fc8SM'boumba Cedric Madianga /** 430*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_handle_rx_done() - Handle byte transfer finished interrupt 431*62817fc8SM'boumba Cedric Madianga * in case of read 432*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 433*62817fc8SM'boumba Cedric Madianga * 434*62817fc8SM'boumba Cedric Madianga * This function is called when a new data is received in the shift register 435*62817fc8SM'boumba Cedric Madianga * but data register has not been read yet. 436*62817fc8SM'boumba Cedric Madianga */ 437*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_rx_done(struct stm32f4_i2c_dev *i2c_dev) 438*62817fc8SM'boumba Cedric Madianga { 439*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 440*62817fc8SM'boumba Cedric Madianga void __iomem *reg; 441*62817fc8SM'boumba Cedric Madianga u32 mask; 442*62817fc8SM'boumba Cedric Madianga int i; 443*62817fc8SM'boumba Cedric Madianga 444*62817fc8SM'boumba Cedric Madianga switch (msg->count) { 445*62817fc8SM'boumba Cedric Madianga case 2: 446*62817fc8SM'boumba Cedric Madianga /* 447*62817fc8SM'boumba Cedric Madianga * In order to correctly send the Stop or Repeated Start 448*62817fc8SM'boumba Cedric Madianga * condition on the I2C bus, the STOP/START bit has to be set 449*62817fc8SM'boumba Cedric Madianga * before reading the last two bytes (data N-1 and N). 450*62817fc8SM'boumba Cedric Madianga * After that, we could read the last two bytes, disable 451*62817fc8SM'boumba Cedric Madianga * remaining interrupts and notify the end of xfer to the 452*62817fc8SM'boumba Cedric Madianga * client 453*62817fc8SM'boumba Cedric Madianga */ 454*62817fc8SM'boumba Cedric Madianga reg = i2c_dev->base + STM32F4_I2C_CR1; 455*62817fc8SM'boumba Cedric Madianga if (msg->stop) 456*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP); 457*62817fc8SM'boumba Cedric Madianga else 458*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START); 459*62817fc8SM'boumba Cedric Madianga 460*62817fc8SM'boumba Cedric Madianga for (i = 2; i > 0; i--) 461*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_read_msg(i2c_dev); 462*62817fc8SM'boumba Cedric Madianga 463*62817fc8SM'boumba Cedric Madianga reg = i2c_dev->base + STM32F4_I2C_CR2; 464*62817fc8SM'boumba Cedric Madianga mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN; 465*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_clr_bits(reg, mask); 466*62817fc8SM'boumba Cedric Madianga 467*62817fc8SM'boumba Cedric Madianga complete(&i2c_dev->complete); 468*62817fc8SM'boumba Cedric Madianga break; 469*62817fc8SM'boumba Cedric Madianga case 3: 470*62817fc8SM'boumba Cedric Madianga /* 471*62817fc8SM'boumba Cedric Madianga * In order to correctly generate the NACK pulse after the last 472*62817fc8SM'boumba Cedric Madianga * received data byte, we have to enable NACK before reading N-2 473*62817fc8SM'boumba Cedric Madianga * data 474*62817fc8SM'boumba Cedric Madianga */ 475*62817fc8SM'boumba Cedric Madianga reg = i2c_dev->base + STM32F4_I2C_CR1; 476*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_clr_bits(reg, STM32F4_I2C_CR1_ACK); 477*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_read_msg(i2c_dev); 478*62817fc8SM'boumba Cedric Madianga break; 479*62817fc8SM'boumba Cedric Madianga default: 480*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_read_msg(i2c_dev); 481*62817fc8SM'boumba Cedric Madianga } 482*62817fc8SM'boumba Cedric Madianga } 483*62817fc8SM'boumba Cedric Madianga 484*62817fc8SM'boumba Cedric Madianga /** 485*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_handle_rx_addr() - Handle address matched interrupt in case of 486*62817fc8SM'boumba Cedric Madianga * master receiver 487*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 488*62817fc8SM'boumba Cedric Madianga */ 489*62817fc8SM'boumba Cedric Madianga static void stm32f4_i2c_handle_rx_addr(struct stm32f4_i2c_dev *i2c_dev) 490*62817fc8SM'boumba Cedric Madianga { 491*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 492*62817fc8SM'boumba Cedric Madianga u32 cr1; 493*62817fc8SM'boumba Cedric Madianga 494*62817fc8SM'boumba Cedric Madianga switch (msg->count) { 495*62817fc8SM'boumba Cedric Madianga case 0: 496*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_terminate_xfer(i2c_dev); 497*62817fc8SM'boumba Cedric Madianga 498*62817fc8SM'boumba Cedric Madianga /* Clear ADDR flag */ 499*62817fc8SM'boumba Cedric Madianga readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2); 500*62817fc8SM'boumba Cedric Madianga break; 501*62817fc8SM'boumba Cedric Madianga case 1: 502*62817fc8SM'boumba Cedric Madianga /* 503*62817fc8SM'boumba Cedric Madianga * Single byte reception: 504*62817fc8SM'boumba Cedric Madianga * Enable NACK and reset POS (Acknowledge position). 505*62817fc8SM'boumba Cedric Madianga * Then, clear ADDR flag and set STOP or RepSTART. 506*62817fc8SM'boumba Cedric Madianga * In that way, the NACK and STOP or RepStart pulses will be 507*62817fc8SM'boumba Cedric Madianga * sent as soon as the byte will be received in shift register 508*62817fc8SM'boumba Cedric Madianga */ 509*62817fc8SM'boumba Cedric Madianga cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1); 510*62817fc8SM'boumba Cedric Madianga cr1 &= ~(STM32F4_I2C_CR1_ACK | STM32F4_I2C_CR1_POS); 511*62817fc8SM'boumba Cedric Madianga writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1); 512*62817fc8SM'boumba Cedric Madianga 513*62817fc8SM'boumba Cedric Madianga readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2); 514*62817fc8SM'boumba Cedric Madianga 515*62817fc8SM'boumba Cedric Madianga if (msg->stop) 516*62817fc8SM'boumba Cedric Madianga cr1 |= STM32F4_I2C_CR1_STOP; 517*62817fc8SM'boumba Cedric Madianga else 518*62817fc8SM'boumba Cedric Madianga cr1 |= STM32F4_I2C_CR1_START; 519*62817fc8SM'boumba Cedric Madianga writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1); 520*62817fc8SM'boumba Cedric Madianga break; 521*62817fc8SM'boumba Cedric Madianga case 2: 522*62817fc8SM'boumba Cedric Madianga /* 523*62817fc8SM'boumba Cedric Madianga * 2-byte reception: 524*62817fc8SM'boumba Cedric Madianga * Enable NACK, set POS (NACK position) and clear ADDR flag. 525*62817fc8SM'boumba Cedric Madianga * In that way, NACK will be sent for the next byte which will 526*62817fc8SM'boumba Cedric Madianga * be received in the shift register instead of the current 527*62817fc8SM'boumba Cedric Madianga * one. 528*62817fc8SM'boumba Cedric Madianga */ 529*62817fc8SM'boumba Cedric Madianga cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1); 530*62817fc8SM'boumba Cedric Madianga cr1 &= ~STM32F4_I2C_CR1_ACK; 531*62817fc8SM'boumba Cedric Madianga cr1 |= STM32F4_I2C_CR1_POS; 532*62817fc8SM'boumba Cedric Madianga writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1); 533*62817fc8SM'boumba Cedric Madianga 534*62817fc8SM'boumba Cedric Madianga readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2); 535*62817fc8SM'boumba Cedric Madianga break; 536*62817fc8SM'boumba Cedric Madianga 537*62817fc8SM'boumba Cedric Madianga default: 538*62817fc8SM'boumba Cedric Madianga /* 539*62817fc8SM'boumba Cedric Madianga * N-byte reception: 540*62817fc8SM'boumba Cedric Madianga * Enable ACK, reset POS (ACK postion) and clear ADDR flag. 541*62817fc8SM'boumba Cedric Madianga * In that way, ACK will be sent as soon as the current byte 542*62817fc8SM'boumba Cedric Madianga * will be received in the shift register 543*62817fc8SM'boumba Cedric Madianga */ 544*62817fc8SM'boumba Cedric Madianga cr1 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR1); 545*62817fc8SM'boumba Cedric Madianga cr1 |= STM32F4_I2C_CR1_ACK; 546*62817fc8SM'boumba Cedric Madianga cr1 &= ~STM32F4_I2C_CR1_POS; 547*62817fc8SM'boumba Cedric Madianga writel_relaxed(cr1, i2c_dev->base + STM32F4_I2C_CR1); 548*62817fc8SM'boumba Cedric Madianga 549*62817fc8SM'boumba Cedric Madianga readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2); 550*62817fc8SM'boumba Cedric Madianga break; 551*62817fc8SM'boumba Cedric Madianga } 552*62817fc8SM'boumba Cedric Madianga } 553*62817fc8SM'boumba Cedric Madianga 554*62817fc8SM'boumba Cedric Madianga /** 555*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_isr_event() - Interrupt routine for I2C bus event 556*62817fc8SM'boumba Cedric Madianga * @irq: interrupt number 557*62817fc8SM'boumba Cedric Madianga * @data: Controller's private data 558*62817fc8SM'boumba Cedric Madianga */ 559*62817fc8SM'boumba Cedric Madianga static irqreturn_t stm32f4_i2c_isr_event(int irq, void *data) 560*62817fc8SM'boumba Cedric Madianga { 561*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev *i2c_dev = data; 562*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 563*62817fc8SM'boumba Cedric Madianga u32 possible_status = STM32F4_I2C_SR1_ITEVTEN_MASK; 564*62817fc8SM'boumba Cedric Madianga u32 status, ien, event, cr2; 565*62817fc8SM'boumba Cedric Madianga 566*62817fc8SM'boumba Cedric Madianga cr2 = readl_relaxed(i2c_dev->base + STM32F4_I2C_CR2); 567*62817fc8SM'boumba Cedric Madianga ien = cr2 & STM32F4_I2C_CR2_IRQ_MASK; 568*62817fc8SM'boumba Cedric Madianga 569*62817fc8SM'boumba Cedric Madianga /* Update possible_status if buffer interrupt is enabled */ 570*62817fc8SM'boumba Cedric Madianga if (ien & STM32F4_I2C_CR2_ITBUFEN) 571*62817fc8SM'boumba Cedric Madianga possible_status |= STM32F4_I2C_SR1_ITBUFEN_MASK; 572*62817fc8SM'boumba Cedric Madianga 573*62817fc8SM'boumba Cedric Madianga status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1); 574*62817fc8SM'boumba Cedric Madianga event = status & possible_status; 575*62817fc8SM'boumba Cedric Madianga if (!event) { 576*62817fc8SM'boumba Cedric Madianga dev_dbg(i2c_dev->dev, 577*62817fc8SM'boumba Cedric Madianga "spurious evt irq (status=0x%08x, ien=0x%08x)\n", 578*62817fc8SM'boumba Cedric Madianga status, ien); 579*62817fc8SM'boumba Cedric Madianga return IRQ_NONE; 580*62817fc8SM'boumba Cedric Madianga } 581*62817fc8SM'boumba Cedric Madianga 582*62817fc8SM'boumba Cedric Madianga /* Start condition generated */ 583*62817fc8SM'boumba Cedric Madianga if (event & STM32F4_I2C_SR1_SB) 584*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_write_byte(i2c_dev, msg->addr); 585*62817fc8SM'boumba Cedric Madianga 586*62817fc8SM'boumba Cedric Madianga /* I2C Address sent */ 587*62817fc8SM'boumba Cedric Madianga if (event & STM32F4_I2C_SR1_ADDR) { 588*62817fc8SM'boumba Cedric Madianga if (msg->addr & I2C_M_RD) 589*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_handle_rx_addr(i2c_dev); 590*62817fc8SM'boumba Cedric Madianga else 591*62817fc8SM'boumba Cedric Madianga readl_relaxed(i2c_dev->base + STM32F4_I2C_SR2); 592*62817fc8SM'boumba Cedric Madianga 593*62817fc8SM'boumba Cedric Madianga /* 594*62817fc8SM'boumba Cedric Madianga * Enable buffer interrupts for RX not empty and TX empty 595*62817fc8SM'boumba Cedric Madianga * events 596*62817fc8SM'boumba Cedric Madianga */ 597*62817fc8SM'boumba Cedric Madianga cr2 |= STM32F4_I2C_CR2_ITBUFEN; 598*62817fc8SM'boumba Cedric Madianga writel_relaxed(cr2, i2c_dev->base + STM32F4_I2C_CR2); 599*62817fc8SM'boumba Cedric Madianga } 600*62817fc8SM'boumba Cedric Madianga 601*62817fc8SM'boumba Cedric Madianga /* TX empty */ 602*62817fc8SM'boumba Cedric Madianga if ((event & STM32F4_I2C_SR1_TXE) && !(msg->addr & I2C_M_RD)) 603*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_handle_write(i2c_dev); 604*62817fc8SM'boumba Cedric Madianga 605*62817fc8SM'boumba Cedric Madianga /* RX not empty */ 606*62817fc8SM'boumba Cedric Madianga if ((event & STM32F4_I2C_SR1_RXNE) && (msg->addr & I2C_M_RD)) 607*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_handle_read(i2c_dev); 608*62817fc8SM'boumba Cedric Madianga 609*62817fc8SM'boumba Cedric Madianga /* 610*62817fc8SM'boumba Cedric Madianga * The BTF (Byte Transfer finished) event occurs when: 611*62817fc8SM'boumba Cedric Madianga * - in reception : a new byte is received in the shift register 612*62817fc8SM'boumba Cedric Madianga * but the previous byte has not been read yet from data register 613*62817fc8SM'boumba Cedric Madianga * - in transmission: a new byte should be sent but the data register 614*62817fc8SM'boumba Cedric Madianga * has not been written yet 615*62817fc8SM'boumba Cedric Madianga */ 616*62817fc8SM'boumba Cedric Madianga if (event & STM32F4_I2C_SR1_BTF) { 617*62817fc8SM'boumba Cedric Madianga if (msg->addr & I2C_M_RD) 618*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_handle_rx_done(i2c_dev); 619*62817fc8SM'boumba Cedric Madianga else 620*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_handle_write(i2c_dev); 621*62817fc8SM'boumba Cedric Madianga } 622*62817fc8SM'boumba Cedric Madianga 623*62817fc8SM'boumba Cedric Madianga return IRQ_HANDLED; 624*62817fc8SM'boumba Cedric Madianga } 625*62817fc8SM'boumba Cedric Madianga 626*62817fc8SM'boumba Cedric Madianga /** 627*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_isr_error() - Interrupt routine for I2C bus error 628*62817fc8SM'boumba Cedric Madianga * @irq: interrupt number 629*62817fc8SM'boumba Cedric Madianga * @data: Controller's private data 630*62817fc8SM'boumba Cedric Madianga */ 631*62817fc8SM'boumba Cedric Madianga static irqreturn_t stm32f4_i2c_isr_error(int irq, void *data) 632*62817fc8SM'boumba Cedric Madianga { 633*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev *i2c_dev = data; 634*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *msg = &i2c_dev->msg; 635*62817fc8SM'boumba Cedric Madianga void __iomem *reg; 636*62817fc8SM'boumba Cedric Madianga u32 status; 637*62817fc8SM'boumba Cedric Madianga 638*62817fc8SM'boumba Cedric Madianga status = readl_relaxed(i2c_dev->base + STM32F4_I2C_SR1); 639*62817fc8SM'boumba Cedric Madianga 640*62817fc8SM'boumba Cedric Madianga /* Arbitration lost */ 641*62817fc8SM'boumba Cedric Madianga if (status & STM32F4_I2C_SR1_ARLO) { 642*62817fc8SM'boumba Cedric Madianga status &= ~STM32F4_I2C_SR1_ARLO; 643*62817fc8SM'boumba Cedric Madianga writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1); 644*62817fc8SM'boumba Cedric Madianga msg->result = -EAGAIN; 645*62817fc8SM'boumba Cedric Madianga } 646*62817fc8SM'boumba Cedric Madianga 647*62817fc8SM'boumba Cedric Madianga /* 648*62817fc8SM'boumba Cedric Madianga * Acknowledge failure: 649*62817fc8SM'boumba Cedric Madianga * In master transmitter mode a Stop must be generated by software 650*62817fc8SM'boumba Cedric Madianga */ 651*62817fc8SM'boumba Cedric Madianga if (status & STM32F4_I2C_SR1_AF) { 652*62817fc8SM'boumba Cedric Madianga if (!(msg->addr & I2C_M_RD)) { 653*62817fc8SM'boumba Cedric Madianga reg = i2c_dev->base + STM32F4_I2C_CR1; 654*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_STOP); 655*62817fc8SM'boumba Cedric Madianga } 656*62817fc8SM'boumba Cedric Madianga status &= ~STM32F4_I2C_SR1_AF; 657*62817fc8SM'boumba Cedric Madianga writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1); 658*62817fc8SM'boumba Cedric Madianga msg->result = -EIO; 659*62817fc8SM'boumba Cedric Madianga } 660*62817fc8SM'boumba Cedric Madianga 661*62817fc8SM'boumba Cedric Madianga /* Bus error */ 662*62817fc8SM'boumba Cedric Madianga if (status & STM32F4_I2C_SR1_BERR) { 663*62817fc8SM'boumba Cedric Madianga status &= ~STM32F4_I2C_SR1_BERR; 664*62817fc8SM'boumba Cedric Madianga writel_relaxed(status, i2c_dev->base + STM32F4_I2C_SR1); 665*62817fc8SM'boumba Cedric Madianga msg->result = -EIO; 666*62817fc8SM'boumba Cedric Madianga } 667*62817fc8SM'boumba Cedric Madianga 668*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_disable_irq(i2c_dev); 669*62817fc8SM'boumba Cedric Madianga complete(&i2c_dev->complete); 670*62817fc8SM'boumba Cedric Madianga 671*62817fc8SM'boumba Cedric Madianga return IRQ_HANDLED; 672*62817fc8SM'boumba Cedric Madianga } 673*62817fc8SM'boumba Cedric Madianga 674*62817fc8SM'boumba Cedric Madianga /** 675*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_xfer_msg() - Transfer a single I2C message 676*62817fc8SM'boumba Cedric Madianga * @i2c_dev: Controller's private data 677*62817fc8SM'boumba Cedric Madianga * @msg: I2C message to transfer 678*62817fc8SM'boumba Cedric Madianga * @is_first: first message of the sequence 679*62817fc8SM'boumba Cedric Madianga * @is_last: last message of the sequence 680*62817fc8SM'boumba Cedric Madianga */ 681*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_xfer_msg(struct stm32f4_i2c_dev *i2c_dev, 682*62817fc8SM'boumba Cedric Madianga struct i2c_msg *msg, bool is_first, 683*62817fc8SM'boumba Cedric Madianga bool is_last) 684*62817fc8SM'boumba Cedric Madianga { 685*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_msg *f4_msg = &i2c_dev->msg; 686*62817fc8SM'boumba Cedric Madianga void __iomem *reg = i2c_dev->base + STM32F4_I2C_CR1; 687*62817fc8SM'boumba Cedric Madianga unsigned long timeout; 688*62817fc8SM'boumba Cedric Madianga u32 mask; 689*62817fc8SM'boumba Cedric Madianga int ret; 690*62817fc8SM'boumba Cedric Madianga 691*62817fc8SM'boumba Cedric Madianga f4_msg->addr = i2c_8bit_addr_from_msg(msg); 692*62817fc8SM'boumba Cedric Madianga f4_msg->buf = msg->buf; 693*62817fc8SM'boumba Cedric Madianga f4_msg->count = msg->len; 694*62817fc8SM'boumba Cedric Madianga f4_msg->result = 0; 695*62817fc8SM'boumba Cedric Madianga f4_msg->stop = is_last; 696*62817fc8SM'boumba Cedric Madianga 697*62817fc8SM'boumba Cedric Madianga reinit_completion(&i2c_dev->complete); 698*62817fc8SM'boumba Cedric Madianga 699*62817fc8SM'boumba Cedric Madianga /* Enable events and errors interrupts */ 700*62817fc8SM'boumba Cedric Madianga mask = STM32F4_I2C_CR2_ITEVTEN | STM32F4_I2C_CR2_ITERREN; 701*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(i2c_dev->base + STM32F4_I2C_CR2, mask); 702*62817fc8SM'boumba Cedric Madianga 703*62817fc8SM'boumba Cedric Madianga if (is_first) { 704*62817fc8SM'boumba Cedric Madianga ret = stm32f4_i2c_wait_free_bus(i2c_dev); 705*62817fc8SM'boumba Cedric Madianga if (ret) 706*62817fc8SM'boumba Cedric Madianga return ret; 707*62817fc8SM'boumba Cedric Madianga 708*62817fc8SM'boumba Cedric Madianga /* START generation */ 709*62817fc8SM'boumba Cedric Madianga stm32f4_i2c_set_bits(reg, STM32F4_I2C_CR1_START); 710*62817fc8SM'boumba Cedric Madianga } 711*62817fc8SM'boumba Cedric Madianga 712*62817fc8SM'boumba Cedric Madianga timeout = wait_for_completion_timeout(&i2c_dev->complete, 713*62817fc8SM'boumba Cedric Madianga i2c_dev->adap.timeout); 714*62817fc8SM'boumba Cedric Madianga ret = f4_msg->result; 715*62817fc8SM'boumba Cedric Madianga 716*62817fc8SM'boumba Cedric Madianga if (!timeout) 717*62817fc8SM'boumba Cedric Madianga ret = -ETIMEDOUT; 718*62817fc8SM'boumba Cedric Madianga 719*62817fc8SM'boumba Cedric Madianga return ret; 720*62817fc8SM'boumba Cedric Madianga } 721*62817fc8SM'boumba Cedric Madianga 722*62817fc8SM'boumba Cedric Madianga /** 723*62817fc8SM'boumba Cedric Madianga * stm32f4_i2c_xfer() - Transfer combined I2C message 724*62817fc8SM'boumba Cedric Madianga * @i2c_adap: Adapter pointer to the controller 725*62817fc8SM'boumba Cedric Madianga * @msgs: Pointer to data to be written. 726*62817fc8SM'boumba Cedric Madianga * @num: Number of messages to be executed 727*62817fc8SM'boumba Cedric Madianga */ 728*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], 729*62817fc8SM'boumba Cedric Madianga int num) 730*62817fc8SM'boumba Cedric Madianga { 731*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); 732*62817fc8SM'boumba Cedric Madianga int ret, i; 733*62817fc8SM'boumba Cedric Madianga 734*62817fc8SM'boumba Cedric Madianga ret = clk_enable(i2c_dev->clk); 735*62817fc8SM'boumba Cedric Madianga if (ret) { 736*62817fc8SM'boumba Cedric Madianga dev_err(i2c_dev->dev, "Failed to enable clock\n"); 737*62817fc8SM'boumba Cedric Madianga return ret; 738*62817fc8SM'boumba Cedric Madianga } 739*62817fc8SM'boumba Cedric Madianga 740*62817fc8SM'boumba Cedric Madianga for (i = 0; i < num && !ret; i++) 741*62817fc8SM'boumba Cedric Madianga ret = stm32f4_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, 742*62817fc8SM'boumba Cedric Madianga i == num - 1); 743*62817fc8SM'boumba Cedric Madianga 744*62817fc8SM'boumba Cedric Madianga clk_disable(i2c_dev->clk); 745*62817fc8SM'boumba Cedric Madianga 746*62817fc8SM'boumba Cedric Madianga return (ret < 0) ? ret : num; 747*62817fc8SM'boumba Cedric Madianga } 748*62817fc8SM'boumba Cedric Madianga 749*62817fc8SM'boumba Cedric Madianga static u32 stm32f4_i2c_func(struct i2c_adapter *adap) 750*62817fc8SM'boumba Cedric Madianga { 751*62817fc8SM'boumba Cedric Madianga return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 752*62817fc8SM'boumba Cedric Madianga } 753*62817fc8SM'boumba Cedric Madianga 754*62817fc8SM'boumba Cedric Madianga static struct i2c_algorithm stm32f4_i2c_algo = { 755*62817fc8SM'boumba Cedric Madianga .master_xfer = stm32f4_i2c_xfer, 756*62817fc8SM'boumba Cedric Madianga .functionality = stm32f4_i2c_func, 757*62817fc8SM'boumba Cedric Madianga }; 758*62817fc8SM'boumba Cedric Madianga 759*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_probe(struct platform_device *pdev) 760*62817fc8SM'boumba Cedric Madianga { 761*62817fc8SM'boumba Cedric Madianga struct device_node *np = pdev->dev.of_node; 762*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev *i2c_dev; 763*62817fc8SM'boumba Cedric Madianga struct resource *res; 764*62817fc8SM'boumba Cedric Madianga u32 irq_event, irq_error, clk_rate; 765*62817fc8SM'boumba Cedric Madianga struct i2c_adapter *adap; 766*62817fc8SM'boumba Cedric Madianga struct reset_control *rst; 767*62817fc8SM'boumba Cedric Madianga int ret; 768*62817fc8SM'boumba Cedric Madianga 769*62817fc8SM'boumba Cedric Madianga i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 770*62817fc8SM'boumba Cedric Madianga if (!i2c_dev) 771*62817fc8SM'boumba Cedric Madianga return -ENOMEM; 772*62817fc8SM'boumba Cedric Madianga 773*62817fc8SM'boumba Cedric Madianga res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 774*62817fc8SM'boumba Cedric Madianga i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); 775*62817fc8SM'boumba Cedric Madianga if (IS_ERR(i2c_dev->base)) 776*62817fc8SM'boumba Cedric Madianga return PTR_ERR(i2c_dev->base); 777*62817fc8SM'boumba Cedric Madianga 778*62817fc8SM'boumba Cedric Madianga irq_event = irq_of_parse_and_map(np, 0); 779*62817fc8SM'boumba Cedric Madianga if (!irq_event) { 780*62817fc8SM'boumba Cedric Madianga dev_err(&pdev->dev, "IRQ event missing or invalid\n"); 781*62817fc8SM'boumba Cedric Madianga return -EINVAL; 782*62817fc8SM'boumba Cedric Madianga } 783*62817fc8SM'boumba Cedric Madianga 784*62817fc8SM'boumba Cedric Madianga irq_error = irq_of_parse_and_map(np, 1); 785*62817fc8SM'boumba Cedric Madianga if (!irq_error) { 786*62817fc8SM'boumba Cedric Madianga dev_err(&pdev->dev, "IRQ error missing or invalid\n"); 787*62817fc8SM'boumba Cedric Madianga return -EINVAL; 788*62817fc8SM'boumba Cedric Madianga } 789*62817fc8SM'boumba Cedric Madianga 790*62817fc8SM'boumba Cedric Madianga i2c_dev->clk = devm_clk_get(&pdev->dev, NULL); 791*62817fc8SM'boumba Cedric Madianga if (IS_ERR(i2c_dev->clk)) { 792*62817fc8SM'boumba Cedric Madianga dev_err(&pdev->dev, "Error: Missing controller clock\n"); 793*62817fc8SM'boumba Cedric Madianga return PTR_ERR(i2c_dev->clk); 794*62817fc8SM'boumba Cedric Madianga } 795*62817fc8SM'boumba Cedric Madianga ret = clk_prepare_enable(i2c_dev->clk); 796*62817fc8SM'boumba Cedric Madianga if (ret) { 797*62817fc8SM'boumba Cedric Madianga dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); 798*62817fc8SM'boumba Cedric Madianga return ret; 799*62817fc8SM'boumba Cedric Madianga } 800*62817fc8SM'boumba Cedric Madianga 801*62817fc8SM'boumba Cedric Madianga rst = devm_reset_control_get(&pdev->dev, NULL); 802*62817fc8SM'boumba Cedric Madianga if (IS_ERR(rst)) { 803*62817fc8SM'boumba Cedric Madianga dev_err(&pdev->dev, "Error: Missing controller reset\n"); 804*62817fc8SM'boumba Cedric Madianga ret = PTR_ERR(rst); 805*62817fc8SM'boumba Cedric Madianga goto clk_free; 806*62817fc8SM'boumba Cedric Madianga } 807*62817fc8SM'boumba Cedric Madianga reset_control_assert(rst); 808*62817fc8SM'boumba Cedric Madianga udelay(2); 809*62817fc8SM'boumba Cedric Madianga reset_control_deassert(rst); 810*62817fc8SM'boumba Cedric Madianga 811*62817fc8SM'boumba Cedric Madianga i2c_dev->speed = STM32F4_I2C_SPEED_STANDARD; 812*62817fc8SM'boumba Cedric Madianga ret = of_property_read_u32(np, "clock-frequency", &clk_rate); 813*62817fc8SM'boumba Cedric Madianga if (!ret && clk_rate >= 400000) 814*62817fc8SM'boumba Cedric Madianga i2c_dev->speed = STM32F4_I2C_SPEED_FAST; 815*62817fc8SM'boumba Cedric Madianga 816*62817fc8SM'boumba Cedric Madianga i2c_dev->dev = &pdev->dev; 817*62817fc8SM'boumba Cedric Madianga 818*62817fc8SM'boumba Cedric Madianga ret = devm_request_irq(&pdev->dev, irq_event, stm32f4_i2c_isr_event, 0, 819*62817fc8SM'boumba Cedric Madianga pdev->name, i2c_dev); 820*62817fc8SM'boumba Cedric Madianga if (ret) { 821*62817fc8SM'boumba Cedric Madianga dev_err(&pdev->dev, "Failed to request irq event %i\n", 822*62817fc8SM'boumba Cedric Madianga irq_event); 823*62817fc8SM'boumba Cedric Madianga goto clk_free; 824*62817fc8SM'boumba Cedric Madianga } 825*62817fc8SM'boumba Cedric Madianga 826*62817fc8SM'boumba Cedric Madianga ret = devm_request_irq(&pdev->dev, irq_error, stm32f4_i2c_isr_error, 0, 827*62817fc8SM'boumba Cedric Madianga pdev->name, i2c_dev); 828*62817fc8SM'boumba Cedric Madianga if (ret) { 829*62817fc8SM'boumba Cedric Madianga dev_err(&pdev->dev, "Failed to request irq error %i\n", 830*62817fc8SM'boumba Cedric Madianga irq_error); 831*62817fc8SM'boumba Cedric Madianga goto clk_free; 832*62817fc8SM'boumba Cedric Madianga } 833*62817fc8SM'boumba Cedric Madianga 834*62817fc8SM'boumba Cedric Madianga ret = stm32f4_i2c_hw_config(i2c_dev); 835*62817fc8SM'boumba Cedric Madianga if (ret) 836*62817fc8SM'boumba Cedric Madianga goto clk_free; 837*62817fc8SM'boumba Cedric Madianga 838*62817fc8SM'boumba Cedric Madianga adap = &i2c_dev->adap; 839*62817fc8SM'boumba Cedric Madianga i2c_set_adapdata(adap, i2c_dev); 840*62817fc8SM'boumba Cedric Madianga snprintf(adap->name, sizeof(adap->name), "STM32 I2C(%pa)", &res->start); 841*62817fc8SM'boumba Cedric Madianga adap->owner = THIS_MODULE; 842*62817fc8SM'boumba Cedric Madianga adap->timeout = 2 * HZ; 843*62817fc8SM'boumba Cedric Madianga adap->retries = 0; 844*62817fc8SM'boumba Cedric Madianga adap->algo = &stm32f4_i2c_algo; 845*62817fc8SM'boumba Cedric Madianga adap->dev.parent = &pdev->dev; 846*62817fc8SM'boumba Cedric Madianga adap->dev.of_node = pdev->dev.of_node; 847*62817fc8SM'boumba Cedric Madianga 848*62817fc8SM'boumba Cedric Madianga init_completion(&i2c_dev->complete); 849*62817fc8SM'boumba Cedric Madianga 850*62817fc8SM'boumba Cedric Madianga ret = i2c_add_adapter(adap); 851*62817fc8SM'boumba Cedric Madianga if (ret) 852*62817fc8SM'boumba Cedric Madianga goto clk_free; 853*62817fc8SM'boumba Cedric Madianga 854*62817fc8SM'boumba Cedric Madianga platform_set_drvdata(pdev, i2c_dev); 855*62817fc8SM'boumba Cedric Madianga 856*62817fc8SM'boumba Cedric Madianga clk_disable(i2c_dev->clk); 857*62817fc8SM'boumba Cedric Madianga 858*62817fc8SM'boumba Cedric Madianga dev_info(i2c_dev->dev, "STM32F4 I2C driver registered\n"); 859*62817fc8SM'boumba Cedric Madianga 860*62817fc8SM'boumba Cedric Madianga return 0; 861*62817fc8SM'boumba Cedric Madianga 862*62817fc8SM'boumba Cedric Madianga clk_free: 863*62817fc8SM'boumba Cedric Madianga clk_disable_unprepare(i2c_dev->clk); 864*62817fc8SM'boumba Cedric Madianga return ret; 865*62817fc8SM'boumba Cedric Madianga } 866*62817fc8SM'boumba Cedric Madianga 867*62817fc8SM'boumba Cedric Madianga static int stm32f4_i2c_remove(struct platform_device *pdev) 868*62817fc8SM'boumba Cedric Madianga { 869*62817fc8SM'boumba Cedric Madianga struct stm32f4_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 870*62817fc8SM'boumba Cedric Madianga 871*62817fc8SM'boumba Cedric Madianga i2c_del_adapter(&i2c_dev->adap); 872*62817fc8SM'boumba Cedric Madianga 873*62817fc8SM'boumba Cedric Madianga clk_unprepare(i2c_dev->clk); 874*62817fc8SM'boumba Cedric Madianga 875*62817fc8SM'boumba Cedric Madianga return 0; 876*62817fc8SM'boumba Cedric Madianga } 877*62817fc8SM'boumba Cedric Madianga 878*62817fc8SM'boumba Cedric Madianga static const struct of_device_id stm32f4_i2c_match[] = { 879*62817fc8SM'boumba Cedric Madianga { .compatible = "st,stm32f4-i2c", }, 880*62817fc8SM'boumba Cedric Madianga {}, 881*62817fc8SM'boumba Cedric Madianga }; 882*62817fc8SM'boumba Cedric Madianga MODULE_DEVICE_TABLE(of, stm32f4_i2c_match); 883*62817fc8SM'boumba Cedric Madianga 884*62817fc8SM'boumba Cedric Madianga static struct platform_driver stm32f4_i2c_driver = { 885*62817fc8SM'boumba Cedric Madianga .driver = { 886*62817fc8SM'boumba Cedric Madianga .name = "stm32f4-i2c", 887*62817fc8SM'boumba Cedric Madianga .of_match_table = stm32f4_i2c_match, 888*62817fc8SM'boumba Cedric Madianga }, 889*62817fc8SM'boumba Cedric Madianga .probe = stm32f4_i2c_probe, 890*62817fc8SM'boumba Cedric Madianga .remove = stm32f4_i2c_remove, 891*62817fc8SM'boumba Cedric Madianga }; 892*62817fc8SM'boumba Cedric Madianga 893*62817fc8SM'boumba Cedric Madianga module_platform_driver(stm32f4_i2c_driver); 894*62817fc8SM'boumba Cedric Madianga 895*62817fc8SM'boumba Cedric Madianga MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>"); 896*62817fc8SM'boumba Cedric Madianga MODULE_DESCRIPTION("STMicroelectronics STM32F4 I2C driver"); 897*62817fc8SM'boumba Cedric Madianga MODULE_LICENSE("GPL v2"); 898