1 /*
2  * API bus driver for ADT7316/7/8 ADT7516/7/9 digital temperature
3  * sensor, ADC and DAC
4  *
5  * Copyright 2010 Analog Devices Inc.
6  *
7  * Licensed under the GPL-2 or later.
8  */
9 
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/spi/spi.h>
15 
16 #include "adt7316.h"
17 
18 #define ADT7316_SPI_MAX_FREQ_HZ		5000000
19 #define ADT7316_SPI_CMD_READ		0x91
20 #define ADT7316_SPI_CMD_WRITE		0x90
21 
22 /*
23  * adt7316 register access by SPI
24  */
25 
adt7316_spi_multi_read(void * client,u8 reg,u8 count,u8 * data)26 static int adt7316_spi_multi_read(void *client, u8 reg, u8 count, u8 *data)
27 {
28 	struct spi_device *spi_dev = client;
29 	u8 cmd[2];
30 	int ret = 0;
31 
32 	if (count > ADT7316_REG_MAX_ADDR)
33 		count = ADT7316_REG_MAX_ADDR;
34 
35 	cmd[0] = ADT7316_SPI_CMD_WRITE;
36 	cmd[1] = reg;
37 
38 	ret = spi_write(spi_dev, cmd, 2);
39 	if (ret < 0) {
40 		dev_err(&spi_dev->dev, "SPI fail to select reg\n");
41 		return ret;
42 	}
43 
44 	cmd[0] = ADT7316_SPI_CMD_READ;
45 
46 	ret = spi_write_then_read(spi_dev, cmd, 1, data, count);
47 	if (ret < 0) {
48 		dev_err(&spi_dev->dev, "SPI read data error\n");
49 		return ret;
50 	}
51 
52 	return 0;
53 }
54 
adt7316_spi_multi_write(void * client,u8 reg,u8 count,u8 * data)55 static int adt7316_spi_multi_write(void *client, u8 reg, u8 count, u8 *data)
56 {
57 	struct spi_device *spi_dev = client;
58 	u8 buf[ADT7316_REG_MAX_ADDR + 2];
59 	int i, ret = 0;
60 
61 	if (count > ADT7316_REG_MAX_ADDR)
62 		count = ADT7316_REG_MAX_ADDR;
63 
64 	buf[0] = ADT7316_SPI_CMD_WRITE;
65 	buf[1] = reg;
66 	for (i = 0; i < count; i++)
67 		buf[i + 2] = data[i];
68 
69 	ret = spi_write(spi_dev, buf, count + 2);
70 	if (ret < 0) {
71 		dev_err(&spi_dev->dev, "SPI write error\n");
72 		return ret;
73 	}
74 
75 	return ret;
76 }
77 
adt7316_spi_read(void * client,u8 reg,u8 * data)78 static int adt7316_spi_read(void *client, u8 reg, u8 *data)
79 {
80 	return adt7316_spi_multi_read(client, reg, 1, data);
81 }
82 
adt7316_spi_write(void * client,u8 reg,u8 val)83 static int adt7316_spi_write(void *client, u8 reg, u8 val)
84 {
85 	return adt7316_spi_multi_write(client, reg, 1, &val);
86 }
87 
88 /*
89  * device probe and remove
90  */
91 
adt7316_spi_probe(struct spi_device * spi_dev)92 static int __devinit adt7316_spi_probe(struct spi_device *spi_dev)
93 {
94 	struct adt7316_bus bus = {
95 		.client = spi_dev,
96 		.irq = spi_dev->irq,
97 		.irq_flags = IRQF_TRIGGER_LOW,
98 		.read = adt7316_spi_read,
99 		.write = adt7316_spi_write,
100 		.multi_read = adt7316_spi_multi_read,
101 		.multi_write = adt7316_spi_multi_write,
102 	};
103 
104 	/* don't exceed max specified SPI CLK frequency */
105 	if (spi_dev->max_speed_hz > ADT7316_SPI_MAX_FREQ_HZ) {
106 		dev_err(&spi_dev->dev, "SPI CLK %d Hz?\n",
107 			spi_dev->max_speed_hz);
108 		return -EINVAL;
109 	}
110 
111 	/* switch from default I2C protocol to SPI protocol */
112 	adt7316_spi_write(spi_dev, 0, 0);
113 	adt7316_spi_write(spi_dev, 0, 0);
114 	adt7316_spi_write(spi_dev, 0, 0);
115 
116 	return adt7316_probe(&spi_dev->dev, &bus, spi_dev->modalias);
117 }
118 
adt7316_spi_remove(struct spi_device * spi_dev)119 static int __devexit adt7316_spi_remove(struct spi_device *spi_dev)
120 {
121 	return adt7316_remove(&spi_dev->dev);
122 }
123 
124 static const struct spi_device_id adt7316_spi_id[] = {
125 	{ "adt7316", 0 },
126 	{ "adt7317", 0 },
127 	{ "adt7318", 0 },
128 	{ "adt7516", 0 },
129 	{ "adt7517", 0 },
130 	{ "adt7519", 0 },
131 	{ }
132 };
133 
134 MODULE_DEVICE_TABLE(spi, adt7316_spi_id);
135 
136 #ifdef CONFIG_PM
adt7316_spi_suspend(struct spi_device * spi_dev,pm_message_t message)137 static int adt7316_spi_suspend(struct spi_device *spi_dev, pm_message_t message)
138 {
139 	return adt7316_disable(&spi_dev->dev);
140 }
141 
adt7316_spi_resume(struct spi_device * spi_dev)142 static int adt7316_spi_resume(struct spi_device *spi_dev)
143 {
144 	return adt7316_enable(&spi_dev->dev);
145 }
146 #else
147 # define adt7316_spi_suspend NULL
148 # define adt7316_spi_resume  NULL
149 #endif
150 
151 static struct spi_driver adt7316_driver = {
152 	.driver = {
153 		.name = "adt7316",
154 		.bus = &spi_bus_type,
155 		.owner = THIS_MODULE,
156 	},
157 	.probe = adt7316_spi_probe,
158 	.remove = __devexit_p(adt7316_spi_remove),
159 	.suspend = adt7316_spi_suspend,
160 	.resume = adt7316_spi_resume,
161 	.id_table = adt7316_spi_id,
162 };
163 
adt7316_spi_init(void)164 static __init int adt7316_spi_init(void)
165 {
166 	return spi_register_driver(&adt7316_driver);
167 }
168 
adt7316_spi_exit(void)169 static __exit void adt7316_spi_exit(void)
170 {
171 	spi_unregister_driver(&adt7316_driver);
172 }
173 
174 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
175 MODULE_DESCRIPTION("SPI bus driver for Analog Devices ADT7316/7/8 and"
176 			"ADT7516/7/9 digital temperature sensor, ADC and DAC");
177 MODULE_LICENSE("GPL v2");
178 
179 module_init(adt7316_spi_init);
180 module_exit(adt7316_spi_exit);
181