1 /* 2 * Driver for 3 * Maxim MAX16065/MAX16066 12-Channel/8-Channel, Flash-Configurable 4 * System Managers with Nonvolatile Fault Registers 5 * Maxim MAX16067/MAX16068 6-Channel, Flash-Configurable System Managers 6 * with Nonvolatile Fault Registers 7 * Maxim MAX16070/MAX16071 12-Channel/8-Channel, Flash-Configurable System 8 * Monitors with Nonvolatile Fault Registers 9 * 10 * Copyright (C) 2011 Ericsson AB. 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; version 2 of the License. 15 */ 16 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/init.h> 20 #include <linux/err.h> 21 #include <linux/slab.h> 22 #include <linux/i2c.h> 23 #include <linux/hwmon.h> 24 #include <linux/hwmon-sysfs.h> 25 #include <linux/jiffies.h> 26 27 enum chips { max16065, max16066, max16067, max16068, max16070, max16071 }; 28 29 /* 30 * Registers 31 */ 32 #define MAX16065_ADC(x) ((x) * 2) 33 34 #define MAX16065_CURR_SENSE 0x18 35 #define MAX16065_CSP_ADC 0x19 36 #define MAX16065_FAULT(x) (0x1b + (x)) 37 #define MAX16065_SCALE(x) (0x43 + (x)) 38 #define MAX16065_CURR_CONTROL 0x47 39 #define MAX16065_LIMIT(l, x) (0x48 + (l) + (x) * 3) /* 40 * l: limit 41 * 0: min/max 42 * 1: crit 43 * 2: lcrit 44 * x: ADC index 45 */ 46 47 #define MAX16065_SW_ENABLE 0x73 48 49 #define MAX16065_WARNING_OV (1 << 3) /* Set if secondary threshold is OV 50 warning */ 51 52 #define MAX16065_CURR_ENABLE (1 << 0) 53 54 #define MAX16065_NUM_LIMIT 3 55 #define MAX16065_NUM_ADC 12 /* maximum number of ADC channels */ 56 57 static const int max16065_num_adc[] = { 58 [max16065] = 12, 59 [max16066] = 8, 60 [max16067] = 6, 61 [max16068] = 6, 62 [max16070] = 12, 63 [max16071] = 8, 64 }; 65 66 static const bool max16065_have_secondary[] = { 67 [max16065] = true, 68 [max16066] = true, 69 [max16067] = false, 70 [max16068] = false, 71 [max16070] = true, 72 [max16071] = true, 73 }; 74 75 static const bool max16065_have_current[] = { 76 [max16065] = true, 77 [max16066] = true, 78 [max16067] = false, 79 [max16068] = false, 80 [max16070] = true, 81 [max16071] = true, 82 }; 83 84 struct max16065_data { 85 enum chips type; 86 struct device *hwmon_dev; 87 struct mutex update_lock; 88 bool valid; 89 unsigned long last_updated; /* in jiffies */ 90 int num_adc; 91 bool have_current; 92 int curr_gain; 93 /* limits are in mV */ 94 int limit[MAX16065_NUM_LIMIT][MAX16065_NUM_ADC]; 95 int range[MAX16065_NUM_ADC + 1];/* voltage range */ 96 int adc[MAX16065_NUM_ADC + 1]; /* adc values (raw) including csp_adc */ 97 int curr_sense; 98 int fault[2]; 99 }; 100 101 static const int max16065_adc_range[] = { 5560, 2780, 1390, 0 }; 102 static const int max16065_csp_adc_range[] = { 7000, 14000 }; 103 104 /* ADC registers have 10 bit resolution. */ 105 static inline int ADC_TO_MV(int adc, int range) 106 { 107 return (adc * range) / 1024; 108 } 109 110 /* 111 * Limit registers have 8 bit resolution and match upper 8 bits of ADC 112 * registers. 113 */ 114 static inline int LIMIT_TO_MV(int limit, int range) 115 { 116 return limit * range / 256; 117 } 118 119 static inline int MV_TO_LIMIT(int mv, int range) 120 { 121 return clamp_val(DIV_ROUND_CLOSEST(mv * 256, range), 0, 255); 122 } 123 124 static inline int ADC_TO_CURR(int adc, int gain) 125 { 126 return adc * 1400000 / (gain * 255); 127 } 128 129 /* 130 * max16065_read_adc() 131 * 132 * Read 16 bit value from <reg>, <reg+1>. 133 * Upper 8 bits are in <reg>, lower 2 bits are in bits 7:6 of <reg+1>. 134 */ 135 static int max16065_read_adc(struct i2c_client *client, int reg) 136 { 137 int rv; 138 139 rv = i2c_smbus_read_word_swapped(client, reg); 140 if (unlikely(rv < 0)) 141 return rv; 142 return rv >> 6; 143 } 144 145 static struct max16065_data *max16065_update_device(struct device *dev) 146 { 147 struct i2c_client *client = to_i2c_client(dev); 148 struct max16065_data *data = i2c_get_clientdata(client); 149 150 mutex_lock(&data->update_lock); 151 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { 152 int i; 153 154 for (i = 0; i < data->num_adc; i++) 155 data->adc[i] 156 = max16065_read_adc(client, MAX16065_ADC(i)); 157 158 if (data->have_current) { 159 data->adc[MAX16065_NUM_ADC] 160 = max16065_read_adc(client, MAX16065_CSP_ADC); 161 data->curr_sense 162 = i2c_smbus_read_byte_data(client, 163 MAX16065_CURR_SENSE); 164 } 165 166 for (i = 0; i < DIV_ROUND_UP(data->num_adc, 8); i++) 167 data->fault[i] 168 = i2c_smbus_read_byte_data(client, MAX16065_FAULT(i)); 169 170 data->last_updated = jiffies; 171 data->valid = 1; 172 } 173 mutex_unlock(&data->update_lock); 174 return data; 175 } 176 177 static ssize_t max16065_show_alarm(struct device *dev, 178 struct device_attribute *da, char *buf) 179 { 180 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da); 181 struct max16065_data *data = max16065_update_device(dev); 182 int val = data->fault[attr2->nr]; 183 184 if (val < 0) 185 return val; 186 187 val &= (1 << attr2->index); 188 if (val) 189 i2c_smbus_write_byte_data(to_i2c_client(dev), 190 MAX16065_FAULT(attr2->nr), val); 191 192 return snprintf(buf, PAGE_SIZE, "%d\n", !!val); 193 } 194 195 static ssize_t max16065_show_input(struct device *dev, 196 struct device_attribute *da, char *buf) 197 { 198 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 199 struct max16065_data *data = max16065_update_device(dev); 200 int adc = data->adc[attr->index]; 201 202 if (unlikely(adc < 0)) 203 return adc; 204 205 return snprintf(buf, PAGE_SIZE, "%d\n", 206 ADC_TO_MV(adc, data->range[attr->index])); 207 } 208 209 static ssize_t max16065_show_current(struct device *dev, 210 struct device_attribute *da, char *buf) 211 { 212 struct max16065_data *data = max16065_update_device(dev); 213 214 if (unlikely(data->curr_sense < 0)) 215 return data->curr_sense; 216 217 return snprintf(buf, PAGE_SIZE, "%d\n", 218 ADC_TO_CURR(data->curr_sense, data->curr_gain)); 219 } 220 221 static ssize_t max16065_set_limit(struct device *dev, 222 struct device_attribute *da, 223 const char *buf, size_t count) 224 { 225 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da); 226 struct i2c_client *client = to_i2c_client(dev); 227 struct max16065_data *data = i2c_get_clientdata(client); 228 unsigned long val; 229 int err; 230 int limit; 231 232 err = kstrtoul(buf, 10, &val); 233 if (unlikely(err < 0)) 234 return err; 235 236 limit = MV_TO_LIMIT(val, data->range[attr2->index]); 237 238 mutex_lock(&data->update_lock); 239 data->limit[attr2->nr][attr2->index] 240 = LIMIT_TO_MV(limit, data->range[attr2->index]); 241 i2c_smbus_write_byte_data(client, 242 MAX16065_LIMIT(attr2->nr, attr2->index), 243 limit); 244 mutex_unlock(&data->update_lock); 245 246 return count; 247 } 248 249 static ssize_t max16065_show_limit(struct device *dev, 250 struct device_attribute *da, char *buf) 251 { 252 struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(da); 253 struct i2c_client *client = to_i2c_client(dev); 254 struct max16065_data *data = i2c_get_clientdata(client); 255 256 return snprintf(buf, PAGE_SIZE, "%d\n", 257 data->limit[attr2->nr][attr2->index]); 258 } 259 260 /* Construct a sensor_device_attribute structure for each register */ 261 262 /* Input voltages */ 263 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, max16065_show_input, NULL, 0); 264 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, max16065_show_input, NULL, 1); 265 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, max16065_show_input, NULL, 2); 266 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, max16065_show_input, NULL, 3); 267 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, max16065_show_input, NULL, 4); 268 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, max16065_show_input, NULL, 5); 269 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, max16065_show_input, NULL, 6); 270 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, max16065_show_input, NULL, 7); 271 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, max16065_show_input, NULL, 8); 272 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, max16065_show_input, NULL, 9); 273 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, max16065_show_input, NULL, 10); 274 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, max16065_show_input, NULL, 11); 275 static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, max16065_show_input, NULL, 12); 276 277 /* Input voltages lcrit */ 278 static SENSOR_DEVICE_ATTR_2(in0_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 279 max16065_set_limit, 2, 0); 280 static SENSOR_DEVICE_ATTR_2(in1_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 281 max16065_set_limit, 2, 1); 282 static SENSOR_DEVICE_ATTR_2(in2_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 283 max16065_set_limit, 2, 2); 284 static SENSOR_DEVICE_ATTR_2(in3_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 285 max16065_set_limit, 2, 3); 286 static SENSOR_DEVICE_ATTR_2(in4_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 287 max16065_set_limit, 2, 4); 288 static SENSOR_DEVICE_ATTR_2(in5_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 289 max16065_set_limit, 2, 5); 290 static SENSOR_DEVICE_ATTR_2(in6_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 291 max16065_set_limit, 2, 6); 292 static SENSOR_DEVICE_ATTR_2(in7_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 293 max16065_set_limit, 2, 7); 294 static SENSOR_DEVICE_ATTR_2(in8_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 295 max16065_set_limit, 2, 8); 296 static SENSOR_DEVICE_ATTR_2(in9_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 297 max16065_set_limit, 2, 9); 298 static SENSOR_DEVICE_ATTR_2(in10_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 299 max16065_set_limit, 2, 10); 300 static SENSOR_DEVICE_ATTR_2(in11_lcrit, S_IWUSR | S_IRUGO, max16065_show_limit, 301 max16065_set_limit, 2, 11); 302 303 /* Input voltages crit */ 304 static SENSOR_DEVICE_ATTR_2(in0_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 305 max16065_set_limit, 1, 0); 306 static SENSOR_DEVICE_ATTR_2(in1_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 307 max16065_set_limit, 1, 1); 308 static SENSOR_DEVICE_ATTR_2(in2_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 309 max16065_set_limit, 1, 2); 310 static SENSOR_DEVICE_ATTR_2(in3_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 311 max16065_set_limit, 1, 3); 312 static SENSOR_DEVICE_ATTR_2(in4_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 313 max16065_set_limit, 1, 4); 314 static SENSOR_DEVICE_ATTR_2(in5_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 315 max16065_set_limit, 1, 5); 316 static SENSOR_DEVICE_ATTR_2(in6_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 317 max16065_set_limit, 1, 6); 318 static SENSOR_DEVICE_ATTR_2(in7_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 319 max16065_set_limit, 1, 7); 320 static SENSOR_DEVICE_ATTR_2(in8_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 321 max16065_set_limit, 1, 8); 322 static SENSOR_DEVICE_ATTR_2(in9_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 323 max16065_set_limit, 1, 9); 324 static SENSOR_DEVICE_ATTR_2(in10_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 325 max16065_set_limit, 1, 10); 326 static SENSOR_DEVICE_ATTR_2(in11_crit, S_IWUSR | S_IRUGO, max16065_show_limit, 327 max16065_set_limit, 1, 11); 328 329 /* Input voltages min */ 330 static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO, max16065_show_limit, 331 max16065_set_limit, 0, 0); 332 static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO, max16065_show_limit, 333 max16065_set_limit, 0, 1); 334 static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO, max16065_show_limit, 335 max16065_set_limit, 0, 2); 336 static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO, max16065_show_limit, 337 max16065_set_limit, 0, 3); 338 static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO, max16065_show_limit, 339 max16065_set_limit, 0, 4); 340 static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO, max16065_show_limit, 341 max16065_set_limit, 0, 5); 342 static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO, max16065_show_limit, 343 max16065_set_limit, 0, 6); 344 static SENSOR_DEVICE_ATTR_2(in7_min, S_IWUSR | S_IRUGO, max16065_show_limit, 345 max16065_set_limit, 0, 7); 346 static SENSOR_DEVICE_ATTR_2(in8_min, S_IWUSR | S_IRUGO, max16065_show_limit, 347 max16065_set_limit, 0, 8); 348 static SENSOR_DEVICE_ATTR_2(in9_min, S_IWUSR | S_IRUGO, max16065_show_limit, 349 max16065_set_limit, 0, 9); 350 static SENSOR_DEVICE_ATTR_2(in10_min, S_IWUSR | S_IRUGO, max16065_show_limit, 351 max16065_set_limit, 0, 10); 352 static SENSOR_DEVICE_ATTR_2(in11_min, S_IWUSR | S_IRUGO, max16065_show_limit, 353 max16065_set_limit, 0, 11); 354 355 /* Input voltages max */ 356 static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO, max16065_show_limit, 357 max16065_set_limit, 0, 0); 358 static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO, max16065_show_limit, 359 max16065_set_limit, 0, 1); 360 static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO, max16065_show_limit, 361 max16065_set_limit, 0, 2); 362 static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO, max16065_show_limit, 363 max16065_set_limit, 0, 3); 364 static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO, max16065_show_limit, 365 max16065_set_limit, 0, 4); 366 static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO, max16065_show_limit, 367 max16065_set_limit, 0, 5); 368 static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO, max16065_show_limit, 369 max16065_set_limit, 0, 6); 370 static SENSOR_DEVICE_ATTR_2(in7_max, S_IWUSR | S_IRUGO, max16065_show_limit, 371 max16065_set_limit, 0, 7); 372 static SENSOR_DEVICE_ATTR_2(in8_max, S_IWUSR | S_IRUGO, max16065_show_limit, 373 max16065_set_limit, 0, 8); 374 static SENSOR_DEVICE_ATTR_2(in9_max, S_IWUSR | S_IRUGO, max16065_show_limit, 375 max16065_set_limit, 0, 9); 376 static SENSOR_DEVICE_ATTR_2(in10_max, S_IWUSR | S_IRUGO, max16065_show_limit, 377 max16065_set_limit, 0, 10); 378 static SENSOR_DEVICE_ATTR_2(in11_max, S_IWUSR | S_IRUGO, max16065_show_limit, 379 max16065_set_limit, 0, 11); 380 381 /* alarms */ 382 static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, max16065_show_alarm, NULL, 383 0, 0); 384 static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, max16065_show_alarm, NULL, 385 0, 1); 386 static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, max16065_show_alarm, NULL, 387 0, 2); 388 static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, max16065_show_alarm, NULL, 389 0, 3); 390 static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, max16065_show_alarm, NULL, 391 0, 4); 392 static SENSOR_DEVICE_ATTR_2(in5_alarm, S_IRUGO, max16065_show_alarm, NULL, 393 0, 5); 394 static SENSOR_DEVICE_ATTR_2(in6_alarm, S_IRUGO, max16065_show_alarm, NULL, 395 0, 6); 396 static SENSOR_DEVICE_ATTR_2(in7_alarm, S_IRUGO, max16065_show_alarm, NULL, 397 0, 7); 398 static SENSOR_DEVICE_ATTR_2(in8_alarm, S_IRUGO, max16065_show_alarm, NULL, 399 1, 0); 400 static SENSOR_DEVICE_ATTR_2(in9_alarm, S_IRUGO, max16065_show_alarm, NULL, 401 1, 1); 402 static SENSOR_DEVICE_ATTR_2(in10_alarm, S_IRUGO, max16065_show_alarm, NULL, 403 1, 2); 404 static SENSOR_DEVICE_ATTR_2(in11_alarm, S_IRUGO, max16065_show_alarm, NULL, 405 1, 3); 406 407 /* Current and alarm */ 408 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, max16065_show_current, NULL, 0); 409 static SENSOR_DEVICE_ATTR_2(curr1_alarm, S_IRUGO, max16065_show_alarm, NULL, 410 1, 4); 411 412 /* 413 * Finally, construct an array of pointers to members of the above objects, 414 * as required for sysfs_create_group() 415 */ 416 static struct attribute *max16065_basic_attributes[] = { 417 &sensor_dev_attr_in0_input.dev_attr.attr, 418 &sensor_dev_attr_in0_lcrit.dev_attr.attr, 419 &sensor_dev_attr_in0_crit.dev_attr.attr, 420 &sensor_dev_attr_in0_alarm.dev_attr.attr, 421 422 &sensor_dev_attr_in1_input.dev_attr.attr, 423 &sensor_dev_attr_in1_lcrit.dev_attr.attr, 424 &sensor_dev_attr_in1_crit.dev_attr.attr, 425 &sensor_dev_attr_in1_alarm.dev_attr.attr, 426 427 &sensor_dev_attr_in2_input.dev_attr.attr, 428 &sensor_dev_attr_in2_lcrit.dev_attr.attr, 429 &sensor_dev_attr_in2_crit.dev_attr.attr, 430 &sensor_dev_attr_in2_alarm.dev_attr.attr, 431 432 &sensor_dev_attr_in3_input.dev_attr.attr, 433 &sensor_dev_attr_in3_lcrit.dev_attr.attr, 434 &sensor_dev_attr_in3_crit.dev_attr.attr, 435 &sensor_dev_attr_in3_alarm.dev_attr.attr, 436 437 &sensor_dev_attr_in4_input.dev_attr.attr, 438 &sensor_dev_attr_in4_lcrit.dev_attr.attr, 439 &sensor_dev_attr_in4_crit.dev_attr.attr, 440 &sensor_dev_attr_in4_alarm.dev_attr.attr, 441 442 &sensor_dev_attr_in5_input.dev_attr.attr, 443 &sensor_dev_attr_in5_lcrit.dev_attr.attr, 444 &sensor_dev_attr_in5_crit.dev_attr.attr, 445 &sensor_dev_attr_in5_alarm.dev_attr.attr, 446 447 &sensor_dev_attr_in6_input.dev_attr.attr, 448 &sensor_dev_attr_in6_lcrit.dev_attr.attr, 449 &sensor_dev_attr_in6_crit.dev_attr.attr, 450 &sensor_dev_attr_in6_alarm.dev_attr.attr, 451 452 &sensor_dev_attr_in7_input.dev_attr.attr, 453 &sensor_dev_attr_in7_lcrit.dev_attr.attr, 454 &sensor_dev_attr_in7_crit.dev_attr.attr, 455 &sensor_dev_attr_in7_alarm.dev_attr.attr, 456 457 &sensor_dev_attr_in8_input.dev_attr.attr, 458 &sensor_dev_attr_in8_lcrit.dev_attr.attr, 459 &sensor_dev_attr_in8_crit.dev_attr.attr, 460 &sensor_dev_attr_in8_alarm.dev_attr.attr, 461 462 &sensor_dev_attr_in9_input.dev_attr.attr, 463 &sensor_dev_attr_in9_lcrit.dev_attr.attr, 464 &sensor_dev_attr_in9_crit.dev_attr.attr, 465 &sensor_dev_attr_in9_alarm.dev_attr.attr, 466 467 &sensor_dev_attr_in10_input.dev_attr.attr, 468 &sensor_dev_attr_in10_lcrit.dev_attr.attr, 469 &sensor_dev_attr_in10_crit.dev_attr.attr, 470 &sensor_dev_attr_in10_alarm.dev_attr.attr, 471 472 &sensor_dev_attr_in11_input.dev_attr.attr, 473 &sensor_dev_attr_in11_lcrit.dev_attr.attr, 474 &sensor_dev_attr_in11_crit.dev_attr.attr, 475 &sensor_dev_attr_in11_alarm.dev_attr.attr, 476 477 NULL 478 }; 479 480 static struct attribute *max16065_current_attributes[] = { 481 &sensor_dev_attr_in12_input.dev_attr.attr, 482 &sensor_dev_attr_curr1_input.dev_attr.attr, 483 &sensor_dev_attr_curr1_alarm.dev_attr.attr, 484 NULL 485 }; 486 487 static struct attribute *max16065_min_attributes[] = { 488 &sensor_dev_attr_in0_min.dev_attr.attr, 489 &sensor_dev_attr_in1_min.dev_attr.attr, 490 &sensor_dev_attr_in2_min.dev_attr.attr, 491 &sensor_dev_attr_in3_min.dev_attr.attr, 492 &sensor_dev_attr_in4_min.dev_attr.attr, 493 &sensor_dev_attr_in5_min.dev_attr.attr, 494 &sensor_dev_attr_in6_min.dev_attr.attr, 495 &sensor_dev_attr_in7_min.dev_attr.attr, 496 &sensor_dev_attr_in8_min.dev_attr.attr, 497 &sensor_dev_attr_in9_min.dev_attr.attr, 498 &sensor_dev_attr_in10_min.dev_attr.attr, 499 &sensor_dev_attr_in11_min.dev_attr.attr, 500 NULL 501 }; 502 503 static struct attribute *max16065_max_attributes[] = { 504 &sensor_dev_attr_in0_max.dev_attr.attr, 505 &sensor_dev_attr_in1_max.dev_attr.attr, 506 &sensor_dev_attr_in2_max.dev_attr.attr, 507 &sensor_dev_attr_in3_max.dev_attr.attr, 508 &sensor_dev_attr_in4_max.dev_attr.attr, 509 &sensor_dev_attr_in5_max.dev_attr.attr, 510 &sensor_dev_attr_in6_max.dev_attr.attr, 511 &sensor_dev_attr_in7_max.dev_attr.attr, 512 &sensor_dev_attr_in8_max.dev_attr.attr, 513 &sensor_dev_attr_in9_max.dev_attr.attr, 514 &sensor_dev_attr_in10_max.dev_attr.attr, 515 &sensor_dev_attr_in11_max.dev_attr.attr, 516 NULL 517 }; 518 519 static const struct attribute_group max16065_basic_group = { 520 .attrs = max16065_basic_attributes, 521 }; 522 523 static const struct attribute_group max16065_current_group = { 524 .attrs = max16065_current_attributes, 525 }; 526 527 static const struct attribute_group max16065_min_group = { 528 .attrs = max16065_min_attributes, 529 }; 530 531 static const struct attribute_group max16065_max_group = { 532 .attrs = max16065_max_attributes, 533 }; 534 535 static void max16065_cleanup(struct i2c_client *client) 536 { 537 sysfs_remove_group(&client->dev.kobj, &max16065_max_group); 538 sysfs_remove_group(&client->dev.kobj, &max16065_min_group); 539 sysfs_remove_group(&client->dev.kobj, &max16065_current_group); 540 sysfs_remove_group(&client->dev.kobj, &max16065_basic_group); 541 } 542 543 static int max16065_probe(struct i2c_client *client, 544 const struct i2c_device_id *id) 545 { 546 struct i2c_adapter *adapter = client->adapter; 547 struct max16065_data *data; 548 int i, j, val, ret; 549 bool have_secondary; /* true if chip has secondary limits */ 550 bool secondary_is_max = false; /* secondary limits reflect max */ 551 552 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA 553 | I2C_FUNC_SMBUS_READ_WORD_DATA)) 554 return -ENODEV; 555 556 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 557 if (unlikely(!data)) 558 return -ENOMEM; 559 560 i2c_set_clientdata(client, data); 561 mutex_init(&data->update_lock); 562 563 data->num_adc = max16065_num_adc[id->driver_data]; 564 data->have_current = max16065_have_current[id->driver_data]; 565 have_secondary = max16065_have_secondary[id->driver_data]; 566 567 if (have_secondary) { 568 val = i2c_smbus_read_byte_data(client, MAX16065_SW_ENABLE); 569 if (unlikely(val < 0)) 570 return val; 571 secondary_is_max = val & MAX16065_WARNING_OV; 572 } 573 574 /* Read scale registers, convert to range */ 575 for (i = 0; i < DIV_ROUND_UP(data->num_adc, 4); i++) { 576 val = i2c_smbus_read_byte_data(client, MAX16065_SCALE(i)); 577 if (unlikely(val < 0)) 578 return val; 579 for (j = 0; j < 4 && i * 4 + j < data->num_adc; j++) { 580 data->range[i * 4 + j] = 581 max16065_adc_range[(val >> (j * 2)) & 0x3]; 582 } 583 } 584 585 /* Read limits */ 586 for (i = 0; i < MAX16065_NUM_LIMIT; i++) { 587 if (i == 0 && !have_secondary) 588 continue; 589 590 for (j = 0; j < data->num_adc; j++) { 591 val = i2c_smbus_read_byte_data(client, 592 MAX16065_LIMIT(i, j)); 593 if (unlikely(val < 0)) 594 return val; 595 data->limit[i][j] = LIMIT_TO_MV(val, data->range[j]); 596 } 597 } 598 599 /* Register sysfs hooks */ 600 for (i = 0; i < data->num_adc * 4; i++) { 601 /* Do not create sysfs entry if channel is disabled */ 602 if (!data->range[i / 4]) 603 continue; 604 605 ret = sysfs_create_file(&client->dev.kobj, 606 max16065_basic_attributes[i]); 607 if (unlikely(ret)) 608 goto out; 609 } 610 611 if (have_secondary) { 612 struct attribute **attr = secondary_is_max ? 613 max16065_max_attributes : max16065_min_attributes; 614 615 for (i = 0; i < data->num_adc; i++) { 616 if (!data->range[i]) 617 continue; 618 619 ret = sysfs_create_file(&client->dev.kobj, attr[i]); 620 if (unlikely(ret)) 621 goto out; 622 } 623 } 624 625 if (data->have_current) { 626 val = i2c_smbus_read_byte_data(client, MAX16065_CURR_CONTROL); 627 if (unlikely(val < 0)) { 628 ret = val; 629 goto out; 630 } 631 if (val & MAX16065_CURR_ENABLE) { 632 /* 633 * Current gain is 6, 12, 24, 48 based on values in 634 * bit 2,3. 635 */ 636 data->curr_gain = 6 << ((val >> 2) & 0x03); 637 data->range[MAX16065_NUM_ADC] 638 = max16065_csp_adc_range[(val >> 1) & 0x01]; 639 ret = sysfs_create_group(&client->dev.kobj, 640 &max16065_current_group); 641 if (unlikely(ret)) 642 goto out; 643 } else { 644 data->have_current = false; 645 } 646 } 647 648 data->hwmon_dev = hwmon_device_register(&client->dev); 649 if (unlikely(IS_ERR(data->hwmon_dev))) { 650 ret = PTR_ERR(data->hwmon_dev); 651 goto out; 652 } 653 return 0; 654 655 out: 656 max16065_cleanup(client); 657 return ret; 658 } 659 660 static int max16065_remove(struct i2c_client *client) 661 { 662 struct max16065_data *data = i2c_get_clientdata(client); 663 664 hwmon_device_unregister(data->hwmon_dev); 665 max16065_cleanup(client); 666 667 return 0; 668 } 669 670 static const struct i2c_device_id max16065_id[] = { 671 { "max16065", max16065 }, 672 { "max16066", max16066 }, 673 { "max16067", max16067 }, 674 { "max16068", max16068 }, 675 { "max16070", max16070 }, 676 { "max16071", max16071 }, 677 { } 678 }; 679 680 MODULE_DEVICE_TABLE(i2c, max16065_id); 681 682 /* This is the driver that will be inserted */ 683 static struct i2c_driver max16065_driver = { 684 .driver = { 685 .name = "max16065", 686 }, 687 .probe = max16065_probe, 688 .remove = max16065_remove, 689 .id_table = max16065_id, 690 }; 691 692 module_i2c_driver(max16065_driver); 693 694 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 695 MODULE_DESCRIPTION("MAX16065 driver"); 696 MODULE_LICENSE("GPL"); 697