Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * lm70.c
0004  *
0005  * The LM70 is a temperature sensor chip from National Semiconductor (NS).
0006  * Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
0007  *
0008  * The LM70 communicates with a host processor via an SPI/Microwire Bus
0009  * interface. The complete datasheet is available at National's website
0010  * here:
0011  * http://www.national.com/pf/LM/LM70.html
0012  */
0013 
0014 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0015 
0016 #include <linux/init.h>
0017 #include <linux/module.h>
0018 #include <linux/kernel.h>
0019 #include <linux/device.h>
0020 #include <linux/err.h>
0021 #include <linux/sysfs.h>
0022 #include <linux/hwmon.h>
0023 #include <linux/mutex.h>
0024 #include <linux/mod_devicetable.h>
0025 #include <linux/of.h>
0026 #include <linux/property.h>
0027 #include <linux/spi/spi.h>
0028 #include <linux/slab.h>
0029 
0030 #define DRVNAME     "lm70"
0031 
0032 #define LM70_CHIP_LM70      0   /* original NS LM70 */
0033 #define LM70_CHIP_TMP121    1   /* TI TMP121/TMP123 */
0034 #define LM70_CHIP_LM71      2   /* NS LM71 */
0035 #define LM70_CHIP_LM74      3   /* NS LM74 */
0036 #define LM70_CHIP_TMP122    4   /* TI TMP122/TMP124 */
0037 #define LM70_CHIP_TMP125    5   /* TI TMP125 */
0038 
0039 struct lm70 {
0040     struct spi_device *spi;
0041     struct mutex lock;
0042     unsigned int chip;
0043 };
0044 
0045 /* sysfs hook function */
0046 static ssize_t temp1_input_show(struct device *dev,
0047                 struct device_attribute *attr, char *buf)
0048 {
0049     struct lm70 *p_lm70 = dev_get_drvdata(dev);
0050     struct spi_device *spi = p_lm70->spi;
0051     int status, val = 0;
0052     u8 rxbuf[2];
0053     s16 raw = 0;
0054 
0055     if (mutex_lock_interruptible(&p_lm70->lock))
0056         return -ERESTARTSYS;
0057 
0058     /*
0059      * spi_read() requires a DMA-safe buffer; so we use
0060      * spi_write_then_read(), transmitting 0 bytes.
0061      */
0062     status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
0063     if (status < 0) {
0064         dev_warn(dev, "spi_write_then_read failed with status %d\n",
0065              status);
0066         goto out;
0067     }
0068     raw = (rxbuf[0] << 8) + rxbuf[1];
0069     dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
0070         rxbuf[0], rxbuf[1], raw);
0071 
0072     /*
0073      * LM70:
0074      * The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
0075      * complement value. Only the MSB 11 bits (1 sign + 10 temperature
0076      * bits) are meaningful; the LSB 5 bits are to be discarded.
0077      * See the datasheet.
0078      *
0079      * Further, each bit represents 0.25 degrees Celsius; so, multiply
0080      * by 0.25. Also multiply by 1000 to represent in millidegrees
0081      * Celsius.
0082      * So it's equivalent to multiplying by 0.25 * 1000 = 250.
0083      *
0084      * LM74 and TMP121/TMP122/TMP123/TMP124:
0085      * 13 bits of 2's complement data, discard LSB 3 bits,
0086      * resolution 0.0625 degrees celsius.
0087      *
0088      * LM71:
0089      * 14 bits of 2's complement data, discard LSB 2 bits,
0090      * resolution 0.0312 degrees celsius.
0091      *
0092      * TMP125:
0093      * MSB/D15 is a leading zero. D14 is the sign-bit. This is
0094      * followed by 9 temperature bits (D13..D5) in 2's complement
0095      * data format with a resolution of 0.25 degrees celsius per unit.
0096      * LSB 5 bits (D4..D0) share the same value as D5 and get discarded.
0097      */
0098     switch (p_lm70->chip) {
0099     case LM70_CHIP_LM70:
0100         val = ((int)raw / 32) * 250;
0101         break;
0102 
0103     case LM70_CHIP_TMP121:
0104     case LM70_CHIP_TMP122:
0105     case LM70_CHIP_LM74:
0106         val = ((int)raw / 8) * 625 / 10;
0107         break;
0108 
0109     case LM70_CHIP_LM71:
0110         val = ((int)raw / 4) * 3125 / 100;
0111         break;
0112 
0113     case LM70_CHIP_TMP125:
0114         val = (sign_extend32(raw, 14) / 32) * 250;
0115         break;
0116     }
0117 
0118     status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
0119 out:
0120     mutex_unlock(&p_lm70->lock);
0121     return status;
0122 }
0123 
0124 static DEVICE_ATTR_RO(temp1_input);
0125 
0126 static struct attribute *lm70_attrs[] = {
0127     &dev_attr_temp1_input.attr,
0128     NULL
0129 };
0130 
0131 ATTRIBUTE_GROUPS(lm70);
0132 
0133 /*----------------------------------------------------------------------*/
0134 
0135 #ifdef CONFIG_OF
0136 static const struct of_device_id lm70_of_ids[] = {
0137     {
0138         .compatible = "ti,lm70",
0139         .data = (void *) LM70_CHIP_LM70,
0140     },
0141     {
0142         .compatible = "ti,tmp121",
0143         .data = (void *) LM70_CHIP_TMP121,
0144     },
0145     {
0146         .compatible = "ti,tmp122",
0147         .data = (void *) LM70_CHIP_TMP122,
0148     },
0149     {
0150         .compatible = "ti,tmp125",
0151         .data = (void *) LM70_CHIP_TMP125,
0152     },
0153     {
0154         .compatible = "ti,lm71",
0155         .data = (void *) LM70_CHIP_LM71,
0156     },
0157     {
0158         .compatible = "ti,lm74",
0159         .data = (void *) LM70_CHIP_LM74,
0160     },
0161     {},
0162 };
0163 MODULE_DEVICE_TABLE(of, lm70_of_ids);
0164 #endif
0165 
0166 static int lm70_probe(struct spi_device *spi)
0167 {
0168     struct device *hwmon_dev;
0169     struct lm70 *p_lm70;
0170     int chip;
0171 
0172     if (dev_fwnode(&spi->dev))
0173         chip = (int)(uintptr_t)device_get_match_data(&spi->dev);
0174     else
0175         chip = spi_get_device_id(spi)->driver_data;
0176 
0177 
0178     /* signaling is SPI_MODE_0 */
0179     if ((spi->mode & SPI_MODE_X_MASK) != SPI_MODE_0)
0180         return -EINVAL;
0181 
0182     /* NOTE:  we assume 8-bit words, and convert to 16 bits manually */
0183 
0184     p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
0185     if (!p_lm70)
0186         return -ENOMEM;
0187 
0188     mutex_init(&p_lm70->lock);
0189     p_lm70->chip = chip;
0190     p_lm70->spi = spi;
0191 
0192     hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
0193                                spi->modalias,
0194                                p_lm70, lm70_groups);
0195     return PTR_ERR_OR_ZERO(hwmon_dev);
0196 }
0197 
0198 static const struct spi_device_id lm70_ids[] = {
0199     { "lm70",   LM70_CHIP_LM70 },
0200     { "tmp121", LM70_CHIP_TMP121 },
0201     { "tmp122", LM70_CHIP_TMP122 },
0202     { "tmp125", LM70_CHIP_TMP125 },
0203     { "lm71",   LM70_CHIP_LM71 },
0204     { "lm74",   LM70_CHIP_LM74 },
0205     { },
0206 };
0207 MODULE_DEVICE_TABLE(spi, lm70_ids);
0208 
0209 static struct spi_driver lm70_driver = {
0210     .driver = {
0211         .name   = "lm70",
0212         .of_match_table = of_match_ptr(lm70_of_ids),
0213     },
0214     .id_table = lm70_ids,
0215     .probe  = lm70_probe,
0216 };
0217 
0218 module_spi_driver(lm70_driver);
0219 
0220 MODULE_AUTHOR("Kaiwan N Billimoria");
0221 MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
0222 MODULE_LICENSE("GPL");