linux/drivers/media/usb/stk1160/stk1160-ac97.c
<<
>>
Prefs
   1/*
   2 * STK1160 driver
   3 *
   4 * Copyright (C) 2012 Ezequiel Garcia
   5 * <elezegarcia--a.t--gmail.com>
   6 *
   7 * Based on Easycap driver by R.M. Thomas
   8 *      Copyright (C) 2010 R.M. Thomas
   9 *      <rmthomas--a.t--sciolus.org>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or
  14 * (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 *
  21 */
  22
  23#include <linux/module.h>
  24#include <sound/core.h>
  25#include <sound/initval.h>
  26#include <sound/ac97_codec.h>
  27
  28#include "stk1160.h"
  29#include "stk1160-reg.h"
  30
  31static struct snd_ac97 *stk1160_ac97;
  32
  33static void stk1160_write_ac97(struct snd_ac97 *ac97, u16 reg, u16 value)
  34{
  35        struct stk1160 *dev = ac97->private_data;
  36
  37        /* Set codec register address */
  38        stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
  39
  40        /* Set codec command */
  41        stk1160_write_reg(dev, STK1160_AC97_CMD, value & 0xff);
  42        stk1160_write_reg(dev, STK1160_AC97_CMD + 1, (value & 0xff00) >> 8);
  43
  44        /*
  45         * Set command write bit to initiate write operation.
  46         * The bit will be cleared when transfer is done.
  47         */
  48        stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
  49}
  50
  51static u16 stk1160_read_ac97(struct snd_ac97 *ac97, u16 reg)
  52{
  53        struct stk1160 *dev = ac97->private_data;
  54        u8 vall = 0;
  55        u8 valh = 0;
  56
  57        /* Set codec register address */
  58        stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
  59
  60        /*
  61         * Set command read bit to initiate read operation.
  62         * The bit will be cleared when transfer is done.
  63         */
  64        stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b);
  65
  66        /* Retrieve register value */
  67        stk1160_read_reg(dev, STK1160_AC97_CMD, &vall);
  68        stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh);
  69
  70        return (valh << 8) | vall;
  71}
  72
  73static void stk1160_reset_ac97(struct snd_ac97 *ac97)
  74{
  75        struct stk1160 *dev = ac97->private_data;
  76        /* Two-step reset AC97 interface and hardware codec */
  77        stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x94);
  78        stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x88);
  79
  80        /* Set 16-bit audio data and choose L&R channel*/
  81        stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01);
  82}
  83
  84static struct snd_ac97_bus_ops stk1160_ac97_ops = {
  85        .read   = stk1160_read_ac97,
  86        .write  = stk1160_write_ac97,
  87        .reset  = stk1160_reset_ac97,
  88};
  89
  90int stk1160_ac97_register(struct stk1160 *dev)
  91{
  92        struct snd_card *card = NULL;
  93        struct snd_ac97_bus *ac97_bus;
  94        struct snd_ac97_template ac97_template;
  95        int rc;
  96
  97        /*
  98         * Just want a card to access ac96 controls,
  99         * the actual capture interface will be handled by snd-usb-audio
 100         */
 101        rc = snd_card_new(dev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
 102                          THIS_MODULE, 0, &card);
 103        if (rc < 0)
 104                return rc;
 105
 106        /* TODO: I'm not sure where should I get these names :-( */
 107        snprintf(card->shortname, sizeof(card->shortname),
 108                 "stk1160-mixer");
 109        snprintf(card->longname, sizeof(card->longname),
 110                 "stk1160 ac97 codec mixer control");
 111        strlcpy(card->driver, dev->dev->driver->name, sizeof(card->driver));
 112
 113        rc = snd_ac97_bus(card, 0, &stk1160_ac97_ops, NULL, &ac97_bus);
 114        if (rc)
 115                goto err;
 116
 117        /* We must set private_data before calling snd_ac97_mixer */
 118        memset(&ac97_template, 0, sizeof(ac97_template));
 119        ac97_template.private_data = dev;
 120        ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
 121        rc = snd_ac97_mixer(ac97_bus, &ac97_template, &stk1160_ac97);
 122        if (rc)
 123                goto err;
 124
 125        dev->snd_card = card;
 126        rc = snd_card_register(card);
 127        if (rc)
 128                goto err;
 129
 130        return 0;
 131
 132err:
 133        dev->snd_card = NULL;
 134        snd_card_free(card);
 135        return rc;
 136}
 137
 138int stk1160_ac97_unregister(struct stk1160 *dev)
 139{
 140        struct snd_card *card = dev->snd_card;
 141
 142        /*
 143         * We need to check usb_device,
 144         * because ac97 release attempts to communicate with codec
 145         */
 146        if (card && dev->udev)
 147                snd_card_free(card);
 148
 149        return 0;
 150}
 151