-
Notifications
You must be signed in to change notification settings - Fork 944
Expand file tree
/
Copy pathmathworks_generic_of.c
More file actions
executable file
·201 lines (158 loc) · 4.98 KB
/
mathworks_generic_of.c
File metadata and controls
executable file
·201 lines (158 loc) · 4.98 KB
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
/*
* MathWorks IP Generic OF Driver
*
* Copyright 2013-2016 The MathWorks, Inc
*
* Licensed under the GPL-2.
*/
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/i2c.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/mathworks/mathworks_ip.h>
#define ip_to_pdev(x) (container_of(x->dev, struct platform_device, dev))
#define DRIVER_NAME "mathworks_generic_of"
#if defined(CONFIG_I2C)
static inline void mwgen_of_node_put(void *data)
{
struct device_node *node = data;
of_node_put(node);
}
static inline void mwgen_put_device(void *data)
{
struct device *dev = data;
put_device(dev);
}
static inline void mwgen_of_unlink_i2c_device(void *data)
{
struct mathworks_ip_info *thisIpcore = data;
sysfs_remove_link(&thisIpcore->char_device->kobj, "i2c_device");
}
static inline void mwgen_of_unlink_i2c_adapter(void *data)
{
struct mathworks_ip_info *thisIpcore = data;
sysfs_remove_link(&thisIpcore->char_device->kobj, "i2c_adapter");
}
static int mathworks_generic_of_i2c_init(struct mathworks_ip_info *thisIpcore){
struct device_node *nodePointer = thisIpcore->dev->of_node;
struct device_node *slave_node;
int status;
slave_node = of_parse_phandle(nodePointer, "i2c-controller", 0);
if (slave_node) {
status = devm_add_action_helper(thisIpcore->dev,
mwgen_of_node_put, slave_node);
if(status)
return status;
dev_info(thisIpcore->dev, "creating i2c link\n");
thisIpcore->i2c = of_find_i2c_device_by_node(slave_node);
if(thisIpcore->i2c == NULL){
dev_err(thisIpcore->dev, "could not find i2c device\n");
return -ENODEV;
}
status = devm_add_action_helper(thisIpcore->dev,
mwgen_put_device,
&thisIpcore->i2c->dev);
if(status)
return status;
dev_info(thisIpcore->dev, "Adding link to %s[%s]\n", thisIpcore->i2c->adapter->name, thisIpcore->i2c->name);
/* add a link to the i2c device */
status = sysfs_create_link(&thisIpcore->char_device->kobj, &thisIpcore->i2c->dev.kobj, "i2c_device");
if (status)
return status;
status = devm_add_action_helper(thisIpcore->dev,
mwgen_of_unlink_i2c_device,
thisIpcore);
if(status)
return status;
/* add a link to the i2c bus */
status = sysfs_create_link(&thisIpcore->char_device->kobj, &thisIpcore->i2c->adapter->dev.kobj, "i2c_adapter");
if (status)
return status;
status = devm_add_action_helper(thisIpcore->dev,
mwgen_of_unlink_i2c_adapter,
thisIpcore);
if(status)
return status;
} else {
thisIpcore->i2c = NULL;
}
return 0;
}
#endif
static int mathworks_generic_of_get_param(struct mathworks_ip_info *thisIpcore, void *arg)
{
struct mathworks_ip_param_info pinfo;
const void *paramData;
int len;
if( copy_from_user(&pinfo, (struct mathworks_ip_param_info *)arg, sizeof(struct mathworks_ip_param_info)) ) {
return -EACCES;
}
paramData = of_get_property(thisIpcore->dev->of_node,pinfo.name, &len);
pinfo.size = len;
/* Copy the struct back to user space */
if( copy_to_user((struct mathworks_ip_param_info*)arg, &pinfo, sizeof(struct mathworks_ip_param_info)) ) {
return -EACCES;
}
/* Copy any data to the user buf */
if (paramData) {
if( copy_to_user((void *)pinfo.buf, paramData, pinfo.size) ){
return -EACCES;
}
} else {
return -ENODEV;
}
return 0;
}
static void mathworks_generic_of_get_devname(struct mathworks_ip_info *thisIpcore,char *devname){
const char *of_devname = of_get_property(thisIpcore->dev->of_node,"mwgen,devname", NULL);
snprintf(devname,MATHWORKS_IP_DEVNAME_LEN, "%s", of_devname);
}
struct mathworks_ip_ops mw_of_ops = {
.get_devname = mathworks_generic_of_get_devname,
.get_param = mathworks_generic_of_get_param,
.fops = &mathworks_ip_common_fops,
};
static const struct of_device_id mathworks_generic_of_match[] = {
{ .compatible = "mathworks,mwgeneric-v1.00",},
{ .compatible = "mathworks,mathworks_ip-v1.00",},
{},
};
MODULE_DEVICE_TABLE(of, mathworks_generic_of_match);
static int mathworks_generic_of_probe(struct platform_device *pdev)
{
int status = 0;
struct mathworks_ip_info *thisIpcore;
thisIpcore = devm_mathworks_ip_of_init(pdev, THIS_MODULE, &mw_of_ops, false);
if (IS_ERR(thisIpcore))
return PTR_ERR(thisIpcore);
status = devm_mathworks_ip_register(thisIpcore);
if(status)
{
dev_err(&pdev->dev, "mwgeneric device registration failed: %d\n", status);
return status;
}
#if defined(CONFIG_I2C)
status = mathworks_generic_of_i2c_init(thisIpcore);
if (status){
dev_err(&pdev->dev, "Failed to link I2C nodes: %d\n", status);
return status;
}
#endif
return 0;
}
static struct platform_driver mathworks_ip_driver = {
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = mathworks_generic_of_match,
},
.probe = mathworks_generic_of_probe,
};
module_platform_driver(mathworks_ip_driver);
MODULE_AUTHOR("MathWorks, Inc");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_NAME ": MathWorks Generic OF driver");
MODULE_ALIAS(DRIVER_NAME);