blob: bd4d3a7cdadd130511cc186679124fd6231a929c [file] [log] [blame]
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03001/*
2 * Guillemot Maxi Radio FM 2000 PCI radio card driver for Linux
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) 2001 Dimitromanolakis Apostolos <apdim@grecian.net>
4 *
5 * Based in the radio Maestro PCI driver. Actually it uses the same chip
6 * for radio but different pci controller.
7 *
8 * I didn't have any specs I reversed engineered the protocol from
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -03009 * the windows driver (radio.dll).
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * The card uses the TEA5757 chip that includes a search function but it
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030012 * is useless as I haven't found any way to read back the frequency. If
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * anybody does please mail me.
14 *
15 * For the pdf file see:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020016 * http://www.nxp.com/acrobat_download2/expired_datasheets/TEA5757_5759_3.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 *
19 * CHANGES:
20 * 0.75b
21 * - better pci interface thanks to Francois Romieu <romieu@cogenit.fr>
22 *
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030023 * 0.75 Sun Feb 4 22:51:27 EET 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * - tiding up
25 * - removed support for multiple devices as it didn't work anyway
26 *
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -030027 * BUGS:
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * - card unmutes if you change frequency
29 *
Mauro Carvalho Chehab06470ed2007-01-25 09:04:34 -030030 * (c) 2006, 2007 by Mauro Carvalho Chehab <mchehab@infradead.org>:
31 * - Conversion to V4L2 API
32 * - Uses video_ioctl2 for parsing and to add debug support
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/ioport.h>
39#include <linux/delay.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020040#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/pci.h>
Mauro Carvalho Chehabe84fef62006-08-08 09:10:05 -030042#include <linux/videodev2.h>
Hans Verkuil2710e6a2009-03-06 13:51:33 -030043#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Hans Verkuilcfb19b02012-02-05 09:53:17 -030045#include <sound/tea575x-tuner.h>
Hans Verkuil2710e6a2009-03-06 13:51:33 -030046#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030047#include <media/v4l2-ioctl.h>
Hans Verkuilcfb19b02012-02-05 09:53:17 -030048#include <media/v4l2-fh.h>
49#include <media/v4l2-ctrls.h>
50#include <media/v4l2-event.h>
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030051
Hans Verkuil2710e6a2009-03-06 13:51:33 -030052MODULE_AUTHOR("Dimitromanolakis Apostolos, apdim@grecian.net");
Hans Verkuilcfb19b02012-02-05 09:53:17 -030053MODULE_DESCRIPTION("Radio driver for the Guillemot Maxi Radio FM2000.");
Hans Verkuil2710e6a2009-03-06 13:51:33 -030054MODULE_LICENSE("GPL");
Hans Verkuilcfb19b02012-02-05 09:53:17 -030055MODULE_VERSION("1.0.0");
Hans Verkuil2710e6a2009-03-06 13:51:33 -030056
57static int radio_nr = -1;
Hans Verkuilcfb19b02012-02-05 09:53:17 -030058module_param(radio_nr, int, 0644);
59MODULE_PARM_DESC(radio_nr, "Radio device number");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* TEA5757 pin mappings */
Hans Verkuil2710e6a2009-03-06 13:51:33 -030062static const int clk = 1, data = 2, wren = 4, mo_st = 8, power = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Hans Verkuilcfb19b02012-02-05 09:53:17 -030064static atomic_t maxiradio_instance = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Hans Verkuilcfb19b02012-02-05 09:53:17 -030066#define PCI_VENDOR_ID_GUILLEMOT 0x5046
67#define PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO 0x1001
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Hans Verkuil2710e6a2009-03-06 13:51:33 -030069struct maxiradio
Hans Verkuil3ca685a2008-08-23 04:49:13 -030070{
Hans Verkuilcfb19b02012-02-05 09:53:17 -030071 struct snd_tea575x tea;
Hans Verkuil2710e6a2009-03-06 13:51:33 -030072 struct v4l2_device v4l2_dev;
Hans Verkuil2710e6a2009-03-06 13:51:33 -030073 struct pci_dev *pdev;
Hans Verkuil3ca685a2008-08-23 04:49:13 -030074
Hans Verkuil2710e6a2009-03-06 13:51:33 -030075 u16 io; /* base of radio io */
Mauro Carvalho Chehab712642b2007-01-26 07:33:07 -030076};
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Hans Verkuil2710e6a2009-03-06 13:51:33 -030078static inline struct maxiradio *to_maxiradio(struct v4l2_device *v4l2_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Hans Verkuil2710e6a2009-03-06 13:51:33 -030080 return container_of(v4l2_dev, struct maxiradio, v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Hans Verkuilcfb19b02012-02-05 09:53:17 -030083static void maxiradio_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
Hans Verkuil2710e6a2009-03-06 13:51:33 -030084{
Hans Verkuilcfb19b02012-02-05 09:53:17 -030085 struct maxiradio *dev = tea->private_data;
86 u8 bits = 0;
Hans Verkuil2710e6a2009-03-06 13:51:33 -030087
Hans Verkuilcfb19b02012-02-05 09:53:17 -030088 bits |= (pins & TEA575X_DATA) ? data : 0;
89 bits |= (pins & TEA575X_CLK) ? clk : 0;
90 bits |= (pins & TEA575X_WREN) ? wren : 0;
91 bits |= power;
92
93 outb(bits, dev->io);
Hans Verkuil2710e6a2009-03-06 13:51:33 -030094}
95
Hans Verkuilcfb19b02012-02-05 09:53:17 -030096/* Note: this card cannot read out the data of the shift registers,
97 only the mono/stereo pin works. */
98static u8 maxiradio_tea575x_get_pins(struct snd_tea575x *tea)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300100 struct maxiradio *dev = tea->private_data;
101 u8 bits = inb(dev->io);
102
103 return ((bits & data) ? TEA575X_DATA : 0) |
104 ((bits & mo_st) ? TEA575X_MOST : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300107static void maxiradio_tea575x_set_direction(struct snd_tea575x *tea, bool output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300111static struct snd_tea575x_ops maxiradio_tea_ops = {
112 .set_pins = maxiradio_tea575x_set_pins,
113 .get_pins = maxiradio_tea575x_get_pins,
114 .set_direction = maxiradio_tea575x_set_direction,
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300115};
116
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800117static int maxiradio_probe(struct pci_dev *pdev,
118 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300120 struct maxiradio *dev;
121 struct v4l2_device *v4l2_dev;
122 int retval = -ENOMEM;
123
124 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
125 if (dev == NULL) {
126 dev_err(&pdev->dev, "not enough memory\n");
127 return -ENOMEM;
128 }
129
130 v4l2_dev = &dev->v4l2_dev;
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300131 v4l2_device_set_name(v4l2_dev, "maxiradio", &maxiradio_instance);
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300132
133 retval = v4l2_device_register(&pdev->dev, v4l2_dev);
134 if (retval < 0) {
135 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
136 goto errfr;
137 }
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300138 dev->tea.private_data = dev;
139 dev->tea.ops = &maxiradio_tea_ops;
140 /* The data pin cannot be read. This may be a hardware limitation, or
141 we just don't know how to read it. */
142 dev->tea.cannot_read_data = true;
143 dev->tea.v4l2_dev = v4l2_dev;
144 dev->tea.radio_nr = radio_nr;
145 strlcpy(dev->tea.card, "Maxi Radio FM2000", sizeof(dev->tea.card));
146 snprintf(dev->tea.bus_info, sizeof(dev->tea.bus_info),
147 "PCI:%s", pci_name(pdev));
148
149 retval = -ENODEV;
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300150
151 if (!request_region(pci_resource_start(pdev, 0),
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300152 pci_resource_len(pdev, 0), v4l2_dev->name)) {
153 dev_err(&pdev->dev, "can't reserve I/O ports\n");
154 goto err_hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
157 if (pci_enable_device(pdev))
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300158 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300160 dev->io = pci_resource_start(pdev, 0);
Hans de Goede5daf53a2012-05-19 07:57:03 -0300161 if (snd_tea575x_init(&dev->tea, THIS_MODULE)) {
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300162 printk(KERN_ERR "radio-maxiradio: Unable to detect TEA575x tuner\n");
Mauro Carvalho Chehab4286c6f2006-04-08 16:06:16 -0300163 goto err_out_free_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166
167err_out_free_region:
168 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300169err_hdl:
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300170 v4l2_device_unregister(v4l2_dev);
171errfr:
172 kfree(dev);
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300173 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800176static void maxiradio_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300178 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
179 struct maxiradio *dev = to_maxiradio(v4l2_dev);
180
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300181 snd_tea575x_exit(&dev->tea);
182 /* Turn off power */
183 outb(0, dev->io);
184 v4l2_device_unregister(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 release_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
186}
187
188static struct pci_device_id maxiradio_pci_tbl[] = {
189 { PCI_VENDOR_ID_GUILLEMOT, PCI_DEVICE_ID_GUILLEMOT_MAXIRADIO,
190 PCI_ANY_ID, PCI_ANY_ID, },
Hans Verkuil2710e6a2009-03-06 13:51:33 -0300191 { 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194MODULE_DEVICE_TABLE(pci, maxiradio_pci_tbl);
195
196static struct pci_driver maxiradio_driver = {
197 .name = "radio-maxiradio",
198 .id_table = maxiradio_pci_tbl,
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300199 .probe = maxiradio_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800200 .remove = maxiradio_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300203static int __init maxiradio_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Richard Knutsson9bfab8c2005-11-30 00:59:34 +0100205 return pci_register_driver(&maxiradio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300208static void __exit maxiradio_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 pci_unregister_driver(&maxiradio_driver);
211}
212
Hans Verkuilcfb19b02012-02-05 09:53:17 -0300213module_init(maxiradio_init);
214module_exit(maxiradio_exit);