Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
0004  *
0005  * storm.c -- ALSA SoC machine driver for QTi ipq806x-based Storm board
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/mod_devicetable.h>
0012 #include <linux/platform_device.h>
0013 #include <sound/pcm.h>
0014 #include <sound/pcm_params.h>
0015 #include <sound/soc.h>
0016 
0017 #define STORM_SYSCLK_MULT           4
0018 
0019 static int storm_ops_hw_params(struct snd_pcm_substream *substream,
0020         struct snd_pcm_hw_params *params)
0021 {
0022     struct snd_soc_pcm_runtime *soc_runtime = asoc_substream_to_rtd(substream);
0023     struct snd_soc_card *card = soc_runtime->card;
0024     snd_pcm_format_t format = params_format(params);
0025     unsigned int rate = params_rate(params);
0026     unsigned int sysclk_freq;
0027     int bitwidth, ret;
0028 
0029     bitwidth = snd_pcm_format_width(format);
0030     if (bitwidth < 0) {
0031         dev_err(card->dev, "invalid bit width given: %d\n", bitwidth);
0032         return bitwidth;
0033     }
0034 
0035     /*
0036      * as the CPU DAI is the I2S bus master and no system clock is needed by
0037      * the MAX98357a DAC, simply set the system clock to be a constant
0038      * multiple of the bit clock for the clock divider
0039      */
0040     sysclk_freq = rate * bitwidth * 2 * STORM_SYSCLK_MULT;
0041 
0042     ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(soc_runtime, 0), 0, sysclk_freq, 0);
0043     if (ret) {
0044         dev_err(card->dev, "error setting sysclk to %u: %d\n",
0045             sysclk_freq, ret);
0046         return ret;
0047     }
0048 
0049     return 0;
0050 }
0051 
0052 static const struct snd_soc_ops storm_soc_ops = {
0053     .hw_params  = storm_ops_hw_params,
0054 };
0055 
0056 SND_SOC_DAILINK_DEFS(hifi,
0057     DAILINK_COMP_ARRAY(COMP_EMPTY()),
0058     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
0059     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0060 
0061 static struct snd_soc_dai_link storm_dai_link = {
0062     .name       = "Primary",
0063     .stream_name    = "Primary",
0064     .ops        = &storm_soc_ops,
0065     SND_SOC_DAILINK_REG(hifi),
0066 };
0067 
0068 static int storm_parse_of(struct snd_soc_card *card)
0069 {
0070     struct snd_soc_dai_link *dai_link = card->dai_link;
0071     struct device_node *np = card->dev->of_node;
0072 
0073     dai_link->cpus->of_node = of_parse_phandle(np, "cpu", 0);
0074     if (!dai_link->cpus->of_node) {
0075         dev_err(card->dev, "error getting cpu phandle\n");
0076         return -EINVAL;
0077     }
0078     dai_link->platforms->of_node = dai_link->cpus->of_node;
0079 
0080     dai_link->codecs->of_node = of_parse_phandle(np, "codec", 0);
0081     if (!dai_link->codecs->of_node) {
0082         dev_err(card->dev, "error getting codec phandle\n");
0083         return -EINVAL;
0084     }
0085 
0086     return 0;
0087 }
0088 
0089 static int storm_platform_probe(struct platform_device *pdev)
0090 {
0091     struct snd_soc_card *card;
0092     int ret;
0093 
0094     card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
0095     if (!card)
0096         return -ENOMEM;
0097 
0098     card->dev = &pdev->dev;
0099     card->owner = THIS_MODULE;
0100 
0101     ret = snd_soc_of_parse_card_name(card, "qcom,model");
0102     if (ret) {
0103         dev_err(&pdev->dev, "error parsing card name: %d\n", ret);
0104         return ret;
0105     }
0106 
0107     card->dai_link  = &storm_dai_link;
0108     card->num_links = 1;
0109 
0110     ret = storm_parse_of(card);
0111     if (ret) {
0112         dev_err(&pdev->dev, "error resolving dai links: %d\n", ret);
0113         return ret;
0114     }
0115 
0116     ret = devm_snd_soc_register_card(&pdev->dev, card);
0117     if (ret)
0118         dev_err(&pdev->dev, "error registering soundcard: %d\n", ret);
0119 
0120     return ret;
0121 
0122 }
0123 
0124 #ifdef CONFIG_OF
0125 static const struct of_device_id storm_device_id[]  = {
0126     { .compatible = "google,storm-audio" },
0127     {},
0128 };
0129 MODULE_DEVICE_TABLE(of, storm_device_id);
0130 #endif
0131 
0132 static struct platform_driver storm_platform_driver = {
0133     .driver = {
0134         .name = "storm-audio",
0135         .of_match_table =
0136             of_match_ptr(storm_device_id),
0137     },
0138     .probe = storm_platform_probe,
0139 };
0140 module_platform_driver(storm_platform_driver);
0141 
0142 MODULE_DESCRIPTION("QTi IPQ806x-based Storm Machine Driver");
0143 MODULE_LICENSE("GPL v2");