Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / hwmon / adt7310.c
blob6a834a37bc65d7ba49f6251c1c2e232d1ec9df85
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ADT7310/ADT7310 digital temperature sensor driver
5 * Copyright 2012-2013 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/regmap.h>
12 #include <linux/spi/spi.h>
13 #include <linux/unaligned.h>
15 #include "adt7x10.h"
17 #define ADT7310_STATUS 0
18 #define ADT7310_CONFIG 1
19 #define ADT7310_TEMPERATURE 2
20 #define ADT7310_ID 3
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)
49 u8 buf[3];
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)
64 u8 buf[2];
66 buf[0] = AD7310_COMMAND(reg);
67 buf[1] = data;
69 return spi_write(spi, buf, sizeof(buf));
72 static bool adt7310_regmap_is_volatile(struct device *dev, unsigned int reg)
74 switch (reg) {
75 case ADT7X10_TEMPERATURE:
76 case ADT7X10_STATUS:
77 return true;
78 default:
79 return false;
83 static int adt7310_reg_read(void *context, unsigned int reg, unsigned int *val)
85 struct spi_device *spi = context;
86 int regval;
88 switch (reg) {
89 case ADT7X10_TEMPERATURE:
90 case ADT7X10_T_ALARM_HIGH:
91 case ADT7X10_T_ALARM_LOW:
92 case ADT7X10_T_CRIT:
93 regval = adt7310_spi_read_word(spi, reg);
94 break;
95 default:
96 regval = adt7310_spi_read_byte(spi, reg);
97 break;
99 if (regval < 0)
100 return regval;
101 *val = regval;
102 return 0;
105 static int adt7310_reg_write(void *context, unsigned int reg, unsigned int val)
107 struct spi_device *spi = context;
108 int ret;
110 switch (reg) {
111 case ADT7X10_TEMPERATURE:
112 case ADT7X10_T_ALARM_HIGH:
113 case ADT7X10_T_ALARM_LOW:
114 case ADT7X10_T_CRIT:
115 ret = adt7310_spi_write_word(spi, reg, val);
116 break;
117 default:
118 ret = adt7310_spi_write_byte(spi, reg, val);
119 break;
121 return ret;
124 static const struct regmap_config adt7310_regmap_config = {
125 .reg_bits = 8,
126 .val_bits = 16,
127 .cache_type = REGCACHE_MAPLE,
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);
138 if (IS_ERR(regmap))
139 return PTR_ERR(regmap);
141 return adt7x10_probe(&spi->dev, spi_get_device_id(spi)->name, spi->irq,
142 regmap);
145 static const struct spi_device_id adt7310_id[] = {
146 { "adt7310", 0 },
147 { "adt7320", 0 },
150 MODULE_DEVICE_TABLE(spi, adt7310_id);
152 static struct spi_driver adt7310_driver = {
153 .driver = {
154 .name = "adt7310",
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");