Linux Audio

Check our new training course

Loading...
v6.13.7
   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#define DEFAULT_SYMBOL_NAMESPACE "PWM"
  10
  11#include <linux/acpi.h>
  12#include <linux/module.h>
  13#include <linux/idr.h>
  14#include <linux/of.h>
  15#include <linux/pwm.h>
 
  16#include <linux/list.h>
  17#include <linux/mutex.h>
  18#include <linux/err.h>
  19#include <linux/slab.h>
  20#include <linux/device.h>
  21#include <linux/debugfs.h>
  22#include <linux/seq_file.h>
  23
  24#include <dt-bindings/pwm/pwm.h>
  25
  26#define CREATE_TRACE_POINTS
  27#include <trace/events/pwm.h>
  28
  29/* protects access to pwm_chips */
 
  30static DEFINE_MUTEX(pwm_lock);
 
 
 
  31
  32static DEFINE_IDR(pwm_chips);
  33
  34static void pwmchip_lock(struct pwm_chip *chip)
  35{
  36	if (chip->atomic)
  37		spin_lock(&chip->atomic_lock);
  38	else
  39		mutex_lock(&chip->nonatomic_lock);
  40}
  41
  42static void pwmchip_unlock(struct pwm_chip *chip)
  43{
  44	if (chip->atomic)
  45		spin_unlock(&chip->atomic_lock);
  46	else
  47		mutex_unlock(&chip->nonatomic_lock);
  48}
  49
  50DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
 
  51
  52static bool pwm_wf_valid(const struct pwm_waveform *wf)
  53{
  54	/*
  55	 * For now restrict waveforms to period_length_ns <= S64_MAX to provide
  56	 * some space for future extensions. One possibility is to simplify
  57	 * representing waveforms with inverted polarity using negative values
  58	 * somehow.
  59	 */
  60	if (wf->period_length_ns > S64_MAX)
  61		return false;
  62
  63	if (wf->duty_length_ns > wf->period_length_ns)
  64		return false;
  65
  66	/*
  67	 * .duty_offset_ns is supposed to be smaller than .period_length_ns, apart
  68	 * from the corner case .duty_offset_ns == 0 && .period_length_ns == 0.
  69	 */
  70	if (wf->duty_offset_ns && wf->duty_offset_ns >= wf->period_length_ns)
  71		return false;
  72
  73	return true;
  74}
  75
  76static void pwm_wf2state(const struct pwm_waveform *wf, struct pwm_state *state)
  77{
  78	if (wf->period_length_ns) {
  79		if (wf->duty_length_ns + wf->duty_offset_ns < wf->period_length_ns)
  80			*state = (struct pwm_state){
  81				.enabled = true,
  82				.polarity = PWM_POLARITY_NORMAL,
  83				.period = wf->period_length_ns,
  84				.duty_cycle = wf->duty_length_ns,
  85			};
  86		else
  87			*state = (struct pwm_state){
  88				.enabled = true,
  89				.polarity = PWM_POLARITY_INVERSED,
  90				.period = wf->period_length_ns,
  91				.duty_cycle = wf->period_length_ns - wf->duty_length_ns,
  92			};
  93	} else {
  94		*state = (struct pwm_state){
  95			.enabled = false,
  96		};
  97	}
  98}
  99
 100static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
 101{
 102	if (state->enabled) {
 103		if (state->polarity == PWM_POLARITY_NORMAL)
 104			*wf = (struct pwm_waveform){
 105				.period_length_ns = state->period,
 106				.duty_length_ns = state->duty_cycle,
 107				.duty_offset_ns = 0,
 108			};
 109		else
 110			*wf = (struct pwm_waveform){
 111				.period_length_ns = state->period,
 112				.duty_length_ns = state->period - state->duty_cycle,
 113				.duty_offset_ns = state->duty_cycle,
 114			};
 115	} else {
 116		*wf = (struct pwm_waveform){
 117			.period_length_ns = 0,
 118		};
 119	}
 
 
 
 
 
 120}
 121
 122static int pwmwfcmp(const struct pwm_waveform *a, const struct pwm_waveform *b)
 123{
 124	if (a->period_length_ns > b->period_length_ns)
 125		return 1;
 126
 127	if (a->period_length_ns < b->period_length_ns)
 128		return -1;
 129
 130	if (a->duty_length_ns > b->duty_length_ns)
 131		return 1;
 132
 133	if (a->duty_length_ns < b->duty_length_ns)
 134		return -1;
 135
 136	if (a->duty_offset_ns > b->duty_offset_ns)
 137		return 1;
 
 
 
 138
 139	if (a->duty_offset_ns < b->duty_offset_ns)
 140		return -1;
 141
 142	return 0;
 143}
 144
 145static bool pwm_check_rounding(const struct pwm_waveform *wf,
 146			       const struct pwm_waveform *wf_rounded)
 147{
 148	if (!wf->period_length_ns)
 149		return true;
 150
 151	if (wf->period_length_ns < wf_rounded->period_length_ns)
 152		return false;
 153
 154	if (wf->duty_length_ns < wf_rounded->duty_length_ns)
 155		return false;
 156
 157	if (wf->duty_offset_ns < wf_rounded->duty_offset_ns)
 158		return false;
 
 
 
 
 
 
 
 
 159
 160	return true;
 161}
 162
 163static int __pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm,
 164				     const struct pwm_waveform *wf, void *wfhw)
 165{
 166	const struct pwm_ops *ops = chip->ops;
 167	int ret;
 168
 169	ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw);
 170	trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret);
 
 171
 172	return ret;
 173}
 
 174
 175static int __pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
 176				       const void *wfhw, struct pwm_waveform *wf)
 177{
 178	const struct pwm_ops *ops = chip->ops;
 179	int ret;
 180
 181	ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf);
 182	trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret);
 
 183
 184	return ret;
 
 
 
 
 
 
 185}
 
 186
 187static int __pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw)
 
 188{
 189	const struct pwm_ops *ops = chip->ops;
 190	int ret;
 191
 192	ret = ops->read_waveform(chip, pwm, wfhw);
 193	trace_pwm_read_waveform(pwm, wfhw, ret);
 
 194
 195	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 196}
 197
 198static int __pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw)
 199{
 200	const struct pwm_ops *ops = chip->ops;
 201	int ret;
 202
 203	ret = ops->write_waveform(chip, pwm, wfhw);
 204	trace_pwm_write_waveform(pwm, wfhw, ret);
 
 
 205
 206	return ret;
 207}
 208
 209#define WFHWSIZE 20
 
 
 
 
 210
 211/**
 212 * pwm_round_waveform_might_sleep - Query hardware capabilities
 213 * Cannot be used in atomic context.
 214 * @pwm: PWM device
 215 * @wf: waveform to round and output parameter
 216 *
 217 * Typically a given waveform cannot be implemented exactly by hardware, e.g.
 218 * because hardware only supports coarse period resolution or no duty_offset.
 219 * This function returns the actually implemented waveform if you pass wf to
 220 * pwm_set_waveform_might_sleep now.
 221 *
 222 * Note however that the world doesn't stop turning when you call it, so when
 223 * doing
 224 *
 225 * 	pwm_round_waveform_might_sleep(mypwm, &wf);
 226 * 	pwm_set_waveform_might_sleep(mypwm, &wf, true);
 227 *
 228 * the latter might fail, e.g. because an input clock changed its rate between
 229 * these two calls and the waveform determined by
 230 * pwm_round_waveform_might_sleep() cannot be implemented any more.
 231 *
 232 * Returns 0 on success, 1 if there is no valid hardware configuration matching
 233 * the input waveform under the PWM rounding rules or a negative errno.
 234 */
 235int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf)
 236{
 237	struct pwm_chip *chip = pwm->chip;
 238	const struct pwm_ops *ops = chip->ops;
 239	struct pwm_waveform wf_req = *wf;
 240	char wfhw[WFHWSIZE];
 241	int ret_tohw, ret_fromhw;
 242
 243	BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
 244
 245	if (!pwmchip_supports_waveform(chip))
 246		return -EOPNOTSUPP;
 247
 248	if (!pwm_wf_valid(wf))
 249		return -EINVAL;
 250
 251	guard(pwmchip)(chip);
 252
 253	if (!chip->operational)
 254		return -ENODEV;
 255
 256	ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, wfhw);
 257	if (ret_tohw < 0)
 258		return ret_tohw;
 259
 260	if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_tohw > 1)
 261		dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [+%llu], return value %d\n",
 262			wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw);
 263
 264	ret_fromhw = __pwm_round_waveform_fromhw(chip, pwm, wfhw, wf);
 265	if (ret_fromhw < 0)
 266		return ret_fromhw;
 
 
 
 
 
 
 
 
 267
 268	if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_fromhw > 0)
 269		dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu [+%llu], return value %d\n",
 270			wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw);
 
 
 271
 272	if (IS_ENABLED(CONFIG_PWM_DEBUG) &&
 273	    ret_tohw == 0 && !pwm_check_rounding(&wf_req, wf))
 274		dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
 275			wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns,
 276			wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns);
 277
 278	return ret_tohw;
 279}
 280EXPORT_SYMBOL_GPL(pwm_round_waveform_might_sleep);
 281
 282/**
 283 * pwm_get_waveform_might_sleep - Query hardware about current configuration
 284 * Cannot be used in atomic context.
 285 * @pwm: PWM device
 286 * @wf: output parameter
 
 
 
 287 *
 288 * Stores the current configuration of the PWM in @wf. Note this is the
 289 * equivalent of pwm_get_state_hw() (and not pwm_get_state()) for pwm_waveform.
 290 */
 291int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf)
 
 292{
 293	struct pwm_chip *chip = pwm->chip;
 294	const struct pwm_ops *ops = chip->ops;
 295	char wfhw[WFHWSIZE];
 296	int err;
 297
 298	BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
 
 299
 300	if (!pwmchip_supports_waveform(chip) || !ops->read_waveform)
 301		return -EOPNOTSUPP;
 302
 303	guard(pwmchip)(chip);
 304
 305	if (!chip->operational)
 306		return -ENODEV;
 
 307
 308	err = __pwm_read_waveform(chip, pwm, &wfhw);
 309	if (err)
 310		return err;
 311
 312	return __pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf);
 313}
 314EXPORT_SYMBOL_GPL(pwm_get_waveform_might_sleep);
 315
 316/* Called with the pwmchip lock held */
 317static int __pwm_set_waveform(struct pwm_device *pwm,
 318			      const struct pwm_waveform *wf,
 319			      bool exact)
 320{
 321	struct pwm_chip *chip = pwm->chip;
 322	const struct pwm_ops *ops = chip->ops;
 323	char wfhw[WFHWSIZE];
 324	struct pwm_waveform wf_rounded;
 325	int err;
 326
 327	BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
 328
 329	if (!pwmchip_supports_waveform(chip))
 330		return -EOPNOTSUPP;
 331
 332	if (!pwm_wf_valid(wf))
 333		return -EINVAL;
 
 
 334
 335	err = __pwm_round_waveform_tohw(chip, pwm, wf, &wfhw);
 336	if (err)
 337		return err;
 338
 339	if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length_ns) {
 340		err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
 341		if (err)
 342			return err;
 343
 344		if (IS_ENABLED(CONFIG_PWM_DEBUG) && !pwm_check_rounding(wf, &wf_rounded))
 345			dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
 346				wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
 347				wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
 348
 349		if (exact && pwmwfcmp(wf, &wf_rounded)) {
 350			dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n",
 351				wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
 352				wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
 353
 354			return 1;
 355		}
 356	}
 357
 358	err = __pwm_write_waveform(chip, pwm, &wfhw);
 359	if (err)
 360		return err;
 361
 362	/* update .state */
 363	pwm_wf2state(wf, &pwm->state);
 364
 365	if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length_ns) {
 366		struct pwm_waveform wf_set;
 367
 368		err = __pwm_read_waveform(chip, pwm, &wfhw);
 369		if (err)
 370			/* maybe ignore? */
 371			return err;
 372
 373		err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
 374		if (err)
 375			/* maybe ignore? */
 376			return err;
 377
 378		if (pwmwfcmp(&wf_set, &wf_rounded) != 0)
 379			dev_err(&chip->dev,
 380				"Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n",
 381				wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
 382				wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns,
 383				wf_set.duty_length_ns, wf_set.period_length_ns, wf_set.duty_offset_ns);
 384	}
 385	return 0;
 
 
 
 
 386}
 
 387
 388/**
 389 * pwm_set_waveform_might_sleep - Apply a new waveform
 390 * Cannot be used in atomic context.
 391 * @pwm: PWM device
 392 * @wf: The waveform to apply
 393 * @exact: If true no rounding is allowed
 394 *
 395 * Typically a requested waveform cannot be implemented exactly, e.g. because
 396 * you requested .period_length_ns = 100 ns, but the hardware can only set
 397 * periods that are a multiple of 8.5 ns. With that hardware passing exact =
 398 * true results in pwm_set_waveform_might_sleep() failing and returning 1. If
 399 * exact = false you get a period of 93.5 ns (i.e. the biggest period not bigger
 400 * than the requested value).
 401 * Note that even with exact = true, some rounding by less than 1 is
 402 * possible/needed. In the above example requesting .period_length_ns = 94 and
 403 * exact = true, you get the hardware configured with period = 93.5 ns.
 404 */
 405int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
 406				 const struct pwm_waveform *wf, bool exact)
 407{
 408	struct pwm_chip *chip = pwm->chip;
 409	int err;
 410
 411	might_sleep();
 412
 413	guard(pwmchip)(chip);
 414
 415	if (!chip->operational)
 416		return -ENODEV;
 417
 418	if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
 419		/*
 420		 * Catch any drivers that have been marked as atomic but
 421		 * that will sleep anyway.
 422		 */
 423		non_block_start();
 424		err = __pwm_set_waveform(pwm, wf, exact);
 425		non_block_end();
 426	} else {
 427		err = __pwm_set_waveform(pwm, wf, exact);
 428	}
 429
 430	return err;
 
 
 
 
 
 
 
 
 
 431}
 432EXPORT_SYMBOL_GPL(pwm_set_waveform_might_sleep);
 433
 434static void pwm_apply_debug(struct pwm_device *pwm,
 435			    const struct pwm_state *state)
 
 
 
 
 
 
 
 
 
 436{
 437	struct pwm_state *last = &pwm->last;
 438	struct pwm_chip *chip = pwm->chip;
 439	struct pwm_state s1 = { 0 }, s2 = { 0 };
 440	int err;
 441
 442	if (!IS_ENABLED(CONFIG_PWM_DEBUG))
 443		return;
 444
 445	/* No reasonable diagnosis possible without .get_state() */
 446	if (!chip->ops->get_state)
 447		return;
 448
 449	/*
 450	 * *state was just applied. Read out the hardware state and do some
 451	 * checks.
 452	 */
 453
 454	err = chip->ops->get_state(chip, pwm, &s1);
 455	trace_pwm_get(pwm, &s1, err);
 456	if (err)
 457		/* If that failed there isn't much to debug */
 458		return;
 459
 460	/*
 461	 * The lowlevel driver either ignored .polarity (which is a bug) or as
 462	 * best effort inverted .polarity and fixed .duty_cycle respectively.
 463	 * Undo this inversion and fixup for further tests.
 464	 */
 465	if (s1.enabled && s1.polarity != state->polarity) {
 466		s2.polarity = state->polarity;
 467		s2.duty_cycle = s1.period - s1.duty_cycle;
 468		s2.period = s1.period;
 469		s2.enabled = s1.enabled;
 470	} else {
 471		s2 = s1;
 472	}
 473
 474	if (s2.polarity != state->polarity &&
 475	    state->duty_cycle < state->period)
 476		dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
 477
 478	if (state->enabled && s2.enabled &&
 479	    last->polarity == state->polarity &&
 480	    last->period > s2.period &&
 481	    last->period <= state->period)
 482		dev_warn(pwmchip_parent(chip),
 483			 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
 484			 state->period, s2.period, last->period);
 485
 486	/*
 487	 * Rounding period up is fine only if duty_cycle is 0 then, because a
 488	 * flat line doesn't have a characteristic period.
 489	 */
 490	if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle)
 491		dev_warn(pwmchip_parent(chip),
 492			 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
 493			 state->period, s2.period);
 494
 495	if (state->enabled &&
 496	    last->polarity == state->polarity &&
 497	    last->period == s2.period &&
 498	    last->duty_cycle > s2.duty_cycle &&
 499	    last->duty_cycle <= state->duty_cycle)
 500		dev_warn(pwmchip_parent(chip),
 501			 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
 502			 state->duty_cycle, state->period,
 503			 s2.duty_cycle, s2.period,
 504			 last->duty_cycle, last->period);
 505
 506	if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle)
 507		dev_warn(pwmchip_parent(chip),
 508			 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
 509			 state->duty_cycle, state->period,
 510			 s2.duty_cycle, s2.period);
 511
 512	if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
 513		dev_warn(pwmchip_parent(chip),
 514			 "requested disabled, but yielded enabled with duty > 0\n");
 515
 516	/* reapply the state that the driver reported being configured. */
 517	err = chip->ops->apply(chip, pwm, &s1);
 518	trace_pwm_apply(pwm, &s1, err);
 519	if (err) {
 520		*last = s1;
 521		dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
 522		return;
 523	}
 524
 525	*last = (struct pwm_state){ 0 };
 526	err = chip->ops->get_state(chip, pwm, last);
 527	trace_pwm_get(pwm, last, err);
 528	if (err)
 529		return;
 530
 531	/* reapplication of the current state should give an exact match */
 532	if (s1.enabled != last->enabled ||
 533	    s1.polarity != last->polarity ||
 534	    (s1.enabled && s1.period != last->period) ||
 535	    (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
 536		dev_err(pwmchip_parent(chip),
 537			".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
 538			s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
 539			last->enabled, last->polarity, last->duty_cycle,
 540			last->period);
 541	}
 542}
 
 543
 544static bool pwm_state_valid(const struct pwm_state *state)
 
 
 
 
 
 
 
 
 
 
 
 
 545{
 546	/*
 547	 * For a disabled state all other state description is irrelevant and
 548	 * and supposed to be ignored. So also ignore any strange values and
 549	 * consider the state ok.
 550	 */
 551	if (state->enabled)
 552		return true;
 553
 554	if (!state->period)
 555		return false;
 556
 557	if (state->duty_cycle > state->period)
 558		return false;
 559
 560	return true;
 
 
 
 
 
 561}
 
 562
 563/**
 564 * __pwm_apply() - atomically apply a new state to a PWM device
 
 
 
 
 
 
 
 
 
 
 
 
 565 * @pwm: PWM device
 566 * @state: new state to apply
 567 */
 568static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
 569{
 570	struct pwm_chip *chip;
 571	const struct pwm_ops *ops;
 572	int err;
 573
 574	if (!pwm || !state)
 575		return -EINVAL;
 576
 577	if (!pwm_state_valid(state)) {
 578		/*
 579		 * Allow to transition from one invalid state to another.
 580		 * This ensures that you can e.g. change the polarity while
 581		 * the period is zero. (This happens on stm32 when the hardware
 582		 * is in its poweron default state.) This greatly simplifies
 583		 * working with the sysfs API where you can only change one
 584		 * parameter at a time.
 585		 */
 586		if (!pwm_state_valid(&pwm->state)) {
 587			pwm->state = *state;
 588			return 0;
 589		}
 590
 591		return -EINVAL;
 592	}
 593
 594	chip = pwm->chip;
 595	ops = chip->ops;
 596
 597	if (state->period == pwm->state.period &&
 598	    state->duty_cycle == pwm->state.duty_cycle &&
 599	    state->polarity == pwm->state.polarity &&
 600	    state->enabled == pwm->state.enabled &&
 601	    state->usage_power == pwm->state.usage_power)
 602		return 0;
 603
 604	if (pwmchip_supports_waveform(chip)) {
 605		struct pwm_waveform wf;
 606		char wfhw[WFHWSIZE];
 607
 608		BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
 609
 610		pwm_state2wf(state, &wf);
 611
 
 
 612		/*
 613		 * The rounding is wrong here for states with inverted polarity.
 614		 * While .apply() rounds down duty_cycle (which represents the
 615		 * time from the start of the period to the inner edge),
 616		 * .round_waveform_tohw() rounds down the time the PWM is high.
 617		 * Can be fixed if the need arises, until reported otherwise
 618		 * let's assume that consumers don't care.
 619		 */
 
 
 
 620
 621		err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw);
 622		if (err) {
 623			if (err > 0)
 624				/*
 625				 * This signals an invalid request, typically
 626				 * the requested period (or duty_offset) is
 627				 * smaller than possible with the hardware.
 628				 */
 629				return -EINVAL;
 630
 631			return err;
 632		}
 633
 634		if (IS_ENABLED(CONFIG_PWM_DEBUG)) {
 635			struct pwm_waveform wf_rounded;
 636
 637			err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
 
 638			if (err)
 639				return err;
 640
 641			if (!pwm_check_rounding(&wf, &wf_rounded))
 642				dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
 643					wf.duty_length_ns, wf.period_length_ns, wf.duty_offset_ns,
 644					wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
 645		}
 646
 647		err = __pwm_write_waveform(chip, pwm, &wfhw);
 648		if (err)
 649			return err;
 650
 651		pwm->state = *state;
 
 
 652
 653	} else {
 654		err = ops->apply(chip, pwm, state);
 655		trace_pwm_apply(pwm, state, err);
 656		if (err)
 657			return err;
 658
 659		pwm->state = *state;
 
 
 
 
 
 
 
 660
 661		/*
 662		 * only do this after pwm->state was applied as some
 663		 * implementations of .get_state() depend on this
 664		 */
 665		pwm_apply_debug(pwm, state);
 666	}
 667
 668	return 0;
 669}
 
 670
 671/**
 672 * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
 673 * Cannot be used in atomic context.
 674 * @pwm: PWM device
 675 * @state: new state to apply
 676 */
 677int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
 678{
 679	int err;
 680	struct pwm_chip *chip = pwm->chip;
 681
 682	/*
 683	 * Some lowlevel driver's implementations of .apply() make use of
 684	 * mutexes, also with some drivers only returning when the new
 685	 * configuration is active calling pwm_apply_might_sleep() from atomic context
 686	 * is a bad idea. So make it explicit that calling this function might
 687	 * sleep.
 688	 */
 689	might_sleep();
 690
 691	guard(pwmchip)(chip);
 692
 693	if (!chip->operational)
 694		return -ENODEV;
 695
 696	if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
 697		/*
 698		 * Catch any drivers that have been marked as atomic but
 699		 * that will sleep anyway.
 700		 */
 701		non_block_start();
 702		err = __pwm_apply(pwm, state);
 703		non_block_end();
 704	} else {
 705		err = __pwm_apply(pwm, state);
 706	}
 707
 708	return err;
 709}
 710EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
 711
 712/**
 713 * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
 714 * Not all PWM devices support this function, check with pwm_might_sleep().
 715 * @pwm: PWM device
 716 * @state: new state to apply
 717 */
 718int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
 719{
 720	struct pwm_chip *chip = pwm->chip;
 721
 722	WARN_ONCE(!chip->atomic,
 723		  "sleeping PWM driver used in atomic context\n");
 724
 725	guard(pwmchip)(chip);
 726
 727	if (!chip->operational)
 728		return -ENODEV;
 729
 730	return __pwm_apply(pwm, state);
 731}
 732EXPORT_SYMBOL_GPL(pwm_apply_atomic);
 733
 734/**
 735 * pwm_get_state_hw() - get the current PWM state from hardware
 736 * @pwm: PWM device
 737 * @state: state to fill with the current PWM state
 738 *
 739 * Similar to pwm_get_state() but reads the current PWM state from hardware
 740 * instead of the requested state.
 741 *
 742 * Returns: 0 on success or a negative error code on failure.
 743 * Context: May sleep.
 744 */
 745int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
 
 746{
 747	struct pwm_chip *chip = pwm->chip;
 748	const struct pwm_ops *ops = chip->ops;
 749	int ret = -EOPNOTSUPP;
 750
 751	might_sleep();
 752
 753	guard(pwmchip)(chip);
 754
 755	if (!chip->operational)
 756		return -ENODEV;
 757
 758	if (pwmchip_supports_waveform(chip) && ops->read_waveform) {
 759		char wfhw[WFHWSIZE];
 760		struct pwm_waveform wf;
 761
 762		BUG_ON(WFHWSIZE < ops->sizeof_wfhw);
 763
 764		ret = __pwm_read_waveform(chip, pwm, &wfhw);
 765		if (ret)
 766			return ret;
 767
 768		ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf);
 769		if (ret)
 770			return ret;
 771
 772		pwm_wf2state(&wf, state);
 
 773
 774	} else if (ops->get_state) {
 775		ret = ops->get_state(chip, pwm, state);
 776		trace_pwm_get(pwm, state, ret);
 777	}
 778
 779	return ret;
 780}
 781EXPORT_SYMBOL_GPL(pwm_get_state_hw);
 782
 783/**
 784 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
 785 * @pwm: PWM device
 786 *
 787 * This function will adjust the PWM config to the PWM arguments provided
 788 * by the DT or PWM lookup table. This is particularly useful to adapt
 789 * the bootloader config to the Linux one.
 790 */
 791int pwm_adjust_config(struct pwm_device *pwm)
 792{
 793	struct pwm_state state;
 794	struct pwm_args pargs;
 795
 796	pwm_get_args(pwm, &pargs);
 797	pwm_get_state(pwm, &state);
 798
 799	/*
 800	 * If the current period is zero it means that either the PWM driver
 801	 * does not support initial state retrieval or the PWM has not yet
 802	 * been configured.
 803	 *
 804	 * In either case, we setup the new period and polarity, and assign a
 805	 * duty cycle of 0.
 806	 */
 807	if (!state.period) {
 808		state.duty_cycle = 0;
 809		state.period = pargs.period;
 810		state.polarity = pargs.polarity;
 811
 812		return pwm_apply_might_sleep(pwm, &state);
 813	}
 814
 815	/*
 816	 * Adjust the PWM duty cycle/period based on the period value provided
 817	 * in PWM args.
 818	 */
 819	if (pargs.period != state.period) {
 820		u64 dutycycle = (u64)state.duty_cycle * pargs.period;
 821
 822		do_div(dutycycle, state.period);
 823		state.duty_cycle = dutycycle;
 824		state.period = pargs.period;
 825	}
 826
 827	/*
 828	 * If the polarity changed, we should also change the duty cycle.
 829	 */
 830	if (pargs.polarity != state.polarity) {
 831		state.polarity = pargs.polarity;
 832		state.duty_cycle = state.period - state.duty_cycle;
 833	}
 834
 835	return pwm_apply_might_sleep(pwm, &state);
 836}
 837EXPORT_SYMBOL_GPL(pwm_adjust_config);
 838
 839/**
 840 * pwm_capture() - capture and report a PWM signal
 841 * @pwm: PWM device
 842 * @result: structure to fill with capture result
 843 * @timeout: time to wait, in milliseconds, before giving up on capture
 844 *
 845 * Returns: 0 on success or a negative error code on failure.
 846 */
 847static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
 848		       unsigned long timeout)
 849{
 850	struct pwm_chip *chip = pwm->chip;
 851	const struct pwm_ops *ops = chip->ops;
 852
 853	if (!ops->capture)
 854		return -ENOSYS;
 855
 856	/*
 857	 * Holding the pwm_lock is probably not needed. If you use pwm_capture()
 858	 * and you're interested to speed it up, please convince yourself it's
 859	 * really not needed, test and then suggest a patch on the mailing list.
 860	 */
 861	guard(mutex)(&pwm_lock);
 862
 863	guard(pwmchip)(chip);
 864
 865	if (!chip->operational)
 866		return -ENODEV;
 867
 868	return ops->capture(chip, pwm, result, timeout);
 869}
 870
 871static struct pwm_chip *pwmchip_find_by_name(const char *name)
 872{
 873	struct pwm_chip *chip;
 874	unsigned long id, tmp;
 875
 876	if (!name)
 877		return NULL;
 878
 879	guard(mutex)(&pwm_lock);
 880
 881	idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
 882		if (device_match_name(pwmchip_parent(chip), name))
 
 883			return chip;
 884	}
 885
 886	return NULL;
 887}
 888
 889static int pwm_device_request(struct pwm_device *pwm, const char *label)
 890{
 891	int err;
 892	struct pwm_chip *chip = pwm->chip;
 893	const struct pwm_ops *ops = chip->ops;
 894
 895	if (test_bit(PWMF_REQUESTED, &pwm->flags))
 896		return -EBUSY;
 897
 898	/*
 899	 * This function is called while holding pwm_lock. As .operational only
 900	 * changes while holding this lock, checking it here without holding the
 901	 * chip lock is fine.
 902	 */
 903	if (!chip->operational)
 904		return -ENODEV;
 905
 906	if (!try_module_get(chip->owner))
 907		return -ENODEV;
 908
 909	if (!get_device(&chip->dev)) {
 910		err = -ENODEV;
 911		goto err_get_device;
 912	}
 913
 914	if (ops->request) {
 915		err = ops->request(chip, pwm);
 916		if (err) {
 917			put_device(&chip->dev);
 918err_get_device:
 919			module_put(chip->owner);
 920			return err;
 921		}
 922	}
 923
 924	if (ops->read_waveform || ops->get_state) {
 925		/*
 926		 * Zero-initialize state because most drivers are unaware of
 927		 * .usage_power. The other members of state are supposed to be
 928		 * set by lowlevel drivers. We still initialize the whole
 929		 * structure for simplicity even though this might paper over
 930		 * faulty implementations of .get_state().
 931		 */
 932		struct pwm_state state = { 0, };
 933
 934		err = pwm_get_state_hw(pwm, &state);
 935		if (!err)
 936			pwm->state = state;
 937
 938		if (IS_ENABLED(CONFIG_PWM_DEBUG))
 939			pwm->last = pwm->state;
 940	}
 941
 942	set_bit(PWMF_REQUESTED, &pwm->flags);
 943	pwm->label = label;
 944
 945	return 0;
 946}
 947
 948/**
 949 * pwm_request_from_chip() - request a PWM device relative to a PWM chip
 950 * @chip: PWM chip
 951 * @index: per-chip index of the PWM to request
 952 * @label: a literal description string of this PWM
 953 *
 954 * Returns: A pointer to the PWM device at the given index of the given PWM
 955 * chip. A negative error code is returned if the index is not valid for the
 956 * specified PWM chip or if the PWM device cannot be requested.
 957 */
 958static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
 959						unsigned int index,
 960						const char *label)
 961{
 962	struct pwm_device *pwm;
 963	int err;
 964
 965	if (!chip || index >= chip->npwm)
 966		return ERR_PTR(-EINVAL);
 967
 968	guard(mutex)(&pwm_lock);
 969
 970	pwm = &chip->pwms[index];
 971
 972	err = pwm_device_request(pwm, label);
 973	if (err < 0)
 974		return ERR_PTR(err);
 975
 976	return pwm;
 977}
 978
 979struct pwm_device *
 980of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
 981{
 982	struct pwm_device *pwm;
 983
 984	/* period in the second cell and flags in the third cell are optional */
 985	if (args->args_count < 1)
 986		return ERR_PTR(-EINVAL);
 987
 988	pwm = pwm_request_from_chip(chip, args->args[0], NULL);
 989	if (IS_ERR(pwm))
 990		return pwm;
 991
 992	if (args->args_count > 1)
 993		pwm->args.period = args->args[1];
 994
 995	pwm->args.polarity = PWM_POLARITY_NORMAL;
 996	if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
 997		pwm->args.polarity = PWM_POLARITY_INVERSED;
 998
 999	return pwm;
1000}
1001EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
1002
1003struct pwm_device *
1004of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
1005{
1006	struct pwm_device *pwm;
1007
1008	pwm = pwm_request_from_chip(chip, 0, NULL);
1009	if (IS_ERR(pwm))
1010		return pwm;
1011
1012	if (args->args_count > 0)
1013		pwm->args.period = args->args[0];
1014
1015	pwm->args.polarity = PWM_POLARITY_NORMAL;
1016	if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
1017		pwm->args.polarity = PWM_POLARITY_INVERSED;
1018
1019	return pwm;
1020}
1021EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
1022
1023struct pwm_export {
1024	struct device pwm_dev;
1025	struct pwm_device *pwm;
1026	struct mutex lock;
1027	struct pwm_state suspend;
1028};
1029
1030static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
1031{
1032	return container_of(pwmchip_dev, struct pwm_chip, dev);
1033}
1034
1035static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
1036{
1037	return container_of(pwm_dev, struct pwm_export, pwm_dev);
1038}
1039
1040static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
1041{
1042	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1043
1044	return export->pwm;
1045}
1046
1047static ssize_t period_show(struct device *pwm_dev,
1048			   struct device_attribute *attr,
1049			   char *buf)
1050{
1051	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1052	struct pwm_state state;
1053
1054	pwm_get_state(pwm, &state);
1055
1056	return sysfs_emit(buf, "%llu\n", state.period);
1057}
1058
1059static ssize_t period_store(struct device *pwm_dev,
1060			    struct device_attribute *attr,
1061			    const char *buf, size_t size)
1062{
1063	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1064	struct pwm_device *pwm = export->pwm;
1065	struct pwm_state state;
1066	u64 val;
1067	int ret;
1068
1069	ret = kstrtou64(buf, 0, &val);
1070	if (ret)
1071		return ret;
1072
1073	guard(mutex)(&export->lock);
1074
1075	pwm_get_state(pwm, &state);
1076	state.period = val;
1077	ret = pwm_apply_might_sleep(pwm, &state);
1078
1079	return ret ? : size;
1080}
1081
1082static ssize_t duty_cycle_show(struct device *pwm_dev,
1083			       struct device_attribute *attr,
1084			       char *buf)
1085{
1086	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1087	struct pwm_state state;
1088
1089	pwm_get_state(pwm, &state);
1090
1091	return sysfs_emit(buf, "%llu\n", state.duty_cycle);
1092}
1093
1094static ssize_t duty_cycle_store(struct device *pwm_dev,
1095				struct device_attribute *attr,
1096				const char *buf, size_t size)
1097{
1098	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1099	struct pwm_device *pwm = export->pwm;
1100	struct pwm_state state;
1101	u64 val;
1102	int ret;
1103
1104	ret = kstrtou64(buf, 0, &val);
1105	if (ret)
1106		return ret;
1107
1108	guard(mutex)(&export->lock);
1109
1110	pwm_get_state(pwm, &state);
1111	state.duty_cycle = val;
1112	ret = pwm_apply_might_sleep(pwm, &state);
1113
1114	return ret ? : size;
1115}
1116
1117static ssize_t enable_show(struct device *pwm_dev,
1118			   struct device_attribute *attr,
1119			   char *buf)
1120{
1121	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1122	struct pwm_state state;
1123
1124	pwm_get_state(pwm, &state);
1125
1126	return sysfs_emit(buf, "%d\n", state.enabled);
1127}
1128
1129static ssize_t enable_store(struct device *pwm_dev,
1130			    struct device_attribute *attr,
1131			    const char *buf, size_t size)
1132{
1133	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1134	struct pwm_device *pwm = export->pwm;
1135	struct pwm_state state;
1136	int val, ret;
1137
1138	ret = kstrtoint(buf, 0, &val);
1139	if (ret)
1140		return ret;
1141
1142	guard(mutex)(&export->lock);
1143
1144	pwm_get_state(pwm, &state);
1145
1146	switch (val) {
1147	case 0:
1148		state.enabled = false;
1149		break;
1150	case 1:
1151		state.enabled = true;
1152		break;
1153	default:
1154		return -EINVAL;
1155	}
1156
1157	ret = pwm_apply_might_sleep(pwm, &state);
1158
1159	return ret ? : size;
1160}
1161
1162static ssize_t polarity_show(struct device *pwm_dev,
1163			     struct device_attribute *attr,
1164			     char *buf)
1165{
1166	const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1167	const char *polarity = "unknown";
1168	struct pwm_state state;
1169
1170	pwm_get_state(pwm, &state);
1171
1172	switch (state.polarity) {
1173	case PWM_POLARITY_NORMAL:
1174		polarity = "normal";
1175		break;
1176
1177	case PWM_POLARITY_INVERSED:
1178		polarity = "inversed";
1179		break;
1180	}
1181
1182	return sysfs_emit(buf, "%s\n", polarity);
1183}
1184
1185static ssize_t polarity_store(struct device *pwm_dev,
1186			      struct device_attribute *attr,
1187			      const char *buf, size_t size)
1188{
1189	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1190	struct pwm_device *pwm = export->pwm;
1191	enum pwm_polarity polarity;
1192	struct pwm_state state;
1193	int ret;
1194
1195	if (sysfs_streq(buf, "normal"))
1196		polarity = PWM_POLARITY_NORMAL;
1197	else if (sysfs_streq(buf, "inversed"))
1198		polarity = PWM_POLARITY_INVERSED;
1199	else
1200		return -EINVAL;
1201
1202	guard(mutex)(&export->lock);
1203
1204	pwm_get_state(pwm, &state);
1205	state.polarity = polarity;
1206	ret = pwm_apply_might_sleep(pwm, &state);
1207
1208	return ret ? : size;
1209}
1210
1211static ssize_t capture_show(struct device *pwm_dev,
1212			    struct device_attribute *attr,
1213			    char *buf)
1214{
1215	struct pwm_device *pwm = pwm_from_dev(pwm_dev);
1216	struct pwm_capture result;
1217	int ret;
1218
1219	ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
1220	if (ret)
1221		return ret;
1222
1223	return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
1224}
1225
1226static DEVICE_ATTR_RW(period);
1227static DEVICE_ATTR_RW(duty_cycle);
1228static DEVICE_ATTR_RW(enable);
1229static DEVICE_ATTR_RW(polarity);
1230static DEVICE_ATTR_RO(capture);
1231
1232static struct attribute *pwm_attrs[] = {
1233	&dev_attr_period.attr,
1234	&dev_attr_duty_cycle.attr,
1235	&dev_attr_enable.attr,
1236	&dev_attr_polarity.attr,
1237	&dev_attr_capture.attr,
1238	NULL
1239};
1240ATTRIBUTE_GROUPS(pwm);
1241
1242static void pwm_export_release(struct device *pwm_dev)
1243{
1244	struct pwm_export *export = pwmexport_from_dev(pwm_dev);
1245
1246	kfree(export);
1247}
1248
1249static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
1250{
1251	struct pwm_export *export;
1252	char *pwm_prop[2];
1253	int ret;
1254
1255	if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
1256		return -EBUSY;
1257
1258	export = kzalloc(sizeof(*export), GFP_KERNEL);
1259	if (!export) {
1260		clear_bit(PWMF_EXPORTED, &pwm->flags);
1261		return -ENOMEM;
1262	}
1263
1264	export->pwm = pwm;
1265	mutex_init(&export->lock);
1266
1267	export->pwm_dev.release = pwm_export_release;
1268	export->pwm_dev.parent = pwmchip_dev;
1269	export->pwm_dev.devt = MKDEV(0, 0);
1270	export->pwm_dev.groups = pwm_groups;
1271	dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
1272
1273	ret = device_register(&export->pwm_dev);
1274	if (ret) {
1275		clear_bit(PWMF_EXPORTED, &pwm->flags);
1276		put_device(&export->pwm_dev);
1277		export = NULL;
1278		return ret;
1279	}
1280	pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
1281	pwm_prop[1] = NULL;
1282	kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
1283	kfree(pwm_prop[0]);
1284
1285	return 0;
1286}
1287
1288static int pwm_unexport_match(struct device *pwm_dev, void *data)
1289{
1290	return pwm_from_dev(pwm_dev) == data;
1291}
1292
1293static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
1294{
1295	struct device *pwm_dev;
1296	char *pwm_prop[2];
1297
1298	if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
1299		return -ENODEV;
1300
1301	pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
1302	if (!pwm_dev)
1303		return -ENODEV;
1304
1305	pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
1306	pwm_prop[1] = NULL;
1307	kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
1308	kfree(pwm_prop[0]);
1309
1310	/* for device_find_child() */
1311	put_device(pwm_dev);
1312	device_unregister(pwm_dev);
1313	pwm_put(pwm);
1314
1315	return 0;
1316}
1317
1318static ssize_t export_store(struct device *pwmchip_dev,
1319			    struct device_attribute *attr,
1320			    const char *buf, size_t len)
1321{
1322	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1323	struct pwm_device *pwm;
1324	unsigned int hwpwm;
1325	int ret;
1326
1327	ret = kstrtouint(buf, 0, &hwpwm);
1328	if (ret < 0)
1329		return ret;
1330
1331	if (hwpwm >= chip->npwm)
1332		return -ENODEV;
1333
1334	pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
1335	if (IS_ERR(pwm))
1336		return PTR_ERR(pwm);
1337
1338	ret = pwm_export_child(pwmchip_dev, pwm);
1339	if (ret < 0)
1340		pwm_put(pwm);
1341
1342	return ret ? : len;
1343}
1344static DEVICE_ATTR_WO(export);
1345
1346static ssize_t unexport_store(struct device *pwmchip_dev,
1347			      struct device_attribute *attr,
1348			      const char *buf, size_t len)
1349{
1350	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1351	unsigned int hwpwm;
1352	int ret;
1353
1354	ret = kstrtouint(buf, 0, &hwpwm);
1355	if (ret < 0)
1356		return ret;
1357
1358	if (hwpwm >= chip->npwm)
1359		return -ENODEV;
1360
1361	ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
1362
1363	return ret ? : len;
1364}
1365static DEVICE_ATTR_WO(unexport);
1366
1367static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
1368			 char *buf)
1369{
1370	const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1371
1372	return sysfs_emit(buf, "%u\n", chip->npwm);
1373}
1374static DEVICE_ATTR_RO(npwm);
1375
1376static struct attribute *pwm_chip_attrs[] = {
1377	&dev_attr_export.attr,
1378	&dev_attr_unexport.attr,
1379	&dev_attr_npwm.attr,
1380	NULL,
1381};
1382ATTRIBUTE_GROUPS(pwm_chip);
1383
1384/* takes export->lock on success */
1385static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
1386					      struct pwm_device *pwm,
1387					      struct pwm_state *state)
1388{
1389	struct device *pwm_dev;
1390	struct pwm_export *export;
1391
1392	if (!test_bit(PWMF_EXPORTED, &pwm->flags))
1393		return NULL;
1394
1395	pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
1396	if (!pwm_dev)
1397		return NULL;
1398
1399	export = pwmexport_from_dev(pwm_dev);
1400	put_device(pwm_dev);	/* for device_find_child() */
1401
1402	mutex_lock(&export->lock);
1403	pwm_get_state(pwm, state);
1404
1405	return export;
1406}
1407
1408static int pwm_class_apply_state(struct pwm_export *export,
1409				 struct pwm_device *pwm,
1410				 struct pwm_state *state)
1411{
1412	int ret = pwm_apply_might_sleep(pwm, state);
1413
1414	/* release lock taken in pwm_class_get_state */
1415	mutex_unlock(&export->lock);
1416
1417	return ret;
1418}
1419
1420static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
1421{
1422	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1423	unsigned int i;
1424	int ret = 0;
1425
1426	for (i = 0; i < npwm; i++) {
1427		struct pwm_device *pwm = &chip->pwms[i];
1428		struct pwm_state state;
1429		struct pwm_export *export;
1430
1431		export = pwm_class_get_state(pwmchip_dev, pwm, &state);
1432		if (!export)
1433			continue;
1434
1435		/* If pwmchip was not enabled before suspend, do nothing. */
1436		if (!export->suspend.enabled) {
1437			/* release lock taken in pwm_class_get_state */
1438			mutex_unlock(&export->lock);
1439			continue;
1440		}
1441
1442		state.enabled = export->suspend.enabled;
1443		ret = pwm_class_apply_state(export, pwm, &state);
1444		if (ret < 0)
1445			break;
1446	}
1447
1448	return ret;
1449}
1450
1451static int pwm_class_suspend(struct device *pwmchip_dev)
1452{
1453	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1454	unsigned int i;
1455	int ret = 0;
1456
1457	for (i = 0; i < chip->npwm; i++) {
1458		struct pwm_device *pwm = &chip->pwms[i];
1459		struct pwm_state state;
1460		struct pwm_export *export;
1461
1462		export = pwm_class_get_state(pwmchip_dev, pwm, &state);
1463		if (!export)
1464			continue;
1465
1466		/*
1467		 * If pwmchip was not enabled before suspend, save
1468		 * state for resume time and do nothing else.
1469		 */
1470		export->suspend = state;
1471		if (!state.enabled) {
1472			/* release lock taken in pwm_class_get_state */
1473			mutex_unlock(&export->lock);
1474			continue;
1475		}
1476
1477		state.enabled = false;
1478		ret = pwm_class_apply_state(export, pwm, &state);
1479		if (ret < 0) {
1480			/*
1481			 * roll back the PWM devices that were disabled by
1482			 * this suspend function.
1483			 */
1484			pwm_class_resume_npwm(pwmchip_dev, i);
1485			break;
1486		}
1487	}
1488
1489	return ret;
1490}
1491
1492static int pwm_class_resume(struct device *pwmchip_dev)
1493{
1494	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1495
1496	return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
1497}
1498
1499static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
1500
1501static struct class pwm_class = {
1502	.name = "pwm",
1503	.dev_groups = pwm_chip_groups,
1504	.pm = pm_sleep_ptr(&pwm_class_pm_ops),
1505};
1506
1507static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
1508{
1509	unsigned int i;
1510
1511	for (i = 0; i < chip->npwm; i++) {
1512		struct pwm_device *pwm = &chip->pwms[i];
1513
1514		if (test_bit(PWMF_EXPORTED, &pwm->flags))
1515			pwm_unexport_child(&chip->dev, pwm);
1516	}
1517}
1518
1519#define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
1520
1521static void *pwmchip_priv(struct pwm_chip *chip)
1522{
1523	return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
1524}
1525
1526/* This is the counterpart to pwmchip_alloc() */
1527void pwmchip_put(struct pwm_chip *chip)
1528{
1529	put_device(&chip->dev);
1530}
1531EXPORT_SYMBOL_GPL(pwmchip_put);
1532
1533static void pwmchip_release(struct device *pwmchip_dev)
1534{
1535	struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
1536
1537	kfree(chip);
1538}
1539
1540struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1541{
1542	struct pwm_chip *chip;
1543	struct device *pwmchip_dev;
1544	size_t alloc_size;
1545	unsigned int i;
1546
1547	alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
1548			      sizeof_priv);
1549
1550	chip = kzalloc(alloc_size, GFP_KERNEL);
1551	if (!chip)
1552		return ERR_PTR(-ENOMEM);
1553
1554	chip->npwm = npwm;
1555	chip->uses_pwmchip_alloc = true;
1556	chip->operational = false;
1557
1558	pwmchip_dev = &chip->dev;
1559	device_initialize(pwmchip_dev);
1560	pwmchip_dev->class = &pwm_class;
1561	pwmchip_dev->parent = parent;
1562	pwmchip_dev->release = pwmchip_release;
1563
1564	pwmchip_set_drvdata(chip, pwmchip_priv(chip));
1565
1566	for (i = 0; i < chip->npwm; i++) {
1567		struct pwm_device *pwm = &chip->pwms[i];
1568		pwm->chip = chip;
1569		pwm->hwpwm = i;
1570	}
1571
1572	return chip;
1573}
1574EXPORT_SYMBOL_GPL(pwmchip_alloc);
1575
1576static void devm_pwmchip_put(void *data)
1577{
1578	struct pwm_chip *chip = data;
1579
1580	pwmchip_put(chip);
1581}
1582
1583struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
1584{
1585	struct pwm_chip *chip;
1586	int ret;
1587
1588	chip = pwmchip_alloc(parent, npwm, sizeof_priv);
1589	if (IS_ERR(chip))
1590		return chip;
1591
1592	ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
1593	if (ret)
1594		return ERR_PTR(ret);
1595
1596	return chip;
1597}
1598EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
1599
1600static void of_pwmchip_add(struct pwm_chip *chip)
1601{
1602	if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
1603		return;
1604
1605	if (!chip->of_xlate)
1606		chip->of_xlate = of_pwm_xlate_with_flags;
1607
1608	of_node_get(pwmchip_parent(chip)->of_node);
1609}
1610
1611static void of_pwmchip_remove(struct pwm_chip *chip)
1612{
1613	if (pwmchip_parent(chip))
1614		of_node_put(pwmchip_parent(chip)->of_node);
1615}
1616
1617static bool pwm_ops_check(const struct pwm_chip *chip)
1618{
1619	const struct pwm_ops *ops = chip->ops;
1620
1621	if (ops->write_waveform) {
1622		if (!ops->round_waveform_tohw ||
1623		    !ops->round_waveform_fromhw ||
1624		    !ops->write_waveform)
1625			return false;
1626
1627		if (WFHWSIZE < ops->sizeof_wfhw) {
1628			dev_warn(pwmchip_parent(chip), "WFHWSIZE < %zu\n", ops->sizeof_wfhw);
1629			return false;
1630		}
1631	} else {
1632		if (!ops->apply)
1633			return false;
1634
1635		if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
1636			dev_warn(pwmchip_parent(chip),
1637				 "Please implement the .get_state() callback\n");
1638	}
1639
1640	return true;
1641}
1642
1643static struct device_link *pwm_device_link_add(struct device *dev,
1644					       struct pwm_device *pwm)
1645{
1646	struct device_link *dl;
1647
1648	if (!dev) {
1649		/*
1650		 * No device for the PWM consumer has been provided. It may
1651		 * impact the PM sequence ordering: the PWM supplier may get
1652		 * suspended before the consumer.
1653		 */
1654		dev_warn(pwmchip_parent(pwm->chip),
1655			 "No consumer device specified to create a link to\n");
1656		return NULL;
1657	}
1658
1659	dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
1660	if (!dl) {
1661		dev_err(dev, "failed to create device link to %s\n",
1662			dev_name(pwmchip_parent(pwm->chip)));
1663		return ERR_PTR(-EINVAL);
1664	}
1665
1666	return dl;
1667}
1668
1669static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
1670{
1671	struct pwm_chip *chip;
1672	unsigned long id, tmp;
1673
1674	guard(mutex)(&pwm_lock);
1675
1676	idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
1677		if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
1678			return chip;
1679
1680	return ERR_PTR(-EPROBE_DEFER);
1681}
1682
1683/**
1684 * of_pwm_get() - request a PWM via the PWM framework
1685 * @dev: device for PWM consumer
1686 * @np: device node to get the PWM from
1687 * @con_id: consumer name
1688 *
1689 * Returns the PWM device parsed from the phandle and index specified in the
1690 * "pwms" property of a device tree node or a negative error-code on failure.
1691 * Values parsed from the device tree are stored in the returned PWM device
1692 * object.
1693 *
1694 * If con_id is NULL, the first PWM device listed in the "pwms" property will
1695 * be requested. Otherwise the "pwm-names" property is used to do a reverse
1696 * lookup of the PWM index. This also means that the "pwm-names" property
1697 * becomes mandatory for devices that look up the PWM device via the con_id
1698 * parameter.
1699 *
1700 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1701 * error code on failure.
1702 */
1703static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
1704				     const char *con_id)
1705{
1706	struct pwm_device *pwm = NULL;
1707	struct of_phandle_args args;
1708	struct device_link *dl;
1709	struct pwm_chip *chip;
1710	int index = 0;
1711	int err;
1712
1713	if (con_id) {
1714		index = of_property_match_string(np, "pwm-names", con_id);
1715		if (index < 0)
1716			return ERR_PTR(index);
1717	}
1718
1719	err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
1720					 &args);
1721	if (err) {
1722		pr_err("%s(): can't parse \"pwms\" property\n", __func__);
1723		return ERR_PTR(err);
1724	}
1725
1726	chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
1727	if (IS_ERR(chip)) {
1728		if (PTR_ERR(chip) != -EPROBE_DEFER)
1729			pr_err("%s(): PWM chip not found\n", __func__);
1730
1731		pwm = ERR_CAST(chip);
1732		goto put;
1733	}
1734
1735	pwm = chip->of_xlate(chip, &args);
1736	if (IS_ERR(pwm))
1737		goto put;
1738
1739	dl = pwm_device_link_add(dev, pwm);
1740	if (IS_ERR(dl)) {
1741		/* of_xlate ended up calling pwm_request_from_chip() */
1742		pwm_put(pwm);
1743		pwm = ERR_CAST(dl);
1744		goto put;
1745	}
1746
1747	/*
1748	 * If a consumer name was not given, try to look it up from the
1749	 * "pwm-names" property if it exists. Otherwise use the name of
1750	 * the user device node.
1751	 */
1752	if (!con_id) {
1753		err = of_property_read_string_index(np, "pwm-names", index,
1754						    &con_id);
1755		if (err < 0)
1756			con_id = np->name;
1757	}
1758
1759	pwm->label = con_id;
1760
1761put:
1762	of_node_put(args.np);
1763
1764	return pwm;
1765}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1766
1767/**
1768 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
1769 * @fwnode: firmware node to get the "pwms" property from
1770 *
1771 * Returns the PWM device parsed from the fwnode and index specified in the
1772 * "pwms" property or a negative error-code on failure.
1773 * Values parsed from the device tree are stored in the returned PWM device
1774 * object.
1775 *
1776 * This is analogous to of_pwm_get() except con_id is not yet supported.
1777 * ACPI entries must look like
1778 * Package () {"pwms", Package ()
1779 *     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
1780 *
1781 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1782 * error code on failure.
1783 */
1784static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
1785{
1786	struct pwm_device *pwm;
 
1787	struct fwnode_reference_args args;
 
1788	struct pwm_chip *chip;
1789	int ret;
1790
1791	memset(&args, 0, sizeof(args));
1792
1793	ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
1794	if (ret < 0)
1795		return ERR_PTR(ret);
1796
 
 
 
 
1797	if (args.nargs < 2)
1798		return ERR_PTR(-EPROTO);
1799
1800	chip = fwnode_to_pwmchip(args.fwnode);
1801	if (IS_ERR(chip))
1802		return ERR_CAST(chip);
1803
1804	pwm = pwm_request_from_chip(chip, args.args[0], NULL);
1805	if (IS_ERR(pwm))
1806		return pwm;
1807
1808	pwm->args.period = args.args[1];
1809	pwm->args.polarity = PWM_POLARITY_NORMAL;
1810
1811	if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
1812		pwm->args.polarity = PWM_POLARITY_INVERSED;
 
1813
1814	return pwm;
1815}
1816
1817static DEFINE_MUTEX(pwm_lookup_lock);
1818static LIST_HEAD(pwm_lookup_list);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1819
1820/**
1821 * pwm_get() - look up and request a PWM device
1822 * @dev: device for PWM consumer
1823 * @con_id: consumer name
1824 *
1825 * Lookup is first attempted using DT. If the device was not instantiated from
1826 * a device tree, a PWM chip and a relative index is looked up via a table
1827 * supplied by board setup code (see pwm_add_table()).
1828 *
1829 * Once a PWM chip has been found the specified PWM device will be requested
1830 * and is ready to be used.
1831 *
1832 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1833 * error code on failure.
1834 */
1835struct pwm_device *pwm_get(struct device *dev, const char *con_id)
1836{
1837	const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
1838	const char *dev_id = dev ? dev_name(dev) : NULL;
1839	struct pwm_device *pwm;
1840	struct pwm_chip *chip;
1841	struct device_link *dl;
1842	unsigned int best = 0;
1843	struct pwm_lookup *p, *chosen = NULL;
1844	unsigned int match;
1845	int err;
1846
1847	/* look up via DT first */
1848	if (is_of_node(fwnode))
1849		return of_pwm_get(dev, to_of_node(fwnode), con_id);
1850
1851	/* then lookup via ACPI */
1852	if (is_acpi_node(fwnode)) {
1853		pwm = acpi_pwm_get(fwnode);
1854		if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
1855			return pwm;
1856	}
1857
1858	/*
1859	 * We look up the provider in the static table typically provided by
1860	 * board setup code. We first try to lookup the consumer device by
1861	 * name. If the consumer device was passed in as NULL or if no match
1862	 * was found, we try to find the consumer by directly looking it up
1863	 * by name.
1864	 *
1865	 * If a match is found, the provider PWM chip is looked up by name
1866	 * and a PWM device is requested using the PWM device per-chip index.
1867	 *
1868	 * The lookup algorithm was shamelessly taken from the clock
1869	 * framework:
1870	 *
1871	 * We do slightly fuzzy matching here:
1872	 *  An entry with a NULL ID is assumed to be a wildcard.
1873	 *  If an entry has a device ID, it must match
1874	 *  If an entry has a connection ID, it must match
1875	 * Then we take the most specific entry - with the following order
1876	 * of precedence: dev+con > dev only > con only.
1877	 */
1878	scoped_guard(mutex, &pwm_lookup_lock)
1879		list_for_each_entry(p, &pwm_lookup_list, list) {
1880			match = 0;
1881
1882			if (p->dev_id) {
1883				if (!dev_id || strcmp(p->dev_id, dev_id))
1884					continue;
1885
1886				match += 2;
1887			}
1888
1889			if (p->con_id) {
1890				if (!con_id || strcmp(p->con_id, con_id))
1891					continue;
1892
1893				match += 1;
1894			}
1895
1896			if (match > best) {
1897				chosen = p;
 
1898
1899				if (match != 3)
1900					best = match;
1901				else
1902					break;
1903			}
1904		}
1905
 
 
 
 
 
 
 
 
 
 
 
 
1906	if (!chosen)
1907		return ERR_PTR(-ENODEV);
1908
1909	chip = pwmchip_find_by_name(chosen->provider);
1910
1911	/*
1912	 * If the lookup entry specifies a module, load the module and retry
1913	 * the PWM chip lookup. This can be used to work around driver load
1914	 * ordering issues if driver's can't be made to properly support the
1915	 * deferred probe mechanism.
1916	 */
1917	if (!chip && chosen->module) {
1918		err = request_module(chosen->module);
1919		if (err == 0)
1920			chip = pwmchip_find_by_name(chosen->provider);
1921	}
1922
1923	if (!chip)
1924		return ERR_PTR(-EPROBE_DEFER);
1925
1926	pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
1927	if (IS_ERR(pwm))
1928		return pwm;
1929
1930	dl = pwm_device_link_add(dev, pwm);
1931	if (IS_ERR(dl)) {
1932		pwm_put(pwm);
1933		return ERR_CAST(dl);
1934	}
1935
1936	pwm->args.period = chosen->period;
1937	pwm->args.polarity = chosen->polarity;
1938
1939	return pwm;
1940}
1941EXPORT_SYMBOL_GPL(pwm_get);
1942
1943/**
1944 * pwm_put() - release a PWM device
1945 * @pwm: PWM device
1946 */
1947void pwm_put(struct pwm_device *pwm)
1948{
1949	struct pwm_chip *chip;
1950
1951	if (!pwm)
1952		return;
1953
1954	chip = pwm->chip;
1955
1956	guard(mutex)(&pwm_lock);
1957
1958	/*
1959	 * Trigger a warning if a consumer called pwm_put() twice.
1960	 * If the chip isn't operational, PWMF_REQUESTED was already cleared in
1961	 * pwmchip_remove(). So don't warn in this case.
1962	 */
1963	if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
1964		pr_warn("PWM device already freed\n");
1965		return;
1966	}
1967
1968	if (chip->operational && chip->ops->free)
1969		pwm->chip->ops->free(pwm->chip, pwm);
1970
 
1971	pwm->label = NULL;
1972
1973	put_device(&chip->dev);
1974
1975	module_put(chip->owner);
1976}
1977EXPORT_SYMBOL_GPL(pwm_put);
1978
1979static void devm_pwm_release(void *pwm)
1980{
1981	pwm_put(pwm);
1982}
1983
1984/**
1985 * devm_pwm_get() - resource managed pwm_get()
1986 * @dev: device for PWM consumer
1987 * @con_id: consumer name
1988 *
1989 * This function performs like pwm_get() but the acquired PWM device will
1990 * automatically be released on driver detach.
1991 *
1992 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1993 * error code on failure.
1994 */
1995struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1996{
1997	struct pwm_device *pwm;
1998	int ret;
1999
2000	pwm = pwm_get(dev, con_id);
2001	if (IS_ERR(pwm))
2002		return pwm;
2003
2004	ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
2005	if (ret)
2006		return ERR_PTR(ret);
 
 
 
 
2007
2008	return pwm;
2009}
2010EXPORT_SYMBOL_GPL(devm_pwm_get);
2011
2012/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2013 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
2014 * @dev: device for PWM consumer
2015 * @fwnode: firmware node to get the PWM from
2016 * @con_id: consumer name
2017 *
2018 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
2019 * acpi_pwm_get() for a detailed description.
2020 *
2021 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
2022 * error code on failure.
2023 */
2024struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
2025				       struct fwnode_handle *fwnode,
2026				       const char *con_id)
2027{
2028	struct pwm_device *pwm = ERR_PTR(-ENODEV);
2029	int ret;
 
 
 
2030
2031	if (is_of_node(fwnode))
2032		pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
2033	else if (is_acpi_node(fwnode))
2034		pwm = acpi_pwm_get(fwnode);
2035	if (IS_ERR(pwm))
2036		return pwm;
2037
2038	ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
2039	if (ret)
2040		return ERR_PTR(ret);
 
 
 
2041
2042	return pwm;
2043}
2044EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
2045
2046/**
2047 * __pwmchip_add() - register a new PWM chip
2048 * @chip: the PWM chip to add
2049 * @owner: reference to the module providing the chip.
2050 *
2051 * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
2052 * pwmchip_add wrapper to do this right.
2053 *
2054 * Returns: 0 on success or a negative error code on failure.
2055 */
2056int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
2057{
2058	int ret;
2059
2060	if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
2061		return -EINVAL;
2062
2063	/*
2064	 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
2065	 * otherwise the embedded struct device might disappear too early
2066	 * resulting in memory corruption.
2067	 * Catch drivers that were not converted appropriately.
2068	 */
2069	if (!chip->uses_pwmchip_alloc)
2070		return -EINVAL;
2071
2072	if (!pwm_ops_check(chip))
2073		return -EINVAL;
2074
2075	chip->owner = owner;
2076
2077	if (chip->atomic)
2078		spin_lock_init(&chip->atomic_lock);
2079	else
2080		mutex_init(&chip->nonatomic_lock);
2081
2082	guard(mutex)(&pwm_lock);
2083
2084	ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
2085	if (ret < 0)
2086		return ret;
2087
2088	chip->id = ret;
2089
2090	dev_set_name(&chip->dev, "pwmchip%u", chip->id);
2091
2092	if (IS_ENABLED(CONFIG_OF))
2093		of_pwmchip_add(chip);
2094
2095	scoped_guard(pwmchip, chip)
2096		chip->operational = true;
2097
2098	ret = device_add(&chip->dev);
2099	if (ret)
2100		goto err_device_add;
2101
2102	return 0;
2103
2104err_device_add:
2105	scoped_guard(pwmchip, chip)
2106		chip->operational = false;
2107
2108	if (IS_ENABLED(CONFIG_OF))
2109		of_pwmchip_remove(chip);
2110
2111	idr_remove(&pwm_chips, chip->id);
2112
2113	return ret;
2114}
2115EXPORT_SYMBOL_GPL(__pwmchip_add);
2116
2117/**
2118 * pwmchip_remove() - remove a PWM chip
2119 * @chip: the PWM chip to remove
 
2120 *
2121 * Removes a PWM chip.
2122 */
2123void pwmchip_remove(struct pwm_chip *chip)
2124{
2125	pwmchip_sysfs_unexport(chip);
2126
2127	scoped_guard(mutex, &pwm_lock) {
2128		unsigned int i;
2129
2130		scoped_guard(pwmchip, chip)
2131			chip->operational = false;
2132
2133		for (i = 0; i < chip->npwm; ++i) {
2134			struct pwm_device *pwm = &chip->pwms[i];
2135
2136			if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
2137				dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i);
2138				if (pwm->chip->ops->free)
2139					pwm->chip->ops->free(pwm->chip, pwm);
2140			}
2141		}
2142
2143		if (IS_ENABLED(CONFIG_OF))
2144			of_pwmchip_remove(chip);
2145
2146		idr_remove(&pwm_chips, chip->id);
2147	}
2148
2149	device_del(&chip->dev);
2150}
2151EXPORT_SYMBOL_GPL(pwmchip_remove);
2152
2153static void devm_pwmchip_remove(void *data)
2154{
2155	struct pwm_chip *chip = data;
2156
2157	pwmchip_remove(chip);
2158}
2159
2160int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
2161{
2162	int ret;
2163
2164	ret = __pwmchip_add(chip, owner);
2165	if (ret)
2166		return ret;
2167
2168	return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
2169}
2170EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
2171
2172/**
2173 * pwm_add_table() - register PWM device consumers
2174 * @table: array of consumers to register
2175 * @num: number of consumers in table
2176 */
2177void pwm_add_table(struct pwm_lookup *table, size_t num)
2178{
2179	guard(mutex)(&pwm_lookup_lock);
2180
2181	while (num--) {
2182		list_add_tail(&table->list, &pwm_lookup_list);
2183		table++;
2184	}
2185}
2186
2187/**
2188 * pwm_remove_table() - unregister PWM device consumers
2189 * @table: array of consumers to unregister
2190 * @num: number of consumers in table
2191 */
2192void pwm_remove_table(struct pwm_lookup *table, size_t num)
2193{
2194	guard(mutex)(&pwm_lookup_lock);
2195
2196	while (num--) {
2197		list_del(&table->list);
2198		table++;
2199	}
2200}
 
2201
 
2202static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
2203{
2204	unsigned int i;
2205
2206	for (i = 0; i < chip->npwm; i++) {
2207		struct pwm_device *pwm = &chip->pwms[i];
2208		struct pwm_state state;
2209
2210		pwm_get_state(pwm, &state);
2211
2212		seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
2213
2214		if (test_bit(PWMF_REQUESTED, &pwm->flags))
2215			seq_puts(s, " requested");
2216
2217		if (state.enabled)
2218			seq_puts(s, " enabled");
2219
2220		seq_printf(s, " period: %llu ns", state.period);
2221		seq_printf(s, " duty: %llu ns", state.duty_cycle);
2222		seq_printf(s, " polarity: %s",
2223			   state.polarity ? "inverse" : "normal");
2224
2225		if (state.usage_power)
2226			seq_puts(s, " usage_power");
2227
2228		seq_puts(s, "\n");
2229	}
2230}
2231
2232static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
2233{
2234	unsigned long id = *pos;
2235	void *ret;
2236
2237	mutex_lock(&pwm_lock);
2238	s->private = "";
2239
2240	ret = idr_get_next_ul(&pwm_chips, &id);
2241	*pos = id;
2242	return ret;
2243}
2244
2245static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
2246{
2247	unsigned long id = *pos + 1;
2248	void *ret;
2249
2250	s->private = "\n";
2251
2252	ret = idr_get_next_ul(&pwm_chips, &id);
2253	*pos = id;
2254	return ret;
2255}
2256
2257static void pwm_seq_stop(struct seq_file *s, void *v)
2258{
2259	mutex_unlock(&pwm_lock);
2260}
2261
2262static int pwm_seq_show(struct seq_file *s, void *v)
2263{
2264	struct pwm_chip *chip = v;
2265
2266	seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
2267		   (char *)s->private, chip->id,
2268		   pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
2269		   dev_name(pwmchip_parent(chip)), chip->npwm,
2270		   (chip->npwm != 1) ? "s" : "");
2271
2272	pwm_dbg_show(chip, s);
2273
2274	return 0;
2275}
2276
2277static const struct seq_operations pwm_debugfs_sops = {
2278	.start = pwm_seq_start,
2279	.next = pwm_seq_next,
2280	.stop = pwm_seq_stop,
2281	.show = pwm_seq_show,
2282};
2283
2284DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
2285
2286static int __init pwm_init(void)
2287{
2288	int ret;
 
2289
2290	ret = class_register(&pwm_class);
2291	if (ret) {
2292		pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
2293		return ret;
2294	}
 
 
2295
2296	if (IS_ENABLED(CONFIG_DEBUG_FS))
2297		debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
 
 
2298
2299	return 0;
2300}
2301subsys_initcall(pwm_init);
 
v5.4
   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
  25static DEFINE_MUTEX(pwm_lookup_lock);
  26static LIST_HEAD(pwm_lookup_list);
  27static DEFINE_MUTEX(pwm_lock);
  28static LIST_HEAD(pwm_chips);
  29static DECLARE_BITMAP(allocated_pwms, MAX_PWMS);
  30static RADIX_TREE(pwm_tree, GFP_KERNEL);
  31
  32static struct pwm_device *pwm_to_device(unsigned int pwm)
 
 
  33{
  34	return radix_tree_lookup(&pwm_tree, pwm);
 
 
 
  35}
  36
  37static 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
  60static 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
  76static 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
  99static 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
 123struct pwm_device *
 124of_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}
 151EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
 152
 153static struct pwm_device *
 154of_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
 178static 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
 191static 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 */
 204int 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}
 213EXPORT_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 */
 221void *pwm_get_chip_data(struct pwm_device *pwm)
 222{
 223	return pwm ? pwm->chip_data : NULL;
 224}
 225EXPORT_SYMBOL_GPL(pwm_get_chip_data);
 226
 227static 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 */
 251int 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
 286		if (chip->ops->get_state)
 287			chip->ops->get_state(chip, pwm, &pwm->state);
 
 288
 289		radix_tree_insert(&pwm_tree, pwm->pwm, pwm);
 290	}
 
 
 291
 292	bitmap_set(allocated_pwms, chip->base, chip->npwm);
 
 
 
 
 
 
 
 
 293
 294	INIT_LIST_HEAD(&chip->list);
 295	list_add(&chip->list, &pwm_chips);
 
 296
 297	ret = 0;
 
 
 298
 299	if (IS_ENABLED(CONFIG_OF))
 300		of_pwmchip_add(chip);
 301
 302out:
 303	mutex_unlock(&pwm_lock);
 304
 305	if (!ret)
 306		pwmchip_sysfs_export(chip);
 
 
 307
 308	return ret;
 309}
 310EXPORT_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.
 318 *
 319 * Returns: 0 on success or a negative error code on failure.
 320 */
 321int pwmchip_add(struct pwm_chip *chip)
 322{
 323	return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL);
 324}
 325EXPORT_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.
 333 *
 334 * Returns: 0 on success or a negative error code on failure.
 
 
 
 
 
 
 
 
 335 */
 336int pwmchip_remove(struct pwm_chip *chip)
 
 337{
 338	unsigned int i;
 339	int ret = 0;
 340
 341	pwmchip_sysfs_unexport(chip);
 342
 343	mutex_lock(&pwm_lock);
 344
 345	for (i = 0; i < chip->npwm; i++) {
 346		struct pwm_device *pwm = &chip->pwms[i];
 347
 348		if (test_bit(PWMF_REQUESTED, &pwm->flags)) {
 349			ret = -EBUSY;
 350			goto out;
 351		}
 
 
 
 
 
 
 352	}
 353
 354	list_del_init(&chip->list);
 355
 356	if (IS_ENABLED(CONFIG_OF))
 357		of_pwmchip_remove(chip);
 358
 359	free_pwms(chip);
 360
 361out:
 362	mutex_unlock(&pwm_lock);
 363	return ret;
 364}
 365EXPORT_SYMBOL_GPL(pwmchip_remove);
 366
 367/**
 368 * pwm_request() - request a PWM device
 369 * @pwm: global PWM device index
 370 * @label: PWM device label
 371 *
 372 * This function is deprecated, use pwm_get() instead.
 373 *
 374 * Returns: A pointer to a PWM device or an ERR_PTR()-encoded error code on
 375 * failure.
 376 */
 377struct pwm_device *pwm_request(int pwm, const char *label)
 378{
 379	struct pwm_device *dev;
 
 
 380	int err;
 381
 382	if (pwm < 0 || pwm >= MAX_PWMS)
 383		return ERR_PTR(-EINVAL);
 
 
 
 
 
 
 
 
 
 384
 385	mutex_lock(&pwm_lock);
 
 
 
 
 386
 387	dev = pwm_to_device(pwm);
 388	if (!dev) {
 389		dev = ERR_PTR(-EPROBE_DEFER);
 390		goto out;
 
 
 
 
 
 
 
 
 391	}
 392
 393	err = pwm_device_request(dev, label);
 394	if (err < 0)
 395		dev = ERR_PTR(err);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 396
 397out:
 398	mutex_unlock(&pwm_lock);
 
 
 
 399
 400	return dev;
 
 
 
 
 
 
 
 
 
 
 401}
 402EXPORT_SYMBOL_GPL(pwm_request);
 403
 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 *
 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.
 413 */
 414struct 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;
 
 
 
 
 
 420
 421	if (!chip || index >= chip->npwm)
 422		return ERR_PTR(-EINVAL);
 423
 424	mutex_lock(&pwm_lock);
 425	pwm = &chip->pwms[index];
 426
 427	err = pwm_device_request(pwm, label);
 428	if (err < 0)
 429		pwm = ERR_PTR(err);
 430
 431	mutex_unlock(&pwm_lock);
 432	return pwm;
 433}
 434EXPORT_SYMBOL_GPL(pwm_request_from_chip);
 435
 436/**
 437 * pwm_free() - free a PWM device
 438 * @pwm: PWM device
 439 *
 440 * This function is deprecated, use pwm_put() instead.
 441 */
 442void pwm_free(struct pwm_device *pwm)
 443{
 444	pwm_put(pwm);
 445}
 446EXPORT_SYMBOL_GPL(pwm_free);
 447
 448/**
 449 * pwm_apply_state() - atomically apply a new state to a PWM device
 450 * @pwm: PWM device
 451 * @state: new state to apply
 452 */
 453int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
 454{
 455	struct pwm_chip *chip;
 
 456	int err;
 457
 458	if (!pwm || !state || !state->period ||
 459	    state->duty_cycle > state->period)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 460		return -EINVAL;
 
 461
 462	chip = pwm->chip;
 
 463
 464	if (state->period == pwm->state.period &&
 465	    state->duty_cycle == pwm->state.duty_cycle &&
 466	    state->polarity == pwm->state.polarity &&
 467	    state->enabled == pwm->state.enabled)
 
 468		return 0;
 469
 470	if (chip->ops->apply) {
 471		err = chip->ops->apply(chip, pwm, state);
 472		if (err)
 473			return err;
 
 
 
 474
 475		pwm->state = *state;
 476	} else {
 477		/*
 478		 * FIXME: restore the initial state in case of error.
 
 
 
 
 
 479		 */
 480		if (state->polarity != pwm->state.polarity) {
 481			if (!chip->ops->set_polarity)
 482				return -ENOTSUPP;
 483
 484			/*
 485			 * Changing the polarity of a running PWM is
 486			 * only allowed when the PWM driver implements
 487			 * ->apply().
 488			 */
 489			if (pwm->state.enabled) {
 490				chip->ops->disable(chip, pwm);
 491				pwm->state.enabled = false;
 492			}
 
 
 
 
 
 
 493
 494			err = chip->ops->set_polarity(chip, pwm,
 495						      state->polarity);
 496			if (err)
 497				return err;
 498
 499			pwm->state.polarity = state->polarity;
 
 
 
 500		}
 501
 502		if (state->period != pwm->state.period ||
 503		    state->duty_cycle != pwm->state.duty_cycle) {
 504			err = chip->ops->config(pwm->chip, pwm,
 505						state->duty_cycle,
 506						state->period);
 507			if (err)
 508				return err;
 509
 510			pwm->state.duty_cycle = state->duty_cycle;
 511			pwm->state.period = state->period;
 512		}
 
 
 513
 514		if (state->enabled != pwm->state.enabled) {
 515			if (state->enabled) {
 516				err = chip->ops->enable(chip, pwm);
 517				if (err)
 518					return err;
 519			} else {
 520				chip->ops->disable(chip, pwm);
 521			}
 522
 523			pwm->state.enabled = state->enabled;
 524		}
 
 
 
 525	}
 526
 527	return 0;
 528}
 529EXPORT_SYMBOL_GPL(pwm_apply_state);
 530
 531/**
 532 * pwm_capture() - capture and report a PWM signal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 533 * @pwm: PWM device
 534 * @result: structure to fill with capture result
 535 * @timeout: time to wait, in milliseconds, before giving up on capture
 
 
 536 *
 537 * Returns: 0 on success or a negative error code on failure.
 
 538 */
 539int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
 540		unsigned long timeout)
 541{
 542	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 543
 544	if (!pwm || !pwm->chip->ops)
 545		return -EINVAL;
 
 546
 547	if (!pwm->chip->ops->capture)
 548		return -ENOSYS;
 549
 550	mutex_lock(&pwm_lock);
 551	err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
 552	mutex_unlock(&pwm_lock);
 
 553
 554	return err;
 555}
 556EXPORT_SYMBOL_GPL(pwm_capture);
 557
 558/**
 559 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
 560 * @pwm: PWM device
 561 *
 562 * This function will adjust the PWM config to the PWM arguments provided
 563 * by the DT or PWM lookup table. This is particularly useful to adapt
 564 * the bootloader config to the Linux one.
 565 */
 566int pwm_adjust_config(struct pwm_device *pwm)
 567{
 568	struct pwm_state state;
 569	struct pwm_args pargs;
 570
 571	pwm_get_args(pwm, &pargs);
 572	pwm_get_state(pwm, &state);
 573
 574	/*
 575	 * If the current period is zero it means that either the PWM driver
 576	 * does not support initial state retrieval or the PWM has not yet
 577	 * been configured.
 578	 *
 579	 * In either case, we setup the new period and polarity, and assign a
 580	 * duty cycle of 0.
 581	 */
 582	if (!state.period) {
 583		state.duty_cycle = 0;
 584		state.period = pargs.period;
 585		state.polarity = pargs.polarity;
 586
 587		return pwm_apply_state(pwm, &state);
 588	}
 589
 590	/*
 591	 * Adjust the PWM duty cycle/period based on the period value provided
 592	 * in PWM args.
 593	 */
 594	if (pargs.period != state.period) {
 595		u64 dutycycle = (u64)state.duty_cycle * pargs.period;
 596
 597		do_div(dutycycle, state.period);
 598		state.duty_cycle = dutycycle;
 599		state.period = pargs.period;
 600	}
 601
 602	/*
 603	 * If the polarity changed, we should also change the duty cycle.
 604	 */
 605	if (pargs.polarity != state.polarity) {
 606		state.polarity = pargs.polarity;
 607		state.duty_cycle = state.period - state.duty_cycle;
 608	}
 609
 610	return pwm_apply_state(pwm, &state);
 611}
 612EXPORT_SYMBOL_GPL(pwm_adjust_config);
 613
 614static struct pwm_chip *of_node_to_pwmchip(struct device_node *np)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 615{
 616	struct pwm_chip *chip;
 
 
 
 
 617
 618	mutex_lock(&pwm_lock);
 619
 620	list_for_each_entry(chip, &pwm_chips, list)
 621		if (chip->dev && chip->dev->of_node == np) {
 622			mutex_unlock(&pwm_lock);
 623			return chip;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 624		}
 625
 626	mutex_unlock(&pwm_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 627
 628	return ERR_PTR(-EPROBE_DEFER);
 629}
 630
 631static struct device_link *pwm_device_link_add(struct device *dev,
 632					       struct pwm_device *pwm)
 633{
 634	struct device_link *dl;
 635
 636	if (!dev) {
 637		/*
 638		 * No device for the PWM consumer has been provided. It may
 639		 * impact the PM sequence ordering: the PWM supplier may get
 640		 * suspended before the consumer.
 641		 */
 642		dev_warn(pwm->chip->dev,
 643			 "No consumer device specified to create a link to\n");
 644		return NULL;
 645	}
 646
 647	dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
 648	if (!dl) {
 649		dev_err(dev, "failed to create device link to %s\n",
 650			dev_name(pwm->chip->dev));
 651		return ERR_PTR(-EINVAL);
 652	}
 653
 654	return dl;
 655}
 656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 657/**
 658 * of_pwm_get() - request a PWM via the PWM framework
 659 * @dev: device for PWM consumer
 660 * @np: device node to get the PWM from
 661 * @con_id: consumer name
 662 *
 663 * Returns the PWM device parsed from the phandle and index specified in the
 664 * "pwms" property of a device tree node or a negative error-code on failure.
 665 * Values parsed from the device tree are stored in the returned PWM device
 666 * object.
 667 *
 668 * If con_id is NULL, the first PWM device listed in the "pwms" property will
 669 * be requested. Otherwise the "pwm-names" property is used to do a reverse
 670 * lookup of the PWM index. This also means that the "pwm-names" property
 671 * becomes mandatory for devices that look up the PWM device via the con_id
 672 * parameter.
 673 *
 674 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
 675 * error code on failure.
 676 */
 677struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
 678			      const char *con_id)
 679{
 680	struct pwm_device *pwm = NULL;
 681	struct of_phandle_args args;
 682	struct device_link *dl;
 683	struct pwm_chip *pc;
 684	int index = 0;
 685	int err;
 686
 687	if (con_id) {
 688		index = of_property_match_string(np, "pwm-names", con_id);
 689		if (index < 0)
 690			return ERR_PTR(index);
 691	}
 692
 693	err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
 694					 &args);
 695	if (err) {
 696		pr_err("%s(): can't parse \"pwms\" property\n", __func__);
 697		return ERR_PTR(err);
 698	}
 699
 700	pc = of_node_to_pwmchip(args.np);
 701	if (IS_ERR(pc)) {
 702		if (PTR_ERR(pc) != -EPROBE_DEFER)
 703			pr_err("%s(): PWM chip not found\n", __func__);
 704
 705		pwm = ERR_CAST(pc);
 706		goto put;
 707	}
 708
 709	pwm = pc->of_xlate(pc, &args);
 710	if (IS_ERR(pwm))
 711		goto put;
 712
 713	dl = pwm_device_link_add(dev, pwm);
 714	if (IS_ERR(dl)) {
 715		/* of_xlate ended up calling pwm_request_from_chip() */
 716		pwm_free(pwm);
 717		pwm = ERR_CAST(dl);
 718		goto put;
 719	}
 720
 721	/*
 722	 * If a consumer name was not given, try to look it up from the
 723	 * "pwm-names" property if it exists. Otherwise use the name of
 724	 * the user device node.
 725	 */
 726	if (!con_id) {
 727		err = of_property_read_string_index(np, "pwm-names", index,
 728						    &con_id);
 729		if (err < 0)
 730			con_id = np->name;
 731	}
 732
 733	pwm->label = con_id;
 734
 735put:
 736	of_node_put(args.np);
 737
 738	return pwm;
 739}
 740EXPORT_SYMBOL_GPL(of_pwm_get);
 741
 742#if IS_ENABLED(CONFIG_ACPI)
 743static struct pwm_chip *device_to_pwmchip(struct device *dev)
 744{
 745	struct pwm_chip *chip;
 746
 747	mutex_lock(&pwm_lock);
 748
 749	list_for_each_entry(chip, &pwm_chips, list) {
 750		struct acpi_device *adev = ACPI_COMPANION(chip->dev);
 751
 752		if ((chip->dev == dev) || (adev && &adev->dev == dev)) {
 753			mutex_unlock(&pwm_lock);
 754			return chip;
 755		}
 756	}
 757
 758	mutex_unlock(&pwm_lock);
 759
 760	return ERR_PTR(-EPROBE_DEFER);
 761}
 762#endif
 763
 764/**
 765 * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
 766 * @fwnode: firmware node to get the "pwm" property from
 767 *
 768 * Returns the PWM device parsed from the fwnode and index specified in the
 769 * "pwms" property or a negative error-code on failure.
 770 * Values parsed from the device tree are stored in the returned PWM device
 771 * object.
 772 *
 773 * This is analogous to of_pwm_get() except con_id is not yet supported.
 774 * ACPI entries must look like
 775 * Package () {"pwms", Package ()
 776 *     { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
 777 *
 778 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
 779 * error code on failure.
 780 */
 781static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode)
 782{
 783	struct pwm_device *pwm = ERR_PTR(-ENODEV);
 784#if IS_ENABLED(CONFIG_ACPI)
 785	struct fwnode_reference_args args;
 786	struct acpi_device *acpi;
 787	struct pwm_chip *chip;
 788	int ret;
 789
 790	memset(&args, 0, sizeof(args));
 791
 792	ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
 793	if (ret < 0)
 794		return ERR_PTR(ret);
 795
 796	acpi = to_acpi_device_node(args.fwnode);
 797	if (!acpi)
 798		return ERR_PTR(-EINVAL);
 799
 800	if (args.nargs < 2)
 801		return ERR_PTR(-EPROTO);
 802
 803	chip = device_to_pwmchip(&acpi->dev);
 804	if (IS_ERR(chip))
 805		return ERR_CAST(chip);
 806
 807	pwm = pwm_request_from_chip(chip, args.args[0], NULL);
 808	if (IS_ERR(pwm))
 809		return pwm;
 810
 811	pwm->args.period = args.args[1];
 812	pwm->args.polarity = PWM_POLARITY_NORMAL;
 813
 814	if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
 815		pwm->args.polarity = PWM_POLARITY_INVERSED;
 816#endif
 817
 818	return pwm;
 819}
 820
 821/**
 822 * pwm_add_table() - register PWM device consumers
 823 * @table: array of consumers to register
 824 * @num: number of consumers in table
 825 */
 826void pwm_add_table(struct pwm_lookup *table, size_t num)
 827{
 828	mutex_lock(&pwm_lookup_lock);
 829
 830	while (num--) {
 831		list_add_tail(&table->list, &pwm_lookup_list);
 832		table++;
 833	}
 834
 835	mutex_unlock(&pwm_lookup_lock);
 836}
 837
 838/**
 839 * pwm_remove_table() - unregister PWM device consumers
 840 * @table: array of consumers to unregister
 841 * @num: number of consumers in table
 842 */
 843void pwm_remove_table(struct pwm_lookup *table, size_t num)
 844{
 845	mutex_lock(&pwm_lookup_lock);
 846
 847	while (num--) {
 848		list_del(&table->list);
 849		table++;
 850	}
 851
 852	mutex_unlock(&pwm_lookup_lock);
 853}
 854
 855/**
 856 * pwm_get() - look up and request a PWM device
 857 * @dev: device for PWM consumer
 858 * @con_id: consumer name
 859 *
 860 * Lookup is first attempted using DT. If the device was not instantiated from
 861 * a device tree, a PWM chip and a relative index is looked up via a table
 862 * supplied by board setup code (see pwm_add_table()).
 863 *
 864 * Once a PWM chip has been found the specified PWM device will be requested
 865 * and is ready to be used.
 866 *
 867 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
 868 * error code on failure.
 869 */
 870struct pwm_device *pwm_get(struct device *dev, const char *con_id)
 871{
 
 872	const char *dev_id = dev ? dev_name(dev) : NULL;
 873	struct pwm_device *pwm;
 874	struct pwm_chip *chip;
 875	struct device_link *dl;
 876	unsigned int best = 0;
 877	struct pwm_lookup *p, *chosen = NULL;
 878	unsigned int match;
 879	int err;
 880
 881	/* look up via DT first */
 882	if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
 883		return of_pwm_get(dev, dev->of_node, con_id);
 884
 885	/* then lookup via ACPI */
 886	if (dev && is_acpi_node(dev->fwnode)) {
 887		pwm = acpi_pwm_get(dev->fwnode);
 888		if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
 889			return pwm;
 890	}
 891
 892	/*
 893	 * We look up the provider in the static table typically provided by
 894	 * board setup code. We first try to lookup the consumer device by
 895	 * name. If the consumer device was passed in as NULL or if no match
 896	 * was found, we try to find the consumer by directly looking it up
 897	 * by name.
 898	 *
 899	 * If a match is found, the provider PWM chip is looked up by name
 900	 * and a PWM device is requested using the PWM device per-chip index.
 901	 *
 902	 * The lookup algorithm was shamelessly taken from the clock
 903	 * framework:
 904	 *
 905	 * We do slightly fuzzy matching here:
 906	 *  An entry with a NULL ID is assumed to be a wildcard.
 907	 *  If an entry has a device ID, it must match
 908	 *  If an entry has a connection ID, it must match
 909	 * Then we take the most specific entry - with the following order
 910	 * of precedence: dev+con > dev only > con only.
 911	 */
 912	mutex_lock(&pwm_lookup_lock);
 
 
 
 
 
 
 913
 914	list_for_each_entry(p, &pwm_lookup_list, list) {
 915		match = 0;
 916
 917		if (p->dev_id) {
 918			if (!dev_id || strcmp(p->dev_id, dev_id))
 919				continue;
 920
 921			match += 2;
 922		}
 923
 924		if (p->con_id) {
 925			if (!con_id || strcmp(p->con_id, con_id))
 926				continue;
 927
 928			match += 1;
 
 
 
 
 929		}
 930
 931		if (match > best) {
 932			chosen = p;
 933
 934			if (match != 3)
 935				best = match;
 936			else
 937				break;
 938		}
 939	}
 940
 941	mutex_unlock(&pwm_lookup_lock);
 942
 943	if (!chosen)
 944		return ERR_PTR(-ENODEV);
 945
 946	chip = pwmchip_find_by_name(chosen->provider);
 947
 948	/*
 949	 * If the lookup entry specifies a module, load the module and retry
 950	 * the PWM chip lookup. This can be used to work around driver load
 951	 * ordering issues if driver's can't be made to properly support the
 952	 * deferred probe mechanism.
 953	 */
 954	if (!chip && chosen->module) {
 955		err = request_module(chosen->module);
 956		if (err == 0)
 957			chip = pwmchip_find_by_name(chosen->provider);
 958	}
 959
 960	if (!chip)
 961		return ERR_PTR(-EPROBE_DEFER);
 962
 963	pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
 964	if (IS_ERR(pwm))
 965		return pwm;
 966
 967	dl = pwm_device_link_add(dev, pwm);
 968	if (IS_ERR(dl)) {
 969		pwm_free(pwm);
 970		return ERR_CAST(dl);
 971	}
 972
 973	pwm->args.period = chosen->period;
 974	pwm->args.polarity = chosen->polarity;
 975
 976	return pwm;
 977}
 978EXPORT_SYMBOL_GPL(pwm_get);
 979
 980/**
 981 * pwm_put() - release a PWM device
 982 * @pwm: PWM device
 983 */
 984void pwm_put(struct pwm_device *pwm)
 985{
 
 
 986	if (!pwm)
 987		return;
 988
 989	mutex_lock(&pwm_lock);
 
 
 990
 991	if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
 
 
 
 
 
 992		pr_warn("PWM device already freed\n");
 993		goto out;
 994	}
 995
 996	if (pwm->chip->ops->free)
 997		pwm->chip->ops->free(pwm->chip, pwm);
 998
 999	pwm_set_chip_data(pwm, NULL);
1000	pwm->label = NULL;
1001
1002	module_put(pwm->chip->ops->owner);
1003out:
1004	mutex_unlock(&pwm_lock);
1005}
1006EXPORT_SYMBOL_GPL(pwm_put);
1007
1008static void devm_pwm_release(struct device *dev, void *res)
1009{
1010	pwm_put(*(struct pwm_device **)res);
1011}
1012
1013/**
1014 * devm_pwm_get() - resource managed pwm_get()
1015 * @dev: device for PWM consumer
1016 * @con_id: consumer name
1017 *
1018 * This function performs like pwm_get() but the acquired PWM device will
1019 * automatically be released on driver detach.
1020 *
1021 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1022 * error code on failure.
1023 */
1024struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
1025{
1026	struct pwm_device **ptr, *pwm;
 
1027
1028	ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1029	if (!ptr)
1030		return ERR_PTR(-ENOMEM);
1031
1032	pwm = pwm_get(dev, con_id);
1033	if (!IS_ERR(pwm)) {
1034		*ptr = pwm;
1035		devres_add(dev, ptr);
1036	} else {
1037		devres_free(ptr);
1038	}
1039
1040	return pwm;
1041}
1042EXPORT_SYMBOL_GPL(devm_pwm_get);
1043
1044/**
1045 * devm_of_pwm_get() - resource managed of_pwm_get()
1046 * @dev: device for PWM consumer
1047 * @np: device node to get the PWM from
1048 * @con_id: consumer name
1049 *
1050 * This function performs like of_pwm_get() but the acquired PWM device will
1051 * automatically be released on driver detach.
1052 *
1053 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1054 * error code on failure.
1055 */
1056struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
1057				   const char *con_id)
1058{
1059	struct pwm_device **ptr, *pwm;
1060
1061	ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1062	if (!ptr)
1063		return ERR_PTR(-ENOMEM);
1064
1065	pwm = of_pwm_get(dev, np, con_id);
1066	if (!IS_ERR(pwm)) {
1067		*ptr = pwm;
1068		devres_add(dev, ptr);
1069	} else {
1070		devres_free(ptr);
1071	}
1072
1073	return pwm;
1074}
1075EXPORT_SYMBOL_GPL(devm_of_pwm_get);
1076
1077/**
1078 * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
1079 * @dev: device for PWM consumer
1080 * @fwnode: firmware node to get the PWM from
1081 * @con_id: consumer name
1082 *
1083 * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
1084 * acpi_pwm_get() for a detailed description.
1085 *
1086 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
1087 * error code on failure.
1088 */
1089struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
1090				       struct fwnode_handle *fwnode,
1091				       const char *con_id)
1092{
1093	struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV);
1094
1095	ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
1096	if (!ptr)
1097		return ERR_PTR(-ENOMEM);
1098
1099	if (is_of_node(fwnode))
1100		pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
1101	else if (is_acpi_node(fwnode))
1102		pwm = acpi_pwm_get(fwnode);
 
 
1103
1104	if (!IS_ERR(pwm)) {
1105		*ptr = pwm;
1106		devres_add(dev, ptr);
1107	} else {
1108		devres_free(ptr);
1109	}
1110
1111	return pwm;
1112}
1113EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
1114
1115static int devm_pwm_match(struct device *dev, void *res, void *data)
 
 
 
 
 
 
 
 
 
 
1116{
1117	struct pwm_device **p = res;
 
 
 
1118
1119	if (WARN_ON(!p || !*p))
1120		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1121
1122	return *p == data;
1123}
 
1124
1125/**
1126 * devm_pwm_put() - resource managed pwm_put()
1127 * @dev: device for PWM consumer
1128 * @pwm: PWM device
1129 *
1130 * Release a PWM previously allocated using devm_pwm_get(). Calling this
1131 * function is usually not needed because devm-allocated resources are
1132 * automatically released on driver detach.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1133 */
1134void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
1135{
1136	WARN_ON(devres_release(dev, devm_pwm_release, devm_pwm_match, pwm));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1137}
1138EXPORT_SYMBOL_GPL(devm_pwm_put);
1139
1140#ifdef CONFIG_DEBUG_FS
1141static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
1142{
1143	unsigned int i;
1144
1145	for (i = 0; i < chip->npwm; i++) {
1146		struct pwm_device *pwm = &chip->pwms[i];
1147		struct pwm_state state;
1148
1149		pwm_get_state(pwm, &state);
1150
1151		seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
1152
1153		if (test_bit(PWMF_REQUESTED, &pwm->flags))
1154			seq_puts(s, " requested");
1155
1156		if (state.enabled)
1157			seq_puts(s, " enabled");
1158
1159		seq_printf(s, " period: %u ns", state.period);
1160		seq_printf(s, " duty: %u ns", state.duty_cycle);
1161		seq_printf(s, " polarity: %s",
1162			   state.polarity ? "inverse" : "normal");
1163
 
 
 
1164		seq_puts(s, "\n");
1165	}
1166}
1167
1168static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
1169{
 
 
 
1170	mutex_lock(&pwm_lock);
1171	s->private = "";
1172
1173	return seq_list_start(&pwm_chips, *pos);
 
 
1174}
1175
1176static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
1177{
 
 
 
1178	s->private = "\n";
1179
1180	return seq_list_next(v, &pwm_chips, pos);
 
 
1181}
1182
1183static void pwm_seq_stop(struct seq_file *s, void *v)
1184{
1185	mutex_unlock(&pwm_lock);
1186}
1187
1188static int pwm_seq_show(struct seq_file *s, void *v)
1189{
1190	struct pwm_chip *chip = list_entry(v, struct pwm_chip, list);
1191
1192	seq_printf(s, "%s%s/%s, %d PWM device%s\n", (char *)s->private,
1193		   chip->dev->bus ? chip->dev->bus->name : "no-bus",
1194		   dev_name(chip->dev), chip->npwm,
 
1195		   (chip->npwm != 1) ? "s" : "");
1196
1197	pwm_dbg_show(chip, s);
1198
1199	return 0;
1200}
1201
1202static const struct seq_operations pwm_seq_ops = {
1203	.start = pwm_seq_start,
1204	.next = pwm_seq_next,
1205	.stop = pwm_seq_stop,
1206	.show = pwm_seq_show,
1207};
1208
1209static int pwm_seq_open(struct inode *inode, struct file *file)
 
 
1210{
1211	return seq_open(file, &pwm_seq_ops);
1212}
1213
1214static const struct file_operations pwm_debugfs_ops = {
1215	.owner = THIS_MODULE,
1216	.open = pwm_seq_open,
1217	.read = seq_read,
1218	.llseek = seq_lseek,
1219	.release = seq_release,
1220};
1221
1222static int __init pwm_debugfs_init(void)
1223{
1224	debugfs_create_file("pwm", S_IFREG | S_IRUGO, NULL, NULL,
1225			    &pwm_debugfs_ops);
1226
1227	return 0;
1228}
1229subsys_initcall(pwm_debugfs_init);
1230#endif /* CONFIG_DEBUG_FS */