Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'i2c-for-6.9-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux

Pull more i2c updates from Wolfram Sang:
"Some more I2C updates after the dependencies have been merged now.

Plus a DT binding fix"

* tag 'i2c-for-6.9-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux:
dt-bindings: i2c: qcom,i2c-cci: Fix OV7251 'data-lanes' entries
i2c: muxes: pca954x: Allow sharing reset GPIO
i2c: nomadik: sort includes
i2c: nomadik: support Mobileye EyeQ5 I2C controller
i2c: nomadik: fetch i2c-transfer-timeout-us property from devicetree
i2c: nomadik: replace jiffies by ktime for FIFO flushing timeout
i2c: nomadik: support short xfer timeouts using waitqueue & hrtimer
i2c: nomadik: use bitops helpers
i2c: nomadik: simplify IRQ masking logic
i2c: nomadik: rename private struct pointers from dev to priv
dt-bindings: i2c: nomadik: add mobileye,eyeq5-i2c bindings and example

+514 -323
+1 -1
Documentation/devicetree/bindings/i2c/qcom,i2c-cci.yaml
··· 270 270 271 271 port { 272 272 ov7251_ep: endpoint { 273 - data-lanes = <0 1>; 273 + data-lanes = <0>; 274 274 link-frequencies = /bits/ 64 <240000000 319200000>; 275 275 remote-endpoint = <&csiphy3_ep>; 276 276 };
+43 -6
Documentation/devicetree/bindings/i2c/st,nomadik-i2c.yaml
··· 14 14 maintainers: 15 15 - Linus Walleij <linus.walleij@linaro.org> 16 16 17 - allOf: 18 - - $ref: /schemas/i2c/i2c-controller.yaml# 19 - 20 17 # Need a custom select here or 'arm,primecell' will match on lots of nodes 21 18 select: 22 19 properties: ··· 21 24 contains: 22 25 enum: 23 26 - st,nomadik-i2c 27 + - mobileye,eyeq5-i2c 24 28 required: 25 29 - compatible 26 30 27 31 properties: 28 32 compatible: 29 33 oneOf: 30 - # The variant found in STn8815 31 34 - items: 32 35 - const: st,nomadik-i2c 33 36 - const: arm,primecell 34 - # The variant found in DB8500 35 37 - items: 36 38 - const: stericsson,db8500-i2c 37 39 - const: st,nomadik-i2c 40 + - const: arm,primecell 41 + - items: 42 + - const: mobileye,eyeq5-i2c 38 43 - const: arm,primecell 39 44 40 45 reg: ··· 54 55 - items: 55 56 - const: mclk 56 57 - const: apb_pclk 57 - # Clock name in DB8500 58 + # Clock name in DB8500 or EyeQ5 58 59 - items: 59 60 - const: i2cclk 60 61 - const: apb_pclk ··· 69 70 minimum: 1 70 71 maximum: 400000 71 72 73 + mobileye,olb: 74 + $ref: /schemas/types.yaml#/definitions/phandle-array 75 + items: 76 + - items: 77 + - description: Phandle to OLB system controller node. 78 + - description: Platform-wide controller ID (integer starting from zero). 79 + description: 80 + The phandle pointing to OLB system controller node, with the I2C 81 + controller index. 82 + 72 83 required: 73 84 - compatible 74 85 - reg ··· 87 78 - clock-names 88 79 89 80 unevaluatedProperties: false 81 + 82 + allOf: 83 + - $ref: /schemas/i2c/i2c-controller.yaml# 84 + - if: 85 + properties: 86 + compatible: 87 + contains: 88 + const: mobileye,eyeq5-i2c 89 + then: 90 + required: 91 + - mobileye,olb 92 + else: 93 + properties: 94 + mobileye,olb: false 90 95 91 96 examples: 92 97 - | ··· 133 110 #size-cells = <0>; 134 111 clocks = <&i2c0clk>, <&pclki2c0>; 135 112 clock-names = "mclk", "apb_pclk"; 113 + }; 114 + - | 115 + #include <dt-bindings/interrupt-controller/mips-gic.h> 116 + i2c@300000 { 117 + compatible = "mobileye,eyeq5-i2c", "arm,primecell"; 118 + reg = <0x300000 0x1000>; 119 + interrupt-parent = <&gic>; 120 + interrupts = <GIC_SHARED 1 IRQ_TYPE_LEVEL_HIGH>; 121 + clock-frequency = <400000>; 122 + #address-cells = <1>; 123 + #size-cells = <0>; 124 + clocks = <&i2c_ser_clk>, <&i2c_clk>; 125 + clock-names = "i2cclk", "apb_pclk"; 126 + mobileye,olb = <&olb 0>; 136 127 }; 137 128 138 129 ...
+432 -308
drivers/i2c/busses/i2c-nomadik.c
··· 6 6 * I2C master mode controller driver, used in Nomadik 8815 7 7 * and Ux500 platforms. 8 8 * 9 + * The Mobileye EyeQ5 platform is also supported; it uses 10 + * the same Ux500/DB8500 IP block with two quirks: 11 + * - The memory bus only supports 32-bit accesses. 12 + * - A register must be configured for the I2C speed mode; 13 + * it is located in a shared register region called OLB. 14 + * 9 15 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com> 10 16 * Author: Sachin Verma <sachin.verma@st.com> 11 17 */ 12 - #include <linux/init.h> 13 - #include <linux/module.h> 14 18 #include <linux/amba/bus.h> 15 - #include <linux/slab.h> 16 - #include <linux/interrupt.h> 17 - #include <linux/i2c.h> 18 - #include <linux/err.h> 19 + #include <linux/bitfield.h> 19 20 #include <linux/clk.h> 21 + #include <linux/err.h> 22 + #include <linux/i2c.h> 23 + #include <linux/init.h> 24 + #include <linux/interrupt.h> 20 25 #include <linux/io.h> 21 - #include <linux/pm_runtime.h> 26 + #include <linux/mfd/syscon.h> 27 + #include <linux/module.h> 22 28 #include <linux/of.h> 23 29 #include <linux/pinctrl/consumer.h> 30 + #include <linux/pm_runtime.h> 31 + #include <linux/regmap.h> 32 + #include <linux/slab.h> 24 33 25 34 #define DRIVER_NAME "nmk-i2c" 26 35 ··· 51 42 #define I2C_ICR (0x038) 52 43 53 44 /* Control registers */ 54 - #define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */ 55 - #define I2C_CR_OM (0x3 << 1) /* Operating mode */ 56 - #define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */ 57 - #define I2C_CR_SM (0x3 << 4) /* Speed mode */ 58 - #define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */ 59 - #define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */ 60 - #define I2C_CR_FRX (0x1 << 8) /* Flush Receive */ 61 - #define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */ 62 - #define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */ 63 - #define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */ 64 - #define I2C_CR_LM (0x1 << 12) /* Loopback mode */ 65 - #define I2C_CR_FON (0x3 << 13) /* Filtering on */ 66 - #define I2C_CR_FS (0x3 << 15) /* Force stop enable */ 45 + #define I2C_CR_PE BIT(0) /* Peripheral Enable */ 46 + #define I2C_CR_OM GENMASK(2, 1) /* Operating mode */ 47 + #define I2C_CR_SAM BIT(3) /* Slave addressing mode */ 48 + #define I2C_CR_SM GENMASK(5, 4) /* Speed mode */ 49 + #define I2C_CR_SGCM BIT(6) /* Slave general call mode */ 50 + #define I2C_CR_FTX BIT(7) /* Flush Transmit */ 51 + #define I2C_CR_FRX BIT(8) /* Flush Receive */ 52 + #define I2C_CR_DMA_TX_EN BIT(9) /* DMA Tx enable */ 53 + #define I2C_CR_DMA_RX_EN BIT(10) /* DMA Rx Enable */ 54 + #define I2C_CR_DMA_SLE BIT(11) /* DMA sync. logic enable */ 55 + #define I2C_CR_LM BIT(12) /* Loopback mode */ 56 + #define I2C_CR_FON GENMASK(14, 13) /* Filtering on */ 57 + #define I2C_CR_FS GENMASK(16, 15) /* Force stop enable */ 58 + 59 + /* Slave control register (SCR) */ 60 + #define I2C_SCR_SLSU GENMASK(31, 16) /* Slave data setup time */ 67 61 68 62 /* Master controller (MCR) register */ 69 - #define I2C_MCR_OP (0x1 << 0) /* Operation */ 70 - #define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */ 71 - #define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */ 72 - #define I2C_MCR_SB (0x1 << 11) /* Extended address */ 73 - #define I2C_MCR_AM (0x3 << 12) /* Address type */ 74 - #define I2C_MCR_STOP (0x1 << 14) /* Stop condition */ 75 - #define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */ 63 + #define I2C_MCR_OP BIT(0) /* Operation */ 64 + #define I2C_MCR_A7 GENMASK(7, 1) /* 7-bit address */ 65 + #define I2C_MCR_EA10 GENMASK(10, 8) /* 10-bit Extended address */ 66 + #define I2C_MCR_SB BIT(11) /* Extended address */ 67 + #define I2C_MCR_AM GENMASK(13, 12) /* Address type */ 68 + #define I2C_MCR_STOP BIT(14) /* Stop condition */ 69 + #define I2C_MCR_LENGTH GENMASK(25, 15) /* Transaction length */ 76 70 77 71 /* Status register (SR) */ 78 - #define I2C_SR_OP (0x3 << 0) /* Operation */ 79 - #define I2C_SR_STATUS (0x3 << 2) /* controller status */ 80 - #define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */ 81 - #define I2C_SR_TYPE (0x3 << 7) /* Receive type */ 82 - #define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */ 72 + #define I2C_SR_OP GENMASK(1, 0) /* Operation */ 73 + #define I2C_SR_STATUS GENMASK(3, 2) /* controller status */ 74 + #define I2C_SR_CAUSE GENMASK(6, 4) /* Abort cause */ 75 + #define I2C_SR_TYPE GENMASK(8, 7) /* Receive type */ 76 + #define I2C_SR_LENGTH GENMASK(19, 9) /* Transfer length */ 77 + 78 + /* Baud-rate counter register (BRCR) */ 79 + #define I2C_BRCR_BRCNT1 GENMASK(31, 16) /* Baud-rate counter 1 */ 80 + #define I2C_BRCR_BRCNT2 GENMASK(15, 0) /* Baud-rate counter 2 */ 83 81 84 82 /* Interrupt mask set/clear (IMSCR) bits */ 85 - #define I2C_IT_TXFE (0x1 << 0) 86 - #define I2C_IT_TXFNE (0x1 << 1) 87 - #define I2C_IT_TXFF (0x1 << 2) 88 - #define I2C_IT_TXFOVR (0x1 << 3) 89 - #define I2C_IT_RXFE (0x1 << 4) 90 - #define I2C_IT_RXFNF (0x1 << 5) 91 - #define I2C_IT_RXFF (0x1 << 6) 92 - #define I2C_IT_RFSR (0x1 << 16) 93 - #define I2C_IT_RFSE (0x1 << 17) 94 - #define I2C_IT_WTSR (0x1 << 18) 95 - #define I2C_IT_MTD (0x1 << 19) 96 - #define I2C_IT_STD (0x1 << 20) 97 - #define I2C_IT_MAL (0x1 << 24) 98 - #define I2C_IT_BERR (0x1 << 25) 99 - #define I2C_IT_MTDWS (0x1 << 28) 100 - 101 - #define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask)) 83 + #define I2C_IT_TXFE BIT(0) 84 + #define I2C_IT_TXFNE BIT(1) 85 + #define I2C_IT_TXFF BIT(2) 86 + #define I2C_IT_TXFOVR BIT(3) 87 + #define I2C_IT_RXFE BIT(4) 88 + #define I2C_IT_RXFNF BIT(5) 89 + #define I2C_IT_RXFF BIT(6) 90 + #define I2C_IT_RFSR BIT(16) 91 + #define I2C_IT_RFSE BIT(17) 92 + #define I2C_IT_WTSR BIT(18) 93 + #define I2C_IT_MTD BIT(19) 94 + #define I2C_IT_STD BIT(20) 95 + #define I2C_IT_MAL BIT(24) 96 + #define I2C_IT_BERR BIT(25) 97 + #define I2C_IT_MTDWS BIT(28) 102 98 103 99 /* some bits in ICR are reserved */ 104 100 #define I2C_CLEAR_ALL_INTS 0x131f007f 105 - 106 - /* first three msb bits are reserved */ 107 - #define IRQ_MASK(mask) (mask & 0x1fffffff) 108 101 109 102 /* maximum threshold value */ 110 103 #define MAX_I2C_FIFO_THRESHOLD 15 ··· 116 105 I2C_FREQ_MODE_FAST, /* up to 400 Kb/s */ 117 106 I2C_FREQ_MODE_HIGH_SPEED, /* up to 3.4 Mb/s */ 118 107 I2C_FREQ_MODE_FAST_PLUS, /* up to 1 Mb/s */ 108 + }; 109 + 110 + /* Mobileye EyeQ5 offset into a shared register region (called OLB) */ 111 + #define NMK_I2C_EYEQ5_OLB_IOCR2 0x0B8 112 + 113 + enum i2c_eyeq5_speed { 114 + I2C_EYEQ5_SPEED_FAST, 115 + I2C_EYEQ5_SPEED_FAST_PLUS, 116 + I2C_EYEQ5_SPEED_HIGH_SPEED, 119 117 }; 120 118 121 119 /** ··· 149 129 I2C_NO_OPERATION = 0xff, 150 130 I2C_WRITE = 0x00, 151 131 I2C_READ = 0x01 132 + }; 133 + 134 + enum i2c_operating_mode { 135 + I2C_OM_SLAVE, 136 + I2C_OM_MASTER, 137 + I2C_OM_MASTER_OR_SLAVE, 152 138 }; 153 139 154 140 /** ··· 185 159 * @clk_freq: clock frequency for the operation mode 186 160 * @tft: Tx FIFO Threshold in bytes 187 161 * @rft: Rx FIFO Threshold in bytes 188 - * @timeout: Slave response timeout (ms) 162 + * @timeout_usecs: Slave response timeout 189 163 * @sm: speed mode 190 164 * @stop: stop condition. 191 - * @xfer_complete: acknowledge completion for a I2C message. 165 + * @xfer_wq: xfer done wait queue. 166 + * @xfer_done: xfer done boolean. 192 167 * @result: controller propogated result. 168 + * @has_32b_bus: controller is on a bus that only supports 32-bit accesses. 193 169 */ 194 170 struct nmk_i2c_dev { 195 171 struct i2c_vendor_data *vendor; ··· 204 176 u32 clk_freq; 205 177 unsigned char tft; 206 178 unsigned char rft; 207 - int timeout; 179 + u32 timeout_usecs; 208 180 enum i2c_freq_mode sm; 209 181 int stop; 210 - struct completion xfer_complete; 182 + struct wait_queue_head xfer_wq; 183 + bool xfer_done; 211 184 int result; 185 + bool has_32b_bus; 212 186 }; 213 187 214 188 /* controller's abort causes */ ··· 234 204 writel(readl(reg) & ~mask, reg); 235 205 } 236 206 207 + static inline u8 nmk_i2c_readb(const struct nmk_i2c_dev *priv, 208 + unsigned long reg) 209 + { 210 + if (priv->has_32b_bus) 211 + return readl(priv->virtbase + reg); 212 + else 213 + return readb(priv->virtbase + reg); 214 + } 215 + 216 + static inline void nmk_i2c_writeb(const struct nmk_i2c_dev *priv, u32 val, 217 + unsigned long reg) 218 + { 219 + if (priv->has_32b_bus) 220 + writel(val, priv->virtbase + reg); 221 + else 222 + writeb(val, priv->virtbase + reg); 223 + } 224 + 237 225 /** 238 226 * flush_i2c_fifo() - This function flushes the I2C FIFO 239 - * @dev: private data of I2C Driver 227 + * @priv: private data of I2C Driver 240 228 * 241 229 * This function flushes the I2C Tx and Rx FIFOs. It returns 242 230 * 0 on successful flushing of FIFO 243 231 */ 244 - static int flush_i2c_fifo(struct nmk_i2c_dev *dev) 232 + static int flush_i2c_fifo(struct nmk_i2c_dev *priv) 245 233 { 246 234 #define LOOP_ATTEMPTS 10 235 + ktime_t timeout; 247 236 int i; 248 - unsigned long timeout; 249 237 250 238 /* 251 239 * flush the transmit and receive FIFO. The flushing ··· 272 224 * bits, until then no one must access Tx, Rx FIFO and 273 225 * should poll on these bits waiting for the completion. 274 226 */ 275 - writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR); 227 + writel((I2C_CR_FTX | I2C_CR_FRX), priv->virtbase + I2C_CR); 276 228 277 229 for (i = 0; i < LOOP_ATTEMPTS; i++) { 278 - timeout = jiffies + dev->adap.timeout; 230 + timeout = ktime_add_us(ktime_get(), priv->timeout_usecs); 279 231 280 - while (!time_after(jiffies, timeout)) { 281 - if ((readl(dev->virtbase + I2C_CR) & 232 + while (ktime_after(timeout, ktime_get())) { 233 + if ((readl(priv->virtbase + I2C_CR) & 282 234 (I2C_CR_FTX | I2C_CR_FRX)) == 0) 283 - return 0; 235 + return 0; 284 236 } 285 237 } 286 238 287 - dev_err(&dev->adev->dev, 239 + dev_err(&priv->adev->dev, 288 240 "flushing operation timed out giving up after %d attempts", 289 241 LOOP_ATTEMPTS); 290 242 ··· 293 245 294 246 /** 295 247 * disable_all_interrupts() - Disable all interrupts of this I2c Bus 296 - * @dev: private data of I2C Driver 248 + * @priv: private data of I2C Driver 297 249 */ 298 - static void disable_all_interrupts(struct nmk_i2c_dev *dev) 250 + static void disable_all_interrupts(struct nmk_i2c_dev *priv) 299 251 { 300 - u32 mask = IRQ_MASK(0); 301 - writel(mask, dev->virtbase + I2C_IMSCR); 252 + writel(0, priv->virtbase + I2C_IMSCR); 302 253 } 303 254 304 255 /** 305 256 * clear_all_interrupts() - Clear all interrupts of I2C Controller 306 - * @dev: private data of I2C Driver 257 + * @priv: private data of I2C Driver 307 258 */ 308 - static void clear_all_interrupts(struct nmk_i2c_dev *dev) 259 + static void clear_all_interrupts(struct nmk_i2c_dev *priv) 309 260 { 310 - u32 mask; 311 - mask = IRQ_MASK(I2C_CLEAR_ALL_INTS); 312 - writel(mask, dev->virtbase + I2C_ICR); 261 + writel(I2C_CLEAR_ALL_INTS, priv->virtbase + I2C_ICR); 313 262 } 314 263 315 264 /** 316 265 * init_hw() - initialize the I2C hardware 317 - * @dev: private data of I2C Driver 266 + * @priv: private data of I2C Driver 318 267 */ 319 - static int init_hw(struct nmk_i2c_dev *dev) 268 + static int init_hw(struct nmk_i2c_dev *priv) 320 269 { 321 270 int stat; 322 271 323 - stat = flush_i2c_fifo(dev); 272 + stat = flush_i2c_fifo(priv); 324 273 if (stat) 325 274 goto exit; 326 275 327 276 /* disable the controller */ 328 - i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE); 277 + i2c_clr_bit(priv->virtbase + I2C_CR, I2C_CR_PE); 329 278 330 - disable_all_interrupts(dev); 279 + disable_all_interrupts(priv); 331 280 332 - clear_all_interrupts(dev); 281 + clear_all_interrupts(priv); 333 282 334 - dev->cli.operation = I2C_NO_OPERATION; 283 + priv->cli.operation = I2C_NO_OPERATION; 335 284 336 285 exit: 337 286 return stat; 338 287 } 339 288 340 289 /* enable peripheral, master mode operation */ 341 - #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE) 290 + #define DEFAULT_I2C_REG_CR (FIELD_PREP(I2C_CR_OM, I2C_OM_MASTER) | I2C_CR_PE) 291 + 292 + /* grab top three bits from extended I2C addresses */ 293 + #define ADR_3MSB_BITS GENMASK(9, 7) 342 294 343 295 /** 344 296 * load_i2c_mcr_reg() - load the MCR register 345 - * @dev: private data of controller 297 + * @priv: private data of controller 346 298 * @flags: message flags 347 299 */ 348 - static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev, u16 flags) 300 + static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *priv, u16 flags) 349 301 { 350 302 u32 mcr = 0; 351 303 unsigned short slave_adr_3msb_bits; 352 304 353 - mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1); 305 + mcr |= FIELD_PREP(I2C_MCR_A7, priv->cli.slave_adr); 354 306 355 307 if (unlikely(flags & I2C_M_TEN)) { 356 308 /* 10-bit address transaction */ 357 - mcr |= GEN_MASK(2, I2C_MCR_AM, 12); 309 + mcr |= FIELD_PREP(I2C_MCR_AM, 2); 358 310 /* 359 311 * Get the top 3 bits. 360 312 * EA10 represents extended address in MCR. This includes 361 313 * the extension (MSB bits) of the 7 bit address loaded 362 314 * in A7 363 315 */ 364 - slave_adr_3msb_bits = (dev->cli.slave_adr >> 7) & 0x7; 316 + slave_adr_3msb_bits = FIELD_GET(ADR_3MSB_BITS, 317 + priv->cli.slave_adr); 365 318 366 - mcr |= GEN_MASK(slave_adr_3msb_bits, I2C_MCR_EA10, 8); 319 + mcr |= FIELD_PREP(I2C_MCR_EA10, slave_adr_3msb_bits); 367 320 } else { 368 321 /* 7-bit address transaction */ 369 - mcr |= GEN_MASK(1, I2C_MCR_AM, 12); 322 + mcr |= FIELD_PREP(I2C_MCR_AM, 1); 370 323 } 371 324 372 325 /* start byte procedure not applied */ 373 - mcr |= GEN_MASK(0, I2C_MCR_SB, 11); 326 + mcr |= FIELD_PREP(I2C_MCR_SB, 0); 374 327 375 328 /* check the operation, master read/write? */ 376 - if (dev->cli.operation == I2C_WRITE) 377 - mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0); 329 + if (priv->cli.operation == I2C_WRITE) 330 + mcr |= FIELD_PREP(I2C_MCR_OP, I2C_WRITE); 378 331 else 379 - mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0); 332 + mcr |= FIELD_PREP(I2C_MCR_OP, I2C_READ); 380 333 381 334 /* stop or repeated start? */ 382 - if (dev->stop) 383 - mcr |= GEN_MASK(1, I2C_MCR_STOP, 14); 335 + if (priv->stop) 336 + mcr |= FIELD_PREP(I2C_MCR_STOP, 1); 384 337 else 385 - mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14)); 338 + mcr &= ~FIELD_PREP(I2C_MCR_STOP, 1); 386 339 387 - mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15); 340 + mcr |= FIELD_PREP(I2C_MCR_LENGTH, priv->cli.count); 388 341 389 342 return mcr; 390 343 } 391 344 392 345 /** 393 346 * setup_i2c_controller() - setup the controller 394 - * @dev: private data of controller 347 + * @priv: private data of controller 395 348 */ 396 - static void setup_i2c_controller(struct nmk_i2c_dev *dev) 349 + static void setup_i2c_controller(struct nmk_i2c_dev *priv) 397 350 { 398 351 u32 brcr1, brcr2; 399 352 u32 i2c_clk, div; 400 353 u32 ns; 401 354 u16 slsu; 402 355 403 - writel(0x0, dev->virtbase + I2C_CR); 404 - writel(0x0, dev->virtbase + I2C_HSMCR); 405 - writel(0x0, dev->virtbase + I2C_TFTR); 406 - writel(0x0, dev->virtbase + I2C_RFTR); 407 - writel(0x0, dev->virtbase + I2C_DMAR); 356 + writel(0x0, priv->virtbase + I2C_CR); 357 + writel(0x0, priv->virtbase + I2C_HSMCR); 358 + writel(0x0, priv->virtbase + I2C_TFTR); 359 + writel(0x0, priv->virtbase + I2C_RFTR); 360 + writel(0x0, priv->virtbase + I2C_DMAR); 408 361 409 - i2c_clk = clk_get_rate(dev->clk); 362 + i2c_clk = clk_get_rate(priv->clk); 410 363 411 364 /* 412 365 * set the slsu: ··· 422 373 * slsu = cycles / (1000000000 / f) + 1 423 374 */ 424 375 ns = DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk); 425 - switch (dev->sm) { 376 + switch (priv->sm) { 426 377 case I2C_FREQ_MODE_FAST: 427 378 case I2C_FREQ_MODE_FAST_PLUS: 428 379 slsu = DIV_ROUND_UP(100, ns); /* Fast */ ··· 437 388 } 438 389 slsu += 1; 439 390 440 - dev_dbg(&dev->adev->dev, "calculated SLSU = %04x\n", slsu); 441 - writel(slsu << 16, dev->virtbase + I2C_SCR); 391 + dev_dbg(&priv->adev->dev, "calculated SLSU = %04x\n", slsu); 392 + writel(FIELD_PREP(I2C_SCR_SLSU, slsu), priv->virtbase + I2C_SCR); 442 393 443 394 /* 444 395 * The spec says, in case of std. mode the divider is 445 396 * 2 whereas it is 3 for fast and fastplus mode of 446 397 * operation. TODO - high speed support. 447 398 */ 448 - div = (dev->clk_freq > I2C_MAX_STANDARD_MODE_FREQ) ? 3 : 2; 399 + div = (priv->clk_freq > I2C_MAX_STANDARD_MODE_FREQ) ? 3 : 2; 449 400 450 401 /* 451 402 * generate the mask for baud rate counters. The controller ··· 454 405 * plus operation. Currently we do not supprt high speed mode 455 406 * so set brcr1 to 0. 456 407 */ 457 - brcr1 = 0 << 16; 458 - brcr2 = (i2c_clk/(dev->clk_freq * div)) & 0xffff; 408 + brcr1 = FIELD_PREP(I2C_BRCR_BRCNT1, 0); 409 + brcr2 = FIELD_PREP(I2C_BRCR_BRCNT2, i2c_clk / (priv->clk_freq * div)); 459 410 460 411 /* set the baud rate counter register */ 461 - writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); 412 + writel((brcr1 | brcr2), priv->virtbase + I2C_BRCR); 462 413 463 414 /* 464 415 * set the speed mode. Currently we support ··· 466 417 * TODO - support for fast mode plus (up to 1Mb/s) 467 418 * and high speed (up to 3.4 Mb/s) 468 419 */ 469 - if (dev->sm > I2C_FREQ_MODE_FAST) { 470 - dev_err(&dev->adev->dev, 420 + if (priv->sm > I2C_FREQ_MODE_FAST) { 421 + dev_err(&priv->adev->dev, 471 422 "do not support this mode defaulting to std. mode\n"); 472 - brcr2 = i2c_clk / (I2C_MAX_STANDARD_MODE_FREQ * 2) & 0xffff; 473 - writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR); 474 - writel(I2C_FREQ_MODE_STANDARD << 4, 475 - dev->virtbase + I2C_CR); 423 + brcr2 = FIELD_PREP(I2C_BRCR_BRCNT2, 424 + i2c_clk / (I2C_MAX_STANDARD_MODE_FREQ * 2)); 425 + writel((brcr1 | brcr2), priv->virtbase + I2C_BRCR); 426 + writel(FIELD_PREP(I2C_CR_SM, I2C_FREQ_MODE_STANDARD), 427 + priv->virtbase + I2C_CR); 476 428 } 477 - writel(dev->sm << 4, dev->virtbase + I2C_CR); 429 + writel(FIELD_PREP(I2C_CR_SM, priv->sm), priv->virtbase + I2C_CR); 478 430 479 431 /* set the Tx and Rx FIFO threshold */ 480 - writel(dev->tft, dev->virtbase + I2C_TFTR); 481 - writel(dev->rft, dev->virtbase + I2C_RFTR); 432 + writel(priv->tft, priv->virtbase + I2C_TFTR); 433 + writel(priv->rft, priv->virtbase + I2C_RFTR); 434 + } 435 + 436 + static bool nmk_i2c_wait_xfer_done(struct nmk_i2c_dev *priv) 437 + { 438 + if (priv->timeout_usecs < jiffies_to_usecs(1)) { 439 + unsigned long timeout_usecs = priv->timeout_usecs; 440 + ktime_t timeout = ktime_set(0, timeout_usecs * NSEC_PER_USEC); 441 + 442 + wait_event_hrtimeout(priv->xfer_wq, priv->xfer_done, timeout); 443 + } else { 444 + unsigned long timeout = usecs_to_jiffies(priv->timeout_usecs); 445 + 446 + wait_event_timeout(priv->xfer_wq, priv->xfer_done, timeout); 447 + } 448 + 449 + return priv->xfer_done; 482 450 } 483 451 484 452 /** 485 453 * read_i2c() - Read from I2C client device 486 - * @dev: private data of I2C Driver 454 + * @priv: private data of I2C Driver 487 455 * @flags: message flags 488 456 * 489 457 * This function reads from i2c client device when controller is in 490 458 * master mode. There is a completion timeout. If there is no transfer 491 459 * before timeout error is returned. 492 460 */ 493 - static int read_i2c(struct nmk_i2c_dev *dev, u16 flags) 461 + static int read_i2c(struct nmk_i2c_dev *priv, u16 flags) 494 462 { 495 - int status = 0; 496 463 u32 mcr, irq_mask; 497 - unsigned long timeout; 464 + int status = 0; 465 + bool xfer_done; 498 466 499 - mcr = load_i2c_mcr_reg(dev, flags); 500 - writel(mcr, dev->virtbase + I2C_MCR); 467 + mcr = load_i2c_mcr_reg(priv, flags); 468 + writel(mcr, priv->virtbase + I2C_MCR); 501 469 502 470 /* load the current CR value */ 503 - writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, 504 - dev->virtbase + I2C_CR); 471 + writel(readl(priv->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, 472 + priv->virtbase + I2C_CR); 505 473 506 474 /* enable the controller */ 507 - i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE); 475 + i2c_set_bit(priv->virtbase + I2C_CR, I2C_CR_PE); 508 476 509 - init_completion(&dev->xfer_complete); 477 + init_waitqueue_head(&priv->xfer_wq); 478 + priv->xfer_done = false; 510 479 511 480 /* enable interrupts by setting the mask */ 512 481 irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF | 513 482 I2C_IT_MAL | I2C_IT_BERR); 514 483 515 - if (dev->stop || !dev->vendor->has_mtdws) 484 + if (priv->stop || !priv->vendor->has_mtdws) 516 485 irq_mask |= I2C_IT_MTD; 517 486 else 518 487 irq_mask |= I2C_IT_MTDWS; 519 488 520 - irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask); 489 + irq_mask &= I2C_CLEAR_ALL_INTS; 521 490 522 - writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask, 523 - dev->virtbase + I2C_IMSCR); 491 + writel(readl(priv->virtbase + I2C_IMSCR) | irq_mask, 492 + priv->virtbase + I2C_IMSCR); 524 493 525 - timeout = wait_for_completion_timeout( 526 - &dev->xfer_complete, dev->adap.timeout); 494 + xfer_done = nmk_i2c_wait_xfer_done(priv); 527 495 528 - if (timeout == 0) { 496 + if (!xfer_done) { 529 497 /* Controller timed out */ 530 - dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n", 531 - dev->cli.slave_adr); 498 + dev_err(&priv->adev->dev, "read from slave 0x%x timed out\n", 499 + priv->cli.slave_adr); 532 500 status = -ETIMEDOUT; 533 501 } 534 502 return status; 535 503 } 536 504 537 - static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes) 505 + static void fill_tx_fifo(struct nmk_i2c_dev *priv, int no_bytes) 538 506 { 539 507 int count; 540 508 541 509 for (count = (no_bytes - 2); 542 510 (count > 0) && 543 - (dev->cli.count != 0); 511 + (priv->cli.count != 0); 544 512 count--) { 545 513 /* write to the Tx FIFO */ 546 - writeb(*dev->cli.buffer, 547 - dev->virtbase + I2C_TFR); 548 - dev->cli.buffer++; 549 - dev->cli.count--; 550 - dev->cli.xfer_bytes++; 514 + nmk_i2c_writeb(priv, *priv->cli.buffer, I2C_TFR); 515 + priv->cli.buffer++; 516 + priv->cli.count--; 517 + priv->cli.xfer_bytes++; 551 518 } 552 519 553 520 } 554 521 555 522 /** 556 523 * write_i2c() - Write data to I2C client. 557 - * @dev: private data of I2C Driver 524 + * @priv: private data of I2C Driver 558 525 * @flags: message flags 559 526 * 560 527 * This function writes data to I2C client 561 528 */ 562 - static int write_i2c(struct nmk_i2c_dev *dev, u16 flags) 529 + static int write_i2c(struct nmk_i2c_dev *priv, u16 flags) 563 530 { 564 - u32 status = 0; 565 531 u32 mcr, irq_mask; 566 - unsigned long timeout; 532 + u32 status = 0; 533 + bool xfer_done; 567 534 568 - mcr = load_i2c_mcr_reg(dev, flags); 535 + mcr = load_i2c_mcr_reg(priv, flags); 569 536 570 - writel(mcr, dev->virtbase + I2C_MCR); 537 + writel(mcr, priv->virtbase + I2C_MCR); 571 538 572 539 /* load the current CR value */ 573 - writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, 574 - dev->virtbase + I2C_CR); 540 + writel(readl(priv->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR, 541 + priv->virtbase + I2C_CR); 575 542 576 543 /* enable the controller */ 577 - i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE); 544 + i2c_set_bit(priv->virtbase + I2C_CR, I2C_CR_PE); 578 545 579 - init_completion(&dev->xfer_complete); 546 + init_waitqueue_head(&priv->xfer_wq); 547 + priv->xfer_done = false; 580 548 581 549 /* enable interrupts by settings the masks */ 582 550 irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR); 583 551 584 552 /* Fill the TX FIFO with transmit data */ 585 - fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD); 553 + fill_tx_fifo(priv, MAX_I2C_FIFO_THRESHOLD); 586 554 587 - if (dev->cli.count != 0) 555 + if (priv->cli.count != 0) 588 556 irq_mask |= I2C_IT_TXFNE; 589 557 590 558 /* ··· 609 543 * set the MTDWS bit (Master Transaction Done Without Stop) 610 544 * to start repeated start operation 611 545 */ 612 - if (dev->stop || !dev->vendor->has_mtdws) 546 + if (priv->stop || !priv->vendor->has_mtdws) 613 547 irq_mask |= I2C_IT_MTD; 614 548 else 615 549 irq_mask |= I2C_IT_MTDWS; 616 550 617 - irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask); 551 + irq_mask &= I2C_CLEAR_ALL_INTS; 618 552 619 - writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask, 620 - dev->virtbase + I2C_IMSCR); 553 + writel(readl(priv->virtbase + I2C_IMSCR) | irq_mask, 554 + priv->virtbase + I2C_IMSCR); 621 555 622 - timeout = wait_for_completion_timeout( 623 - &dev->xfer_complete, dev->adap.timeout); 556 + xfer_done = nmk_i2c_wait_xfer_done(priv); 624 557 625 - if (timeout == 0) { 558 + if (!xfer_done) { 626 559 /* Controller timed out */ 627 - dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n", 628 - dev->cli.slave_adr); 560 + dev_err(&priv->adev->dev, "write to slave 0x%x timed out\n", 561 + priv->cli.slave_adr); 629 562 status = -ETIMEDOUT; 630 563 } 631 564 ··· 633 568 634 569 /** 635 570 * nmk_i2c_xfer_one() - transmit a single I2C message 636 - * @dev: device with a message encoded into it 571 + * @priv: device with a message encoded into it 637 572 * @flags: message flags 638 573 */ 639 - static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags) 574 + static int nmk_i2c_xfer_one(struct nmk_i2c_dev *priv, u16 flags) 640 575 { 641 576 int status; 642 577 643 578 if (flags & I2C_M_RD) { 644 579 /* read operation */ 645 - dev->cli.operation = I2C_READ; 646 - status = read_i2c(dev, flags); 580 + priv->cli.operation = I2C_READ; 581 + status = read_i2c(priv, flags); 647 582 } else { 648 583 /* write operation */ 649 - dev->cli.operation = I2C_WRITE; 650 - status = write_i2c(dev, flags); 584 + priv->cli.operation = I2C_WRITE; 585 + status = write_i2c(priv, flags); 651 586 } 652 587 653 - if (status || (dev->result)) { 588 + if (status || priv->result) { 654 589 u32 i2c_sr; 655 590 u32 cause; 656 591 657 - i2c_sr = readl(dev->virtbase + I2C_SR); 658 - /* 659 - * Check if the controller I2C operation status 660 - * is set to ABORT(11b). 661 - */ 662 - if (((i2c_sr >> 2) & 0x3) == 0x3) { 663 - /* get the abort cause */ 664 - cause = (i2c_sr >> 4) & 0x7; 665 - dev_err(&dev->adev->dev, "%s\n", 592 + i2c_sr = readl(priv->virtbase + I2C_SR); 593 + if (FIELD_GET(I2C_SR_STATUS, i2c_sr) == I2C_ABORT) { 594 + cause = FIELD_GET(I2C_SR_CAUSE, i2c_sr); 595 + dev_err(&priv->adev->dev, "%s\n", 666 596 cause >= ARRAY_SIZE(abort_causes) ? 667 597 "unknown reason" : 668 598 abort_causes[cause]); 669 599 } 670 600 671 - (void) init_hw(dev); 601 + init_hw(priv); 672 602 673 - status = status ? status : dev->result; 603 + status = status ? status : priv->result; 674 604 } 675 605 676 606 return status; ··· 723 663 { 724 664 int status = 0; 725 665 int i; 726 - struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); 666 + struct nmk_i2c_dev *priv = i2c_get_adapdata(i2c_adap); 727 667 int j; 728 668 729 - pm_runtime_get_sync(&dev->adev->dev); 669 + pm_runtime_get_sync(&priv->adev->dev); 730 670 731 671 /* Attempt three times to send the message queue */ 732 672 for (j = 0; j < 3; j++) { 733 673 /* setup the i2c controller */ 734 - setup_i2c_controller(dev); 674 + setup_i2c_controller(priv); 735 675 736 676 for (i = 0; i < num_msgs; i++) { 737 - dev->cli.slave_adr = msgs[i].addr; 738 - dev->cli.buffer = msgs[i].buf; 739 - dev->cli.count = msgs[i].len; 740 - dev->stop = (i < (num_msgs - 1)) ? 0 : 1; 741 - dev->result = 0; 677 + priv->cli.slave_adr = msgs[i].addr; 678 + priv->cli.buffer = msgs[i].buf; 679 + priv->cli.count = msgs[i].len; 680 + priv->stop = (i < (num_msgs - 1)) ? 0 : 1; 681 + priv->result = 0; 742 682 743 - status = nmk_i2c_xfer_one(dev, msgs[i].flags); 683 + status = nmk_i2c_xfer_one(priv, msgs[i].flags); 744 684 if (status != 0) 745 685 break; 746 686 } ··· 748 688 break; 749 689 } 750 690 751 - pm_runtime_put_sync(&dev->adev->dev); 691 + pm_runtime_put_sync(&priv->adev->dev); 752 692 753 693 /* return the no. messages processed */ 754 694 if (status) ··· 759 699 760 700 /** 761 701 * disable_interrupts() - disable the interrupts 762 - * @dev: private data of controller 702 + * @priv: private data of controller 763 703 * @irq: interrupt number 764 704 */ 765 - static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq) 705 + static int disable_interrupts(struct nmk_i2c_dev *priv, u32 irq) 766 706 { 767 - irq = IRQ_MASK(irq); 768 - writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq), 769 - dev->virtbase + I2C_IMSCR); 707 + irq &= I2C_CLEAR_ALL_INTS; 708 + writel(readl(priv->virtbase + I2C_IMSCR) & ~irq, 709 + priv->virtbase + I2C_IMSCR); 770 710 return 0; 771 711 } 772 712 ··· 783 723 */ 784 724 static irqreturn_t i2c_irq_handler(int irq, void *arg) 785 725 { 786 - struct nmk_i2c_dev *dev = arg; 726 + struct nmk_i2c_dev *priv = arg; 727 + struct device *dev = &priv->adev->dev; 787 728 u32 tft, rft; 788 729 u32 count; 789 730 u32 misr, src; 790 731 791 732 /* load Tx FIFO and Rx FIFO threshold values */ 792 - tft = readl(dev->virtbase + I2C_TFTR); 793 - rft = readl(dev->virtbase + I2C_RFTR); 733 + tft = readl(priv->virtbase + I2C_TFTR); 734 + rft = readl(priv->virtbase + I2C_RFTR); 794 735 795 736 /* read interrupt status register */ 796 - misr = readl(dev->virtbase + I2C_MISR); 737 + misr = readl(priv->virtbase + I2C_MISR); 797 738 798 739 src = __ffs(misr); 799 - switch ((1 << src)) { 740 + switch (BIT(src)) { 800 741 801 742 /* Transmit FIFO nearly empty interrupt */ 802 743 case I2C_IT_TXFNE: 803 744 { 804 - if (dev->cli.operation == I2C_READ) { 745 + if (priv->cli.operation == I2C_READ) { 805 746 /* 806 747 * in read operation why do we care for writing? 807 748 * so disable the Transmit FIFO interrupt 808 749 */ 809 - disable_interrupts(dev, I2C_IT_TXFNE); 750 + disable_interrupts(priv, I2C_IT_TXFNE); 810 751 } else { 811 - fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft)); 752 + fill_tx_fifo(priv, (MAX_I2C_FIFO_THRESHOLD - tft)); 812 753 /* 813 754 * if done, close the transfer by disabling the 814 755 * corresponding TXFNE interrupt 815 756 */ 816 - if (dev->cli.count == 0) 817 - disable_interrupts(dev, I2C_IT_TXFNE); 757 + if (priv->cli.count == 0) 758 + disable_interrupts(priv, I2C_IT_TXFNE); 818 759 } 819 760 } 820 761 break; ··· 829 768 case I2C_IT_RXFNF: 830 769 for (count = rft; count > 0; count--) { 831 770 /* Read the Rx FIFO */ 832 - *dev->cli.buffer = readb(dev->virtbase + I2C_RFR); 833 - dev->cli.buffer++; 771 + *priv->cli.buffer = nmk_i2c_readb(priv, I2C_RFR); 772 + priv->cli.buffer++; 834 773 } 835 - dev->cli.count -= rft; 836 - dev->cli.xfer_bytes += rft; 774 + priv->cli.count -= rft; 775 + priv->cli.xfer_bytes += rft; 837 776 break; 838 777 839 778 /* Rx FIFO full */ 840 779 case I2C_IT_RXFF: 841 780 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) { 842 - *dev->cli.buffer = readb(dev->virtbase + I2C_RFR); 843 - dev->cli.buffer++; 781 + *priv->cli.buffer = nmk_i2c_readb(priv, I2C_RFR); 782 + priv->cli.buffer++; 844 783 } 845 - dev->cli.count -= MAX_I2C_FIFO_THRESHOLD; 846 - dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD; 784 + priv->cli.count -= MAX_I2C_FIFO_THRESHOLD; 785 + priv->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD; 847 786 break; 848 787 849 788 /* Master Transaction Done with/without stop */ 850 789 case I2C_IT_MTD: 851 790 case I2C_IT_MTDWS: 852 - if (dev->cli.operation == I2C_READ) { 853 - while (!(readl(dev->virtbase + I2C_RISR) 791 + if (priv->cli.operation == I2C_READ) { 792 + while (!(readl(priv->virtbase + I2C_RISR) 854 793 & I2C_IT_RXFE)) { 855 - if (dev->cli.count == 0) 794 + if (priv->cli.count == 0) 856 795 break; 857 - *dev->cli.buffer = 858 - readb(dev->virtbase + I2C_RFR); 859 - dev->cli.buffer++; 860 - dev->cli.count--; 861 - dev->cli.xfer_bytes++; 796 + *priv->cli.buffer = 797 + nmk_i2c_readb(priv, I2C_RFR); 798 + priv->cli.buffer++; 799 + priv->cli.count--; 800 + priv->cli.xfer_bytes++; 862 801 } 863 802 } 864 803 865 - disable_all_interrupts(dev); 866 - clear_all_interrupts(dev); 804 + disable_all_interrupts(priv); 805 + clear_all_interrupts(priv); 867 806 868 - if (dev->cli.count) { 869 - dev->result = -EIO; 870 - dev_err(&dev->adev->dev, 871 - "%lu bytes still remain to be xfered\n", 872 - dev->cli.count); 873 - (void) init_hw(dev); 807 + if (priv->cli.count) { 808 + priv->result = -EIO; 809 + dev_err(dev, "%lu bytes still remain to be xfered\n", 810 + priv->cli.count); 811 + init_hw(priv); 874 812 } 875 - complete(&dev->xfer_complete); 813 + priv->xfer_done = true; 814 + wake_up(&priv->xfer_wq); 815 + 876 816 877 817 break; 878 818 879 819 /* Master Arbitration lost interrupt */ 880 820 case I2C_IT_MAL: 881 - dev->result = -EIO; 882 - (void) init_hw(dev); 821 + priv->result = -EIO; 822 + init_hw(priv); 883 823 884 - i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL); 885 - complete(&dev->xfer_complete); 824 + i2c_set_bit(priv->virtbase + I2C_ICR, I2C_IT_MAL); 825 + priv->xfer_done = true; 826 + wake_up(&priv->xfer_wq); 827 + 886 828 887 829 break; 888 830 ··· 895 831 * during the transaction. 896 832 */ 897 833 case I2C_IT_BERR: 898 - dev->result = -EIO; 899 - /* get the status */ 900 - if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT) 901 - (void) init_hw(dev); 834 + { 835 + u32 sr; 902 836 903 - i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR); 904 - complete(&dev->xfer_complete); 837 + sr = readl(priv->virtbase + I2C_SR); 838 + priv->result = -EIO; 839 + if (FIELD_GET(I2C_SR_STATUS, sr) == I2C_ABORT) 840 + init_hw(priv); 905 841 906 - break; 842 + i2c_set_bit(priv->virtbase + I2C_ICR, I2C_IT_BERR); 843 + priv->xfer_done = true; 844 + wake_up(&priv->xfer_wq); 845 + 846 + } 847 + break; 907 848 908 849 /* 909 850 * Tx FIFO overrun interrupt. ··· 916 847 * the Tx FIFO is full. 917 848 */ 918 849 case I2C_IT_TXFOVR: 919 - dev->result = -EIO; 920 - (void) init_hw(dev); 850 + priv->result = -EIO; 851 + init_hw(priv); 921 852 922 - dev_err(&dev->adev->dev, "Tx Fifo Over run\n"); 923 - complete(&dev->xfer_complete); 853 + dev_err(dev, "Tx Fifo Over run\n"); 854 + priv->xfer_done = true; 855 + wake_up(&priv->xfer_wq); 856 + 924 857 925 858 break; 926 859 ··· 934 863 case I2C_IT_RFSE: 935 864 case I2C_IT_WTSR: 936 865 case I2C_IT_STD: 937 - dev_err(&dev->adev->dev, "unhandled Interrupt\n"); 866 + dev_err(dev, "unhandled Interrupt\n"); 938 867 break; 939 868 default: 940 - dev_err(&dev->adev->dev, "spurious Interrupt..\n"); 869 + dev_err(dev, "spurious Interrupt..\n"); 941 870 break; 942 871 } 943 872 ··· 964 893 static int nmk_i2c_runtime_suspend(struct device *dev) 965 894 { 966 895 struct amba_device *adev = to_amba_device(dev); 967 - struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev); 896 + struct nmk_i2c_dev *priv = amba_get_drvdata(adev); 968 897 969 - clk_disable_unprepare(nmk_i2c->clk); 898 + clk_disable_unprepare(priv->clk); 970 899 pinctrl_pm_select_idle_state(dev); 971 900 return 0; 972 901 } ··· 974 903 static int nmk_i2c_runtime_resume(struct device *dev) 975 904 { 976 905 struct amba_device *adev = to_amba_device(dev); 977 - struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev); 906 + struct nmk_i2c_dev *priv = amba_get_drvdata(adev); 978 907 int ret; 979 908 980 - ret = clk_prepare_enable(nmk_i2c->clk); 909 + ret = clk_prepare_enable(priv->clk); 981 910 if (ret) { 982 911 dev_err(dev, "can't prepare_enable clock\n"); 983 912 return ret; ··· 985 914 986 915 pinctrl_pm_select_default_state(dev); 987 916 988 - ret = init_hw(nmk_i2c); 917 + ret = init_hw(priv); 989 918 if (ret) { 990 - clk_disable_unprepare(nmk_i2c->clk); 919 + clk_disable_unprepare(priv->clk); 991 920 pinctrl_pm_select_idle_state(dev); 992 921 } 993 922 ··· 1010 939 }; 1011 940 1012 941 static void nmk_i2c_of_probe(struct device_node *np, 1013 - struct nmk_i2c_dev *nmk) 942 + struct nmk_i2c_dev *priv) 1014 943 { 944 + u32 timeout_usecs; 945 + 1015 946 /* Default to 100 kHz if no frequency is given in the node */ 1016 - if (of_property_read_u32(np, "clock-frequency", &nmk->clk_freq)) 1017 - nmk->clk_freq = I2C_MAX_STANDARD_MODE_FREQ; 947 + if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq)) 948 + priv->clk_freq = I2C_MAX_STANDARD_MODE_FREQ; 1018 949 1019 950 /* This driver only supports 'standard' and 'fast' modes of operation. */ 1020 - if (nmk->clk_freq <= I2C_MAX_STANDARD_MODE_FREQ) 1021 - nmk->sm = I2C_FREQ_MODE_STANDARD; 951 + if (priv->clk_freq <= I2C_MAX_STANDARD_MODE_FREQ) 952 + priv->sm = I2C_FREQ_MODE_STANDARD; 1022 953 else 1023 - nmk->sm = I2C_FREQ_MODE_FAST; 1024 - nmk->tft = 1; /* Tx FIFO threshold */ 1025 - nmk->rft = 8; /* Rx FIFO threshold */ 1026 - nmk->timeout = 200; /* Slave response timeout(ms) */ 954 + priv->sm = I2C_FREQ_MODE_FAST; 955 + priv->tft = 1; /* Tx FIFO threshold */ 956 + priv->rft = 8; /* Rx FIFO threshold */ 957 + 958 + /* Slave response timeout */ 959 + if (!of_property_read_u32(np, "i2c-transfer-timeout-us", &timeout_usecs)) 960 + priv->timeout_usecs = timeout_usecs; 961 + else 962 + priv->timeout_usecs = 200 * USEC_PER_MSEC; 963 + } 964 + 965 + static const unsigned int nmk_i2c_eyeq5_masks[] = { 966 + GENMASK(5, 4), 967 + GENMASK(7, 6), 968 + GENMASK(9, 8), 969 + GENMASK(11, 10), 970 + GENMASK(13, 12), 971 + }; 972 + 973 + static int nmk_i2c_eyeq5_probe(struct nmk_i2c_dev *priv) 974 + { 975 + struct device *dev = &priv->adev->dev; 976 + struct device_node *np = dev->of_node; 977 + unsigned int mask, speed_mode; 978 + struct regmap *olb; 979 + unsigned int id; 980 + 981 + priv->has_32b_bus = true; 982 + 983 + olb = syscon_regmap_lookup_by_phandle_args(np, "mobileye,olb", 1, &id); 984 + if (IS_ERR(olb)) 985 + return PTR_ERR(olb); 986 + if (id >= ARRAY_SIZE(nmk_i2c_eyeq5_masks)) 987 + return -ENOENT; 988 + 989 + if (priv->clk_freq <= 400000) 990 + speed_mode = I2C_EYEQ5_SPEED_FAST; 991 + else if (priv->clk_freq <= 1000000) 992 + speed_mode = I2C_EYEQ5_SPEED_FAST_PLUS; 993 + else 994 + speed_mode = I2C_EYEQ5_SPEED_HIGH_SPEED; 995 + 996 + mask = nmk_i2c_eyeq5_masks[id]; 997 + regmap_update_bits(olb, NMK_I2C_EYEQ5_OLB_IOCR2, 998 + mask, speed_mode << __fls(mask)); 999 + 1000 + return 0; 1027 1001 } 1028 1002 1029 1003 static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id) 1030 1004 { 1031 1005 int ret = 0; 1006 + struct nmk_i2c_dev *priv; 1032 1007 struct device_node *np = adev->dev.of_node; 1033 - struct nmk_i2c_dev *dev; 1008 + struct device *dev = &adev->dev; 1034 1009 struct i2c_adapter *adap; 1035 1010 struct i2c_vendor_data *vendor = id->data; 1036 1011 u32 max_fifo_threshold = (vendor->fifodepth / 2) - 1; 1037 1012 1038 - dev = devm_kzalloc(&adev->dev, sizeof(*dev), GFP_KERNEL); 1039 - if (!dev) 1013 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 1014 + if (!priv) 1040 1015 return -ENOMEM; 1041 1016 1042 - dev->vendor = vendor; 1043 - dev->adev = adev; 1044 - nmk_i2c_of_probe(np, dev); 1017 + priv->vendor = vendor; 1018 + priv->adev = adev; 1019 + priv->has_32b_bus = false; 1020 + nmk_i2c_of_probe(np, priv); 1045 1021 1046 - if (dev->tft > max_fifo_threshold) { 1047 - dev_warn(&adev->dev, "requested TX FIFO threshold %u, adjusted down to %u\n", 1048 - dev->tft, max_fifo_threshold); 1049 - dev->tft = max_fifo_threshold; 1022 + if (of_device_is_compatible(np, "mobileye,eyeq5-i2c")) { 1023 + ret = nmk_i2c_eyeq5_probe(priv); 1024 + if (ret) 1025 + return dev_err_probe(dev, ret, "failed OLB lookup\n"); 1050 1026 } 1051 1027 1052 - if (dev->rft > max_fifo_threshold) { 1053 - dev_warn(&adev->dev, "requested RX FIFO threshold %u, adjusted down to %u\n", 1054 - dev->rft, max_fifo_threshold); 1055 - dev->rft = max_fifo_threshold; 1028 + if (priv->tft > max_fifo_threshold) { 1029 + dev_warn(dev, "requested TX FIFO threshold %u, adjusted down to %u\n", 1030 + priv->tft, max_fifo_threshold); 1031 + priv->tft = max_fifo_threshold; 1056 1032 } 1057 1033 1058 - amba_set_drvdata(adev, dev); 1034 + if (priv->rft > max_fifo_threshold) { 1035 + dev_warn(dev, "requested RX FIFO threshold %u, adjusted down to %u\n", 1036 + priv->rft, max_fifo_threshold); 1037 + priv->rft = max_fifo_threshold; 1038 + } 1059 1039 1060 - dev->virtbase = devm_ioremap(&adev->dev, adev->res.start, 1061 - resource_size(&adev->res)); 1062 - if (!dev->virtbase) 1040 + amba_set_drvdata(adev, priv); 1041 + 1042 + priv->virtbase = devm_ioremap(dev, adev->res.start, 1043 + resource_size(&adev->res)); 1044 + if (!priv->virtbase) 1063 1045 return -ENOMEM; 1064 1046 1065 - dev->irq = adev->irq[0]; 1066 - ret = devm_request_irq(&adev->dev, dev->irq, i2c_irq_handler, 0, 1067 - DRIVER_NAME, dev); 1047 + priv->irq = adev->irq[0]; 1048 + ret = devm_request_irq(dev, priv->irq, i2c_irq_handler, 0, 1049 + DRIVER_NAME, priv); 1068 1050 if (ret) 1069 - return dev_err_probe(&adev->dev, ret, 1070 - "cannot claim the irq %d\n", dev->irq); 1051 + return dev_err_probe(dev, ret, 1052 + "cannot claim the irq %d\n", priv->irq); 1071 1053 1072 - dev->clk = devm_clk_get_enabled(&adev->dev, NULL); 1073 - if (IS_ERR(dev->clk)) 1074 - return dev_err_probe(&adev->dev, PTR_ERR(dev->clk), 1054 + priv->clk = devm_clk_get_enabled(dev, NULL); 1055 + if (IS_ERR(priv->clk)) 1056 + return dev_err_probe(dev, PTR_ERR(priv->clk), 1075 1057 "could enable i2c clock\n"); 1076 1058 1077 - init_hw(dev); 1059 + init_hw(priv); 1078 1060 1079 - adap = &dev->adap; 1061 + adap = &priv->adap; 1080 1062 adap->dev.of_node = np; 1081 - adap->dev.parent = &adev->dev; 1063 + adap->dev.parent = dev; 1082 1064 adap->owner = THIS_MODULE; 1083 1065 adap->class = I2C_CLASS_DEPRECATED; 1084 1066 adap->algo = &nmk_i2c_algo; 1085 - adap->timeout = msecs_to_jiffies(dev->timeout); 1067 + adap->timeout = usecs_to_jiffies(priv->timeout_usecs); 1086 1068 snprintf(adap->name, sizeof(adap->name), 1087 1069 "Nomadik I2C at %pR", &adev->res); 1088 1070 1089 - i2c_set_adapdata(adap, dev); 1071 + i2c_set_adapdata(adap, priv); 1090 1072 1091 - dev_info(&adev->dev, 1073 + dev_info(dev, 1092 1074 "initialize %s on virtual base %p\n", 1093 - adap->name, dev->virtbase); 1075 + adap->name, priv->virtbase); 1094 1076 1095 1077 ret = i2c_add_adapter(adap); 1096 1078 if (ret) 1097 1079 return ret; 1098 1080 1099 - pm_runtime_put(&adev->dev); 1081 + pm_runtime_put(dev); 1100 1082 1101 1083 return 0; 1102 1084 } 1103 1085 1104 1086 static void nmk_i2c_remove(struct amba_device *adev) 1105 1087 { 1106 - struct nmk_i2c_dev *dev = amba_get_drvdata(adev); 1088 + struct nmk_i2c_dev *priv = amba_get_drvdata(adev); 1107 1089 1108 - i2c_del_adapter(&dev->adap); 1109 - flush_i2c_fifo(dev); 1110 - disable_all_interrupts(dev); 1111 - clear_all_interrupts(dev); 1090 + i2c_del_adapter(&priv->adap); 1091 + flush_i2c_fifo(priv); 1092 + disable_all_interrupts(priv); 1093 + clear_all_interrupts(priv); 1112 1094 /* disable the controller */ 1113 - i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE); 1095 + i2c_clr_bit(priv->virtbase + I2C_CR, I2C_CR_PE); 1114 1096 } 1115 1097 1116 1098 static struct i2c_vendor_data vendor_stn8815 = {
+38 -8
drivers/i2c/muxes/i2c-mux-pca954x.c
··· 49 49 #include <linux/pm.h> 50 50 #include <linux/property.h> 51 51 #include <linux/regulator/consumer.h> 52 + #include <linux/reset.h> 52 53 #include <linux/slab.h> 53 54 #include <linux/spinlock.h> 54 55 #include <dt-bindings/mux/mux.h> ··· 117 116 unsigned int irq_mask; 118 117 raw_spinlock_t lock; 119 118 struct regulator *supply; 119 + 120 + struct gpio_desc *reset_gpio; 121 + struct reset_control *reset_cont; 120 122 }; 121 123 122 124 /* Provide specs for the MAX735x, PCA954x and PCA984x types we know about */ ··· 522 518 return ret; 523 519 } 524 520 521 + static int pca954x_get_reset(struct device *dev, struct pca954x *data) 522 + { 523 + data->reset_cont = devm_reset_control_get_optional_shared(dev, NULL); 524 + if (IS_ERR(data->reset_cont)) 525 + return dev_err_probe(dev, PTR_ERR(data->reset_cont), 526 + "Failed to get reset\n"); 527 + else if (data->reset_cont) 528 + return 0; 529 + 530 + /* 531 + * fallback to legacy reset-gpios 532 + */ 533 + data->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 534 + if (IS_ERR(data->reset_gpio)) { 535 + return dev_err_probe(dev, PTR_ERR(data->reset_gpio), 536 + "Failed to get reset gpio"); 537 + } 538 + 539 + return 0; 540 + } 541 + 542 + static void pca954x_reset_deassert(struct pca954x *data) 543 + { 544 + if (data->reset_cont) 545 + reset_control_deassert(data->reset_cont); 546 + else 547 + gpiod_set_value_cansleep(data->reset_gpio, 0); 548 + } 549 + 525 550 /* 526 551 * I2C init/probing/exit functions 527 552 */ ··· 559 526 const struct i2c_device_id *id = i2c_client_get_device_id(client); 560 527 struct i2c_adapter *adap = client->adapter; 561 528 struct device *dev = &client->dev; 562 - struct gpio_desc *gpio; 563 529 struct i2c_mux_core *muxc; 564 530 struct pca954x *data; 565 531 int num; ··· 586 554 return dev_err_probe(dev, ret, 587 555 "Failed to enable vdd supply\n"); 588 556 589 - /* Reset the mux if a reset GPIO is specified. */ 590 - gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 591 - if (IS_ERR(gpio)) { 592 - ret = PTR_ERR(gpio); 557 + ret = pca954x_get_reset(dev, data); 558 + if (ret) 593 559 goto fail_cleanup; 594 - } 595 - if (gpio) { 560 + 561 + if (data->reset_cont || data->reset_gpio) { 596 562 udelay(1); 597 - gpiod_set_value_cansleep(gpio, 0); 563 + pca954x_reset_deassert(data); 598 564 /* Give the chip some time to recover. */ 599 565 udelay(1); 600 566 }