aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorJean Delvare <khali@linux-fr.org>2012-10-05 16:23:54 -0400
committerJean Delvare <khali@endymion.delvare>2012-10-05 16:23:54 -0400
commite7ee51405835cac72e7b6e0ff26dba608cf186cc (patch)
treecbc011e79a70e32475c3f7f4d91e5a6739eedfd0 /drivers/i2c
parent2614a8594152cad49047ed02255a3d9fbbbea8dd (diff)
i2c-mux-gpio: Add support for dynamically allocated GPIO pins
The code instantiating an i2c-mux-gpio platform device doesn't necessarily know in advance the GPIO pin numbers it wants to use. If pins are on a GPIO device which gets its base GPIO number assigned dynamically at run-time, the values can't be hard-coded. In that case, let the caller tell i2c-mux-gpio the name of the GPIO chip and the (relative) GPIO pin numbers to use. At probe time, the i2c-mux-gpio driver will look for the chip and apply the proper offset to turn relative GPIO pin numbers to absolute GPIO pin numbers. The same could be (and was so far) done on the caller's end, however doing it in i2c-mux-gpio has two benefits: * It avoids duplicating the code on every caller's side (about 30 lines of code.) * It allows for deferred probing for the muxed part of the I2C bus only. If finding the GPIO chip is the caller's responsibility, then deferred probing (if the GPIO chip isn't there yet) will not only affect the mux and the I2C bus segments behind it, but also the I2C bus trunk. Signed-off-by: Jean Delvare <khali@linux-fr.org> Cc: Peter Korsgaard <peter.korsgaard@barco.com>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/muxes/i2c-mux-gpio.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/drivers/i2c/muxes/i2c-mux-gpio.c b/drivers/i2c/muxes/i2c-mux-gpio.c
index 7c23bb51bd33..566a6757a33d 100644
--- a/drivers/i2c/muxes/i2c-mux-gpio.c
+++ b/drivers/i2c/muxes/i2c-mux-gpio.c
@@ -21,6 +21,7 @@ struct gpiomux {
21 struct i2c_adapter *parent; 21 struct i2c_adapter *parent;
22 struct i2c_adapter **adap; /* child busses */ 22 struct i2c_adapter **adap; /* child busses */
23 struct i2c_mux_gpio_platform_data data; 23 struct i2c_mux_gpio_platform_data data;
24 unsigned gpio_base;
24}; 25};
25 26
26static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) 27static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
@@ -28,7 +29,8 @@ static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
28 int i; 29 int i;
29 30
30 for (i = 0; i < mux->data.n_gpios; i++) 31 for (i = 0; i < mux->data.n_gpios; i++)
31 gpio_set_value(mux->data.gpios[i], val & (1 << i)); 32 gpio_set_value(mux->gpio_base + mux->data.gpios[i],
33 val & (1 << i));
32} 34}
33 35
34static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan) 36static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
@@ -49,13 +51,19 @@ static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
49 return 0; 51 return 0;
50} 52}
51 53
54static int __devinit match_gpio_chip_by_label(struct gpio_chip *chip,
55 void *data)
56{
57 return !strcmp(chip->label, data);
58}
59
52static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev) 60static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
53{ 61{
54 struct gpiomux *mux; 62 struct gpiomux *mux;
55 struct i2c_mux_gpio_platform_data *pdata; 63 struct i2c_mux_gpio_platform_data *pdata;
56 struct i2c_adapter *parent; 64 struct i2c_adapter *parent;
57 int (*deselect) (struct i2c_adapter *, void *, u32); 65 int (*deselect) (struct i2c_adapter *, void *, u32);
58 unsigned initial_state; 66 unsigned initial_state, gpio_base;
59 int i, ret; 67 int i, ret;
60 68
61 pdata = pdev->dev.platform_data; 69 pdata = pdev->dev.platform_data;
@@ -64,6 +72,23 @@ static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
64 return -ENODEV; 72 return -ENODEV;
65 } 73 }
66 74
75 /*
76 * If a GPIO chip name is provided, the GPIO pin numbers provided are
77 * relative to its base GPIO number. Otherwise they are absolute.
78 */
79 if (pdata->gpio_chip) {
80 struct gpio_chip *gpio;
81
82 gpio = gpiochip_find(pdata->gpio_chip,
83 match_gpio_chip_by_label);
84 if (!gpio)
85 return -EPROBE_DEFER;
86
87 gpio_base = gpio->base;
88 } else {
89 gpio_base = 0;
90 }
91
67 parent = i2c_get_adapter(pdata->parent); 92 parent = i2c_get_adapter(pdata->parent);
68 if (!parent) { 93 if (!parent) {
69 dev_err(&pdev->dev, "Parent adapter (%d) not found\n", 94 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
@@ -79,6 +104,7 @@ static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
79 104
80 mux->parent = parent; 105 mux->parent = parent;
81 mux->data = *pdata; 106 mux->data = *pdata;
107 mux->gpio_base = gpio_base;
82 mux->adap = devm_kzalloc(&pdev->dev, 108 mux->adap = devm_kzalloc(&pdev->dev,
83 sizeof(*mux->adap) * pdata->n_values, 109 sizeof(*mux->adap) * pdata->n_values,
84 GFP_KERNEL); 110 GFP_KERNEL);
@@ -96,10 +122,10 @@ static int __devinit i2c_mux_gpio_probe(struct platform_device *pdev)
96 } 122 }
97 123
98 for (i = 0; i < pdata->n_gpios; i++) { 124 for (i = 0; i < pdata->n_gpios; i++) {
99 ret = gpio_request(pdata->gpios[i], "i2c-mux-gpio"); 125 ret = gpio_request(gpio_base + pdata->gpios[i], "i2c-mux-gpio");
100 if (ret) 126 if (ret)
101 goto err_request_gpio; 127 goto err_request_gpio;
102 gpio_direction_output(pdata->gpios[i], 128 gpio_direction_output(gpio_base + pdata->gpios[i],
103 initial_state & (1 << i)); 129 initial_state & (1 << i));
104 } 130 }
105 131
@@ -130,7 +156,7 @@ add_adapter_failed:
130 i = pdata->n_gpios; 156 i = pdata->n_gpios;
131err_request_gpio: 157err_request_gpio:
132 for (; i > 0; i--) 158 for (; i > 0; i--)
133 gpio_free(pdata->gpios[i - 1]); 159 gpio_free(gpio_base + pdata->gpios[i - 1]);
134alloc_failed: 160alloc_failed:
135 i2c_put_adapter(parent); 161 i2c_put_adapter(parent);
136 162
@@ -146,7 +172,7 @@ static int __devexit i2c_mux_gpio_remove(struct platform_device *pdev)
146 i2c_del_mux_adapter(mux->adap[i]); 172 i2c_del_mux_adapter(mux->adap[i]);
147 173
148 for (i = 0; i < mux->data.n_gpios; i++) 174 for (i = 0; i < mux->data.n_gpios; i++)
149 gpio_free(mux->data.gpios[i]); 175 gpio_free(mux->gpio_base + mux->data.gpios[i]);
150 176
151 platform_set_drvdata(pdev, NULL); 177 platform_set_drvdata(pdev, NULL);
152 i2c_put_adapter(mux->parent); 178 i2c_put_adapter(mux->parent);