1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADT7310/ADT7310 digital temperature sensor driver
5 * Copyright 2012-2013 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/regmap.h>
12 #include <linux/spi/spi.h>
13 #include <asm/unaligned.h>
17 #define ADT7310_STATUS 0
18 #define ADT7310_CONFIG 1
19 #define ADT7310_TEMPERATURE 2
21 #define ADT7310_T_CRIT 4
22 #define ADT7310_T_HYST 5
23 #define ADT7310_T_ALARM_HIGH 6
24 #define ADT7310_T_ALARM_LOW 7
26 static const u8 adt7310_reg_table[] = {
27 [ADT7X10_TEMPERATURE] = ADT7310_TEMPERATURE,
28 [ADT7X10_STATUS] = ADT7310_STATUS,
29 [ADT7X10_CONFIG] = ADT7310_CONFIG,
30 [ADT7X10_T_ALARM_HIGH] = ADT7310_T_ALARM_HIGH,
31 [ADT7X10_T_ALARM_LOW] = ADT7310_T_ALARM_LOW,
32 [ADT7X10_T_CRIT] = ADT7310_T_CRIT,
33 [ADT7X10_T_HYST] = ADT7310_T_HYST,
34 [ADT7X10_ID] = ADT7310_ID,
37 #define ADT7310_CMD_REG_OFFSET 3
38 #define ADT7310_CMD_READ 0x40
40 #define AD7310_COMMAND(reg) (adt7310_reg_table[(reg)] << ADT7310_CMD_REG_OFFSET)
42 static int adt7310_spi_read_word(struct spi_device *spi, u8 reg)
44 return spi_w8r16be(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
47 static int adt7310_spi_write_word(struct spi_device *spi, u8 reg, u16 data)
51 buf[0] = AD7310_COMMAND(reg);
52 put_unaligned_be16(data, &buf[1]);
54 return spi_write(spi, buf, sizeof(buf));
57 static int adt7310_spi_read_byte(struct spi_device *spi, u8 reg)
59 return spi_w8r8(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
62 static int adt7310_spi_write_byte(struct spi_device *spi, u8 reg, u8 data)
66 buf[0] = AD7310_COMMAND(reg);
69 return spi_write(spi, buf, sizeof(buf));
72 static bool adt7310_regmap_is_volatile(struct device *dev, unsigned int reg)
75 case ADT7X10_TEMPERATURE:
83 static int adt7310_reg_read(void *context, unsigned int reg, unsigned int *val)
85 struct spi_device *spi = context;
89 case ADT7X10_TEMPERATURE:
90 case ADT7X10_T_ALARM_HIGH:
91 case ADT7X10_T_ALARM_LOW:
93 regval = adt7310_spi_read_word(spi, reg);
96 regval = adt7310_spi_read_byte(spi, reg);
105 static int adt7310_reg_write(void *context, unsigned int reg, unsigned int val)
107 struct spi_device *spi = context;
111 case ADT7X10_TEMPERATURE:
112 case ADT7X10_T_ALARM_HIGH:
113 case ADT7X10_T_ALARM_LOW:
115 ret = adt7310_spi_write_word(spi, reg, val);
118 ret = adt7310_spi_write_byte(spi, reg, val);
124 static const struct regmap_config adt7310_regmap_config = {
127 .cache_type = REGCACHE_RBTREE,
128 .volatile_reg = adt7310_regmap_is_volatile,
129 .reg_read = adt7310_reg_read,
130 .reg_write = adt7310_reg_write,
133 static int adt7310_spi_probe(struct spi_device *spi)
135 struct regmap *regmap;
137 regmap = devm_regmap_init(&spi->dev, NULL, spi, &adt7310_regmap_config);
139 return PTR_ERR(regmap);
141 return adt7x10_probe(&spi->dev, spi_get_device_id(spi)->name, spi->irq,
145 static const struct spi_device_id adt7310_id[] = {
150 MODULE_DEVICE_TABLE(spi, adt7310_id);
152 static struct spi_driver adt7310_driver = {
155 .pm = pm_sleep_ptr(&adt7x10_dev_pm_ops),
157 .probe = adt7310_spi_probe,
158 .id_table = adt7310_id,
160 module_spi_driver(adt7310_driver);
162 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
163 MODULE_DESCRIPTION("ADT7310/ADT7320 driver");
164 MODULE_LICENSE("GPL");