paul@114 | 1 | /* |
paul@114 | 2 | * I2C support for the JZ4730. |
paul@114 | 3 | * |
paul@114 | 4 | * Copyright (C) 2017, 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk> |
paul@114 | 5 | * |
paul@114 | 6 | * This program is free software; you can redistribute it and/or |
paul@114 | 7 | * modify it under the terms of the GNU General Public License as |
paul@114 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@114 | 9 | * the License, or (at your option) any later version. |
paul@114 | 10 | * |
paul@114 | 11 | * This program is distributed in the hope that it will be useful, |
paul@114 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@114 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@114 | 14 | * GNU General Public License for more details. |
paul@114 | 15 | * |
paul@114 | 16 | * You should have received a copy of the GNU General Public License |
paul@114 | 17 | * along with this program; if not, write to the Free Software |
paul@114 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@114 | 19 | * Boston, MA 02110-1301, USA |
paul@114 | 20 | */ |
paul@114 | 21 | |
paul@114 | 22 | #pragma once |
paul@114 | 23 | |
paul@114 | 24 | #include <l4/sys/types.h> |
paul@114 | 25 | #include <stdint.h> |
paul@114 | 26 | |
paul@114 | 27 | |
paul@114 | 28 | |
paul@114 | 29 | #ifdef __cplusplus |
paul@114 | 30 | |
paul@114 | 31 | #include <l4/devices/cpm-jz4730.h> |
paul@114 | 32 | #include <l4/devices/hw_mmio_register_block.h> |
paul@114 | 33 | |
paul@114 | 34 | // I2C channel. |
paul@114 | 35 | |
paul@114 | 36 | class I2c_jz4730_channel |
paul@114 | 37 | { |
paul@114 | 38 | private: |
paul@114 | 39 | Hw::Register_block<32> _regs; |
paul@114 | 40 | Cpm_jz4730_chip *_cpm; |
paul@114 | 41 | uint32_t _frequency; |
paul@114 | 42 | |
paul@114 | 43 | public: |
paul@114 | 44 | I2c_jz4730_channel(l4_addr_t start, Cpm_jz4730_chip *cpm, |
paul@114 | 45 | uint32_t frequency); |
paul@114 | 46 | |
paul@114 | 47 | void disable(); |
paul@114 | 48 | void enable(); |
paul@114 | 49 | |
paul@114 | 50 | unsigned int read(uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 51 | unsigned int write(uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 52 | |
paul@114 | 53 | protected: |
paul@114 | 54 | void set_frequency(); |
paul@114 | 55 | |
paul@114 | 56 | // Transaction control. |
paul@114 | 57 | |
paul@114 | 58 | void set_address(uint8_t address, bool read); |
paul@114 | 59 | |
paul@114 | 60 | void request_next(); |
paul@114 | 61 | void send_next(); |
paul@114 | 62 | void signal_last(); |
paul@114 | 63 | |
paul@114 | 64 | void start(); |
paul@114 | 65 | void stop(); |
paul@114 | 66 | |
paul@114 | 67 | // Specific status conditions. |
paul@114 | 68 | |
paul@114 | 69 | bool data_valid(); |
paul@114 | 70 | bool nack(); |
paul@114 | 71 | bool transferred(); |
paul@114 | 72 | }; |
paul@114 | 73 | |
paul@114 | 74 | // I2C device control. |
paul@114 | 75 | |
paul@114 | 76 | class I2c_jz4730_chip |
paul@114 | 77 | { |
paul@114 | 78 | private: |
paul@114 | 79 | l4_addr_t _start, _end; |
paul@114 | 80 | Cpm_jz4730_chip *_cpm; |
paul@114 | 81 | uint32_t _frequency; |
paul@114 | 82 | |
paul@114 | 83 | public: |
paul@114 | 84 | I2c_jz4730_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4730_chip *cpm, |
paul@114 | 85 | uint32_t frequency); |
paul@114 | 86 | |
paul@114 | 87 | I2c_jz4730_channel *get_channel(uint8_t channel); |
paul@114 | 88 | }; |
paul@114 | 89 | |
paul@114 | 90 | #endif /* __cplusplus */ |
paul@114 | 91 | |
paul@114 | 92 | |
paul@114 | 93 | |
paul@114 | 94 | /* C language interface. */ |
paul@114 | 95 | |
paul@114 | 96 | EXTERN_C_BEGIN |
paul@114 | 97 | |
paul@114 | 98 | void *jz4730_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, |
paul@114 | 99 | uint32_t frequency); |
paul@114 | 100 | |
paul@114 | 101 | void *jz4730_i2c_get_channel(void *i2c, uint8_t channel); |
paul@114 | 102 | |
paul@114 | 103 | void jz4730_i2c_disable(void *i2c_channel); |
paul@114 | 104 | |
paul@114 | 105 | void jz4730_i2c_enable(void *i2c_channel); |
paul@114 | 106 | |
paul@114 | 107 | unsigned int jz4730_i2c_read(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 108 | |
paul@114 | 109 | unsigned int jz4730_i2c_write(void *i2c_channel, uint8_t address, uint8_t buf[], unsigned int length); |
paul@114 | 110 | |
paul@114 | 111 | EXTERN_C_END |