xref: /freebsd/sys/dev/iicbus/iicbus.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1c3e2dc6bSNicolas Souchu /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4c3e2dc6bSNicolas Souchu  * Copyright (c) 1998 Nicolas Souchu
5c3e2dc6bSNicolas Souchu  * All rights reserved.
6c3e2dc6bSNicolas Souchu  *
7c3e2dc6bSNicolas Souchu  * Redistribution and use in source and binary forms, with or without
8c3e2dc6bSNicolas Souchu  * modification, are permitted provided that the following conditions
9c3e2dc6bSNicolas Souchu  * are met:
10c3e2dc6bSNicolas Souchu  * 1. Redistributions of source code must retain the above copyright
11c3e2dc6bSNicolas Souchu  *    notice, this list of conditions and the following disclaimer.
12c3e2dc6bSNicolas Souchu  * 2. Redistributions in binary form must reproduce the above copyright
13c3e2dc6bSNicolas Souchu  *    notice, this list of conditions and the following disclaimer in the
14c3e2dc6bSNicolas Souchu  *    documentation and/or other materials provided with the distribution.
15c3e2dc6bSNicolas Souchu  *
16c3e2dc6bSNicolas Souchu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c3e2dc6bSNicolas Souchu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c3e2dc6bSNicolas Souchu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c3e2dc6bSNicolas Souchu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c3e2dc6bSNicolas Souchu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c3e2dc6bSNicolas Souchu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c3e2dc6bSNicolas Souchu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c3e2dc6bSNicolas Souchu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c3e2dc6bSNicolas Souchu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c3e2dc6bSNicolas Souchu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c3e2dc6bSNicolas Souchu  * SUCH DAMAGE.
27c3e2dc6bSNicolas Souchu  *
28c3e2dc6bSNicolas Souchu  */
29c3e2dc6bSNicolas Souchu #ifndef __IICBUS_H
30c3e2dc6bSNicolas Souchu #define __IICBUS_H
31c3e2dc6bSNicolas Souchu 
32313f8941SJohn Baldwin #include <sys/_lock.h>
33313f8941SJohn Baldwin #include <sys/_mutex.h>
34313f8941SJohn Baldwin 
35ee952d0eSWarner Losh #define IICBUS_IVAR(d) (struct iicbus_ivar *) device_get_ivars(d)
36ee952d0eSWarner Losh #define IICBUS_SOFTC(d) (struct iicbus_softc *) device_get_softc(d)
37c3e2dc6bSNicolas Souchu 
38ee952d0eSWarner Losh struct iicbus_softc
39ee952d0eSWarner Losh {
40ee952d0eSWarner Losh 	device_t dev;		/* Myself */
41c3e2dc6bSNicolas Souchu 	device_t owner;		/* iicbus owner device structure */
42873bf31bSIan Lepore 	device_t busydev;	/* iicbus_release_bus calls unbusy on this */
4334199ee0SIan Lepore 	u_int owncount;		/* iicbus ownership nesting count */
44bf896bd0SNicolas Souchu 	u_char started;		/* address of the 'started' slave
45bf896bd0SNicolas Souchu 				 * 0 if no start condition succeeded */
469844b3b3SAdrian Chadd 	u_char strict;		/* deny operations that violate the
479844b3b3SAdrian Chadd 				 * I2C protocol */
48313f8941SJohn Baldwin 	struct mtx lock;
49844aff82SIan Lepore 	u_int bus_freq;		/* Configured bus Hz. */
50c3e2dc6bSNicolas Souchu };
51c3e2dc6bSNicolas Souchu 
52ee952d0eSWarner Losh struct iicbus_ivar
53ee952d0eSWarner Losh {
54ee952d0eSWarner Losh 	uint32_t	addr;
553bb6bf47SLuiz Otavio O Souza 	struct resource_list	rl;
56ee952d0eSWarner Losh };
57ee952d0eSWarner Losh 
589b7938dcSVladimir Kondratyev /* Value of 0x100 is reserved for ACPI_IVAR_HANDLE used by acpi_iicbus */
59ee952d0eSWarner Losh enum {
60fec2f12eSAndriy Gapon 	IICBUS_IVAR_ADDR		/* Address or base address */
61ee952d0eSWarner Losh };
62ee952d0eSWarner Losh 
63ee952d0eSWarner Losh #define IICBUS_ACCESSOR(A, B, T)					\
64313f8941SJohn Baldwin 	__BUS_ACCESSOR(iicbus, A, IICBUS, B, T)
65ee952d0eSWarner Losh 
66ee952d0eSWarner Losh IICBUS_ACCESSOR(addr,		ADDR,		uint32_t)
67ee952d0eSWarner Losh 
68313f8941SJohn Baldwin #define	IICBUS_LOCK(sc)			mtx_lock(&(sc)->lock)
69313f8941SJohn Baldwin #define	IICBUS_UNLOCK(sc)      		mtx_unlock(&(sc)->lock)
70313f8941SJohn Baldwin #define	IICBUS_ASSERT_LOCKED(sc)       	mtx_assert(&(sc)->lock, MA_OWNED)
71313f8941SJohn Baldwin 
72b609a553SIan Lepore #ifdef FDT
737a038f29SIan Lepore #define	IICBUS_FDT_PNP_INFO(t)	FDTCOMPAT_PNP_INFO(t, iicbus)
74b609a553SIan Lepore #else
757a038f29SIan Lepore #define	IICBUS_FDT_PNP_INFO(t)
76b609a553SIan Lepore #endif
77b609a553SIan Lepore 
789c6e3774SVladimir Kondratyev #ifdef DEV_ACPI
799c6e3774SVladimir Kondratyev #define	IICBUS_ACPI_PNP_INFO(t)	ACPICOMPAT_PNP_INFO(t, iicbus)
809c6e3774SVladimir Kondratyev #else
819c6e3774SVladimir Kondratyev #define	IICBUS_ACPI_PNP_INFO(t)
829c6e3774SVladimir Kondratyev #endif
839c6e3774SVladimir Kondratyev 
84844aff82SIan Lepore int  iicbus_generic_intr(device_t dev, int event, char *buf);
85844aff82SIan Lepore void iicbus_init_frequency(device_t dev, u_int bus_freq);
86c3e2dc6bSNicolas Souchu 
879b7938dcSVladimir Kondratyev int iicbus_attach_common(device_t dev, u_int bus_freq);
889b7938dcSVladimir Kondratyev device_t iicbus_add_child_common(device_t dev, u_int order, const char *name,
899b7938dcSVladimir Kondratyev     int unit, size_t ivars_size);
909b7938dcSVladimir Kondratyev int iicbus_detach(device_t dev);
919b7938dcSVladimir Kondratyev void iicbus_probe_nomatch(device_t bus, device_t child);
929b7938dcSVladimir Kondratyev int iicbus_read_ivar(device_t bus, device_t child, int which,
939b7938dcSVladimir Kondratyev     uintptr_t *result);
949b7938dcSVladimir Kondratyev int iicbus_write_ivar(device_t bus, device_t child, int which,
959b7938dcSVladimir Kondratyev     uintptr_t value);
96ddfc9c4cSWarner Losh int iicbus_child_location(device_t bus, device_t child, struct sbuf *sb);
97ddfc9c4cSWarner Losh int iicbus_child_pnpinfo(device_t bus, device_t child, struct sbuf *sb);
989b7938dcSVladimir Kondratyev 
99d7fac973SWarner Losh extern driver_t iicbus_driver;
100c5fe9c7bSIan Lepore extern driver_t ofw_iicbus_driver;
1019b7938dcSVladimir Kondratyev extern driver_t acpi_iicbus_driver;
102d7fac973SWarner Losh 
103c3e2dc6bSNicolas Souchu #endif
104