alarm8hz_acts.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2021 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Alarm 8HZ driver for Actions SoC
  9. */
  10. #include <errno.h>
  11. #include <device.h>
  12. #include <init.h>
  13. #include <kernel.h>
  14. #include <string.h>
  15. #include <soc.h>
  16. #include <board_cfg.h>
  17. #include <drivers/alarm.h>
  18. #include <logging/log.h>
  19. LOG_MODULE_REGISTER(alarm8hz0, CONFIG_LOG_DEFAULT_LEVEL);
  20. struct acts_alarm8hz_data {
  21. void (*alarm_cb_fn)(const void *cb_data);
  22. const void *cb_data;
  23. uint8_t alarm_wakeup : 1; /* System wakeup from alarm8hz indicator */
  24. };
  25. static int alarm8hz_acts_set_alarm(const struct device *dev, struct alarm_config *config, bool enable)
  26. {
  27. struct acts_alarm8hz_data *data = dev->data;
  28. int ret = 0;
  29. if ((!config) && enable) {
  30. LOG_ERR("Invalid parameter");
  31. return -EINVAL;
  32. }
  33. if (enable) {
  34. ret = soc_pmu_alarm8hz_enable(config->alarm_msec);
  35. if (ret)
  36. return ret;
  37. data->alarm_cb_fn = config->cb_fn;
  38. data->cb_data = config->cb_data;
  39. } else {
  40. soc_pmu_alarm8hz_disable();
  41. data->alarm_cb_fn = NULL;
  42. data->cb_data = NULL;
  43. }
  44. return ret;
  45. }
  46. static int alarm8hz_acts_get_alarm(const struct device *dev, struct alarm_status *sts)
  47. {
  48. struct acts_alarm8hz_data *data = dev->data;
  49. if (!sts) {
  50. LOG_ERR("Invalid parameter");
  51. return -EINVAL;
  52. }
  53. if (data->alarm_cb_fn)
  54. sts->is_on = 1;
  55. else
  56. sts->is_on = 0;
  57. sts->alarm_msec = soc_pmu_get_alarm8hz() * 125;
  58. return 0;
  59. }
  60. static bool alarm8hz_acts_is_alarm_wakeup(const struct device *dev)
  61. {
  62. struct acts_alarm8hz_data *data = dev->data;
  63. if (data->alarm_wakeup)
  64. return true;
  65. else
  66. return false;
  67. }
  68. static void alarm8hz_acts_pmu_notify(void *cb_data, int state)
  69. {
  70. const struct device *dev = (const struct device *)cb_data;
  71. struct acts_alarm8hz_data *data = dev->data;
  72. LOG_DBG("on alarm8hz func:%p", data->alarm_cb_fn);
  73. LOG_INF("alarm8hz wakeup cur_8hz=%d\n", soc_pmu_get_counter8hz_cycles(false));
  74. if (data->alarm_cb_fn)
  75. data->alarm_cb_fn(data->cb_data);
  76. }
  77. const struct alarm_driver_api alarm8hz_acts_driver_api = {
  78. .set_alarm = alarm8hz_acts_set_alarm,
  79. .get_alarm = alarm8hz_acts_get_alarm,
  80. .is_alarm_wakeup = alarm8hz_acts_is_alarm_wakeup,
  81. };
  82. int alarm8hz_acts_init(const struct device *dev)
  83. {
  84. struct acts_alarm8hz_data *data = dev->data;
  85. struct detect_param_t detect_param = {PMU_DETECT_DEV_ALARM8HZ, alarm8hz_acts_pmu_notify, (void *)dev};
  86. if (soc_pmu_is_alarm_wakeup()) {
  87. data->alarm_wakeup = true;
  88. LOG_INF("alarm8hz wakeup");
  89. }
  90. soc_pmu_counter8hz_enable();
  91. if (soc_pmu_register_notify(&detect_param)) {
  92. LOG_ERR("failed to register pmu notify");
  93. return -ENXIO;
  94. }
  95. printk("alarm8hz initialized\n");
  96. return 0;
  97. }
  98. struct acts_alarm8hz_data alarm8hz_acts_ddata;
  99. DEVICE_DEFINE(alarm8hz0, CONFIG_ALARM8HZ_0_NAME, alarm8hz_acts_init, NULL,
  100. &alarm8hz_acts_ddata, NULL,
  101. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
  102. &alarm8hz_acts_driver_api);