Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 1 | /* Miro PCM20 radio driver for Linux radio support |
| 2 | * (c) 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl> |
| 3 | * Thanks to Norberto Pellici for the ACI device interface specification |
| 4 | * The API part is based on the radiotrack driver by M. Kirkwood |
| 5 | * This driver relies on the aci mixer provided by the snd-miro |
| 6 | * ALSA driver. |
| 7 | * Look there for further info... |
| 8 | */ |
| 9 | |
| 10 | /* What ever you think about the ACI, version 0x07 is not very well! |
| 11 | * I can't get frequency, 'tuner status', 'tuner flags' or mute/mono |
| 12 | * conditions... Robert |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/videodev2.h> |
| 18 | #include <media/v4l2-device.h> |
| 19 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 20 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | f7c096f | 2013-01-30 05:54:33 -0300 | [diff] [blame] | 21 | #include <media/v4l2-fh.h> |
| 22 | #include <media/v4l2-event.h> |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 23 | #include <sound/aci.h> |
| 24 | |
| 25 | static int radio_nr = -1; |
| 26 | module_param(radio_nr, int, 0); |
| 27 | MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)"); |
| 28 | |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 29 | struct pcm20 { |
| 30 | struct v4l2_device v4l2_dev; |
| 31 | struct video_device vdev; |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 32 | struct v4l2_ctrl_handler ctrl_handler; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 33 | unsigned long freq; |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 34 | u32 audmode; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 35 | struct snd_miro_aci *aci; |
Hans Verkuil | 32958fd | 2010-11-14 09:36:23 -0300 | [diff] [blame] | 36 | struct mutex lock; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static struct pcm20 pcm20_card = { |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 40 | .freq = 87 * 16000, |
| 41 | .audmode = V4L2_TUNER_MODE_STEREO, |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 44 | static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq) |
| 45 | { |
| 46 | unsigned char freql; |
| 47 | unsigned char freqh; |
| 48 | struct snd_miro_aci *aci = dev->aci; |
| 49 | |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 50 | freq /= 160; |
| 51 | if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0)) |
| 52 | freq /= 10; /* I don't know exactly which version |
| 53 | * needs this hack */ |
| 54 | freql = freq & 0xff; |
| 55 | freqh = freq >> 8; |
| 56 | |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 57 | return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh); |
| 58 | } |
| 59 | |
| 60 | static const struct v4l2_file_operations pcm20_fops = { |
| 61 | .owner = THIS_MODULE, |
Hans Verkuil | f7c096f | 2013-01-30 05:54:33 -0300 | [diff] [blame] | 62 | .open = v4l2_fh_open, |
| 63 | .poll = v4l2_ctrl_poll, |
| 64 | .release = v4l2_fh_release, |
Hans Verkuil | 32958fd | 2010-11-14 09:36:23 -0300 | [diff] [blame] | 65 | .unlocked_ioctl = video_ioctl2, |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static int vidioc_querycap(struct file *file, void *priv, |
| 69 | struct v4l2_capability *v) |
| 70 | { |
Hans Verkuil | f122d9a | 2013-01-30 11:26:36 -0300 | [diff] [blame] | 71 | struct pcm20 *dev = video_drvdata(file); |
| 72 | |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 73 | strlcpy(v->driver, "Miro PCM20", sizeof(v->driver)); |
| 74 | strlcpy(v->card, "Miro PCM20", sizeof(v->card)); |
Hans Verkuil | f122d9a | 2013-01-30 11:26:36 -0300 | [diff] [blame] | 75 | snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", dev->v4l2_dev.name); |
| 76 | v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
| 77 | v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 82 | struct v4l2_tuner *v) |
| 83 | { |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 84 | struct pcm20 *dev = video_drvdata(file); |
Hans Verkuil | 9bbc582 | 2012-05-28 11:10:01 -0300 | [diff] [blame] | 85 | int res; |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 86 | |
| 87 | if (v->index) |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 88 | return -EINVAL; |
| 89 | strlcpy(v->name, "FM", sizeof(v->name)); |
| 90 | v->type = V4L2_TUNER_RADIO; |
| 91 | v->rangelow = 87*16000; |
| 92 | v->rangehigh = 108*16000; |
Hans Verkuil | 9bbc582 | 2012-05-28 11:10:01 -0300 | [diff] [blame] | 93 | res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTATION, -1, -1); |
| 94 | v->signal = (res & 0x80) ? 0 : 0xffff; |
| 95 | /* Note: stereo detection does not work if the audio is muted, |
| 96 | it will default to mono in that case. */ |
| 97 | res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTEREO, -1, -1); |
| 98 | v->rxsubchans = (res & 0x40) ? V4L2_TUNER_SUB_MONO : |
| 99 | V4L2_TUNER_SUB_STEREO; |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 100 | v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; |
| 101 | v->audmode = dev->audmode; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int vidioc_s_tuner(struct file *file, void *priv, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame^] | 106 | const struct v4l2_tuner *v) |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 107 | { |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 108 | struct pcm20 *dev = video_drvdata(file); |
| 109 | |
| 110 | if (v->index) |
| 111 | return -EINVAL; |
| 112 | if (v->audmode > V4L2_TUNER_MODE_STEREO) |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame^] | 113 | dev->audmode = V4L2_TUNER_MODE_STEREO; |
| 114 | else |
| 115 | dev->audmode = v->audmode; |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 116 | snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame^] | 117 | dev->audmode == V4L2_TUNER_MODE_MONO, -1); |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 118 | return 0; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 122 | struct v4l2_frequency *f) |
| 123 | { |
| 124 | struct pcm20 *dev = video_drvdata(file); |
| 125 | |
| 126 | if (f->tuner != 0) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | f->type = V4L2_TUNER_RADIO; |
| 130 | f->frequency = dev->freq; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static int vidioc_s_frequency(struct file *file, void *priv, |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 136 | const struct v4l2_frequency *f) |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 137 | { |
| 138 | struct pcm20 *dev = video_drvdata(file); |
| 139 | |
| 140 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
| 141 | return -EINVAL; |
| 142 | |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 143 | dev->freq = clamp_t(u32, f->frequency, 87 * 16000U, 108 * 16000U); |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 144 | pcm20_setfreq(dev, dev->freq); |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 148 | static int pcm20_s_ctrl(struct v4l2_ctrl *ctrl) |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 149 | { |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 150 | struct pcm20 *dev = container_of(ctrl->handler, struct pcm20, ctrl_handler); |
| 151 | |
| 152 | switch (ctrl->id) { |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 153 | case V4L2_CID_AUDIO_MUTE: |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 154 | snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, ctrl->val, -1); |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 155 | return 0; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 156 | } |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 160 | static const struct v4l2_ioctl_ops pcm20_ioctl_ops = { |
| 161 | .vidioc_querycap = vidioc_querycap, |
| 162 | .vidioc_g_tuner = vidioc_g_tuner, |
| 163 | .vidioc_s_tuner = vidioc_s_tuner, |
| 164 | .vidioc_g_frequency = vidioc_g_frequency, |
| 165 | .vidioc_s_frequency = vidioc_s_frequency, |
Hans Verkuil | f7c096f | 2013-01-30 05:54:33 -0300 | [diff] [blame] | 166 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 167 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 168 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | static const struct v4l2_ctrl_ops pcm20_ctrl_ops = { |
| 172 | .s_ctrl = pcm20_s_ctrl, |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | static int __init pcm20_init(void) |
| 176 | { |
| 177 | struct pcm20 *dev = &pcm20_card; |
| 178 | struct v4l2_device *v4l2_dev = &dev->v4l2_dev; |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 179 | struct v4l2_ctrl_handler *hdl; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 180 | int res; |
| 181 | |
| 182 | dev->aci = snd_aci_get_aci(); |
| 183 | if (dev->aci == NULL) { |
| 184 | v4l2_err(v4l2_dev, |
| 185 | "you must load the snd-miro driver first!\n"); |
| 186 | return -ENODEV; |
| 187 | } |
Hans Verkuil | f122d9a | 2013-01-30 11:26:36 -0300 | [diff] [blame] | 188 | strlcpy(v4l2_dev->name, "radio-miropcm20", sizeof(v4l2_dev->name)); |
Hans Verkuil | 32958fd | 2010-11-14 09:36:23 -0300 | [diff] [blame] | 189 | mutex_init(&dev->lock); |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 190 | |
| 191 | res = v4l2_device_register(NULL, v4l2_dev); |
| 192 | if (res < 0) { |
| 193 | v4l2_err(v4l2_dev, "could not register v4l2_device\n"); |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 197 | hdl = &dev->ctrl_handler; |
| 198 | v4l2_ctrl_handler_init(hdl, 1); |
| 199 | v4l2_ctrl_new_std(hdl, &pcm20_ctrl_ops, |
| 200 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); |
| 201 | v4l2_dev->ctrl_handler = hdl; |
| 202 | if (hdl->error) { |
| 203 | res = hdl->error; |
| 204 | v4l2_err(v4l2_dev, "Could not register control\n"); |
| 205 | goto err_hdl; |
| 206 | } |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 207 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); |
| 208 | dev->vdev.v4l2_dev = v4l2_dev; |
| 209 | dev->vdev.fops = &pcm20_fops; |
| 210 | dev->vdev.ioctl_ops = &pcm20_ioctl_ops; |
| 211 | dev->vdev.release = video_device_release_empty; |
Hans Verkuil | 32958fd | 2010-11-14 09:36:23 -0300 | [diff] [blame] | 212 | dev->vdev.lock = &dev->lock; |
Hans Verkuil | f7c096f | 2013-01-30 05:54:33 -0300 | [diff] [blame] | 213 | set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags); |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 214 | video_set_drvdata(&dev->vdev, dev); |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 215 | snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO, |
| 216 | dev->audmode == V4L2_TUNER_MODE_MONO, -1); |
| 217 | pcm20_setfreq(dev, dev->freq); |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 218 | |
| 219 | if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 220 | goto err_hdl; |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 221 | |
| 222 | v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n"); |
| 223 | return 0; |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 224 | err_hdl: |
| 225 | v4l2_ctrl_handler_free(hdl); |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 226 | v4l2_device_unregister(v4l2_dev); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt"); |
| 231 | MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card."); |
| 232 | MODULE_LICENSE("GPL"); |
| 233 | |
| 234 | static void __exit pcm20_cleanup(void) |
| 235 | { |
| 236 | struct pcm20 *dev = &pcm20_card; |
| 237 | |
| 238 | video_unregister_device(&dev->vdev); |
Hans Verkuil | f5e7cc4 | 2013-01-30 05:55:50 -0300 | [diff] [blame] | 239 | snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, 1, -1); |
Hans Verkuil | 7f51a61 | 2013-01-30 05:50:28 -0300 | [diff] [blame] | 240 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
Krzysztof Helt | 8366fc3 | 2009-11-27 11:24:13 +0100 | [diff] [blame] | 241 | v4l2_device_unregister(&dev->v4l2_dev); |
| 242 | } |
| 243 | |
| 244 | module_init(pcm20_init); |
| 245 | module_exit(pcm20_cleanup); |