Stefan Bosch | b4bb31d | 2020-07-10 19:07:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd. |
| 4 | * (http://www.friendlyarm.com) |
| 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/clk.h> |
| 12 | #include <i2c.h> |
| 13 | #include <pwm.h> |
| 14 | |
| 15 | #include <irq_func.h> |
| 16 | |
| 17 | #include <asm/arch/nexell.h> |
| 18 | #include <asm/arch/nx_gpio.h> |
| 19 | |
| 20 | #ifndef NSEC_PER_SEC |
| 21 | #define NSEC_PER_SEC 1000000000L |
| 22 | #endif |
| 23 | |
| 24 | #define SAMPLE_BPS 9600 |
| 25 | #define SAMPLE_IN_US 101 /* (1000000 / BPS) */ |
| 26 | |
| 27 | #define REQ_INFO 0x60U |
| 28 | #define REQ_BL 0x80U |
| 29 | |
| 30 | #define BUS_I2C 0x18 |
| 31 | #define ONEWIRE_I2C_BUS 2 |
| 32 | #define ONEWIRE_I2C_ADDR 0x2f |
| 33 | |
| 34 | static int bus_type = -1; |
| 35 | static int lcd_id = -1; |
| 36 | static unsigned short lcd_fwrev; |
| 37 | static int current_brightness = -1; |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 38 | #if CONFIG_IS_ENABLED(DM_I2C) |
Stefan Bosch | b4bb31d | 2020-07-10 19:07:37 +0200 | [diff] [blame] | 39 | static struct udevice *i2c_dev; |
| 40 | #endif |
| 41 | |
| 42 | /* debug */ |
| 43 | #if (0) |
| 44 | #define DBGOUT(msg...) do { printf("onewire: " msg); } while (0) |
| 45 | #else |
| 46 | #define DBGOUT(msg...) do {} while (0) |
| 47 | #endif |
| 48 | |
| 49 | /* based on web page from http://lfh1986.blogspot.com */ |
| 50 | static unsigned char crc8_ow(unsigned int v, unsigned int len) |
| 51 | { |
| 52 | unsigned char crc = 0xACU; |
| 53 | |
| 54 | while (len--) { |
| 55 | if ((crc & 0x80U) != 0) { |
| 56 | crc <<= 1; |
| 57 | crc ^= 0x7U; |
| 58 | } else { |
| 59 | crc <<= 1; |
| 60 | } |
| 61 | if ((v & (1U << 31)) != 0) |
| 62 | crc ^= 0x7U; |
| 63 | v <<= 1; |
| 64 | } |
| 65 | return crc; |
| 66 | } |
| 67 | |
| 68 | /* GPIO helpers */ |
| 69 | #define __IO_GRP 2 /* GPIOC15 */ |
| 70 | #define __IO_IDX 15 |
| 71 | |
| 72 | static inline void set_pin_as_input(void) |
| 73 | { |
| 74 | nx_gpio_set_output_enable(__IO_GRP, __IO_IDX, 0); |
| 75 | } |
| 76 | |
| 77 | static inline void set_pin_as_output(void) |
| 78 | { |
| 79 | nx_gpio_set_output_enable(__IO_GRP, __IO_IDX, 1); |
| 80 | } |
| 81 | |
| 82 | static inline void set_pin_value(int v) |
| 83 | { |
| 84 | nx_gpio_set_output_value(__IO_GRP, __IO_IDX, !!v); |
| 85 | } |
| 86 | |
| 87 | static inline int get_pin_value(void) |
| 88 | { |
| 89 | return nx_gpio_get_input_value(__IO_GRP, __IO_IDX); |
| 90 | } |
| 91 | |
| 92 | /* Timer helpers */ |
| 93 | #define PWM_CH 3 |
| 94 | #define PWM_TCON (PHY_BASEADDR_PWM + 0x08) |
| 95 | #define PWM_TCON_START (1 << 16) |
| 96 | #define PWM_TINT_CSTAT (PHY_BASEADDR_PWM + 0x44) |
| 97 | |
| 98 | static int onewire_init_timer(void) |
| 99 | { |
| 100 | int period_ns = NSEC_PER_SEC / SAMPLE_BPS; |
| 101 | |
| 102 | /* range: 1080~1970 */ |
| 103 | period_ns -= 1525; |
| 104 | |
| 105 | return pwm_config(PWM_CH, period_ns >> 1, period_ns); |
| 106 | } |
| 107 | |
| 108 | static void wait_one_tick(void) |
| 109 | { |
| 110 | unsigned int tcon; |
| 111 | |
| 112 | tcon = readl(PWM_TCON); |
| 113 | tcon |= PWM_TCON_START; |
| 114 | writel(tcon, PWM_TCON); |
| 115 | |
| 116 | while (1) { |
| 117 | if (readl(PWM_TINT_CSTAT) & (1 << (5 + PWM_CH))) |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | writel((1 << (5 + PWM_CH)), PWM_TINT_CSTAT); |
| 122 | |
| 123 | tcon &= ~PWM_TCON_START; |
| 124 | writel(tcon, PWM_TCON); |
| 125 | } |
| 126 | |
| 127 | /* Session handler */ |
| 128 | static int onewire_session(unsigned char req, unsigned char res[]) |
| 129 | { |
| 130 | unsigned int Req; |
| 131 | unsigned int *Res; |
| 132 | int ints = disable_interrupts(); |
| 133 | int i; |
| 134 | int ret; |
| 135 | |
| 136 | Req = (req << 24) | (crc8_ow(req << 24, 8) << 16); |
| 137 | Res = (unsigned int *)res; |
| 138 | |
| 139 | set_pin_value(1); |
| 140 | set_pin_as_output(); |
| 141 | for (i = 0; i < 60; i++) |
| 142 | wait_one_tick(); |
| 143 | |
| 144 | set_pin_value(0); |
| 145 | for (i = 0; i < 2; i++) |
| 146 | wait_one_tick(); |
| 147 | |
| 148 | for (i = 0; i < 16; i++) { |
| 149 | int v = !!(Req & (1U << 31)); |
| 150 | |
| 151 | Req <<= 1; |
| 152 | set_pin_value(v); |
| 153 | wait_one_tick(); |
| 154 | } |
| 155 | |
| 156 | wait_one_tick(); |
| 157 | set_pin_as_input(); |
| 158 | wait_one_tick(); |
| 159 | for (i = 0; i < 32; i++) { |
| 160 | (*Res) <<= 1; |
| 161 | (*Res) |= get_pin_value(); |
| 162 | wait_one_tick(); |
| 163 | } |
| 164 | set_pin_value(1); |
| 165 | set_pin_as_output(); |
| 166 | |
| 167 | if (ints) |
| 168 | enable_interrupts(); |
| 169 | |
| 170 | ret = crc8_ow(*Res, 24) == res[0]; |
| 171 | DBGOUT("req = %02X, res = %02X%02X%02X%02X, ret = %d\n", |
| 172 | req, res[3], res[2], res[1], res[0], ret); |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static int onewire_i2c_do_request(unsigned char req, unsigned char *buf) |
| 178 | { |
| 179 | unsigned char tx[4]; |
| 180 | int ret; |
| 181 | |
| 182 | tx[0] = req; |
| 183 | tx[1] = crc8_ow(req << 24, 8); |
| 184 | |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 185 | #if CONFIG_IS_ENABLED(DM_I2C) |
Stefan Bosch | b4bb31d | 2020-07-10 19:07:37 +0200 | [diff] [blame] | 186 | if (dm_i2c_write(i2c_dev, 0, tx, 2)) |
| 187 | return -EIO; |
| 188 | |
| 189 | if (!buf) |
| 190 | return 0; |
| 191 | |
| 192 | if (dm_i2c_read(i2c_dev, 0, buf, 4)) |
| 193 | return -EIO; |
| 194 | #else |
| 195 | if (i2c_write(ONEWIRE_I2C_ADDR, 0, 0, tx, 2)) |
| 196 | return -EIO; |
| 197 | |
| 198 | if (!buf) /* NO READ */ |
| 199 | return 0; |
| 200 | |
| 201 | if (i2c_read(ONEWIRE_I2C_ADDR, 0, 0, buf, 4)) |
| 202 | return -EIO; |
| 203 | #endif |
| 204 | |
| 205 | ret = crc8_ow((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8), 24); |
| 206 | DBGOUT("req = %02X, res = %02X%02X%02X%02X, ret = %02x\n", |
| 207 | req, buf[0], buf[1], buf[2], buf[3], ret); |
| 208 | |
| 209 | return (ret == buf[3]) ? 0 : -EIO; |
| 210 | } |
| 211 | |
| 212 | static void onewire_i2c_init(void) |
| 213 | { |
| 214 | unsigned char buf[4]; |
| 215 | int ret; |
| 216 | |
Igor Opaniuk | f7c9176 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 217 | #if CONFIG_IS_ENABLED(DM_I2C) |
Stefan Bosch | b4bb31d | 2020-07-10 19:07:37 +0200 | [diff] [blame] | 218 | ret = i2c_get_chip_for_busnum(ONEWIRE_I2C_BUS, |
| 219 | ONEWIRE_I2C_ADDR, 0, &i2c_dev); |
| 220 | #else |
| 221 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 222 | i2c_set_bus_num(ONEWIRE_I2C_BUS); |
| 223 | |
| 224 | ret = i2c_probe(ONEWIRE_I2C_ADDR); |
| 225 | #endif |
| 226 | if (ret) |
| 227 | return; |
| 228 | |
| 229 | ret = onewire_i2c_do_request(REQ_INFO, buf); |
| 230 | if (!ret) { |
| 231 | lcd_id = buf[0]; |
| 232 | lcd_fwrev = buf[1] * 0x100 + buf[2]; |
| 233 | bus_type = BUS_I2C; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void onewire_init(void) |
| 238 | { |
| 239 | /* GPIO, Pull-off */ |
| 240 | nx_gpio_set_pad_function(__IO_GRP, __IO_IDX, 1); |
| 241 | nx_gpio_set_pull_mode(__IO_GRP, __IO_IDX, 2); |
| 242 | |
| 243 | onewire_init_timer(); |
| 244 | onewire_i2c_init(); |
| 245 | } |
| 246 | |
| 247 | int onewire_get_info(unsigned char *lcd, unsigned short *fw_ver) |
| 248 | { |
| 249 | unsigned char res[4]; |
| 250 | int i; |
| 251 | |
| 252 | if (bus_type == BUS_I2C && lcd_id > 0) { |
| 253 | *lcd = lcd_id; |
| 254 | *fw_ver = lcd_fwrev; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | for (i = 0; i < 3; i++) { |
| 259 | if (onewire_session(REQ_INFO, res)) { |
| 260 | *lcd = res[3]; |
| 261 | *fw_ver = res[2] * 0x100 + res[1]; |
| 262 | lcd_id = *lcd; |
| 263 | DBGOUT("lcd = %d, fw_ver = %x\n", *lcd, *fw_ver); |
| 264 | return 0; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* LCD unknown or not connected */ |
| 269 | *lcd = 0; |
| 270 | *fw_ver = -1; |
| 271 | |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | int onewire_get_lcd_id(void) |
| 276 | { |
| 277 | return lcd_id; |
| 278 | } |
| 279 | |
| 280 | int onewire_set_backlight(int brightness) |
| 281 | { |
| 282 | unsigned char res[4]; |
| 283 | int i; |
| 284 | |
| 285 | if (brightness == current_brightness) |
| 286 | return 0; |
| 287 | |
| 288 | if (brightness > 127) |
| 289 | brightness = 127; |
| 290 | else if (brightness < 0) |
| 291 | brightness = 0; |
| 292 | |
| 293 | if (bus_type == BUS_I2C) { |
| 294 | onewire_i2c_do_request((REQ_BL | brightness), NULL); |
| 295 | current_brightness = brightness; |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | for (i = 0; i < 3; i++) { |
| 300 | if (onewire_session((REQ_BL | brightness), res)) { |
| 301 | current_brightness = brightness; |
| 302 | return 0; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return -1; |
| 307 | } |