paul@0 | 1 | /* |
paul@31 | 2 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 3 | * |
paul@0 | 4 | * This program is free software; you can redistribute it and/or |
paul@0 | 5 | * modify it under the terms of the GNU General Public License as |
paul@0 | 6 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 7 | * the License, or (at your option) any later version. |
paul@0 | 8 | * |
paul@0 | 9 | * This program is distributed in the hope that it will be useful, |
paul@0 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 12 | * GNU General Public License for more details. |
paul@0 | 13 | * |
paul@0 | 14 | * You should have received a copy of the GNU General Public License |
paul@0 | 15 | * along with this program; if not, write to the Free Software |
paul@0 | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 17 | * Boston, MA 02110-1301, USA |
paul@0 | 18 | */ |
paul@0 | 19 | |
paul@0 | 20 | #pragma once |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/sys/types.h> |
paul@0 | 23 | #include <stdint.h> |
paul@0 | 24 | |
paul@0 | 25 | |
paul@0 | 26 | |
paul@0 | 27 | #ifdef __cplusplus |
paul@0 | 28 | |
paul@0 | 29 | #include <l4/devices/cpm-jz4780.h> |
paul@0 | 30 | #include <l4/devices/hw_mmio_register_block.h> |
paul@0 | 31 | |
paul@0 | 32 | // I2C channel. |
paul@0 | 33 | |
paul@0 | 34 | class I2c_jz4780_channel |
paul@0 | 35 | { |
paul@0 | 36 | private: |
paul@0 | 37 | Hw::Register_block<32> _regs; |
paul@0 | 38 | Cpm_jz4780_chip *_cpm; |
paul@0 | 39 | uint32_t _frequency; |
paul@0 | 40 | unsigned queued, _pos, _reqpos, _total; |
paul@0 | 41 | uint8_t *_buf; |
paul@0 | 42 | int _fail, _stop, _last; |
paul@0 | 43 | |
paul@0 | 44 | public: |
paul@0 | 45 | I2c_jz4780_channel(l4_addr_t start, Cpm_jz4780_chip *cpm, |
paul@0 | 46 | uint32_t frequency); |
paul@0 | 47 | |
paul@0 | 48 | void set_target(uint8_t addr); |
paul@0 | 49 | void start_read(uint8_t buf[], unsigned total); |
paul@0 | 50 | void read(); |
paul@0 | 51 | int read_done(); |
paul@0 | 52 | unsigned have_read(); |
paul@0 | 53 | int read_incomplete(); |
paul@0 | 54 | void write(uint8_t buf[], unsigned total); |
paul@0 | 55 | void stop(); |
paul@0 | 56 | |
paul@0 | 57 | private: |
paul@0 | 58 | void disable(); |
paul@0 | 59 | void enable(); |
paul@0 | 60 | |
paul@0 | 61 | int active(); |
paul@0 | 62 | int have_input(); |
paul@0 | 63 | int have_output(); |
paul@0 | 64 | int can_send(); |
paul@0 | 65 | int read_failed(); |
paul@0 | 66 | int write_failed(); |
paul@0 | 67 | |
paul@0 | 68 | void reset_flags(); |
paul@0 | 69 | void init_parameters(); |
paul@0 | 70 | void set_frequency(); |
paul@0 | 71 | |
paul@0 | 72 | void set_read_threshold(); |
paul@0 | 73 | void queue_reads(); |
paul@0 | 74 | void queue_writes(uint8_t buf[], unsigned *pos, unsigned total); |
paul@0 | 75 | void store_reads(); |
paul@0 | 76 | }; |
paul@0 | 77 | |
paul@0 | 78 | // I2C device control. |
paul@0 | 79 | |
paul@0 | 80 | class I2c_jz4780_chip |
paul@0 | 81 | { |
paul@0 | 82 | private: |
paul@0 | 83 | l4_addr_t _start, _end; |
paul@0 | 84 | Cpm_jz4780_chip *_cpm; |
paul@0 | 85 | uint32_t _frequency; |
paul@0 | 86 | |
paul@0 | 87 | public: |
paul@0 | 88 | I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm, |
paul@0 | 89 | uint32_t frequency); |
paul@0 | 90 | |
paul@0 | 91 | I2c_jz4780_channel *get_channel(uint8_t channel); |
paul@0 | 92 | }; |
paul@0 | 93 | |
paul@0 | 94 | #endif /* __cplusplus */ |
paul@0 | 95 | |
paul@0 | 96 | |
paul@0 | 97 | |
paul@0 | 98 | /* C language interface. */ |
paul@0 | 99 | |
paul@0 | 100 | EXTERN_C_BEGIN |
paul@0 | 101 | |
paul@0 | 102 | void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, |
paul@0 | 103 | uint32_t frequency); |
paul@0 | 104 | |
paul@0 | 105 | void *jz4780_i2c_get_channel(void *i2c, uint8_t channel); |
paul@0 | 106 | |
paul@0 | 107 | void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr); |
paul@0 | 108 | void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned total); |
paul@0 | 109 | void jz4780_i2c_read(void *i2c_channel); |
paul@0 | 110 | int jz4780_i2c_read_done(void *i2c_channel); |
paul@0 | 111 | unsigned jz4780_i2c_have_read(void *i2c_channel); |
paul@0 | 112 | int jz4780_i2c_read_incomplete(void *i2c_channel); |
paul@0 | 113 | void jz4780_i2c_write(void *i2c_channel, uint8_t buf[], unsigned total); |
paul@0 | 114 | void jz4780_i2c_stop(void *i2c_channel); |
paul@0 | 115 | |
paul@0 | 116 | EXTERN_C_END |