xref: /openbmc/linux/drivers/acpi/acpi_apd.c (revision a217726a7df146fcfea6f37cbbc80849083260b6)
192082a88SKen Xue /*
292082a88SKen Xue  * AMD ACPI support for ACPI2platform device.
392082a88SKen Xue  *
492082a88SKen Xue  * Copyright (c) 2014,2015 AMD Corporation.
592082a88SKen Xue  * Authors: Ken Xue <Ken.Xue@amd.com>
692082a88SKen Xue  *	Wu, Jeff <Jeff.Wu@amd.com>
792082a88SKen Xue  *
892082a88SKen Xue  * This program is free software; you can redistribute it and/or modify
992082a88SKen Xue  * it under the terms of the GNU General Public License version 2 as
1092082a88SKen Xue  * published by the Free Software Foundation.
1192082a88SKen Xue  */
1292082a88SKen Xue 
1392082a88SKen Xue #include <linux/clk-provider.h>
1492082a88SKen Xue #include <linux/platform_device.h>
1592082a88SKen Xue #include <linux/pm_domain.h>
1692082a88SKen Xue #include <linux/clkdev.h>
1792082a88SKen Xue #include <linux/acpi.h>
1892082a88SKen Xue #include <linux/err.h>
1992082a88SKen Xue #include <linux/pm.h>
2092082a88SKen Xue 
2192082a88SKen Xue #include "internal.h"
2292082a88SKen Xue 
2392082a88SKen Xue ACPI_MODULE_NAME("acpi_apd");
2492082a88SKen Xue struct apd_private_data;
2592082a88SKen Xue 
2692082a88SKen Xue /**
2792082a88SKen Xue  * ACPI_APD_SYSFS : add device attributes in sysfs
2892082a88SKen Xue  * ACPI_APD_PM : attach power domain to device
2992082a88SKen Xue  */
3092082a88SKen Xue #define ACPI_APD_SYSFS	BIT(0)
3192082a88SKen Xue #define ACPI_APD_PM	BIT(1)
3292082a88SKen Xue 
3392082a88SKen Xue /**
3492082a88SKen Xue  * struct apd_device_desc - a descriptor for apd device
3592082a88SKen Xue  * @flags: device flags like %ACPI_APD_SYSFS, %ACPI_APD_PM
3692082a88SKen Xue  * @fixed_clk_rate: fixed rate input clock source for acpi device;
3792082a88SKen Xue  *			0 means no fixed rate input clock source
3892082a88SKen Xue  * @setup: a hook routine to set device resource during create platform device
3992082a88SKen Xue  *
4092082a88SKen Xue  * Device description defined as acpi_device_id.driver_data
4192082a88SKen Xue  */
4292082a88SKen Xue struct apd_device_desc {
4392082a88SKen Xue 	unsigned int flags;
4492082a88SKen Xue 	unsigned int fixed_clk_rate;
457ff55d17SHeikki Krogerus 	struct property_entry *properties;
4692082a88SKen Xue 	int (*setup)(struct apd_private_data *pdata);
4792082a88SKen Xue };
4892082a88SKen Xue 
4992082a88SKen Xue struct apd_private_data {
5092082a88SKen Xue 	struct clk *clk;
5192082a88SKen Xue 	struct acpi_device *adev;
5292082a88SKen Xue 	const struct apd_device_desc *dev_desc;
5392082a88SKen Xue };
5492082a88SKen Xue 
55b790eb20SLoc Ho #if defined(CONFIG_X86_AMD_PLATFORM_DEVICE) || defined(CONFIG_ARM64)
5692082a88SKen Xue #define APD_ADDR(desc)	((unsigned long)&desc)
5792082a88SKen Xue 
5892082a88SKen Xue static int acpi_apd_setup(struct apd_private_data *pdata)
5992082a88SKen Xue {
6092082a88SKen Xue 	const struct apd_device_desc *dev_desc = pdata->dev_desc;
6192082a88SKen Xue 	struct clk *clk = ERR_PTR(-ENODEV);
6292082a88SKen Xue 
6392082a88SKen Xue 	if (dev_desc->fixed_clk_rate) {
6492082a88SKen Xue 		clk = clk_register_fixed_rate(&pdata->adev->dev,
6592082a88SKen Xue 					dev_name(&pdata->adev->dev),
66cdd9a6b2SStephen Boyd 					NULL, 0, dev_desc->fixed_clk_rate);
6792082a88SKen Xue 		clk_register_clkdev(clk, NULL, dev_name(&pdata->adev->dev));
6892082a88SKen Xue 		pdata->clk = clk;
6992082a88SKen Xue 	}
7092082a88SKen Xue 
7192082a88SKen Xue 	return 0;
7292082a88SKen Xue }
7392082a88SKen Xue 
74b790eb20SLoc Ho #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
75*a217726aSJulia Lawall static const struct apd_device_desc cz_i2c_desc = {
7692082a88SKen Xue 	.setup = acpi_apd_setup,
7792082a88SKen Xue 	.fixed_clk_rate = 133000000,
7892082a88SKen Xue };
7992082a88SKen Xue 
807ff55d17SHeikki Krogerus static struct property_entry uart_properties[] = {
817ff55d17SHeikki Krogerus 	PROPERTY_ENTRY_U32("reg-io-width", 4),
827ff55d17SHeikki Krogerus 	PROPERTY_ENTRY_U32("reg-shift", 2),
837ff55d17SHeikki Krogerus 	PROPERTY_ENTRY_BOOL("snps,uart-16550-compatible"),
847ff55d17SHeikki Krogerus 	{ },
857ff55d17SHeikki Krogerus };
867ff55d17SHeikki Krogerus 
87*a217726aSJulia Lawall static const struct apd_device_desc cz_uart_desc = {
8892082a88SKen Xue 	.setup = acpi_apd_setup,
8992082a88SKen Xue 	.fixed_clk_rate = 48000000,
907ff55d17SHeikki Krogerus 	.properties = uart_properties,
9192082a88SKen Xue };
92b790eb20SLoc Ho #endif
93b790eb20SLoc Ho 
94b790eb20SLoc Ho #ifdef CONFIG_ARM64
95*a217726aSJulia Lawall static const struct apd_device_desc xgene_i2c_desc = {
96b790eb20SLoc Ho 	.setup = acpi_apd_setup,
97b790eb20SLoc Ho 	.fixed_clk_rate = 100000000,
98b790eb20SLoc Ho };
99bd2058dcSKamlakant Patel 
100*a217726aSJulia Lawall static const struct apd_device_desc vulcan_spi_desc = {
101bd2058dcSKamlakant Patel 	.setup = acpi_apd_setup,
102bd2058dcSKamlakant Patel 	.fixed_clk_rate = 133000000,
103bd2058dcSKamlakant Patel };
104b790eb20SLoc Ho #endif
10592082a88SKen Xue 
10692082a88SKen Xue #else
10792082a88SKen Xue 
10892082a88SKen Xue #define APD_ADDR(desc) (0UL)
10992082a88SKen Xue 
11092082a88SKen Xue #endif /* CONFIG_X86_AMD_PLATFORM_DEVICE */
11192082a88SKen Xue 
11292082a88SKen Xue /**
11392082a88SKen Xue * Create platform device during acpi scan attach handle.
11492082a88SKen Xue * Return value > 0 on success of creating device.
11592082a88SKen Xue */
11692082a88SKen Xue static int acpi_apd_create_device(struct acpi_device *adev,
11792082a88SKen Xue 				   const struct acpi_device_id *id)
11892082a88SKen Xue {
11992082a88SKen Xue 	const struct apd_device_desc *dev_desc = (void *)id->driver_data;
12092082a88SKen Xue 	struct apd_private_data *pdata;
12192082a88SKen Xue 	struct platform_device *pdev;
12292082a88SKen Xue 	int ret;
12392082a88SKen Xue 
12492082a88SKen Xue 	if (!dev_desc) {
12592082a88SKen Xue 		pdev = acpi_create_platform_device(adev);
12692082a88SKen Xue 		return IS_ERR_OR_NULL(pdev) ? PTR_ERR(pdev) : 1;
12792082a88SKen Xue 	}
12892082a88SKen Xue 
12992082a88SKen Xue 	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
13092082a88SKen Xue 	if (!pdata)
13192082a88SKen Xue 		return -ENOMEM;
13292082a88SKen Xue 
13392082a88SKen Xue 	pdata->adev = adev;
13492082a88SKen Xue 	pdata->dev_desc = dev_desc;
13592082a88SKen Xue 
13692082a88SKen Xue 	if (dev_desc->setup) {
13792082a88SKen Xue 		ret = dev_desc->setup(pdata);
13892082a88SKen Xue 		if (ret)
13992082a88SKen Xue 			goto err_out;
14092082a88SKen Xue 	}
14192082a88SKen Xue 
1427ff55d17SHeikki Krogerus 	if (dev_desc->properties) {
1437ff55d17SHeikki Krogerus 		ret = device_add_properties(&adev->dev, dev_desc->properties);
1447ff55d17SHeikki Krogerus 		if (ret)
1457ff55d17SHeikki Krogerus 			goto err_out;
1467ff55d17SHeikki Krogerus 	}
1477ff55d17SHeikki Krogerus 
14892082a88SKen Xue 	adev->driver_data = pdata;
14992082a88SKen Xue 	pdev = acpi_create_platform_device(adev);
15092082a88SKen Xue 	if (!IS_ERR_OR_NULL(pdev))
15192082a88SKen Xue 		return 1;
15292082a88SKen Xue 
15392082a88SKen Xue 	ret = PTR_ERR(pdev);
15492082a88SKen Xue 	adev->driver_data = NULL;
15592082a88SKen Xue 
15692082a88SKen Xue  err_out:
15792082a88SKen Xue 	kfree(pdata);
15892082a88SKen Xue 	return ret;
15992082a88SKen Xue }
16092082a88SKen Xue 
16192082a88SKen Xue static const struct acpi_device_id acpi_apd_device_ids[] = {
16292082a88SKen Xue 	/* Generic apd devices */
163b790eb20SLoc Ho #ifdef CONFIG_X86_AMD_PLATFORM_DEVICE
16492082a88SKen Xue 	{ "AMD0010", APD_ADDR(cz_i2c_desc) },
165e4e666baSXiangliang Yu 	{ "AMDI0010", APD_ADDR(cz_i2c_desc) },
16692082a88SKen Xue 	{ "AMD0020", APD_ADDR(cz_uart_desc) },
167f5eda99eSWang Hongcheng 	{ "AMDI0020", APD_ADDR(cz_uart_desc) },
16892082a88SKen Xue 	{ "AMD0030", },
169b790eb20SLoc Ho #endif
170b790eb20SLoc Ho #ifdef CONFIG_ARM64
171b790eb20SLoc Ho 	{ "APMC0D0F", APD_ADDR(xgene_i2c_desc) },
172bd2058dcSKamlakant Patel 	{ "BRCM900D", APD_ADDR(vulcan_spi_desc) },
173b790eb20SLoc Ho #endif
17492082a88SKen Xue 	{ }
17592082a88SKen Xue };
17692082a88SKen Xue 
17792082a88SKen Xue static struct acpi_scan_handler apd_handler = {
17892082a88SKen Xue 	.ids = acpi_apd_device_ids,
17992082a88SKen Xue 	.attach = acpi_apd_create_device,
18092082a88SKen Xue };
18192082a88SKen Xue 
18292082a88SKen Xue void __init acpi_apd_init(void)
18392082a88SKen Xue {
18492082a88SKen Xue 	acpi_scan_add_handler(&apd_handler);
18592082a88SKen Xue }
186