xref: /freebsd/sys/dev/ichiic/ig4_acpi.c (revision c4fde87a3236b500246d4fce76dbca2a3d64f73a)
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 #include "opt_acpi.h"
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/proc.h>
35 #include <sys/rman.h>
36 
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 
40 #include <contrib/dev/acpica/include/acpi.h>
41 #include <contrib/dev/acpica/include/accommon.h>
42 
43 #include <dev/acpica/acpivar.h>
44 #include <dev/iicbus/iiconf.h>
45 
46 #include <dev/ichiic/ig4_reg.h>
47 #include <dev/ichiic/ig4_var.h>
48 
49 static int	ig4iic_acpi_probe(device_t dev);
50 static int	ig4iic_acpi_attach(device_t dev);
51 static int	ig4iic_acpi_detach(device_t dev);
52 
53 static char *ig4iic_ids[] = {
54 	"INT33C2",
55 	"INT33C3",
56 	"INT3432",
57 	"INT3433",
58 	"INT3446",
59 	"80860F41",
60 	"808622C1",
61 	"AMDI0510",
62 	"AMDI0010",
63 	"AMD0010",
64 	"APMC0D0F",
65 	NULL
66 };
67 
68 static int
69 ig4iic_acpi_probe(device_t dev)
70 {
71 	int rv;
72 
73 	if (acpi_disabled("ig4iic"))
74 		return (ENXIO);
75 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, ig4iic_ids, NULL);
76 	if (rv > 0)
77 		return (rv);
78 
79 	device_set_desc(dev, "Designware I2C Controller");
80 	return (rv);
81 }
82 
83 static int
84 ig4iic_acpi_attach(device_t dev)
85 {
86 	ig4iic_softc_t	*sc;
87 	char *str;
88 	int error;
89 
90 	sc = device_get_softc(dev);
91 
92 	sc->dev = dev;
93 	error = ACPI_ID_PROBE(device_get_parent(dev), dev, ig4iic_ids, &str);
94 	if (error > 0)
95 		return (error);
96 	if (strcmp(str, "APMC0D0F") == 0) {
97 		sc->version = IG4_EMAG;
98 	} else if (strcmp(str, "INT33C2") == 0 || strcmp(str, "INT33C3") == 0 ||
99 	    strcmp(str, "INT3432") == 0 || strcmp(str, "INT3433") == 0) {
100 		/* Lynx Point-LP / Wildcat Point-LP LPSS in ACPI mode. */
101 		sc->version = IG4_HASWELL;
102 	} else {
103 		/* All the other HIDs matched are Atom SOCs. */
104 		sc->version = IG4_ATOM;
105 	}
106 	/*
107 	 * Firmware may leave LPSS functions in D3 (e.g. Dell XPS 13 9343):
108 	 * the registers then read as all-ones and attach fails.  Run _PS0.
109 	 */
110 	acpi_set_powerstate(dev, ACPI_STATE_D0);
111 
112 	sc->regs_rid = 0;
113 	sc->regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
114 					  &sc->regs_rid, RF_ACTIVE);
115 	if (sc->regs_res == NULL) {
116 		device_printf(dev, "unable to map registers\n");
117 		ig4iic_acpi_detach(dev);
118 		return (ENXIO);
119 	}
120 	sc->intr_rid = 0;
121 	sc->intr_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
122 					  &sc->intr_rid, RF_SHAREABLE | RF_ACTIVE);
123 	if (sc->intr_res == NULL) {
124 		device_printf(dev, "unable to map interrupt\n");
125 		ig4iic_acpi_detach(dev);
126 		return (ENXIO);
127 	}
128 	sc->platform_attached = true;
129 
130 	error = ig4iic_attach(sc);
131 	if (error)
132 		ig4iic_acpi_detach(dev);
133 
134 	return (error);
135 }
136 
137 static int
138 ig4iic_acpi_detach(device_t dev)
139 {
140 	ig4iic_softc_t *sc = device_get_softc(dev);
141 	int error;
142 
143 	if (sc->platform_attached) {
144 		error = ig4iic_detach(sc);
145 		if (error)
146 			return (error);
147 		sc->platform_attached = false;
148 	}
149 
150 	if (sc->intr_res) {
151 		bus_release_resource(dev, SYS_RES_IRQ,
152 				     sc->intr_rid, sc->intr_res);
153 		sc->intr_res = NULL;
154 	}
155 	if (sc->regs_res) {
156 		bus_release_resource(dev, SYS_RES_MEMORY,
157 				     sc->regs_rid, sc->regs_res);
158 		sc->regs_res = NULL;
159 	}
160 
161 	return (0);
162 }
163 
164 static int
165 ig4iic_acpi_suspend(device_t dev)
166 {
167 	ig4iic_softc_t *sc = device_get_softc(dev);
168 
169 	return (ig4iic_suspend(sc));
170 }
171 
172 static int
173 ig4iic_acpi_resume(device_t dev)
174 {
175 	ig4iic_softc_t *sc  = device_get_softc(dev);
176 
177 	return (ig4iic_resume(sc));
178 }
179 
180 static device_method_t ig4iic_acpi_methods[] = {
181 	/* Device interface */
182 	DEVMETHOD(device_probe, ig4iic_acpi_probe),
183 	DEVMETHOD(device_attach, ig4iic_acpi_attach),
184 	DEVMETHOD(device_detach, ig4iic_acpi_detach),
185 	DEVMETHOD(device_suspend, ig4iic_acpi_suspend),
186 	DEVMETHOD(device_resume, ig4iic_acpi_resume),
187 
188 	/* Bus interface */
189 	DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
190 	DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
191 	DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
192 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
193 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
194 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
195 	DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
196 
197 	/* iicbus interface */
198 	DEVMETHOD(iicbus_transfer, ig4iic_transfer),
199 	DEVMETHOD(iicbus_reset, ig4iic_reset),
200 	DEVMETHOD(iicbus_callback, ig4iic_callback),
201 
202 	DEVMETHOD_END
203 };
204 
205 static driver_t ig4iic_acpi_driver = {
206 	"ig4iic",
207 	ig4iic_acpi_methods,
208 	sizeof(struct ig4iic_softc),
209 };
210 
211 DRIVER_MODULE_ORDERED(ig4iic, acpi, ig4iic_acpi_driver, 0, 0, SI_ORDER_ANY);
212 MODULE_DEPEND(ig4iic, acpi, 1, 1, 1);
213 ACPI_PNP_INFO(ig4iic_ids);
214