Linux 3.11-rc3
[cris-mirror.git] / drivers / media / usb / stk1160 / stk1160-ac97.c
blobc8583c262c3d0b6f055cdf73a77e7d78037e2929
1 /*
2 * STK1160 driver
4 * Copyright (C) 2012 Ezequiel Garcia
5 * <elezegarcia--a.t--gmail.com>
7 * Based on Easycap driver by R.M. Thomas
8 * Copyright (C) 2010 R.M. Thomas
9 * <rmthomas--a.t--sciolus.org>
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.
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.
23 #include <linux/module.h>
24 #include <sound/core.h>
25 #include <sound/initval.h>
26 #include <sound/ac97_codec.h>
28 #include "stk1160.h"
29 #include "stk1160-reg.h"
31 static struct snd_ac97 *stk1160_ac97;
33 static void stk1160_write_ac97(struct snd_ac97 *ac97, u16 reg, u16 value)
35 struct stk1160 *dev = ac97->private_data;
37 /* Set codec register address */
38 stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
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);
45 * Set command write bit to initiate write operation.
46 * The bit will be cleared when transfer is done.
48 stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c);
51 static u16 stk1160_read_ac97(struct snd_ac97 *ac97, u16 reg)
53 struct stk1160 *dev = ac97->private_data;
54 u8 vall = 0;
55 u8 valh = 0;
57 /* Set codec register address */
58 stk1160_write_reg(dev, STK1160_AC97_ADDR, reg);
61 * Set command read bit to initiate read operation.
62 * The bit will be cleared when transfer is done.
64 stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b);
66 /* Retrieve register value */
67 stk1160_read_reg(dev, STK1160_AC97_CMD, &vall);
68 stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh);
70 return (valh << 8) | vall;
73 static void stk1160_reset_ac97(struct snd_ac97 *ac97)
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);
80 /* Set 16-bit audio data and choose L&R channel*/
81 stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01);
84 static struct snd_ac97_bus_ops stk1160_ac97_ops = {
85 .read = stk1160_read_ac97,
86 .write = stk1160_write_ac97,
87 .reset = stk1160_reset_ac97,
90 int stk1160_ac97_register(struct stk1160 *dev)
92 struct snd_card *card = NULL;
93 struct snd_ac97_bus *ac97_bus;
94 struct snd_ac97_template ac97_template;
95 int rc;
98 * Just want a card to access ac96 controls,
99 * the actual capture interface will be handled by snd-usb-audio
101 rc = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
102 THIS_MODULE, 0, &card);
103 if (rc < 0)
104 return rc;
106 snd_card_set_dev(card, dev->dev);
108 /* TODO: I'm not sure where should I get these names :-( */
109 snprintf(card->shortname, sizeof(card->shortname),
110 "stk1160-mixer");
111 snprintf(card->longname, sizeof(card->longname),
112 "stk1160 ac97 codec mixer control");
113 strncpy(card->driver, dev->dev->driver->name, sizeof(card->driver));
115 rc = snd_ac97_bus(card, 0, &stk1160_ac97_ops, NULL, &ac97_bus);
116 if (rc)
117 goto err;
119 /* We must set private_data before calling snd_ac97_mixer */
120 memset(&ac97_template, 0, sizeof(ac97_template));
121 ac97_template.private_data = dev;
122 ac97_template.scaps = AC97_SCAP_SKIP_MODEM;
123 rc = snd_ac97_mixer(ac97_bus, &ac97_template, &stk1160_ac97);
124 if (rc)
125 goto err;
127 dev->snd_card = card;
128 rc = snd_card_register(card);
129 if (rc)
130 goto err;
132 return 0;
134 err:
135 dev->snd_card = NULL;
136 snd_card_free(card);
137 return rc;
140 int stk1160_ac97_unregister(struct stk1160 *dev)
142 struct snd_card *card = dev->snd_card;
145 * We need to check usb_device,
146 * because ac97 release attempts to communicate with codec
148 if (card && dev->udev)
149 snd_card_free(card);
151 return 0;