-
Notifications
You must be signed in to change notification settings - Fork 0
/
ads7870-spi.c
205 lines (157 loc) · 4.49 KB
/
ads7870-spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <linux/err.h>
#include <plat/mcspi.h>
#include <asm/uaccess.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/spi/spi.h>
#include "ads7870-core.h"
#include <linux/module.h>
#define ADS7870_SPI_HZ 1500000
#define MODULE_DEBUG 0
u16 ads7870_spi_read_reg8(struct spi_device *spi, u16 reg)
{
struct spi_transfer t[2];
struct spi_message m;
u16 data = 0, cmd;
cmd = (reg & 0x1f) | (1<<6); // Mask R/Wn 16/8n
memset(t, 0, sizeof(t));
spi_message_init(&m);
m.spi = spi;
t[0].tx_buf = &cmd;
t[0].rx_buf = NULL;
t[0].len = 1;
spi_message_add_tail(&t[0], &m);
t[1].tx_buf = NULL;
t[1].rx_buf = &data;
t[1].len = 1;
spi_message_add_tail(&t[1], &m);
spi_sync(m.spi, &m);
if(MODULE_DEBUG)
printk(KERN_DEBUG "ADS7870: Read Reg8 Addr 0x%02x Data: 0x%02x\n", cmd, data);
return data;
}
u16 ads7870_spi_read_reg16(struct spi_device *spi, u16 reg)
{
struct spi_transfer t[2];
struct spi_message m;
u16 data = 0, cmd;
cmd = (reg & 0x1f) | (1<<6) | (1<<5); // Mask R/Wn 16/8n
memset(t, 0, sizeof(t));
spi_message_init(&m);
m.spi = spi;
t[0].tx_buf = &cmd;
t[0].rx_buf = NULL;
t[0].len = 1;
if(MODULE_DEBUG)
printk("requesting data from addr 0x%x\n", cmd);
spi_message_add_tail(&t[0], &m);
t[1].tx_buf = NULL;
t[1].rx_buf = &data;
t[1].len = 2;
spi_message_add_tail(&t[1], &m);
spi_sync(m.spi, &m);
if(MODULE_DEBUG)
printk(KERN_DEBUG "ADS7870: Read Reg16 Addr 0x%02x Data: 0x%04x\n", cmd, data);
return data;
}
void ads7870_spi_write_reg8(struct spi_device *spi, u16 reg, u16 val)
{
struct spi_transfer t[2];
struct spi_message m;
u8 data[2];
u16 valA, regA;
valA = val & 0xff;
regA = reg & 0x1f;
/* Now we prepare the command for transferring */
data[0] = (u8)((reg << 4) | (val >> 8));
data[1] = (u8)(val);
memset(&t, 0, sizeof(t));
spi_message_init(&m);
m.spi = spi;
if(MODULE_DEBUG)
printk(KERN_DEBUG "ADS7870: Write Reg8 Addr 0x%x Data 0x%02x\n", data[0], data[1]);
/* t.tx_buf = data; */
/* t.rx_buf = NULL; */
/* t.len = 2; // one because single byte (bits_pr_word = 8-bit) */
/* spi_message_add_tail(&t, &m); */
t[0].tx_buf = ®A;
t[0].rx_buf = NULL;
t[0].len = 2;
spi_message_add_tail(&t[0], &m);
t[1].tx_buf = &valA;
t[1].rx_buf = NULL;
t[1].len = 1;
spi_message_add_tail(&t[1], &m);
spi_sync(m.spi, &m);
}
/*
* ADS7870 Probe
* Used by the SPI Master to probe the device
* When the SPI device is registered.
*/
static int __devinit ads7870_spi_probe(struct spi_device *spi)
{
spi->bits_per_word = 8;
spi_setup(spi);
printk(KERN_DEBUG "Probing ADS7870, ADS7870 Revision %i\n",
ads7870_spi_read_reg8(spi, ADS7870_ID));
return 0;
}
static int __devexit ads7870_remove(struct spi_device *spi)
{
return 0;
}
static struct spi_driver ads7870_spi_driver = {
.driver = {
.name = "ads7870",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
.probe = ads7870_spi_probe,
.remove = __devexit_p(ads7870_remove),
};
/*
* Init / Exit routines called from
* character driver. Init registers the spi driver
* and the spi host probes the device upon this.
* Exit unregisters the driver and the spi host
* calls _remove upon this
*/
int ads7870_spi_init(struct spi_device **ads7870_spi_device,
struct spi_board_info *ads7870_spi_board_info)
{
int err, bus_num;
struct spi_master *ads7870_spi_master;
/* Add the ads7870 device to the SPI bus *
*
* Using this method we actually add a device
* before adding a driver. A second way would be to
* register the driver and probe the devices in _probe
* this is how it's done in I2C, where multiple devices
* may be available for one driver. In this case
* it would be overkill. If done in _probe we couldn't
* use the spi_device parameter, as it wouldn't be
* assigned yet.
*/
bus_num = ads7870_spi_board_info->bus_num;
ads7870_spi_master = spi_busnum_to_master(bus_num);
*ads7870_spi_device = spi_new_device(ads7870_spi_master,
ads7870_spi_board_info);
if((err=spi_register_driver(&ads7870_spi_driver))<0)
printk (KERN_ALERT "Error %d registering the ads7870 SPI driver\n", err);
else
printk(KERN_ALERT "Successfully registered the DAC driver");
ads7870_spi_write_reg8(*ads7870_spi_device, 1, 1500);
return err;
}
int ads7870_spi_exit(struct spi_device *spi)
{
/*
* Un-register spi driver and device
* Spi host calls _remove upon this
*/
ads7870_spi_write_reg8(spi, 1, 2000);
spi_unregister_driver(&ads7870_spi_driver);
spi_unregister_device(spi);
return 0;
}