From patchwork Wed Nov 4 17:12:44 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Jarzmik X-Patchwork-Id: 7552301 Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 61E14BEEA4 for ; Wed, 4 Nov 2015 17:20:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6D2BF20742 for ; Wed, 4 Nov 2015 17:20:07 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 2410120739 for ; Wed, 4 Nov 2015 17:20:05 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 9B5CA2606CB; Wed, 4 Nov 2015 18:20:03 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 843A4260522; Wed, 4 Nov 2015 18:19:55 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 6F047260537; Wed, 4 Nov 2015 18:19:54 +0100 (CET) Received: from smtp.smtpout.orange.fr (smtp05.smtpout.orange.fr [80.12.242.127]) by alsa0.perex.cz (Postfix) with ESMTP id 42FAB260481 for ; Wed, 4 Nov 2015 18:19:47 +0100 (CET) Received: from belgarion.home ([90.38.168.74]) by mwinf5d28 with ME id dVKi1r00R1cem4A03VKjqw; Wed, 04 Nov 2015 18:19:46 +0100 X-ME-Helo: belgarion.home X-ME-Date: Wed, 04 Nov 2015 18:19:46 +0100 X-ME-IP: 90.38.168.74 From: Robert Jarzmik To: Liam Girdwood , Mark Brown , Jaroslav Kysela , Takashi Iwai Date: Wed, 4 Nov 2015 18:12:44 +0100 Message-Id: <1446657164-25012-1-git-send-email-robert.jarzmik@free.fr> X-Mailer: git-send-email 2.1.4 Cc: alsa-devel@alsa-project.org, patches@opensource.wolfsonmicro.com, Robert Jarzmik , linux-kernel@vger.kernel.org Subject: [alsa-devel] [PATCH] ASoC: wm9713: add gpio chip X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP The Wolfson WM9713 provides 8 GPIOs. If the gpiolib is compiled in the kernel, declare a gpio chip. Signed-off-by: Robert Jarzmik --- sound/soc/codecs/wm9713.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++ sound/soc/codecs/wm9713.h | 1 + 2 files changed, 124 insertions(+) diff --git a/sound/soc/codecs/wm9713.c b/sound/soc/codecs/wm9713.c index 79e143625ac3..904fe4fc5bf1 100644 --- a/sound/soc/codecs/wm9713.c +++ b/sound/soc/codecs/wm9713.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -33,11 +34,21 @@ #define WM9713_VENDOR_ID 0x574d4c13 #define WM9713_VENDOR_ID_MASK 0xffffffff +#define AC97_GPIO_PUSH_PULL 0x58 + +struct wm9713_gpio_priv { +#ifdef CONFIG_GPIOLIB + struct gpio_chip gpio_chip; +#endif + struct snd_soc_codec *codec; +}; + struct wm9713_priv { struct snd_ac97 *ac97; u32 pll_in; /* PLL input frequency */ unsigned int hp_mixer[2]; struct mutex lock; + struct wm9713_gpio_priv *gpio_priv; }; #define HPL_MIXER 0 @@ -1202,10 +1213,116 @@ static int wm9713_soc_resume(struct snd_soc_codec *codec) return ret; } +#ifdef CONFIG_GPIOLIB +static inline struct snd_soc_codec *gpio_to_codec(struct gpio_chip *chip) +{ + struct wm9713_gpio_priv *gpio_priv = + container_of(chip, struct wm9713_gpio_priv, gpio_chip); + + return gpio_priv->codec; +} + +static int wm9713_gpio_request(struct gpio_chip *chip, unsigned offset) +{ + if (offset >= WM9713_NUM_GPIOS) + return -EINVAL; + + return 0; +} + +static int wm9713_gpio_direction_in(struct gpio_chip *chip, unsigned offset) +{ + struct snd_soc_codec *codec = gpio_to_codec(chip); + + return snd_soc_update_bits(codec, AC97_GPIO_CFG, + 1 << (offset + 1), 1 << (offset + 1)); +} + +static int wm9713_gpio_get(struct gpio_chip *chip, unsigned offset) +{ + struct snd_soc_codec *codec = gpio_to_codec(chip); + int ret; + + ret = snd_soc_read(codec, AC97_GPIO_STATUS); + + return ret < 0 ? ret : ret & (1 << (offset + 1)); +} + +static void wm9713_gpio_set(struct gpio_chip *chip, unsigned offset, int value) +{ + struct snd_soc_codec *codec = gpio_to_codec(chip); + + snd_soc_update_bits(codec, AC97_GPIO_PUSH_PULL, + 1 << offset | (1 << (offset + 8)), + 1 << (offset + 8 * !!value)); +} + +static int wm9713_gpio_direction_out(struct gpio_chip *chip, + unsigned offset, int value) +{ + struct snd_soc_codec *codec = gpio_to_codec(chip); + + wm9713_gpio_set(chip, offset, value); + + return snd_soc_update_bits(codec, AC97_GPIO_CFG, 1 << (offset + 1), 0); +} + +static struct gpio_chip wm9713_gpio_chip = { + .label = "wm9713", + .owner = THIS_MODULE, + .request = wm9713_gpio_request, + .direction_input = wm9713_gpio_direction_in, + .get = wm9713_gpio_get, + .direction_output = wm9713_gpio_direction_out, + .set = wm9713_gpio_set, + .can_sleep = 1, +}; + +static int wm9713_init_gpio(struct snd_soc_codec *codec) +{ + struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec); + struct wm9713_gpio_priv *gpio_priv; + int ret; + + gpio_priv = devm_kzalloc(codec->dev, sizeof(*wm9713->gpio_priv), + GFP_KERNEL); + if (!gpio_priv) + return -ENOMEM; + wm9713->gpio_priv = gpio_priv; + gpio_priv->codec = codec; + gpio_priv->gpio_chip = wm9713_gpio_chip; + gpio_priv->gpio_chip.ngpio = WM9713_NUM_GPIOS; + gpio_priv->gpio_chip.dev = codec->dev; + gpio_priv->gpio_chip.base = -1; + + ret = gpiochip_add(&gpio_priv->gpio_chip); + if (ret != 0) + dev_err(codec->dev, "Failed to add GPIOs: %d\n", ret); + return ret; +} + +static void wm9713_free_gpio(struct snd_soc_codec *codec) +{ + struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec); + + gpiochip_remove(&wm9713->gpio_priv->gpio_chip); +} +#else +static int wm9713_init_gpio(struct snd_soc_codec *codec) +{ + return 0; +} + +static void wm9713_free_gpio(struct snd_soc_codec *codec) +{ +} +#endif + static int wm9713_soc_probe(struct snd_soc_codec *codec) { struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec); struct regmap *regmap; + int ret; wm9713->ac97 = snd_soc_new_ac97_codec(codec, WM9713_VENDOR_ID, WM9713_VENDOR_ID_MASK); @@ -1223,6 +1340,11 @@ static int wm9713_soc_probe(struct snd_soc_codec *codec) /* unmute the adc - move to kcontrol */ snd_soc_update_bits(codec, AC97_CD, 0x7fff, 0x0000); + ret = wm9713_init_gpio(codec); + if (ret) { + snd_soc_free_ac97_codec(wm9713->ac97); + return ret; + } return 0; } @@ -1230,6 +1352,7 @@ static int wm9713_soc_remove(struct snd_soc_codec *codec) { struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec); + wm9713_free_gpio(codec); snd_soc_codec_exit_regmap(codec); snd_soc_free_ac97_codec(wm9713->ac97); return 0; diff --git a/sound/soc/codecs/wm9713.h b/sound/soc/codecs/wm9713.h index 53df11b1f727..d72f96e653d1 100644 --- a/sound/soc/codecs/wm9713.h +++ b/sound/soc/codecs/wm9713.h @@ -45,4 +45,5 @@ #define WM9713_DAI_AC97_AUX 1 #define WM9713_DAI_PCM_VOICE 2 +#define WM9713_NUM_GPIOS 8 #endif