b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Generic pwmlib implementation |
| 4 | * |
| 5 | * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de> |
| 6 | * Copyright (C) 2011-2012 Avionic Design GmbH |
| 7 | */ |
| 8 | |
| 9 | #include <linux/acpi.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/pwm.h> |
| 12 | #include <linux/radix-tree.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/debugfs.h> |
| 19 | #include <linux/seq_file.h> |
| 20 | |
| 21 | #include <dt-bindings/pwm/pwm.h> |
| 22 | |
| 23 | #define MAX_PWMS 1024 |
| 24 | |
| 25 | static DEFINE_MUTEX(pwm_lookup_lock); |
| 26 | static LIST_HEAD(pwm_lookup_list); |
| 27 | static DEFINE_MUTEX(pwm_lock); |
| 28 | static LIST_HEAD(pwm_chips); |
| 29 | static DECLARE_BITMAP(allocated_pwms, MAX_PWMS); |
| 30 | static RADIX_TREE(pwm_tree, GFP_KERNEL); |
| 31 | |
| 32 | static struct pwm_device *pwm_to_device(unsigned int pwm) |
| 33 | { |
| 34 | return radix_tree_lookup(&pwm_tree, pwm); |
| 35 | } |
| 36 | |
| 37 | static int alloc_pwms(int pwm, unsigned int count) |
| 38 | { |
| 39 | unsigned int from = 0; |
| 40 | unsigned int start; |
| 41 | |
| 42 | if (pwm >= MAX_PWMS) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | if (pwm >= 0) |
| 46 | from = pwm; |
| 47 | |
| 48 | start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, |
| 49 | count, 0); |
| 50 | |
| 51 | if (pwm >= 0 && start != pwm) |
| 52 | return -EEXIST; |
| 53 | |
| 54 | if (start + count > MAX_PWMS) |
| 55 | return -ENOSPC; |
| 56 | |
| 57 | return start; |
| 58 | } |
| 59 | |
| 60 | static void free_pwms(struct pwm_chip *chip) |
| 61 | { |
| 62 | unsigned int i; |
| 63 | |
| 64 | for (i = 0; i < chip->npwm; i++) { |
| 65 | struct pwm_device *pwm = &chip->pwms[i]; |
| 66 | |
| 67 | radix_tree_delete(&pwm_tree, pwm->pwm); |
| 68 | } |
| 69 | |
| 70 | bitmap_clear(allocated_pwms, chip->base, chip->npwm); |
| 71 | |
| 72 | kfree(chip->pwms); |
| 73 | chip->pwms = NULL; |
| 74 | } |
| 75 | |
| 76 | static struct pwm_chip *pwmchip_find_by_name(const char *name) |
| 77 | { |
| 78 | struct pwm_chip *chip; |
| 79 | |
| 80 | if (!name) |
| 81 | return NULL; |
| 82 | |
| 83 | mutex_lock(&pwm_lock); |
| 84 | |
| 85 | list_for_each_entry(chip, &pwm_chips, list) { |
| 86 | const char *chip_name = dev_name(chip->dev); |
| 87 | |
| 88 | if (chip_name && strcmp(chip_name, name) == 0) { |
| 89 | mutex_unlock(&pwm_lock); |
| 90 | return chip; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | mutex_unlock(&pwm_lock); |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | static int pwm_device_request(struct pwm_device *pwm, const char *label) |
| 100 | { |
| 101 | int err; |
| 102 | |
| 103 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) |
| 104 | return -EBUSY; |
| 105 | |
| 106 | if (!try_module_get(pwm->chip->ops->owner)) |
| 107 | return -ENODEV; |
| 108 | |
| 109 | if (pwm->chip->ops->request) { |
| 110 | err = pwm->chip->ops->request(pwm->chip, pwm); |
| 111 | if (err) { |
| 112 | module_put(pwm->chip->ops->owner); |
| 113 | return err; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | set_bit(PWMF_REQUESTED, &pwm->flags); |
| 118 | pwm->label = label; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | struct pwm_device * |
| 124 | of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args) |
| 125 | { |
| 126 | struct pwm_device *pwm; |
| 127 | |
| 128 | /* check, whether the driver supports a third cell for flags */ |
| 129 | if (pc->of_pwm_n_cells < 3) |
| 130 | return ERR_PTR(-EINVAL); |
| 131 | |
| 132 | /* flags in the third cell are optional */ |
| 133 | if (args->args_count < 2) |
| 134 | return ERR_PTR(-EINVAL); |
| 135 | |
| 136 | if (args->args[0] >= pc->npwm) |
| 137 | return ERR_PTR(-EINVAL); |
| 138 | |
| 139 | pwm = pwm_request_from_chip(pc, args->args[0], NULL); |
| 140 | if (IS_ERR(pwm)) |
| 141 | return pwm; |
| 142 | |
| 143 | pwm->args.period = args->args[1]; |
| 144 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
| 145 | |
| 146 | if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) |
| 147 | pwm->args.polarity = PWM_POLARITY_INVERSED; |
| 148 | |
| 149 | return pwm; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); |
| 152 | |
| 153 | static struct pwm_device * |
| 154 | of_pwm_simple_xlate(struct pwm_chip *pc, const struct of_phandle_args *args) |
| 155 | { |
| 156 | struct pwm_device *pwm; |
| 157 | |
| 158 | /* sanity check driver support */ |
| 159 | if (pc->of_pwm_n_cells < 2) |
| 160 | return ERR_PTR(-EINVAL); |
| 161 | |
| 162 | /* all cells are required */ |
| 163 | if (args->args_count != pc->of_pwm_n_cells) |
| 164 | return ERR_PTR(-EINVAL); |
| 165 | |
| 166 | if (args->args[0] >= pc->npwm) |
| 167 | return ERR_PTR(-EINVAL); |
| 168 | |
| 169 | pwm = pwm_request_from_chip(pc, args->args[0], NULL); |
| 170 | if (IS_ERR(pwm)) |
| 171 | return pwm; |
| 172 | |
| 173 | pwm->args.period = args->args[1]; |
| 174 | |
| 175 | return pwm; |
| 176 | } |
| 177 | |
| 178 | static void of_pwmchip_add(struct pwm_chip *chip) |
| 179 | { |
| 180 | if (!chip->dev || !chip->dev->of_node) |
| 181 | return; |
| 182 | |
| 183 | if (!chip->of_xlate) { |
| 184 | chip->of_xlate = of_pwm_simple_xlate; |
| 185 | chip->of_pwm_n_cells = 2; |
| 186 | } |
| 187 | |
| 188 | of_node_get(chip->dev->of_node); |
| 189 | } |
| 190 | |
| 191 | static void of_pwmchip_remove(struct pwm_chip *chip) |
| 192 | { |
| 193 | if (chip->dev) |
| 194 | of_node_put(chip->dev->of_node); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * pwm_set_chip_data() - set private chip data for a PWM |
| 199 | * @pwm: PWM device |
| 200 | * @data: pointer to chip-specific data |
| 201 | * |
| 202 | * Returns: 0 on success or a negative error code on failure. |
| 203 | */ |
| 204 | int pwm_set_chip_data(struct pwm_device *pwm, void *data) |
| 205 | { |
| 206 | if (!pwm) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | pwm->chip_data = data; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(pwm_set_chip_data); |
| 214 | |
| 215 | /** |
| 216 | * pwm_get_chip_data() - get private chip data for a PWM |
| 217 | * @pwm: PWM device |
| 218 | * |
| 219 | * Returns: A pointer to the chip-private data for the PWM device. |
| 220 | */ |
| 221 | void *pwm_get_chip_data(struct pwm_device *pwm) |
| 222 | { |
| 223 | return pwm ? pwm->chip_data : NULL; |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(pwm_get_chip_data); |
| 226 | |
| 227 | static bool pwm_ops_check(const struct pwm_ops *ops) |
| 228 | { |
| 229 | /* driver supports legacy, non-atomic operation */ |
| 230 | if (ops->config && ops->enable && ops->disable) |
| 231 | return true; |
| 232 | |
| 233 | /* driver supports atomic operation */ |
| 234 | if (ops->apply) |
| 235 | return true; |
| 236 | |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * pwmchip_add_with_polarity() - register a new PWM chip |
| 242 | * @chip: the PWM chip to add |
| 243 | * @polarity: initial polarity of PWM channels |
| 244 | * |
| 245 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base |
| 246 | * will be used. The initial polarity for all channels is specified by the |
| 247 | * @polarity parameter. |
| 248 | * |
| 249 | * Returns: 0 on success or a negative error code on failure. |
| 250 | */ |
| 251 | int pwmchip_add_with_polarity(struct pwm_chip *chip, |
| 252 | enum pwm_polarity polarity) |
| 253 | { |
| 254 | struct pwm_device *pwm; |
| 255 | unsigned int i; |
| 256 | int ret; |
| 257 | |
| 258 | if (!chip || !chip->dev || !chip->ops || !chip->npwm) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | if (!pwm_ops_check(chip->ops)) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | mutex_lock(&pwm_lock); |
| 265 | |
| 266 | ret = alloc_pwms(chip->base, chip->npwm); |
| 267 | if (ret < 0) |
| 268 | goto out; |
| 269 | |
| 270 | chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); |
| 271 | if (!chip->pwms) { |
| 272 | ret = -ENOMEM; |
| 273 | goto out; |
| 274 | } |
| 275 | |
| 276 | chip->base = ret; |
| 277 | |
| 278 | for (i = 0; i < chip->npwm; i++) { |
| 279 | pwm = &chip->pwms[i]; |
| 280 | |
| 281 | pwm->chip = chip; |
| 282 | pwm->pwm = chip->base + i; |
| 283 | pwm->hwpwm = i; |
| 284 | pwm->state.polarity = polarity; |
| 285 | pwm->state.output_type = PWM_OUTPUT_FIXED; |
| 286 | |
| 287 | if (chip->ops->get_state) |
| 288 | chip->ops->get_state(chip, pwm, &pwm->state); |
| 289 | |
| 290 | radix_tree_insert(&pwm_tree, pwm->pwm, pwm); |
| 291 | } |
| 292 | |
| 293 | bitmap_set(allocated_pwms, chip->base, chip->npwm); |
| 294 | |
| 295 | INIT_LIST_HEAD(&chip->list); |
| 296 | list_add(&chip->list, &pwm_chips); |
| 297 | |
| 298 | ret = 0; |
| 299 | |
| 300 | if (IS_ENABLED(CONFIG_OF)) |
| 301 | of_pwmchip_add(chip); |
| 302 | |
| 303 | out: |
| 304 | mutex_unlock(&pwm_lock); |
| 305 | |
| 306 | if (!ret) |
| 307 | pwmchip_sysfs_export(chip); |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity); |
| 312 | |
| 313 | /** |
| 314 | * pwmchip_add() - register a new PWM chip |
| 315 | * @chip: the PWM chip to add |
| 316 | * |
| 317 | * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base |
| 318 | * will be used. The initial polarity for all channels is normal. |
| 319 | * |
| 320 | * Returns: 0 on success or a negative error code on failure. |
| 321 | */ |
| 322 | int pwmchip_add(struct pwm_chip *chip) |
| 323 | { |
| 324 | return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL); |
| 325 | } |
| 326 | EXPORT_SYMBOL_GPL(pwmchip_add); |
| 327 | |
| 328 | /** |
| 329 | * pwmchip_remove() - remove a PWM chip |
| 330 | * @chip: the PWM chip to remove |
| 331 | * |
| 332 | * Removes a PWM chip. This function may return busy if the PWM chip provides |
| 333 | * a PWM device that is still requested. |
| 334 | * |
| 335 | * Returns: 0 on success or a negative error code on failure. |
| 336 | */ |
| 337 | int pwmchip_remove(struct pwm_chip *chip) |
| 338 | { |
| 339 | unsigned int i; |
| 340 | int ret = 0; |
| 341 | |
| 342 | pwmchip_sysfs_unexport(chip); |
| 343 | |
| 344 | mutex_lock(&pwm_lock); |
| 345 | |
| 346 | for (i = 0; i < chip->npwm; i++) { |
| 347 | struct pwm_device *pwm = &chip->pwms[i]; |
| 348 | |
| 349 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) { |
| 350 | ret = -EBUSY; |
| 351 | goto out; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | list_del_init(&chip->list); |
| 356 | |
| 357 | if (IS_ENABLED(CONFIG_OF)) |
| 358 | of_pwmchip_remove(chip); |
| 359 | |
| 360 | free_pwms(chip); |
| 361 | |
| 362 | out: |
| 363 | mutex_unlock(&pwm_lock); |
| 364 | return ret; |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(pwmchip_remove); |
| 367 | |
| 368 | /** |
| 369 | * pwm_request() - request a PWM device |
| 370 | * @pwm: global PWM device index |
| 371 | * @label: PWM device label |
| 372 | * |
| 373 | * This function is deprecated, use pwm_get() instead. |
| 374 | * |
| 375 | * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on |
| 376 | * failure. |
| 377 | */ |
| 378 | struct pwm_device *pwm_request(int pwm, const char *label) |
| 379 | { |
| 380 | struct pwm_device *dev; |
| 381 | int err; |
| 382 | |
| 383 | if (pwm < 0 || pwm >= MAX_PWMS) |
| 384 | return ERR_PTR(-EINVAL); |
| 385 | |
| 386 | mutex_lock(&pwm_lock); |
| 387 | |
| 388 | dev = pwm_to_device(pwm); |
| 389 | if (!dev) { |
| 390 | dev = ERR_PTR(-EPROBE_DEFER); |
| 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | err = pwm_device_request(dev, label); |
| 395 | if (err < 0) |
| 396 | dev = ERR_PTR(err); |
| 397 | |
| 398 | out: |
| 399 | mutex_unlock(&pwm_lock); |
| 400 | |
| 401 | return dev; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(pwm_request); |
| 404 | |
| 405 | /** |
| 406 | * pwm_request_from_chip() - request a PWM device relative to a PWM chip |
| 407 | * @chip: PWM chip |
| 408 | * @index: per-chip index of the PWM to request |
| 409 | * @label: a literal description string of this PWM |
| 410 | * |
| 411 | * Returns: A pointer to the PWM device at the given index of the given PWM |
| 412 | * chip. A negative error code is returned if the index is not valid for the |
| 413 | * specified PWM chip or if the PWM device cannot be requested. |
| 414 | */ |
| 415 | struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, |
| 416 | unsigned int index, |
| 417 | const char *label) |
| 418 | { |
| 419 | struct pwm_device *pwm; |
| 420 | int err; |
| 421 | |
| 422 | if (!chip || index >= chip->npwm) |
| 423 | return ERR_PTR(-EINVAL); |
| 424 | |
| 425 | mutex_lock(&pwm_lock); |
| 426 | pwm = &chip->pwms[index]; |
| 427 | |
| 428 | err = pwm_device_request(pwm, label); |
| 429 | if (err < 0) |
| 430 | pwm = ERR_PTR(err); |
| 431 | |
| 432 | mutex_unlock(&pwm_lock); |
| 433 | return pwm; |
| 434 | } |
| 435 | EXPORT_SYMBOL_GPL(pwm_request_from_chip); |
| 436 | |
| 437 | /** |
| 438 | * pwm_free() - free a PWM device |
| 439 | * @pwm: PWM device |
| 440 | * |
| 441 | * This function is deprecated, use pwm_put() instead. |
| 442 | */ |
| 443 | void pwm_free(struct pwm_device *pwm) |
| 444 | { |
| 445 | pwm_put(pwm); |
| 446 | } |
| 447 | EXPORT_SYMBOL_GPL(pwm_free); |
| 448 | |
| 449 | /** |
| 450 | * pwm_apply_state() - atomically apply a new state to a PWM device |
| 451 | * @pwm: PWM device |
| 452 | * @state: new state to apply |
| 453 | */ |
| 454 | int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) |
| 455 | { |
| 456 | struct pwm_chip *chip; |
| 457 | int err; |
| 458 | |
| 459 | if (!pwm || !state || !state->period || |
| 460 | state->duty_cycle > state->period) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | chip = pwm->chip; |
| 464 | |
| 465 | if (state->period == pwm->state.period && |
| 466 | state->duty_cycle == pwm->state.duty_cycle && |
| 467 | state->polarity == pwm->state.polarity && |
| 468 | state->enabled == pwm->state.enabled) |
| 469 | return 0; |
| 470 | |
| 471 | if (chip->ops->apply) { |
| 472 | err = chip->ops->apply(chip, pwm, state); |
| 473 | if (err) |
| 474 | return err; |
| 475 | |
| 476 | pwm->state = *state; |
| 477 | } else { |
| 478 | /* |
| 479 | * FIXME: restore the initial state in case of error. |
| 480 | */ |
| 481 | if (state->polarity != pwm->state.polarity) { |
| 482 | if (!chip->ops->set_polarity) |
| 483 | return -ENOTSUPP; |
| 484 | |
| 485 | /* |
| 486 | * Changing the polarity of a running PWM is |
| 487 | * only allowed when the PWM driver implements |
| 488 | * ->apply(). |
| 489 | */ |
| 490 | if (pwm->state.enabled) { |
| 491 | chip->ops->disable(chip, pwm); |
| 492 | pwm->state.enabled = false; |
| 493 | } |
| 494 | |
| 495 | err = chip->ops->set_polarity(chip, pwm, |
| 496 | state->polarity); |
| 497 | if (err) |
| 498 | return err; |
| 499 | |
| 500 | pwm->state.polarity = state->polarity; |
| 501 | } |
| 502 | |
| 503 | if (state->period != pwm->state.period || |
| 504 | state->duty_cycle != pwm->state.duty_cycle) { |
| 505 | err = chip->ops->config(pwm->chip, pwm, |
| 506 | state->duty_cycle, |
| 507 | state->period); |
| 508 | if (err) |
| 509 | return err; |
| 510 | |
| 511 | pwm->state.duty_cycle = state->duty_cycle; |
| 512 | pwm->state.period = state->period; |
| 513 | } |
| 514 | |
| 515 | if (state->enabled != pwm->state.enabled) { |
| 516 | if (state->enabled) { |
| 517 | err = chip->ops->enable(chip, pwm); |
| 518 | if (err) |
| 519 | return err; |
| 520 | } else { |
| 521 | chip->ops->disable(chip, pwm); |
| 522 | } |
| 523 | |
| 524 | pwm->state.enabled = state->enabled; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | EXPORT_SYMBOL_GPL(pwm_apply_state); |
| 531 | |
| 532 | /** |
| 533 | * pwm_capture() - capture and report a PWM signal |
| 534 | * @pwm: PWM device |
| 535 | * @result: structure to fill with capture result |
| 536 | * @timeout: time to wait, in milliseconds, before giving up on capture |
| 537 | * |
| 538 | * Returns: 0 on success or a negative error code on failure. |
| 539 | */ |
| 540 | int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, |
| 541 | unsigned long timeout) |
| 542 | { |
| 543 | int err; |
| 544 | |
| 545 | if (!pwm || !pwm->chip->ops) |
| 546 | return -EINVAL; |
| 547 | |
| 548 | if (!pwm->chip->ops->capture) |
| 549 | return -ENOSYS; |
| 550 | |
| 551 | mutex_lock(&pwm_lock); |
| 552 | err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); |
| 553 | mutex_unlock(&pwm_lock); |
| 554 | |
| 555 | return err; |
| 556 | } |
| 557 | EXPORT_SYMBOL_GPL(pwm_capture); |
| 558 | |
| 559 | /** |
| 560 | * pwm_adjust_config() - adjust the current PWM config to the PWM arguments |
| 561 | * @pwm: PWM device |
| 562 | * |
| 563 | * This function will adjust the PWM config to the PWM arguments provided |
| 564 | * by the DT or PWM lookup table. This is particularly useful to adapt |
| 565 | * the bootloader config to the Linux one. |
| 566 | */ |
| 567 | int pwm_adjust_config(struct pwm_device *pwm) |
| 568 | { |
| 569 | struct pwm_state state; |
| 570 | struct pwm_args pargs; |
| 571 | |
| 572 | pwm_get_args(pwm, &pargs); |
| 573 | pwm_get_state(pwm, &state); |
| 574 | |
| 575 | /* |
| 576 | * If the current period is zero it means that either the PWM driver |
| 577 | * does not support initial state retrieval or the PWM has not yet |
| 578 | * been configured. |
| 579 | * |
| 580 | * In either case, we setup the new period and polarity, and assign a |
| 581 | * duty cycle of 0. |
| 582 | */ |
| 583 | if (!state.period) { |
| 584 | state.duty_cycle = 0; |
| 585 | state.period = pargs.period; |
| 586 | state.polarity = pargs.polarity; |
| 587 | |
| 588 | return pwm_apply_state(pwm, &state); |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Adjust the PWM duty cycle/period based on the period value provided |
| 593 | * in PWM args. |
| 594 | */ |
| 595 | if (pargs.period != state.period) { |
| 596 | u64 dutycycle = (u64)state.duty_cycle * pargs.period; |
| 597 | |
| 598 | do_div(dutycycle, state.period); |
| 599 | state.duty_cycle = dutycycle; |
| 600 | state.period = pargs.period; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * If the polarity changed, we should also change the duty cycle. |
| 605 | */ |
| 606 | if (pargs.polarity != state.polarity) { |
| 607 | state.polarity = pargs.polarity; |
| 608 | state.duty_cycle = state.period - state.duty_cycle; |
| 609 | } |
| 610 | |
| 611 | return pwm_apply_state(pwm, &state); |
| 612 | } |
| 613 | EXPORT_SYMBOL_GPL(pwm_adjust_config); |
| 614 | |
| 615 | static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) |
| 616 | { |
| 617 | struct pwm_chip *chip; |
| 618 | |
| 619 | mutex_lock(&pwm_lock); |
| 620 | |
| 621 | list_for_each_entry(chip, &pwm_chips, list) |
| 622 | if (chip->dev && chip->dev->of_node == np) { |
| 623 | mutex_unlock(&pwm_lock); |
| 624 | return chip; |
| 625 | } |
| 626 | |
| 627 | mutex_unlock(&pwm_lock); |
| 628 | |
| 629 | return ERR_PTR(-EPROBE_DEFER); |
| 630 | } |
| 631 | |
| 632 | static struct device_link *pwm_device_link_add(struct device *dev, |
| 633 | struct pwm_device *pwm) |
| 634 | { |
| 635 | struct device_link *dl; |
| 636 | |
| 637 | if (!dev) { |
| 638 | /* |
| 639 | * No device for the PWM consumer has been provided. It may |
| 640 | * impact the PM sequence ordering: the PWM supplier may get |
| 641 | * suspended before the consumer. |
| 642 | */ |
| 643 | dev_warn(pwm->chip->dev, |
| 644 | "No consumer device specified to create a link to\n"); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); |
| 649 | if (!dl) { |
| 650 | dev_err(dev, "failed to create device link to %s\n", |
| 651 | dev_name(pwm->chip->dev)); |
| 652 | return ERR_PTR(-EINVAL); |
| 653 | } |
| 654 | |
| 655 | return dl; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * of_pwm_get() - request a PWM via the PWM framework |
| 660 | * @dev: device for PWM consumer |
| 661 | * @np: device node to get the PWM from |
| 662 | * @con_id: consumer name |
| 663 | * |
| 664 | * Returns the PWM device parsed from the phandle and index specified in the |
| 665 | * "pwms" property of a device tree node or a negative error-code on failure. |
| 666 | * Values parsed from the device tree are stored in the returned PWM device |
| 667 | * object. |
| 668 | * |
| 669 | * If con_id is NULL, the first PWM device listed in the "pwms" property will |
| 670 | * be requested. Otherwise the "pwm-names" property is used to do a reverse |
| 671 | * lookup of the PWM index. This also means that the "pwm-names" property |
| 672 | * becomes mandatory for devices that look up the PWM device via the con_id |
| 673 | * parameter. |
| 674 | * |
| 675 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 676 | * error code on failure. |
| 677 | */ |
| 678 | struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, |
| 679 | const char *con_id) |
| 680 | { |
| 681 | struct pwm_device *pwm = NULL; |
| 682 | struct of_phandle_args args; |
| 683 | struct device_link *dl; |
| 684 | struct pwm_chip *pc; |
| 685 | int index = 0; |
| 686 | int err; |
| 687 | |
| 688 | if (con_id) { |
| 689 | index = of_property_match_string(np, "pwm-names", con_id); |
| 690 | if (index < 0) |
| 691 | return ERR_PTR(index); |
| 692 | } |
| 693 | |
| 694 | err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index, |
| 695 | &args); |
| 696 | if (err) { |
| 697 | pr_err("%s(): can't parse \"pwms\" property\n", __func__); |
| 698 | return ERR_PTR(err); |
| 699 | } |
| 700 | |
| 701 | pc = of_node_to_pwmchip(args.np); |
| 702 | if (IS_ERR(pc)) { |
| 703 | if (PTR_ERR(pc) != -EPROBE_DEFER) |
| 704 | pr_err("%s(): PWM chip not found\n", __func__); |
| 705 | |
| 706 | pwm = ERR_CAST(pc); |
| 707 | goto put; |
| 708 | } |
| 709 | |
| 710 | pwm = pc->of_xlate(pc, &args); |
| 711 | if (IS_ERR(pwm)) |
| 712 | goto put; |
| 713 | |
| 714 | dl = pwm_device_link_add(dev, pwm); |
| 715 | if (IS_ERR(dl)) { |
| 716 | /* of_xlate ended up calling pwm_request_from_chip() */ |
| 717 | pwm_free(pwm); |
| 718 | pwm = ERR_CAST(dl); |
| 719 | goto put; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * If a consumer name was not given, try to look it up from the |
| 724 | * "pwm-names" property if it exists. Otherwise use the name of |
| 725 | * the user device node. |
| 726 | */ |
| 727 | if (!con_id) { |
| 728 | err = of_property_read_string_index(np, "pwm-names", index, |
| 729 | &con_id); |
| 730 | if (err < 0) |
| 731 | con_id = np->name; |
| 732 | } |
| 733 | |
| 734 | pwm->label = con_id; |
| 735 | |
| 736 | put: |
| 737 | of_node_put(args.np); |
| 738 | |
| 739 | return pwm; |
| 740 | } |
| 741 | EXPORT_SYMBOL_GPL(of_pwm_get); |
| 742 | |
| 743 | #if IS_ENABLED(CONFIG_ACPI) |
| 744 | static struct pwm_chip *device_to_pwmchip(struct device *dev) |
| 745 | { |
| 746 | struct pwm_chip *chip; |
| 747 | |
| 748 | mutex_lock(&pwm_lock); |
| 749 | |
| 750 | list_for_each_entry(chip, &pwm_chips, list) { |
| 751 | struct acpi_device *adev = ACPI_COMPANION(chip->dev); |
| 752 | |
| 753 | if ((chip->dev == dev) || (adev && &adev->dev == dev)) { |
| 754 | mutex_unlock(&pwm_lock); |
| 755 | return chip; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | mutex_unlock(&pwm_lock); |
| 760 | |
| 761 | return ERR_PTR(-EPROBE_DEFER); |
| 762 | } |
| 763 | #endif |
| 764 | |
| 765 | /** |
| 766 | * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI |
| 767 | * @fwnode: firmware node to get the "pwm" property from |
| 768 | * |
| 769 | * Returns the PWM device parsed from the fwnode and index specified in the |
| 770 | * "pwms" property or a negative error-code on failure. |
| 771 | * Values parsed from the device tree are stored in the returned PWM device |
| 772 | * object. |
| 773 | * |
| 774 | * This is analogous to of_pwm_get() except con_id is not yet supported. |
| 775 | * ACPI entries must look like |
| 776 | * Package () {"pwms", Package () |
| 777 | * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} |
| 778 | * |
| 779 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 780 | * error code on failure. |
| 781 | */ |
| 782 | static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode) |
| 783 | { |
| 784 | struct pwm_device *pwm = ERR_PTR(-ENODEV); |
| 785 | #if IS_ENABLED(CONFIG_ACPI) |
| 786 | struct fwnode_reference_args args; |
| 787 | struct acpi_device *acpi; |
| 788 | struct pwm_chip *chip; |
| 789 | int ret; |
| 790 | |
| 791 | memset(&args, 0, sizeof(args)); |
| 792 | |
| 793 | ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); |
| 794 | if (ret < 0) |
| 795 | return ERR_PTR(ret); |
| 796 | |
| 797 | acpi = to_acpi_device_node(args.fwnode); |
| 798 | if (!acpi) |
| 799 | return ERR_PTR(-EINVAL); |
| 800 | |
| 801 | if (args.nargs < 2) |
| 802 | return ERR_PTR(-EPROTO); |
| 803 | |
| 804 | chip = device_to_pwmchip(&acpi->dev); |
| 805 | if (IS_ERR(chip)) |
| 806 | return ERR_CAST(chip); |
| 807 | |
| 808 | pwm = pwm_request_from_chip(chip, args.args[0], NULL); |
| 809 | if (IS_ERR(pwm)) |
| 810 | return pwm; |
| 811 | |
| 812 | pwm->args.period = args.args[1]; |
| 813 | pwm->args.polarity = PWM_POLARITY_NORMAL; |
| 814 | |
| 815 | if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) |
| 816 | pwm->args.polarity = PWM_POLARITY_INVERSED; |
| 817 | #endif |
| 818 | |
| 819 | return pwm; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * pwm_add_table() - register PWM device consumers |
| 824 | * @table: array of consumers to register |
| 825 | * @num: number of consumers in table |
| 826 | */ |
| 827 | void pwm_add_table(struct pwm_lookup *table, size_t num) |
| 828 | { |
| 829 | mutex_lock(&pwm_lookup_lock); |
| 830 | |
| 831 | while (num--) { |
| 832 | list_add_tail(&table->list, &pwm_lookup_list); |
| 833 | table++; |
| 834 | } |
| 835 | |
| 836 | mutex_unlock(&pwm_lookup_lock); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * pwm_remove_table() - unregister PWM device consumers |
| 841 | * @table: array of consumers to unregister |
| 842 | * @num: number of consumers in table |
| 843 | */ |
| 844 | void pwm_remove_table(struct pwm_lookup *table, size_t num) |
| 845 | { |
| 846 | mutex_lock(&pwm_lookup_lock); |
| 847 | |
| 848 | while (num--) { |
| 849 | list_del(&table->list); |
| 850 | table++; |
| 851 | } |
| 852 | |
| 853 | mutex_unlock(&pwm_lookup_lock); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * pwm_get() - look up and request a PWM device |
| 858 | * @dev: device for PWM consumer |
| 859 | * @con_id: consumer name |
| 860 | * |
| 861 | * Lookup is first attempted using DT. If the device was not instantiated from |
| 862 | * a device tree, a PWM chip and a relative index is looked up via a table |
| 863 | * supplied by board setup code (see pwm_add_table()). |
| 864 | * |
| 865 | * Once a PWM chip has been found the specified PWM device will be requested |
| 866 | * and is ready to be used. |
| 867 | * |
| 868 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 869 | * error code on failure. |
| 870 | */ |
| 871 | struct pwm_device *pwm_get(struct device *dev, const char *con_id) |
| 872 | { |
| 873 | const char *dev_id = dev ? dev_name(dev) : NULL; |
| 874 | struct pwm_device *pwm; |
| 875 | struct pwm_chip *chip; |
| 876 | struct device_link *dl; |
| 877 | unsigned int best = 0; |
| 878 | struct pwm_lookup *p, *chosen = NULL; |
| 879 | unsigned int match; |
| 880 | int err; |
| 881 | |
| 882 | /* look up via DT first */ |
| 883 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) |
| 884 | return of_pwm_get(dev, dev->of_node, con_id); |
| 885 | |
| 886 | /* then lookup via ACPI */ |
| 887 | if (dev && is_acpi_node(dev->fwnode)) { |
| 888 | pwm = acpi_pwm_get(dev->fwnode); |
| 889 | if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT) |
| 890 | return pwm; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * We look up the provider in the static table typically provided by |
| 895 | * board setup code. We first try to lookup the consumer device by |
| 896 | * name. If the consumer device was passed in as NULL or if no match |
| 897 | * was found, we try to find the consumer by directly looking it up |
| 898 | * by name. |
| 899 | * |
| 900 | * If a match is found, the provider PWM chip is looked up by name |
| 901 | * and a PWM device is requested using the PWM device per-chip index. |
| 902 | * |
| 903 | * The lookup algorithm was shamelessly taken from the clock |
| 904 | * framework: |
| 905 | * |
| 906 | * We do slightly fuzzy matching here: |
| 907 | * An entry with a NULL ID is assumed to be a wildcard. |
| 908 | * If an entry has a device ID, it must match |
| 909 | * If an entry has a connection ID, it must match |
| 910 | * Then we take the most specific entry - with the following order |
| 911 | * of precedence: dev+con > dev only > con only. |
| 912 | */ |
| 913 | mutex_lock(&pwm_lookup_lock); |
| 914 | |
| 915 | list_for_each_entry(p, &pwm_lookup_list, list) { |
| 916 | match = 0; |
| 917 | |
| 918 | if (p->dev_id) { |
| 919 | if (!dev_id || strcmp(p->dev_id, dev_id)) |
| 920 | continue; |
| 921 | |
| 922 | match += 2; |
| 923 | } |
| 924 | |
| 925 | if (p->con_id) { |
| 926 | if (!con_id || strcmp(p->con_id, con_id)) |
| 927 | continue; |
| 928 | |
| 929 | match += 1; |
| 930 | } |
| 931 | |
| 932 | if (match > best) { |
| 933 | chosen = p; |
| 934 | |
| 935 | if (match != 3) |
| 936 | best = match; |
| 937 | else |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | mutex_unlock(&pwm_lookup_lock); |
| 943 | |
| 944 | if (!chosen) |
| 945 | return ERR_PTR(-ENODEV); |
| 946 | |
| 947 | chip = pwmchip_find_by_name(chosen->provider); |
| 948 | |
| 949 | /* |
| 950 | * If the lookup entry specifies a module, load the module and retry |
| 951 | * the PWM chip lookup. This can be used to work around driver load |
| 952 | * ordering issues if driver's can't be made to properly support the |
| 953 | * deferred probe mechanism. |
| 954 | */ |
| 955 | if (!chip && chosen->module) { |
| 956 | err = request_module(chosen->module); |
| 957 | if (err == 0) |
| 958 | chip = pwmchip_find_by_name(chosen->provider); |
| 959 | } |
| 960 | |
| 961 | if (!chip) |
| 962 | return ERR_PTR(-EPROBE_DEFER); |
| 963 | |
| 964 | pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); |
| 965 | if (IS_ERR(pwm)) |
| 966 | return pwm; |
| 967 | |
| 968 | dl = pwm_device_link_add(dev, pwm); |
| 969 | if (IS_ERR(dl)) { |
| 970 | pwm_free(pwm); |
| 971 | return ERR_CAST(dl); |
| 972 | } |
| 973 | |
| 974 | pwm->args.period = chosen->period; |
| 975 | pwm->args.polarity = chosen->polarity; |
| 976 | |
| 977 | return pwm; |
| 978 | } |
| 979 | EXPORT_SYMBOL_GPL(pwm_get); |
| 980 | |
| 981 | /** |
| 982 | * pwm_put() - release a PWM device |
| 983 | * @pwm: PWM device |
| 984 | */ |
| 985 | void pwm_put(struct pwm_device *pwm) |
| 986 | { |
| 987 | if (!pwm) |
| 988 | return; |
| 989 | |
| 990 | mutex_lock(&pwm_lock); |
| 991 | |
| 992 | if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) { |
| 993 | pr_warn("PWM device already freed\n"); |
| 994 | goto out; |
| 995 | } |
| 996 | |
| 997 | if (pwm->chip->ops->free) |
| 998 | pwm->chip->ops->free(pwm->chip, pwm); |
| 999 | |
| 1000 | pwm_set_chip_data(pwm, NULL); |
| 1001 | pwm->label = NULL; |
| 1002 | |
| 1003 | module_put(pwm->chip->ops->owner); |
| 1004 | out: |
| 1005 | mutex_unlock(&pwm_lock); |
| 1006 | } |
| 1007 | EXPORT_SYMBOL_GPL(pwm_put); |
| 1008 | |
| 1009 | static void devm_pwm_release(struct device *dev, void *res) |
| 1010 | { |
| 1011 | pwm_put(*(struct pwm_device **)res); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * devm_pwm_get() - resource managed pwm_get() |
| 1016 | * @dev: device for PWM consumer |
| 1017 | * @con_id: consumer name |
| 1018 | * |
| 1019 | * This function performs like pwm_get() but the acquired PWM device will |
| 1020 | * automatically be released on driver detach. |
| 1021 | * |
| 1022 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 1023 | * error code on failure. |
| 1024 | */ |
| 1025 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id) |
| 1026 | { |
| 1027 | struct pwm_device **ptr, *pwm; |
| 1028 | |
| 1029 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
| 1030 | if (!ptr) |
| 1031 | return ERR_PTR(-ENOMEM); |
| 1032 | |
| 1033 | pwm = pwm_get(dev, con_id); |
| 1034 | if (!IS_ERR(pwm)) { |
| 1035 | *ptr = pwm; |
| 1036 | devres_add(dev, ptr); |
| 1037 | } else { |
| 1038 | devres_free(ptr); |
| 1039 | } |
| 1040 | |
| 1041 | return pwm; |
| 1042 | } |
| 1043 | EXPORT_SYMBOL_GPL(devm_pwm_get); |
| 1044 | |
| 1045 | /** |
| 1046 | * devm_of_pwm_get() - resource managed of_pwm_get() |
| 1047 | * @dev: device for PWM consumer |
| 1048 | * @np: device node to get the PWM from |
| 1049 | * @con_id: consumer name |
| 1050 | * |
| 1051 | * This function performs like of_pwm_get() but the acquired PWM device will |
| 1052 | * automatically be released on driver detach. |
| 1053 | * |
| 1054 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 1055 | * error code on failure. |
| 1056 | */ |
| 1057 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
| 1058 | const char *con_id) |
| 1059 | { |
| 1060 | struct pwm_device **ptr, *pwm; |
| 1061 | |
| 1062 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
| 1063 | if (!ptr) |
| 1064 | return ERR_PTR(-ENOMEM); |
| 1065 | |
| 1066 | pwm = of_pwm_get(dev, np, con_id); |
| 1067 | if (!IS_ERR(pwm)) { |
| 1068 | *ptr = pwm; |
| 1069 | devres_add(dev, ptr); |
| 1070 | } else { |
| 1071 | devres_free(ptr); |
| 1072 | } |
| 1073 | |
| 1074 | return pwm; |
| 1075 | } |
| 1076 | EXPORT_SYMBOL_GPL(devm_of_pwm_get); |
| 1077 | |
| 1078 | /** |
| 1079 | * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node |
| 1080 | * @dev: device for PWM consumer |
| 1081 | * @fwnode: firmware node to get the PWM from |
| 1082 | * @con_id: consumer name |
| 1083 | * |
| 1084 | * Returns the PWM device parsed from the firmware node. See of_pwm_get() and |
| 1085 | * acpi_pwm_get() for a detailed description. |
| 1086 | * |
| 1087 | * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded |
| 1088 | * error code on failure. |
| 1089 | */ |
| 1090 | struct pwm_device *devm_fwnode_pwm_get(struct device *dev, |
| 1091 | struct fwnode_handle *fwnode, |
| 1092 | const char *con_id) |
| 1093 | { |
| 1094 | struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV); |
| 1095 | |
| 1096 | ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); |
| 1097 | if (!ptr) |
| 1098 | return ERR_PTR(-ENOMEM); |
| 1099 | |
| 1100 | if (is_of_node(fwnode)) |
| 1101 | pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); |
| 1102 | else if (is_acpi_node(fwnode)) |
| 1103 | pwm = acpi_pwm_get(fwnode); |
| 1104 | |
| 1105 | if (!IS_ERR(pwm)) { |
| 1106 | *ptr = pwm; |
| 1107 | devres_add(dev, ptr); |
| 1108 | } else { |
| 1109 | devres_free(ptr); |
| 1110 | } |
| 1111 | |
| 1112 | return pwm; |
| 1113 | } |
| 1114 | EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); |
| 1115 | |
| 1116 | static int devm_pwm_match(struct device *dev, void *res, void *data) |
| 1117 | { |
| 1118 | struct pwm_device **p = res; |
| 1119 | |
| 1120 | if (WARN_ON(!p || !*p)) |
| 1121 | return 0; |
| 1122 | |
| 1123 | return *p == data; |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * devm_pwm_put() - resource managed pwm_put() |
| 1128 | * @dev: device for PWM consumer |
| 1129 | * @pwm: PWM device |
| 1130 | * |
| 1131 | * Release a PWM previously allocated using devm_pwm_get(). Calling this |
| 1132 | * function is usually not needed because devm-allocated resources are |
| 1133 | * automatically released on driver detach. |
| 1134 | */ |
| 1135 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
| 1136 | { |
| 1137 | WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm)); |
| 1138 | } |
| 1139 | EXPORT_SYMBOL_GPL(devm_pwm_put); |
| 1140 | |
| 1141 | #ifdef CONFIG_DEBUG_FS |
| 1142 | static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s) |
| 1143 | { |
| 1144 | unsigned int i; |
| 1145 | |
| 1146 | for (i = 0; i < chip->npwm; i++) { |
| 1147 | struct pwm_device *pwm = &chip->pwms[i]; |
| 1148 | struct pwm_state state; |
| 1149 | |
| 1150 | pwm_get_state(pwm, &state); |
| 1151 | |
| 1152 | seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label); |
| 1153 | |
| 1154 | if (test_bit(PWMF_REQUESTED, &pwm->flags)) |
| 1155 | seq_puts(s, " requested"); |
| 1156 | |
| 1157 | if (state.enabled) |
| 1158 | seq_puts(s, " enabled"); |
| 1159 | |
| 1160 | seq_printf(s, " period: %llu ns", state.period); |
| 1161 | seq_printf(s, " duty: %llu ns", state.duty_cycle); |
| 1162 | seq_printf(s, " polarity: %s", |
| 1163 | state.polarity ? "inverse" : "normal"); |
| 1164 | |
| 1165 | seq_puts(s, "\n"); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | static void *pwm_seq_start(struct seq_file *s, loff_t *pos) |
| 1170 | { |
| 1171 | mutex_lock(&pwm_lock); |
| 1172 | s->private = ""; |
| 1173 | |
| 1174 | return seq_list_start(&pwm_chips, *pos); |
| 1175 | } |
| 1176 | |
| 1177 | static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1178 | { |
| 1179 | s->private = "\n"; |
| 1180 | |
| 1181 | return seq_list_next(v, &pwm_chips, pos); |
| 1182 | } |
| 1183 | |
| 1184 | static void pwm_seq_stop(struct seq_file *s, void *v) |
| 1185 | { |
| 1186 | mutex_unlock(&pwm_lock); |
| 1187 | } |
| 1188 | |
| 1189 | static int pwm_seq_show(struct seq_file *s, void *v) |
| 1190 | { |
| 1191 | struct pwm_chip *chip = list_entry(v, struct pwm_chip, list); |
| 1192 | |
| 1193 | seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private, |
| 1194 | chip->dev->bus ? chip->dev->bus->name : "no-bus", |
| 1195 | dev_name(chip->dev), chip->npwm, |
| 1196 | (chip->npwm != 1) ? "s" : ""); |
| 1197 | |
| 1198 | pwm_dbg_show(chip, s); |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static const struct seq_operations pwm_seq_ops = { |
| 1204 | .start = pwm_seq_start, |
| 1205 | .next = pwm_seq_next, |
| 1206 | .stop = pwm_seq_stop, |
| 1207 | .show = pwm_seq_show, |
| 1208 | }; |
| 1209 | |
| 1210 | static int pwm_seq_open(struct inode *inode, struct file *file) |
| 1211 | { |
| 1212 | return seq_open(file, &pwm_seq_ops); |
| 1213 | } |
| 1214 | |
| 1215 | static const struct file_operations pwm_debugfs_ops = { |
| 1216 | .owner = THIS_MODULE, |
| 1217 | .open = pwm_seq_open, |
| 1218 | .read = seq_read, |
| 1219 | .llseek = seq_lseek, |
| 1220 | .release = seq_release, |
| 1221 | }; |
| 1222 | |
| 1223 | static int __init pwm_debugfs_init(void) |
| 1224 | { |
| 1225 | debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL, |
| 1226 | &pwm_debugfs_ops); |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | subsys_initcall(pwm_debugfs_init); |
| 1231 | #endif /* CONFIG_DEBUG_FS */ |