blob: c29200cf755add249d6a05b2fce8d91365d9b031 [file] [log] [blame]
Fabio Estevam6b24e032018-07-24 09:48:33 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright 2012 Freescale Semiconductor, Inc.
4// Copyright 2012 Linaro Ltd.
Shawn Guoc4483032012-03-16 16:56:44 +08005
6#include <linux/module.h>
7#include <linux/of.h>
8#include <linux/of_platform.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +01009#include <linux/i2c.h>
Richard Zhao81e8e492012-04-27 15:02:57 +080010#include <linux/clk.h>
Shawn Guoc4483032012-03-16 16:56:44 +080011#include <sound/soc.h>
12
13#include "../codecs/sgtl5000.h"
14#include "imx-audmux.h"
15
16#define DAI_NAME_SIZE 32
17
18struct imx_sgtl5000_data {
19 struct snd_soc_dai_link dai;
20 struct snd_soc_card card;
21 char codec_dai_name[DAI_NAME_SIZE];
22 char platform_name[DAI_NAME_SIZE];
Richard Zhao81e8e492012-04-27 15:02:57 +080023 struct clk *codec_clk;
Shawn Guoc4483032012-03-16 16:56:44 +080024 unsigned int clk_frequency;
25};
26
27static int imx_sgtl5000_dai_init(struct snd_soc_pcm_runtime *rtd)
28{
Shawn Guo47cf84e2014-02-08 13:20:35 +080029 struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(rtd->card);
Shawn Guoc4483032012-03-16 16:56:44 +080030 struct device *dev = rtd->card->dev;
31 int ret;
32
33 ret = snd_soc_dai_set_sysclk(rtd->codec_dai, SGTL5000_SYSCLK,
34 data->clk_frequency, SND_SOC_CLOCK_IN);
35 if (ret) {
36 dev_err(dev, "could not set codec driver clock params\n");
37 return ret;
38 }
39
40 return 0;
41}
42
Shawn Guo172b4c5c2012-03-30 00:13:03 +080043static const struct snd_soc_dapm_widget imx_sgtl5000_dapm_widgets[] = {
44 SND_SOC_DAPM_MIC("Mic Jack", NULL),
45 SND_SOC_DAPM_LINE("Line In Jack", NULL),
46 SND_SOC_DAPM_HP("Headphone Jack", NULL),
47 SND_SOC_DAPM_SPK("Line Out Jack", NULL),
48 SND_SOC_DAPM_SPK("Ext Spk", NULL),
49};
50
Bill Pembertona0a3d512012-12-07 09:26:16 -050051static int imx_sgtl5000_probe(struct platform_device *pdev)
Shawn Guoc4483032012-03-16 16:56:44 +080052{
53 struct device_node *np = pdev->dev.of_node;
54 struct device_node *ssi_np, *codec_np;
55 struct platform_device *ssi_pdev;
Richard Zhao81e8e492012-04-27 15:02:57 +080056 struct i2c_client *codec_dev;
Philipp Zabel50d4a792013-09-25 15:22:01 +020057 struct imx_sgtl5000_data *data = NULL;
Shawn Guoc4483032012-03-16 16:56:44 +080058 int int_port, ext_port;
59 int ret;
60
61 ret = of_property_read_u32(np, "mux-int-port", &int_port);
62 if (ret) {
63 dev_err(&pdev->dev, "mux-int-port missing or invalid\n");
64 return ret;
65 }
66 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
67 if (ret) {
68 dev_err(&pdev->dev, "mux-ext-port missing or invalid\n");
69 return ret;
70 }
71
72 /*
73 * The port numbering in the hardware manual starts at 1, while
74 * the audmux API expects it starts at 0.
75 */
76 int_port--;
77 ext_port--;
78 ret = imx_audmux_v2_configure_port(int_port,
79 IMX_AUDMUX_V2_PTCR_SYN |
80 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
81 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
82 IMX_AUDMUX_V2_PTCR_TFSDIR |
83 IMX_AUDMUX_V2_PTCR_TCLKDIR,
84 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
85 if (ret) {
86 dev_err(&pdev->dev, "audmux internal port setup failed\n");
87 return ret;
88 }
Julia Lawalldb8b6242012-08-19 09:02:52 +020089 ret = imx_audmux_v2_configure_port(ext_port,
Hui Wangef3207c2012-07-04 15:38:27 +080090 IMX_AUDMUX_V2_PTCR_SYN,
Shawn Guoc4483032012-03-16 16:56:44 +080091 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
92 if (ret) {
93 dev_err(&pdev->dev, "audmux external port setup failed\n");
94 return ret;
95 }
96
97 ssi_np = of_parse_phandle(pdev->dev.of_node, "ssi-controller", 0);
98 codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
99 if (!ssi_np || !codec_np) {
100 dev_err(&pdev->dev, "phandle missing or invalid\n");
Richard Zhao717071d2012-04-27 15:02:56 +0800101 ret = -EINVAL;
102 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800103 }
104
105 ssi_pdev = of_find_device_by_node(ssi_np);
106 if (!ssi_pdev) {
107 dev_err(&pdev->dev, "failed to find SSI platform device\n");
Arnaud Patard (Rtp)28e5ca72013-06-20 23:20:49 +0200108 ret = -EPROBE_DEFER;
Richard Zhao717071d2012-04-27 15:02:56 +0800109 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800110 }
Richard Zhao81e8e492012-04-27 15:02:57 +0800111 codec_dev = of_find_i2c_device_by_node(codec_np);
112 if (!codec_dev) {
113 dev_err(&pdev->dev, "failed to find codec platform device\n");
Arnaud Patard (Rtp)28e5ca72013-06-20 23:20:49 +0200114 return -EPROBE_DEFER;
Richard Zhao81e8e492012-04-27 15:02:57 +0800115 }
Shawn Guoc4483032012-03-16 16:56:44 +0800116
117 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Richard Zhao717071d2012-04-27 15:02:56 +0800118 if (!data) {
119 ret = -ENOMEM;
120 goto fail;
121 }
Shawn Guoc4483032012-03-16 16:56:44 +0800122
Philipp Zabela8b22c12013-09-25 15:21:08 +0200123 data->codec_clk = clk_get(&codec_dev->dev, NULL);
Wei Yongjunf1269ae2013-07-16 20:05:07 +0800124 if (IS_ERR(data->codec_clk)) {
125 ret = PTR_ERR(data->codec_clk);
Fabio Estevam9e13f342013-06-09 22:07:46 -0300126 goto fail;
Wei Yongjunf1269ae2013-07-16 20:05:07 +0800127 }
Fabio Estevam9e13f342013-06-09 22:07:46 -0300128
129 data->clk_frequency = clk_get_rate(data->codec_clk);
Shawn Guoc4483032012-03-16 16:56:44 +0800130
131 data->dai.name = "HiFi";
132 data->dai.stream_name = "HiFi";
133 data->dai.codec_dai_name = "sgtl5000";
134 data->dai.codec_of_node = codec_np;
Shawn Guofa659d82013-03-25 11:19:12 +0800135 data->dai.cpu_of_node = ssi_np;
Shawn Guobd41bc92013-04-25 11:18:46 +0800136 data->dai.platform_of_node = ssi_np;
Shawn Guoc4483032012-03-16 16:56:44 +0800137 data->dai.init = &imx_sgtl5000_dai_init;
138 data->dai.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
139 SND_SOC_DAIFMT_CBM_CFM;
140
141 data->card.dev = &pdev->dev;
142 ret = snd_soc_of_parse_card_name(&data->card, "model");
143 if (ret)
Fabio Estevam9e13f342013-06-09 22:07:46 -0300144 goto fail;
Shawn Guo172b4c5c2012-03-30 00:13:03 +0800145 ret = snd_soc_of_parse_audio_routing(&data->card, "audio-routing");
146 if (ret)
Fabio Estevam9e13f342013-06-09 22:07:46 -0300147 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800148 data->card.num_links = 1;
Lothar Waßmann29df43062012-11-22 13:31:08 +0100149 data->card.owner = THIS_MODULE;
Shawn Guoc4483032012-03-16 16:56:44 +0800150 data->card.dai_link = &data->dai;
Shawn Guo172b4c5c2012-03-30 00:13:03 +0800151 data->card.dapm_widgets = imx_sgtl5000_dapm_widgets;
152 data->card.num_dapm_widgets = ARRAY_SIZE(imx_sgtl5000_dapm_widgets);
Shawn Guoc4483032012-03-16 16:56:44 +0800153
Shawn Guo47cf84e2014-02-08 13:20:35 +0800154 platform_set_drvdata(pdev, &data->card);
155 snd_soc_card_set_drvdata(&data->card, data);
156
Sachin Kamat01984a42013-09-17 09:42:46 +0530157 ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
Shawn Guoc4483032012-03-16 16:56:44 +0800158 if (ret) {
159 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
Fabio Estevam9e13f342013-06-09 22:07:46 -0300160 goto fail;
Shawn Guoc4483032012-03-16 16:56:44 +0800161 }
162
Fabio Estevam2fc059f2013-04-24 11:54:43 -0300163 of_node_put(ssi_np);
164 of_node_put(codec_np);
165
166 return 0;
167
Richard Zhao717071d2012-04-27 15:02:56 +0800168fail:
Philipp Zabel50d4a792013-09-25 15:22:01 +0200169 if (data && !IS_ERR(data->codec_clk))
Philipp Zabela8b22c12013-09-25 15:21:08 +0200170 clk_put(data->codec_clk);
Fabio Estevam7e3bc3c2014-10-07 15:13:25 -0300171 of_node_put(ssi_np);
172 of_node_put(codec_np);
Shawn Guoc4483032012-03-16 16:56:44 +0800173
Richard Zhao717071d2012-04-27 15:02:56 +0800174 return ret;
Shawn Guoc4483032012-03-16 16:56:44 +0800175}
176
Bill Pembertona0a3d512012-12-07 09:26:16 -0500177static int imx_sgtl5000_remove(struct platform_device *pdev)
Shawn Guoc4483032012-03-16 16:56:44 +0800178{
Shawn Guo47cf84e2014-02-08 13:20:35 +0800179 struct snd_soc_card *card = platform_get_drvdata(pdev);
180 struct imx_sgtl5000_data *data = snd_soc_card_get_drvdata(card);
Shawn Guoc4483032012-03-16 16:56:44 +0800181
Philipp Zabela8b22c12013-09-25 15:21:08 +0200182 clk_put(data->codec_clk);
Shawn Guoc4483032012-03-16 16:56:44 +0800183
184 return 0;
185}
186
187static const struct of_device_id imx_sgtl5000_dt_ids[] = {
188 { .compatible = "fsl,imx-audio-sgtl5000", },
189 { /* sentinel */ }
190};
191MODULE_DEVICE_TABLE(of, imx_sgtl5000_dt_ids);
192
193static struct platform_driver imx_sgtl5000_driver = {
194 .driver = {
195 .name = "imx-sgtl5000",
Nicolin Chen1abe7292013-10-24 18:15:29 +0800196 .pm = &snd_soc_pm_ops,
Shawn Guoc4483032012-03-16 16:56:44 +0800197 .of_match_table = imx_sgtl5000_dt_ids,
198 },
199 .probe = imx_sgtl5000_probe,
Fabio Estevam4fa8dbc2013-09-30 22:17:37 -0300200 .remove = imx_sgtl5000_remove,
Shawn Guoc4483032012-03-16 16:56:44 +0800201};
202module_platform_driver(imx_sgtl5000_driver);
203
204MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
205MODULE_DESCRIPTION("Freescale i.MX SGTL5000 ASoC machine driver");
206MODULE_LICENSE("GPL v2");
207MODULE_ALIAS("platform:imx-sgtl5000");