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
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-12-06 GuEe-GUI first version
* 2025-12-25 lhxj mark OFW node as taken to prevent platform bus duplication; fix cppcheck warning
*/
#include "dev_spi_dm.h"
#define DBG_TAG "spi.bus"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
extern rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name);
static struct rt_bus spi_bus;
void spi_bus_scan_devices(struct rt_spi_bus *bus)
{
#ifdef RT_USING_OFW
if (bus->parent.ofw_node)
{
struct rt_ofw_node *np = bus->parent.ofw_node, *spi_dev_np;
rt_ofw_foreach_available_child_node(np, spi_dev_np)
{
struct rt_spi_device *spi_dev;
if (!rt_ofw_prop_read_bool(spi_dev_np, "compatible"))
{
continue;
}
if ((bus->mode & RT_SPI_BUS_MODE_SPI) == RT_SPI_BUS_MODE_SPI)
{
spi_dev = rt_calloc(1, sizeof(struct rt_spi_device));
}
else if ((bus->mode & RT_SPI_BUS_MODE_QSPI) == RT_SPI_BUS_MODE_QSPI)
{
spi_dev = rt_calloc(1, sizeof(struct rt_qspi_device));
}
else
{
LOG_E("Unknown bus mode = %x", bus->mode);
RT_ASSERT(0);
}
if (!spi_dev)
{
rt_ofw_node_put(spi_dev_np);
LOG_E("Not memory to create spi device: %s",
rt_ofw_node_full_name(spi_dev_np));
return;
}
spi_dev->parent.ofw_node = spi_dev_np;
spi_dev->parent.type = RT_Device_Class_Unknown;
spi_dev->name = rt_ofw_node_name(spi_dev_np);
spi_dev->bus = bus;
rt_dm_dev_set_name(&spi_dev->parent, rt_ofw_node_full_name(spi_dev_np));
if (spi_device_ofw_parse(spi_dev))
{
continue;
}
/* Mark this OFW node as taken to prevent platform bus from creating duplicate device */
spi_dev_np->dev = &spi_dev->parent;
rt_spi_device_register(spi_dev);
}
}
#endif /* RT_USING_OFW */
}
rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver)
{
RT_ASSERT(driver != RT_NULL);
driver->parent.bus = &spi_bus;
return rt_driver_register(&driver->parent);
}
rt_err_t rt_spi_device_register(struct rt_spi_device *device)
{
RT_ASSERT(device != RT_NULL);
return rt_bus_add_device(&spi_bus, &device->parent);
}
static rt_bool_t spi_match(rt_driver_t drv, rt_device_t dev)
{
const struct rt_spi_device_id *id;
struct rt_spi_driver *driver = rt_container_of(drv, struct rt_spi_driver, parent);
struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
if ((id = driver->ids))
{
for (; id->name[0]; ++id)
{
if (!rt_strcmp(id->name, device->name))
{
device->id = id;
device->ofw_id = RT_NULL;
return RT_TRUE;
}
}
}
#ifdef RT_USING_OFW
device->ofw_id = rt_ofw_node_match(device->parent.ofw_node, driver->ofw_ids);
if (device->ofw_id)
{
device->id = RT_NULL;
return RT_TRUE;
}
#endif
return RT_FALSE;
}
static rt_err_t spi_probe(rt_device_t dev)
{
rt_err_t err;
struct rt_spi_bus *bus;
struct rt_spi_driver *driver = rt_container_of(dev->drv, struct rt_spi_driver, parent);
struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
if (!device->bus)
{
return -RT_EINVAL;
}
err = driver->probe(device);
if (err)
{
return err;
}
bus = device->bus;
if (bus->cs_pins[0] >= 0)
{
device->cs_pin = bus->cs_pins[device->chip_select[0]];
rt_pin_mode(device->cs_pin, PIN_MODE_OUTPUT);
}
else
{
device->cs_pin = PIN_NONE;
}
/* Driver not register SPI device to system */
if (device->parent.type == RT_Device_Class_Unknown)
{
rt_spidev_device_init(device, rt_dm_dev_get_name(&device->parent));
}
return RT_EOK;
}
static rt_err_t spi_remove(rt_device_t dev)
{
struct rt_spi_driver *driver = rt_container_of(dev->drv, struct rt_spi_driver, parent);
struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
if (driver && driver->remove)
{
driver->remove(device);
}
rt_free(device);
return RT_EOK;
}
static rt_err_t spi_shutdown(rt_device_t dev)
{
struct rt_spi_driver *driver = rt_container_of(dev->drv, struct rt_spi_driver, parent);
struct rt_spi_device *device = rt_container_of(dev, struct rt_spi_device, parent);
if (driver && driver->shutdown)
{
driver->shutdown(device);
}
rt_free(device);
return RT_EOK;
}
static struct rt_bus spi_bus =
{
.name = "spi",
.match = spi_match,
.probe = spi_probe,
.remove = spi_remove,
.shutdown = spi_shutdown,
};
static int spi_bus_init(void)
{
rt_bus_register(&spi_bus);
return 0;
}
INIT_CORE_EXPORT(spi_bus_init);
|