acpi_apd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_data/clk-st.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_domain.h>
  16. #include <linux/clkdev.h>
  17. #include <linux/acpi.h>
  18. #include <linux/err.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. struct property_entry *properties;
  42. int (*setup)(struct apd_private_data *pdata);
  43. };
  44. struct apd_private_data {
  45. struct clk *clk;
  46. struct acpi_device *adev;
  47. const struct apd_device_desc *dev_desc;
  48. };
  49. #if defined(CONFIG_X86_AMD_PLATFORM_DEVICE) || defined(CONFIG_ARM64)
  50. #define APD_ADDR(desc) ((unsigned long)&desc)
  51. static int acpi_apd_setup(struct apd_private_data *pdata)
  52. {
  53. const struct apd_device_desc *dev_desc = pdata->dev_desc;
  54. struct clk *clk = ERR_PTR(-ENODEV);
  55. if (dev_desc->fixed_clk_rate) {
  56. clk = clk_register_fixed_rate(&pdata->adev->dev,
  57. dev_name(&pdata->adev->dev),
  58. NULL, 0, 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. #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
  65. static int misc_check_res(struct acpi_resource *ares, void *data)
  66. {
  67. struct resource res;
  68. return !acpi_dev_resource_memory(ares, &res);
  69. }
  70. static int st_misc_setup(struct apd_private_data *pdata)
  71. {
  72. struct acpi_device *adev = pdata->adev;
  73. struct platform_device *clkdev;
  74. struct st_clk_data *clk_data;
  75. struct resource_entry *rentry;
  76. struct list_head resource_list;
  77. int ret;
  78. clk_data = devm_kzalloc(&adev->dev, sizeof(*clk_data), GFP_KERNEL);
  79. if (!clk_data)
  80. return -ENOMEM;
  81. INIT_LIST_HEAD(&resource_list);
  82. ret = acpi_dev_get_resources(adev, &resource_list, misc_check_res,
  83. NULL);
  84. if (ret < 0)
  85. return -ENOENT;
  86. list_for_each_entry(rentry, &resource_list, node) {
  87. clk_data->base = devm_ioremap(&adev->dev, rentry->res->start,
  88. resource_size(rentry->res));
  89. break;
  90. }
  91. acpi_dev_free_resource_list(&resource_list);
  92. clkdev = platform_device_register_data(&adev->dev, "clk-st",
  93. PLATFORM_DEVID_NONE, clk_data,
  94. sizeof(*clk_data));
  95. return PTR_ERR_OR_ZERO(clkdev);
  96. }
  97. static const struct apd_device_desc cz_i2c_desc = {
  98. .setup = acpi_apd_setup,
  99. .fixed_clk_rate = 133000000,
  100. };
  101. static const struct apd_device_desc wt_i2c_desc = {
  102. .setup = acpi_apd_setup,
  103. .fixed_clk_rate = 150000000,
  104. };
  105. static struct property_entry uart_properties[] = {
  106. PROPERTY_ENTRY_U32("reg-io-width", 4),
  107. PROPERTY_ENTRY_U32("reg-shift", 2),
  108. PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
  109. { },
  110. };
  111. static const struct apd_device_desc cz_uart_desc = {
  112. .setup = acpi_apd_setup,
  113. .fixed_clk_rate = 48000000,
  114. .properties = uart_properties,
  115. };
  116. static const struct apd_device_desc st_misc_desc = {
  117. .setup = st_misc_setup,
  118. };
  119. #endif
  120. #ifdef CONFIG_ARM64
  121. static const struct apd_device_desc xgene_i2c_desc = {
  122. .setup = acpi_apd_setup,
  123. .fixed_clk_rate = 100000000,
  124. };
  125. static const struct apd_device_desc vulcan_spi_desc = {
  126. .setup = acpi_apd_setup,
  127. .fixed_clk_rate = 133000000,
  128. };
  129. static const struct apd_device_desc hip07_i2c_desc = {
  130. .setup = acpi_apd_setup,
  131. .fixed_clk_rate = 200000000,
  132. };
  133. static const struct apd_device_desc hip08_i2c_desc = {
  134. .setup = acpi_apd_setup,
  135. .fixed_clk_rate = 250000000,
  136. };
  137. static const struct apd_device_desc thunderx2_i2c_desc = {
  138. .setup = acpi_apd_setup,
  139. .fixed_clk_rate = 125000000,
  140. };
  141. #endif
  142. #else
  143. #define APD_ADDR(desc) (0UL)
  144. #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */
  145. /**
  146. * Create platform device during acpi scan attach handle.
  147. * Return value > 0 on success of creating device.
  148. */
  149. static int acpi_apd_create_device(struct acpi_device *adev,
  150. const struct acpi_device_id *id)
  151. {
  152. const struct apd_device_desc *dev_desc = (void *)id->driver_data;
  153. struct apd_private_data *pdata;
  154. struct platform_device *pdev;
  155. int ret;
  156. if (!dev_desc) {
  157. pdev = acpi_create_platform_device(adev, NULL);
  158. return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
  159. }
  160. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  161. if (!pdata)
  162. return -ENOMEM;
  163. pdata->adev = adev;
  164. pdata->dev_desc = dev_desc;
  165. if (dev_desc->setup) {
  166. ret = dev_desc->setup(pdata);
  167. if (ret)
  168. goto err_out;
  169. }
  170. adev->driver_data = pdata;
  171. pdev = acpi_create_platform_device(adev, dev_desc->properties);
  172. if (!IS_ERR_OR_NULL(pdev))
  173. return 1;
  174. ret = PTR_ERR(pdev);
  175. adev->driver_data = NULL;
  176. err_out:
  177. kfree(pdata);
  178. return ret;
  179. }
  180. static const struct acpi_device_id acpi_apd_device_ids[] = {
  181. /* Generic apd devices */
  182. #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
  183. { "AMD0010", APD_ADDR(cz_i2c_desc) },
  184. { "AMDI0010", APD_ADDR(wt_i2c_desc) },
  185. { "AMD0020", APD_ADDR(cz_uart_desc) },
  186. { "AMDI0020", APD_ADDR(cz_uart_desc) },
  187. { "AMD0030", },
  188. { "AMD0040", APD_ADDR(st_misc_desc)},
  189. #endif
  190. #ifdef CONFIG_ARM64
  191. { "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
  192. { "BRCM900D", APD_ADDR(vulcan_spi_desc) },
  193. { "CAV900D", APD_ADDR(vulcan_spi_desc) },
  194. { "CAV9007", APD_ADDR(thunderx2_i2c_desc) },
  195. { "HISI02A1", APD_ADDR(hip07_i2c_desc) },
  196. { "HISI02A2", APD_ADDR(hip08_i2c_desc) },
  197. #endif
  198. { }
  199. };
  200. static struct acpi_scan_handler apd_handler = {
  201. .ids = acpi_apd_device_ids,
  202. .attach = acpi_apd_create_device,
  203. };
  204. void __init acpi_apd_init(void)
  205. {
  206. acpi_scan_add_handler(&apd_handler);
  207. }