drv_adc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <drivers/adc.h>
  31. #include <ioremap.h>
  32. #include <rtthread.h>
  33. #include <rthw.h>
  34. #include <rtdevice.h>
  35. #include <riscv_io.h>
  36. #include <string.h>
  37. #include "board.h"
  38. #include "drv_adc.h"
  39. struct k230_adc adc_dev;
  40. static int k230_adc_hw_init(struct k230_adc_regs *adc_regs)
  41. {
  42. rt_uint32_t reg;
  43. reg = readl(&adc_regs->trim_reg);
  44. reg &= (~(0x1));
  45. writel(reg, &adc_regs->trim_reg);
  46. reg = readl(&adc_regs->trim_reg);
  47. reg |= 0x1;
  48. writel(reg, &adc_regs->trim_reg);
  49. reg = readl(&adc_regs->trim_reg);
  50. reg |= (0x1 << 20);
  51. writel(reg, &adc_regs->trim_reg);
  52. /* delay 150us */
  53. rt_hw_us_delay(150);
  54. reg &= ~(0x1 << 20);
  55. writel(reg, &adc_regs->trim_reg);
  56. writel(0x0, &adc_regs->mode_reg);
  57. return RT_EOK;
  58. }
  59. static int k230_adc_init()
  60. {
  61. int i;
  62. adc_dev.adc_regs = (struct k230_adc_regs*)rt_ioremap((void *)ADC_BASE_ADDR, ADC_IO_SIZE);
  63. for (i = 0; i < ADC_MAX_DMA_CHN; i++)
  64. {
  65. adc_dev.chn[i].chn_num = i;
  66. adc_dev.chn[i].enabled = 0;
  67. }
  68. k230_adc_hw_init(adc_dev.adc_regs);
  69. return RT_EOK;
  70. }
  71. static int k_adc_drv_enabled(struct k230_adc_regs *adc_regs)
  72. {
  73. rt_uint32_t reg;
  74. reg = readl(&adc_regs->trim_reg);
  75. reg |= 0x1;
  76. writel(reg, &adc_regs->trim_reg);
  77. return RT_EOK;
  78. }
  79. static int k_adc_drv_disabled(struct k230_adc_regs *adc_regs)
  80. {
  81. rt_uint32_t reg;
  82. reg = readl(&adc_regs->trim_reg);
  83. reg = reg & (~(0x1));
  84. writel(reg, &adc_regs->trim_reg);
  85. return RT_EOK;
  86. }
  87. rt_err_t k230_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
  88. {
  89. if (channel >= ADC_MAX_CHANNEL)
  90. return -RT_ERROR;
  91. struct k230_adc *kd_adc = rt_container_of(device, struct k230_adc, dev);
  92. kd_adc->chn[channel].enabled = 1;
  93. if (enabled)
  94. {
  95. kd_adc->chn[channel].enabled = 1;
  96. }
  97. else
  98. {
  99. kd_adc->chn[channel].enabled = 0;
  100. }
  101. return RT_EOK;
  102. }
  103. rt_err_t k230_get_adc_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
  104. {
  105. if (channel >= ADC_MAX_CHANNEL)
  106. return -RT_ERROR;
  107. struct k230_adc *kd_adc = rt_container_of(device, struct k230_adc, dev);
  108. if (!kd_adc->chn[channel].enabled)
  109. return -RT_ERROR;
  110. writel(channel | 0x10, &kd_adc->adc_regs->cfg_reg);
  111. while ((readl(&kd_adc->adc_regs->cfg_reg) & 0x10000) == 0);
  112. *value = readl(&kd_adc->adc_regs->data_reg[channel]);
  113. return RT_EOK;
  114. }
  115. static const struct rt_adc_ops k230_adc_ops =
  116. {
  117. .enabled = k230_adc_enabled,
  118. .convert = k230_get_adc_value,
  119. };
  120. int rt_hw_adc_init(void)
  121. {
  122. k230_adc_init();
  123. rt_hw_adc_register(&adc_dev.dev, K230_ADC_NAME, &k230_adc_ops, NULL);
  124. return RT_EOK;
  125. }
  126. INIT_BOARD_EXPORT(rt_hw_adc_init);