• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ADT7310/ADT7310 digital temperature sensor driver
3  *
4  * Copyright 2012-2013 Analog Devices Inc.
5  *   Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2 or later.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/spi/spi.h>
13 #include <asm/unaligned.h>
14 
15 #include "adt7x10.h"
16 
17 #define ADT7310_STATUS			0
18 #define ADT7310_CONFIG			1
19 #define ADT7310_TEMPERATURE		2
20 #define ADT7310_ID			3
21 #define ADT7310_T_CRIT			4
22 #define ADT7310_T_HYST			5
23 #define ADT7310_T_ALARM_HIGH		6
24 #define ADT7310_T_ALARM_LOW		7
25 
26 static const u8 adt7310_reg_table[] = {
27 	[ADT7X10_TEMPERATURE]   = ADT7310_TEMPERATURE,
28 	[ADT7X10_STATUS]	= ADT7310_STATUS,
29 	[ADT7X10_CONFIG]	= ADT7310_CONFIG,
30 	[ADT7X10_T_ALARM_HIGH]	= ADT7310_T_ALARM_HIGH,
31 	[ADT7X10_T_ALARM_LOW]	= ADT7310_T_ALARM_LOW,
32 	[ADT7X10_T_CRIT]	= ADT7310_T_CRIT,
33 	[ADT7X10_T_HYST]	= ADT7310_T_HYST,
34 	[ADT7X10_ID]		= ADT7310_ID,
35 };
36 
37 #define ADT7310_CMD_REG_OFFSET	3
38 #define ADT7310_CMD_READ	0x40
39 
40 #define AD7310_COMMAND(reg) (adt7310_reg_table[(reg)] << ADT7310_CMD_REG_OFFSET)
41 
adt7310_spi_read_word(struct device * dev,u8 reg)42 static int adt7310_spi_read_word(struct device *dev, u8 reg)
43 {
44 	struct spi_device *spi = to_spi_device(dev);
45 	int ret;
46 
47 	ret = spi_w8r16(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
48 	if (ret < 0)
49 		return ret;
50 
51 	return be16_to_cpu((__force __be16)ret);
52 }
53 
adt7310_spi_write_word(struct device * dev,u8 reg,u16 data)54 static int adt7310_spi_write_word(struct device *dev, u8 reg, u16 data)
55 {
56 	struct spi_device *spi = to_spi_device(dev);
57 	u8 buf[3];
58 
59 	buf[0] = AD7310_COMMAND(reg);
60 	put_unaligned_be16(data, &buf[1]);
61 
62 	return spi_write(spi, buf, sizeof(buf));
63 }
64 
adt7310_spi_read_byte(struct device * dev,u8 reg)65 static int adt7310_spi_read_byte(struct device *dev, u8 reg)
66 {
67 	struct spi_device *spi = to_spi_device(dev);
68 
69 	return spi_w8r8(spi, AD7310_COMMAND(reg) | ADT7310_CMD_READ);
70 }
71 
adt7310_spi_write_byte(struct device * dev,u8 reg,u8 data)72 static int adt7310_spi_write_byte(struct device *dev, u8 reg,
73 	u8 data)
74 {
75 	struct spi_device *spi = to_spi_device(dev);
76 	u8 buf[2];
77 
78 	buf[0] = AD7310_COMMAND(reg);
79 	buf[1] = data;
80 
81 	return spi_write(spi, buf, sizeof(buf));
82 }
83 
84 static const struct adt7x10_ops adt7310_spi_ops = {
85 	.read_word = adt7310_spi_read_word,
86 	.write_word = adt7310_spi_write_word,
87 	.read_byte = adt7310_spi_read_byte,
88 	.write_byte = adt7310_spi_write_byte,
89 };
90 
adt7310_spi_probe(struct spi_device * spi)91 static int adt7310_spi_probe(struct spi_device *spi)
92 {
93 	return adt7x10_probe(&spi->dev, spi_get_device_id(spi)->name, spi->irq,
94 			&adt7310_spi_ops);
95 }
96 
adt7310_spi_remove(struct spi_device * spi)97 static int adt7310_spi_remove(struct spi_device *spi)
98 {
99 	return adt7x10_remove(&spi->dev, spi->irq);
100 }
101 
102 static const struct spi_device_id adt7310_id[] = {
103 	{ "adt7310", 0 },
104 	{ "adt7320", 0 },
105 	{}
106 };
107 MODULE_DEVICE_TABLE(spi, adt7310_id);
108 
109 static struct spi_driver adt7310_driver = {
110 	.driver = {
111 		.name	= "adt7310",
112 		.owner	= THIS_MODULE,
113 		.pm	= ADT7X10_DEV_PM_OPS,
114 	},
115 	.probe		= adt7310_spi_probe,
116 	.remove		= adt7310_spi_remove,
117 	.id_table	= adt7310_id,
118 };
119 module_spi_driver(adt7310_driver);
120 
121 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
122 MODULE_DESCRIPTION("ADT7310/ADT7320 driver");
123 MODULE_LICENSE("GPL");
124