提交 bb448147 编写于 作者: P Peter Rosin 提交者: Wolfram Sang

i2c: i2c-mux-gpio: convert to use an explicit i2c mux core

Allocate an explicit i2c mux core to handle parent and child adapters
etc. Update the select/deselect ops to be in terms of the i2c mux core
instead of the child adapter.
Signed-off-by: NPeter Rosin <peda@axentia.se>
Signed-off-by: NWolfram Sang <wsa@the-dreams.de>
上级 a7ab7239
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
#include <linux/of_gpio.h> #include <linux/of_gpio.h>
struct gpiomux { struct gpiomux {
struct i2c_adapter *parent;
struct i2c_adapter **adap; /* child busses */
struct i2c_mux_gpio_platform_data data; struct i2c_mux_gpio_platform_data data;
unsigned gpio_base; unsigned gpio_base;
}; };
...@@ -33,18 +31,18 @@ static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) ...@@ -33,18 +31,18 @@ static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
val & (1 << i)); val & (1 << i));
} }
static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan) static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
{ {
struct gpiomux *mux = data; struct gpiomux *mux = i2c_mux_priv(muxc);
i2c_mux_gpio_set(mux, chan); i2c_mux_gpio_set(mux, chan);
return 0; return 0;
} }
static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan) static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
{ {
struct gpiomux *mux = data; struct gpiomux *mux = i2c_mux_priv(muxc);
i2c_mux_gpio_set(mux, mux->data.idle); i2c_mux_gpio_set(mux, mux->data.idle);
...@@ -136,19 +134,15 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux, ...@@ -136,19 +134,15 @@ static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
static int i2c_mux_gpio_probe(struct platform_device *pdev) static int i2c_mux_gpio_probe(struct platform_device *pdev)
{ {
struct i2c_mux_core *muxc;
struct gpiomux *mux; struct gpiomux *mux;
struct i2c_adapter *parent; struct i2c_adapter *parent;
int (*deselect) (struct i2c_adapter *, void *, u32);
unsigned initial_state, gpio_base; unsigned initial_state, gpio_base;
int i, ret; int i, ret;
mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
if (!mux) { if (!mux)
dev_err(&pdev->dev, "Cannot allocate gpiomux structure");
return -ENOMEM; return -ENOMEM;
}
platform_set_drvdata(pdev, mux);
if (!dev_get_platdata(&pdev->dev)) { if (!dev_get_platdata(&pdev->dev)) {
ret = i2c_mux_gpio_probe_dt(mux, pdev); ret = i2c_mux_gpio_probe_dt(mux, pdev);
...@@ -180,24 +174,23 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) ...@@ -180,24 +174,23 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
if (!parent) if (!parent)
return -EPROBE_DEFER; return -EPROBE_DEFER;
mux->parent = parent; muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
mux->gpio_base = gpio_base; i2c_mux_gpio_select, NULL);
if (!muxc) {
mux->adap = devm_kzalloc(&pdev->dev,
sizeof(*mux->adap) * mux->data.n_values,
GFP_KERNEL);
if (!mux->adap) {
dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure");
ret = -ENOMEM; ret = -ENOMEM;
goto alloc_failed; goto alloc_failed;
} }
muxc->priv = mux;
platform_set_drvdata(pdev, muxc);
mux->gpio_base = gpio_base;
if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
initial_state = mux->data.idle; initial_state = mux->data.idle;
deselect = i2c_mux_gpio_deselect; muxc->deselect = i2c_mux_gpio_deselect;
} else { } else {
initial_state = mux->data.values[0]; initial_state = mux->data.values[0];
deselect = NULL;
} }
for (i = 0; i < mux->data.n_gpios; i++) { for (i = 0; i < mux->data.n_gpios; i++) {
...@@ -223,11 +216,8 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) ...@@ -223,11 +216,8 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
unsigned int class = mux->data.classes ? mux->data.classes[i] : 0; unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr, ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
mux->data.values[i], class, if (ret) {
i2c_mux_gpio_select, deselect);
if (!mux->adap[i]) {
ret = -ENODEV;
dev_err(&pdev->dev, "Failed to add adapter %d\n", i); dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
goto add_adapter_failed; goto add_adapter_failed;
} }
...@@ -239,8 +229,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) ...@@ -239,8 +229,7 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
return 0; return 0;
add_adapter_failed: add_adapter_failed:
for (; i > 0; i--) i2c_mux_del_adapters(muxc);
i2c_del_mux_adapter(mux->adap[i - 1]);
i = mux->data.n_gpios; i = mux->data.n_gpios;
err_request_gpio: err_request_gpio:
for (; i > 0; i--) for (; i > 0; i--)
...@@ -253,16 +242,16 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev) ...@@ -253,16 +242,16 @@ static int i2c_mux_gpio_probe(struct platform_device *pdev)
static int i2c_mux_gpio_remove(struct platform_device *pdev) static int i2c_mux_gpio_remove(struct platform_device *pdev)
{ {
struct gpiomux *mux = platform_get_drvdata(pdev); struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
struct gpiomux *mux = i2c_mux_priv(muxc);
int i; int i;
for (i = 0; i < mux->data.n_values; i++) i2c_mux_del_adapters(muxc);
i2c_del_mux_adapter(mux->adap[i]);
for (i = 0; i < mux->data.n_gpios; i++) for (i = 0; i < mux->data.n_gpios; i++)
gpio_free(mux->gpio_base + mux->data.gpios[i]); gpio_free(mux->gpio_base + mux->data.gpios[i]);
i2c_put_adapter(mux->parent); i2c_put_adapter(muxc->parent);
return 0; return 0;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册