xref: /freebsd/sys/dev/ichiic/ig4_acpi.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * Copyright (c) 2016 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_acpi.h"
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/proc.h>
37 #include <sys/rman.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 
42 #include <contrib/dev/acpica/include/acpi.h>
43 #include <contrib/dev/acpica/include/accommon.h>
44 
45 #include <dev/acpica/acpivar.h>
46 #include <dev/iicbus/iiconf.h>
47 
48 #include <dev/ichiic/ig4_reg.h>
49 #include <dev/ichiic/ig4_var.h>
50 
51 static int	ig4iic_acpi_probe(device_t dev);
52 static int	ig4iic_acpi_attach(device_t dev);
53 static int	ig4iic_acpi_detach(device_t dev);
54 
55 static char *ig4iic_ids[] = {
56 	"INT33C2",
57 	"INT33C3",
58 	"INT3432",
59 	"INT3433",
60 	"80860F41",
61 	"808622C1",
62 	"AMDI0510",
63 	"APMC0D0F",
64 	NULL
65 };
66 
67 static int
68 ig4iic_acpi_probe(device_t dev)
69 {
70 
71 	if (acpi_disabled("ig4iic") ||
72 	    ACPI_ID_PROBE(device_get_parent(dev), dev, ig4iic_ids) == NULL)
73 	return (ENXIO);
74 
75 	device_set_desc(dev, "Designware I2C Controller");
76 	return (0);
77 }
78 
79 static int
80 ig4iic_acpi_attach(device_t dev)
81 {
82 	ig4iic_softc_t	*sc;
83 	int error;
84 
85 	sc = device_get_softc(dev);
86 
87 	sc->dev = dev;
88 	sc->regs_rid = 0;
89 	sc->regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
90 					  &sc->regs_rid, RF_ACTIVE);
91 	if (sc->regs_res == NULL) {
92 		device_printf(dev, "unable to map registers\n");
93 		ig4iic_acpi_detach(dev);
94 		return (ENXIO);
95 	}
96 	sc->intr_rid = 0;
97 	sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
98 					  &sc->intr_rid, RF_SHAREABLE | RF_ACTIVE);
99 	if (sc->intr_res == NULL) {
100 		device_printf(dev, "unable to map interrupt\n");
101 		ig4iic_acpi_detach(dev);
102 		return (ENXIO);
103 	}
104 	sc->platform_attached = 1;
105 
106 	error = ig4iic_attach(sc);
107 	if (error)
108 		ig4iic_acpi_detach(dev);
109 
110 	return (error);
111 }
112 
113 static int
114 ig4iic_acpi_detach(device_t dev)
115 {
116 	ig4iic_softc_t *sc = device_get_softc(dev);
117 	int error;
118 
119 	if (sc->platform_attached) {
120 		error = ig4iic_detach(sc);
121 		if (error)
122 			return (error);
123 		sc->platform_attached = 0;
124 	}
125 
126 	if (sc->intr_res) {
127 		bus_release_resource(dev, SYS_RES_IRQ,
128 				     sc->intr_rid, sc->intr_res);
129 		sc->intr_res = NULL;
130 	}
131 	if (sc->regs_res) {
132 		bus_release_resource(dev, SYS_RES_MEMORY,
133 				     sc->regs_rid, sc->regs_res);
134 		sc->regs_res = NULL;
135 	}
136 
137 	return (0);
138 }
139 
140 static device_method_t ig4iic_acpi_methods[] = {
141 	/* Device interface */
142 	DEVMETHOD(device_probe, ig4iic_acpi_probe),
143 	DEVMETHOD(device_attach, ig4iic_acpi_attach),
144 	DEVMETHOD(device_detach, ig4iic_acpi_detach),
145 
146 	/* iicbus interface */
147 	DEVMETHOD(iicbus_transfer, ig4iic_transfer),
148 	DEVMETHOD(iicbus_reset, ig4iic_reset),
149 	DEVMETHOD(iicbus_callback, iicbus_null_callback),
150 
151 	DEVMETHOD_END
152 };
153 
154 static driver_t ig4iic_acpi_driver = {
155 	"ig4iic_acpi",
156 	ig4iic_acpi_methods,
157 	sizeof(struct ig4iic_softc),
158 };
159 
160 static devclass_t ig4iic_acpi_devclass;
161 DRIVER_MODULE(ig4iic_acpi, acpi, ig4iic_acpi_driver, ig4iic_acpi_devclass, 0, 0);
162 
163 MODULE_DEPEND(ig4iic_acpi, acpi, 1, 1, 1);
164 MODULE_DEPEND(ig4iic_acpi, pci, 1, 1, 1);
165 MODULE_DEPEND(ig4iic_acpi, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
166 MODULE_VERSION(ig4iic_acpi, 1);
167