acpi_apd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD ACPI support for ACPI2platform device.
  4. *
  5. * Copyright (c) 2014,2015 AMD Corporation.
  6. * Authors: Ken Xue <Ken.Xue@amd.com>
  7. * Wu, Jeff <Jeff.Wu@amd.com>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/clkdev.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/platform_data/clk-fch.h>
  15. #include <linux/platform_device.h>
  16. #include "internal.h"
  17. struct apd_private_data;
  18. /**
  19. * struct apd_device_desc - a descriptor for apd device
  20. * @fixed_clk_rate: fixed rate input clock source for acpi device;
  21. * 0 means no fixed rate input clock source
  22. * @properties: build-in properties of the device such as UART
  23. * @setup: a hook routine to set device resource during create platform device
  24. *
  25. * Device description defined as acpi_device_id.driver_data
  26. */
  27. struct apd_device_desc {
  28. unsigned int fixed_clk_rate;
  29. struct property_entry *properties;
  30. int (*setup)(struct apd_private_data *pdata);
  31. };
  32. struct apd_private_data {
  33. struct clk *clk;
  34. struct acpi_device *adev;
  35. const struct apd_device_desc *dev_desc;
  36. };
  37. #if defined(CONFIG_X86_AMD_PLATFORM_DEVICE) || defined(CONFIG_ARM64)
  38. #define APD_ADDR(desc) ((unsigned long)&desc)
  39. static int acpi_apd_setup(struct apd_private_data *pdata)
  40. {
  41. const struct apd_device_desc *dev_desc = pdata->dev_desc;
  42. struct clk *clk;
  43. if (dev_desc->fixed_clk_rate) {
  44. clk = clk_register_fixed_rate(&pdata->adev->dev,
  45. dev_name(&pdata->adev->dev),
  46. NULL, 0, dev_desc->fixed_clk_rate);
  47. clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev));
  48. pdata->clk = clk;
  49. }
  50. return 0;
  51. }
  52. #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
  53. static int misc_check_res(struct acpi_resource *ares, void *data)
  54. {
  55. struct resource res;
  56. return !acpi_dev_resource_memory(ares, &res);
  57. }
  58. static int fch_misc_setup(struct apd_private_data *pdata)
  59. {
  60. struct acpi_device *adev = pdata->adev;
  61. const union acpi_object *obj;
  62. struct platform_device *clkdev;
  63. struct fch_clk_data *clk_data;
  64. struct resource_entry *rentry;
  65. struct list_head resource_list;
  66. int ret;
  67. clk_data = devm_kzalloc(&adev->dev, sizeof(*clk_data), GFP_KERNEL);
  68. if (!clk_data)
  69. return -ENOMEM;
  70. INIT_LIST_HEAD(&resource_list);
  71. ret = acpi_dev_get_resources(adev, &resource_list, misc_check_res,
  72. NULL);
  73. if (ret < 0)
  74. return -ENOENT;
  75. if (!acpi_dev_get_property(adev, "is-rv", ACPI_TYPE_INTEGER, &obj))
  76. clk_data->is_rv = obj->integer.value;
  77. list_for_each_entry(rentry, &resource_list, node) {
  78. clk_data->base = devm_ioremap(&adev->dev, rentry->res->start,
  79. resource_size(rentry->res));
  80. break;
  81. }
  82. acpi_dev_free_resource_list(&resource_list);
  83. clkdev = platform_device_register_data(&adev->dev, "clk-fch",
  84. PLATFORM_DEVID_NONE, clk_data,
  85. sizeof(*clk_data));
  86. return PTR_ERR_OR_ZERO(clkdev);
  87. }
  88. static const struct apd_device_desc cz_i2c_desc = {
  89. .setup = acpi_apd_setup,
  90. .fixed_clk_rate = 133000000,
  91. };
  92. static const struct apd_device_desc wt_i2c_desc = {
  93. .setup = acpi_apd_setup,
  94. .fixed_clk_rate = 150000000,
  95. };
  96. static struct property_entry uart_properties[] = {
  97. PROPERTY_ENTRY_U32("reg-io-width", 4),
  98. PROPERTY_ENTRY_U32("reg-shift", 2),
  99. PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
  100. { },
  101. };
  102. static const struct apd_device_desc cz_uart_desc = {
  103. .setup = acpi_apd_setup,
  104. .fixed_clk_rate = 48000000,
  105. .properties = uart_properties,
  106. };
  107. static const struct apd_device_desc fch_misc_desc = {
  108. .setup = fch_misc_setup,
  109. };
  110. #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */
  111. #ifdef CONFIG_ARM64
  112. static const struct apd_device_desc xgene_i2c_desc = {
  113. .setup = acpi_apd_setup,
  114. .fixed_clk_rate = 100000000,
  115. };
  116. static const struct apd_device_desc vulcan_spi_desc = {
  117. .setup = acpi_apd_setup,
  118. .fixed_clk_rate = 133000000,
  119. };
  120. static const struct apd_device_desc hip07_i2c_desc = {
  121. .setup = acpi_apd_setup,
  122. .fixed_clk_rate = 200000000,
  123. };
  124. static const struct apd_device_desc hip08_i2c_desc = {
  125. .setup = acpi_apd_setup,
  126. .fixed_clk_rate = 250000000,
  127. };
  128. static const struct apd_device_desc hip08_lite_i2c_desc = {
  129. .setup = acpi_apd_setup,
  130. .fixed_clk_rate = 125000000,
  131. };
  132. static const struct apd_device_desc thunderx2_i2c_desc = {
  133. .setup = acpi_apd_setup,
  134. .fixed_clk_rate = 125000000,
  135. };
  136. static const struct apd_device_desc nxp_i2c_desc = {
  137. .setup = acpi_apd_setup,
  138. .fixed_clk_rate = 350000000,
  139. };
  140. static const struct apd_device_desc hip08_spi_desc = {
  141. .setup = acpi_apd_setup,
  142. .fixed_clk_rate = 250000000,
  143. };
  144. #endif /* CONFIG_ARM64 */
  145. #endif
  146. /**
  147. * Create platform device during acpi scan attach handle.
  148. * Return value > 0 on success of creating device.
  149. */
  150. static int acpi_apd_create_device(struct acpi_device *adev,
  151. const struct acpi_device_id *id)
  152. {
  153. const struct apd_device_desc *dev_desc = (void *)id->driver_data;
  154. struct apd_private_data *pdata;
  155. struct platform_device *pdev;
  156. int ret;
  157. if (!dev_desc) {
  158. pdev = acpi_create_platform_device(adev, NULL);
  159. return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
  160. }
  161. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  162. if (!pdata)
  163. return -ENOMEM;
  164. pdata->adev = adev;
  165. pdata->dev_desc = dev_desc;
  166. if (dev_desc->setup) {
  167. ret = dev_desc->setup(pdata);
  168. if (ret)
  169. goto err_out;
  170. }
  171. adev->driver_data = pdata;
  172. pdev = acpi_create_platform_device(adev, dev_desc->properties);
  173. if (!IS_ERR_OR_NULL(pdev))
  174. return 1;
  175. ret = PTR_ERR(pdev);
  176. adev->driver_data = NULL;
  177. err_out:
  178. kfree(pdata);
  179. return ret;
  180. }
  181. static const struct acpi_device_id acpi_apd_device_ids[] = {
  182. /* Generic apd devices */
  183. #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
  184. { "AMD0010", APD_ADDR(cz_i2c_desc) },
  185. { "AMDI0010", APD_ADDR(wt_i2c_desc) },
  186. { "AMD0020", APD_ADDR(cz_uart_desc) },
  187. { "AMDI0020", APD_ADDR(cz_uart_desc) },
  188. { "AMDI0022", APD_ADDR(cz_uart_desc) },
  189. { "AMD0030", },
  190. { "AMD0040", APD_ADDR(fch_misc_desc)},
  191. { "HYGO0010", APD_ADDR(wt_i2c_desc) },
  192. #endif
  193. #ifdef CONFIG_ARM64
  194. { "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
  195. { "BRCM900D", APD_ADDR(vulcan_spi_desc) },
  196. { "CAV900D", APD_ADDR(vulcan_spi_desc) },
  197. { "CAV9007", APD_ADDR(thunderx2_i2c_desc) },
  198. { "HISI02A1", APD_ADDR(hip07_i2c_desc) },
  199. { "HISI02A2", APD_ADDR(hip08_i2c_desc) },
  200. { "HISI02A3", APD_ADDR(hip08_lite_i2c_desc) },
  201. { "HISI0173", APD_ADDR(hip08_spi_desc) },
  202. { "NXP0001", APD_ADDR(nxp_i2c_desc) },
  203. #endif
  204. { }
  205. };
  206. static struct acpi_scan_handler apd_handler = {
  207. .ids = acpi_apd_device_ids,
  208. .attach = acpi_apd_create_device,
  209. };
  210. void __init acpi_apd_init(void)
  211. {
  212. acpi_scan_add_handler(&apd_handler);
  213. }