2 * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
3 * temperature/power/energy sensors 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 platform_driver aem_driver
= {
94 .bus
= &platform_bus_type
,
98 struct aem_ipmi_data
{
99 struct completion read_complete
;
100 struct ipmi_addr address
;
104 struct kernel_ipmi_msg tx_message
;
108 unsigned short rx_msg_len
;
109 unsigned char rx_result
;
112 struct device
*bmc_device
;
115 struct aem_ro_sensor_template
{
117 ssize_t (*show
)(struct device
*dev
,
118 struct device_attribute
*devattr
,
123 struct aem_rw_sensor_template
{
125 ssize_t (*show
)(struct device
*dev
,
126 struct device_attribute
*devattr
,
128 ssize_t (*set
)(struct device
*dev
,
129 struct device_attribute
*devattr
,
130 const char *buf
, size_t count
);
135 struct list_head list
;
137 struct device
*hwmon_dev
;
138 struct platform_device
*pdev
;
141 unsigned long last_updated
; /* In jiffies */
146 struct aem_ipmi_data ipmi
;
148 /* Function to update sensors */
149 void (*update
)(struct aem_data
*data
);
160 * Two temperature sensors
161 * Six power cap registers
165 struct sensor_device_attribute sensors
[AEM_NUM_SENSORS
];
167 /* energy use in mJ */
168 u64 energy
[AEM_NUM_ENERGY_REGS
];
170 /* power sampling interval in ms */
171 unsigned long power_period
[AEM_NUM_ENERGY_REGS
];
173 /* Everything past here is for AEM2 only */
175 /* power caps in dW */
176 u16 pcap
[AEM2_NUM_PCAP_REGS
];
178 /* exhaust temperature in C */
179 u8 temp
[AEM2_NUM_TEMP_REGS
];
182 /* Data structures returned by the AEM firmware */
186 static struct aem_iana_id system_x_id
= {
187 .bytes
= {0x4D, 0x4F, 0x00}
190 /* These are used to find AEM1 instances */
191 struct aem_find_firmware_req
{
192 struct aem_iana_id id
;
195 __be16 module_type_id
;
198 struct aem_find_firmware_resp
{
199 struct aem_iana_id id
;
203 /* These are used to find AEM2 instances */
204 struct aem_find_instance_req
{
205 struct aem_iana_id id
;
207 __be16 module_type_id
;
210 struct aem_find_instance_resp
{
211 struct aem_iana_id id
;
219 /* These are used to query sensors */
220 struct aem_read_sensor_req
{
221 struct aem_iana_id id
;
229 struct aem_read_sensor_resp
{
230 struct aem_iana_id id
;
234 /* Data structures to talk to the IPMI layer */
235 struct aem_driver_data
{
236 struct list_head aem_devices
;
237 struct ipmi_smi_watcher bmc_events
;
238 struct ipmi_user_hndl ipmi_hndlrs
;
241 static void aem_register_bmc(int iface
, struct device
*dev
);
242 static void aem_bmc_gone(int iface
);
243 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
245 static void aem_remove_sensors(struct aem_data
*data
);
246 static int aem_init_aem1(struct aem_ipmi_data
*probe
);
247 static int aem_init_aem2(struct aem_ipmi_data
*probe
);
248 static int aem1_find_sensors(struct aem_data
*data
);
249 static int aem2_find_sensors(struct aem_data
*data
);
250 static void update_aem1_sensors(struct aem_data
*data
);
251 static void update_aem2_sensors(struct aem_data
*data
);
253 static struct aem_driver_data driver_data
= {
254 .aem_devices
= LIST_HEAD_INIT(driver_data
.aem_devices
),
256 .owner
= THIS_MODULE
,
257 .new_smi
= aem_register_bmc
,
258 .smi_gone
= aem_bmc_gone
,
261 .ipmi_recv_hndl
= aem_msg_handler
,
265 /* Functions to talk to the IPMI layer */
267 /* Initialize IPMI address, message buffers and user data */
268 static int aem_init_ipmi_data(struct aem_ipmi_data
*data
, int iface
,
273 init_completion(&data
->read_complete
);
274 data
->bmc_device
= bmc
;
276 /* Initialize IPMI address */
277 data
->address
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
278 data
->address
.channel
= IPMI_BMC_CHANNEL
;
279 data
->address
.data
[0] = 0;
280 data
->interface
= iface
;
282 /* Initialize message buffers */
284 data
->tx_message
.netfn
= AEM_NETFN
;
286 /* Create IPMI messaging interface user */
287 err
= ipmi_create_user(data
->interface
, &driver_data
.ipmi_hndlrs
,
290 dev_err(bmc
, "Unable to register user with IPMI "
291 "interface %d\n", data
->interface
);
298 /* Send an IPMI command */
299 static int aem_send_message(struct aem_ipmi_data
*data
)
303 err
= ipmi_validate_addr(&data
->address
, sizeof(data
->address
));
308 err
= ipmi_request_settime(data
->user
, &data
->address
, data
->tx_msgid
,
309 &data
->tx_message
, data
, 0, 0, 0);
315 dev_err(data
->bmc_device
, "request_settime=%x\n", err
);
318 dev_err(data
->bmc_device
, "validate_addr=%x\n", err
);
322 /* Dispatch IPMI messages to callers */
323 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
)
325 unsigned short rx_len
;
326 struct aem_ipmi_data
*data
= user_msg_data
;
328 if (msg
->msgid
!= data
->tx_msgid
) {
329 dev_err(data
->bmc_device
, "Mismatch between received msgid "
330 "(%02x) and transmitted msgid (%02x)!\n",
332 (int)data
->tx_msgid
);
333 ipmi_free_recv_msg(msg
);
337 data
->rx_recv_type
= msg
->recv_type
;
338 if (msg
->msg
.data_len
> 0)
339 data
->rx_result
= msg
->msg
.data
[0];
341 data
->rx_result
= IPMI_UNKNOWN_ERR_COMPLETION_CODE
;
343 if (msg
->msg
.data_len
> 1) {
344 rx_len
= msg
->msg
.data_len
- 1;
345 if (data
->rx_msg_len
< rx_len
)
346 rx_len
= data
->rx_msg_len
;
347 data
->rx_msg_len
= rx_len
;
348 memcpy(data
->rx_msg_data
, msg
->msg
.data
+ 1, data
->rx_msg_len
);
350 data
->rx_msg_len
= 0;
352 ipmi_free_recv_msg(msg
);
353 complete(&data
->read_complete
);
359 static int aem_idr_get(int *id
)
364 if (unlikely(!idr_pre_get(&aem_idr
, GFP_KERNEL
)))
367 spin_lock(&aem_idr_lock
);
368 err
= idr_get_new(&aem_idr
, NULL
, &i
);
369 spin_unlock(&aem_idr_lock
);
371 if (unlikely(err
== -EAGAIN
))
373 else if (unlikely(err
))
376 *id
= i
& MAX_ID_MASK
;
380 /* Release an object ID */
381 static void aem_idr_put(int id
)
383 spin_lock(&aem_idr_lock
);
384 idr_remove(&aem_idr
, id
);
385 spin_unlock(&aem_idr_lock
);
388 /* Sensor support functions */
390 /* Read a sensor value */
391 static int aem_read_sensor(struct aem_data
*data
, u8 elt
, u8 reg
,
392 void *buf
, size_t size
)
395 struct aem_read_sensor_req rs_req
;
396 struct aem_read_sensor_resp
*rs_resp
;
397 struct aem_ipmi_data
*ipmi
= &data
->ipmi
;
399 /* AEM registers are 1, 2, 4 or 8 bytes */
410 rs_req
.id
= system_x_id
;
411 rs_req
.module_handle
= data
->module_handle
;
412 rs_req
.element
= elt
;
413 rs_req
.subcommand
= AEM_READ_REGISTER
;
415 rs_req
.rx_buf_size
= size
;
417 ipmi
->tx_message
.cmd
= AEM_ELEMENT_CMD
;
418 ipmi
->tx_message
.data
= (char *)&rs_req
;
419 ipmi
->tx_message
.data_len
= sizeof(rs_req
);
421 rs_size
= sizeof(*rs_resp
) + size
;
422 rs_resp
= kzalloc(rs_size
, GFP_KERNEL
);
426 ipmi
->rx_msg_data
= rs_resp
;
427 ipmi
->rx_msg_len
= rs_size
;
429 aem_send_message(ipmi
);
431 res
= wait_for_completion_timeout(&ipmi
->read_complete
, IPMI_TIMEOUT
);
435 if (ipmi
->rx_result
|| ipmi
->rx_msg_len
!= rs_size
||
436 memcmp(&rs_resp
->id
, &system_x_id
, sizeof(system_x_id
))) {
444 *x
= rs_resp
->bytes
[0];
449 *x
= be16_to_cpup((__be16
*)rs_resp
->bytes
);
454 *x
= be32_to_cpup((__be32
*)rs_resp
->bytes
);
459 *x
= be64_to_cpup((__be64
*)rs_resp
->bytes
);
467 /* Update AEM energy registers */
468 static void update_aem_energy_one(struct aem_data
*data
, int which
)
470 aem_read_sensor(data
, AEM_ENERGY_ELEMENT
, which
,
471 &data
->energy
[which
], 8);
474 static void update_aem_energy(struct aem_data
*data
)
476 update_aem_energy_one(data
, 0);
477 if (data
->ver_major
< 2)
479 update_aem_energy_one(data
, 1);
482 /* Update all AEM1 sensors */
483 static void update_aem1_sensors(struct aem_data
*data
)
485 mutex_lock(&data
->lock
);
486 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
490 update_aem_energy(data
);
492 mutex_unlock(&data
->lock
);
495 /* Update all AEM2 sensors */
496 static void update_aem2_sensors(struct aem_data
*data
)
500 mutex_lock(&data
->lock
);
501 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
505 update_aem_energy(data
);
506 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 0, &data
->temp
[0], 1);
507 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 1, &data
->temp
[1], 1);
509 for (i
= POWER_CAP
; i
<= POWER_AUX
; i
++)
510 aem_read_sensor(data
, AEM_POWER_CAP_ELEMENT
, i
,
513 mutex_unlock(&data
->lock
);
516 /* Delete an AEM instance */
517 static void aem_delete(struct aem_data
*data
)
519 list_del(&data
->list
);
520 aem_remove_sensors(data
);
521 hwmon_device_unregister(data
->hwmon_dev
);
522 ipmi_destroy_user(data
->ipmi
.user
);
523 dev_set_drvdata(&data
->pdev
->dev
, NULL
);
524 platform_device_unregister(data
->pdev
);
525 aem_idr_put(data
->id
);
529 /* Probe functions for AEM1 devices */
531 /* Retrieve version and module handle for an AEM1 instance */
532 static int aem_find_aem1_count(struct aem_ipmi_data
*data
)
535 struct aem_find_firmware_req ff_req
;
536 struct aem_find_firmware_resp ff_resp
;
538 ff_req
.id
= system_x_id
;
540 ff_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
542 data
->tx_message
.cmd
= AEM_FIND_FW_CMD
;
543 data
->tx_message
.data
= (char *)&ff_req
;
544 data
->tx_message
.data_len
= sizeof(ff_req
);
546 data
->rx_msg_data
= &ff_resp
;
547 data
->rx_msg_len
= sizeof(ff_resp
);
549 aem_send_message(data
);
551 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
555 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(ff_resp
) ||
556 memcmp(&ff_resp
.id
, &system_x_id
, sizeof(system_x_id
)))
559 return ff_resp
.num_instances
;
562 /* Find and initialize one AEM1 instance */
563 static int aem_init_aem1_inst(struct aem_ipmi_data
*probe
, u8 module_handle
)
565 struct aem_data
*data
;
569 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
572 mutex_init(&data
->lock
);
574 /* Copy instance data */
577 data
->module_handle
= module_handle
;
578 for (i
= 0; i
< AEM1_NUM_ENERGY_REGS
; i
++)
579 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
581 /* Create sub-device for this fw instance */
582 if (aem_idr_get(&data
->id
))
585 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
588 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
590 res
= platform_device_add(data
->pdev
);
594 dev_set_drvdata(&data
->pdev
->dev
, data
);
596 /* Set up IPMI interface */
597 if (aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
601 /* Register with hwmon */
602 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
604 if (IS_ERR(data
->hwmon_dev
)) {
605 dev_err(&data
->pdev
->dev
, "Unable to register hwmon "
606 "device for IPMI interface %d\n",
611 data
->update
= update_aem1_sensors
;
614 if (aem1_find_sensors(data
))
617 /* Add to our list of AEM devices */
618 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
620 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
621 data
->ver_major
, data
->ver_minor
,
622 data
->module_handle
);
626 hwmon_device_unregister(data
->hwmon_dev
);
628 ipmi_destroy_user(data
->ipmi
.user
);
630 dev_set_drvdata(&data
->pdev
->dev
, NULL
);
631 platform_device_unregister(data
->pdev
);
633 aem_idr_put(data
->id
);
640 /* Find and initialize all AEM1 instances */
641 static int aem_init_aem1(struct aem_ipmi_data
*probe
)
645 num
= aem_find_aem1_count(probe
);
646 for (i
= 0; i
< num
; i
++) {
647 err
= aem_init_aem1_inst(probe
, i
);
649 dev_err(probe
->bmc_device
,
650 "Error %d initializing AEM1 0x%X\n",
659 /* Probe functions for AEM2 devices */
661 /* Retrieve version and module handle for an AEM2 instance */
662 static int aem_find_aem2(struct aem_ipmi_data
*data
,
663 struct aem_find_instance_resp
*fi_resp
,
667 struct aem_find_instance_req fi_req
;
669 fi_req
.id
= system_x_id
;
670 fi_req
.instance_number
= instance_num
;
671 fi_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
673 data
->tx_message
.cmd
= AEM_FW_INSTANCE_CMD
;
674 data
->tx_message
.data
= (char *)&fi_req
;
675 data
->tx_message
.data_len
= sizeof(fi_req
);
677 data
->rx_msg_data
= fi_resp
;
678 data
->rx_msg_len
= sizeof(*fi_resp
);
680 aem_send_message(data
);
682 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
686 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(*fi_resp
) ||
687 memcmp(&fi_resp
->id
, &system_x_id
, sizeof(system_x_id
)) ||
688 fi_resp
->num_instances
<= instance_num
)
694 /* Find and initialize one AEM2 instance */
695 static int aem_init_aem2_inst(struct aem_ipmi_data
*probe
,
696 struct aem_find_instance_resp
*fi_resp
)
698 struct aem_data
*data
;
702 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
705 mutex_init(&data
->lock
);
707 /* Copy instance data */
708 data
->ver_major
= fi_resp
->major
;
709 data
->ver_minor
= fi_resp
->minor
;
710 data
->module_handle
= fi_resp
->module_handle
;
711 for (i
= 0; i
< AEM2_NUM_ENERGY_REGS
; i
++)
712 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
714 /* Create sub-device for this fw instance */
715 if (aem_idr_get(&data
->id
))
718 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
721 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
723 res
= platform_device_add(data
->pdev
);
727 dev_set_drvdata(&data
->pdev
->dev
, data
);
729 /* Set up IPMI interface */
730 if (aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
734 /* Register with hwmon */
735 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
737 if (IS_ERR(data
->hwmon_dev
)) {
738 dev_err(&data
->pdev
->dev
, "Unable to register hwmon "
739 "device for IPMI interface %d\n",
744 data
->update
= update_aem2_sensors
;
747 if (aem2_find_sensors(data
))
750 /* Add to our list of AEM devices */
751 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
753 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
754 data
->ver_major
, data
->ver_minor
,
755 data
->module_handle
);
759 hwmon_device_unregister(data
->hwmon_dev
);
761 ipmi_destroy_user(data
->ipmi
.user
);
763 dev_set_drvdata(&data
->pdev
->dev
, NULL
);
764 platform_device_unregister(data
->pdev
);
766 aem_idr_put(data
->id
);
773 /* Find and initialize all AEM2 instances */
774 static int aem_init_aem2(struct aem_ipmi_data
*probe
)
776 struct aem_find_instance_resp fi_resp
;
780 while (!aem_find_aem2(probe
, &fi_resp
, i
)) {
781 if (fi_resp
.major
!= 2) {
782 dev_err(probe
->bmc_device
, "Unknown AEM v%d; please "
783 "report this to the maintainer.\n",
788 err
= aem_init_aem2_inst(probe
, &fi_resp
);
790 dev_err(probe
->bmc_device
,
791 "Error %d initializing AEM2 0x%X\n",
792 err
, fi_resp
.module_handle
);
801 /* Probe a BMC for AEM firmware instances */
802 static void aem_register_bmc(int iface
, struct device
*dev
)
804 struct aem_ipmi_data probe
;
806 if (aem_init_ipmi_data(&probe
, iface
, dev
))
809 /* Ignore probe errors; they won't cause problems */
810 aem_init_aem1(&probe
);
811 aem_init_aem2(&probe
);
813 ipmi_destroy_user(probe
.user
);
816 /* Handle BMC deletion */
817 static void aem_bmc_gone(int iface
)
819 struct aem_data
*p1
, *next1
;
821 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
822 if (p1
->ipmi
.interface
== iface
)
826 /* sysfs support functions */
828 /* AEM device name */
829 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*devattr
,
832 struct aem_data
*data
= dev_get_drvdata(dev
);
834 return sprintf(buf
, "%s%d\n", DRVNAME
, data
->ver_major
);
836 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, 0);
838 /* AEM device version */
839 static ssize_t
show_version(struct device
*dev
,
840 struct device_attribute
*devattr
,
843 struct aem_data
*data
= dev_get_drvdata(dev
);
845 return sprintf(buf
, "%d.%d\n", data
->ver_major
, data
->ver_minor
);
847 static SENSOR_DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
, 0);
849 /* Display power use */
850 static ssize_t
aem_show_power(struct device
*dev
,
851 struct device_attribute
*devattr
,
854 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
855 struct aem_data
*data
= dev_get_drvdata(dev
);
856 u64 before
, after
, delta
, time
;
857 signed long leftover
;
858 struct timespec b
, a
;
860 mutex_lock(&data
->lock
);
861 update_aem_energy_one(data
, attr
->index
);
863 before
= data
->energy
[attr
->index
];
865 leftover
= schedule_timeout_interruptible(
866 msecs_to_jiffies(data
->power_period
[attr
->index
])
869 mutex_unlock(&data
->lock
);
873 update_aem_energy_one(data
, attr
->index
);
875 after
= data
->energy
[attr
->index
];
876 mutex_unlock(&data
->lock
);
878 time
= timespec_to_ns(&a
) - timespec_to_ns(&b
);
879 delta
= (after
- before
) * UJ_PER_MJ
;
881 return sprintf(buf
, "%llu\n",
882 (unsigned long long)div64_u64(delta
* NSEC_PER_SEC
, time
));
885 /* Display energy use */
886 static ssize_t
aem_show_energy(struct device
*dev
,
887 struct device_attribute
*devattr
,
890 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
891 struct aem_data
*a
= dev_get_drvdata(dev
);
892 mutex_lock(&a
->lock
);
893 update_aem_energy_one(a
, attr
->index
);
894 mutex_unlock(&a
->lock
);
896 return sprintf(buf
, "%llu\n",
897 (unsigned long long)a
->energy
[attr
->index
] * 1000);
900 /* Display power interval registers */
901 static ssize_t
aem_show_power_period(struct device
*dev
,
902 struct device_attribute
*devattr
,
905 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
906 struct aem_data
*a
= dev_get_drvdata(dev
);
909 return sprintf(buf
, "%lu\n", a
->power_period
[attr
->index
]);
912 /* Set power interval registers */
913 static ssize_t
aem_set_power_period(struct device
*dev
,
914 struct device_attribute
*devattr
,
915 const char *buf
, size_t count
)
917 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
918 struct aem_data
*a
= dev_get_drvdata(dev
);
922 res
= strict_strtoul(buf
, 10, &temp
);
926 if (temp
< AEM_MIN_POWER_INTERVAL
)
929 mutex_lock(&a
->lock
);
930 a
->power_period
[attr
->index
] = temp
;
931 mutex_unlock(&a
->lock
);
936 /* Discover sensors on an AEM device */
937 static int aem_register_sensors(struct aem_data
*data
,
938 struct aem_ro_sensor_template
*ro
,
939 struct aem_rw_sensor_template
*rw
)
941 struct device
*dev
= &data
->pdev
->dev
;
942 struct sensor_device_attribute
*sensors
= data
->sensors
;
945 /* Set up read-only sensors */
947 sensors
->dev_attr
.attr
.name
= ro
->label
;
948 sensors
->dev_attr
.attr
.mode
= S_IRUGO
;
949 sensors
->dev_attr
.show
= ro
->show
;
950 sensors
->index
= ro
->index
;
952 err
= device_create_file(dev
, &sensors
->dev_attr
);
954 sensors
->dev_attr
.attr
.name
= NULL
;
961 /* Set up read-write sensors */
963 sensors
->dev_attr
.attr
.name
= rw
->label
;
964 sensors
->dev_attr
.attr
.mode
= S_IRUGO
| S_IWUSR
;
965 sensors
->dev_attr
.show
= rw
->show
;
966 sensors
->dev_attr
.store
= rw
->set
;
967 sensors
->index
= rw
->index
;
969 err
= device_create_file(dev
, &sensors
->dev_attr
);
971 sensors
->dev_attr
.attr
.name
= NULL
;
978 err
= device_create_file(dev
, &sensor_dev_attr_name
.dev_attr
);
981 err
= device_create_file(dev
, &sensor_dev_attr_version
.dev_attr
);
985 aem_remove_sensors(data
);
989 /* sysfs support functions for AEM2 sensors */
991 /* Display temperature use */
992 static ssize_t
aem2_show_temp(struct device
*dev
,
993 struct device_attribute
*devattr
,
996 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
997 struct aem_data
*a
= dev_get_drvdata(dev
);
1000 return sprintf(buf
, "%u\n", a
->temp
[attr
->index
] * 1000);
1003 /* Display power-capping registers */
1004 static ssize_t
aem2_show_pcap_value(struct device
*dev
,
1005 struct device_attribute
*devattr
,
1008 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
1009 struct aem_data
*a
= dev_get_drvdata(dev
);
1012 return sprintf(buf
, "%u\n", a
->pcap
[attr
->index
] * 100000);
1015 /* Remove sensors attached to an AEM device */
1016 static void aem_remove_sensors(struct aem_data
*data
)
1020 for (i
= 0; i
< AEM_NUM_SENSORS
; i
++) {
1021 if (!data
->sensors
[i
].dev_attr
.attr
.name
)
1023 device_remove_file(&data
->pdev
->dev
,
1024 &data
->sensors
[i
].dev_attr
);
1027 device_remove_file(&data
->pdev
->dev
,
1028 &sensor_dev_attr_name
.dev_attr
);
1029 device_remove_file(&data
->pdev
->dev
,
1030 &sensor_dev_attr_version
.dev_attr
);
1033 /* Sensor probe functions */
1035 /* Description of AEM1 sensors */
1036 static struct aem_ro_sensor_template aem1_ro_sensors
[] = {
1037 {"energy1_input", aem_show_energy
, 0},
1038 {"power1_average", aem_show_power
, 0},
1042 static struct aem_rw_sensor_template aem1_rw_sensors
[] = {
1043 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1044 {NULL
, NULL
, NULL
, 0},
1047 /* Description of AEM2 sensors */
1048 static struct aem_ro_sensor_template aem2_ro_sensors
[] = {
1049 {"energy1_input", aem_show_energy
, 0},
1050 {"energy2_input", aem_show_energy
, 1},
1051 {"power1_average", aem_show_power
, 0},
1052 {"power2_average", aem_show_power
, 1},
1053 {"temp1_input", aem2_show_temp
, 0},
1054 {"temp2_input", aem2_show_temp
, 1},
1056 {"power4_average", aem2_show_pcap_value
, POWER_CAP_MAX_HOTPLUG
},
1057 {"power5_average", aem2_show_pcap_value
, POWER_CAP_MAX
},
1058 {"power6_average", aem2_show_pcap_value
, POWER_CAP_MIN_WARNING
},
1059 {"power7_average", aem2_show_pcap_value
, POWER_CAP_MIN
},
1061 {"power3_average", aem2_show_pcap_value
, POWER_AUX
},
1062 {"power_cap", aem2_show_pcap_value
, POWER_CAP
},
1066 static struct aem_rw_sensor_template aem2_rw_sensors
[] = {
1067 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1068 {"power2_average_interval", aem_show_power_period
, aem_set_power_period
, 1},
1069 {NULL
, NULL
, NULL
, 0},
1072 /* Set up AEM1 sensor attrs */
1073 static int aem1_find_sensors(struct aem_data
*data
)
1075 return aem_register_sensors(data
, aem1_ro_sensors
, aem1_rw_sensors
);
1078 /* Set up AEM2 sensor attrs */
1079 static int aem2_find_sensors(struct aem_data
*data
)
1081 return aem_register_sensors(data
, aem2_ro_sensors
, aem2_rw_sensors
);
1084 /* Module init/exit routines */
1086 static int __init
aem_init(void)
1090 res
= driver_register(&aem_driver
.driver
);
1092 printk(KERN_ERR
"Can't register aem driver\n");
1096 res
= ipmi_smi_watcher_register(&driver_data
.bmc_events
);
1102 driver_unregister(&aem_driver
.driver
);
1107 static void __exit
aem_exit(void)
1109 struct aem_data
*p1
, *next1
;
1111 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
1112 driver_unregister(&aem_driver
.driver
);
1113 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
1117 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1118 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1119 MODULE_LICENSE("GPL");
1121 module_init(aem_init
);
1122 module_exit(aem_exit
);