xref: /OK3568_Linux_fs/kernel/drivers/hwmon/acpi_power_meter.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * A hwmon driver for ACPI 4.0 power meters
4*4882a593Smuzhiyun  * Copyright (C) 2009 IBM
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Author: Darrick J. Wong <darrick.wong@oracle.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/hwmon.h>
11*4882a593Smuzhiyun #include <linux/hwmon-sysfs.h>
12*4882a593Smuzhiyun #include <linux/jiffies.h>
13*4882a593Smuzhiyun #include <linux/mutex.h>
14*4882a593Smuzhiyun #include <linux/dmi.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/kdev_t.h>
17*4882a593Smuzhiyun #include <linux/sched.h>
18*4882a593Smuzhiyun #include <linux/time.h>
19*4882a593Smuzhiyun #include <linux/err.h>
20*4882a593Smuzhiyun #include <linux/acpi.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define ACPI_POWER_METER_NAME		"power_meter"
23*4882a593Smuzhiyun ACPI_MODULE_NAME(ACPI_POWER_METER_NAME);
24*4882a593Smuzhiyun #define ACPI_POWER_METER_DEVICE_NAME	"Power Meter"
25*4882a593Smuzhiyun #define ACPI_POWER_METER_CLASS		"pwr_meter_resource"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define NUM_SENSORS			17
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define POWER_METER_CAN_MEASURE	(1 << 0)
30*4882a593Smuzhiyun #define POWER_METER_CAN_TRIP	(1 << 1)
31*4882a593Smuzhiyun #define POWER_METER_CAN_CAP	(1 << 2)
32*4882a593Smuzhiyun #define POWER_METER_CAN_NOTIFY	(1 << 3)
33*4882a593Smuzhiyun #define POWER_METER_IS_BATTERY	(1 << 8)
34*4882a593Smuzhiyun #define UNKNOWN_HYSTERESIS	0xFFFFFFFF
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #define METER_NOTIFY_CONFIG	0x80
37*4882a593Smuzhiyun #define METER_NOTIFY_TRIP	0x81
38*4882a593Smuzhiyun #define METER_NOTIFY_CAP	0x82
39*4882a593Smuzhiyun #define METER_NOTIFY_CAPPING	0x83
40*4882a593Smuzhiyun #define METER_NOTIFY_INTERVAL	0x84
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define POWER_AVERAGE_NAME	"power1_average"
43*4882a593Smuzhiyun #define POWER_CAP_NAME		"power1_cap"
44*4882a593Smuzhiyun #define POWER_AVG_INTERVAL_NAME	"power1_average_interval"
45*4882a593Smuzhiyun #define POWER_ALARM_NAME	"power1_alarm"
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static int cap_in_hardware;
48*4882a593Smuzhiyun static bool force_cap_on;
49*4882a593Smuzhiyun 
can_cap_in_hardware(void)50*4882a593Smuzhiyun static int can_cap_in_hardware(void)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	return force_cap_on || cap_in_hardware;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static const struct acpi_device_id power_meter_ids[] = {
56*4882a593Smuzhiyun 	{"ACPI000D", 0},
57*4882a593Smuzhiyun 	{"", 0},
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, power_meter_ids);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun struct acpi_power_meter_capabilities {
62*4882a593Smuzhiyun 	u64		flags;
63*4882a593Smuzhiyun 	u64		units;
64*4882a593Smuzhiyun 	u64		type;
65*4882a593Smuzhiyun 	u64		accuracy;
66*4882a593Smuzhiyun 	u64		sampling_time;
67*4882a593Smuzhiyun 	u64		min_avg_interval;
68*4882a593Smuzhiyun 	u64		max_avg_interval;
69*4882a593Smuzhiyun 	u64		hysteresis;
70*4882a593Smuzhiyun 	u64		configurable_cap;
71*4882a593Smuzhiyun 	u64		min_cap;
72*4882a593Smuzhiyun 	u64		max_cap;
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun struct acpi_power_meter_resource {
76*4882a593Smuzhiyun 	struct acpi_device	*acpi_dev;
77*4882a593Smuzhiyun 	acpi_bus_id		name;
78*4882a593Smuzhiyun 	struct mutex		lock;
79*4882a593Smuzhiyun 	struct device		*hwmon_dev;
80*4882a593Smuzhiyun 	struct acpi_power_meter_capabilities	caps;
81*4882a593Smuzhiyun 	acpi_string		model_number;
82*4882a593Smuzhiyun 	acpi_string		serial_number;
83*4882a593Smuzhiyun 	acpi_string		oem_info;
84*4882a593Smuzhiyun 	u64		power;
85*4882a593Smuzhiyun 	u64		cap;
86*4882a593Smuzhiyun 	u64		avg_interval;
87*4882a593Smuzhiyun 	int			sensors_valid;
88*4882a593Smuzhiyun 	unsigned long		sensors_last_updated;
89*4882a593Smuzhiyun 	struct sensor_device_attribute	sensors[NUM_SENSORS];
90*4882a593Smuzhiyun 	int			num_sensors;
91*4882a593Smuzhiyun 	s64			trip[2];
92*4882a593Smuzhiyun 	int			num_domain_devices;
93*4882a593Smuzhiyun 	struct acpi_device	**domain_devices;
94*4882a593Smuzhiyun 	struct kobject		*holders_dir;
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun struct sensor_template {
98*4882a593Smuzhiyun 	char *label;
99*4882a593Smuzhiyun 	ssize_t (*show)(struct device *dev,
100*4882a593Smuzhiyun 			struct device_attribute *devattr,
101*4882a593Smuzhiyun 			char *buf);
102*4882a593Smuzhiyun 	ssize_t (*set)(struct device *dev,
103*4882a593Smuzhiyun 		       struct device_attribute *devattr,
104*4882a593Smuzhiyun 		       const char *buf, size_t count);
105*4882a593Smuzhiyun 	int index;
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* Averaging interval */
update_avg_interval(struct acpi_power_meter_resource * resource)109*4882a593Smuzhiyun static int update_avg_interval(struct acpi_power_meter_resource *resource)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	unsigned long long data;
112*4882a593Smuzhiyun 	acpi_status status;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI",
115*4882a593Smuzhiyun 				       NULL, &data);
116*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
117*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _GAI"));
118*4882a593Smuzhiyun 		return -ENODEV;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	resource->avg_interval = data;
122*4882a593Smuzhiyun 	return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
show_avg_interval(struct device * dev,struct device_attribute * devattr,char * buf)125*4882a593Smuzhiyun static ssize_t show_avg_interval(struct device *dev,
126*4882a593Smuzhiyun 				 struct device_attribute *devattr,
127*4882a593Smuzhiyun 				 char *buf)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
130*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
133*4882a593Smuzhiyun 	update_avg_interval(resource);
134*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n", resource->avg_interval);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
set_avg_interval(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)139*4882a593Smuzhiyun static ssize_t set_avg_interval(struct device *dev,
140*4882a593Smuzhiyun 				struct device_attribute *devattr,
141*4882a593Smuzhiyun 				const char *buf, size_t count)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
144*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
145*4882a593Smuzhiyun 	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
146*4882a593Smuzhiyun 	struct acpi_object_list args = { 1, &arg0 };
147*4882a593Smuzhiyun 	int res;
148*4882a593Smuzhiyun 	unsigned long temp;
149*4882a593Smuzhiyun 	unsigned long long data;
150*4882a593Smuzhiyun 	acpi_status status;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	res = kstrtoul(buf, 10, &temp);
153*4882a593Smuzhiyun 	if (res)
154*4882a593Smuzhiyun 		return res;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (temp > resource->caps.max_avg_interval ||
157*4882a593Smuzhiyun 	    temp < resource->caps.min_avg_interval)
158*4882a593Smuzhiyun 		return -EINVAL;
159*4882a593Smuzhiyun 	arg0.integer.value = temp;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
162*4882a593Smuzhiyun 	status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI",
163*4882a593Smuzhiyun 				       &args, &data);
164*4882a593Smuzhiyun 	if (!ACPI_FAILURE(status))
165*4882a593Smuzhiyun 		resource->avg_interval = temp;
166*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
169*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PAI"));
170*4882a593Smuzhiyun 		return -EINVAL;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	/* _PAI returns 0 on success, nonzero otherwise */
174*4882a593Smuzhiyun 	if (data)
175*4882a593Smuzhiyun 		return -EINVAL;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	return count;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /* Cap functions */
update_cap(struct acpi_power_meter_resource * resource)181*4882a593Smuzhiyun static int update_cap(struct acpi_power_meter_resource *resource)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	unsigned long long data;
184*4882a593Smuzhiyun 	acpi_status status;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL",
187*4882a593Smuzhiyun 				       NULL, &data);
188*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
189*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _GHL"));
190*4882a593Smuzhiyun 		return -ENODEV;
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	resource->cap = data;
194*4882a593Smuzhiyun 	return 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
show_cap(struct device * dev,struct device_attribute * devattr,char * buf)197*4882a593Smuzhiyun static ssize_t show_cap(struct device *dev,
198*4882a593Smuzhiyun 			struct device_attribute *devattr,
199*4882a593Smuzhiyun 			char *buf)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
202*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
205*4882a593Smuzhiyun 	update_cap(resource);
206*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n", resource->cap * 1000);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
set_cap(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)211*4882a593Smuzhiyun static ssize_t set_cap(struct device *dev, struct device_attribute *devattr,
212*4882a593Smuzhiyun 		       const char *buf, size_t count)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
215*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
216*4882a593Smuzhiyun 	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
217*4882a593Smuzhiyun 	struct acpi_object_list args = { 1, &arg0 };
218*4882a593Smuzhiyun 	int res;
219*4882a593Smuzhiyun 	unsigned long temp;
220*4882a593Smuzhiyun 	unsigned long long data;
221*4882a593Smuzhiyun 	acpi_status status;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	res = kstrtoul(buf, 10, &temp);
224*4882a593Smuzhiyun 	if (res)
225*4882a593Smuzhiyun 		return res;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	temp = DIV_ROUND_CLOSEST(temp, 1000);
228*4882a593Smuzhiyun 	if (temp > resource->caps.max_cap || temp < resource->caps.min_cap)
229*4882a593Smuzhiyun 		return -EINVAL;
230*4882a593Smuzhiyun 	arg0.integer.value = temp;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
233*4882a593Smuzhiyun 	status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL",
234*4882a593Smuzhiyun 				       &args, &data);
235*4882a593Smuzhiyun 	if (!ACPI_FAILURE(status))
236*4882a593Smuzhiyun 		resource->cap = temp;
237*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
240*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SHL"));
241*4882a593Smuzhiyun 		return -EINVAL;
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* _SHL returns 0 on success, nonzero otherwise */
245*4882a593Smuzhiyun 	if (data)
246*4882a593Smuzhiyun 		return -EINVAL;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	return count;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /* Power meter trip points */
set_acpi_trip(struct acpi_power_meter_resource * resource)252*4882a593Smuzhiyun static int set_acpi_trip(struct acpi_power_meter_resource *resource)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	union acpi_object arg_objs[] = {
255*4882a593Smuzhiyun 		{ACPI_TYPE_INTEGER},
256*4882a593Smuzhiyun 		{ACPI_TYPE_INTEGER}
257*4882a593Smuzhiyun 	};
258*4882a593Smuzhiyun 	struct acpi_object_list args = { 2, arg_objs };
259*4882a593Smuzhiyun 	unsigned long long data;
260*4882a593Smuzhiyun 	acpi_status status;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Both trip levels must be set */
263*4882a593Smuzhiyun 	if (resource->trip[0] < 0 || resource->trip[1] < 0)
264*4882a593Smuzhiyun 		return 0;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/* This driver stores min, max; ACPI wants max, min. */
267*4882a593Smuzhiyun 	arg_objs[0].integer.value = resource->trip[1];
268*4882a593Smuzhiyun 	arg_objs[1].integer.value = resource->trip[0];
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP",
271*4882a593Smuzhiyun 				       &args, &data);
272*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
273*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTP"));
274*4882a593Smuzhiyun 		return -EINVAL;
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/* _PTP returns 0 on success, nonzero otherwise */
278*4882a593Smuzhiyun 	if (data)
279*4882a593Smuzhiyun 		return -EINVAL;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	return 0;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
set_trip(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)284*4882a593Smuzhiyun static ssize_t set_trip(struct device *dev, struct device_attribute *devattr,
285*4882a593Smuzhiyun 			const char *buf, size_t count)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
288*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
289*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
290*4882a593Smuzhiyun 	int res;
291*4882a593Smuzhiyun 	unsigned long temp;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	res = kstrtoul(buf, 10, &temp);
294*4882a593Smuzhiyun 	if (res)
295*4882a593Smuzhiyun 		return res;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	temp = DIV_ROUND_CLOSEST(temp, 1000);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
300*4882a593Smuzhiyun 	resource->trip[attr->index - 7] = temp;
301*4882a593Smuzhiyun 	res = set_acpi_trip(resource);
302*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (res)
305*4882a593Smuzhiyun 		return res;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	return count;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun /* Power meter */
update_meter(struct acpi_power_meter_resource * resource)311*4882a593Smuzhiyun static int update_meter(struct acpi_power_meter_resource *resource)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	unsigned long long data;
314*4882a593Smuzhiyun 	acpi_status status;
315*4882a593Smuzhiyun 	unsigned long local_jiffies = jiffies;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	if (time_before(local_jiffies, resource->sensors_last_updated +
318*4882a593Smuzhiyun 			msecs_to_jiffies(resource->caps.sampling_time)) &&
319*4882a593Smuzhiyun 			resource->sensors_valid)
320*4882a593Smuzhiyun 		return 0;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
323*4882a593Smuzhiyun 				       NULL, &data);
324*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
325*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMM"));
326*4882a593Smuzhiyun 		return -ENODEV;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	resource->power = data;
330*4882a593Smuzhiyun 	resource->sensors_valid = 1;
331*4882a593Smuzhiyun 	resource->sensors_last_updated = jiffies;
332*4882a593Smuzhiyun 	return 0;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
show_power(struct device * dev,struct device_attribute * devattr,char * buf)335*4882a593Smuzhiyun static ssize_t show_power(struct device *dev,
336*4882a593Smuzhiyun 			  struct device_attribute *devattr,
337*4882a593Smuzhiyun 			  char *buf)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
340*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
343*4882a593Smuzhiyun 	update_meter(resource);
344*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n", resource->power * 1000);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /* Miscellaneous */
show_str(struct device * dev,struct device_attribute * devattr,char * buf)350*4882a593Smuzhiyun static ssize_t show_str(struct device *dev,
351*4882a593Smuzhiyun 			struct device_attribute *devattr,
352*4882a593Smuzhiyun 			char *buf)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
355*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
356*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
357*4882a593Smuzhiyun 	acpi_string val;
358*4882a593Smuzhiyun 	int ret;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	mutex_lock(&resource->lock);
361*4882a593Smuzhiyun 	switch (attr->index) {
362*4882a593Smuzhiyun 	case 0:
363*4882a593Smuzhiyun 		val = resource->model_number;
364*4882a593Smuzhiyun 		break;
365*4882a593Smuzhiyun 	case 1:
366*4882a593Smuzhiyun 		val = resource->serial_number;
367*4882a593Smuzhiyun 		break;
368*4882a593Smuzhiyun 	case 2:
369*4882a593Smuzhiyun 		val = resource->oem_info;
370*4882a593Smuzhiyun 		break;
371*4882a593Smuzhiyun 	default:
372*4882a593Smuzhiyun 		WARN(1, "Implementation error: unexpected attribute index %d\n",
373*4882a593Smuzhiyun 		     attr->index);
374*4882a593Smuzhiyun 		val = "";
375*4882a593Smuzhiyun 		break;
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 	ret = sprintf(buf, "%s\n", val);
378*4882a593Smuzhiyun 	mutex_unlock(&resource->lock);
379*4882a593Smuzhiyun 	return ret;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
show_val(struct device * dev,struct device_attribute * devattr,char * buf)382*4882a593Smuzhiyun static ssize_t show_val(struct device *dev,
383*4882a593Smuzhiyun 			struct device_attribute *devattr,
384*4882a593Smuzhiyun 			char *buf)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
387*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
388*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
389*4882a593Smuzhiyun 	u64 val = 0;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	switch (attr->index) {
392*4882a593Smuzhiyun 	case 0:
393*4882a593Smuzhiyun 		val = resource->caps.min_avg_interval;
394*4882a593Smuzhiyun 		break;
395*4882a593Smuzhiyun 	case 1:
396*4882a593Smuzhiyun 		val = resource->caps.max_avg_interval;
397*4882a593Smuzhiyun 		break;
398*4882a593Smuzhiyun 	case 2:
399*4882a593Smuzhiyun 		val = resource->caps.min_cap * 1000;
400*4882a593Smuzhiyun 		break;
401*4882a593Smuzhiyun 	case 3:
402*4882a593Smuzhiyun 		val = resource->caps.max_cap * 1000;
403*4882a593Smuzhiyun 		break;
404*4882a593Smuzhiyun 	case 4:
405*4882a593Smuzhiyun 		if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS)
406*4882a593Smuzhiyun 			return sprintf(buf, "unknown\n");
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 		val = resource->caps.hysteresis * 1000;
409*4882a593Smuzhiyun 		break;
410*4882a593Smuzhiyun 	case 5:
411*4882a593Smuzhiyun 		if (resource->caps.flags & POWER_METER_IS_BATTERY)
412*4882a593Smuzhiyun 			val = 1;
413*4882a593Smuzhiyun 		else
414*4882a593Smuzhiyun 			val = 0;
415*4882a593Smuzhiyun 		break;
416*4882a593Smuzhiyun 	case 6:
417*4882a593Smuzhiyun 		if (resource->power > resource->cap)
418*4882a593Smuzhiyun 			val = 1;
419*4882a593Smuzhiyun 		else
420*4882a593Smuzhiyun 			val = 0;
421*4882a593Smuzhiyun 		break;
422*4882a593Smuzhiyun 	case 7:
423*4882a593Smuzhiyun 	case 8:
424*4882a593Smuzhiyun 		if (resource->trip[attr->index - 7] < 0)
425*4882a593Smuzhiyun 			return sprintf(buf, "unknown\n");
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 		val = resource->trip[attr->index - 7] * 1000;
428*4882a593Smuzhiyun 		break;
429*4882a593Smuzhiyun 	default:
430*4882a593Smuzhiyun 		WARN(1, "Implementation error: unexpected attribute index %d\n",
431*4882a593Smuzhiyun 		     attr->index);
432*4882a593Smuzhiyun 		break;
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n", val);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
show_accuracy(struct device * dev,struct device_attribute * devattr,char * buf)438*4882a593Smuzhiyun static ssize_t show_accuracy(struct device *dev,
439*4882a593Smuzhiyun 			     struct device_attribute *devattr,
440*4882a593Smuzhiyun 			     char *buf)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun 	struct acpi_device *acpi_dev = to_acpi_device(dev);
443*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
444*4882a593Smuzhiyun 	unsigned int acc = resource->caps.accuracy;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
show_name(struct device * dev,struct device_attribute * devattr,char * buf)449*4882a593Smuzhiyun static ssize_t show_name(struct device *dev,
450*4882a593Smuzhiyun 			 struct device_attribute *devattr,
451*4882a593Smuzhiyun 			 char *buf)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun #define RO_SENSOR_TEMPLATE(_label, _show, _index)	\
457*4882a593Smuzhiyun 	{						\
458*4882a593Smuzhiyun 		.label = _label,			\
459*4882a593Smuzhiyun 		.show  = _show,				\
460*4882a593Smuzhiyun 		.index = _index,			\
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun #define RW_SENSOR_TEMPLATE(_label, _show, _set, _index)	\
464*4882a593Smuzhiyun 	{						\
465*4882a593Smuzhiyun 		.label = _label,			\
466*4882a593Smuzhiyun 		.show  = _show,				\
467*4882a593Smuzhiyun 		.set   = _set,				\
468*4882a593Smuzhiyun 		.index = _index,			\
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun /* Sensor descriptions.  If you add a sensor, update NUM_SENSORS above! */
472*4882a593Smuzhiyun static struct sensor_template meter_attrs[] = {
473*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE(POWER_AVERAGE_NAME, show_power, 0),
474*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_accuracy", show_accuracy, 0),
475*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_average_interval_min", show_val, 0),
476*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_average_interval_max", show_val, 1),
477*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_is_battery", show_val, 5),
478*4882a593Smuzhiyun 	RW_SENSOR_TEMPLATE(POWER_AVG_INTERVAL_NAME, show_avg_interval,
479*4882a593Smuzhiyun 		set_avg_interval, 0),
480*4882a593Smuzhiyun 	{},
481*4882a593Smuzhiyun };
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun static struct sensor_template misc_cap_attrs[] = {
484*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_cap_min", show_val, 2),
485*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_cap_max", show_val, 3),
486*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_cap_hyst", show_val, 4),
487*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE(POWER_ALARM_NAME, show_val, 6),
488*4882a593Smuzhiyun 	{},
489*4882a593Smuzhiyun };
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun static struct sensor_template ro_cap_attrs[] = {
492*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, 0),
493*4882a593Smuzhiyun 	{},
494*4882a593Smuzhiyun };
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun static struct sensor_template rw_cap_attrs[] = {
497*4882a593Smuzhiyun 	RW_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, set_cap, 0),
498*4882a593Smuzhiyun 	{},
499*4882a593Smuzhiyun };
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun static struct sensor_template trip_attrs[] = {
502*4882a593Smuzhiyun 	RW_SENSOR_TEMPLATE("power1_average_min", show_val, set_trip, 7),
503*4882a593Smuzhiyun 	RW_SENSOR_TEMPLATE("power1_average_max", show_val, set_trip, 8),
504*4882a593Smuzhiyun 	{},
505*4882a593Smuzhiyun };
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun static struct sensor_template misc_attrs[] = {
508*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("name", show_name, 0),
509*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_model_number", show_str, 0),
510*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_oem_info", show_str, 2),
511*4882a593Smuzhiyun 	RO_SENSOR_TEMPLATE("power1_serial_number", show_str, 1),
512*4882a593Smuzhiyun 	{},
513*4882a593Smuzhiyun };
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun #undef RO_SENSOR_TEMPLATE
516*4882a593Smuzhiyun #undef RW_SENSOR_TEMPLATE
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /* Read power domain data */
remove_domain_devices(struct acpi_power_meter_resource * resource)519*4882a593Smuzhiyun static void remove_domain_devices(struct acpi_power_meter_resource *resource)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun 	int i;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	if (!resource->num_domain_devices)
524*4882a593Smuzhiyun 		return;
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	for (i = 0; i < resource->num_domain_devices; i++) {
527*4882a593Smuzhiyun 		struct acpi_device *obj = resource->domain_devices[i];
528*4882a593Smuzhiyun 		if (!obj)
529*4882a593Smuzhiyun 			continue;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		sysfs_remove_link(resource->holders_dir,
532*4882a593Smuzhiyun 				  kobject_name(&obj->dev.kobj));
533*4882a593Smuzhiyun 		put_device(&obj->dev);
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	kfree(resource->domain_devices);
537*4882a593Smuzhiyun 	kobject_put(resource->holders_dir);
538*4882a593Smuzhiyun 	resource->num_domain_devices = 0;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun 
read_domain_devices(struct acpi_power_meter_resource * resource)541*4882a593Smuzhiyun static int read_domain_devices(struct acpi_power_meter_resource *resource)
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun 	int res = 0;
544*4882a593Smuzhiyun 	int i;
545*4882a593Smuzhiyun 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
546*4882a593Smuzhiyun 	union acpi_object *pss;
547*4882a593Smuzhiyun 	acpi_status status;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
550*4882a593Smuzhiyun 				      &buffer);
551*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
552*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMD"));
553*4882a593Smuzhiyun 		return -ENODEV;
554*4882a593Smuzhiyun 	}
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	pss = buffer.pointer;
557*4882a593Smuzhiyun 	if (!pss ||
558*4882a593Smuzhiyun 	    pss->type != ACPI_TYPE_PACKAGE) {
559*4882a593Smuzhiyun 		dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
560*4882a593Smuzhiyun 			"Invalid _PMD data\n");
561*4882a593Smuzhiyun 		res = -EFAULT;
562*4882a593Smuzhiyun 		goto end;
563*4882a593Smuzhiyun 	}
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	if (!pss->package.count)
566*4882a593Smuzhiyun 		goto end;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	resource->domain_devices = kcalloc(pss->package.count,
569*4882a593Smuzhiyun 					   sizeof(struct acpi_device *),
570*4882a593Smuzhiyun 					   GFP_KERNEL);
571*4882a593Smuzhiyun 	if (!resource->domain_devices) {
572*4882a593Smuzhiyun 		res = -ENOMEM;
573*4882a593Smuzhiyun 		goto end;
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	resource->holders_dir = kobject_create_and_add("measures",
577*4882a593Smuzhiyun 					&resource->acpi_dev->dev.kobj);
578*4882a593Smuzhiyun 	if (!resource->holders_dir) {
579*4882a593Smuzhiyun 		res = -ENOMEM;
580*4882a593Smuzhiyun 		goto exit_free;
581*4882a593Smuzhiyun 	}
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	resource->num_domain_devices = pss->package.count;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	for (i = 0; i < pss->package.count; i++) {
586*4882a593Smuzhiyun 		struct acpi_device *obj;
587*4882a593Smuzhiyun 		union acpi_object *element = &(pss->package.elements[i]);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		/* Refuse non-references */
590*4882a593Smuzhiyun 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
591*4882a593Smuzhiyun 			continue;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 		/* Create a symlink to domain objects */
594*4882a593Smuzhiyun 		resource->domain_devices[i] = NULL;
595*4882a593Smuzhiyun 		if (acpi_bus_get_device(element->reference.handle,
596*4882a593Smuzhiyun 					&resource->domain_devices[i]))
597*4882a593Smuzhiyun 			continue;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		obj = resource->domain_devices[i];
600*4882a593Smuzhiyun 		get_device(&obj->dev);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 		res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
603*4882a593Smuzhiyun 				      kobject_name(&obj->dev.kobj));
604*4882a593Smuzhiyun 		if (res) {
605*4882a593Smuzhiyun 			put_device(&obj->dev);
606*4882a593Smuzhiyun 			resource->domain_devices[i] = NULL;
607*4882a593Smuzhiyun 		}
608*4882a593Smuzhiyun 	}
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	res = 0;
611*4882a593Smuzhiyun 	goto end;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun exit_free:
614*4882a593Smuzhiyun 	kfree(resource->domain_devices);
615*4882a593Smuzhiyun end:
616*4882a593Smuzhiyun 	kfree(buffer.pointer);
617*4882a593Smuzhiyun 	return res;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun /* Registration and deregistration */
register_attrs(struct acpi_power_meter_resource * resource,struct sensor_template * attrs)621*4882a593Smuzhiyun static int register_attrs(struct acpi_power_meter_resource *resource,
622*4882a593Smuzhiyun 			  struct sensor_template *attrs)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	struct device *dev = &resource->acpi_dev->dev;
625*4882a593Smuzhiyun 	struct sensor_device_attribute *sensors =
626*4882a593Smuzhiyun 		&resource->sensors[resource->num_sensors];
627*4882a593Smuzhiyun 	int res = 0;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	while (attrs->label) {
630*4882a593Smuzhiyun 		sensors->dev_attr.attr.name = attrs->label;
631*4882a593Smuzhiyun 		sensors->dev_attr.attr.mode = 0444;
632*4882a593Smuzhiyun 		sensors->dev_attr.show = attrs->show;
633*4882a593Smuzhiyun 		sensors->index = attrs->index;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 		if (attrs->set) {
636*4882a593Smuzhiyun 			sensors->dev_attr.attr.mode |= 0200;
637*4882a593Smuzhiyun 			sensors->dev_attr.store = attrs->set;
638*4882a593Smuzhiyun 		}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 		sysfs_attr_init(&sensors->dev_attr.attr);
641*4882a593Smuzhiyun 		res = device_create_file(dev, &sensors->dev_attr);
642*4882a593Smuzhiyun 		if (res) {
643*4882a593Smuzhiyun 			sensors->dev_attr.attr.name = NULL;
644*4882a593Smuzhiyun 			goto error;
645*4882a593Smuzhiyun 		}
646*4882a593Smuzhiyun 		sensors++;
647*4882a593Smuzhiyun 		resource->num_sensors++;
648*4882a593Smuzhiyun 		attrs++;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun error:
652*4882a593Smuzhiyun 	return res;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun 
remove_attrs(struct acpi_power_meter_resource * resource)655*4882a593Smuzhiyun static void remove_attrs(struct acpi_power_meter_resource *resource)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	int i;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	for (i = 0; i < resource->num_sensors; i++) {
660*4882a593Smuzhiyun 		if (!resource->sensors[i].dev_attr.attr.name)
661*4882a593Smuzhiyun 			continue;
662*4882a593Smuzhiyun 		device_remove_file(&resource->acpi_dev->dev,
663*4882a593Smuzhiyun 				   &resource->sensors[i].dev_attr);
664*4882a593Smuzhiyun 	}
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	remove_domain_devices(resource);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	resource->num_sensors = 0;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
setup_attrs(struct acpi_power_meter_resource * resource)671*4882a593Smuzhiyun static int setup_attrs(struct acpi_power_meter_resource *resource)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun 	int res = 0;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	res = read_domain_devices(resource);
676*4882a593Smuzhiyun 	if (res)
677*4882a593Smuzhiyun 		return res;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
680*4882a593Smuzhiyun 		res = register_attrs(resource, meter_attrs);
681*4882a593Smuzhiyun 		if (res)
682*4882a593Smuzhiyun 			goto error;
683*4882a593Smuzhiyun 	}
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (resource->caps.flags & POWER_METER_CAN_CAP) {
686*4882a593Smuzhiyun 		if (!can_cap_in_hardware()) {
687*4882a593Smuzhiyun 			dev_warn(&resource->acpi_dev->dev,
688*4882a593Smuzhiyun 				 "Ignoring unsafe software power cap!\n");
689*4882a593Smuzhiyun 			goto skip_unsafe_cap;
690*4882a593Smuzhiyun 		}
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 		if (resource->caps.configurable_cap)
693*4882a593Smuzhiyun 			res = register_attrs(resource, rw_cap_attrs);
694*4882a593Smuzhiyun 		else
695*4882a593Smuzhiyun 			res = register_attrs(resource, ro_cap_attrs);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 		if (res)
698*4882a593Smuzhiyun 			goto error;
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 		res = register_attrs(resource, misc_cap_attrs);
701*4882a593Smuzhiyun 		if (res)
702*4882a593Smuzhiyun 			goto error;
703*4882a593Smuzhiyun 	}
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun skip_unsafe_cap:
706*4882a593Smuzhiyun 	if (resource->caps.flags & POWER_METER_CAN_TRIP) {
707*4882a593Smuzhiyun 		res = register_attrs(resource, trip_attrs);
708*4882a593Smuzhiyun 		if (res)
709*4882a593Smuzhiyun 			goto error;
710*4882a593Smuzhiyun 	}
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	res = register_attrs(resource, misc_attrs);
713*4882a593Smuzhiyun 	if (res)
714*4882a593Smuzhiyun 		goto error;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	return res;
717*4882a593Smuzhiyun error:
718*4882a593Smuzhiyun 	remove_attrs(resource);
719*4882a593Smuzhiyun 	return res;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun 
free_capabilities(struct acpi_power_meter_resource * resource)722*4882a593Smuzhiyun static void free_capabilities(struct acpi_power_meter_resource *resource)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun 	acpi_string *str;
725*4882a593Smuzhiyun 	int i;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	str = &resource->model_number;
728*4882a593Smuzhiyun 	for (i = 0; i < 3; i++, str++)
729*4882a593Smuzhiyun 		kfree(*str);
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun 
read_capabilities(struct acpi_power_meter_resource * resource)732*4882a593Smuzhiyun static int read_capabilities(struct acpi_power_meter_resource *resource)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	int res = 0;
735*4882a593Smuzhiyun 	int i;
736*4882a593Smuzhiyun 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
737*4882a593Smuzhiyun 	struct acpi_buffer state = { 0, NULL };
738*4882a593Smuzhiyun 	struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
739*4882a593Smuzhiyun 	union acpi_object *pss;
740*4882a593Smuzhiyun 	acpi_string *str;
741*4882a593Smuzhiyun 	acpi_status status;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
744*4882a593Smuzhiyun 				      &buffer);
745*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
746*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMC"));
747*4882a593Smuzhiyun 		return -ENODEV;
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	pss = buffer.pointer;
751*4882a593Smuzhiyun 	if (!pss ||
752*4882a593Smuzhiyun 	    pss->type != ACPI_TYPE_PACKAGE ||
753*4882a593Smuzhiyun 	    pss->package.count != 14) {
754*4882a593Smuzhiyun 		dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
755*4882a593Smuzhiyun 			"Invalid _PMC data\n");
756*4882a593Smuzhiyun 		res = -EFAULT;
757*4882a593Smuzhiyun 		goto end;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/* Grab all the integer data at once */
761*4882a593Smuzhiyun 	state.length = sizeof(struct acpi_power_meter_capabilities);
762*4882a593Smuzhiyun 	state.pointer = &resource->caps;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	status = acpi_extract_package(pss, &format, &state);
765*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
766*4882a593Smuzhiyun 		ACPI_EXCEPTION((AE_INFO, status, "Invalid data"));
767*4882a593Smuzhiyun 		res = -EFAULT;
768*4882a593Smuzhiyun 		goto end;
769*4882a593Smuzhiyun 	}
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	if (resource->caps.units) {
772*4882a593Smuzhiyun 		dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
773*4882a593Smuzhiyun 			"Unknown units %llu.\n",
774*4882a593Smuzhiyun 			resource->caps.units);
775*4882a593Smuzhiyun 		res = -EINVAL;
776*4882a593Smuzhiyun 		goto end;
777*4882a593Smuzhiyun 	}
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	/* Grab the string data */
780*4882a593Smuzhiyun 	str = &resource->model_number;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	for (i = 11; i < 14; i++) {
783*4882a593Smuzhiyun 		union acpi_object *element = &(pss->package.elements[i]);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 		if (element->type != ACPI_TYPE_STRING) {
786*4882a593Smuzhiyun 			res = -EINVAL;
787*4882a593Smuzhiyun 			goto error;
788*4882a593Smuzhiyun 		}
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 		*str = kcalloc(element->string.length + 1, sizeof(u8),
791*4882a593Smuzhiyun 			       GFP_KERNEL);
792*4882a593Smuzhiyun 		if (!*str) {
793*4882a593Smuzhiyun 			res = -ENOMEM;
794*4882a593Smuzhiyun 			goto error;
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		strncpy(*str, element->string.pointer, element->string.length);
798*4882a593Smuzhiyun 		str++;
799*4882a593Smuzhiyun 	}
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
802*4882a593Smuzhiyun 	goto end;
803*4882a593Smuzhiyun error:
804*4882a593Smuzhiyun 	str = &resource->model_number;
805*4882a593Smuzhiyun 	for (i = 0; i < 3; i++, str++)
806*4882a593Smuzhiyun 		kfree(*str);
807*4882a593Smuzhiyun end:
808*4882a593Smuzhiyun 	kfree(buffer.pointer);
809*4882a593Smuzhiyun 	return res;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun /* Handle ACPI event notifications */
acpi_power_meter_notify(struct acpi_device * device,u32 event)813*4882a593Smuzhiyun static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource;
816*4882a593Smuzhiyun 	int res;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	if (!device || !acpi_driver_data(device))
819*4882a593Smuzhiyun 		return;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	resource = acpi_driver_data(device);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	switch (event) {
824*4882a593Smuzhiyun 	case METER_NOTIFY_CONFIG:
825*4882a593Smuzhiyun 		mutex_lock(&resource->lock);
826*4882a593Smuzhiyun 		free_capabilities(resource);
827*4882a593Smuzhiyun 		res = read_capabilities(resource);
828*4882a593Smuzhiyun 		mutex_unlock(&resource->lock);
829*4882a593Smuzhiyun 		if (res)
830*4882a593Smuzhiyun 			break;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 		remove_attrs(resource);
833*4882a593Smuzhiyun 		setup_attrs(resource);
834*4882a593Smuzhiyun 		break;
835*4882a593Smuzhiyun 	case METER_NOTIFY_TRIP:
836*4882a593Smuzhiyun 		sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
837*4882a593Smuzhiyun 		break;
838*4882a593Smuzhiyun 	case METER_NOTIFY_CAP:
839*4882a593Smuzhiyun 		sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
840*4882a593Smuzhiyun 		break;
841*4882a593Smuzhiyun 	case METER_NOTIFY_INTERVAL:
842*4882a593Smuzhiyun 		sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
843*4882a593Smuzhiyun 		break;
844*4882a593Smuzhiyun 	case METER_NOTIFY_CAPPING:
845*4882a593Smuzhiyun 		sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
846*4882a593Smuzhiyun 		dev_info(&device->dev, "Capping in progress.\n");
847*4882a593Smuzhiyun 		break;
848*4882a593Smuzhiyun 	default:
849*4882a593Smuzhiyun 		WARN(1, "Unexpected event %d\n", event);
850*4882a593Smuzhiyun 		break;
851*4882a593Smuzhiyun 	}
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
854*4882a593Smuzhiyun 					dev_name(&device->dev), event, 0);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
acpi_power_meter_add(struct acpi_device * device)857*4882a593Smuzhiyun static int acpi_power_meter_add(struct acpi_device *device)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun 	int res;
860*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	if (!device)
863*4882a593Smuzhiyun 		return -EINVAL;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	resource = kzalloc(sizeof(struct acpi_power_meter_resource),
866*4882a593Smuzhiyun 			   GFP_KERNEL);
867*4882a593Smuzhiyun 	if (!resource)
868*4882a593Smuzhiyun 		return -ENOMEM;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	resource->sensors_valid = 0;
871*4882a593Smuzhiyun 	resource->acpi_dev = device;
872*4882a593Smuzhiyun 	mutex_init(&resource->lock);
873*4882a593Smuzhiyun 	strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
874*4882a593Smuzhiyun 	strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
875*4882a593Smuzhiyun 	device->driver_data = resource;
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	free_capabilities(resource);
878*4882a593Smuzhiyun 	res = read_capabilities(resource);
879*4882a593Smuzhiyun 	if (res)
880*4882a593Smuzhiyun 		goto exit_free;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	resource->trip[0] = resource->trip[1] = -1;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	res = setup_attrs(resource);
885*4882a593Smuzhiyun 	if (res)
886*4882a593Smuzhiyun 		goto exit_free_capability;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	resource->hwmon_dev = hwmon_device_register(&device->dev);
889*4882a593Smuzhiyun 	if (IS_ERR(resource->hwmon_dev)) {
890*4882a593Smuzhiyun 		res = PTR_ERR(resource->hwmon_dev);
891*4882a593Smuzhiyun 		goto exit_remove;
892*4882a593Smuzhiyun 	}
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	res = 0;
895*4882a593Smuzhiyun 	goto exit;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun exit_remove:
898*4882a593Smuzhiyun 	remove_attrs(resource);
899*4882a593Smuzhiyun exit_free_capability:
900*4882a593Smuzhiyun 	free_capabilities(resource);
901*4882a593Smuzhiyun exit_free:
902*4882a593Smuzhiyun 	kfree(resource);
903*4882a593Smuzhiyun exit:
904*4882a593Smuzhiyun 	return res;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun 
acpi_power_meter_remove(struct acpi_device * device)907*4882a593Smuzhiyun static int acpi_power_meter_remove(struct acpi_device *device)
908*4882a593Smuzhiyun {
909*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	if (!device || !acpi_driver_data(device))
912*4882a593Smuzhiyun 		return -EINVAL;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	resource = acpi_driver_data(device);
915*4882a593Smuzhiyun 	hwmon_device_unregister(resource->hwmon_dev);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	remove_attrs(resource);
918*4882a593Smuzhiyun 	free_capabilities(resource);
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	kfree(resource);
921*4882a593Smuzhiyun 	return 0;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
925*4882a593Smuzhiyun 
acpi_power_meter_resume(struct device * dev)926*4882a593Smuzhiyun static int acpi_power_meter_resume(struct device *dev)
927*4882a593Smuzhiyun {
928*4882a593Smuzhiyun 	struct acpi_power_meter_resource *resource;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	if (!dev)
931*4882a593Smuzhiyun 		return -EINVAL;
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	resource = acpi_driver_data(to_acpi_device(dev));
934*4882a593Smuzhiyun 	if (!resource)
935*4882a593Smuzhiyun 		return -EINVAL;
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	free_capabilities(resource);
938*4882a593Smuzhiyun 	read_capabilities(resource);
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	return 0;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL, acpi_power_meter_resume);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun static struct acpi_driver acpi_power_meter_driver = {
948*4882a593Smuzhiyun 	.name = "power_meter",
949*4882a593Smuzhiyun 	.class = ACPI_POWER_METER_CLASS,
950*4882a593Smuzhiyun 	.ids = power_meter_ids,
951*4882a593Smuzhiyun 	.ops = {
952*4882a593Smuzhiyun 		.add = acpi_power_meter_add,
953*4882a593Smuzhiyun 		.remove = acpi_power_meter_remove,
954*4882a593Smuzhiyun 		.notify = acpi_power_meter_notify,
955*4882a593Smuzhiyun 		},
956*4882a593Smuzhiyun 	.drv.pm = &acpi_power_meter_pm,
957*4882a593Smuzhiyun };
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun /* Module init/exit routines */
enable_cap_knobs(const struct dmi_system_id * d)960*4882a593Smuzhiyun static int __init enable_cap_knobs(const struct dmi_system_id *d)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun 	cap_in_hardware = 1;
963*4882a593Smuzhiyun 	return 0;
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun static const struct dmi_system_id pm_dmi_table[] __initconst = {
967*4882a593Smuzhiyun 	{
968*4882a593Smuzhiyun 		enable_cap_knobs, "IBM Active Energy Manager",
969*4882a593Smuzhiyun 		{
970*4882a593Smuzhiyun 			DMI_MATCH(DMI_SYS_VENDOR, "IBM")
971*4882a593Smuzhiyun 		},
972*4882a593Smuzhiyun 	},
973*4882a593Smuzhiyun 	{}
974*4882a593Smuzhiyun };
975*4882a593Smuzhiyun 
acpi_power_meter_init(void)976*4882a593Smuzhiyun static int __init acpi_power_meter_init(void)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	int result;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	if (acpi_disabled)
981*4882a593Smuzhiyun 		return -ENODEV;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	dmi_check_system(pm_dmi_table);
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	result = acpi_bus_register_driver(&acpi_power_meter_driver);
986*4882a593Smuzhiyun 	if (result < 0)
987*4882a593Smuzhiyun 		return result;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	return 0;
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun 
acpi_power_meter_exit(void)992*4882a593Smuzhiyun static void __exit acpi_power_meter_exit(void)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun 	acpi_bus_unregister_driver(&acpi_power_meter_driver);
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
998*4882a593Smuzhiyun MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
999*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun module_param(force_cap_on, bool, 0644);
1002*4882a593Smuzhiyun MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun module_init(acpi_power_meter_init);
1005*4882a593Smuzhiyun module_exit(acpi_power_meter_exit);
1006