blob: bf8084f2e6257f6123849bd9ecc50c9743aaecca [file] [log] [blame]
Philipp Zabel73772262009-03-19 09:34:46 +01001/*
2 * SoC audio for HTC Magician
3 *
4 * Copyright (c) 2006 Philipp Zabel <philipp.zabel@gmail.com>
5 *
6 * based on spitz.c,
7 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
8 * Richard Purdie <richard@openedhand.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/timer.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/delay.h>
22#include <linux/gpio.h>
Philipp Zabel1abd9182009-06-15 22:18:23 +020023#include <linux/i2c.h>
Philipp Zabel73772262009-03-19 09:34:46 +010024
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Philipp Zabel1abd9182009-06-15 22:18:23 +020029#include <sound/uda1380.h>
Philipp Zabel73772262009-03-19 09:34:46 +010030
Philipp Zabel73772262009-03-19 09:34:46 +010031#include <mach/magician.h>
32#include <asm/mach-types.h>
33#include "../codecs/uda1380.h"
Philipp Zabel73772262009-03-19 09:34:46 +010034#include "pxa2xx-i2s.h"
35#include "pxa-ssp.h"
36
37#define MAGICIAN_MIC 0
38#define MAGICIAN_MIC_EXT 1
39
40static int magician_hp_switch;
41static int magician_spk_switch = 1;
42static int magician_in_sel = MAGICIAN_MIC;
43
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +010044static void magician_ext_control(struct snd_soc_dapm_context *dapm)
Philipp Zabel73772262009-03-19 09:34:46 +010045{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020046
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000047 snd_soc_dapm_mutex_lock(dapm);
48
Philipp Zabel73772262009-03-19 09:34:46 +010049 if (magician_spk_switch)
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000050 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
Philipp Zabel73772262009-03-19 09:34:46 +010051 else
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000052 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
Philipp Zabel73772262009-03-19 09:34:46 +010053 if (magician_hp_switch)
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000054 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
Philipp Zabel73772262009-03-19 09:34:46 +010055 else
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000056 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
Philipp Zabel73772262009-03-19 09:34:46 +010057
58 switch (magician_in_sel) {
59 case MAGICIAN_MIC:
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000060 snd_soc_dapm_disable_pin_unlocked(dapm, "Headset Mic");
61 snd_soc_dapm_enable_pin_unlocked(dapm, "Call Mic");
Philipp Zabel73772262009-03-19 09:34:46 +010062 break;
63 case MAGICIAN_MIC_EXT:
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000064 snd_soc_dapm_disable_pin_unlocked(dapm, "Call Mic");
65 snd_soc_dapm_enable_pin_unlocked(dapm, "Headset Mic");
Philipp Zabel73772262009-03-19 09:34:46 +010066 break;
67 }
68
Charles Keepaxf71b0fb2014-02-18 15:22:26 +000069 snd_soc_dapm_sync_unlocked(dapm);
70
71 snd_soc_dapm_mutex_unlock(dapm);
Philipp Zabel73772262009-03-19 09:34:46 +010072}
73
74static int magician_startup(struct snd_pcm_substream *substream)
75{
76 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Philipp Zabel73772262009-03-19 09:34:46 +010077
78 /* check the jack status at stream startup */
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +010079 magician_ext_control(&rtd->card->dapm);
Philipp Zabel73772262009-03-19 09:34:46 +010080
81 return 0;
82}
83
84/*
85 * Magician uses SSP port for playback.
86 */
87static int magician_playback_hw_params(struct snd_pcm_substream *substream,
88 struct snd_pcm_hw_params *params)
89{
90 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000091 struct snd_soc_dai *codec_dai = rtd->codec_dai;
92 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Daniel Macka8e43c22018-07-16 08:24:45 +020093 unsigned int width;
Philipp Zabel73772262009-03-19 09:34:46 +010094 int ret = 0;
95
Philipp Zabel73772262009-03-19 09:34:46 +010096 /* set codec DAI configuration */
97 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_MSB |
98 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
99 if (ret < 0)
100 return ret;
101
102 /* set cpu DAI configuration */
103 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
Philipp Zabel33745fb2009-06-14 12:51:30 +0200104 SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_CBS_CFS);
Philipp Zabel73772262009-03-19 09:34:46 +0100105 if (ret < 0)
106 return ret;
107
Daniel Macka8e43c22018-07-16 08:24:45 +0200108 width = snd_pcm_format_physical_width(params_format(params));
Daniel Ribeiroa5479e32009-06-15 21:44:31 -0300109 ret = snd_soc_dai_set_tdm_slot(cpu_dai, 1, 0, 1, width);
Philipp Zabel73772262009-03-19 09:34:46 +0100110 if (ret < 0)
111 return ret;
112
113 /* set audio clock as clock source */
114 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0,
115 SND_SOC_CLOCK_OUT);
116 if (ret < 0)
117 return ret;
118
Philipp Zabel73772262009-03-19 09:34:46 +0100119 return 0;
120}
121
122/*
123 * Magician uses I2S for capture.
124 */
125static int magician_capture_hw_params(struct snd_pcm_substream *substream,
126 struct snd_pcm_hw_params *params)
127{
128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000129 struct snd_soc_dai *codec_dai = rtd->codec_dai;
130 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Philipp Zabel73772262009-03-19 09:34:46 +0100131 int ret = 0;
132
133 /* set codec DAI configuration */
134 ret = snd_soc_dai_set_fmt(codec_dai,
135 SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
136 SND_SOC_DAIFMT_CBS_CFS);
137 if (ret < 0)
138 return ret;
139
140 /* set cpu DAI configuration */
141 ret = snd_soc_dai_set_fmt(cpu_dai,
142 SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF |
143 SND_SOC_DAIFMT_CBS_CFS);
144 if (ret < 0)
145 return ret;
146
147 /* set the I2S system clock as output */
148 ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0,
149 SND_SOC_CLOCK_OUT);
150 if (ret < 0)
151 return ret;
152
153 return 0;
154}
155
Bhumika Goyal943b7312017-03-14 01:16:40 +0530156static const struct snd_soc_ops magician_capture_ops = {
Philipp Zabel73772262009-03-19 09:34:46 +0100157 .startup = magician_startup,
158 .hw_params = magician_capture_hw_params,
159};
160
Bhumika Goyal943b7312017-03-14 01:16:40 +0530161static const struct snd_soc_ops magician_playback_ops = {
Philipp Zabel73772262009-03-19 09:34:46 +0100162 .startup = magician_startup,
163 .hw_params = magician_playback_hw_params,
164};
165
166static int magician_get_hp(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
168{
169 ucontrol->value.integer.value[0] = magician_hp_switch;
170 return 0;
171}
172
173static int magician_set_hp(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +0100176 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Philipp Zabel73772262009-03-19 09:34:46 +0100177
178 if (magician_hp_switch == ucontrol->value.integer.value[0])
179 return 0;
180
181 magician_hp_switch = ucontrol->value.integer.value[0];
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +0100182 magician_ext_control(&card->dapm);
Philipp Zabel73772262009-03-19 09:34:46 +0100183 return 1;
184}
185
186static int magician_get_spk(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
189 ucontrol->value.integer.value[0] = magician_spk_switch;
190 return 0;
191}
192
193static int magician_set_spk(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
195{
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +0100196 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
Philipp Zabel73772262009-03-19 09:34:46 +0100197
198 if (magician_spk_switch == ucontrol->value.integer.value[0])
199 return 0;
200
201 magician_spk_switch = ucontrol->value.integer.value[0];
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +0100202 magician_ext_control(&card->dapm);
Philipp Zabel73772262009-03-19 09:34:46 +0100203 return 1;
204}
205
206static int magician_get_input(struct snd_kcontrol *kcontrol,
207 struct snd_ctl_elem_value *ucontrol)
208{
Takashi Iwai127ee192016-02-29 17:23:50 +0100209 ucontrol->value.enumerated.item[0] = magician_in_sel;
Philipp Zabel73772262009-03-19 09:34:46 +0100210 return 0;
211}
212
213static int magician_set_input(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
215{
Takashi Iwai127ee192016-02-29 17:23:50 +0100216 if (magician_in_sel == ucontrol->value.enumerated.item[0])
Philipp Zabel73772262009-03-19 09:34:46 +0100217 return 0;
218
Takashi Iwai127ee192016-02-29 17:23:50 +0100219 magician_in_sel = ucontrol->value.enumerated.item[0];
Philipp Zabel73772262009-03-19 09:34:46 +0100220
221 switch (magician_in_sel) {
222 case MAGICIAN_MIC:
223 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 1);
224 break;
225 case MAGICIAN_MIC_EXT:
226 gpio_set_value(EGPIO_MAGICIAN_IN_SEL1, 0);
227 }
228
229 return 1;
230}
231
232static int magician_spk_power(struct snd_soc_dapm_widget *w,
233 struct snd_kcontrol *k, int event)
234{
235 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, SND_SOC_DAPM_EVENT_ON(event));
236 return 0;
237}
238
239static int magician_hp_power(struct snd_soc_dapm_widget *w,
240 struct snd_kcontrol *k, int event)
241{
242 gpio_set_value(EGPIO_MAGICIAN_EP_POWER, SND_SOC_DAPM_EVENT_ON(event));
243 return 0;
244}
245
246static int magician_mic_bias(struct snd_soc_dapm_widget *w,
247 struct snd_kcontrol *k, int event)
248{
249 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, SND_SOC_DAPM_EVENT_ON(event));
250 return 0;
251}
252
253/* magician machine dapm widgets */
254static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = {
255 SND_SOC_DAPM_HP("Headphone Jack", magician_hp_power),
256 SND_SOC_DAPM_SPK("Speaker", magician_spk_power),
257 SND_SOC_DAPM_MIC("Call Mic", magician_mic_bias),
258 SND_SOC_DAPM_MIC("Headset Mic", magician_mic_bias),
259};
260
261/* magician machine audio_map */
262static const struct snd_soc_dapm_route audio_map[] = {
263
264 /* Headphone connected to VOUTL, VOUTR */
265 {"Headphone Jack", NULL, "VOUTL"},
266 {"Headphone Jack", NULL, "VOUTR"},
267
268 /* Speaker connected to VOUTL, VOUTR */
269 {"Speaker", NULL, "VOUTL"},
270 {"Speaker", NULL, "VOUTR"},
271
272 /* Mics are connected to VINM */
273 {"VINM", NULL, "Headset Mic"},
274 {"VINM", NULL, "Call Mic"},
275};
276
Lars-Peter Clausen92be5812016-11-12 14:23:13 +0100277static const char * const input_select[] = {"Call Mic", "Headset Mic"};
Philipp Zabel73772262009-03-19 09:34:46 +0100278static const struct soc_enum magician_in_sel_enum =
279 SOC_ENUM_SINGLE_EXT(2, input_select);
280
281static const struct snd_kcontrol_new uda1380_magician_controls[] = {
282 SOC_SINGLE_BOOL_EXT("Headphone Switch",
283 (unsigned long)&magician_hp_switch,
284 magician_get_hp, magician_set_hp),
285 SOC_SINGLE_BOOL_EXT("Speaker Switch",
286 (unsigned long)&magician_spk_switch,
287 magician_get_spk, magician_set_spk),
288 SOC_ENUM_EXT("Input Select", magician_in_sel_enum,
289 magician_get_input, magician_set_input),
290};
291
Philipp Zabel73772262009-03-19 09:34:46 +0100292/* magician digital audio interface glue - connects codec <--> CPU */
Kuninori Morimoto01825272019-06-06 13:11:51 +0900293SND_SOC_DAILINK_DEFS(playback,
294 DAILINK_COMP_ARRAY(COMP_CPU("pxa-ssp-dai.0")),
295 DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-0018",
296 "uda1380-hifi-playback")),
297 DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
298
299SND_SOC_DAILINK_DEFS(capture,
300 DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-i2s")),
301 DAILINK_COMP_ARRAY(COMP_CODEC("uda1380-codec.0-0018",
302 "uda1380-hifi-capture")),
303 DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
304
Philipp Zabel73772262009-03-19 09:34:46 +0100305static struct snd_soc_dai_link magician_dai[] = {
306{
307 .name = "uda1380",
308 .stream_name = "UDA1380 Playback",
Philipp Zabel73772262009-03-19 09:34:46 +0100309 .ops = &magician_playback_ops,
Kuninori Morimoto01825272019-06-06 13:11:51 +0900310 SND_SOC_DAILINK_REG(playback),
Philipp Zabel73772262009-03-19 09:34:46 +0100311},
312{
313 .name = "uda1380",
314 .stream_name = "UDA1380 Capture",
Philipp Zabel73772262009-03-19 09:34:46 +0100315 .ops = &magician_capture_ops,
Kuninori Morimoto01825272019-06-06 13:11:51 +0900316 SND_SOC_DAILINK_REG(capture),
Philipp Zabel73772262009-03-19 09:34:46 +0100317}
318};
319
320/* magician audio machine driver */
321static struct snd_soc_card snd_soc_card_magician = {
322 .name = "Magician",
Axel Lin561c6a12011-12-22 09:44:43 +0800323 .owner = THIS_MODULE,
Philipp Zabel73772262009-03-19 09:34:46 +0100324 .dai_link = magician_dai,
325 .num_links = ARRAY_SIZE(magician_dai),
Philipp Zabel73772262009-03-19 09:34:46 +0100326
Lars-Peter Clausen0fbd44a2014-03-12 15:27:38 +0100327 .controls = uda1380_magician_controls,
328 .num_controls = ARRAY_SIZE(uda1380_magician_controls),
329 .dapm_widgets = uda1380_dapm_widgets,
330 .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets),
331 .dapm_routes = audio_map,
332 .num_dapm_routes = ARRAY_SIZE(audio_map),
Lars-Peter Clausen3cfaaaa2015-01-09 22:03:30 +0100333 .fully_routed = true,
Philipp Zabel73772262009-03-19 09:34:46 +0100334};
335
336static struct platform_device *magician_snd_device;
337
Philipp Zabel1abd9182009-06-15 22:18:23 +0200338/*
339 * FIXME: move into magician board file once merged into the pxa tree
340 */
341static struct uda1380_platform_data uda1380_info = {
342 .gpio_power = EGPIO_MAGICIAN_CODEC_POWER,
343 .gpio_reset = EGPIO_MAGICIAN_CODEC_RESET,
344 .dac_clk = UDA1380_DAC_CLK_WSPLL,
345};
346
347static struct i2c_board_info i2c_board_info[] = {
348 {
349 I2C_BOARD_INFO("uda1380", 0x18),
350 .platform_data = &uda1380_info,
351 },
352};
353
Philipp Zabel73772262009-03-19 09:34:46 +0100354static int __init magician_init(void)
355{
356 int ret;
Philipp Zabel1abd9182009-06-15 22:18:23 +0200357 struct i2c_adapter *adapter;
358 struct i2c_client *client;
Philipp Zabel73772262009-03-19 09:34:46 +0100359
360 if (!machine_is_magician())
361 return -ENODEV;
362
Philipp Zabel1abd9182009-06-15 22:18:23 +0200363 adapter = i2c_get_adapter(0);
364 if (!adapter)
365 return -ENODEV;
366 client = i2c_new_device(adapter, i2c_board_info);
367 i2c_put_adapter(adapter);
368 if (!client)
369 return -ENODEV;
370
Philipp Zabel73772262009-03-19 09:34:46 +0100371 ret = gpio_request(EGPIO_MAGICIAN_SPK_POWER, "SPK_POWER");
372 if (ret)
373 goto err_request_spk;
374 ret = gpio_request(EGPIO_MAGICIAN_EP_POWER, "EP_POWER");
375 if (ret)
376 goto err_request_ep;
377 ret = gpio_request(EGPIO_MAGICIAN_MIC_POWER, "MIC_POWER");
378 if (ret)
379 goto err_request_mic;
380 ret = gpio_request(EGPIO_MAGICIAN_IN_SEL0, "IN_SEL0");
381 if (ret)
382 goto err_request_in_sel0;
383 ret = gpio_request(EGPIO_MAGICIAN_IN_SEL1, "IN_SEL1");
384 if (ret)
385 goto err_request_in_sel1;
386
Philipp Zabel73772262009-03-19 09:34:46 +0100387 gpio_set_value(EGPIO_MAGICIAN_IN_SEL0, 0);
388
Philipp Zabel73772262009-03-19 09:34:46 +0100389 magician_snd_device = platform_device_alloc("soc-audio", -1);
390 if (!magician_snd_device) {
391 ret = -ENOMEM;
392 goto err_pdev;
393 }
394
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000395 platform_set_drvdata(magician_snd_device, &snd_soc_card_magician);
Philipp Zabel73772262009-03-19 09:34:46 +0100396 ret = platform_device_add(magician_snd_device);
397 if (ret) {
398 platform_device_put(magician_snd_device);
399 goto err_pdev;
400 }
401
402 return 0;
403
404err_pdev:
405 gpio_free(EGPIO_MAGICIAN_IN_SEL1);
406err_request_in_sel1:
407 gpio_free(EGPIO_MAGICIAN_IN_SEL0);
408err_request_in_sel0:
409 gpio_free(EGPIO_MAGICIAN_MIC_POWER);
410err_request_mic:
411 gpio_free(EGPIO_MAGICIAN_EP_POWER);
412err_request_ep:
413 gpio_free(EGPIO_MAGICIAN_SPK_POWER);
414err_request_spk:
Philipp Zabel73772262009-03-19 09:34:46 +0100415 return ret;
416}
417
418static void __exit magician_exit(void)
419{
420 platform_device_unregister(magician_snd_device);
421
422 gpio_set_value(EGPIO_MAGICIAN_SPK_POWER, 0);
423 gpio_set_value(EGPIO_MAGICIAN_EP_POWER, 0);
424 gpio_set_value(EGPIO_MAGICIAN_MIC_POWER, 0);
Philipp Zabel73772262009-03-19 09:34:46 +0100425
426 gpio_free(EGPIO_MAGICIAN_IN_SEL1);
427 gpio_free(EGPIO_MAGICIAN_IN_SEL0);
428 gpio_free(EGPIO_MAGICIAN_MIC_POWER);
429 gpio_free(EGPIO_MAGICIAN_EP_POWER);
430 gpio_free(EGPIO_MAGICIAN_SPK_POWER);
Philipp Zabel73772262009-03-19 09:34:46 +0100431}
432
433module_init(magician_init);
434module_exit(magician_exit);
435
436MODULE_AUTHOR("Philipp Zabel");
437MODULE_DESCRIPTION("ALSA SoC Magician");
438MODULE_LICENSE("GPL");