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

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

Pull more i2c updates from Wolfram Sang:

- Second part of rtl9300 updates since dependencies are in now:
- general cleanups
- implement block read/write support
- add RTL9310 support

- DT schema conversion of hix5hd2 binding

- namespace cleanup for i2c-algo-pca

- minor simplification for mt65xx

* tag 'i2c-for-6.18-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux:
dt-bindings: i2c: hisilicon,hix5hd2: convert to DT schema
i2c: mt65xx: convert set_speed function to void
i2c: rename wait_for_completion callback to wait_for_completion_cb
i2c: rtl9300: add support for RTL9310 I2C controller
dt-bindings: i2c: realtek,rtl9301-i2c: extend for RTL9310 support
i2c: rtl9300: use scoped guard instead of explicit lock/unlock
i2c: rtl9300: separate xfer configuration and execution
i2c: rtl9300: do not set read mode on every transfer
i2c: rtl9300: move setting SCL frequency to config_io
i2c: rtl9300: rename internal sda_pin to sda_num
dt-bindings: i2c: realtek,rtl9301-i2c: fix wording and typos
i2c: rtl9300: use regmap fields and API for registers
i2c: rtl9300: Implement I2C block read and write

+392 -218
+51
Documentation/devicetree/bindings/i2c/hisilicon,hix5hd2-i2c.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/i2c/hisilicon,hix5hd2-i2c.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + title: I2C for HiSilicon hix5hd2 chipset platform 7 + 8 + maintainers: 9 + - Wei Yan <sledge.yanwei@huawei.com> 10 + 11 + allOf: 12 + - $ref: /schemas/i2c/i2c-controller.yaml# 13 + 14 + properties: 15 + compatible: 16 + enum: 17 + - hisilicon,hix5hd2-i2c 18 + 19 + reg: 20 + maxItems: 1 21 + 22 + interrupts: 23 + maxItems: 1 24 + 25 + clocks: 26 + maxItems: 1 27 + 28 + clock-frequency: 29 + description: Desired I2C bus frequency in Hz 30 + default: 100000 31 + 32 + required: 33 + - compatible 34 + - reg 35 + - interrupts 36 + - clocks 37 + 38 + unevaluatedProperties: false 39 + 40 + examples: 41 + - | 42 + #include <dt-bindings/clock/hix5hd2-clock.h> 43 + 44 + i2c@f8b10000 { 45 + compatible = "hisilicon,hix5hd2-i2c"; 46 + reg = <0xf8b10000 0x1000>; 47 + interrupts = <0 38 4>; 48 + clocks = <&clock HIX5HD2_I2C0_RST>; 49 + #address-cells = <1>; 50 + #size-cells = <0>; 51 + };
-24
Documentation/devicetree/bindings/i2c/i2c-hix5hd2.txt
··· 1 - I2C for Hisilicon hix5hd2 chipset platform 2 - 3 - Required properties: 4 - - compatible: Must be "hisilicon,hix5hd2-i2c" 5 - - reg: physical base address of the controller and length of memory mapped 6 - region. 7 - - interrupts: interrupt number to the cpu. 8 - - #address-cells = <1>; 9 - - #size-cells = <0>; 10 - - clocks: phandles to input clocks. 11 - 12 - Optional properties: 13 - - clock-frequency: Desired I2C bus frequency in Hz, otherwise defaults to 100000 14 - - Child nodes conforming to i2c bus binding 15 - 16 - Examples: 17 - I2C0@f8b10000 { 18 - compatible = "hisilicon,hix5hd2-i2c"; 19 - reg = <0xf8b10000 0x1000>; 20 - interrupts = <0 38 4>; 21 - clocks = <&clock HIX5HD2_I2C0_RST>; 22 - #address-cells = <1>; 23 - #size-cells = <0>; 24 - }
+40 -5
Documentation/devicetree/bindings/i2c/realtek,rtl9301-i2c.yaml
··· 10 10 - Chris Packham <chris.packham@alliedtelesis.co.nz> 11 11 12 12 description: 13 - The RTL9300 SoC has two I2C controllers. Each of these has an SCL line (which 13 + RTL9300 SoCs have two I2C controllers. Each of these has an SCL line (which 14 14 if not-used for SCL can be a GPIO). There are 8 common SDA lines that can be 15 15 assigned to either I2C controller. 16 + RTL9310 SoCs have equal capabilities but support 12 common SDA lines which 17 + can be assigned to either I2C controller. 16 18 17 19 properties: 18 20 compatible: ··· 25 23 - realtek,rtl9302c-i2c 26 24 - realtek,rtl9303-i2c 27 25 - const: realtek,rtl9301-i2c 28 - - const: realtek,rtl9301-i2c 26 + - items: 27 + - enum: 28 + - realtek,rtl9311-i2c 29 + - realtek,rtl9312-i2c 30 + - realtek,rtl9313-i2c 31 + - const: realtek,rtl9310-i2c 32 + - enum: 33 + - realtek,rtl9301-i2c 34 + - realtek,rtl9310-i2c 29 35 30 36 reg: 31 37 items: 32 - - description: Register offset and size this I2C controller. 38 + - description: Register offset and size of this I2C controller. 33 39 34 40 "#address-cells": 35 41 const: 1 ··· 45 35 "#size-cells": 46 36 const: 0 47 37 38 + realtek,scl: 39 + $ref: /schemas/types.yaml#/definitions/uint32 40 + description: 41 + The SCL line number of this I2C controller. 42 + enum: [ 0, 1 ] 43 + 48 44 patternProperties: 49 - '^i2c@[0-7]$': 45 + '^i2c@[0-9ab]$': 50 46 $ref: /schemas/i2c/i2c-controller.yaml 51 47 unevaluatedProperties: false 52 48 53 49 properties: 54 50 reg: 55 - description: The SDA pin associated with the I2C bus. 51 + description: The SDA line number associated with the I2C bus. 56 52 maxItems: 1 57 53 58 54 required: 59 55 - reg 56 + 57 + 58 + allOf: 59 + - if: 60 + properties: 61 + compatible: 62 + contains: 63 + const: realtek,rtl9310-i2c 64 + then: 65 + required: 66 + - realtek,scl 67 + - if: 68 + properties: 69 + compatible: 70 + contains: 71 + const: realtek,rtl9301-i2c 72 + then: 73 + patternProperties: 74 + '^i2c@[89ab]$': false 60 75 61 76 required: 62 77 - compatible
+1 -1
drivers/i2c/algos/i2c-algo-pca.c
··· 30 30 #define pca_clock(adap) adap->i2c_clock 31 31 #define pca_set_con(adap, val) pca_outw(adap, I2C_PCA_CON, val) 32 32 #define pca_get_con(adap) pca_inw(adap, I2C_PCA_CON) 33 - #define pca_wait(adap) adap->wait_for_completion(adap->data) 33 + #define pca_wait(adap) adap->wait_for_completion_cb(adap->data) 34 34 35 35 static void pca_reset(struct i2c_algo_pca_data *adap) 36 36 {
+2 -9
drivers/i2c/busses/i2c-mt65xx.c
··· 868 868 return 0; 869 869 } 870 870 871 - static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 871 + static void mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk) 872 872 { 873 873 unsigned int clk_src; 874 874 unsigned int step_cnt; ··· 938 938 939 939 break; 940 940 } 941 - 942 - 943 - return 0; 944 941 } 945 942 946 943 static void i2c_dump_register(struct mtk_i2c *i2c) ··· 1457 1460 1458 1461 strscpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name)); 1459 1462 1460 - ret = mtk_i2c_set_speed(i2c, clk_get_rate(i2c->clocks[speed_clk].clk)); 1461 - if (ret) { 1462 - dev_err(&pdev->dev, "Failed to set the speed.\n"); 1463 - return -EINVAL; 1464 - } 1463 + mtk_i2c_set_speed(i2c, clk_get_rate(i2c->clocks[speed_clk].clk)); 1465 1464 1466 1465 if (i2c->dev_comp->max_dma_support > 32) { 1467 1466 ret = dma_set_mask(&pdev->dev,
+1 -1
drivers/i2c/busses/i2c-pca-isa.c
··· 95 95 /* .data intentionally left NULL, not needed with ISA */ 96 96 .write_byte = pca_isa_writebyte, 97 97 .read_byte = pca_isa_readbyte, 98 - .wait_for_completion = pca_isa_waitforcompletion, 98 + .wait_for_completion_cb = pca_isa_waitforcompletion, 99 99 .reset_chip = pca_isa_resetchip, 100 100 }; 101 101
+1 -1
drivers/i2c/busses/i2c-pca-platform.c
··· 180 180 } 181 181 182 182 i2c->algo_data.data = i2c; 183 - i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion; 183 + i2c->algo_data.wait_for_completion_cb = i2c_pca_pf_waitforcompletion; 184 184 if (i2c->gpio) 185 185 i2c->algo_data.reset_chip = i2c_pca_pf_resetchip; 186 186 else
+295 -176
drivers/i2c/busses/i2c-rtl9300.c
··· 8 8 #include <linux/mutex.h> 9 9 #include <linux/platform_device.h> 10 10 #include <linux/regmap.h> 11 + #include <linux/unaligned.h> 11 12 12 13 enum rtl9300_bus_freq { 13 14 RTL9300_I2C_STD_FREQ, ··· 21 20 struct i2c_adapter adap; 22 21 struct rtl9300_i2c *i2c; 23 22 enum rtl9300_bus_freq bus_freq; 24 - u8 sda_pin; 23 + u8 sda_num; 24 + }; 25 + 26 + enum rtl9300_i2c_reg_scope { 27 + REG_SCOPE_GLOBAL, 28 + REG_SCOPE_MASTER, 29 + }; 30 + 31 + struct rtl9300_i2c_reg_field { 32 + struct reg_field field; 33 + enum rtl9300_i2c_reg_scope scope; 34 + }; 35 + 36 + enum rtl9300_i2c_reg_fields { 37 + F_DATA_WIDTH = 0, 38 + F_DEV_ADDR, 39 + F_I2C_FAIL, 40 + F_I2C_TRIG, 41 + F_MEM_ADDR, 42 + F_MEM_ADDR_WIDTH, 43 + F_RD_MODE, 44 + F_RWOP, 45 + F_SCL_FREQ, 46 + F_SCL_SEL, 47 + F_SDA_OUT_SEL, 48 + F_SDA_SEL, 49 + 50 + /* keep last */ 51 + F_NUM_FIELDS 52 + }; 53 + 54 + struct rtl9300_i2c_drv_data { 55 + struct rtl9300_i2c_reg_field field_desc[F_NUM_FIELDS]; 56 + int (*select_scl)(struct rtl9300_i2c *i2c, u8 scl); 57 + u32 data_reg; 58 + u8 max_nchan; 25 59 }; 26 60 27 61 #define RTL9300_I2C_MUX_NCHAN 8 62 + #define RTL9310_I2C_MUX_NCHAN 12 28 63 29 64 struct rtl9300_i2c { 30 65 struct regmap *regmap; 31 66 struct device *dev; 32 - struct rtl9300_i2c_chan chans[RTL9300_I2C_MUX_NCHAN]; 67 + struct rtl9300_i2c_chan chans[RTL9310_I2C_MUX_NCHAN]; 68 + struct regmap_field *fields[F_NUM_FIELDS]; 33 69 u32 reg_base; 34 - u8 sda_pin; 70 + u32 data_reg; 71 + u8 scl_num; 72 + u8 sda_num; 35 73 struct mutex lock; 36 74 }; 37 75 76 + DEFINE_GUARD(rtl9300_i2c, struct rtl9300_i2c *, mutex_lock(&_T->lock), mutex_unlock(&_T->lock)) 77 + 78 + enum rtl9300_i2c_xfer_type { 79 + RTL9300_I2C_XFER_BYTE, 80 + RTL9300_I2C_XFER_WORD, 81 + RTL9300_I2C_XFER_BLOCK, 82 + }; 83 + 84 + struct rtl9300_i2c_xfer { 85 + enum rtl9300_i2c_xfer_type type; 86 + u16 dev_addr; 87 + u8 reg_addr; 88 + u8 reg_addr_len; 89 + u8 *data; 90 + u8 data_len; 91 + bool write; 92 + }; 93 + 38 94 #define RTL9300_I2C_MST_CTRL1 0x0 39 - #define RTL9300_I2C_MST_CTRL1_MEM_ADDR_OFS 8 40 - #define RTL9300_I2C_MST_CTRL1_MEM_ADDR_MASK GENMASK(31, 8) 41 - #define RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_OFS 4 42 - #define RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_MASK GENMASK(6, 4) 43 - #define RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL BIT(3) 44 - #define RTL9300_I2C_MST_CTRL1_RWOP BIT(2) 45 - #define RTL9300_I2C_MST_CTRL1_I2C_FAIL BIT(1) 46 - #define RTL9300_I2C_MST_CTRL1_I2C_TRIG BIT(0) 47 95 #define RTL9300_I2C_MST_CTRL2 0x4 48 - #define RTL9300_I2C_MST_CTRL2_RD_MODE BIT(15) 49 - #define RTL9300_I2C_MST_CTRL2_DEV_ADDR_OFS 8 50 - #define RTL9300_I2C_MST_CTRL2_DEV_ADDR_MASK GENMASK(14, 8) 51 - #define RTL9300_I2C_MST_CTRL2_DATA_WIDTH_OFS 4 52 - #define RTL9300_I2C_MST_CTRL2_DATA_WIDTH_MASK GENMASK(7, 4) 53 - #define RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_OFS 2 54 - #define RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_MASK GENMASK(3, 2) 55 - #define RTL9300_I2C_MST_CTRL2_SCL_FREQ_OFS 0 56 - #define RTL9300_I2C_MST_CTRL2_SCL_FREQ_MASK GENMASK(1, 0) 57 96 #define RTL9300_I2C_MST_DATA_WORD0 0x8 58 97 #define RTL9300_I2C_MST_DATA_WORD1 0xc 59 98 #define RTL9300_I2C_MST_DATA_WORD2 0x10 60 99 #define RTL9300_I2C_MST_DATA_WORD3 0x14 61 - 62 100 #define RTL9300_I2C_MST_GLB_CTRL 0x384 101 + 102 + #define RTL9310_I2C_MST_IF_CTRL 0x1004 103 + #define RTL9310_I2C_MST_IF_SEL 0x1008 104 + #define RTL9310_I2C_MST_CTRL 0x0 105 + #define RTL9310_I2C_MST_MEMADDR_CTRL 0x4 106 + #define RTL9310_I2C_MST_DATA_CTRL 0x8 63 107 64 108 static int rtl9300_i2c_reg_addr_set(struct rtl9300_i2c *i2c, u32 reg, u16 len) 65 109 { 66 - u32 val, mask; 67 110 int ret; 68 111 69 - val = len << RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_OFS; 70 - mask = RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_MASK; 71 - 72 - ret = regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL2, mask, val); 112 + ret = regmap_field_write(i2c->fields[F_MEM_ADDR_WIDTH], len); 73 113 if (ret) 74 114 return ret; 75 115 76 - val = reg << RTL9300_I2C_MST_CTRL1_MEM_ADDR_OFS; 77 - mask = RTL9300_I2C_MST_CTRL1_MEM_ADDR_MASK; 78 - 79 - return regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, mask, val); 116 + return regmap_field_write(i2c->fields[F_MEM_ADDR], reg); 80 117 } 81 118 82 - static int rtl9300_i2c_config_io(struct rtl9300_i2c *i2c, u8 sda_pin) 119 + static int rtl9300_i2c_select_scl(struct rtl9300_i2c *i2c, u8 scl) 83 120 { 84 - int ret; 85 - u32 val, mask; 121 + return regmap_field_write(i2c->fields[F_SCL_SEL], 1); 122 + } 86 123 87 - ret = regmap_update_bits(i2c->regmap, RTL9300_I2C_MST_GLB_CTRL, BIT(sda_pin), BIT(sda_pin)); 124 + static int rtl9310_i2c_select_scl(struct rtl9300_i2c *i2c, u8 scl) 125 + { 126 + return regmap_field_update_bits(i2c->fields[F_SCL_SEL], BIT(scl), BIT(scl)); 127 + } 128 + 129 + static int rtl9300_i2c_config_chan(struct rtl9300_i2c *i2c, struct rtl9300_i2c_chan *chan) 130 + { 131 + struct rtl9300_i2c_drv_data *drv_data; 132 + int ret; 133 + 134 + if (i2c->sda_num == chan->sda_num) 135 + return 0; 136 + 137 + ret = regmap_field_write(i2c->fields[F_SCL_FREQ], chan->bus_freq); 88 138 if (ret) 89 139 return ret; 90 140 91 - val = (sda_pin << RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_OFS) | 92 - RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL; 93 - mask = RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_MASK | RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL; 141 + drv_data = (struct rtl9300_i2c_drv_data *)device_get_match_data(i2c->dev); 142 + ret = drv_data->select_scl(i2c, i2c->scl_num); 143 + if (ret) 144 + return ret; 94 145 95 - return regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, mask, val); 146 + ret = regmap_field_update_bits(i2c->fields[F_SDA_SEL], BIT(chan->sda_num), 147 + BIT(chan->sda_num)); 148 + if (ret) 149 + return ret; 150 + 151 + ret = regmap_field_write(i2c->fields[F_SDA_OUT_SEL], chan->sda_num); 152 + if (ret) 153 + return ret; 154 + 155 + i2c->sda_num = chan->sda_num; 156 + return 0; 96 157 } 97 158 98 - static int rtl9300_i2c_config_xfer(struct rtl9300_i2c *i2c, struct rtl9300_i2c_chan *chan, 99 - u16 addr, u16 len) 100 - { 101 - u32 val, mask; 102 - 103 - if (len < 1 || len > 16) 104 - return -EINVAL; 105 - 106 - val = chan->bus_freq << RTL9300_I2C_MST_CTRL2_SCL_FREQ_OFS; 107 - mask = RTL9300_I2C_MST_CTRL2_SCL_FREQ_MASK; 108 - 109 - val |= addr << RTL9300_I2C_MST_CTRL2_DEV_ADDR_OFS; 110 - mask |= RTL9300_I2C_MST_CTRL2_DEV_ADDR_MASK; 111 - 112 - val |= ((len - 1) & 0xf) << RTL9300_I2C_MST_CTRL2_DATA_WIDTH_OFS; 113 - mask |= RTL9300_I2C_MST_CTRL2_DATA_WIDTH_MASK; 114 - 115 - mask |= RTL9300_I2C_MST_CTRL2_RD_MODE; 116 - 117 - return regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL2, mask, val); 118 - } 119 - 120 - static int rtl9300_i2c_read(struct rtl9300_i2c *i2c, u8 *buf, int len) 159 + static int rtl9300_i2c_read(struct rtl9300_i2c *i2c, u8 *buf, u8 len) 121 160 { 122 161 u32 vals[4] = {}; 123 162 int i, ret; ··· 165 124 if (len > 16) 166 125 return -EIO; 167 126 168 - ret = regmap_bulk_read(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, 169 - vals, ARRAY_SIZE(vals)); 127 + ret = regmap_bulk_read(i2c->regmap, i2c->data_reg, vals, ARRAY_SIZE(vals)); 170 128 if (ret) 171 129 return ret; 172 130 ··· 177 137 return 0; 178 138 } 179 139 180 - static int rtl9300_i2c_write(struct rtl9300_i2c *i2c, u8 *buf, int len) 140 + static int rtl9300_i2c_write(struct rtl9300_i2c *i2c, u8 *buf, u8 len) 181 141 { 182 142 u32 vals[4] = {}; 183 143 int i; ··· 192 152 vals[reg] |= buf[i] << shift; 193 153 } 194 154 195 - return regmap_bulk_write(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, 196 - vals, ARRAY_SIZE(vals)); 155 + return regmap_bulk_write(i2c->regmap, i2c->data_reg, vals, ARRAY_SIZE(vals)); 197 156 } 198 157 199 158 static int rtl9300_i2c_writel(struct rtl9300_i2c *i2c, u32 data) 200 159 { 201 - return regmap_write(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, data); 160 + return regmap_write(i2c->regmap, i2c->data_reg, data); 202 161 } 203 162 204 - static int rtl9300_i2c_execute_xfer(struct rtl9300_i2c *i2c, char read_write, 205 - int size, union i2c_smbus_data *data, int len) 163 + static int rtl9300_i2c_prepare_xfer(struct rtl9300_i2c *i2c, struct rtl9300_i2c_xfer *xfer) 206 164 { 207 - u32 val, mask; 208 165 int ret; 209 166 210 - val = read_write == I2C_SMBUS_WRITE ? RTL9300_I2C_MST_CTRL1_RWOP : 0; 211 - mask = RTL9300_I2C_MST_CTRL1_RWOP; 167 + if (xfer->data_len < 1 || xfer->data_len > 16) 168 + return -EINVAL; 212 169 213 - val |= RTL9300_I2C_MST_CTRL1_I2C_TRIG; 214 - mask |= RTL9300_I2C_MST_CTRL1_I2C_TRIG; 215 - 216 - ret = regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, mask, val); 170 + ret = regmap_field_write(i2c->fields[F_DEV_ADDR], xfer->dev_addr); 217 171 if (ret) 218 172 return ret; 219 173 220 - ret = regmap_read_poll_timeout(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, 221 - val, !(val & RTL9300_I2C_MST_CTRL1_I2C_TRIG), 100, 100000); 174 + ret = rtl9300_i2c_reg_addr_set(i2c, xfer->reg_addr, xfer->reg_addr_len); 222 175 if (ret) 223 176 return ret; 224 177 225 - if (val & RTL9300_I2C_MST_CTRL1_I2C_FAIL) 178 + ret = regmap_field_write(i2c->fields[F_RWOP], xfer->write); 179 + if (ret) 180 + return ret; 181 + 182 + ret = regmap_field_write(i2c->fields[F_DATA_WIDTH], (xfer->data_len - 1) & 0xf); 183 + if (ret) 184 + return ret; 185 + 186 + if (xfer->write) { 187 + switch (xfer->type) { 188 + case RTL9300_I2C_XFER_BYTE: 189 + ret = rtl9300_i2c_writel(i2c, *xfer->data); 190 + break; 191 + case RTL9300_I2C_XFER_WORD: 192 + ret = rtl9300_i2c_writel(i2c, get_unaligned((const u16 *)xfer->data)); 193 + break; 194 + default: 195 + ret = rtl9300_i2c_write(i2c, xfer->data, xfer->data_len); 196 + break; 197 + } 198 + } 199 + 200 + return ret; 201 + } 202 + 203 + static int rtl9300_i2c_do_xfer(struct rtl9300_i2c *i2c, struct rtl9300_i2c_xfer *xfer) 204 + { 205 + u32 val; 206 + int ret; 207 + 208 + ret = regmap_field_write(i2c->fields[F_I2C_TRIG], 1); 209 + if (ret) 210 + return ret; 211 + 212 + ret = regmap_field_read_poll_timeout(i2c->fields[F_I2C_TRIG], val, !val, 100, 100000); 213 + if (ret) 214 + return ret; 215 + 216 + ret = regmap_field_read(i2c->fields[F_I2C_FAIL], &val); 217 + if (ret) 218 + return ret; 219 + if (val) 226 220 return -EIO; 227 221 228 - if (read_write == I2C_SMBUS_READ) { 229 - if (size == I2C_SMBUS_BYTE || size == I2C_SMBUS_BYTE_DATA) { 230 - ret = regmap_read(i2c->regmap, 231 - i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, &val); 222 + if (!xfer->write) { 223 + switch (xfer->type) { 224 + case RTL9300_I2C_XFER_BYTE: 225 + ret = regmap_read(i2c->regmap, i2c->data_reg, &val); 232 226 if (ret) 233 227 return ret; 234 - data->byte = val & 0xff; 235 - } else if (size == I2C_SMBUS_WORD_DATA) { 236 - ret = regmap_read(i2c->regmap, 237 - i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, &val); 228 + 229 + *xfer->data = val & 0xff; 230 + break; 231 + case RTL9300_I2C_XFER_WORD: 232 + ret = regmap_read(i2c->regmap, i2c->data_reg, &val); 238 233 if (ret) 239 234 return ret; 240 - data->word = val & 0xffff; 241 - } else { 242 - ret = rtl9300_i2c_read(i2c, &data->block[0], len); 235 + 236 + put_unaligned(val & 0xffff, (u16*)xfer->data); 237 + break; 238 + default: 239 + ret = rtl9300_i2c_read(i2c, xfer->data, xfer->data_len); 243 240 if (ret) 244 241 return ret; 242 + break; 245 243 } 246 244 } 247 245 ··· 292 214 { 293 215 struct rtl9300_i2c_chan *chan = i2c_get_adapdata(adap); 294 216 struct rtl9300_i2c *i2c = chan->i2c; 295 - int len = 0, ret; 217 + struct rtl9300_i2c_xfer xfer = {0}; 218 + int ret; 296 219 297 - mutex_lock(&i2c->lock); 298 - if (chan->sda_pin != i2c->sda_pin) { 299 - ret = rtl9300_i2c_config_io(i2c, chan->sda_pin); 300 - if (ret) 301 - goto out_unlock; 302 - i2c->sda_pin = chan->sda_pin; 303 - } 220 + if (addr > 0x7f) 221 + return -EINVAL; 222 + 223 + guard(rtl9300_i2c)(i2c); 224 + 225 + ret = rtl9300_i2c_config_chan(i2c, chan); 226 + if (ret) 227 + return ret; 228 + 229 + xfer.dev_addr = addr & 0x7f; 230 + xfer.write = (read_write == I2C_SMBUS_WRITE); 231 + xfer.reg_addr = command; 232 + xfer.reg_addr_len = 1; 304 233 305 234 switch (size) { 306 235 case I2C_SMBUS_BYTE: 307 - if (read_write == I2C_SMBUS_WRITE) { 308 - ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 0); 309 - if (ret) 310 - goto out_unlock; 311 - ret = rtl9300_i2c_reg_addr_set(i2c, command, 1); 312 - if (ret) 313 - goto out_unlock; 314 - } else { 315 - ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 1); 316 - if (ret) 317 - goto out_unlock; 318 - ret = rtl9300_i2c_reg_addr_set(i2c, 0, 0); 319 - if (ret) 320 - goto out_unlock; 321 - } 236 + xfer.data = (read_write == I2C_SMBUS_READ) ? &data->byte : &command; 237 + xfer.data_len = 1; 238 + xfer.reg_addr = 0; 239 + xfer.reg_addr_len = 0; 240 + xfer.type = RTL9300_I2C_XFER_BYTE; 322 241 break; 323 - 324 242 case I2C_SMBUS_BYTE_DATA: 325 - ret = rtl9300_i2c_reg_addr_set(i2c, command, 1); 326 - if (ret) 327 - goto out_unlock; 328 - ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 1); 329 - if (ret) 330 - goto out_unlock; 331 - if (read_write == I2C_SMBUS_WRITE) { 332 - ret = rtl9300_i2c_writel(i2c, data->byte); 333 - if (ret) 334 - goto out_unlock; 335 - } 243 + xfer.data = &data->byte; 244 + xfer.data_len = 1; 245 + xfer.type = RTL9300_I2C_XFER_BYTE; 336 246 break; 337 - 338 247 case I2C_SMBUS_WORD_DATA: 339 - ret = rtl9300_i2c_reg_addr_set(i2c, command, 1); 340 - if (ret) 341 - goto out_unlock; 342 - ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 2); 343 - if (ret) 344 - goto out_unlock; 345 - if (read_write == I2C_SMBUS_WRITE) { 346 - ret = rtl9300_i2c_writel(i2c, data->word); 347 - if (ret) 348 - goto out_unlock; 349 - } 248 + xfer.data = (u8 *)&data->word; 249 + xfer.data_len = 2; 250 + xfer.type = RTL9300_I2C_XFER_WORD; 350 251 break; 351 - 352 252 case I2C_SMBUS_BLOCK_DATA: 353 - ret = rtl9300_i2c_reg_addr_set(i2c, command, 1); 354 - if (ret) 355 - goto out_unlock; 356 - if (data->block[0] < 1 || data->block[0] > I2C_SMBUS_BLOCK_MAX) { 357 - ret = -EINVAL; 358 - goto out_unlock; 359 - } 360 - ret = rtl9300_i2c_config_xfer(i2c, chan, addr, data->block[0] + 1); 361 - if (ret) 362 - goto out_unlock; 363 - if (read_write == I2C_SMBUS_WRITE) { 364 - ret = rtl9300_i2c_write(i2c, &data->block[0], data->block[0] + 1); 365 - if (ret) 366 - goto out_unlock; 367 - } 368 - len = data->block[0] + 1; 253 + xfer.data = &data->block[0]; 254 + xfer.data_len = data->block[0] + 1; 255 + xfer.type = RTL9300_I2C_XFER_BLOCK; 369 256 break; 370 - 257 + case I2C_SMBUS_I2C_BLOCK_DATA: 258 + xfer.data = &data->block[1]; 259 + xfer.data_len = data->block[0]; 260 + xfer.type = RTL9300_I2C_XFER_BLOCK; 261 + break; 371 262 default: 372 263 dev_err(&adap->dev, "Unsupported transaction %d\n", size); 373 - ret = -EOPNOTSUPP; 374 - goto out_unlock; 264 + return -EOPNOTSUPP; 375 265 } 376 266 377 - ret = rtl9300_i2c_execute_xfer(i2c, read_write, size, data, len); 267 + ret = rtl9300_i2c_prepare_xfer(i2c, &xfer); 268 + if (ret) 269 + return ret; 378 270 379 - out_unlock: 380 - mutex_unlock(&i2c->lock); 381 - 382 - return ret; 271 + return rtl9300_i2c_do_xfer(i2c, &xfer); 383 272 } 384 273 385 274 static u32 rtl9300_i2c_func(struct i2c_adapter *a) 386 275 { 387 276 return I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA | 388 - I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA; 277 + I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA | 278 + I2C_FUNC_SMBUS_I2C_BLOCK; 389 279 } 390 280 391 281 static const struct i2c_algorithm rtl9300_i2c_algo = { ··· 371 325 { 372 326 struct device *dev = &pdev->dev; 373 327 struct rtl9300_i2c *i2c; 374 - u32 clock_freq, sda_pin; 375 - int ret, i = 0; 376 328 struct fwnode_handle *child; 329 + struct rtl9300_i2c_drv_data *drv_data; 330 + struct reg_field fields[F_NUM_FIELDS]; 331 + u32 clock_freq, scl_num, sda_num; 332 + int ret, i = 0; 377 333 378 334 i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL); 379 335 if (!i2c) ··· 392 344 if (ret) 393 345 return ret; 394 346 347 + ret = device_property_read_u32(dev, "realtek,scl", &scl_num); 348 + if (ret || scl_num != 1) 349 + scl_num = 0; 350 + i2c->scl_num = (u8)scl_num; 351 + 395 352 platform_set_drvdata(pdev, i2c); 396 353 397 - if (device_get_child_node_count(dev) > RTL9300_I2C_MUX_NCHAN) 354 + drv_data = (struct rtl9300_i2c_drv_data *)device_get_match_data(i2c->dev); 355 + if (device_get_child_node_count(dev) > drv_data->max_nchan) 398 356 return dev_err_probe(dev, -EINVAL, "Too many channels\n"); 399 357 358 + i2c->data_reg = i2c->reg_base + drv_data->data_reg; 359 + for (i = 0; i < F_NUM_FIELDS; i++) { 360 + fields[i] = drv_data->field_desc[i].field; 361 + if (drv_data->field_desc[i].scope == REG_SCOPE_MASTER) 362 + fields[i].reg += i2c->reg_base; 363 + } 364 + ret = devm_regmap_field_bulk_alloc(dev, i2c->regmap, i2c->fields, 365 + fields, F_NUM_FIELDS); 366 + if (ret) 367 + return ret; 368 + 369 + i = 0; 400 370 device_for_each_child_node(dev, child) { 401 371 struct rtl9300_i2c_chan *chan = &i2c->chans[i]; 402 372 struct i2c_adapter *adap = &chan->adap; 403 373 404 - ret = fwnode_property_read_u32(child, "reg", &sda_pin); 374 + ret = fwnode_property_read_u32(child, "reg", &sda_num); 405 375 if (ret) 406 376 return ret; 407 377 ··· 431 365 case I2C_MAX_STANDARD_MODE_FREQ: 432 366 chan->bus_freq = RTL9300_I2C_STD_FREQ; 433 367 break; 434 - 435 368 case I2C_MAX_FAST_MODE_FREQ: 436 369 chan->bus_freq = RTL9300_I2C_FAST_FREQ; 437 370 break; 438 371 default: 439 372 dev_warn(i2c->dev, "SDA%d clock-frequency %d not supported using default\n", 440 - sda_pin, clock_freq); 373 + sda_num, clock_freq); 441 374 break; 442 375 } 443 376 444 - chan->sda_pin = sda_pin; 377 + chan->sda_num = sda_num; 445 378 chan->i2c = i2c; 446 379 adap = &i2c->chans[i].adap; 447 380 adap->owner = THIS_MODULE; ··· 450 385 adap->dev.parent = dev; 451 386 i2c_set_adapdata(adap, chan); 452 387 adap->dev.of_node = to_of_node(child); 453 - snprintf(adap->name, sizeof(adap->name), "%s SDA%d\n", dev_name(dev), sda_pin); 388 + snprintf(adap->name, sizeof(adap->name), "%s SDA%d\n", dev_name(dev), sda_num); 454 389 i++; 455 390 456 391 ret = devm_i2c_add_adapter(dev, adap); 457 392 if (ret) 458 393 return ret; 459 394 } 460 - i2c->sda_pin = 0xff; 395 + i2c->sda_num = 0xff; 396 + 397 + /* only use standard read format */ 398 + ret = regmap_field_write(i2c->fields[F_RD_MODE], 0); 399 + if (ret) 400 + return ret; 461 401 462 402 return 0; 463 403 } 464 404 405 + #define GLB_REG_FIELD(reg, msb, lsb) \ 406 + { .field = REG_FIELD(reg, msb, lsb), .scope = REG_SCOPE_GLOBAL } 407 + #define MST_REG_FIELD(reg, msb, lsb) \ 408 + { .field = REG_FIELD(reg, msb, lsb), .scope = REG_SCOPE_MASTER } 409 + 410 + static const struct rtl9300_i2c_drv_data rtl9300_i2c_drv_data = { 411 + .field_desc = { 412 + [F_MEM_ADDR] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL1, 8, 31), 413 + [F_SDA_OUT_SEL] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL1, 4, 6), 414 + [F_SCL_SEL] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL1, 3, 3), 415 + [F_RWOP] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL1, 2, 2), 416 + [F_I2C_FAIL] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL1, 1, 1), 417 + [F_I2C_TRIG] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL1, 0, 0), 418 + [F_RD_MODE] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL2, 15, 15), 419 + [F_DEV_ADDR] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL2, 8, 14), 420 + [F_DATA_WIDTH] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL2, 4, 7), 421 + [F_MEM_ADDR_WIDTH] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL2, 2, 3), 422 + [F_SCL_FREQ] = MST_REG_FIELD(RTL9300_I2C_MST_CTRL2, 0, 1), 423 + [F_SDA_SEL] = GLB_REG_FIELD(RTL9300_I2C_MST_GLB_CTRL, 0, 7), 424 + }, 425 + .select_scl = rtl9300_i2c_select_scl, 426 + .data_reg = RTL9300_I2C_MST_DATA_WORD0, 427 + .max_nchan = RTL9300_I2C_MUX_NCHAN, 428 + }; 429 + 430 + static const struct rtl9300_i2c_drv_data rtl9310_i2c_drv_data = { 431 + .field_desc = { 432 + [F_SCL_SEL] = GLB_REG_FIELD(RTL9310_I2C_MST_IF_SEL, 12, 13), 433 + [F_SDA_SEL] = GLB_REG_FIELD(RTL9310_I2C_MST_IF_SEL, 0, 11), 434 + [F_SCL_FREQ] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 30, 31), 435 + [F_DEV_ADDR] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 11, 17), 436 + [F_SDA_OUT_SEL] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 18, 21), 437 + [F_MEM_ADDR_WIDTH] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 9, 10), 438 + [F_DATA_WIDTH] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 5, 8), 439 + [F_RD_MODE] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 4, 4), 440 + [F_RWOP] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 2, 2), 441 + [F_I2C_FAIL] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 1, 1), 442 + [F_I2C_TRIG] = MST_REG_FIELD(RTL9310_I2C_MST_CTRL, 0, 0), 443 + [F_MEM_ADDR] = MST_REG_FIELD(RTL9310_I2C_MST_MEMADDR_CTRL, 0, 23), 444 + }, 445 + .select_scl = rtl9310_i2c_select_scl, 446 + .data_reg = RTL9310_I2C_MST_DATA_CTRL, 447 + .max_nchan = RTL9310_I2C_MUX_NCHAN, 448 + }; 449 + 465 450 static const struct of_device_id i2c_rtl9300_dt_ids[] = { 466 - { .compatible = "realtek,rtl9301-i2c" }, 467 - { .compatible = "realtek,rtl9302b-i2c" }, 468 - { .compatible = "realtek,rtl9302c-i2c" }, 469 - { .compatible = "realtek,rtl9303-i2c" }, 451 + { .compatible = "realtek,rtl9301-i2c", .data = (void *) &rtl9300_i2c_drv_data }, 452 + { .compatible = "realtek,rtl9302b-i2c", .data = (void *) &rtl9300_i2c_drv_data }, 453 + { .compatible = "realtek,rtl9302c-i2c", .data = (void *) &rtl9300_i2c_drv_data }, 454 + { .compatible = "realtek,rtl9303-i2c", .data = (void *) &rtl9300_i2c_drv_data }, 455 + { .compatible = "realtek,rtl9310-i2c", .data = (void *) &rtl9310_i2c_drv_data }, 456 + { .compatible = "realtek,rtl9311-i2c", .data = (void *) &rtl9310_i2c_drv_data }, 457 + { .compatible = "realtek,rtl9312-i2c", .data = (void *) &rtl9310_i2c_drv_data }, 458 + { .compatible = "realtek,rtl9313-i2c", .data = (void *) &rtl9310_i2c_drv_data }, 470 459 {} 471 460 }; 472 461 MODULE_DEVICE_TABLE(of, i2c_rtl9300_dt_ids);
+1 -1
include/linux/i2c-algo-pca.h
··· 71 71 void *data; /* private low level data */ 72 72 void (*write_byte) (void *data, int reg, int val); 73 73 int (*read_byte) (void *data, int reg); 74 - int (*wait_for_completion) (void *data); 74 + int (*wait_for_completion_cb) (void *data); 75 75 void (*reset_chip) (void *data); 76 76 /* For PCA9564, use one of the predefined frequencies: 77 77 * 330000, 288000, 217000, 146000, 88000, 59000, 44000, 36000