|
| 1 | +/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without |
| 4 | + * modification, are permitted provided that the following conditions are met: |
| 5 | + * 1. Redistributions of source code must retain the above copyright |
| 6 | + * notice, this list of conditions and the following disclaimer. |
| 7 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer in the |
| 9 | + * documentation and/or other materials provided with the distribution. |
| 10 | + * |
| 11 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 12 | + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 13 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 14 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 15 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 16 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 17 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 18 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 19 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 21 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 22 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <rtthread.h> |
| 27 | +#include <rthw.h> |
| 28 | +#include <rtdevice.h> |
| 29 | +#include <riscv_io.h> |
| 30 | +#include "ioremap.h" |
| 31 | +#include "drv_ts.h" |
| 32 | +#include "board.h" |
| 33 | +#include "drv_hardlock.h" |
| 34 | +#if defined(RT_USING_POSIX_DEVIO) |
| 35 | +#include <dfs_posix.h> |
| 36 | +#include <poll.h> |
| 37 | +#include <termios.h> |
| 38 | +#endif |
| 39 | + |
| 40 | +#include <rthw.h> |
| 41 | + |
| 42 | +static struct rt_device g_ts_device = {0}; |
| 43 | +void *ts_base_addr = RT_NULL; |
| 44 | +static int ts_hardlock; |
| 45 | + |
| 46 | +rt_ssize_t ts_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) |
| 47 | +{ |
| 48 | + rt_uint32_t *outbuf = (rt_uint32_t *)buffer; |
| 49 | + rt_uint32_t val = 0; |
| 50 | + rt_uint32_t ret = -1; |
| 51 | + void *ts_read_addr = (void *)((char *)ts_base_addr + TS_READ_OFFSET); |
| 52 | + void *ts_config_addr = (void *)((char *)ts_base_addr + TS_CONFIG_OFFSET); |
| 53 | + |
| 54 | + while(0 != kd_hardlock_lock(ts_hardlock)); |
| 55 | + |
| 56 | + writel(0x22, ts_config_addr); |
| 57 | + writel(0x23, ts_config_addr); |
| 58 | + rt_thread_mdelay(20); |
| 59 | + kd_hardlock_unlock(ts_hardlock); |
| 60 | + |
| 61 | + while(1) |
| 62 | + { |
| 63 | + val = readl(ts_read_addr); |
| 64 | + |
| 65 | + if(val >> 12) |
| 66 | + { |
| 67 | + *outbuf = val; |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + ret = sizeof(outbuf) / sizeof(outbuf[0]); |
| 72 | + |
| 73 | + return ret; |
| 74 | +} |
| 75 | + |
| 76 | +const static struct rt_device_ops ts_ops = |
| 77 | +{ |
| 78 | + RT_NULL, |
| 79 | + RT_NULL, |
| 80 | + RT_NULL, |
| 81 | + ts_device_read, |
| 82 | + RT_NULL, |
| 83 | + RT_NULL |
| 84 | +}; |
| 85 | + |
| 86 | +int rt_hw_ts_init(void) |
| 87 | +{ |
| 88 | + rt_err_t ret = RT_EOK; |
| 89 | + rt_device_t ts_device = &g_ts_device; |
| 90 | + |
| 91 | + ts_base_addr = rt_ioremap((void*)TS_BASE_ADDR, TS_IO_SIZE); |
| 92 | + if(ts_base_addr == RT_NULL) { |
| 93 | + rt_kprintf("ts ioremap error\n"); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + |
| 97 | + ret = rt_device_register(ts_device, "ts", RT_DEVICE_FLAG_RDWR); |
| 98 | + if(ret) { |
| 99 | + rt_kprintf("ts device register fail\n"); |
| 100 | + return ret; |
| 101 | + } |
| 102 | + |
| 103 | + ts_device->ops = &ts_ops; |
| 104 | + |
| 105 | + if(kd_request_lock(HARDLOCK_TS)) { |
| 106 | + rt_kprintf("fail to request hardlock-%d\n", HARDLOCK_TS); |
| 107 | + ts_hardlock = -1; |
| 108 | + } else |
| 109 | + ts_hardlock = HARDLOCK_TS; |
| 110 | +#ifndef RT_FASTBOOT |
| 111 | + if(!ret) |
| 112 | + rt_kprintf("%s OK\n", __func__); |
| 113 | +#endif |
| 114 | + return ret; |
| 115 | +} |
| 116 | +INIT_BOARD_EXPORT(rt_hw_ts_init); |
0 commit comments