Commit fc760cb8c4cc16061079fb45e2e0bad11e5acedc

Authored by Simon Glass
1 parent d4bf91ada1

dm: pwm: Add a PWM uclass

Add a uclass that supports Pulse Width Modulation (PWM) devices. It
provides methods to enable/disable and configure the device.

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 6 changed files with 103 additions and 0 deletions Side-by-side Diff

... ... @@ -46,6 +46,8 @@
46 46  
47 47 source "drivers/power/Kconfig"
48 48  
  49 +source "drivers/pwm/Kconfig"
  50 +
49 51 source "drivers/ram/Kconfig"
50 52  
51 53 source "drivers/remoteproc/Kconfig"
  1 +config DM_PWM
  2 + bool "Enable support for pulse-width modulation devices (PWM)"
  3 + depends on DM
  4 + help
  5 + A pulse-width modulator emits a pulse of varying width and provides
  6 + control over the duty cycle (high and low time) of the signal. This
  7 + is often used to control a voltage level. The more time the PWM
  8 + spends in the 'high' state, the higher the voltage. The PWM's
  9 + frequency/period can be controlled along with the proportion of that
  10 + time that the signal is high.
drivers/pwm/Makefile
... ... @@ -10,5 +10,6 @@
10 10  
11 11 #ccflags-y += -DDEBUG
12 12  
  13 +obj-$(CONFIG_DM_PWM) += pwm-uclass.o
13 14 obj-$(CONFIG_PWM_IMX) += pwm-imx.o pwm-imx-util.o
drivers/pwm/pwm-uclass.c
  1 +/*
  2 + * Copyright (c) 2016 Google, Inc
  3 + * Written by Simon Glass <sjg@chromium.org>
  4 + *
  5 + * SPDX-License-Identifier: GPL-2.0+
  6 + */
  7 +
  8 +#include <common.h>
  9 +#include <dm.h>
  10 +#include <pwm.h>
  11 +
  12 +int pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
  13 + uint duty_ns)
  14 +{
  15 + struct pwm_ops *ops = pwm_get_ops(dev);
  16 +
  17 + if (!ops->set_config)
  18 + return -ENOSYS;
  19 +
  20 + return ops->set_config(dev, channel, period_ns, duty_ns);
  21 +}
  22 +
  23 +int pwm_set_enable(struct udevice *dev, uint channel, bool enable)
  24 +{
  25 + struct pwm_ops *ops = pwm_get_ops(dev);
  26 +
  27 + if (!ops->set_enable)
  28 + return -ENOSYS;
  29 +
  30 + return ops->set_enable(dev, channel, enable);
  31 +}
  32 +
  33 +UCLASS_DRIVER(pwm) = {
  34 + .id = UCLASS_PWM,
  35 + .name = "pwm",
  36 +};
include/dm/uclass-id.h
... ... @@ -51,6 +51,7 @@
51 51 UCLASS_PINCTRL, /* Pinctrl (pin muxing/configuration) device */
52 52 UCLASS_PINCONFIG, /* Pin configuration node device */
53 53 UCLASS_PMIC, /* PMIC I/O device */
  54 + UCLASS_PWM, /* Pulse-width modulator */
54 55 UCLASS_PWRSEQ, /* Power sequence device */
55 56 UCLASS_REGULATOR, /* Regulator device */
56 57 UCLASS_RESET, /* Reset device */
1 1 /*
2 2 * header file for pwm driver.
3 3 *
  4 + * Copyright 2016 Google Inc.
4 5 * Copyright (c) 2011 samsung electronics
5 6 * Donghwa Lee <dh09.lee@samsung.com>
6 7 *
7 8  
... ... @@ -10,10 +11,62 @@
10 11 #ifndef _pwm_h_
11 12 #define _pwm_h_
12 13  
  14 +/* struct pwm_ops: Operations for the PWM uclass */
  15 +struct pwm_ops {
  16 + /**
  17 + * set_config() - Set the PWM configuration
  18 + *
  19 + * @dev: PWM device to update
  20 + * @channel: PWM channel to update
  21 + * @period_ns: PWM period in nanoseconds
  22 + * @duty_ns: PWM duty period in nanoseconds
  23 + * @return 0 if OK, -ve on error
  24 + */
  25 + int (*set_config)(struct udevice *dev, uint channel, uint period_ns,
  26 + uint duty_ns);
  27 +
  28 + /**
  29 + * set_enable() - Enable or disable the PWM
  30 + *
  31 + * @dev: PWM device to update
  32 + * @channel: PWM channel to update
  33 + * @enable: true to enable, false to disable
  34 + * @return 0 if OK, -ve on error
  35 + */
  36 + int (*set_enable)(struct udevice *dev, uint channel, bool enable);
  37 +};
  38 +
  39 +#define pwm_get_ops(dev) ((struct pwm_ops *)(dev)->driver->ops)
  40 +
  41 +/**
  42 + * pwm_set_config() - Set the PWM configuration
  43 + *
  44 + * @dev: PWM device to update
  45 + * @channel: PWM channel to update
  46 + * @period_ns: PWM period in nanoseconds
  47 + * @duty_ns: PWM duty period in nanoseconds
  48 + * @return 0 if OK, -ve on error
  49 + */
  50 +int pwm_set_config(struct udevice *dev, uint channel, uint period_ns,
  51 + uint duty_ns);
  52 +
  53 +/**
  54 + * pwm_set_enable() - Enable or disable the PWM
  55 + *
  56 + * @dev: PWM device to update
  57 + * @channel: PWM channel to update
  58 + * @enable: true to enable, false to disable
  59 + * @return 0 if OK, -ve on error
  60 + */
  61 +int pwm_set_enable(struct udevice *dev, uint channel, bool enable);
  62 +
  63 +/* Legacy interface */
  64 +#ifndef CONFIG_DM_PWM
13 65 int pwm_init (int pwm_id, int div, int invert);
14 66 int pwm_config (int pwm_id, int duty_ns, int period_ns);
15 67 int pwm_enable (int pwm_id);
16 68 void pwm_disable (int pwm_id);
  69 +#endif
17 70  
18 71 #endif /* _pwm_h_ */