123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/device.h>
- #include <linux/err.h>
- #include <linux/sysfs.h>
- #include <linux/hwmon.h>
- #include <linux/mutex.h>
- #include <linux/mod_devicetable.h>
- #include <linux/spi/spi.h>
- #include <linux/slab.h>
- #include <linux/of_device.h>
- #define DRVNAME "lm70"
- #define LM70_CHIP_LM70 0
- #define LM70_CHIP_TMP121 1
- #define LM70_CHIP_LM71 2
- #define LM70_CHIP_LM74 3
- struct lm70 {
- struct spi_device *spi;
- struct mutex lock;
- unsigned int chip;
- };
- static ssize_t lm70_sense_temp(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct lm70 *p_lm70 = dev_get_drvdata(dev);
- struct spi_device *spi = p_lm70->spi;
- int status, val = 0;
- u8 rxbuf[2];
- s16 raw = 0;
- if (mutex_lock_interruptible(&p_lm70->lock))
- return -ERESTARTSYS;
-
- status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
- if (status < 0) {
- pr_warn("spi_write_then_read failed with status %d\n", status);
- goto out;
- }
- raw = (rxbuf[0] << 8) + rxbuf[1];
- dev_dbg(dev, "rxbuf[0] : 0x%02x rxbuf[1] : 0x%02x raw=0x%04x\n",
- rxbuf[0], rxbuf[1], raw);
-
- switch (p_lm70->chip) {
- case LM70_CHIP_LM70:
- val = ((int)raw / 32) * 250;
- break;
- case LM70_CHIP_TMP121:
- case LM70_CHIP_LM74:
- val = ((int)raw / 8) * 625 / 10;
- break;
- case LM70_CHIP_LM71:
- val = ((int)raw / 4) * 3125 / 100;
- break;
- }
- status = sprintf(buf, "%d\n", val);
- out:
- mutex_unlock(&p_lm70->lock);
- return status;
- }
- static DEVICE_ATTR(temp1_input, S_IRUGO, lm70_sense_temp, NULL);
- static struct attribute *lm70_attrs[] = {
- &dev_attr_temp1_input.attr,
- NULL
- };
- ATTRIBUTE_GROUPS(lm70);
- #ifdef CONFIG_OF
- static const struct of_device_id lm70_of_ids[] = {
- {
- .compatible = "ti,lm70",
- .data = (void *) LM70_CHIP_LM70,
- },
- {
- .compatible = "ti,tmp121",
- .data = (void *) LM70_CHIP_TMP121,
- },
- {
- .compatible = "ti,lm71",
- .data = (void *) LM70_CHIP_LM71,
- },
- {
- .compatible = "ti,lm74",
- .data = (void *) LM70_CHIP_LM74,
- },
- {},
- };
- MODULE_DEVICE_TABLE(of, lm70_of_ids);
- #endif
- static int lm70_probe(struct spi_device *spi)
- {
- const struct of_device_id *match;
- struct device *hwmon_dev;
- struct lm70 *p_lm70;
- int chip;
- match = of_match_device(lm70_of_ids, &spi->dev);
- if (match)
- chip = (int)(uintptr_t)match->data;
- else
- chip = spi_get_device_id(spi)->driver_data;
-
- if (spi->mode & (SPI_CPOL | SPI_CPHA))
- return -EINVAL;
-
- p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
- if (!p_lm70)
- return -ENOMEM;
- mutex_init(&p_lm70->lock);
- p_lm70->chip = chip;
- p_lm70->spi = spi;
- hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
- spi->modalias,
- p_lm70, lm70_groups);
- return PTR_ERR_OR_ZERO(hwmon_dev);
- }
- static const struct spi_device_id lm70_ids[] = {
- { "lm70", LM70_CHIP_LM70 },
- { "tmp121", LM70_CHIP_TMP121 },
- { "lm71", LM70_CHIP_LM71 },
- { "lm74", LM70_CHIP_LM74 },
- { },
- };
- MODULE_DEVICE_TABLE(spi, lm70_ids);
- static struct spi_driver lm70_driver = {
- .driver = {
- .name = "lm70",
- .of_match_table = of_match_ptr(lm70_of_ids),
- },
- .id_table = lm70_ids,
- .probe = lm70_probe,
- };
- module_spi_driver(lm70_driver);
- MODULE_AUTHOR("Kaiwan N Billimoria");
- MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
- MODULE_LICENSE("GPL");
|