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> |
| 27 | #include <sound/core.h> |
| 28 | #include <sound/tea575x-tuner.h> |
| 29 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 30 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | /* |
| 35 | * definitions |
| 36 | */ |
| 37 | |
| 38 | #define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */ |
| 39 | #define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */ |
| 40 | #define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */ |
| 41 | #define TEA575X_BIT_BAND_MASK (3<<20) |
| 42 | #define TEA575X_BIT_BAND_FM (0<<20) |
| 43 | #define TEA575X_BIT_BAND_MW (1<<20) |
| 44 | #define TEA575X_BIT_BAND_LW (1<<21) |
| 45 | #define TEA575X_BIT_BAND_SW (1<<22) |
| 46 | #define TEA575X_BIT_PORT_0 (1<<19) /* user bit */ |
| 47 | #define TEA575X_BIT_PORT_1 (1<<18) /* user bit */ |
| 48 | #define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */ |
| 49 | #define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */ |
| 50 | #define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */ |
| 51 | #define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */ |
| 52 | #define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */ |
| 53 | #define TEA575X_BIT_DUMMY (1<<15) /* buffer */ |
| 54 | #define TEA575X_BIT_FREQ_MASK 0x7fff |
| 55 | |
| 56 | /* |
| 57 | * lowlevel part |
| 58 | */ |
| 59 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 60 | static void snd_tea575x_set_freq(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | unsigned long freq; |
| 63 | |
| 64 | freq = tea->freq / 16; /* to kHz */ |
| 65 | if (freq > 108000) |
| 66 | freq = 108000; |
| 67 | if (freq < 87000) |
| 68 | freq = 87000; |
| 69 | /* crystal fixup */ |
| 70 | if (tea->tea5759) |
| 71 | freq -= tea->freq_fixup; |
| 72 | else |
| 73 | freq += tea->freq_fixup; |
| 74 | /* freq /= 12.5 */ |
| 75 | freq *= 10; |
| 76 | freq /= 125; |
| 77 | |
| 78 | tea->val &= ~TEA575X_BIT_FREQ_MASK; |
| 79 | tea->val |= freq & TEA575X_BIT_FREQ_MASK; |
| 80 | tea->ops->write(tea, tea->val); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Linux Video interface |
| 85 | */ |
| 86 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame^] | 87 | static int snd_tea575x_ioctl(struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | unsigned int cmd, unsigned long data) |
| 89 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 90 | struct snd_tea575x *tea = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | void __user *arg = (void __user *)data; |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | switch(cmd) { |
| 94 | case VIDIOCGCAP: |
| 95 | { |
| 96 | struct video_capability v; |
| 97 | v.type = VID_TYPE_TUNER; |
| 98 | v.channels = 1; |
| 99 | v.audios = 1; |
| 100 | /* No we don't do pictures */ |
| 101 | v.maxwidth = 0; |
| 102 | v.maxheight = 0; |
| 103 | v.minwidth = 0; |
| 104 | v.minheight = 0; |
| 105 | strcpy(v.name, tea->tea5759 ? "TEA5759" : "TEA5757"); |
| 106 | if (copy_to_user(arg,&v,sizeof(v))) |
| 107 | return -EFAULT; |
| 108 | return 0; |
| 109 | } |
| 110 | case VIDIOCGTUNER: |
| 111 | { |
| 112 | struct video_tuner v; |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 113 | if (copy_from_user(&v, arg,sizeof(v))!=0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return -EFAULT; |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 115 | if (v.tuner) /* Only 1 tuner */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return -EINVAL; |
| 117 | v.rangelow = (87*16000); |
| 118 | v.rangehigh = (108*16000); |
| 119 | v.flags = VIDEO_TUNER_LOW; |
| 120 | v.mode = VIDEO_MODE_AUTO; |
| 121 | strcpy(v.name, "FM"); |
| 122 | v.signal = 0xFFFF; |
| 123 | if (copy_to_user(arg, &v, sizeof(v))) |
| 124 | return -EFAULT; |
| 125 | return 0; |
| 126 | } |
| 127 | case VIDIOCSTUNER: |
| 128 | { |
| 129 | struct video_tuner v; |
| 130 | if(copy_from_user(&v, arg, sizeof(v))) |
| 131 | return -EFAULT; |
| 132 | if(v.tuner!=0) |
| 133 | return -EINVAL; |
| 134 | /* Only 1 tuner so no setting needed ! */ |
| 135 | return 0; |
| 136 | } |
| 137 | case VIDIOCGFREQ: |
| 138 | if(copy_to_user(arg, &tea->freq, sizeof(tea->freq))) |
| 139 | return -EFAULT; |
| 140 | return 0; |
| 141 | case VIDIOCSFREQ: |
| 142 | if(copy_from_user(&tea->freq, arg, sizeof(tea->freq))) |
| 143 | return -EFAULT; |
| 144 | snd_tea575x_set_freq(tea); |
| 145 | return 0; |
| 146 | case VIDIOCGAUDIO: |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | struct video_audio v; |
| 149 | memset(&v, 0, sizeof(v)); |
| 150 | strcpy(v.name, "Radio"); |
| 151 | if(copy_to_user(arg,&v, sizeof(v))) |
| 152 | return -EFAULT; |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 153 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | case VIDIOCSAUDIO: |
| 156 | { |
| 157 | struct video_audio v; |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 158 | if(copy_from_user(&v, arg, sizeof(v))) |
| 159 | return -EFAULT; |
Andy Shevchenko | 6925212 | 2008-01-24 18:11:53 +0100 | [diff] [blame] | 160 | if (tea->ops->mute) |
| 161 | tea->ops->mute(tea, |
| 162 | (v.flags & |
| 163 | VIDEO_AUDIO_MUTE) ? 1 : 0); |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 164 | if(v.audio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | return 0; |
| 167 | } |
| 168 | default: |
| 169 | return -ENOIOCTLCMD; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static void snd_tea575x_release(struct video_device *vfd) |
| 174 | { |
| 175 | } |
| 176 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame^] | 177 | static int snd_tea575x_exclusive_open(struct file *file) |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 178 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 179 | struct snd_tea575x *tea = video_drvdata(file); |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 180 | |
| 181 | return test_and_set_bit(0, &tea->in_use) ? -EBUSY : 0; |
| 182 | } |
| 183 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame^] | 184 | static int snd_tea575x_exclusive_release(struct file *file) |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 185 | { |
Hans Verkuil | c170ecf | 2008-08-23 08:32:09 -0300 | [diff] [blame] | 186 | struct snd_tea575x *tea = video_drvdata(file); |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 187 | |
| 188 | clear_bit(0, &tea->in_use); |
| 189 | return 0; |
| 190 | } |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
| 193 | * initialize all the tea575x chips |
| 194 | */ |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 195 | void snd_tea575x_init(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | unsigned int val; |
| 198 | |
| 199 | val = tea->ops->read(tea); |
| 200 | if (val == 0x1ffffff || val == 0) { |
| 201 | snd_printk(KERN_ERR "Cannot find TEA575x chip\n"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | memset(&tea->vd, 0, sizeof(tea->vd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | strcpy(tea->vd.name, tea->tea5759 ? "TEA5759 radio" : "TEA5757 radio"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | tea->vd.release = snd_tea575x_release; |
| 208 | video_set_drvdata(&tea->vd, tea); |
| 209 | tea->vd.fops = &tea->fops; |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 210 | tea->in_use = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | tea->fops.owner = tea->card->module; |
Hans Verkuil | 2f3d002 | 2008-08-23 04:52:00 -0300 | [diff] [blame] | 212 | tea->fops.open = snd_tea575x_exclusive_open; |
| 213 | tea->fops.release = snd_tea575x_exclusive_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | tea->fops.ioctl = snd_tea575x_ioctl; |
| 215 | if (video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->dev_nr - 1) < 0) { |
| 216 | snd_printk(KERN_ERR "unable to register tea575x tuner\n"); |
| 217 | return; |
| 218 | } |
| 219 | tea->vd_registered = 1; |
| 220 | |
| 221 | tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40; |
| 222 | tea->freq = 90500 * 16; /* 90.5Mhz default */ |
| 223 | |
| 224 | snd_tea575x_set_freq(tea); |
Andy Shevchenko | 6925212 | 2008-01-24 18:11:53 +0100 | [diff] [blame] | 225 | |
| 226 | /* mute on init */ |
| 227 | if (tea->ops->mute) |
| 228 | tea->ops->mute(tea, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 231 | void snd_tea575x_exit(struct snd_tea575x *tea) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | if (tea->vd_registered) { |
| 234 | video_unregister_device(&tea->vd); |
| 235 | tea->vd_registered = 0; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static int __init alsa_tea575x_module_init(void) |
| 240 | { |
| 241 | return 0; |
| 242 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | static void __exit alsa_tea575x_module_exit(void) |
| 245 | { |
| 246 | } |
Igor M. Liplianin | 4b29631 | 2008-11-09 15:25:31 -0300 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | module_init(alsa_tea575x_module_init) |
| 249 | module_exit(alsa_tea575x_module_exit) |
| 250 | |
| 251 | EXPORT_SYMBOL(snd_tea575x_init); |
| 252 | EXPORT_SYMBOL(snd_tea575x_exit); |