blob: 8f67cbd96e17a90249b6a36ca898c7076445e19c [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
Rajeshwari Shindec0c88532012-10-25 19:49:24 +00007#include <command.h>
Simon Glassd4901892018-12-10 10:37:36 -07008#include <dm.h>
Rajeshwari Shindec0c88532012-10-25 19:49:24 +00009#include <fdtdec.h>
10#include <sound.h>
Simon Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000012
13DECLARE_GLOBAL_DATA_PTR;
14
15/* Initilaise sound subsystem */
Simon Glass09140112020-05-10 11:40:03 -060016static int do_init(struct cmd_tbl *cmdtp, int flag, int argc,
17 char *const argv[])
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000018{
Simon Glassd4901892018-12-10 10:37:36 -070019 struct udevice *dev;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000020 int ret;
21
Simon Glassd4901892018-12-10 10:37:36 -070022 ret = uclass_first_device_err(UCLASS_SOUND, &dev);
23 if (!ret)
24 ret = sound_setup(dev);
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000025 if (ret) {
Simon Glassd4901892018-12-10 10:37:36 -070026 printf("Initialise Audio driver failed (ret=%d)\n", ret);
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000027 return CMD_RET_FAILURE;
28 }
29
30 return 0;
31}
32
33/* play sound from buffer */
Simon Glass09140112020-05-10 11:40:03 -060034static int do_play(struct cmd_tbl *cmdtp, int flag, int argc,
35 char *const argv[])
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000036{
Simon Glassd4901892018-12-10 10:37:36 -070037 struct udevice *dev;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000038 int ret = 0;
39 int msec = 1000;
40 int freq = 400;
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080041 bool first = true;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000042
Simon Glassd4901892018-12-10 10:37:36 -070043 ret = uclass_first_device_err(UCLASS_SOUND, &dev);
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080044 if (ret)
45 goto err;
46 --argc;
47 ++argv;
48 while (argc || first) {
49 first = false;
Heinrich Schuchardt532952f2022-12-31 00:49:10 +010050 if (argc) {
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080051 msec = dectoul(argv[0], NULL);
52 --argc;
53 ++argv;
54 }
Heinrich Schuchardt532952f2022-12-31 00:49:10 +010055 if (argc) {
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080056 freq = dectoul(argv[0], NULL);
57 --argc;
58 ++argv;
59 }
Simon Glassd4901892018-12-10 10:37:36 -070060 ret = sound_beep(dev, msec, freq);
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080061 if (ret)
62 goto err;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000063 }
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000064 return 0;
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080065
66err:
67 printf("Sound device failed to play (err=%d)\n", ret);
68 return CMD_RET_FAILURE;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000069}
70
Simon Glass09140112020-05-10 11:40:03 -060071static struct cmd_tbl cmd_sound_sub[] = {
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000072 U_BOOT_CMD_MKENT(init, 0, 1, do_init, "", ""),
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080073 U_BOOT_CMD_MKENT(play, INT_MAX, 1, do_play, "", ""),
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000074};
75
76/* process sound command */
Simon Glass09140112020-05-10 11:40:03 -060077static int do_sound(struct cmd_tbl *cmdtp, int flag, int argc,
78 char *const argv[])
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000079{
Simon Glass09140112020-05-10 11:40:03 -060080 struct cmd_tbl *c;
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000081
82 if (argc < 1)
83 return CMD_RET_USAGE;
84
85 /* Strip off leading 'sound' command argument */
86 argc--;
87 argv++;
88
89 c = find_cmd_tbl(argv[0], &cmd_sound_sub[0], ARRAY_SIZE(cmd_sound_sub));
90
91 if (c)
92 return c->cmd(cmdtp, flag, argc, argv);
93 else
94 return CMD_RET_USAGE;
95}
96
97U_BOOT_CMD(
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -080098 sound, INT_MAX, 1, do_sound,
Rajeshwari Shindec0c88532012-10-25 19:49:24 +000099 "sound sub-system",
100 "init - initialise the sound driver\n"
Quentin Schulzca6a9922024-06-10 18:11:44 +0200101 "sound play [len [freq [len [freq ...]]]] - play sounds\n"
Heinrich Schuchardtea58b9a2022-12-15 16:50:39 -0800102 " len - duration in ms\n"
103 " freq - frequency in Hz\n"
Rajeshwari Shindec0c88532012-10-25 19:49:24 +0000104);