blob: 8f67cbd96e17a90249b6a36ca898c7076445e19c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shinde805adac2012-10-25 19:49:24 +00002/*
3 * Copyright (C) 2012 Samsung Electronics
4 * Rajeshwari Shinde <rajeshwari.s@samsung.com>
Rajeshwari Shinde805adac2012-10-25 19:49:24 +00005 */
6
Rajeshwari Shinde805adac2012-10-25 19:49:24 +00007#include <command.h>
Simon Glass76072ac2018-12-10 10:37:36 -07008#include <dm.h>
Rajeshwari Shinde805adac2012-10-25 19:49:24 +00009#include <fdtdec.h>
10#include <sound.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000012
13DECLARE_GLOBAL_DATA_PTR;
14
15/* Initilaise sound subsystem */
Simon Glassed38aef2020-05-10 11:40:03 -060016static int do_init(struct cmd_tbl *cmdtp, int flag, int argc,
17 char *const argv[])
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000018{
Simon Glass76072ac2018-12-10 10:37:36 -070019 struct udevice *dev;
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000020 int ret;
21
Simon Glass76072ac2018-12-10 10:37:36 -070022 ret = uclass_first_device_err(UCLASS_SOUND, &dev);
23 if (!ret)
24 ret = sound_setup(dev);
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000025 if (ret) {
Simon Glass76072ac2018-12-10 10:37:36 -070026 printf("Initialise Audio driver failed (ret=%d)\n", ret);
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000027 return CMD_RET_FAILURE;
28 }
29
30 return 0;
31}
32
33/* play sound from buffer */
Simon Glassed38aef2020-05-10 11:40:03 -060034static int do_play(struct cmd_tbl *cmdtp, int flag, int argc,
35 char *const argv[])
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000036{
Simon Glass76072ac2018-12-10 10:37:36 -070037 struct udevice *dev;
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000038 int ret = 0;
39 int msec = 1000;
40 int freq = 400;
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080041 bool first = true;
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000042
Simon Glass76072ac2018-12-10 10:37:36 -070043 ret = uclass_first_device_err(UCLASS_SOUND, &dev);
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080044 if (ret)
45 goto err;
46 --argc;
47 ++argv;
48 while (argc || first) {
49 first = false;
Heinrich Schuchardt75272c02022-12-31 00:49:10 +010050 if (argc) {
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080051 msec = dectoul(argv[0], NULL);
52 --argc;
53 ++argv;
54 }
Heinrich Schuchardt75272c02022-12-31 00:49:10 +010055 if (argc) {
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080056 freq = dectoul(argv[0], NULL);
57 --argc;
58 ++argv;
59 }
Simon Glass76072ac2018-12-10 10:37:36 -070060 ret = sound_beep(dev, msec, freq);
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080061 if (ret)
62 goto err;
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000063 }
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000064 return 0;
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080065
66err:
67 printf("Sound device failed to play (err=%d)\n", ret);
68 return CMD_RET_FAILURE;
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000069}
70
Simon Glassed38aef2020-05-10 11:40:03 -060071static struct cmd_tbl cmd_sound_sub[] = {
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000072 U_BOOT_CMD_MKENT(init, 0, 1, do_init, "", ""),
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -080073 U_BOOT_CMD_MKENT(play, INT_MAX, 1, do_play, "", ""),
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000074};
75
76/* process sound command */
Simon Glassed38aef2020-05-10 11:40:03 -060077static int do_sound(struct cmd_tbl *cmdtp, int flag, int argc,
78 char *const argv[])
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000079{
Simon Glassed38aef2020-05-10 11:40:03 -060080 struct cmd_tbl *c;
Rajeshwari Shinde805adac2012-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 Schuchardtd5f527a2022-12-15 16:50:39 -080098 sound, INT_MAX, 1, do_sound,
Rajeshwari Shinde805adac2012-10-25 19:49:24 +000099 "sound sub-system",
100 "init - initialise the sound driver\n"
Quentin Schulz51027c72024-06-10 18:11:44 +0200101 "sound play [len [freq [len [freq ...]]]] - play sounds\n"
Heinrich Schuchardtd5f527a2022-12-15 16:50:39 -0800102 " len - duration in ms\n"
103 " freq - frequency in Hz\n"
Rajeshwari Shinde805adac2012-10-25 19:49:24 +0000104);