2 * MEN 14F021P00 Board Management Controller (BMC) hwmon driver.
4 * This is the core hwmon driver of the MEN 14F021P00 BMC.
5 * The BMC monitors the board voltages which can be access with this
6 * driver through sysfs.
8 * Copyright (C) 2014 MEN Mikro Elektronik Nuernberg GmbH
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/jiffies.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <linux/err.h>
26 #define DRV_NAME "menf21bmc_hwmon"
28 #define BMC_VOLT_COUNT 5
29 #define MENF21BMC_V33 0
30 #define MENF21BMC_V5 1
31 #define MENF21BMC_V12 2
32 #define MENF21BMC_V5_SB 3
33 #define MENF21BMC_VBAT 4
35 #define IDX_TO_VOLT_MIN_CMD(idx) (0x40 + idx)
36 #define IDX_TO_VOLT_MAX_CMD(idx) (0x50 + idx)
37 #define IDX_TO_VOLT_INP_CMD(idx) (0x60 + idx)
39 struct menf21bmc_hwmon
{
41 struct i2c_client
*i2c_client
;
42 unsigned long last_update
;
43 int in_val
[BMC_VOLT_COUNT
];
44 int in_min
[BMC_VOLT_COUNT
];
45 int in_max
[BMC_VOLT_COUNT
];
48 static const char *const input_names
[] = {
49 [MENF21BMC_V33
] = "MON_3_3V",
50 [MENF21BMC_V5
] = "MON_5V",
51 [MENF21BMC_V12
] = "MON_12V",
52 [MENF21BMC_V5_SB
] = "5V_STANDBY",
53 [MENF21BMC_VBAT
] = "VBAT"
56 static struct menf21bmc_hwmon
*menf21bmc_hwmon_update(struct device
*dev
)
60 struct menf21bmc_hwmon
*drv_data
= dev_get_drvdata(dev
);
61 struct menf21bmc_hwmon
*data_ret
= drv_data
;
63 if (time_after(jiffies
, drv_data
->last_update
+ HZ
)
64 || !drv_data
->valid
) {
65 for (i
= 0; i
< BMC_VOLT_COUNT
; i
++) {
66 val
= i2c_smbus_read_word_data(drv_data
->i2c_client
,
67 IDX_TO_VOLT_INP_CMD(i
));
69 data_ret
= ERR_PTR(val
);
72 drv_data
->in_val
[i
] = val
;
74 drv_data
->last_update
= jiffies
;
75 drv_data
->valid
= true;
81 static int menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon
*drv_data
)
85 for (i
= 0; i
< BMC_VOLT_COUNT
; i
++) {
86 val
= i2c_smbus_read_word_data(drv_data
->i2c_client
,
87 IDX_TO_VOLT_MIN_CMD(i
));
91 drv_data
->in_min
[i
] = val
;
93 val
= i2c_smbus_read_word_data(drv_data
->i2c_client
,
94 IDX_TO_VOLT_MAX_CMD(i
));
98 drv_data
->in_max
[i
] = val
;
104 show_label(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
106 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
108 return sprintf(buf
, "%s\n", input_names
[attr
->index
]);
112 show_in(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
114 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
115 struct menf21bmc_hwmon
*drv_data
= menf21bmc_hwmon_update(dev
);
117 if (IS_ERR(drv_data
))
118 return PTR_ERR(drv_data
);
120 return sprintf(buf
, "%d\n", drv_data
->in_val
[attr
->index
]);
124 show_min(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
126 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
127 struct menf21bmc_hwmon
*drv_data
= dev_get_drvdata(dev
);
129 return sprintf(buf
, "%d\n", drv_data
->in_min
[attr
->index
]);
133 show_max(struct device
*dev
, struct device_attribute
*devattr
, char *buf
)
135 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
136 struct menf21bmc_hwmon
*drv_data
= dev_get_drvdata(dev
);
138 return sprintf(buf
, "%d\n", drv_data
->in_max
[attr
->index
]);
141 #define create_voltage_sysfs(idx) \
142 static SENSOR_DEVICE_ATTR(in##idx##_input, S_IRUGO, \
143 show_in, NULL, idx); \
144 static SENSOR_DEVICE_ATTR(in##idx##_min, S_IRUGO, \
145 show_min, NULL, idx); \
146 static SENSOR_DEVICE_ATTR(in##idx##_max, S_IRUGO, \
147 show_max, NULL, idx); \
148 static SENSOR_DEVICE_ATTR(in##idx##_label, S_IRUGO, \
149 show_label, NULL, idx);
151 create_voltage_sysfs(0);
152 create_voltage_sysfs(1);
153 create_voltage_sysfs(2);
154 create_voltage_sysfs(3);
155 create_voltage_sysfs(4);
157 static struct attribute
*menf21bmc_hwmon_attrs
[] = {
158 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
159 &sensor_dev_attr_in0_min
.dev_attr
.attr
,
160 &sensor_dev_attr_in0_max
.dev_attr
.attr
,
161 &sensor_dev_attr_in0_label
.dev_attr
.attr
,
163 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
164 &sensor_dev_attr_in1_min
.dev_attr
.attr
,
165 &sensor_dev_attr_in1_max
.dev_attr
.attr
,
166 &sensor_dev_attr_in1_label
.dev_attr
.attr
,
168 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
169 &sensor_dev_attr_in2_min
.dev_attr
.attr
,
170 &sensor_dev_attr_in2_max
.dev_attr
.attr
,
171 &sensor_dev_attr_in2_label
.dev_attr
.attr
,
173 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
174 &sensor_dev_attr_in3_min
.dev_attr
.attr
,
175 &sensor_dev_attr_in3_max
.dev_attr
.attr
,
176 &sensor_dev_attr_in3_label
.dev_attr
.attr
,
178 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
179 &sensor_dev_attr_in4_min
.dev_attr
.attr
,
180 &sensor_dev_attr_in4_max
.dev_attr
.attr
,
181 &sensor_dev_attr_in4_label
.dev_attr
.attr
,
185 ATTRIBUTE_GROUPS(menf21bmc_hwmon
);
187 static int menf21bmc_hwmon_probe(struct platform_device
*pdev
)
190 struct menf21bmc_hwmon
*drv_data
;
191 struct i2c_client
*i2c_client
= to_i2c_client(pdev
->dev
.parent
);
192 struct device
*hwmon_dev
;
194 drv_data
= devm_kzalloc(&pdev
->dev
, sizeof(struct menf21bmc_hwmon
),
199 drv_data
->i2c_client
= i2c_client
;
201 ret
= menf21bmc_hwmon_get_volt_limits(drv_data
);
203 dev_err(&pdev
->dev
, "failed to read sensor limits");
207 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
,
208 "menf21bmc", drv_data
,
209 menf21bmc_hwmon_groups
);
210 if (IS_ERR(hwmon_dev
))
211 return PTR_ERR(hwmon_dev
);
213 dev_info(&pdev
->dev
, "MEN 14F021P00 BMC hwmon device enabled");
218 static struct platform_driver menf21bmc_hwmon
= {
219 .probe
= menf21bmc_hwmon_probe
,
222 .owner
= THIS_MODULE
,
226 module_platform_driver(menf21bmc_hwmon
);
228 MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
229 MODULE_DESCRIPTION("MEN 14F021P00 BMC hwmon");
230 MODULE_LICENSE("GPL v2");
231 MODULE_ALIAS("platform:menf21bmc_hwmon");