Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 1 | /* |
| 2 | * SoC audio for HP iPAQ hx4700 |
| 3 | * |
| 4 | * Copyright (c) 2009 Philipp Zabel |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/timer.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/gpio.h> |
| 19 | |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/jack.h> |
| 22 | #include <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
| 24 | #include <sound/soc.h> |
| 25 | |
| 26 | #include <mach/hx4700.h> |
| 27 | #include <asm/mach-types.h> |
| 28 | #include "pxa2xx-i2s.h" |
| 29 | |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 30 | static struct snd_soc_jack hs_jack; |
| 31 | |
| 32 | /* Headphones jack detection DAPM pin */ |
| 33 | static struct snd_soc_jack_pin hs_jack_pin[] = { |
| 34 | { |
| 35 | .pin = "Headphone Jack", |
| 36 | .mask = SND_JACK_HEADPHONE, |
| 37 | }, |
| 38 | { |
| 39 | .pin = "Speaker", |
| 40 | /* disable speaker when hp jack is inserted */ |
| 41 | .mask = SND_JACK_HEADPHONE, |
| 42 | .invert = 1, |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | /* Headphones jack detection GPIO */ |
| 47 | static struct snd_soc_jack_gpio hs_jack_gpio = { |
| 48 | .gpio = GPIO75_HX4700_EARPHONE_nDET, |
| 49 | .invert = true, |
| 50 | .name = "hp-gpio", |
| 51 | .report = SND_JACK_HEADPHONE, |
| 52 | .debounce_time = 200, |
| 53 | }; |
| 54 | |
| 55 | /* |
| 56 | * iPAQ hx4700 uses I2S for capture and playback. |
| 57 | */ |
| 58 | static int hx4700_hw_params(struct snd_pcm_substream *substream, |
| 59 | struct snd_pcm_hw_params *params) |
| 60 | { |
| 61 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 62 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 63 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 64 | int ret = 0; |
| 65 | |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 66 | /* set the I2S system clock as output */ |
| 67 | ret = snd_soc_dai_set_sysclk(cpu_dai, PXA2XX_I2S_SYSCLK, 0, |
| 68 | SND_SOC_CLOCK_OUT); |
| 69 | if (ret < 0) |
| 70 | return ret; |
| 71 | |
| 72 | /* inform codec driver about clock freq * |
| 73 | * (PXA I2S always uses divider 256) */ |
| 74 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, 256 * params_rate(params), |
| 75 | SND_SOC_CLOCK_IN); |
| 76 | if (ret < 0) |
| 77 | return ret; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Bhumika Goyal | 943b731 | 2017-03-14 01:16:40 +0530 | [diff] [blame^] | 82 | static const struct snd_soc_ops hx4700_ops = { |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 83 | .hw_params = hx4700_hw_params, |
| 84 | }; |
| 85 | |
| 86 | static int hx4700_spk_power(struct snd_soc_dapm_widget *w, |
| 87 | struct snd_kcontrol *k, int event) |
| 88 | { |
| 89 | gpio_set_value(GPIO107_HX4700_SPK_nSD, !!SND_SOC_DAPM_EVENT_ON(event)); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int hx4700_hp_power(struct snd_soc_dapm_widget *w, |
| 94 | struct snd_kcontrol *k, int event) |
| 95 | { |
| 96 | gpio_set_value(GPIO92_HX4700_HP_DRIVER, !!SND_SOC_DAPM_EVENT_ON(event)); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* hx4700 machine dapm widgets */ |
| 101 | static const struct snd_soc_dapm_widget hx4700_dapm_widgets[] = { |
| 102 | SND_SOC_DAPM_HP("Headphone Jack", hx4700_hp_power), |
| 103 | SND_SOC_DAPM_SPK("Speaker", hx4700_spk_power), |
| 104 | SND_SOC_DAPM_MIC("Built-in Microphone", NULL), |
| 105 | }; |
| 106 | |
| 107 | /* hx4700 machine audio_map */ |
| 108 | static const struct snd_soc_dapm_route hx4700_audio_map[] = { |
| 109 | |
| 110 | /* Headphone connected to LOUT, ROUT */ |
| 111 | {"Headphone Jack", NULL, "LOUT"}, |
| 112 | {"Headphone Jack", NULL, "ROUT"}, |
| 113 | |
| 114 | /* Speaker connected to MOUT2 */ |
| 115 | {"Speaker", NULL, "MOUT2"}, |
| 116 | |
| 117 | /* Microphone connected to MICIN */ |
| 118 | {"MICIN", NULL, "Built-in Microphone"}, |
| 119 | {"AIN", NULL, "MICOUT"}, |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * Logic for a ak4641 as connected on a HP iPAQ hx4700 |
| 124 | */ |
| 125 | static int hx4700_ak4641_init(struct snd_soc_pcm_runtime *rtd) |
| 126 | { |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 127 | int err; |
| 128 | |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 129 | /* Jack detection API stuff */ |
Lars-Peter Clausen | f7a4433 | 2015-03-04 10:33:29 +0100 | [diff] [blame] | 130 | err = snd_soc_card_jack_new(rtd->card, "Headphone Jack", |
| 131 | SND_JACK_HEADPHONE, &hs_jack, hs_jack_pin, |
| 132 | ARRAY_SIZE(hs_jack_pin)); |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 133 | if (err) |
| 134 | return err; |
| 135 | |
| 136 | err = snd_soc_jack_add_gpios(&hs_jack, 1, &hs_jack_gpio); |
| 137 | |
| 138 | return err; |
| 139 | } |
| 140 | |
Takashi Iwai | 16088cb | 2014-06-03 12:31:46 +0200 | [diff] [blame] | 141 | static int hx4700_card_remove(struct snd_soc_card *card) |
Stephen Warren | e1d4d3c | 2014-05-30 12:42:57 -0600 | [diff] [blame] | 142 | { |
| 143 | snd_soc_jack_free_gpios(&hs_jack, 1, &hs_jack_gpio); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 148 | /* hx4700 digital audio interface glue - connects codec <--> CPU */ |
| 149 | static struct snd_soc_dai_link hx4700_dai = { |
| 150 | .name = "ak4641", |
| 151 | .stream_name = "AK4641", |
| 152 | .cpu_dai_name = "pxa2xx-i2s", |
| 153 | .codec_dai_name = "ak4641-hifi", |
| 154 | .platform_name = "pxa-pcm-audio", |
| 155 | .codec_name = "ak4641.0-0012", |
| 156 | .init = hx4700_ak4641_init, |
Axel Lin | 673847c | 2011-12-20 16:13:26 +0800 | [diff] [blame] | 157 | .dai_fmt = SND_SOC_DAIFMT_MSB | SND_SOC_DAIFMT_NB_NF | |
| 158 | SND_SOC_DAIFMT_CBS_CFS, |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 159 | .ops = &hx4700_ops, |
| 160 | }; |
| 161 | |
| 162 | /* hx4700 audio machine driver */ |
| 163 | static struct snd_soc_card snd_soc_card_hx4700 = { |
| 164 | .name = "iPAQ hx4700", |
Axel Lin | 561c6a1 | 2011-12-22 09:44:43 +0800 | [diff] [blame] | 165 | .owner = THIS_MODULE, |
Stephen Warren | e1d4d3c | 2014-05-30 12:42:57 -0600 | [diff] [blame] | 166 | .remove = hx4700_card_remove, |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 167 | .dai_link = &hx4700_dai, |
| 168 | .num_links = 1, |
| 169 | .dapm_widgets = hx4700_dapm_widgets, |
| 170 | .num_dapm_widgets = ARRAY_SIZE(hx4700_dapm_widgets), |
| 171 | .dapm_routes = hx4700_audio_map, |
| 172 | .num_dapm_routes = ARRAY_SIZE(hx4700_audio_map), |
Lars-Peter Clausen | c529d0a | 2015-01-09 22:03:29 +0100 | [diff] [blame] | 173 | .fully_routed = true, |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static struct gpio hx4700_audio_gpios[] = { |
| 177 | { GPIO107_HX4700_SPK_nSD, GPIOF_OUT_INIT_HIGH, "SPK_POWER" }, |
| 178 | { GPIO92_HX4700_HP_DRIVER, GPIOF_OUT_INIT_LOW, "EP_POWER" }, |
| 179 | }; |
| 180 | |
Bill Pemberton | d7f1be8 | 2012-12-07 09:26:14 -0500 | [diff] [blame] | 181 | static int hx4700_audio_probe(struct platform_device *pdev) |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | if (!machine_is_h4700()) |
| 186 | return -ENODEV; |
| 187 | |
| 188 | ret = gpio_request_array(hx4700_audio_gpios, |
| 189 | ARRAY_SIZE(hx4700_audio_gpios)); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | |
| 193 | snd_soc_card_hx4700.dev = &pdev->dev; |
Axel Lin | 2fd7076 | 2015-09-01 10:32:59 +0800 | [diff] [blame] | 194 | ret = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_hx4700); |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 195 | if (ret) |
Axel Lin | 497d496 | 2011-12-12 11:26:00 +0800 | [diff] [blame] | 196 | gpio_free_array(hx4700_audio_gpios, |
| 197 | ARRAY_SIZE(hx4700_audio_gpios)); |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 198 | |
Axel Lin | 497d496 | 2011-12-12 11:26:00 +0800 | [diff] [blame] | 199 | return ret; |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 200 | } |
| 201 | |
Bill Pemberton | d7f1be8 | 2012-12-07 09:26:14 -0500 | [diff] [blame] | 202 | static int hx4700_audio_remove(struct platform_device *pdev) |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 203 | { |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 204 | gpio_set_value(GPIO92_HX4700_HP_DRIVER, 0); |
| 205 | gpio_set_value(GPIO107_HX4700_SPK_nSD, 0); |
| 206 | |
| 207 | gpio_free_array(hx4700_audio_gpios, ARRAY_SIZE(hx4700_audio_gpios)); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct platform_driver hx4700_audio_driver = { |
| 212 | .driver = { |
| 213 | .name = "hx4700-audio", |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 214 | .pm = &snd_soc_pm_ops, |
| 215 | }, |
| 216 | .probe = hx4700_audio_probe, |
Bill Pemberton | d7f1be8 | 2012-12-07 09:26:14 -0500 | [diff] [blame] | 217 | .remove = hx4700_audio_remove, |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 218 | }; |
| 219 | |
Axel Lin | 2f702a1 | 2011-11-25 10:13:37 +0800 | [diff] [blame] | 220 | module_platform_driver(hx4700_audio_driver); |
Dmitry Artamonow | c26f642 | 2011-05-18 19:25:10 +0400 | [diff] [blame] | 221 | |
| 222 | MODULE_AUTHOR("Philipp Zabel"); |
| 223 | MODULE_DESCRIPTION("ALSA SoC iPAQ hx4700"); |
| 224 | MODULE_LICENSE("GPL"); |
| 225 | MODULE_ALIAS("platform:hx4700-audio"); |