blob: 7802bbf5f9587f1491f4617d17de77cbea4a0756 [file] [log] [blame]
Thomas Gleixner80503b22019-05-24 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jonathan Cameron4f3a6592011-09-29 12:50:04 -04002/*
3 * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
4 *
5 * Copyright 2010 Analog Devices Inc.
6 *
Jonathan Cameron4f3a6592011-09-29 12:50:04 -04007 * Conversion to hwmon from IIO done by Jonathan Cameron <jic23@cam.ac.uk>
8 */
9#include <linux/device.h>
10#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/sysfs.h>
13#include <linux/spi/spi.h>
14#include <linux/module.h>
15#include <linux/err.h>
16#include <linux/hwmon.h>
17#include <linux/hwmon-sysfs.h>
Rasmus Villemoes984faa12015-01-22 23:44:13 +010018#include <linux/bitops.h>
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040019
20/*
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040021 * AD7314 temperature masks
22 */
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040023#define AD7314_TEMP_MASK 0x7FE0
Guenter Roeck1137a9a2012-04-22 00:22:00 -040024#define AD7314_TEMP_SHIFT 5
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040025
26/*
27 * ADT7301 and ADT7302 temperature masks
28 */
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040029#define ADT7301_TEMP_MASK 0x3FFF
30
31enum ad7314_variant {
32 adt7301,
33 adt7302,
34 ad7314,
35};
36
37struct ad7314_data {
38 struct spi_device *spi_dev;
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040039 u16 rx ____cacheline_aligned;
40};
41
Guenter Roeckeae14152012-04-20 11:39:17 -040042static int ad7314_spi_read(struct ad7314_data *chip)
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040043{
44 int ret;
45
46 ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
47 if (ret < 0) {
48 dev_err(&chip->spi_dev->dev, "SPI read error\n");
49 return ret;
50 }
51
Guenter Roeckeae14152012-04-20 11:39:17 -040052 return be16_to_cpu(chip->rx);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040053}
54
Guenter Roeck1ba3e022018-12-10 14:01:59 -080055static ssize_t ad7314_temperature_show(struct device *dev,
56 struct device_attribute *attr,
57 char *buf)
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040058{
59 struct ad7314_data *chip = dev_get_drvdata(dev);
60 s16 data;
61 int ret;
62
Guenter Roeckeae14152012-04-20 11:39:17 -040063 ret = ad7314_spi_read(chip);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040064 if (ret < 0)
65 return ret;
66 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
67 case ad7314:
Guenter Roeck1137a9a2012-04-22 00:22:00 -040068 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
Rasmus Villemoes984faa12015-01-22 23:44:13 +010069 data = sign_extend32(data, 9);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040070
71 return sprintf(buf, "%d\n", 250 * data);
72 case adt7301:
73 case adt7302:
74 /*
75 * Documented as a 13 bit twos complement register
76 * with a sign bit - which is a 14 bit 2's complement
77 * register. 1lsb - 31.25 milli degrees centigrade
78 */
Guenter Roeckeae14152012-04-20 11:39:17 -040079 data = ret & ADT7301_TEMP_MASK;
Rasmus Villemoes984faa12015-01-22 23:44:13 +010080 data = sign_extend32(data, 13);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040081
82 return sprintf(buf, "%d\n",
83 DIV_ROUND_CLOSEST(data * 3125, 100));
84 default:
85 return -EINVAL;
86 }
87}
88
Guenter Roeck1ba3e022018-12-10 14:01:59 -080089static SENSOR_DEVICE_ATTR_RO(temp1_input, ad7314_temperature, 0);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040090
Axel Lin157926c2014-07-01 22:29:28 +080091static struct attribute *ad7314_attrs[] = {
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040092 &sensor_dev_attr_temp1_input.dev_attr.attr,
93 NULL,
94};
95
Axel Lin157926c2014-07-01 22:29:28 +080096ATTRIBUTE_GROUPS(ad7314);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040097
Bill Pemberton6c931ae2012-11-19 13:22:35 -050098static int ad7314_probe(struct spi_device *spi_dev)
Jonathan Cameron4f3a6592011-09-29 12:50:04 -040099{
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400100 struct ad7314_data *chip;
Axel Lin157926c2014-07-01 22:29:28 +0800101 struct device *hwmon_dev;
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400102
Guenter Roeckbe923042012-06-02 09:57:57 -0700103 chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
104 if (chip == NULL)
105 return -ENOMEM;
106
Graeme Smechere16de912012-04-03 19:42:21 -0400107 chip->spi_dev = spi_dev;
Axel Lin157926c2014-07-01 22:29:28 +0800108 hwmon_dev = devm_hwmon_device_register_with_groups(&spi_dev->dev,
109 spi_dev->modalias,
110 chip, ad7314_groups);
111 return PTR_ERR_OR_ZERO(hwmon_dev);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400112}
113
114static const struct spi_device_id ad7314_id[] = {
115 { "adt7301", adt7301 },
116 { "adt7302", adt7302 },
117 { "ad7314", ad7314 },
118 { }
119};
120MODULE_DEVICE_TABLE(spi, ad7314_id);
121
122static struct spi_driver ad7314_driver = {
123 .driver = {
124 .name = "ad7314",
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400125 },
126 .probe = ad7314_probe,
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400127 .id_table = ad7314_id,
128};
129
Axel Lin91efffe2012-01-20 15:53:47 +0800130module_spi_driver(ad7314_driver);
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400131
132MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
Guenter Roeckb55f3752013-01-10 10:01:24 -0800133MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
Jonathan Cameron4f3a6592011-09-29 12:50:04 -0400134MODULE_LICENSE("GPL v2");