xref: /openbmc/linux/drivers/hwmon/lm70.c (revision e8295146)
1e1a8e913SKaiwan N Billimoria /*
2e1a8e913SKaiwan N Billimoria  * lm70.c
3e1a8e913SKaiwan N Billimoria  *
4e1a8e913SKaiwan N Billimoria  * The LM70 is a temperature sensor chip from National Semiconductor (NS).
5e1a8e913SKaiwan N Billimoria  * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
6e1a8e913SKaiwan N Billimoria  *
7e1a8e913SKaiwan N Billimoria  * The LM70 communicates with a host processor via an SPI/Microwire Bus
8e1a8e913SKaiwan N Billimoria  * interface. The complete datasheet is available at National's website
9e1a8e913SKaiwan N Billimoria  * here:
10e1a8e913SKaiwan N Billimoria  * http://www.national.com/pf/LM/LM70.html
11e1a8e913SKaiwan N Billimoria  *
12e1a8e913SKaiwan N Billimoria  * This program is free software; you can redistribute it and/or modify
13e1a8e913SKaiwan N Billimoria  * it under the terms of the GNU General Public License as published by
14e1a8e913SKaiwan N Billimoria  * the Free Software Foundation; either version 2 of the License, or
15e1a8e913SKaiwan N Billimoria  * (at your option) any later version.
16e1a8e913SKaiwan N Billimoria  *
17e1a8e913SKaiwan N Billimoria  * This program is distributed in the hope that it will be useful,
18e1a8e913SKaiwan N Billimoria  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19e1a8e913SKaiwan N Billimoria  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20e1a8e913SKaiwan N Billimoria  * GNU General Public License for more details.
21e1a8e913SKaiwan N Billimoria  *
22e1a8e913SKaiwan N Billimoria  * You should have received a copy of the GNU General Public License
23e1a8e913SKaiwan N Billimoria  * along with this program; if not, write to the Free Software
24e1a8e913SKaiwan N Billimoria  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25e1a8e913SKaiwan N Billimoria  */
26e1a8e913SKaiwan N Billimoria 
275713017eSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
285713017eSJoe Perches 
29e1a8e913SKaiwan N Billimoria #include <linux/init.h>
30e1a8e913SKaiwan N Billimoria #include <linux/module.h>
31e1a8e913SKaiwan N Billimoria #include <linux/kernel.h>
32e1a8e913SKaiwan N Billimoria #include <linux/device.h>
33e1a8e913SKaiwan N Billimoria #include <linux/err.h>
34e1a8e913SKaiwan N Billimoria #include <linux/sysfs.h>
35e1a8e913SKaiwan N Billimoria #include <linux/hwmon.h>
364bfe6604SMatthias Kaehlcke #include <linux/mutex.h>
378cec03eeSAnton Vorontsov #include <linux/mod_devicetable.h>
38e1a8e913SKaiwan N Billimoria #include <linux/spi/spi.h>
395a0e3ad6STejun Heo #include <linux/slab.h>
40a1dc86ebSRabin Vincent #include <linux/of_device.h>
414bfe6604SMatthias Kaehlcke 
42e1a8e913SKaiwan N Billimoria 
43e1a8e913SKaiwan N Billimoria #define DRVNAME		"lm70"
44e1a8e913SKaiwan N Billimoria 
45c8ac32e4SManuel Lauss #define LM70_CHIP_LM70		0	/* original NS LM70 */
46c8ac32e4SManuel Lauss #define LM70_CHIP_TMP121	1	/* TI TMP121/TMP123 */
47a86e94dcSChristophe Leroy #define LM70_CHIP_LM71		2	/* NS LM71 */
48a86e94dcSChristophe Leroy #define LM70_CHIP_LM74		3	/* NS LM74 */
49c8ac32e4SManuel Lauss 
50e1a8e913SKaiwan N Billimoria struct lm70 {
51aa9bcddaSGuenter Roeck 	struct spi_device *spi;
524bfe6604SMatthias Kaehlcke 	struct mutex lock;
53c8ac32e4SManuel Lauss 	unsigned int chip;
54e1a8e913SKaiwan N Billimoria };
55e1a8e913SKaiwan N Billimoria 
56e1a8e913SKaiwan N Billimoria /* sysfs hook function */
5789cb4af8SJulia Lawall static ssize_t temp1_input_show(struct device *dev,
58e1a8e913SKaiwan N Billimoria 				struct device_attribute *attr, char *buf)
59e1a8e913SKaiwan N Billimoria {
60aa9bcddaSGuenter Roeck 	struct lm70 *p_lm70 = dev_get_drvdata(dev);
61aa9bcddaSGuenter Roeck 	struct spi_device *spi = p_lm70->spi;
62c8ac32e4SManuel Lauss 	int status, val = 0;
63e1a8e913SKaiwan N Billimoria 	u8 rxbuf[2];
64e1a8e913SKaiwan N Billimoria 	s16 raw = 0;
65e1a8e913SKaiwan N Billimoria 
664bfe6604SMatthias Kaehlcke 	if (mutex_lock_interruptible(&p_lm70->lock))
67e1a8e913SKaiwan N Billimoria 		return -ERESTARTSYS;
68e1a8e913SKaiwan N Billimoria 
69e1a8e913SKaiwan N Billimoria 	/*
70e1a8e913SKaiwan N Billimoria 	 * spi_read() requires a DMA-safe buffer; so we use
71e1a8e913SKaiwan N Billimoria 	 * spi_write_then_read(), transmitting 0 bytes.
72e1a8e913SKaiwan N Billimoria 	 */
73e1a8e913SKaiwan N Billimoria 	status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
74e1a8e913SKaiwan N Billimoria 	if (status < 0) {
75e8295146SFlorian Fainelli 		dev_warn(dev, "spi_write_then_read failed with status %d\n",
76e8295146SFlorian Fainelli 			 status);
77e1a8e913SKaiwan N Billimoria 		goto out;
78e1a8e913SKaiwan N Billimoria 	}
792b730051SKaiwan N Billimoria 	raw = (rxbuf[0] << 8) + rxbuf[1];
802b730051SKaiwan N Billimoria 	dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
812b730051SKaiwan N Billimoria 		rxbuf[0], rxbuf[1], raw);
82e1a8e913SKaiwan N Billimoria 
83e1a8e913SKaiwan N Billimoria 	/*
84c8ac32e4SManuel Lauss 	 * LM70:
85e1a8e913SKaiwan N Billimoria 	 * The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
86e1a8e913SKaiwan N Billimoria 	 * complement value. Only the MSB 11 bits (1 sign + 10 temperature
87e1a8e913SKaiwan N Billimoria 	 * bits) are meaningful; the LSB 5 bits are to be discarded.
88e1a8e913SKaiwan N Billimoria 	 * See the datasheet.
89e1a8e913SKaiwan N Billimoria 	 *
90e1a8e913SKaiwan N Billimoria 	 * Further, each bit represents 0.25 degrees Celsius; so, multiply
91e1a8e913SKaiwan N Billimoria 	 * by 0.25. Also multiply by 1000 to represent in millidegrees
92e1a8e913SKaiwan N Billimoria 	 * Celsius.
93e1a8e913SKaiwan N Billimoria 	 * So it's equivalent to multiplying by 0.25 * 1000 = 250.
94c8ac32e4SManuel Lauss 	 *
95a86e94dcSChristophe Leroy 	 * LM74 and TMP121/TMP123:
96c8ac32e4SManuel Lauss 	 * 13 bits of 2's complement data, discard LSB 3 bits,
97c8ac32e4SManuel Lauss 	 * resolution 0.0625 degrees celsius.
98a86e94dcSChristophe Leroy 	 *
99a86e94dcSChristophe Leroy 	 * LM71:
100a86e94dcSChristophe Leroy 	 * 14 bits of 2's complement data, discard LSB 2 bits,
101a86e94dcSChristophe Leroy 	 * resolution 0.0312 degrees celsius.
102e1a8e913SKaiwan N Billimoria 	 */
103c8ac32e4SManuel Lauss 	switch (p_lm70->chip) {
104c8ac32e4SManuel Lauss 	case LM70_CHIP_LM70:
105e1a8e913SKaiwan N Billimoria 		val = ((int)raw / 32) * 250;
106c8ac32e4SManuel Lauss 		break;
107c8ac32e4SManuel Lauss 
108c8ac32e4SManuel Lauss 	case LM70_CHIP_TMP121:
109a86e94dcSChristophe Leroy 	case LM70_CHIP_LM74:
110c8ac32e4SManuel Lauss 		val = ((int)raw / 8) * 625 / 10;
111c8ac32e4SManuel Lauss 		break;
112a86e94dcSChristophe Leroy 
113a86e94dcSChristophe Leroy 	case LM70_CHIP_LM71:
114a86e94dcSChristophe Leroy 		val = ((int)raw / 4) * 3125 / 100;
115a86e94dcSChristophe Leroy 		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 	{
146a1dc86ebSRabin Vincent 		.compatible = "ti,lm71",
147a1dc86ebSRabin Vincent 		.data = (void *) LM70_CHIP_LM71,
148a1dc86ebSRabin Vincent 	},
149a1dc86ebSRabin Vincent 	{
150a1dc86ebSRabin Vincent 		.compatible = "ti,lm74",
151a1dc86ebSRabin Vincent 		.data = (void *) LM70_CHIP_LM74,
152a1dc86ebSRabin Vincent 	},
153a1dc86ebSRabin Vincent 	{},
154a1dc86ebSRabin Vincent };
155a1dc86ebSRabin Vincent MODULE_DEVICE_TABLE(of, lm70_of_ids);
156a1dc86ebSRabin Vincent #endif
157a1dc86ebSRabin Vincent 
1586c931ae1SBill Pemberton static int lm70_probe(struct spi_device *spi)
159e1a8e913SKaiwan N Billimoria {
160a1dc86ebSRabin Vincent 	const struct of_device_id *match;
161aa9bcddaSGuenter Roeck 	struct device *hwmon_dev;
162e1a8e913SKaiwan N Billimoria 	struct lm70 *p_lm70;
163a1dc86ebSRabin Vincent 	int chip;
164a1dc86ebSRabin Vincent 
165a1dc86ebSRabin Vincent 	match = of_match_device(lm70_of_ids, &spi->dev);
166a1dc86ebSRabin Vincent 	if (match)
167a1dc86ebSRabin Vincent 		chip = (int)(uintptr_t)match->data;
168a1dc86ebSRabin Vincent 	else
169a1dc86ebSRabin Vincent 		chip = spi_get_device_id(spi)->driver_data;
170e1a8e913SKaiwan N Billimoria 
171a86e94dcSChristophe Leroy 	/* signaling is SPI_MODE_0 */
1728cec03eeSAnton Vorontsov 	if (spi->mode & (SPI_CPOL | SPI_CPHA))
1738cec03eeSAnton Vorontsov 		return -EINVAL;
1748cec03eeSAnton Vorontsov 
1752b730051SKaiwan N Billimoria 	/* NOTE:  we assume 8-bit words, and convert to 16 bits manually */
1762b730051SKaiwan N Billimoria 
17733ed6d4aSGuenter Roeck 	p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
178e1a8e913SKaiwan N Billimoria 	if (!p_lm70)
179e1a8e913SKaiwan N Billimoria 		return -ENOMEM;
180e1a8e913SKaiwan N Billimoria 
1814bfe6604SMatthias Kaehlcke 	mutex_init(&p_lm70->lock);
182c8ac32e4SManuel Lauss 	p_lm70->chip = chip;
183aa9bcddaSGuenter Roeck 	p_lm70->spi = spi;
184e1a8e913SKaiwan N Billimoria 
185aa9bcddaSGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
186aa9bcddaSGuenter Roeck 							   spi->modalias,
187aa9bcddaSGuenter Roeck 							   p_lm70, lm70_groups);
188aa9bcddaSGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
189e200c14fSGuenter Roeck }
190e200c14fSGuenter Roeck 
1918cec03eeSAnton Vorontsov static const struct spi_device_id lm70_ids[] = {
1928cec03eeSAnton Vorontsov 	{ "lm70",   LM70_CHIP_LM70 },
1938cec03eeSAnton Vorontsov 	{ "tmp121", LM70_CHIP_TMP121 },
194a86e94dcSChristophe Leroy 	{ "lm71",   LM70_CHIP_LM71 },
195a86e94dcSChristophe Leroy 	{ "lm74",   LM70_CHIP_LM74 },
1968cec03eeSAnton Vorontsov 	{ },
197c8ac32e4SManuel Lauss };
1988cec03eeSAnton Vorontsov MODULE_DEVICE_TABLE(spi, lm70_ids);
199c8ac32e4SManuel Lauss 
200e1a8e913SKaiwan N Billimoria static struct spi_driver lm70_driver = {
201e1a8e913SKaiwan N Billimoria 	.driver = {
202e1a8e913SKaiwan N Billimoria 		.name	= "lm70",
203a1dc86ebSRabin Vincent 		.of_match_table	= of_match_ptr(lm70_of_ids),
204e1a8e913SKaiwan N Billimoria 	},
2058cec03eeSAnton Vorontsov 	.id_table = lm70_ids,
206e1a8e913SKaiwan N Billimoria 	.probe	= lm70_probe,
207e1a8e913SKaiwan N Billimoria };
208e1a8e913SKaiwan N Billimoria 
20991efffe2SAxel Lin module_spi_driver(lm70_driver);
210e1a8e913SKaiwan N Billimoria 
211e1a8e913SKaiwan N Billimoria MODULE_AUTHOR("Kaiwan N Billimoria");
212a86e94dcSChristophe Leroy MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
213e1a8e913SKaiwan N Billimoria MODULE_LICENSE("GPL");
214