Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hwmon / menf21bmc_hwmon.c
blobadb50115c32bf7e442b773292d767ef3c80b9195
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * MEN 14F021P00 Board Management Controller (BMC) hwmon driver.
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.
9 * Copyright (C) 2014 MEN Mikro Elektronik Nuernberg GmbH
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>
22 #define DRV_NAME "menf21bmc_hwmon"
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
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)
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];
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"
52 static struct menf21bmc_hwmon *menf21bmc_hwmon_update(struct device *dev)
54 int i;
55 int val;
56 struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
57 struct menf21bmc_hwmon *data_ret = drv_data;
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;
68 drv_data->in_val[i] = val;
70 drv_data->last_update = jiffies;
71 drv_data->valid = true;
73 abort:
74 return data_ret;
77 static int menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon *drv_data)
79 int i, val;
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;
87 drv_data->in_min[i] = val;
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;
94 drv_data->in_max[i] = val;
96 return 0;
99 static ssize_t
100 label_show(struct device *dev, struct device_attribute *devattr, char *buf)
102 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
104 return sprintf(buf, "%s\n", input_names[attr->index]);
107 static ssize_t
108 in_show(struct device *dev, struct device_attribute *devattr, char *buf)
110 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
111 struct menf21bmc_hwmon *drv_data = menf21bmc_hwmon_update(dev);
113 if (IS_ERR(drv_data))
114 return PTR_ERR(drv_data);
116 return sprintf(buf, "%d\n", drv_data->in_val[attr->index]);
119 static ssize_t
120 min_show(struct device *dev, struct device_attribute *devattr, char *buf)
122 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
123 struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
125 return sprintf(buf, "%d\n", drv_data->in_min[attr->index]);
128 static ssize_t
129 max_show(struct device *dev, struct device_attribute *devattr, char *buf)
131 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
132 struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
134 return sprintf(buf, "%d\n", drv_data->in_max[attr->index]);
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);
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,
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,
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,
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,
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
186 ATTRIBUTE_GROUPS(menf21bmc_hwmon);
188 static int menf21bmc_hwmon_probe(struct platform_device *pdev)
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;
195 drv_data = devm_kzalloc(&pdev->dev, sizeof(struct menf21bmc_hwmon),
196 GFP_KERNEL);
197 if (!drv_data)
198 return -ENOMEM;
200 drv_data->i2c_client = i2c_client;
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;
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);
214 dev_info(&pdev->dev, "MEN 14F021P00 BMC hwmon device enabled");
216 return 0;
219 static struct platform_driver menf21bmc_hwmon = {
220 .probe = menf21bmc_hwmon_probe,
221 .driver = {
222 .name = DRV_NAME,
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");