imx_i2c.c revision 1.3
11.3Sthorpej/*	$NetBSD: imx_i2c.c,v 1.3 2022/07/22 23:43:24 thorpej Exp $	*/
21.1Sskrll
31.1Sskrll/*-
41.1Sskrll * Copyright (c) 2019 Genetec Corporation.  All rights reserved.
51.1Sskrll * Written by Hashimoto Kenichi for Genetec Corporation.
61.1Sskrll *
71.1Sskrll * Redistribution and use in source and binary forms, with or without
81.1Sskrll * modification, are permitted provided that the following conditions
91.1Sskrll * are met:
101.1Sskrll * 1. Redistributions of source code must retain the above copyright
111.1Sskrll *    notice, this list of conditions and the following disclaimer.
121.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright
131.1Sskrll *    notice, this list of conditions and the following disclaimer in the
141.1Sskrll *    documentation and/or other materials provided with the distribution.
151.1Sskrll *
161.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sskrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sskrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sskrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sskrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sskrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sskrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sskrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sskrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sskrll * SUCH DAMAGE.
271.1Sskrll */
281.1Sskrll
291.1Sskrll#include <sys/cdefs.h>
301.3Sthorpej__KERNEL_RCSID(0, "$NetBSD: imx_i2c.c,v 1.3 2022/07/22 23:43:24 thorpej Exp $");
311.1Sskrll
321.1Sskrll#include <sys/bus.h>
331.1Sskrll
341.1Sskrll#include <arm/imx/imxi2cvar.h>
351.1Sskrll
361.1Sskrll#include <dev/fdt/fdtvar.h>
371.1Sskrll
381.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
391.2Sthorpej	{ .compat = "fsl,imx21-i2c" },
401.2Sthorpej	DEVICE_COMPAT_EOL
411.2Sthorpej};
421.2Sthorpej
431.1Sskrllint
441.1Sskrllimxi2c_match(device_t parent, cfdata_t cf, void *aux)
451.1Sskrll{
461.1Sskrll	struct fdt_attach_args * const faa = aux;
471.1Sskrll
481.2Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
491.1Sskrll}
501.1Sskrll
511.1Sskrllvoid
521.1Sskrllimxi2c_attach(device_t parent __unused, device_t self, void *aux)
531.1Sskrll{
541.3Sthorpej	struct imxi2c_softc *imxsc = device_private(self);
551.1Sskrll	struct fdt_attach_args * const faa = aux;
561.1Sskrll	const int phandle = faa->faa_phandle;
571.1Sskrll	bus_space_tag_t bst = faa->faa_bst;
581.1Sskrll	bus_addr_t addr;
591.1Sskrll	bus_size_t size;
601.1Sskrll	int error;
611.1Sskrll
621.1Sskrll	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
631.1Sskrll		aprint_error(": couldn't get registers\n");
641.1Sskrll		return;
651.1Sskrll	}
661.1Sskrll
671.3Sthorpej	imxsc->sc_clk = fdtbus_clock_get_index(phandle, 0);
681.3Sthorpej	if (imxsc->sc_clk == NULL) {
691.1Sskrll		aprint_error(": couldn't get clock\n");
701.1Sskrll		return;
711.1Sskrll	}
721.1Sskrll
731.3Sthorpej	error = clk_enable(imxsc->sc_clk);
741.1Sskrll	if (error) {
751.3Sthorpej		aprint_error_dev(self, "couldn't enable: %d\n", error);
761.1Sskrll		return;
771.1Sskrll	}
781.1Sskrll
791.1Sskrll	u_int freq;
801.1Sskrll	error = of_getprop_uint32(phandle, "clock-frequency", &freq);
811.1Sskrll	if (error)
821.1Sskrll		freq = 100000;
831.1Sskrll
841.3Sthorpej	imxsc->sc_motoi2c.sc_phandle = phandle;
851.3Sthorpej	imxi2c_attach_common(self, bst, addr, size,
861.3Sthorpej	    clk_get_rate(imxsc->sc_clk), freq);
871.1Sskrll}
88