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