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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-3-08 GuEe-GUI the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "thermal.cool.pwm-fan"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define MAX_PWM 255
struct pwm_fan_cool
{
struct rt_thermal_cooling_device parent;
rt_uint32_t pwm_fan_level;
rt_uint32_t pwm_fan_max_level;
rt_uint32_t *pwm_fan_cooling_levels;
struct rt_device_pwm *pwm_dev;
struct rt_pwm_configuration pwm_conf;
struct rt_regulator *supply;
struct rt_mutex lock;
};
#define raw_to_pwm_fan_cool(raw) rt_container_of(raw, struct pwm_fan_cool, parent)
static rt_err_t pwm_fan_power_on(struct pwm_fan_cool *pf_cool)
{
rt_err_t err = RT_EOK;
if ((err = rt_pwm_enable(pf_cool->pwm_dev, pf_cool->pwm_conf.channel)))
{
return err;
}
if (pf_cool->supply && (err = rt_regulator_enable(pf_cool->supply)))
{
rt_pwm_disable(pf_cool->pwm_dev, pf_cool->pwm_conf.channel);
return err;
}
return err;
}
static rt_err_t pwm_fan_power_off(struct pwm_fan_cool *pf_cool)
{
rt_err_t err = RT_EOK;
if (pf_cool->supply && (err = rt_regulator_disable(pf_cool->supply)))
{
return err;
}
if ((err = rt_pwm_disable(pf_cool->pwm_dev, pf_cool->pwm_conf.channel)))
{
rt_regulator_enable(pf_cool->supply);
return err;
}
return err;
}
static rt_err_t pwm_fan_cool_get_max_level(struct rt_thermal_cooling_device *cdev,
rt_ubase_t *out_level)
{
struct pwm_fan_cool *pf_cool = raw_to_pwm_fan_cool(cdev);
*out_level = pf_cool->pwm_fan_max_level;
return RT_EOK;
}
static rt_err_t pwm_fan_cool_get_cur_level(struct rt_thermal_cooling_device *cdev,
rt_ubase_t *out_level)
{
struct pwm_fan_cool *pf_cool = raw_to_pwm_fan_cool(cdev);
*out_level = pf_cool->pwm_fan_level;
return RT_EOK;
}
static rt_err_t pwm_fan_cool_set_cur_level(struct rt_thermal_cooling_device *cdev,
rt_ubase_t level)
{
rt_ubase_t pwm;
rt_err_t err = RT_EOK;
struct pwm_fan_cool *pf_cool = raw_to_pwm_fan_cool(cdev);
if (level > pf_cool->pwm_fan_max_level)
{
return -RT_EINVAL;
}
rt_mutex_take(&pf_cool->lock, RT_WAITING_FOREVER);
if (pf_cool->pwm_fan_level == level)
{
rt_mutex_release(&pf_cool->lock);
return RT_EOK;
}
if ((pwm = pf_cool->pwm_fan_cooling_levels[level]))
{
rt_ubase_t period;
struct rt_pwm_configuration *pwm_conf = &pf_cool->pwm_conf;
period = pwm_conf->period;
pwm_conf->pulse = RT_DIV_ROUND_UP(pwm * (period - 1), MAX_PWM);
err = rt_pwm_set(pf_cool->pwm_dev,
pwm_conf->channel, pwm_conf->period, pwm_conf->pulse);
if (!err && pf_cool->pwm_fan_level == 0)
{
err = pwm_fan_power_on(pf_cool);
}
}
else if (pf_cool->pwm_fan_level > 0)
{
err = pwm_fan_power_off(pf_cool);
}
if (!err)
{
pf_cool->pwm_fan_level = level;
}
rt_mutex_release(&pf_cool->lock);
return err;
}
const static struct rt_thermal_cooling_device_ops pwm_fan_cool_ops =
{
.get_max_level = pwm_fan_cool_get_max_level,
.get_cur_level = pwm_fan_cool_get_cur_level,
.set_cur_level = pwm_fan_cool_set_cur_level,
};
static void pwm_fan_cool_free(struct pwm_fan_cool *pf_cool)
{
if (!rt_is_err_or_null(pf_cool->supply))
{
rt_regulator_put(pf_cool->supply);
}
if (pf_cool->pwm_fan_cooling_levels)
{
rt_free(pf_cool->pwm_fan_cooling_levels);
}
rt_mutex_detach(&pf_cool->lock);
rt_free(pf_cool);
}
static rt_err_t pwm_fan_cool_probe(struct rt_platform_device *pdev)
{
rt_err_t err;
int levels_nr;
struct rt_ofw_cell_args pwm_args;
struct rt_device *dev = &pdev->parent;
struct rt_ofw_node *np = dev->ofw_node, *pwm_np;
struct pwm_fan_cool *pf_cool = rt_calloc(1, sizeof(*pf_cool));
if (!pf_cool)
{
return -RT_ENOMEM;
}
rt_mutex_init(&pf_cool->lock, rt_dm_dev_get_name(dev), RT_IPC_FLAG_PRIO);
if (rt_ofw_parse_phandle_cells(np, "pwms", "#pwm-cells", 0, &pwm_args))
{
err = -RT_EINVAL;
goto _fail;
}
pwm_np = pwm_args.data;
if (!rt_ofw_data(pwm_np))
{
rt_platform_ofw_request(pwm_np);
}
pf_cool->pwm_dev = rt_ofw_data(pwm_np);
rt_ofw_node_put(pwm_np);
if (!pf_cool->pwm_dev)
{
err = -RT_EINVAL;
goto _fail;
}
pf_cool->pwm_conf.channel = pwm_args.args[0];
pf_cool->pwm_conf.period = pwm_args.args[1];
pf_cool->supply = rt_regulator_get(dev, "fan");
if (rt_is_err(pf_cool->supply))
{
err = rt_ptr_err(pf_cool->supply);
goto _fail;
}
if ((levels_nr = rt_dm_dev_prop_count_of_u32(dev, "cooling-levels")) <= 0)
{
err = -RT_EINVAL;
goto _fail;
}
pf_cool->pwm_fan_cooling_levels = rt_calloc(levels_nr, sizeof(rt_uint32_t));
if (!pf_cool->pwm_fan_cooling_levels)
{
err = -RT_ENOMEM;
goto _fail;
}
if (rt_dm_dev_prop_read_u32_array_index(dev, "cooling-levels",
0, levels_nr, pf_cool->pwm_fan_cooling_levels) <= 0)
{
err = -RT_EINVAL;
goto _fail;
}
pf_cool->pwm_fan_level = MAX_PWM;
pf_cool->pwm_fan_max_level = levels_nr - 1;
if ((err = pwm_fan_cool_set_cur_level(&pf_cool->parent, 0)))
{
goto _fail;
}
rt_dm_dev_set_name(&pf_cool->parent.parent, "%s", rt_dm_dev_get_name(&pdev->parent));
pf_cool->parent.parent.ofw_node = dev->ofw_node;
pf_cool->parent.ops = &pwm_fan_cool_ops;
if ((err = rt_thermal_cooling_device_register(&pf_cool->parent)))
{
goto _fail;
}
dev->user_data = pf_cool;
return RT_EOK;
_fail:
pwm_fan_cool_free(pf_cool);
return err;
}
static rt_err_t pwm_fan_cool_remove(struct rt_platform_device *pdev)
{
struct pwm_fan_cool *pf_cool = pdev->parent.user_data;
rt_thermal_cooling_device_unregister(&pf_cool->parent);
pwm_fan_power_off(pf_cool);
pwm_fan_cool_free(pf_cool);
return RT_EOK;
}
static rt_err_t pwm_fan_cool_shutdown(struct rt_platform_device *pdev)
{
return pwm_fan_cool_remove(pdev);
}
static const struct rt_ofw_node_id pwm_fan_cool_ofw_ids[] =
{
{ .compatible = "pwm-fan" },
{ /* sentinel */ }
};
static struct rt_platform_driver pwm_fan_cool_driver =
{
.name = "pwm-fan-cool",
.ids = pwm_fan_cool_ofw_ids,
.probe = pwm_fan_cool_probe,
.remove = pwm_fan_cool_remove,
.shutdown = pwm_fan_cool_shutdown,
};
RT_PLATFORM_DRIVER_EXPORT(pwm_fan_cool_driver);
|