Commit | Line | Data |
---|---|---|
0c2498f1 SH |
1 | /* |
2 | * Generic pwmlib implementation | |
3 | * | |
4 | * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> | |
f051c466 | 5 | * Copyright (C) 2011-2012 Avionic Design GmbH |
0c2498f1 SH |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2, or (at your option) | |
10 | * any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; see the file COPYING. If not, write to | |
19 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/pwm.h> | |
f051c466 | 24 | #include <linux/radix-tree.h> |
0c2498f1 SH |
25 | #include <linux/list.h> |
26 | #include <linux/mutex.h> | |
27 | #include <linux/err.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/device.h> | |
62099abf TR |
30 | #include <linux/debugfs.h> |
31 | #include <linux/seq_file.h> | |
0c2498f1 | 32 | |
208be769 | 33 | #include <dt-bindings/pwm/pwm.h> |
0c2498f1 | 34 | |
208be769 | 35 | #define MAX_PWMS 1024 |
83af2402 | 36 | |
8138d2dd TR |
37 | static DEFINE_MUTEX(pwm_lookup_lock); |
38 | static LIST_HEAD(pwm_lookup_list); | |
0c2498f1 | 39 | static DEFINE_MUTEX(pwm_lock); |
f051c466 TR |
40 | static LIST_HEAD(pwm_chips); |
41 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); | |
42 | static RADIX_TREE(pwm_tree, GFP_KERNEL); | |
0c2498f1 | 43 | |
f051c466 | 44 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
0c2498f1 | 45 | { |
f051c466 TR |
46 | return radix_tree_lookup(&pwm_tree, pwm); |
47 | } | |
48 | ||
49 | static int alloc_pwms(int pwm, unsigned int count) | |
50 | { | |
51 | unsigned int from = 0; | |
52 | unsigned int start; | |
53 | ||
54 | if (pwm >= MAX_PWMS) | |
55 | return -EINVAL; | |
56 | ||
57 | if (pwm >= 0) | |
58 | from = pwm; | |
0c2498f1 | 59 | |
f051c466 TR |
60 | start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, |
61 | count, 0); | |
62 | ||
63 | if (pwm >= 0 && start != pwm) | |
64 | return -EEXIST; | |
65 | ||
66 | if (start + count > MAX_PWMS) | |
67 | return -ENOSPC; | |
68 | ||
69 | return start; | |
70 | } | |
71 | ||
72 | static void free_pwms(struct pwm_chip *chip) | |
73 | { | |
74 | unsigned int i; | |
75 | ||
76 | for (i = 0; i < chip->npwm; i++) { | |
77 | struct pwm_device *pwm = &chip->pwms[i]; | |
78 | radix_tree_delete(&pwm_tree, pwm->pwm); | |
0c2498f1 SH |
79 | } |
80 | ||
f051c466 TR |
81 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
82 | ||
83 | kfree(chip->pwms); | |
84 | chip->pwms = NULL; | |
85 | } | |
86 | ||
8138d2dd TR |
87 | static struct pwm_chip *pwmchip_find_by_name(const char *name) |
88 | { | |
89 | struct pwm_chip *chip; | |
90 | ||
91 | if (!name) | |
92 | return NULL; | |
93 | ||
94 | mutex_lock(&pwm_lock); | |
95 | ||
96 | list_for_each_entry(chip, &pwm_chips, list) { | |
97 | const char *chip_name = dev_name(chip->dev); | |
98 | ||
99 | if (chip_name && strcmp(chip_name, name) == 0) { | |
100 | mutex_unlock(&pwm_lock); | |
101 | return chip; | |
102 | } | |
103 | } | |
104 | ||
105 | mutex_unlock(&pwm_lock); | |
106 | ||
107 | return NULL; | |
108 | } | |
109 | ||
f051c466 TR |
110 | static int pwm_device_request(struct pwm_device *pwm, const char *label) |
111 | { | |
112 | int err; | |
113 | ||
114 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
115 | return -EBUSY; | |
116 | ||
117 | if (!try_module_get(pwm->chip->ops->owner)) | |
118 | return -ENODEV; | |
119 | ||
120 | if (pwm->chip->ops->request) { | |
121 | err = pwm->chip->ops->request(pwm->chip, pwm); | |
122 | if (err) { | |
123 | module_put(pwm->chip->ops->owner); | |
124 | return err; | |
125 | } | |
126 | } | |
127 | ||
128 | set_bit(PWMF_REQUESTED, &pwm->flags); | |
129 | pwm->label = label; | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
83af2402 PA |
134 | struct pwm_device * |
135 | of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args) | |
136 | { | |
137 | struct pwm_device *pwm; | |
138 | ||
139 | if (pc->of_pwm_n_cells < 3) | |
140 | return ERR_PTR(-EINVAL); | |
141 | ||
142 | if (args->args[0] >= pc->npwm) | |
143 | return ERR_PTR(-EINVAL); | |
144 | ||
145 | pwm = pwm_request_from_chip(pc, args->args[0], NULL); | |
146 | if (IS_ERR(pwm)) | |
147 | return pwm; | |
148 | ||
e39c0df1 | 149 | pwm->args.period = args->args[1]; |
83af2402 | 150 | |
208be769 | 151 | if (args->args[2] & PWM_POLARITY_INVERTED) |
e39c0df1 | 152 | pwm->args.polarity = PWM_POLARITY_INVERSED; |
83af2402 | 153 | else |
e39c0df1 | 154 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
83af2402 PA |
155 | |
156 | return pwm; | |
157 | } | |
417328c3 | 158 | EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); |
83af2402 | 159 | |
e50d3523 SK |
160 | static struct pwm_device * |
161 | of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args) | |
7299ab70 TR |
162 | { |
163 | struct pwm_device *pwm; | |
164 | ||
165 | if (pc->of_pwm_n_cells < 2) | |
166 | return ERR_PTR(-EINVAL); | |
167 | ||
168 | if (args->args[0] >= pc->npwm) | |
169 | return ERR_PTR(-EINVAL); | |
170 | ||
171 | pwm = pwm_request_from_chip(pc, args->args[0], NULL); | |
172 | if (IS_ERR(pwm)) | |
173 | return pwm; | |
174 | ||
e39c0df1 | 175 | pwm->args.period = args->args[1]; |
7299ab70 TR |
176 | |
177 | return pwm; | |
178 | } | |
179 | ||
dfeb86ec | 180 | static void of_pwmchip_add(struct pwm_chip *chip) |
7299ab70 TR |
181 | { |
182 | if (!chip->dev || !chip->dev->of_node) | |
183 | return; | |
184 | ||
185 | if (!chip->of_xlate) { | |
186 | chip->of_xlate = of_pwm_simple_xlate; | |
187 | chip->of_pwm_n_cells = 2; | |
188 | } | |
189 | ||
190 | of_node_get(chip->dev->of_node); | |
191 | } | |
192 | ||
dfeb86ec | 193 | static void of_pwmchip_remove(struct pwm_chip *chip) |
7299ab70 | 194 | { |
8d6cc073 | 195 | if (chip->dev) |
7299ab70 TR |
196 | of_node_put(chip->dev->of_node); |
197 | } | |
198 | ||
f051c466 TR |
199 | /** |
200 | * pwm_set_chip_data() - set private chip data for a PWM | |
201 | * @pwm: PWM device | |
202 | * @data: pointer to chip-specific data | |
04883802 TR |
203 | * |
204 | * Returns: 0 on success or a negative error code on failure. | |
f051c466 TR |
205 | */ |
206 | int pwm_set_chip_data(struct pwm_device *pwm, void *data) | |
207 | { | |
208 | if (!pwm) | |
209 | return -EINVAL; | |
210 | ||
211 | pwm->chip_data = data; | |
212 | ||
213 | return 0; | |
214 | } | |
928c4477 | 215 | EXPORT_SYMBOL_GPL(pwm_set_chip_data); |
f051c466 TR |
216 | |
217 | /** | |
218 | * pwm_get_chip_data() - get private chip data for a PWM | |
219 | * @pwm: PWM device | |
04883802 TR |
220 | * |
221 | * Returns: A pointer to the chip-private data for the PWM device. | |
f051c466 TR |
222 | */ |
223 | void *pwm_get_chip_data(struct pwm_device *pwm) | |
224 | { | |
225 | return pwm ? pwm->chip_data : NULL; | |
0c2498f1 | 226 | } |
928c4477 | 227 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); |
0c2498f1 | 228 | |
5ec803ed BB |
229 | static bool pwm_ops_check(const struct pwm_ops *ops) |
230 | { | |
231 | /* driver supports legacy, non-atomic operation */ | |
232 | if (ops->config && ops->enable && ops->disable) | |
233 | return true; | |
234 | ||
235 | /* driver supports atomic operation */ | |
236 | if (ops->apply) | |
237 | return true; | |
238 | ||
239 | return false; | |
240 | } | |
241 | ||
0c2498f1 | 242 | /** |
b6a00fae | 243 | * pwmchip_add_with_polarity() - register a new PWM chip |
0c2498f1 | 244 | * @chip: the PWM chip to add |
b6a00fae | 245 | * @polarity: initial polarity of PWM channels |
f051c466 TR |
246 | * |
247 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base | |
b6a00fae TK |
248 | * will be used. The initial polarity for all channels is specified by the |
249 | * @polarity parameter. | |
04883802 TR |
250 | * |
251 | * Returns: 0 on success or a negative error code on failure. | |
0c2498f1 | 252 | */ |
b6a00fae TK |
253 | int pwmchip_add_with_polarity(struct pwm_chip *chip, |
254 | enum pwm_polarity polarity) | |
0c2498f1 SH |
255 | { |
256 | struct pwm_device *pwm; | |
f051c466 TR |
257 | unsigned int i; |
258 | int ret; | |
0c2498f1 | 259 | |
5ec803ed BB |
260 | if (!chip || !chip->dev || !chip->ops || !chip->npwm) |
261 | return -EINVAL; | |
262 | ||
263 | if (!pwm_ops_check(chip->ops)) | |
f051c466 | 264 | return -EINVAL; |
0c2498f1 SH |
265 | |
266 | mutex_lock(&pwm_lock); | |
267 | ||
f051c466 TR |
268 | ret = alloc_pwms(chip->base, chip->npwm); |
269 | if (ret < 0) | |
270 | goto out; | |
271 | ||
272 | chip->pwms = kzalloc(chip->npwm * sizeof(*pwm), GFP_KERNEL); | |
273 | if (!chip->pwms) { | |
274 | ret = -ENOMEM; | |
0c2498f1 SH |
275 | goto out; |
276 | } | |
277 | ||
f051c466 TR |
278 | chip->base = ret; |
279 | ||
280 | for (i = 0; i < chip->npwm; i++) { | |
281 | pwm = &chip->pwms[i]; | |
282 | ||
283 | pwm->chip = chip; | |
284 | pwm->pwm = chip->base + i; | |
285 | pwm->hwpwm = i; | |
43a276b0 | 286 | pwm->state.polarity = polarity; |
0c2498f1 | 287 | |
15fa8a43 BB |
288 | if (chip->ops->get_state) |
289 | chip->ops->get_state(chip, pwm, &pwm->state); | |
290 | ||
f051c466 TR |
291 | radix_tree_insert(&pwm_tree, pwm->pwm, pwm); |
292 | } | |
293 | ||
294 | bitmap_set(allocated_pwms, chip->base, chip->npwm); | |
295 | ||
296 | INIT_LIST_HEAD(&chip->list); | |
297 | list_add(&chip->list, &pwm_chips); | |
0c2498f1 | 298 | |
f051c466 TR |
299 | ret = 0; |
300 | ||
7299ab70 TR |
301 | if (IS_ENABLED(CONFIG_OF)) |
302 | of_pwmchip_add(chip); | |
303 | ||
76abbdde HS |
304 | pwmchip_sysfs_export(chip); |
305 | ||
f051c466 TR |
306 | out: |
307 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
308 | return ret; |
309 | } | |
b6a00fae TK |
310 | EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity); |
311 | ||
312 | /** | |
313 | * pwmchip_add() - register a new PWM chip | |
314 | * @chip: the PWM chip to add | |
315 | * | |
316 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base | |
317 | * will be used. The initial polarity for all channels is normal. | |
04883802 TR |
318 | * |
319 | * Returns: 0 on success or a negative error code on failure. | |
b6a00fae TK |
320 | */ |
321 | int pwmchip_add(struct pwm_chip *chip) | |
322 | { | |
323 | return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL); | |
324 | } | |
0c2498f1 SH |
325 | EXPORT_SYMBOL_GPL(pwmchip_add); |
326 | ||
327 | /** | |
328 | * pwmchip_remove() - remove a PWM chip | |
329 | * @chip: the PWM chip to remove | |
330 | * | |
331 | * Removes a PWM chip. This function may return busy if the PWM chip provides | |
332 | * a PWM device that is still requested. | |
04883802 TR |
333 | * |
334 | * Returns: 0 on success or a negative error code on failure. | |
0c2498f1 SH |
335 | */ |
336 | int pwmchip_remove(struct pwm_chip *chip) | |
337 | { | |
f051c466 | 338 | unsigned int i; |
0c2498f1 SH |
339 | int ret = 0; |
340 | ||
341 | mutex_lock(&pwm_lock); | |
342 | ||
f051c466 TR |
343 | for (i = 0; i < chip->npwm; i++) { |
344 | struct pwm_device *pwm = &chip->pwms[i]; | |
0c2498f1 | 345 | |
f051c466 TR |
346 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) { |
347 | ret = -EBUSY; | |
348 | goto out; | |
349 | } | |
0c2498f1 SH |
350 | } |
351 | ||
f051c466 | 352 | list_del_init(&chip->list); |
7299ab70 TR |
353 | |
354 | if (IS_ENABLED(CONFIG_OF)) | |
355 | of_pwmchip_remove(chip); | |
356 | ||
f051c466 | 357 | free_pwms(chip); |
0c2498f1 | 358 | |
76abbdde HS |
359 | pwmchip_sysfs_unexport(chip); |
360 | ||
0c2498f1 SH |
361 | out: |
362 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
363 | return ret; |
364 | } | |
365 | EXPORT_SYMBOL_GPL(pwmchip_remove); | |
366 | ||
367 | /** | |
368 | * pwm_request() - request a PWM device | |
04883802 | 369 | * @pwm: global PWM device index |
0c2498f1 | 370 | * @label: PWM device label |
8138d2dd TR |
371 | * |
372 | * This function is deprecated, use pwm_get() instead. | |
04883802 TR |
373 | * |
374 | * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on | |
375 | * failure. | |
0c2498f1 | 376 | */ |
f051c466 | 377 | struct pwm_device *pwm_request(int pwm, const char *label) |
0c2498f1 | 378 | { |
f051c466 TR |
379 | struct pwm_device *dev; |
380 | int err; | |
381 | ||
382 | if (pwm < 0 || pwm >= MAX_PWMS) | |
383 | return ERR_PTR(-EINVAL); | |
0c2498f1 SH |
384 | |
385 | mutex_lock(&pwm_lock); | |
386 | ||
f051c466 TR |
387 | dev = pwm_to_device(pwm); |
388 | if (!dev) { | |
389 | dev = ERR_PTR(-EPROBE_DEFER); | |
0c2498f1 SH |
390 | goto out; |
391 | } | |
392 | ||
f051c466 TR |
393 | err = pwm_device_request(dev, label); |
394 | if (err < 0) | |
395 | dev = ERR_PTR(err); | |
0c2498f1 | 396 | |
f051c466 TR |
397 | out: |
398 | mutex_unlock(&pwm_lock); | |
0c2498f1 | 399 | |
f051c466 TR |
400 | return dev; |
401 | } | |
402 | EXPORT_SYMBOL_GPL(pwm_request); | |
0c2498f1 | 403 | |
f051c466 TR |
404 | /** |
405 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip | |
406 | * @chip: PWM chip | |
407 | * @index: per-chip index of the PWM to request | |
408 | * @label: a literal description string of this PWM | |
409 | * | |
04883802 TR |
410 | * Returns: A pointer to the PWM device at the given index of the given PWM |
411 | * chip. A negative error code is returned if the index is not valid for the | |
412 | * specified PWM chip or if the PWM device cannot be requested. | |
f051c466 TR |
413 | */ |
414 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, | |
415 | unsigned int index, | |
416 | const char *label) | |
417 | { | |
418 | struct pwm_device *pwm; | |
419 | int err; | |
0c2498f1 | 420 | |
f051c466 TR |
421 | if (!chip || index >= chip->npwm) |
422 | return ERR_PTR(-EINVAL); | |
0c2498f1 | 423 | |
f051c466 TR |
424 | mutex_lock(&pwm_lock); |
425 | pwm = &chip->pwms[index]; | |
0c2498f1 | 426 | |
f051c466 TR |
427 | err = pwm_device_request(pwm, label); |
428 | if (err < 0) | |
429 | pwm = ERR_PTR(err); | |
430 | ||
431 | mutex_unlock(&pwm_lock); | |
0c2498f1 SH |
432 | return pwm; |
433 | } | |
f051c466 | 434 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
0c2498f1 SH |
435 | |
436 | /** | |
437 | * pwm_free() - free a PWM device | |
438 | * @pwm: PWM device | |
8138d2dd TR |
439 | * |
440 | * This function is deprecated, use pwm_put() instead. | |
0c2498f1 SH |
441 | */ |
442 | void pwm_free(struct pwm_device *pwm) | |
443 | { | |
8138d2dd | 444 | pwm_put(pwm); |
0c2498f1 SH |
445 | } |
446 | EXPORT_SYMBOL_GPL(pwm_free); | |
447 | ||
448 | /** | |
5ec803ed | 449 | * pwm_apply_state() - atomically apply a new state to a PWM device |
0c2498f1 | 450 | * @pwm: PWM device |
5ec803ed BB |
451 | * @state: new state to apply. This can be adjusted by the PWM driver |
452 | * if the requested config is not achievable, for example, | |
453 | * ->duty_cycle and ->period might be approximated. | |
0c2498f1 | 454 | */ |
5ec803ed | 455 | int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state) |
0c2498f1 | 456 | { |
76abbdde HS |
457 | int err; |
458 | ||
5ec803ed | 459 | if (!pwm) |
f051c466 TR |
460 | return -EINVAL; |
461 | ||
5ec803ed BB |
462 | if (!memcmp(state, &pwm->state, sizeof(*state))) |
463 | return 0; | |
76abbdde | 464 | |
5ec803ed BB |
465 | if (pwm->chip->ops->apply) { |
466 | err = pwm->chip->ops->apply(pwm->chip, pwm, state); | |
467 | if (err) | |
468 | return err; | |
76abbdde | 469 | |
5ec803ed BB |
470 | pwm->state = *state; |
471 | } else { | |
472 | /* | |
473 | * FIXME: restore the initial state in case of error. | |
474 | */ | |
475 | if (state->polarity != pwm->state.polarity) { | |
476 | if (!pwm->chip->ops->set_polarity) | |
477 | return -ENOTSUPP; | |
478 | ||
479 | /* | |
480 | * Changing the polarity of a running PWM is | |
481 | * only allowed when the PWM driver implements | |
482 | * ->apply(). | |
483 | */ | |
484 | if (pwm->state.enabled) { | |
485 | pwm->chip->ops->disable(pwm->chip, pwm); | |
486 | pwm->state.enabled = false; | |
487 | } | |
488 | ||
489 | err = pwm->chip->ops->set_polarity(pwm->chip, pwm, | |
490 | state->polarity); | |
491 | if (err) | |
492 | return err; | |
493 | ||
494 | pwm->state.polarity = state->polarity; | |
495 | } | |
0aa0869c | 496 | |
5ec803ed BB |
497 | if (state->period != pwm->state.period || |
498 | state->duty_cycle != pwm->state.duty_cycle) { | |
499 | err = pwm->chip->ops->config(pwm->chip, pwm, | |
500 | state->duty_cycle, | |
501 | state->period); | |
502 | if (err) | |
503 | return err; | |
0aa0869c | 504 | |
5ec803ed BB |
505 | pwm->state.duty_cycle = state->duty_cycle; |
506 | pwm->state.period = state->period; | |
507 | } | |
0aa0869c | 508 | |
5ec803ed BB |
509 | if (state->enabled != pwm->state.enabled) { |
510 | if (state->enabled) { | |
511 | err = pwm->chip->ops->enable(pwm->chip, pwm); | |
512 | if (err) | |
513 | return err; | |
514 | } else { | |
515 | pwm->chip->ops->disable(pwm->chip, pwm); | |
516 | } | |
76abbdde | 517 | |
5ec803ed BB |
518 | pwm->state.enabled = state->enabled; |
519 | } | |
520 | } | |
76abbdde | 521 | |
459a25af | 522 | return 0; |
0aa0869c | 523 | } |
5ec803ed | 524 | EXPORT_SYMBOL_GPL(pwm_apply_state); |
0aa0869c | 525 | |
0c2498f1 | 526 | /** |
5ec803ed | 527 | * pwm_adjust_config() - adjust the current PWM config to the PWM arguments |
0c2498f1 | 528 | * @pwm: PWM device |
04883802 | 529 | * |
5ec803ed BB |
530 | * This function will adjust the PWM config to the PWM arguments provided |
531 | * by the DT or PWM lookup table. This is particularly useful to adapt | |
532 | * the bootloader config to the Linux one. | |
0c2498f1 | 533 | */ |
5ec803ed | 534 | int pwm_adjust_config(struct pwm_device *pwm) |
0c2498f1 | 535 | { |
5ec803ed BB |
536 | struct pwm_state state; |
537 | struct pwm_args pargs; | |
538 | ||
539 | pwm_get_args(pwm, &pargs); | |
540 | pwm_get_state(pwm, &state); | |
541 | ||
542 | /* | |
543 | * If the current period is zero it means that either the PWM driver | |
544 | * does not support initial state retrieval or the PWM has not yet | |
545 | * been configured. | |
546 | * | |
547 | * In either case, we setup the new period and polarity, and assign a | |
548 | * duty cycle of 0. | |
549 | */ | |
550 | if (!state.period) { | |
551 | state.duty_cycle = 0; | |
552 | state.period = pargs.period; | |
553 | state.polarity = pargs.polarity; | |
554 | ||
555 | return pwm_apply_state(pwm, &state); | |
d1cd2142 JR |
556 | } |
557 | ||
5ec803ed BB |
558 | /* |
559 | * Adjust the PWM duty cycle/period based on the period value provided | |
560 | * in PWM args. | |
561 | */ | |
562 | if (pargs.period != state.period) { | |
563 | u64 dutycycle = (u64)state.duty_cycle * pargs.period; | |
0c2498f1 | 564 | |
5ec803ed BB |
565 | do_div(dutycycle, state.period); |
566 | state.duty_cycle = dutycycle; | |
567 | state.period = pargs.period; | |
568 | } | |
09a7e4a3 | 569 | |
5ec803ed BB |
570 | /* |
571 | * If the polarity changed, we should also change the duty cycle. | |
572 | */ | |
573 | if (pargs.polarity != state.polarity) { | |
574 | state.polarity = pargs.polarity; | |
575 | state.duty_cycle = state.period - state.duty_cycle; | |
09a7e4a3 | 576 | } |
5ec803ed BB |
577 | |
578 | return pwm_apply_state(pwm, &state); | |
0c2498f1 | 579 | } |
5ec803ed | 580 | EXPORT_SYMBOL_GPL(pwm_adjust_config); |
62099abf | 581 | |
7299ab70 TR |
582 | static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) |
583 | { | |
584 | struct pwm_chip *chip; | |
585 | ||
586 | mutex_lock(&pwm_lock); | |
587 | ||
588 | list_for_each_entry(chip, &pwm_chips, list) | |
589 | if (chip->dev && chip->dev->of_node == np) { | |
590 | mutex_unlock(&pwm_lock); | |
591 | return chip; | |
592 | } | |
593 | ||
594 | mutex_unlock(&pwm_lock); | |
595 | ||
596 | return ERR_PTR(-EPROBE_DEFER); | |
597 | } | |
598 | ||
599 | /** | |
8eb96127 | 600 | * of_pwm_get() - request a PWM via the PWM framework |
7299ab70 TR |
601 | * @np: device node to get the PWM from |
602 | * @con_id: consumer name | |
603 | * | |
604 | * Returns the PWM device parsed from the phandle and index specified in the | |
605 | * "pwms" property of a device tree node or a negative error-code on failure. | |
606 | * Values parsed from the device tree are stored in the returned PWM device | |
607 | * object. | |
608 | * | |
609 | * If con_id is NULL, the first PWM device listed in the "pwms" property will | |
610 | * be requested. Otherwise the "pwm-names" property is used to do a reverse | |
611 | * lookup of the PWM index. This also means that the "pwm-names" property | |
612 | * becomes mandatory for devices that look up the PWM device via the con_id | |
613 | * parameter. | |
04883802 TR |
614 | * |
615 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
616 | * error code on failure. | |
7299ab70 | 617 | */ |
8eb96127 | 618 | struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id) |
7299ab70 TR |
619 | { |
620 | struct pwm_device *pwm = NULL; | |
621 | struct of_phandle_args args; | |
622 | struct pwm_chip *pc; | |
623 | int index = 0; | |
624 | int err; | |
625 | ||
626 | if (con_id) { | |
627 | index = of_property_match_string(np, "pwm-names", con_id); | |
628 | if (index < 0) | |
629 | return ERR_PTR(index); | |
630 | } | |
631 | ||
632 | err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, | |
633 | &args); | |
634 | if (err) { | |
635 | pr_debug("%s(): can't parse \"pwms\" property\n", __func__); | |
636 | return ERR_PTR(err); | |
637 | } | |
638 | ||
639 | pc = of_node_to_pwmchip(args.np); | |
640 | if (IS_ERR(pc)) { | |
641 | pr_debug("%s(): PWM chip not found\n", __func__); | |
642 | pwm = ERR_CAST(pc); | |
643 | goto put; | |
644 | } | |
645 | ||
646 | if (args.args_count != pc->of_pwm_n_cells) { | |
647 | pr_debug("%s: wrong #pwm-cells for %s\n", np->full_name, | |
648 | args.np->full_name); | |
649 | pwm = ERR_PTR(-EINVAL); | |
650 | goto put; | |
651 | } | |
652 | ||
653 | pwm = pc->of_xlate(pc, &args); | |
654 | if (IS_ERR(pwm)) | |
655 | goto put; | |
656 | ||
657 | /* | |
658 | * If a consumer name was not given, try to look it up from the | |
659 | * "pwm-names" property if it exists. Otherwise use the name of | |
660 | * the user device node. | |
661 | */ | |
662 | if (!con_id) { | |
663 | err = of_property_read_string_index(np, "pwm-names", index, | |
664 | &con_id); | |
665 | if (err < 0) | |
666 | con_id = np->name; | |
667 | } | |
668 | ||
669 | pwm->label = con_id; | |
670 | ||
671 | put: | |
672 | of_node_put(args.np); | |
673 | ||
674 | return pwm; | |
675 | } | |
8eb96127 | 676 | EXPORT_SYMBOL_GPL(of_pwm_get); |
7299ab70 | 677 | |
8138d2dd TR |
678 | /** |
679 | * pwm_add_table() - register PWM device consumers | |
680 | * @table: array of consumers to register | |
681 | * @num: number of consumers in table | |
682 | */ | |
c264f111 | 683 | void pwm_add_table(struct pwm_lookup *table, size_t num) |
8138d2dd TR |
684 | { |
685 | mutex_lock(&pwm_lookup_lock); | |
686 | ||
687 | while (num--) { | |
688 | list_add_tail(&table->list, &pwm_lookup_list); | |
689 | table++; | |
690 | } | |
691 | ||
692 | mutex_unlock(&pwm_lookup_lock); | |
693 | } | |
694 | ||
efb0de55 SK |
695 | /** |
696 | * pwm_remove_table() - unregister PWM device consumers | |
697 | * @table: array of consumers to unregister | |
698 | * @num: number of consumers in table | |
699 | */ | |
700 | void pwm_remove_table(struct pwm_lookup *table, size_t num) | |
701 | { | |
702 | mutex_lock(&pwm_lookup_lock); | |
703 | ||
704 | while (num--) { | |
705 | list_del(&table->list); | |
706 | table++; | |
707 | } | |
708 | ||
709 | mutex_unlock(&pwm_lookup_lock); | |
710 | } | |
711 | ||
8138d2dd TR |
712 | /** |
713 | * pwm_get() - look up and request a PWM device | |
714 | * @dev: device for PWM consumer | |
715 | * @con_id: consumer name | |
716 | * | |
7299ab70 TR |
717 | * Lookup is first attempted using DT. If the device was not instantiated from |
718 | * a device tree, a PWM chip and a relative index is looked up via a table | |
719 | * supplied by board setup code (see pwm_add_table()). | |
8138d2dd TR |
720 | * |
721 | * Once a PWM chip has been found the specified PWM device will be requested | |
722 | * and is ready to be used. | |
04883802 TR |
723 | * |
724 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
725 | * error code on failure. | |
8138d2dd TR |
726 | */ |
727 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) | |
728 | { | |
729 | struct pwm_device *pwm = ERR_PTR(-EPROBE_DEFER); | |
e50d3523 | 730 | const char *dev_id = dev ? dev_name(dev) : NULL; |
8138d2dd TR |
731 | struct pwm_chip *chip = NULL; |
732 | unsigned int best = 0; | |
70145f87 | 733 | struct pwm_lookup *p, *chosen = NULL; |
8138d2dd TR |
734 | unsigned int match; |
735 | ||
7299ab70 TR |
736 | /* look up via DT first */ |
737 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) | |
8eb96127 | 738 | return of_pwm_get(dev->of_node, con_id); |
7299ab70 | 739 | |
8138d2dd TR |
740 | /* |
741 | * We look up the provider in the static table typically provided by | |
742 | * board setup code. We first try to lookup the consumer device by | |
743 | * name. If the consumer device was passed in as NULL or if no match | |
744 | * was found, we try to find the consumer by directly looking it up | |
745 | * by name. | |
746 | * | |
747 | * If a match is found, the provider PWM chip is looked up by name | |
748 | * and a PWM device is requested using the PWM device per-chip index. | |
749 | * | |
750 | * The lookup algorithm was shamelessly taken from the clock | |
751 | * framework: | |
752 | * | |
753 | * We do slightly fuzzy matching here: | |
754 | * An entry with a NULL ID is assumed to be a wildcard. | |
755 | * If an entry has a device ID, it must match | |
756 | * If an entry has a connection ID, it must match | |
757 | * Then we take the most specific entry - with the following order | |
758 | * of precedence: dev+con > dev only > con only. | |
759 | */ | |
760 | mutex_lock(&pwm_lookup_lock); | |
761 | ||
762 | list_for_each_entry(p, &pwm_lookup_list, list) { | |
763 | match = 0; | |
764 | ||
765 | if (p->dev_id) { | |
766 | if (!dev_id || strcmp(p->dev_id, dev_id)) | |
767 | continue; | |
768 | ||
769 | match += 2; | |
770 | } | |
771 | ||
772 | if (p->con_id) { | |
773 | if (!con_id || strcmp(p->con_id, con_id)) | |
774 | continue; | |
775 | ||
776 | match += 1; | |
777 | } | |
778 | ||
779 | if (match > best) { | |
70145f87 | 780 | chosen = p; |
8138d2dd TR |
781 | |
782 | if (match != 3) | |
783 | best = match; | |
784 | else | |
785 | break; | |
786 | } | |
787 | } | |
788 | ||
655a0355 TR |
789 | if (!chosen) { |
790 | pwm = ERR_PTR(-ENODEV); | |
70145f87 | 791 | goto out; |
655a0355 | 792 | } |
3796ce1d | 793 | |
70145f87 GU |
794 | chip = pwmchip_find_by_name(chosen->provider); |
795 | if (!chip) | |
796 | goto out; | |
3796ce1d | 797 | |
70145f87 GU |
798 | pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); |
799 | if (IS_ERR(pwm)) | |
800 | goto out; | |
8138d2dd | 801 | |
fbd45a12 BB |
802 | pwm->args.period = chosen->period; |
803 | pwm->args.polarity = chosen->polarity; | |
804 | ||
70145f87 GU |
805 | out: |
806 | mutex_unlock(&pwm_lookup_lock); | |
8138d2dd TR |
807 | return pwm; |
808 | } | |
809 | EXPORT_SYMBOL_GPL(pwm_get); | |
810 | ||
811 | /** | |
812 | * pwm_put() - release a PWM device | |
813 | * @pwm: PWM device | |
814 | */ | |
815 | void pwm_put(struct pwm_device *pwm) | |
816 | { | |
817 | if (!pwm) | |
818 | return; | |
819 | ||
820 | mutex_lock(&pwm_lock); | |
821 | ||
822 | if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { | |
e50d3523 | 823 | pr_warn("PWM device already freed\n"); |
8138d2dd TR |
824 | goto out; |
825 | } | |
826 | ||
827 | if (pwm->chip->ops->free) | |
828 | pwm->chip->ops->free(pwm->chip, pwm); | |
829 | ||
830 | pwm->label = NULL; | |
831 | ||
832 | module_put(pwm->chip->ops->owner); | |
833 | out: | |
834 | mutex_unlock(&pwm_lock); | |
835 | } | |
836 | EXPORT_SYMBOL_GPL(pwm_put); | |
837 | ||
6354316d AC |
838 | static void devm_pwm_release(struct device *dev, void *res) |
839 | { | |
840 | pwm_put(*(struct pwm_device **)res); | |
841 | } | |
842 | ||
843 | /** | |
844 | * devm_pwm_get() - resource managed pwm_get() | |
845 | * @dev: device for PWM consumer | |
846 | * @con_id: consumer name | |
847 | * | |
848 | * This function performs like pwm_get() but the acquired PWM device will | |
849 | * automatically be released on driver detach. | |
04883802 TR |
850 | * |
851 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
852 | * error code on failure. | |
6354316d AC |
853 | */ |
854 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) | |
855 | { | |
856 | struct pwm_device **ptr, *pwm; | |
857 | ||
77f0b9d2 | 858 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
6354316d AC |
859 | if (!ptr) |
860 | return ERR_PTR(-ENOMEM); | |
861 | ||
862 | pwm = pwm_get(dev, con_id); | |
863 | if (!IS_ERR(pwm)) { | |
864 | *ptr = pwm; | |
865 | devres_add(dev, ptr); | |
866 | } else { | |
867 | devres_free(ptr); | |
868 | } | |
869 | ||
870 | return pwm; | |
871 | } | |
872 | EXPORT_SYMBOL_GPL(devm_pwm_get); | |
873 | ||
261a5edd PU |
874 | /** |
875 | * devm_of_pwm_get() - resource managed of_pwm_get() | |
876 | * @dev: device for PWM consumer | |
877 | * @np: device node to get the PWM from | |
878 | * @con_id: consumer name | |
879 | * | |
880 | * This function performs like of_pwm_get() but the acquired PWM device will | |
881 | * automatically be released on driver detach. | |
04883802 TR |
882 | * |
883 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded | |
884 | * error code on failure. | |
261a5edd PU |
885 | */ |
886 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, | |
887 | const char *con_id) | |
888 | { | |
889 | struct pwm_device **ptr, *pwm; | |
890 | ||
77f0b9d2 | 891 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
261a5edd PU |
892 | if (!ptr) |
893 | return ERR_PTR(-ENOMEM); | |
894 | ||
895 | pwm = of_pwm_get(np, con_id); | |
896 | if (!IS_ERR(pwm)) { | |
897 | *ptr = pwm; | |
898 | devres_add(dev, ptr); | |
899 | } else { | |
900 | devres_free(ptr); | |
901 | } | |
902 | ||
903 | return pwm; | |
904 | } | |
905 | EXPORT_SYMBOL_GPL(devm_of_pwm_get); | |
906 | ||
6354316d AC |
907 | static int devm_pwm_match(struct device *dev, void *res, void *data) |
908 | { | |
909 | struct pwm_device **p = res; | |
910 | ||
911 | if (WARN_ON(!p || !*p)) | |
912 | return 0; | |
913 | ||
914 | return *p == data; | |
915 | } | |
916 | ||
917 | /** | |
918 | * devm_pwm_put() - resource managed pwm_put() | |
919 | * @dev: device for PWM consumer | |
920 | * @pwm: PWM device | |
921 | * | |
922 | * Release a PWM previously allocated using devm_pwm_get(). Calling this | |
923 | * function is usually not needed because devm-allocated resources are | |
924 | * automatically released on driver detach. | |
925 | */ | |
926 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm) | |
927 | { | |
928 | WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm)); | |
929 | } | |
930 | EXPORT_SYMBOL_GPL(devm_pwm_put); | |
931 | ||
6e69ab13 FV |
932 | /** |
933 | * pwm_can_sleep() - report whether PWM access will sleep | |
934 | * @pwm: PWM device | |
935 | * | |
04883802 | 936 | * Returns: True if accessing the PWM can sleep, false otherwise. |
6e69ab13 FV |
937 | */ |
938 | bool pwm_can_sleep(struct pwm_device *pwm) | |
939 | { | |
ff01c944 | 940 | return true; |
6e69ab13 FV |
941 | } |
942 | EXPORT_SYMBOL_GPL(pwm_can_sleep); | |
943 | ||
62099abf TR |
944 | #ifdef CONFIG_DEBUG_FS |
945 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) | |
946 | { | |
947 | unsigned int i; | |
948 | ||
949 | for (i = 0; i < chip->npwm; i++) { | |
950 | struct pwm_device *pwm = &chip->pwms[i]; | |
951 | ||
952 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); | |
953 | ||
954 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) | |
adcba1e3 | 955 | seq_puts(s, " requested"); |
62099abf | 956 | |
5c31252c | 957 | if (pwm_is_enabled(pwm)) |
adcba1e3 | 958 | seq_puts(s, " enabled"); |
62099abf | 959 | |
adcba1e3 | 960 | seq_puts(s, "\n"); |
62099abf TR |
961 | } |
962 | } | |
963 | ||
964 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) | |
965 | { | |
966 | mutex_lock(&pwm_lock); | |
967 | s->private = ""; | |
968 | ||
969 | return seq_list_start(&pwm_chips, *pos); | |
970 | } | |
971 | ||
972 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
973 | { | |
974 | s->private = "\n"; | |
975 | ||
976 | return seq_list_next(v, &pwm_chips, pos); | |
977 | } | |
978 | ||
979 | static void pwm_seq_stop(struct seq_file *s, void *v) | |
980 | { | |
981 | mutex_unlock(&pwm_lock); | |
982 | } | |
983 | ||
984 | static int pwm_seq_show(struct seq_file *s, void *v) | |
985 | { | |
986 | struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); | |
987 | ||
988 | seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, | |
989 | chip->dev->bus ? chip->dev->bus->name : "no-bus", | |
990 | dev_name(chip->dev), chip->npwm, | |
991 | (chip->npwm != 1) ? "s" : ""); | |
992 | ||
993 | if (chip->ops->dbg_show) | |
994 | chip->ops->dbg_show(chip, s); | |
995 | else | |
996 | pwm_dbg_show(chip, s); | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static const struct seq_operations pwm_seq_ops = { | |
1002 | .start = pwm_seq_start, | |
1003 | .next = pwm_seq_next, | |
1004 | .stop = pwm_seq_stop, | |
1005 | .show = pwm_seq_show, | |
1006 | }; | |
1007 | ||
1008 | static int pwm_seq_open(struct inode *inode, struct file *file) | |
1009 | { | |
1010 | return seq_open(file, &pwm_seq_ops); | |
1011 | } | |
1012 | ||
1013 | static const struct file_operations pwm_debugfs_ops = { | |
1014 | .owner = THIS_MODULE, | |
1015 | .open = pwm_seq_open, | |
1016 | .read = seq_read, | |
1017 | .llseek = seq_lseek, | |
1018 | .release = seq_release, | |
1019 | }; | |
1020 | ||
1021 | static int __init pwm_debugfs_init(void) | |
1022 | { | |
1023 | debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, | |
1024 | &pwm_debugfs_ops); | |
1025 | ||
1026 | return 0; | |
1027 | } | |
62099abf TR |
1028 | subsys_initcall(pwm_debugfs_init); |
1029 | #endif /* CONFIG_DEBUG_FS */ |