1 // SPDX-License-Identifier: GPL-2.0-only
3 * dptf_power: DPTF platform power driver
4 * Copyright (c) 2016, Intel Corporation.
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/acpi.h>
10 #include <linux/platform_device.h>
13 * Presentation of attributes which are defined for INT3407 and INT3532.
15 * PMAX : Maximum platform power
16 * PSRC : Platform power source
17 * ARTG : Adapter rating
19 * PROP : Rest of worst case platform Power
20 * PBSS : Power Battery Steady State
21 * RBHF : High Frequency Impedance
22 * VBNL : Instantaneous No-Load Voltage
23 * CMPP : Current Discharge Capability
25 #define DPTF_POWER_SHOW(name, object) \
26 static ssize_t name##_show(struct device *dev,\
27 struct device_attribute *attr,\
30 struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
31 unsigned long long val;\
34 status = acpi_evaluate_integer(acpi_dev->handle, #object,\
36 if (ACPI_SUCCESS(status))\
37 return sprintf(buf, "%d\n", (int)val);\
42 DPTF_POWER_SHOW(max_platform_power_mw
, PMAX
)
43 DPTF_POWER_SHOW(platform_power_source
, PSRC
)
44 DPTF_POWER_SHOW(adapter_rating_mw
, ARTG
)
45 DPTF_POWER_SHOW(battery_steady_power_mw
, PBSS
)
46 DPTF_POWER_SHOW(charger_type
, CTYP
)
47 DPTF_POWER_SHOW(rest_of_platform_power_mw
, PROP
)
48 DPTF_POWER_SHOW(max_steady_state_power_mw
, PBSS
)
49 DPTF_POWER_SHOW(high_freq_impedance_mohm
, RBHF
)
50 DPTF_POWER_SHOW(no_load_voltage_mv
, VBNL
)
51 DPTF_POWER_SHOW(current_discharge_capbility_ma
, CMPP
);
53 static DEVICE_ATTR_RO(max_platform_power_mw
);
54 static DEVICE_ATTR_RO(platform_power_source
);
55 static DEVICE_ATTR_RO(adapter_rating_mw
);
56 static DEVICE_ATTR_RO(battery_steady_power_mw
);
57 static DEVICE_ATTR_RO(charger_type
);
58 static DEVICE_ATTR_RO(rest_of_platform_power_mw
);
59 static DEVICE_ATTR_RO(max_steady_state_power_mw
);
60 static DEVICE_ATTR_RO(high_freq_impedance_mohm
);
61 static DEVICE_ATTR_RO(no_load_voltage_mv
);
62 static DEVICE_ATTR_RO(current_discharge_capbility_ma
);
64 static ssize_t
prochot_confirm_store(struct device
*dev
,
65 struct device_attribute
*attr
,
66 const char *buf
, size_t count
)
68 struct acpi_device
*acpi_dev
= dev_get_drvdata(dev
);
72 if (kstrtouint(buf
, 0, &seq_no
) < 0)
75 status
= acpi_execute_simple_method(acpi_dev
->handle
, "PBOK", seq_no
);
76 if (ACPI_SUCCESS(status
))
82 static DEVICE_ATTR_WO(prochot_confirm
);
84 static struct attribute
*dptf_power_attrs
[] = {
85 &dev_attr_max_platform_power_mw
.attr
,
86 &dev_attr_platform_power_source
.attr
,
87 &dev_attr_adapter_rating_mw
.attr
,
88 &dev_attr_battery_steady_power_mw
.attr
,
89 &dev_attr_charger_type
.attr
,
90 &dev_attr_rest_of_platform_power_mw
.attr
,
91 &dev_attr_prochot_confirm
.attr
,
95 static const struct attribute_group dptf_power_attribute_group
= {
96 .attrs
= dptf_power_attrs
,
100 static struct attribute
*dptf_battery_attrs
[] = {
101 &dev_attr_max_platform_power_mw
.attr
,
102 &dev_attr_max_steady_state_power_mw
.attr
,
103 &dev_attr_high_freq_impedance_mohm
.attr
,
104 &dev_attr_no_load_voltage_mv
.attr
,
105 &dev_attr_current_discharge_capbility_ma
.attr
,
109 static const struct attribute_group dptf_battery_attribute_group
= {
110 .attrs
= dptf_battery_attrs
,
111 .name
= "dptf_battery"
114 #define MAX_POWER_CHANGED 0x80
115 #define POWER_STATE_CHANGED 0x81
116 #define STEADY_STATE_POWER_CHANGED 0x83
117 #define POWER_PROP_CHANGE_EVENT 0x84
118 #define IMPEDANCE_CHANGED 0x85
119 #define VOLTAGE_CURRENT_CHANGED 0x86
121 static long long dptf_participant_type(acpi_handle handle
)
123 unsigned long long ptype
;
126 status
= acpi_evaluate_integer(handle
, "PTYP", NULL
, &ptype
);
127 if (ACPI_FAILURE(status
))
133 static void dptf_power_notify(acpi_handle handle
, u32 event
, void *data
)
135 struct platform_device
*pdev
= data
;
139 case POWER_STATE_CHANGED
:
140 attr
= "platform_power_source";
142 case POWER_PROP_CHANGE_EVENT
:
143 attr
= "rest_of_platform_power_mw";
145 case MAX_POWER_CHANGED
:
146 attr
= "max_platform_power_mw";
148 case STEADY_STATE_POWER_CHANGED
:
149 attr
= "max_steady_state_power_mw";
151 case IMPEDANCE_CHANGED
:
152 attr
= "high_freq_impedance_mohm";
154 case VOLTAGE_CURRENT_CHANGED
:
155 attr
= "no_load_voltage_mv";
158 dev_err(&pdev
->dev
, "Unsupported event [0x%x]\n", event
);
163 * Notify that an attribute is changed, so that user space can read
166 if (dptf_participant_type(handle
) == 0x0CULL
)
167 sysfs_notify(&pdev
->dev
.kobj
, "dptf_battery", attr
);
169 sysfs_notify(&pdev
->dev
.kobj
, "dptf_power", attr
);
172 static int dptf_power_add(struct platform_device
*pdev
)
174 const struct attribute_group
*attr_group
;
175 struct acpi_device
*acpi_dev
;
176 unsigned long long ptype
;
179 acpi_dev
= ACPI_COMPANION(&(pdev
->dev
));
183 ptype
= dptf_participant_type(acpi_dev
->handle
);
185 attr_group
= &dptf_power_attribute_group
;
186 else if (ptype
== 0x0C)
187 attr_group
= &dptf_battery_attribute_group
;
191 result
= acpi_install_notify_handler(acpi_dev
->handle
,
198 result
= sysfs_create_group(&pdev
->dev
.kobj
,
201 acpi_remove_notify_handler(acpi_dev
->handle
,
207 platform_set_drvdata(pdev
, acpi_dev
);
212 static void dptf_power_remove(struct platform_device
*pdev
)
214 struct acpi_device
*acpi_dev
= platform_get_drvdata(pdev
);
216 acpi_remove_notify_handler(acpi_dev
->handle
,
220 if (dptf_participant_type(acpi_dev
->handle
) == 0x0CULL
)
221 sysfs_remove_group(&pdev
->dev
.kobj
, &dptf_battery_attribute_group
);
223 sysfs_remove_group(&pdev
->dev
.kobj
, &dptf_power_attribute_group
);
226 static const struct acpi_device_id int3407_device_ids
[] = {
241 MODULE_DEVICE_TABLE(acpi
, int3407_device_ids
);
243 static struct platform_driver dptf_power_driver
= {
244 .probe
= dptf_power_add
,
245 .remove
= dptf_power_remove
,
247 .name
= "dptf_power",
248 .acpi_match_table
= int3407_device_ids
,
252 module_platform_driver(dptf_power_driver
);
254 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
255 MODULE_LICENSE("GPL v2");
256 MODULE_DESCRIPTION("ACPI DPTF platform power driver");