2 * A hwmon driver for the IBM Active Energy Manager temperature/power sensors
3 * and capping functionality.
4 * Copyright (C) 2008 IBM
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/ipmi.h>
24 #include <linux/module.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/jiffies.h>
28 #include <linux/mutex.h>
29 #include <linux/kdev_t.h>
30 #include <linux/spinlock.h>
31 #include <linux/idr.h>
32 #include <linux/sched.h>
33 #include <linux/platform_device.h>
34 #include <linux/math64.h>
35 #include <linux/time.h>
37 #define REFRESH_INTERVAL (HZ)
38 #define IPMI_TIMEOUT (30 * HZ)
41 #define AEM_NETFN 0x2E
43 #define AEM_FIND_FW_CMD 0x80
44 #define AEM_ELEMENT_CMD 0x81
45 #define AEM_FW_INSTANCE_CMD 0x82
47 #define AEM_READ_ELEMENT_CFG 0x80
48 #define AEM_READ_BUFFER 0x81
49 #define AEM_READ_REGISTER 0x82
50 #define AEM_WRITE_REGISTER 0x83
51 #define AEM_SET_REG_MASK 0x84
52 #define AEM_CLEAR_REG_MASK 0x85
53 #define AEM_READ_ELEMENT_CFG2 0x86
55 #define AEM_CONTROL_ELEMENT 0
56 #define AEM_ENERGY_ELEMENT 1
57 #define AEM_CLOCK_ELEMENT 4
58 #define AEM_POWER_CAP_ELEMENT 7
59 #define AEM_EXHAUST_ELEMENT 9
60 #define AEM_POWER_ELEMENT 10
62 #define AEM_MODULE_TYPE_ID 0x0001
64 #define AEM2_NUM_ENERGY_REGS 2
65 #define AEM2_NUM_PCAP_REGS 6
66 #define AEM2_NUM_TEMP_REGS 2
67 #define AEM2_NUM_SENSORS 14
69 #define AEM1_NUM_ENERGY_REGS 1
70 #define AEM1_NUM_SENSORS 3
72 /* AEM 2.x has more energy registers */
73 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
74 /* AEM 2.x needs more sensor files */
75 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
78 #define POWER_CAP_MAX_HOTPLUG 1
79 #define POWER_CAP_MAX 2
80 #define POWER_CAP_MIN_WARNING 3
81 #define POWER_CAP_MIN 4
84 #define AEM_DEFAULT_POWER_INTERVAL 1000
85 #define AEM_MIN_POWER_INTERVAL 200
86 #define UJ_PER_MJ 1000L
88 static DEFINE_IDR(aem_idr);
89 static DEFINE_SPINLOCK(aem_idr_lock);
91 static struct device_driver aem_driver = {
93 .bus = &platform_bus_type,
96 struct aem_ipmi_data {
97 struct completion read_complete;
98 struct ipmi_addr address;
102 struct kernel_ipmi_msg tx_message;
106 unsigned short rx_msg_len;
107 unsigned char rx_result;
110 struct device *bmc_device;
113 struct aem_ro_sensor_template {
115 ssize_t (*show)(struct device *dev,
116 struct device_attribute *devattr,
121 struct aem_rw_sensor_template {
123 ssize_t (*show)(struct device *dev,
124 struct device_attribute *devattr,
126 ssize_t (*set)(struct device *dev,
127 struct device_attribute *devattr,
128 const char *buf, size_t count);
133 struct list_head list;
135 struct device *hwmon_dev;
136 struct platform_device *pdev;
139 unsigned long last_updated; /* In jiffies */
144 struct aem_ipmi_data ipmi;
146 /* Function to update sensors */
147 void (*update)(struct aem_data *data);
158 * Two temperature sensors
159 * Six power cap registers
163 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
165 /* energy use in mJ */
166 u64 energy[AEM_NUM_ENERGY_REGS];
168 /* power sampling interval in ms */
169 unsigned long power_period[AEM_NUM_ENERGY_REGS];
171 /* Everything past here is for AEM2 only */
173 /* power caps in dW */
174 u16 pcap[AEM2_NUM_PCAP_REGS];
176 /* exhaust temperature in C */
177 u8 temp[AEM2_NUM_TEMP_REGS];
180 /* Data structures returned by the AEM firmware */
184 static struct aem_iana_id system_x_id = {
185 .bytes = {0x4D, 0x4F, 0x00}
188 /* These are used to find AEM1 instances */
189 struct aem_find_firmware_req {
190 struct aem_iana_id id;
196 struct aem_find_firmware_resp {
197 struct aem_iana_id id;
201 /* These are used to find AEM2 instances */
202 struct aem_find_instance_req {
203 struct aem_iana_id id;
208 struct aem_find_instance_resp {
209 struct aem_iana_id id;
217 /* These are used to query sensors */
218 struct aem_read_sensor_req {
219 struct aem_iana_id id;
227 struct aem_read_sensor_resp {
228 struct aem_iana_id id;
232 /* Data structures to talk to the IPMI layer */
233 struct aem_driver_data {
234 struct list_head aem_devices;
235 struct ipmi_smi_watcher bmc_events;
236 struct ipmi_user_hndl ipmi_hndlrs;
239 static void aem_register_bmc(int iface, struct device *dev);
240 static void aem_bmc_gone(int iface);
241 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
243 static void aem_remove_sensors(struct aem_data *data);
244 static int aem_init_aem1(struct aem_ipmi_data *probe);
245 static int aem_init_aem2(struct aem_ipmi_data *probe);
246 static int aem1_find_sensors(struct aem_data *data);
247 static int aem2_find_sensors(struct aem_data *data);
248 static void update_aem1_sensors(struct aem_data *data);
249 static void update_aem2_sensors(struct aem_data *data);
251 static struct aem_driver_data driver_data = {
252 .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
254 .owner = THIS_MODULE,
255 .new_smi = aem_register_bmc,
256 .smi_gone = aem_bmc_gone,
259 .ipmi_recv_hndl = aem_msg_handler,
263 /* Functions to talk to the IPMI layer */
265 /* Initialize IPMI address, message buffers and user data */
266 static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
271 init_completion(&data->read_complete);
272 data->bmc_device = bmc;
274 /* Initialize IPMI address */
275 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
276 data->address.channel = IPMI_BMC_CHANNEL;
277 data->address.data[0] = 0;
278 data->interface = iface;
280 /* Initialize message buffers */
282 data->tx_message.netfn = AEM_NETFN;
284 /* Create IPMI messaging interface user */
285 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
288 dev_err(bmc, "Unable to register user with IPMI "
289 "interface %d\n", data->interface);
296 /* Send an IPMI command */
297 static int aem_send_message(struct aem_ipmi_data *data)
301 err = ipmi_validate_addr(&data->address, sizeof(data->address));
306 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
307 &data->tx_message, data, 0, 0, 0);
313 dev_err(data->bmc_device, "request_settime=%x\n", err);
316 dev_err(data->bmc_device, "validate_addr=%x\n", err);
320 /* Dispatch IPMI messages to callers */
321 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
323 unsigned short rx_len;
324 struct aem_ipmi_data *data = user_msg_data;
326 if (msg->msgid != data->tx_msgid) {
327 dev_err(data->bmc_device, "Mismatch between received msgid "
328 "(%02x) and transmitted msgid (%02x)!\n",
330 (int)data->tx_msgid);
331 ipmi_free_recv_msg(msg);
335 data->rx_recv_type = msg->recv_type;
336 if (msg->msg.data_len > 0)
337 data->rx_result = msg->msg.data[0];
339 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
341 if (msg->msg.data_len > 1) {
342 rx_len = msg->msg.data_len - 1;
343 if (data->rx_msg_len < rx_len)
344 rx_len = data->rx_msg_len;
345 data->rx_msg_len = rx_len;
346 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
348 data->rx_msg_len = 0;
350 ipmi_free_recv_msg(msg);
351 complete(&data->read_complete);
357 static int aem_idr_get(int *id)
362 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
365 spin_lock(&aem_idr_lock);
366 err = idr_get_new(&aem_idr, NULL, &i);
367 spin_unlock(&aem_idr_lock);
369 if (unlikely(err == -EAGAIN))
371 else if (unlikely(err))
374 *id = i & MAX_ID_MASK;
378 /* Release an object ID */
379 static void aem_idr_put(int id)
381 spin_lock(&aem_idr_lock);
382 idr_remove(&aem_idr, id);
383 spin_unlock(&aem_idr_lock);
386 /* Sensor support functions */
388 /* Read a sensor value */
389 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
390 void *buf, size_t size)
393 struct aem_read_sensor_req rs_req;
394 struct aem_read_sensor_resp *rs_resp;
395 struct aem_ipmi_data *ipmi = &data->ipmi;
397 /* AEM registers are 1, 2, 4 or 8 bytes */
408 rs_req.id = system_x_id;
409 rs_req.module_handle = data->module_handle;
410 rs_req.element = elt;
411 rs_req.subcommand = AEM_READ_REGISTER;
413 rs_req.rx_buf_size = size;
415 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
416 ipmi->tx_message.data = (char *)&rs_req;
417 ipmi->tx_message.data_len = sizeof(rs_req);
419 rs_size = sizeof(*rs_resp) + size;
420 rs_resp = kzalloc(rs_size, GFP_KERNEL);
424 ipmi->rx_msg_data = rs_resp;
425 ipmi->rx_msg_len = rs_size;
427 aem_send_message(ipmi);
429 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
433 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
434 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
442 *x = rs_resp->bytes[0];
447 *x = be16_to_cpup((u16 *)rs_resp->bytes);
452 *x = be32_to_cpup((u32 *)rs_resp->bytes);
457 *x = be64_to_cpup((u64 *)rs_resp->bytes);
465 /* Update AEM energy registers */
466 static void update_aem_energy(struct aem_data *data)
468 aem_read_sensor(data, AEM_ENERGY_ELEMENT, 0, &data->energy[0], 8);
469 if (data->ver_major < 2)
471 aem_read_sensor(data, AEM_ENERGY_ELEMENT, 1, &data->energy[1], 8);
474 /* Update all AEM1 sensors */
475 static void update_aem1_sensors(struct aem_data *data)
477 mutex_lock(&data->lock);
478 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
482 update_aem_energy(data);
484 mutex_unlock(&data->lock);
487 /* Update all AEM2 sensors */
488 static void update_aem2_sensors(struct aem_data *data)
492 mutex_lock(&data->lock);
493 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
497 update_aem_energy(data);
498 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
499 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
501 for (i = POWER_CAP; i <= POWER_AUX; i++)
502 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
505 mutex_unlock(&data->lock);
508 /* Delete an AEM instance */
509 static void aem_delete(struct aem_data *data)
511 list_del(&data->list);
512 aem_remove_sensors(data);
513 hwmon_device_unregister(data->hwmon_dev);
514 ipmi_destroy_user(data->ipmi.user);
515 dev_set_drvdata(&data->pdev->dev, NULL);
516 platform_device_unregister(data->pdev);
517 aem_idr_put(data->id);
521 /* Probe functions for AEM1 devices */
523 /* Retrieve version and module handle for an AEM1 instance */
524 static int aem_find_aem1_count(struct aem_ipmi_data *data)
527 struct aem_find_firmware_req ff_req;
528 struct aem_find_firmware_resp ff_resp;
530 ff_req.id = system_x_id;
532 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
534 data->tx_message.cmd = AEM_FIND_FW_CMD;
535 data->tx_message.data = (char *)&ff_req;
536 data->tx_message.data_len = sizeof(ff_req);
538 data->rx_msg_data = &ff_resp;
539 data->rx_msg_len = sizeof(ff_resp);
541 aem_send_message(data);
543 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
547 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
548 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
551 return ff_resp.num_instances;
554 /* Find and initialize one AEM1 instance */
555 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
557 struct aem_data *data;
561 data = kzalloc(sizeof(*data), GFP_KERNEL);
564 mutex_init(&data->lock);
566 /* Copy instance data */
569 data->module_handle = module_handle;
570 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
571 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
573 /* Create sub-device for this fw instance */
574 if (aem_idr_get(&data->id))
577 data->pdev = platform_device_alloc(DRVNAME, data->id);
580 data->pdev->dev.driver = &aem_driver;
582 res = platform_device_add(data->pdev);
586 dev_set_drvdata(&data->pdev->dev, data);
588 /* Set up IPMI interface */
589 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
593 /* Register with hwmon */
594 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
596 if (IS_ERR(data->hwmon_dev)) {
597 dev_err(&data->pdev->dev, "Unable to register hwmon "
598 "device for IPMI interface %d\n",
603 data->update = update_aem1_sensors;
606 if (aem1_find_sensors(data))
609 /* Add to our list of AEM devices */
610 list_add_tail(&data->list, &driver_data.aem_devices);
612 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
613 data->ver_major, data->ver_minor,
614 data->module_handle);
618 hwmon_device_unregister(data->hwmon_dev);
620 ipmi_destroy_user(data->ipmi.user);
622 dev_set_drvdata(&data->pdev->dev, NULL);
623 platform_device_unregister(data->pdev);
625 aem_idr_put(data->id);
632 /* Find and initialize all AEM1 instances */
633 static int aem_init_aem1(struct aem_ipmi_data *probe)
637 num = aem_find_aem1_count(probe);
638 for (i = 0; i < num; i++) {
639 err = aem_init_aem1_inst(probe, i);
641 dev_err(probe->bmc_device,
642 "Error %d initializing AEM1 0x%X\n",
651 /* Probe functions for AEM2 devices */
653 /* Retrieve version and module handle for an AEM2 instance */
654 static int aem_find_aem2(struct aem_ipmi_data *data,
655 struct aem_find_instance_resp *fi_resp,
659 struct aem_find_instance_req fi_req;
661 fi_req.id = system_x_id;
662 fi_req.instance_number = instance_num;
663 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
665 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
666 data->tx_message.data = (char *)&fi_req;
667 data->tx_message.data_len = sizeof(fi_req);
669 data->rx_msg_data = fi_resp;
670 data->rx_msg_len = sizeof(*fi_resp);
672 aem_send_message(data);
674 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
678 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
679 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)))
685 /* Find and initialize one AEM2 instance */
686 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
687 struct aem_find_instance_resp *fi_resp)
689 struct aem_data *data;
693 data = kzalloc(sizeof(*data), GFP_KERNEL);
696 mutex_init(&data->lock);
698 /* Copy instance data */
699 data->ver_major = fi_resp->major;
700 data->ver_minor = fi_resp->minor;
701 data->module_handle = fi_resp->module_handle;
702 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
703 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
705 /* Create sub-device for this fw instance */
706 if (aem_idr_get(&data->id))
709 data->pdev = platform_device_alloc(DRVNAME, data->id);
712 data->pdev->dev.driver = &aem_driver;
714 res = platform_device_add(data->pdev);
718 dev_set_drvdata(&data->pdev->dev, data);
720 /* Set up IPMI interface */
721 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
725 /* Register with hwmon */
726 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
728 if (IS_ERR(data->hwmon_dev)) {
729 dev_err(&data->pdev->dev, "Unable to register hwmon "
730 "device for IPMI interface %d\n",
735 data->update = update_aem2_sensors;
738 if (aem2_find_sensors(data))
741 /* Add to our list of AEM devices */
742 list_add_tail(&data->list, &driver_data.aem_devices);
744 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
745 data->ver_major, data->ver_minor,
746 data->module_handle);
750 hwmon_device_unregister(data->hwmon_dev);
752 ipmi_destroy_user(data->ipmi.user);
754 dev_set_drvdata(&data->pdev->dev, NULL);
755 platform_device_unregister(data->pdev);
757 aem_idr_put(data->id);
764 /* Find and initialize all AEM2 instances */
765 static int aem_init_aem2(struct aem_ipmi_data *probe)
767 struct aem_find_instance_resp fi_resp;
771 while (!aem_find_aem2(probe, &fi_resp, i)) {
772 if (fi_resp.major != 2) {
773 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
774 "report this to the maintainer.\n",
779 err = aem_init_aem2_inst(probe, &fi_resp);
781 dev_err(probe->bmc_device,
782 "Error %d initializing AEM2 0x%X\n",
783 err, fi_resp.module_handle);
792 /* Probe a BMC for AEM firmware instances */
793 static void aem_register_bmc(int iface, struct device *dev)
795 struct aem_ipmi_data probe;
797 if (aem_init_ipmi_data(&probe, iface, dev))
800 /* Ignore probe errors; they won't cause problems */
801 aem_init_aem1(&probe);
802 aem_init_aem2(&probe);
804 ipmi_destroy_user(probe.user);
807 /* Handle BMC deletion */
808 static void aem_bmc_gone(int iface)
810 struct aem_data *p1, *next1;
812 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
813 if (p1->ipmi.interface == iface)
817 /* sysfs support functions */
819 /* AEM device name */
820 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
823 struct aem_data *data = dev_get_drvdata(dev);
825 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
827 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
829 /* AEM device version */
830 static ssize_t show_version(struct device *dev,
831 struct device_attribute *devattr,
834 struct aem_data *data = dev_get_drvdata(dev);
836 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
838 static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
840 /* Display power use */
841 static ssize_t aem_show_power(struct device *dev,
842 struct device_attribute *devattr,
845 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
846 struct aem_data *data = dev_get_drvdata(dev);
847 u64 before, after, delta, time;
848 signed long leftover;
849 struct timespec b, a;
851 mutex_lock(&data->lock);
852 update_aem_energy(data);
854 before = data->energy[attr->index];
856 leftover = schedule_timeout_interruptible(
857 msecs_to_jiffies(data->power_period[attr->index])
860 mutex_unlock(&data->lock);
864 update_aem_energy(data);
866 after = data->energy[attr->index];
867 mutex_unlock(&data->lock);
869 time = timespec_to_ns(&a) - timespec_to_ns(&b);
870 delta = (after - before) * UJ_PER_MJ;
872 return sprintf(buf, "%llu\n",
873 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
876 /* Display energy use */
877 static ssize_t aem_show_energy(struct device *dev,
878 struct device_attribute *devattr,
881 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
882 struct aem_data *a = dev_get_drvdata(dev);
885 return sprintf(buf, "%llu\n",
886 (unsigned long long)a->energy[attr->index] * 1000);
889 /* Display power interval registers */
890 static ssize_t aem_show_power_period(struct device *dev,
891 struct device_attribute *devattr,
894 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
895 struct aem_data *a = dev_get_drvdata(dev);
898 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
901 /* Set power interval registers */
902 static ssize_t aem_set_power_period(struct device *dev,
903 struct device_attribute *devattr,
904 const char *buf, size_t count)
906 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 struct aem_data *a = dev_get_drvdata(dev);
911 res = strict_strtoul(buf, 10, &temp);
915 if (temp < AEM_MIN_POWER_INTERVAL)
918 mutex_lock(&a->lock);
919 a->power_period[attr->index] = temp;
920 mutex_unlock(&a->lock);
925 /* Discover sensors on an AEM device */
926 static int aem_register_sensors(struct aem_data *data,
927 struct aem_ro_sensor_template *ro,
928 struct aem_rw_sensor_template *rw)
930 struct device *dev = &data->pdev->dev;
931 struct sensor_device_attribute *sensors = data->sensors;
934 /* Set up read-only sensors */
936 sensors->dev_attr.attr.name = ro->label;
937 sensors->dev_attr.attr.mode = S_IRUGO;
938 sensors->dev_attr.show = ro->show;
939 sensors->index = ro->index;
941 err = device_create_file(dev, &sensors->dev_attr);
943 sensors->dev_attr.attr.name = NULL;
950 /* Set up read-write sensors */
952 sensors->dev_attr.attr.name = rw->label;
953 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
954 sensors->dev_attr.show = rw->show;
955 sensors->dev_attr.store = rw->set;
956 sensors->index = rw->index;
958 err = device_create_file(dev, &sensors->dev_attr);
960 sensors->dev_attr.attr.name = NULL;
967 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
970 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
974 aem_remove_sensors(data);
978 /* sysfs support functions for AEM2 sensors */
980 /* Display temperature use */
981 static ssize_t aem2_show_temp(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->temp[attr->index] * 1000);
992 /* Display power-capping registers */
993 static ssize_t aem2_show_pcap_value(struct device *dev,
994 struct device_attribute *devattr,
997 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
998 struct aem_data *a = dev_get_drvdata(dev);
1001 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1004 /* Remove sensors attached to an AEM device */
1005 static void aem_remove_sensors(struct aem_data *data)
1009 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1010 if (!data->sensors[i].dev_attr.attr.name)
1012 device_remove_file(&data->pdev->dev,
1013 &data->sensors[i].dev_attr);
1016 device_remove_file(&data->pdev->dev,
1017 &sensor_dev_attr_name.dev_attr);
1018 device_remove_file(&data->pdev->dev,
1019 &sensor_dev_attr_version.dev_attr);
1022 /* Sensor probe functions */
1024 /* Description of AEM1 sensors */
1025 static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1026 {"energy1_input", aem_show_energy, 0},
1027 {"power1_average", aem_show_power, 0},
1031 static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1032 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1033 {NULL, NULL, NULL, 0},
1036 /* Description of AEM2 sensors */
1037 static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1038 {"energy1_input", aem_show_energy, 0},
1039 {"energy2_input", aem_show_energy, 1},
1040 {"power1_average", aem_show_power, 0},
1041 {"power2_average", aem_show_power, 1},
1042 {"temp1_input", aem2_show_temp, 0},
1043 {"temp2_input", aem2_show_temp, 1},
1045 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1046 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1047 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1048 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1050 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1051 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1055 static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1056 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1057 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1058 {NULL, NULL, NULL, 0},
1061 /* Set up AEM1 sensor attrs */
1062 static int aem1_find_sensors(struct aem_data *data)
1064 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1067 /* Set up AEM2 sensor attrs */
1068 static int aem2_find_sensors(struct aem_data *data)
1070 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1073 /* Module init/exit routines */
1075 static int __init aem_init(void)
1079 res = driver_register(&aem_driver);
1081 printk(KERN_ERR "Can't register aem driver\n");
1085 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1091 driver_unregister(&aem_driver);
1096 static void __exit aem_exit(void)
1098 struct aem_data *p1, *next1;
1100 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1101 driver_unregister(&aem_driver);
1102 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1106 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1107 MODULE_DESCRIPTION("IBM Active Energy Manager power/temp sensor driver");
1108 MODULE_LICENSE("GPL");
1110 module_init(aem_init);
1111 module_exit(aem_exit);