V4L/DVB (8913): cx18: Create cx18_ specific wrappers for all pci mmio accessesors.
[linux-2.6] / drivers / media / video / cx18 / cx18-dvb.c
1 /*
2  *  cx18 functions for DVB support
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "cx18-version.h"
23 #include "cx18-dvb.h"
24 #include "cx18-io.h"
25 #include "cx18-streams.h"
26 #include "cx18-cards.h"
27 #include "s5h1409.h"
28 #include "mxl5005s.h"
29
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31
32 #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
33
34 static struct mxl5005s_config hauppauge_hvr1600_tuner = {
35         .i2c_address     = 0xC6 >> 1,
36         .if_freq         = IF_FREQ_5380000HZ,
37         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
38         .agc_mode        = MXL_SINGLE_AGC,
39         .tracking_filter = MXL_TF_C_H,
40         .rssi_enable     = MXL_RSSI_ENABLE,
41         .cap_select      = MXL_CAP_SEL_ENABLE,
42         .div_out         = MXL_DIV_OUT_4,
43         .clock_out       = MXL_CLOCK_OUT_DISABLE,
44         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
45         .top             = MXL5005S_TOP_25P2,
46         .mod_mode        = MXL_DIGITAL_MODE,
47         .if_mode         = MXL_ZERO_IF,
48         .AgcMasterByte   = 0x00,
49 };
50
51 static struct s5h1409_config hauppauge_hvr1600_config = {
52         .demod_address = 0x32 >> 1,
53         .output_mode   = S5H1409_SERIAL_OUTPUT,
54         .gpio          = S5H1409_GPIO_ON,
55         .qam_if        = 44000,
56         .inversion     = S5H1409_INVERSION_OFF,
57         .status_mode   = S5H1409_DEMODLOCKING,
58         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
59
60 };
61
62 static int dvb_register(struct cx18_stream *stream);
63
64 /* Kernel DVB framework calls this when the feed needs to start.
65  * The CX18 framework should enable the transport DMA handling
66  * and queue processing.
67  */
68 static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
69 {
70         struct dvb_demux *demux = feed->demux;
71         struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
72         struct cx18 *cx = stream->cx;
73         int ret;
74         u32 v;
75
76         CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
77                         feed->pid, feed->index);
78
79         mutex_lock(&cx->serialize_lock);
80         ret = cx18_init_on_first_open(cx);
81         mutex_unlock(&cx->serialize_lock);
82         if (ret) {
83                 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
84                 return ret;
85         }
86         ret = -EINVAL;
87
88         switch (cx->card->type) {
89         case CX18_CARD_HVR_1600_ESMT:
90         case CX18_CARD_HVR_1600_SAMSUNG:
91                 v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
92                 v |= 0x00400000; /* Serial Mode */
93                 v |= 0x00002000; /* Data Length - Byte */
94                 v |= 0x00010000; /* Error - Polarity */
95                 v |= 0x00020000; /* Error - Passthru */
96                 v |= 0x000c0000; /* Error - Ignore */
97                 cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
98                 break;
99
100         default:
101                 /* Assumption - Parallel transport - Signalling
102                  * undefined or default.
103                  */
104                 break;
105         }
106
107         if (!demux->dmx.frontend)
108                 return -EINVAL;
109
110         if (stream) {
111                 mutex_lock(&stream->dvb.feedlock);
112                 if (stream->dvb.feeding++ == 0) {
113                         CX18_DEBUG_INFO("Starting Transport DMA\n");
114                         ret = cx18_start_v4l2_encode_stream(stream);
115                         if (ret < 0) {
116                                 CX18_DEBUG_INFO(
117                                         "Failed to start Transport DMA\n");
118                                 stream->dvb.feeding--;
119                         }
120                 } else
121                         ret = 0;
122                 mutex_unlock(&stream->dvb.feedlock);
123         }
124
125         return ret;
126 }
127
128 /* Kernel DVB framework calls this when the feed needs to stop. */
129 static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
130 {
131         struct dvb_demux *demux = feed->demux;
132         struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
133         struct cx18 *cx = stream->cx;
134         int ret = -EINVAL;
135
136         CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
137                         feed->pid, feed->index);
138
139         if (stream) {
140                 mutex_lock(&stream->dvb.feedlock);
141                 if (--stream->dvb.feeding == 0) {
142                         CX18_DEBUG_INFO("Stopping Transport DMA\n");
143                         ret = cx18_stop_v4l2_encode_stream(stream, 0);
144                 } else
145                         ret = 0;
146                 mutex_unlock(&stream->dvb.feedlock);
147         }
148
149         return ret;
150 }
151
152 int cx18_dvb_register(struct cx18_stream *stream)
153 {
154         struct cx18 *cx = stream->cx;
155         struct cx18_dvb *dvb = &stream->dvb;
156         struct dvb_adapter *dvb_adapter;
157         struct dvb_demux *dvbdemux;
158         struct dmx_demux *dmx;
159         int ret;
160
161         if (!dvb)
162                 return -EINVAL;
163
164         ret = dvb_register_adapter(&dvb->dvb_adapter,
165                         CX18_DRIVER_NAME,
166                         THIS_MODULE, &cx->dev->dev, adapter_nr);
167         if (ret < 0)
168                 goto err_out;
169
170         dvb_adapter = &dvb->dvb_adapter;
171
172         dvbdemux = &dvb->demux;
173
174         dvbdemux->priv = (void *)stream;
175
176         dvbdemux->filternum = 256;
177         dvbdemux->feednum = 256;
178         dvbdemux->start_feed = cx18_dvb_start_feed;
179         dvbdemux->stop_feed = cx18_dvb_stop_feed;
180         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
181                 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
182         ret = dvb_dmx_init(dvbdemux);
183         if (ret < 0)
184                 goto err_dvb_unregister_adapter;
185
186         dmx = &dvbdemux->dmx;
187
188         dvb->hw_frontend.source = DMX_FRONTEND_0;
189         dvb->mem_frontend.source = DMX_MEMORY_FE;
190         dvb->dmxdev.filternum = 256;
191         dvb->dmxdev.demux = dmx;
192
193         ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
194         if (ret < 0)
195                 goto err_dvb_dmx_release;
196
197         ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
198         if (ret < 0)
199                 goto err_dvb_dmxdev_release;
200
201         ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
202         if (ret < 0)
203                 goto err_remove_hw_frontend;
204
205         ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
206         if (ret < 0)
207                 goto err_remove_mem_frontend;
208
209         ret = dvb_register(stream);
210         if (ret < 0)
211                 goto err_disconnect_frontend;
212
213         dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
214
215         CX18_INFO("DVB Frontend registered\n");
216         mutex_init(&dvb->feedlock);
217         dvb->enabled = 1;
218         return ret;
219
220 err_disconnect_frontend:
221         dmx->disconnect_frontend(dmx);
222 err_remove_mem_frontend:
223         dmx->remove_frontend(dmx, &dvb->mem_frontend);
224 err_remove_hw_frontend:
225         dmx->remove_frontend(dmx, &dvb->hw_frontend);
226 err_dvb_dmxdev_release:
227         dvb_dmxdev_release(&dvb->dmxdev);
228 err_dvb_dmx_release:
229         dvb_dmx_release(dvbdemux);
230 err_dvb_unregister_adapter:
231         dvb_unregister_adapter(dvb_adapter);
232 err_out:
233         return ret;
234 }
235
236 void cx18_dvb_unregister(struct cx18_stream *stream)
237 {
238         struct cx18 *cx = stream->cx;
239         struct cx18_dvb *dvb = &stream->dvb;
240         struct dvb_adapter *dvb_adapter;
241         struct dvb_demux *dvbdemux;
242         struct dmx_demux *dmx;
243
244         CX18_INFO("unregister DVB\n");
245
246         dvb_adapter = &dvb->dvb_adapter;
247         dvbdemux = &dvb->demux;
248         dmx = &dvbdemux->dmx;
249
250         dmx->close(dmx);
251         dvb_net_release(&dvb->dvbnet);
252         dmx->remove_frontend(dmx, &dvb->mem_frontend);
253         dmx->remove_frontend(dmx, &dvb->hw_frontend);
254         dvb_dmxdev_release(&dvb->dmxdev);
255         dvb_dmx_release(dvbdemux);
256         dvb_unregister_frontend(dvb->fe);
257         dvb_frontend_detach(dvb->fe);
258         dvb_unregister_adapter(dvb_adapter);
259 }
260
261 /* All the DVB attach calls go here, this function get's modified
262  * for each new card. No other function in this file needs
263  * to change.
264  */
265 static int dvb_register(struct cx18_stream *stream)
266 {
267         struct cx18_dvb *dvb = &stream->dvb;
268         struct cx18 *cx = stream->cx;
269         int ret = 0;
270
271         switch (cx->card->type) {
272         case CX18_CARD_HVR_1600_ESMT:
273         case CX18_CARD_HVR_1600_SAMSUNG:
274                 dvb->fe = dvb_attach(s5h1409_attach,
275                         &hauppauge_hvr1600_config,
276                         &cx->i2c_adap[0]);
277                 if (dvb->fe != NULL) {
278                         dvb_attach(mxl5005s_attach, dvb->fe,
279                                 &cx->i2c_adap[0],
280                                 &hauppauge_hvr1600_tuner);
281                         ret = 0;
282                 }
283                 break;
284         default:
285                 /* No Digital Tv Support */
286                 break;
287         }
288
289         if (dvb->fe == NULL) {
290                 CX18_ERR("frontend initialization failed\n");
291                 return -1;
292         }
293
294         ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
295         if (ret < 0) {
296                 if (dvb->fe->ops.release)
297                         dvb->fe->ops.release(dvb->fe);
298                 return ret;
299         }
300
301         return ret;
302 }