xref: /linux/drivers/hwmon/menf21bmc_hwmon.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  MEN 14F021P00 Board Management Controller (BMC) hwmon driver.
4  *
5  *  This is the core hwmon driver of the MEN 14F021P00 BMC.
6  *  The BMC monitors the board voltages which can be access with this
7  *  driver through sysfs.
8  *
9  *  Copyright (C) 2014 MEN Mikro Elektronik Nuernberg GmbH
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/hwmon.h>
16 #include <linux/hwmon-sysfs.h>
17 #include <linux/jiffies.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/err.h>
21 
22 #define DRV_NAME  "menf21bmc_hwmon"
23 
24 #define BMC_VOLT_COUNT	5
25 #define MENF21BMC_V33	0
26 #define MENF21BMC_V5	1
27 #define MENF21BMC_V12	2
28 #define MENF21BMC_V5_SB	3
29 #define MENF21BMC_VBAT	4
30 
31 #define IDX_TO_VOLT_MIN_CMD(idx) (0x40 + idx)
32 #define IDX_TO_VOLT_MAX_CMD(idx) (0x50 + idx)
33 #define IDX_TO_VOLT_INP_CMD(idx) (0x60 + idx)
34 
35 struct menf21bmc_hwmon {
36 	bool valid;
37 	struct i2c_client *i2c_client;
38 	unsigned long last_update;
39 	int in_val[BMC_VOLT_COUNT];
40 	int in_min[BMC_VOLT_COUNT];
41 	int in_max[BMC_VOLT_COUNT];
42 };
43 
44 static const char *const input_names[] = {
45 	[MENF21BMC_V33]		= "MON_3_3V",
46 	[MENF21BMC_V5]		= "MON_5V",
47 	[MENF21BMC_V12]		= "MON_12V",
48 	[MENF21BMC_V5_SB]	= "5V_STANDBY",
49 	[MENF21BMC_VBAT]	= "VBAT"
50 };
51 
menf21bmc_hwmon_update(struct device * dev)52 static struct menf21bmc_hwmon *menf21bmc_hwmon_update(struct device *dev)
53 {
54 	int i;
55 	int val;
56 	struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
57 	struct menf21bmc_hwmon *data_ret = drv_data;
58 
59 	if (time_after(jiffies, drv_data->last_update + HZ)
60 	    || !drv_data->valid) {
61 		for (i = 0; i < BMC_VOLT_COUNT; i++) {
62 			val = i2c_smbus_read_word_data(drv_data->i2c_client,
63 						       IDX_TO_VOLT_INP_CMD(i));
64 			if (val < 0) {
65 				data_ret = ERR_PTR(val);
66 				goto abort;
67 			}
68 			drv_data->in_val[i] = val;
69 		}
70 		drv_data->last_update = jiffies;
71 		drv_data->valid = true;
72 	}
73 abort:
74 	return data_ret;
75 }
76 
menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon * drv_data)77 static int menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon *drv_data)
78 {
79 	int i, val;
80 
81 	for (i = 0; i < BMC_VOLT_COUNT; i++) {
82 		val = i2c_smbus_read_word_data(drv_data->i2c_client,
83 					       IDX_TO_VOLT_MIN_CMD(i));
84 		if (val < 0)
85 			return val;
86 
87 		drv_data->in_min[i] = val;
88 
89 		val = i2c_smbus_read_word_data(drv_data->i2c_client,
90 					       IDX_TO_VOLT_MAX_CMD(i));
91 		if (val < 0)
92 			return val;
93 
94 		drv_data->in_max[i] = val;
95 	}
96 	return 0;
97 }
98 
99 static ssize_t
label_show(struct device * dev,struct device_attribute * devattr,char * buf)100 label_show(struct device *dev, struct device_attribute *devattr, char *buf)
101 {
102 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
103 
104 	return sprintf(buf, "%s\n", input_names[attr->index]);
105 }
106 
107 static ssize_t
in_show(struct device * dev,struct device_attribute * devattr,char * buf)108 in_show(struct device *dev, struct device_attribute *devattr, char *buf)
109 {
110 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
111 	struct menf21bmc_hwmon *drv_data = menf21bmc_hwmon_update(dev);
112 
113 	if (IS_ERR(drv_data))
114 		return PTR_ERR(drv_data);
115 
116 	return sprintf(buf, "%d\n", drv_data->in_val[attr->index]);
117 }
118 
119 static ssize_t
min_show(struct device * dev,struct device_attribute * devattr,char * buf)120 min_show(struct device *dev, struct device_attribute *devattr, char *buf)
121 {
122 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
123 	struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
124 
125 	return sprintf(buf, "%d\n", drv_data->in_min[attr->index]);
126 }
127 
128 static ssize_t
max_show(struct device * dev,struct device_attribute * devattr,char * buf)129 max_show(struct device *dev, struct device_attribute *devattr, char *buf)
130 {
131 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
132 	struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
133 
134 	return sprintf(buf, "%d\n", drv_data->in_max[attr->index]);
135 }
136 
137 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
138 static SENSOR_DEVICE_ATTR_RO(in0_min, min, 0);
139 static SENSOR_DEVICE_ATTR_RO(in0_max, max, 0);
140 static SENSOR_DEVICE_ATTR_RO(in0_label, label, 0);
141 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
142 static SENSOR_DEVICE_ATTR_RO(in1_min, min, 1);
143 static SENSOR_DEVICE_ATTR_RO(in1_max, max, 1);
144 static SENSOR_DEVICE_ATTR_RO(in1_label, label, 1);
145 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
146 static SENSOR_DEVICE_ATTR_RO(in2_min, min, 2);
147 static SENSOR_DEVICE_ATTR_RO(in2_max, max, 2);
148 static SENSOR_DEVICE_ATTR_RO(in2_label, label, 2);
149 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
150 static SENSOR_DEVICE_ATTR_RO(in3_min, min, 3);
151 static SENSOR_DEVICE_ATTR_RO(in3_max, max, 3);
152 static SENSOR_DEVICE_ATTR_RO(in3_label, label, 3);
153 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
154 static SENSOR_DEVICE_ATTR_RO(in4_min, min, 4);
155 static SENSOR_DEVICE_ATTR_RO(in4_max, max, 4);
156 static SENSOR_DEVICE_ATTR_RO(in4_label, label, 4);
157 
158 static struct attribute *menf21bmc_hwmon_attrs[] = {
159 	&sensor_dev_attr_in0_input.dev_attr.attr,
160 	&sensor_dev_attr_in0_min.dev_attr.attr,
161 	&sensor_dev_attr_in0_max.dev_attr.attr,
162 	&sensor_dev_attr_in0_label.dev_attr.attr,
163 
164 	&sensor_dev_attr_in1_input.dev_attr.attr,
165 	&sensor_dev_attr_in1_min.dev_attr.attr,
166 	&sensor_dev_attr_in1_max.dev_attr.attr,
167 	&sensor_dev_attr_in1_label.dev_attr.attr,
168 
169 	&sensor_dev_attr_in2_input.dev_attr.attr,
170 	&sensor_dev_attr_in2_min.dev_attr.attr,
171 	&sensor_dev_attr_in2_max.dev_attr.attr,
172 	&sensor_dev_attr_in2_label.dev_attr.attr,
173 
174 	&sensor_dev_attr_in3_input.dev_attr.attr,
175 	&sensor_dev_attr_in3_min.dev_attr.attr,
176 	&sensor_dev_attr_in3_max.dev_attr.attr,
177 	&sensor_dev_attr_in3_label.dev_attr.attr,
178 
179 	&sensor_dev_attr_in4_input.dev_attr.attr,
180 	&sensor_dev_attr_in4_min.dev_attr.attr,
181 	&sensor_dev_attr_in4_max.dev_attr.attr,
182 	&sensor_dev_attr_in4_label.dev_attr.attr,
183 	NULL
184 };
185 
186 ATTRIBUTE_GROUPS(menf21bmc_hwmon);
187 
menf21bmc_hwmon_probe(struct platform_device * pdev)188 static int menf21bmc_hwmon_probe(struct platform_device *pdev)
189 {
190 	int ret;
191 	struct menf21bmc_hwmon *drv_data;
192 	struct i2c_client *i2c_client = to_i2c_client(pdev->dev.parent);
193 	struct device *hwmon_dev;
194 
195 	drv_data = devm_kzalloc(&pdev->dev, sizeof(struct menf21bmc_hwmon),
196 				GFP_KERNEL);
197 	if (!drv_data)
198 		return -ENOMEM;
199 
200 	drv_data->i2c_client = i2c_client;
201 
202 	ret = menf21bmc_hwmon_get_volt_limits(drv_data);
203 	if (ret) {
204 		dev_err(&pdev->dev, "failed to read sensor limits");
205 		return ret;
206 	}
207 
208 	hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
209 						   "menf21bmc", drv_data,
210 						   menf21bmc_hwmon_groups);
211 	if (IS_ERR(hwmon_dev))
212 		return PTR_ERR(hwmon_dev);
213 
214 	dev_info(&pdev->dev, "MEN 14F021P00 BMC hwmon device enabled");
215 
216 	return 0;
217 }
218 
219 static struct platform_driver menf21bmc_hwmon = {
220 	.probe		= menf21bmc_hwmon_probe,
221 	.driver		= {
222 		.name		= DRV_NAME,
223 	},
224 };
225 
226 module_platform_driver(menf21bmc_hwmon);
227 
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");
232