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 <linux/delay.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 27 | #include <linux/sched.h> |
Mauro Carvalho Chehab | eab924d | 2014-01-13 15:54:59 -0200 | [diff] [blame] | 28 | #include <asm/io.h> |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 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> |
Ondrej Zary | 59b5645 | 2013-07-28 16:01:43 -0300 | [diff] [blame] | 34 | #include <media/tea575x.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 40 | /* |
| 41 | * definitions |
| 42 | */ |
| 43 | |
| 44 | #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */ |
| 45 | #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */ |
| 46 | #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */ |
| 47 | #define TEA575X_BIT_BAND_MASK (3<<20) |
| 48 | #define TEA575X_BIT_BAND_FM (0<<20) |
| 49 | #define TEA575X_BIT_BAND_MW (1<<20) |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 50 | #define TEA575X_BIT_BAND_LW (2<<20) |
| 51 | #define TEA575X_BIT_BAND_SW (3<<20) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */ |
| 53 | #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */ |
| 54 | #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */ |
| 55 | #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */ |
| 56 | #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */ |
| 57 | #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */ |
| 58 | #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */ |
| 59 | #define TEA575X_BIT_DUMMY (1<<15) /* buffer */ |
| 60 | #define TEA575X_BIT_FREQ_MASK 0x7fff |
| 61 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 62 | enum { BAND_FM, BAND_FM_JAPAN, BAND_AM }; |
| 63 | |
| 64 | static const struct v4l2_frequency_band bands[] = { |
| 65 | { |
| 66 | .type = V4L2_TUNER_RADIO, |
| 67 | .index = 0, |
| 68 | .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | |
| 69 | V4L2_TUNER_CAP_FREQ_BANDS, |
| 70 | .rangelow = 87500 * 16, |
| 71 | .rangehigh = 108000 * 16, |
| 72 | .modulation = V4L2_BAND_MODULATION_FM, |
| 73 | }, |
| 74 | { |
| 75 | .type = V4L2_TUNER_RADIO, |
| 76 | .index = 0, |
| 77 | .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | |
| 78 | V4L2_TUNER_CAP_FREQ_BANDS, |
| 79 | .rangelow = 76000 * 16, |
| 80 | .rangehigh = 91000 * 16, |
| 81 | .modulation = V4L2_BAND_MODULATION_FM, |
| 82 | }, |
| 83 | { |
| 84 | .type = V4L2_TUNER_RADIO, |
| 85 | .index = 1, |
| 86 | .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS, |
| 87 | .rangelow = 530 * 16, |
| 88 | .rangehigh = 1710 * 16, |
| 89 | .modulation = V4L2_BAND_MODULATION_AM, |
| 90 | }, |
| 91 | }; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* |
| 94 | * lowlevel part |
| 95 | */ |
| 96 | |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 97 | static void snd_tea575x_write(struct snd_tea575x *tea, unsigned int val) |
| 98 | { |
| 99 | u16 l; |
| 100 | u8 data; |
| 101 | |
Hans de Goede | 31a62d4 | 2012-05-18 09:36:17 -0300 | [diff] [blame] | 102 | if (tea->ops->write_val) |
| 103 | return tea->ops->write_val(tea, val); |
| 104 | |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 105 | tea->ops->set_direction(tea, 1); |
| 106 | udelay(16); |
| 107 | |
| 108 | for (l = 25; l > 0; l--) { |
| 109 | data = (val >> 24) & TEA575X_DATA; |
| 110 | val <<= 1; /* shift data */ |
| 111 | tea->ops->set_pins(tea, data | TEA575X_WREN); |
| 112 | udelay(2); |
| 113 | tea->ops->set_pins(tea, data | TEA575X_WREN | TEA575X_CLK); |
| 114 | udelay(2); |
| 115 | tea->ops->set_pins(tea, data | TEA575X_WREN); |
| 116 | udelay(2); |
| 117 | } |
| 118 | |
| 119 | if (!tea->mute) |
| 120 | tea->ops->set_pins(tea, 0); |
| 121 | } |
| 122 | |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 123 | static u32 snd_tea575x_read(struct snd_tea575x *tea) |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 124 | { |
| 125 | u16 l, rdata; |
| 126 | u32 data = 0; |
| 127 | |
Hans de Goede | 31a62d4 | 2012-05-18 09:36:17 -0300 | [diff] [blame] | 128 | if (tea->ops->read_val) |
| 129 | return tea->ops->read_val(tea); |
| 130 | |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 131 | tea->ops->set_direction(tea, 0); |
| 132 | tea->ops->set_pins(tea, 0); |
| 133 | udelay(16); |
| 134 | |
| 135 | for (l = 24; l--;) { |
| 136 | tea->ops->set_pins(tea, TEA575X_CLK); |
| 137 | udelay(2); |
| 138 | if (!l) |
| 139 | tea->tuned = tea->ops->get_pins(tea) & TEA575X_MOST ? 0 : 1; |
| 140 | tea->ops->set_pins(tea, 0); |
| 141 | udelay(2); |
| 142 | data <<= 1; /* shift data */ |
| 143 | rdata = tea->ops->get_pins(tea); |
| 144 | if (!l) |
| 145 | tea->stereo = (rdata & TEA575X_MOST) ? 0 : 1; |
| 146 | if (rdata & TEA575X_DATA) |
| 147 | data++; |
| 148 | udelay(2); |
| 149 | } |
| 150 | |
| 151 | if (tea->mute) |
| 152 | tea->ops->set_pins(tea, TEA575X_WREN); |
| 153 | |
| 154 | return data; |
| 155 | } |
| 156 | |
Hans de Goede | 40e006a | 2012-05-19 11:19:06 -0300 | [diff] [blame] | 157 | static u32 snd_tea575x_val_to_freq(struct snd_tea575x *tea, u32 val) |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 158 | { |
Hans de Goede | 40e006a | 2012-05-19 11:19:06 -0300 | [diff] [blame] | 159 | u32 freq = val & TEA575X_BIT_FREQ_MASK; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 160 | |
| 161 | if (freq == 0) |
| 162 | return freq; |
| 163 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 164 | switch (tea->band) { |
| 165 | case BAND_FM: |
| 166 | /* freq *= 12.5 */ |
| 167 | freq *= 125; |
| 168 | freq /= 10; |
| 169 | /* crystal fixup */ |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 170 | freq -= TEA575X_FMIF; |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 171 | break; |
| 172 | case BAND_FM_JAPAN: |
| 173 | /* freq *= 12.5 */ |
| 174 | freq *= 125; |
| 175 | freq /= 10; |
| 176 | /* crystal fixup */ |
| 177 | freq += TEA575X_FMIF; |
| 178 | break; |
| 179 | case BAND_AM: |
| 180 | /* crystal fixup */ |
| 181 | freq -= TEA575X_AMIF; |
| 182 | break; |
| 183 | } |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 184 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 185 | return clamp(freq * 16, bands[tea->band].rangelow, |
| 186 | bands[tea->band].rangehigh); /* from kHz */ |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 187 | } |
| 188 | |
Hans de Goede | 40e006a | 2012-05-19 11:19:06 -0300 | [diff] [blame] | 189 | static u32 snd_tea575x_get_freq(struct snd_tea575x *tea) |
| 190 | { |
| 191 | return snd_tea575x_val_to_freq(tea, snd_tea575x_read(tea)); |
| 192 | } |
| 193 | |
Hans de Goede | 559c200 | 2012-08-11 12:55:22 -0300 | [diff] [blame] | 194 | void snd_tea575x_set_freq(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 196 | u32 freq = tea->freq / 16; /* to kHz */ |
| 197 | u32 band = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 199 | switch (tea->band) { |
| 200 | case BAND_FM: |
| 201 | band = TEA575X_BIT_BAND_FM; |
| 202 | /* crystal fixup */ |
Ondrej Zary | ea27316 | 2011-05-12 22:17:56 +0200 | [diff] [blame] | 203 | freq += TEA575X_FMIF; |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 204 | /* freq /= 12.5 */ |
| 205 | freq *= 10; |
| 206 | freq /= 125; |
| 207 | break; |
| 208 | case BAND_FM_JAPAN: |
| 209 | band = TEA575X_BIT_BAND_FM; |
| 210 | /* crystal fixup */ |
| 211 | freq -= TEA575X_FMIF; |
| 212 | /* freq /= 12.5 */ |
| 213 | freq *= 10; |
| 214 | freq /= 125; |
| 215 | break; |
| 216 | case BAND_AM: |
| 217 | band = TEA575X_BIT_BAND_MW; |
| 218 | /* crystal fixup */ |
| 219 | freq += TEA575X_AMIF; |
| 220 | break; |
| 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 223 | tea->val &= ~(TEA575X_BIT_FREQ_MASK | TEA575X_BIT_BAND_MASK); |
| 224 | tea->val |= band; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | tea->val |= freq & TEA575X_BIT_FREQ_MASK; |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 226 | snd_tea575x_write(tea, tea->val); |
Hans de Goede | 40e006a | 2012-05-19 11:19:06 -0300 | [diff] [blame] | 227 | tea->freq = snd_tea575x_val_to_freq(tea, tea->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Linux Video interface |
| 232 | */ |
| 233 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 234 | static int vidioc_querycap(struct file *file, void *priv, |
| 235 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 237 | struct snd_tea575x *tea = video_drvdata(file); |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 238 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 239 | strlcpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver)); |
Ondrej Zary | 10ca720 | 2011-05-12 22:18:22 +0200 | [diff] [blame] | 240 | strlcpy(v->card, tea->card, sizeof(v->card)); |
| 241 | strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card)); |
| 242 | strlcpy(v->bus_info, tea->bus_info, sizeof(v->bus_info)); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 243 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 244 | if (!tea->cannot_read_data) |
| 245 | v->device_caps |= V4L2_CAP_HW_FREQ_SEEK; |
| 246 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 247 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 250 | static int vidioc_enum_freq_bands(struct file *file, void *priv, |
| 251 | struct v4l2_frequency_band *band) |
| 252 | { |
| 253 | struct snd_tea575x *tea = video_drvdata(file); |
| 254 | int index; |
| 255 | |
| 256 | if (band->tuner != 0) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | switch (band->index) { |
| 260 | case 0: |
| 261 | if (tea->tea5759) |
| 262 | index = BAND_FM_JAPAN; |
| 263 | else |
| 264 | index = BAND_FM; |
| 265 | break; |
| 266 | case 1: |
| 267 | if (tea->has_am) { |
| 268 | index = BAND_AM; |
| 269 | break; |
| 270 | } |
| 271 | /* Fall through */ |
| 272 | default: |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | *band = bands[index]; |
| 277 | if (!tea->cannot_read_data) |
| 278 | band->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 283 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 284 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 286 | struct snd_tea575x *tea = video_drvdata(file); |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 287 | struct v4l2_frequency_band band_fm = { 0, }; |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 288 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 289 | if (v->index > 0) |
| 290 | return -EINVAL; |
| 291 | |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 292 | snd_tea575x_read(tea); |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 293 | vidioc_enum_freq_bands(file, priv, &band_fm); |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 294 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 295 | memset(v, 0, sizeof(*v)); |
| 296 | strlcpy(v->name, tea->has_am ? "FM/AM" : "FM", sizeof(v->name)); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 297 | v->type = V4L2_TUNER_RADIO; |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 298 | v->capability = band_fm.capability; |
| 299 | v->rangelow = tea->has_am ? bands[BAND_AM].rangelow : band_fm.rangelow; |
| 300 | v->rangehigh = band_fm.rangehigh; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 301 | v->rxsubchans = tea->stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; |
| 302 | v->audmode = (tea->val & TEA575X_BIT_MONO) ? |
| 303 | V4L2_TUNER_MODE_MONO : V4L2_TUNER_MODE_STEREO; |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 304 | v->signal = tea->tuned ? 0xffff : 0; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int vidioc_s_tuner(struct file *file, void *priv, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame] | 309 | const struct v4l2_tuner *v) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 310 | { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 311 | struct snd_tea575x *tea = video_drvdata(file); |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 312 | u32 orig_val = tea->val; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 313 | |
| 314 | if (v->index) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 315 | return -EINVAL; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 316 | tea->val &= ~TEA575X_BIT_MONO; |
| 317 | if (v->audmode == V4L2_TUNER_MODE_MONO) |
| 318 | tea->val |= TEA575X_BIT_MONO; |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 319 | /* Only apply changes if currently tuning FM */ |
| 320 | if (tea->band != BAND_AM && tea->val != orig_val) |
| 321 | snd_tea575x_set_freq(tea); |
| 322 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 327 | struct v4l2_frequency *f) |
| 328 | { |
| 329 | struct snd_tea575x *tea = video_drvdata(file); |
| 330 | |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 331 | if (f->tuner != 0) |
| 332 | return -EINVAL; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 333 | f->type = V4L2_TUNER_RADIO; |
| 334 | f->frequency = tea->freq; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int vidioc_s_frequency(struct file *file, void *priv, |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 339 | const struct v4l2_frequency *f) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 340 | { |
| 341 | struct snd_tea575x *tea = video_drvdata(file); |
| 342 | |
Ondrej Zary | 375d135 | 2011-03-19 16:32:53 +0100 | [diff] [blame] | 343 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
| 344 | return -EINVAL; |
| 345 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 346 | if (tea->has_am && f->frequency < (20000 * 16)) |
| 347 | tea->band = BAND_AM; |
| 348 | else if (tea->tea5759) |
| 349 | tea->band = BAND_FM_JAPAN; |
| 350 | else |
| 351 | tea->band = BAND_FM; |
| 352 | |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 353 | tea->freq = clamp_t(u32, f->frequency, bands[tea->band].rangelow, |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 354 | bands[tea->band].rangehigh); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 355 | snd_tea575x_set_freq(tea); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 359 | static int vidioc_s_hw_freq_seek(struct file *file, void *fh, |
Hans Verkuil | ec6f432 | 2012-09-14 07:41:18 -0300 | [diff] [blame] | 360 | const struct v4l2_hw_freq_seek *a) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 361 | { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 362 | struct snd_tea575x *tea = video_drvdata(file); |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 363 | unsigned long timeout; |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 364 | int i, spacing; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 365 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 366 | if (tea->cannot_read_data) |
| 367 | return -ENOTTY; |
| 368 | if (a->tuner || a->wrap_around) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 369 | return -EINVAL; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 370 | |
Hans Verkuil | 617ade6 | 2012-09-21 09:33:35 -0300 | [diff] [blame] | 371 | if (file->f_flags & O_NONBLOCK) |
| 372 | return -EWOULDBLOCK; |
| 373 | |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 374 | if (a->rangelow || a->rangehigh) { |
| 375 | for (i = 0; i < ARRAY_SIZE(bands); i++) { |
| 376 | if ((i == BAND_FM && tea->tea5759) || |
| 377 | (i == BAND_FM_JAPAN && !tea->tea5759) || |
| 378 | (i == BAND_AM && !tea->has_am)) |
| 379 | continue; |
| 380 | if (bands[i].rangelow == a->rangelow && |
| 381 | bands[i].rangehigh == a->rangehigh) |
| 382 | break; |
| 383 | } |
| 384 | if (i == ARRAY_SIZE(bands)) |
| 385 | return -EINVAL; /* No matching band found */ |
| 386 | if (i != tea->band) { |
| 387 | tea->band = i; |
| 388 | tea->freq = clamp(tea->freq, bands[i].rangelow, |
| 389 | bands[i].rangehigh); |
| 390 | snd_tea575x_set_freq(tea); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | spacing = (tea->band == BAND_AM) ? 5 : 50; /* kHz */ |
| 395 | |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 396 | /* clear the frequency, HW will fill it in */ |
| 397 | tea->val &= ~TEA575X_BIT_FREQ_MASK; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 398 | tea->val |= TEA575X_BIT_SEARCH; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 399 | if (a->seek_upward) |
| 400 | tea->val |= TEA575X_BIT_UPDOWN; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 401 | else |
| 402 | tea->val &= ~TEA575X_BIT_UPDOWN; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 403 | snd_tea575x_write(tea, tea->val); |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 404 | timeout = jiffies + msecs_to_jiffies(10000); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 405 | for (;;) { |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 406 | if (time_after(jiffies, timeout)) |
| 407 | break; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 408 | if (schedule_timeout_interruptible(msecs_to_jiffies(10))) { |
| 409 | /* some signal arrived, stop search */ |
| 410 | tea->val &= ~TEA575X_BIT_SEARCH; |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 411 | snd_tea575x_set_freq(tea); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 412 | return -ERESTARTSYS; |
| 413 | } |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 414 | if (!(snd_tea575x_read(tea) & TEA575X_BIT_SEARCH)) { |
| 415 | u32 freq; |
| 416 | |
| 417 | /* Found a frequency, wait until it can be read */ |
| 418 | for (i = 0; i < 100; i++) { |
| 419 | msleep(10); |
| 420 | freq = snd_tea575x_get_freq(tea); |
| 421 | if (freq) /* available */ |
| 422 | break; |
| 423 | } |
| 424 | if (freq == 0) /* shouldn't happen */ |
| 425 | break; |
| 426 | /* |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 427 | * if we moved by less than the spacing, or in the |
| 428 | * wrong direction, continue seeking |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 429 | */ |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 430 | if (abs(tea->freq - freq) < 16 * spacing || |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 431 | (a->seek_upward && freq < tea->freq) || |
| 432 | (!a->seek_upward && freq > tea->freq)) { |
| 433 | snd_tea575x_write(tea, tea->val); |
| 434 | continue; |
| 435 | } |
| 436 | tea->freq = freq; |
| 437 | tea->val &= ~TEA575X_BIT_SEARCH; |
| 438 | return 0; |
| 439 | } |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 440 | } |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 441 | tea->val &= ~TEA575X_BIT_SEARCH; |
| 442 | snd_tea575x_set_freq(tea); |
Hans Verkuil | 54f6019 | 2012-05-27 07:25:06 -0300 | [diff] [blame] | 443 | return -ENODATA; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 444 | } |
| 445 | |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 446 | static int tea575x_s_ctrl(struct v4l2_ctrl *ctrl) |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 447 | { |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 448 | 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] | 449 | |
| 450 | switch (ctrl->id) { |
| 451 | case V4L2_CID_AUDIO_MUTE: |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 452 | tea->mute = ctrl->val; |
| 453 | snd_tea575x_set_freq(tea); |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 454 | return 0; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 455 | } |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 456 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 457 | return -EINVAL; |
| 458 | } |
| 459 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 460 | static const struct v4l2_file_operations tea575x_fops = { |
Ondrej Zary | 6a529c1 | 2011-06-11 10:28:59 -0300 | [diff] [blame] | 461 | .unlocked_ioctl = video_ioctl2, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 462 | .open = v4l2_fh_open, |
| 463 | .release = v4l2_fh_release, |
| 464 | .poll = v4l2_ctrl_poll, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | static const struct v4l2_ioctl_ops tea575x_ioctl_ops = { |
| 468 | .vidioc_querycap = vidioc_querycap, |
| 469 | .vidioc_g_tuner = vidioc_g_tuner, |
| 470 | .vidioc_s_tuner = vidioc_s_tuner, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 471 | .vidioc_g_frequency = vidioc_g_frequency, |
| 472 | .vidioc_s_frequency = vidioc_s_frequency, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 473 | .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek, |
Hans de Goede | fc48851 | 2012-07-12 17:39:18 -0300 | [diff] [blame] | 474 | .vidioc_enum_freq_bands = vidioc_enum_freq_bands, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 475 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 476 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 477 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 478 | }; |
| 479 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 480 | static const struct video_device tea575x_radio = { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 481 | .ioctl_ops = &tea575x_ioctl_ops, |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 482 | .release = video_device_release_empty, |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | static const struct v4l2_ctrl_ops tea575x_ctrl_ops = { |
| 486 | .s_ctrl = tea575x_s_ctrl, |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 487 | }; |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Ondrej Zary | 8fd7957 | 2013-05-14 16:54:43 -0300 | [diff] [blame] | 490 | int snd_tea575x_hw_init(struct snd_tea575x *tea) |
| 491 | { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 492 | tea->mute = true; |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 493 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 494 | /* Not all devices can or know how to read the data back. |
| 495 | Such devices can set cannot_read_data to true. */ |
| 496 | if (!tea->cannot_read_data) { |
| 497 | snd_tea575x_write(tea, 0x55AA); |
| 498 | if (snd_tea575x_read(tea) != 0x55AA) |
| 499 | return -ENODEV; |
| 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Hans Verkuil | bc2b395 | 2012-02-27 05:31:58 -0300 | [diff] [blame] | 502 | tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_5_28; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | tea->freq = 90500 * 16; /* 90.5Mhz default */ |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 504 | snd_tea575x_set_freq(tea); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Ondrej Zary | 8fd7957 | 2013-05-14 16:54:43 -0300 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | EXPORT_SYMBOL(snd_tea575x_hw_init); |
| 509 | |
| 510 | int snd_tea575x_init(struct snd_tea575x *tea, struct module *owner) |
| 511 | { |
| 512 | int retval = snd_tea575x_hw_init(tea); |
| 513 | |
| 514 | if (retval) |
| 515 | return retval; |
| 516 | |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 517 | tea->vd = tea575x_radio; |
| 518 | video_set_drvdata(&tea->vd, tea); |
Ondrej Zary | 6a529c1 | 2011-06-11 10:28:59 -0300 | [diff] [blame] | 519 | mutex_init(&tea->mutex); |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 520 | strlcpy(tea->vd.name, tea->v4l2_dev->name, sizeof(tea->vd.name)); |
Ondrej Zary | 6a529c1 | 2011-06-11 10:28:59 -0300 | [diff] [blame] | 521 | tea->vd.lock = &tea->mutex; |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 522 | tea->vd.v4l2_dev = tea->v4l2_dev; |
Hans de Goede | 5daf53a | 2012-05-19 07:57:03 -0300 | [diff] [blame] | 523 | tea->fops = tea575x_fops; |
| 524 | tea->fops.owner = owner; |
| 525 | tea->vd.fops = &tea->fops; |
Hans Verkuil | 6539799 | 2012-05-10 03:16:14 -0300 | [diff] [blame] | 526 | /* disable hw_freq_seek if we can't use it */ |
| 527 | if (tea->cannot_read_data) |
Hans Verkuil | 152a3a7 | 2012-05-14 11:32:48 -0300 | [diff] [blame] | 528 | v4l2_disable_ioctl(&tea->vd, VIDIOC_S_HW_FREQ_SEEK); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 529 | |
Hans de Goede | 3d0fe51 | 2012-05-18 12:21:57 -0300 | [diff] [blame] | 530 | if (!tea->cannot_mute) { |
| 531 | tea->vd.ctrl_handler = &tea->ctrl_handler; |
| 532 | v4l2_ctrl_handler_init(&tea->ctrl_handler, 1); |
| 533 | v4l2_ctrl_new_std(&tea->ctrl_handler, &tea575x_ctrl_ops, |
| 534 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); |
| 535 | retval = tea->ctrl_handler.error; |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 536 | if (retval) { |
Hans de Goede | 3d0fe51 | 2012-05-18 12:21:57 -0300 | [diff] [blame] | 537 | v4l2_err(tea->v4l2_dev, "can't initialize controls\n"); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 538 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
| 539 | return retval; |
| 540 | } |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 541 | |
Hans de Goede | 3d0fe51 | 2012-05-18 12:21:57 -0300 | [diff] [blame] | 542 | if (tea->ext_init) { |
| 543 | retval = tea->ext_init(tea); |
| 544 | if (retval) { |
| 545 | v4l2_ctrl_handler_free(&tea->ctrl_handler); |
| 546 | return retval; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | v4l2_ctrl_handler_setup(&tea->ctrl_handler); |
| 551 | } |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 552 | |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 553 | retval = video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->radio_nr); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 554 | if (retval) { |
Hans Verkuil | d4ecc83 | 2012-02-27 05:30:13 -0300 | [diff] [blame] | 555 | v4l2_err(tea->v4l2_dev, "can't register video device!\n"); |
Hans de Goede | 3d0fe51 | 2012-05-18 12:21:57 -0300 | [diff] [blame] | 556 | v4l2_ctrl_handler_free(tea->vd.ctrl_handler); |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 557 | return retval; |
| 558 | } |
Ondrej Zary | 14219d0 | 2011-05-09 23:39:26 +0200 | [diff] [blame] | 559 | |
| 560 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 563 | void snd_tea575x_exit(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Ondrej Zary | 4522e82 | 2011-05-23 09:17:19 -0300 | [diff] [blame] | 565 | video_unregister_device(&tea->vd); |
Hans de Goede | 3d0fe51 | 2012-05-18 12:21:57 -0300 | [diff] [blame] | 566 | v4l2_ctrl_handler_free(tea->vd.ctrl_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static int __init alsa_tea575x_module_init(void) |
| 570 | { |
| 571 | return 0; |
| 572 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | static void __exit alsa_tea575x_module_exit(void) |
| 575 | { |
| 576 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | module_init(alsa_tea575x_module_init) |
| 579 | module_exit(alsa_tea575x_module_exit) |
| 580 | |
| 581 | EXPORT_SYMBOL(snd_tea575x_init); |
| 582 | EXPORT_SYMBOL(snd_tea575x_exit); |
Hans de Goede | 559c200 | 2012-08-11 12:55:22 -0300 | [diff] [blame] | 583 | EXPORT_SYMBOL(snd_tea575x_set_freq); |