1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Jedec 5118 compliant temperature sensors
5 * Derived from https://github.com/Steve-Tech/SPD5118-DKMS
6 * Originally from T/2 driver at https://t2sde.org/packages/linux
7 * Copyright (c) 2023 René Rebe, ExactCODE GmbH; Germany.
9 * Copyright (c) 2024 Guenter Roeck
11 * Inspired by ee1004.c and jc42.c.
13 * SPD5118 compliant temperature sensors are typically used on DDR5
17 #include <linux/bitops.h>
18 #include <linux/bits.h>
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/hwmon.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/nvmem-provider.h>
26 #include <linux/regmap.h>
27 #include <linux/units.h>
29 /* Addresses to scan */
30 static const unsigned short normal_i2c
[] = {
31 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, I2C_CLIENT_END
};
33 /* SPD5118 registers. */
34 #define SPD5118_REG_TYPE 0x00 /* MR0:MR1 */
35 #define SPD5118_REG_REVISION 0x02 /* MR2 */
36 #define SPD5118_REG_VENDOR 0x03 /* MR3:MR4 */
37 #define SPD5118_REG_CAPABILITY 0x05 /* MR5 */
38 #define SPD5118_REG_I2C_LEGACY_MODE 0x0B /* MR11 */
39 #define SPD5118_REG_TEMP_CLR 0x13 /* MR19 */
40 #define SPD5118_REG_ERROR_CLR 0x14 /* MR20 */
41 #define SPD5118_REG_TEMP_CONFIG 0x1A /* MR26 */
42 #define SPD5118_REG_TEMP_MAX 0x1c /* MR28:MR29 */
43 #define SPD5118_REG_TEMP_MIN 0x1e /* MR30:MR31 */
44 #define SPD5118_REG_TEMP_CRIT 0x20 /* MR32:MR33 */
45 #define SPD5118_REG_TEMP_LCRIT 0x22 /* MR34:MR35 */
46 #define SPD5118_REG_TEMP 0x31 /* MR49:MR50 */
47 #define SPD5118_REG_TEMP_STATUS 0x33 /* MR51 */
49 #define SPD5118_TEMP_STATUS_HIGH BIT(0)
50 #define SPD5118_TEMP_STATUS_LOW BIT(1)
51 #define SPD5118_TEMP_STATUS_CRIT BIT(2)
52 #define SPD5118_TEMP_STATUS_LCRIT BIT(3)
54 #define SPD5118_CAP_TS_SUPPORT BIT(1) /* temperature sensor support */
56 #define SPD5118_TS_DISABLE BIT(0) /* temperature sensor disable */
58 #define SPD5118_LEGACY_MODE_ADDR BIT(3)
59 #define SPD5118_LEGACY_PAGE_MASK GENMASK(2, 0)
60 #define SPD5118_LEGACY_MODE_MASK (SPD5118_LEGACY_MODE_ADDR | SPD5118_LEGACY_PAGE_MASK)
62 #define SPD5118_NUM_PAGES 8
63 #define SPD5118_PAGE_SIZE 128
64 #define SPD5118_PAGE_SHIFT 7
65 #define SPD5118_PAGE_MASK GENMASK(6, 0)
66 #define SPD5118_EEPROM_BASE 0x80
67 #define SPD5118_EEPROM_SIZE (SPD5118_PAGE_SIZE * SPD5118_NUM_PAGES)
69 /* Temperature unit in millicelsius */
70 #define SPD5118_TEMP_UNIT (MILLIDEGREE_PER_DEGREE / 4)
71 /* Representable temperature range in millicelsius */
72 #define SPD5118_TEMP_RANGE_MIN -256000
73 #define SPD5118_TEMP_RANGE_MAX 255750
76 struct regmap
*regmap
;
77 struct mutex nvmem_lock
;
82 static int spd5118_temp_from_reg(u16 reg
)
84 int temp
= sign_extend32(reg
>> 2, 10);
86 return temp
* SPD5118_TEMP_UNIT
;
89 static u16
spd5118_temp_to_reg(long temp
)
91 temp
= clamp_val(temp
, SPD5118_TEMP_RANGE_MIN
, SPD5118_TEMP_RANGE_MAX
);
92 return (DIV_ROUND_CLOSEST(temp
, SPD5118_TEMP_UNIT
) & 0x7ff) << 2;
95 static int spd5118_read_temp(struct regmap
*regmap
, u32 attr
, long *val
)
102 case hwmon_temp_input
:
103 reg
= SPD5118_REG_TEMP
;
106 reg
= SPD5118_REG_TEMP_MAX
;
109 reg
= SPD5118_REG_TEMP_MIN
;
111 case hwmon_temp_crit
:
112 reg
= SPD5118_REG_TEMP_CRIT
;
114 case hwmon_temp_lcrit
:
115 reg
= SPD5118_REG_TEMP_LCRIT
;
121 err
= regmap_bulk_read(regmap
, reg
, regval
, 2);
125 temp
= (regval
[1] << 8) | regval
[0];
127 *val
= spd5118_temp_from_reg(temp
);
131 static int spd5118_read_alarm(struct regmap
*regmap
, u32 attr
, long *val
)
133 unsigned int mask
, regval
;
137 case hwmon_temp_max_alarm
:
138 mask
= SPD5118_TEMP_STATUS_HIGH
;
140 case hwmon_temp_min_alarm
:
141 mask
= SPD5118_TEMP_STATUS_LOW
;
143 case hwmon_temp_crit_alarm
:
144 mask
= SPD5118_TEMP_STATUS_CRIT
;
146 case hwmon_temp_lcrit_alarm
:
147 mask
= SPD5118_TEMP_STATUS_LCRIT
;
153 err
= regmap_read(regmap
, SPD5118_REG_TEMP_STATUS
, ®val
);
156 *val
= !!(regval
& mask
);
158 return regmap_write(regmap
, SPD5118_REG_TEMP_CLR
, mask
);
162 static int spd5118_read_enable(struct regmap
*regmap
, long *val
)
167 err
= regmap_read(regmap
, SPD5118_REG_TEMP_CONFIG
, ®val
);
170 *val
= !(regval
& SPD5118_TS_DISABLE
);
174 static int spd5118_read(struct device
*dev
, enum hwmon_sensor_types type
,
175 u32 attr
, int channel
, long *val
)
177 struct regmap
*regmap
= dev_get_drvdata(dev
);
179 if (type
!= hwmon_temp
)
183 case hwmon_temp_input
:
186 case hwmon_temp_crit
:
187 case hwmon_temp_lcrit
:
188 return spd5118_read_temp(regmap
, attr
, val
);
189 case hwmon_temp_max_alarm
:
190 case hwmon_temp_min_alarm
:
191 case hwmon_temp_crit_alarm
:
192 case hwmon_temp_lcrit_alarm
:
193 return spd5118_read_alarm(regmap
, attr
, val
);
194 case hwmon_temp_enable
:
195 return spd5118_read_enable(regmap
, val
);
201 static int spd5118_write_temp(struct regmap
*regmap
, u32 attr
, long val
)
209 reg
= SPD5118_REG_TEMP_MAX
;
212 reg
= SPD5118_REG_TEMP_MIN
;
214 case hwmon_temp_crit
:
215 reg
= SPD5118_REG_TEMP_CRIT
;
217 case hwmon_temp_lcrit
:
218 reg
= SPD5118_REG_TEMP_LCRIT
;
224 temp
= spd5118_temp_to_reg(val
);
225 regval
[0] = temp
& 0xff;
226 regval
[1] = temp
>> 8;
228 return regmap_bulk_write(regmap
, reg
, regval
, 2);
231 static int spd5118_write_enable(struct regmap
*regmap
, long val
)
236 return regmap_update_bits(regmap
, SPD5118_REG_TEMP_CONFIG
,
238 val
? 0 : SPD5118_TS_DISABLE
);
241 static int spd5118_temp_write(struct regmap
*regmap
, u32 attr
, long val
)
246 case hwmon_temp_crit
:
247 case hwmon_temp_lcrit
:
248 return spd5118_write_temp(regmap
, attr
, val
);
249 case hwmon_temp_enable
:
250 return spd5118_write_enable(regmap
, val
);
256 static int spd5118_write(struct device
*dev
, enum hwmon_sensor_types type
,
257 u32 attr
, int channel
, long val
)
259 struct regmap
*regmap
= dev_get_drvdata(dev
);
263 return spd5118_temp_write(regmap
, attr
, val
);
269 static umode_t
spd5118_is_visible(const void *_data
, enum hwmon_sensor_types type
,
270 u32 attr
, int channel
)
272 if (type
!= hwmon_temp
)
276 case hwmon_temp_input
:
280 case hwmon_temp_lcrit
:
281 case hwmon_temp_crit
:
282 case hwmon_temp_enable
:
284 case hwmon_temp_min_alarm
:
285 case hwmon_temp_max_alarm
:
286 case hwmon_temp_crit_alarm
:
287 case hwmon_temp_lcrit_alarm
:
294 static inline bool spd5118_parity8(u8 w
)
297 return (0x6996 >> (w
& 0xf)) & 1;
301 * Bank and vendor id are 8-bit fields with seven data bits and odd parity.
302 * Vendor IDs 0 and 0x7f are invalid.
303 * See Jedec standard JEP106BJ for details and a list of assigned vendor IDs.
305 static bool spd5118_vendor_valid(u8 bank
, u8 id
)
307 if (!spd5118_parity8(bank
) || !spd5118_parity8(id
))
311 return id
&& id
!= 0x7f;
314 /* Return 0 if detection is successful, -ENODEV otherwise */
315 static int spd5118_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
317 struct i2c_adapter
*adapter
= client
->adapter
;
320 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
321 I2C_FUNC_SMBUS_WORD_DATA
))
324 regval
= i2c_smbus_read_word_swapped(client
, SPD5118_REG_TYPE
);
325 if (regval
!= 0x5118)
328 regval
= i2c_smbus_read_word_data(client
, SPD5118_REG_VENDOR
);
329 if (regval
< 0 || !spd5118_vendor_valid(regval
& 0xff, regval
>> 8))
332 regval
= i2c_smbus_read_byte_data(client
, SPD5118_REG_CAPABILITY
);
335 if (!(regval
& SPD5118_CAP_TS_SUPPORT
) || (regval
& 0xfc))
338 regval
= i2c_smbus_read_byte_data(client
, SPD5118_REG_TEMP_CLR
);
341 regval
= i2c_smbus_read_byte_data(client
, SPD5118_REG_ERROR_CLR
);
345 regval
= i2c_smbus_read_byte_data(client
, SPD5118_REG_REVISION
);
346 if (regval
< 0 || (regval
& 0xc1))
349 regval
= i2c_smbus_read_byte_data(client
, SPD5118_REG_TEMP_CONFIG
);
352 if (regval
& ~SPD5118_TS_DISABLE
)
355 strscpy(info
->type
, "spd5118", I2C_NAME_SIZE
);
359 static const struct hwmon_channel_info
*spd5118_info
[] = {
360 HWMON_CHANNEL_INFO(chip
,
361 HWMON_C_REGISTER_TZ
),
362 HWMON_CHANNEL_INFO(temp
,
364 HWMON_T_LCRIT
| HWMON_T_LCRIT_ALARM
|
365 HWMON_T_MIN
| HWMON_T_MIN_ALARM
|
366 HWMON_T_MAX
| HWMON_T_MAX_ALARM
|
367 HWMON_T_CRIT
| HWMON_T_CRIT_ALARM
|
372 static const struct hwmon_ops spd5118_hwmon_ops
= {
373 .is_visible
= spd5118_is_visible
,
374 .read
= spd5118_read
,
375 .write
= spd5118_write
,
378 static const struct hwmon_chip_info spd5118_chip_info
= {
379 .ops
= &spd5118_hwmon_ops
,
380 .info
= spd5118_info
,
385 static ssize_t
spd5118_nvmem_read_page(struct regmap
*regmap
, char *buf
,
386 unsigned int offset
, size_t count
)
388 int addr
= (offset
>> SPD5118_PAGE_SHIFT
) * 0x100 + SPD5118_EEPROM_BASE
;
391 offset
&= SPD5118_PAGE_MASK
;
393 /* Can't cross page boundaries */
394 if (offset
+ count
> SPD5118_PAGE_SIZE
)
395 count
= SPD5118_PAGE_SIZE
- offset
;
397 err
= regmap_bulk_read(regmap
, addr
+ offset
, buf
, count
);
404 static int spd5118_nvmem_read(void *priv
, unsigned int off
, void *val
, size_t count
)
406 struct spd5118_data
*data
= priv
;
410 if (unlikely(!count
))
413 if (off
+ count
> SPD5118_EEPROM_SIZE
)
416 mutex_lock(&data
->nvmem_lock
);
419 ret
= spd5118_nvmem_read_page(data
->regmap
, buf
, off
, count
);
421 mutex_unlock(&data
->nvmem_lock
);
428 mutex_unlock(&data
->nvmem_lock
);
432 static int spd5118_nvmem_init(struct device
*dev
, struct spd5118_data
*data
)
434 struct nvmem_config nvmem_config
= {
435 .type
= NVMEM_TYPE_EEPROM
,
436 .name
= dev_name(dev
),
437 .id
= NVMEM_DEVID_NONE
,
442 .owner
= THIS_MODULE
,
444 .reg_read
= spd5118_nvmem_read
,
448 .size
= SPD5118_EEPROM_SIZE
,
450 struct nvmem_device
*nvmem
;
452 nvmem
= devm_nvmem_register(dev
, &nvmem_config
);
453 return PTR_ERR_OR_ZERO(nvmem
);
458 static bool spd5118_writeable_reg(struct device
*dev
, unsigned int reg
)
461 case SPD5118_REG_I2C_LEGACY_MODE
:
462 case SPD5118_REG_TEMP_CLR
:
463 case SPD5118_REG_TEMP_CONFIG
:
464 case SPD5118_REG_TEMP_MAX
:
465 case SPD5118_REG_TEMP_MAX
+ 1:
466 case SPD5118_REG_TEMP_MIN
:
467 case SPD5118_REG_TEMP_MIN
+ 1:
468 case SPD5118_REG_TEMP_CRIT
:
469 case SPD5118_REG_TEMP_CRIT
+ 1:
470 case SPD5118_REG_TEMP_LCRIT
:
471 case SPD5118_REG_TEMP_LCRIT
+ 1:
478 static bool spd5118_volatile_reg(struct device
*dev
, unsigned int reg
)
481 case SPD5118_REG_TEMP_CLR
:
482 case SPD5118_REG_ERROR_CLR
:
483 case SPD5118_REG_TEMP
:
484 case SPD5118_REG_TEMP
+ 1:
485 case SPD5118_REG_TEMP_STATUS
:
492 static const struct regmap_range_cfg spd5118_regmap_range_cfg
[] = {
494 .selector_reg
= SPD5118_REG_I2C_LEGACY_MODE
,
495 .selector_mask
= SPD5118_LEGACY_PAGE_MASK
,
504 static const struct regmap_config spd5118_regmap_config
= {
507 .max_register
= 0x7ff,
508 .writeable_reg
= spd5118_writeable_reg
,
509 .volatile_reg
= spd5118_volatile_reg
,
510 .cache_type
= REGCACHE_MAPLE
,
512 .ranges
= spd5118_regmap_range_cfg
,
513 .num_ranges
= ARRAY_SIZE(spd5118_regmap_range_cfg
),
516 static int spd5118_init(struct i2c_client
*client
)
518 struct i2c_adapter
*adapter
= client
->adapter
;
519 int err
, regval
, mode
;
521 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
522 I2C_FUNC_SMBUS_WORD_DATA
))
525 regval
= i2c_smbus_read_word_swapped(client
, SPD5118_REG_TYPE
);
526 if (regval
< 0 || (regval
&& regval
!= 0x5118))
530 * If the device type registers return 0, it is possible that the chip
531 * has a non-zero page selected and takes the specification literally,
532 * i.e. disables access to volatile registers besides the page register
533 * if the page is not 0. Try to identify such chips.
536 /* Vendor ID registers must also be 0 */
537 regval
= i2c_smbus_read_word_data(client
, SPD5118_REG_VENDOR
);
541 /* The selected page in MR11 must not be 0 */
542 mode
= i2c_smbus_read_byte_data(client
, SPD5118_REG_I2C_LEGACY_MODE
);
543 if (mode
< 0 || (mode
& ~SPD5118_LEGACY_MODE_MASK
) ||
544 !(mode
& SPD5118_LEGACY_PAGE_MASK
))
547 err
= i2c_smbus_write_byte_data(client
, SPD5118_REG_I2C_LEGACY_MODE
,
548 mode
& SPD5118_LEGACY_MODE_ADDR
);
553 * If the device type registers are still bad after selecting
554 * page 0, this is not a SPD5118 device. Restore original
555 * legacy mode register value and abort.
557 regval
= i2c_smbus_read_word_swapped(client
, SPD5118_REG_TYPE
);
558 if (regval
!= 0x5118) {
559 i2c_smbus_write_byte_data(client
, SPD5118_REG_I2C_LEGACY_MODE
, mode
);
564 /* We are reasonably sure that this is really a SPD5118 hub controller */
568 static int spd5118_probe(struct i2c_client
*client
)
570 struct device
*dev
= &client
->dev
;
571 unsigned int regval
, revision
, vendor
, bank
;
572 struct spd5118_data
*data
;
573 struct device
*hwmon_dev
;
574 struct regmap
*regmap
;
577 err
= spd5118_init(client
);
581 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
585 regmap
= devm_regmap_init_i2c(client
, &spd5118_regmap_config
);
587 return dev_err_probe(dev
, PTR_ERR(regmap
), "regmap init failed\n");
589 err
= regmap_read(regmap
, SPD5118_REG_CAPABILITY
, ®val
);
592 if (!(regval
& SPD5118_CAP_TS_SUPPORT
))
595 err
= regmap_read(regmap
, SPD5118_REG_REVISION
, &revision
);
599 err
= regmap_read(regmap
, SPD5118_REG_VENDOR
, &bank
);
602 err
= regmap_read(regmap
, SPD5118_REG_VENDOR
+ 1, &vendor
);
605 if (!spd5118_vendor_valid(bank
, vendor
))
608 data
->regmap
= regmap
;
609 mutex_init(&data
->nvmem_lock
);
610 dev_set_drvdata(dev
, data
);
612 err
= spd5118_nvmem_init(dev
, data
);
613 /* Ignore if NVMEM support is disabled */
614 if (err
&& err
!= -EOPNOTSUPP
) {
615 dev_err_probe(dev
, err
, "failed to register nvmem\n");
619 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, "spd5118",
620 regmap
, &spd5118_chip_info
,
622 if (IS_ERR(hwmon_dev
))
623 return PTR_ERR(hwmon_dev
);
627 * MR2 bits [5:4]: Major revision, 1..4
628 * MR2 bits [3:1]: Minor revision, 0..8? Probably a typo, assume 1..8
630 dev_info(dev
, "DDR5 temperature sensor: vendor 0x%02x:0x%02x revision %d.%d\n",
631 bank
& 0x7f, vendor
, ((revision
>> 4) & 0x03) + 1, ((revision
>> 1) & 0x07) + 1);
636 static int spd5118_suspend(struct device
*dev
)
638 struct spd5118_data
*data
= dev_get_drvdata(dev
);
639 struct regmap
*regmap
= data
->regmap
;
644 * Make sure the configuration register in the regmap cache is current
645 * before bypassing it.
647 err
= regmap_read(regmap
, SPD5118_REG_TEMP_CONFIG
, ®val
);
651 regcache_cache_bypass(regmap
, true);
652 regmap_update_bits(regmap
, SPD5118_REG_TEMP_CONFIG
, SPD5118_TS_DISABLE
,
654 regcache_cache_bypass(regmap
, false);
656 regcache_cache_only(regmap
, true);
657 regcache_mark_dirty(regmap
);
662 static int spd5118_resume(struct device
*dev
)
664 struct spd5118_data
*data
= dev_get_drvdata(dev
);
665 struct regmap
*regmap
= data
->regmap
;
667 regcache_cache_only(regmap
, false);
668 return regcache_sync(regmap
);
671 static DEFINE_SIMPLE_DEV_PM_OPS(spd5118_pm_ops
, spd5118_suspend
, spd5118_resume
);
673 static const struct i2c_device_id spd5118_id
[] = {
677 MODULE_DEVICE_TABLE(i2c
, spd5118_id
);
679 static const struct of_device_id spd5118_of_ids
[] = {
680 { .compatible
= "jedec,spd5118", },
683 MODULE_DEVICE_TABLE(of
, spd5118_of_ids
);
685 static struct i2c_driver spd5118_driver
= {
686 .class = I2C_CLASS_HWMON
,
689 .of_match_table
= spd5118_of_ids
,
690 .pm
= pm_sleep_ptr(&spd5118_pm_ops
),
692 .probe
= spd5118_probe
,
693 .id_table
= spd5118_id
,
694 .detect
= IS_ENABLED(CONFIG_SENSORS_SPD5118_DETECT
) ? spd5118_detect
: NULL
,
695 .address_list
= IS_ENABLED(CONFIG_SENSORS_SPD5118_DETECT
) ? normal_i2c
: NULL
,
698 module_i2c_driver(spd5118_driver
);
700 MODULE_AUTHOR("René Rebe <rene@exactcode.de>");
701 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
702 MODULE_DESCRIPTION("SPD 5118 driver");
703 MODULE_LICENSE("GPL");