17 #include <linux/kernel.h>
18 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/i2c.h>
32 #define MAX16065_ADC(x) ((x) * 2)
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)
47 #define MAX16065_SW_ENABLE 0x73
49 #define MAX16065_WARNING_OV (1 << 3)
52 #define MAX16065_CURR_ENABLE (1 << 0)
54 #define MAX16065_NUM_LIMIT 3
55 #define MAX16065_NUM_ADC 12
57 static const int max16065_num_adc[] = {
66 static const bool max16065_have_secondary[] = {
75 static const bool max16065_have_current[] = {
101 static const int max16065_adc_range[] = { 5560, 2780, 1390, 0 };
102 static const int max16065_csp_adc_range[] = { 7000, 14000 };
105 static inline int ADC_TO_MV(
int adc,
int range)
107 return (adc * range) / 1024;
114 static inline int LIMIT_TO_MV(
int limit,
int range)
116 return limit * range / 256;
119 static inline int MV_TO_LIMIT(
int mv,
int range)
124 static inline int ADC_TO_CURR(
int adc,
int gain)
126 return adc * 1400000 / (gain * 255);
139 rv = i2c_smbus_read_word_swapped(client, reg);
154 for (i = 0; i < data->
num_adc; i++)
187 val &= (1 << attr2->
index);
223 const char *buf,
size_t count)
232 err = kstrtoul(buf, 10, &val);
236 limit = MV_TO_LIMIT(val, data->
range[attr2->
index]);
240 = LIMIT_TO_MV(limit, data->
range[attr2->
index]);
279 max16065_set_limit, 2, 0);
281 max16065_set_limit, 2, 1);
283 max16065_set_limit, 2, 2);
285 max16065_set_limit, 2, 3);
287 max16065_set_limit, 2, 4);
289 max16065_set_limit, 2, 5);
291 max16065_set_limit, 2, 6);
293 max16065_set_limit, 2, 7);
295 max16065_set_limit, 2, 8);
297 max16065_set_limit, 2, 9);
299 max16065_set_limit, 2, 10);
301 max16065_set_limit, 2, 11);
305 max16065_set_limit, 1, 0);
307 max16065_set_limit, 1, 1);
309 max16065_set_limit, 1, 2);
311 max16065_set_limit, 1, 3);
313 max16065_set_limit, 1, 4);
315 max16065_set_limit, 1, 5);
317 max16065_set_limit, 1, 6);
319 max16065_set_limit, 1, 7);
321 max16065_set_limit, 1, 8);
323 max16065_set_limit, 1, 9);
325 max16065_set_limit, 1, 10);
327 max16065_set_limit, 1, 11);
331 max16065_set_limit, 0, 0);
333 max16065_set_limit, 0, 1);
335 max16065_set_limit, 0, 2);
337 max16065_set_limit, 0, 3);
339 max16065_set_limit, 0, 4);
341 max16065_set_limit, 0, 5);
343 max16065_set_limit, 0, 6);
345 max16065_set_limit, 0, 7);
347 max16065_set_limit, 0, 8);
349 max16065_set_limit, 0, 9);
351 max16065_set_limit, 0, 10);
353 max16065_set_limit, 0, 11);
357 max16065_set_limit, 0, 0);
359 max16065_set_limit, 0, 1);
361 max16065_set_limit, 0, 2);
363 max16065_set_limit, 0, 3);
365 max16065_set_limit, 0, 4);
367 max16065_set_limit, 0, 5);
369 max16065_set_limit, 0, 6);
371 max16065_set_limit, 0, 7);
373 max16065_set_limit, 0, 8);
375 max16065_set_limit, 0, 9);
377 max16065_set_limit, 0, 10);
379 max16065_set_limit, 0, 11);
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
520 .attrs = max16065_basic_attributes,
524 .attrs = max16065_current_attributes,
528 .attrs = max16065_min_attributes,
532 .attrs = max16065_max_attributes,
535 static void max16065_cleanup(
struct i2c_client *client)
543 static int max16065_probe(
struct i2c_client *client,
550 bool secondary_is_max =
false;
560 i2c_set_clientdata(client, data);
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];
567 if (have_secondary) {
579 for (j = 0; j < 4 && i * 4 + j < data->
num_adc; j++) {
581 max16065_adc_range[(val >> (j * 2)) & 0x3];
587 if (i == 0 && !have_secondary)
590 for (j = 0; j < data->
num_adc; j++) {
600 for (i = 0; i < data->
num_adc * 4; i++) {
602 if (!data->
range[i / 4])
606 max16065_basic_attributes[i]);
611 if (have_secondary) {
612 struct attribute **attr = secondary_is_max ?
613 max16065_max_attributes : max16065_min_attributes;
615 for (i = 0; i < data->
num_adc; i++) {
636 data->
curr_gain = 6 << ((val >> 2) & 0x03);
638 = max16065_csp_adc_range[(val >> 1) & 0x01];
640 &max16065_current_group);
656 max16065_cleanup(client);
660 static int max16065_remove(
struct i2c_client *client)
665 max16065_cleanup(client);
687 .probe = max16065_probe,
688 .remove = max16065_remove,
689 .id_table = max16065_id,