mm: update get_user_pages_longterm to migrate pages allocated from CMA region
[linux-2.6-block.git] / drivers / hwmon / ad7314.c
CommitLineData
4f3a6595
JC
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>
984faa1f 19#include <linux/bitops.h>
4f3a6595 20
4f3a6595
JC
21/*
22 * AD7314 temperature masks
23 */
4f3a6595 24#define AD7314_TEMP_MASK 0x7FE0
1137a9a6 25#define AD7314_TEMP_SHIFT 5
4f3a6595
JC
26
27/*
28 * ADT7301 and ADT7302 temperature masks
29 */
4f3a6595
JC
30#define ADT7301_TEMP_MASK 0x3FFF
31
32enum ad7314_variant {
33 adt7301,
34 adt7302,
35 ad7314,
36};
37
38struct ad7314_data {
39 struct spi_device *spi_dev;
4f3a6595
JC
40 u16 rx ____cacheline_aligned;
41};
42
eae1415d 43static int ad7314_spi_read(struct ad7314_data *chip)
4f3a6595
JC
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
eae1415d 53 return be16_to_cpu(chip->rx);
4f3a6595
JC
54}
55
1ba3e023
GR
56static ssize_t ad7314_temperature_show(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
4f3a6595
JC
59{
60 struct ad7314_data *chip = dev_get_drvdata(dev);
61 s16 data;
62 int ret;
63
eae1415d 64 ret = ad7314_spi_read(chip);
4f3a6595
JC
65 if (ret < 0)
66 return ret;
67 switch (spi_get_device_id(chip->spi_dev)->driver_data) {
68 case ad7314:
1137a9a6 69 data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
984faa1f 70 data = sign_extend32(data, 9);
4f3a6595
JC
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 */
eae1415d 80 data = ret & ADT7301_TEMP_MASK;
984faa1f 81 data = sign_extend32(data, 13);
4f3a6595
JC
82
83 return sprintf(buf, "%d\n",
84 DIV_ROUND_CLOSEST(data * 3125, 100));
85 default:
86 return -EINVAL;
87 }
88}
89
1ba3e023 90static SENSOR_DEVICE_ATTR_RO(temp1_input, ad7314_temperature, 0);
4f3a6595 91
157926c0 92static struct attribute *ad7314_attrs[] = {
4f3a6595
JC
93 &sensor_dev_attr_temp1_input.dev_attr.attr,
94 NULL,
95};
96
157926c0 97ATTRIBUTE_GROUPS(ad7314);
4f3a6595 98
6c931ae1 99static int ad7314_probe(struct spi_device *spi_dev)
4f3a6595 100{
4f3a6595 101 struct ad7314_data *chip;
157926c0 102 struct device *hwmon_dev;
4f3a6595 103
be923040
GR
104 chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL);
105 if (chip == NULL)
106 return -ENOMEM;
107
e16de913 108 chip->spi_dev = spi_dev;
157926c0
AL
109 hwmon_dev = devm_hwmon_device_register_with_groups(&spi_dev->dev,
110 spi_dev->modalias,
111 chip, ad7314_groups);
112 return PTR_ERR_OR_ZERO(hwmon_dev);
4f3a6595
JC
113}
114
115static const struct spi_device_id ad7314_id[] = {
116 { "adt7301", adt7301 },
117 { "adt7302", adt7302 },
118 { "ad7314", ad7314 },
119 { }
120};
121MODULE_DEVICE_TABLE(spi, ad7314_id);
122
123static struct spi_driver ad7314_driver = {
124 .driver = {
125 .name = "ad7314",
4f3a6595
JC
126 },
127 .probe = ad7314_probe,
4f3a6595
JC
128 .id_table = ad7314_id,
129};
130
91efffe2 131module_spi_driver(ad7314_driver);
4f3a6595
JC
132
133MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
b55f3757 134MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver");
4f3a6595 135MODULE_LICENSE("GPL v2");