Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Hardware monitoring driver for PMBus devices |
| 3 | * |
| 4 | * Copyright (c) 2010, 2011 Ericsson AB. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/i2c/pmbus.h> |
| 31 | #include "pmbus.h" |
| 32 | |
| 33 | /* |
| 34 | * Constants needed to determine number of sensors, booleans, and labels. |
| 35 | */ |
| 36 | #define PMBUS_MAX_INPUT_SENSORS 11 /* 6*volt, 3*curr, 2*power */ |
| 37 | #define PMBUS_VOUT_SENSORS_PER_PAGE 5 /* input, min, max, lcrit, |
| 38 | crit */ |
| 39 | #define PMBUS_IOUT_SENSORS_PER_PAGE 4 /* input, min, max, crit */ |
| 40 | #define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */ |
| 41 | #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */ |
| 42 | #define PMBUS_MAX_SENSORS_PER_TEMP 5 /* input, min, max, lcrit, |
| 43 | crit */ |
| 44 | |
| 45 | #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm, |
| 46 | lcrit_alarm, crit_alarm; |
| 47 | c: alarm, crit_alarm; |
| 48 | p: crit_alarm */ |
| 49 | #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm, |
| 50 | lcrit_alarm, crit_alarm */ |
| 51 | #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm, |
| 52 | crit_alarm */ |
| 53 | #define PMBUS_POUT_BOOLEANS_PER_PAGE 2 /* alarm, crit_alarm */ |
| 54 | #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */ |
| 55 | #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm, |
| 56 | lcrit_alarm, crit_alarm */ |
| 57 | |
| 58 | #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */ |
| 59 | |
| 60 | /* |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 61 | * status, status_vout, status_iout, status_fans, status_fan34, and status_temp |
| 62 | * are paged. status_input is unpaged. |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 63 | */ |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 64 | #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Index into status register array, per status register group |
| 68 | */ |
| 69 | #define PB_STATUS_BASE 0 |
| 70 | #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES) |
| 71 | #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES) |
| 72 | #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES) |
| 73 | #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES) |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 74 | #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 75 | #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1) |
| 76 | |
| 77 | struct pmbus_sensor { |
| 78 | char name[I2C_NAME_SIZE]; /* sysfs sensor name */ |
| 79 | struct sensor_device_attribute attribute; |
| 80 | u8 page; /* page number */ |
| 81 | u8 reg; /* register */ |
| 82 | enum pmbus_sensor_classes class; /* sensor class */ |
| 83 | bool update; /* runtime sensor update needed */ |
| 84 | int data; /* Sensor data. |
| 85 | Negative if there was a read error */ |
| 86 | }; |
| 87 | |
| 88 | struct pmbus_boolean { |
| 89 | char name[I2C_NAME_SIZE]; /* sysfs boolean name */ |
| 90 | struct sensor_device_attribute attribute; |
| 91 | }; |
| 92 | |
| 93 | struct pmbus_label { |
| 94 | char name[I2C_NAME_SIZE]; /* sysfs label name */ |
| 95 | struct sensor_device_attribute attribute; |
| 96 | char label[I2C_NAME_SIZE]; /* label */ |
| 97 | }; |
| 98 | |
| 99 | struct pmbus_data { |
| 100 | struct device *hwmon_dev; |
| 101 | |
| 102 | u32 flags; /* from platform data */ |
| 103 | |
| 104 | int exponent; /* linear mode: exponent for output voltages */ |
| 105 | |
| 106 | const struct pmbus_driver_info *info; |
| 107 | |
| 108 | int max_attributes; |
| 109 | int num_attributes; |
| 110 | struct attribute **attributes; |
| 111 | struct attribute_group group; |
| 112 | |
| 113 | /* |
| 114 | * Sensors cover both sensor and limit registers. |
| 115 | */ |
| 116 | int max_sensors; |
| 117 | int num_sensors; |
| 118 | struct pmbus_sensor *sensors; |
| 119 | /* |
| 120 | * Booleans are used for alarms. |
| 121 | * Values are determined from status registers. |
| 122 | */ |
| 123 | int max_booleans; |
| 124 | int num_booleans; |
| 125 | struct pmbus_boolean *booleans; |
| 126 | /* |
| 127 | * Labels are used to map generic names (e.g., "in1") |
| 128 | * to PMBus specific names (e.g., "vin" or "vout1"). |
| 129 | */ |
| 130 | int max_labels; |
| 131 | int num_labels; |
| 132 | struct pmbus_label *labels; |
| 133 | |
| 134 | struct mutex update_lock; |
| 135 | bool valid; |
| 136 | unsigned long last_updated; /* in jiffies */ |
| 137 | |
| 138 | /* |
| 139 | * A single status register covers multiple attributes, |
| 140 | * so we keep them all together. |
| 141 | */ |
| 142 | u8 status_bits; |
| 143 | u8 status[PB_NUM_STATUS_REG]; |
| 144 | |
| 145 | u8 currpage; |
| 146 | }; |
| 147 | |
| 148 | int pmbus_set_page(struct i2c_client *client, u8 page) |
| 149 | { |
| 150 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 151 | int rv = 0; |
| 152 | int newpage; |
| 153 | |
| 154 | if (page != data->currpage) { |
| 155 | rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); |
| 156 | newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE); |
| 157 | if (newpage != page) |
| 158 | rv = -EINVAL; |
| 159 | else |
| 160 | data->currpage = page; |
| 161 | } |
| 162 | return rv; |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(pmbus_set_page); |
| 165 | |
| 166 | static int pmbus_write_byte(struct i2c_client *client, u8 page, u8 value) |
| 167 | { |
| 168 | int rv; |
| 169 | |
| 170 | rv = pmbus_set_page(client, page); |
| 171 | if (rv < 0) |
| 172 | return rv; |
| 173 | |
| 174 | return i2c_smbus_write_byte(client, value); |
| 175 | } |
| 176 | |
| 177 | static int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, |
| 178 | u16 word) |
| 179 | { |
| 180 | int rv; |
| 181 | |
| 182 | rv = pmbus_set_page(client, page); |
| 183 | if (rv < 0) |
| 184 | return rv; |
| 185 | |
| 186 | return i2c_smbus_write_word_data(client, reg, word); |
| 187 | } |
| 188 | |
| 189 | int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg) |
| 190 | { |
| 191 | int rv; |
| 192 | |
| 193 | rv = pmbus_set_page(client, page); |
| 194 | if (rv < 0) |
| 195 | return rv; |
| 196 | |
| 197 | return i2c_smbus_read_word_data(client, reg); |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(pmbus_read_word_data); |
| 200 | |
| 201 | static int pmbus_read_byte_data(struct i2c_client *client, u8 page, u8 reg) |
| 202 | { |
| 203 | int rv; |
| 204 | |
| 205 | rv = pmbus_set_page(client, page); |
| 206 | if (rv < 0) |
| 207 | return rv; |
| 208 | |
| 209 | return i2c_smbus_read_byte_data(client, reg); |
| 210 | } |
| 211 | |
| 212 | static void pmbus_clear_fault_page(struct i2c_client *client, int page) |
| 213 | { |
| 214 | pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); |
| 215 | } |
| 216 | |
| 217 | void pmbus_clear_faults(struct i2c_client *client) |
| 218 | { |
| 219 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < data->info->pages; i++) |
| 223 | pmbus_clear_fault_page(client, i); |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(pmbus_clear_faults); |
| 226 | |
| 227 | static int pmbus_check_status_cml(struct i2c_client *client, int page) |
| 228 | { |
| 229 | int status, status2; |
| 230 | |
| 231 | status = pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE); |
| 232 | if (status < 0 || (status & PB_STATUS_CML)) { |
| 233 | status2 = pmbus_read_byte_data(client, page, PMBUS_STATUS_CML); |
| 234 | if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) |
| 241 | { |
| 242 | int rv; |
| 243 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 244 | |
| 245 | rv = pmbus_read_byte_data(client, page, reg); |
| 246 | if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) |
| 247 | rv = pmbus_check_status_cml(client, page); |
| 248 | pmbus_clear_fault_page(client, page); |
| 249 | return rv >= 0; |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(pmbus_check_byte_register); |
| 252 | |
| 253 | bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) |
| 254 | { |
| 255 | int rv; |
| 256 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 257 | |
| 258 | rv = pmbus_read_word_data(client, page, reg); |
| 259 | if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) |
| 260 | rv = pmbus_check_status_cml(client, page); |
| 261 | pmbus_clear_fault_page(client, page); |
| 262 | return rv >= 0; |
| 263 | } |
| 264 | EXPORT_SYMBOL_GPL(pmbus_check_word_register); |
| 265 | |
| 266 | const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) |
| 267 | { |
| 268 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 269 | |
| 270 | return data->info; |
| 271 | } |
| 272 | EXPORT_SYMBOL_GPL(pmbus_get_driver_info); |
| 273 | |
| 274 | static int pmbus_get_status(struct i2c_client *client, int page, int reg) |
| 275 | { |
| 276 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 277 | const struct pmbus_driver_info *info = data->info; |
| 278 | int status; |
| 279 | |
| 280 | if (info->get_status) { |
| 281 | status = info->get_status(client, page, reg); |
| 282 | if (status != -ENODATA) |
| 283 | return status; |
| 284 | } |
| 285 | return pmbus_read_byte_data(client, page, reg); |
| 286 | } |
| 287 | |
| 288 | static struct pmbus_data *pmbus_update_device(struct device *dev) |
| 289 | { |
| 290 | struct i2c_client *client = to_i2c_client(dev); |
| 291 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 292 | const struct pmbus_driver_info *info = data->info; |
| 293 | |
| 294 | mutex_lock(&data->update_lock); |
| 295 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 296 | int i; |
| 297 | |
| 298 | for (i = 0; i < info->pages; i++) |
| 299 | data->status[PB_STATUS_BASE + i] |
| 300 | = pmbus_read_byte_data(client, i, |
| 301 | PMBUS_STATUS_BYTE); |
| 302 | for (i = 0; i < info->pages; i++) { |
| 303 | if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT)) |
| 304 | continue; |
| 305 | data->status[PB_STATUS_VOUT_BASE + i] |
| 306 | = pmbus_get_status(client, i, PMBUS_STATUS_VOUT); |
| 307 | } |
| 308 | for (i = 0; i < info->pages; i++) { |
| 309 | if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT)) |
| 310 | continue; |
| 311 | data->status[PB_STATUS_IOUT_BASE + i] |
| 312 | = pmbus_get_status(client, i, PMBUS_STATUS_IOUT); |
| 313 | } |
| 314 | for (i = 0; i < info->pages; i++) { |
| 315 | if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP)) |
| 316 | continue; |
| 317 | data->status[PB_STATUS_TEMP_BASE + i] |
| 318 | = pmbus_get_status(client, i, |
| 319 | PMBUS_STATUS_TEMPERATURE); |
| 320 | } |
| 321 | for (i = 0; i < info->pages; i++) { |
| 322 | if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12)) |
| 323 | continue; |
| 324 | data->status[PB_STATUS_FAN_BASE + i] |
| 325 | = pmbus_get_status(client, i, PMBUS_STATUS_FAN_12); |
| 326 | } |
| 327 | |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 328 | for (i = 0; i < info->pages; i++) { |
| 329 | if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34)) |
| 330 | continue; |
| 331 | data->status[PB_STATUS_FAN34_BASE + i] |
| 332 | = pmbus_get_status(client, i, PMBUS_STATUS_FAN_34); |
| 333 | } |
| 334 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 335 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) |
| 336 | data->status[PB_STATUS_INPUT_BASE] |
| 337 | = pmbus_get_status(client, 0, PMBUS_STATUS_INPUT); |
| 338 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 339 | for (i = 0; i < data->num_sensors; i++) { |
| 340 | struct pmbus_sensor *sensor = &data->sensors[i]; |
| 341 | |
| 342 | if (!data->valid || sensor->update) |
| 343 | sensor->data |
| 344 | = pmbus_read_word_data(client, sensor->page, |
| 345 | sensor->reg); |
| 346 | } |
| 347 | pmbus_clear_faults(client); |
| 348 | data->last_updated = jiffies; |
| 349 | data->valid = 1; |
| 350 | } |
| 351 | mutex_unlock(&data->update_lock); |
| 352 | return data; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Convert linear sensor values to milli- or micro-units |
| 357 | * depending on sensor type. |
| 358 | */ |
| 359 | static int pmbus_reg2data_linear(struct pmbus_data *data, |
| 360 | struct pmbus_sensor *sensor) |
| 361 | { |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 362 | s16 exponent; |
| 363 | s32 mantissa; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 364 | long val; |
| 365 | |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 366 | if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 367 | exponent = data->exponent; |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 368 | mantissa = (u16) sensor->data; |
| 369 | } else { /* LINEAR11 */ |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 370 | exponent = (sensor->data >> 11) & 0x001f; |
| 371 | mantissa = sensor->data & 0x07ff; |
| 372 | |
| 373 | if (exponent > 0x0f) |
| 374 | exponent |= 0xffe0; /* sign extend exponent */ |
| 375 | if (mantissa > 0x03ff) |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 376 | mantissa |= 0xfffff800; /* sign extend mantissa */ |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | val = mantissa; |
| 380 | |
| 381 | /* scale result to milli-units for all sensors except fans */ |
| 382 | if (sensor->class != PSC_FAN) |
| 383 | val = val * 1000L; |
| 384 | |
| 385 | /* scale result to micro-units for power sensors */ |
| 386 | if (sensor->class == PSC_POWER) |
| 387 | val = val * 1000L; |
| 388 | |
| 389 | if (exponent >= 0) |
| 390 | val <<= exponent; |
| 391 | else |
| 392 | val >>= -exponent; |
| 393 | |
| 394 | return (int)val; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Convert direct sensor values to milli- or micro-units |
| 399 | * depending on sensor type. |
| 400 | */ |
| 401 | static int pmbus_reg2data_direct(struct pmbus_data *data, |
| 402 | struct pmbus_sensor *sensor) |
| 403 | { |
| 404 | long val = (s16) sensor->data; |
| 405 | long m, b, R; |
| 406 | |
| 407 | m = data->info->m[sensor->class]; |
| 408 | b = data->info->b[sensor->class]; |
| 409 | R = data->info->R[sensor->class]; |
| 410 | |
| 411 | if (m == 0) |
| 412 | return 0; |
| 413 | |
| 414 | /* X = 1/m * (Y * 10^-R - b) */ |
| 415 | R = -R; |
| 416 | /* scale result to milli-units for everything but fans */ |
| 417 | if (sensor->class != PSC_FAN) { |
| 418 | R += 3; |
| 419 | b *= 1000; |
| 420 | } |
| 421 | |
| 422 | /* scale result to micro-units for power sensors */ |
| 423 | if (sensor->class == PSC_POWER) { |
| 424 | R += 3; |
| 425 | b *= 1000; |
| 426 | } |
| 427 | |
| 428 | while (R > 0) { |
| 429 | val *= 10; |
| 430 | R--; |
| 431 | } |
| 432 | while (R < 0) { |
| 433 | val = DIV_ROUND_CLOSEST(val, 10); |
| 434 | R++; |
| 435 | } |
| 436 | |
| 437 | return (int)((val - b) / m); |
| 438 | } |
| 439 | |
| 440 | static int pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) |
| 441 | { |
| 442 | int val; |
| 443 | |
| 444 | if (data->info->direct[sensor->class]) |
| 445 | val = pmbus_reg2data_direct(data, sensor); |
| 446 | else |
| 447 | val = pmbus_reg2data_linear(data, sensor); |
| 448 | |
| 449 | return val; |
| 450 | } |
| 451 | |
| 452 | #define MAX_MANTISSA (1023 * 1000) |
| 453 | #define MIN_MANTISSA (511 * 1000) |
| 454 | |
| 455 | static u16 pmbus_data2reg_linear(struct pmbus_data *data, |
| 456 | enum pmbus_sensor_classes class, long val) |
| 457 | { |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 458 | s16 exponent = 0, mantissa; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 459 | bool negative = false; |
| 460 | |
| 461 | /* simple case */ |
| 462 | if (val == 0) |
| 463 | return 0; |
| 464 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 465 | if (class == PSC_VOLTAGE_OUT) { |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 466 | /* LINEAR16 does not support negative voltages */ |
| 467 | if (val < 0) |
| 468 | return 0; |
| 469 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 470 | /* |
| 471 | * For a static exponents, we don't have a choice |
| 472 | * but to adjust the value to it. |
| 473 | */ |
| 474 | if (data->exponent < 0) |
| 475 | val <<= -data->exponent; |
| 476 | else |
| 477 | val >>= data->exponent; |
| 478 | val = DIV_ROUND_CLOSEST(val, 1000); |
Guenter Roeck | 9f6ad1c | 2011-03-07 18:34:50 -0800 | [diff] [blame] | 479 | return val & 0xffff; |
| 480 | } |
| 481 | |
| 482 | if (val < 0) { |
| 483 | negative = true; |
| 484 | val = -val; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Power is in uW. Convert to mW before converting. */ |
| 488 | if (class == PSC_POWER) |
| 489 | val = DIV_ROUND_CLOSEST(val, 1000L); |
| 490 | |
| 491 | /* |
| 492 | * For simplicity, convert fan data to milli-units |
| 493 | * before calculating the exponent. |
| 494 | */ |
| 495 | if (class == PSC_FAN) |
| 496 | val = val * 1000; |
| 497 | |
| 498 | /* Reduce large mantissa until it fits into 10 bit */ |
| 499 | while (val >= MAX_MANTISSA && exponent < 15) { |
| 500 | exponent++; |
| 501 | val >>= 1; |
| 502 | } |
| 503 | /* Increase small mantissa to improve precision */ |
| 504 | while (val < MIN_MANTISSA && exponent > -15) { |
| 505 | exponent--; |
| 506 | val <<= 1; |
| 507 | } |
| 508 | |
| 509 | /* Convert mantissa from milli-units to units */ |
| 510 | mantissa = DIV_ROUND_CLOSEST(val, 1000); |
| 511 | |
| 512 | /* Ensure that resulting number is within range */ |
| 513 | if (mantissa > 0x3ff) |
| 514 | mantissa = 0x3ff; |
| 515 | |
| 516 | /* restore sign */ |
| 517 | if (negative) |
| 518 | mantissa = -mantissa; |
| 519 | |
| 520 | /* Convert to 5 bit exponent, 11 bit mantissa */ |
| 521 | return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); |
| 522 | } |
| 523 | |
| 524 | static u16 pmbus_data2reg_direct(struct pmbus_data *data, |
| 525 | enum pmbus_sensor_classes class, long val) |
| 526 | { |
| 527 | long m, b, R; |
| 528 | |
| 529 | m = data->info->m[class]; |
| 530 | b = data->info->b[class]; |
| 531 | R = data->info->R[class]; |
| 532 | |
| 533 | /* Power is in uW. Adjust R and b. */ |
| 534 | if (class == PSC_POWER) { |
| 535 | R -= 3; |
| 536 | b *= 1000; |
| 537 | } |
| 538 | |
| 539 | /* Calculate Y = (m * X + b) * 10^R */ |
| 540 | if (class != PSC_FAN) { |
| 541 | R -= 3; /* Adjust R and b for data in milli-units */ |
| 542 | b *= 1000; |
| 543 | } |
| 544 | val = val * m + b; |
| 545 | |
| 546 | while (R > 0) { |
| 547 | val *= 10; |
| 548 | R--; |
| 549 | } |
| 550 | while (R < 0) { |
| 551 | val = DIV_ROUND_CLOSEST(val, 10); |
| 552 | R++; |
| 553 | } |
| 554 | |
| 555 | return val; |
| 556 | } |
| 557 | |
| 558 | static u16 pmbus_data2reg(struct pmbus_data *data, |
| 559 | enum pmbus_sensor_classes class, long val) |
| 560 | { |
| 561 | u16 regval; |
| 562 | |
| 563 | if (data->info->direct[class]) |
| 564 | regval = pmbus_data2reg_direct(data, class, val); |
| 565 | else |
| 566 | regval = pmbus_data2reg_linear(data, class, val); |
| 567 | |
| 568 | return regval; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Return boolean calculated from converted data. |
| 573 | * <index> defines a status register index and mask, and optionally |
| 574 | * two sensor indexes. |
| 575 | * The upper half-word references the two sensors, |
| 576 | * two sensor indices. |
| 577 | * The upper half-word references the two optional sensors, |
| 578 | * the lower half word references status register and mask. |
| 579 | * The function returns true if (status[reg] & mask) is true and, |
| 580 | * if specified, if v1 >= v2. |
| 581 | * To determine if an object exceeds upper limits, specify <v, limit>. |
| 582 | * To determine if an object exceeds lower limits, specify <limit, v>. |
| 583 | * |
| 584 | * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of |
| 585 | * index are set. s1 and s2 (the sensor index values) are zero in this case. |
| 586 | * The function returns true if (status[reg] & mask) is true. |
| 587 | * |
| 588 | * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against |
| 589 | * a specified limit has to be performed to determine the boolean result. |
| 590 | * In this case, the function returns true if v1 >= v2 (where v1 and v2 are |
| 591 | * sensor values referenced by sensor indices s1 and s2). |
| 592 | * |
| 593 | * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. |
| 594 | * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. |
| 595 | * |
| 596 | * If a negative value is stored in any of the referenced registers, this value |
| 597 | * reflects an error code which will be returned. |
| 598 | */ |
| 599 | static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val) |
| 600 | { |
| 601 | u8 s1 = (index >> 24) & 0xff; |
| 602 | u8 s2 = (index >> 16) & 0xff; |
| 603 | u8 reg = (index >> 8) & 0xff; |
| 604 | u8 mask = index & 0xff; |
| 605 | int status; |
| 606 | u8 regval; |
| 607 | |
| 608 | status = data->status[reg]; |
| 609 | if (status < 0) |
| 610 | return status; |
| 611 | |
| 612 | regval = status & mask; |
| 613 | if (!s1 && !s2) |
| 614 | *val = !!regval; |
| 615 | else { |
| 616 | int v1, v2; |
| 617 | struct pmbus_sensor *sensor1, *sensor2; |
| 618 | |
| 619 | sensor1 = &data->sensors[s1]; |
| 620 | if (sensor1->data < 0) |
| 621 | return sensor1->data; |
| 622 | sensor2 = &data->sensors[s2]; |
| 623 | if (sensor2->data < 0) |
| 624 | return sensor2->data; |
| 625 | |
| 626 | v1 = pmbus_reg2data(data, sensor1); |
| 627 | v2 = pmbus_reg2data(data, sensor2); |
| 628 | *val = !!(regval && v1 >= v2); |
| 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | static ssize_t pmbus_show_boolean(struct device *dev, |
| 634 | struct device_attribute *da, char *buf) |
| 635 | { |
| 636 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 637 | struct pmbus_data *data = pmbus_update_device(dev); |
| 638 | int val; |
| 639 | int err; |
| 640 | |
| 641 | err = pmbus_get_boolean(data, attr->index, &val); |
| 642 | if (err) |
| 643 | return err; |
| 644 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
| 645 | } |
| 646 | |
| 647 | static ssize_t pmbus_show_sensor(struct device *dev, |
| 648 | struct device_attribute *da, char *buf) |
| 649 | { |
| 650 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 651 | struct pmbus_data *data = pmbus_update_device(dev); |
| 652 | struct pmbus_sensor *sensor; |
| 653 | |
| 654 | sensor = &data->sensors[attr->index]; |
| 655 | if (sensor->data < 0) |
| 656 | return sensor->data; |
| 657 | |
| 658 | return snprintf(buf, PAGE_SIZE, "%d\n", pmbus_reg2data(data, sensor)); |
| 659 | } |
| 660 | |
| 661 | static ssize_t pmbus_set_sensor(struct device *dev, |
| 662 | struct device_attribute *devattr, |
| 663 | const char *buf, size_t count) |
| 664 | { |
| 665 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 666 | struct i2c_client *client = to_i2c_client(dev); |
| 667 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 668 | struct pmbus_sensor *sensor = &data->sensors[attr->index]; |
| 669 | ssize_t rv = count; |
| 670 | long val = 0; |
| 671 | int ret; |
| 672 | u16 regval; |
| 673 | |
| 674 | if (strict_strtol(buf, 10, &val) < 0) |
| 675 | return -EINVAL; |
| 676 | |
| 677 | mutex_lock(&data->update_lock); |
| 678 | regval = pmbus_data2reg(data, sensor->class, val); |
| 679 | ret = pmbus_write_word_data(client, sensor->page, sensor->reg, regval); |
| 680 | if (ret < 0) |
| 681 | rv = ret; |
| 682 | else |
| 683 | data->sensors[attr->index].data = regval; |
| 684 | mutex_unlock(&data->update_lock); |
| 685 | return rv; |
| 686 | } |
| 687 | |
| 688 | static ssize_t pmbus_show_label(struct device *dev, |
| 689 | struct device_attribute *da, char *buf) |
| 690 | { |
| 691 | struct i2c_client *client = to_i2c_client(dev); |
| 692 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 693 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 694 | |
| 695 | return snprintf(buf, PAGE_SIZE, "%s\n", |
| 696 | data->labels[attr->index].label); |
| 697 | } |
| 698 | |
| 699 | #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \ |
| 700 | do { \ |
| 701 | struct sensor_device_attribute *a \ |
| 702 | = &data->_type##s[data->num_##_type##s].attribute; \ |
| 703 | BUG_ON(data->num_attributes >= data->max_attributes); \ |
| 704 | a->dev_attr.attr.name = _name; \ |
| 705 | a->dev_attr.attr.mode = _mode; \ |
| 706 | a->dev_attr.show = _show; \ |
| 707 | a->dev_attr.store = _set; \ |
| 708 | a->index = _idx; \ |
| 709 | data->attributes[data->num_attributes] = &a->dev_attr.attr; \ |
| 710 | data->num_attributes++; \ |
| 711 | } while (0) |
| 712 | |
| 713 | #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \ |
| 714 | PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \ |
| 715 | pmbus_show_##_type, NULL) |
| 716 | |
| 717 | #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \ |
| 718 | PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \ |
| 719 | pmbus_show_##_type, pmbus_set_##_type) |
| 720 | |
| 721 | static void pmbus_add_boolean(struct pmbus_data *data, |
| 722 | const char *name, const char *type, int seq, |
| 723 | int idx) |
| 724 | { |
| 725 | struct pmbus_boolean *boolean; |
| 726 | |
| 727 | BUG_ON(data->num_booleans >= data->max_booleans); |
| 728 | |
| 729 | boolean = &data->booleans[data->num_booleans]; |
| 730 | |
| 731 | snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", |
| 732 | name, seq, type); |
| 733 | PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx); |
| 734 | data->num_booleans++; |
| 735 | } |
| 736 | |
| 737 | static void pmbus_add_boolean_reg(struct pmbus_data *data, |
| 738 | const char *name, const char *type, |
| 739 | int seq, int reg, int bit) |
| 740 | { |
| 741 | pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit); |
| 742 | } |
| 743 | |
| 744 | static void pmbus_add_boolean_cmp(struct pmbus_data *data, |
| 745 | const char *name, const char *type, |
| 746 | int seq, int i1, int i2, int reg, int mask) |
| 747 | { |
| 748 | pmbus_add_boolean(data, name, type, seq, |
| 749 | (i1 << 24) | (i2 << 16) | (reg << 8) | mask); |
| 750 | } |
| 751 | |
| 752 | static void pmbus_add_sensor(struct pmbus_data *data, |
| 753 | const char *name, const char *type, int seq, |
| 754 | int page, int reg, enum pmbus_sensor_classes class, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 755 | bool update, bool readonly) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 756 | { |
| 757 | struct pmbus_sensor *sensor; |
| 758 | |
| 759 | BUG_ON(data->num_sensors >= data->max_sensors); |
| 760 | |
| 761 | sensor = &data->sensors[data->num_sensors]; |
| 762 | snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", |
| 763 | name, seq, type); |
| 764 | sensor->page = page; |
| 765 | sensor->reg = reg; |
| 766 | sensor->class = class; |
| 767 | sensor->update = update; |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 768 | if (readonly) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 769 | PMBUS_ADD_GET_ATTR(data, sensor->name, sensor, |
| 770 | data->num_sensors); |
| 771 | else |
| 772 | PMBUS_ADD_SET_ATTR(data, sensor->name, sensor, |
| 773 | data->num_sensors); |
| 774 | data->num_sensors++; |
| 775 | } |
| 776 | |
| 777 | static void pmbus_add_label(struct pmbus_data *data, |
| 778 | const char *name, int seq, |
| 779 | const char *lstring, int index) |
| 780 | { |
| 781 | struct pmbus_label *label; |
| 782 | |
| 783 | BUG_ON(data->num_labels >= data->max_labels); |
| 784 | |
| 785 | label = &data->labels[data->num_labels]; |
| 786 | snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); |
| 787 | if (!index) |
| 788 | strncpy(label->label, lstring, sizeof(label->label) - 1); |
| 789 | else |
| 790 | snprintf(label->label, sizeof(label->label), "%s%d", lstring, |
| 791 | index); |
| 792 | |
| 793 | PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels); |
| 794 | data->num_labels++; |
| 795 | } |
| 796 | |
| 797 | static const int pmbus_temp_registers[] = { |
| 798 | PMBUS_READ_TEMPERATURE_1, |
| 799 | PMBUS_READ_TEMPERATURE_2, |
| 800 | PMBUS_READ_TEMPERATURE_3 |
| 801 | }; |
| 802 | |
Guenter Roeck | b49547a | 2011-03-06 10:56:52 -0800 | [diff] [blame] | 803 | static const int pmbus_temp_flags[] = { |
| 804 | PMBUS_HAVE_TEMP, |
| 805 | PMBUS_HAVE_TEMP2, |
| 806 | PMBUS_HAVE_TEMP3 |
| 807 | }; |
| 808 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 809 | static const int pmbus_fan_registers[] = { |
| 810 | PMBUS_READ_FAN_SPEED_1, |
| 811 | PMBUS_READ_FAN_SPEED_2, |
| 812 | PMBUS_READ_FAN_SPEED_3, |
| 813 | PMBUS_READ_FAN_SPEED_4 |
| 814 | }; |
| 815 | |
| 816 | static const int pmbus_fan_config_registers[] = { |
| 817 | PMBUS_FAN_CONFIG_12, |
| 818 | PMBUS_FAN_CONFIG_12, |
| 819 | PMBUS_FAN_CONFIG_34, |
| 820 | PMBUS_FAN_CONFIG_34 |
| 821 | }; |
| 822 | |
| 823 | static const int pmbus_fan_status_registers[] = { |
| 824 | PMBUS_STATUS_FAN_12, |
| 825 | PMBUS_STATUS_FAN_12, |
| 826 | PMBUS_STATUS_FAN_34, |
| 827 | PMBUS_STATUS_FAN_34 |
| 828 | }; |
| 829 | |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 830 | static const u32 pmbus_fan_flags[] = { |
| 831 | PMBUS_HAVE_FAN12, |
| 832 | PMBUS_HAVE_FAN12, |
| 833 | PMBUS_HAVE_FAN34, |
| 834 | PMBUS_HAVE_FAN34 |
| 835 | }; |
| 836 | |
| 837 | static const u32 pmbus_fan_status_flags[] = { |
| 838 | PMBUS_HAVE_STATUS_FAN12, |
| 839 | PMBUS_HAVE_STATUS_FAN12, |
| 840 | PMBUS_HAVE_STATUS_FAN34, |
| 841 | PMBUS_HAVE_STATUS_FAN34 |
| 842 | }; |
| 843 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 844 | /* |
| 845 | * Determine maximum number of sensors, booleans, and labels. |
| 846 | * To keep things simple, only make a rough high estimate. |
| 847 | */ |
| 848 | static void pmbus_find_max_attr(struct i2c_client *client, |
| 849 | struct pmbus_data *data) |
| 850 | { |
| 851 | const struct pmbus_driver_info *info = data->info; |
| 852 | int page, max_sensors, max_booleans, max_labels; |
| 853 | |
| 854 | max_sensors = PMBUS_MAX_INPUT_SENSORS; |
| 855 | max_booleans = PMBUS_MAX_INPUT_BOOLEANS; |
| 856 | max_labels = PMBUS_MAX_INPUT_LABELS; |
| 857 | |
| 858 | for (page = 0; page < info->pages; page++) { |
| 859 | if (info->func[page] & PMBUS_HAVE_VOUT) { |
| 860 | max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE; |
| 861 | max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE; |
| 862 | max_labels++; |
| 863 | } |
| 864 | if (info->func[page] & PMBUS_HAVE_IOUT) { |
| 865 | max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE; |
| 866 | max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE; |
| 867 | max_labels++; |
| 868 | } |
| 869 | if (info->func[page] & PMBUS_HAVE_POUT) { |
| 870 | max_sensors += PMBUS_POUT_SENSORS_PER_PAGE; |
| 871 | max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE; |
| 872 | max_labels++; |
| 873 | } |
| 874 | if (info->func[page] & PMBUS_HAVE_FAN12) { |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 875 | max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN; |
| 876 | max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN; |
| 877 | } |
| 878 | if (info->func[page] & PMBUS_HAVE_FAN34) { |
| 879 | max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN; |
| 880 | max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 881 | } |
| 882 | if (info->func[page] & PMBUS_HAVE_TEMP) { |
Guenter Roeck | b49547a | 2011-03-06 10:56:52 -0800 | [diff] [blame] | 883 | max_sensors += PMBUS_MAX_SENSORS_PER_TEMP; |
| 884 | max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP; |
| 885 | } |
| 886 | if (info->func[page] & PMBUS_HAVE_TEMP2) { |
| 887 | max_sensors += PMBUS_MAX_SENSORS_PER_TEMP; |
| 888 | max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP; |
| 889 | } |
| 890 | if (info->func[page] & PMBUS_HAVE_TEMP3) { |
| 891 | max_sensors += PMBUS_MAX_SENSORS_PER_TEMP; |
| 892 | max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | data->max_sensors = max_sensors; |
| 896 | data->max_booleans = max_booleans; |
| 897 | data->max_labels = max_labels; |
| 898 | data->max_attributes = max_sensors + max_booleans + max_labels; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Search for attributes. Allocate sensors, booleans, and labels as needed. |
| 903 | */ |
| 904 | static void pmbus_find_attributes(struct i2c_client *client, |
| 905 | struct pmbus_data *data) |
| 906 | { |
| 907 | const struct pmbus_driver_info *info = data->info; |
| 908 | int page, i0, i1, in_index; |
| 909 | |
| 910 | /* |
| 911 | * Input voltage sensors |
| 912 | */ |
| 913 | in_index = 1; |
| 914 | if (info->func[0] & PMBUS_HAVE_VIN) { |
| 915 | bool have_alarm = false; |
| 916 | |
| 917 | i0 = data->num_sensors; |
| 918 | pmbus_add_label(data, "in", in_index, "vin", 0); |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 919 | pmbus_add_sensor(data, "in", "input", in_index, 0, |
| 920 | PMBUS_READ_VIN, PSC_VOLTAGE_IN, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 921 | if (pmbus_check_word_register(client, 0, |
| 922 | PMBUS_VIN_UV_WARN_LIMIT)) { |
| 923 | i1 = data->num_sensors; |
| 924 | pmbus_add_sensor(data, "in", "min", in_index, |
| 925 | 0, PMBUS_VIN_UV_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 926 | PSC_VOLTAGE_IN, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 927 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) { |
| 928 | pmbus_add_boolean_reg(data, "in", "min_alarm", |
| 929 | in_index, |
| 930 | PB_STATUS_INPUT_BASE, |
| 931 | PB_VOLTAGE_UV_WARNING); |
| 932 | have_alarm = true; |
| 933 | } |
| 934 | } |
| 935 | if (pmbus_check_word_register(client, 0, |
| 936 | PMBUS_VIN_UV_FAULT_LIMIT)) { |
| 937 | i1 = data->num_sensors; |
| 938 | pmbus_add_sensor(data, "in", "lcrit", in_index, |
| 939 | 0, PMBUS_VIN_UV_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 940 | PSC_VOLTAGE_IN, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 941 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) { |
| 942 | pmbus_add_boolean_reg(data, "in", "lcrit_alarm", |
| 943 | in_index, |
| 944 | PB_STATUS_INPUT_BASE, |
| 945 | PB_VOLTAGE_UV_FAULT); |
| 946 | have_alarm = true; |
| 947 | } |
| 948 | } |
| 949 | if (pmbus_check_word_register(client, 0, |
| 950 | PMBUS_VIN_OV_WARN_LIMIT)) { |
| 951 | i1 = data->num_sensors; |
| 952 | pmbus_add_sensor(data, "in", "max", in_index, |
| 953 | 0, PMBUS_VIN_OV_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 954 | PSC_VOLTAGE_IN, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 955 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) { |
| 956 | pmbus_add_boolean_reg(data, "in", "max_alarm", |
| 957 | in_index, |
| 958 | PB_STATUS_INPUT_BASE, |
| 959 | PB_VOLTAGE_OV_WARNING); |
| 960 | have_alarm = true; |
| 961 | } |
| 962 | } |
| 963 | if (pmbus_check_word_register(client, 0, |
| 964 | PMBUS_VIN_OV_FAULT_LIMIT)) { |
| 965 | i1 = data->num_sensors; |
| 966 | pmbus_add_sensor(data, "in", "crit", in_index, |
| 967 | 0, PMBUS_VIN_OV_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 968 | PSC_VOLTAGE_IN, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 969 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) { |
| 970 | pmbus_add_boolean_reg(data, "in", "crit_alarm", |
| 971 | in_index, |
| 972 | PB_STATUS_INPUT_BASE, |
| 973 | PB_VOLTAGE_OV_FAULT); |
| 974 | have_alarm = true; |
| 975 | } |
| 976 | } |
| 977 | /* |
| 978 | * Add generic alarm attribute only if there are no individual |
| 979 | * attributes. |
| 980 | */ |
| 981 | if (!have_alarm) |
| 982 | pmbus_add_boolean_reg(data, "in", "alarm", |
| 983 | in_index, |
| 984 | PB_STATUS_BASE, |
| 985 | PB_STATUS_VIN_UV); |
| 986 | in_index++; |
| 987 | } |
| 988 | if (info->func[0] & PMBUS_HAVE_VCAP) { |
| 989 | pmbus_add_label(data, "in", in_index, "vcap", 0); |
| 990 | pmbus_add_sensor(data, "in", "input", in_index, 0, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 991 | PMBUS_READ_VCAP, PSC_VOLTAGE_IN, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 992 | in_index++; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Output voltage sensors |
| 997 | */ |
| 998 | for (page = 0; page < info->pages; page++) { |
| 999 | bool have_alarm = false; |
| 1000 | |
| 1001 | if (!(info->func[page] & PMBUS_HAVE_VOUT)) |
| 1002 | continue; |
| 1003 | |
| 1004 | i0 = data->num_sensors; |
| 1005 | pmbus_add_label(data, "in", in_index, "vout", page + 1); |
| 1006 | pmbus_add_sensor(data, "in", "input", in_index, page, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1007 | PMBUS_READ_VOUT, PSC_VOLTAGE_OUT, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1008 | if (pmbus_check_word_register(client, page, |
| 1009 | PMBUS_VOUT_UV_WARN_LIMIT)) { |
| 1010 | i1 = data->num_sensors; |
| 1011 | pmbus_add_sensor(data, "in", "min", in_index, page, |
| 1012 | PMBUS_VOUT_UV_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1013 | PSC_VOLTAGE_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1014 | if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) { |
| 1015 | pmbus_add_boolean_reg(data, "in", "min_alarm", |
| 1016 | in_index, |
| 1017 | PB_STATUS_VOUT_BASE + |
| 1018 | page, |
| 1019 | PB_VOLTAGE_UV_WARNING); |
| 1020 | have_alarm = true; |
| 1021 | } |
| 1022 | } |
| 1023 | if (pmbus_check_word_register(client, page, |
| 1024 | PMBUS_VOUT_UV_FAULT_LIMIT)) { |
| 1025 | i1 = data->num_sensors; |
| 1026 | pmbus_add_sensor(data, "in", "lcrit", in_index, page, |
| 1027 | PMBUS_VOUT_UV_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1028 | PSC_VOLTAGE_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1029 | if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) { |
| 1030 | pmbus_add_boolean_reg(data, "in", "lcrit_alarm", |
| 1031 | in_index, |
| 1032 | PB_STATUS_VOUT_BASE + |
| 1033 | page, |
| 1034 | PB_VOLTAGE_UV_FAULT); |
| 1035 | have_alarm = true; |
| 1036 | } |
| 1037 | } |
| 1038 | if (pmbus_check_word_register(client, page, |
| 1039 | PMBUS_VOUT_OV_WARN_LIMIT)) { |
| 1040 | i1 = data->num_sensors; |
| 1041 | pmbus_add_sensor(data, "in", "max", in_index, page, |
| 1042 | PMBUS_VOUT_OV_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1043 | PSC_VOLTAGE_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1044 | if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) { |
| 1045 | pmbus_add_boolean_reg(data, "in", "max_alarm", |
| 1046 | in_index, |
| 1047 | PB_STATUS_VOUT_BASE + |
| 1048 | page, |
| 1049 | PB_VOLTAGE_OV_WARNING); |
| 1050 | have_alarm = true; |
| 1051 | } |
| 1052 | } |
| 1053 | if (pmbus_check_word_register(client, page, |
| 1054 | PMBUS_VOUT_OV_FAULT_LIMIT)) { |
| 1055 | i1 = data->num_sensors; |
| 1056 | pmbus_add_sensor(data, "in", "crit", in_index, page, |
| 1057 | PMBUS_VOUT_OV_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1058 | PSC_VOLTAGE_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1059 | if (info->func[page] & PMBUS_HAVE_STATUS_VOUT) { |
| 1060 | pmbus_add_boolean_reg(data, "in", "crit_alarm", |
| 1061 | in_index, |
| 1062 | PB_STATUS_VOUT_BASE + |
| 1063 | page, |
| 1064 | PB_VOLTAGE_OV_FAULT); |
| 1065 | have_alarm = true; |
| 1066 | } |
| 1067 | } |
| 1068 | /* |
| 1069 | * Add generic alarm attribute only if there are no individual |
| 1070 | * attributes. |
| 1071 | */ |
| 1072 | if (!have_alarm) |
| 1073 | pmbus_add_boolean_reg(data, "in", "alarm", |
| 1074 | in_index, |
| 1075 | PB_STATUS_BASE + page, |
| 1076 | PB_STATUS_VOUT_OV); |
| 1077 | in_index++; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Current sensors |
| 1082 | */ |
| 1083 | |
| 1084 | /* |
| 1085 | * Input current sensors |
| 1086 | */ |
| 1087 | in_index = 1; |
| 1088 | if (info->func[0] & PMBUS_HAVE_IIN) { |
| 1089 | i0 = data->num_sensors; |
| 1090 | pmbus_add_label(data, "curr", in_index, "iin", 0); |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1091 | pmbus_add_sensor(data, "curr", "input", in_index, 0, |
| 1092 | PMBUS_READ_IIN, PSC_CURRENT_IN, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1093 | if (pmbus_check_word_register(client, 0, |
| 1094 | PMBUS_IIN_OC_WARN_LIMIT)) { |
| 1095 | i1 = data->num_sensors; |
| 1096 | pmbus_add_sensor(data, "curr", "max", in_index, |
| 1097 | 0, PMBUS_IIN_OC_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1098 | PSC_CURRENT_IN, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1099 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) { |
| 1100 | pmbus_add_boolean_reg(data, "curr", "max_alarm", |
| 1101 | in_index, |
| 1102 | PB_STATUS_INPUT_BASE, |
| 1103 | PB_IIN_OC_WARNING); |
| 1104 | } |
| 1105 | } |
| 1106 | if (pmbus_check_word_register(client, 0, |
| 1107 | PMBUS_IIN_OC_FAULT_LIMIT)) { |
| 1108 | i1 = data->num_sensors; |
| 1109 | pmbus_add_sensor(data, "curr", "crit", in_index, |
| 1110 | 0, PMBUS_IIN_OC_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1111 | PSC_CURRENT_IN, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1112 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) |
| 1113 | pmbus_add_boolean_reg(data, "curr", |
| 1114 | "crit_alarm", |
| 1115 | in_index, |
| 1116 | PB_STATUS_INPUT_BASE, |
| 1117 | PB_IIN_OC_FAULT); |
| 1118 | } |
| 1119 | in_index++; |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Output current sensors |
| 1124 | */ |
| 1125 | for (page = 0; page < info->pages; page++) { |
| 1126 | bool have_alarm = false; |
| 1127 | |
| 1128 | if (!(info->func[page] & PMBUS_HAVE_IOUT)) |
| 1129 | continue; |
| 1130 | |
| 1131 | i0 = data->num_sensors; |
| 1132 | pmbus_add_label(data, "curr", in_index, "iout", page + 1); |
| 1133 | pmbus_add_sensor(data, "curr", "input", in_index, page, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1134 | PMBUS_READ_IOUT, PSC_CURRENT_OUT, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1135 | if (pmbus_check_word_register(client, page, |
| 1136 | PMBUS_IOUT_OC_WARN_LIMIT)) { |
| 1137 | i1 = data->num_sensors; |
| 1138 | pmbus_add_sensor(data, "curr", "max", in_index, page, |
| 1139 | PMBUS_IOUT_OC_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1140 | PSC_CURRENT_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1141 | if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) { |
| 1142 | pmbus_add_boolean_reg(data, "curr", "max_alarm", |
| 1143 | in_index, |
| 1144 | PB_STATUS_IOUT_BASE + |
| 1145 | page, PB_IOUT_OC_WARNING); |
| 1146 | have_alarm = true; |
| 1147 | } |
| 1148 | } |
| 1149 | if (pmbus_check_word_register(client, page, |
| 1150 | PMBUS_IOUT_UC_FAULT_LIMIT)) { |
| 1151 | i1 = data->num_sensors; |
| 1152 | pmbus_add_sensor(data, "curr", "lcrit", in_index, page, |
| 1153 | PMBUS_IOUT_UC_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1154 | PSC_CURRENT_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1155 | if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) { |
| 1156 | pmbus_add_boolean_reg(data, "curr", |
| 1157 | "lcrit_alarm", |
| 1158 | in_index, |
| 1159 | PB_STATUS_IOUT_BASE + |
| 1160 | page, PB_IOUT_UC_FAULT); |
| 1161 | have_alarm = true; |
| 1162 | } |
| 1163 | } |
| 1164 | if (pmbus_check_word_register(client, page, |
| 1165 | PMBUS_IOUT_OC_FAULT_LIMIT)) { |
| 1166 | i1 = data->num_sensors; |
| 1167 | pmbus_add_sensor(data, "curr", "crit", in_index, page, |
| 1168 | PMBUS_IOUT_OC_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1169 | PSC_CURRENT_OUT, false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1170 | if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) { |
| 1171 | pmbus_add_boolean_reg(data, "curr", |
| 1172 | "crit_alarm", |
| 1173 | in_index, |
| 1174 | PB_STATUS_IOUT_BASE + |
| 1175 | page, PB_IOUT_OC_FAULT); |
| 1176 | have_alarm = true; |
| 1177 | } |
| 1178 | } |
| 1179 | /* |
| 1180 | * Add generic alarm attribute only if there are no individual |
| 1181 | * attributes. |
| 1182 | */ |
| 1183 | if (!have_alarm) |
| 1184 | pmbus_add_boolean_reg(data, "curr", "alarm", |
| 1185 | in_index, |
| 1186 | PB_STATUS_BASE + page, |
| 1187 | PB_STATUS_IOUT_OC); |
| 1188 | in_index++; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
| 1192 | * Power sensors |
| 1193 | */ |
| 1194 | /* |
| 1195 | * Input Power sensors |
| 1196 | */ |
| 1197 | in_index = 1; |
| 1198 | if (info->func[0] & PMBUS_HAVE_PIN) { |
| 1199 | i0 = data->num_sensors; |
| 1200 | pmbus_add_label(data, "power", in_index, "pin", 0); |
| 1201 | pmbus_add_sensor(data, "power", "input", in_index, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1202 | 0, PMBUS_READ_PIN, PSC_POWER, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1203 | if (pmbus_check_word_register(client, 0, |
| 1204 | PMBUS_PIN_OP_WARN_LIMIT)) { |
| 1205 | i1 = data->num_sensors; |
| 1206 | pmbus_add_sensor(data, "power", "max", in_index, |
| 1207 | 0, PMBUS_PIN_OP_WARN_LIMIT, PSC_POWER, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1208 | false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1209 | if (info->func[0] & PMBUS_HAVE_STATUS_INPUT) |
| 1210 | pmbus_add_boolean_reg(data, "power", |
| 1211 | "alarm", |
| 1212 | in_index, |
| 1213 | PB_STATUS_INPUT_BASE, |
| 1214 | PB_PIN_OP_WARNING); |
| 1215 | } |
| 1216 | in_index++; |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Output Power sensors |
| 1221 | */ |
| 1222 | for (page = 0; page < info->pages; page++) { |
| 1223 | bool need_alarm = false; |
| 1224 | |
| 1225 | if (!(info->func[page] & PMBUS_HAVE_POUT)) |
| 1226 | continue; |
| 1227 | |
| 1228 | i0 = data->num_sensors; |
| 1229 | pmbus_add_label(data, "power", in_index, "pout", page + 1); |
| 1230 | pmbus_add_sensor(data, "power", "input", in_index, page, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1231 | PMBUS_READ_POUT, PSC_POWER, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1232 | /* |
| 1233 | * Per hwmon sysfs API, power_cap is to be used to limit output |
| 1234 | * power. |
| 1235 | * We have two registers related to maximum output power, |
| 1236 | * PMBUS_POUT_MAX and PMBUS_POUT_OP_WARN_LIMIT. |
| 1237 | * PMBUS_POUT_MAX matches the powerX_cap attribute definition. |
| 1238 | * There is no attribute in the API to match |
| 1239 | * PMBUS_POUT_OP_WARN_LIMIT. We use powerX_max for now. |
| 1240 | */ |
| 1241 | if (pmbus_check_word_register(client, page, PMBUS_POUT_MAX)) { |
| 1242 | i1 = data->num_sensors; |
| 1243 | pmbus_add_sensor(data, "power", "cap", in_index, page, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1244 | PMBUS_POUT_MAX, PSC_POWER, |
| 1245 | false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1246 | need_alarm = true; |
| 1247 | } |
| 1248 | if (pmbus_check_word_register(client, page, |
| 1249 | PMBUS_POUT_OP_WARN_LIMIT)) { |
| 1250 | i1 = data->num_sensors; |
| 1251 | pmbus_add_sensor(data, "power", "max", in_index, page, |
| 1252 | PMBUS_POUT_OP_WARN_LIMIT, PSC_POWER, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1253 | false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1254 | need_alarm = true; |
| 1255 | } |
| 1256 | if (need_alarm && (info->func[page] & PMBUS_HAVE_STATUS_IOUT)) |
| 1257 | pmbus_add_boolean_reg(data, "power", "alarm", |
| 1258 | in_index, |
| 1259 | PB_STATUS_IOUT_BASE + page, |
| 1260 | PB_POUT_OP_WARNING |
| 1261 | | PB_POWER_LIMITING); |
| 1262 | |
| 1263 | if (pmbus_check_word_register(client, page, |
| 1264 | PMBUS_POUT_OP_FAULT_LIMIT)) { |
| 1265 | i1 = data->num_sensors; |
| 1266 | pmbus_add_sensor(data, "power", "crit", in_index, page, |
| 1267 | PMBUS_POUT_OP_FAULT_LIMIT, PSC_POWER, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1268 | false, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1269 | if (info->func[page] & PMBUS_HAVE_STATUS_IOUT) |
| 1270 | pmbus_add_boolean_reg(data, "power", |
| 1271 | "crit_alarm", |
| 1272 | in_index, |
| 1273 | PB_STATUS_IOUT_BASE |
| 1274 | + page, |
| 1275 | PB_POUT_OP_FAULT); |
| 1276 | } |
| 1277 | in_index++; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Temperature sensors |
| 1282 | */ |
| 1283 | in_index = 1; |
| 1284 | for (page = 0; page < info->pages; page++) { |
Guenter Roeck | b49547a | 2011-03-06 10:56:52 -0800 | [diff] [blame] | 1285 | int t; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1286 | |
Guenter Roeck | b49547a | 2011-03-06 10:56:52 -0800 | [diff] [blame] | 1287 | for (t = 0; t < ARRAY_SIZE(pmbus_temp_registers); t++) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1288 | bool have_alarm = false; |
| 1289 | |
Guenter Roeck | b49547a | 2011-03-06 10:56:52 -0800 | [diff] [blame] | 1290 | /* |
| 1291 | * A PMBus chip may support any combination of |
| 1292 | * temperature registers on any page. So we can not |
| 1293 | * abort after a failure to detect a register, but have |
| 1294 | * to continue checking for all registers on all pages. |
| 1295 | */ |
| 1296 | if (!(info->func[page] & pmbus_temp_flags[t])) |
| 1297 | continue; |
| 1298 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1299 | if (!pmbus_check_word_register |
| 1300 | (client, page, pmbus_temp_registers[t])) |
Guenter Roeck | b49547a | 2011-03-06 10:56:52 -0800 | [diff] [blame] | 1301 | continue; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1302 | |
| 1303 | i0 = data->num_sensors; |
| 1304 | pmbus_add_sensor(data, "temp", "input", in_index, page, |
| 1305 | pmbus_temp_registers[t], |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1306 | PSC_TEMPERATURE, true, true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1307 | |
| 1308 | /* |
| 1309 | * PMBus provides only one status register for TEMP1-3. |
| 1310 | * Thus, we can not use the status register to determine |
| 1311 | * which of the three sensors actually caused an alarm. |
| 1312 | * Always compare current temperature against the limit |
| 1313 | * registers to determine alarm conditions for a |
| 1314 | * specific sensor. |
Guenter Roeck | 8677011 | 2011-03-06 12:28:24 -0800 | [diff] [blame] | 1315 | * |
| 1316 | * Since there is only one set of limit registers for |
| 1317 | * up to three temperature sensors, we need to update |
| 1318 | * all limit registers after the limit was changed for |
| 1319 | * one of the sensors. This ensures that correct limits |
| 1320 | * are reported for all temperature sensors. |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1321 | */ |
| 1322 | if (pmbus_check_word_register |
| 1323 | (client, page, PMBUS_UT_WARN_LIMIT)) { |
| 1324 | i1 = data->num_sensors; |
| 1325 | pmbus_add_sensor(data, "temp", "min", in_index, |
| 1326 | page, PMBUS_UT_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1327 | PSC_TEMPERATURE, true, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1328 | if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) { |
| 1329 | pmbus_add_boolean_cmp(data, "temp", |
| 1330 | "min_alarm", in_index, i1, i0, |
| 1331 | PB_STATUS_TEMP_BASE + page, |
| 1332 | PB_TEMP_UT_WARNING); |
| 1333 | have_alarm = true; |
| 1334 | } |
| 1335 | } |
| 1336 | if (pmbus_check_word_register(client, page, |
| 1337 | PMBUS_UT_FAULT_LIMIT)) { |
| 1338 | i1 = data->num_sensors; |
| 1339 | pmbus_add_sensor(data, "temp", "lcrit", |
| 1340 | in_index, page, |
| 1341 | PMBUS_UT_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1342 | PSC_TEMPERATURE, true, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1343 | if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) { |
| 1344 | pmbus_add_boolean_cmp(data, "temp", |
| 1345 | "lcrit_alarm", in_index, i1, i0, |
| 1346 | PB_STATUS_TEMP_BASE + page, |
| 1347 | PB_TEMP_UT_FAULT); |
| 1348 | have_alarm = true; |
| 1349 | } |
| 1350 | } |
| 1351 | if (pmbus_check_word_register |
| 1352 | (client, page, PMBUS_OT_WARN_LIMIT)) { |
| 1353 | i1 = data->num_sensors; |
| 1354 | pmbus_add_sensor(data, "temp", "max", in_index, |
| 1355 | page, PMBUS_OT_WARN_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1356 | PSC_TEMPERATURE, true, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1357 | if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) { |
| 1358 | pmbus_add_boolean_cmp(data, "temp", |
| 1359 | "max_alarm", in_index, i0, i1, |
| 1360 | PB_STATUS_TEMP_BASE + page, |
| 1361 | PB_TEMP_OT_WARNING); |
| 1362 | have_alarm = true; |
| 1363 | } |
| 1364 | } |
| 1365 | if (pmbus_check_word_register(client, page, |
| 1366 | PMBUS_OT_FAULT_LIMIT)) { |
| 1367 | i1 = data->num_sensors; |
| 1368 | pmbus_add_sensor(data, "temp", "crit", in_index, |
| 1369 | page, PMBUS_OT_FAULT_LIMIT, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1370 | PSC_TEMPERATURE, true, false); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1371 | if (info->func[page] & PMBUS_HAVE_STATUS_TEMP) { |
| 1372 | pmbus_add_boolean_cmp(data, "temp", |
| 1373 | "crit_alarm", in_index, i0, i1, |
| 1374 | PB_STATUS_TEMP_BASE + page, |
| 1375 | PB_TEMP_OT_FAULT); |
| 1376 | have_alarm = true; |
| 1377 | } |
| 1378 | } |
| 1379 | /* |
| 1380 | * Last resort - we were not able to create any alarm |
| 1381 | * registers. Report alarm for all sensors using the |
| 1382 | * status register temperature alarm bit. |
| 1383 | */ |
| 1384 | if (!have_alarm) |
| 1385 | pmbus_add_boolean_reg(data, "temp", "alarm", |
| 1386 | in_index, |
| 1387 | PB_STATUS_BASE + page, |
| 1388 | PB_STATUS_TEMPERATURE); |
| 1389 | in_index++; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * Fans |
| 1395 | */ |
| 1396 | in_index = 1; |
| 1397 | for (page = 0; page < info->pages; page++) { |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 1398 | int f; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1399 | |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 1400 | for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1401 | int regval; |
| 1402 | |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 1403 | if (!(info->func[page] & pmbus_fan_flags[f])) |
| 1404 | break; |
| 1405 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1406 | if (!pmbus_check_word_register(client, page, |
| 1407 | pmbus_fan_registers[f]) |
| 1408 | || !pmbus_check_byte_register(client, page, |
| 1409 | pmbus_fan_config_registers[f])) |
| 1410 | break; |
| 1411 | |
| 1412 | /* |
| 1413 | * Skip fan if not installed. |
| 1414 | * Each fan configuration register covers multiple fans, |
| 1415 | * so we have to do some magic. |
| 1416 | */ |
| 1417 | regval = pmbus_read_byte_data(client, page, |
| 1418 | pmbus_fan_config_registers[f]); |
| 1419 | if (regval < 0 || |
| 1420 | (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) |
| 1421 | continue; |
| 1422 | |
| 1423 | i0 = data->num_sensors; |
| 1424 | pmbus_add_sensor(data, "fan", "input", in_index, page, |
Guenter Roeck | 3506999 | 2011-03-21 21:31:00 -0700 | [diff] [blame] | 1425 | pmbus_fan_registers[f], PSC_FAN, true, |
| 1426 | true); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1427 | |
| 1428 | /* |
| 1429 | * Each fan status register covers multiple fans, |
| 1430 | * so we have to do some magic. |
| 1431 | */ |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 1432 | if ((info->func[page] & pmbus_fan_status_flags[f]) && |
| 1433 | pmbus_check_byte_register(client, |
| 1434 | page, pmbus_fan_status_registers[f])) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1435 | int base; |
| 1436 | |
| 1437 | if (f > 1) /* fan 3, 4 */ |
Guenter Roeck | 954df67 | 2011-03-05 07:55:10 -0800 | [diff] [blame] | 1438 | base = PB_STATUS_FAN34_BASE + page; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1439 | else |
| 1440 | base = PB_STATUS_FAN_BASE + page; |
| 1441 | pmbus_add_boolean_reg(data, "fan", "alarm", |
| 1442 | in_index, base, |
| 1443 | PB_FAN_FAN1_WARNING >> (f & 1)); |
| 1444 | pmbus_add_boolean_reg(data, "fan", "fault", |
| 1445 | in_index, base, |
| 1446 | PB_FAN_FAN1_FAULT >> (f & 1)); |
| 1447 | } |
| 1448 | in_index++; |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * Identify chip parameters. |
| 1455 | * This function is called for all chips. |
| 1456 | */ |
| 1457 | static int pmbus_identify_common(struct i2c_client *client, |
| 1458 | struct pmbus_data *data) |
| 1459 | { |
Guenter Roeck | 83274c6 | 2011-03-01 13:49:18 -0800 | [diff] [blame] | 1460 | int vout_mode = -1, exponent; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1461 | |
Guenter Roeck | 83274c6 | 2011-03-01 13:49:18 -0800 | [diff] [blame] | 1462 | if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) |
| 1463 | vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE); |
| 1464 | if (vout_mode >= 0 && vout_mode != 0xff) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 1465 | /* |
| 1466 | * Not all chips support the VOUT_MODE command, |
| 1467 | * so a failure to read it is not an error. |
| 1468 | */ |
| 1469 | switch (vout_mode >> 5) { |
| 1470 | case 0: /* linear mode */ |
| 1471 | if (data->info->direct[PSC_VOLTAGE_OUT]) |
| 1472 | return -ENODEV; |
| 1473 | |
| 1474 | exponent = vout_mode & 0x1f; |
| 1475 | /* and sign-extend it */ |
| 1476 | if (exponent & 0x10) |
| 1477 | exponent |= ~0x1f; |
| 1478 | data->exponent = exponent; |
| 1479 | break; |
| 1480 | case 2: /* direct mode */ |
| 1481 | if (!data->info->direct[PSC_VOLTAGE_OUT]) |
| 1482 | return -ENODEV; |
| 1483 | break; |
| 1484 | default: |
| 1485 | return -ENODEV; |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | /* Determine maximum number of sensors, booleans, and labels */ |
| 1490 | pmbus_find_max_attr(client, data); |
| 1491 | pmbus_clear_fault_page(client, 0); |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id, |
| 1496 | struct pmbus_driver_info *info) |
| 1497 | { |
| 1498 | const struct pmbus_platform_data *pdata = client->dev.platform_data; |
| 1499 | struct pmbus_data *data; |
| 1500 | int ret; |
| 1501 | |
| 1502 | if (!info) { |
| 1503 | dev_err(&client->dev, "Missing chip information"); |
| 1504 | return -ENODEV; |
| 1505 | } |
| 1506 | |
| 1507 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE |
| 1508 | | I2C_FUNC_SMBUS_BYTE_DATA |
| 1509 | | I2C_FUNC_SMBUS_WORD_DATA)) |
| 1510 | return -ENODEV; |
| 1511 | |
| 1512 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1513 | if (!data) { |
| 1514 | dev_err(&client->dev, "No memory to allocate driver data\n"); |
| 1515 | return -ENOMEM; |
| 1516 | } |
| 1517 | |
| 1518 | i2c_set_clientdata(client, data); |
| 1519 | mutex_init(&data->update_lock); |
| 1520 | |
| 1521 | /* |
| 1522 | * Bail out if status register or PMBus revision register |
| 1523 | * does not exist. |
| 1524 | */ |
| 1525 | if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0 |
| 1526 | || i2c_smbus_read_byte_data(client, PMBUS_REVISION) < 0) { |
| 1527 | dev_err(&client->dev, |
| 1528 | "Status or revision register not found\n"); |
| 1529 | ret = -ENODEV; |
| 1530 | goto out_data; |
| 1531 | } |
| 1532 | |
| 1533 | if (pdata) |
| 1534 | data->flags = pdata->flags; |
| 1535 | data->info = info; |
| 1536 | |
| 1537 | pmbus_clear_faults(client); |
| 1538 | |
| 1539 | if (info->identify) { |
| 1540 | ret = (*info->identify)(client, info); |
| 1541 | if (ret < 0) { |
| 1542 | dev_err(&client->dev, "Chip identification failed\n"); |
| 1543 | goto out_data; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | if (info->pages <= 0 || info->pages > PMBUS_PAGES) { |
| 1548 | dev_err(&client->dev, "Bad number of PMBus pages: %d\n", |
| 1549 | info->pages); |
| 1550 | ret = -EINVAL; |
| 1551 | goto out_data; |
| 1552 | } |
| 1553 | /* |
| 1554 | * Bail out if more than one page was configured, but we can not |
| 1555 | * select the highest page. This is an indication that the wrong |
| 1556 | * chip type was selected. Better bail out now than keep |
| 1557 | * returning errors later on. |
| 1558 | */ |
| 1559 | if (info->pages > 1 && pmbus_set_page(client, info->pages - 1) < 0) { |
| 1560 | dev_err(&client->dev, "Failed to select page %d\n", |
| 1561 | info->pages - 1); |
| 1562 | ret = -EINVAL; |
| 1563 | goto out_data; |
| 1564 | } |
| 1565 | |
| 1566 | ret = pmbus_identify_common(client, data); |
| 1567 | if (ret < 0) { |
| 1568 | dev_err(&client->dev, "Failed to identify chip capabilities\n"); |
| 1569 | goto out_data; |
| 1570 | } |
| 1571 | |
| 1572 | ret = -ENOMEM; |
| 1573 | data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors, |
| 1574 | GFP_KERNEL); |
| 1575 | if (!data->sensors) { |
| 1576 | dev_err(&client->dev, "No memory to allocate sensor data\n"); |
| 1577 | goto out_data; |
| 1578 | } |
| 1579 | |
| 1580 | data->booleans = kzalloc(sizeof(struct pmbus_boolean) |
| 1581 | * data->max_booleans, GFP_KERNEL); |
| 1582 | if (!data->booleans) { |
| 1583 | dev_err(&client->dev, "No memory to allocate boolean data\n"); |
| 1584 | goto out_sensors; |
| 1585 | } |
| 1586 | |
| 1587 | data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels, |
| 1588 | GFP_KERNEL); |
| 1589 | if (!data->labels) { |
| 1590 | dev_err(&client->dev, "No memory to allocate label data\n"); |
| 1591 | goto out_booleans; |
| 1592 | } |
| 1593 | |
| 1594 | data->attributes = kzalloc(sizeof(struct attribute *) |
| 1595 | * data->max_attributes, GFP_KERNEL); |
| 1596 | if (!data->attributes) { |
| 1597 | dev_err(&client->dev, "No memory to allocate attribute data\n"); |
| 1598 | goto out_labels; |
| 1599 | } |
| 1600 | |
| 1601 | pmbus_find_attributes(client, data); |
| 1602 | |
| 1603 | /* |
| 1604 | * If there are no attributes, something is wrong. |
| 1605 | * Bail out instead of trying to register nothing. |
| 1606 | */ |
| 1607 | if (!data->num_attributes) { |
| 1608 | dev_err(&client->dev, "No attributes found\n"); |
| 1609 | ret = -ENODEV; |
| 1610 | goto out_attributes; |
| 1611 | } |
| 1612 | |
| 1613 | /* Register sysfs hooks */ |
| 1614 | data->group.attrs = data->attributes; |
| 1615 | ret = sysfs_create_group(&client->dev.kobj, &data->group); |
| 1616 | if (ret) { |
| 1617 | dev_err(&client->dev, "Failed to create sysfs entries\n"); |
| 1618 | goto out_attributes; |
| 1619 | } |
| 1620 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 1621 | if (IS_ERR(data->hwmon_dev)) { |
| 1622 | ret = PTR_ERR(data->hwmon_dev); |
| 1623 | dev_err(&client->dev, "Failed to register hwmon device\n"); |
| 1624 | goto out_hwmon_device_register; |
| 1625 | } |
| 1626 | return 0; |
| 1627 | |
| 1628 | out_hwmon_device_register: |
| 1629 | sysfs_remove_group(&client->dev.kobj, &data->group); |
| 1630 | out_attributes: |
| 1631 | kfree(data->attributes); |
| 1632 | out_labels: |
| 1633 | kfree(data->labels); |
| 1634 | out_booleans: |
| 1635 | kfree(data->booleans); |
| 1636 | out_sensors: |
| 1637 | kfree(data->sensors); |
| 1638 | out_data: |
| 1639 | kfree(data); |
| 1640 | return ret; |
| 1641 | } |
| 1642 | EXPORT_SYMBOL_GPL(pmbus_do_probe); |
| 1643 | |
| 1644 | int pmbus_do_remove(struct i2c_client *client) |
| 1645 | { |
| 1646 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 1647 | hwmon_device_unregister(data->hwmon_dev); |
| 1648 | sysfs_remove_group(&client->dev.kobj, &data->group); |
| 1649 | kfree(data->attributes); |
| 1650 | kfree(data->labels); |
| 1651 | kfree(data->booleans); |
| 1652 | kfree(data->sensors); |
| 1653 | kfree(data); |
| 1654 | return 0; |
| 1655 | } |
| 1656 | EXPORT_SYMBOL_GPL(pmbus_do_remove); |
| 1657 | |
| 1658 | MODULE_AUTHOR("Guenter Roeck"); |
| 1659 | MODULE_DESCRIPTION("PMBus core driver"); |
| 1660 | MODULE_LICENSE("GPL"); |