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