1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * dptf_power: DPTF platform power driver
4*4882a593Smuzhiyun * Copyright (c) 2016, Intel Corporation.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/acpi.h>
10*4882a593Smuzhiyun #include <linux/platform_device.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /*
13*4882a593Smuzhiyun * Presentation of attributes which are defined for INT3407 and INT3532.
14*4882a593Smuzhiyun * They are:
15*4882a593Smuzhiyun * PMAX : Maximum platform powe
16*4882a593Smuzhiyun * PSRC : Platform power source
17*4882a593Smuzhiyun * ARTG : Adapter rating
18*4882a593Smuzhiyun * CTYP : Charger type
19*4882a593Smuzhiyun * PBSS : Battery steady power
20*4882a593Smuzhiyun * PROP : Rest of worst case platform Power
21*4882a593Smuzhiyun * PBSS : Power Battery Steady State
22*4882a593Smuzhiyun * PBSS : Power Battery Steady State
23*4882a593Smuzhiyun * RBHF : High Frequency Impedance
24*4882a593Smuzhiyun * VBNL : Instantaneous No-Load Voltage
25*4882a593Smuzhiyun * CMPP : Current Discharge Capability
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun #define DPTF_POWER_SHOW(name, object) \
28*4882a593Smuzhiyun static ssize_t name##_show(struct device *dev,\
29*4882a593Smuzhiyun struct device_attribute *attr,\
30*4882a593Smuzhiyun char *buf)\
31*4882a593Smuzhiyun {\
32*4882a593Smuzhiyun struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
33*4882a593Smuzhiyun unsigned long long val;\
34*4882a593Smuzhiyun acpi_status status;\
35*4882a593Smuzhiyun \
36*4882a593Smuzhiyun status = acpi_evaluate_integer(acpi_dev->handle, #object,\
37*4882a593Smuzhiyun NULL, &val);\
38*4882a593Smuzhiyun if (ACPI_SUCCESS(status))\
39*4882a593Smuzhiyun return sprintf(buf, "%d\n", (int)val);\
40*4882a593Smuzhiyun else \
41*4882a593Smuzhiyun return -EINVAL;\
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun DPTF_POWER_SHOW(max_platform_power_mw, PMAX)
45*4882a593Smuzhiyun DPTF_POWER_SHOW(platform_power_source, PSRC)
46*4882a593Smuzhiyun DPTF_POWER_SHOW(adapter_rating_mw, ARTG)
47*4882a593Smuzhiyun DPTF_POWER_SHOW(battery_steady_power_mw, PBSS)
48*4882a593Smuzhiyun DPTF_POWER_SHOW(charger_type, CTYP)
49*4882a593Smuzhiyun DPTF_POWER_SHOW(rest_of_platform_power_mw, PROP)
50*4882a593Smuzhiyun DPTF_POWER_SHOW(max_steady_state_power_mw, PBSS)
51*4882a593Smuzhiyun DPTF_POWER_SHOW(high_freq_impedance_mohm, RBHF)
52*4882a593Smuzhiyun DPTF_POWER_SHOW(no_load_voltage_mv, VBNL)
53*4882a593Smuzhiyun DPTF_POWER_SHOW(current_discharge_capbility_ma, CMPP);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static DEVICE_ATTR_RO(max_platform_power_mw);
56*4882a593Smuzhiyun static DEVICE_ATTR_RO(platform_power_source);
57*4882a593Smuzhiyun static DEVICE_ATTR_RO(adapter_rating_mw);
58*4882a593Smuzhiyun static DEVICE_ATTR_RO(battery_steady_power_mw);
59*4882a593Smuzhiyun static DEVICE_ATTR_RO(charger_type);
60*4882a593Smuzhiyun static DEVICE_ATTR_RO(rest_of_platform_power_mw);
61*4882a593Smuzhiyun static DEVICE_ATTR_RO(max_steady_state_power_mw);
62*4882a593Smuzhiyun static DEVICE_ATTR_RO(high_freq_impedance_mohm);
63*4882a593Smuzhiyun static DEVICE_ATTR_RO(no_load_voltage_mv);
64*4882a593Smuzhiyun static DEVICE_ATTR_RO(current_discharge_capbility_ma);
65*4882a593Smuzhiyun
prochot_confirm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)66*4882a593Smuzhiyun static ssize_t prochot_confirm_store(struct device *dev,
67*4882a593Smuzhiyun struct device_attribute *attr,
68*4882a593Smuzhiyun const char *buf, size_t count)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun struct acpi_device *acpi_dev = dev_get_drvdata(dev);
71*4882a593Smuzhiyun acpi_status status;
72*4882a593Smuzhiyun int seq_no;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun if (kstrtouint(buf, 0, &seq_no) < 0)
75*4882a593Smuzhiyun return -EINVAL;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun status = acpi_execute_simple_method(acpi_dev->handle, "PBOK", seq_no);
78*4882a593Smuzhiyun if (ACPI_SUCCESS(status))
79*4882a593Smuzhiyun return count;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return -EINVAL;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun static DEVICE_ATTR_WO(prochot_confirm);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun static struct attribute *dptf_power_attrs[] = {
87*4882a593Smuzhiyun &dev_attr_max_platform_power_mw.attr,
88*4882a593Smuzhiyun &dev_attr_platform_power_source.attr,
89*4882a593Smuzhiyun &dev_attr_adapter_rating_mw.attr,
90*4882a593Smuzhiyun &dev_attr_battery_steady_power_mw.attr,
91*4882a593Smuzhiyun &dev_attr_charger_type.attr,
92*4882a593Smuzhiyun &dev_attr_rest_of_platform_power_mw.attr,
93*4882a593Smuzhiyun &dev_attr_prochot_confirm.attr,
94*4882a593Smuzhiyun NULL
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static const struct attribute_group dptf_power_attribute_group = {
98*4882a593Smuzhiyun .attrs = dptf_power_attrs,
99*4882a593Smuzhiyun .name = "dptf_power"
100*4882a593Smuzhiyun };
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun static struct attribute *dptf_battery_attrs[] = {
103*4882a593Smuzhiyun &dev_attr_max_platform_power_mw.attr,
104*4882a593Smuzhiyun &dev_attr_max_steady_state_power_mw.attr,
105*4882a593Smuzhiyun &dev_attr_high_freq_impedance_mohm.attr,
106*4882a593Smuzhiyun &dev_attr_no_load_voltage_mv.attr,
107*4882a593Smuzhiyun &dev_attr_current_discharge_capbility_ma.attr,
108*4882a593Smuzhiyun NULL
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun static const struct attribute_group dptf_battery_attribute_group = {
112*4882a593Smuzhiyun .attrs = dptf_battery_attrs,
113*4882a593Smuzhiyun .name = "dptf_battery"
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun #define MAX_POWER_CHANGED 0x80
117*4882a593Smuzhiyun #define POWER_STATE_CHANGED 0x81
118*4882a593Smuzhiyun #define STEADY_STATE_POWER_CHANGED 0x83
119*4882a593Smuzhiyun #define POWER_PROP_CHANGE_EVENT 0x84
120*4882a593Smuzhiyun #define IMPEDANCED_CHNGED 0x85
121*4882a593Smuzhiyun #define VOLTAGE_CURRENT_CHANGED 0x86
122*4882a593Smuzhiyun
dptf_participant_type(acpi_handle handle)123*4882a593Smuzhiyun static long long dptf_participant_type(acpi_handle handle)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun unsigned long long ptype;
126*4882a593Smuzhiyun acpi_status status;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun status = acpi_evaluate_integer(handle, "PTYP", NULL, &ptype);
129*4882a593Smuzhiyun if (ACPI_FAILURE(status))
130*4882a593Smuzhiyun return -ENODEV;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun return ptype;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
dptf_power_notify(acpi_handle handle,u32 event,void * data)135*4882a593Smuzhiyun static void dptf_power_notify(acpi_handle handle, u32 event, void *data)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun struct platform_device *pdev = data;
138*4882a593Smuzhiyun char *attr;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun switch (event) {
141*4882a593Smuzhiyun case POWER_STATE_CHANGED:
142*4882a593Smuzhiyun attr = "platform_power_source";
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun case POWER_PROP_CHANGE_EVENT:
145*4882a593Smuzhiyun attr = "rest_of_platform_power_mw";
146*4882a593Smuzhiyun break;
147*4882a593Smuzhiyun case MAX_POWER_CHANGED:
148*4882a593Smuzhiyun attr = "max_platform_power_mw";
149*4882a593Smuzhiyun break;
150*4882a593Smuzhiyun case STEADY_STATE_POWER_CHANGED:
151*4882a593Smuzhiyun attr = "max_steady_state_power_mw";
152*4882a593Smuzhiyun break;
153*4882a593Smuzhiyun case VOLTAGE_CURRENT_CHANGED:
154*4882a593Smuzhiyun attr = "no_load_voltage_mv";
155*4882a593Smuzhiyun break;
156*4882a593Smuzhiyun default:
157*4882a593Smuzhiyun dev_err(&pdev->dev, "Unsupported event [0x%x]\n", event);
158*4882a593Smuzhiyun return;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Notify that an attribute is changed, so that user space can read
163*4882a593Smuzhiyun * again.
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun if (dptf_participant_type(handle) == 0x0CULL)
166*4882a593Smuzhiyun sysfs_notify(&pdev->dev.kobj, "dptf_battery", attr);
167*4882a593Smuzhiyun else
168*4882a593Smuzhiyun sysfs_notify(&pdev->dev.kobj, "dptf_power", attr);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
dptf_power_add(struct platform_device * pdev)171*4882a593Smuzhiyun static int dptf_power_add(struct platform_device *pdev)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun const struct attribute_group *attr_group;
174*4882a593Smuzhiyun struct acpi_device *acpi_dev;
175*4882a593Smuzhiyun unsigned long long ptype;
176*4882a593Smuzhiyun int result;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun acpi_dev = ACPI_COMPANION(&(pdev->dev));
179*4882a593Smuzhiyun if (!acpi_dev)
180*4882a593Smuzhiyun return -ENODEV;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun ptype = dptf_participant_type(acpi_dev->handle);
183*4882a593Smuzhiyun if (ptype == 0x11)
184*4882a593Smuzhiyun attr_group = &dptf_power_attribute_group;
185*4882a593Smuzhiyun else if (ptype == 0x0C)
186*4882a593Smuzhiyun attr_group = &dptf_battery_attribute_group;
187*4882a593Smuzhiyun else
188*4882a593Smuzhiyun return -ENODEV;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun result = acpi_install_notify_handler(acpi_dev->handle,
191*4882a593Smuzhiyun ACPI_DEVICE_NOTIFY,
192*4882a593Smuzhiyun dptf_power_notify,
193*4882a593Smuzhiyun (void *)pdev);
194*4882a593Smuzhiyun if (result)
195*4882a593Smuzhiyun return result;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun result = sysfs_create_group(&pdev->dev.kobj,
198*4882a593Smuzhiyun attr_group);
199*4882a593Smuzhiyun if (result) {
200*4882a593Smuzhiyun acpi_remove_notify_handler(acpi_dev->handle,
201*4882a593Smuzhiyun ACPI_DEVICE_NOTIFY,
202*4882a593Smuzhiyun dptf_power_notify);
203*4882a593Smuzhiyun return result;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun platform_set_drvdata(pdev, acpi_dev);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun return 0;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
dptf_power_remove(struct platform_device * pdev)211*4882a593Smuzhiyun static int dptf_power_remove(struct platform_device *pdev)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun struct acpi_device *acpi_dev = platform_get_drvdata(pdev);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun acpi_remove_notify_handler(acpi_dev->handle,
216*4882a593Smuzhiyun ACPI_DEVICE_NOTIFY,
217*4882a593Smuzhiyun dptf_power_notify);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun if (dptf_participant_type(acpi_dev->handle) == 0x0CULL)
220*4882a593Smuzhiyun sysfs_remove_group(&pdev->dev.kobj, &dptf_battery_attribute_group);
221*4882a593Smuzhiyun else
222*4882a593Smuzhiyun sysfs_remove_group(&pdev->dev.kobj, &dptf_power_attribute_group);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun return 0;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun static const struct acpi_device_id int3407_device_ids[] = {
228*4882a593Smuzhiyun {"INT3407", 0},
229*4882a593Smuzhiyun {"INT3532", 0},
230*4882a593Smuzhiyun {"INTC1047", 0},
231*4882a593Smuzhiyun {"INTC1050", 0},
232*4882a593Smuzhiyun {"INTC1060", 0},
233*4882a593Smuzhiyun {"INTC1061", 0},
234*4882a593Smuzhiyun {"", 0},
235*4882a593Smuzhiyun };
236*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, int3407_device_ids);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun static struct platform_driver dptf_power_driver = {
239*4882a593Smuzhiyun .probe = dptf_power_add,
240*4882a593Smuzhiyun .remove = dptf_power_remove,
241*4882a593Smuzhiyun .driver = {
242*4882a593Smuzhiyun .name = "dptf_power",
243*4882a593Smuzhiyun .acpi_match_table = int3407_device_ids,
244*4882a593Smuzhiyun },
245*4882a593Smuzhiyun };
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun module_platform_driver(dptf_power_driver);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
250*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
251*4882a593Smuzhiyun MODULE_DESCRIPTION("ACPI DPTF platform power driver");
252