16b34b16eSOleksandr Tymoshenko#- 26b34b16eSOleksandr Tymoshenko# Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org> 36b34b16eSOleksandr Tymoshenko# All rights reserved. 46b34b16eSOleksandr Tymoshenko# 56b34b16eSOleksandr Tymoshenko# Redistribution and use in source and binary forms, with or without 66b34b16eSOleksandr Tymoshenko# modification, are permitted provided that the following conditions 76b34b16eSOleksandr Tymoshenko# are met: 86b34b16eSOleksandr Tymoshenko# 1. Redistributions of source code must retain the above copyright 96b34b16eSOleksandr Tymoshenko# notice, this list of conditions and the following disclaimer. 106b34b16eSOleksandr Tymoshenko# 2. Redistributions in binary form must reproduce the above copyright 116b34b16eSOleksandr Tymoshenko# notice, this list of conditions and the following disclaimer in the 126b34b16eSOleksandr Tymoshenko# documentation and/or other materials provided with the distribution. 136b34b16eSOleksandr Tymoshenko# 146b34b16eSOleksandr Tymoshenko# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 156b34b16eSOleksandr Tymoshenko# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 166b34b16eSOleksandr Tymoshenko# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 176b34b16eSOleksandr Tymoshenko# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 186b34b16eSOleksandr Tymoshenko# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 196b34b16eSOleksandr Tymoshenko# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 206b34b16eSOleksandr Tymoshenko# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 216b34b16eSOleksandr Tymoshenko# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 226b34b16eSOleksandr Tymoshenko# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 236b34b16eSOleksandr Tymoshenko# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 246b34b16eSOleksandr Tymoshenko# SUCH DAMAGE. 256b34b16eSOleksandr Tymoshenko# 266b34b16eSOleksandr Tymoshenko# $FreeBSD$ 276b34b16eSOleksandr Tymoshenko# 286b34b16eSOleksandr Tymoshenko 296b34b16eSOleksandr Tymoshenko#include <sys/bus.h> 306b34b16eSOleksandr Tymoshenko#include <sys/gpio.h> 316b34b16eSOleksandr Tymoshenko 326b34b16eSOleksandr TymoshenkoINTERFACE gpio; 336b34b16eSOleksandr Tymoshenko 348d250595SLuiz Otavio O SouzaCODE { 35*2b8b8321SLuiz Otavio O Souza static device_t 36*2b8b8321SLuiz Otavio O Souza gpio_default_get_bus(void) 37*2b8b8321SLuiz Otavio O Souza { 38*2b8b8321SLuiz Otavio O Souza 39*2b8b8321SLuiz Otavio O Souza return (NULL); 40*2b8b8321SLuiz Otavio O Souza } 41*2b8b8321SLuiz Otavio O Souza 42edd14576SLuiz Otavio O Souza static int 438d250595SLuiz Otavio O Souza gpio_default_map_gpios(device_t bus, phandle_t dev, 448d250595SLuiz Otavio O Souza phandle_t gparent, int gcells, pcell_t *gpios, uint32_t *pin, 458d250595SLuiz Otavio O Souza uint32_t *flags) 468d250595SLuiz Otavio O Souza { 478d250595SLuiz Otavio O Souza /* Propagate up the bus hierarchy until someone handles it. */ 488d250595SLuiz Otavio O Souza if (device_get_parent(bus) != NULL) 498d250595SLuiz Otavio O Souza return (GPIO_MAP_GPIOS(device_get_parent(bus), dev, 508d250595SLuiz Otavio O Souza gparent, gcells, gpios, pin, flags)); 518d250595SLuiz Otavio O Souza 528d250595SLuiz Otavio O Souza /* If that fails, then assume the FreeBSD defaults. */ 538d250595SLuiz Otavio O Souza *pin = gpios[0]; 548d250595SLuiz Otavio O Souza if (gcells == 2 || gcells == 3) 558d250595SLuiz Otavio O Souza *flags = gpios[gcells - 1]; 568d250595SLuiz Otavio O Souza 578d250595SLuiz Otavio O Souza return (0); 588d250595SLuiz Otavio O Souza } 598d250595SLuiz Otavio O Souza}; 608d250595SLuiz Otavio O Souza 618d250595SLuiz Otavio O SouzaHEADER { 628d250595SLuiz Otavio O Souza #include <dev/ofw/openfirm.h> 638d250595SLuiz Otavio O Souza}; 648d250595SLuiz Otavio O Souza 656b34b16eSOleksandr Tymoshenko# 66*2b8b8321SLuiz Otavio O Souza# Return the gpiobus device reference 67*2b8b8321SLuiz Otavio O Souza# 68*2b8b8321SLuiz Otavio O SouzaMETHOD device_t get_bus { 69*2b8b8321SLuiz Otavio O Souza device_t dev; 70*2b8b8321SLuiz Otavio O Souza} DEFAULT gpio_default_get_bus; 71*2b8b8321SLuiz Otavio O Souza 72*2b8b8321SLuiz Otavio O Souza# 730bac52c1SLuiz Otavio O Souza# Get maximum pin number 746b34b16eSOleksandr Tymoshenko# 756b34b16eSOleksandr TymoshenkoMETHOD int pin_max { 766b34b16eSOleksandr Tymoshenko device_t dev; 770bac52c1SLuiz Otavio O Souza int *maxpin; 786b34b16eSOleksandr Tymoshenko}; 796b34b16eSOleksandr Tymoshenko 806b34b16eSOleksandr Tymoshenko# 816b34b16eSOleksandr Tymoshenko# Set value of pin specifed by pin_num 826b34b16eSOleksandr Tymoshenko# 836b34b16eSOleksandr TymoshenkoMETHOD int pin_set { 846b34b16eSOleksandr Tymoshenko device_t dev; 856b34b16eSOleksandr Tymoshenko uint32_t pin_num; 866b34b16eSOleksandr Tymoshenko uint32_t pin_value; 876b34b16eSOleksandr Tymoshenko}; 886b34b16eSOleksandr Tymoshenko 896b34b16eSOleksandr Tymoshenko# 906b34b16eSOleksandr Tymoshenko# Get value of pin specifed by pin_num 916b34b16eSOleksandr Tymoshenko# 926b34b16eSOleksandr TymoshenkoMETHOD int pin_get { 936b34b16eSOleksandr Tymoshenko device_t dev; 946b34b16eSOleksandr Tymoshenko uint32_t pin_num; 956b34b16eSOleksandr Tymoshenko uint32_t *pin_value; 966b34b16eSOleksandr Tymoshenko}; 976b34b16eSOleksandr Tymoshenko 986b34b16eSOleksandr Tymoshenko# 996b34b16eSOleksandr Tymoshenko# Toggle value of pin specifed by pin_num 1006b34b16eSOleksandr Tymoshenko# 1016b34b16eSOleksandr TymoshenkoMETHOD int pin_toggle { 1026b34b16eSOleksandr Tymoshenko device_t dev; 1036b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1046b34b16eSOleksandr Tymoshenko}; 1056b34b16eSOleksandr Tymoshenko 1066b34b16eSOleksandr Tymoshenko# 1076b34b16eSOleksandr Tymoshenko# Get pin capabilities 1086b34b16eSOleksandr Tymoshenko# 1096b34b16eSOleksandr TymoshenkoMETHOD int pin_getcaps { 1106b34b16eSOleksandr Tymoshenko device_t dev; 1116b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1126b34b16eSOleksandr Tymoshenko uint32_t *caps; 1136b34b16eSOleksandr Tymoshenko}; 1146b34b16eSOleksandr Tymoshenko 1156b34b16eSOleksandr Tymoshenko# 1166b34b16eSOleksandr Tymoshenko# Get pin flags 1176b34b16eSOleksandr Tymoshenko# 1186b34b16eSOleksandr TymoshenkoMETHOD int pin_getflags { 1196b34b16eSOleksandr Tymoshenko device_t dev; 1206b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1216b34b16eSOleksandr Tymoshenko uint32_t *flags; 1226b34b16eSOleksandr Tymoshenko}; 1236b34b16eSOleksandr Tymoshenko 1246b34b16eSOleksandr Tymoshenko# 1256b34b16eSOleksandr Tymoshenko# Get pin name 1266b34b16eSOleksandr Tymoshenko# 1276b34b16eSOleksandr TymoshenkoMETHOD int pin_getname { 1286b34b16eSOleksandr Tymoshenko device_t dev; 1296b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1306b34b16eSOleksandr Tymoshenko char *name; 1316b34b16eSOleksandr Tymoshenko}; 1326b34b16eSOleksandr Tymoshenko 1336b34b16eSOleksandr Tymoshenko# 1346b34b16eSOleksandr Tymoshenko# Set current configuration and capabilities 1356b34b16eSOleksandr Tymoshenko# 1366b34b16eSOleksandr TymoshenkoMETHOD int pin_setflags { 1376b34b16eSOleksandr Tymoshenko device_t dev; 1386b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1396b34b16eSOleksandr Tymoshenko uint32_t flags; 1406b34b16eSOleksandr Tymoshenko}; 1418d250595SLuiz Otavio O Souza 1428d250595SLuiz Otavio O Souza# 1438d250595SLuiz Otavio O Souza# Allow the GPIO controller to map the gpio-specifier on its own. 1448d250595SLuiz Otavio O Souza# 1458d250595SLuiz Otavio O SouzaMETHOD int map_gpios { 1468d250595SLuiz Otavio O Souza device_t bus; 1478d250595SLuiz Otavio O Souza phandle_t dev; 1488d250595SLuiz Otavio O Souza phandle_t gparent; 1498d250595SLuiz Otavio O Souza int gcells; 1508d250595SLuiz Otavio O Souza pcell_t *gpios; 1518d250595SLuiz Otavio O Souza uint32_t *pin; 1528d250595SLuiz Otavio O Souza uint32_t *flags; 1538d250595SLuiz Otavio O Souza} DEFAULT gpio_default_map_gpios; 154