1 /*-
2 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 */
26
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/malloc.h>
32
33 #include <dev/iicbus/iicbus.h>
34 #include <dev/iicbus/iiconf.h>
35
36 #include <linux/device.h>
37 #include <linux/i2c.h>
38 #include <linux/i2c-algo-bit.h>
39 #include <linux/list.h>
40 #include <linux/pci.h>
41
42 #include "iicbus_if.h"
43 #include "iicbb_if.h"
44 #include "lkpi_iic_if.h"
45
46 static int lkpi_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
47 static int lkpi_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr);
48
49 struct lkpi_iic_softc {
50 device_t iicbus;
51 struct i2c_adapter *adapter;
52 };
53
54 static struct sx lkpi_sx_i2c;
55
56 static void
lkpi_sysinit_i2c(void * arg __unused)57 lkpi_sysinit_i2c(void *arg __unused)
58 {
59
60 sx_init(&lkpi_sx_i2c, "lkpi-i2c");
61 }
62
63 static void
lkpi_sysuninit_i2c(void * arg __unused)64 lkpi_sysuninit_i2c(void *arg __unused)
65 {
66
67 sx_destroy(&lkpi_sx_i2c);
68 }
69
70 SYSINIT(lkpi_i2c, SI_SUB_DRIVERS, SI_ORDER_ANY,
71 lkpi_sysinit_i2c, NULL);
72 SYSUNINIT(lkpi_i2c, SI_SUB_DRIVERS, SI_ORDER_ANY,
73 lkpi_sysuninit_i2c, NULL);
74
75 static int
lkpi_iic_probe(device_t dev)76 lkpi_iic_probe(device_t dev)
77 {
78
79 device_set_desc(dev, "LinuxKPI I2C");
80 return (BUS_PROBE_NOWILDCARD);
81 }
82
83 static int
lkpi_iic_attach(device_t dev)84 lkpi_iic_attach(device_t dev)
85 {
86 struct lkpi_iic_softc *sc;
87
88 sc = device_get_softc(dev);
89 sc->iicbus = device_add_child(dev, "iicbus", -1);
90 if (sc->iicbus == NULL) {
91 device_printf(dev, "Couldn't add iicbus child, aborting\n");
92 return (ENXIO);
93 }
94 bus_generic_attach(dev);
95 return (0);
96 }
97
98 static int
lkpi_iic_detach(device_t dev)99 lkpi_iic_detach(device_t dev)
100 {
101 struct lkpi_iic_softc *sc;
102
103 sc = device_get_softc(dev);
104 if (sc->iicbus)
105 device_delete_child(dev, sc->iicbus);
106 return (0);
107 }
108
109 static int
lkpi_iic_add_adapter(device_t dev,struct i2c_adapter * adapter)110 lkpi_iic_add_adapter(device_t dev, struct i2c_adapter *adapter)
111 {
112 struct lkpi_iic_softc *sc;
113
114 sc = device_get_softc(dev);
115 sc->adapter = adapter;
116
117 return (0);
118 }
119
120 static struct i2c_adapter *
lkpi_iic_get_adapter(device_t dev)121 lkpi_iic_get_adapter(device_t dev)
122 {
123 struct lkpi_iic_softc *sc;
124
125 sc = device_get_softc(dev);
126 return (sc->adapter);
127 }
128
129 static device_method_t lkpi_iic_methods[] = {
130 /* device interface */
131 DEVMETHOD(device_probe, lkpi_iic_probe),
132 DEVMETHOD(device_attach, lkpi_iic_attach),
133 DEVMETHOD(device_detach, lkpi_iic_detach),
134 DEVMETHOD(device_suspend, bus_generic_suspend),
135 DEVMETHOD(device_resume, bus_generic_resume),
136
137 /* iicbus interface */
138 DEVMETHOD(iicbus_transfer, lkpi_i2c_transfer),
139 DEVMETHOD(iicbus_reset, lkpi_i2c_reset),
140 DEVMETHOD(iicbus_callback, iicbus_null_callback),
141
142 /* lkpi_iic interface */
143 DEVMETHOD(lkpi_iic_add_adapter, lkpi_iic_add_adapter),
144 DEVMETHOD(lkpi_iic_get_adapter, lkpi_iic_get_adapter),
145
146 DEVMETHOD_END
147 };
148
149 driver_t lkpi_iic_driver = {
150 "lkpi_iic",
151 lkpi_iic_methods,
152 sizeof(struct lkpi_iic_softc),
153 };
154
155 DRIVER_MODULE(lkpi_iic, drmn, lkpi_iic_driver, 0, 0);
156 DRIVER_MODULE(lkpi_iic, drm, lkpi_iic_driver, 0, 0);
157 DRIVER_MODULE(iicbus, lkpi_iic, iicbus_driver, 0, 0);
158 MODULE_DEPEND(linuxkpi, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
159
160 static int
lkpi_i2c_reset(device_t dev,u_char speed,u_char addr,u_char * oldaddr)161 lkpi_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
162 {
163
164 /* That doesn't seems to be supported in linux */
165 return (0);
166 }
167
i2c_check_for_quirks(struct i2c_adapter * adapter,struct iic_msg * msgs,uint32_t nmsgs)168 static int i2c_check_for_quirks(struct i2c_adapter *adapter,
169 struct iic_msg *msgs, uint32_t nmsgs)
170 {
171 const struct i2c_adapter_quirks *quirks;
172 device_t dev;
173 int i, max_nmsgs;
174 bool check_len;
175
176 dev = adapter->dev.parent->bsddev;
177 quirks = adapter->quirks;
178 if (quirks == NULL)
179 return (0);
180
181 check_len = true;
182 max_nmsgs = quirks->max_num_msgs;
183
184 if (quirks->flags & I2C_AQ_COMB) {
185 max_nmsgs = 2;
186
187 if (nmsgs == 2) {
188 if (quirks->flags & I2C_AQ_COMB_WRITE_FIRST &&
189 msgs[0].flags & IIC_M_RD) {
190 device_printf(dev,
191 "Error: "
192 "first combined message must be write\n");
193 return (EOPNOTSUPP);
194 }
195 if (quirks->flags & I2C_AQ_COMB_READ_SECOND &&
196 !(msgs[1].flags & IIC_M_RD)) {
197 device_printf(dev,
198 "Error: "
199 "second combined message must be read\n");
200 return (EOPNOTSUPP);
201 }
202
203 if (quirks->flags & I2C_AQ_COMB_SAME_ADDR &&
204 msgs[0].slave != msgs[1].slave) {
205 device_printf(dev,
206 "Error: "
207 "combined message must be use the same "
208 "address\n");
209 return (EOPNOTSUPP);
210 }
211
212 if (quirks->max_comb_1st_msg_len &&
213 msgs[0].len > quirks->max_comb_1st_msg_len) {
214 device_printf(dev,
215 "Error: "
216 "message too long: %hu > %hu max\n",
217 msgs[0].len,
218 quirks->max_comb_1st_msg_len);
219 return (EOPNOTSUPP);
220 }
221 if (quirks->max_comb_2nd_msg_len &&
222 msgs[1].len > quirks->max_comb_2nd_msg_len) {
223 device_printf(dev,
224 "Error: "
225 "message too long: %hu > %hu max\n",
226 msgs[1].len,
227 quirks->max_comb_2nd_msg_len);
228 return (EOPNOTSUPP);
229 }
230
231 check_len = false;
232 }
233 }
234
235 if (max_nmsgs && nmsgs > max_nmsgs) {
236 device_printf(dev,
237 "Error: too many messages: %d > %d max\n",
238 nmsgs, max_nmsgs);
239 return (EOPNOTSUPP);
240 }
241
242 for (i = 0; i < nmsgs; i++) {
243 if (msgs[i].flags & IIC_M_RD) {
244 if (check_len && quirks->max_read_len &&
245 msgs[i].len > quirks->max_read_len) {
246 device_printf(dev,
247 "Error: "
248 "message %d too long: %hu > %hu max\n",
249 i, msgs[i].len, quirks->max_read_len);
250 return (EOPNOTSUPP);
251 }
252 if (quirks->flags & I2C_AQ_NO_ZERO_LEN_READ &&
253 msgs[i].len == 0) {
254 device_printf(dev,
255 "Error: message %d of length 0\n", i);
256 return (EOPNOTSUPP);
257 }
258 } else {
259 if (check_len && quirks->max_write_len &&
260 msgs[i].len > quirks->max_write_len) {
261 device_printf(dev,
262 "Message %d too long: %hu > %hu max\n",
263 i, msgs[i].len, quirks->max_write_len);
264 return (EOPNOTSUPP);
265 }
266 if (quirks->flags & I2C_AQ_NO_ZERO_LEN_WRITE &&
267 msgs[i].len == 0) {
268 device_printf(dev,
269 "Error: message %d of length 0\n", i);
270 return (EOPNOTSUPP);
271 }
272 }
273 }
274
275 return (0);
276 }
277
278 static int
lkpi_i2c_transfer(device_t dev,struct iic_msg * msgs,uint32_t nmsgs)279 lkpi_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
280 {
281 struct lkpi_iic_softc *sc;
282 struct i2c_msg *linux_msgs;
283 int i, ret = 0;
284
285 sc = device_get_softc(dev);
286 if (sc->adapter == NULL)
287 return (ENXIO);
288 ret = i2c_check_for_quirks(sc->adapter, msgs, nmsgs);
289 if (ret != 0)
290 return (ret);
291 linux_set_current(curthread);
292
293 linux_msgs = malloc(sizeof(struct i2c_msg) * nmsgs,
294 M_DEVBUF, M_WAITOK | M_ZERO);
295
296 for (i = 0; i < nmsgs; i++) {
297 linux_msgs[i].addr = msgs[i].slave >> 1;
298 linux_msgs[i].len = msgs[i].len;
299 linux_msgs[i].buf = msgs[i].buf;
300 if (msgs[i].flags & IIC_M_RD) {
301 linux_msgs[i].flags |= I2C_M_RD;
302 for (int j = 0; j < msgs[i].len; j++)
303 msgs[i].buf[j] = 0;
304 }
305 if (msgs[i].flags & IIC_M_NOSTART)
306 linux_msgs[i].flags |= I2C_M_NOSTART;
307 }
308 ret = i2c_transfer(sc->adapter, linux_msgs, nmsgs);
309 free(linux_msgs, M_DEVBUF);
310
311 if (ret < 0)
312 return (-ret);
313 return (0);
314 }
315
316 int
lkpi_i2c_add_adapter(struct i2c_adapter * adapter)317 lkpi_i2c_add_adapter(struct i2c_adapter *adapter)
318 {
319 device_t lkpi_iic;
320 int error;
321
322 if (adapter->name[0] == '\0')
323 return (-EINVAL);
324 if (bootverbose)
325 device_printf(adapter->dev.parent->bsddev,
326 "Adding i2c adapter %s\n", adapter->name);
327 sx_xlock(&lkpi_sx_i2c);
328 lkpi_iic = device_add_child(adapter->dev.parent->bsddev, "lkpi_iic", -1);
329 if (lkpi_iic == NULL) {
330 device_printf(adapter->dev.parent->bsddev, "Couldn't add lkpi_iic\n");
331 sx_xunlock(&lkpi_sx_i2c);
332 return (ENXIO);
333 }
334
335 bus_topo_lock();
336 error = bus_generic_attach(adapter->dev.parent->bsddev);
337 bus_topo_unlock();
338 if (error) {
339 device_printf(adapter->dev.parent->bsddev,
340 "failed to attach child: error %d\n", error);
341 sx_xunlock(&lkpi_sx_i2c);
342 return (ENXIO);
343 }
344 LKPI_IIC_ADD_ADAPTER(lkpi_iic, adapter);
345 sx_xunlock(&lkpi_sx_i2c);
346 return (0);
347 }
348
349 int
lkpi_i2c_del_adapter(struct i2c_adapter * adapter)350 lkpi_i2c_del_adapter(struct i2c_adapter *adapter)
351 {
352 device_t child;
353 int unit, rv;
354
355 if (adapter == NULL)
356 return (-EINVAL);
357 if (bootverbose)
358 device_printf(adapter->dev.parent->bsddev,
359 "Removing i2c adapter %s\n", adapter->name);
360 sx_xlock(&lkpi_sx_i2c);
361 unit = 0;
362 while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iic", unit++)) != NULL) {
363
364 if (adapter == LKPI_IIC_GET_ADAPTER(child)) {
365 bus_topo_lock();
366 device_delete_child(adapter->dev.parent->bsddev, child);
367 bus_topo_unlock();
368 rv = 0;
369 goto out;
370 }
371 }
372
373 unit = 0;
374 while ((child = device_find_child(adapter->dev.parent->bsddev, "lkpi_iicbb", unit++)) != NULL) {
375
376 if (adapter == LKPI_IIC_GET_ADAPTER(child)) {
377 bus_topo_lock();
378 device_delete_child(adapter->dev.parent->bsddev, child);
379 bus_topo_unlock();
380 rv = 0;
381 goto out;
382 }
383 }
384 rv = -EINVAL;
385 out:
386 sx_xunlock(&lkpi_sx_i2c);
387 return (rv);
388 }
389