1 /* 2 * GPIO driver for Ingenic JZ4740. 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_jz4740_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_jz4740_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_jz4740_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 // General configuration register updates. 76 77 void _config(unsigned bitmap, unsigned mode); 78 79 // General pull-up register updates. 80 81 void _config_pull(unsigned bitmap, unsigned mode); 82 83 // General pad register updates. 84 85 void _config_pad(unsigned bitmap, unsigned func, unsigned value); 86 87 public: 88 explicit Gpio_jz4740_chip(l4_addr_t start, uint8_t port_number); 89 90 // Configuration methods. 91 92 void setup(unsigned pin, unsigned mode, int value = 0); 93 void config_pull(unsigned pin, unsigned mode); 94 void config_pad(unsigned pin, unsigned func, unsigned value); 95 void config_get(unsigned pin, unsigned reg, unsigned *value); 96 void config_pad_get(unsigned pin, unsigned *func, unsigned *value); 97 98 // Multiple pin configuration methods. 99 100 void multi_setup(Pin_slice const &mask, unsigned mode, unsigned outvalues = 0); 101 void multi_config_pull(Pin_slice const &mask, unsigned mode); 102 void multi_config_pad(Pin_slice const &mask, unsigned func, unsigned value = 0); 103 void multi_set(Pin_slice const &mask, unsigned data); 104 unsigned multi_get(unsigned offset); 105 106 // IRQ pin configuration. 107 108 Hw::Gpio_irq_pin *get_irq(unsigned pin); 109 110 // Pin/port data methods. 111 112 int get(unsigned pin); 113 void set(unsigned pin, int value); 114 115 private: 116 void config(unsigned pin, unsigned mode); 117 }; 118 119 Hw::Gpio_chip *jz4740_gpio_chip(l4_addr_t start, uint8_t port_number, 120 bool shadow = false); 121 122 #endif /* __cplusplus */ 123 124 125 126 /* C language interface. */ 127 128 EXTERN_C_BEGIN 129 130 void *jz4740_gpio_init(l4_addr_t start, uint8_t port_number); 131 132 void jz4740_gpio_setup(void *gpio, unsigned pin, unsigned mode, int value); 133 void jz4740_gpio_config_pull(void *gpio, unsigned pin, unsigned mode); 134 void jz4740_gpio_config_pad(void *gpio, unsigned pin, unsigned func, unsigned value); 135 void jz4740_gpio_config_get(void *gpio, unsigned pin, unsigned reg, unsigned *value); 136 void jz4740_gpio_config_pad_get(void *gpio, unsigned pin, unsigned *func, unsigned *value); 137 138 void jz4740_gpio_multi_setup(void *gpio, Pin_slice const *mask, unsigned mode, unsigned outvalues); 139 void jz4740_gpio_multi_config_pull(void *gpio, Pin_slice const *mask, unsigned mode); 140 void jz4740_gpio_multi_config_pad(void *gpio, Pin_slice const *mask, unsigned func, unsigned value); 141 void jz4740_gpio_multi_set(void *gpio, Pin_slice const *mask, unsigned data); 142 unsigned jz4740_gpio_multi_get(void *gpio, unsigned offset); 143 144 int jz4740_gpio_get(void *gpio, unsigned pin); 145 void jz4740_gpio_set(void *gpio, unsigned pin, int value); 146 147 void *jz4740_gpio_get_irq(void *gpio, unsigned pin); 148 bool jz4740_gpio_irq_set_mode(void *gpio_irq, unsigned mode); 149 150 EXTERN_C_END