0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0033 #define LM70_CHIP_TMP121 1
0034 #define LM70_CHIP_LM71 2
0035 #define LM70_CHIP_LM74 3
0036 #define LM70_CHIP_TMP122 4
0037 #define LM70_CHIP_TMP125 5
0038
0039 struct lm70 {
0040 struct spi_device *spi;
0041 struct mutex lock;
0042 unsigned int chip;
0043 };
0044
0045
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
0060
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
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
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);
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
0179 if ((spi->mode & SPI_MODE_X_MASK) != SPI_MODE_0)
0180 return -EINVAL;
0181
0182
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");