Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 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> |
Rasmus Villemoes | 984faa1 | 2015-01-22 23:44:13 +0100 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 20 | |
| 21 | /* |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 22 | * AD7314 temperature masks |
| 23 | */ |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 24 | #define AD7314_TEMP_MASK 0x7FE0 |
Guenter Roeck | 1137a9a | 2012-04-22 00:22:00 -0400 | [diff] [blame] | 25 | #define AD7314_TEMP_SHIFT 5 |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * ADT7301 and ADT7302 temperature masks |
| 29 | */ |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 30 | #define ADT7301_TEMP_MASK 0x3FFF |
| 31 | |
| 32 | enum ad7314_variant { |
| 33 | adt7301, |
| 34 | adt7302, |
| 35 | ad7314, |
| 36 | }; |
| 37 | |
| 38 | struct ad7314_data { |
| 39 | struct spi_device *spi_dev; |
| 40 | struct device *hwmon_dev; |
| 41 | u16 rx ____cacheline_aligned; |
| 42 | }; |
| 43 | |
Guenter Roeck | eae1415 | 2012-04-20 11:39:17 -0400 | [diff] [blame] | 44 | static int ad7314_spi_read(struct ad7314_data *chip) |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 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 | |
Guenter Roeck | eae1415 | 2012-04-20 11:39:17 -0400 | [diff] [blame] | 54 | return be16_to_cpu(chip->rx); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static ssize_t ad7314_show_temperature(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 | |
Guenter Roeck | eae1415 | 2012-04-20 11:39:17 -0400 | [diff] [blame] | 65 | ret = ad7314_spi_read(chip); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 66 | if (ret < 0) |
| 67 | return ret; |
| 68 | switch (spi_get_device_id(chip->spi_dev)->driver_data) { |
| 69 | case ad7314: |
Guenter Roeck | 1137a9a | 2012-04-22 00:22:00 -0400 | [diff] [blame] | 70 | data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT; |
Rasmus Villemoes | 984faa1 | 2015-01-22 23:44:13 +0100 | [diff] [blame] | 71 | data = sign_extend32(data, 9); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 72 | |
| 73 | return sprintf(buf, "%d\n", 250 * data); |
| 74 | case adt7301: |
| 75 | case adt7302: |
| 76 | /* |
| 77 | * Documented as a 13 bit twos complement register |
| 78 | * with a sign bit - which is a 14 bit 2's complement |
| 79 | * register. 1lsb - 31.25 milli degrees centigrade |
| 80 | */ |
Guenter Roeck | eae1415 | 2012-04-20 11:39:17 -0400 | [diff] [blame] | 81 | data = ret & ADT7301_TEMP_MASK; |
Rasmus Villemoes | 984faa1 | 2015-01-22 23:44:13 +0100 | [diff] [blame] | 82 | data = sign_extend32(data, 13); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 83 | |
| 84 | return sprintf(buf, "%d\n", |
| 85 | DIV_ROUND_CLOSEST(data * 3125, 100)); |
| 86 | default: |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | } |
| 90 | |
Guenter Roeck | 3ceefe4 | 2012-09-11 13:43:17 -0700 | [diff] [blame] | 91 | static ssize_t ad7314_show_name(struct device *dev, |
| 92 | struct device_attribute *devattr, char *buf) |
| 93 | { |
| 94 | return sprintf(buf, "%s\n", to_spi_device(dev)->modalias); |
| 95 | } |
| 96 | |
| 97 | static DEVICE_ATTR(name, S_IRUGO, ad7314_show_name, NULL); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 98 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, |
| 99 | ad7314_show_temperature, NULL, 0); |
| 100 | |
| 101 | static struct attribute *ad7314_attributes[] = { |
Guenter Roeck | 3ceefe4 | 2012-09-11 13:43:17 -0700 | [diff] [blame] | 102 | &dev_attr_name.attr, |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 103 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 104 | NULL, |
| 105 | }; |
| 106 | |
| 107 | static const struct attribute_group ad7314_group = { |
| 108 | .attrs = ad7314_attributes, |
| 109 | }; |
| 110 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 111 | static int ad7314_probe(struct spi_device *spi_dev) |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 112 | { |
| 113 | int ret; |
| 114 | struct ad7314_data *chip; |
| 115 | |
Guenter Roeck | be92304 | 2012-06-02 09:57:57 -0700 | [diff] [blame] | 116 | chip = devm_kzalloc(&spi_dev->dev, sizeof(*chip), GFP_KERNEL); |
| 117 | if (chip == NULL) |
| 118 | return -ENOMEM; |
| 119 | |
Jingoo Han | debe597 | 2013-04-05 10:51:29 +0900 | [diff] [blame] | 120 | spi_set_drvdata(spi_dev, chip); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 121 | |
| 122 | ret = sysfs_create_group(&spi_dev->dev.kobj, &ad7314_group); |
| 123 | if (ret < 0) |
Guenter Roeck | be92304 | 2012-06-02 09:57:57 -0700 | [diff] [blame] | 124 | return ret; |
| 125 | |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 126 | chip->hwmon_dev = hwmon_device_register(&spi_dev->dev); |
| 127 | if (IS_ERR(chip->hwmon_dev)) { |
| 128 | ret = PTR_ERR(chip->hwmon_dev); |
| 129 | goto error_remove_group; |
| 130 | } |
Graeme Smecher | e16de91 | 2012-04-03 19:42:21 -0400 | [diff] [blame] | 131 | chip->spi_dev = spi_dev; |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | error_remove_group: |
| 135 | sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 136 | return ret; |
| 137 | } |
| 138 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 139 | static int ad7314_remove(struct spi_device *spi_dev) |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 140 | { |
Jingoo Han | debe597 | 2013-04-05 10:51:29 +0900 | [diff] [blame] | 141 | struct ad7314_data *chip = spi_get_drvdata(spi_dev); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 142 | |
| 143 | hwmon_device_unregister(chip->hwmon_dev); |
| 144 | sysfs_remove_group(&spi_dev->dev.kobj, &ad7314_group); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static const struct spi_device_id ad7314_id[] = { |
| 150 | { "adt7301", adt7301 }, |
| 151 | { "adt7302", adt7302 }, |
| 152 | { "ad7314", ad7314 }, |
| 153 | { } |
| 154 | }; |
| 155 | MODULE_DEVICE_TABLE(spi, ad7314_id); |
| 156 | |
| 157 | static struct spi_driver ad7314_driver = { |
| 158 | .driver = { |
| 159 | .name = "ad7314", |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 160 | .owner = THIS_MODULE, |
| 161 | }, |
| 162 | .probe = ad7314_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 163 | .remove = ad7314_remove, |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 164 | .id_table = ad7314_id, |
| 165 | }; |
| 166 | |
Axel Lin | 91efffe | 2012-01-20 15:53:47 +0800 | [diff] [blame] | 167 | module_spi_driver(ad7314_driver); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 168 | |
| 169 | MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 170 | MODULE_DESCRIPTION("Analog Devices AD7314, ADT7301 and ADT7302 digital temperature sensor driver"); |
Jonathan Cameron | 4f3a659 | 2011-09-29 12:50:04 -0400 | [diff] [blame] | 171 | MODULE_LICENSE("GPL v2"); |