aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/iicbus/mux/pca9547.c
blob: 2bdc6cb2e8670f6c54e736c0f73f22f0fad252d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>

#include <dev/iicbus/iicbus.h>
#include <dev/iicbus/iiconf.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>

#include "iicbus_if.h"
#include "iicmux_if.h"


static struct ofw_compat_data compat_data[] = {
	{"nxp,pca9547",		1},
	{NULL,			0}
};

#include <dev/iicbus/mux/iicmux.h>

struct pca9547_softc {
	struct iicmux_softc mux;
	device_t	dev;
	bool idle_disconnect;
};

static int
pca9547_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
{
	struct pca9547_softc *sc = device_get_softc(dev);
	uint8_t busbits;

	/*
	 * The iicmux caller ensures busidx is between 0 and the number of buses
	 * we passed to iicmux_init_softc(), no need for validation here.  If
	 * the fdt data has the idle_disconnect property we idle the bus by
	 * selecting no downstream buses, otherwise we just leave the current
	 * bus active.  The upper bits of control register 3 activate the
	 * downstream buses; bit 7 is the first bus, bit 6 the second, etc.
	 */
	if (busidx == IICMUX_SELECT_IDLE) {
		if (sc->idle_disconnect)
			busbits = 0;
		else
			return (0);
	} else {
		busbits = 0x8 | (busidx & 0x7);
	}

	/*
	 * We have to add the IIC_RECURSIVE flag because the iicmux core has
	 * already reserved the bus for us, and iicdev_writeto() is going to try
	 * to reserve it again, which is allowed with the recursive flag.
	 */

	return (iicdev_writeto(dev, busbits, NULL, 0,
	    rd->flags | IIC_RECURSIVE));
}

static int
pca9547_probe(device_t dev)
{
	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
		return (ENXIO);

	device_set_desc(dev, "PCA9547 IIC bus multiplexor");
	return (BUS_PROBE_DEFAULT);
}

static int
pca9547_attach(device_t dev)
{
	struct pca9547_softc *sc;
	phandle_t node;
	int rv;

	sc = device_get_softc(dev);
	sc ->dev = dev;

	node = ofw_bus_get_node(dev);
	sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect");

	rv = iicmux_attach(sc->dev, device_get_parent(dev), 8);
	if (rv != 0)
		return (rv);
	rv = bus_generic_attach(dev);

	return (rv);
}

static int
pca9547_detach(device_t dev)
{
	int rv;

	rv = iicmux_detach(dev);
	if (rv != 0)
		return (rv);

	return (0);
}

static device_method_t pca9547_methods[] = {
	/* device methods */
	DEVMETHOD(device_probe,			pca9547_probe),
	DEVMETHOD(device_attach,		pca9547_attach),
	DEVMETHOD(device_detach,		pca9547_detach),

	/* iicmux methods */
	DEVMETHOD(iicmux_bus_select,		pca9547_bus_select),

	DEVMETHOD_END
};

static devclass_t pca9547_devclass;
DEFINE_CLASS_1(iicmux, pca9547_driver, pca9547_methods,
    sizeof(struct pca9547_softc), iicmux_driver);
DRIVER_MODULE(pca_iicmux, iicbus, pca9547_driver, pca9547_devclass, 0, 0);
IICBUS_FDT_PNP_INFO(compat_data);
DRIVER_MODULE(iicbus, iicmux, iicbus_driver, iicbus_devclass, 0, 0);
DRIVER_MODULE(ofw_iicbus, iicmux, ofw_iicbus_driver, ofw_iicbus_devclass,
    0, 0);
MODULE_DEPEND(pca9547, iicmux, 1, 1, 1);
MODULE_DEPEND(pca9547, iicbus, 1, 1, 1);