1 /* 2 * GPIO driver for Ingenic JZ4730. 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_jz4730_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 // Convenience methods for clearing and setting bits in the absence of 58 // dedicated registers. 59 60 void clear_reg_pin(unsigned reg); 61 void set_reg_pin(unsigned reg); 62 void clear_reg_pins(uint32_t reg_upper, uint32_t reg_lower); 63 void set_reg_pins(uint32_t reg_upper, uint32_t reg_lower, uint8_t value); 64 65 public: 66 Gpio_jz4730_irq_pin(unsigned pin, Hw::Register_block<32> const ®s); 67 68 void do_mask(); 69 void do_unmask(); 70 bool do_set_mode(unsigned mode); 71 int clear(); 72 bool enabled(); 73 }; 74 75 class Gpio_jz4730_chip : public Hw::Gpio_chip 76 { 77 private: 78 Hw::Register_block<32> _regs; 79 80 l4_addr_t _start; 81 struct gpio_port *_pull_config; 82 83 // General configuration register updates. 84 85 void _config(unsigned bitmap, unsigned mode); 86 87 // General pull-up register updates. 88 89 void _config_pull(unsigned bitmap, unsigned mode); 90 91 // General pad register updates. 92 93 void _config_pad(unsigned bitmap, unsigned func, unsigned value); 94 95 // Paired register field access. 96 97 void _get_pin_value(unsigned pin, uint32_t reg_upper, uint32_t reg_lower, 98 unsigned *value); 99 100 public: 101 explicit Gpio_jz4730_chip(l4_addr_t start, uint8_t port_number); 102 103 // Configuration methods. 104 105 void setup(unsigned pin, unsigned mode, int value = 0); 106 void config_pull(unsigned pin, unsigned mode); 107 void config_pad(unsigned pin, unsigned func, unsigned value); 108 void config_get(unsigned pin, unsigned reg, unsigned *value); 109 void config_pad_get(unsigned pin, unsigned *func, unsigned *value); 110 111 // Multiple pin configuration methods. 112 113 void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalues = 0); 114 void multi_config_pull(Pin_slice const &mask, unsigned mode); 115 void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value = 0); 116 void multi_set(Pin_slice const &mask, unsigned data); 117 unsigned multi_get(unsigned offset); 118 119 // IRQ pin configuration. 120 121 Hw::Gpio_irq_pin *get_irq(unsigned pin); 122 123 // Pin/port data methods. 124 125 int get(unsigned pin); 126 void set(unsigned pin, int value); 127 128 private: 129 void config(unsigned pin, unsigned mode); 130 }; 131 132 Hw::Gpio_chip *jz4730_gpio_chip(l4_addr_t start, uint8_t port_number, 133 bool shadow = false); 134 135 #endif /* __cplusplus */ 136 137 138 139 /* C language interface. */ 140 141 EXTERN_C_BEGIN 142 143 void *jz4730_gpio_init(l4_addr_t start, uint8_t port_number); 144 145 void jz4730_gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); 146 void jz4730_gpio_config_pull(void *gpio, unsigned pin, unsigned mode); 147 void jz4730_gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); 148 void jz4730_gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); 149 void jz4730_gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); 150 151 void jz4730_gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); 152 void jz4730_gpio_multi_config_pull(void *gpio, Pin_slice const *mask, unsigned mode); 153 void jz4730_gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); 154 void jz4730_gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); 155 unsigned jz4730_gpio_multi_get(void *gpio, unsigned offset); 156 157 int jz4730_gpio_get(void *gpio, unsigned pin); 158 void jz4730_gpio_set(void *gpio, unsigned pin, int value); 159 160 void *jz4730_gpio_get_irq(void *gpio, unsigned pin); 161 bool jz4730_gpio_irq_set_mode(void *gpio_irq, unsigned mode); 162 163 EXTERN_C_END