-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmcp3901.c
128 lines (99 loc) · 2.71 KB
/
mcp3901.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
/*
* Copyright 2011 Frank Maker <[email protected]>
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the license, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <linux/module.h>
#include <linux/spi/spi.h>
#define SPI_BUS1 1
static struct spi_device *mcp3901_device;
static __initdata struct spi_board_info mcp3901_board_info = {
.modalias = "mcp3901",
.platform_data = NULL,
.mode = SPI_MODE_0,
.irq = NULL,
.max_speed_hz = 1000,
.bus_num = SPI_BUS1,
.chip_select = 0,
};
struct mcp3901_state {
struct spi_device *dev;
struct mutex lock;
};
/* Bind driver to board_info using modalias "mcp3901" */
static int __devinit mcp3901_probe(struct spi_device *spi)
{
struct mcp3901_state *state;
struct mcp3901_platform_data *pdata;
pdata = spi->dev.platform_data;
if(!pdata)
return -ENODEV;
state = kzalloc(sizeof(struct mcp3901_state), GFP_KERNEL);
if(!state)
return -ENOMEM;
spi_set_drvdata(spi, state);
return 0;
}
static int __devexit mcp3901_remove(struct spi_device *spi)
{
struct mcp3901_state *st = spi_get_drvdata(spi);
kfree(st);
return 0;
}
static struct spi_driver mcp3901_driver = {
.driver = {
.name = "mcp3901",
.owner = THIS_MODULE,
},
.probe = mcp3901_probe,
.remove = __devexit_p(mcp3901_remove),
};
static int __init mcp3901_hotplug(void)
{
int ret;
struct spi_master *master;
master = spi_busnum_to_master(SPI_BUS1);
if(master == NULL)
return -ENODEV;
mcp3901_device = spi_new_device(master, &mcp3901_board_info);
if(mcp3901_device == NULL)
return -ENODEV;
printk("Hotplugged MCP3901.");
return 0;
}
static int __init mcp3901_init(void)
{
int ret;
ret = spi_register_driver(&mcp3901_driver);
if(ret)
goto error;
ret = mcp3901_hotplug();
if(ret)
goto error;
return 0;
error:
return ret;
}
module_init(mcp3901_init);
static void __exit mcp3901_exit(void)
{
spi_unregister_device(mcp3901_device);
spi_unregister_driver(&mcp3901_driver);
}
module_exit(mcp3901_exit);
MODULE_AUTHOR("Frank Maker <[email protected]>");
MODULE_DESCRIPTION("MCP3901 ADC SPI Driver");
MODULE_LICENSE("GPL");