1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
4 * temperature/power/energy sensors and capping functionality.
5 * Copyright (C) 2008 IBM
7 * Author: Darrick J. Wong <darrick.wong@oracle.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/ipmi.h>
13 #include <linux/module.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/jiffies.h>
17 #include <linux/mutex.h>
18 #include <linux/kdev_t.h>
19 #include <linux/spinlock.h>
20 #include <linux/idr.h>
21 #include <linux/slab.h>
22 #include <linux/sched.h>
23 #include <linux/platform_device.h>
24 #include <linux/math64.h>
25 #include <linux/time.h>
26 #include <linux/err.h>
28 #define REFRESH_INTERVAL (HZ)
29 #define IPMI_TIMEOUT (30 * HZ)
32 #define AEM_NETFN 0x2E
34 #define AEM_FIND_FW_CMD 0x80
35 #define AEM_ELEMENT_CMD 0x81
36 #define AEM_FW_INSTANCE_CMD 0x82
38 #define AEM_READ_ELEMENT_CFG 0x80
39 #define AEM_READ_BUFFER 0x81
40 #define AEM_READ_REGISTER 0x82
41 #define AEM_WRITE_REGISTER 0x83
42 #define AEM_SET_REG_MASK 0x84
43 #define AEM_CLEAR_REG_MASK 0x85
44 #define AEM_READ_ELEMENT_CFG2 0x86
46 #define AEM_CONTROL_ELEMENT 0
47 #define AEM_ENERGY_ELEMENT 1
48 #define AEM_CLOCK_ELEMENT 4
49 #define AEM_POWER_CAP_ELEMENT 7
50 #define AEM_EXHAUST_ELEMENT 9
51 #define AEM_POWER_ELEMENT 10
53 #define AEM_MODULE_TYPE_ID 0x0001
55 #define AEM2_NUM_ENERGY_REGS 2
56 #define AEM2_NUM_PCAP_REGS 6
57 #define AEM2_NUM_TEMP_REGS 2
58 #define AEM2_NUM_SENSORS 14
60 #define AEM1_NUM_ENERGY_REGS 1
61 #define AEM1_NUM_SENSORS 3
63 /* AEM 2.x has more energy registers */
64 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
65 /* AEM 2.x needs more sensor files */
66 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
69 #define POWER_CAP_MAX_HOTPLUG 1
70 #define POWER_CAP_MAX 2
71 #define POWER_CAP_MIN_WARNING 3
72 #define POWER_CAP_MIN 4
75 #define AEM_DEFAULT_POWER_INTERVAL 1000
76 #define AEM_MIN_POWER_INTERVAL 200
77 #define UJ_PER_MJ 1000L
79 static DEFINE_IDA(aem_ida);
81 static struct platform_driver aem_driver = {
84 .bus = &platform_bus_type,
88 struct aem_ipmi_data {
89 struct completion read_complete;
90 struct ipmi_addr address;
91 struct ipmi_user *user;
94 struct kernel_ipmi_msg tx_message;
98 unsigned short rx_msg_len;
99 unsigned char rx_result;
102 struct device *bmc_device;
105 struct aem_ro_sensor_template {
107 ssize_t (*show)(struct device *dev,
108 struct device_attribute *devattr,
113 struct aem_rw_sensor_template {
115 ssize_t (*show)(struct device *dev,
116 struct device_attribute *devattr,
118 ssize_t (*set)(struct device *dev,
119 struct device_attribute *devattr,
120 const char *buf, size_t count);
125 struct list_head list;
127 struct device *hwmon_dev;
128 struct platform_device *pdev;
131 unsigned long last_updated; /* In jiffies */
136 struct aem_ipmi_data ipmi;
138 /* Function and buffer to update sensors */
139 void (*update)(struct aem_data *data);
140 struct aem_read_sensor_resp *rs_resp;
151 * Two temperature sensors
152 * Six power cap registers
156 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
158 /* energy use in mJ */
159 u64 energy[AEM_NUM_ENERGY_REGS];
161 /* power sampling interval in ms */
162 unsigned long power_period[AEM_NUM_ENERGY_REGS];
164 /* Everything past here is for AEM2 only */
166 /* power caps in dW */
167 u16 pcap[AEM2_NUM_PCAP_REGS];
169 /* exhaust temperature in C */
170 u8 temp[AEM2_NUM_TEMP_REGS];
173 /* Data structures returned by the AEM firmware */
177 static struct aem_iana_id system_x_id = {
178 .bytes = {0x4D, 0x4F, 0x00}
181 /* These are used to find AEM1 instances */
182 struct aem_find_firmware_req {
183 struct aem_iana_id id;
186 __be16 module_type_id;
189 struct aem_find_firmware_resp {
190 struct aem_iana_id id;
194 /* These are used to find AEM2 instances */
195 struct aem_find_instance_req {
196 struct aem_iana_id id;
198 __be16 module_type_id;
201 struct aem_find_instance_resp {
202 struct aem_iana_id id;
210 /* These are used to query sensors */
211 struct aem_read_sensor_req {
212 struct aem_iana_id id;
220 struct aem_read_sensor_resp {
221 struct aem_iana_id id;
225 /* Data structures to talk to the IPMI layer */
226 struct aem_driver_data {
227 struct list_head aem_devices;
228 struct ipmi_smi_watcher bmc_events;
229 struct ipmi_user_hndl ipmi_hndlrs;
232 static void aem_register_bmc(int iface, struct device *dev);
233 static void aem_bmc_gone(int iface);
234 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
236 static void aem_remove_sensors(struct aem_data *data);
237 static int aem1_find_sensors(struct aem_data *data);
238 static int aem2_find_sensors(struct aem_data *data);
239 static void update_aem1_sensors(struct aem_data *data);
240 static void update_aem2_sensors(struct aem_data *data);
242 static struct aem_driver_data driver_data = {
243 .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
245 .owner = THIS_MODULE,
246 .new_smi = aem_register_bmc,
247 .smi_gone = aem_bmc_gone,
250 .ipmi_recv_hndl = aem_msg_handler,
254 /* Functions to talk to the IPMI layer */
256 /* Initialize IPMI address, message buffers and user data */
257 static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
262 init_completion(&data->read_complete);
263 data->bmc_device = bmc;
265 /* Initialize IPMI address */
266 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
267 data->address.channel = IPMI_BMC_CHANNEL;
268 data->address.data[0] = 0;
269 data->interface = iface;
271 /* Initialize message buffers */
273 data->tx_message.netfn = AEM_NETFN;
275 /* Create IPMI messaging interface user */
276 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
280 "Unable to register user with IPMI interface %d\n",
288 /* Send an IPMI command */
289 static int aem_send_message(struct aem_ipmi_data *data)
293 err = ipmi_validate_addr(&data->address, sizeof(data->address));
298 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
299 &data->tx_message, data, 0, 0, 0);
305 dev_err(data->bmc_device, "request_settime=%x\n", err);
308 dev_err(data->bmc_device, "validate_addr=%x\n", err);
312 /* Dispatch IPMI messages to callers */
313 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
315 unsigned short rx_len;
316 struct aem_ipmi_data *data = user_msg_data;
318 if (msg->msgid != data->tx_msgid) {
319 dev_err(data->bmc_device,
320 "Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
322 (int)data->tx_msgid);
323 ipmi_free_recv_msg(msg);
327 data->rx_recv_type = msg->recv_type;
328 if (msg->msg.data_len > 0)
329 data->rx_result = msg->msg.data[0];
331 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
333 if (msg->msg.data_len > 1) {
334 rx_len = msg->msg.data_len - 1;
335 if (data->rx_msg_len < rx_len)
336 rx_len = data->rx_msg_len;
337 data->rx_msg_len = rx_len;
338 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
340 data->rx_msg_len = 0;
342 ipmi_free_recv_msg(msg);
343 complete(&data->read_complete);
346 /* Sensor support functions */
348 /* Read a sensor value; must be called with data->lock held */
349 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
350 void *buf, size_t size)
353 struct aem_read_sensor_req rs_req;
354 /* Use preallocated rx buffer */
355 struct aem_read_sensor_resp *rs_resp = data->rs_resp;
356 struct aem_ipmi_data *ipmi = &data->ipmi;
358 /* AEM registers are 1, 2, 4 or 8 bytes */
369 rs_req.id = system_x_id;
370 rs_req.module_handle = data->module_handle;
371 rs_req.element = elt;
372 rs_req.subcommand = AEM_READ_REGISTER;
374 rs_req.rx_buf_size = size;
376 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
377 ipmi->tx_message.data = (char *)&rs_req;
378 ipmi->tx_message.data_len = sizeof(rs_req);
380 rs_size = sizeof(*rs_resp) + size;
381 ipmi->rx_msg_data = rs_resp;
382 ipmi->rx_msg_len = rs_size;
384 aem_send_message(ipmi);
386 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
392 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
393 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
401 *x = rs_resp->bytes[0];
406 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
411 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
416 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
426 /* Update AEM energy registers */
427 static void update_aem_energy_one(struct aem_data *data, int which)
429 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
430 &data->energy[which], 8);
433 static void update_aem_energy(struct aem_data *data)
435 update_aem_energy_one(data, 0);
436 if (data->ver_major < 2)
438 update_aem_energy_one(data, 1);
441 /* Update all AEM1 sensors */
442 static void update_aem1_sensors(struct aem_data *data)
444 mutex_lock(&data->lock);
445 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
449 update_aem_energy(data);
451 mutex_unlock(&data->lock);
454 /* Update all AEM2 sensors */
455 static void update_aem2_sensors(struct aem_data *data)
459 mutex_lock(&data->lock);
460 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
464 update_aem_energy(data);
465 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
466 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
468 for (i = POWER_CAP; i <= POWER_AUX; i++)
469 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
472 mutex_unlock(&data->lock);
475 /* Delete an AEM instance */
476 static void aem_delete(struct aem_data *data)
478 list_del(&data->list);
479 aem_remove_sensors(data);
480 kfree(data->rs_resp);
481 hwmon_device_unregister(data->hwmon_dev);
482 ipmi_destroy_user(data->ipmi.user);
483 platform_set_drvdata(data->pdev, NULL);
484 platform_device_unregister(data->pdev);
485 ida_free(&aem_ida, data->id);
489 /* Probe functions for AEM1 devices */
491 /* Retrieve version and module handle for an AEM1 instance */
492 static int aem_find_aem1_count(struct aem_ipmi_data *data)
495 struct aem_find_firmware_req ff_req;
496 struct aem_find_firmware_resp ff_resp;
498 ff_req.id = system_x_id;
500 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
502 data->tx_message.cmd = AEM_FIND_FW_CMD;
503 data->tx_message.data = (char *)&ff_req;
504 data->tx_message.data_len = sizeof(ff_req);
506 data->rx_msg_data = &ff_resp;
507 data->rx_msg_len = sizeof(ff_resp);
509 aem_send_message(data);
511 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
515 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
516 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
519 return ff_resp.num_instances;
522 /* Find and initialize one AEM1 instance */
523 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
525 struct aem_data *data;
529 data = kzalloc(sizeof(*data), GFP_KERNEL);
532 mutex_init(&data->lock);
534 /* Copy instance data */
537 data->module_handle = module_handle;
538 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
539 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
541 /* Create sub-device for this fw instance */
542 data->id = ida_alloc(&aem_ida, GFP_KERNEL);
546 data->pdev = platform_device_alloc(DRVNAME, data->id);
549 data->pdev->dev.driver = &aem_driver.driver;
551 res = platform_device_add(data->pdev);
555 platform_set_drvdata(data->pdev, data);
557 /* Set up IPMI interface */
558 res = aem_init_ipmi_data(&data->ipmi, probe->interface,
563 /* Register with hwmon */
564 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
565 if (IS_ERR(data->hwmon_dev)) {
566 dev_err(&data->pdev->dev,
567 "Unable to register hwmon device for IPMI interface %d\n",
569 res = PTR_ERR(data->hwmon_dev);
573 data->update = update_aem1_sensors;
574 data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL);
575 if (!data->rs_resp) {
581 res = aem1_find_sensors(data);
585 /* Add to our list of AEM devices */
586 list_add_tail(&data->list, &driver_data.aem_devices);
588 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
589 data->ver_major, data->ver_minor,
590 data->module_handle);
594 kfree(data->rs_resp);
596 hwmon_device_unregister(data->hwmon_dev);
598 ipmi_destroy_user(data->ipmi.user);
600 platform_set_drvdata(data->pdev, NULL);
601 platform_device_del(data->pdev);
603 platform_device_put(data->pdev);
605 ida_free(&aem_ida, data->id);
612 /* Find and initialize all AEM1 instances */
613 static void aem_init_aem1(struct aem_ipmi_data *probe)
617 num = aem_find_aem1_count(probe);
618 for (i = 0; i < num; i++) {
619 err = aem_init_aem1_inst(probe, i);
621 dev_err(probe->bmc_device,
622 "Error %d initializing AEM1 0x%X\n",
628 /* Probe functions for AEM2 devices */
630 /* Retrieve version and module handle for an AEM2 instance */
631 static int aem_find_aem2(struct aem_ipmi_data *data,
632 struct aem_find_instance_resp *fi_resp,
636 struct aem_find_instance_req fi_req;
638 fi_req.id = system_x_id;
639 fi_req.instance_number = instance_num;
640 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
642 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
643 data->tx_message.data = (char *)&fi_req;
644 data->tx_message.data_len = sizeof(fi_req);
646 data->rx_msg_data = fi_resp;
647 data->rx_msg_len = sizeof(*fi_resp);
649 aem_send_message(data);
651 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
655 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
656 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
657 fi_resp->num_instances <= instance_num)
663 /* Find and initialize one AEM2 instance */
664 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
665 struct aem_find_instance_resp *fi_resp)
667 struct aem_data *data;
671 data = kzalloc(sizeof(*data), GFP_KERNEL);
674 mutex_init(&data->lock);
676 /* Copy instance data */
677 data->ver_major = fi_resp->major;
678 data->ver_minor = fi_resp->minor;
679 data->module_handle = fi_resp->module_handle;
680 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
681 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
683 /* Create sub-device for this fw instance */
684 data->id = ida_alloc(&aem_ida, GFP_KERNEL);
688 data->pdev = platform_device_alloc(DRVNAME, data->id);
691 data->pdev->dev.driver = &aem_driver.driver;
693 res = platform_device_add(data->pdev);
697 platform_set_drvdata(data->pdev, data);
699 /* Set up IPMI interface */
700 res = aem_init_ipmi_data(&data->ipmi, probe->interface,
705 /* Register with hwmon */
706 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
707 if (IS_ERR(data->hwmon_dev)) {
708 dev_err(&data->pdev->dev,
709 "Unable to register hwmon device for IPMI interface %d\n",
711 res = PTR_ERR(data->hwmon_dev);
715 data->update = update_aem2_sensors;
716 data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL);
717 if (!data->rs_resp) {
723 res = aem2_find_sensors(data);
727 /* Add to our list of AEM devices */
728 list_add_tail(&data->list, &driver_data.aem_devices);
730 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
731 data->ver_major, data->ver_minor,
732 data->module_handle);
736 kfree(data->rs_resp);
738 hwmon_device_unregister(data->hwmon_dev);
740 ipmi_destroy_user(data->ipmi.user);
742 platform_set_drvdata(data->pdev, NULL);
743 platform_device_del(data->pdev);
745 platform_device_put(data->pdev);
747 ida_free(&aem_ida, data->id);
754 /* Find and initialize all AEM2 instances */
755 static void aem_init_aem2(struct aem_ipmi_data *probe)
757 struct aem_find_instance_resp fi_resp;
761 while (!aem_find_aem2(probe, &fi_resp, i)) {
762 if (fi_resp.major != 2) {
763 dev_err(probe->bmc_device,
764 "Unknown AEM v%d; please report this to the maintainer.\n",
769 err = aem_init_aem2_inst(probe, &fi_resp);
771 dev_err(probe->bmc_device,
772 "Error %d initializing AEM2 0x%X\n",
773 err, fi_resp.module_handle);
779 /* Probe a BMC for AEM firmware instances */
780 static void aem_register_bmc(int iface, struct device *dev)
782 struct aem_ipmi_data probe;
784 if (aem_init_ipmi_data(&probe, iface, dev))
787 /* Ignore probe errors; they won't cause problems */
788 aem_init_aem1(&probe);
789 aem_init_aem2(&probe);
791 ipmi_destroy_user(probe.user);
794 /* Handle BMC deletion */
795 static void aem_bmc_gone(int iface)
797 struct aem_data *p1, *next1;
799 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
800 if (p1->ipmi.interface == iface)
804 /* sysfs support functions */
806 /* AEM device name */
807 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
810 struct aem_data *data = dev_get_drvdata(dev);
812 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
814 static SENSOR_DEVICE_ATTR_RO(name, name, 0);
816 /* AEM device version */
817 static ssize_t version_show(struct device *dev,
818 struct device_attribute *devattr, char *buf)
820 struct aem_data *data = dev_get_drvdata(dev);
822 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
824 static SENSOR_DEVICE_ATTR_RO(version, version, 0);
826 /* Display power use */
827 static ssize_t aem_show_power(struct device *dev,
828 struct device_attribute *devattr,
831 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
832 struct aem_data *data = dev_get_drvdata(dev);
833 u64 before, after, delta, time;
834 signed long leftover;
836 mutex_lock(&data->lock);
837 update_aem_energy_one(data, attr->index);
838 time = ktime_get_ns();
839 before = data->energy[attr->index];
841 leftover = schedule_timeout_interruptible(
842 msecs_to_jiffies(data->power_period[attr->index])
845 mutex_unlock(&data->lock);
849 update_aem_energy_one(data, attr->index);
850 time = ktime_get_ns() - time;
851 after = data->energy[attr->index];
852 mutex_unlock(&data->lock);
854 delta = (after - before) * UJ_PER_MJ;
856 return sprintf(buf, "%llu\n",
857 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
860 /* Display energy use */
861 static ssize_t aem_show_energy(struct device *dev,
862 struct device_attribute *devattr,
865 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
866 struct aem_data *a = dev_get_drvdata(dev);
867 mutex_lock(&a->lock);
868 update_aem_energy_one(a, attr->index);
869 mutex_unlock(&a->lock);
871 return sprintf(buf, "%llu\n",
872 (unsigned long long)a->energy[attr->index] * 1000);
875 /* Display power interval registers */
876 static ssize_t aem_show_power_period(struct device *dev,
877 struct device_attribute *devattr,
880 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
881 struct aem_data *a = dev_get_drvdata(dev);
884 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
887 /* Set power interval registers */
888 static ssize_t aem_set_power_period(struct device *dev,
889 struct device_attribute *devattr,
890 const char *buf, size_t count)
892 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
893 struct aem_data *a = dev_get_drvdata(dev);
897 res = kstrtoul(buf, 10, &temp);
901 if (temp < AEM_MIN_POWER_INTERVAL)
904 mutex_lock(&a->lock);
905 a->power_period[attr->index] = temp;
906 mutex_unlock(&a->lock);
911 /* Discover sensors on an AEM device */
912 static int aem_register_sensors(struct aem_data *data,
913 const struct aem_ro_sensor_template *ro,
914 const struct aem_rw_sensor_template *rw)
916 struct device *dev = &data->pdev->dev;
917 struct sensor_device_attribute *sensors = data->sensors;
920 /* Set up read-only sensors */
922 sysfs_attr_init(&sensors->dev_attr.attr);
923 sensors->dev_attr.attr.name = ro->label;
924 sensors->dev_attr.attr.mode = 0444;
925 sensors->dev_attr.show = ro->show;
926 sensors->index = ro->index;
928 err = device_create_file(dev, &sensors->dev_attr);
930 sensors->dev_attr.attr.name = NULL;
937 /* Set up read-write sensors */
939 sysfs_attr_init(&sensors->dev_attr.attr);
940 sensors->dev_attr.attr.name = rw->label;
941 sensors->dev_attr.attr.mode = 0644;
942 sensors->dev_attr.show = rw->show;
943 sensors->dev_attr.store = rw->set;
944 sensors->index = rw->index;
946 err = device_create_file(dev, &sensors->dev_attr);
948 sensors->dev_attr.attr.name = NULL;
955 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
958 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
962 aem_remove_sensors(data);
966 /* sysfs support functions for AEM2 sensors */
968 /* Display temperature use */
969 static ssize_t aem2_show_temp(struct device *dev,
970 struct device_attribute *devattr,
973 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
974 struct aem_data *a = dev_get_drvdata(dev);
977 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
980 /* Display power-capping registers */
981 static ssize_t aem2_show_pcap_value(struct device *dev,
982 struct device_attribute *devattr,
985 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
986 struct aem_data *a = dev_get_drvdata(dev);
989 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
992 /* Remove sensors attached to an AEM device */
993 static void aem_remove_sensors(struct aem_data *data)
997 for (i = 0; i < AEM_NUM_SENSORS; i++) {
998 if (!data->sensors[i].dev_attr.attr.name)
1000 device_remove_file(&data->pdev->dev,
1001 &data->sensors[i].dev_attr);
1004 device_remove_file(&data->pdev->dev,
1005 &sensor_dev_attr_name.dev_attr);
1006 device_remove_file(&data->pdev->dev,
1007 &sensor_dev_attr_version.dev_attr);
1010 /* Sensor probe functions */
1012 /* Description of AEM1 sensors */
1013 static const struct aem_ro_sensor_template aem1_ro_sensors[] = {
1014 {"energy1_input", aem_show_energy, 0},
1015 {"power1_average", aem_show_power, 0},
1019 static const struct aem_rw_sensor_template aem1_rw_sensors[] = {
1020 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1021 {NULL, NULL, NULL, 0},
1024 /* Description of AEM2 sensors */
1025 static const struct aem_ro_sensor_template aem2_ro_sensors[] = {
1026 {"energy1_input", aem_show_energy, 0},
1027 {"energy2_input", aem_show_energy, 1},
1028 {"power1_average", aem_show_power, 0},
1029 {"power2_average", aem_show_power, 1},
1030 {"temp1_input", aem2_show_temp, 0},
1031 {"temp2_input", aem2_show_temp, 1},
1033 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1034 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1035 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1036 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1038 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1039 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1043 static const struct aem_rw_sensor_template aem2_rw_sensors[] = {
1044 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1045 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1046 {NULL, NULL, NULL, 0},
1049 /* Set up AEM1 sensor attrs */
1050 static int aem1_find_sensors(struct aem_data *data)
1052 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1055 /* Set up AEM2 sensor attrs */
1056 static int aem2_find_sensors(struct aem_data *data)
1058 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1061 /* Module init/exit routines */
1063 static int __init aem_init(void)
1067 res = driver_register(&aem_driver.driver);
1069 pr_err("Can't register aem driver\n");
1073 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1079 driver_unregister(&aem_driver.driver);
1084 static void __exit aem_exit(void)
1086 struct aem_data *p1, *next1;
1088 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1089 driver_unregister(&aem_driver.driver);
1090 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1094 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1095 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1096 MODULE_LICENSE("GPL");
1098 module_init(aem_init);
1099 module_exit(aem_exit);
1101 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1102 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1103 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1104 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1105 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1106 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1107 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");