Skip to content

Commit

Permalink
hwmon: (lm70) Add TI TMP121 support
Browse files Browse the repository at this point in the history
The Texas Instruments TMP121 is a SPI temperature sensor very similar
to the LM70, with slightly higher resolution.  This patch extends the
LM70 driver to support the TMP121.  The TMP123 differs in pin assign-
ment.

Signed-off-by: Manuel Lauss <mano@roarinelk.homelinux.net>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
  • Loading branch information
Manuel Lauss authored and Jean Delvare committed Jan 7, 2009
1 parent 2b73005 commit c8ac32e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
8 changes: 7 additions & 1 deletion Documentation/hwmon/lm70
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Kernel driver lm70
==================

Supported chip:
Supported chips:
* National Semiconductor LM70
Datasheet: http://www.national.com/pf/LM/LM70.html
* Texas Instruments TMP121/TMP123
Information: http://focus.ti.com/docs/prod/folders/print/tmp121.html

Author:
Kaiwan N Billimoria <kaiwan@designergraphix.com>
Expand All @@ -29,6 +31,10 @@ As a real (in-tree) example of this "SPI protocol driver" interfacing
with a "SPI master controller driver", see drivers/spi/spi_lm70llp.c
and its associated documentation.

The TMP121/TMP123 are very similar; main differences are 4 wire SPI inter-
face (read only) and 13-bit temperature data (0.0625 degrees celsius reso-
lution).

Thanks to
---------
Jean Delvare <khali@linux-fr.org> for mentoring the hwmon-side driver
Expand Down
5 changes: 3 additions & 2 deletions drivers/hwmon/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,12 @@ config SENSORS_LM63
will be called lm63.

config SENSORS_LM70
tristate "National Semiconductor LM70"
tristate "National Semiconductor LM70 / Texas Instruments TMP121"
depends on SPI_MASTER && EXPERIMENTAL
help
If you say yes here you get support for the National Semiconductor
LM70 digital temperature sensor chip.
LM70 and Texas Instruments TMP121/TMP123 digital temperature
sensor chips.

This driver can also be built as a module. If so, the module
will be called lm70.
Expand Down
84 changes: 74 additions & 10 deletions drivers/hwmon/lm70.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@

#define DRVNAME "lm70"

#define LM70_CHIP_LM70 0 /* original NS LM70 */
#define LM70_CHIP_TMP121 1 /* TI TMP121/TMP123 */

struct lm70 {
struct device *hwmon_dev;
struct mutex lock;
unsigned int chip;
};

/* sysfs hook function */
static ssize_t lm70_sense_temp(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct spi_device *spi = to_spi_device(dev);
int status, val;
int status, val = 0;
u8 rxbuf[2];
s16 raw=0;
struct lm70 *p_lm70 = dev_get_drvdata(&spi->dev);
Expand All @@ -70,6 +74,7 @@ static ssize_t lm70_sense_temp(struct device *dev,
rxbuf[0], rxbuf[1], raw);

/*
* LM70:
* 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.
Expand All @@ -79,8 +84,21 @@ static ssize_t lm70_sense_temp(struct device *dev,
* by 0.25. Also multiply by 1000 to represent in millidegrees
* Celsius.
* So it's equivalent to multiplying by 0.25 * 1000 = 250.
*
* TMP121/TMP123:
* 13 bits of 2's complement data, discard LSB 3 bits,
* resolution 0.0625 degrees celsius.
*/
val = ((int)raw/32) * 250;
switch (p_lm70->chip) {
case LM70_CHIP_LM70:
val = ((int)raw / 32) * 250;
break;

case LM70_CHIP_TMP121:
val = ((int)raw / 8) * 625 / 10;
break;
}

status = sprintf(buf, "%d\n", val); /* millidegrees Celsius */
out:
mutex_unlock(&p_lm70->lock);
Expand All @@ -92,29 +110,39 @@ static DEVICE_ATTR(temp1_input, S_IRUGO, lm70_sense_temp, NULL);
static ssize_t lm70_show_name(struct device *dev, struct device_attribute
*devattr, char *buf)
{
return sprintf(buf, "lm70\n");
struct lm70 *p_lm70 = dev_get_drvdata(dev);
int ret;

switch (p_lm70->chip) {
case LM70_CHIP_LM70:
ret = sprintf(buf, "lm70\n");
break;
case LM70_CHIP_TMP121:
ret = sprintf(buf, "tmp121\n");
break;
default:
ret = -EINVAL;
}
return ret;
}

static DEVICE_ATTR(name, S_IRUGO, lm70_show_name, NULL);

/*----------------------------------------------------------------------*/

static int __devinit lm70_probe(struct spi_device *spi)
static int __devinit common_probe(struct spi_device *spi, int chip)
{
struct lm70 *p_lm70;
int status;

/* signaling is SPI_MODE_0 on a 3-wire link (shared SI/SO) */
if ((spi->mode & (SPI_CPOL|SPI_CPHA)) || !(spi->mode & SPI_3WIRE))
return -EINVAL;

/* NOTE: we assume 8-bit words, and convert to 16 bits manually */

p_lm70 = kzalloc(sizeof *p_lm70, GFP_KERNEL);
if (!p_lm70)
return -ENOMEM;

mutex_init(&p_lm70->lock);
p_lm70->chip = chip;

/* sysfs hook */
p_lm70->hwmon_dev = hwmon_device_register(&spi->dev);
Expand Down Expand Up @@ -142,6 +170,24 @@ static int __devinit lm70_probe(struct spi_device *spi)
return status;
}

static int __devinit lm70_probe(struct spi_device *spi)
{
/* signaling is SPI_MODE_0 on a 3-wire link (shared SI/SO) */
if ((spi->mode & (SPI_CPOL | SPI_CPHA)) || !(spi->mode & SPI_3WIRE))
return -EINVAL;

return common_probe(spi, LM70_CHIP_LM70);
}

static int __devinit tmp121_probe(struct spi_device *spi)
{
/* signaling is SPI_MODE_0 with only MISO connected */
if (spi->mode & (SPI_CPOL | SPI_CPHA))
return -EINVAL;

return common_probe(spi, LM70_CHIP_TMP121);
}

static int __devexit lm70_remove(struct spi_device *spi)
{
struct lm70 *p_lm70 = dev_get_drvdata(&spi->dev);
Expand All @@ -155,6 +201,15 @@ static int __devexit lm70_remove(struct spi_device *spi)
return 0;
}

static struct spi_driver tmp121_driver = {
.driver = {
.name = "tmp121",
.owner = THIS_MODULE,
},
.probe = tmp121_probe,
.remove = __devexit_p(lm70_remove),
};

static struct spi_driver lm70_driver = {
.driver = {
.name = "lm70",
Expand All @@ -166,17 +221,26 @@ static struct spi_driver lm70_driver = {

static int __init init_lm70(void)
{
return spi_register_driver(&lm70_driver);
int ret = spi_register_driver(&lm70_driver);
if (ret)
return ret;

ret = spi_register_driver(&tmp121_driver);
if (ret)
spi_unregister_driver(&lm70_driver);

return ret;
}

static void __exit cleanup_lm70(void)
{
spi_unregister_driver(&lm70_driver);
spi_unregister_driver(&tmp121_driver);
}

module_init(init_lm70);
module_exit(cleanup_lm70);

MODULE_AUTHOR("Kaiwan N Billimoria");
MODULE_DESCRIPTION("National Semiconductor LM70 Linux driver");
MODULE_DESCRIPTION("NS LM70 / TI TMP121/TMP123 Linux driver");
MODULE_LICENSE("GPL");

0 comments on commit c8ac32e

Please sign in to comment.