acpi_apd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * AMD ACPI support for ACPI2platform device.
  3. *
  4. * Copyright (c) 2014,2015 AMD Corporation.
  5. * Authors: Ken Xue <Ken.Xue@amd.com>
  6. * Wu, Jeff <Jeff.Wu@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/acpi.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/pm.h>
  20. #include "internal.h"
  21. ACPI_MODULE_NAME("acpi_apd");
  22. struct apd_private_data;
  23. /**
  24. * ACPI_APD_SYSFS : add device attributes in sysfs
  25. * ACPI_APD_PM : attach power domain to device
  26. */
  27. #define ACPI_APD_SYSFS BIT(0)
  28. #define ACPI_APD_PM BIT(1)
  29. /**
  30. * struct apd_device_desc - a descriptor for apd device
  31. * @flags: device flags like %ACPI_APD_SYSFS, %ACPI_APD_PM
  32. * @fixed_clk_rate: fixed rate input clock source for acpi device;
  33. * 0 means no fixed rate input clock source
  34. * @setup: a hook routine to set device resource during create platform device
  35. *
  36. * Device description defined as acpi_device_id.driver_data
  37. */
  38. struct apd_device_desc {
  39. unsigned int flags;
  40. unsigned int fixed_clk_rate;
  41. int (*setup)(struct apd_private_data *pdata);
  42. };
  43. struct apd_private_data {
  44. struct clk *clk;
  45. struct acpi_device *adev;
  46. const struct apd_device_desc *dev_desc;
  47. };
  48. #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
  49. #define APD_ADDR(desc) ((unsigned long)&desc)
  50. static int acpi_apd_setup(struct apd_private_data *pdata)
  51. {
  52. const struct apd_device_desc *dev_desc = pdata->dev_desc;
  53. struct clk *clk = ERR_PTR(-ENODEV);
  54. if (dev_desc->fixed_clk_rate) {
  55. clk = clk_register_fixed_rate(&pdata->adev->dev,
  56. dev_name(&pdata->adev->dev),
  57. NULL, CLK_IS_ROOT,
  58. dev_desc->fixed_clk_rate);
  59. clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev));
  60. pdata->clk = clk;
  61. }
  62. return 0;
  63. }
  64. static struct apd_device_desc cz_i2c_desc = {
  65. .setup = acpi_apd_setup,
  66. .fixed_clk_rate = 133000000,
  67. };
  68. static struct apd_device_desc cz_uart_desc = {
  69. .setup = acpi_apd_setup,
  70. .fixed_clk_rate = 48000000,
  71. };
  72. #else
  73. #define APD_ADDR(desc) (0UL)
  74. #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */
  75. /**
  76. * Create platform device during acpi scan attach handle.
  77. * Return value > 0 on success of creating device.
  78. */
  79. static int acpi_apd_create_device(struct acpi_device *adev,
  80. const struct acpi_device_id *id)
  81. {
  82. const struct apd_device_desc *dev_desc = (void *)id->driver_data;
  83. struct apd_private_data *pdata;
  84. struct platform_device *pdev;
  85. int ret;
  86. if (!dev_desc) {
  87. pdev = acpi_create_platform_device(adev);
  88. return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
  89. }
  90. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  91. if (!pdata)
  92. return -ENOMEM;
  93. pdata->adev = adev;
  94. pdata->dev_desc = dev_desc;
  95. if (dev_desc->setup) {
  96. ret = dev_desc->setup(pdata);
  97. if (ret)
  98. goto err_out;
  99. }
  100. adev->driver_data = pdata;
  101. pdev = acpi_create_platform_device(adev);
  102. if (!IS_ERR_OR_NULL(pdev))
  103. return 1;
  104. ret = PTR_ERR(pdev);
  105. adev->driver_data = NULL;
  106. err_out:
  107. kfree(pdata);
  108. return ret;
  109. }
  110. static const struct acpi_device_id acpi_apd_device_ids[] = {
  111. /* Generic apd devices */
  112. { "AMD0010", APD_ADDR(cz_i2c_desc) },
  113. { "AMD0020", APD_ADDR(cz_uart_desc) },
  114. { "AMD0030", },
  115. { }
  116. };
  117. static struct acpi_scan_handler apd_handler = {
  118. .ids = acpi_apd_device_ids,
  119. .attach = acpi_apd_create_device,
  120. };
  121. void __init acpi_apd_init(void)
  122. {
  123. acpi_scan_add_handler(&apd_handler);
  124. }