Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 1 | /* |
| 2 | * IBM PowerNV platform sensors for temperature/fan/voltage/power |
| 3 | * Copyright (C) 2014 IBM |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. |
| 17 | */ |
| 18 | |
| 19 | #define DRVNAME "ibmpowernv" |
| 20 | #define pr_fmt(fmt) DRVNAME ": " fmt |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/hwmon.h> |
| 26 | #include <linux/hwmon-sysfs.h> |
| 27 | #include <linux/of.h> |
| 28 | #include <linux/slab.h> |
| 29 | |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <asm/opal.h> |
| 32 | #include <linux/err.h> |
Cédric Le Goater | 3df2f59 | 2015-04-08 19:19:50 +0200 | [diff] [blame] | 33 | #include <asm/cputhreads.h> |
Guenter Roeck | 8416915 | 2015-04-08 21:02:47 -0700 | [diff] [blame] | 34 | #include <asm/smp.h> |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 35 | |
| 36 | #define MAX_ATTR_LEN 32 |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 37 | #define MAX_LABEL_LEN 64 |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 38 | |
| 39 | /* Sensor suffix name from DT */ |
| 40 | #define DT_FAULT_ATTR_SUFFIX "faulted" |
| 41 | #define DT_DATA_ATTR_SUFFIX "data" |
| 42 | #define DT_THRESHOLD_ATTR_SUFFIX "thrs" |
| 43 | |
| 44 | /* |
| 45 | * Enumerates all the types of sensors in the POWERNV platform and does index |
| 46 | * into 'struct sensor_group' |
| 47 | */ |
| 48 | enum sensors { |
| 49 | FAN, |
Cédric Le Goater | 9612461 | 2015-03-19 18:44:41 +0100 | [diff] [blame] | 50 | TEMP, |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 51 | POWER_SUPPLY, |
| 52 | POWER_INPUT, |
Shilpasri G Bhat | 3a2b3d3 | 2017-06-20 10:38:13 +0530 | [diff] [blame] | 53 | CURRENT, |
Shilpasri G Bhat | 43d2974 | 2018-05-07 15:55:38 +0530 | [diff] [blame] | 54 | ENERGY, |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 55 | MAX_SENSOR_TYPE, |
| 56 | }; |
| 57 | |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 58 | #define INVALID_INDEX (-1U) |
| 59 | |
Cédric Le Goater | 3ab5216 | 2017-06-20 10:38:12 +0530 | [diff] [blame] | 60 | /* |
| 61 | * 'compatible' string properties for sensor types as defined in old |
| 62 | * PowerNV firmware (skiboot). These are ordered as 'enum sensors'. |
| 63 | */ |
| 64 | static const char * const legacy_compatibles[] = { |
| 65 | "ibm,opal-sensor-cooling-fan", |
| 66 | "ibm,opal-sensor-amb-temp", |
| 67 | "ibm,opal-sensor-power-supply", |
| 68 | "ibm,opal-sensor-power" |
| 69 | }; |
| 70 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 71 | static struct sensor_group { |
Cédric Le Goater | 3ab5216 | 2017-06-20 10:38:12 +0530 | [diff] [blame] | 72 | const char *name; /* matches property 'sensor-type' */ |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 73 | struct attribute_group group; |
| 74 | u32 attr_count; |
Cédric Le Goater | fcaf57b | 2015-03-19 18:44:45 +0100 | [diff] [blame] | 75 | u32 hwmon_index; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 76 | } sensor_groups[] = { |
Cédric Le Goater | 3ab5216 | 2017-06-20 10:38:12 +0530 | [diff] [blame] | 77 | { "fan" }, |
| 78 | { "temp" }, |
| 79 | { "in" }, |
Shilpasri G Bhat | 3a2b3d3 | 2017-06-20 10:38:13 +0530 | [diff] [blame] | 80 | { "power" }, |
| 81 | { "curr" }, |
Shilpasri G Bhat | 43d2974 | 2018-05-07 15:55:38 +0530 | [diff] [blame] | 82 | { "energy" }, |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct sensor_data { |
| 86 | u32 id; /* An opaque id of the firmware for each sensor */ |
Cédric Le Goater | fcaf57b | 2015-03-19 18:44:45 +0100 | [diff] [blame] | 87 | u32 hwmon_index; |
| 88 | u32 opal_index; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 89 | enum sensors type; |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 90 | char label[MAX_LABEL_LEN]; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 91 | char name[MAX_ATTR_LEN]; |
| 92 | struct device_attribute dev_attr; |
| 93 | }; |
| 94 | |
| 95 | struct platform_data { |
| 96 | const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1]; |
| 97 | u32 sensors_count; /* Total count of sensors from each group */ |
| 98 | }; |
| 99 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 100 | static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr, |
| 101 | char *buf) |
| 102 | { |
| 103 | struct sensor_data *sdata = container_of(devattr, struct sensor_data, |
| 104 | dev_attr); |
| 105 | ssize_t ret; |
Shilpasri G Bhat | 3c8c049 | 2018-05-07 15:55:37 +0530 | [diff] [blame] | 106 | u64 x; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 107 | |
Shilpasri G Bhat | 3c8c049 | 2018-05-07 15:55:37 +0530 | [diff] [blame] | 108 | ret = opal_get_sensor_data_u64(sdata->id, &x); |
| 109 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 110 | if (ret) |
| 111 | return ret; |
| 112 | |
| 113 | /* Convert temperature to milli-degrees */ |
Cédric Le Goater | 9612461 | 2015-03-19 18:44:41 +0100 | [diff] [blame] | 114 | if (sdata->type == TEMP) |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 115 | x *= 1000; |
| 116 | /* Convert power to micro-watts */ |
| 117 | else if (sdata->type == POWER_INPUT) |
| 118 | x *= 1000000; |
| 119 | |
Shilpasri G Bhat | 3c8c049 | 2018-05-07 15:55:37 +0530 | [diff] [blame] | 120 | return sprintf(buf, "%llu\n", x); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 121 | } |
| 122 | |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 123 | static ssize_t show_label(struct device *dev, struct device_attribute *devattr, |
| 124 | char *buf) |
| 125 | { |
| 126 | struct sensor_data *sdata = container_of(devattr, struct sensor_data, |
| 127 | dev_attr); |
| 128 | |
| 129 | return sprintf(buf, "%s\n", sdata->label); |
| 130 | } |
| 131 | |
Cédric Le Goater | 3df2f59 | 2015-04-08 19:19:50 +0200 | [diff] [blame] | 132 | static int __init get_logical_cpu(int hwcpu) |
| 133 | { |
| 134 | int cpu; |
| 135 | |
| 136 | for_each_possible_cpu(cpu) |
| 137 | if (get_hard_smp_processor_id(cpu) == hwcpu) |
| 138 | return cpu; |
| 139 | |
| 140 | return -ENOENT; |
| 141 | } |
| 142 | |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 143 | static void __init make_sensor_label(struct device_node *np, |
| 144 | struct sensor_data *sdata, |
| 145 | const char *label) |
| 146 | { |
Cédric Le Goater | 3df2f59 | 2015-04-08 19:19:50 +0200 | [diff] [blame] | 147 | u32 id; |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 148 | size_t n; |
| 149 | |
| 150 | n = snprintf(sdata->label, sizeof(sdata->label), "%s", label); |
Cédric Le Goater | 3df2f59 | 2015-04-08 19:19:50 +0200 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Core temp pretty print |
| 154 | */ |
| 155 | if (!of_property_read_u32(np, "ibm,pir", &id)) { |
| 156 | int cpuid = get_logical_cpu(id); |
| 157 | |
| 158 | if (cpuid >= 0) |
| 159 | /* |
| 160 | * The digital thermal sensors are associated |
Michael Neuling | acf3296 | 2016-09-13 15:47:41 +1000 | [diff] [blame] | 161 | * with a core. |
Cédric Le Goater | 3df2f59 | 2015-04-08 19:19:50 +0200 | [diff] [blame] | 162 | */ |
| 163 | n += snprintf(sdata->label + n, |
Michael Neuling | acf3296 | 2016-09-13 15:47:41 +1000 | [diff] [blame] | 164 | sizeof(sdata->label) - n, " %d", |
| 165 | cpuid); |
Cédric Le Goater | 3df2f59 | 2015-04-08 19:19:50 +0200 | [diff] [blame] | 166 | else |
| 167 | n += snprintf(sdata->label + n, |
| 168 | sizeof(sdata->label) - n, " phy%d", id); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Membuffer pretty print |
| 173 | */ |
| 174 | if (!of_property_read_u32(np, "ibm,chip-id", &id)) |
| 175 | n += snprintf(sdata->label + n, sizeof(sdata->label) - n, |
| 176 | " %d", id & 0xffff); |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int get_sensor_index_attr(const char *name, u32 *index, char *attr) |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 180 | { |
| 181 | char *hash_pos = strchr(name, '#'); |
| 182 | char buf[8] = { 0 }; |
| 183 | char *dash_pos; |
| 184 | u32 copy_len; |
| 185 | int err; |
| 186 | |
| 187 | if (!hash_pos) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | dash_pos = strchr(hash_pos, '-'); |
| 191 | if (!dash_pos) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | copy_len = dash_pos - hash_pos - 1; |
| 195 | if (copy_len >= sizeof(buf)) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | strncpy(buf, hash_pos + 1, copy_len); |
| 199 | |
| 200 | err = kstrtou32(buf, 10, index); |
| 201 | if (err) |
| 202 | return err; |
| 203 | |
| 204 | strncpy(attr, dash_pos + 1, MAX_ATTR_LEN); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Cédric Le Goater | ccc9ac6 | 2015-03-19 18:44:43 +0100 | [diff] [blame] | 209 | static const char *convert_opal_attr_name(enum sensors type, |
| 210 | const char *opal_attr) |
| 211 | { |
| 212 | const char *attr_name = NULL; |
| 213 | |
| 214 | if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) { |
| 215 | attr_name = "fault"; |
| 216 | } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) { |
| 217 | attr_name = "input"; |
| 218 | } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) { |
| 219 | if (type == TEMP) |
| 220 | attr_name = "max"; |
| 221 | else if (type == FAN) |
| 222 | attr_name = "min"; |
| 223 | } |
| 224 | |
| 225 | return attr_name; |
| 226 | } |
| 227 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 228 | /* |
| 229 | * This function translates the DT node name into the 'hwmon' attribute name. |
| 230 | * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc. |
| 231 | * which need to be mapped as fan2_input, temp1_max respectively before |
| 232 | * populating them inside hwmon device class. |
| 233 | */ |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 234 | static const char *parse_opal_node_name(const char *node_name, |
| 235 | enum sensors type, u32 *index) |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 236 | { |
| 237 | char attr_suffix[MAX_ATTR_LEN]; |
Cédric Le Goater | ccc9ac6 | 2015-03-19 18:44:43 +0100 | [diff] [blame] | 238 | const char *attr_name; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 239 | int err; |
| 240 | |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 241 | err = get_sensor_index_attr(node_name, index, attr_suffix); |
| 242 | if (err) |
| 243 | return ERR_PTR(err); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 244 | |
Cédric Le Goater | ccc9ac6 | 2015-03-19 18:44:43 +0100 | [diff] [blame] | 245 | attr_name = convert_opal_attr_name(type, attr_suffix); |
| 246 | if (!attr_name) |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 247 | return ERR_PTR(-ENOENT); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 248 | |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 249 | return attr_name; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 250 | } |
| 251 | |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 252 | static int get_sensor_type(struct device_node *np) |
| 253 | { |
| 254 | enum sensors type; |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 255 | const char *str; |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 256 | |
Cédric Le Goater | 3ab5216 | 2017-06-20 10:38:12 +0530 | [diff] [blame] | 257 | for (type = 0; type < ARRAY_SIZE(legacy_compatibles); type++) { |
| 258 | if (of_device_is_compatible(np, legacy_compatibles[type])) |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 259 | return type; |
| 260 | } |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | * Let's check if we have a newer device tree |
| 264 | */ |
| 265 | if (!of_device_is_compatible(np, "ibm,opal-sensor")) |
| 266 | return MAX_SENSOR_TYPE; |
| 267 | |
| 268 | if (of_property_read_string(np, "sensor-type", &str)) |
| 269 | return MAX_SENSOR_TYPE; |
| 270 | |
| 271 | for (type = 0; type < MAX_SENSOR_TYPE; type++) |
| 272 | if (!strcmp(str, sensor_groups[type].name)) |
| 273 | return type; |
| 274 | |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 275 | return MAX_SENSOR_TYPE; |
| 276 | } |
| 277 | |
Cédric Le Goater | fcaf57b | 2015-03-19 18:44:45 +0100 | [diff] [blame] | 278 | static u32 get_sensor_hwmon_index(struct sensor_data *sdata, |
| 279 | struct sensor_data *sdata_table, int count) |
| 280 | { |
| 281 | int i; |
| 282 | |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 283 | /* |
| 284 | * We don't use the OPAL index on newer device trees |
| 285 | */ |
| 286 | if (sdata->opal_index != INVALID_INDEX) { |
| 287 | for (i = 0; i < count; i++) |
| 288 | if (sdata_table[i].opal_index == sdata->opal_index && |
| 289 | sdata_table[i].type == sdata->type) |
| 290 | return sdata_table[i].hwmon_index; |
| 291 | } |
Cédric Le Goater | fcaf57b | 2015-03-19 18:44:45 +0100 | [diff] [blame] | 292 | return ++sensor_groups[sdata->type].hwmon_index; |
| 293 | } |
| 294 | |
Neelesh Gupta | 8de303ba | 2014-11-05 16:45:14 +0530 | [diff] [blame] | 295 | static int populate_attr_groups(struct platform_device *pdev) |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 296 | { |
| 297 | struct platform_data *pdata = platform_get_drvdata(pdev); |
| 298 | const struct attribute_group **pgroups = pdata->attr_groups; |
| 299 | struct device_node *opal, *np; |
| 300 | enum sensors type; |
| 301 | |
| 302 | opal = of_find_node_by_path("/ibm,opal/sensors"); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 303 | for_each_child_of_node(opal, np) { |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 304 | const char *label; |
| 305 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 306 | if (np->name == NULL) |
| 307 | continue; |
| 308 | |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 309 | type = get_sensor_type(np); |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 310 | if (type == MAX_SENSOR_TYPE) |
| 311 | continue; |
| 312 | |
| 313 | sensor_groups[type].attr_count++; |
| 314 | |
| 315 | /* |
Shilpasri G Bhat | 996cf5a | 2017-05-29 10:16:01 +0530 | [diff] [blame] | 316 | * add attributes for labels, min and max |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 317 | */ |
| 318 | if (!of_property_read_string(np, "label", &label)) |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 319 | sensor_groups[type].attr_count++; |
Shilpasri G Bhat | 996cf5a | 2017-05-29 10:16:01 +0530 | [diff] [blame] | 320 | if (of_find_property(np, "sensor-data-min", NULL)) |
| 321 | sensor_groups[type].attr_count++; |
| 322 | if (of_find_property(np, "sensor-data-max", NULL)) |
| 323 | sensor_groups[type].attr_count++; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | of_node_put(opal); |
| 327 | |
| 328 | for (type = 0; type < MAX_SENSOR_TYPE; type++) { |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 329 | sensor_groups[type].group.attrs = devm_kcalloc(&pdev->dev, |
| 330 | sensor_groups[type].attr_count + 1, |
| 331 | sizeof(struct attribute *), |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 332 | GFP_KERNEL); |
| 333 | if (!sensor_groups[type].group.attrs) |
| 334 | return -ENOMEM; |
| 335 | |
| 336 | pgroups[type] = &sensor_groups[type].group; |
| 337 | pdata->sensors_count += sensor_groups[type].attr_count; |
| 338 | sensor_groups[type].attr_count = 0; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
Cédric Le Goater | 9e4f74b | 2015-04-08 19:19:47 +0200 | [diff] [blame] | 344 | static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name, |
| 345 | ssize_t (*show)(struct device *dev, |
| 346 | struct device_attribute *attr, |
| 347 | char *buf)) |
| 348 | { |
| 349 | snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s", |
| 350 | sensor_groups[sdata->type].name, sdata->hwmon_index, |
| 351 | attr_name); |
| 352 | |
| 353 | sysfs_attr_init(&sdata->dev_attr.attr); |
| 354 | sdata->dev_attr.attr.name = sdata->name; |
| 355 | sdata->dev_attr.attr.mode = S_IRUGO; |
| 356 | sdata->dev_attr.show = show; |
| 357 | } |
| 358 | |
Shilpasri G Bhat | 996cf5a | 2017-05-29 10:16:01 +0530 | [diff] [blame] | 359 | static void populate_sensor(struct sensor_data *sdata, int od, int hd, int sid, |
| 360 | const char *attr_name, enum sensors type, |
| 361 | const struct attribute_group *pgroup, |
| 362 | ssize_t (*show)(struct device *dev, |
| 363 | struct device_attribute *attr, |
| 364 | char *buf)) |
| 365 | { |
| 366 | sdata->id = sid; |
| 367 | sdata->type = type; |
| 368 | sdata->opal_index = od; |
| 369 | sdata->hwmon_index = hd; |
| 370 | create_hwmon_attr(sdata, attr_name, show); |
| 371 | pgroup->attrs[sensor_groups[type].attr_count++] = &sdata->dev_attr.attr; |
| 372 | } |
| 373 | |
| 374 | static char *get_max_attr(enum sensors type) |
| 375 | { |
| 376 | switch (type) { |
| 377 | case POWER_INPUT: |
| 378 | return "input_highest"; |
| 379 | default: |
| 380 | return "highest"; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static char *get_min_attr(enum sensors type) |
| 385 | { |
| 386 | switch (type) { |
| 387 | case POWER_INPUT: |
| 388 | return "input_lowest"; |
| 389 | default: |
| 390 | return "lowest"; |
| 391 | } |
| 392 | } |
| 393 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 394 | /* |
| 395 | * Iterate through the device tree for each child of 'sensors' node, create |
| 396 | * a sysfs attribute file, the file is named by translating the DT node name |
| 397 | * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max |
| 398 | * etc.. |
| 399 | */ |
Neelesh Gupta | 8de303ba | 2014-11-05 16:45:14 +0530 | [diff] [blame] | 400 | static int create_device_attrs(struct platform_device *pdev) |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 401 | { |
| 402 | struct platform_data *pdata = platform_get_drvdata(pdev); |
| 403 | const struct attribute_group **pgroups = pdata->attr_groups; |
| 404 | struct device_node *opal, *np; |
| 405 | struct sensor_data *sdata; |
Axel Lin | 18d03f3 | 2014-08-01 12:38:47 +0800 | [diff] [blame] | 406 | u32 sensor_id; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 407 | enum sensors type; |
| 408 | u32 count = 0; |
| 409 | int err = 0; |
| 410 | |
| 411 | opal = of_find_node_by_path("/ibm,opal/sensors"); |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 412 | sdata = devm_kcalloc(&pdev->dev, |
| 413 | pdata->sensors_count, sizeof(*sdata), |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 414 | GFP_KERNEL); |
| 415 | if (!sdata) { |
| 416 | err = -ENOMEM; |
| 417 | goto exit_put_node; |
| 418 | } |
| 419 | |
| 420 | for_each_child_of_node(opal, np) { |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 421 | const char *attr_name; |
| 422 | u32 opal_index; |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 423 | const char *label; |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 424 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 425 | if (np->name == NULL) |
| 426 | continue; |
| 427 | |
Cédric Le Goater | c4ad472 | 2015-03-19 18:44:42 +0100 | [diff] [blame] | 428 | type = get_sensor_type(np); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 429 | if (type == MAX_SENSOR_TYPE) |
| 430 | continue; |
| 431 | |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 432 | /* |
| 433 | * Newer device trees use a "sensor-data" property |
| 434 | * name for input. |
| 435 | */ |
| 436 | if (of_property_read_u32(np, "sensor-id", &sensor_id) && |
| 437 | of_property_read_u32(np, "sensor-data", &sensor_id)) { |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 438 | dev_info(&pdev->dev, |
| 439 | "'sensor-id' missing in the node '%s'\n", |
| 440 | np->name); |
| 441 | continue; |
| 442 | } |
| 443 | |
Axel Lin | 18d03f3 | 2014-08-01 12:38:47 +0800 | [diff] [blame] | 444 | sdata[count].id = sensor_id; |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 445 | sdata[count].type = type; |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 446 | |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 447 | /* |
| 448 | * If we can not parse the node name, it means we are |
| 449 | * running on a newer device tree. We can just forget |
| 450 | * about the OPAL index and use a defaut value for the |
| 451 | * hwmon attribute name |
| 452 | */ |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 453 | attr_name = parse_opal_node_name(np->name, type, &opal_index); |
| 454 | if (IS_ERR(attr_name)) { |
Cédric Le Goater | 1468163 | 2015-04-08 19:19:48 +0200 | [diff] [blame] | 455 | attr_name = "input"; |
| 456 | opal_index = INVALID_INDEX; |
Cédric Le Goater | f9f54f1 | 2015-03-19 18:44:44 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Cédric Le Goater | fcaf57b | 2015-03-19 18:44:45 +0100 | [diff] [blame] | 459 | sdata[count].opal_index = opal_index; |
| 460 | sdata[count].hwmon_index = |
| 461 | get_sensor_hwmon_index(&sdata[count], sdata, count); |
| 462 | |
Cédric Le Goater | 9e4f74b | 2015-04-08 19:19:47 +0200 | [diff] [blame] | 463 | create_hwmon_attr(&sdata[count], attr_name, show_sensor); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 464 | |
| 465 | pgroups[type]->attrs[sensor_groups[type].attr_count++] = |
| 466 | &sdata[count++].dev_attr.attr; |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 467 | |
| 468 | if (!of_property_read_string(np, "label", &label)) { |
| 469 | /* |
| 470 | * For the label attribute, we can reuse the |
| 471 | * "properties" of the previous "input" |
| 472 | * attribute. They are related to the same |
| 473 | * sensor. |
| 474 | */ |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 475 | |
| 476 | make_sensor_label(np, &sdata[count], label); |
Shilpasri G Bhat | 996cf5a | 2017-05-29 10:16:01 +0530 | [diff] [blame] | 477 | populate_sensor(&sdata[count], opal_index, |
| 478 | sdata[count - 1].hwmon_index, |
| 479 | sensor_id, "label", type, pgroups[type], |
| 480 | show_label); |
| 481 | count++; |
| 482 | } |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 483 | |
Shilpasri G Bhat | 996cf5a | 2017-05-29 10:16:01 +0530 | [diff] [blame] | 484 | if (!of_property_read_u32(np, "sensor-data-max", &sensor_id)) { |
| 485 | attr_name = get_max_attr(type); |
| 486 | populate_sensor(&sdata[count], opal_index, |
| 487 | sdata[count - 1].hwmon_index, |
| 488 | sensor_id, attr_name, type, |
| 489 | pgroups[type], show_sensor); |
| 490 | count++; |
| 491 | } |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 492 | |
Shilpasri G Bhat | 996cf5a | 2017-05-29 10:16:01 +0530 | [diff] [blame] | 493 | if (!of_property_read_u32(np, "sensor-data-min", &sensor_id)) { |
| 494 | attr_name = get_min_attr(type); |
| 495 | populate_sensor(&sdata[count], opal_index, |
| 496 | sdata[count - 1].hwmon_index, |
| 497 | sensor_id, attr_name, type, |
| 498 | pgroups[type], show_sensor); |
| 499 | count++; |
Cédric Le Goater | 2bcd378 | 2015-04-08 19:19:49 +0200 | [diff] [blame] | 500 | } |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | exit_put_node: |
| 504 | of_node_put(opal); |
| 505 | return err; |
| 506 | } |
| 507 | |
Neelesh Gupta | 8de303ba | 2014-11-05 16:45:14 +0530 | [diff] [blame] | 508 | static int ibmpowernv_probe(struct platform_device *pdev) |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 509 | { |
| 510 | struct platform_data *pdata; |
| 511 | struct device *hwmon_dev; |
| 512 | int err; |
| 513 | |
| 514 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 515 | if (!pdata) |
| 516 | return -ENOMEM; |
| 517 | |
| 518 | platform_set_drvdata(pdev, pdata); |
| 519 | pdata->sensors_count = 0; |
| 520 | err = populate_attr_groups(pdev); |
| 521 | if (err) |
| 522 | return err; |
| 523 | |
| 524 | /* Create sysfs attribute data for each sensor found in the DT */ |
| 525 | err = create_device_attrs(pdev); |
| 526 | if (err) |
| 527 | return err; |
| 528 | |
| 529 | /* Finally, register with hwmon */ |
| 530 | hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME, |
| 531 | pdata, |
| 532 | pdata->attr_groups); |
| 533 | |
| 534 | return PTR_ERR_OR_ZERO(hwmon_dev); |
| 535 | } |
| 536 | |
Neelesh Gupta | 8de303ba | 2014-11-05 16:45:14 +0530 | [diff] [blame] | 537 | static const struct platform_device_id opal_sensor_driver_ids[] = { |
| 538 | { |
| 539 | .name = "opal-sensor", |
| 540 | }, |
| 541 | { } |
| 542 | }; |
| 543 | MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids); |
| 544 | |
Cédric Le Goater | 0b056b2 | 2015-09-23 14:44:48 +0200 | [diff] [blame] | 545 | static const struct of_device_id opal_sensor_match[] = { |
| 546 | { .compatible = "ibm,opal-sensor" }, |
| 547 | { }, |
| 548 | }; |
| 549 | MODULE_DEVICE_TABLE(of, opal_sensor_match); |
| 550 | |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 551 | static struct platform_driver ibmpowernv_driver = { |
Neelesh Gupta | 8de303ba | 2014-11-05 16:45:14 +0530 | [diff] [blame] | 552 | .probe = ibmpowernv_probe, |
| 553 | .id_table = opal_sensor_driver_ids, |
| 554 | .driver = { |
Neelesh Gupta | 8de303ba | 2014-11-05 16:45:14 +0530 | [diff] [blame] | 555 | .name = DRVNAME, |
Cédric Le Goater | 0b056b2 | 2015-09-23 14:44:48 +0200 | [diff] [blame] | 556 | .of_match_table = opal_sensor_match, |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 557 | }, |
| 558 | }; |
| 559 | |
Axel Lin | 3bdec67 | 2014-11-12 16:36:47 +0800 | [diff] [blame] | 560 | module_platform_driver(ibmpowernv_driver); |
Neelesh Gupta | 24c1aa8 | 2014-07-08 14:38:38 +0530 | [diff] [blame] | 561 | |
| 562 | MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>"); |
| 563 | MODULE_DESCRIPTION("IBM POWERNV platform sensors"); |
| 564 | MODULE_LICENSE("GPL"); |