2 * Hardware monitoring driver for Maxim MAX6621
4 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2017 Vadim Pasternak <vadimp@mellanox.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/bitops.h>
19 #include <linux/hwmon.h>
20 #include <linux/hwmon-sysfs.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/regmap.h>
27 #define MAX6621_DRV_NAME "max6621"
28 #define MAX6621_TEMP_INPUT_REG_NUM 9
29 #define MAX6621_TEMP_INPUT_MIN -127000
30 #define MAX6621_TEMP_INPUT_MAX 128000
31 #define MAX6621_TEMP_ALERT_CHAN_SHIFT 1
33 #define MAX6621_TEMP_S0D0_REG 0x00
34 #define MAX6621_TEMP_S0D1_REG 0x01
35 #define MAX6621_TEMP_S1D0_REG 0x02
36 #define MAX6621_TEMP_S1D1_REG 0x03
37 #define MAX6621_TEMP_S2D0_REG 0x04
38 #define MAX6621_TEMP_S2D1_REG 0x05
39 #define MAX6621_TEMP_S3D0_REG 0x06
40 #define MAX6621_TEMP_S3D1_REG 0x07
41 #define MAX6621_TEMP_MAX_REG 0x08
42 #define MAX6621_TEMP_MAX_ADDR_REG 0x0a
43 #define MAX6621_TEMP_ALERT_CAUSE_REG 0x0b
44 #define MAX6621_CONFIG0_REG 0x0c
45 #define MAX6621_CONFIG1_REG 0x0d
46 #define MAX6621_CONFIG2_REG 0x0e
47 #define MAX6621_CONFIG3_REG 0x0f
48 #define MAX6621_TEMP_S0_ALERT_REG 0x10
49 #define MAX6621_TEMP_S1_ALERT_REG 0x11
50 #define MAX6621_TEMP_S2_ALERT_REG 0x12
51 #define MAX6621_TEMP_S3_ALERT_REG 0x13
52 #define MAX6621_CLEAR_ALERT_REG 0x15
53 #define MAX6621_REG_MAX (MAX6621_CLEAR_ALERT_REG + 1)
54 #define MAX6621_REG_TEMP_SHIFT 0x06
56 #define MAX6621_ENABLE_TEMP_ALERTS_BIT 4
57 #define MAX6621_ENABLE_I2C_CRC_BIT 5
58 #define MAX6621_ENABLE_ALTERNATE_DATA 6
59 #define MAX6621_ENABLE_LOCKUP_TO 7
60 #define MAX6621_ENABLE_S0D0_BIT 8
61 #define MAX6621_ENABLE_S3D1_BIT 15
62 #define MAX6621_ENABLE_TEMP_ALL GENMASK(MAX6621_ENABLE_S3D1_BIT, \
63 MAX6621_ENABLE_S0D0_BIT)
64 #define MAX6621_POLL_DELAY_MASK 0x5
65 #define MAX6621_CONFIG0_INIT (MAX6621_ENABLE_TEMP_ALL | \
66 BIT(MAX6621_ENABLE_LOCKUP_TO) | \
67 BIT(MAX6621_ENABLE_I2C_CRC_BIT) | \
68 MAX6621_POLL_DELAY_MASK)
69 #define MAX6621_PECI_BIT_TIME 0x2
70 #define MAX6621_PECI_RETRY_NUM 0x3
71 #define MAX6621_CONFIG1_INIT ((MAX6621_PECI_BIT_TIME << 8) | \
72 MAX6621_PECI_RETRY_NUM)
75 #define MAX6621_TRAN_FAILED 0x8100 /*
76 * PECI transaction failed for more
77 * than the configured number of
78 * consecutive retries.
80 #define MAX6621_POOL_DIS 0x8101 /*
81 * Polling disabled for requested
84 #define MAX6621_POOL_UNCOMPLETE 0x8102 /*
85 * First poll not yet completed for
86 * requested socket/domain (on
89 #define MAX6621_SD_DIS 0x8103 /*
90 * Read maximum temperature requested,
91 * but no sockets/domains enabled or
92 * all enabled sockets/domains have
93 * errors; or read maximum temperature
94 * address requested, but read maximum
95 * temperature was not called.
97 #define MAX6621_ALERT_DIS 0x8104 /*
98 * Get alert socket/domain requested,
99 * but no alert active.
101 #define MAX6621_PECI_ERR_MIN 0x8000 /* Intel spec PECI error min value. */
102 #define MAX6621_PECI_ERR_MAX 0x80ff /* Intel spec PECI error max value. */
104 static const u32 max6621_temp_regs
[] = {
105 MAX6621_TEMP_MAX_REG
, MAX6621_TEMP_S0D0_REG
, MAX6621_TEMP_S1D0_REG
,
106 MAX6621_TEMP_S2D0_REG
, MAX6621_TEMP_S3D0_REG
, MAX6621_TEMP_S0D1_REG
,
107 MAX6621_TEMP_S1D1_REG
, MAX6621_TEMP_S2D1_REG
, MAX6621_TEMP_S3D1_REG
,
110 static const char *const max6621_temp_labels
[] = {
122 static const int max6621_temp_alert_chan2reg
[] = {
123 MAX6621_TEMP_S0_ALERT_REG
,
124 MAX6621_TEMP_S1_ALERT_REG
,
125 MAX6621_TEMP_S2_ALERT_REG
,
126 MAX6621_TEMP_S3_ALERT_REG
,
130 * struct max6621_data - private data:
132 * @client: I2C client;
133 * @regmap: register map handle;
134 * @input_chan2reg: mapping from channel to register;
136 struct max6621_data
{
137 struct i2c_client
*client
;
138 struct regmap
*regmap
;
139 int input_chan2reg
[MAX6621_TEMP_INPUT_REG_NUM
+ 1];
142 static long max6621_temp_mc2reg(long val
)
144 return (val
/ 1000L) << MAX6621_REG_TEMP_SHIFT
;
148 max6621_is_visible(const void *data
, enum hwmon_sensor_types type
, u32 attr
,
151 /* Skip channels which are not physically conncted. */
152 if (((struct max6621_data
*)data
)->input_chan2reg
[channel
] < 0)
158 case hwmon_temp_input
:
159 case hwmon_temp_label
:
160 case hwmon_temp_crit_alarm
:
162 case hwmon_temp_offset
:
163 case hwmon_temp_crit
:
176 static int max6621_verify_reg_data(struct device
*dev
, int regval
)
178 if (regval
>= MAX6621_PECI_ERR_MIN
&&
179 regval
<= MAX6621_PECI_ERR_MAX
) {
180 dev_dbg(dev
, "PECI error code - err 0x%04x.\n",
187 case MAX6621_TRAN_FAILED
:
188 dev_dbg(dev
, "PECI transaction failed - err 0x%04x.\n",
191 case MAX6621_POOL_DIS
:
192 dev_dbg(dev
, "Polling disabled - err 0x%04x.\n", regval
);
194 case MAX6621_POOL_UNCOMPLETE
:
195 dev_dbg(dev
, "First poll not completed on startup - err 0x%04x.\n",
199 dev_dbg(dev
, "Resource is disabled - err 0x%04x.\n", regval
);
201 case MAX6621_ALERT_DIS
:
202 dev_dbg(dev
, "No alert active - err 0x%04x.\n", regval
);
210 max6621_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
211 int channel
, long *val
)
213 struct max6621_data
*data
= dev_get_drvdata(dev
);
222 case hwmon_temp_input
:
223 reg
= data
->input_chan2reg
[channel
];
224 ret
= regmap_read(data
->regmap
, reg
, ®val
);
228 ret
= max6621_verify_reg_data(dev
, regval
);
233 * Bit MAX6621_REG_TEMP_SHIFT represents 1 degree step.
234 * The temperature is given in two's complement and 8
235 * bits is used for the register conversion.
237 temp
= (regval
>> MAX6621_REG_TEMP_SHIFT
);
241 case hwmon_temp_offset
:
242 ret
= regmap_read(data
->regmap
, MAX6621_CONFIG2_REG
,
247 ret
= max6621_verify_reg_data(dev
, regval
);
251 *val
= (regval
>> MAX6621_REG_TEMP_SHIFT
) *
255 case hwmon_temp_crit
:
256 channel
-= MAX6621_TEMP_ALERT_CHAN_SHIFT
;
257 reg
= max6621_temp_alert_chan2reg
[channel
];
258 ret
= regmap_read(data
->regmap
, reg
, ®val
);
262 ret
= max6621_verify_reg_data(dev
, regval
);
266 *val
= regval
* 1000L;
269 case hwmon_temp_crit_alarm
:
271 * Set val to zero to recover the case, when reading
272 * MAX6621_TEMP_ALERT_CAUSE_REG results in for example
273 * MAX6621_ALERT_DIS. Reading will return with error,
274 * but in such case alarm should be returned as 0.
277 ret
= regmap_read(data
->regmap
,
278 MAX6621_TEMP_ALERT_CAUSE_REG
,
283 ret
= max6621_verify_reg_data(dev
, regval
);
285 /* Do not report error if alert is disabled. */
286 if (regval
== MAX6621_ALERT_DIS
)
293 * Clear the alert automatically, using send-byte
294 * smbus protocol for clearing alert.
297 ret
= i2c_smbus_write_byte(data
->client
,
298 MAX6621_CLEAR_ALERT_REG
);
319 max6621_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
320 int channel
, long val
)
322 struct max6621_data
*data
= dev_get_drvdata(dev
);
328 case hwmon_temp_offset
:
329 /* Clamp to allowed range to prevent overflow. */
330 val
= clamp_val(val
, MAX6621_TEMP_INPUT_MIN
,
331 MAX6621_TEMP_INPUT_MAX
);
332 val
= max6621_temp_mc2reg(val
);
334 return regmap_write(data
->regmap
,
335 MAX6621_CONFIG2_REG
, val
);
336 case hwmon_temp_crit
:
337 channel
-= MAX6621_TEMP_ALERT_CHAN_SHIFT
;
338 reg
= max6621_temp_alert_chan2reg
[channel
];
339 /* Clamp to allowed range to prevent overflow. */
340 val
= clamp_val(val
, MAX6621_TEMP_INPUT_MIN
,
341 MAX6621_TEMP_INPUT_MAX
);
344 return regmap_write(data
->regmap
, reg
, val
);
358 max6621_read_string(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
359 int channel
, const char **str
)
364 case hwmon_temp_label
:
365 *str
= max6621_temp_labels
[channel
];
378 static bool max6621_writeable_reg(struct device
*dev
, unsigned int reg
)
381 case MAX6621_CONFIG0_REG
:
382 case MAX6621_CONFIG1_REG
:
383 case MAX6621_CONFIG2_REG
:
384 case MAX6621_CONFIG3_REG
:
385 case MAX6621_TEMP_S0_ALERT_REG
:
386 case MAX6621_TEMP_S1_ALERT_REG
:
387 case MAX6621_TEMP_S2_ALERT_REG
:
388 case MAX6621_TEMP_S3_ALERT_REG
:
389 case MAX6621_TEMP_ALERT_CAUSE_REG
:
395 static bool max6621_readable_reg(struct device
*dev
, unsigned int reg
)
398 case MAX6621_TEMP_S0D0_REG
:
399 case MAX6621_TEMP_S0D1_REG
:
400 case MAX6621_TEMP_S1D0_REG
:
401 case MAX6621_TEMP_S1D1_REG
:
402 case MAX6621_TEMP_S2D0_REG
:
403 case MAX6621_TEMP_S2D1_REG
:
404 case MAX6621_TEMP_S3D0_REG
:
405 case MAX6621_TEMP_S3D1_REG
:
406 case MAX6621_TEMP_MAX_REG
:
407 case MAX6621_TEMP_MAX_ADDR_REG
:
408 case MAX6621_CONFIG0_REG
:
409 case MAX6621_CONFIG1_REG
:
410 case MAX6621_CONFIG2_REG
:
411 case MAX6621_CONFIG3_REG
:
412 case MAX6621_TEMP_S0_ALERT_REG
:
413 case MAX6621_TEMP_S1_ALERT_REG
:
414 case MAX6621_TEMP_S2_ALERT_REG
:
415 case MAX6621_TEMP_S3_ALERT_REG
:
421 static bool max6621_volatile_reg(struct device
*dev
, unsigned int reg
)
424 case MAX6621_TEMP_S0D0_REG
:
425 case MAX6621_TEMP_S0D1_REG
:
426 case MAX6621_TEMP_S1D0_REG
:
427 case MAX6621_TEMP_S1D1_REG
:
428 case MAX6621_TEMP_S2D0_REG
:
429 case MAX6621_TEMP_S2D1_REG
:
430 case MAX6621_TEMP_S3D0_REG
:
431 case MAX6621_TEMP_S3D1_REG
:
432 case MAX6621_TEMP_MAX_REG
:
433 case MAX6621_TEMP_S0_ALERT_REG
:
434 case MAX6621_TEMP_S1_ALERT_REG
:
435 case MAX6621_TEMP_S2_ALERT_REG
:
436 case MAX6621_TEMP_S3_ALERT_REG
:
437 case MAX6621_TEMP_ALERT_CAUSE_REG
:
443 static const struct reg_default max6621_regmap_default
[] = {
444 { MAX6621_CONFIG0_REG
, MAX6621_CONFIG0_INIT
},
445 { MAX6621_CONFIG1_REG
, MAX6621_CONFIG1_INIT
},
448 static const struct regmap_config max6621_regmap_config
= {
451 .max_register
= MAX6621_REG_MAX
,
452 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
453 .cache_type
= REGCACHE_FLAT
,
454 .writeable_reg
= max6621_writeable_reg
,
455 .readable_reg
= max6621_readable_reg
,
456 .volatile_reg
= max6621_volatile_reg
,
457 .reg_defaults
= max6621_regmap_default
,
458 .num_reg_defaults
= ARRAY_SIZE(max6621_regmap_default
),
461 static u32 max6621_chip_config
[] = {
466 static const struct hwmon_channel_info max6621_chip
= {
468 .config
= max6621_chip_config
,
471 static const u32 max6621_temp_config
[] = {
472 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_OFFSET
,
473 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_LABEL
,
474 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_LABEL
,
475 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_LABEL
,
476 HWMON_T_INPUT
| HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
| HWMON_T_LABEL
,
477 HWMON_T_INPUT
| HWMON_T_LABEL
,
478 HWMON_T_INPUT
| HWMON_T_LABEL
,
479 HWMON_T_INPUT
| HWMON_T_LABEL
,
480 HWMON_T_INPUT
| HWMON_T_LABEL
,
484 static const struct hwmon_channel_info max6621_temp
= {
486 .config
= max6621_temp_config
,
489 static const struct hwmon_channel_info
*max6621_info
[] = {
495 static const struct hwmon_ops max6621_hwmon_ops
= {
496 .read
= max6621_read
,
497 .write
= max6621_write
,
498 .read_string
= max6621_read_string
,
499 .is_visible
= max6621_is_visible
,
502 static const struct hwmon_chip_info max6621_chip_info
= {
503 .ops
= &max6621_hwmon_ops
,
504 .info
= max6621_info
,
507 static int max6621_probe(struct i2c_client
*client
,
508 const struct i2c_device_id
*id
)
510 struct device
*dev
= &client
->dev
;
511 struct max6621_data
*data
;
512 struct device
*hwmon_dev
;
516 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
520 data
->regmap
= devm_regmap_init_i2c(client
, &max6621_regmap_config
);
521 if (IS_ERR(data
->regmap
))
522 return PTR_ERR(data
->regmap
);
524 i2c_set_clientdata(client
, data
);
525 data
->client
= client
;
527 /* Set CONFIG0 register masking temperature alerts and PEC. */
528 ret
= regmap_write(data
->regmap
, MAX6621_CONFIG0_REG
,
529 MAX6621_CONFIG0_INIT
);
533 /* Set CONFIG1 register for PEC access retry number. */
534 ret
= regmap_write(data
->regmap
, MAX6621_CONFIG1_REG
,
535 MAX6621_CONFIG1_INIT
);
539 /* Sync registers with hardware. */
540 regcache_mark_dirty(data
->regmap
);
541 ret
= regcache_sync(data
->regmap
);
545 /* Verify which temperature input registers are enabled. */
546 for (i
= 0; i
< MAX6621_TEMP_INPUT_REG_NUM
; i
++) {
547 ret
= i2c_smbus_read_word_data(client
, max6621_temp_regs
[i
]);
550 ret
= max6621_verify_reg_data(dev
, ret
);
552 data
->input_chan2reg
[i
] = -1;
556 data
->input_chan2reg
[i
] = max6621_temp_regs
[i
];
559 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
564 return PTR_ERR_OR_ZERO(hwmon_dev
);
567 static const struct i2c_device_id max6621_id
[] = {
568 { MAX6621_DRV_NAME
, 0 },
571 MODULE_DEVICE_TABLE(i2c
, max6621_id
);
573 static const struct of_device_id max6621_of_match
[] = {
574 { .compatible
= "maxim,max6621" },
577 MODULE_DEVICE_TABLE(of
, max6621_of_match
);
579 static struct i2c_driver max6621_driver
= {
580 .class = I2C_CLASS_HWMON
,
582 .name
= MAX6621_DRV_NAME
,
583 .of_match_table
= of_match_ptr(max6621_of_match
),
585 .probe
= max6621_probe
,
586 .id_table
= max6621_id
,
589 module_i2c_driver(max6621_driver
);
591 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
592 MODULE_DESCRIPTION("Driver for Maxim MAX6621");
593 MODULE_LICENSE("GPL");