xref: /netbsd-src/sys/dev/ic/mcp23xxxgpiovar.h (revision aa7530e36b678b04a7c6d2ad374ea9e232743361)
1 /*      $NetBSD: mcp23xxxgpiovar.h,v 1.2 2022/01/17 19:36:54 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2014, 2022 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank Kardel, and by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _DEV_IC_MCP23xxxGPIOVAR_H_
33 #define	_DEV_IC_MCP23xxxGPIOVAR_H_
34 
35 /*
36  * Driver for Microchip serial I/O expansers:
37  *
38  *	MCP23008	8-bit, I2C interface
39  *	MCP23S08	8-bit, SPI interface
40  *	MCP23017	16-bit, I2C interface
41  *	MCP23S17	16-bit, SPI interface
42  *	MCP23018	16-bit (open-drain outputs), I2C interface
43  *	MCP23S18	16-bit (open-drain outputs), SPI interface
44  *
45  * Data sheet:
46  *
47  *	https://ww1.microchip.com/downloads/en/DeviceDoc/20001952C.pdf
48  */
49 
50 #include <sys/gpio.h>
51 #include <dev/gpio/gpiovar.h>
52 
53 struct mcpgpio_softc;
54 
55 typedef enum {
56 	MCPGPIO_TYPE_23x08	= 0,
57 	MCPGPIO_TYPE_23x17	= 1,
58 	MCPGPIO_TYPE_23x18	= 2,
59 } mcpgpio_type;
60 
61 struct mcpgpio_variant {
62 	const char		*name;
63 	mcpgpio_type		type;
64 };
65 
66 struct mcpgpio_accessops {
67 	int			(*lock)(struct mcpgpio_softc *);
68 	void			(*unlock)(struct mcpgpio_softc *);
69 	int			(*read)(struct mcpgpio_softc *,
70 				    unsigned int, uint8_t, uint8_t *);
71 	int			(*write)(struct mcpgpio_softc *,
72 				    unsigned int, uint8_t, uint8_t);
73 };
74 
75 struct mcpgpio_softc {
76 	device_t                sc_dev;
77 	const struct mcpgpio_variant *sc_variant;
78 	struct gpio_chipset_tag	sc_gpio_gc;
79 	gpio_pin_t		*sc_gpio_pins;
80 	unsigned int		sc_npins;
81 	uint8_t			sc_iocon; /* I/O configuration */
82 
83 	/* Bus-specific access functions. */
84 	const struct mcpgpio_accessops *sc_accessops;
85 };
86 
87 void	mcpgpio_attach(struct mcpgpio_softc *);
88 
89 #endif /* _DEV_IC_MCP23xxxGPIOVAR_H_ */
90