Home | History | Annotate | Line # | Download | only in acpi
plgpio_acpi.c revision 1.1.2.2
      1 /* $NetBSD: plgpio_acpi.c,v 1.1.2.2 2018/10/20 06:58:30 pgoyette Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.1.2.2 2018/10/20 06:58:30 pgoyette Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 #include <sys/device.h>
     39 #include <sys/gpio.h>
     40 
     41 #include <dev/acpi/acpireg.h>
     42 #include <dev/acpi/acpivar.h>
     43 
     44 #include <dev/gpio/gpiovar.h>
     45 #include <dev/ic/pl061var.h>
     46 
     47 static int	plgpio_acpi_match(device_t, cfdata_t, void *);
     48 static void	plgpio_acpi_attach(device_t, device_t, void *);
     49 
     50 CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
     51 
     52 static const char * const compatible[] = {
     53 	"ARMH0061",
     54 	NULL
     55 };
     56 
     57 static int
     58 plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
     59 {
     60 	struct acpi_attach_args *aa = aux;
     61 
     62 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     63 		return 0;
     64 
     65 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
     66 }
     67 
     68 static void
     69 plgpio_acpi_attach(device_t parent, device_t self, void *aux)
     70 {
     71 	struct plgpio_softc * const sc = device_private(self);
     72 	struct acpi_attach_args *aa = aux;
     73 	struct acpi_resources res;
     74 	struct acpi_mem *mem;
     75 	ACPI_STATUS rv;
     76 	int error;
     77 
     78 	sc->sc_dev = self;
     79 
     80 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
     81 	    &res, &acpi_resource_parse_ops_default);
     82 	if (ACPI_FAILURE(rv))
     83 		return;
     84 
     85 	mem = acpi_res_mem(&res, 0);
     86 	if (mem == NULL) {
     87 		aprint_error_dev(self, "couldn't find mem resource\n");
     88 		goto done;
     89 	}
     90 
     91 	sc->sc_dev = self;
     92 	sc->sc_bst = aa->aa_memt;
     93 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
     94 	if (error) {
     95 		aprint_error_dev(self, "couldn't map registers\n");
     96 		return;
     97 	}
     98 
     99 	plgpio_attach(sc);
    100 
    101 done:
    102 	acpi_resource_cleanup(&res);
    103 }
    104