imx_i2c.c revision 1.1
11.1Sskrll/* $NetBSD: imx_i2c.c,v 1.1 2020/12/23 14:42:38 skrll 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.1Sskrll__KERNEL_RCSID(0, "$NetBSD: imx_i2c.c,v 1.1 2020/12/23 14:42:38 skrll 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.1Sskrllint 391.1Sskrllimxi2c_match(device_t parent, cfdata_t cf, void *aux) 401.1Sskrll{ 411.1Sskrll const char * const compatible[] = { "fsl,imx21-i2c", NULL }; 421.1Sskrll struct fdt_attach_args * const faa = aux; 431.1Sskrll 441.1Sskrll return of_match_compatible(faa->faa_phandle, compatible); 451.1Sskrll} 461.1Sskrll 471.1Sskrllvoid 481.1Sskrllimxi2c_attach(device_t parent __unused, device_t self, void *aux) 491.1Sskrll{ 501.1Sskrll struct imxi2c_softc *sc = device_private(self); 511.1Sskrll struct fdt_attach_args * const faa = aux; 521.1Sskrll const int phandle = faa->faa_phandle; 531.1Sskrll bus_space_tag_t bst = faa->faa_bst; 541.1Sskrll bus_addr_t addr; 551.1Sskrll bus_size_t size; 561.1Sskrll int error; 571.1Sskrll 581.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 591.1Sskrll aprint_error(": couldn't get registers\n"); 601.1Sskrll return; 611.1Sskrll } 621.1Sskrll 631.1Sskrll sc->sc_clk = fdtbus_clock_get_index(phandle, 0); 641.1Sskrll if (sc->sc_clk == NULL) { 651.1Sskrll aprint_error(": couldn't get clock\n"); 661.1Sskrll return; 671.1Sskrll } 681.1Sskrll 691.1Sskrll error = clk_enable(sc->sc_clk); 701.1Sskrll if (error) { 711.1Sskrll aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error); 721.1Sskrll return; 731.1Sskrll } 741.1Sskrll 751.1Sskrll u_int freq; 761.1Sskrll error = of_getprop_uint32(phandle, "clock-frequency", &freq); 771.1Sskrll if (error) 781.1Sskrll freq = 100000; 791.1Sskrll imxi2c_set_freq(self, clk_get_rate(sc->sc_clk), freq); 801.1Sskrll 811.1Sskrll sc->sc_motoi2c.sc_phandle = phandle; 821.1Sskrll 831.1Sskrll imxi2c_attach_common(parent, self, bst, addr, size, -1, 0); 841.1Sskrll} 851.1Sskrll 86