Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <pwm.h> |
| 11 | #include <asm/test.h> |
| 12 | |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 13 | enum { |
| 14 | NUM_CHANNELS = 3, |
| 15 | }; |
| 16 | |
Simon Glass | 5620cf8 | 2018-10-01 12:22:40 -0600 | [diff] [blame] | 17 | /** |
| 18 | * struct sandbox_pwm_chan - a sandbox PWM channel |
| 19 | * |
| 20 | * @period_ns: Period of the PWM in nanoseconds |
| 21 | * @duty_ns: Current duty cycle of the PWM in nanoseconds |
| 22 | * @enable: true if the PWM is enabled |
| 23 | * @polarity: true if the PWM polarity is active high |
| 24 | */ |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 25 | struct sandbox_pwm_chan { |
| 26 | uint period_ns; |
| 27 | uint duty_ns; |
| 28 | bool enable; |
Kever Yang | 809c3e6 | 2017-04-24 10:27:52 +0800 | [diff] [blame] | 29 | bool polarity; |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct sandbox_pwm_priv { |
| 33 | struct sandbox_pwm_chan chan[NUM_CHANNELS]; |
| 34 | }; |
| 35 | |
Simon Glass | 5620cf8 | 2018-10-01 12:22:40 -0600 | [diff] [blame] | 36 | int sandbox_pwm_get_config(struct udevice *dev, uint channel, uint *period_nsp, |
| 37 | uint *duty_nsp, bool *enablep, bool *polarityp) |
| 38 | { |
| 39 | struct sandbox_pwm_priv *priv = dev_get_priv(dev); |
| 40 | struct sandbox_pwm_chan *chan; |
| 41 | |
| 42 | if (channel >= NUM_CHANNELS) |
| 43 | return -ENOSPC; |
| 44 | chan = &priv->chan[channel]; |
| 45 | *period_nsp = chan->period_ns; |
| 46 | *duty_nsp = chan->duty_ns; |
| 47 | *enablep = chan->enable; |
| 48 | *polarityp = chan->polarity; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 53 | static int sandbox_pwm_set_config(struct udevice *dev, uint channel, |
| 54 | uint period_ns, uint duty_ns) |
| 55 | { |
| 56 | struct sandbox_pwm_priv *priv = dev_get_priv(dev); |
| 57 | struct sandbox_pwm_chan *chan; |
| 58 | |
| 59 | if (channel >= NUM_CHANNELS) |
| 60 | return -ENOSPC; |
| 61 | chan = &priv->chan[channel]; |
| 62 | chan->period_ns = period_ns; |
| 63 | chan->duty_ns = duty_ns; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int sandbox_pwm_set_enable(struct udevice *dev, uint channel, |
| 69 | bool enable) |
| 70 | { |
| 71 | struct sandbox_pwm_priv *priv = dev_get_priv(dev); |
| 72 | struct sandbox_pwm_chan *chan; |
| 73 | |
| 74 | if (channel >= NUM_CHANNELS) |
| 75 | return -ENOSPC; |
| 76 | chan = &priv->chan[channel]; |
| 77 | chan->enable = enable; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Kever Yang | 809c3e6 | 2017-04-24 10:27:52 +0800 | [diff] [blame] | 82 | static int sandbox_pwm_set_invert(struct udevice *dev, uint channel, |
| 83 | bool polarity) |
| 84 | { |
| 85 | struct sandbox_pwm_priv *priv = dev_get_priv(dev); |
| 86 | struct sandbox_pwm_chan *chan; |
| 87 | |
| 88 | if (channel >= NUM_CHANNELS) |
| 89 | return -ENOSPC; |
| 90 | chan = &priv->chan[channel]; |
| 91 | chan->polarity = polarity; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 96 | static const struct pwm_ops sandbox_pwm_ops = { |
| 97 | .set_config = sandbox_pwm_set_config, |
| 98 | .set_enable = sandbox_pwm_set_enable, |
Kever Yang | 809c3e6 | 2017-04-24 10:27:52 +0800 | [diff] [blame] | 99 | .set_invert = sandbox_pwm_set_invert, |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static const struct udevice_id sandbox_pwm_ids[] = { |
| 103 | { .compatible = "sandbox,pwm" }, |
| 104 | { } |
| 105 | }; |
| 106 | |
| 107 | U_BOOT_DRIVER(warm_pwm_sandbox) = { |
| 108 | .name = "pwm_sandbox", |
| 109 | .id = UCLASS_PWM, |
| 110 | .of_match = sandbox_pwm_ids, |
| 111 | .ops = &sandbox_pwm_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 112 | .priv_auto = sizeof(struct sandbox_pwm_priv), |
Simon Glass | e62f4be | 2017-04-16 21:01:11 -0600 | [diff] [blame] | 113 | }; |