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