Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
4 *
5 * Copyright 2010 Analog Devices Inc.
6 *
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>
18#include <linux/bitops.h>
19
20/*
21 * AD7314 temperature masks
22 */
23#define AD7314_TEMP_MASK 0x7FE0
24#define AD7314_TEMP_SHIFT 5
25#define AD7314_LEADING_ZEROS_MASK BIT(15)
26
27/*
28 * ADT7301 and ADT7302 temperature masks
29 */
30#define ADT7301_TEMP_MASK 0x3FFF
31#define ADT7301_LEADING_ZEROS_MASK (BIT(15) | BIT(14))
32
33enum ad7314_variant {
34 adt7301,
35 adt7302,
36 ad7314,
37};
38
39struct ad7314_data {
40 struct spi_device *spi_dev;
41 u16 rx ____cacheline_aligned;
42};
43
44static int ad7314_spi_read(struct ad7314_data *chip)
45{
46 int ret;
47
48 ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
49 if (ret < 0) {
50 dev_err(&chip->spi_dev->dev, "SPI read error\n");
51 return ret;
52 }
53
54 return be16_to_cpu(chip->rx);
55}
56
57static ssize_t ad7314_temperature_show(struct device *dev,
58 struct device_attribute *attr,
59 char *buf)
60{
61 struct ad7314_data *chip = dev_get_drvdata(dev);
62 s16 data;
63 int ret;
64
65 ret = ad7314_spi_read(chip);
66 if (ret < 0)
67 return ret;
68 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
69 case ad7314:
70 if (ret & AD7314_LEADING_ZEROS_MASK) {
71 /* Invalid read-out, leading zero part is missing */
72 return -EIO;
73 }
74 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
75 data = sign_extend32(data, 9);
76
77 return sprintf(buf, "%d\n", 250 * data);
78 case adt7301:
79 case adt7302:
80 if (ret & ADT7301_LEADING_ZEROS_MASK) {
81 /* Invalid read-out, leading zero part is missing */
82 return -EIO;
83 }
84 /*
85 * Documented as a 13 bit twos complement register
86 * with a sign bit - which is a 14 bit 2's complement
87 * register. 1lsb - 31.25 milli degrees centigrade
88 */
89 data = ret & ADT7301_TEMP_MASK;
90 data = sign_extend32(data, 13);
91
92 return sprintf(buf, "%d\n",
93 DIV_ROUND_CLOSEST(data * 3125, 100));
94 default:
95 return -EINVAL;
96 }
97}
98
99static SENSOR_DEVICE_ATTR_RO(temp1_input, ad7314_temperature, 0);
100
101static struct attribute *ad7314_attrs[] = {
102 &sensor_dev_attr_temp1_input.dev_attr.attr,
103 NULL,
104};
105
106ATTRIBUTE_GROUPS(ad7314);
107
108static int ad7314_probe(struct spi_device *spi_dev)
109{
110 struct ad7314_data *chip;
111 struct device *hwmon_dev;
112
113 chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
114 if (chip == NULL)
115 return -ENOMEM;
116
117 chip->spi_dev = spi_dev;
118 hwmon_dev = devm_hwmon_device_register_with_groups(&spi_dev->dev,
119 spi_dev->modalias,
120 chip, ad7314_groups);
121 return PTR_ERR_OR_ZERO(hwmon_dev);
122}
123
124static const struct spi_device_id ad7314_id[] = {
125 { "adt7301", adt7301 },
126 { "adt7302", adt7302 },
127 { "ad7314", ad7314 },
128 { }
129};
130MODULE_DEVICE_TABLE(spi, ad7314_id);
131
132static struct spi_driver ad7314_driver = {
133 .driver = {
134 .name = "ad7314",
135 },
136 .probe = ad7314_probe,
137 .id_table = ad7314_id,
138};
139
140module_spi_driver(ad7314_driver);
141
142MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
143MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
144MODULE_LICENSE("GPL v2");
1/*
2 * AD7314 digital temperature sensor driver for AD7314, ADT7301 and ADT7302
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 *
8 * Conversion to hwmon from IIO done by Jonathan Cameron <jic23@cam.ac.uk>
9 */
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/sysfs.h>
14#include <linux/spi/spi.h>
15#include <linux/module.h>
16#include <linux/err.h>
17#include <linux/hwmon.h>
18#include <linux/hwmon-sysfs.h>
19
20/*
21 * AD7314 temperature masks
22 */
23#define AD7314_TEMP_MASK 0x7FE0
24#define AD7314_TEMP_SHIFT 5
25
26/*
27 * ADT7301 and ADT7302 temperature masks
28 */
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;
39 struct device *hwmon_dev;
40 u16 rx ____cacheline_aligned;
41};
42
43static int ad7314_spi_read(struct ad7314_data *chip)
44{
45 int ret;
46
47 ret = spi_read(chip->spi_dev, (u8 *)&chip->rx, sizeof(chip->rx));
48 if (ret < 0) {
49 dev_err(&chip->spi_dev->dev, "SPI read error\n");
50 return ret;
51 }
52
53 return be16_to_cpu(chip->rx);
54}
55
56static ssize_t ad7314_show_temperature(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
59{
60 struct ad7314_data *chip = dev_get_drvdata(dev);
61 s16 data;
62 int ret;
63
64 ret = ad7314_spi_read(chip);
65 if (ret < 0)
66 return ret;
67 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
68 case ad7314:
69 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
70 data = (data << 6) >> 6;
71
72 return sprintf(buf, "%d\n", 250 * data);
73 case adt7301:
74 case adt7302:
75 /*
76 * Documented as a 13 bit twos complement register
77 * with a sign bit - which is a 14 bit 2's complement
78 * register. 1lsb - 31.25 milli degrees centigrade
79 */
80 data = ret & ADT7301_TEMP_MASK;
81 data = (data << 2) >> 2;
82
83 return sprintf(buf, "%d\n",
84 DIV_ROUND_CLOSEST(data * 3125, 100));
85 default:
86 return -EINVAL;
87 }
88}
89
90static ssize_t ad7314_show_name(struct device *dev,
91 struct device_attribute *devattr, char *buf)
92{
93 return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
94}
95
96static DEVICE_ATTR(name, S_IRUGO, ad7314_show_name, NULL);
97static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
98 ad7314_show_temperature, NULL, 0);
99
100static struct attribute *ad7314_attributes[] = {
101 &dev_attr_name.attr,
102 &sensor_dev_attr_temp1_input.dev_attr.attr,
103 NULL,
104};
105
106static const struct attribute_group ad7314_group = {
107 .attrs = ad7314_attributes,
108};
109
110static int __devinit ad7314_probe(struct spi_device *spi_dev)
111{
112 int ret;
113 struct ad7314_data *chip;
114
115 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
116 if (chip == NULL) {
117 ret = -ENOMEM;
118 goto error_ret;
119 }
120 dev_set_drvdata(&spi_dev->dev, chip);
121
122 ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group);
123 if (ret < 0)
124 goto error_free_chip;
125 chip->hwmon_dev = hwmon_device_register(&spi_dev->dev);
126 if (IS_ERR(chip->hwmon_dev)) {
127 ret = PTR_ERR(chip->hwmon_dev);
128 goto error_remove_group;
129 }
130 chip->spi_dev = spi_dev;
131
132 return 0;
133error_remove_group:
134 sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
135error_free_chip:
136 kfree(chip);
137error_ret:
138 return ret;
139}
140
141static int __devexit ad7314_remove(struct spi_device *spi_dev)
142{
143 struct ad7314_data *chip = dev_get_drvdata(&spi_dev->dev);
144
145 hwmon_device_unregister(chip->hwmon_dev);
146 sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group);
147 kfree(chip);
148
149 return 0;
150}
151
152static const struct spi_device_id ad7314_id[] = {
153 { "adt7301", adt7301 },
154 { "adt7302", adt7302 },
155 { "ad7314", ad7314 },
156 { }
157};
158MODULE_DEVICE_TABLE(spi, ad7314_id);
159
160static struct spi_driver ad7314_driver = {
161 .driver = {
162 .name = "ad7314",
163 .owner = THIS_MODULE,
164 },
165 .probe = ad7314_probe,
166 .remove = __devexit_p(ad7314_remove),
167 .id_table = ad7314_id,
168};
169
170module_spi_driver(ad7314_driver);
171
172MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
173MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital"
174 " temperature sensor driver");
175MODULE_LICENSE("GPL v2");