Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / drivers / hwmon / ad7314.c
CommitLineData
80503b23 1// SPDX-License-Identifier: GPL-2.0-or-later
4f3a6595
JC
2/*
3 * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
4 *
5 * Copyright 2010 Analog Devices Inc.
6 *
4f3a6595
JC
7 * 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>
984faa1f 18#include <linux/bitops.h>
4f3a6595 19
4f3a6595
JC
20/*
21 * AD7314 temperature masks
22 */
4f3a6595 23#define AD7314_TEMP_MASK 0x7FE0
1137a9a6 24#define AD7314_TEMP_SHIFT 5
4f3a6595
JC
25
26/*
27 * ADT7301 and ADT7302 temperature masks
28 */
4f3a6595
JC
29#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;
4f3a6595
JC
39 u16 rx ____cacheline_aligned;
40};
41
eae1415d 42static int ad7314_spi_read(struct ad7314_data *chip)
4f3a6595
JC
43{
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
eae1415d 52 return be16_to_cpu(chip->rx);
4f3a6595
JC
53}
54
1ba3e023
GR
55static ssize_t ad7314_temperature_show(struct device *dev,
56 struct device_attribute *attr,
57 char *buf)
4f3a6595
JC
58{
59 struct ad7314_data *chip = dev_get_drvdata(dev);
60 s16 data;
61 int ret;
62
eae1415d 63 ret = ad7314_spi_read(chip);
4f3a6595
JC
64 if (ret < 0)
65 return ret;
66 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
67 case ad7314:
1137a9a6 68 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
984faa1f 69 data = sign_extend32(data, 9);
4f3a6595
JC
70
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 */
eae1415d 79 data = ret & ADT7301_TEMP_MASK;
984faa1f 80 data = sign_extend32(data, 13);
4f3a6595
JC
81
82 return sprintf(buf, "%d\n",
83 DIV_ROUND_CLOSEST(data * 3125, 100));
84 default:
85 return -EINVAL;
86 }
87}
88
1ba3e023 89static SENSOR_DEVICE_ATTR_RO(temp1_input, ad7314_temperature, 0);
4f3a6595 90
157926c0 91static struct attribute *ad7314_attrs[] = {
4f3a6595
JC
92 &sensor_dev_attr_temp1_input.dev_attr.attr,
93 NULL,
94};
95
157926c0 96ATTRIBUTE_GROUPS(ad7314);
4f3a6595 97
6c931ae1 98static int ad7314_probe(struct spi_device *spi_dev)
4f3a6595 99{
4f3a6595 100 struct ad7314_data *chip;
157926c0 101 struct device *hwmon_dev;
4f3a6595 102
be923040
GR
103 chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
104 if (chip == NULL)
105 return -ENOMEM;
106
e16de913 107 chip->spi_dev = spi_dev;
157926c0
AL
108 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);
4f3a6595
JC
112}
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",
4f3a6595
JC
125 },
126 .probe = ad7314_probe,
4f3a6595
JC
127 .id_table = ad7314_id,
128};
129
91efffe2 130module_spi_driver(ad7314_driver);
4f3a6595
JC
131
132MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
b55f3757 133MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
4f3a6595 134MODULE_LICENSE("GPL v2");