xref: /freebsd/sys/dev/ichiic/ig4_acpi.c (revision 81ad6265)
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 	"AMDI0010",
64 	"AMD0010",
65 	"APMC0D0F",
66 	NULL
67 };
68 
69 static int
70 ig4iic_acpi_probe(device_t dev)
71 {
72 	int rv;
73 
74 	if (acpi_disabled("ig4iic"))
75 		return (ENXIO);
76 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, ig4iic_ids, NULL);
77 	if (rv > 0)
78 		return (rv);
79 
80 	device_set_desc(dev, "Designware I2C Controller");
81 	return (rv);
82 }
83 
84 static int
85 ig4iic_acpi_attach(device_t dev)
86 {
87 	ig4iic_softc_t	*sc;
88 	int error;
89 
90 	sc = device_get_softc(dev);
91 
92 	sc->dev = dev;
93 	/* All the HIDs matched are Atom SOCs. */
94 	sc->version = IG4_ATOM;
95 	sc->regs_rid = 0;
96 	sc->regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
97 					  &sc->regs_rid, RF_ACTIVE);
98 	if (sc->regs_res == NULL) {
99 		device_printf(dev, "unable to map registers\n");
100 		ig4iic_acpi_detach(dev);
101 		return (ENXIO);
102 	}
103 	sc->intr_rid = 0;
104 	sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
105 					  &sc->intr_rid, RF_SHAREABLE | RF_ACTIVE);
106 	if (sc->intr_res == NULL) {
107 		device_printf(dev, "unable to map interrupt\n");
108 		ig4iic_acpi_detach(dev);
109 		return (ENXIO);
110 	}
111 	sc->platform_attached = 1;
112 
113 	error = ig4iic_attach(sc);
114 	if (error)
115 		ig4iic_acpi_detach(dev);
116 
117 	return (error);
118 }
119 
120 static int
121 ig4iic_acpi_detach(device_t dev)
122 {
123 	ig4iic_softc_t *sc = device_get_softc(dev);
124 	int error;
125 
126 	if (sc->platform_attached) {
127 		error = ig4iic_detach(sc);
128 		if (error)
129 			return (error);
130 		sc->platform_attached = 0;
131 	}
132 
133 	if (sc->intr_res) {
134 		bus_release_resource(dev, SYS_RES_IRQ,
135 				     sc->intr_rid, sc->intr_res);
136 		sc->intr_res = NULL;
137 	}
138 	if (sc->regs_res) {
139 		bus_release_resource(dev, SYS_RES_MEMORY,
140 				     sc->regs_rid, sc->regs_res);
141 		sc->regs_res = NULL;
142 	}
143 
144 	return (0);
145 }
146 
147 static int
148 ig4iic_acpi_suspend(device_t dev)
149 {
150 	ig4iic_softc_t *sc = device_get_softc(dev);
151 
152 	return (ig4iic_suspend(sc));
153 }
154 
155 static int
156 ig4iic_acpi_resume(device_t dev)
157 {
158 	ig4iic_softc_t *sc  = device_get_softc(dev);
159 
160 	return (ig4iic_resume(sc));
161 }
162 
163 static device_method_t ig4iic_acpi_methods[] = {
164 	/* Device interface */
165 	DEVMETHOD(device_probe, ig4iic_acpi_probe),
166 	DEVMETHOD(device_attach, ig4iic_acpi_attach),
167 	DEVMETHOD(device_detach, ig4iic_acpi_detach),
168 	DEVMETHOD(device_suspend, ig4iic_acpi_suspend),
169 	DEVMETHOD(device_resume, ig4iic_acpi_resume),
170 
171 	/* Bus interface */
172 	DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
173 	DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
174 	DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
175 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
176 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
177 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
178 	DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
179 
180 	/* iicbus interface */
181 	DEVMETHOD(iicbus_transfer, ig4iic_transfer),
182 	DEVMETHOD(iicbus_reset, ig4iic_reset),
183 	DEVMETHOD(iicbus_callback, ig4iic_callback),
184 
185 	DEVMETHOD_END
186 };
187 
188 static driver_t ig4iic_acpi_driver = {
189 	"ig4iic",
190 	ig4iic_acpi_methods,
191 	sizeof(struct ig4iic_softc),
192 };
193 
194 DRIVER_MODULE_ORDERED(ig4iic, acpi, ig4iic_acpi_driver, 0, 0, SI_ORDER_ANY);
195 MODULE_DEPEND(ig4iic, acpi, 1, 1, 1);
196 ACPI_PNP_INFO(ig4iic_ids);
197