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# 276b34b16eSOleksandr Tymoshenko 286b34b16eSOleksandr Tymoshenko#include <sys/bus.h> 296b34b16eSOleksandr Tymoshenko#include <sys/gpio.h> 306b34b16eSOleksandr Tymoshenko 316b34b16eSOleksandr TymoshenkoINTERFACE gpio; 326b34b16eSOleksandr Tymoshenko 338d250595SLuiz Otavio O SouzaCODE { 342b8b8321SLuiz Otavio O Souza static device_t 352b8b8321SLuiz Otavio O Souza gpio_default_get_bus(void) 362b8b8321SLuiz Otavio O Souza { 372b8b8321SLuiz Otavio O Souza 382b8b8321SLuiz Otavio O Souza return (NULL); 392b8b8321SLuiz Otavio O Souza } 402b8b8321SLuiz Otavio O Souza 41edd14576SLuiz Otavio O Souza static int 42e1275c68SIan Lepore gpio_default_nosupport(void) 43e1275c68SIan Lepore { 44e1275c68SIan Lepore 45e1275c68SIan Lepore return (EOPNOTSUPP); 46e1275c68SIan Lepore } 47e1275c68SIan Lepore 48e1275c68SIan Lepore static int 498d250595SLuiz Otavio O Souza gpio_default_map_gpios(device_t bus, phandle_t dev, 508d250595SLuiz Otavio O Souza phandle_t gparent, int gcells, pcell_t *gpios, uint32_t *pin, 518d250595SLuiz Otavio O Souza uint32_t *flags) 528d250595SLuiz Otavio O Souza { 538d250595SLuiz Otavio O Souza /* Propagate up the bus hierarchy until someone handles it. */ 548d250595SLuiz Otavio O Souza if (device_get_parent(bus) != NULL) 558d250595SLuiz Otavio O Souza return (GPIO_MAP_GPIOS(device_get_parent(bus), dev, 568d250595SLuiz Otavio O Souza gparent, gcells, gpios, pin, flags)); 578d250595SLuiz Otavio O Souza 588d250595SLuiz Otavio O Souza /* If that fails, then assume the FreeBSD defaults. */ 598d250595SLuiz Otavio O Souza *pin = gpios[0]; 608d250595SLuiz Otavio O Souza if (gcells == 2 || gcells == 3) 618d250595SLuiz Otavio O Souza *flags = gpios[gcells - 1]; 628d250595SLuiz Otavio O Souza 638d250595SLuiz Otavio O Souza return (0); 648d250595SLuiz Otavio O Souza } 658d250595SLuiz Otavio O Souza}; 668d250595SLuiz Otavio O Souza 678d250595SLuiz Otavio O SouzaHEADER { 688d250595SLuiz Otavio O Souza #include <dev/ofw/openfirm.h> 698d250595SLuiz Otavio O Souza}; 708d250595SLuiz Otavio O Souza 716b34b16eSOleksandr Tymoshenko# 722b8b8321SLuiz Otavio O Souza# Return the gpiobus device reference 732b8b8321SLuiz Otavio O Souza# 742b8b8321SLuiz Otavio O SouzaMETHOD device_t get_bus { 752b8b8321SLuiz Otavio O Souza device_t dev; 762b8b8321SLuiz Otavio O Souza} DEFAULT gpio_default_get_bus; 772b8b8321SLuiz Otavio O Souza 782b8b8321SLuiz Otavio O Souza# 790bac52c1SLuiz Otavio O Souza# Get maximum pin number 806b34b16eSOleksandr Tymoshenko# 816b34b16eSOleksandr TymoshenkoMETHOD int pin_max { 826b34b16eSOleksandr Tymoshenko device_t dev; 830bac52c1SLuiz Otavio O Souza int *maxpin; 846b34b16eSOleksandr Tymoshenko}; 856b34b16eSOleksandr Tymoshenko 866b34b16eSOleksandr Tymoshenko# 87*04389c85SGordon Bergling# Set value of pin specified by pin_num 886b34b16eSOleksandr Tymoshenko# 896b34b16eSOleksandr TymoshenkoMETHOD int pin_set { 906b34b16eSOleksandr Tymoshenko device_t dev; 916b34b16eSOleksandr Tymoshenko uint32_t pin_num; 926b34b16eSOleksandr Tymoshenko uint32_t pin_value; 936b34b16eSOleksandr Tymoshenko}; 946b34b16eSOleksandr Tymoshenko 956b34b16eSOleksandr Tymoshenko# 96*04389c85SGordon Bergling# Get value of pin specified by pin_num 976b34b16eSOleksandr Tymoshenko# 986b34b16eSOleksandr TymoshenkoMETHOD int pin_get { 996b34b16eSOleksandr Tymoshenko device_t dev; 1006b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1016b34b16eSOleksandr Tymoshenko uint32_t *pin_value; 1026b34b16eSOleksandr Tymoshenko}; 1036b34b16eSOleksandr Tymoshenko 1046b34b16eSOleksandr Tymoshenko# 105*04389c85SGordon Bergling# Toggle value of pin specified by pin_num 1066b34b16eSOleksandr Tymoshenko# 1076b34b16eSOleksandr TymoshenkoMETHOD int pin_toggle { 1086b34b16eSOleksandr Tymoshenko device_t dev; 1096b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1106b34b16eSOleksandr Tymoshenko}; 1116b34b16eSOleksandr Tymoshenko 1126b34b16eSOleksandr Tymoshenko# 1136b34b16eSOleksandr Tymoshenko# Get pin capabilities 1146b34b16eSOleksandr Tymoshenko# 1156b34b16eSOleksandr TymoshenkoMETHOD int pin_getcaps { 1166b34b16eSOleksandr Tymoshenko device_t dev; 1176b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1186b34b16eSOleksandr Tymoshenko uint32_t *caps; 1196b34b16eSOleksandr Tymoshenko}; 1206b34b16eSOleksandr Tymoshenko 1216b34b16eSOleksandr Tymoshenko# 1226b34b16eSOleksandr Tymoshenko# Get pin flags 1236b34b16eSOleksandr Tymoshenko# 1246b34b16eSOleksandr TymoshenkoMETHOD int pin_getflags { 1256b34b16eSOleksandr Tymoshenko device_t dev; 1266b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1276b34b16eSOleksandr Tymoshenko uint32_t *flags; 1286b34b16eSOleksandr Tymoshenko}; 1296b34b16eSOleksandr Tymoshenko 1306b34b16eSOleksandr Tymoshenko# 1316b34b16eSOleksandr Tymoshenko# Get pin name 1326b34b16eSOleksandr Tymoshenko# 1336b34b16eSOleksandr TymoshenkoMETHOD int pin_getname { 1346b34b16eSOleksandr Tymoshenko device_t dev; 1356b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1366b34b16eSOleksandr Tymoshenko char *name; 1376b34b16eSOleksandr Tymoshenko}; 1386b34b16eSOleksandr Tymoshenko 1396b34b16eSOleksandr Tymoshenko# 1406b34b16eSOleksandr Tymoshenko# Set current configuration and capabilities 1416b34b16eSOleksandr Tymoshenko# 1426b34b16eSOleksandr TymoshenkoMETHOD int pin_setflags { 1436b34b16eSOleksandr Tymoshenko device_t dev; 1446b34b16eSOleksandr Tymoshenko uint32_t pin_num; 1456b34b16eSOleksandr Tymoshenko uint32_t flags; 1466b34b16eSOleksandr Tymoshenko}; 1478d250595SLuiz Otavio O Souza 1488d250595SLuiz Otavio O Souza# 1498d250595SLuiz Otavio O Souza# Allow the GPIO controller to map the gpio-specifier on its own. 1508d250595SLuiz Otavio O Souza# 1518d250595SLuiz Otavio O SouzaMETHOD int map_gpios { 1528d250595SLuiz Otavio O Souza device_t bus; 1538d250595SLuiz Otavio O Souza phandle_t dev; 1548d250595SLuiz Otavio O Souza phandle_t gparent; 1558d250595SLuiz Otavio O Souza int gcells; 1568d250595SLuiz Otavio O Souza pcell_t *gpios; 1578d250595SLuiz Otavio O Souza uint32_t *pin; 1588d250595SLuiz Otavio O Souza uint32_t *flags; 1598d250595SLuiz Otavio O Souza} DEFAULT gpio_default_map_gpios; 160e1275c68SIan Lepore 161e1275c68SIan Lepore# 162e1275c68SIan Lepore# Simultaneously read and/or change up to 32 adjacent pins. 163e1275c68SIan Lepore# If the device cannot change the pins simultaneously, returns EOPNOTSUPP. 164e1275c68SIan Lepore# 165e1275c68SIan Lepore# More details about using this interface can be found in sys/gpio.h 166e1275c68SIan Lepore# 167e1275c68SIan LeporeMETHOD int pin_access_32 { 168e1275c68SIan Lepore device_t dev; 169e1275c68SIan Lepore uint32_t first_pin; 170e1275c68SIan Lepore uint32_t clear_pins; 171e1275c68SIan Lepore uint32_t change_pins; 172e1275c68SIan Lepore uint32_t *orig_pins; 173e1275c68SIan Lepore} DEFAULT gpio_default_nosupport; 174e1275c68SIan Lepore 175e1275c68SIan Lepore# 176e1275c68SIan Lepore# Simultaneously configure up to 32 adjacent pins. 177e1275c68SIan Lepore# This is intended to change the configuration of all the pins simultaneously, 178e1275c68SIan Lepore# but unlike pin_access_32, this will not fail if the hardware can't do so. 179e1275c68SIan Lepore# 180e1275c68SIan Lepore# More details about using this interface can be found in sys/gpio.h 181e1275c68SIan Lepore# 182e1275c68SIan LeporeMETHOD int pin_config_32 { 183e1275c68SIan Lepore device_t dev; 184e1275c68SIan Lepore uint32_t first_pin; 185e1275c68SIan Lepore uint32_t num_pins; 186e1275c68SIan Lepore uint32_t *pin_flags; 187e1275c68SIan Lepore} DEFAULT gpio_default_nosupport; 188