Unfortunately a test for the PWM uclass was not included when it was submitted. This was noticed when trying to add more functionality: http://patchwork.ozlabs.org/patch/748172/ Add a simple test to get us started. Signed-off-by: Simon Glass <sjg@chromium.org>master
parent
29f089a605
commit
43b41566f7
@ -0,0 +1,75 @@ |
||||
/*
|
||||
* Copyright (c) 2015 Google, Inc |
||||
* Written by Simon Glass <sjg@chromium.org> |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <common.h> |
||||
#include <dm.h> |
||||
#include <errno.h> |
||||
#include <pwm.h> |
||||
#include <asm/test.h> |
||||
|
||||
DECLARE_GLOBAL_DATA_PTR; |
||||
|
||||
enum { |
||||
NUM_CHANNELS = 3, |
||||
}; |
||||
|
||||
struct sandbox_pwm_chan { |
||||
uint period_ns; |
||||
uint duty_ns; |
||||
bool enable; |
||||
}; |
||||
|
||||
struct sandbox_pwm_priv { |
||||
struct sandbox_pwm_chan chan[NUM_CHANNELS]; |
||||
}; |
||||
|
||||
static int sandbox_pwm_set_config(struct udevice *dev, uint channel, |
||||
uint period_ns, uint duty_ns) |
||||
{ |
||||
struct sandbox_pwm_priv *priv = dev_get_priv(dev); |
||||
struct sandbox_pwm_chan *chan; |
||||
|
||||
if (channel >= NUM_CHANNELS) |
||||
return -ENOSPC; |
||||
chan = &priv->chan[channel]; |
||||
chan->period_ns = period_ns; |
||||
chan->duty_ns = duty_ns; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int sandbox_pwm_set_enable(struct udevice *dev, uint channel, |
||||
bool enable) |
||||
{ |
||||
struct sandbox_pwm_priv *priv = dev_get_priv(dev); |
||||
struct sandbox_pwm_chan *chan; |
||||
|
||||
if (channel >= NUM_CHANNELS) |
||||
return -ENOSPC; |
||||
chan = &priv->chan[channel]; |
||||
chan->enable = enable; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static const struct pwm_ops sandbox_pwm_ops = { |
||||
.set_config = sandbox_pwm_set_config, |
||||
.set_enable = sandbox_pwm_set_enable, |
||||
}; |
||||
|
||||
static const struct udevice_id sandbox_pwm_ids[] = { |
||||
{ .compatible = "sandbox,pwm" }, |
||||
{ } |
||||
}; |
||||
|
||||
U_BOOT_DRIVER(warm_pwm_sandbox) = { |
||||
.name = "pwm_sandbox", |
||||
.id = UCLASS_PWM, |
||||
.of_match = sandbox_pwm_ids, |
||||
.ops = &sandbox_pwm_ops, |
||||
.priv_auto_alloc_size = sizeof(struct sandbox_pwm_priv), |
||||
}; |
@ -0,0 +1,32 @@ |
||||
/*
|
||||
* Copyright (C) 2017 Google, Inc |
||||
* |
||||
* SPDX-License-Identifier: GPL-2.0+ |
||||
*/ |
||||
|
||||
#include <common.h> |
||||
#include <dm.h> |
||||
#include <pwm.h> |
||||
#include <dm/test.h> |
||||
#include <test/ut.h> |
||||
|
||||
DECLARE_GLOBAL_DATA_PTR; |
||||
|
||||
/* Basic test of the pwm uclass */ |
||||
static int dm_test_pwm_base(struct unit_test_state *uts) |
||||
{ |
||||
struct udevice *dev; |
||||
|
||||
ut_assertok(uclass_get_device(UCLASS_PWM, 0, &dev)); |
||||
ut_assertok(pwm_set_config(dev, 0, 100, 50)); |
||||
ut_assertok(pwm_set_enable(dev, 0, true)); |
||||
ut_assertok(pwm_set_enable(dev, 1, true)); |
||||
ut_assertok(pwm_set_enable(dev, 2, true)); |
||||
ut_asserteq(-ENOSPC, pwm_set_enable(dev, 3, true)); |
||||
|
||||
ut_assertok(uclass_get_device(UCLASS_PWM, 1, &dev)); |
||||
ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PWM, 2, &dev)); |
||||
|
||||
return 0; |
||||
} |
||||
DM_TEST(dm_test_pwm_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Loading…
Reference in new issue