1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2023-04-12 ErikChan the first version
9 * 2023-10-13 zmshahaha distinguish ofw and none-ofw situation
10 */
11
12 #include <rtthread.h>
13
14 #define DBG_TAG "rtdm.pltaform"
15 #define DBG_LVL DBG_INFO
16 #include <rtdbg.h>
17
18 #include <drivers/platform.h>
19 #include <drivers/core/bus.h>
20 #include <drivers/core/dm.h>
21 #include <drivers/core/power_domain.h>
22
23 static struct rt_bus platform_bus;
24
25 /**
26 * @brief This function create a platform device.
27 *
28 * @param name is name of the platform device.
29 *
30 * @return a new platform device.
31 */
rt_platform_device_alloc(const char * name)32 struct rt_platform_device *rt_platform_device_alloc(const char *name)
33 {
34 struct rt_platform_device *pdev = rt_calloc(1, sizeof(*pdev));
35
36 if (!pdev)
37 {
38 return RT_NULL;
39 }
40
41 pdev->parent.bus = &platform_bus;
42 pdev->name = name;
43
44 return pdev;
45 }
46
47 /**
48 * @brief This function register a rt_driver to platform bus.
49 *
50 * @return the error code, RT_EOK on successfully.
51 */
rt_platform_driver_register(struct rt_platform_driver * pdrv)52 rt_err_t rt_platform_driver_register(struct rt_platform_driver *pdrv)
53 {
54 RT_ASSERT(pdrv != RT_NULL);
55
56 pdrv->parent.bus = &platform_bus;
57 #if RT_NAME_MAX > 0
58 rt_strcpy(pdrv->parent.parent.name, pdrv->name);
59 #else
60 pdrv->parent.parent.name = pdrv->name;
61 #endif
62 return rt_driver_register(&pdrv->parent);
63 }
64
65 /**
66 * @brief This function register a rt_device to platform bus.
67 *
68 * @return the error code, RT_EOK on successfully.
69 */
rt_platform_device_register(struct rt_platform_device * pdev)70 rt_err_t rt_platform_device_register(struct rt_platform_device *pdev)
71 {
72 RT_ASSERT(pdev != RT_NULL);
73
74 return rt_bus_add_device(&platform_bus, &pdev->parent);
75 }
76
platform_match(rt_driver_t drv,rt_device_t dev)77 static rt_bool_t platform_match(rt_driver_t drv, rt_device_t dev)
78 {
79 struct rt_platform_driver *pdrv = rt_container_of(drv, struct rt_platform_driver, parent);
80 struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
81 struct rt_ofw_node *np = dev->ofw_node;
82
83 /* 1、match with ofw node */
84 if (np)
85 {
86 #ifdef RT_USING_OFW
87 pdev->id = rt_ofw_node_match(np, pdrv->ids);
88 #else
89 pdev->id = RT_NULL;
90 #endif
91 if (pdev->id)
92 {
93 return RT_TRUE;
94 }
95 }
96
97 /* 2、match with name */
98 if (pdev->name && pdrv->name)
99 {
100 if (pdev->name == pdrv->name)
101 {
102 return RT_TRUE;
103 }
104 else
105 {
106 return !rt_strcmp(pdrv->name, pdev->name);
107 }
108 }
109
110 return RT_FALSE;
111 }
112
platform_probe(rt_device_t dev)113 static rt_err_t platform_probe(rt_device_t dev)
114 {
115 rt_err_t err;
116 struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
117 struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
118 #ifdef RT_USING_OFW
119 struct rt_ofw_node *np = dev->ofw_node;
120 #endif
121
122 err = rt_dm_power_domain_attach(dev, RT_TRUE);
123
124 if (err && err != -RT_EEMPTY)
125 {
126 LOG_E("Attach power domain error = %s in device %s", rt_strerror(err),
127 #ifdef RT_USING_OFW
128 (pdev->name && pdev->name[0]) ? pdev->name : rt_ofw_node_full_name(np)
129 #else
130 pdev->name
131 #endif
132 );
133
134 return err;
135 }
136
137 err = pdrv->probe(pdev);
138
139 if (!err)
140 {
141 #ifdef RT_USING_OFW
142 if (np)
143 {
144 rt_ofw_node_set_flag(np, RT_OFW_F_READLY);
145 }
146 #endif
147 }
148 else
149 {
150 if (err == -RT_ENOMEM)
151 {
152 LOG_W("System not memory in driver %s", pdrv->name);
153 }
154
155 rt_dm_power_domain_detach(dev, RT_TRUE);
156 }
157
158 return err;
159 }
160
platform_remove(rt_device_t dev)161 static rt_err_t platform_remove(rt_device_t dev)
162 {
163 struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
164 struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
165
166 if (pdrv && pdrv->remove)
167 {
168 pdrv->remove(pdev);
169 }
170
171 rt_dm_power_domain_detach(dev, RT_TRUE);
172 rt_platform_ofw_free(pdev);
173
174 return RT_EOK;
175 }
176
platform_shutdown(rt_device_t dev)177 static rt_err_t platform_shutdown(rt_device_t dev)
178 {
179 struct rt_platform_driver *pdrv = rt_container_of(dev->drv, struct rt_platform_driver, parent);
180 struct rt_platform_device *pdev = rt_container_of(dev, struct rt_platform_device, parent);
181
182 if (pdrv && pdrv->shutdown)
183 {
184 pdrv->shutdown(pdev);
185 }
186
187 rt_dm_power_domain_detach(dev, RT_TRUE);
188 rt_platform_ofw_free(pdev);
189
190 return RT_EOK;
191 }
192
193 static struct rt_bus platform_bus =
194 {
195 .name = "platform",
196 .match = platform_match,
197 .probe = platform_probe,
198 .remove = platform_remove,
199 .shutdown = platform_shutdown,
200 };
201
platform_bus_init(void)202 static int platform_bus_init(void)
203 {
204 rt_bus_register(&platform_bus);
205
206 return 0;
207 }
208 INIT_CORE_EXPORT(platform_bus_init);
209