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