b.liu | 4e243dc | 2023-11-27 11:20:00 +0800 | [diff] [blame] | 1 | #include "lynq-irq.h" |
| 2 | #include "mbtk_type.h" |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 3 | #include "mbtk_info_api.h" |
| 4 | #include "mbtk_log.h" |
| 5 | |
| 6 | |
| 7 | #include <ctype.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <pthread.h> |
| 12 | |
| 13 | #define _GNU_SOURCE |
| 14 | |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <limits.h> |
| 18 | #include <poll.h> |
| 19 | |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/sysmacros.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | |
| 27 | struct libirq_info { |
| 28 | unsigned int line; |
| 29 | unsigned int type; |
| 30 | int wake; |
| 31 | int fd; |
| 32 | irq_handler handler; |
| 33 | unsigned int used; |
| 34 | }; |
| 35 | |
| 36 | struct libirq_context { |
| 37 | unsigned int inited; |
| 38 | int fd; |
| 39 | pthread_t th; |
| 40 | struct libirq_info info[SC_LIBIRQ_MAX]; |
| 41 | }; |
| 42 | static struct libirq_context irq_ctx = {0}; |
| 43 | |
| 44 | #define irq_init() irq_ctx.inited |
| 45 | #define line_used(l) irq_ctx.info[l].used |
| 46 | #define irq_fd(l) irq_ctx.info[l].fd |
| 47 | #define libirq_fd() irq_ctx.fd |
| 48 | |
| 49 | static void *libirq_loop(void *arg) |
| 50 | { |
| 51 | unsigned int int_status = 0; |
| 52 | int fd = libirq_fd(); |
| 53 | int ret = 0; |
| 54 | int i; |
| 55 | |
| 56 | while(1) { |
| 57 | ret = ioctl(fd, SC_IRQ_GET_STATUS, &int_status); |
| 58 | if (ret < 0) { |
| 59 | LOGE("libirq_loop get status failed:%d", ret); |
| 60 | } else { |
| 61 | /* printf("libirq_loop get status :0x%x\n", int_status); */ |
| 62 | } |
| 63 | |
| 64 | for (i=0; i<SC_LIBIRQ_MAX; i++) { |
| 65 | if ((int_status & (1<<i)) && line_used(i) && irq_ctx.info[i].handler) { |
| 66 | irq_ctx.info[i].handler(); |
| 67 | |
| 68 | ret = ioctl(fd, SC_IRQ_CLEAR_STATUS, 0); |
| 69 | if (ret < 0) { |
| 70 | LOGE("libirq_loop clear status failed:%d", ret); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | static int libirq_init_thread(void) |
| 80 | { |
| 81 | int ret = 0; |
| 82 | pthread_attr_t attribute; |
| 83 | |
| 84 | pthread_attr_init(&attribute); |
| 85 | pthread_attr_setstacksize(&attribute, 32*1024); |
| 86 | |
| 87 | ret = pthread_create(&irq_ctx.th, &attribute, libirq_loop, NULL); |
| 88 | if(ret) { |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Add a handler for an interrupt line. |
| 97 | * |
| 98 | * line : The interrupt line |
| 99 | * handler : Function to be called when the IRQ occurs. |
| 100 | * trig_type : rising edge or fallling edge |
| 101 | * |
| 102 | * return 0 if succeed, others failed |
| 103 | */ |
| 104 | int sc_irq_install(unsigned int line, irq_handler handler, int trig_type) |
| 105 | { |
| 106 | int fd; |
| 107 | struct libirq_info *info; |
| 108 | char *usr_name; |
| 109 | int ret = 0; |
| 110 | |
| 111 | if ((line >= SC_LIBIRQ_MAX) || (handler == NULL) || (trig_type >= SC_LIBIRQ_TYPE_MAX)) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | if (line_used(line)) |
| 115 | return -EEXIST; |
| 116 | |
| 117 | ret = asprintf(&usr_name, "%s%d", SC_IRQ_DEV, line); |
| 118 | if (ret < 0) { |
| 119 | return -ENOMEM; |
| 120 | } |
| 121 | |
| 122 | fd = open(usr_name, O_RDWR); |
| 123 | if(fd < 0) { |
| 124 | free(usr_name); |
| 125 | return -ENODEV; |
| 126 | } |
| 127 | irq_fd(line) = fd; |
| 128 | free(usr_name); |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 129 | info = &irq_ctx.info[line]; |
| 130 | info->line = line; |
| 131 | info->type = trig_type; |
| 132 | info->handler = handler; |
| 133 | |
| 134 | if (ioctl(fd, SC_IRQ_INSTALL, trig_type) < 0) { |
| 135 | return -EPERM; |
| 136 | } |
| 137 | |
| 138 | line_used(line) = 1; |
| 139 | |
| 140 | if (!irq_init()) { |
| 141 | ret = libirq_init_thread(); |
| 142 | if (ret) { |
| 143 | LOGE("libirq_init_thread, err:%d", ret); |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | libirq_fd() = fd; |
| 148 | irq_init() = 1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * free an interrupt allocated with sc_irq_install. |
| 156 | * |
| 157 | * line : The interrupt line |
| 158 | * |
| 159 | * return 0 if succeed, others failed |
| 160 | */ |
| 161 | int sc_irq_uninstall(unsigned int line) |
| 162 | { |
| 163 | int fd; |
| 164 | |
| 165 | if (line >= SC_LIBIRQ_MAX) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | if (!line_used(line)) |
| 169 | return -ENODEV; |
| 170 | |
| 171 | if (ioctl(irq_fd(line), SC_IRQ_UNINSTALL, 0) < 0) { |
| 172 | return -EPERM; |
| 173 | } |
| 174 | |
| 175 | fd = libirq_fd(); |
| 176 | if (fd) |
| 177 | close(fd); |
| 178 | |
| 179 | line_used(line) = 0; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * set the irq trigger type for an irq. |
| 186 | * |
| 187 | * line : The interrupt line |
| 188 | * trig_type : edge or level type |
| 189 | * |
| 190 | * return 0 if succeed, others failed |
| 191 | */ |
| 192 | int sc_irq_set_type(unsigned int line, int trig_type) |
| 193 | { |
| 194 | struct libirq_info *info; |
| 195 | |
| 196 | if ((line >= SC_LIBIRQ_MAX) || (trig_type >= SC_LIBIRQ_TYPE_MAX)) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | if (!line_used(line)) |
| 200 | return -EEXIST; |
| 201 | |
| 202 | info = &irq_ctx.info[line]; |
n.nie | b3c0472 | 2024-09-13 20:15:56 -0700 | [diff] [blame] | 203 | //if (info->type != trig_type) { |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 204 | if (ioctl(irq_fd(line), SC_IRQ_SET_TYPE, trig_type) < 0) { |
| 205 | return -EPERM; |
| 206 | } |
n.nie | b3c0472 | 2024-09-13 20:15:56 -0700 | [diff] [blame] | 207 | //} |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 208 | |
| 209 | info->type = trig_type; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * get the irq trigger type for an irq. |
| 216 | * |
| 217 | * line : The interrupt line |
| 218 | * trig_type : edge or level type |
| 219 | * |
| 220 | * return 0 if succeed, others failed |
| 221 | */ |
| 222 | int sc_irq_get_type(unsigned int line, int *trig_type) |
| 223 | { |
| 224 | struct libirq_info *info; |
| 225 | |
| 226 | if ((line >= SC_LIBIRQ_MAX) || !trig_type) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | if (!line_used(line)) |
| 230 | return -EEXIST; |
| 231 | |
| 232 | info = &irq_ctx.info[line]; |
| 233 | *trig_type = info->type; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * control irq power management wakeup. |
| 240 | * |
| 241 | * line : The interrupt line |
| 242 | * en : enable/disable power management wakeup |
| 243 | * |
| 244 | * return 0 if succeed, others failed |
| 245 | */ |
| 246 | int sc_irq_set_wake(unsigned int line, int en) |
| 247 | { |
| 248 | struct libirq_info *info; |
| 249 | |
| 250 | if (line >= SC_LIBIRQ_MAX) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | if (!line_used(line)) |
| 254 | return -EEXIST; |
| 255 | |
| 256 | info = &irq_ctx.info[line]; |
| 257 | if (info->wake != en) { |
| 258 | if (ioctl(irq_fd(line), SC_IRQ_SET_WAKE, en) < 0) { |
| 259 | return -EPERM; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | info->wake = en; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * get the irq awake status for an irq. |
| 270 | * |
| 271 | * line : The interrupt line |
| 272 | * en : enable/disable power management wakeup |
| 273 | * |
| 274 | * return 0 if succeed, others failed |
| 275 | */ |
| 276 | int sc_irq_get_wake(unsigned int line, int *en) |
| 277 | { |
| 278 | struct libirq_info *info; |
| 279 | unsigned int wake; |
| 280 | |
| 281 | if (line >= SC_LIBIRQ_MAX) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | if (!line_used(line)) |
| 285 | return -EEXIST; |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 286 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 287 | if (ioctl(irq_fd(line), SC_IRQ_GET_WAKE, &wake) < 0) { |
| 288 | return -EPERM; |
| 289 | } |
| 290 | |
| 291 | info = &irq_ctx.info[line]; |
| 292 | info->wake = wake; |
| 293 | *en = wake; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | /***************************************** |
| 302 | * @brief:lynq_irq_install |
| 303 | * @param count [IN]:2 |
| 304 | * @param sum [OUT]:NA |
| 305 | * @return :success 0, failed other |
| 306 | * @todo:NA |
| 307 | * @see:NA |
| 308 | * @warning:NA |
| 309 | ******************************************/ |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 310 | |
r.xiao | 917e64f | 2024-04-22 04:18:28 -0700 | [diff] [blame] | 311 | int lynq_irq_install(int line, irq_handler irq_test_handler, trig_type_e trig_type) |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 312 | { |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 313 | int ret; |
| 314 | if(trig_type < 0) |
| 315 | { |
| 316 | return -1; |
| 317 | } |
| 318 | #if defined(MBTK_PROJECT_T108) |
r.xiao | e332507 | 2024-06-06 02:12:04 -0700 | [diff] [blame] | 319 | |
r.xiao | 7093885 | 2024-06-12 05:35:31 -0700 | [diff] [blame] | 320 | if (trig_type != 0 && trig_type != 1) |
r.xiao | e332507 | 2024-06-06 02:12:04 -0700 | [diff] [blame] | 321 | { |
| 322 | LOGE("lynq_irq_install error trig_type:%d", trig_type); |
| 323 | return -1; |
| 324 | } |
| 325 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 326 | line = line-117; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 327 | |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 328 | if (line < 0) |
| 329 | { |
| 330 | LOGE("lynq_irq_install error line:%d", line); |
| 331 | return -1; |
| 332 | } |
| 333 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 334 | #endif |
| 335 | |
| 336 | ret = sc_irq_install(line, irq_test_handler, trig_type); |
| 337 | if (ret != 0) |
| 338 | { |
| 339 | LOGE("do_install_irq failed, ret:%d", ret); |
| 340 | return ret; |
| 341 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 345 | |
| 346 | /***************************************** |
| 347 | * @brief:lynq_irq_uninstall |
| 348 | * @param count [IN]:2 |
| 349 | * @param sum [OUT]:NA |
| 350 | * @return :success 0, failed other |
| 351 | * @todo:NA |
| 352 | * @see:NA |
| 353 | * @warning:NA |
| 354 | ******************************************/ |
| 355 | |
| 356 | int lynq_irq_uninstall(int line) |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 357 | { |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 358 | #if defined(MBTK_PROJECT_T108) |
| 359 | line = line-117; |
| 360 | #endif |
| 361 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 362 | int ret; |
| 363 | ret = sc_irq_uninstall(line); |
| 364 | if (ret != 0) |
| 365 | { |
| 366 | LOGE("unistall failed, ret:%d", ret); |
| 367 | return ret; |
| 368 | } |
| 369 | LOGI("uninstall irq(%d) ok", line); |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 373 | |
| 374 | /***************************************** |
| 375 | * @brief:lynq_irq_set_type |
| 376 | * @param count [IN]:2 |
| 377 | * @param sum [OUT]:NA |
| 378 | * @return :success 0, failed other |
| 379 | * @todo:NA |
| 380 | * @see:NA |
| 381 | * @warning:NA |
| 382 | ******************************************/ |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 383 | int lynq_irq_set_type(int line, int trig_type) |
| 384 | { |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 385 | #if defined(MBTK_PROJECT_T108) |
| 386 | line = line-117; |
r.xiao | e332507 | 2024-06-06 02:12:04 -0700 | [diff] [blame] | 387 | |
r.xiao | 7093885 | 2024-06-12 05:35:31 -0700 | [diff] [blame] | 388 | if (trig_type != 0 && trig_type != 1) |
r.xiao | e332507 | 2024-06-06 02:12:04 -0700 | [diff] [blame] | 389 | { |
| 390 | LOGE("lynq_irq_set_type error trig_type:%d", trig_type); |
| 391 | return -1; |
| 392 | } |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 393 | #endif |
| 394 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 395 | int ret; |
| 396 | if(trig_type < 0) |
| 397 | { |
| 398 | return -1; |
| 399 | } |
| 400 | ret = sc_irq_set_type(line, trig_type); |
| 401 | if (ret != 0) |
| 402 | { |
| 403 | LOGE("set_type failed, ret:%d", ret); |
| 404 | return ret; |
| 405 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 406 | return 0; |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 407 | |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 408 | } |
| 409 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 410 | /***************************************** |
| 411 | * @brief:lynq_irq_get_type |
| 412 | * @param count [IN]:1 |
| 413 | * @param sum [OUT]:NA |
| 414 | * @return :success >= 0, failed other |
| 415 | * @todo:NA |
| 416 | * @see:NA |
| 417 | * @warning:NA |
| 418 | ******************************************/ |
| 419 | int lynq_irq_get_type(int line) |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 420 | { |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 421 | #if defined(MBTK_PROJECT_T108) |
| 422 | line = line-117; |
| 423 | #endif |
| 424 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 425 | int ret; |
| 426 | int trig_type; |
| 427 | ret = sc_irq_get_type(line, &trig_type); |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 428 | if (ret != 0) |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 429 | { |
| 430 | LOGE("get_type failed, ret:%d", ret); |
| 431 | return ret; |
| 432 | } |
| 433 | LOGI("get_type readback(%d)", trig_type); |
| 434 | return trig_type; |
| 435 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 436 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 437 | |
| 438 | /***************************************** |
| 439 | * @brief:lynq_irq_set_wake |
| 440 | * @param count [IN]:2 |
| 441 | * @param sum [OUT]:NA |
| 442 | * @return :success 0, failed other |
| 443 | * @todo:NA |
| 444 | * @see:NA |
| 445 | * @warning:NA |
| 446 | ******************************************/ |
| 447 | int lynq_irq_set_wake(int line, int en) |
| 448 | { |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 449 | #if defined(MBTK_PROJECT_T108) |
| 450 | line = line-117; |
| 451 | #endif |
| 452 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 453 | int ret; |
| 454 | if((en != 0) && (en != 1)) |
| 455 | { |
| 456 | LOGE("wake_state is not 0 or 1"); |
| 457 | return -1; |
| 458 | } |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 459 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 460 | ret = sc_irq_set_wake(line, en); |
| 461 | if (ret != 0) |
| 462 | { |
| 463 | LOGE("set_wake failed, ret:%d", ret); |
| 464 | return ret; |
| 465 | } |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 469 | /***************************************** |
| 470 | * @brief:lynq_irq_get_wake |
| 471 | * @param count [IN]:1 |
| 472 | * @param sum [OUT]:NA |
| 473 | * @return :success >= 0, failed other |
| 474 | * @todo:NA |
| 475 | * @see:NA |
| 476 | * @warning:NA |
| 477 | ******************************************/ |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 478 | int lynq_irq_get_wake(int line) |
| 479 | { |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 480 | #if defined(MBTK_PROJECT_T108) |
| 481 | line = line-117; |
| 482 | #endif |
| 483 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 484 | int ret; |
| 485 | int en; |
| 486 | ret = sc_irq_get_wake(line, &en); |
| 487 | if (ret != 0) |
| 488 | { |
| 489 | LOGE("get_wake failed, ret:%d", ret); |
| 490 | return ret; |
| 491 | } |
r.xiao | d94654a | 2024-04-22 02:47:51 -0700 | [diff] [blame] | 492 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 493 | LOGI("get_wake readback(%d)", en); |
| 494 | return en; |
b.liu | 5fa9e77 | 2023-11-23 18:00:55 +0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
r.xiao | 06db9a1 | 2024-04-14 18:51:15 -0700 | [diff] [blame] | 498 | |