1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 292082a88SKen Xue /* 392082a88SKen Xue * AMD ACPI support for ACPI2platform device. 492082a88SKen Xue * 592082a88SKen Xue * Copyright (c) 2014,2015 AMD Corporation. 692082a88SKen Xue * Authors: Ken Xue <Ken.Xue@amd.com> 792082a88SKen Xue * Wu, Jeff <Jeff.Wu@amd.com> 892082a88SKen Xue */ 992082a88SKen Xue 1092082a88SKen Xue #include <linux/acpi.h> 1132c6f3ffSHanjun Guo #include <linux/clkdev.h> 1232c6f3ffSHanjun Guo #include <linux/clk-provider.h> 1392082a88SKen Xue #include <linux/err.h> 1462e59c4eSStephen Boyd #include <linux/io.h> 1532c6f3ffSHanjun Guo #include <linux/platform_data/clk-fch.h> 1632c6f3ffSHanjun Guo #include <linux/platform_device.h> 1792082a88SKen Xue 1892082a88SKen Xue #include "internal.h" 1992082a88SKen Xue 2092082a88SKen Xue struct apd_private_data; 2192082a88SKen Xue 2292082a88SKen Xue /** 2392082a88SKen Xue * struct apd_device_desc - a descriptor for apd device 2492082a88SKen Xue * @fixed_clk_rate: fixed rate input clock source for acpi device; 2592082a88SKen Xue * 0 means no fixed rate input clock source 26ad4a0f24SHanjun Guo * @properties: build-in properties of the device such as UART 2792082a88SKen Xue * @setup: a hook routine to set device resource during create platform device 2892082a88SKen Xue * 2992082a88SKen Xue * Device description defined as acpi_device_id.driver_data 3092082a88SKen Xue */ 3192082a88SKen Xue struct apd_device_desc { 3292082a88SKen Xue unsigned int fixed_clk_rate; 337ff55d17SHeikki Krogerus struct property_entry *properties; 3492082a88SKen Xue int (*setup)(struct apd_private_data *pdata); 3592082a88SKen Xue }; 3692082a88SKen Xue 3792082a88SKen Xue struct apd_private_data { 3892082a88SKen Xue struct clk *clk; 3992082a88SKen Xue struct acpi_device *adev; 4092082a88SKen Xue const struct apd_device_desc *dev_desc; 4192082a88SKen Xue }; 4292082a88SKen Xue 43b790eb20SLoc Ho #if defined(CONFIG_X86_AMD_PLATFORM_DEVICE) || defined(CONFIG_ARM64) 4492082a88SKen Xue #define APD_ADDR(desc) ((unsigned long)&desc) 4592082a88SKen Xue 4692082a88SKen Xue static int acpi_apd_setup(struct apd_private_data *pdata) 4792082a88SKen Xue { 4892082a88SKen Xue const struct apd_device_desc *dev_desc = pdata->dev_desc; 49b244883cSColin Ian King struct clk *clk; 5092082a88SKen Xue 5192082a88SKen Xue if (dev_desc->fixed_clk_rate) { 5292082a88SKen Xue clk = clk_register_fixed_rate(&pdata->adev->dev, 5392082a88SKen Xue dev_name(&pdata->adev->dev), 54cdd9a6b2SStephen Boyd NULL, 0, dev_desc->fixed_clk_rate); 5592082a88SKen Xue clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev)); 5692082a88SKen Xue pdata->clk = clk; 5792082a88SKen Xue } 5892082a88SKen Xue 5992082a88SKen Xue return 0; 6092082a88SKen Xue } 6192082a88SKen Xue 62b790eb20SLoc Ho #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE 633f4ba94eSAkshu Agrawal static int misc_check_res(struct acpi_resource *ares, void *data) 643f4ba94eSAkshu Agrawal { 653f4ba94eSAkshu Agrawal struct resource res; 663f4ba94eSAkshu Agrawal 673f4ba94eSAkshu Agrawal return !acpi_dev_resource_memory(ares, &res); 683f4ba94eSAkshu Agrawal } 693f4ba94eSAkshu Agrawal 70d58669b0SAkshu Agrawal static int fch_misc_setup(struct apd_private_data *pdata) 713f4ba94eSAkshu Agrawal { 723f4ba94eSAkshu Agrawal struct acpi_device *adev = pdata->adev; 737f8802f2SAkshu Agrawal const union acpi_object *obj; 743f4ba94eSAkshu Agrawal struct platform_device *clkdev; 75d58669b0SAkshu Agrawal struct fch_clk_data *clk_data; 763f4ba94eSAkshu Agrawal struct resource_entry *rentry; 773f4ba94eSAkshu Agrawal struct list_head resource_list; 783f4ba94eSAkshu Agrawal int ret; 793f4ba94eSAkshu Agrawal 803f4ba94eSAkshu Agrawal clk_data = devm_kzalloc(&adev->dev, sizeof(*clk_data), GFP_KERNEL); 813f4ba94eSAkshu Agrawal if (!clk_data) 823f4ba94eSAkshu Agrawal return -ENOMEM; 833f4ba94eSAkshu Agrawal 843f4ba94eSAkshu Agrawal INIT_LIST_HEAD(&resource_list); 853f4ba94eSAkshu Agrawal ret = acpi_dev_get_resources(adev, &resource_list, misc_check_res, 863f4ba94eSAkshu Agrawal NULL); 873f4ba94eSAkshu Agrawal if (ret < 0) 883f4ba94eSAkshu Agrawal return -ENOENT; 893f4ba94eSAkshu Agrawal 906556b0a8SFurquan Shaikh if (!acpi_dev_get_property(adev, "is-rv", ACPI_TYPE_INTEGER, &obj)) 917f8802f2SAkshu Agrawal clk_data->is_rv = obj->integer.value; 927f8802f2SAkshu Agrawal 933f4ba94eSAkshu Agrawal list_for_each_entry(rentry, &resource_list, node) { 943f4ba94eSAkshu Agrawal clk_data->base = devm_ioremap(&adev->dev, rentry->res->start, 953f4ba94eSAkshu Agrawal resource_size(rentry->res)); 963f4ba94eSAkshu Agrawal break; 973f4ba94eSAkshu Agrawal } 98*2cea3ec5SJiasheng Jiang if (!clk_data->base) 99*2cea3ec5SJiasheng Jiang return -ENOMEM; 1003f4ba94eSAkshu Agrawal 1013f4ba94eSAkshu Agrawal acpi_dev_free_resource_list(&resource_list); 1023f4ba94eSAkshu Agrawal 103d58669b0SAkshu Agrawal clkdev = platform_device_register_data(&adev->dev, "clk-fch", 1043f4ba94eSAkshu Agrawal PLATFORM_DEVID_NONE, clk_data, 1053f4ba94eSAkshu Agrawal sizeof(*clk_data)); 1063f4ba94eSAkshu Agrawal return PTR_ERR_OR_ZERO(clkdev); 1073f4ba94eSAkshu Agrawal } 1083f4ba94eSAkshu Agrawal 109a217726aSJulia Lawall static const struct apd_device_desc cz_i2c_desc = { 11092082a88SKen Xue .setup = acpi_apd_setup, 11192082a88SKen Xue .fixed_clk_rate = 133000000, 11292082a88SKen Xue }; 11392082a88SKen Xue 1144881f0baSNehal Shah static const struct apd_device_desc wt_i2c_desc = { 1154881f0baSNehal Shah .setup = acpi_apd_setup, 1164881f0baSNehal Shah .fixed_clk_rate = 150000000, 1174881f0baSNehal Shah }; 1184881f0baSNehal Shah 1197ff55d17SHeikki Krogerus static struct property_entry uart_properties[] = { 1207ff55d17SHeikki Krogerus PROPERTY_ENTRY_U32("reg-io-width", 4), 1217ff55d17SHeikki Krogerus PROPERTY_ENTRY_U32("reg-shift", 2), 1227ff55d17SHeikki Krogerus PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"), 1237ff55d17SHeikki Krogerus { }, 1247ff55d17SHeikki Krogerus }; 1257ff55d17SHeikki Krogerus 126a217726aSJulia Lawall static const struct apd_device_desc cz_uart_desc = { 12792082a88SKen Xue .setup = acpi_apd_setup, 12892082a88SKen Xue .fixed_clk_rate = 48000000, 1297ff55d17SHeikki Krogerus .properties = uart_properties, 13092082a88SKen Xue }; 1313f4ba94eSAkshu Agrawal 132d58669b0SAkshu Agrawal static const struct apd_device_desc fch_misc_desc = { 133d58669b0SAkshu Agrawal .setup = fch_misc_setup, 1343f4ba94eSAkshu Agrawal }; 135ee2bc5d2SHanjun Guo #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */ 136b790eb20SLoc Ho 137b790eb20SLoc Ho #ifdef CONFIG_ARM64 138a217726aSJulia Lawall static const struct apd_device_desc xgene_i2c_desc = { 139b790eb20SLoc Ho .setup = acpi_apd_setup, 140b790eb20SLoc Ho .fixed_clk_rate = 100000000, 141b790eb20SLoc Ho }; 142bd2058dcSKamlakant Patel 143a217726aSJulia Lawall static const struct apd_device_desc vulcan_spi_desc = { 144bd2058dcSKamlakant Patel .setup = acpi_apd_setup, 145bd2058dcSKamlakant Patel .fixed_clk_rate = 133000000, 146bd2058dcSKamlakant Patel }; 1476e14cf36SHanjun Guo 1486e14cf36SHanjun Guo static const struct apd_device_desc hip07_i2c_desc = { 1496e14cf36SHanjun Guo .setup = acpi_apd_setup, 1506e14cf36SHanjun Guo .fixed_clk_rate = 200000000, 1516e14cf36SHanjun Guo }; 1526e14cf36SHanjun Guo 1536e14cf36SHanjun Guo static const struct apd_device_desc hip08_i2c_desc = { 1546e14cf36SHanjun Guo .setup = acpi_apd_setup, 1556e14cf36SHanjun Guo .fixed_clk_rate = 250000000, 1566e14cf36SHanjun Guo }; 15711330a9fSChuanhua Han 158c01a4a13SHanjun Guo static const struct apd_device_desc hip08_lite_i2c_desc = { 159c01a4a13SHanjun Guo .setup = acpi_apd_setup, 160c01a4a13SHanjun Guo .fixed_clk_rate = 125000000, 161c01a4a13SHanjun Guo }; 162c01a4a13SHanjun Guo 1631977dbefSJayachandran C static const struct apd_device_desc thunderx2_i2c_desc = { 1641977dbefSJayachandran C .setup = acpi_apd_setup, 1651977dbefSJayachandran C .fixed_clk_rate = 125000000, 1661977dbefSJayachandran C }; 16756131d6dSJay Fang 16811330a9fSChuanhua Han static const struct apd_device_desc nxp_i2c_desc = { 16911330a9fSChuanhua Han .setup = acpi_apd_setup, 17011330a9fSChuanhua Han .fixed_clk_rate = 350000000, 17111330a9fSChuanhua Han }; 17211330a9fSChuanhua Han 17356131d6dSJay Fang static const struct apd_device_desc hip08_spi_desc = { 17456131d6dSJay Fang .setup = acpi_apd_setup, 17556131d6dSJay Fang .fixed_clk_rate = 250000000, 17656131d6dSJay Fang }; 177ee2bc5d2SHanjun Guo #endif /* CONFIG_ARM64 */ 178ee2bc5d2SHanjun Guo 179b790eb20SLoc Ho #endif 18092082a88SKen Xue 1810955b3a5SXiaofei Tan /* 18292082a88SKen Xue * Create platform device during acpi scan attach handle. 18392082a88SKen Xue * Return value > 0 on success of creating device. 18492082a88SKen Xue */ 18592082a88SKen Xue static int acpi_apd_create_device(struct acpi_device *adev, 18692082a88SKen Xue const struct acpi_device_id *id) 18792082a88SKen Xue { 18892082a88SKen Xue const struct apd_device_desc *dev_desc = (void *)id->driver_data; 18992082a88SKen Xue struct apd_private_data *pdata; 19092082a88SKen Xue struct platform_device *pdev; 19192082a88SKen Xue int ret; 19292082a88SKen Xue 19392082a88SKen Xue if (!dev_desc) { 1941571875bSHeikki Krogerus pdev = acpi_create_platform_device(adev, NULL); 19592082a88SKen Xue return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1; 19692082a88SKen Xue } 19792082a88SKen Xue 19892082a88SKen Xue pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); 19992082a88SKen Xue if (!pdata) 20092082a88SKen Xue return -ENOMEM; 20192082a88SKen Xue 20292082a88SKen Xue pdata->adev = adev; 20392082a88SKen Xue pdata->dev_desc = dev_desc; 20492082a88SKen Xue 20592082a88SKen Xue if (dev_desc->setup) { 20692082a88SKen Xue ret = dev_desc->setup(pdata); 20792082a88SKen Xue if (ret) 20892082a88SKen Xue goto err_out; 20992082a88SKen Xue } 21092082a88SKen Xue 21192082a88SKen Xue adev->driver_data = pdata; 2121571875bSHeikki Krogerus pdev = acpi_create_platform_device(adev, dev_desc->properties); 21392082a88SKen Xue if (!IS_ERR_OR_NULL(pdev)) 21492082a88SKen Xue return 1; 21592082a88SKen Xue 21692082a88SKen Xue ret = PTR_ERR(pdev); 21792082a88SKen Xue adev->driver_data = NULL; 21892082a88SKen Xue 21992082a88SKen Xue err_out: 22092082a88SKen Xue kfree(pdata); 22192082a88SKen Xue return ret; 22292082a88SKen Xue } 22392082a88SKen Xue 22492082a88SKen Xue static const struct acpi_device_id acpi_apd_device_ids[] = { 22592082a88SKen Xue /* Generic apd devices */ 226b790eb20SLoc Ho #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE 22792082a88SKen Xue { "AMD0010", APD_ADDR(cz_i2c_desc) }, 2284881f0baSNehal Shah { "AMDI0010", APD_ADDR(wt_i2c_desc) }, 22992082a88SKen Xue { "AMD0020", APD_ADDR(cz_uart_desc) }, 230f5eda99eSWang Hongcheng { "AMDI0020", APD_ADDR(cz_uart_desc) }, 2313c35d2a9SMaximilian Luz { "AMDI0022", APD_ADDR(cz_uart_desc) }, 23292082a88SKen Xue { "AMD0030", }, 233d58669b0SAkshu Agrawal { "AMD0040", APD_ADDR(fch_misc_desc)}, 234384b02d6SPu Wen { "HYGO0010", APD_ADDR(wt_i2c_desc) }, 235b790eb20SLoc Ho #endif 236b790eb20SLoc Ho #ifdef CONFIG_ARM64 237b790eb20SLoc Ho { "APMC0D0F", APD_ADDR(xgene_i2c_desc) }, 238bd2058dcSKamlakant Patel { "BRCM900D", APD_ADDR(vulcan_spi_desc) }, 239251831bdSJayachandran C { "CAV900D", APD_ADDR(vulcan_spi_desc) }, 2401977dbefSJayachandran C { "CAV9007", APD_ADDR(thunderx2_i2c_desc) }, 241f7f3dd5bSHanjun Guo { "HISI02A1", APD_ADDR(hip07_i2c_desc) }, 242f7f3dd5bSHanjun Guo { "HISI02A2", APD_ADDR(hip08_i2c_desc) }, 243c01a4a13SHanjun Guo { "HISI02A3", APD_ADDR(hip08_lite_i2c_desc) }, 24456131d6dSJay Fang { "HISI0173", APD_ADDR(hip08_spi_desc) }, 24511330a9fSChuanhua Han { "NXP0001", APD_ADDR(nxp_i2c_desc) }, 246b790eb20SLoc Ho #endif 24792082a88SKen Xue { } 24892082a88SKen Xue }; 24992082a88SKen Xue 25092082a88SKen Xue static struct acpi_scan_handler apd_handler = { 25192082a88SKen Xue .ids = acpi_apd_device_ids, 25292082a88SKen Xue .attach = acpi_apd_create_device, 25392082a88SKen Xue }; 25492082a88SKen Xue 25592082a88SKen Xue void __init acpi_apd_init(void) 25692082a88SKen Xue { 25792082a88SKen Xue acpi_scan_add_handler(&apd_handler); 25892082a88SKen Xue } 259