Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips |
| 3 | * |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 4 | * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 21 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/io.h> |
| 24 | #include <linux/delay.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 28 | #include <linux/sched.h> |
| 29 | #include <media/v4l2-device.h> |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 30 | #include <media/v4l2-dev.h> |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 31 | #include <media/v4l2-fh.h> |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 32 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 33 | #include <media/v4l2-event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <sound/tea575x-tuner.h> |
| 35 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 36 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips"); |
| 38 | MODULE_LICENSE("GPL"); |
| 39 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 40 | #define FREQ_LO (76U * 16000) |
| 41 | #define FREQ_HI (108U * 16000) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * definitions |
| 45 | */ |
| 46 | |
| 47 | #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */ |
| 48 | #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */ |
| 49 | #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */ |
| 50 | #define TEA575X_BIT_BAND_MASK (3<<20) |
| 51 | #define TEA575X_BIT_BAND_FM (0<<20) |
| 52 | #define TEA575X_BIT_BAND_MW (1<<20) |
| 53 | #define TEA575X_BIT_BAND_LW (1<<21) |
| 54 | #define TEA575X_BIT_BAND_SW (1<<22) |
| 55 | #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */ |
| 56 | #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */ |
| 57 | #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */ |
| 58 | #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */ |
| 59 | #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */ |
| 60 | #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */ |
| 61 | #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */ |
| 62 | #define TEA575X_BIT_DUMMY (1<<15) /* buffer */ |
| 63 | #define TEA575X_BIT_FREQ_MASK 0x7fff |
| 64 | |
| 65 | /* |
| 66 | * lowlevel part |
| 67 | */ |
| 68 | |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 69 | static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val) |
| 70 | { |
| 71 | u16 l; |
| 72 | u8 data; |
| 73 | |
| 74 | tea->ops->set_direction(tea, 1); |
| 75 | udelay(16); |
| 76 | |
| 77 | for (l = 25; l > 0; l--) { |
| 78 | data = (val >> 24) & TEA575X_DATA; |
| 79 | val <<= 1; /* shift data */ |
| 80 | tea->ops->set_pins(tea, data | TEA575X_WREN); |
| 81 | udelay(2); |
| 82 | tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK); |
| 83 | udelay(2); |
| 84 | tea->ops->set_pins(tea, data | TEA575X_WREN); |
| 85 | udelay(2); |
| 86 | } |
| 87 | |
| 88 | if (!tea->mute) |
| 89 | tea->ops->set_pins(tea, 0); |
| 90 | } |
| 91 | |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 92 | static u32 snd_tea575x_read(struct snd_tea575x *tea) |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 93 | { |
| 94 | u16 l, rdata; |
| 95 | u32 data = 0; |
| 96 | |
| 97 | tea->ops->set_direction(tea, 0); |
| 98 | tea->ops->set_pins(tea, 0); |
| 99 | udelay(16); |
| 100 | |
| 101 | for (l = 24; l--;) { |
| 102 | tea->ops->set_pins(tea, TEA575X_CLK); |
| 103 | udelay(2); |
| 104 | if (!l) |
| 105 | tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1; |
| 106 | tea->ops->set_pins(tea, 0); |
| 107 | udelay(2); |
| 108 | data <<= 1; /* shift data */ |
| 109 | rdata = tea->ops->get_pins(tea); |
| 110 | if (!l) |
| 111 | tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1; |
| 112 | if (rdata & TEA575X_DATA) |
| 113 | data++; |
| 114 | udelay(2); |
| 115 | } |
| 116 | |
| 117 | if (tea->mute) |
| 118 | tea->ops->set_pins(tea, TEA575X_WREN); |
| 119 | |
| 120 | return data; |
| 121 | } |
| 122 | |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 123 | static u32 snd_tea575x_get_freq(struct snd_tea575x *tea) |
| 124 | { |
| 125 | u32 freq = snd_tea575x_read(tea) & TEA575X_BIT_FREQ_MASK; |
| 126 | |
| 127 | if (freq == 0) |
| 128 | return freq; |
| 129 | |
| 130 | /* freq *= 12.5 */ |
| 131 | freq *= 125; |
| 132 | freq /= 10; |
| 133 | /* crystal fixup */ |
| 134 | if (tea->tea5759) |
| 135 | freq += TEA575X_FMIF; |
| 136 | else |
| 137 | freq -= TEA575X_FMIF; |
| 138 | |
| 139 | return clamp(freq * 16, FREQ_LO, FREQ_HI); /* from kHz */ |
| 140 | } |
| 141 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 142 | static void snd_tea575x_set_freq(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 144 | u32 freq = tea->freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 146 | freq /= 16; /* to kHz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* crystal fixup */ |
| 148 | if (tea->tea5759) |
Ondrej Zary | ea27316 | 2011-05-12 22:17:56 +0200 | [diff] [blame] | 149 | freq -= TEA575X_FMIF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | else |
Ondrej Zary | ea27316 | 2011-05-12 22:17:56 +0200 | [diff] [blame] | 151 | freq += TEA575X_FMIF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* freq /= 12.5 */ |
| 153 | freq *= 10; |
| 154 | freq /= 125; |
| 155 | |
| 156 | tea->val &= ~TEA575X_BIT_FREQ_MASK; |
| 157 | tea->val |= freq & TEA575X_BIT_FREQ_MASK; |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 158 | snd_tea575x_write(tea, tea->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Linux Video interface |
| 163 | */ |
| 164 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 165 | static int vidioc_querycap(struct file *file, void *priv, |
| 166 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 168 | struct snd_tea575x *tea = video_drvdata(file); |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 169 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 170 | strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver)); |
Ondrej Zary | 10ca720 | 2011-05-12 22:18:22 +0200 | [diff] [blame] | 171 | strlcpy(v->card, tea->card, sizeof(v->card)); |
| 172 | strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card)); |
| 173 | strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info)); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 174 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 175 | if (!tea->cannot_read_data) |
| 176 | v->device_caps |= V4L2_CAP_HW_FREQ_SEEK; |
| 177 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 178 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 181 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 182 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 184 | struct snd_tea575x *tea = video_drvdata(file); |
| 185 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 186 | if (v->index > 0) |
| 187 | return -EINVAL; |
| 188 | |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 189 | snd_tea575x_read(tea); |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 190 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 191 | strcpy(v->name, "FM"); |
| 192 | v->type = V4L2_TUNER_RADIO; |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 193 | v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; |
Hans Verkuil | 54f6019 | 2012-05-27 07:25:06 -0300 | [diff] [blame^] | 194 | if (!tea->cannot_read_data) |
| 195 | v->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 196 | v->rangelow = FREQ_LO; |
| 197 | v->rangehigh = FREQ_HI; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 198 | v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; |
| 199 | v->audmode = (tea->val & TEA575X_BIT_MONO) ? |
| 200 | V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO; |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 201 | v->signal = tea->tuned ? 0xffff : 0; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 206 | struct v4l2_tuner *v) |
| 207 | { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 208 | struct snd_tea575x *tea = video_drvdata(file); |
| 209 | |
| 210 | if (v->index) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 211 | return -EINVAL; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 212 | tea->val &= ~TEA575X_BIT_MONO; |
| 213 | if (v->audmode == V4L2_TUNER_MODE_MONO) |
| 214 | tea->val |= TEA575X_BIT_MONO; |
| 215 | snd_tea575x_write(tea, tea->val); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 220 | struct v4l2_frequency *f) |
| 221 | { |
| 222 | struct snd_tea575x *tea = video_drvdata(file); |
| 223 | |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 224 | if (f->tuner != 0) |
| 225 | return -EINVAL; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 226 | f->type = V4L2_TUNER_RADIO; |
| 227 | f->frequency = tea->freq; |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 232 | struct v4l2_frequency *f) |
| 233 | { |
| 234 | struct snd_tea575x *tea = video_drvdata(file); |
| 235 | |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 236 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
| 237 | return -EINVAL; |
| 238 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 239 | tea->val &= ~TEA575X_BIT_SEARCH; |
| 240 | tea->freq = clamp(f->frequency, FREQ_LO, FREQ_HI); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 241 | snd_tea575x_set_freq(tea); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 245 | static int vidioc_s_hw_freq_seek(struct file *file, void *fh, |
| 246 | struct v4l2_hw_freq_seek *a) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 247 | { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 248 | struct snd_tea575x *tea = video_drvdata(file); |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 249 | unsigned long timeout; |
| 250 | int i; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 251 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 252 | if (tea->cannot_read_data) |
| 253 | return -ENOTTY; |
| 254 | if (a->tuner || a->wrap_around) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 255 | return -EINVAL; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 256 | |
| 257 | /* clear the frequency, HW will fill it in */ |
| 258 | tea->val &= ~TEA575X_BIT_FREQ_MASK; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 259 | tea->val |= TEA575X_BIT_SEARCH; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 260 | if (a->seek_upward) |
| 261 | tea->val |= TEA575X_BIT_UPDOWN; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 262 | else |
| 263 | tea->val &= ~TEA575X_BIT_UPDOWN; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 264 | snd_tea575x_write(tea, tea->val); |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 265 | timeout = jiffies + msecs_to_jiffies(10000); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 266 | for (;;) { |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 267 | if (time_after(jiffies, timeout)) |
| 268 | break; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 269 | if (schedule_timeout_interruptible(msecs_to_jiffies(10))) { |
| 270 | /* some signal arrived, stop search */ |
| 271 | tea->val &= ~TEA575X_BIT_SEARCH; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 272 | snd_tea575x_set_freq(tea); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 273 | return -ERESTARTSYS; |
| 274 | } |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 275 | if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) { |
| 276 | u32 freq; |
| 277 | |
| 278 | /* Found a frequency, wait until it can be read */ |
| 279 | for (i = 0; i < 100; i++) { |
| 280 | msleep(10); |
| 281 | freq = snd_tea575x_get_freq(tea); |
| 282 | if (freq) /* available */ |
| 283 | break; |
| 284 | } |
| 285 | if (freq == 0) /* shouldn't happen */ |
| 286 | break; |
| 287 | /* |
| 288 | * if we moved by less than 50 kHz, or in the wrong |
| 289 | * direction, continue seeking |
| 290 | */ |
| 291 | if (abs(tea->freq - freq) < 16 * 50 || |
| 292 | (a->seek_upward && freq < tea->freq) || |
| 293 | (!a->seek_upward && freq > tea->freq)) { |
| 294 | snd_tea575x_write(tea, tea->val); |
| 295 | continue; |
| 296 | } |
| 297 | tea->freq = freq; |
| 298 | tea->val &= ~TEA575X_BIT_SEARCH; |
| 299 | return 0; |
| 300 | } |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 301 | } |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 302 | tea->val &= ~TEA575X_BIT_SEARCH; |
| 303 | snd_tea575x_set_freq(tea); |
Hans Verkuil | 54f6019 | 2012-05-27 07:25:06 -0300 | [diff] [blame^] | 304 | return -ENODATA; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 305 | } |
| 306 | |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 307 | static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 308 | { |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 309 | struct snd_tea575x *tea = container_of(ctrl->handler, struct snd_tea575x, ctrl_handler); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 310 | |
| 311 | switch (ctrl->id) { |
| 312 | case V4L2_CID_AUDIO_MUTE: |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 313 | tea->mute = ctrl->val; |
| 314 | snd_tea575x_set_freq(tea); |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 315 | return 0; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 316 | } |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 317 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 318 | return -EINVAL; |
| 319 | } |
| 320 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 321 | static const struct v4l2_file_operations tea575x_fops = { |
| 322 | .owner = THIS_MODULE, |
Ondrej Zary | 6a529c1 | 2011-06-11 10:28:59 -0300 | [diff] [blame] | 323 | .unlocked_ioctl = video_ioctl2, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 324 | .open = v4l2_fh_open, |
| 325 | .release = v4l2_fh_release, |
| 326 | .poll = v4l2_ctrl_poll, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | static const struct v4l2_ioctl_ops tea575x_ioctl_ops = { |
| 330 | .vidioc_querycap = vidioc_querycap, |
| 331 | .vidioc_g_tuner = vidioc_g_tuner, |
| 332 | .vidioc_s_tuner = vidioc_s_tuner, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 333 | .vidioc_g_frequency = vidioc_g_frequency, |
| 334 | .vidioc_s_frequency = vidioc_s_frequency, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 335 | .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek, |
| 336 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 337 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 338 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 339 | }; |
| 340 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 341 | static const struct video_device tea575x_radio = { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 342 | .fops = &tea575x_fops, |
| 343 | .ioctl_ops = &tea575x_ioctl_ops, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 344 | .release = video_device_release_empty, |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | static const struct v4l2_ctrl_ops tea575x_ctrl_ops = { |
| 348 | .s_ctrl = tea575x_s_ctrl, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 349 | }; |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | /* |
| 352 | * initialize all the tea575x chips |
| 353 | */ |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 354 | int snd_tea575x_init(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 356 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 358 | tea->mute = true; |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 359 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 360 | /* Not all devices can or know how to read the data back. |
| 361 | Such devices can set cannot_read_data to true. */ |
| 362 | if (!tea->cannot_read_data) { |
| 363 | snd_tea575x_write(tea, 0x55AA); |
| 364 | if (snd_tea575x_read(tea) != 0x55AA) |
| 365 | return -ENODEV; |
| 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 368 | tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | tea->freq = 90500 * 16; /* 90.5Mhz default */ |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 370 | snd_tea575x_set_freq(tea); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 372 | tea->vd = tea575x_radio; |
| 373 | video_set_drvdata(&tea->vd, tea); |
Ondrej Zary | 6a529c1 | 2011-06-11 10:28:59 -0300 | [diff] [blame] | 374 | mutex_init(&tea->mutex); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 375 | strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name)); |
Ondrej Zary | 6a529c1 | 2011-06-11 10:28:59 -0300 | [diff] [blame] | 376 | tea->vd.lock = &tea->mutex; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 377 | tea->vd.v4l2_dev = tea->v4l2_dev; |
| 378 | tea->vd.ctrl_handler = &tea->ctrl_handler; |
| 379 | set_bit(V4L2_FL_USE_FH_PRIO, &tea->vd.flags); |
Hans Verkuil | 6539799 | 2012-05-10 03:16:14 -0300 | [diff] [blame] | 380 | /* disable hw_freq_seek if we can't use it */ |
| 381 | if (tea->cannot_read_data) |
Hans Verkuil | 152a3a7 | 2012-05-14 11:32:48 -0300 | [diff] [blame] | 382 | v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 383 | |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 384 | v4l2_ctrl_handler_init(&tea->ctrl_handler, 1); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 385 | v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); |
| 386 | retval = tea->ctrl_handler.error; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 387 | if (retval) { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 388 | v4l2_err(tea->v4l2_dev, "can't initialize controls\n"); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 389 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 390 | return retval; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 391 | } |
| 392 | |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 393 | if (tea->ext_init) { |
| 394 | retval = tea->ext_init(tea); |
| 395 | if (retval) { |
| 396 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
| 397 | return retval; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | v4l2_ctrl_handler_setup(&tea->ctrl_handler); |
| 402 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 403 | retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 404 | if (retval) { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 405 | v4l2_err(tea->v4l2_dev, "can't register video device!\n"); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 406 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
| 407 | return retval; |
| 408 | } |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 409 | |
| 410 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 413 | void snd_tea575x_exit(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 415 | video_unregister_device(&tea->vd); |
| 416 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int __init alsa_tea575x_module_init(void) |
| 420 | { |
| 421 | return 0; |
| 422 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | static void __exit alsa_tea575x_module_exit(void) |
| 425 | { |
| 426 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | module_init(alsa_tea575x_module_init) |
| 429 | module_exit(alsa_tea575x_module_exit) |
| 430 | |
| 431 | EXPORT_SYMBOL(snd_tea575x_init); |
| 432 | EXPORT_SYMBOL(snd_tea575x_exit); |