1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * lm70.c
4 *
5 * The LM70 is a temperature sensor chip from National Semiconductor (NS).
6 * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
7 *
8 * The LM70 communicates with a host processor via an SPI/Microwire Bus
9 * interface. The complete datasheet is available at TI's website
10 * here:
11 * https://www.ti.com/product/LM70
12 */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/sysfs.h>
22 #include <linux/hwmon.h>
23 #include <linux/mutex.h>
24 #include <linux/of.h>
25 #include <linux/property.h>
26 #include <linux/spi/spi.h>
27 #include <linux/slab.h>
28
29 #define DRVNAME "lm70"
30
31 #define LM70_CHIP_LM70 0 /* original NS LM70 */
32 #define LM70_CHIP_TMP121 1 /* TI TMP121/TMP123 */
33 #define LM70_CHIP_LM71 2 /* NS LM71 */
34 #define LM70_CHIP_LM74 3 /* NS LM74 */
35 #define LM70_CHIP_TMP122 4 /* TI TMP122/TMP124 */
36 #define LM70_CHIP_TMP125 5 /* TI TMP125 */
37
38 struct lm70 {
39 struct spi_device *spi;
40 struct mutex lock;
41 unsigned int chip;
42 };
43
44 /* sysfs hook function */
temp1_input_show(struct device * dev,struct device_attribute * attr,char * buf)45 static ssize_t temp1_input_show(struct device *dev,
46 struct device_attribute *attr, char *buf)
47 {
48 struct lm70 *p_lm70 = dev_get_drvdata(dev);
49 struct spi_device *spi = p_lm70->spi;
50 int status, val = 0;
51 u8 rxbuf[2];
52 s16 raw = 0;
53
54 if (mutex_lock_interruptible(&p_lm70->lock))
55 return -ERESTARTSYS;
56
57 /*
58 * spi_read() requires a DMA-safe buffer; so we use
59 * spi_write_then_read(), transmitting 0 bytes.
60 */
61 status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
62 if (status < 0) {
63 dev_warn(dev, "spi_write_then_read failed with status %d\n",
64 status);
65 goto out;
66 }
67 raw = (rxbuf[0] << 8) + rxbuf[1];
68 dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
69 rxbuf[0], rxbuf[1], raw);
70
71 /*
72 * LM70:
73 * The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
74 * complement value. Only the MSB 11 bits (1 sign + 10 temperature
75 * bits) are meaningful; the LSB 5 bits are to be discarded.
76 * See the datasheet.
77 *
78 * Further, each bit represents 0.25 degrees Celsius; so, multiply
79 * by 0.25. Also multiply by 1000 to represent in millidegrees
80 * Celsius.
81 * So it's equivalent to multiplying by 0.25 * 1000 = 250.
82 *
83 * LM74 and TMP121/TMP122/TMP123/TMP124:
84 * 13 bits of 2's complement data, discard LSB 3 bits,
85 * resolution 0.0625 degrees celsius.
86 *
87 * LM71:
88 * 14 bits of 2's complement data, discard LSB 2 bits,
89 * resolution 0.0312 degrees celsius.
90 *
91 * TMP125:
92 * MSB/D15 is a leading zero. D14 is the sign-bit. This is
93 * followed by 9 temperature bits (D13..D5) in 2's complement
94 * data format with a resolution of 0.25 degrees celsius per unit.
95 * LSB 5 bits (D4..D0) share the same value as D5 and get discarded.
96 */
97 switch (p_lm70->chip) {
98 case LM70_CHIP_LM70:
99 val = ((int)raw / 32) * 250;
100 break;
101
102 case LM70_CHIP_TMP121:
103 case LM70_CHIP_TMP122:
104 case LM70_CHIP_LM74:
105 val = ((int)raw / 8) * 625 / 10;
106 break;
107
108 case LM70_CHIP_LM71:
109 val = ((int)raw / 4) * 3125 / 100;
110 break;
111
112 case LM70_CHIP_TMP125:
113 val = (sign_extend32(raw, 14) / 32) * 250;
114 break;
115 }
116
117 status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
118 out:
119 mutex_unlock(&p_lm70->lock);
120 return status;
121 }
122
123 static DEVICE_ATTR_RO(temp1_input);
124
125 static struct attribute *lm70_attrs[] = {
126 &dev_attr_temp1_input.attr,
127 NULL
128 };
129
130 ATTRIBUTE_GROUPS(lm70);
131
132 /*----------------------------------------------------------------------*/
133
134 #ifdef CONFIG_OF
135 static const struct of_device_id lm70_of_ids[] = {
136 {
137 .compatible = "ti,lm70",
138 .data = (void *) LM70_CHIP_LM70,
139 },
140 {
141 .compatible = "ti,tmp121",
142 .data = (void *) LM70_CHIP_TMP121,
143 },
144 {
145 .compatible = "ti,tmp122",
146 .data = (void *) LM70_CHIP_TMP122,
147 },
148 {
149 .compatible = "ti,tmp125",
150 .data = (void *) LM70_CHIP_TMP125,
151 },
152 {
153 .compatible = "ti,lm71",
154 .data = (void *) LM70_CHIP_LM71,
155 },
156 {
157 .compatible = "ti,lm74",
158 .data = (void *) LM70_CHIP_LM74,
159 },
160 {},
161 };
162 MODULE_DEVICE_TABLE(of, lm70_of_ids);
163 #endif
164
lm70_probe(struct spi_device * spi)165 static int lm70_probe(struct spi_device *spi)
166 {
167 struct device *hwmon_dev;
168 struct lm70 *p_lm70;
169 int chip;
170
171 chip = (kernel_ulong_t)spi_get_device_match_data(spi);
172
173 /* signaling is SPI_MODE_0 */
174 if ((spi->mode & SPI_MODE_X_MASK) != SPI_MODE_0)
175 return -EINVAL;
176
177 /* NOTE: we assume 8-bit words, and convert to 16 bits manually */
178
179 p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
180 if (!p_lm70)
181 return -ENOMEM;
182
183 mutex_init(&p_lm70->lock);
184 p_lm70->chip = chip;
185 p_lm70->spi = spi;
186
187 hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
188 spi->modalias,
189 p_lm70, lm70_groups);
190 return PTR_ERR_OR_ZERO(hwmon_dev);
191 }
192
193 static const struct spi_device_id lm70_ids[] = {
194 { "lm70", LM70_CHIP_LM70 },
195 { "tmp121", LM70_CHIP_TMP121 },
196 { "tmp122", LM70_CHIP_TMP122 },
197 { "tmp125", LM70_CHIP_TMP125 },
198 { "lm71", LM70_CHIP_LM71 },
199 { "lm74", LM70_CHIP_LM74 },
200 { },
201 };
202 MODULE_DEVICE_TABLE(spi, lm70_ids);
203
204 static struct spi_driver lm70_driver = {
205 .driver = {
206 .name = "lm70",
207 .of_match_table = of_match_ptr(lm70_of_ids),
208 },
209 .id_table = lm70_ids,
210 .probe = lm70_probe,
211 };
212
213 module_spi_driver(lm70_driver);
214
215 MODULE_AUTHOR("Kaiwan N Billimoria");
216 MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
217 MODULE_LICENSE("GPL");
218