Lines Matching +full:i2c +full:- +full:mux

1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C multiplexer using GPIO API
12 #include <linux/i2c.h>
13 #include <linux/i2c-mux.h>
16 #include <linux/platform_data/i2c-mux-gpio.h>
26 static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned int val) in i2c_mux_gpio_set() argument
32 gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values); in i2c_mux_gpio_set()
37 struct gpiomux *mux = i2c_mux_priv(muxc); in i2c_mux_gpio_select() local
39 i2c_mux_gpio_set(mux, chan); in i2c_mux_gpio_select()
41 if (mux->data.settle_time) in i2c_mux_gpio_select()
42 fsleep(mux->data.settle_time); in i2c_mux_gpio_select()
49 struct gpiomux *mux = i2c_mux_priv(muxc); in i2c_mux_gpio_deselect() local
51 i2c_mux_gpio_set(mux, mux->data.idle); in i2c_mux_gpio_deselect()
56 static int i2c_mux_gpio_probe_fw(struct gpiomux *mux, in i2c_mux_gpio_probe_fw() argument
59 struct device *dev = &pdev->dev; in i2c_mux_gpio_probe_fw()
61 struct device_node *np = dev->of_node; in i2c_mux_gpio_probe_fw()
70 return -ENODEV; in i2c_mux_gpio_probe_fw()
72 adapter_np = of_parse_phandle(np, "i2c-parent", 0); in i2c_mux_gpio_probe_fw()
74 dev_err(&pdev->dev, "Cannot parse i2c-parent\n"); in i2c_mux_gpio_probe_fw()
75 return -ENODEV; in i2c_mux_gpio_probe_fw()
82 * In ACPI land the mux should be a direct child of the i2c in i2c_mux_gpio_probe_fw()
85 acpi_handle dev_handle = ACPI_HANDLE(dev->parent); in i2c_mux_gpio_probe_fw()
91 return -EPROBE_DEFER; in i2c_mux_gpio_probe_fw()
93 mux->data.parent = i2c_adapter_id(adapter); in i2c_mux_gpio_probe_fw()
94 put_device(&adapter->dev); in i2c_mux_gpio_probe_fw()
96 mux->data.n_values = device_get_child_node_count(dev); in i2c_mux_gpio_probe_fw()
98 mux->data.n_values, sizeof(*mux->data.values), in i2c_mux_gpio_probe_fw()
102 return -ENOMEM; in i2c_mux_gpio_probe_fw()
118 mux->data.values = values; in i2c_mux_gpio_probe_fw()
120 if (device_property_read_u32(dev, "idle-state", &mux->data.idle)) in i2c_mux_gpio_probe_fw()
121 mux->data.idle = I2C_MUX_GPIO_NO_IDLE; in i2c_mux_gpio_probe_fw()
123 device_property_read_u32(dev, "settle-time-us", &mux->data.settle_time); in i2c_mux_gpio_probe_fw()
131 struct gpiomux *mux; in i2c_mux_gpio_probe() local
137 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); in i2c_mux_gpio_probe()
138 if (!mux) in i2c_mux_gpio_probe()
139 return -ENOMEM; in i2c_mux_gpio_probe()
141 if (!dev_get_platdata(&pdev->dev)) { in i2c_mux_gpio_probe()
142 ret = i2c_mux_gpio_probe_fw(mux, pdev); in i2c_mux_gpio_probe()
146 memcpy(&mux->data, dev_get_platdata(&pdev->dev), in i2c_mux_gpio_probe()
147 sizeof(mux->data)); in i2c_mux_gpio_probe()
150 ngpios = gpiod_count(&pdev->dev, "mux"); in i2c_mux_gpio_probe()
152 dev_err(&pdev->dev, "no valid gpios provided\n"); in i2c_mux_gpio_probe()
153 return ngpios ?: -EINVAL; in i2c_mux_gpio_probe()
155 mux->ngpios = ngpios; in i2c_mux_gpio_probe()
157 parent = i2c_get_adapter(mux->data.parent); in i2c_mux_gpio_probe()
159 return -EPROBE_DEFER; in i2c_mux_gpio_probe()
161 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, in i2c_mux_gpio_probe()
162 array_size(ngpios, sizeof(*mux->gpios)), 0, in i2c_mux_gpio_probe()
165 ret = -ENOMEM; in i2c_mux_gpio_probe()
168 mux->gpios = muxc->priv; in i2c_mux_gpio_probe()
169 muxc->priv = mux; in i2c_mux_gpio_probe()
173 root = i2c_root_adapter(&parent->dev); in i2c_mux_gpio_probe()
175 muxc->mux_locked = true; in i2c_mux_gpio_probe()
177 if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { in i2c_mux_gpio_probe()
178 initial_state = mux->data.idle; in i2c_mux_gpio_probe()
179 muxc->deselect = i2c_mux_gpio_deselect; in i2c_mux_gpio_probe()
181 initial_state = mux->data.values[0]; in i2c_mux_gpio_probe()
194 gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag); in i2c_mux_gpio_probe()
200 mux->gpios[i] = gpiod; in i2c_mux_gpio_probe()
202 if (!muxc->mux_locked) in i2c_mux_gpio_probe()
207 muxc->mux_locked = i2c_root_adapter(dev) == root; in i2c_mux_gpio_probe()
210 if (muxc->mux_locked) in i2c_mux_gpio_probe()
211 dev_info(&pdev->dev, "mux-locked i2c mux\n"); in i2c_mux_gpio_probe()
213 for (i = 0; i < mux->data.n_values; i++) { in i2c_mux_gpio_probe()
214 u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; in i2c_mux_gpio_probe()
216 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i]); in i2c_mux_gpio_probe()
221 dev_info(&pdev->dev, "%d port mux on %s adapter\n", in i2c_mux_gpio_probe()
222 mux->data.n_values, parent->name); in i2c_mux_gpio_probe()
239 i2c_put_adapter(muxc->parent); in i2c_mux_gpio_remove()
243 { .compatible = "i2c-mux-gpio", },
252 .name = "i2c-mux-gpio",
259 MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
262 MODULE_ALIAS("platform:i2c-mux-gpio");