1 /* 2 * GPIO driver for Ingenic JZ4780. 3 * (See below for additional copyright and licensing notices.) 4 * 5 * Copyright (C) 2017, 2018, 2023, 2024 Paul Boddie <paul@boddie.org.uk> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation; either version 2 of 10 * the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 * Boston, MA 02110-1301, USA 21 * 22 * 23 * Subject to other copyrights, being derived from the bcm2835.cc and 24 * omap.cc GPIO driver implementations. 25 * 26 * This file is part of TUD:OS and distributed under the terms of the 27 * GNU General Public License 2. 28 * Please see the COPYING-GPL-2 file for details. 29 */ 30 31 #pragma once 32 33 #include <l4/sys/types.h> 34 #include <stdint.h> 35 #include "gpio-generic.h" 36 37 38 39 #ifdef __cplusplus 40 41 #include <l4/devices/hw_mmio_register_block.h> 42 43 // GPIO device control. 44 45 class Gpio_jz4780_irq_pin : public Hw::Gpio_irq_pin 46 { 47 unsigned _pin; 48 Hw::Register_block<32> _regs; 49 50 // Convenience method for obtaining the bit corresponding to a pin. 51 52 l4_uint32_t _pin_bit(unsigned pin) 53 { return 1 << (pin & 31); } 54 55 void write_reg_pin(unsigned reg); 56 57 public: 58 Gpio_jz4780_irq_pin(unsigned pin, Hw::Register_block<32> const ®s); 59 60 void do_mask(); 61 void do_unmask(); 62 bool do_set_mode(unsigned mode); 63 int clear(); 64 bool enabled(); 65 }; 66 67 class Gpio_jz4780_chip : public Hw::Gpio_chip 68 { 69 private: 70 Hw::Register_block<32> _regs; 71 72 l4_addr_t _start; 73 struct gpio_port *_pull_config; 74 75 public: 76 explicit Gpio_jz4780_chip(l4_addr_t start, uint8_t port_number); 77 78 // Configuration methods. 79 80 void setup(unsigned pin, unsigned mode, int value = 0); 81 void config_pull(unsigned pin, unsigned mode); 82 void config_pad(unsigned pin, unsigned func, unsigned value); 83 void config_get(unsigned pin, unsigned reg, unsigned *value); 84 void config_pad_get(unsigned pin, unsigned *func, unsigned *value); 85 86 // Multiple pin configuration methods. 87 88 void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalues = 0); 89 void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value = 0); 90 void multi_set(Pin_slice const &mask, unsigned data); 91 unsigned multi_get(unsigned offset); 92 93 // IRQ pin configuration. 94 95 Hw::Gpio_irq_pin *get_irq(unsigned pin); 96 97 // Pin/port data methods. 98 99 int get(unsigned pin); 100 void set(unsigned pin, int value); 101 102 private: 103 void config(unsigned pin, unsigned mode); 104 }; 105 106 Hw::Gpio_chip *jz4780_gpio_chip(l4_addr_t start, uint8_t port_number, 107 bool shadow = false); 108 109 #endif /* __cplusplus */ 110 111 112 113 /* C language interface. */ 114 115 EXTERN_C_BEGIN 116 117 void *jz4780_gpio_init(l4_addr_t start, uint8_t port_number); 118 119 void jz4780_gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); 120 void jz4780_gpio_config_pull(void *gpio, unsigned pin, unsigned mode); 121 void jz4780_gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); 122 void jz4780_gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); 123 void jz4780_gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); 124 125 void jz4780_gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); 126 void jz4780_gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); 127 void jz4780_gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); 128 unsigned jz4780_gpio_multi_get(void *gpio, unsigned offset); 129 130 int jz4780_gpio_get(void *gpio, unsigned pin); 131 void jz4780_gpio_set(void *gpio, unsigned pin, int value); 132 133 void *jz4780_gpio_get_irq(void *gpio, unsigned pin); 134 bool jz4780_gpio_irq_set_mode(void *gpio_irq, unsigned mode); 135 136 EXTERN_C_END