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 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 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 104 label_show(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 112 in_show(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 124 min_show(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 133 max_show(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 static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0); 142 static SENSOR_DEVICE_ATTR_RO(in0_min, min, 0); 143 static SENSOR_DEVICE_ATTR_RO(in0_max, max, 0); 144 static SENSOR_DEVICE_ATTR_RO(in0_label, label, 0); 145 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1); 146 static SENSOR_DEVICE_ATTR_RO(in1_min, min, 1); 147 static SENSOR_DEVICE_ATTR_RO(in1_max, max, 1); 148 static SENSOR_DEVICE_ATTR_RO(in1_label, label, 1); 149 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2); 150 static SENSOR_DEVICE_ATTR_RO(in2_min, min, 2); 151 static SENSOR_DEVICE_ATTR_RO(in2_max, max, 2); 152 static SENSOR_DEVICE_ATTR_RO(in2_label, label, 2); 153 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3); 154 static SENSOR_DEVICE_ATTR_RO(in3_min, min, 3); 155 static SENSOR_DEVICE_ATTR_RO(in3_max, max, 3); 156 static SENSOR_DEVICE_ATTR_RO(in3_label, label, 3); 157 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4); 158 static SENSOR_DEVICE_ATTR_RO(in4_min, min, 4); 159 static SENSOR_DEVICE_ATTR_RO(in4_max, max, 4); 160 static SENSOR_DEVICE_ATTR_RO(in4_label, label, 4); 161 162 static struct attribute *menf21bmc_hwmon_attrs[] = { 163 &sensor_dev_attr_in0_input.dev_attr.attr, 164 &sensor_dev_attr_in0_min.dev_attr.attr, 165 &sensor_dev_attr_in0_max.dev_attr.attr, 166 &sensor_dev_attr_in0_label.dev_attr.attr, 167 168 &sensor_dev_attr_in1_input.dev_attr.attr, 169 &sensor_dev_attr_in1_min.dev_attr.attr, 170 &sensor_dev_attr_in1_max.dev_attr.attr, 171 &sensor_dev_attr_in1_label.dev_attr.attr, 172 173 &sensor_dev_attr_in2_input.dev_attr.attr, 174 &sensor_dev_attr_in2_min.dev_attr.attr, 175 &sensor_dev_attr_in2_max.dev_attr.attr, 176 &sensor_dev_attr_in2_label.dev_attr.attr, 177 178 &sensor_dev_attr_in3_input.dev_attr.attr, 179 &sensor_dev_attr_in3_min.dev_attr.attr, 180 &sensor_dev_attr_in3_max.dev_attr.attr, 181 &sensor_dev_attr_in3_label.dev_attr.attr, 182 183 &sensor_dev_attr_in4_input.dev_attr.attr, 184 &sensor_dev_attr_in4_min.dev_attr.attr, 185 &sensor_dev_attr_in4_max.dev_attr.attr, 186 &sensor_dev_attr_in4_label.dev_attr.attr, 187 NULL 188 }; 189 190 ATTRIBUTE_GROUPS(menf21bmc_hwmon); 191 192 static int menf21bmc_hwmon_probe(struct platform_device *pdev) 193 { 194 int ret; 195 struct menf21bmc_hwmon *drv_data; 196 struct i2c_client *i2c_client = to_i2c_client(pdev->dev.parent); 197 struct device *hwmon_dev; 198 199 drv_data = devm_kzalloc(&pdev->dev, sizeof(struct menf21bmc_hwmon), 200 GFP_KERNEL); 201 if (!drv_data) 202 return -ENOMEM; 203 204 drv_data->i2c_client = i2c_client; 205 206 ret = menf21bmc_hwmon_get_volt_limits(drv_data); 207 if (ret) { 208 dev_err(&pdev->dev, "failed to read sensor limits"); 209 return ret; 210 } 211 212 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, 213 "menf21bmc", drv_data, 214 menf21bmc_hwmon_groups); 215 if (IS_ERR(hwmon_dev)) 216 return PTR_ERR(hwmon_dev); 217 218 dev_info(&pdev->dev, "MEN 14F021P00 BMC hwmon device enabled"); 219 220 return 0; 221 } 222 223 static struct platform_driver menf21bmc_hwmon = { 224 .probe = menf21bmc_hwmon_probe, 225 .driver = { 226 .name = DRV_NAME, 227 }, 228 }; 229 230 module_platform_driver(menf21bmc_hwmon); 231 232 MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>"); 233 MODULE_DESCRIPTION("MEN 14F021P00 BMC hwmon"); 234 MODULE_LICENSE("GPL v2"); 235 MODULE_ALIAS("platform:menf21bmc_hwmon"); 236