]> git.ipfire.org Git - thirdparty/openwrt.git/blob
269bb7219a6e299c94bf14edc13bbadb7ccb0245
[thirdparty/openwrt.git] /
1 From 8b5840b077e3b51ccdf840010600aec211a8990b Mon Sep 17 00:00:00 2001
2 From: Sean Young <sean@mess.org>
3 Date: Tue, 19 Dec 2023 16:30:27 +0000
4 Subject: [PATCH 1128/1135] pwm: Make it possible to apply PWM changes in
5 atomic context
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 commit 7170d3beafc2373dd76b6b5d6e617d89e4e42b8b upstream.
11
12 Some PWM devices require sleeping, for example if the pwm device is
13 connected over I2C. However, many PWM devices could be used from atomic
14 context, e.g. memory mapped PWM. This is useful for, for example, the
15 pwm-ir-tx driver which requires precise timing. Sleeping causes havoc
16 with the generated IR signal.
17
18 Since not all PWM devices can support atomic context, we also add a
19 pwm_might_sleep() function to check if is not supported.
20
21 Signed-off-by: Sean Young <sean@mess.org>
22 Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
23 Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
24 ---
25 Documentation/driver-api/pwm.rst | 9 +++++
26 MAINTAINERS | 2 +-
27 drivers/pwm/core.c | 62 ++++++++++++++++++++++++++------
28 include/linux/pwm.h | 25 +++++++++++++
29 4 files changed, 86 insertions(+), 12 deletions(-)
30
31 --- a/Documentation/driver-api/pwm.rst
32 +++ b/Documentation/driver-api/pwm.rst
33 @@ -46,6 +46,15 @@ After being requested, a PWM has to be c
34 This API controls both the PWM period/duty_cycle config and the
35 enable/disable state.
36
37 +PWM devices can be used from atomic context, if the PWM does not sleep. You
38 +can check if this the case with::
39 +
40 + bool pwm_might_sleep(struct pwm_device *pwm);
41 +
42 +If false, the PWM can also be configured from atomic context with::
43 +
44 + int pwm_apply_atomic(struct pwm_device *pwm, struct pwm_state *state);
45 +
46 As a consumer, don't rely on the output's state for a disabled PWM. If it's
47 easily possible, drivers are supposed to emit the inactive state, but some
48 drivers cannot. If you rely on getting the inactive state, use .duty_cycle=0,
49 --- a/MAINTAINERS
50 +++ b/MAINTAINERS
51 @@ -17437,7 +17437,7 @@ F: drivers/video/backlight/pwm_bl.c
52 F: include/dt-bindings/pwm/
53 F: include/linux/pwm.h
54 F: include/linux/pwm_backlight.h
55 -K: pwm_(config|apply_might_sleep|ops)
56 +K: pwm_(config|apply_might_sleep|apply_atomic|ops)
57
58 PXA GPIO DRIVER
59 M: Robert Jarzmik <robert.jarzmik@free.fr>
60 --- a/drivers/pwm/core.c
61 +++ b/drivers/pwm/core.c
62 @@ -489,24 +489,15 @@ static void pwm_apply_debug(struct pwm_d
63 }
64
65 /**
66 - * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
67 + * __pwm_apply() - atomically apply a new state to a PWM device
68 * @pwm: PWM device
69 * @state: new state to apply
70 */
71 -int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
72 +static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
73 {
74 struct pwm_chip *chip;
75 int err;
76
77 - /*
78 - * Some lowlevel driver's implementations of .apply() make use of
79 - * mutexes, also with some drivers only returning when the new
80 - * configuration is active calling pwm_apply_might_sleep() from atomic context
81 - * is a bad idea. So make it explicit that calling this function might
82 - * sleep.
83 - */
84 - might_sleep();
85 -
86 if (!pwm || !state || !state->period ||
87 state->duty_cycle > state->period)
88 return -EINVAL;
89 @@ -535,9 +526,58 @@ int pwm_apply_might_sleep(struct pwm_dev
90
91 return 0;
92 }
93 +
94 +/**
95 + * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
96 + * Cannot be used in atomic context.
97 + * @pwm: PWM device
98 + * @state: new state to apply
99 + */
100 +int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
101 +{
102 + int err;
103 +
104 + /*
105 + * Some lowlevel driver's implementations of .apply() make use of
106 + * mutexes, also with some drivers only returning when the new
107 + * configuration is active calling pwm_apply_might_sleep() from atomic context
108 + * is a bad idea. So make it explicit that calling this function might
109 + * sleep.
110 + */
111 + might_sleep();
112 +
113 + if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
114 + /*
115 + * Catch any drivers that have been marked as atomic but
116 + * that will sleep anyway.
117 + */
118 + non_block_start();
119 + err = __pwm_apply(pwm, state);
120 + non_block_end();
121 + } else {
122 + err = __pwm_apply(pwm, state);
123 + }
124 +
125 + return err;
126 +}
127 EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
128
129 /**
130 + * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
131 + * Not all PWM devices support this function, check with pwm_might_sleep().
132 + * @pwm: PWM device
133 + * @state: new state to apply
134 + */
135 +int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
136 +{
137 + WARN_ONCE(!pwm->chip->atomic,
138 + "sleeping PWM driver used in atomic context\n");
139 +
140 + return __pwm_apply(pwm, state);
141 +}
142 +EXPORT_SYMBOL_GPL(pwm_apply_atomic);
143 +
144 +/**
145 * pwm_capture() - capture and report a PWM signal
146 * @pwm: PWM device
147 * @result: structure to fill with capture result
148 --- a/include/linux/pwm.h
149 +++ b/include/linux/pwm.h
150 @@ -289,6 +289,7 @@ struct pwm_ops {
151 * @npwm: number of PWMs controlled by this chip
152 * @of_xlate: request a PWM device given a device tree PWM specifier
153 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
154 + * @atomic: can the driver's ->apply() be called in atomic context
155 * @list: list node for internal use
156 * @pwms: array of PWM devices allocated by the framework
157 */
158 @@ -301,6 +302,7 @@ struct pwm_chip {
159 struct pwm_device * (*of_xlate)(struct pwm_chip *chip,
160 const struct of_phandle_args *args);
161 unsigned int of_pwm_n_cells;
162 + bool atomic;
163
164 /* only used internally by the PWM framework */
165 struct list_head list;
166 @@ -310,6 +312,7 @@ struct pwm_chip {
167 #if IS_ENABLED(CONFIG_PWM)
168 /* PWM user APIs */
169 int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state);
170 +int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state);
171 int pwm_adjust_config(struct pwm_device *pwm);
172
173 /**
174 @@ -380,6 +383,17 @@ static inline void pwm_disable(struct pw
175 pwm_apply_might_sleep(pwm, &state);
176 }
177
178 +/**
179 + * pwm_might_sleep() - is pwm_apply_atomic() supported?
180 + * @pwm: PWM device
181 + *
182 + * Returns: false if pwm_apply_atomic() can be called from atomic context.
183 + */
184 +static inline bool pwm_might_sleep(struct pwm_device *pwm)
185 +{
186 + return !pwm->chip->atomic;
187 +}
188 +
189 /* PWM provider APIs */
190 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
191 unsigned long timeout);
192 @@ -408,6 +422,11 @@ struct pwm_device *devm_fwnode_pwm_get(s
193 struct fwnode_handle *fwnode,
194 const char *con_id);
195 #else
196 +static inline bool pwm_might_sleep(struct pwm_device *pwm)
197 +{
198 + return true;
199 +}
200 +
201 static inline int pwm_apply_might_sleep(struct pwm_device *pwm,
202 const struct pwm_state *state)
203 {
204 @@ -415,6 +434,12 @@ static inline int pwm_apply_might_sleep(
205 return -EOPNOTSUPP;
206 }
207
208 +static inline int pwm_apply_atomic(struct pwm_device *pwm,
209 + const struct pwm_state *state)
210 +{
211 + return -EOPNOTSUPP;
212 +}
213 +
214 static inline int pwm_adjust_config(struct pwm_device *pwm)
215 {
216 return -EOPNOTSUPP;