paul@0 | 1 | /* |
paul@114 | 2 | * I2C support for the JZ4780. |
paul@114 | 3 | * |
paul@214 | 4 | * Copyright (C) 2017, 2018, 2021, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software; you can redistribute it and/or |
paul@0 | 7 | * modify it under the terms of the GNU General Public License as |
paul@0 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@0 | 9 | * the License, or (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program; if not, write to the Free Software |
paul@0 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@0 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | #include <l4/devices/i2c-jz4780.h> |
paul@0 | 23 | |
paul@0 | 24 | /* |
paul@0 | 25 | I2C pins: |
paul@0 | 26 | |
paul@0 | 27 | I2C0: PD30/SMB0_SDA, PD31/SMB0_SCK |
paul@0 | 28 | I2C1: PD30/SMB1_SDA, PD31/SMB1_SCK (pulled up on CI20) |
paul@0 | 29 | I2C2: PF16/SMB2_SDA, PF17/SMB2_SCK |
paul@0 | 30 | PWM: PD10/SMB3_SDA, PD11/SMB3_SCK |
paul@0 | 31 | PWM: PE3/SMB4_SDA, PE4/SMB4_SCK |
paul@0 | 32 | I2C4: PE12/SMB4_SDA, PE13/SMB4_SCK |
paul@0 | 33 | HDMI: PF25/SMB4_SDA/DDCSDA, PF24/SMB4_SCK/DDCSCK |
paul@0 | 34 | |
paul@0 | 35 | See: http://mipscreator.imgtec.com/CI20/hardware/board/ci20_jz4780_v2.0.pdf |
paul@0 | 36 | */ |
paul@0 | 37 | |
paul@0 | 38 | |
paul@0 | 39 | |
paul@0 | 40 | // Initialise a channel. |
paul@0 | 41 | |
paul@0 | 42 | I2c_jz4780_channel::I2c_jz4780_channel(l4_addr_t start, |
paul@214 | 43 | enum Clock_identifiers clock, |
paul@214 | 44 | Cpm_chip *cpm, |
paul@53 | 45 | uint32_t frequency) |
paul@214 | 46 | : I2c_channel(start, clock, cpm, frequency) |
paul@0 | 47 | { |
paul@0 | 48 | } |
paul@0 | 49 | |
paul@0 | 50 | |
paul@0 | 51 | |
paul@0 | 52 | // Initialise the I2C controller. |
paul@0 | 53 | |
paul@0 | 54 | I2c_jz4780_chip::I2c_jz4780_chip(l4_addr_t start, l4_addr_t end, |
paul@214 | 55 | Cpm_chip *cpm, |
paul@0 | 56 | uint32_t frequency) |
paul@214 | 57 | : I2c_chip(start, end, cpm, frequency) |
paul@0 | 58 | { |
paul@0 | 59 | } |
paul@0 | 60 | |
paul@0 | 61 | |
paul@0 | 62 | |
paul@0 | 63 | // C language interface functions. |
paul@0 | 64 | |
paul@0 | 65 | void *jz4780_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm, uint32_t frequency) |
paul@0 | 66 | { |
paul@214 | 67 | return (void *) new I2c_jz4780_chip(start, end, static_cast<Cpm_chip *>(cpm), frequency); |
paul@53 | 68 | } |
paul@53 | 69 | |
paul@0 | 70 | void *jz4780_i2c_get_channel(void *i2c, uint8_t channel) |
paul@0 | 71 | { |
paul@0 | 72 | return static_cast<I2c_jz4780_chip *>(i2c)->get_channel(channel); |
paul@0 | 73 | } |
paul@0 | 74 | |
paul@214 | 75 | uint32_t jz4780_i2c_get_frequency(void *i2c_channel) |
paul@214 | 76 | { |
paul@214 | 77 | return static_cast<I2c_jz4780_channel *>(i2c_channel)->get_frequency(); |
paul@214 | 78 | } |
paul@214 | 79 | |
paul@0 | 80 | void jz4780_i2c_set_target(void *i2c_channel, uint8_t addr) |
paul@0 | 81 | { |
paul@0 | 82 | static_cast<I2c_jz4780_channel *>(i2c_channel)->set_target(addr); |
paul@0 | 83 | } |
paul@0 | 84 | |
paul@214 | 85 | void jz4780_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total, |
paul@214 | 86 | int stop) |
paul@0 | 87 | { |
paul@214 | 88 | static_cast<I2c_jz4780_channel *>(i2c_channel)->start_read(buf, total, stop); |
paul@0 | 89 | } |
paul@0 | 90 | |
paul@0 | 91 | void jz4780_i2c_read(void *i2c_channel) |
paul@0 | 92 | { |
paul@0 | 93 | static_cast<I2c_jz4780_channel *>(i2c_channel)->read(); |
paul@0 | 94 | } |
paul@0 | 95 | |
paul@214 | 96 | void jz4780_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total, |
paul@214 | 97 | int stop) |
paul@56 | 98 | { |
paul@214 | 99 | static_cast<I2c_jz4780_channel *>(i2c_channel)->start_write(buf, total, stop); |
paul@56 | 100 | } |
paul@56 | 101 | |
paul@56 | 102 | void jz4780_i2c_write(void *i2c_channel) |
paul@56 | 103 | { |
paul@56 | 104 | static_cast<I2c_jz4780_channel *>(i2c_channel)->write(); |
paul@56 | 105 | } |
paul@56 | 106 | |
paul@0 | 107 | int jz4780_i2c_read_done(void *i2c_channel) |
paul@0 | 108 | { |
paul@0 | 109 | return static_cast<I2c_jz4780_channel *>(i2c_channel)->read_done(); |
paul@0 | 110 | } |
paul@0 | 111 | |
paul@56 | 112 | int jz4780_i2c_write_done(void *i2c_channel) |
paul@56 | 113 | { |
paul@56 | 114 | return static_cast<I2c_jz4780_channel *>(i2c_channel)->write_done(); |
paul@56 | 115 | } |
paul@56 | 116 | |
paul@53 | 117 | unsigned int jz4780_i2c_have_read(void *i2c_channel) |
paul@0 | 118 | { |
paul@0 | 119 | return static_cast<I2c_jz4780_channel *>(i2c_channel)->have_read(); |
paul@0 | 120 | } |
paul@0 | 121 | |
paul@56 | 122 | unsigned int jz4780_i2c_have_written(void *i2c_channel) |
paul@0 | 123 | { |
paul@56 | 124 | return static_cast<I2c_jz4780_channel *>(i2c_channel)->have_written(); |
paul@0 | 125 | } |
paul@0 | 126 | |
paul@56 | 127 | int jz4780_i2c_failed(void *i2c_channel) |
paul@0 | 128 | { |
paul@56 | 129 | return static_cast<I2c_jz4780_channel *>(i2c_channel)->failed(); |
paul@0 | 130 | } |