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