2019-05-20 10:19:02 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2006-06-13 00:00:05 +04:00
|
|
|
/*
|
|
|
|
* lm70.c
|
|
|
|
*
|
|
|
|
* The LM70 is a temperature sensor chip from National Semiconductor (NS).
|
|
|
|
* Copyright (C) 2006 Kaiwan N Billimoria <kaiwan@designergraphix.com>
|
|
|
|
*
|
|
|
|
* The LM70 communicates with a host processor via an SPI/Microwire Bus
|
|
|
|
* interface. The complete datasheet is available at National's website
|
|
|
|
* here:
|
|
|
|
* http://www.national.com/pf/LM/LM70.html
|
|
|
|
*/
|
|
|
|
|
2010-10-20 10:51:41 +04:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2006-06-13 00:00:05 +04:00
|
|
|
#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>
|
2007-10-24 16:59:09 +04:00
|
|
|
#include <linux/mutex.h>
|
2009-09-23 03:46:07 +04:00
|
|
|
#include <linux/mod_devicetable.h>
|
2006-06-13 00:00:05 +04:00
|
|
|
#include <linux/spi/spi.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
|
|
|
#include <linux/slab.h>
|
2015-07-19 01:29:14 +03:00
|
|
|
#include <linux/of_device.h>
|
2020-04-23 16:27:33 +03:00
|
|
|
#include <linux/acpi.h>
|
2006-06-13 00:00:05 +04:00
|
|
|
|
|
|
|
#define DRVNAME "lm70"
|
|
|
|
|
2009-01-07 18:37:34 +03:00
|
|
|
#define LM70_CHIP_LM70 0 /* original NS LM70 */
|
|
|
|
#define LM70_CHIP_TMP121 1 /* TI TMP121/TMP123 */
|
2012-09-05 13:49:14 +04:00
|
|
|
#define LM70_CHIP_LM71 2 /* NS LM71 */
|
|
|
|
#define LM70_CHIP_LM74 3 /* NS LM74 */
|
2017-01-21 22:20:10 +03:00
|
|
|
#define LM70_CHIP_TMP122 4 /* TI TMP122/TMP124 */
|
2009-01-07 18:37:34 +03:00
|
|
|
|
2006-06-13 00:00:05 +04:00
|
|
|
struct lm70 {
|
2014-04-06 08:44:47 +04:00
|
|
|
struct spi_device *spi;
|
2007-10-24 16:59:09 +04:00
|
|
|
struct mutex lock;
|
2009-01-07 18:37:34 +03:00
|
|
|
unsigned int chip;
|
2006-06-13 00:00:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* sysfs hook function */
|
2016-12-22 15:04:50 +03:00
|
|
|
static ssize_t temp1_input_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2006-06-13 00:00:05 +04:00
|
|
|
{
|
2014-04-06 08:44:47 +04:00
|
|
|
struct lm70 *p_lm70 = dev_get_drvdata(dev);
|
|
|
|
struct spi_device *spi = p_lm70->spi;
|
2009-01-07 18:37:34 +03:00
|
|
|
int status, val = 0;
|
2006-06-13 00:00:05 +04:00
|
|
|
u8 rxbuf[2];
|
2012-01-08 22:34:11 +04:00
|
|
|
s16 raw = 0;
|
2006-06-13 00:00:05 +04:00
|
|
|
|
2007-10-24 16:59:09 +04:00
|
|
|
if (mutex_lock_interruptible(&p_lm70->lock))
|
2006-06-13 00:00:05 +04:00
|
|
|
return -ERESTARTSYS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* spi_read() requires a DMA-safe buffer; so we use
|
|
|
|
* spi_write_then_read(), transmitting 0 bytes.
|
|
|
|
*/
|
|
|
|
status = spi_write_then_read(spi, NULL, 0, &rxbuf[0], 2);
|
|
|
|
if (status < 0) {
|
2017-01-21 22:20:09 +03:00
|
|
|
dev_warn(dev, "spi_write_then_read failed with status %d\n",
|
|
|
|
status);
|
2006-06-13 00:00:05 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2009-01-07 18:37:34 +03:00
|
|
|
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);
|
2006-06-13 00:00:05 +04:00
|
|
|
|
|
|
|
/*
|
2009-01-07 18:37:34 +03:00
|
|
|
* LM70:
|
2006-06-13 00:00:05 +04:00
|
|
|
* The "raw" temperature read into rxbuf[] is a 16-bit signed 2's
|
|
|
|
* complement value. Only the MSB 11 bits (1 sign + 10 temperature
|
|
|
|
* bits) are meaningful; the LSB 5 bits are to be discarded.
|
|
|
|
* See the datasheet.
|
|
|
|
*
|
|
|
|
* Further, each bit represents 0.25 degrees Celsius; so, multiply
|
|
|
|
* by 0.25. Also multiply by 1000 to represent in millidegrees
|
|
|
|
* Celsius.
|
|
|
|
* So it's equivalent to multiplying by 0.25 * 1000 = 250.
|
2009-01-07 18:37:34 +03:00
|
|
|
*
|
2017-01-21 22:20:10 +03:00
|
|
|
* LM74 and TMP121/TMP122/TMP123/TMP124:
|
2009-01-07 18:37:34 +03:00
|
|
|
* 13 bits of 2's complement data, discard LSB 3 bits,
|
|
|
|
* resolution 0.0625 degrees celsius.
|
2012-09-05 13:49:14 +04:00
|
|
|
*
|
|
|
|
* LM71:
|
|
|
|
* 14 bits of 2's complement data, discard LSB 2 bits,
|
|
|
|
* resolution 0.0312 degrees celsius.
|
2006-06-13 00:00:05 +04:00
|
|
|
*/
|
2009-01-07 18:37:34 +03:00
|
|
|
switch (p_lm70->chip) {
|
|
|
|
case LM70_CHIP_LM70:
|
|
|
|
val = ((int)raw / 32) * 250;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM70_CHIP_TMP121:
|
2017-01-21 22:20:10 +03:00
|
|
|
case LM70_CHIP_TMP122:
|
2012-09-05 13:49:14 +04:00
|
|
|
case LM70_CHIP_LM74:
|
2009-01-07 18:37:34 +03:00
|
|
|
val = ((int)raw / 8) * 625 / 10;
|
|
|
|
break;
|
2012-09-05 13:49:14 +04:00
|
|
|
|
|
|
|
case LM70_CHIP_LM71:
|
|
|
|
val = ((int)raw / 4) * 3125 / 100;
|
|
|
|
break;
|
2009-01-07 18:37:34 +03:00
|
|
|
}
|
|
|
|
|
2007-08-17 19:31:35 +04:00
|
|
|
status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
|
2006-06-13 00:00:05 +04:00
|
|
|
out:
|
2007-10-24 16:59:09 +04:00
|
|
|
mutex_unlock(&p_lm70->lock);
|
2006-06-13 00:00:05 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:04:50 +03:00
|
|
|
static DEVICE_ATTR_RO(temp1_input);
|
2006-06-13 00:00:05 +04:00
|
|
|
|
2014-04-06 08:44:47 +04:00
|
|
|
static struct attribute *lm70_attrs[] = {
|
|
|
|
&dev_attr_temp1_input.attr,
|
|
|
|
NULL
|
|
|
|
};
|
2007-08-17 19:31:35 +04:00
|
|
|
|
2014-04-06 08:44:47 +04:00
|
|
|
ATTRIBUTE_GROUPS(lm70);
|
2007-08-17 19:31:35 +04:00
|
|
|
|
2006-06-13 00:00:05 +04:00
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
|
2015-07-19 01:29:14 +03:00
|
|
|
#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,
|
|
|
|
},
|
2017-01-21 22:20:10 +03:00
|
|
|
{
|
|
|
|
.compatible = "ti,tmp122",
|
|
|
|
.data = (void *) LM70_CHIP_TMP122,
|
|
|
|
},
|
2015-07-19 01:29:14 +03:00
|
|
|
{
|
|
|
|
.compatible = "ti,lm71",
|
|
|
|
.data = (void *) LM70_CHIP_LM71,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.compatible = "ti,lm74",
|
|
|
|
.data = (void *) LM70_CHIP_LM74,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, lm70_of_ids);
|
|
|
|
#endif
|
|
|
|
|
2020-04-23 16:27:33 +03:00
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
static const struct acpi_device_id lm70_acpi_ids[] = {
|
|
|
|
{
|
|
|
|
.id = "LM000070",
|
|
|
|
.driver_data = LM70_CHIP_LM70,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.id = "TMP00121",
|
|
|
|
.driver_data = LM70_CHIP_TMP121,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.id = "LM000071",
|
|
|
|
.driver_data = LM70_CHIP_LM71,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.id = "LM000074",
|
|
|
|
.driver_data = LM70_CHIP_LM74,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(acpi, lm70_acpi_ids);
|
|
|
|
#endif
|
|
|
|
|
2012-11-19 22:22:35 +04:00
|
|
|
static int lm70_probe(struct spi_device *spi)
|
2006-06-13 00:00:05 +04:00
|
|
|
{
|
2020-04-23 16:27:33 +03:00
|
|
|
const struct of_device_id *of_match;
|
2014-04-06 08:44:47 +04:00
|
|
|
struct device *hwmon_dev;
|
2006-06-13 00:00:05 +04:00
|
|
|
struct lm70 *p_lm70;
|
2015-07-19 01:29:14 +03:00
|
|
|
int chip;
|
|
|
|
|
2020-04-23 16:27:33 +03:00
|
|
|
of_match = of_match_device(lm70_of_ids, &spi->dev);
|
|
|
|
if (of_match)
|
|
|
|
chip = (int)(uintptr_t)of_match->data;
|
|
|
|
else {
|
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
const struct acpi_device_id *acpi_match;
|
|
|
|
|
|
|
|
acpi_match = acpi_match_device(lm70_acpi_ids, &spi->dev);
|
|
|
|
if (acpi_match)
|
|
|
|
chip = (int)(uintptr_t)acpi_match->driver_data;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
chip = spi_get_device_id(spi)->driver_data;
|
|
|
|
}
|
2006-06-13 00:00:05 +04:00
|
|
|
|
2012-09-05 13:49:14 +04:00
|
|
|
/* signaling is SPI_MODE_0 */
|
2009-09-23 03:46:07 +04:00
|
|
|
if (spi->mode & (SPI_CPOL | SPI_CPHA))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-01-07 18:37:34 +03:00
|
|
|
/* NOTE: we assume 8-bit words, and convert to 16 bits manually */
|
|
|
|
|
2012-06-02 20:58:08 +04:00
|
|
|
p_lm70 = devm_kzalloc(&spi->dev, sizeof(*p_lm70), GFP_KERNEL);
|
2006-06-13 00:00:05 +04:00
|
|
|
if (!p_lm70)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-10-24 16:59:09 +04:00
|
|
|
mutex_init(&p_lm70->lock);
|
2009-01-07 18:37:34 +03:00
|
|
|
p_lm70->chip = chip;
|
2014-04-06 08:44:47 +04:00
|
|
|
p_lm70->spi = spi;
|
2006-06-13 00:00:05 +04:00
|
|
|
|
2014-04-06 08:44:47 +04:00
|
|
|
hwmon_dev = devm_hwmon_device_register_with_groups(&spi->dev,
|
|
|
|
spi->modalias,
|
|
|
|
p_lm70, lm70_groups);
|
|
|
|
return PTR_ERR_OR_ZERO(hwmon_dev);
|
2006-06-13 00:00:05 +04:00
|
|
|
}
|
|
|
|
|
2009-09-23 03:46:07 +04:00
|
|
|
static const struct spi_device_id lm70_ids[] = {
|
|
|
|
{ "lm70", LM70_CHIP_LM70 },
|
|
|
|
{ "tmp121", LM70_CHIP_TMP121 },
|
2017-01-21 22:20:10 +03:00
|
|
|
{ "tmp122", LM70_CHIP_TMP122 },
|
2012-09-05 13:49:14 +04:00
|
|
|
{ "lm71", LM70_CHIP_LM71 },
|
|
|
|
{ "lm74", LM70_CHIP_LM74 },
|
2009-09-23 03:46:07 +04:00
|
|
|
{ },
|
2009-01-07 18:37:34 +03:00
|
|
|
};
|
2009-09-23 03:46:07 +04:00
|
|
|
MODULE_DEVICE_TABLE(spi, lm70_ids);
|
2009-01-07 18:37:34 +03:00
|
|
|
|
2006-06-13 00:00:05 +04:00
|
|
|
static struct spi_driver lm70_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "lm70",
|
2015-07-19 01:29:14 +03:00
|
|
|
.of_match_table = of_match_ptr(lm70_of_ids),
|
2020-04-23 16:27:33 +03:00
|
|
|
.acpi_match_table = ACPI_PTR(lm70_acpi_ids),
|
2006-06-13 00:00:05 +04:00
|
|
|
},
|
2009-09-23 03:46:07 +04:00
|
|
|
.id_table = lm70_ids,
|
2006-06-13 00:00:05 +04:00
|
|
|
.probe = lm70_probe,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:53:47 +04:00
|
|
|
module_spi_driver(lm70_driver);
|
2006-06-13 00:00:05 +04:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Kaiwan N Billimoria");
|
2012-09-05 13:49:14 +04:00
|
|
|
MODULE_DESCRIPTION("NS LM70 and compatibles Linux driver");
|
2006-06-13 00:00:05 +04:00
|
|
|
MODULE_LICENSE("GPL");
|