[PATCH] remove many unneeded #includes of sched.h
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / radio / radio-maestro.c
CommitLineData
1da177e4
LT
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 *
4286c6f6 5 * + Frequency control is done digitally
1da177e4
LT
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
b6055d7b
MCC
17 *
18 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
1da177e4
LT
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
1da177e4
LT
25#include <asm/io.h>
26#include <asm/uaccess.h>
3593cab5 27#include <linux/mutex.h>
1da177e4 28#include <linux/pci.h>
b6055d7b 29#include <linux/videodev2.h>
5e87efa3 30#include <media/v4l2-common.h>
3593cab5 31
b6055d7b
MCC
32#include <linux/version.h> /* for KERNEL_VERSION MACRO */
33#define RADIO_VERSION KERNEL_VERSION(0,0,6)
34#define DRIVER_VERSION "0.06"
35
36static struct v4l2_queryctrl radio_qctrl[] = {
37 {
38 .id = V4L2_CID_AUDIO_MUTE,
39 .name = "Mute",
40 .minimum = 0,
41 .maximum = 1,
42 .default_value = 1,
43 .type = V4L2_CTRL_TYPE_BOOLEAN,
44 }
45};
1da177e4 46
6a2cf8ee 47#define GPIO_DATA 0x60 /* port offset from ESS_IO_BASE */
1da177e4
LT
48
49#define IO_MASK 4 /* mask register offset from GPIO_DATA
50 bits 1=unmask write to given bit */
51#define IO_DIR 8 /* direction register offset from GPIO_DATA
52 bits 0/1=read/write direction */
53
6a2cf8ee
JS
54#define GPIO6 0x0040 /* mask bits for GPIO lines */
55#define GPIO7 0x0080
56#define GPIO8 0x0100
57#define GPIO9 0x0200
1da177e4 58
6a2cf8ee
JS
59#define STR_DATA GPIO6 /* radio TEA5757 pins and GPIO bits */
60#define STR_CLK GPIO7
61#define STR_WREN GPIO8
62#define STR_MOST GPIO9
1da177e4
LT
63
64#define FREQ_LO 50*16000
65#define FREQ_HI 150*16000
66
6a2cf8ee
JS
67#define FREQ_IF 171200 /* 10.7*16000 */
68#define FREQ_STEP 200 /* 12.5*16 */
1da177e4
LT
69
70#define FREQ2BITS(x) ((((unsigned int)(x)+FREQ_IF+(FREQ_STEP<<1))\
71 /(FREQ_STEP<<2))<<2) /* (x==fmhz*16*1000) -> bits */
72
73#define BITS2FREQ(x) ((x) * FREQ_STEP - FREQ_IF)
74
75static int radio_nr = -1;
76module_param(radio_nr, int, 0);
77
78static int radio_ioctl(struct inode *inode, struct file *file,
6a2cf8ee 79 unsigned int cmd, unsigned long arg);
89dad8f0
JS
80static int maestro_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
81static void maestro_remove(struct pci_dev *pdev);
82
83static struct pci_device_id maestro_r_pci_tbl[] = {
84 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1968),
85 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
86 .class_mask = 0xffff00 },
87 { PCI_DEVICE(PCI_VENDOR_ID_ESS, PCI_DEVICE_ID_ESS_ESS1978),
88 .class = PCI_CLASS_MULTIMEDIA_AUDIO << 8,
89 .class_mask = 0xffff00 },
90 { 0 }
91};
92MODULE_DEVICE_TABLE(pci, maestro_r_pci_tbl);
93
94static struct pci_driver maestro_r_driver = {
95 .name = "maestro_radio",
96 .id_table = maestro_r_pci_tbl,
97 .probe = maestro_probe,
98 .remove = __devexit_p(maestro_remove),
99};
1da177e4 100
fa027c2a 101static const struct file_operations maestro_fops = {
1da177e4
LT
102 .owner = THIS_MODULE,
103 .open = video_exclusive_open,
104 .release = video_exclusive_release,
105 .ioctl = radio_ioctl,
0d0fbf81 106 .compat_ioctl = v4l_compat_ioctl32,
1da177e4
LT
107 .llseek = no_llseek,
108};
109
6a2cf8ee 110static struct video_device maestro_radio = {
1da177e4
LT
111 .name = "Maestro radio",
112 .type = VID_TYPE_TUNER,
b6055d7b 113 .hardware = 0,
6a2cf8ee 114 .fops = &maestro_fops,
1da177e4
LT
115};
116
6a2cf8ee 117struct radio_device {
0eaa21fd 118 u16 io, /* base of Maestro card radio io (GPIO_DATA)*/
1da177e4 119 muted, /* VIDEO_AUDIO_MUTE */
4286c6f6 120 stereo, /* VIDEO_TUNER_STEREO_ON */
1da177e4 121 tuned; /* signal strength (0 or 0xffff) */
3593cab5 122 struct mutex lock;
89dad8f0 123};
1da177e4 124
0eaa21fd 125static u32 radio_bits_get(struct radio_device *dev)
1da177e4 126{
0eaa21fd
JS
127 register u16 io=dev->io, l, rdata;
128 register u32 data=0;
129 u16 omask;
6a2cf8ee 130
1da177e4
LT
131 omask = inw(io + IO_MASK);
132 outw(~(STR_CLK | STR_WREN), io + IO_MASK);
133 outw(0, io);
134 udelay(16);
135
136 for (l=24;l--;) {
137 outw(STR_CLK, io); /* HI state */
138 udelay(2);
4286c6f6 139 if(!l)
1da177e4
LT
140 dev->tuned = inw(io) & STR_MOST ? 0 : 0xffff;
141 outw(0, io); /* LO state */
142 udelay(2);
143 data <<= 1; /* shift data */
144 rdata = inw(io);
145 if(!l)
4286c6f6 146 dev->stereo = rdata & STR_MOST ?
b6055d7b 147 0 : 1;
1da177e4
LT
148 else
149 if(rdata & STR_DATA)
150 data++;
151 udelay(2);
152 }
6a2cf8ee 153
1da177e4
LT
154 if(dev->muted)
155 outw(STR_WREN, io);
6a2cf8ee 156
1da177e4
LT
157 udelay(4);
158 outw(omask, io + IO_MASK);
6a2cf8ee 159
1da177e4
LT
160 return data & 0x3ffe;
161}
162
0eaa21fd 163static void radio_bits_set(struct radio_device *dev, u32 data)
1da177e4 164{
0eaa21fd
JS
165 register u16 io=dev->io, l, bits;
166 u16 omask, odir;
6a2cf8ee 167
1da177e4
LT
168 omask = inw(io + IO_MASK);
169 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
170 outw(odir | STR_DATA, io + IO_DIR);
171 outw(~(STR_DATA | STR_CLK | STR_WREN), io + IO_MASK);
172 udelay(16);
173 for (l=25;l;l--) {
174 bits = ((data >> 18) & STR_DATA) | STR_WREN ;
175 data <<= 1; /* shift data */
176 outw(bits, io); /* start strobe */
177 udelay(2);
178 outw(bits | STR_CLK, io); /* HI level */
179 udelay(2);
180 outw(bits, io); /* LO level */
181 udelay(4);
182 }
6a2cf8ee 183
1da177e4
LT
184 if(!dev->muted)
185 outw(0, io);
6a2cf8ee 186
1da177e4
LT
187 udelay(4);
188 outw(omask, io + IO_MASK);
189 outw(odir, io + IO_DIR);
190 msleep(125);
191}
192
77933d72 193static inline int radio_function(struct inode *inode, struct file *file,
6a2cf8ee 194 unsigned int cmd, void *arg)
1da177e4
LT
195{
196 struct video_device *dev = video_devdata(file);
f86e7767 197 struct radio_device *card = video_get_drvdata(dev);
6a2cf8ee
JS
198
199 switch (cmd) {
b6055d7b
MCC
200 case VIDIOC_QUERYCAP:
201 {
202 struct v4l2_capability *v = arg;
203 memset(v,0,sizeof(*v));
204 strlcpy(v->driver, "radio-maestro", sizeof (v->driver));
205 strlcpy(v->card, "Maestro Radio", sizeof (v->card));
206 sprintf(v->bus_info,"PCI");
207 v->version = RADIO_VERSION;
208 v->capabilities = V4L2_CAP_TUNER;
209
210 return 0;
211 }
212 case VIDIOC_G_TUNER:
213 {
214 struct v4l2_tuner *v = arg;
215
216 if (v->index > 0)
217 return -EINVAL;
218
219 (void)radio_bits_get(card);
220
221 memset(v,0,sizeof(*v));
222 strcpy(v->name, "FM");
223 v->type = V4L2_TUNER_RADIO;
224
225 v->rangelow = FREQ_LO;
226 v->rangehigh = FREQ_HI;
227 v->rxsubchans =V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
228 v->capability=V4L2_TUNER_CAP_LOW;
229 if(card->stereo)
230 v->audmode = V4L2_TUNER_MODE_STEREO;
231 else
232 v->audmode = V4L2_TUNER_MODE_MONO;
233 v->signal=card->tuned;
234
235 return 0;
236 }
237 case VIDIOC_S_TUNER:
238 {
239 struct v4l2_tuner *v = arg;
240
241 if (v->index > 0)
242 return -EINVAL;
243
244 return 0;
245 }
246 case VIDIOC_S_FREQUENCY:
247 {
248 struct v4l2_frequency *f = arg;
249
250 if (f->frequency < FREQ_LO || f->frequency > FREQ_HI)
251 return -EINVAL;
252 radio_bits_set(card, FREQ2BITS(f->frequency));
253
254 return 0;
255 }
256 case VIDIOC_G_FREQUENCY:
257 {
258 struct v4l2_frequency *f = arg;
259
260 f->type = V4L2_TUNER_RADIO;
261 f->frequency = BITS2FREQ(radio_bits_get(card));
262
263 return 0;
264 }
265 case VIDIOC_QUERYCTRL:
266 {
267 struct v4l2_queryctrl *qc = arg;
268 int i;
269
270 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
271 if (qc->id && qc->id == radio_qctrl[i].id) {
272 memcpy(qc, &(radio_qctrl[i]),
273 sizeof(*qc));
274 return (0);
275 }
276 }
6a2cf8ee 277 return -EINVAL;
b6055d7b
MCC
278 }
279 case VIDIOC_G_CTRL:
280 {
281 struct v4l2_control *ctrl= arg;
282
283 switch (ctrl->id) {
284 case V4L2_CID_AUDIO_MUTE:
285 ctrl->value=card->muted;
286 return (0);
287 }
6a2cf8ee 288 return -EINVAL;
b6055d7b
MCC
289 }
290 case VIDIOC_S_CTRL:
6a2cf8ee 291 {
b6055d7b
MCC
292 struct v4l2_control *ctrl= arg;
293
294 switch (ctrl->id) {
295 case V4L2_CID_AUDIO_MUTE:
296 {
297 register u16 io = card->io;
298 register u16 omask = inw(io + IO_MASK);
299 outw(~STR_WREN, io + IO_MASK);
300 outw((card->muted = ctrl->value ) ?
301 STR_WREN : 0, io);
302 udelay(4);
303 outw(omask, io + IO_MASK);
304 msleep(125);
305
306 return (0);
307 }
308 }
309 return -EINVAL;
1da177e4 310 }
b6055d7b
MCC
311 default:
312 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
313 radio_function);
1da177e4
LT
314 }
315}
316
317static int radio_ioctl(struct inode *inode, struct file *file,
6a2cf8ee 318 unsigned int cmd, unsigned long arg)
1da177e4
LT
319{
320 struct video_device *dev = video_devdata(file);
f86e7767 321 struct radio_device *card = video_get_drvdata(dev);
1da177e4
LT
322 int ret;
323
3593cab5 324 mutex_lock(&card->lock);
1da177e4 325 ret = video_usercopy(inode, file, cmd, arg, radio_function);
3593cab5 326 mutex_unlock(&card->lock);
6a2cf8ee 327
1da177e4
LT
328 return ret;
329}
330
0eaa21fd 331static u16 __devinit radio_power_on(struct radio_device *dev)
1da177e4 332{
0eaa21fd
JS
333 register u16 io = dev->io;
334 register u32 ofreq;
335 u16 omask, odir;
6a2cf8ee 336
1da177e4 337 omask = inw(io + IO_MASK);
6a2cf8ee 338 odir = (inw(io + IO_DIR) & ~STR_DATA) | (STR_CLK | STR_WREN);
1da177e4 339 outw(odir & ~STR_WREN, io + IO_DIR);
b6055d7b 340 dev->muted = inw(io) & STR_WREN ? 0 : 1;
1da177e4
LT
341 outw(odir, io + IO_DIR);
342 outw(~(STR_WREN | STR_CLK), io + IO_MASK);
343 outw(dev->muted ? 0 : STR_WREN, io);
344 udelay(16);
345 outw(omask, io + IO_MASK);
346 ofreq = radio_bits_get(dev);
6a2cf8ee
JS
347
348 if ((ofreq < FREQ2BITS(FREQ_LO)) || (ofreq > FREQ2BITS(FREQ_HI)))
1da177e4
LT
349 ofreq = FREQ2BITS(FREQ_LO);
350 radio_bits_set(dev, ofreq);
6a2cf8ee 351
1da177e4
LT
352 return (ofreq == radio_bits_get(dev));
353}
354
89dad8f0
JS
355static int __devinit maestro_probe(struct pci_dev *pdev,
356 const struct pci_device_id *ent)
1da177e4 357{
89dad8f0
JS
358 struct radio_device *radio_unit;
359 struct video_device *maestro_radio_inst;
360 int retval;
361
362 retval = pci_enable_device(pdev);
363 if (retval) {
364 dev_err(&pdev->dev, "enabling pci device failed!\n");
365 goto err;
366 }
367
368 retval = -ENOMEM;
369
370 radio_unit = kzalloc(sizeof(*radio_unit), GFP_KERNEL);
371 if (radio_unit == NULL) {
372 dev_err(&pdev->dev, "not enough memory\n");
373 goto err;
374 }
375
376 radio_unit->io = pci_resource_start(pdev, 0) + GPIO_DATA;
3593cab5 377 mutex_init(&radio_unit->lock);
89dad8f0
JS
378
379 maestro_radio_inst = video_device_alloc();
380 if (maestro_radio_inst == NULL) {
381 dev_err(&pdev->dev, "not enough memory\n");
382 goto errfr;
383 }
384
385 memcpy(maestro_radio_inst, &maestro_radio, sizeof(maestro_radio));
386 video_set_drvdata(maestro_radio_inst, radio_unit);
387 pci_set_drvdata(pdev, maestro_radio_inst);
388
389 retval = video_register_device(maestro_radio_inst, VFL_TYPE_RADIO,
390 radio_nr);
391 if (retval) {
392 printk(KERN_ERR "can't register video device!\n");
393 goto errfr1;
394 }
395
396 if (!radio_power_on(radio_unit)) {
397 retval = -EIO;
398 goto errunr;
399 }
400
401 dev_info(&pdev->dev, "version " DRIVER_VERSION " time " __TIME__ " "
6a2cf8ee 402 __DATE__ "\n");
89dad8f0
JS
403 dev_info(&pdev->dev, "radio chip initialized\n");
404
405 return 0;
406errunr:
407 video_unregister_device(maestro_radio_inst);
408errfr1:
409 kfree(maestro_radio_inst);
410errfr:
411 kfree(radio_unit);
412err:
413 return retval;
414
415}
416
417static void __devexit maestro_remove(struct pci_dev *pdev)
418{
419 struct video_device *vdev = pci_get_drvdata(pdev);
420
421 video_unregister_device(vdev);
1da177e4
LT
422}
423
89dad8f0
JS
424static int __init maestro_radio_init(void)
425{
426 int retval = pci_register_driver(&maestro_r_driver);
427
428 if (retval)
429 printk(KERN_ERR "error during registration pci driver\n");
430
431 return retval;
432}
433
434static void __exit maestro_radio_exit(void)
435{
436 pci_unregister_driver(&maestro_r_driver);
437}
438
439module_init(maestro_radio_init);
440module_exit(maestro_radio_exit);
6a2cf8ee
JS
441
442MODULE_AUTHOR("Adam Tlalka, atlka@pg.gda.pl");
443MODULE_DESCRIPTION("Radio driver for the Maestro PCI sound card radio.");
444MODULE_LICENSE("GPL");