clk-fixed-rate.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <drivers/platform.h>
  13. static rt_err_t fixed_clk_ofw_init(struct rt_platform_device *pdev, struct rt_clk_fixed_rate *clk_fixed)
  14. {
  15. rt_err_t err = RT_EOK;
  16. rt_uint32_t rate, accuracy;
  17. struct rt_ofw_node *np = pdev->parent.ofw_node;
  18. const char *clk_name = np->name;
  19. if (!rt_ofw_prop_read_u32(np, "clock-frequency", &rate))
  20. {
  21. rt_ofw_prop_read_u32(np, "clock-accuracy", &accuracy);
  22. rt_ofw_prop_read_string(np, "clock-output-names", &clk_name);
  23. clk_fixed->clk.name = clk_name;
  24. clk_fixed->clk.rate = rate;
  25. clk_fixed->clk.min_rate = rate;
  26. clk_fixed->clk.max_rate = rate;
  27. clk_fixed->fixed_rate = rate;
  28. clk_fixed->fixed_accuracy = accuracy;
  29. rt_ofw_data(np) = &clk_fixed->clk;
  30. }
  31. else
  32. {
  33. err = -RT_EIO;
  34. }
  35. return err;
  36. }
  37. static rt_err_t fixed_clk_probe(struct rt_platform_device *pdev)
  38. {
  39. rt_err_t err = RT_EOK;
  40. struct rt_clk_fixed_rate *clk_fixed = rt_calloc(1, sizeof(*clk_fixed));
  41. if (clk_fixed)
  42. {
  43. err = fixed_clk_ofw_init(pdev, clk_fixed);
  44. }
  45. else
  46. {
  47. err = -RT_ENOMEM;
  48. }
  49. if (!err)
  50. {
  51. err = rt_clk_register(&clk_fixed->clk, RT_NULL);
  52. }
  53. if (err && clk_fixed)
  54. {
  55. rt_free(clk_fixed);
  56. }
  57. return err;
  58. }
  59. static const struct rt_ofw_node_id fixed_clk_ofw_ids[] =
  60. {
  61. { .compatible = "fixed-clock" },
  62. { /* sentinel */ }
  63. };
  64. static struct rt_platform_driver fixed_clk_driver =
  65. {
  66. .name = "clk-fixed-rate",
  67. .ids = fixed_clk_ofw_ids,
  68. .probe = fixed_clk_probe,
  69. };
  70. static int fixed_clk_drv_register(void)
  71. {
  72. rt_platform_driver_register(&fixed_clk_driver);
  73. return 0;
  74. }
  75. INIT_SUBSYS_EXPORT(fixed_clk_drv_register);