2020-12-22 06:00:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Control PWM channels
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 SiFive, Inc
|
|
|
|
* author: Pragnesh Patel <pragnesh.patel@sifive.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <command.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <pwm.h>
|
|
|
|
|
|
|
|
enum pwm_cmd {
|
|
|
|
PWM_SET_INVERT,
|
|
|
|
PWM_SET_CONFIG,
|
|
|
|
PWM_SET_ENABLE,
|
|
|
|
PWM_SET_DISABLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int do_pwm(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
|
|
|
{
|
|
|
|
const char *str_cmd, *str_channel = NULL, *str_enable = NULL;
|
|
|
|
const char *str_pwm = NULL, *str_period = NULL, *str_duty = NULL;
|
|
|
|
enum pwm_cmd sub_cmd;
|
|
|
|
struct udevice *dev;
|
|
|
|
u32 channel, pwm_enable, pwm_dev, period_ns = 0, duty_ns = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc < 4)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
str_cmd = argv[1];
|
|
|
|
argc -= 2;
|
|
|
|
argv += 2;
|
|
|
|
|
2021-01-26 16:44:37 +00:00
|
|
|
str_pwm = *argv;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2020-12-22 06:00:05 +00:00
|
|
|
|
|
|
|
if (!str_pwm)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
switch (*str_cmd) {
|
|
|
|
case 'i':
|
|
|
|
sub_cmd = PWM_SET_INVERT;
|
2021-01-26 16:44:37 +00:00
|
|
|
if (argc != 2)
|
|
|
|
return CMD_RET_USAGE;
|
2020-12-22 06:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
sub_cmd = PWM_SET_CONFIG;
|
2021-01-26 16:44:37 +00:00
|
|
|
if (argc != 3)
|
|
|
|
return CMD_RET_USAGE;
|
2020-12-22 06:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
sub_cmd = PWM_SET_ENABLE;
|
2021-01-26 16:44:37 +00:00
|
|
|
if (argc != 1)
|
|
|
|
return CMD_RET_USAGE;
|
2020-12-22 06:00:05 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
sub_cmd = PWM_SET_DISABLE;
|
2021-01-26 16:44:37 +00:00
|
|
|
if (argc != 1)
|
|
|
|
return CMD_RET_USAGE;
|
2020-12-22 06:00:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
|
2021-07-24 15:03:30 +00:00
|
|
|
pwm_dev = dectoul(str_pwm, NULL);
|
2020-12-22 06:00:05 +00:00
|
|
|
ret = uclass_get_device(UCLASS_PWM, pwm_dev, &dev);
|
|
|
|
if (ret) {
|
|
|
|
printf("pwm: '%s' not found\n", str_pwm);
|
|
|
|
return cmd_process_error(cmdtp, ret);
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:44:37 +00:00
|
|
|
str_channel = *argv;
|
2021-07-24 15:03:30 +00:00
|
|
|
channel = dectoul(str_channel, NULL);
|
2021-01-26 16:44:37 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
2020-12-22 06:00:05 +00:00
|
|
|
|
2021-01-26 16:44:37 +00:00
|
|
|
if (sub_cmd == PWM_SET_INVERT) {
|
2020-12-22 06:00:05 +00:00
|
|
|
str_enable = *argv;
|
2021-07-24 15:03:30 +00:00
|
|
|
pwm_enable = dectoul(str_enable, NULL);
|
2020-12-22 06:00:05 +00:00
|
|
|
ret = pwm_set_invert(dev, channel, pwm_enable);
|
2021-01-26 16:44:37 +00:00
|
|
|
} else if (sub_cmd == PWM_SET_CONFIG) {
|
2020-12-22 06:00:05 +00:00
|
|
|
str_period = *argv;
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2021-07-24 15:03:30 +00:00
|
|
|
period_ns = dectoul(str_period, NULL);
|
2020-12-22 06:00:05 +00:00
|
|
|
|
2021-01-26 16:44:37 +00:00
|
|
|
str_duty = *argv;
|
2021-07-24 15:03:30 +00:00
|
|
|
duty_ns = dectoul(str_duty, NULL);
|
2020-12-22 06:00:05 +00:00
|
|
|
|
|
|
|
ret = pwm_set_config(dev, channel, period_ns, duty_ns);
|
|
|
|
} else if (sub_cmd == PWM_SET_ENABLE) {
|
|
|
|
ret = pwm_set_enable(dev, channel, 1);
|
|
|
|
} else if (sub_cmd == PWM_SET_DISABLE) {
|
|
|
|
ret = pwm_set_enable(dev, channel, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
printf("error(%d)\n", ret);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_RET_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(pwm, 6, 0, do_pwm,
|
|
|
|
"control pwm channels",
|
2021-07-01 09:44:51 +00:00
|
|
|
"invert <pwm_dev_num> <channel> <polarity> - invert polarity\n"
|
|
|
|
"pwm config <pwm_dev_num> <channel> <period_ns> <duty_ns> - config PWM\n"
|
|
|
|
"pwm enable <pwm_dev_num> <channel> - enable PWM output\n"
|
2022-02-25 13:48:54 +00:00
|
|
|
"pwm disable <pwm_dev_num> <channel> - disable PWM output\n"
|
2020-12-22 06:00:05 +00:00
|
|
|
"Note: All input values are in decimal");
|