xref: /openbmc/linux/drivers/acpi/dptf/dptf_power.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * dptf_power:  DPTF platform power driver
4  * Copyright (c) 2016, Intel Corporation.
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/acpi.h>
10 #include <linux/platform_device.h>
11 
12 /*
13  * Presentation of attributes which are defined for INT3407. They are:
14  * PMAX : Maximum platform powe
15  * PSRC : Platform power source
16  * ARTG : Adapter rating
17  * CTYP : Charger type
18  * PBSS : Battery steady power
19  */
20 #define DPTF_POWER_SHOW(name, object) \
21 static ssize_t name##_show(struct device *dev,\
22 			   struct device_attribute *attr,\
23 			   char *buf)\
24 {\
25 	struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
26 	unsigned long long val;\
27 	acpi_status status;\
28 \
29 	status = acpi_evaluate_integer(acpi_dev->handle, #object,\
30 				       NULL, &val);\
31 	if (ACPI_SUCCESS(status))\
32 		return sprintf(buf, "%d\n", (int)val);\
33 	else \
34 		return -EINVAL;\
35 }
36 
37 DPTF_POWER_SHOW(max_platform_power_mw, PMAX)
38 DPTF_POWER_SHOW(platform_power_source, PSRC)
39 DPTF_POWER_SHOW(adapter_rating_mw, ARTG)
40 DPTF_POWER_SHOW(battery_steady_power_mw, PBSS)
41 DPTF_POWER_SHOW(charger_type, CTYP)
42 
43 static DEVICE_ATTR_RO(max_platform_power_mw);
44 static DEVICE_ATTR_RO(platform_power_source);
45 static DEVICE_ATTR_RO(adapter_rating_mw);
46 static DEVICE_ATTR_RO(battery_steady_power_mw);
47 static DEVICE_ATTR_RO(charger_type);
48 
49 static struct attribute *dptf_power_attrs[] = {
50 	&dev_attr_max_platform_power_mw.attr,
51 	&dev_attr_platform_power_source.attr,
52 	&dev_attr_adapter_rating_mw.attr,
53 	&dev_attr_battery_steady_power_mw.attr,
54 	&dev_attr_charger_type.attr,
55 	NULL
56 };
57 
58 static const struct attribute_group dptf_power_attribute_group = {
59 	.attrs = dptf_power_attrs,
60 	.name = "dptf_power"
61 };
62 
63 static int dptf_power_add(struct platform_device *pdev)
64 {
65 	struct acpi_device *acpi_dev;
66 	acpi_status status;
67 	unsigned long long ptype;
68 	int result;
69 
70 	acpi_dev = ACPI_COMPANION(&(pdev->dev));
71 	if (!acpi_dev)
72 		return -ENODEV;
73 
74 	status = acpi_evaluate_integer(acpi_dev->handle, "PTYP", NULL, &ptype);
75 	if (ACPI_FAILURE(status))
76 		return -ENODEV;
77 
78 	if (ptype != 0x11)
79 		return -ENODEV;
80 
81 	result = sysfs_create_group(&pdev->dev.kobj,
82 				    &dptf_power_attribute_group);
83 	if (result)
84 		return result;
85 
86 	platform_set_drvdata(pdev, acpi_dev);
87 
88 	return 0;
89 }
90 
91 static int dptf_power_remove(struct platform_device *pdev)
92 {
93 
94 	sysfs_remove_group(&pdev->dev.kobj, &dptf_power_attribute_group);
95 
96 	return 0;
97 }
98 
99 static const struct acpi_device_id int3407_device_ids[] = {
100 	{"INT1047", 0},
101 	{"INT3407", 0},
102 	{"", 0},
103 };
104 MODULE_DEVICE_TABLE(acpi, int3407_device_ids);
105 
106 static struct platform_driver dptf_power_driver = {
107 	.probe = dptf_power_add,
108 	.remove = dptf_power_remove,
109 	.driver = {
110 		.name = "DPTF Platform Power",
111 		.acpi_match_table = int3407_device_ids,
112 	},
113 };
114 
115 module_platform_driver(dptf_power_driver);
116 
117 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
118 MODULE_LICENSE("GPL v2");
119 MODULE_DESCRIPTION("ACPI DPTF platform power driver");
120