nxpiic_acpi.c revision 1.1
11.1Sjmcneill/* $NetBSD: nxpiic_acpi.c,v 1.1 2021/01/24 18:02:51 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2021 The NetBSD Foundation, Inc.
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
81.1Sjmcneill * by Jared McNeill <jmcneill@invisible.ca>.
91.1Sjmcneill *
101.1Sjmcneill * Redistribution and use in source and binary forms, with or without
111.1Sjmcneill * modification, are permitted provided that the following conditions
121.1Sjmcneill * are met:
131.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
141.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
151.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
171.1Sjmcneill *    documentation and/or other materials provided with the distribution.
181.1Sjmcneill *
191.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
301.1Sjmcneill */
311.1Sjmcneill
321.1Sjmcneill#include <sys/cdefs.h>
331.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: nxpiic_acpi.c,v 1.1 2021/01/24 18:02:51 jmcneill Exp $");
341.1Sjmcneill
351.1Sjmcneill#include <sys/param.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/cpu.h>
381.1Sjmcneill#include <sys/device.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/acpi/acpireg.h>
411.1Sjmcneill#include <dev/acpi/acpivar.h>
421.1Sjmcneill#include <dev/acpi/acpi_intr.h>
431.1Sjmcneill#include <dev/acpi/acpi_i2c.h>
441.1Sjmcneill
451.1Sjmcneill#include <dev/i2c/motoi2cvar.h>
461.1Sjmcneill#include <dev/i2c/motoi2creg.h>
471.1Sjmcneill
481.1Sjmcneillstruct nxpiic_softc {
491.1Sjmcneill	device_t		sc_dev;
501.1Sjmcneill	struct motoi2c_softc	sc_motoi2c;
511.1Sjmcneill};
521.1Sjmcneill
531.1Sjmcneillstatic int	nxpiic_acpi_match(device_t, cfdata_t, void *);
541.1Sjmcneillstatic void	nxpiic_acpi_attach(device_t, device_t, void *);
551.1Sjmcneill
561.1Sjmcneillstatic uint8_t	nxpiic_acpi_iord(struct motoi2c_softc *, bus_size_t);
571.1Sjmcneillstatic void	nxpiic_acpi_iowr(struct motoi2c_softc *, bus_size_t, uint8_t);
581.1Sjmcneill
591.1SjmcneillCFATTACH_DECL_NEW(nxpiic_acpi, sizeof(struct nxpiic_softc),
601.1Sjmcneill    nxpiic_acpi_match, nxpiic_acpi_attach, NULL, NULL);
611.1Sjmcneill
621.1Sjmcneillstatic const char * const compatible[] = {
631.1Sjmcneill	"NXP0001",
641.1Sjmcneill	NULL
651.1Sjmcneill};
661.1Sjmcneill
671.1Sjmcneillstatic int
681.1Sjmcneillnxpiic_acpi_match(device_t parent, cfdata_t cf, void *aux)
691.1Sjmcneill{
701.1Sjmcneill	struct acpi_attach_args *aa = aux;
711.1Sjmcneill
721.1Sjmcneill	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
731.1Sjmcneill		return 0;
741.1Sjmcneill
751.1Sjmcneill	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
761.1Sjmcneill}
771.1Sjmcneill
781.1Sjmcneillstatic void
791.1Sjmcneillnxpiic_acpi_attach(device_t parent, device_t self, void *aux)
801.1Sjmcneill{
811.1Sjmcneill	struct nxpiic_softc * const sc = device_private(self);
821.1Sjmcneill	struct motoi2c_softc * const msc = &sc->sc_motoi2c;
831.1Sjmcneill	struct acpi_attach_args *aa = aux;
841.1Sjmcneill	struct acpi_resources res;
851.1Sjmcneill	struct acpi_mem *mem;
861.1Sjmcneill	ACPI_INTEGER clock_freq;
871.1Sjmcneill	ACPI_STATUS rv;
881.1Sjmcneill	int error;
891.1Sjmcneill
901.1Sjmcneill	sc->sc_dev = self;
911.1Sjmcneill	msc->sc_iot = aa->aa_memt;
921.1Sjmcneill
931.1Sjmcneill	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
941.1Sjmcneill	    &res, &acpi_resource_parse_ops_default);
951.1Sjmcneill	if (ACPI_FAILURE(rv))
961.1Sjmcneill		return;
971.1Sjmcneill
981.1Sjmcneill	mem = acpi_res_mem(&res, 0);
991.1Sjmcneill	if (mem == NULL) {
1001.1Sjmcneill		aprint_error_dev(self, "couldn't find mem resource\n");
1011.1Sjmcneill		goto done;
1021.1Sjmcneill	}
1031.1Sjmcneill
1041.1Sjmcneill	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
1051.1Sjmcneill	    &clock_freq);
1061.1Sjmcneill	if (ACPI_FAILURE(rv) || clock_freq == 0) {
1071.1Sjmcneill		aprint_error_dev(self, "couldn't get clock frequency\n");
1081.1Sjmcneill		goto done;
1091.1Sjmcneill	}
1101.1Sjmcneill
1111.1Sjmcneill	error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0,
1121.1Sjmcneill	    &msc->sc_ioh);
1131.1Sjmcneill	if (error) {
1141.1Sjmcneill		aprint_error_dev(self, "couldn't map registers\n");
1151.1Sjmcneill		return;
1161.1Sjmcneill	}
1171.1Sjmcneill
1181.1Sjmcneill	msc->sc_iord = nxpiic_acpi_iord;
1191.1Sjmcneill	msc->sc_iowr = nxpiic_acpi_iowr;
1201.1Sjmcneill	msc->sc_child_devices = acpi_enter_i2c_devs(aa->aa_node);
1211.1Sjmcneill
1221.1Sjmcneill	motoi2c_attach_common(self, msc, NULL);
1231.1Sjmcneill
1241.1Sjmcneilldone:
1251.1Sjmcneill	acpi_resource_cleanup(&res);
1261.1Sjmcneill
1271.1Sjmcneill}
1281.1Sjmcneill
1291.1Sjmcneillstatic uint8_t
1301.1Sjmcneillnxpiic_acpi_iord(struct motoi2c_softc *msc, bus_size_t off)
1311.1Sjmcneill{
1321.1Sjmcneill	KASSERT((off & 3) == 0);
1331.1Sjmcneill
1341.1Sjmcneill	if (off >= I2CDFSRR)
1351.1Sjmcneill		return 0;
1361.1Sjmcneill
1371.1Sjmcneill	return bus_space_read_1(msc->sc_iot, msc->sc_ioh, off >> 2);
1381.1Sjmcneill}
1391.1Sjmcneill
1401.1Sjmcneillstatic void
1411.1Sjmcneillnxpiic_acpi_iowr(struct motoi2c_softc *msc, bus_size_t off, uint8_t val)
1421.1Sjmcneill{
1431.1Sjmcneill	KASSERT((off & 3) == 0);
1441.1Sjmcneill
1451.1Sjmcneill	if (off >= I2CDFSRR)
1461.1Sjmcneill		return;
1471.1Sjmcneill
1481.1Sjmcneill	bus_space_write_1(msc->sc_iot, msc->sc_ioh, off >> 2, val);
1491.1Sjmcneill}
150