acpi_apd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <[email protected]>
  7. * Wu, Jeff <[email protected]>
  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 fch_misc_setup(struct apd_private_data *pdata)
  54. {
  55. struct acpi_device *adev = pdata->adev;
  56. const union acpi_object *obj;
  57. struct platform_device *clkdev;
  58. struct fch_clk_data *clk_data;
  59. struct resource_entry *rentry;
  60. struct list_head resource_list;
  61. int ret;
  62. clk_data = devm_kzalloc(&adev->dev, sizeof(*clk_data), GFP_KERNEL);
  63. if (!clk_data)
  64. return -ENOMEM;
  65. INIT_LIST_HEAD(&resource_list);
  66. ret = acpi_dev_get_memory_resources(adev, &resource_list);
  67. if (ret < 0)
  68. return -ENOENT;
  69. if (!acpi_dev_get_property(adev, "clk-name", ACPI_TYPE_STRING, &obj)) {
  70. clk_data->name = devm_kzalloc(&adev->dev, obj->string.length,
  71. GFP_KERNEL);
  72. if (!clk_data->name)
  73. return -ENOMEM;
  74. strcpy(clk_data->name, obj->string.pointer);
  75. } else {
  76. /* Set default name to mclk if entry missing in firmware */
  77. clk_data->name = "mclk";
  78. }
  79. list_for_each_entry(rentry, &resource_list, node) {
  80. clk_data->base = devm_ioremap(&adev->dev, rentry->res->start,
  81. resource_size(rentry->res));
  82. break;
  83. }
  84. if (!clk_data->base)
  85. return -ENOMEM;
  86. acpi_dev_free_resource_list(&resource_list);
  87. clkdev = platform_device_register_data(&adev->dev, "clk-fch",
  88. PLATFORM_DEVID_NONE, clk_data,
  89. sizeof(*clk_data));
  90. return PTR_ERR_OR_ZERO(clkdev);
  91. }
  92. static const struct apd_device_desc cz_i2c_desc = {
  93. .setup = acpi_apd_setup,
  94. .fixed_clk_rate = 133000000,
  95. };
  96. static const struct apd_device_desc wt_i2c_desc = {
  97. .setup = acpi_apd_setup,
  98. .fixed_clk_rate = 150000000,
  99. };
  100. static struct property_entry uart_properties[] = {
  101. PROPERTY_ENTRY_U32("reg-io-width", 4),
  102. PROPERTY_ENTRY_U32("reg-shift", 2),
  103. PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
  104. { },
  105. };
  106. static const struct apd_device_desc cz_uart_desc = {
  107. .setup = acpi_apd_setup,
  108. .fixed_clk_rate = 48000000,
  109. .properties = uart_properties,
  110. };
  111. static const struct apd_device_desc fch_misc_desc = {
  112. .setup = fch_misc_setup,
  113. };
  114. #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */
  115. #ifdef CONFIG_ARM64
  116. static const struct apd_device_desc xgene_i2c_desc = {
  117. .setup = acpi_apd_setup,
  118. .fixed_clk_rate = 100000000,
  119. };
  120. static const struct apd_device_desc vulcan_spi_desc = {
  121. .setup = acpi_apd_setup,
  122. .fixed_clk_rate = 133000000,
  123. };
  124. static const struct apd_device_desc hip07_i2c_desc = {
  125. .setup = acpi_apd_setup,
  126. .fixed_clk_rate = 200000000,
  127. };
  128. static const struct apd_device_desc hip08_i2c_desc = {
  129. .setup = acpi_apd_setup,
  130. .fixed_clk_rate = 250000000,
  131. };
  132. static const struct apd_device_desc hip08_lite_i2c_desc = {
  133. .setup = acpi_apd_setup,
  134. .fixed_clk_rate = 125000000,
  135. };
  136. static const struct apd_device_desc thunderx2_i2c_desc = {
  137. .setup = acpi_apd_setup,
  138. .fixed_clk_rate = 125000000,
  139. };
  140. static const struct apd_device_desc nxp_i2c_desc = {
  141. .setup = acpi_apd_setup,
  142. .fixed_clk_rate = 350000000,
  143. };
  144. static const struct apd_device_desc hip08_spi_desc = {
  145. .setup = acpi_apd_setup,
  146. .fixed_clk_rate = 250000000,
  147. };
  148. #endif /* CONFIG_ARM64 */
  149. #endif
  150. /*
  151. * Create platform device during acpi scan attach handle.
  152. * Return value > 0 on success of creating device.
  153. */
  154. static int acpi_apd_create_device(struct acpi_device *adev,
  155. const struct acpi_device_id *id)
  156. {
  157. const struct apd_device_desc *dev_desc = (void *)id->driver_data;
  158. struct apd_private_data *pdata;
  159. struct platform_device *pdev;
  160. int ret;
  161. if (!dev_desc) {
  162. pdev = acpi_create_platform_device(adev, NULL);
  163. return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
  164. }
  165. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  166. if (!pdata)
  167. return -ENOMEM;
  168. pdata->adev = adev;
  169. pdata->dev_desc = dev_desc;
  170. if (dev_desc->setup) {
  171. ret = dev_desc->setup(pdata);
  172. if (ret)
  173. goto err_out;
  174. }
  175. adev->driver_data = pdata;
  176. pdev = acpi_create_platform_device(adev, dev_desc->properties);
  177. if (!IS_ERR_OR_NULL(pdev))
  178. return 1;
  179. ret = PTR_ERR(pdev);
  180. adev->driver_data = NULL;
  181. err_out:
  182. kfree(pdata);
  183. return ret;
  184. }
  185. static const struct acpi_device_id acpi_apd_device_ids[] = {
  186. /* Generic apd devices */
  187. #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
  188. { "AMD0010", APD_ADDR(cz_i2c_desc) },
  189. { "AMD0020", APD_ADDR(cz_uart_desc) },
  190. { "AMD0030", },
  191. { "AMD0040", APD_ADDR(fch_misc_desc)},
  192. { "AMDI0010", APD_ADDR(wt_i2c_desc) },
  193. { "AMDI0019", APD_ADDR(wt_i2c_desc) },
  194. { "AMDI0020", APD_ADDR(cz_uart_desc) },
  195. { "AMDI0022", APD_ADDR(cz_uart_desc) },
  196. { "HYGO0010", APD_ADDR(wt_i2c_desc) },
  197. #endif
  198. #ifdef CONFIG_ARM64
  199. { "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
  200. { "BRCM900D", APD_ADDR(vulcan_spi_desc) },
  201. { "CAV900D", APD_ADDR(vulcan_spi_desc) },
  202. { "CAV9007", APD_ADDR(thunderx2_i2c_desc) },
  203. { "HISI02A1", APD_ADDR(hip07_i2c_desc) },
  204. { "HISI02A2", APD_ADDR(hip08_i2c_desc) },
  205. { "HISI02A3", APD_ADDR(hip08_lite_i2c_desc) },
  206. { "HISI0173", APD_ADDR(hip08_spi_desc) },
  207. { "NXP0001", APD_ADDR(nxp_i2c_desc) },
  208. #endif
  209. { }
  210. };
  211. static struct acpi_scan_handler apd_handler = {
  212. .ids = acpi_apd_device_ids,
  213. .attach = acpi_apd_create_device,
  214. };
  215. void __init acpi_apd_init(void)
  216. {
  217. acpi_scan_add_handler(&apd_handler);
  218. }