174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e1a8e913SKaiwan N Billimoria /*
3e1a8e913SKaiwan N Billimoria * lm70.c
4e1a8e913SKaiwan N Billimoria *
5e1a8e913SKaiwan N Billimoria * The LM70 is a temperature sensor chip from National Semiconductor (NS).
6e1a8e913SKaiwan N Billimoria * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
7e1a8e913SKaiwan N Billimoria *
8e1a8e913SKaiwan N Billimoria * The LM70 communicates with a host processor via an SPI/Microwire Bus
9e1a8e913SKaiwan N Billimoria * interface. The complete datasheet is available at National's website
10e1a8e913SKaiwan N Billimoria * here:
11e1a8e913SKaiwan N Billimoria * http://www.national.com/pf/LM/LM70.html
12e1a8e913SKaiwan N Billimoria */
13e1a8e913SKaiwan N Billimoria
145713017eSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
155713017eSJoe Perches
16e1a8e913SKaiwan N Billimoria #include <linux/init.h>
17e1a8e913SKaiwan N Billimoria #include <linux/module.h>
18e1a8e913SKaiwan N Billimoria #include <linux/kernel.h>
19e1a8e913SKaiwan N Billimoria #include <linux/device.h>
20e1a8e913SKaiwan N Billimoria #include <linux/err.h>
21e1a8e913SKaiwan N Billimoria #include <linux/sysfs.h>
22e1a8e913SKaiwan N Billimoria #include <linux/hwmon.h>
234bfe6604SMatthias Kaehlcke #include <linux/mutex.h>
248cec03eeSAnton Vorontsov #include <linux/mod_devicetable.h>
25ac61c8aaSGuenter Roeck #include <linux/of.h>
266e09d755SStephen Boyd #include <linux/property.h>
27e1a8e913SKaiwan N Billimoria #include <linux/spi/spi.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
29e1a8e913SKaiwan N Billimoria
30e1a8e913SKaiwan N Billimoria #define DRVNAME "lm70"
31e1a8e913SKaiwan N Billimoria
32c8ac32e4SManuel Lauss #define LM70_CHIP_LM70 0 /* original NS LM70 */
33c8ac32e4SManuel Lauss #define LM70_CHIP_TMP121 1 /* TI TMP121/TMP123 */
34a86e94dcSChristophe Leroy #define LM70_CHIP_LM71 2 /* NS LM71 */
35a86e94dcSChristophe Leroy #define LM70_CHIP_LM74 3 /* NS LM74 */
3668f0c8c9SFlorian Fainelli #define LM70_CHIP_TMP122 4 /* TI TMP122/TMP124 */
37*cd929672SChristian Lamparter #define LM70_CHIP_TMP125 5 /* TI TMP125 */
38c8ac32e4SManuel Lauss
39e1a8e913SKaiwan N Billimoria struct lm70 {
40aa9bcddaSGuenter Roeck struct spi_device *spi;
414bfe6604SMatthias Kaehlcke struct mutex lock;
42c8ac32e4SManuel Lauss unsigned int chip;
43e1a8e913SKaiwan N Billimoria };
44e1a8e913SKaiwan N Billimoria
45e1a8e913SKaiwan N Billimoria /* sysfs hook function */
temp1_input_show(struct device * dev,struct device_attribute * attr,char * buf)4689cb4af8SJulia Lawall static ssize_t temp1_input_show(struct device *dev,
47e1a8e913SKaiwan N Billimoria struct device_attribute *attr, char *buf)
48e1a8e913SKaiwan N Billimoria {
49aa9bcddaSGuenter Roeck struct lm70 *p_lm70 = dev_get_drvdata(dev);
50aa9bcddaSGuenter Roeck struct spi_device *spi = p_lm70->spi;
51c8ac32e4SManuel Lauss int status, val = 0;
52e1a8e913SKaiwan N Billimoria u8 rxbuf[2];
53e1a8e913SKaiwan N Billimoria s16 raw = 0;
54e1a8e913SKaiwan N Billimoria
554bfe6604SMatthias Kaehlcke if (mutex_lock_interruptible(&p_lm70->lock))
56e1a8e913SKaiwan N Billimoria return -ERESTARTSYS;
57e1a8e913SKaiwan N Billimoria
58e1a8e913SKaiwan N Billimoria /*
59e1a8e913SKaiwan N Billimoria * spi_read() requires a DMA-safe buffer; so we use
60e1a8e913SKaiwan N Billimoria * spi_write_then_read(), transmitting 0 bytes.
61e1a8e913SKaiwan N Billimoria */
62e1a8e913SKaiwan N Billimoria status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
63e1a8e913SKaiwan N Billimoria if (status < 0) {
64e8295146SFlorian Fainelli dev_warn(dev, "spi_write_then_read failed with status %d\n",
65e8295146SFlorian Fainelli status);
66e1a8e913SKaiwan N Billimoria goto out;
67e1a8e913SKaiwan N Billimoria }
682b730051SKaiwan N Billimoria raw = (rxbuf[0] << 8) + rxbuf[1];
692b730051SKaiwan N Billimoria dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
702b730051SKaiwan N Billimoria rxbuf[0], rxbuf[1], raw);
71e1a8e913SKaiwan N Billimoria
72e1a8e913SKaiwan N Billimoria /*
73c8ac32e4SManuel Lauss * LM70:
74e1a8e913SKaiwan N Billimoria * The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
75e1a8e913SKaiwan N Billimoria * complement value. Only the MSB 11 bits (1 sign + 10 temperature
76e1a8e913SKaiwan N Billimoria * bits) are meaningful; the LSB 5 bits are to be discarded.
77e1a8e913SKaiwan N Billimoria * See the datasheet.
78e1a8e913SKaiwan N Billimoria *
79e1a8e913SKaiwan N Billimoria * Further, each bit represents 0.25 degrees Celsius; so, multiply
80e1a8e913SKaiwan N Billimoria * by 0.25. Also multiply by 1000 to represent in millidegrees
81e1a8e913SKaiwan N Billimoria * Celsius.
82e1a8e913SKaiwan N Billimoria * So it's equivalent to multiplying by 0.25 * 1000 = 250.
83c8ac32e4SManuel Lauss *
8468f0c8c9SFlorian Fainelli * LM74 and TMP121/TMP122/TMP123/TMP124:
85c8ac32e4SManuel Lauss * 13 bits of 2's complement data, discard LSB 3 bits,
86c8ac32e4SManuel Lauss * resolution 0.0625 degrees celsius.
87a86e94dcSChristophe Leroy *
88a86e94dcSChristophe Leroy * LM71:
89a86e94dcSChristophe Leroy * 14 bits of 2's complement data, discard LSB 2 bits,
90a86e94dcSChristophe Leroy * resolution 0.0312 degrees celsius.
91*cd929672SChristian Lamparter *
92*cd929672SChristian Lamparter * TMP125:
93*cd929672SChristian Lamparter * MSB/D15 is a leading zero. D14 is the sign-bit. This is
94*cd929672SChristian Lamparter * followed by 9 temperature bits (D13..D5) in 2's complement
95*cd929672SChristian Lamparter * data format with a resolution of 0.25 degrees celsius per unit.
96*cd929672SChristian Lamparter * LSB 5 bits (D4..D0) share the same value as D5 and get discarded.
97e1a8e913SKaiwan N Billimoria */
98c8ac32e4SManuel Lauss switch (p_lm70->chip) {
99c8ac32e4SManuel Lauss case LM70_CHIP_LM70:
100e1a8e913SKaiwan N Billimoria val = ((int)raw / 32) * 250;
101c8ac32e4SManuel Lauss break;
102c8ac32e4SManuel Lauss
103c8ac32e4SManuel Lauss case LM70_CHIP_TMP121:
10468f0c8c9SFlorian Fainelli case LM70_CHIP_TMP122:
105a86e94dcSChristophe Leroy case LM70_CHIP_LM74:
106c8ac32e4SManuel Lauss val = ((int)raw / 8) * 625 / 10;
107c8ac32e4SManuel Lauss break;
108a86e94dcSChristophe Leroy
109a86e94dcSChristophe Leroy case LM70_CHIP_LM71:
110a86e94dcSChristophe Leroy val = ((int)raw / 4) * 3125 / 100;
111a86e94dcSChristophe Leroy break;
112*cd929672SChristian Lamparter
113*cd929672SChristian Lamparter case LM70_CHIP_TMP125:
114*cd929672SChristian Lamparter val = (sign_extend32(raw, 14) / 32) * 250;
115*cd929672SChristian Lamparter break;
116c8ac32e4SManuel Lauss }
117c8ac32e4SManuel Lauss
11867f921d1SJean Delvare status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
119e1a8e913SKaiwan N Billimoria out:
1204bfe6604SMatthias Kaehlcke mutex_unlock(&p_lm70->lock);
121e1a8e913SKaiwan N Billimoria return status;
122e1a8e913SKaiwan N Billimoria }
123e1a8e913SKaiwan N Billimoria
12489cb4af8SJulia Lawall static DEVICE_ATTR_RO(temp1_input);
125e1a8e913SKaiwan N Billimoria
126aa9bcddaSGuenter Roeck static struct attribute *lm70_attrs[] = {
127aa9bcddaSGuenter Roeck &dev_attr_temp1_input.attr,
128aa9bcddaSGuenter Roeck NULL
129aa9bcddaSGuenter Roeck };
13067f921d1SJean Delvare
131aa9bcddaSGuenter Roeck ATTRIBUTE_GROUPS(lm70);
13267f921d1SJean Delvare
133e1a8e913SKaiwan N Billimoria /*----------------------------------------------------------------------*/
134e1a8e913SKaiwan N Billimoria
135a1dc86ebSRabin Vincent #ifdef CONFIG_OF
136a1dc86ebSRabin Vincent static const struct of_device_id lm70_of_ids[] = {
137a1dc86ebSRabin Vincent {
138a1dc86ebSRabin Vincent .compatible = "ti,lm70",
139a1dc86ebSRabin Vincent .data = (void *) LM70_CHIP_LM70,
140a1dc86ebSRabin Vincent },
141a1dc86ebSRabin Vincent {
142a1dc86ebSRabin Vincent .compatible = "ti,tmp121",
143a1dc86ebSRabin Vincent .data = (void *) LM70_CHIP_TMP121,
144a1dc86ebSRabin Vincent },
145a1dc86ebSRabin Vincent {
14668f0c8c9SFlorian Fainelli .compatible = "ti,tmp122",
14768f0c8c9SFlorian Fainelli .data = (void *) LM70_CHIP_TMP122,
14868f0c8c9SFlorian Fainelli },
14968f0c8c9SFlorian Fainelli {
150*cd929672SChristian Lamparter .compatible = "ti,tmp125",
151*cd929672SChristian Lamparter .data = (void *) LM70_CHIP_TMP125,
152*cd929672SChristian Lamparter },
153*cd929672SChristian Lamparter {
154a1dc86ebSRabin Vincent .compatible = "ti,lm71",
155a1dc86ebSRabin Vincent .data = (void *) LM70_CHIP_LM71,
156a1dc86ebSRabin Vincent },
157a1dc86ebSRabin Vincent {
158a1dc86ebSRabin Vincent .compatible = "ti,lm74",
159a1dc86ebSRabin Vincent .data = (void *) LM70_CHIP_LM74,
160a1dc86ebSRabin Vincent },
161a1dc86ebSRabin Vincent {},
162a1dc86ebSRabin Vincent };
163a1dc86ebSRabin Vincent MODULE_DEVICE_TABLE(of, lm70_of_ids);
164a1dc86ebSRabin Vincent #endif
165a1dc86ebSRabin Vincent
lm70_probe(struct spi_device * spi)1666c931ae1SBill Pemberton static int lm70_probe(struct spi_device *spi)
167e1a8e913SKaiwan N Billimoria {
168aa9bcddaSGuenter Roeck struct device *hwmon_dev;
169e1a8e913SKaiwan N Billimoria struct lm70 *p_lm70;
170a1dc86ebSRabin Vincent int chip;
171a1dc86ebSRabin Vincent
1726e09d755SStephen Boyd if (dev_fwnode(&spi->dev))
1736e09d755SStephen Boyd chip = (int)(uintptr_t)device_get_match_data(&spi->dev);
174a1dc86ebSRabin Vincent else
175a1dc86ebSRabin Vincent chip = spi_get_device_id(spi)->driver_data;
1766e09d755SStephen Boyd
177e1a8e913SKaiwan N Billimoria
178a86e94dcSChristophe Leroy /* signaling is SPI_MODE_0 */
179ba9c5fc3SAndy Shevchenko if ((spi->mode & SPI_MODE_X_MASK) != SPI_MODE_0)
1808cec03eeSAnton Vorontsov return -EINVAL;
1818cec03eeSAnton Vorontsov
1822b730051SKaiwan N Billimoria /* NOTE: we assume 8-bit words, and convert to 16 bits manually */
1832b730051SKaiwan N Billimoria
18433ed6d4aSGuenter Roeck p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
185e1a8e913SKaiwan N Billimoria if (!p_lm70)
186e1a8e913SKaiwan N Billimoria return -ENOMEM;
187e1a8e913SKaiwan N Billimoria
1884bfe6604SMatthias Kaehlcke mutex_init(&p_lm70->lock);
189c8ac32e4SManuel Lauss p_lm70->chip = chip;
190aa9bcddaSGuenter Roeck p_lm70->spi = spi;
191e1a8e913SKaiwan N Billimoria
192aa9bcddaSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
193aa9bcddaSGuenter Roeck spi->modalias,
194aa9bcddaSGuenter Roeck p_lm70, lm70_groups);
195aa9bcddaSGuenter Roeck return PTR_ERR_OR_ZERO(hwmon_dev);
196e200c14fSGuenter Roeck }
197e200c14fSGuenter Roeck
1988cec03eeSAnton Vorontsov static const struct spi_device_id lm70_ids[] = {
1998cec03eeSAnton Vorontsov { "lm70", LM70_CHIP_LM70 },
2008cec03eeSAnton Vorontsov { "tmp121", LM70_CHIP_TMP121 },
20168f0c8c9SFlorian Fainelli { "tmp122", LM70_CHIP_TMP122 },
202*cd929672SChristian Lamparter { "tmp125", LM70_CHIP_TMP125 },
203a86e94dcSChristophe Leroy { "lm71", LM70_CHIP_LM71 },
204a86e94dcSChristophe Leroy { "lm74", LM70_CHIP_LM74 },
2058cec03eeSAnton Vorontsov { },
206c8ac32e4SManuel Lauss };
2078cec03eeSAnton Vorontsov MODULE_DEVICE_TABLE(spi, lm70_ids);
208c8ac32e4SManuel Lauss
209e1a8e913SKaiwan N Billimoria static struct spi_driver lm70_driver = {
210e1a8e913SKaiwan N Billimoria .driver = {
211e1a8e913SKaiwan N Billimoria .name = "lm70",
212a1dc86ebSRabin Vincent .of_match_table = of_match_ptr(lm70_of_ids),
213e1a8e913SKaiwan N Billimoria },
2148cec03eeSAnton Vorontsov .id_table = lm70_ids,
215e1a8e913SKaiwan N Billimoria .probe = lm70_probe,
216e1a8e913SKaiwan N Billimoria };
217e1a8e913SKaiwan N Billimoria
21891efffe2SAxel Lin module_spi_driver(lm70_driver);
219e1a8e913SKaiwan N Billimoria
220e1a8e913SKaiwan N Billimoria MODULE_AUTHOR("Kaiwan N Billimoria");
221a86e94dcSChristophe Leroy MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
222e1a8e913SKaiwan N Billimoria MODULE_LICENSE("GPL");
223