blob: 0b7f9599716b29a6d1f9d68fedaafe2caae648d4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shindec0c88532012-10-25 19:49:24 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * Rajeshwari Shinde <rajeshwari.s@samsung.com>
Rajeshwari Shindec0c88532012-10-25 19:49:24 +00005 */
6
7#include <common.h>
8#include <command.h>
Simon Glassd4901892018-12-10 10:37:36 -07009#include <dm.h>
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000010#include <fdtdec.h>
11#include <sound.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000013
14DECLARE_GLOBAL_DATA_PTR;
15
16/* Initilaise sound subsystem */
Simon Glass09140112020-05-10 11:40:03 -060017static int do_init(struct cmd_tbl *cmdtp, int flag, int argc,
18 char *const argv[])
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000019{
Simon Glassd4901892018-12-10 10:37:36 -070020 struct udevice *dev;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000021 int ret;
22
Simon Glassd4901892018-12-10 10:37:36 -070023 ret = uclass_first_device_err(UCLASS_SOUND, &dev);
24 if (!ret)
25 ret = sound_setup(dev);
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000026 if (ret) {
Simon Glassd4901892018-12-10 10:37:36 -070027 printf("Initialise Audio driver failed (ret=%d)\n", ret);
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000028 return CMD_RET_FAILURE;
29 }
30
31 return 0;
32}
33
34/* play sound from buffer */
Simon Glass09140112020-05-10 11:40:03 -060035static int do_play(struct cmd_tbl *cmdtp, int flag, int argc,
36 char *const argv[])
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000037{
Simon Glassd4901892018-12-10 10:37:36 -070038 struct udevice *dev;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000039 int ret = 0;
40 int msec = 1000;
41 int freq = 400;
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080042 bool first = true;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000043
Simon Glassd4901892018-12-10 10:37:36 -070044 ret = uclass_first_device_err(UCLASS_SOUND, &dev);
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080045 if (ret)
46 goto err;
47 --argc;
48 ++argv;
49 while (argc || first) {
50 first = false;
Heinrich Schuchardt532952f2022-12-31 00:49:10 +010051 if (argc) {
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080052 msec = dectoul(argv[0], NULL);
53 --argc;
54 ++argv;
55 }
Heinrich Schuchardt532952f2022-12-31 00:49:10 +010056 if (argc) {
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080057 freq = dectoul(argv[0], NULL);
58 --argc;
59 ++argv;
60 }
Simon Glassd4901892018-12-10 10:37:36 -070061 ret = sound_beep(dev, msec, freq);
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080062 if (ret)
63 goto err;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000064 }
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000065 return 0;
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080066
67err:
68 printf("Sound device failed to play (err=%d)\n", ret);
69 return CMD_RET_FAILURE;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000070}
71
Simon Glass09140112020-05-10 11:40:03 -060072static struct cmd_tbl cmd_sound_sub[] = {
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000073 U_BOOT_CMD_MKENT(init, 0, 1, do_init, "", ""),
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080074 U_BOOT_CMD_MKENT(play, INT_MAX, 1, do_play, "", ""),
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000075};
76
77/* process sound command */
Simon Glass09140112020-05-10 11:40:03 -060078static int do_sound(struct cmd_tbl *cmdtp, int flag, int argc,
79 char *const argv[])
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000080{
Simon Glass09140112020-05-10 11:40:03 -060081 struct cmd_tbl *c;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000082
83 if (argc < 1)
84 return CMD_RET_USAGE;
85
86 /* Strip off leading 'sound' command argument */
87 argc--;
88 argv++;
89
90 c = find_cmd_tbl(argv[0], &cmd_sound_sub[0], ARRAY_SIZE(cmd_sound_sub));
91
92 if (c)
93 return c->cmd(cmdtp, flag, argc, argv);
94 else
95 return CMD_RET_USAGE;
96}
97
98U_BOOT_CMD(
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080099 sound, INT_MAX, 1, do_sound,
Rajeshwari Shindec0c88532012-10-25 19:49:24 +0000100 "sound sub-system",
101 "init - initialise the sound driver\n"
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -0800102 "sound play [[[-q|-s] len [freq]] ...] - play sounds\n"
103 " len - duration in ms\n"
104 " freq - frequency in Hz\n"
Rajeshwari Shindec0c88532012-10-25 19:49:24 +0000105);