Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
92ed1a76 PK |
2 | /* |
3 | * I2C multiplexer using GPIO API | |
4 | * | |
5 | * Peter Korsgaard <peter.korsgaard@barco.com> | |
92ed1a76 PK |
6 | */ |
7 | ||
8 | #include <linux/i2c.h> | |
9 | #include <linux/i2c-mux.h> | |
a2fd6f6b | 10 | #include <linux/overflow.h> |
62ea22c4 | 11 | #include <linux/platform_data/i2c-mux-gpio.h> |
92ed1a76 | 12 | #include <linux/platform_device.h> |
92ed1a76 PK |
13 | #include <linux/module.h> |
14 | #include <linux/slab.h> | |
d308dfbf LW |
15 | #include <linux/bits.h> |
16 | #include <linux/gpio/consumer.h> | |
17 | /* FIXME: stop poking around inside gpiolib */ | |
6ef91fcc | 18 | #include "../../gpio/gpiolib.h" |
92ed1a76 PK |
19 | |
20 | struct gpiomux { | |
e7065e20 | 21 | struct i2c_mux_gpio_platform_data data; |
d308dfbf | 22 | int ngpios; |
feff5c0a | 23 | struct gpio_desc **gpios; |
92ed1a76 PK |
24 | }; |
25 | ||
e7065e20 | 26 | static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val) |
92ed1a76 | 27 | { |
b9762beb | 28 | DECLARE_BITMAP(values, BITS_PER_TYPE(val)); |
92ed1a76 | 29 | |
b9762beb | 30 | values[0] = val; |
feff5c0a | 31 | |
d308dfbf | 32 | gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values); |
92ed1a76 PK |
33 | } |
34 | ||
bb448147 | 35 | static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan) |
92ed1a76 | 36 | { |
bb448147 | 37 | struct gpiomux *mux = i2c_mux_priv(muxc); |
92ed1a76 | 38 | |
8c0ec250 | 39 | i2c_mux_gpio_set(mux, chan); |
92ed1a76 PK |
40 | |
41 | return 0; | |
42 | } | |
43 | ||
bb448147 | 44 | static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan) |
92ed1a76 | 45 | { |
bb448147 | 46 | struct gpiomux *mux = i2c_mux_priv(muxc); |
92ed1a76 | 47 | |
e7065e20 | 48 | i2c_mux_gpio_set(mux, mux->data.idle); |
92ed1a76 PK |
49 | |
50 | return 0; | |
51 | } | |
52 | ||
98b2b712 EG |
53 | static int i2c_mux_gpio_probe_fw(struct gpiomux *mux, |
54 | struct platform_device *pdev) | |
55 | { | |
56 | struct device *dev = &pdev->dev; | |
533f05f0 | 57 | struct fwnode_handle *fwnode = dev_fwnode(dev); |
98b2b712 EG |
58 | struct device_node *np = dev->of_node; |
59 | struct device_node *adapter_np; | |
60 | struct i2c_adapter *adapter = NULL; | |
61 | struct fwnode_handle *child; | |
62 | unsigned *values; | |
63 | int rc, i = 0; | |
64 | ||
533f05f0 | 65 | if (is_of_node(fwnode)) { |
98b2b712 EG |
66 | if (!np) |
67 | return -ENODEV; | |
68 | ||
69 | adapter_np = of_parse_phandle(np, "i2c-parent", 0); | |
70 | if (!adapter_np) { | |
71 | dev_err(&pdev->dev, "Cannot parse i2c-parent\n"); | |
72 | return -ENODEV; | |
73 | } | |
74 | adapter = of_find_i2c_adapter_by_node(adapter_np); | |
75 | of_node_put(adapter_np); | |
76 | ||
533f05f0 | 77 | } else if (is_acpi_node(fwnode)) { |
98b2b712 EG |
78 | /* |
79 | * In ACPI land the mux should be a direct child of the i2c | |
80 | * bus it muxes. | |
81 | */ | |
82 | acpi_handle dev_handle = ACPI_HANDLE(dev->parent); | |
83 | ||
84 | adapter = i2c_acpi_find_adapter_by_handle(dev_handle); | |
9b7a0c40 | 85 | } |
98b2b712 | 86 | |
623e4ecb | 87 | if (!adapter) |
9937b8d8 | 88 | return -EPROBE_DEFER; |
623e4ecb | 89 | |
9b7a0c40 MR |
90 | mux->data.parent = i2c_adapter_id(adapter); |
91 | put_device(&adapter->dev); | |
92 | ||
98b2b712 | 93 | mux->data.n_values = device_get_child_node_count(dev); |
19eb29b0 | 94 | values = devm_kcalloc(dev, |
a86854d0 | 95 | mux->data.n_values, sizeof(*mux->data.values), |
9b7a0c40 MR |
96 | GFP_KERNEL); |
97 | if (!values) { | |
19eb29b0 | 98 | dev_err(dev, "Cannot allocate values array"); |
9b7a0c40 MR |
99 | return -ENOMEM; |
100 | } | |
101 | ||
98b2b712 EG |
102 | device_for_each_child_node(dev, child) { |
103 | if (is_of_node(child)) { | |
104 | fwnode_property_read_u32(child, "reg", values + i); | |
105 | ||
106 | } else if (is_acpi_node(child)) { | |
379920f5 | 107 | rc = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), values + i); |
98b2b712 | 108 | if (rc) |
379920f5 | 109 | return dev_err_probe(dev, rc, "Cannot get address\n"); |
98b2b712 EG |
110 | } |
111 | ||
9b7a0c40 MR |
112 | i++; |
113 | } | |
114 | mux->data.values = values; | |
115 | ||
533f05f0 | 116 | if (device_property_read_u32(dev, "idle-state", &mux->data.idle)) |
9b7a0c40 MR |
117 | mux->data.idle = I2C_MUX_GPIO_NO_IDLE; |
118 | ||
9b7a0c40 MR |
119 | return 0; |
120 | } | |
9b7a0c40 | 121 | |
0b255e92 | 122 | static int i2c_mux_gpio_probe(struct platform_device *pdev) |
92ed1a76 | 123 | { |
bb448147 | 124 | struct i2c_mux_core *muxc; |
92ed1a76 | 125 | struct gpiomux *mux; |
92ed1a76 | 126 | struct i2c_adapter *parent; |
6ef91fcc | 127 | struct i2c_adapter *root; |
d308dfbf LW |
128 | unsigned initial_state; |
129 | int i, ngpios, ret; | |
92ed1a76 | 130 | |
9b7a0c40 | 131 | mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL); |
bb448147 | 132 | if (!mux) |
9b7a0c40 | 133 | return -ENOMEM; |
9b7a0c40 | 134 | |
6d4028c6 | 135 | if (!dev_get_platdata(&pdev->dev)) { |
98b2b712 | 136 | ret = i2c_mux_gpio_probe_fw(mux, pdev); |
9b7a0c40 MR |
137 | if (ret < 0) |
138 | return ret; | |
6d4028c6 JH |
139 | } else { |
140 | memcpy(&mux->data, dev_get_platdata(&pdev->dev), | |
141 | sizeof(mux->data)); | |
142 | } | |
9b7a0c40 | 143 | |
d308dfbf LW |
144 | ngpios = gpiod_count(&pdev->dev, "mux"); |
145 | if (ngpios <= 0) { | |
146 | dev_err(&pdev->dev, "no valid gpios provided\n"); | |
147 | return ngpios ?: -EINVAL; | |
e7ee5140 | 148 | } |
d308dfbf | 149 | mux->ngpios = ngpios; |
e7ee5140 | 150 | |
9b7a0c40 | 151 | parent = i2c_get_adapter(mux->data.parent); |
623e4ecb | 152 | if (!parent) |
9937b8d8 | 153 | return -EPROBE_DEFER; |
92ed1a76 | 154 | |
feff5c0a | 155 | muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, |
a2fd6f6b | 156 | array_size(ngpios, sizeof(*mux->gpios)), 0, |
bb448147 PR |
157 | i2c_mux_gpio_select, NULL); |
158 | if (!muxc) { | |
92ed1a76 | 159 | ret = -ENOMEM; |
2614a859 | 160 | goto alloc_failed; |
92ed1a76 | 161 | } |
feff5c0a | 162 | mux->gpios = muxc->priv; |
bb448147 PR |
163 | muxc->priv = mux; |
164 | ||
165 | platform_set_drvdata(pdev, muxc); | |
166 | ||
6ef91fcc PR |
167 | root = i2c_root_adapter(&parent->dev); |
168 | ||
169 | muxc->mux_locked = true; | |
92ed1a76 | 170 | |
9b7a0c40 MR |
171 | if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) { |
172 | initial_state = mux->data.idle; | |
bb448147 | 173 | muxc->deselect = i2c_mux_gpio_deselect; |
92ed1a76 | 174 | } else { |
9b7a0c40 | 175 | initial_state = mux->data.values[0]; |
92ed1a76 PK |
176 | } |
177 | ||
d308dfbf | 178 | for (i = 0; i < ngpios; i++) { |
6ef91fcc | 179 | struct device *gpio_dev; |
d308dfbf LW |
180 | struct gpio_desc *gpiod; |
181 | enum gpiod_flags flag; | |
182 | ||
183 | if (initial_state & BIT(i)) | |
184 | flag = GPIOD_OUT_HIGH; | |
185 | else | |
186 | flag = GPIOD_OUT_LOW; | |
187 | gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag); | |
188 | if (IS_ERR(gpiod)) { | |
189 | ret = PTR_ERR(gpiod); | |
190 | goto alloc_failed; | |
7fafae66 JD |
191 | } |
192 | ||
d308dfbf | 193 | mux->gpios[i] = gpiod; |
feff5c0a | 194 | |
6ef91fcc PR |
195 | if (!muxc->mux_locked) |
196 | continue; | |
197 | ||
d308dfbf LW |
198 | /* FIXME: find a proper way to access the GPIO device */ |
199 | gpio_dev = &gpiod->gdev->dev; | |
6ef91fcc | 200 | muxc->mux_locked = i2c_root_adapter(gpio_dev) == root; |
92ed1a76 PK |
201 | } |
202 | ||
6ef91fcc PR |
203 | if (muxc->mux_locked) |
204 | dev_info(&pdev->dev, "mux-locked i2c mux\n"); | |
205 | ||
9b7a0c40 MR |
206 | for (i = 0; i < mux->data.n_values; i++) { |
207 | u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0; | |
208 | unsigned int class = mux->data.classes ? mux->data.classes[i] : 0; | |
92ed1a76 | 209 | |
bb448147 | 210 | ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class); |
1b00900f | 211 | if (ret) |
92ed1a76 | 212 | goto add_adapter_failed; |
92ed1a76 PK |
213 | } |
214 | ||
215 | dev_info(&pdev->dev, "%d port mux on %s adapter\n", | |
9b7a0c40 | 216 | mux->data.n_values, parent->name); |
92ed1a76 PK |
217 | |
218 | return 0; | |
219 | ||
220 | add_adapter_failed: | |
bb448147 | 221 | i2c_mux_del_adapters(muxc); |
92ed1a76 PK |
222 | alloc_failed: |
223 | i2c_put_adapter(parent); | |
224 | ||
225 | return ret; | |
226 | } | |
227 | ||
0b255e92 | 228 | static int i2c_mux_gpio_remove(struct platform_device *pdev) |
92ed1a76 | 229 | { |
bb448147 | 230 | struct i2c_mux_core *muxc = platform_get_drvdata(pdev); |
92ed1a76 | 231 | |
bb448147 | 232 | i2c_mux_del_adapters(muxc); |
bb448147 | 233 | i2c_put_adapter(muxc->parent); |
92ed1a76 PK |
234 | |
235 | return 0; | |
236 | } | |
237 | ||
0b255e92 | 238 | static const struct of_device_id i2c_mux_gpio_of_match[] = { |
9b7a0c40 MR |
239 | { .compatible = "i2c-mux-gpio", }, |
240 | {}, | |
241 | }; | |
242 | MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match); | |
243 | ||
e7065e20 JD |
244 | static struct platform_driver i2c_mux_gpio_driver = { |
245 | .probe = i2c_mux_gpio_probe, | |
0b255e92 | 246 | .remove = i2c_mux_gpio_remove, |
92ed1a76 | 247 | .driver = { |
e7065e20 | 248 | .name = "i2c-mux-gpio", |
4e905323 | 249 | .of_match_table = i2c_mux_gpio_of_match, |
92ed1a76 PK |
250 | }, |
251 | }; | |
252 | ||
e7065e20 | 253 | module_platform_driver(i2c_mux_gpio_driver); |
92ed1a76 PK |
254 | |
255 | MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver"); | |
256 | MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>"); | |
257 | MODULE_LICENSE("GPL"); | |
e7065e20 | 258 | MODULE_ALIAS("platform:i2c-mux-gpio"); |