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