xref: /openbmc/linux/drivers/hwmon/lm70.c (revision 89cb4af8)
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) {
755713017eSJoe Perches 		pr_warn("spi_write_then_read failed with status %d\n", status);
76e1a8e913SKaiwan N Billimoria 		goto out;
77e1a8e913SKaiwan N Billimoria 	}
782b730051SKaiwan N Billimoria 	raw = (rxbuf[0] << 8) + rxbuf[1];
792b730051SKaiwan N Billimoria 	dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
802b730051SKaiwan N Billimoria 		rxbuf[0], rxbuf[1], raw);
81e1a8e913SKaiwan N Billimoria 
82e1a8e913SKaiwan N Billimoria 	/*
83c8ac32e4SManuel Lauss 	 * LM70:
84e1a8e913SKaiwan N Billimoria 	 * The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
85e1a8e913SKaiwan N Billimoria 	 * complement value. Only the MSB 11 bits (1 sign + 10 temperature
86e1a8e913SKaiwan N Billimoria 	 * bits) are meaningful; the LSB 5 bits are to be discarded.
87e1a8e913SKaiwan N Billimoria 	 * See the datasheet.
88e1a8e913SKaiwan N Billimoria 	 *
89e1a8e913SKaiwan N Billimoria 	 * Further, each bit represents 0.25 degrees Celsius; so, multiply
90e1a8e913SKaiwan N Billimoria 	 * by 0.25. Also multiply by 1000 to represent in millidegrees
91e1a8e913SKaiwan N Billimoria 	 * Celsius.
92e1a8e913SKaiwan N Billimoria 	 * So it's equivalent to multiplying by 0.25 * 1000 = 250.
93c8ac32e4SManuel Lauss 	 *
94a86e94dcSChristophe Leroy 	 * LM74 and TMP121/TMP123:
95c8ac32e4SManuel Lauss 	 * 13 bits of 2's complement data, discard LSB 3 bits,
96c8ac32e4SManuel Lauss 	 * resolution 0.0625 degrees celsius.
97a86e94dcSChristophe Leroy 	 *
98a86e94dcSChristophe Leroy 	 * LM71:
99a86e94dcSChristophe Leroy 	 * 14 bits of 2's complement data, discard LSB 2 bits,
100a86e94dcSChristophe Leroy 	 * resolution 0.0312 degrees celsius.
101e1a8e913SKaiwan N Billimoria 	 */
102c8ac32e4SManuel Lauss 	switch (p_lm70->chip) {
103c8ac32e4SManuel Lauss 	case LM70_CHIP_LM70:
104e1a8e913SKaiwan N Billimoria 		val = ((int)raw / 32) * 250;
105c8ac32e4SManuel Lauss 		break;
106c8ac32e4SManuel Lauss 
107c8ac32e4SManuel Lauss 	case LM70_CHIP_TMP121:
108a86e94dcSChristophe Leroy 	case LM70_CHIP_LM74:
109c8ac32e4SManuel Lauss 		val = ((int)raw / 8) * 625 / 10;
110c8ac32e4SManuel Lauss 		break;
111a86e94dcSChristophe Leroy 
112a86e94dcSChristophe Leroy 	case LM70_CHIP_LM71:
113a86e94dcSChristophe Leroy 		val = ((int)raw / 4) * 3125 / 100;
114a86e94dcSChristophe Leroy 		break;
115c8ac32e4SManuel Lauss 	}
116c8ac32e4SManuel Lauss 
11767f921d1SJean Delvare 	status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
118e1a8e913SKaiwan N Billimoria out:
1194bfe6604SMatthias Kaehlcke 	mutex_unlock(&p_lm70->lock);
120e1a8e913SKaiwan N Billimoria 	return status;
121e1a8e913SKaiwan N Billimoria }
122e1a8e913SKaiwan N Billimoria 
12389cb4af8SJulia Lawall static DEVICE_ATTR_RO(temp1_input);
124e1a8e913SKaiwan N Billimoria 
125aa9bcddaSGuenter Roeck static struct attribute *lm70_attrs[] = {
126aa9bcddaSGuenter Roeck 	&dev_attr_temp1_input.attr,
127aa9bcddaSGuenter Roeck 	NULL
128aa9bcddaSGuenter Roeck };
12967f921d1SJean Delvare 
130aa9bcddaSGuenter Roeck ATTRIBUTE_GROUPS(lm70);
13167f921d1SJean Delvare 
132e1a8e913SKaiwan N Billimoria /*----------------------------------------------------------------------*/
133e1a8e913SKaiwan N Billimoria 
134a1dc86ebSRabin Vincent #ifdef CONFIG_OF
135a1dc86ebSRabin Vincent static const struct of_device_id lm70_of_ids[] = {
136a1dc86ebSRabin Vincent 	{
137a1dc86ebSRabin Vincent 		.compatible = "ti,lm70",
138a1dc86ebSRabin Vincent 		.data = (void *) LM70_CHIP_LM70,
139a1dc86ebSRabin Vincent 	},
140a1dc86ebSRabin Vincent 	{
141a1dc86ebSRabin Vincent 		.compatible = "ti,tmp121",
142a1dc86ebSRabin Vincent 		.data = (void *) LM70_CHIP_TMP121,
143a1dc86ebSRabin Vincent 	},
144a1dc86ebSRabin Vincent 	{
145a1dc86ebSRabin Vincent 		.compatible = "ti,lm71",
146a1dc86ebSRabin Vincent 		.data = (void *) LM70_CHIP_LM71,
147a1dc86ebSRabin Vincent 	},
148a1dc86ebSRabin Vincent 	{
149a1dc86ebSRabin Vincent 		.compatible = "ti,lm74",
150a1dc86ebSRabin Vincent 		.data = (void *) LM70_CHIP_LM74,
151a1dc86ebSRabin Vincent 	},
152a1dc86ebSRabin Vincent 	{},
153a1dc86ebSRabin Vincent };
154a1dc86ebSRabin Vincent MODULE_DEVICE_TABLE(of, lm70_of_ids);
155a1dc86ebSRabin Vincent #endif
156a1dc86ebSRabin Vincent 
1576c931ae1SBill Pemberton static int lm70_probe(struct spi_device *spi)
158e1a8e913SKaiwan N Billimoria {
159a1dc86ebSRabin Vincent 	const struct of_device_id *match;
160aa9bcddaSGuenter Roeck 	struct device *hwmon_dev;
161e1a8e913SKaiwan N Billimoria 	struct lm70 *p_lm70;
162a1dc86ebSRabin Vincent 	int chip;
163a1dc86ebSRabin Vincent 
164a1dc86ebSRabin Vincent 	match = of_match_device(lm70_of_ids, &spi->dev);
165a1dc86ebSRabin Vincent 	if (match)
166a1dc86ebSRabin Vincent 		chip = (int)(uintptr_t)match->data;
167a1dc86ebSRabin Vincent 	else
168a1dc86ebSRabin Vincent 		chip = spi_get_device_id(spi)->driver_data;
169e1a8e913SKaiwan N Billimoria 
170a86e94dcSChristophe Leroy 	/* signaling is SPI_MODE_0 */
1718cec03eeSAnton Vorontsov 	if (spi->mode & (SPI_CPOL | SPI_CPHA))
1728cec03eeSAnton Vorontsov 		return -EINVAL;
1738cec03eeSAnton Vorontsov 
1742b730051SKaiwan N Billimoria 	/* NOTE:  we assume 8-bit words, and convert to 16 bits manually */
1752b730051SKaiwan N Billimoria 
17633ed6d4aSGuenter Roeck 	p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
177e1a8e913SKaiwan N Billimoria 	if (!p_lm70)
178e1a8e913SKaiwan N Billimoria 		return -ENOMEM;
179e1a8e913SKaiwan N Billimoria 
1804bfe6604SMatthias Kaehlcke 	mutex_init(&p_lm70->lock);
181c8ac32e4SManuel Lauss 	p_lm70->chip = chip;
182aa9bcddaSGuenter Roeck 	p_lm70->spi = spi;
183e1a8e913SKaiwan N Billimoria 
184aa9bcddaSGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
185aa9bcddaSGuenter Roeck 							   spi->modalias,
186aa9bcddaSGuenter Roeck 							   p_lm70, lm70_groups);
187aa9bcddaSGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
188e200c14fSGuenter Roeck }
189e200c14fSGuenter Roeck 
1908cec03eeSAnton Vorontsov static const struct spi_device_id lm70_ids[] = {
1918cec03eeSAnton Vorontsov 	{ "lm70",   LM70_CHIP_LM70 },
1928cec03eeSAnton Vorontsov 	{ "tmp121", LM70_CHIP_TMP121 },
193a86e94dcSChristophe Leroy 	{ "lm71",   LM70_CHIP_LM71 },
194a86e94dcSChristophe Leroy 	{ "lm74",   LM70_CHIP_LM74 },
1958cec03eeSAnton Vorontsov 	{ },
196c8ac32e4SManuel Lauss };
1978cec03eeSAnton Vorontsov MODULE_DEVICE_TABLE(spi, lm70_ids);
198c8ac32e4SManuel Lauss 
199e1a8e913SKaiwan N Billimoria static struct spi_driver lm70_driver = {
200e1a8e913SKaiwan N Billimoria 	.driver = {
201e1a8e913SKaiwan N Billimoria 		.name	= "lm70",
202a1dc86ebSRabin Vincent 		.of_match_table	= of_match_ptr(lm70_of_ids),
203e1a8e913SKaiwan N Billimoria 	},
2048cec03eeSAnton Vorontsov 	.id_table = lm70_ids,
205e1a8e913SKaiwan N Billimoria 	.probe	= lm70_probe,
206e1a8e913SKaiwan N Billimoria };
207e1a8e913SKaiwan N Billimoria 
20891efffe2SAxel Lin module_spi_driver(lm70_driver);
209e1a8e913SKaiwan N Billimoria 
210e1a8e913SKaiwan N Billimoria MODULE_AUTHOR("Kaiwan N Billimoria");
211a86e94dcSChristophe Leroy MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
212e1a8e913SKaiwan N Billimoria MODULE_LICENSE("GPL");
213