| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Driver for Lineage Compact Power Line series of power entry modules. | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2010, 2011 Ericsson AB. | 
|  | 5 | * | 
|  | 6 | * Documentation: | 
|  | 7 | *  http://www.lineagepower.com/oem/pdf/CPLI2C.pdf | 
|  | 8 | * | 
|  | 9 | * This program is free software; you can redistribute it and/or modify | 
|  | 10 | * it under the terms of the GNU General Public License as published by | 
|  | 11 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 12 | * (at your option) any later version. | 
|  | 13 | * | 
|  | 14 | * This program is distributed in the hope that it will be useful, | 
|  | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 17 | * GNU General Public License for more details. | 
|  | 18 | * | 
|  | 19 | * You should have received a copy of the GNU General Public License | 
|  | 20 | * along with this program; if not, write to the Free Software | 
|  | 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 22 | */ | 
|  | 23 |  | 
|  | 24 | #include <linux/kernel.h> | 
|  | 25 | #include <linux/module.h> | 
|  | 26 | #include <linux/init.h> | 
|  | 27 | #include <linux/err.h> | 
|  | 28 | #include <linux/slab.h> | 
|  | 29 | #include <linux/i2c.h> | 
|  | 30 | #include <linux/hwmon.h> | 
|  | 31 | #include <linux/hwmon-sysfs.h> | 
|  | 32 | #include <linux/jiffies.h> | 
|  | 33 |  | 
|  | 34 | /* | 
|  | 35 | * This driver supports various Lineage Compact Power Line DC/DC and AC/DC | 
|  | 36 | * converters such as CP1800, CP2000AC, CP2000DC, CP2100DC, and others. | 
|  | 37 | * | 
|  | 38 | * The devices are nominally PMBus compliant. However, most standard PMBus | 
|  | 39 | * commands are not supported. Specifically, all hardware monitoring and | 
|  | 40 | * status reporting commands are non-standard. For this reason, a standard | 
|  | 41 | * PMBus driver can not be used. | 
|  | 42 | * | 
|  | 43 | * All Lineage CPL devices have a built-in I2C bus master selector (PCA9541). | 
|  | 44 | * To ensure device access, this driver should only be used as client driver | 
|  | 45 | * to the pca9541 I2C master selector driver. | 
|  | 46 | */ | 
|  | 47 |  | 
|  | 48 | /* Command codes */ | 
|  | 49 | #define PEM_OPERATION		0x01 | 
|  | 50 | #define PEM_CLEAR_INFO_FLAGS	0x03 | 
|  | 51 | #define PEM_VOUT_COMMAND	0x21 | 
|  | 52 | #define PEM_VOUT_OV_FAULT_LIMIT	0x40 | 
|  | 53 | #define PEM_READ_DATA_STRING	0xd0 | 
|  | 54 | #define PEM_READ_INPUT_STRING	0xdc | 
|  | 55 | #define PEM_READ_FIRMWARE_REV	0xdd | 
|  | 56 | #define PEM_READ_RUN_TIMER	0xde | 
|  | 57 | #define PEM_FAN_HI_SPEED	0xdf | 
|  | 58 | #define PEM_FAN_NORMAL_SPEED	0xe0 | 
|  | 59 | #define PEM_READ_FAN_SPEED	0xe1 | 
|  | 60 |  | 
|  | 61 | /* offsets in data string */ | 
|  | 62 | #define PEM_DATA_STATUS_2	0 | 
|  | 63 | #define PEM_DATA_STATUS_1	1 | 
|  | 64 | #define PEM_DATA_ALARM_2	2 | 
|  | 65 | #define PEM_DATA_ALARM_1	3 | 
|  | 66 | #define PEM_DATA_VOUT_LSB	4 | 
|  | 67 | #define PEM_DATA_VOUT_MSB	5 | 
|  | 68 | #define PEM_DATA_CURRENT	6 | 
|  | 69 | #define PEM_DATA_TEMP		7 | 
|  | 70 |  | 
|  | 71 | /* Virtual entries, to report constants */ | 
|  | 72 | #define PEM_DATA_TEMP_MAX	10 | 
|  | 73 | #define PEM_DATA_TEMP_CRIT	11 | 
|  | 74 |  | 
|  | 75 | /* offsets in input string */ | 
|  | 76 | #define PEM_INPUT_VOLTAGE	0 | 
|  | 77 | #define PEM_INPUT_POWER_LSB	1 | 
|  | 78 | #define PEM_INPUT_POWER_MSB	2 | 
|  | 79 |  | 
|  | 80 | /* offsets in fan data */ | 
|  | 81 | #define PEM_FAN_ADJUSTMENT	0 | 
|  | 82 | #define PEM_FAN_FAN1		1 | 
|  | 83 | #define PEM_FAN_FAN2		2 | 
|  | 84 | #define PEM_FAN_FAN3		3 | 
|  | 85 |  | 
|  | 86 | /* Status register bits */ | 
|  | 87 | #define STS1_OUTPUT_ON		(1 << 0) | 
|  | 88 | #define STS1_LEDS_FLASHING	(1 << 1) | 
|  | 89 | #define STS1_EXT_FAULT		(1 << 2) | 
|  | 90 | #define STS1_SERVICE_LED_ON	(1 << 3) | 
|  | 91 | #define STS1_SHUTDOWN_OCCURRED	(1 << 4) | 
|  | 92 | #define STS1_INT_FAULT		(1 << 5) | 
|  | 93 | #define STS1_ISOLATION_TEST_OK	(1 << 6) | 
|  | 94 |  | 
|  | 95 | #define STS2_ENABLE_PIN_HI	(1 << 0) | 
|  | 96 | #define STS2_DATA_OUT_RANGE	(1 << 1) | 
|  | 97 | #define STS2_RESTARTED_OK	(1 << 1) | 
|  | 98 | #define STS2_ISOLATION_TEST_FAIL (1 << 3) | 
|  | 99 | #define STS2_HIGH_POWER_CAP	(1 << 4) | 
|  | 100 | #define STS2_INVALID_INSTR	(1 << 5) | 
|  | 101 | #define STS2_WILL_RESTART	(1 << 6) | 
|  | 102 | #define STS2_PEC_ERR		(1 << 7) | 
|  | 103 |  | 
|  | 104 | /* Alarm register bits */ | 
|  | 105 | #define ALRM1_VIN_OUT_LIMIT	(1 << 0) | 
|  | 106 | #define ALRM1_VOUT_OUT_LIMIT	(1 << 1) | 
|  | 107 | #define ALRM1_OV_VOLT_SHUTDOWN	(1 << 2) | 
|  | 108 | #define ALRM1_VIN_OVERCURRENT	(1 << 3) | 
|  | 109 | #define ALRM1_TEMP_WARNING	(1 << 4) | 
|  | 110 | #define ALRM1_TEMP_SHUTDOWN	(1 << 5) | 
|  | 111 | #define ALRM1_PRIMARY_FAULT	(1 << 6) | 
|  | 112 | #define ALRM1_POWER_LIMIT	(1 << 7) | 
|  | 113 |  | 
|  | 114 | #define ALRM2_5V_OUT_LIMIT	(1 << 1) | 
|  | 115 | #define ALRM2_TEMP_FAULT	(1 << 2) | 
|  | 116 | #define ALRM2_OV_LOW		(1 << 3) | 
|  | 117 | #define ALRM2_DCDC_TEMP_HIGH	(1 << 4) | 
|  | 118 | #define ALRM2_PRI_TEMP_HIGH	(1 << 5) | 
|  | 119 | #define ALRM2_NO_PRIMARY	(1 << 6) | 
|  | 120 | #define ALRM2_FAN_FAULT		(1 << 7) | 
|  | 121 |  | 
|  | 122 | #define FIRMWARE_REV_LEN	4 | 
|  | 123 | #define DATA_STRING_LEN		9 | 
|  | 124 | #define INPUT_STRING_LEN	5	/* 4 for most devices	*/ | 
|  | 125 | #define FAN_SPEED_LEN		5 | 
|  | 126 |  | 
|  | 127 | struct pem_data { | 
|  | 128 | struct i2c_client *client; | 
|  | 129 | const struct attribute_group *groups[4]; | 
|  | 130 |  | 
|  | 131 | struct mutex update_lock; | 
|  | 132 | bool valid; | 
|  | 133 | bool fans_supported; | 
|  | 134 | int input_length; | 
|  | 135 | unsigned long last_updated;	/* in jiffies */ | 
|  | 136 |  | 
|  | 137 | u8 firmware_rev[FIRMWARE_REV_LEN]; | 
|  | 138 | u8 data_string[DATA_STRING_LEN]; | 
|  | 139 | u8 input_string[INPUT_STRING_LEN]; | 
|  | 140 | u8 fan_speed[FAN_SPEED_LEN]; | 
|  | 141 | }; | 
|  | 142 |  | 
|  | 143 | static int pem_read_block(struct i2c_client *client, u8 command, u8 *data, | 
|  | 144 | int data_len) | 
|  | 145 | { | 
|  | 146 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX]; | 
|  | 147 | int result; | 
|  | 148 |  | 
|  | 149 | result = i2c_smbus_read_block_data(client, command, block_buffer); | 
|  | 150 | if (unlikely(result < 0)) | 
|  | 151 | goto abort; | 
|  | 152 | if (unlikely(result == 0xff || result != data_len)) { | 
|  | 153 | result = -EIO; | 
|  | 154 | goto abort; | 
|  | 155 | } | 
|  | 156 | memcpy(data, block_buffer, data_len); | 
|  | 157 | result = 0; | 
|  | 158 | abort: | 
|  | 159 | return result; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | static struct pem_data *pem_update_device(struct device *dev) | 
|  | 163 | { | 
|  | 164 | struct pem_data *data = dev_get_drvdata(dev); | 
|  | 165 | struct i2c_client *client = data->client; | 
|  | 166 | struct pem_data *ret = data; | 
|  | 167 |  | 
|  | 168 | mutex_lock(&data->update_lock); | 
|  | 169 |  | 
|  | 170 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | 
|  | 171 | int result; | 
|  | 172 |  | 
|  | 173 | /* Read data string */ | 
|  | 174 | result = pem_read_block(client, PEM_READ_DATA_STRING, | 
|  | 175 | data->data_string, | 
|  | 176 | sizeof(data->data_string)); | 
|  | 177 | if (unlikely(result < 0)) { | 
|  | 178 | ret = ERR_PTR(result); | 
|  | 179 | goto abort; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | /* Read input string */ | 
|  | 183 | if (data->input_length) { | 
|  | 184 | result = pem_read_block(client, PEM_READ_INPUT_STRING, | 
|  | 185 | data->input_string, | 
|  | 186 | data->input_length); | 
|  | 187 | if (unlikely(result < 0)) { | 
|  | 188 | ret = ERR_PTR(result); | 
|  | 189 | goto abort; | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | /* Read fan speeds */ | 
|  | 194 | if (data->fans_supported) { | 
|  | 195 | result = pem_read_block(client, PEM_READ_FAN_SPEED, | 
|  | 196 | data->fan_speed, | 
|  | 197 | sizeof(data->fan_speed)); | 
|  | 198 | if (unlikely(result < 0)) { | 
|  | 199 | ret = ERR_PTR(result); | 
|  | 200 | goto abort; | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); | 
|  | 205 |  | 
|  | 206 | data->last_updated = jiffies; | 
|  | 207 | data->valid = 1; | 
|  | 208 | } | 
|  | 209 | abort: | 
|  | 210 | mutex_unlock(&data->update_lock); | 
|  | 211 | return ret; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | static long pem_get_data(u8 *data, int len, int index) | 
|  | 215 | { | 
|  | 216 | long val; | 
|  | 217 |  | 
|  | 218 | switch (index) { | 
|  | 219 | case PEM_DATA_VOUT_LSB: | 
|  | 220 | val = (data[index] + (data[index+1] << 8)) * 5 / 2; | 
|  | 221 | break; | 
|  | 222 | case PEM_DATA_CURRENT: | 
|  | 223 | val = data[index] * 200; | 
|  | 224 | break; | 
|  | 225 | case PEM_DATA_TEMP: | 
|  | 226 | val = data[index] * 1000; | 
|  | 227 | break; | 
|  | 228 | case PEM_DATA_TEMP_MAX: | 
|  | 229 | val = 97 * 1000;	/* 97 degrees C per datasheet */ | 
|  | 230 | break; | 
|  | 231 | case PEM_DATA_TEMP_CRIT: | 
|  | 232 | val = 107 * 1000;	/* 107 degrees C per datasheet */ | 
|  | 233 | break; | 
|  | 234 | default: | 
|  | 235 | WARN_ON_ONCE(1); | 
|  | 236 | val = 0; | 
|  | 237 | } | 
|  | 238 | return val; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | static long pem_get_input(u8 *data, int len, int index) | 
|  | 242 | { | 
|  | 243 | long val; | 
|  | 244 |  | 
|  | 245 | switch (index) { | 
|  | 246 | case PEM_INPUT_VOLTAGE: | 
|  | 247 | if (len == INPUT_STRING_LEN) | 
|  | 248 | val = (data[index] + (data[index+1] << 8) - 75) * 1000; | 
|  | 249 | else | 
|  | 250 | val = (data[index] - 75) * 1000; | 
|  | 251 | break; | 
|  | 252 | case PEM_INPUT_POWER_LSB: | 
|  | 253 | if (len == INPUT_STRING_LEN) | 
|  | 254 | index++; | 
|  | 255 | val = (data[index] + (data[index+1] << 8)) * 1000000L; | 
|  | 256 | break; | 
|  | 257 | default: | 
|  | 258 | WARN_ON_ONCE(1); | 
|  | 259 | val = 0; | 
|  | 260 | } | 
|  | 261 | return val; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | static long pem_get_fan(u8 *data, int len, int index) | 
|  | 265 | { | 
|  | 266 | long val; | 
|  | 267 |  | 
|  | 268 | switch (index) { | 
|  | 269 | case PEM_FAN_FAN1: | 
|  | 270 | case PEM_FAN_FAN2: | 
|  | 271 | case PEM_FAN_FAN3: | 
|  | 272 | val = data[index] * 100; | 
|  | 273 | break; | 
|  | 274 | default: | 
|  | 275 | WARN_ON_ONCE(1); | 
|  | 276 | val = 0; | 
|  | 277 | } | 
|  | 278 | return val; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | /* | 
|  | 282 | * Show boolean, either a fault or an alarm. | 
|  | 283 | * .nr points to the register, .index is the bit mask to check | 
|  | 284 | */ | 
|  | 285 | static ssize_t pem_show_bool(struct device *dev, | 
|  | 286 | struct device_attribute *da, char *buf) | 
|  | 287 | { | 
|  | 288 | struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da); | 
|  | 289 | struct pem_data *data = pem_update_device(dev); | 
|  | 290 | u8 status; | 
|  | 291 |  | 
|  | 292 | if (IS_ERR(data)) | 
|  | 293 | return PTR_ERR(data); | 
|  | 294 |  | 
|  | 295 | status = data->data_string[attr->nr] & attr->index; | 
|  | 296 | return snprintf(buf, PAGE_SIZE, "%d\n", !!status); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | static ssize_t pem_show_data(struct device *dev, struct device_attribute *da, | 
|  | 300 | char *buf) | 
|  | 301 | { | 
|  | 302 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 303 | struct pem_data *data = pem_update_device(dev); | 
|  | 304 | long value; | 
|  | 305 |  | 
|  | 306 | if (IS_ERR(data)) | 
|  | 307 | return PTR_ERR(data); | 
|  | 308 |  | 
|  | 309 | value = pem_get_data(data->data_string, sizeof(data->data_string), | 
|  | 310 | attr->index); | 
|  | 311 |  | 
|  | 312 | return snprintf(buf, PAGE_SIZE, "%ld\n", value); | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | static ssize_t pem_show_input(struct device *dev, struct device_attribute *da, | 
|  | 316 | char *buf) | 
|  | 317 | { | 
|  | 318 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 319 | struct pem_data *data = pem_update_device(dev); | 
|  | 320 | long value; | 
|  | 321 |  | 
|  | 322 | if (IS_ERR(data)) | 
|  | 323 | return PTR_ERR(data); | 
|  | 324 |  | 
|  | 325 | value = pem_get_input(data->input_string, sizeof(data->input_string), | 
|  | 326 | attr->index); | 
|  | 327 |  | 
|  | 328 | return snprintf(buf, PAGE_SIZE, "%ld\n", value); | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | static ssize_t pem_show_fan(struct device *dev, struct device_attribute *da, | 
|  | 332 | char *buf) | 
|  | 333 | { | 
|  | 334 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); | 
|  | 335 | struct pem_data *data = pem_update_device(dev); | 
|  | 336 | long value; | 
|  | 337 |  | 
|  | 338 | if (IS_ERR(data)) | 
|  | 339 | return PTR_ERR(data); | 
|  | 340 |  | 
|  | 341 | value = pem_get_fan(data->fan_speed, sizeof(data->fan_speed), | 
|  | 342 | attr->index); | 
|  | 343 |  | 
|  | 344 | return snprintf(buf, PAGE_SIZE, "%ld\n", value); | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | /* Voltages */ | 
|  | 348 | static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, pem_show_data, NULL, | 
|  | 349 | PEM_DATA_VOUT_LSB); | 
|  | 350 | static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 351 | PEM_DATA_ALARM_1, ALRM1_VOUT_OUT_LIMIT); | 
|  | 352 | static SENSOR_DEVICE_ATTR_2(in1_crit_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 353 | PEM_DATA_ALARM_1, ALRM1_OV_VOLT_SHUTDOWN); | 
|  | 354 | static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, pem_show_input, NULL, | 
|  | 355 | PEM_INPUT_VOLTAGE); | 
|  | 356 | static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 357 | PEM_DATA_ALARM_1, | 
|  | 358 | ALRM1_VIN_OUT_LIMIT | ALRM1_PRIMARY_FAULT); | 
|  | 359 |  | 
|  | 360 | /* Currents */ | 
|  | 361 | static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, pem_show_data, NULL, | 
|  | 362 | PEM_DATA_CURRENT); | 
|  | 363 | static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 364 | PEM_DATA_ALARM_1, ALRM1_VIN_OVERCURRENT); | 
|  | 365 |  | 
|  | 366 | /* Power */ | 
|  | 367 | static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, pem_show_input, NULL, | 
|  | 368 | PEM_INPUT_POWER_LSB); | 
|  | 369 | static SENSOR_DEVICE_ATTR_2(power1_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 370 | PEM_DATA_ALARM_1, ALRM1_POWER_LIMIT); | 
|  | 371 |  | 
|  | 372 | /* Fans */ | 
|  | 373 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, pem_show_fan, NULL, | 
|  | 374 | PEM_FAN_FAN1); | 
|  | 375 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, pem_show_fan, NULL, | 
|  | 376 | PEM_FAN_FAN2); | 
|  | 377 | static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, pem_show_fan, NULL, | 
|  | 378 | PEM_FAN_FAN3); | 
|  | 379 | static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 380 | PEM_DATA_ALARM_2, ALRM2_FAN_FAULT); | 
|  | 381 |  | 
|  | 382 | /* Temperatures */ | 
|  | 383 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, pem_show_data, NULL, | 
|  | 384 | PEM_DATA_TEMP); | 
|  | 385 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, pem_show_data, NULL, | 
|  | 386 | PEM_DATA_TEMP_MAX); | 
|  | 387 | static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, pem_show_data, NULL, | 
|  | 388 | PEM_DATA_TEMP_CRIT); | 
|  | 389 | static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 390 | PEM_DATA_ALARM_1, ALRM1_TEMP_WARNING); | 
|  | 391 | static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, pem_show_bool, NULL, | 
|  | 392 | PEM_DATA_ALARM_1, ALRM1_TEMP_SHUTDOWN); | 
|  | 393 | static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, pem_show_bool, NULL, | 
|  | 394 | PEM_DATA_ALARM_2, ALRM2_TEMP_FAULT); | 
|  | 395 |  | 
|  | 396 | static struct attribute *pem_attributes[] = { | 
|  | 397 | &sensor_dev_attr_in1_input.dev_attr.attr, | 
|  | 398 | &sensor_dev_attr_in1_alarm.dev_attr.attr, | 
|  | 399 | &sensor_dev_attr_in1_crit_alarm.dev_attr.attr, | 
|  | 400 | &sensor_dev_attr_in2_alarm.dev_attr.attr, | 
|  | 401 |  | 
|  | 402 | &sensor_dev_attr_curr1_alarm.dev_attr.attr, | 
|  | 403 |  | 
|  | 404 | &sensor_dev_attr_power1_alarm.dev_attr.attr, | 
|  | 405 |  | 
|  | 406 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, | 
|  | 407 |  | 
|  | 408 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 
|  | 409 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 
|  | 410 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | 
|  | 411 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, | 
|  | 412 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, | 
|  | 413 | &sensor_dev_attr_temp1_fault.dev_attr.attr, | 
|  | 414 |  | 
|  | 415 | NULL, | 
|  | 416 | }; | 
|  | 417 |  | 
|  | 418 | static const struct attribute_group pem_group = { | 
|  | 419 | .attrs = pem_attributes, | 
|  | 420 | }; | 
|  | 421 |  | 
|  | 422 | static struct attribute *pem_input_attributes[] = { | 
|  | 423 | &sensor_dev_attr_in2_input.dev_attr.attr, | 
|  | 424 | &sensor_dev_attr_curr1_input.dev_attr.attr, | 
|  | 425 | &sensor_dev_attr_power1_input.dev_attr.attr, | 
|  | 426 | NULL | 
|  | 427 | }; | 
|  | 428 |  | 
|  | 429 | static const struct attribute_group pem_input_group = { | 
|  | 430 | .attrs = pem_input_attributes, | 
|  | 431 | }; | 
|  | 432 |  | 
|  | 433 | static struct attribute *pem_fan_attributes[] = { | 
|  | 434 | &sensor_dev_attr_fan1_input.dev_attr.attr, | 
|  | 435 | &sensor_dev_attr_fan2_input.dev_attr.attr, | 
|  | 436 | &sensor_dev_attr_fan3_input.dev_attr.attr, | 
|  | 437 | NULL | 
|  | 438 | }; | 
|  | 439 |  | 
|  | 440 | static const struct attribute_group pem_fan_group = { | 
|  | 441 | .attrs = pem_fan_attributes, | 
|  | 442 | }; | 
|  | 443 |  | 
|  | 444 | static int pem_probe(struct i2c_client *client, | 
|  | 445 | const struct i2c_device_id *id) | 
|  | 446 | { | 
|  | 447 | struct i2c_adapter *adapter = client->adapter; | 
|  | 448 | struct device *dev = &client->dev; | 
|  | 449 | struct device *hwmon_dev; | 
|  | 450 | struct pem_data *data; | 
|  | 451 | int ret, idx = 0; | 
|  | 452 |  | 
|  | 453 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BLOCK_DATA | 
|  | 454 | | I2C_FUNC_SMBUS_WRITE_BYTE)) | 
|  | 455 | return -ENODEV; | 
|  | 456 |  | 
|  | 457 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); | 
|  | 458 | if (!data) | 
|  | 459 | return -ENOMEM; | 
|  | 460 |  | 
|  | 461 | data->client = client; | 
|  | 462 | mutex_init(&data->update_lock); | 
|  | 463 |  | 
|  | 464 | /* | 
|  | 465 | * We use the next two commands to determine if the device is really | 
|  | 466 | * there. | 
|  | 467 | */ | 
|  | 468 | ret = pem_read_block(client, PEM_READ_FIRMWARE_REV, | 
|  | 469 | data->firmware_rev, sizeof(data->firmware_rev)); | 
|  | 470 | if (ret < 0) | 
|  | 471 | return ret; | 
|  | 472 |  | 
|  | 473 | ret = i2c_smbus_write_byte(client, PEM_CLEAR_INFO_FLAGS); | 
|  | 474 | if (ret < 0) | 
|  | 475 | return ret; | 
|  | 476 |  | 
|  | 477 | dev_info(dev, "Firmware revision %d.%d.%d\n", | 
|  | 478 | data->firmware_rev[0], data->firmware_rev[1], | 
|  | 479 | data->firmware_rev[2]); | 
|  | 480 |  | 
|  | 481 | /* sysfs hooks */ | 
|  | 482 | data->groups[idx++] = &pem_group; | 
|  | 483 |  | 
|  | 484 | /* | 
|  | 485 | * Check if input readings are supported. | 
|  | 486 | * This is the case if we can read input data, | 
|  | 487 | * and if the returned data is not all zeros. | 
|  | 488 | * Note that input alarms are always supported. | 
|  | 489 | */ | 
|  | 490 | ret = pem_read_block(client, PEM_READ_INPUT_STRING, | 
|  | 491 | data->input_string, | 
|  | 492 | sizeof(data->input_string) - 1); | 
|  | 493 | if (!ret && (data->input_string[0] || data->input_string[1] || | 
|  | 494 | data->input_string[2])) | 
|  | 495 | data->input_length = sizeof(data->input_string) - 1; | 
|  | 496 | else if (ret < 0) { | 
|  | 497 | /* Input string is one byte longer for some devices */ | 
|  | 498 | ret = pem_read_block(client, PEM_READ_INPUT_STRING, | 
|  | 499 | data->input_string, | 
|  | 500 | sizeof(data->input_string)); | 
|  | 501 | if (!ret && (data->input_string[0] || data->input_string[1] || | 
|  | 502 | data->input_string[2] || data->input_string[3])) | 
|  | 503 | data->input_length = sizeof(data->input_string); | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | if (data->input_length) | 
|  | 507 | data->groups[idx++] = &pem_input_group; | 
|  | 508 |  | 
|  | 509 | /* | 
|  | 510 | * Check if fan speed readings are supported. | 
|  | 511 | * This is the case if we can read fan speed data, | 
|  | 512 | * and if the returned data is not all zeros. | 
|  | 513 | * Note that the fan alarm is always supported. | 
|  | 514 | */ | 
|  | 515 | ret = pem_read_block(client, PEM_READ_FAN_SPEED, | 
|  | 516 | data->fan_speed, | 
|  | 517 | sizeof(data->fan_speed)); | 
|  | 518 | if (!ret && (data->fan_speed[0] || data->fan_speed[1] || | 
|  | 519 | data->fan_speed[2] || data->fan_speed[3])) { | 
|  | 520 | data->fans_supported = true; | 
|  | 521 | data->groups[idx++] = &pem_fan_group; | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, | 
|  | 525 | data, data->groups); | 
|  | 526 | return PTR_ERR_OR_ZERO(hwmon_dev); | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | static const struct i2c_device_id pem_id[] = { | 
|  | 530 | {"lineage_pem", 0}, | 
|  | 531 | {} | 
|  | 532 | }; | 
|  | 533 | MODULE_DEVICE_TABLE(i2c, pem_id); | 
|  | 534 |  | 
|  | 535 | static struct i2c_driver pem_driver = { | 
|  | 536 | .driver = { | 
|  | 537 | .name = "lineage_pem", | 
|  | 538 | }, | 
|  | 539 | .probe = pem_probe, | 
|  | 540 | .id_table = pem_id, | 
|  | 541 | }; | 
|  | 542 |  | 
|  | 543 | module_i2c_driver(pem_driver); | 
|  | 544 |  | 
|  | 545 | MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); | 
|  | 546 | MODULE_DESCRIPTION("Lineage CPL PEM hardware monitoring driver"); | 
|  | 547 | MODULE_LICENSE("GPL"); |