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