1 /* 2 * GPIO driver for Ingenic JZ4780. 3 * (See below for additional copyright and licensing notices.) 4 * 5 * Copyright (C) 2017, 2018, 2023 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/err.h> 34 #include <l4/sys/types.h> 35 #include <stdint.h> 36 #include "gpio.h" 37 38 39 40 #ifdef __cplusplus 41 42 #include <l4/devices/hw_mmio_register_block.h> 43 44 // GPIO device control. 45 46 class Gpio_jz4780_irq_pin : public Hw::Gpio_irq_pin 47 { 48 unsigned _pin; 49 Hw::Register_block<32> _regs; 50 51 // Convenience method for obtaining the bit corresponding to a pin. 52 53 l4_uint32_t _pin_bit(unsigned pin) 54 { return 1 << (pin & 31); } 55 56 void write_reg_pin(unsigned reg); 57 58 public: 59 Gpio_jz4780_irq_pin(unsigned pin, Hw::Register_block<32> const ®s); 60 61 void do_mask(); 62 void do_unmask(); 63 bool do_set_mode(unsigned mode); 64 int clear(); 65 bool enabled(); 66 }; 67 68 class Gpio_jz4780_chip : public Hw::Gpio_chip 69 { 70 private: 71 Hw::Register_block<32> _regs; 72 73 l4_addr_t _start; 74 unsigned _nr_pins; 75 struct gpio_port *_pull_config; 76 77 // Convenience method for obtaining the bit corresponding to a pin. 78 79 l4_uint32_t _pin_bit(unsigned pin) 80 { return 1 << (pin & 31); } 81 82 // Convenience method for obtaining the bit position of a pin. 83 84 unsigned _pin_shift(unsigned pin) 85 { return pin % 32; } 86 87 // Permit only "aligned" accesses to registers. 88 89 unsigned _reg_offset_check(unsigned pin_offset) const 90 { 91 switch (pin_offset) 92 { 93 case 0: 94 return 0; 95 96 default: 97 throw -L4_EINVAL; 98 } 99 } 100 101 public: 102 Gpio_jz4780_chip(l4_addr_t start, uint8_t port_number); 103 104 // Obtain the number of pins. 105 106 unsigned nr_pins() const { return _nr_pins; } 107 108 // Unnecessary operations. 109 110 void request(unsigned) {} 111 void free(unsigned) {} 112 113 // Configuration methods. 114 115 void setup(unsigned pin, unsigned mode, int value = 0); 116 void config_pull(unsigned pin, unsigned mode); 117 void config_pad(unsigned pin, unsigned func, unsigned value); 118 void config_get(unsigned pin, unsigned reg, unsigned *value); 119 void config_pad_get(unsigned pin, unsigned *func, unsigned *value); 120 121 // Multiple pin configuration methods. 122 123 void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalues = 0); 124 void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value = 0); 125 void multi_set(Pin_slice const &mask, unsigned data); 126 unsigned multi_get(unsigned offset); 127 128 // IRQ pin configuration. 129 130 Hw::Gpio_irq_pin *get_irq(unsigned pin); 131 132 // Pin/port data methods. 133 134 int get(unsigned pin); 135 void set(unsigned pin, int value); 136 137 private: 138 void config(unsigned pin, unsigned mode); 139 }; 140 141 #endif /* __cplusplus */ 142 143 144 145 /* C language interface. */ 146 147 EXTERN_C_BEGIN 148 149 void *jz4780_gpio_init(l4_addr_t start, uint8_t port_number); 150 151 void jz4780_gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); 152 void jz4780_gpio_config_pull(void *gpio, unsigned pin, unsigned mode); 153 void jz4780_gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); 154 void jz4780_gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); 155 void jz4780_gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); 156 157 void jz4780_gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); 158 void jz4780_gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); 159 void jz4780_gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); 160 unsigned jz4780_gpio_multi_get(void *gpio, unsigned offset); 161 162 int jz4780_gpio_get(void *gpio, unsigned pin); 163 void jz4780_gpio_set(void *gpio, unsigned pin, int value); 164 165 void *jz4780_gpio_get_irq(void *gpio, unsigned pin); 166 bool jz4780_gpio_irq_set_mode(void *gpio_irq, unsigned mode); 167 168 EXTERN_C_END