i2c: ls2x: Add support for the Loongson-2K/LS7A I2C controller
Subsystems (5)
Binbin Zhou
maintainerv8 cover i2c: ls2x: Add support for the Loongson-2K/LS7A I2C controller
Hi all:
This patch series adds support for the I2C module found on various Loongson systems with the Loongson-2K SoC or the Loongson LS7A bridge chip.
For now, the I2C driver is suitable for DT-based or ACPI-based systems.
I have tested on Loongson-3A5000LA+LS7A1000/LS7A2000, Loongson-2K1000LA and Loongson-2K0500.
Thanks.
Changes since V7:
- patch (3/4)
- I2C_LS2X should be added to the Kconfig/Makefile in Latin
alphabetical order;
- To avoid repeated type conversions, 'struct ls2x_i2c_priv *priv'
replaces 'struct i2c_adapter *adap' as the function parameter,
e.g. ls2x_i2c_start();
- Refactor ls2x_i2c_stop() with readb_poll_timeout(), mainly with
LS2X_SR_BUSY to indicate the status (success/timeout) of the stop
command;
- Remove extra parentheses;
- For consistency, the variable 'r' in ls2x_i2c_probe() is renamed to
'ret'.Changes since V6:
- patch (1/4)
- Add Reviewed-by tag.
- patch (3/4)
- GPL-2.0 -> GPL-2.0-only;
- Add property.h;
- writew() should be used to operate I2C_LS2X_PRER, and drop the
suffix of I2C_LS2X_PRER_LO/I2C_LS2X_PRER_HI;
- Drop ls2x_i2c_priv->dev, for it can be completely replaced by
'adapter.dev.parent';
- Reasonable return value handling in ls2x_i2c_xfer_one();
- Dropping the I2C_M_STOP flag judgment, the stop parameter of
ls2x_i2c_xfer_one() represents the last msg; - Add comments for subsys_initcall();
- Code formatting, such as dropping unnecessary blank lines.
Changes since V5:
- patch (1/4)
- Add property.h.
- patch (3/4)
- Put the ls2x_i2c_reginit() in front of irq requst;
- Refact ls2x_i2c_adjust_bus_speed: discard the magic value of the divider register and LS2X_I2C_FREQ_STD is used to calculate the frequency;
- Drop useless parameters: priv->suspended, and also disable I2C
interrupts during suspend;
- Drop ls2x_i2c_remove(), for the adapter will be auto deleted on
driver detach; - Drop MODULE_ALIAS;
- Code formatting, such as alignment.
Thanks Andy for your comments.
Changes since V4:
- patch (1/4)
- Drop unneeded headers: of.h;
- xxx_props -> xxx_properties.
- patch (2/4)
- Add interrupt headers to fix syntax error found by Rob.
- patch (3/4)
- Drop atmoic loop in ls2x_i2c_master_xfer(), I have tested it on the
appropriate environment with no problems;
- Define the corresponding bits in I2C_LS2X_CTR to avoid magic
numbers;
- dev_get_drvdata() is used to get ls2x_i2c_priv() in
ls2x_i2c_suspend(); - i2c_add_adapter() -> devm_i2c_add_adapter();
- SET_SYSTEM_SLEEP_PM_OPS() -> DEFINE_RUNTIME_DEV_PM_OPS();
- Code formatting, such as alignment.
Details: https://lore.kernel.org/all/Y4e%[email protected]/
Changes since V3:
- Addressed all review comments from v3
- Change the changelog text to make it clearer (1/5);
- Fix some minor bugs, such as formatting issues (2/5);
- Fix some formatting issues (3/5);
- Deep refactoring of code for clarity (4/5).
Details: https://lore.kernel.org/all/[email protected]/
Thanks to all for their suggestions.
Changes since V2:
- Addressed all review comments from v2
- Drop of_match_ptr() in i2c-gpio to avoid potential unused warnings
(1/5);
- Introduce i2c_gpio_get_props() function as the generic interface
to get i2c-gpio props from DT or ACPI table (2/5);- Refact ls2x i2c code, similar to removing excessive goto tags (4/5).
Thanks to Andy and Mika for their suggestions.
Changes since V1:
- Remove the function of getting the static i2c bus number from ACPI "_UID";
- Fix build warning from kernel test robot.
Binbin Zhou (4):
i2c: gpio: Add support on ACPI-based system
dt-bindings: i2c: add Loongson LS2X I2C controller
i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
LoongArch: Enable LS2X I2C in loongson3_defconfig
.../bindings/i2c/loongson,ls2x-i2c.yaml | 51 +++ arch/loongarch/configs/loongson3_defconfig | 1 + drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-gpio.c | 28 +- drivers/i2c/busses/i2c-ls2x.c | 366 ++++++++++++++++++ 6 files changed, 448 insertions(+), 10 deletions(-) create mode 100644 Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml create mode 100644 drivers/i2c/busses/i2c-ls2x.c
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v8 1/4 i2c: gpio: Add support on ACPI-based system
Add support for the ACPI-based device registration, so that the driver can be also enabled through ACPI table.
--- drivers/i2c/busses/i2c-gpio.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-gpio.c +18 -10
@@ -13,9 +13,9 @@#include <linux/init.h> #include <linux/interrupt.h> #include <linux/module.h> -#include <linux/of.h> #include <linux/platform_data/i2c-gpio.h> #include <linux/platform_device.h> +#include <linux/property.h> #include <linux/slab.h> struct i2c_gpio_private_data {
@@ -300,22 +300,23 @@ static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}static inline void i2c_gpio_fault_injector_exit (struct platform_device *pdev) {}#endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/ -static void of_i2c_gpio_get_props (struct device_node *np, -struct i2c_gpio_platform_data *pdata) +/* Get i2c-gpio properties from DT or ACPI table */ +static void i2c_gpio_get_properties (struct device *dev, +struct i2c_gpio_platform_data *pdata) {u32 reg; -of_property_read_u32 (np,"i2c-gpio,delay-us" , &pdata->udelay); +device_property_read_u32 (dev,"i2c-gpio,delay-us" , &pdata->udelay); -if (!of_property_read_u32 (np,"i2c-gpio,timeout-ms" , ®)) +if (!device_property_read_u32 (dev,"i2c-gpio,timeout-ms" , ®)) pdata->timeout =msecs_to_jiffies (reg); pdata->sda_is_open_drain = -of_property_read_bool (np,"i2c-gpio,sda-open-drain" ); +device_property_read_bool (dev,"i2c-gpio,sda-open-drain" ); pdata->scl_is_open_drain = -of_property_read_bool (np,"i2c-gpio,scl-open-drain" ); +device_property_read_bool (dev,"i2c-gpio,scl-open-drain" ); pdata->scl_is_output_only = -of_property_read_bool (np,"i2c-gpio,scl-output-only" ); +device_property_read_bool (dev,"i2c-gpio,scl-output-only" ); } static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
@@ -373,8 +374,8 @@ static int i2c_gpio_probe(struct platform_device *pdev) bit_data = &priv->bit_data; pdata = &priv->pdata; -if (np) { -of_i2c_gpio_get_props (np, pdata); +if (dev_fwnode (dev)) { +i2c_gpio_get_properties (dev, pdata); }else {/* * If all platform data settings are zero it is OK
@@ -489,10 +490,17 @@ static const struct of_device_id i2c_gpio_dt_ids[] = {MODULE_DEVICE_TABLE (of, i2c_gpio_dt_ids); +static const struct acpi_device_id i2c_gpio_acpi_match[] = { + {"LOON0005" },/* LoongArch */ + { } +}; +MODULE_DEVICE_TABLE (acpi, i2c_gpio_acpi_match); +static struct platform_driver i2c_gpio_driver = { .driver = { .name ="i2c-gpio" , .of_match_table = i2c_gpio_dt_ids, + .acpi_match_table = i2c_gpio_acpi_match, }, .probe = i2c_gpio_probe, .remove = i2c_gpio_remove,
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v8 2/4 dt-bindings: i2c: add Loongson LS2X I2C controller
In reply to Binbin ZhouAdd Loongson LS2X I2C controller binding with DT schema format using json-schema.
--- .../bindings/i2c/loongson,ls2x-i2c.yaml | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml
Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml new file +51 -0
@@ -0,0 +1,51 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/i2c/loongson,ls2x-i2c.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Loongson LS2X I2C Controller + +maintainers: + - Binbin Zhou <[email protected]> + +allOf: + - $ref: /schemas/i2c/i2c-controller.yaml# + +properties: + compatible: + enum: + - loongson,ls2k-i2c + - loongson,ls7a-i2c + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + +required: + - compatible + - reg + - interrupts + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + i2c0: i2c@1fe21000 { + compatible = "loongson,ls2k-i2c"; + reg = <0x1fe21000 0x8>; + interrupt-parent = <&extioiic>; + interrupts = <22 IRQ_TYPE_LEVEL_LOW>; + #address-cells = <1>; + #size-cells = <0>; + + eeprom@57 { + compatible = "atmel,24c16"; + reg = <0x57>; + pagesize = <16>; + }; + };
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v8 3/4 i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
In reply to Binbin ZhouThis I2C module is integrated into the Loongson-2K SoCs and Loongson LS7A bridge chip.
--- drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ls2x.c | 366 ++++++++++++++++++++++++++++++++++ 3 files changed, 378 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ls2x.c
drivers/i2c/busses/Kconfig +11 -0
@@ -761,6 +761,17 @@ config I2C_LPC2K This driver can also be built as a module. If so, the module will be called i2c-lpc2k. +config I2C_LS2X + tristate "Loongson LS2X I2C adapter" + depends on MACH_LOONGSON64 || COMPILE_TEST + help + If you say yes to this option, support will be included for the + I2C interface on the Loongson-2K SoCs and Loongson LS7A bridge + chip. + + This driver can also be built as a module. If so, the module + will be called i2c-ls2x. + config I2C_MLXBF tristate "Mellanox BlueField I2C controller" depends on MELLANOX_PLATFORM && ARM64
drivers/i2c/busses/Makefile +1 -0
@@ -77,6 +77,7 @@ obj-$(CONFIG_I2C_IOP3XX) += i2c-iop3xx.o obj-$(CONFIG_I2C_JZ4780) += i2c-jz4780.o obj-$(CONFIG_I2C_KEMPLD) += i2c-kempld.o obj-$(CONFIG_I2C_LPC2K) += i2c-lpc2k.o +obj-$(CONFIG_I2C_LS2X) += i2c-ls2x.o obj-$(CONFIG_I2C_MESON) += i2c-meson.o obj-$(CONFIG_I2C_MICROCHIP_CORE) += i2c-microchip-corei2c.o obj-$(CONFIG_I2C_MPC) += i2c-mpc.o
drivers/i2c/busses/i2c-ls2x.c new file +366 -0
366 lines not on this page · raw message
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v8 4/4 LoongArch: Enable LS2X I2C in loongson3_defconfig
In reply to Binbin ZhouThis is now supported, enable for Loongson-3 systems. Other systems are unaffected.
--- arch/loongarch/configs/loongson3_defconfig | 1 + 1 file changed, 1 insertion(+)
arch/loongarch/configs/loongson3_defconfig +1 -0
@@ -602,6 +602,7 @@ CONFIG_HW_RANDOM_VIRTIO=m CONFIG_I2C_CHARDEV=y CONFIG_I2C_PIIX4=y CONFIG_I2C_GPIO=y +CONFIG_I2C_LS2X=y CONFIG_SPI=y CONFIG_GPIO_SYSFS=y CONFIG_GPIO_LOONGSON=y
Signature · 1 line
2.31.1
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
In reply to Binbin ZhouQuoted 2 lines · Binbin Zhou
This I2C module is integrated into the Loongson-2K SoCs and Loongson LS7A bridge chip.
...
Quoted 15 lines
drivers/i2c/busses/i2c-ls2x.c new file +15
-0+static int ls2x_i2c_xfer_one (struct ls2x_i2c_priv *priv, +struct i2c_msg *msg,bool stop) +{ +int ret; +bool is_read = msg->flags &I2C_M_RD ; + +/* Contains steps to send start condition and address */ + ret =ls2x_i2c_start (priv, msg); +if (!ret) { +if (is_read) + ret =ls2x_i2c_rx (priv, msg->buf, msg->len); +else + ret =ls2x_i2c_tx (priv, msg->buf, msg->len); + +if (!ret && stop)
drivers/i2c/busses/i2c-ls2x.c line 237
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x.c new file +10
-0+ ret =ls2x_i2c_stop (priv); + } + +if (ret == -ENXIO ) +ls2x_i2c_stop (priv); +else if (ret <0 ) +ls2x_i2c_init (priv); + +return ret; +}
Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two functions and then do something like
_read_one()
{
ret = start();
if (ret)
goto _stop; // Do we really need this?
ret = rx();
if (ret)
goto _stop; // Do we need this?
/* By setting this call the stop */
if (stop)
ret = -ENXIO;
out_send_stop:
if (ret == ...)
return _stop();
// I don't like above, so this error checking/setting parts
// also can be rethought and refactored accordingly
return ret;
}if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
Hi Andy:
Sorry for my late reply.
Quoted 27 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou
This I2C module is integrated into the Loongson-2K SoCs and Loongson LS7A bridge chip.
...
Quoted 20 lines
drivers/i2c/busses/i2c-ls2x.c new file +20
-0+static int ls2x_i2c_xfer_one (struct ls2x_i2c_priv *priv, +struct i2c_msg *msg,bool stop) +{ +int ret; +bool is_read = msg->flags &I2C_M_RD ; + +/* Contains steps to send start condition and address */ + ret =ls2x_i2c_start (priv, msg); +if (!ret) { +if (is_read) + ret =ls2x_i2c_rx (priv, msg->buf, msg->len); +else + ret =ls2x_i2c_tx (priv, msg->buf, msg->len); + +if (!ret && stop) + ret =ls2x_i2c_stop (priv); + } + +if (ret == -ENXIO ) +ls2x_i2c_stop (priv);
Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c line 237
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5
-0+else if (ret <0 ) +ls2x_i2c_init (priv); + +return ret; +}Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two
Sorry,
Actually, I don't quite understand why you keep thinking that the stop can be called twice in a row.
As I said in my last email, the logic here should be:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.
The key pointer is the stop function will only return a TIMEOUT error or 0 for success, so if the stop function above is failed, the stop function below will never be called twice.
Anyway, I also admit that this part of the code may not be concise and clear enough, and I have tried the following changes:
1. put the start function into the rx/tx function respectively. As followers:
@@ -177,10 +177,16 @@ static int ls2x_i2c_start(struct ls2x_i2c_priv *priv, struct i2c_msg *msgs)
return ls2x_i2c_send_byte(priv, LS2X_CR_START | LS2X_CR_WRITE);
}
-static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg)
{
int ret;
- u8 rxdata;
+ u8 rxdata, *buf = msg->buf;
+ u16 len = msg->len;
+
+ /* Contains steps to send start condition and address */
+ ret = ls2x_i2c_start(priv, msg);
+ if (ret)+ return ret;
while (len--) {
ret = ls2x_i2c_xfer_byte(priv,
@@ -195,9 +201,16 @@ static int ls2x_i2c_rx(struct ls2x_i2c_priv
*priv, u8 *buf, u16 len)
return 0;}
-static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg)
{
int ret;
+ u8 *buf = msg->buf;
+ u16 len = msg->len;
+
+ /* Contains steps to send start condition and address */
+ ret = ls2x_i2c_start(priv, msg);
+ if (ret)+ return ret;
while (len--) {
writeb(*buf++, priv->base + I2C_LS2X_TXR);2. define the variable 'reinit' in the xfer_one function to mark the cases where reinit is needed. As follows:
static int ls2x_i2c_xfer_one(struct ls2x_i2c_priv *priv,
struct i2c_msg *msg, bool stop)
{
int ret, ret2;
bool reinit = false;
bool is_read = msg->flags & I2C_M_RD;
if (is_read)
ret = ls2x_i2c_rx(priv, msg);
else
ret = ls2x_i2c_tx(priv, msg);
if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */
return ret;
if (ret == -ETIMEDOUT) {
/* Fatal error. Needs reinit. */
stop = false;
reinit = true;
}
if (stop) {
ret2 = ls2x_i2c_stop(priv);
if (ret2) {
/* Failed to issue STOP. Needs reinit. */
reinit = true;
ret = ret ?: ret2;
}
}
if (reinit)
ls2x_i2c_init(priv);
return ret;
}Do you think this is better?
Thanks.
Binbin
Quoted 44 lines
functions and then do something like
_read_one(){ ret = start(); if (ret) goto _stop; // Do we really need this? ret = rx(); if (ret) goto _stop; // Do we need this? /* By setting this call the stop */ if (stop) ret = -ENXIO; out_send_stop: if (ret == ...) return _stop(); // I don't like above, so this error checking/setting parts // also can be rethought and refactored accordingly return ret; }if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;--
With Best Regards,
Andy Shevchenko
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko
On Fri, Dec 23, 2022 at 05:00:51PM +0800, Binbin Zhou wrote:
...
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
drivers/i2c/busses/i2c-ls2x.c new file +20
-0+static int ls2x_i2c_xfer_one (struct ls2x_i2c_priv *priv, +struct i2c_msg *msg,bool stop) +{ +int ret; +bool is_read = msg->flags &I2C_M_RD ; + +/* Contains steps to send start condition and address */ + ret =ls2x_i2c_start (priv, msg); +if (!ret) { +if (is_read) + ret =ls2x_i2c_rx (priv, msg->buf, msg->len); +else + ret =ls2x_i2c_tx (priv, msg->buf, msg->len); + +if (!ret && stop) + ret =ls2x_i2c_stop (priv); + } + +if (ret == -ENXIO ) +ls2x_i2c_stop (priv);Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c line 237
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5
-0+else if (ret <0 ) +ls2x_i2c_init (priv); + +return ret; +}Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two
Quoted 3 lines
Sorry,
Actually, I don't quite understand why you keep thinking that the stop can be called twice in a row.
Because nothing in the code suggests otherwise. You need deeply understand the flow to ensure that it won't. This means that the code is fragile and needs refactoring (even comment, which you can do a least won't help, because changing code in the other parts may break all this and you won't notice it).
Quoted 97 lines
As I said in my last email, the logic here should be:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.The key pointer is the stop function will only return a TIMEOUT error or 0 for success, so if the stop function above is failed, the stop function below will never be called twice.
Anyway, I also admit that this part of the code may not be concise and clear enough, and I have tried the following changes:
1. put the start function into the rx/tx function respectively. As followers:
(quoted diff) +8
-2@@ -177,10 +177,16 @@ static int ls2x_i2c_start(struct ls2x_i2c_priv *priv, struct i2c_msg *msgs) return ls2x_i2c_send_byte(priv, LS2X_CR_START | LS2X_CR_WRITE); } -static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; - u8 rxdata; + u8 rxdata, *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {ret = ls2x_i2c_xfer_byte(priv,(quoted diff) +8
-1@@ -195,9 +201,16 @@ static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) return 0; } -static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; + u8 *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {writeb(*buf++, priv->base + I2C_LS2X_TXR);2. define the variable 'reinit' in the
xfer_onefunction to mark the cases where reinit is needed. As follows:static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret, ret2; bool reinit = false; bool is_read = msg->flags & I2C_M_RD; if (is_read) ret = ls2x_i2c_rx(priv, msg); else ret = ls2x_i2c_tx(priv, msg); if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */ return ret; if (ret == -ETIMEDOUT) { /* Fatal error. Needs reinit. */ stop = false; reinit = true; } if (stop) { ret2 = ls2x_i2c_stop(priv); if (ret2) { /* Failed to issue STOP. Needs reinit. */ reinit = true; ret = ret ?: ret2; } } if (reinit) ls2x_i2c_init(priv); return ret; }
Quoted 1 line
Do you think this is better?
Slightly, but still twisted at the end with the play of error codes. Try to make it even more clear.
Quoted 35 lines
Quoted 35 lines
functions and then do something like
_read_one(){ ret = start(); if (ret) goto _stop; // Do we really need this? ret = rx(); if (ret) goto _stop; // Do we need this? /* By setting this call the stop */ if (stop) ret = -ENXIO; out_send_stop: if (ret == ...) return _stop(); // I don't like above, so this error checking/setting parts // also can be rethought and refactored accordingly return ret; }if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko
On Fri, Dec 23, 2022 at 05:00:51PM +0800, Binbin Zhou wrote:
...
Quoted 109 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
drivers/i2c/busses/i2c-ls2x.c new file +20
-0+static int ls2x_i2c_xfer_one (struct ls2x_i2c_priv *priv, +struct i2c_msg *msg,bool stop) +{ +int ret; +bool is_read = msg->flags &I2C_M_RD ; + +/* Contains steps to send start condition and address */ + ret =ls2x_i2c_start (priv, msg); +if (!ret) { +if (is_read) + ret =ls2x_i2c_rx (priv, msg->buf, msg->len); +else + ret =ls2x_i2c_tx (priv, msg->buf, msg->len); + +if (!ret && stop) + ret =ls2x_i2c_stop (priv); + } + +if (ret == -ENXIO ) +ls2x_i2c_stop (priv);Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c line 237
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5
-0+else if (ret <0 ) +ls2x_i2c_init (priv); + +return ret; +}Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two
Quoted 3 lines
Sorry,
Actually, I don't quite understand why you keep thinking that the stop can be called twice in a row.Because nothing in the code suggests otherwise. You need deeply understand the flow to ensure that it won't. This means that the code is fragile and needs refactoring (even comment, which you can do a least won't help, because changing code in the other parts may break all this and you won't notice it).
Quoted 71 lines
As I said in my last email, the logic here should be:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.The key pointer is the stop function will only return a TIMEOUT error or 0 for success, so if the stop function above is failed, the stop function below will never be called twice.
Anyway, I also admit that this part of the code may not be concise and clear enough, and I have tried the following changes:
1. put the start function into the rx/tx function respectively. As followers:
(quoted diff) +8
-2@@ -177,10 +177,16 @@ static int ls2x_i2c_start(struct ls2x_i2c_priv *priv, struct i2c_msg *msgs) return ls2x_i2c_send_byte(priv, LS2X_CR_START | LS2X_CR_WRITE); } -static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; - u8 rxdata; + u8 rxdata, *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {ret = ls2x_i2c_xfer_byte(priv,(quoted diff) +8
-1@@ -195,9 +201,16 @@ static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) return 0; } -static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; + u8 *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {writeb(*buf++, priv->base + I2C_LS2X_TXR);2. define the variable 'reinit' in the
xfer_onefunction to mark the cases where reinit is needed. As follows:static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret, ret2; bool reinit = false; bool is_read = msg->flags & I2C_M_RD; if (is_read) ret = ls2x_i2c_rx(priv, msg); else ret = ls2x_i2c_tx(priv, msg);
Quoted 7 lines
Quoted 7 lines
if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */ return ret; if (ret == -ETIMEDOUT) { /* Fatal error. Needs reinit. */ stop = false; reinit = true;
Why do you need to initialize stop here?
Why not to call reinit here and bailout?
Quoted 9 lines
Quoted 9 lines
} if (stop) { ret2 = ls2x_i2c_stop(priv); if (ret2) { /* Failed to issue STOP. Needs reinit. */ reinit = true; ret = ret ?: ret2;
All the same, try to be less verbose with unneeded variables.
Quoted 49 lines
Quoted 10 lines
} } if (reinit) ls2x_i2c_init(priv); return ret; }Do you think this is better?
Slightly, but still twisted at the end with the play of error codes. Try to make it even more clear.
Quoted 35 lines
Quoted 35 lines
functions and then do something like
_read_one(){ ret = start(); if (ret) goto _stop; // Do we really need this? ret = rx(); if (ret) goto _stop; // Do we need this? /* By setting this call the stop */ if (stop) ret = -ENXIO; out_send_stop: if (ret == ...) return _stop(); // I don't like above, so this error checking/setting parts // also can be rethought and refactored accordingly return ret; }if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
Hi Andy:
Quoted 133 lines · Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko
On Fri, Dec 23, 2022 at 05:00:51PM +0800, Binbin Zhou wrote:
...
Quoted 109 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
drivers/i2c/busses/i2c-ls2x.c new file +20
-0+static int ls2x_i2c_xfer_one (struct ls2x_i2c_priv *priv, +struct i2c_msg *msg,bool stop) +{ +int ret; +bool is_read = msg->flags &I2C_M_RD ; + +/* Contains steps to send start condition and address */ + ret =ls2x_i2c_start (priv, msg); +if (!ret) { +if (is_read) + ret =ls2x_i2c_rx (priv, msg->buf, msg->len); +else + ret =ls2x_i2c_tx (priv, msg->buf, msg->len); + +if (!ret && stop) + ret =ls2x_i2c_stop (priv); + } + +if (ret == -ENXIO ) +ls2x_i2c_stop (priv);Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c line 237
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5
-0+else if (ret <0 ) +ls2x_i2c_init (priv); + +return ret; +}Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two
Quoted 3 lines
Sorry,
Actually, I don't quite understand why you keep thinking that the stop can be called twice in a row.Because nothing in the code suggests otherwise. You need deeply understand the flow to ensure that it won't. This means that the code is fragile and needs refactoring (even comment, which you can do a least won't help, because changing code in the other parts may break all this and you won't notice it).
Quoted 71 lines
As I said in my last email, the logic here should be:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.The key pointer is the stop function will only return a TIMEOUT error or 0 for success, so if the stop function above is failed, the stop function below will never be called twice.
Anyway, I also admit that this part of the code may not be concise and clear enough, and I have tried the following changes:
1. put the start function into the rx/tx function respectively. As followers:
(quoted diff) +8
-2@@ -177,10 +177,16 @@ static int ls2x_i2c_start(struct ls2x_i2c_priv *priv, struct i2c_msg *msgs) return ls2x_i2c_send_byte(priv, LS2X_CR_START | LS2X_CR_WRITE); } -static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; - u8 rxdata; + u8 rxdata, *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {ret = ls2x_i2c_xfer_byte(priv,(quoted diff) +8
-1@@ -195,9 +201,16 @@ static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) return 0; } -static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; + u8 *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {writeb(*buf++, priv->base + I2C_LS2X_TXR);2. define the variable 'reinit' in the
xfer_onefunction to mark the cases where reinit is needed. As follows:static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret, ret2; bool reinit = false; bool is_read = msg->flags & I2C_M_RD; if (is_read) ret = ls2x_i2c_rx(priv, msg); else ret = ls2x_i2c_tx(priv, msg);Quoted 7 lines
Quoted 7 lines
if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */ return ret; if (ret == -ETIMEDOUT) { /* Fatal error. Needs reinit. */ stop = false; reinit = true;Why do you need to initialize stop here?
Why not to call reinit here and bailout?Quoted 5 lines
Quoted 5 lines
} if (stop) { ret2 = ls2x_i2c_stop(priv);
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
if (ret2) { /* Failed to issue STOP. Needs reinit. */ reinit = true; ret = ret ?: ret2;All the same, try to be less verbose with unneeded variables.
Ok, the reinit and ret2 variables seem to be a bit redundant, I will remove them.
I will divide the whole thing into two parts:
The first part is to handle errors: if ret < 0, return ret directly. One of the special handling is the fatal error timeout, which requires reinit.
if (ret < 0) {
if (ret == -ETIMEDOUT) /* Fatel error. Needs reinit. */
ls2x_i2c_init(priv);
return ret;
}The second part is to handle the final stop command: it should be noted that if the stop command fails, reinit is also required.
if (stop) {
/* Failed to issue STOP. Needs reinit. */
ret = ls2x_i2c_stop(priv);
if (ret)
ls2x_i2c_init(priv);
}The complete code is as follows:
static int ls2x_i2c_xfer_one(struct ls2x_i2c_priv *priv,
struct i2c_msg *msg, bool stop)
{
int ret;
bool is_read = msg->flags & I2C_M_RD;
if (is_read)
ret = ls2x_i2c_rx(priv, msg);
else
ret = ls2x_i2c_tx(priv, msg);
if (ret < 0) {
if (ret == -ETIMEDOUT) /* Fatel error. Needs reinit. */
ls2x_i2c_init(priv);
return ret;
}
if (stop) {
/* Failed to issue STOP. Needs reinit. */
ret = ls2x_i2c_stop(priv);
if (ret)
ls2x_i2c_init(priv);
}
return ret;
}Do you think this is better?
Thanks.
Binbin
Quoted 56 lines
Quoted 7 lines
Quoted 7 lines
} } if (reinit) ls2x_i2c_init(priv);Quoted 43 lines
Quoted 4 lines
return ret; }Do you think this is better?
Slightly, but still twisted at the end with the play of error codes. Try to make it even more clear.
Quoted 35 lines
Quoted 35 lines
functions and then do something like
_read_one(){ ret = start(); if (ret) goto _stop; // Do we really need this? ret = rx(); if (ret) goto _stop; // Do we need this? /* By setting this call the stop */ if (stop) ret = -ENXIO; out_send_stop: if (ret == ...) return _stop(); // I don't like above, so this error checking/setting parts // also can be rethought and refactored accordingly return ret; }if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;--
With Best Regards,
Andy Shevchenko
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
Quoted 6 lines · Binbin Zhou
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko
On Fri, Dec 23, 2022 at 05:00:51PM +0800, Binbin Zhou wrote:
...
Quoted 149 lines
Quoted 125 lines
Quoted 109 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
drivers/i2c/busses/i2c-ls2x.c new file +20
-0+static int ls2x_i2c_xfer_one (struct ls2x_i2c_priv *priv, +struct i2c_msg *msg,bool stop) +{ +int ret; +bool is_read = msg->flags &I2C_M_RD ; + +/* Contains steps to send start condition and address */ + ret =ls2x_i2c_start (priv, msg); +if (!ret) { +if (is_read) + ret =ls2x_i2c_rx (priv, msg->buf, msg->len); +else + ret =ls2x_i2c_tx (priv, msg->buf, msg->len); + +if (!ret && stop) + ret =ls2x_i2c_stop (priv); + } + +if (ret == -ENXIO ) +ls2x_i2c_stop (priv);Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c line 237
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5
-0+else if (ret <0 ) +ls2x_i2c_init (priv); + +return ret; +}Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two
Quoted 3 lines
Sorry,
Actually, I don't quite understand why you keep thinking that the stop can be called twice in a row.Because nothing in the code suggests otherwise. You need deeply understand the flow to ensure that it won't. This means that the code is fragile and needs refactoring (even comment, which you can do a least won't help, because changing code in the other parts may break all this and you won't notice it).
Quoted 71 lines
As I said in my last email, the logic here should be:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.The key pointer is the stop function will only return a TIMEOUT error or 0 for success, so if the stop function above is failed, the stop function below will never be called twice.
Anyway, I also admit that this part of the code may not be concise and clear enough, and I have tried the following changes:
1. put the start function into the rx/tx function respectively. As followers:
(quoted diff) +8
-2@@ -177,10 +177,16 @@ static int ls2x_i2c_start(struct ls2x_i2c_priv *priv, struct i2c_msg *msgs) return ls2x_i2c_send_byte(priv, LS2X_CR_START | LS2X_CR_WRITE); } -static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; - u8 rxdata; + u8 rxdata, *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {ret = ls2x_i2c_xfer_byte(priv,(quoted diff) +8
-1@@ -195,9 +201,16 @@ static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) return 0; } -static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; + u8 *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {writeb(*buf++, priv->base + I2C_LS2X_TXR);2. define the variable 'reinit' in the
xfer_onefunction to mark the cases where reinit is needed. As follows:static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret, ret2; bool reinit = false; bool is_read = msg->flags & I2C_M_RD; if (is_read) ret = ls2x_i2c_rx(priv, msg); else ret = ls2x_i2c_tx(priv, msg);Quoted 7 lines
Quoted 7 lines
if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */ return ret; if (ret == -ETIMEDOUT) { /* Fatal error. Needs reinit. */ stop = false; reinit = true;Why do you need to initialize stop here?
Why not to call reinit here and bailout?Quoted 5 lines
Quoted 5 lines
} if (stop) { ret2 = ls2x_i2c_stop(priv);Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
if (ret2) { /* Failed to issue STOP. Needs reinit. */ reinit = true; ret = ret ?: ret2;All the same, try to be less verbose with unneeded variables.
Ok, the reinit and ret2 variables seem to be a bit redundant, I will remove them.
I will divide the whole thing into two parts:
The first part is to handle errors: if ret < 0, return ret directly. One of the special handling is the fatal error timeout, which requires reinit.if (ret < 0) { if (ret == -ETIMEDOUT) /* Fatel error. Needs reinit. */ ls2x_i2c_init(priv); return ret; }The second part is to handle the final stop command: it should be noted that if the stop command fails, reinit is also required.
Quoted 8 lines
if (stop) { /* Failed to issue STOP. Needs reinit. */ ret = ls2x_i2c_stop(priv); if (ret) ls2x_i2c_init(priv); }The complete code is as follows:
This looks much better!
See a couple of nit-picks below.
Quoted 8 lines
static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret; bool is_read = msg->flags & I2C_M_RD; if (is_read)
With this you don't need to have is_read variable.
Quoted 6 lines
ret = ls2x_i2c_rx(priv, msg);
else
ret = ls2x_i2c_tx(priv, msg);
if (ret < 0) {
if (ret == -ETIMEDOUT) /* Fatel error. Needs reinit. */Split comment and code, so the comment is followed by the code.
Quoted 65 lines
ls2x_i2c_init(priv); return ret; } if (stop) { /* Failed to issue STOP. Needs reinit. */ ret = ls2x_i2c_stop(priv); if (ret) ls2x_i2c_init(priv); } return ret; }Do you think this is better?
Quoted 49 lines
Quoted 49 lines
Quoted 10 lines
} } if (reinit) ls2x_i2c_init(priv); return ret; }Do you think this is better?
Slightly, but still twisted at the end with the play of error codes. Try to make it even more clear.
Quoted 35 lines
Quoted 35 lines
functions and then do something like
_read_one(){ ret = start(); if (ret) goto _stop; // Do we really need this? ret = rx(); if (ret) goto _stop; // Do we need this? /* By setting this call the stop */ if (stop) ret = -ENXIO; out_send_stop: if (ret == ...) return _stop(); // I don't like above, so this error checking/setting parts // also can be rethought and refactored accordingly return ret; }if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH V8 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
Quoted 177 lines · Andy Shevchenko
Quoted 6 lines · Binbin Zhou
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko
On Fri, Dec 23, 2022 at 05:00:51PM +0800, Binbin Zhou wrote:
...
Quoted 149 lines
Quoted 125 lines
Quoted 109 lines
Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+static int ls2x_i2c_xfer_one(struct ls2x_i2c_priv *priv, + struct i2c_msg *msg, bool stop) +{ + int ret; + bool is_read = msg->flags & I2C_M_RD; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (!ret) { + if (is_read) + ret = ls2x_i2c_rx(priv, msg->buf, msg->len); + else + ret = ls2x_i2c_tx(priv, msg->buf, msg->len); + + if (!ret && stop) + ret = ls2x_i2c_stop(priv); + } + + if (ret == -ENXIO) + ls2x_i2c_stop(priv);Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ else if (ret < 0) + ls2x_i2c_init(priv); + + return ret; +}Still this code is odd from reader's perspective. It's in particular not clear if the stop can be called twice in a row. I recommend to split it to two
Quoted 3 lines
Sorry,
Actually, I don't quite understand why you keep thinking that the stop can be called twice in a row.Because nothing in the code suggests otherwise. You need deeply understand the flow to ensure that it won't. This means that the code is fragile and needs refactoring (even comment, which you can do a least won't help, because changing code in the other parts may break all this and you won't notice it).
Quoted 71 lines
As I said in my last email, the logic here should be:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.The key pointer is the stop function will only return a TIMEOUT error or 0 for success, so if the stop function above is failed, the stop function below will never be called twice.
Anyway, I also admit that this part of the code may not be concise and clear enough, and I have tried the following changes:
1. put the start function into the rx/tx function respectively. As followers:
(quoted diff) +8
-2@@ -177,10 +177,16 @@ static int ls2x_i2c_start(struct ls2x_i2c_priv *priv, struct i2c_msg *msgs) return ls2x_i2c_send_byte(priv, LS2X_CR_START | LS2X_CR_WRITE); } -static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; - u8 rxdata; + u8 rxdata, *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {ret = ls2x_i2c_xfer_byte(priv,(quoted diff) +8
-1@@ -195,9 +201,16 @@ static int ls2x_i2c_rx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) return 0; } -static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, u8 *buf, u16 len) +static int ls2x_i2c_tx(struct ls2x_i2c_priv *priv, struct i2c_msg *msg) { int ret; + u8 *buf = msg->buf; + u16 len = msg->len; + + /* Contains steps to send start condition and address */ + ret = ls2x_i2c_start(priv, msg); + if (ret) + return ret; while (len--) {writeb(*buf++, priv->base + I2C_LS2X_TXR);2. define the variable 'reinit' in the
xfer_onefunction to mark the cases where reinit is needed. As follows:static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret, ret2; bool reinit = false; bool is_read = msg->flags & I2C_M_RD; if (is_read) ret = ls2x_i2c_rx(priv, msg); else ret = ls2x_i2c_tx(priv, msg);Quoted 7 lines
Quoted 7 lines
if (ret == -EAGAIN) /* could not acquire bus. bail out without STOP */ return ret; if (ret == -ETIMEDOUT) { /* Fatal error. Needs reinit. */ stop = false; reinit = true;Why do you need to initialize stop here?
Why not to call reinit here and bailout?Quoted 5 lines
Quoted 5 lines
} if (stop) { ret2 = ls2x_i2c_stop(priv);Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
if (ret2) { /* Failed to issue STOP. Needs reinit. */ reinit = true; ret = ret ?: ret2;All the same, try to be less verbose with unneeded variables.
Ok, the reinit and ret2 variables seem to be a bit redundant, I will remove them.
I will divide the whole thing into two parts:
The first part is to handle errors: if ret < 0, return ret directly. One of the special handling is the fatal error timeout, which requires reinit.if (ret < 0) { if (ret == -ETIMEDOUT) /* Fatel error. Needs reinit. */ ls2x_i2c_init(priv); return ret; }The second part is to handle the final stop command: it should be noted that if the stop command fails, reinit is also required.
Quoted 8 lines
if (stop) { /* Failed to issue STOP. Needs reinit. */ ret = ls2x_i2c_stop(priv); if (ret) ls2x_i2c_init(priv); }The complete code is as follows:
This looks much better!
See a couple of nit-picks below.Quoted 4 lines
static int ls2x_i2c_xfer_one(struct
ls2x_i2c_priv*priv,
structi2c_msg*msg, bool stop){ int ret;
Quoted 5 lines
Quoted 3 lines
bool is_read = msg->flags & I2C_M_RD; if (is_read)With this you don't need to have
is_readvariable.
Well, is_read is a bit redundant, I would use 'msg->flags & I2C_M_RD' directly in the if judgment condition.
Quoted 9 lines
Quoted 7 lines
ret = ls2x_i2c_rx(priv, msg); else ret = ls2x_i2c_tx(priv, msg); if (ret < 0) { if (ret == -ETIMEDOUT) /* Fatel error. Needs reinit. */Split comment and code, so the comment is followed by the code.
Got it.
In the meantime I'll double check the rest!
Thanks.
Binbin
Quoted 72 lines
Quoted 15 lines
ls2x_i2c_init(priv); return ret; } if (stop) { /* Failed to issue STOP. Needs reinit. */ ret = ls2x_i2c_stop(priv); if (ret) ls2x_i2c_init(priv); } return ret; }Quoted 51 lines
Do you think this is better?
Quoted 49 lines
Quoted 49 lines
Quoted 10 lines
} } if (reinit) ls2x_i2c_init(priv); return ret; }Do you think this is better?
Slightly, but still twisted at the end with the play of error codes. Try to make it even more clear.
Quoted 35 lines
Quoted 35 lines
functions and then do something like
_read_one(){ ret = start(); if (ret) goto _stop; // Do we really need this? ret = rx(); if (ret) goto _stop; // Do we need this? /* By setting this call the stop */ if (stop) ret = -ENXIO; out_send_stop: if (ret == ...) return _stop(); // I don't like above, so this error checking/setting parts // also can be rethought and refactored accordingly return ret; }if (is_read) ret = _read_one(); else ret = _write_one(); if (ret) _init(); return ret;--
With Best Regards,
Andy Shevchenko