1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2020 SiFive, Inc
6 * author: Pragnesh Patel <pragnesh.patel@sifive.com>
20 static int do_pwm(struct cmd_tbl *cmdtp, int flag, int argc,
23 const char *str_cmd, *str_channel = NULL, *str_enable = NULL;
24 const char *str_pwm = NULL, *str_period = NULL, *str_duty = NULL;
27 u32 channel, pwm_enable, pwm_dev, period_ns = 0, duty_ns = 0;
46 sub_cmd = PWM_SET_INVERT;
51 sub_cmd = PWM_SET_CONFIG;
56 sub_cmd = PWM_SET_ENABLE;
61 sub_cmd = PWM_SET_DISABLE;
69 pwm_dev = dectoul(str_pwm, NULL);
70 ret = uclass_get_device(UCLASS_PWM, pwm_dev, &dev);
72 printf("pwm: '%s' not found\n", str_pwm);
73 return cmd_process_error(cmdtp, ret);
77 channel = dectoul(str_channel, NULL);
81 if (sub_cmd == PWM_SET_INVERT) {
83 pwm_enable = dectoul(str_enable, NULL);
84 ret = pwm_set_invert(dev, channel, pwm_enable);
85 } else if (sub_cmd == PWM_SET_CONFIG) {
89 period_ns = dectoul(str_period, NULL);
92 duty_ns = dectoul(str_duty, NULL);
94 ret = pwm_set_config(dev, channel, period_ns, duty_ns);
95 } else if (sub_cmd == PWM_SET_ENABLE) {
96 ret = pwm_set_enable(dev, channel, 1);
97 } else if (sub_cmd == PWM_SET_DISABLE) {
98 ret = pwm_set_enable(dev, channel, 0);
102 printf("error(%d)\n", ret);
103 return CMD_RET_FAILURE;
106 return CMD_RET_SUCCESS;
109 U_BOOT_CMD(pwm, 6, 0, do_pwm,
110 "control pwm channels",
111 "invert <pwm_dev_num> <channel> <polarity> - invert polarity\n"
112 "pwm config <pwm_dev_num> <channel> <period_ns> <duty_ns> - config PWM\n"
113 "pwm enable <pwm_dev_num> <channel> - enable PWM output\n"
114 "pwm disable <pwm_dev_num> <channel> - disable PWM output\n"
115 "Note: All input values are in decimal");