1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * dptf_pch_fivr: DPTF PCH FIVR Participant driver
4 * Copyright (c) 2020, Intel Corporation.
5 */
6
7 #include <linux/acpi.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11
12 struct pch_fivr_resp {
13 u64 status;
14 u64 result;
15 };
16
pch_fivr_read(acpi_handle handle,char * method,struct pch_fivr_resp * fivr_resp)17 static int pch_fivr_read(acpi_handle handle, char *method, struct pch_fivr_resp *fivr_resp)
18 {
19 struct acpi_buffer resp = { sizeof(struct pch_fivr_resp), fivr_resp};
20 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
21 struct acpi_buffer format = { sizeof("NN"), "NN" };
22 union acpi_object *obj;
23 acpi_status status;
24 int ret = -EFAULT;
25
26 status = acpi_evaluate_object(handle, method, NULL, &buffer);
27 if (ACPI_FAILURE(status))
28 return ret;
29
30 obj = buffer.pointer;
31 if (!obj || obj->type != ACPI_TYPE_PACKAGE)
32 goto release_buffer;
33
34 status = acpi_extract_package(obj, &format, &resp);
35 if (ACPI_FAILURE(status))
36 goto release_buffer;
37
38 if (fivr_resp->status)
39 goto release_buffer;
40
41 ret = 0;
42
43 release_buffer:
44 kfree(buffer.pointer);
45 return ret;
46 }
47
48 /*
49 * Presentation of attributes which are defined for INT1045
50 * They are:
51 * freq_mhz_low_clock : Set PCH FIVR switching freq for
52 * FIVR clock 19.2MHz and 24MHz
53 * freq_mhz_high_clock : Set PCH FIVR switching freq for
54 * FIVR clock 38.4MHz
55 */
56 #define PCH_FIVR_SHOW(name, method) \
57 static ssize_t name##_show(struct device *dev,\
58 struct device_attribute *attr,\
59 char *buf)\
60 {\
61 struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
62 struct pch_fivr_resp fivr_resp;\
63 int status;\
64 \
65 status = pch_fivr_read(acpi_dev->handle, #method, &fivr_resp);\
66 if (status)\
67 return status;\
68 \
69 return sprintf(buf, "%llu\n", fivr_resp.result);\
70 }
71
72 #define PCH_FIVR_STORE(name, method) \
73 static ssize_t name##_store(struct device *dev,\
74 struct device_attribute *attr,\
75 const char *buf, size_t count)\
76 {\
77 struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
78 acpi_status status;\
79 u32 val;\
80 \
81 if (kstrtouint(buf, 0, &val) < 0)\
82 return -EINVAL;\
83 \
84 status = acpi_execute_simple_method(acpi_dev->handle, #method, val);\
85 if (ACPI_SUCCESS(status))\
86 return count;\
87 \
88 return -EINVAL;\
89 }
90
91 PCH_FIVR_SHOW(freq_mhz_low_clock, GFC0)
92 PCH_FIVR_SHOW(freq_mhz_high_clock, GFC1)
93 PCH_FIVR_STORE(freq_mhz_low_clock, RFC0)
94 PCH_FIVR_STORE(freq_mhz_high_clock, RFC1)
95
96 static DEVICE_ATTR_RW(freq_mhz_low_clock);
97 static DEVICE_ATTR_RW(freq_mhz_high_clock);
98
99 static struct attribute *fivr_attrs[] = {
100 &dev_attr_freq_mhz_low_clock.attr,
101 &dev_attr_freq_mhz_high_clock.attr,
102 NULL
103 };
104
105 static const struct attribute_group pch_fivr_attribute_group = {
106 .attrs = fivr_attrs,
107 .name = "pch_fivr_switch_frequency"
108 };
109
pch_fivr_add(struct platform_device * pdev)110 static int pch_fivr_add(struct platform_device *pdev)
111 {
112 struct acpi_device *acpi_dev;
113 unsigned long long ptype;
114 acpi_status status;
115 int result;
116
117 acpi_dev = ACPI_COMPANION(&(pdev->dev));
118 if (!acpi_dev)
119 return -ENODEV;
120
121 status = acpi_evaluate_integer(acpi_dev->handle, "PTYP", NULL, &ptype);
122 if (ACPI_FAILURE(status) || ptype != 0x05)
123 return -ENODEV;
124
125 result = sysfs_create_group(&pdev->dev.kobj,
126 &pch_fivr_attribute_group);
127 if (result)
128 return result;
129
130 platform_set_drvdata(pdev, acpi_dev);
131
132 return 0;
133 }
134
pch_fivr_remove(struct platform_device * pdev)135 static int pch_fivr_remove(struct platform_device *pdev)
136 {
137 sysfs_remove_group(&pdev->dev.kobj, &pch_fivr_attribute_group);
138
139 return 0;
140 }
141
142 static const struct acpi_device_id pch_fivr_device_ids[] = {
143 {"INTC1045", 0},
144 {"INTC1049", 0},
145 {"", 0},
146 };
147 MODULE_DEVICE_TABLE(acpi, pch_fivr_device_ids);
148
149 static struct platform_driver pch_fivr_driver = {
150 .probe = pch_fivr_add,
151 .remove = pch_fivr_remove,
152 .driver = {
153 .name = "dptf_pch_fivr",
154 .acpi_match_table = pch_fivr_device_ids,
155 },
156 };
157
158 module_platform_driver(pch_fivr_driver);
159
160 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
161 MODULE_LICENSE("GPL v2");
162 MODULE_DESCRIPTION("ACPI DPTF PCH FIVR driver");
163