Home | History | Annotate | Line # | Download | only in samsung
exynos_pinctrl.c revision 1.9
      1  1.9     marty /*	$NetBSD: exynos_pinctrl.c,v 1.9 2016/01/01 22:37:07 marty Exp $ */
      2  1.1     marty 
      3  1.1     marty /*-
      4  1.1     marty * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.1     marty * All rights reserved.
      6  1.1     marty *
      7  1.1     marty * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     marty * by Marty Fouts
      9  1.1     marty *
     10  1.1     marty * Redistribution and use in source and binary forms, with or without
     11  1.1     marty * modification, are permitted provided that the following conditions
     12  1.1     marty * are met:
     13  1.1     marty * 1. Redistributions of source code must retain the above copyright
     14  1.1     marty *    notice, this list of conditions and the following disclaimer.
     15  1.1     marty * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     marty *    notice, this list of conditions and the following disclaimer in the
     17  1.1     marty *    documentation and/or other materials provided with the distribution.
     18  1.1     marty *
     19  1.1     marty * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     marty * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     marty * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     marty * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     marty * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     marty * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     marty * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     marty * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     marty * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     marty * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     marty * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     marty */
     31  1.1     marty 
     32  1.1     marty #include "opt_exynos.h"
     33  1.1     marty #include "opt_arm_debug.h"
     34  1.1     marty #include "gpio.h"
     35  1.1     marty 
     36  1.1     marty #include <sys/cdefs.h>
     37  1.9     marty __KERNEL_RCSID(1, "$NetBSD: exynos_pinctrl.c,v 1.9 2016/01/01 22:37:07 marty Exp $");
     38  1.1     marty 
     39  1.1     marty #include <sys/param.h>
     40  1.1     marty #include <sys/bus.h>
     41  1.1     marty #include <sys/device.h>
     42  1.1     marty #include <sys/intr.h>
     43  1.1     marty #include <sys/systm.h>
     44  1.1     marty #include <sys/kmem.h>
     45  1.1     marty #include <sys/gpio.h>
     46  1.1     marty 
     47  1.1     marty #include <dev/gpio/gpiovar.h>
     48  1.1     marty 
     49  1.1     marty #include <arm/samsung/exynos_reg.h>
     50  1.3     marty #include <arm/samsung/exynos_var.h>
     51  1.1     marty #include <arm/samsung/exynos_intr.h>
     52  1.1     marty #include <arm/samsung/exynos_pinctrl.h>
     53  1.1     marty 
     54  1.1     marty #include <dev/fdt/fdtvar.h>
     55  1.1     marty 
     56  1.9     marty struct exynos_pinctrl_config {
     57  1.9     marty 	int pc_phandle;
     58  1.9     marty 	struct exynos_gpio_pin_cfg *pc_pincfg;
     59  1.9     marty 	struct exynos_pinctrl_softc *pc_sc;
     60  1.9     marty };
     61  1.9     marty 
     62  1.1     marty static int exynos_pinctrl_match(device_t, cfdata_t, void *);
     63  1.1     marty static void exynos_pinctrl_attach(device_t, device_t, void *);
     64  1.1     marty 
     65  1.9     marty static int  exynos_pinctrl_set_cfg(void *);
     66  1.9     marty static struct exynos_gpio_pin_cfg *
     67  1.9     marty exynos_parse_config(struct exynos_pinctrl_config *pc);
     68  1.8     marty 
     69  1.8     marty static struct fdtbus_pinctrl_controller_func exynos_pinctrl_controller_func = {
     70  1.9     marty 	.set_config = exynos_pinctrl_set_cfg
     71  1.8     marty };
     72  1.8     marty 
     73  1.1     marty CFATTACH_DECL_NEW(exynos_pinctrl, sizeof(struct exynos_pinctrl_softc),
     74  1.1     marty 	exynos_pinctrl_match, exynos_pinctrl_attach, NULL, NULL);
     75  1.1     marty 
     76  1.1     marty static int
     77  1.1     marty exynos_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
     78  1.1     marty {
     79  1.7  jmcneill 	const char * const compatible[] = { "samsung,exynos5420-pinctrl",
     80  1.1     marty 					    NULL };
     81  1.1     marty 	struct fdt_attach_args * const faa = aux;
     82  1.1     marty 	return of_match_compatible(faa->faa_phandle, compatible);
     83  1.1     marty }
     84  1.1     marty 
     85  1.9     marty static bool
     86  1.9     marty is_pinctrl(int phandle)
     87  1.9     marty {
     88  1.9     marty 	int len = OF_getproplen(phandle, "samsung,pins");
     89  1.9     marty 	return len > 0;
     90  1.9     marty }
     91  1.9     marty 
     92  1.1     marty static void
     93  1.1     marty exynos_pinctrl_attach(device_t parent, device_t self, void *aux)
     94  1.1     marty {
     95  1.1     marty 	struct exynos_pinctrl_softc * const sc
     96  1.1     marty 		= kmem_zalloc(sizeof(*sc), KM_SLEEP);
     97  1.1     marty 	struct fdt_attach_args * const faa = aux;
     98  1.1     marty 	bus_addr_t addr;
     99  1.1     marty 	bus_size_t size;
    100  1.1     marty 	int error;
    101  1.1     marty 	int child;
    102  1.1     marty 
    103  1.1     marty 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    104  1.1     marty 		aprint_error(": couldn't get registers\n");
    105  1.1     marty 		return;
    106  1.1     marty 	}
    107  1.1     marty 
    108  1.4     marty 	aprint_normal(" pinctl @ 0x%08x ", (uint)addr);
    109  1.1     marty 	sc->sc_dev = self;
    110  1.1     marty 	sc->sc_bst = faa->faa_bst;
    111  1.1     marty 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    112  1.1     marty 	if (error) {
    113  1.1     marty 		aprint_error(": couldn't map %#llx: %d",
    114  1.1     marty 			     (uint64_t)addr, error);
    115  1.1     marty 		return;
    116  1.1     marty 	}
    117  1.1     marty 
    118  1.7  jmcneill 	aprint_naive("\n");
    119  1.7  jmcneill 	aprint_normal("\n");
    120  1.7  jmcneill 
    121  1.1     marty 	for (child = OF_child(faa->faa_phandle); child;
    122  1.1     marty 	     child = OF_peer(child)) {
    123  1.9     marty 
    124  1.9     marty 		if (of_getprop_bool(child, "gpio-controller")) {
    125  1.9     marty 			exynos_gpio_bank_config(sc, faa, child);
    126  1.9     marty 		}
    127  1.9     marty 
    128  1.9     marty 		if (is_pinctrl(child)) {
    129  1.9     marty 			struct exynos_pinctrl_config *pc;
    130  1.9     marty 			pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
    131  1.9     marty 			pc->pc_phandle = child;
    132  1.9     marty 			pc->pc_sc = sc;
    133  1.9     marty 			pc->pc_pincfg = exynos_parse_config(pc);
    134  1.9     marty 			fdtbus_register_pinctrl_config(pc, child,
    135  1.9     marty 						       &exynos_pinctrl_controller_func);
    136  1.9     marty 		}
    137  1.1     marty 	}
    138  1.1     marty }
    139  1.8     marty 
    140  1.9     marty static struct exynos_gpio_pin_cfg *
    141  1.9     marty exynos_parse_config(struct exynos_pinctrl_config *pc)
    142  1.9     marty {
    143  1.9     marty 	struct exynos_gpio_pin_cfg *gc = kmem_zalloc(sizeof(*gc), KM_SLEEP);
    144  1.9     marty 	int len;
    145  1.9     marty 	int value;
    146  1.9     marty 
    147  1.9     marty 	len = OF_getprop(pc->pc_phandle, "samsung,pin-function",
    148  1.9     marty 			 &value, sizeof(value));
    149  1.9     marty 	if (len > 0) {
    150  1.9     marty 		gc->cfg = be32toh(value);
    151  1.9     marty 	}
    152  1.9     marty 
    153  1.9     marty 	len = OF_getprop(pc->pc_phandle, "samsung,pin-pud", &value,
    154  1.9     marty 			 sizeof(&value));
    155  1.9     marty 	if (len > 0) {
    156  1.9     marty 		gc->pud = be32toh(value);
    157  1.9     marty 	}
    158  1.9     marty 
    159  1.9     marty 	len = OF_getprop(pc->pc_phandle, "samsung,pin-drv", &value,
    160  1.9     marty 			 sizeof(&value));
    161  1.9     marty 	if (len > 0) {
    162  1.9     marty 		gc->drv = be32toh(value);
    163  1.9     marty 	}
    164  1.9     marty 
    165  1.9     marty 	len = OF_getprop(pc->pc_phandle, "samsung,pin-conpwd", &value,
    166  1.9     marty 			 sizeof(&value));
    167  1.9     marty 	if (len > 0) {
    168  1.9     marty 		gc->conpwd = be32toh(value);
    169  1.9     marty 	}
    170  1.8     marty 
    171  1.9     marty 	len = OF_getprop(pc->pc_phandle, "samsung,pin-pudpwd", &value,
    172  1.9     marty 			 sizeof(&value));
    173  1.9     marty 	if (len > 0) {
    174  1.9     marty 		gc->pudpwd = be32toh(value);
    175  1.9     marty 	}
    176  1.9     marty 	return gc;
    177  1.8     marty }
    178  1.8     marty 
    179  1.9     marty static int
    180  1.9     marty exynos_do_config(struct exynos_pinctrl_config *pc)
    181  1.8     marty {
    182  1.9     marty 	struct exynos_gpio_pin_cfg *gc = pc->pc_pincfg;
    183  1.9     marty 	struct exynos_gpio_bank *bank;
    184  1.9     marty 	int len;
    185  1.9     marty 	char result[20];
    186  1.9     marty 
    187  1.9     marty 	if (gc == NULL) {
    188  1.9     marty 		printf("%s: No configuration available\n", __func__);
    189  1.9     marty 		return -1;
    190  1.9     marty 	}
    191  1.9     marty 
    192  1.9     marty 	len = OF_getprop(pc->pc_phandle, "samsung,pins", result,
    193  1.9     marty 			 sizeof(result));
    194  1.9     marty 	if (len <= 0) {
    195  1.9     marty 		printf("%s: couldn't get pins. (%d)\n", __func__,
    196  1.9     marty 			pc->pc_phandle);
    197  1.9     marty 		return -1;
    198  1.9     marty 	}
    199  1.9     marty 
    200  1.9     marty 	bank = exynos_gpio_bank_lookup(&result[0]);
    201  1.9     marty 	if (!bank) {
    202  1.9     marty 		printf("%s: Couldn't get bank \"%s\".\n", __func__, result);
    203  1.9     marty 		return -1;
    204  1.9     marty 	}
    205  1.8     marty 
    206  1.9     marty 	exynos_gpio_pin_ctl_write(bank, gc);
    207  1.9     marty 	return 0;
    208  1.8     marty }
    209  1.9     marty 
    210  1.9     marty static int
    211  1.9     marty exynos_pinctrl_set_cfg(void *cookie)
    212  1.8     marty {
    213  1.9     marty 	struct exynos_pinctrl_config *pc = cookie;
    214  1.9     marty 	return exynos_do_config(pc);
    215  1.8     marty }
    216