xref: /openbmc/linux/drivers/acpi/acpi_apd.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas 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/clk-provider.h>
113f4ba94eSAkshu Agrawal #include <linux/platform_data/clk-st.h>
1292082a88SKen Xue #include <linux/platform_device.h>
1392082a88SKen Xue #include <linux/pm_domain.h>
1492082a88SKen Xue #include <linux/clkdev.h>
1592082a88SKen Xue #include <linux/acpi.h>
1692082a88SKen Xue #include <linux/err.h>
1762e59c4eSStephen Boyd #include <linux/io.h>
1892082a88SKen Xue #include <linux/pm.h>
1992082a88SKen Xue 
2092082a88SKen Xue #include "internal.h"
2192082a88SKen Xue 
2292082a88SKen Xue ACPI_MODULE_NAME("acpi_apd");
2392082a88SKen Xue struct apd_private_data;
2492082a88SKen Xue 
2592082a88SKen Xue /**
2692082a88SKen Xue  * ACPI_APD_SYSFS : add device attributes in sysfs
2792082a88SKen Xue  * ACPI_APD_PM : attach power domain to device
2892082a88SKen Xue  */
2992082a88SKen Xue #define ACPI_APD_SYSFS	BIT(0)
3092082a88SKen Xue #define ACPI_APD_PM	BIT(1)
3192082a88SKen Xue 
3292082a88SKen Xue /**
3392082a88SKen Xue  * struct apd_device_desc - a descriptor for apd device
3492082a88SKen Xue  * @flags: device flags like %ACPI_APD_SYSFS, %ACPI_APD_PM
3592082a88SKen Xue  * @fixed_clk_rate: fixed rate input clock source for acpi device;
3692082a88SKen Xue  *			0 means no fixed rate input clock source
3792082a88SKen Xue  * @setup: a hook routine to set device resource during create platform device
3892082a88SKen Xue  *
3992082a88SKen Xue  * Device description defined as acpi_device_id.driver_data
4092082a88SKen Xue  */
4192082a88SKen Xue struct apd_device_desc {
4292082a88SKen Xue 	unsigned int flags;
4392082a88SKen Xue 	unsigned int fixed_clk_rate;
447ff55d17SHeikki Krogerus 	struct property_entry *properties;
4592082a88SKen Xue 	int (*setup)(struct apd_private_data *pdata);
4692082a88SKen Xue };
4792082a88SKen Xue 
4892082a88SKen Xue struct apd_private_data {
4992082a88SKen Xue 	struct clk *clk;
5092082a88SKen Xue 	struct acpi_device *adev;
5192082a88SKen Xue 	const struct apd_device_desc *dev_desc;
5292082a88SKen Xue };
5392082a88SKen Xue 
54b790eb20SLoc Ho #if defined(CONFIG_X86_AMD_PLATFORM_DEVICE) || defined(CONFIG_ARM64)
5592082a88SKen Xue #define APD_ADDR(desc)	((unsigned long)&desc)
5692082a88SKen Xue 
5792082a88SKen Xue static int acpi_apd_setup(struct apd_private_data *pdata)
5892082a88SKen Xue {
5992082a88SKen Xue 	const struct apd_device_desc *dev_desc = pdata->dev_desc;
6092082a88SKen Xue 	struct clk *clk = ERR_PTR(-ENODEV);
6192082a88SKen Xue 
6292082a88SKen Xue 	if (dev_desc->fixed_clk_rate) {
6392082a88SKen Xue 		clk = clk_register_fixed_rate(&pdata->adev->dev,
6492082a88SKen Xue 					dev_name(&pdata->adev->dev),
65cdd9a6b2SStephen Boyd 					NULL, 0, dev_desc->fixed_clk_rate);
6692082a88SKen Xue 		clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev));
6792082a88SKen Xue 		pdata->clk = clk;
6892082a88SKen Xue 	}
6992082a88SKen Xue 
7092082a88SKen Xue 	return 0;
7192082a88SKen Xue }
7292082a88SKen Xue 
73b790eb20SLoc Ho #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
743f4ba94eSAkshu Agrawal 
753f4ba94eSAkshu Agrawal static int misc_check_res(struct acpi_resource *ares, void *data)
763f4ba94eSAkshu Agrawal {
773f4ba94eSAkshu Agrawal 	struct resource res;
783f4ba94eSAkshu Agrawal 
793f4ba94eSAkshu Agrawal 	return !acpi_dev_resource_memory(ares, &res);
803f4ba94eSAkshu Agrawal }
813f4ba94eSAkshu Agrawal 
823f4ba94eSAkshu Agrawal static int st_misc_setup(struct apd_private_data *pdata)
833f4ba94eSAkshu Agrawal {
843f4ba94eSAkshu Agrawal 	struct acpi_device *adev = pdata->adev;
853f4ba94eSAkshu Agrawal 	struct platform_device *clkdev;
863f4ba94eSAkshu Agrawal 	struct st_clk_data *clk_data;
873f4ba94eSAkshu Agrawal 	struct resource_entry *rentry;
883f4ba94eSAkshu Agrawal 	struct list_head resource_list;
893f4ba94eSAkshu Agrawal 	int ret;
903f4ba94eSAkshu Agrawal 
913f4ba94eSAkshu Agrawal 	clk_data = devm_kzalloc(&adev->dev, sizeof(*clk_data), GFP_KERNEL);
923f4ba94eSAkshu Agrawal 	if (!clk_data)
933f4ba94eSAkshu Agrawal 		return -ENOMEM;
943f4ba94eSAkshu Agrawal 
953f4ba94eSAkshu Agrawal 	INIT_LIST_HEAD(&resource_list);
963f4ba94eSAkshu Agrawal 	ret = acpi_dev_get_resources(adev, &resource_list, misc_check_res,
973f4ba94eSAkshu Agrawal 				     NULL);
983f4ba94eSAkshu Agrawal 	if (ret < 0)
993f4ba94eSAkshu Agrawal 		return -ENOENT;
1003f4ba94eSAkshu Agrawal 
1013f4ba94eSAkshu Agrawal 	list_for_each_entry(rentry, &resource_list, node) {
1023f4ba94eSAkshu Agrawal 		clk_data->base = devm_ioremap(&adev->dev, rentry->res->start,
1033f4ba94eSAkshu Agrawal 					      resource_size(rentry->res));
1043f4ba94eSAkshu Agrawal 		break;
1053f4ba94eSAkshu Agrawal 	}
1063f4ba94eSAkshu Agrawal 
1073f4ba94eSAkshu Agrawal 	acpi_dev_free_resource_list(&resource_list);
1083f4ba94eSAkshu Agrawal 
1093f4ba94eSAkshu Agrawal 	clkdev = platform_device_register_data(&adev->dev, "clk-st",
1103f4ba94eSAkshu Agrawal 					       PLATFORM_DEVID_NONE, clk_data,
1113f4ba94eSAkshu Agrawal 					       sizeof(*clk_data));
1123f4ba94eSAkshu Agrawal 	return PTR_ERR_OR_ZERO(clkdev);
1133f4ba94eSAkshu Agrawal }
1143f4ba94eSAkshu Agrawal 
115a217726aSJulia Lawall static const struct apd_device_desc cz_i2c_desc = {
11692082a88SKen Xue 	.setup = acpi_apd_setup,
11792082a88SKen Xue 	.fixed_clk_rate = 133000000,
11892082a88SKen Xue };
11992082a88SKen Xue 
1204881f0baSNehal Shah static const struct apd_device_desc wt_i2c_desc = {
1214881f0baSNehal Shah 	.setup = acpi_apd_setup,
1224881f0baSNehal Shah 	.fixed_clk_rate = 150000000,
1234881f0baSNehal Shah };
1244881f0baSNehal Shah 
1257ff55d17SHeikki Krogerus static struct property_entry uart_properties[] = {
1267ff55d17SHeikki Krogerus 	PROPERTY_ENTRY_U32("reg-io-width", 4),
1277ff55d17SHeikki Krogerus 	PROPERTY_ENTRY_U32("reg-shift", 2),
1287ff55d17SHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
1297ff55d17SHeikki Krogerus 	{ },
1307ff55d17SHeikki Krogerus };
1317ff55d17SHeikki Krogerus 
132a217726aSJulia Lawall static const struct apd_device_desc cz_uart_desc = {
13392082a88SKen Xue 	.setup = acpi_apd_setup,
13492082a88SKen Xue 	.fixed_clk_rate = 48000000,
1357ff55d17SHeikki Krogerus 	.properties = uart_properties,
13692082a88SKen Xue };
1373f4ba94eSAkshu Agrawal 
1383f4ba94eSAkshu Agrawal static const struct apd_device_desc st_misc_desc = {
1393f4ba94eSAkshu Agrawal 	.setup = st_misc_setup,
1403f4ba94eSAkshu Agrawal };
141b790eb20SLoc Ho #endif
142b790eb20SLoc Ho 
143b790eb20SLoc Ho #ifdef CONFIG_ARM64
144a217726aSJulia Lawall static const struct apd_device_desc xgene_i2c_desc = {
145b790eb20SLoc Ho 	.setup = acpi_apd_setup,
146b790eb20SLoc Ho 	.fixed_clk_rate = 100000000,
147b790eb20SLoc Ho };
148bd2058dcSKamlakant Patel 
149a217726aSJulia Lawall static const struct apd_device_desc vulcan_spi_desc = {
150bd2058dcSKamlakant Patel 	.setup = acpi_apd_setup,
151bd2058dcSKamlakant Patel 	.fixed_clk_rate = 133000000,
152bd2058dcSKamlakant Patel };
1536e14cf36SHanjun Guo 
1546e14cf36SHanjun Guo static const struct apd_device_desc hip07_i2c_desc = {
1556e14cf36SHanjun Guo 	.setup = acpi_apd_setup,
1566e14cf36SHanjun Guo 	.fixed_clk_rate = 200000000,
1576e14cf36SHanjun Guo };
1586e14cf36SHanjun Guo 
1596e14cf36SHanjun Guo static const struct apd_device_desc hip08_i2c_desc = {
1606e14cf36SHanjun Guo 	.setup = acpi_apd_setup,
1616e14cf36SHanjun Guo 	.fixed_clk_rate = 250000000,
1626e14cf36SHanjun 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 
16856131d6dSJay Fang static const struct apd_device_desc hip08_spi_desc = {
16956131d6dSJay Fang 	.setup = acpi_apd_setup,
17056131d6dSJay Fang 	.fixed_clk_rate = 250000000,
17156131d6dSJay Fang };
172b790eb20SLoc Ho #endif
17392082a88SKen Xue 
17492082a88SKen Xue #else
17592082a88SKen Xue 
17692082a88SKen Xue #define APD_ADDR(desc) (0UL)
17792082a88SKen Xue 
17892082a88SKen Xue #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */
17992082a88SKen Xue 
18092082a88SKen Xue /**
18192082a88SKen Xue * Create platform device during acpi scan attach handle.
18292082a88SKen Xue * Return value > 0 on success of creating device.
18392082a88SKen Xue */
18492082a88SKen Xue static int acpi_apd_create_device(struct acpi_device *adev,
18592082a88SKen Xue 				   const struct acpi_device_id *id)
18692082a88SKen Xue {
18792082a88SKen Xue 	const struct apd_device_desc *dev_desc = (void *)id->driver_data;
18892082a88SKen Xue 	struct apd_private_data *pdata;
18992082a88SKen Xue 	struct platform_device *pdev;
19092082a88SKen Xue 	int ret;
19192082a88SKen Xue 
19292082a88SKen Xue 	if (!dev_desc) {
1931571875bSHeikki Krogerus 		pdev = acpi_create_platform_device(adev, NULL);
19492082a88SKen Xue 		return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
19592082a88SKen Xue 	}
19692082a88SKen Xue 
19792082a88SKen Xue 	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
19892082a88SKen Xue 	if (!pdata)
19992082a88SKen Xue 		return -ENOMEM;
20092082a88SKen Xue 
20192082a88SKen Xue 	pdata->adev = adev;
20292082a88SKen Xue 	pdata->dev_desc = dev_desc;
20392082a88SKen Xue 
20492082a88SKen Xue 	if (dev_desc->setup) {
20592082a88SKen Xue 		ret = dev_desc->setup(pdata);
20692082a88SKen Xue 		if (ret)
20792082a88SKen Xue 			goto err_out;
20892082a88SKen Xue 	}
20992082a88SKen Xue 
21092082a88SKen Xue 	adev->driver_data = pdata;
2111571875bSHeikki Krogerus 	pdev = acpi_create_platform_device(adev, dev_desc->properties);
21292082a88SKen Xue 	if (!IS_ERR_OR_NULL(pdev))
21392082a88SKen Xue 		return 1;
21492082a88SKen Xue 
21592082a88SKen Xue 	ret = PTR_ERR(pdev);
21692082a88SKen Xue 	adev->driver_data = NULL;
21792082a88SKen Xue 
21892082a88SKen Xue  err_out:
21992082a88SKen Xue 	kfree(pdata);
22092082a88SKen Xue 	return ret;
22192082a88SKen Xue }
22292082a88SKen Xue 
22392082a88SKen Xue static const struct acpi_device_id acpi_apd_device_ids[] = {
22492082a88SKen Xue 	/* Generic apd devices */
225b790eb20SLoc Ho #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
22692082a88SKen Xue 	{ "AMD0010", APD_ADDR(cz_i2c_desc) },
2274881f0baSNehal Shah 	{ "AMDI0010", APD_ADDR(wt_i2c_desc) },
22892082a88SKen Xue 	{ "AMD0020", APD_ADDR(cz_uart_desc) },
229f5eda99eSWang Hongcheng 	{ "AMDI0020", APD_ADDR(cz_uart_desc) },
23092082a88SKen Xue 	{ "AMD0030", },
2313f4ba94eSAkshu Agrawal 	{ "AMD0040", APD_ADDR(st_misc_desc)},
232b790eb20SLoc Ho #endif
233b790eb20SLoc Ho #ifdef CONFIG_ARM64
234b790eb20SLoc Ho 	{ "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
235bd2058dcSKamlakant Patel 	{ "BRCM900D", APD_ADDR(vulcan_spi_desc) },
236251831bdSJayachandran C 	{ "CAV900D",  APD_ADDR(vulcan_spi_desc) },
2371977dbefSJayachandran C 	{ "CAV9007",  APD_ADDR(thunderx2_i2c_desc) },
238f7f3dd5bSHanjun Guo 	{ "HISI02A1", APD_ADDR(hip07_i2c_desc) },
239f7f3dd5bSHanjun Guo 	{ "HISI02A2", APD_ADDR(hip08_i2c_desc) },
24056131d6dSJay Fang 	{ "HISI0173", APD_ADDR(hip08_spi_desc) },
241b790eb20SLoc Ho #endif
24292082a88SKen Xue 	{ }
24392082a88SKen Xue };
24492082a88SKen Xue 
24592082a88SKen Xue static struct acpi_scan_handler apd_handler = {
24692082a88SKen Xue 	.ids = acpi_apd_device_ids,
24792082a88SKen Xue 	.attach = acpi_apd_create_device,
24892082a88SKen Xue };
24992082a88SKen Xue 
25092082a88SKen Xue void __init acpi_apd_init(void)
25192082a88SKen Xue {
25292082a88SKen Xue 	acpi_scan_add_handler(&apd_handler);
25392082a88SKen Xue }
254