xref: /openbmc/linux/drivers/hwmon/adt7310.c (revision 85250a24)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ADT7310/ADT7310 digital temperature sensor driver
4  *
5  * Copyright 2012-2013 Analog Devices Inc.
6  *   Author: Lars-Peter Clausen <lars@metafoo.de>
7  */
8 
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>
14 
15 #include "adt7x10.h"
16 
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
25 
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,
35 };
36 
37 #define ADT7310_CMD_REG_OFFSET	3
38 #define ADT7310_CMD_READ	0x40
39 
40 #define AD7310_COMMAND(reg) (adt7310_reg_table[(reg)] << ADT7310_CMD_REG_OFFSET)
41 
42 static int adt7310_spi_read_word(struct spi_device *spi, u8 reg)
43 {
44 	return spi_w8r16be(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
45 }
46 
47 static int adt7310_spi_write_word(struct spi_device *spi, u8 reg, u16 data)
48 {
49 	u8 buf[3];
50 
51 	buf[0] = AD7310_COMMAND(reg);
52 	put_unaligned_be16(data, &buf[1]);
53 
54 	return spi_write(spi, buf, sizeof(buf));
55 }
56 
57 static int adt7310_spi_read_byte(struct spi_device *spi, u8 reg)
58 {
59 	return spi_w8r8(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
60 }
61 
62 static int adt7310_spi_write_byte(struct spi_device *spi, u8 reg, u8 data)
63 {
64 	u8 buf[2];
65 
66 	buf[0] = AD7310_COMMAND(reg);
67 	buf[1] = data;
68 
69 	return spi_write(spi, buf, sizeof(buf));
70 }
71 
72 static bool adt7310_regmap_is_volatile(struct device *dev, unsigned int reg)
73 {
74 	switch (reg) {
75 	case ADT7X10_TEMPERATURE:
76 	case ADT7X10_STATUS:
77 		return true;
78 	default:
79 		return false;
80 	}
81 }
82 
83 static int adt7310_reg_read(void *context, unsigned int reg, unsigned int *val)
84 {
85 	struct spi_device *spi = context;
86 	int regval;
87 
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;
98 	}
99 	if (regval < 0)
100 		return regval;
101 	*val = regval;
102 	return 0;
103 }
104 
105 static int adt7310_reg_write(void *context, unsigned int reg, unsigned int val)
106 {
107 	struct spi_device *spi = context;
108 	int ret;
109 
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;
120 	}
121 	return ret;
122 }
123 
124 static const struct regmap_config adt7310_regmap_config = {
125 	.reg_bits = 8,
126 	.val_bits = 16,
127 	.cache_type = REGCACHE_RBTREE,
128 	.volatile_reg = adt7310_regmap_is_volatile,
129 	.reg_read = adt7310_reg_read,
130 	.reg_write = adt7310_reg_write,
131 };
132 
133 static int adt7310_spi_probe(struct spi_device *spi)
134 {
135 	struct regmap *regmap;
136 
137 	regmap = devm_regmap_init(&spi->dev, NULL, spi, &adt7310_regmap_config);
138 	if (IS_ERR(regmap))
139 		return PTR_ERR(regmap);
140 
141 	return adt7x10_probe(&spi->dev, spi_get_device_id(spi)->name, spi->irq,
142 			     regmap);
143 }
144 
145 static const struct spi_device_id adt7310_id[] = {
146 	{ "adt7310", 0 },
147 	{ "adt7320", 0 },
148 	{}
149 };
150 MODULE_DEVICE_TABLE(spi, adt7310_id);
151 
152 static struct spi_driver adt7310_driver = {
153 	.driver = {
154 		.name	= "adt7310",
155 		.pm	= pm_sleep_ptr(&adt7x10_dev_pm_ops),
156 	},
157 	.probe		= adt7310_spi_probe,
158 	.id_table	= adt7310_id,
159 };
160 module_spi_driver(adt7310_driver);
161 
162 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
163 MODULE_DESCRIPTION("ADT7310/ADT7320 driver");
164 MODULE_LICENSE("GPL");
165