Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 1 | /* radio-trust.c - Trust FM Radio card driver for Linux 2.2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * by Eric Lammerts <eric@scintilla.utwente.nl> |
| 3 | * |
| 4 | * Based on radio-aztech.c. Original notes: |
| 5 | * |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 6 | * Adapted to support the Video for Linux API by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Russell Kroll <rkroll@exploits.org>. Based on original tuner code by: |
| 8 | * |
| 9 | * Quay Ly |
| 10 | * Donald Song |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 11 | * Jason Lewis (jlewis@twilight.vtc.vsc.edu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Scott McGrath (smcgrath@twilight.vtc.vsc.edu) |
| 13 | * William McGrath (wmcgrath@twilight.vtc.vsc.edu) |
| 14 | * |
Mauro Carvalho Chehab | 982eddb | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 15 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <stdarg.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/ioport.h> |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 22 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
Mauro Carvalho Chehab | 982eddb | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 23 | #include <linux/videodev2.h> |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 24 | #include <linux/io.h> |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 25 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 26 | #include <media/v4l2-ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 28 | MODULE_AUTHOR("Eric Lammerts, Russell Kroll, Quay Lu, Donald Song, Jason Lewis, Scott McGrath, William McGrath"); |
| 29 | MODULE_DESCRIPTION("A driver for the Trust FM Radio card."); |
| 30 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 982eddb | 2006-08-08 09:10:05 -0300 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */ |
| 33 | |
| 34 | #ifndef CONFIG_RADIO_TRUST_PORT |
| 35 | #define CONFIG_RADIO_TRUST_PORT -1 |
| 36 | #endif |
| 37 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 38 | static int io = CONFIG_RADIO_TRUST_PORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static int radio_nr = -1; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 40 | |
| 41 | module_param(io, int, 0); |
| 42 | MODULE_PARM_DESC(io, "I/O address of the Trust FM Radio card (0x350 or 0x358)"); |
| 43 | module_param(radio_nr, int, 0); |
| 44 | |
| 45 | #define RADIO_VERSION KERNEL_VERSION(0, 0, 2) |
| 46 | |
| 47 | struct trust { |
| 48 | struct v4l2_device v4l2_dev; |
| 49 | struct video_device vdev; |
| 50 | int io; |
| 51 | int ioval; |
| 52 | __u16 curvol; |
| 53 | __u16 curbass; |
| 54 | __u16 curtreble; |
| 55 | int muted; |
| 56 | unsigned long curfreq; |
| 57 | int curstereo; |
| 58 | int curmute; |
| 59 | struct mutex lock; |
| 60 | }; |
| 61 | |
| 62 | static struct trust trust_card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* i2c addresses */ |
| 65 | #define TDA7318_ADDR 0x88 |
| 66 | #define TSA6060T_ADDR 0xc4 |
| 67 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 68 | #define TR_DELAY do { inb(tr->io); inb(tr->io); inb(tr->io); } while (0) |
| 69 | #define TR_SET_SCL outb(tr->ioval |= 2, tr->io) |
| 70 | #define TR_CLR_SCL outb(tr->ioval &= 0xfd, tr->io) |
| 71 | #define TR_SET_SDA outb(tr->ioval |= 1, tr->io) |
| 72 | #define TR_CLR_SDA outb(tr->ioval &= 0xfe, tr->io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 74 | static void write_i2c(struct trust *tr, int n, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | unsigned char val, mask; |
| 77 | va_list args; |
| 78 | |
| 79 | va_start(args, n); |
| 80 | |
| 81 | /* start condition */ |
| 82 | TR_SET_SDA; |
| 83 | TR_SET_SCL; |
| 84 | TR_DELAY; |
| 85 | TR_CLR_SDA; |
| 86 | TR_CLR_SCL; |
| 87 | TR_DELAY; |
| 88 | |
| 89 | for(; n; n--) { |
| 90 | val = va_arg(args, unsigned); |
| 91 | for(mask = 0x80; mask; mask >>= 1) { |
| 92 | if(val & mask) |
| 93 | TR_SET_SDA; |
| 94 | else |
| 95 | TR_CLR_SDA; |
| 96 | TR_SET_SCL; |
| 97 | TR_DELAY; |
| 98 | TR_CLR_SCL; |
| 99 | TR_DELAY; |
| 100 | } |
| 101 | /* acknowledge bit */ |
| 102 | TR_SET_SDA; |
| 103 | TR_SET_SCL; |
| 104 | TR_DELAY; |
| 105 | TR_CLR_SCL; |
| 106 | TR_DELAY; |
| 107 | } |
| 108 | |
| 109 | /* stop condition */ |
| 110 | TR_CLR_SDA; |
| 111 | TR_DELAY; |
| 112 | TR_SET_SCL; |
| 113 | TR_DELAY; |
| 114 | TR_SET_SDA; |
| 115 | TR_DELAY; |
| 116 | |
| 117 | va_end(args); |
| 118 | } |
| 119 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 120 | static void tr_setvol(struct trust *tr, __u16 vol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 122 | mutex_lock(&tr->lock); |
| 123 | tr->curvol = vol / 2048; |
| 124 | write_i2c(tr, 2, TDA7318_ADDR, tr->curvol ^ 0x1f); |
| 125 | mutex_unlock(&tr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static int basstreble2chip[15] = { |
| 129 | 0, 1, 2, 3, 4, 5, 6, 7, 14, 13, 12, 11, 10, 9, 8 |
| 130 | }; |
| 131 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 132 | static void tr_setbass(struct trust *tr, __u16 bass) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 134 | mutex_lock(&tr->lock); |
| 135 | tr->curbass = bass / 4370; |
| 136 | write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[tr->curbass]); |
| 137 | mutex_unlock(&tr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 140 | static void tr_settreble(struct trust *tr, __u16 treble) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 142 | mutex_lock(&tr->lock); |
| 143 | tr->curtreble = treble / 4370; |
| 144 | write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[tr->curtreble]); |
| 145 | mutex_unlock(&tr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 148 | static void tr_setstereo(struct trust *tr, int stereo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 150 | mutex_lock(&tr->lock); |
| 151 | tr->curstereo = !!stereo; |
| 152 | tr->ioval = (tr->ioval & 0xfb) | (!tr->curstereo << 2); |
| 153 | outb(tr->ioval, tr->io); |
| 154 | mutex_unlock(&tr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 157 | static void tr_setmute(struct trust *tr, int mute) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 159 | mutex_lock(&tr->lock); |
| 160 | tr->curmute = !!mute; |
| 161 | tr->ioval = (tr->ioval & 0xf7) | (tr->curmute << 3); |
| 162 | outb(tr->ioval, tr->io); |
| 163 | mutex_unlock(&tr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 166 | static int tr_getsigstr(struct trust *tr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | int i, v; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 169 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 170 | mutex_lock(&tr->lock); |
| 171 | for (i = 0, v = 0; i < 100; i++) |
| 172 | v |= inb(tr->io); |
| 173 | mutex_unlock(&tr->lock); |
| 174 | return (v & 1) ? 0 : 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 177 | static int tr_getstereo(struct trust *tr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
| 179 | /* don't know how to determine it, just return the setting */ |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 180 | return tr->curstereo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 183 | static void tr_setfreq(struct trust *tr, unsigned long f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 185 | mutex_lock(&tr->lock); |
| 186 | tr->curfreq = f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | f /= 160; /* Convert to 10 kHz units */ |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 188 | f += 1070; /* Add 10.7 MHz IF */ |
| 189 | write_i2c(tr, 5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0); |
| 190 | mutex_unlock(&tr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 193 | static int vidioc_querycap(struct file *file, void *priv, |
| 194 | struct v4l2_capability *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 196 | strlcpy(v->driver, "radio-trust", sizeof(v->driver)); |
| 197 | strlcpy(v->card, "Trust FM Radio", sizeof(v->card)); |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 198 | strlcpy(v->bus_info, "ISA", sizeof(v->bus_info)); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 199 | v->version = RADIO_VERSION; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 200 | v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 201 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 204 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 205 | struct v4l2_tuner *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 207 | struct trust *tr = video_drvdata(file); |
| 208 | |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 209 | if (v->index > 0) |
| 210 | return -EINVAL; |
| 211 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 212 | strlcpy(v->name, "FM", sizeof(v->name)); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 213 | v->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 214 | v->rangelow = 87.5 * 16000; |
| 215 | v->rangehigh = 108 * 16000; |
| 216 | v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 217 | v->capability = V4L2_TUNER_CAP_LOW; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 218 | if (tr_getstereo(tr)) |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 219 | v->audmode = V4L2_TUNER_MODE_STEREO; |
| 220 | else |
| 221 | v->audmode = V4L2_TUNER_MODE_MONO; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 222 | v->signal = tr_getsigstr(tr); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 227 | struct v4l2_tuner *v) |
| 228 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 229 | struct trust *tr = video_drvdata(file); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 230 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 231 | if (v->index) |
| 232 | return -EINVAL; |
| 233 | tr_setstereo(tr, v->audmode == V4L2_TUNER_MODE_STEREO); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 238 | struct v4l2_frequency *f) |
| 239 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 240 | struct trust *tr = video_drvdata(file); |
| 241 | |
Hans Verkuil | a3a9e28 | 2009-11-27 04:33:25 -0300 | [diff] [blame] | 242 | if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) |
| 243 | return -EINVAL; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 244 | tr_setfreq(tr, f->frequency); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 249 | struct v4l2_frequency *f) |
| 250 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 251 | struct trust *tr = video_drvdata(file); |
| 252 | |
Hans Verkuil | a3a9e28 | 2009-11-27 04:33:25 -0300 | [diff] [blame] | 253 | if (f->tuner != 0) |
| 254 | return -EINVAL; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 255 | f->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 256 | f->frequency = tr->curfreq; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 261 | struct v4l2_queryctrl *qc) |
| 262 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 263 | switch (qc->id) { |
| 264 | case V4L2_CID_AUDIO_MUTE: |
| 265 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1); |
| 266 | case V4L2_CID_AUDIO_VOLUME: |
| 267 | return v4l2_ctrl_query_fill(qc, 0, 65535, 2048, 65535); |
| 268 | case V4L2_CID_AUDIO_BASS: |
| 269 | case V4L2_CID_AUDIO_TREBLE: |
| 270 | return v4l2_ctrl_query_fill(qc, 0, 65535, 4370, 32768); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 271 | } |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 276 | struct v4l2_control *ctrl) |
| 277 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 278 | struct trust *tr = video_drvdata(file); |
| 279 | |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 280 | switch (ctrl->id) { |
| 281 | case V4L2_CID_AUDIO_MUTE: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 282 | ctrl->value = tr->curmute; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 283 | return 0; |
| 284 | case V4L2_CID_AUDIO_VOLUME: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 285 | ctrl->value = tr->curvol * 2048; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 286 | return 0; |
| 287 | case V4L2_CID_AUDIO_BASS: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 288 | ctrl->value = tr->curbass * 4370; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 289 | return 0; |
| 290 | case V4L2_CID_AUDIO_TREBLE: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 291 | ctrl->value = tr->curtreble * 4370; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 298 | struct v4l2_control *ctrl) |
| 299 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 300 | struct trust *tr = video_drvdata(file); |
| 301 | |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 302 | switch (ctrl->id) { |
| 303 | case V4L2_CID_AUDIO_MUTE: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 304 | tr_setmute(tr, ctrl->value); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 305 | return 0; |
| 306 | case V4L2_CID_AUDIO_VOLUME: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 307 | tr_setvol(tr, ctrl->value); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 308 | return 0; |
| 309 | case V4L2_CID_AUDIO_BASS: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 310 | tr_setbass(tr, ctrl->value); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 311 | return 0; |
| 312 | case V4L2_CID_AUDIO_TREBLE: |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 313 | tr_settreble(tr, ctrl->value); |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 319 | static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i) |
| 320 | { |
| 321 | *i = 0; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int vidioc_s_input(struct file *filp, void *priv, unsigned int i) |
| 326 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 327 | return i ? -EINVAL : 0; |
| 328 | } |
| 329 | |
| 330 | static int vidioc_g_audio(struct file *file, void *priv, |
| 331 | struct v4l2_audio *a) |
| 332 | { |
| 333 | a->index = 0; |
| 334 | strlcpy(a->name, "Radio", sizeof(a->name)); |
| 335 | a->capability = V4L2_AUDCAP_STEREO; |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int vidioc_s_audio(struct file *file, void *priv, |
| 340 | struct v4l2_audio *a) |
| 341 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 342 | return a->index ? -EINVAL : 0; |
| 343 | } |
| 344 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 345 | static const struct v4l2_file_operations trust_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | .owner = THIS_MODULE, |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 347 | .ioctl = video_ioctl2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 350 | static const struct v4l2_ioctl_ops trust_ioctl_ops = { |
Douglas Landgraf | c5f822b | 2007-04-20 18:22:19 -0300 | [diff] [blame] | 351 | .vidioc_querycap = vidioc_querycap, |
| 352 | .vidioc_g_tuner = vidioc_g_tuner, |
| 353 | .vidioc_s_tuner = vidioc_s_tuner, |
| 354 | .vidioc_g_frequency = vidioc_g_frequency, |
| 355 | .vidioc_s_frequency = vidioc_s_frequency, |
| 356 | .vidioc_queryctrl = vidioc_queryctrl, |
| 357 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 358 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 359 | .vidioc_g_audio = vidioc_g_audio, |
| 360 | .vidioc_s_audio = vidioc_s_audio, |
| 361 | .vidioc_g_input = vidioc_g_input, |
| 362 | .vidioc_s_input = vidioc_s_input, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | static int __init trust_init(void) |
| 366 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 367 | struct trust *tr = &trust_card; |
| 368 | struct v4l2_device *v4l2_dev = &tr->v4l2_dev; |
| 369 | int res; |
| 370 | |
| 371 | strlcpy(v4l2_dev->name, "trust", sizeof(v4l2_dev->name)); |
| 372 | tr->io = io; |
| 373 | tr->ioval = 0xf; |
| 374 | mutex_init(&tr->lock); |
| 375 | |
| 376 | if (tr->io == -1) { |
Hans Verkuil | b24c20cc | 2009-03-09 08:11:21 -0300 | [diff] [blame] | 377 | v4l2_err(v4l2_dev, "You must set an I/O address with io=0x0x350 or 0x358\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return -EINVAL; |
| 379 | } |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 380 | if (!request_region(tr->io, 2, "Trust FM Radio")) { |
| 381 | v4l2_err(v4l2_dev, "port 0x%x already in use\n", tr->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return -EBUSY; |
| 383 | } |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 384 | |
| 385 | res = v4l2_device_register(NULL, v4l2_dev); |
| 386 | if (res < 0) { |
| 387 | release_region(tr->io, 2); |
| 388 | v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); |
| 389 | return res; |
| 390 | } |
| 391 | |
| 392 | strlcpy(tr->vdev.name, v4l2_dev->name, sizeof(tr->vdev.name)); |
| 393 | tr->vdev.v4l2_dev = v4l2_dev; |
| 394 | tr->vdev.fops = &trust_fops; |
| 395 | tr->vdev.ioctl_ops = &trust_ioctl_ops; |
| 396 | tr->vdev.release = video_device_release_empty; |
| 397 | video_set_drvdata(&tr->vdev, tr); |
| 398 | |
| 399 | if (video_register_device(&tr->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { |
| 400 | v4l2_device_unregister(v4l2_dev); |
| 401 | release_region(tr->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return -EINVAL; |
| 403 | } |
| 404 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 405 | v4l2_info(v4l2_dev, "Trust FM Radio card driver v1.0.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 407 | write_i2c(tr, 2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */ |
| 408 | write_i2c(tr, 2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */ |
| 409 | write_i2c(tr, 2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */ |
| 410 | write_i2c(tr, 2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */ |
| 411 | write_i2c(tr, 2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 413 | tr_setvol(tr, 0xffff); |
| 414 | tr_setbass(tr, 0x8000); |
| 415 | tr_settreble(tr, 0x8000); |
| 416 | tr_setstereo(tr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | /* mute card - prevents noisy bootups */ |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 419 | tr_setmute(tr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | static void __exit cleanup_trust_module(void) |
| 425 | { |
Hans Verkuil | 1dc8aaf | 2009-03-06 13:54:23 -0300 | [diff] [blame] | 426 | struct trust *tr = &trust_card; |
| 427 | |
| 428 | video_unregister_device(&tr->vdev); |
| 429 | v4l2_device_unregister(&tr->v4l2_dev); |
| 430 | release_region(tr->io, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | module_init(trust_init); |
| 434 | module_exit(cleanup_trust_module); |