Skip to content

Commit bd26861

Browse files
committed
Merge branch 'for-4.8/capture' into for-next
2 parents 489babe + 1a366fe commit bd26861

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

Documentation/ABI/testing/sysfs-class-pwm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ Description:
7777
Enable/disable the PWM signal.
7878
0 is disabled
7979
1 is enabled
80+
81+
What: /sys/class/pwm/pwmchipN/pwmX/capture
82+
Date: June 2016
83+
KernelVersion: 4.8
84+
Contact: Lee Jones <lee.jones@linaro.org>
85+
Description:
86+
Capture information about a PWM signal. The output format is a
87+
pair unsigned integers (period and duty cycle), separated by a
88+
single space.

drivers/pwm/core.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,33 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state)
525525
}
526526
EXPORT_SYMBOL_GPL(pwm_apply_state);
527527

528+
/**
529+
* pwm_capture() - capture and report a PWM signal
530+
* @pwm: PWM device
531+
* @result: structure to fill with capture result
532+
* @timeout: time to wait, in milliseconds, before giving up on capture
533+
*
534+
* Returns: 0 on success or a negative error code on failure.
535+
*/
536+
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
537+
unsigned long timeout)
538+
{
539+
int err;
540+
541+
if (!pwm || !pwm->chip->ops)
542+
return -EINVAL;
543+
544+
if (!pwm->chip->ops->capture)
545+
return -ENOSYS;
546+
547+
mutex_lock(&pwm_lock);
548+
err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
549+
mutex_unlock(&pwm_lock);
550+
551+
return err;
552+
}
553+
EXPORT_SYMBOL_GPL(pwm_capture);
554+
528555
/**
529556
* pwm_adjust_config() - adjust the current PWM config to the PWM arguments
530557
* @pwm: PWM device

drivers/pwm/sysfs.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,33 @@ static ssize_t polarity_store(struct device *child,
208208
return ret ? : size;
209209
}
210210

211+
static ssize_t capture_show(struct device *child,
212+
struct device_attribute *attr,
213+
char *buf)
214+
{
215+
struct pwm_device *pwm = child_to_pwm_device(child);
216+
struct pwm_capture result;
217+
int ret;
218+
219+
ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
220+
if (ret)
221+
return ret;
222+
223+
return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
224+
}
225+
211226
static DEVICE_ATTR_RW(period);
212227
static DEVICE_ATTR_RW(duty_cycle);
213228
static DEVICE_ATTR_RW(enable);
214229
static DEVICE_ATTR_RW(polarity);
230+
static DEVICE_ATTR_RO(capture);
215231

216232
static struct attribute *pwm_attrs[] = {
217233
&dev_attr_period.attr,
218234
&dev_attr_duty_cycle.attr,
219235
&dev_attr_enable.attr,
220236
&dev_attr_polarity.attr,
237+
&dev_attr_capture.attr,
221238
NULL
222239
};
223240
ATTRIBUTE_GROUPS(pwm);

include/linux/pwm.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include <linux/mutex.h>
66
#include <linux/of.h>
77

8+
struct pwm_capture;
89
struct seq_file;
10+
911
struct pwm_chip;
1012

1113
/**
@@ -241,6 +243,7 @@ pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
241243
* @free: optional hook for freeing a PWM
242244
* @config: configure duty cycles and period length for this PWM
243245
* @set_polarity: configure the polarity of this PWM
246+
* @capture: capture and report PWM signal
244247
* @enable: enable PWM output toggling
245248
* @disable: disable PWM output toggling
246249
* @apply: atomically apply a new PWM config. The state argument
@@ -260,6 +263,8 @@ struct pwm_ops {
260263
int duty_ns, int period_ns);
261264
int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
262265
enum pwm_polarity polarity);
266+
int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
267+
struct pwm_capture *result, unsigned long timeout);
263268
int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
264269
void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
265270
int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
@@ -300,6 +305,16 @@ struct pwm_chip {
300305
bool can_sleep;
301306
};
302307

308+
/**
309+
* struct pwm_capture - PWM capture data
310+
* @period: period of the PWM signal (in nanoseconds)
311+
* @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
312+
*/
313+
struct pwm_capture {
314+
unsigned int period;
315+
unsigned int duty_cycle;
316+
};
317+
303318
#if IS_ENABLED(CONFIG_PWM)
304319
/* PWM user APIs */
305320
struct pwm_device *pwm_request(int pwm_id, const char *label);
@@ -412,6 +427,8 @@ static inline void pwm_disable(struct pwm_device *pwm)
412427
}
413428

414429
/* PWM provider APIs */
430+
int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
431+
unsigned long timeout);
415432
int pwm_set_chip_data(struct pwm_device *pwm, void *data);
416433
void *pwm_get_chip_data(struct pwm_device *pwm);
417434

@@ -463,6 +480,13 @@ static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
463480
return -EINVAL;
464481
}
465482

483+
static inline int pwm_capture(struct pwm_device *pwm,
484+
struct pwm_capture *result,
485+
unsigned long timeout)
486+
{
487+
return -EINVAL;
488+
}
489+
466490
static inline int pwm_set_polarity(struct pwm_device *pwm,
467491
enum pwm_polarity polarity)
468492
{

0 commit comments

Comments
 (0)