Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / thermal / intel / int340x_thermal / processor_thermal_wt_req.c
blobb95810f4a0118a16729d81421c5178b8ffbd3bd6
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * processor thermal device for Workload type hints
4 * update from user space
6 * Copyright (c) 2020-2023, Intel Corporation.
7 */
9 #include <linux/pci.h>
10 #include "processor_thermal_device.h"
12 /* List of workload types */
13 static const char * const workload_types[] = {
14 "none",
15 "idle",
16 "semi_active",
17 "bursty",
18 "sustained",
19 "battery_life",
20 NULL
23 static ssize_t workload_available_types_show(struct device *dev,
24 struct device_attribute *attr,
25 char *buf)
27 int i = 0;
28 int ret = 0;
30 while (workload_types[i] != NULL)
31 ret += sprintf(&buf[ret], "%s ", workload_types[i++]);
33 ret += sprintf(&buf[ret], "\n");
35 return ret;
38 static DEVICE_ATTR_RO(workload_available_types);
40 static ssize_t workload_type_store(struct device *dev,
41 struct device_attribute *attr,
42 const char *buf, size_t count)
44 struct pci_dev *pdev = to_pci_dev(dev);
45 char str_preference[15];
46 u32 data = 0;
47 ssize_t ret;
49 ret = sscanf(buf, "%14s", str_preference);
50 if (ret != 1)
51 return -EINVAL;
53 ret = match_string(workload_types, -1, str_preference);
54 if (ret < 0)
55 return ret;
57 ret &= 0xff;
59 if (ret)
60 data = BIT(MBOX_DATA_BIT_VALID) | BIT(MBOX_DATA_BIT_AC_DC);
62 data |= ret;
64 ret = processor_thermal_send_mbox_write_cmd(pdev, MBOX_CMD_WORKLOAD_TYPE_WRITE, data);
65 if (ret)
66 return false;
68 return count;
71 static ssize_t workload_type_show(struct device *dev,
72 struct device_attribute *attr,
73 char *buf)
75 struct pci_dev *pdev = to_pci_dev(dev);
76 u64 cmd_resp;
77 int ret;
79 ret = processor_thermal_send_mbox_read_cmd(pdev, MBOX_CMD_WORKLOAD_TYPE_READ, &cmd_resp);
80 if (ret)
81 return false;
83 cmd_resp &= 0xff;
85 if (cmd_resp > ARRAY_SIZE(workload_types) - 1)
86 return -EINVAL;
88 return sprintf(buf, "%s\n", workload_types[cmd_resp]);
91 static DEVICE_ATTR_RW(workload_type);
93 static struct attribute *workload_req_attrs[] = {
94 &dev_attr_workload_available_types.attr,
95 &dev_attr_workload_type.attr,
96 NULL
99 static const struct attribute_group workload_req_attribute_group = {
100 .attrs = workload_req_attrs,
101 .name = "workload_request"
104 static bool workload_req_created;
106 int proc_thermal_wt_req_add(struct pci_dev *pdev, struct proc_thermal_device *proc_priv)
108 u64 cmd_resp;
109 int ret;
111 /* Check if there is a mailbox support, if fails return success */
112 ret = processor_thermal_send_mbox_read_cmd(pdev, MBOX_CMD_WORKLOAD_TYPE_READ, &cmd_resp);
113 if (ret)
114 return 0;
116 ret = sysfs_create_group(&pdev->dev.kobj, &workload_req_attribute_group);
117 if (ret)
118 return ret;
120 workload_req_created = true;
122 return 0;
124 EXPORT_SYMBOL_GPL(proc_thermal_wt_req_add);
126 void proc_thermal_wt_req_remove(struct pci_dev *pdev)
128 if (workload_req_created)
129 sysfs_remove_group(&pdev->dev.kobj, &workload_req_attribute_group);
131 workload_req_created = false;
133 EXPORT_SYMBOL_GPL(proc_thermal_wt_req_remove);
135 MODULE_IMPORT_NS("INT340X_THERMAL");
136 MODULE_LICENSE("GPL");
137 MODULE_DESCRIPTION("Processor Thermal Work Load type request Interface");