1 /*
2 * MEN 14F021P00 Board Management Controller (BMC) hwmon driver.
3 *
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.
7 *
8 * Copyright (C) 2014 MEN Mikro Elektronik Nuernberg GmbH
9 *
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.
14 */
15
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>
25
26 #define DRV_NAME "menf21bmc_hwmon"
27
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
34
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)
38
39 struct menf21bmc_hwmon {
40 bool valid;
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];
46 };
47
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"
54 };
55
menf21bmc_hwmon_update(struct device * dev)56 static struct menf21bmc_hwmon *menf21bmc_hwmon_update(struct device *dev)
57 {
58 int i;
59 int val;
60 struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
61 struct menf21bmc_hwmon *data_ret = drv_data;
62
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));
68 if (val < 0) {
69 data_ret = ERR_PTR(val);
70 goto abort;
71 }
72 drv_data->in_val[i] = val;
73 }
74 drv_data->last_update = jiffies;
75 drv_data->valid = true;
76 }
77 abort:
78 return data_ret;
79 }
80
menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon * drv_data)81 static int menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon *drv_data)
82 {
83 int i, val;
84
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));
88 if (val < 0)
89 return val;
90
91 drv_data->in_min[i] = val;
92
93 val = i2c_smbus_read_word_data(drv_data->i2c_client,
94 IDX_TO_VOLT_MAX_CMD(i));
95 if (val < 0)
96 return val;
97
98 drv_data->in_max[i] = val;
99 }
100 return 0;
101 }
102
103 static ssize_t
show_label(struct device * dev,struct device_attribute * devattr,char * buf)104 show_label(struct device *dev, struct device_attribute *devattr, char *buf)
105 {
106 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
107
108 return sprintf(buf, "%s\n", input_names[attr->index]);
109 }
110
111 static ssize_t
show_in(struct device * dev,struct device_attribute * devattr,char * buf)112 show_in(struct device *dev, struct device_attribute *devattr, char *buf)
113 {
114 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
115 struct menf21bmc_hwmon *drv_data = menf21bmc_hwmon_update(dev);
116
117 if (IS_ERR(drv_data))
118 return PTR_ERR(drv_data);
119
120 return sprintf(buf, "%d\n", drv_data->in_val[attr->index]);
121 }
122
123 static ssize_t
show_min(struct device * dev,struct device_attribute * devattr,char * buf)124 show_min(struct device *dev, struct device_attribute *devattr, char *buf)
125 {
126 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
127 struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
128
129 return sprintf(buf, "%d\n", drv_data->in_min[attr->index]);
130 }
131
132 static ssize_t
show_max(struct device * dev,struct device_attribute * devattr,char * buf)133 show_max(struct device *dev, struct device_attribute *devattr, char *buf)
134 {
135 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
136 struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
137
138 return sprintf(buf, "%d\n", drv_data->in_max[attr->index]);
139 }
140
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);
150
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);
156
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,
162
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,
167
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,
172
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,
177
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,
182 NULL
183 };
184
185 ATTRIBUTE_GROUPS(menf21bmc_hwmon);
186
menf21bmc_hwmon_probe(struct platform_device * pdev)187 static int menf21bmc_hwmon_probe(struct platform_device *pdev)
188 {
189 int ret;
190 struct menf21bmc_hwmon *drv_data;
191 struct i2c_client *i2c_client = to_i2c_client(pdev->dev.parent);
192 struct device *hwmon_dev;
193
194 drv_data = devm_kzalloc(&pdev->dev, sizeof(struct menf21bmc_hwmon),
195 GFP_KERNEL);
196 if (!drv_data)
197 return -ENOMEM;
198
199 drv_data->i2c_client = i2c_client;
200
201 ret = menf21bmc_hwmon_get_volt_limits(drv_data);
202 if (ret) {
203 dev_err(&pdev->dev, "failed to read sensor limits");
204 return ret;
205 }
206
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);
212
213 dev_info(&pdev->dev, "MEN 14F021P00 BMC hwmon device enabled");
214
215 return 0;
216 }
217
218 static struct platform_driver menf21bmc_hwmon = {
219 .probe = menf21bmc_hwmon_probe,
220 .driver = {
221 .name = DRV_NAME,
222 },
223 };
224
225 module_platform_driver(menf21bmc_hwmon);
226
227 MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
228 MODULE_DESCRIPTION("MEN 14F021P00 BMC hwmon");
229 MODULE_LICENSE("GPL v2");
230 MODULE_ALIAS("platform:menf21bmc_hwmon");
231