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> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/init.h> |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 27 | #include <linux/version.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include <sound/tea575x-tuner.h> |
| 30 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 31 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips"); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 35 | static int radio_nr = -1; |
| 36 | module_param(radio_nr, int, 0); |
| 37 | |
| 38 | #define RADIO_VERSION KERNEL_VERSION(0, 0, 2) |
| 39 | #define FREQ_LO (87 * 16000) |
| 40 | #define FREQ_HI (108 * 16000) |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
| 43 | * definitions |
| 44 | */ |
| 45 | |
| 46 | #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */ |
| 47 | #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */ |
| 48 | #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */ |
| 49 | #define TEA575X_BIT_BAND_MASK (3<<20) |
| 50 | #define TEA575X_BIT_BAND_FM (0<<20) |
| 51 | #define TEA575X_BIT_BAND_MW (1<<20) |
| 52 | #define TEA575X_BIT_BAND_LW (1<<21) |
| 53 | #define TEA575X_BIT_BAND_SW (1<<22) |
| 54 | #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */ |
| 55 | #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */ |
| 56 | #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */ |
| 57 | #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */ |
| 58 | #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */ |
| 59 | #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */ |
| 60 | #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */ |
| 61 | #define TEA575X_BIT_DUMMY (1<<15) /* buffer */ |
| 62 | #define TEA575X_BIT_FREQ_MASK 0x7fff |
| 63 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 64 | static struct v4l2_queryctrl radio_qctrl[] = { |
| 65 | { |
| 66 | .id = V4L2_CID_AUDIO_MUTE, |
| 67 | .name = "Mute", |
| 68 | .minimum = 0, |
| 69 | .maximum = 1, |
| 70 | .default_value = 1, |
| 71 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 72 | } |
| 73 | }; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
| 76 | * lowlevel part |
| 77 | */ |
| 78 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 79 | static void snd_tea575x_set_freq(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | unsigned long freq; |
| 82 | |
| 83 | freq = tea->freq / 16; /* to kHz */ |
| 84 | if (freq > 108000) |
| 85 | freq = 108000; |
| 86 | if (freq < 87000) |
| 87 | freq = 87000; |
| 88 | /* crystal fixup */ |
| 89 | if (tea->tea5759) |
| 90 | freq -= tea->freq_fixup; |
| 91 | else |
| 92 | freq += tea->freq_fixup; |
| 93 | /* freq /= 12.5 */ |
| 94 | freq *= 10; |
| 95 | freq /= 125; |
| 96 | |
| 97 | tea->val &= ~TEA575X_BIT_FREQ_MASK; |
| 98 | tea->val |= freq & TEA575X_BIT_FREQ_MASK; |
| 99 | tea->ops->write(tea, tea->val); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Linux Video interface |
| 104 | */ |
| 105 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 106 | static int vidioc_querycap(struct file *file, void *priv, |
| 107 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 109 | struct snd_tea575x *tea = video_drvdata(file); |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 111 | strcpy(v->card, tea->tea5759 ? "TEA5759" : "TEA5757"); |
| 112 | strlcpy(v->driver, "tea575x-tuner", sizeof(v->driver)); |
| 113 | strlcpy(v->card, "Maestro Radio", sizeof(v->card)); |
| 114 | sprintf(v->bus_info, "PCI"); |
| 115 | v->version = RADIO_VERSION; |
| 116 | v->capabilities = V4L2_CAP_TUNER; |
| 117 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 120 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 121 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 123 | if (v->index > 0) |
| 124 | return -EINVAL; |
| 125 | |
| 126 | strcpy(v->name, "FM"); |
| 127 | v->type = V4L2_TUNER_RADIO; |
| 128 | v->rangelow = FREQ_LO; |
| 129 | v->rangehigh = FREQ_HI; |
| 130 | v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO; |
| 131 | v->capability = V4L2_TUNER_CAP_LOW; |
| 132 | v->audmode = V4L2_TUNER_MODE_MONO; |
| 133 | v->signal = 0xffff; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 138 | struct v4l2_tuner *v) |
| 139 | { |
| 140 | if (v->index > 0) |
| 141 | return -EINVAL; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 146 | struct v4l2_frequency *f) |
| 147 | { |
| 148 | struct snd_tea575x *tea = video_drvdata(file); |
| 149 | |
| 150 | f->type = V4L2_TUNER_RADIO; |
| 151 | f->frequency = tea->freq; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 156 | struct v4l2_frequency *f) |
| 157 | { |
| 158 | struct snd_tea575x *tea = video_drvdata(file); |
| 159 | |
| 160 | if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | tea->freq = f->frequency; |
| 164 | |
| 165 | snd_tea575x_set_freq(tea); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int vidioc_g_audio(struct file *file, void *priv, |
| 171 | struct v4l2_audio *a) |
| 172 | { |
| 173 | if (a->index > 1) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | strcpy(a->name, "Radio"); |
| 177 | a->capability = V4L2_AUDCAP_STEREO; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int vidioc_s_audio(struct file *file, void *priv, |
| 182 | struct v4l2_audio *a) |
| 183 | { |
| 184 | if (a->index != 0) |
| 185 | return -EINVAL; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 190 | struct v4l2_queryctrl *qc) |
| 191 | { |
| 192 | int i; |
| 193 | |
| 194 | for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) { |
| 195 | if (qc->id && qc->id == radio_qctrl[i].id) { |
| 196 | memcpy(qc, &(radio_qctrl[i]), |
| 197 | sizeof(*qc)); |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 205 | struct v4l2_control *ctrl) |
| 206 | { |
| 207 | struct snd_tea575x *tea = video_drvdata(file); |
| 208 | |
| 209 | switch (ctrl->id) { |
| 210 | case V4L2_CID_AUDIO_MUTE: |
| 211 | if (tea->ops->mute) { |
| 212 | ctrl->value = tea->mute; |
| 213 | return 0; |
| 214 | } |
| 215 | } |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 220 | struct v4l2_control *ctrl) |
| 221 | { |
| 222 | struct snd_tea575x *tea = video_drvdata(file); |
| 223 | |
| 224 | switch (ctrl->id) { |
| 225 | case V4L2_CID_AUDIO_MUTE: |
| 226 | if (tea->ops->mute) { |
| 227 | tea->ops->mute(tea, ctrl->value); |
Ondrej Zary | 1233faa | 2009-11-27 18:19:28 +0100 | [diff] [blame] | 228 | tea->mute = ctrl->value; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | } |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 236 | { |
| 237 | *i = 0; |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 242 | { |
| 243 | if (i != 0) |
| 244 | return -EINVAL; |
| 245 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 248 | static int snd_tea575x_exclusive_open(struct file *file) |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 249 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 250 | struct snd_tea575x *tea = video_drvdata(file); |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 251 | |
| 252 | return test_and_set_bit(0, &tea->in_use) ? -EBUSY : 0; |
| 253 | } |
| 254 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 255 | static int snd_tea575x_exclusive_release(struct file *file) |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 256 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 257 | struct snd_tea575x *tea = video_drvdata(file); |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 258 | |
| 259 | clear_bit(0, &tea->in_use); |
| 260 | return 0; |
| 261 | } |
| 262 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 263 | static const struct v4l2_file_operations tea575x_fops = { |
| 264 | .owner = THIS_MODULE, |
| 265 | .open = snd_tea575x_exclusive_open, |
| 266 | .release = snd_tea575x_exclusive_release, |
| 267 | .ioctl = video_ioctl2, |
| 268 | }; |
| 269 | |
| 270 | static const struct v4l2_ioctl_ops tea575x_ioctl_ops = { |
| 271 | .vidioc_querycap = vidioc_querycap, |
| 272 | .vidioc_g_tuner = vidioc_g_tuner, |
| 273 | .vidioc_s_tuner = vidioc_s_tuner, |
| 274 | .vidioc_g_audio = vidioc_g_audio, |
| 275 | .vidioc_s_audio = vidioc_s_audio, |
| 276 | .vidioc_g_input = vidioc_g_input, |
| 277 | .vidioc_s_input = vidioc_s_input, |
| 278 | .vidioc_g_frequency = vidioc_g_frequency, |
| 279 | .vidioc_s_frequency = vidioc_s_frequency, |
| 280 | .vidioc_queryctrl = vidioc_queryctrl, |
| 281 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 282 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 283 | }; |
| 284 | |
| 285 | static struct video_device tea575x_radio = { |
| 286 | .name = "tea575x-tuner", |
| 287 | .fops = &tea575x_fops, |
| 288 | .ioctl_ops = &tea575x_ioctl_ops, |
| 289 | .release = video_device_release, |
| 290 | }; |
| 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | /* |
| 293 | * initialize all the tea575x chips |
| 294 | */ |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 295 | void snd_tea575x_init(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 297 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | unsigned int val; |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 299 | struct video_device *tea575x_radio_inst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | val = tea->ops->read(tea); |
| 302 | if (val == 0x1ffffff || val == 0) { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 303 | snd_printk(KERN_ERR |
| 304 | "tea575x-tuner: Cannot find TEA575x chip\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 308 | tea->in_use = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40; |
| 310 | tea->freq = 90500 * 16; /* 90.5Mhz default */ |
| 311 | |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 312 | tea575x_radio_inst = video_device_alloc(); |
| 313 | if (tea575x_radio_inst == NULL) { |
| 314 | printk(KERN_ERR "tea575x-tuner: not enough memory\n"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | memcpy(tea575x_radio_inst, &tea575x_radio, sizeof(tea575x_radio)); |
| 319 | |
| 320 | strcpy(tea575x_radio.name, tea->tea5759 ? |
| 321 | "TEA5759 radio" : "TEA5757 radio"); |
| 322 | |
| 323 | video_set_drvdata(tea575x_radio_inst, tea); |
| 324 | |
| 325 | retval = video_register_device(tea575x_radio_inst, |
| 326 | VFL_TYPE_RADIO, radio_nr); |
| 327 | if (retval) { |
| 328 | printk(KERN_ERR "tea575x-tuner: can't register video device!\n"); |
| 329 | kfree(tea575x_radio_inst); |
| 330 | return; |
| 331 | } |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | snd_tea575x_set_freq(tea); |
Andy Shevchenko | 6925212 | 2008-01-24 18:11:53 +0100 | [diff] [blame] | 334 | |
| 335 | /* mute on init */ |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 336 | if (tea->ops->mute) { |
Andy Shevchenko | 6925212 | 2008-01-24 18:11:53 +0100 | [diff] [blame] | 337 | tea->ops->mute(tea, 1); |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 338 | tea->mute = 1; |
| 339 | } |
| 340 | tea->vd = tea575x_radio_inst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 343 | void snd_tea575x_exit(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Mauro Carvalho Chehab | 9b76ede | 2009-02-27 11:51:24 -0300 | [diff] [blame] | 345 | if (tea->vd) { |
| 346 | video_unregister_device(tea->vd); |
| 347 | tea->vd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | static int __init alsa_tea575x_module_init(void) |
| 352 | { |
| 353 | return 0; |
| 354 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | static void __exit alsa_tea575x_module_exit(void) |
| 357 | { |
| 358 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | module_init(alsa_tea575x_module_init) |
| 361 | module_exit(alsa_tea575x_module_exit) |
| 362 | |
| 363 | EXPORT_SYMBOL(snd_tea575x_init); |
| 364 | EXPORT_SYMBOL(snd_tea575x_exit); |