xref: /freebsd/sys/dev/iicbus/mux/iicmux.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef __IICMUX_H
29 #define __IICMUX_H
30 
31 #define	IICMUX_MAX_BUSES	16	/* More than any available mux chip. */
32 
33 /*
34  * IICMUX_SELECT_IDLE instructs the mux hardware driver to do whatever it is
35  * configured to do when the downstream buses are idle.  Hardware has varying
36  * capabilities; it may disconnect all downstream buses, or connect a specific
37  * bus, or just leave whatever bus was last used connected.  Hardware which is
38  * capable of various choices will have some mechanism to configure the choice
39  * which is handled outside of the iicmux framework.
40  */
41 #define	IICMUX_SELECT_IDLE	(-1)
42 
43 /*
44  * The iicmux softc; chip drivers should embed one of these as the first member
45  * variable of their own softc struct, and must call iicmux_attach() to
46  * initialize it before calling any other iicmux functions.
47  */
48 struct iicmux_softc {
49 	device_t	busdev;   /* Upstream i2c bus (may not be our parent). */
50 	device_t	dev;      /* Ourself. */
51 	int		maxbus;   /* Index of highest populated busdevs slot. */
52 	int		numbuses; /* Number of buses supported by the chip. */
53 	int		debugmux; /* Write debug messages when > 0. */
54 	device_t	childdevs[IICMUX_MAX_BUSES]; /* Child bus instances. */
55 #ifdef FDT
56 	phandle_t	childnodes[IICMUX_MAX_BUSES]; /* Child bus fdt nodes. */
57 #endif
58 };
59 
60 DECLARE_CLASS(iicmux_driver);
61 
62 /*
63  * Helpers to call from attach/detach functions of chip-specific drivers.
64  *
65  * The iicmux_attach() function initializes the core driver's portion of the
66  * softc, and creates child iicbus instances for any children it can identify
67  * using hints and FDT data.  If a chip driver does its own device_add_child()
68  * calls to add other downstream buses that participate in the mux switching, it
69  * must call iicmux_add_child() to inform the core driver of the downstream
70  * busidx<->device_t relationship.
71  */
72 int  iicmux_add_child(device_t dev, device_t child, int busidx);
73 int  iicmux_attach(device_t dev, device_t busdev, int numbuses);
74 int  iicmux_detach(device_t dev);
75 
76 #endif
77