Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Maestro PCI sound card radio driver for Linux support |
| 2 | * (c) 2000 A. Tlalka, atlka@pg.gda.pl |
| 3 | * Notes on the hardware |
| 4 | * |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 5 | * + Frequency control is done digitally |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * + No volume control - only mute/unmute - you have to use Aux line volume |
| 7 | * control on Maestro card to set the volume |
| 8 | * + Radio status (tuned/not_tuned and stereo/mono) is valid some time after |
| 9 | * frequency setting (>100ms) and only when the radio is unmuted. |
| 10 | * version 0.02 |
| 11 | * + io port is automatically detected - only the first radio is used |
| 12 | * version 0.03 |
| 13 | * + thread access locking additions |
| 14 | * version 0.04 |
| 15 | * + code improvements |
| 16 | * + VIDEO_TUNER_LOW is permanent |
Mauro Carvalho Chehab | b6055d7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 17 | * |
| 18 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/delay.h> |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 25 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/pci.h> |
Mauro Carvalho Chehab | b6055d7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 27 | #include <linux/videodev2.h> |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 28 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 30 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 31 | #include <media/v4l2-ioctl.h> |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 32 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 33 | MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl"); |
| 34 | MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio."); |
| 35 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | b6055d7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 36 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 37 | static int radio_nr = -1; |
| 38 | module_param(radio_nr, int, 0); |
| 39 | |
| 40 | #define RADIO_VERSION KERNEL_VERSION(0, 0, 6) |
| 41 | #define DRIVER_VERSION "0.06" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 43 | #define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #define IO_MASK 4 /* mask register offset from GPIO_DATA |
| 46 | bits 1=unmask write to given bit */ |
| 47 | #define IO_DIR 8 /* direction register offset from GPIO_DATA |
| 48 | bits 0/1=read/write direction */ |
| 49 | |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 50 | #define GPIO6 0x0040 /* mask bits for GPIO lines */ |
| 51 | #define GPIO7 0x0080 |
| 52 | #define GPIO8 0x0100 |
| 53 | #define GPIO9 0x0200 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 55 | #define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */ |
| 56 | #define STR_CLK GPIO7 |
| 57 | #define STR_WREN GPIO8 |
| 58 | #define STR_MOST GPIO9 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | #define FREQ_LO 50*16000 |
| 61 | #define FREQ_HI 150*16000 |
| 62 | |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 63 | #define FREQ_IF 171200 /* 10.7*16000 */ |
| 64 | #define FREQ_STEP 200 /* 12.5*16 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | #define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\ |
| 67 | /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */ |
| 68 | |
| 69 | #define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF) |
| 70 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 71 | struct maestro { |
| 72 | struct v4l2_device v4l2_dev; |
| 73 | struct video_device vdev; |
| 74 | struct pci_dev *pdev; |
| 75 | struct mutex lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 77 | u16 io; /* base of Maestro card radio io (GPIO_DATA)*/ |
| 78 | u16 muted; /* VIDEO_AUDIO_MUTE */ |
| 79 | u16 stereo; /* VIDEO_TUNER_STEREO_ON */ |
| 80 | u16 tuned; /* signal strength (0 or 0xffff) */ |
| 81 | }; |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 82 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 83 | static inline struct maestro *to_maestro(struct v4l2_device *v4l2_dev) |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 84 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 85 | return container_of(v4l2_dev, struct maestro, v4l2_dev); |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 86 | } |
| 87 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 88 | static u32 radio_bits_get(struct maestro *dev) |
Hans Verkuil | 3ca685a | 2008-08-23 04:49:13 -0300 | [diff] [blame] | 89 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 90 | u16 io = dev->io, l, rdata; |
| 91 | u32 data = 0; |
Jiri Slaby | 0eaa21f | 2006-01-09 20:52:49 -0800 | [diff] [blame] | 92 | u16 omask; |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | omask = inw(io + IO_MASK); |
| 95 | outw(~(STR_CLK | STR_WREN), io + IO_MASK); |
| 96 | outw(0, io); |
| 97 | udelay(16); |
| 98 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 99 | for (l = 24; l--;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | outw(STR_CLK, io); /* HI state */ |
| 101 | udelay(2); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 102 | if (!l) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff; |
| 104 | outw(0, io); /* LO state */ |
| 105 | udelay(2); |
| 106 | data <<= 1; /* shift data */ |
| 107 | rdata = inw(io); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 108 | if (!l) |
| 109 | dev->stereo = (rdata & STR_MOST) ? 0 : 1; |
| 110 | else if (rdata & STR_DATA) |
| 111 | data++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | udelay(2); |
| 113 | } |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 114 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 115 | if (dev->muted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | outw(STR_WREN, io); |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | udelay(4); |
| 119 | outw(omask, io + IO_MASK); |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return data & 0x3ffe; |
| 122 | } |
| 123 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 124 | static void radio_bits_set(struct maestro *dev, u32 data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 126 | u16 io = dev->io, l, bits; |
Jiri Slaby | 0eaa21f | 2006-01-09 20:52:49 -0800 | [diff] [blame] | 127 | u16 omask, odir; |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | omask = inw(io + IO_MASK); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 130 | odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | outw(odir | STR_DATA, io + IO_DIR); |
| 132 | outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK); |
| 133 | udelay(16); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 134 | for (l = 25; l; l--) { |
| 135 | bits = ((data >> 18) & STR_DATA) | STR_WREN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | data <<= 1; /* shift data */ |
| 137 | outw(bits, io); /* start strobe */ |
| 138 | udelay(2); |
| 139 | outw(bits | STR_CLK, io); /* HI level */ |
| 140 | udelay(2); |
| 141 | outw(bits, io); /* LO level */ |
| 142 | udelay(4); |
| 143 | } |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 144 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 145 | if (!dev->muted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | outw(0, io); |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | udelay(4); |
| 149 | outw(omask, io + IO_MASK); |
| 150 | outw(odir, io + IO_DIR); |
| 151 | msleep(125); |
| 152 | } |
| 153 | |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 154 | static int vidioc_querycap(struct file *file, void *priv, |
| 155 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 157 | struct maestro *dev = video_drvdata(file); |
| 158 | |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 159 | strlcpy(v->driver, "radio-maestro", sizeof(v->driver)); |
| 160 | strlcpy(v->card, "Maestro Radio", sizeof(v->card)); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 161 | snprintf(v->bus_info, sizeof(v->bus_info), "PCI:%s", pci_name(dev->pdev)); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 162 | v->version = RADIO_VERSION; |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 163 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 164 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 167 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 168 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 170 | struct maestro *dev = video_drvdata(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 172 | if (v->index > 0) |
| 173 | return -EINVAL; |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 174 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 175 | mutex_lock(&dev->lock); |
| 176 | radio_bits_get(dev); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 177 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 178 | strlcpy(v->name, "FM", sizeof(v->name)); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 179 | v->type = V4L2_TUNER_RADIO; |
| 180 | v->rangelow = FREQ_LO; |
| 181 | v->rangehigh = FREQ_HI; |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 182 | v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 183 | v->capability = V4L2_TUNER_CAP_LOW; |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 184 | if (dev->stereo) |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 185 | v->audmode = V4L2_TUNER_MODE_STEREO; |
| 186 | else |
| 187 | v->audmode = V4L2_TUNER_MODE_MONO; |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 188 | v->signal = dev->tuned; |
| 189 | mutex_unlock(&dev->lock); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 194 | struct v4l2_tuner *v) |
| 195 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 196 | return v->index ? -EINVAL : 0; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 200 | struct v4l2_frequency *f) |
| 201 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 202 | struct maestro *dev = video_drvdata(file); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 203 | |
Hans Verkuil | a3a9e28 | 2009-11-27 04:33:25 -0300 | [diff] [blame] | 204 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
| 205 | return -EINVAL; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 206 | if (f->frequency < FREQ_LO || f->frequency > FREQ_HI) |
| 207 | return -EINVAL; |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 208 | mutex_lock(&dev->lock); |
| 209 | radio_bits_set(dev, FREQ2BITS(f->frequency)); |
| 210 | mutex_unlock(&dev->lock); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 215 | struct v4l2_frequency *f) |
| 216 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 217 | struct maestro *dev = video_drvdata(file); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 218 | |
Hans Verkuil | a3a9e28 | 2009-11-27 04:33:25 -0300 | [diff] [blame] | 219 | if (f->tuner != 0) |
| 220 | return -EINVAL; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 221 | f->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 222 | mutex_lock(&dev->lock); |
| 223 | f->frequency = BITS2FREQ(radio_bits_get(dev)); |
| 224 | mutex_unlock(&dev->lock); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 229 | struct v4l2_queryctrl *qc) |
| 230 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 231 | switch (qc->id) { |
| 232 | case V4L2_CID_AUDIO_MUTE: |
| 233 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 234 | } |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 239 | struct v4l2_control *ctrl) |
| 240 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 241 | struct maestro *dev = video_drvdata(file); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 242 | |
| 243 | switch (ctrl->id) { |
| 244 | case V4L2_CID_AUDIO_MUTE: |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 245 | ctrl->value = dev->muted; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 252 | struct v4l2_control *ctrl) |
| 253 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 254 | struct maestro *dev = video_drvdata(file); |
| 255 | u16 io = dev->io; |
| 256 | u16 omask; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 257 | |
| 258 | switch (ctrl->id) { |
| 259 | case V4L2_CID_AUDIO_MUTE: |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 260 | mutex_lock(&dev->lock); |
| 261 | omask = inw(io + IO_MASK); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 262 | outw(~STR_WREN, io + IO_MASK); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 263 | dev->muted = ctrl->value; |
| 264 | outw(dev->muted ? STR_WREN : 0, io); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 265 | udelay(4); |
| 266 | outw(omask, io + IO_MASK); |
| 267 | msleep(125); |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 268 | mutex_unlock(&dev->lock); |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 274 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 275 | { |
| 276 | *i = 0; |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 281 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 282 | return i ? -EINVAL : 0; |
| 283 | } |
| 284 | |
| 285 | static int vidioc_g_audio(struct file *file, void *priv, |
| 286 | struct v4l2_audio *a) |
| 287 | { |
| 288 | a->index = 0; |
| 289 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 290 | a->capability = V4L2_AUDCAP_STEREO; |
Douglas Landgraf | d455cf5 | 2007-04-26 16:44:55 -0300 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int vidioc_s_audio(struct file *file, void *priv, |
| 295 | struct v4l2_audio *a) |
| 296 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 297 | return a->index ? -EINVAL : 0; |
| 298 | } |
| 299 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 300 | static const struct v4l2_file_operations maestro_fops = { |
| 301 | .owner = THIS_MODULE, |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 302 | .ioctl = video_ioctl2, |
| 303 | }; |
| 304 | |
| 305 | static const struct v4l2_ioctl_ops maestro_ioctl_ops = { |
| 306 | .vidioc_querycap = vidioc_querycap, |
| 307 | .vidioc_g_tuner = vidioc_g_tuner, |
| 308 | .vidioc_s_tuner = vidioc_s_tuner, |
| 309 | .vidioc_g_audio = vidioc_g_audio, |
| 310 | .vidioc_s_audio = vidioc_s_audio, |
| 311 | .vidioc_g_input = vidioc_g_input, |
| 312 | .vidioc_s_input = vidioc_s_input, |
| 313 | .vidioc_g_frequency = vidioc_g_frequency, |
| 314 | .vidioc_s_frequency = vidioc_s_frequency, |
| 315 | .vidioc_queryctrl = vidioc_queryctrl, |
| 316 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 317 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 318 | }; |
| 319 | |
| 320 | static u16 __devinit radio_power_on(struct maestro *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Jiri Slaby | 0eaa21f | 2006-01-09 20:52:49 -0800 | [diff] [blame] | 322 | register u16 io = dev->io; |
| 323 | register u32 ofreq; |
| 324 | u16 omask, odir; |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | omask = inw(io + IO_MASK); |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 327 | odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | outw(odir & ~STR_WREN, io + IO_DIR); |
Mauro Carvalho Chehab | b6055d7 | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 329 | dev->muted = inw(io) & STR_WREN ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | outw(odir, io + IO_DIR); |
| 331 | outw(~(STR_WREN | STR_CLK), io + IO_MASK); |
| 332 | outw(dev->muted ? 0 : STR_WREN, io); |
| 333 | udelay(16); |
| 334 | outw(omask, io + IO_MASK); |
| 335 | ofreq = radio_bits_get(dev); |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 336 | |
| 337 | if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | ofreq = FREQ2BITS(FREQ_LO); |
| 339 | radio_bits_set(dev, ofreq); |
Jiri Slaby | 6a2cf8e | 2006-01-09 20:52:48 -0800 | [diff] [blame] | 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return (ofreq == radio_bits_get(dev)); |
| 342 | } |
| 343 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 344 | static int __devinit maestro_probe(struct pci_dev *pdev, |
| 345 | const struct pci_device_id *ent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 347 | struct maestro *dev; |
| 348 | struct v4l2_device *v4l2_dev; |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 349 | int retval; |
| 350 | |
| 351 | retval = pci_enable_device(pdev); |
| 352 | if (retval) { |
| 353 | dev_err(&pdev->dev, "enabling pci device failed!\n"); |
| 354 | goto err; |
| 355 | } |
| 356 | |
| 357 | retval = -ENOMEM; |
| 358 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 359 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 360 | if (dev == NULL) { |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 361 | dev_err(&pdev->dev, "not enough memory\n"); |
| 362 | goto err; |
| 363 | } |
| 364 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 365 | v4l2_dev = &dev->v4l2_dev; |
| 366 | mutex_init(&dev->lock); |
| 367 | dev->pdev = pdev; |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 368 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 369 | strlcpy(v4l2_dev->name, "maestro", sizeof(v4l2_dev->name)); |
| 370 | |
| 371 | retval = v4l2_device_register(&pdev->dev, v4l2_dev); |
| 372 | if (retval < 0) { |
| 373 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 374 | goto errfr; |
| 375 | } |
| 376 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 377 | dev->io = pci_resource_start(pdev, 0) + GPIO_DATA; |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 378 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 379 | strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); |
| 380 | dev->vdev.v4l2_dev = v4l2_dev; |
| 381 | dev->vdev.fops = &maestro_fops; |
| 382 | dev->vdev.ioctl_ops = &maestro_ioctl_ops; |
| 383 | dev->vdev.release = video_device_release_empty; |
| 384 | video_set_drvdata(&dev->vdev, dev); |
| 385 | |
| 386 | retval = video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 387 | if (retval) { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 388 | v4l2_err(v4l2_dev, "can't register video device!\n"); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 389 | goto errfr1; |
| 390 | } |
| 391 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 392 | if (!radio_power_on(dev)) { |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 393 | retval = -EIO; |
| 394 | goto errunr; |
| 395 | } |
| 396 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 397 | v4l2_info(v4l2_dev, "version " DRIVER_VERSION "\n"); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | errunr: |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 401 | video_unregister_device(&dev->vdev); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 402 | errfr1: |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 403 | v4l2_device_unregister(v4l2_dev); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 404 | errfr: |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 405 | kfree(dev); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 406 | err: |
| 407 | return retval; |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 411 | static void __devexit maestro_remove(struct pci_dev *pdev) |
| 412 | { |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 413 | struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev); |
| 414 | struct maestro *dev = to_maestro(v4l2_dev); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 415 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 416 | video_unregister_device(&dev->vdev); |
| 417 | v4l2_device_unregister(&dev->v4l2_dev); |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Hans Verkuil | 087c618 | 2009-03-06 13:51:08 -0300 | [diff] [blame] | 420 | static struct pci_device_id maestro_r_pci_tbl[] = { |
| 421 | { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968), |
| 422 | .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8, |
| 423 | .class_mask = 0xffff00 }, |
| 424 | { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978), |
| 425 | .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8, |
| 426 | .class_mask = 0xffff00 }, |
| 427 | { 0 } |
| 428 | }; |
| 429 | MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl); |
| 430 | |
| 431 | static struct pci_driver maestro_r_driver = { |
| 432 | .name = "maestro_radio", |
| 433 | .id_table = maestro_r_pci_tbl, |
| 434 | .probe = maestro_probe, |
| 435 | .remove = __devexit_p(maestro_remove), |
| 436 | }; |
| 437 | |
Jiri Slaby | 89dad8f | 2006-01-09 20:52:47 -0800 | [diff] [blame] | 438 | static int __init maestro_radio_init(void) |
| 439 | { |
| 440 | int retval = pci_register_driver(&maestro_r_driver); |
| 441 | |
| 442 | if (retval) |
| 443 | printk(KERN_ERR "error during registration pci driver\n"); |
| 444 | |
| 445 | return retval; |
| 446 | } |
| 447 | |
| 448 | static void __exit maestro_radio_exit(void) |
| 449 | { |
| 450 | pci_unregister_driver(&maestro_r_driver); |
| 451 | } |
| 452 | |
| 453 | module_init(maestro_radio_init); |
| 454 | module_exit(maestro_radio_exit); |