paul@62 | 1 | /* |
paul@62 | 2 | * Access the HDMI I2C peripheral on the MIPS Creator CI20 board. |
paul@65 | 3 | * |
paul@65 | 4 | * Copyright (C) 2020 Paul Boddie <paul@boddie.org.uk> |
paul@65 | 5 | * |
paul@65 | 6 | * This program is free software; you can redistribute it and/or |
paul@65 | 7 | * modify it under the terms of the GNU General Public License as |
paul@65 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@65 | 9 | * the License, or (at your option) any later version. |
paul@65 | 10 | * |
paul@65 | 11 | * This program is distributed in the hope that it will be useful, |
paul@65 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@65 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@65 | 14 | * GNU General Public License for more details. |
paul@65 | 15 | * |
paul@65 | 16 | * You should have received a copy of the GNU General Public License |
paul@65 | 17 | * along with this program; if not, write to the Free Software |
paul@65 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@65 | 19 | * Boston, MA 02110-1301, USA |
paul@62 | 20 | */ |
paul@62 | 21 | |
paul@62 | 22 | #include <l4/devices/cpm-jz4780.h> |
paul@62 | 23 | #include <l4/devices/gpio-jz4780.h> |
paul@62 | 24 | #include <l4/devices/hdmi-jz4780.h> |
paul@64 | 25 | |
paul@64 | 26 | #include <l4/devices/lcd-jz4740.h> |
paul@64 | 27 | #include <l4/devices/lcd-jz4740-config.h> |
paul@64 | 28 | #include <l4/devices/lcd-jz4740-panel.h> |
paul@64 | 29 | |
paul@62 | 30 | #include <l4/devices/memory.h> |
paul@62 | 31 | #include <l4/io/io.h> |
paul@62 | 32 | #include <l4/libedid/edid.h> |
paul@62 | 33 | #include <l4/re/env.h> |
paul@62 | 34 | #include <l4/re/c/util/cap_alloc.h> |
paul@62 | 35 | #include <l4/sys/factory.h> |
paul@62 | 36 | #include <l4/sys/icu.h> |
paul@62 | 37 | #include <l4/sys/ipc.h> |
paul@62 | 38 | #include <l4/sys/irq.h> |
paul@62 | 39 | #include <l4/sys/rcv_endpoint.h> |
paul@62 | 40 | #include <l4/vbus/vbus.h> |
paul@62 | 41 | #include <stdio.h> |
paul@62 | 42 | #include <unistd.h> |
paul@62 | 43 | #include <stdint.h> |
paul@62 | 44 | #include <string.h> |
paul@62 | 45 | |
paul@62 | 46 | |
paul@62 | 47 | |
paul@62 | 48 | enum { |
paul@62 | 49 | DDCSCL = 24, /* via PORTF */ |
paul@62 | 50 | DDCSDA = 25, /* via PORTF */ |
paul@62 | 51 | }; |
paul@62 | 52 | |
paul@62 | 53 | |
paul@62 | 54 | |
paul@64 | 55 | /* Test panel. */ |
paul@64 | 56 | |
paul@64 | 57 | static struct Jz4740_lcd_panel panel = { |
paul@64 | 58 | .config = ( |
paul@64 | 59 | Jz4740_lcd_mode_tft_generic |
paul@64 | 60 | | Jz4740_lcd_pclock_negative |
paul@64 | 61 | | Jz4740_lcd_hsync_positive |
paul@64 | 62 | | Jz4740_lcd_vsync_positive |
paul@64 | 63 | | Jz4740_lcd_de_positive), |
paul@64 | 64 | |
paul@64 | 65 | .width = 1280, |
paul@64 | 66 | .height = 1024, |
paul@64 | 67 | .bpp = 32, |
paul@64 | 68 | .frame_rate = 60, |
paul@64 | 69 | .hsync = 112, |
paul@64 | 70 | .vsync = 3, |
paul@64 | 71 | .line_start = 248, // back porch (blanking - vsyn - offset) |
paul@64 | 72 | .line_end = 48, // front porch (sync offset) |
paul@64 | 73 | .frame_start = 36, // back porch (blanking - vsyn - offset) |
paul@64 | 74 | .frame_end = 3, // front porch (sync offset) |
paul@64 | 75 | }; |
paul@64 | 76 | |
paul@64 | 77 | |
paul@64 | 78 | |
paul@62 | 79 | /* Device and resource discovery. */ |
paul@62 | 80 | |
paul@62 | 81 | static long item_in_range(long start, long end, long index) |
paul@62 | 82 | { |
paul@62 | 83 | if (start < end) |
paul@62 | 84 | return start + index; |
paul@62 | 85 | else |
paul@62 | 86 | return start - index; |
paul@62 | 87 | } |
paul@62 | 88 | |
paul@62 | 89 | static void show_timings(uint8_t *buf) |
paul@62 | 90 | { |
paul@62 | 91 | unsigned int width, height; |
paul@62 | 92 | |
paul@62 | 93 | /* Attempt to decode EDID information. */ |
paul@62 | 94 | |
paul@62 | 95 | libedid_prefered_resolution(buf, &width, &height); |
paul@62 | 96 | printf("Preferred resolution: %d x %d\n", width, height); |
paul@62 | 97 | |
paul@62 | 98 | libedid_dump_standard_timings(buf); |
paul@62 | 99 | } |
paul@62 | 100 | |
paul@62 | 101 | int main(void) |
paul@62 | 102 | { |
paul@62 | 103 | long err; |
paul@62 | 104 | |
paul@62 | 105 | /* Buffer for EDID data. */ |
paul@62 | 106 | |
paul@62 | 107 | uint8_t edid[128]; |
paul@62 | 108 | unsigned int length = 0, i; |
paul@62 | 109 | |
paul@62 | 110 | /* Version details. */ |
paul@62 | 111 | |
paul@62 | 112 | uint8_t hdmi_major; |
paul@62 | 113 | uint16_t hdmi_minor; |
paul@62 | 114 | |
paul@62 | 115 | /* Peripheral memory. */ |
paul@62 | 116 | |
paul@62 | 117 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@62 | 118 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@62 | 119 | l4_addr_t hdmi_base = 0, hdmi_base_end = 0; |
paul@64 | 120 | l4_addr_t lcd_base = 0, lcd_base_end = 0; |
paul@62 | 121 | l4_addr_t port_f, port_f_end; |
paul@62 | 122 | |
paul@62 | 123 | /* Peripheral abstractions. */ |
paul@62 | 124 | |
paul@62 | 125 | void *cpm; |
paul@62 | 126 | void *gpio_port_f; |
paul@62 | 127 | void *hdmi; |
paul@64 | 128 | void *lcd; |
paul@62 | 129 | |
paul@62 | 130 | /* Access to IRQs. */ |
paul@62 | 131 | |
paul@62 | 132 | l4_uint32_t hdmi_irq_start = 0, hdmi_irq_end = 0; |
paul@64 | 133 | l4_uint32_t lcd_irq_start = 0, lcd_irq_end = 0; |
paul@64 | 134 | l4_cap_idx_t icucap, hdmi_irq, lcd_irq; |
paul@62 | 135 | |
paul@64 | 136 | hdmi_irq = l4re_util_cap_alloc(); |
paul@64 | 137 | lcd_irq = l4re_util_cap_alloc(); |
paul@62 | 138 | icucap = l4re_env_get_cap("icu"); |
paul@62 | 139 | |
paul@62 | 140 | if (l4_is_invalid_cap(icucap)) |
paul@62 | 141 | { |
paul@62 | 142 | printf("No 'icu' capability available in the virtual bus.\n"); |
paul@62 | 143 | return 1; |
paul@62 | 144 | } |
paul@62 | 145 | |
paul@64 | 146 | if (l4_is_invalid_cap(hdmi_irq) || l4_is_invalid_cap(lcd_irq)) |
paul@62 | 147 | { |
paul@62 | 148 | printf("Capabilities not available for interrupts.\n"); |
paul@62 | 149 | return 1; |
paul@62 | 150 | } |
paul@62 | 151 | |
paul@62 | 152 | /* Obtain resource details describing the interrupt for HDMI I2C. */ |
paul@62 | 153 | |
paul@62 | 154 | printf("Access IRQ...\n"); |
paul@62 | 155 | |
paul@62 | 156 | if (get_irq("jz4780-hdmi", &hdmi_irq_start, &hdmi_irq_end) < 0) |
paul@62 | 157 | return 1; |
paul@62 | 158 | |
paul@64 | 159 | printf("HDMI IRQ range at %d...%d.\n", hdmi_irq_start, hdmi_irq_end); |
paul@64 | 160 | |
paul@64 | 161 | if (get_irq("jz4780-lcd", &lcd_irq_start, &lcd_irq_end) < 0) |
paul@64 | 162 | return 1; |
paul@64 | 163 | |
paul@64 | 164 | printf("LCD IRQ range at %d...%d.\n", lcd_irq_start, lcd_irq_end); |
paul@62 | 165 | |
paul@62 | 166 | /* Create interrupt objects. */ |
paul@62 | 167 | |
paul@64 | 168 | err = l4_error(l4_factory_create_irq(l4re_global_env->factory, hdmi_irq)) || |
paul@64 | 169 | l4_error(l4_factory_create_irq(l4re_global_env->factory, lcd_irq)); |
paul@62 | 170 | |
paul@62 | 171 | if (err) |
paul@62 | 172 | { |
paul@62 | 173 | printf("Could not create IRQ object: %lx\n", err); |
paul@62 | 174 | return 1; |
paul@62 | 175 | } |
paul@62 | 176 | |
paul@62 | 177 | /* Bind interrupt objects to IRQ numbers. Here, the first HDMI interrupt is |
paul@62 | 178 | bound, this being the general HDMI interrupt. */ |
paul@62 | 179 | |
paul@62 | 180 | err = l4_error(l4_icu_bind(icucap, |
paul@62 | 181 | item_in_range(hdmi_irq_start, hdmi_irq_end, 0), |
paul@64 | 182 | hdmi_irq)) || |
paul@64 | 183 | l4_error(l4_icu_bind(icucap, |
paul@64 | 184 | item_in_range(lcd_irq_start, lcd_irq_end, 0), |
paul@64 | 185 | lcd_irq)); |
paul@62 | 186 | |
paul@62 | 187 | if (err) |
paul@62 | 188 | { |
paul@62 | 189 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@62 | 190 | return 1; |
paul@62 | 191 | } |
paul@62 | 192 | |
paul@62 | 193 | /* Attach ourselves to the interrupt handler. */ |
paul@62 | 194 | |
paul@64 | 195 | err = l4_error(l4_rcv_ep_bind_thread(hdmi_irq, l4re_env()->main_thread, 0)) || |
paul@64 | 196 | l4_error(l4_rcv_ep_bind_thread(lcd_irq, l4re_env()->main_thread, 0)); |
paul@62 | 197 | |
paul@62 | 198 | if (err) |
paul@62 | 199 | { |
paul@62 | 200 | printf("Could not attach to IRQs: %ld\n", err); |
paul@62 | 201 | return 1; |
paul@62 | 202 | } |
paul@62 | 203 | |
paul@62 | 204 | /* Obtain resource details describing I/O memory. */ |
paul@62 | 205 | |
paul@62 | 206 | printf("Access CPM...\n"); |
paul@62 | 207 | |
paul@62 | 208 | if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) |
paul@62 | 209 | return 1; |
paul@62 | 210 | |
paul@62 | 211 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@62 | 212 | |
paul@62 | 213 | printf("Access GPIO...\n"); |
paul@62 | 214 | |
paul@62 | 215 | if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) |
paul@62 | 216 | return 1; |
paul@62 | 217 | |
paul@62 | 218 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@62 | 219 | |
paul@62 | 220 | printf("Access HDMI...\n"); |
paul@62 | 221 | |
paul@62 | 222 | if (get_memory("jz4780-hdmi", &hdmi_base, &hdmi_base_end) < 0) |
paul@62 | 223 | return 1; |
paul@62 | 224 | |
paul@62 | 225 | printf("HDMI at 0x%lx...0x%lx.\n", hdmi_base, hdmi_base_end); |
paul@62 | 226 | |
paul@64 | 227 | printf("Access LCD...\n"); |
paul@64 | 228 | |
paul@64 | 229 | if (get_memory("jz4780-lcd", &lcd_base, &lcd_base_end) < 0) |
paul@64 | 230 | return 1; |
paul@64 | 231 | |
paul@64 | 232 | printf("LCD at 0x%lx...0x%lx.\n", lcd_base, lcd_base_end); |
paul@64 | 233 | |
paul@62 | 234 | /* Obtain CPM object. */ |
paul@62 | 235 | |
paul@62 | 236 | cpm = jz4780_cpm_init(cpm_base); |
paul@62 | 237 | |
paul@62 | 238 | printf("VPLL frequency: %d\n", jz4780_cpm_get_vpll_frequency(cpm)); |
paul@62 | 239 | printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); |
paul@62 | 240 | printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); |
paul@62 | 241 | |
paul@62 | 242 | jz4780_cpm_stop_hdmi(cpm); |
paul@62 | 243 | jz4780_cpm_set_hdmi_frequency(cpm, 27000000); |
paul@62 | 244 | |
paul@62 | 245 | printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); |
paul@62 | 246 | printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); |
paul@62 | 247 | |
paul@62 | 248 | jz4780_cpm_start_hdmi(cpm); |
paul@62 | 249 | |
paul@62 | 250 | /* Configure pins. */ |
paul@62 | 251 | |
paul@62 | 252 | port_f = gpio_base + 0x500; |
paul@62 | 253 | port_f_end = port_f + 0x100; |
paul@62 | 254 | |
paul@62 | 255 | printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); |
paul@62 | 256 | |
paul@62 | 257 | gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0x7fa7f00f, 0x00580ff0); |
paul@62 | 258 | |
paul@62 | 259 | printf("Set up GPIO pins...\n"); |
paul@62 | 260 | |
paul@62 | 261 | jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 0); |
paul@62 | 262 | jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 0); |
paul@62 | 263 | |
paul@62 | 264 | /* Obtain HDMI reference. */ |
paul@62 | 265 | |
paul@62 | 266 | printf("Set up HDMI...\n"); |
paul@62 | 267 | |
paul@64 | 268 | hdmi = jz4780_hdmi_init(hdmi_base, hdmi_base_end, hdmi_irq); |
paul@62 | 269 | |
paul@62 | 270 | printf("Read version...\n"); |
paul@62 | 271 | |
paul@62 | 272 | jz4780_hdmi_get_version(hdmi, &hdmi_major, &hdmi_minor); |
paul@62 | 273 | |
paul@62 | 274 | printf("HDMI version is %x.%03x\n", hdmi_major, hdmi_minor); |
paul@62 | 275 | |
paul@65 | 276 | printf("Connected: %s\n", jz4780_hdmi_connected(hdmi) ? "yes" : "no"); |
paul@65 | 277 | |
paul@65 | 278 | while (!jz4780_hdmi_connected(hdmi)) |
paul@65 | 279 | jz4780_hdmi_wait_for_connection(hdmi); |
paul@65 | 280 | |
paul@62 | 281 | printf("Read EDID...\n"); |
paul@62 | 282 | |
paul@62 | 283 | jz4780_hdmi_i2c_set_address(hdmi, 0x50); |
paul@62 | 284 | length = jz4780_hdmi_i2c_read(hdmi, edid, 128); |
paul@62 | 285 | |
paul@62 | 286 | if (length) |
paul@62 | 287 | { |
paul@62 | 288 | for (i = 0; i < length; i++) |
paul@62 | 289 | printf("%02x%c", edid[i], ((i % 16) != 15) ? ' ' : '\n'); |
paul@62 | 290 | } |
paul@62 | 291 | |
paul@62 | 292 | show_timings(edid); |
paul@62 | 293 | |
paul@64 | 294 | /* Obtain LCD reference. */ |
paul@64 | 295 | |
paul@64 | 296 | printf("Set up LCD...\n"); |
paul@64 | 297 | |
paul@64 | 298 | lcd = jz4740_lcd_init(lcd_base, &panel); |
paul@64 | 299 | |
paul@64 | 300 | /* Test initialisation with a frequency appropriate for the test panel. */ |
paul@64 | 301 | |
paul@64 | 302 | printf("LCD divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm)); |
paul@64 | 303 | printf("LCD frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm)); |
paul@64 | 304 | printf("Desired frequency: %d\n", jz4740_lcd_get_pixel_clock(lcd)); |
paul@62 | 305 | |
paul@64 | 306 | jz4780_cpm_stop_lcd(cpm); |
paul@64 | 307 | |
paul@64 | 308 | jz4780_cpm_set_lcd_frequencies(cpm, jz4740_lcd_get_pixel_clock(lcd), 3); |
paul@64 | 309 | |
paul@64 | 310 | printf("LCD divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm)); |
paul@64 | 311 | printf("LCD frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm)); |
paul@64 | 312 | |
paul@64 | 313 | jz4780_cpm_start_lcd(cpm); |
paul@64 | 314 | |
paul@64 | 315 | /* Detach from the interrupts. */ |
paul@64 | 316 | |
paul@64 | 317 | err = l4_error(l4_irq_detach(hdmi_irq)) || |
paul@64 | 318 | l4_error(l4_irq_detach(lcd_irq)); |
paul@62 | 319 | |
paul@62 | 320 | if (err) |
paul@62 | 321 | printf("Error detaching from IRQ: %ld\n", err); |
paul@62 | 322 | |
paul@62 | 323 | printf("Done.\n"); |
paul@62 | 324 | |
paul@62 | 325 | return 0; |
paul@62 | 326 | } |