Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[pandora-kernel.git] / drivers / media / video / pvrusb2 / pvrusb2-tuner.c
1 /*
2  *
3  *  $Id$
4  *
5  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
6  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "pvrusb2.h"
24 #include "pvrusb2-util.h"
25 #include "pvrusb2-tuner.h"
26 #include "pvrusb2-hdw-internal.h"
27 #include "pvrusb2-debug.h"
28 #include <linux/videodev2.h>
29 #include <media/tuner.h>
30 #include <media/v4l2-common.h>
31
32 struct pvr2_tuner_handler {
33         struct pvr2_hdw *hdw;
34         struct pvr2_i2c_client *client;
35         struct pvr2_i2c_handler i2c_handler;
36         int type_update_fl;
37 };
38
39
40 static void set_type(struct pvr2_tuner_handler *ctxt)
41 {
42         struct pvr2_hdw *hdw = ctxt->hdw;
43         struct tuner_setup setup;
44         pvr2_trace(PVR2_TRACE_CHIPS,"i2c tuner set_type(%d)",hdw->tuner_type);
45         if (((int)(hdw->tuner_type)) < 0) return;
46
47         setup.addr = ADDR_UNSET;
48         setup.type = hdw->tuner_type;
49         setup.mode_mask = T_RADIO | T_ANALOG_TV;
50         /* We may really want mode_mask to be T_ANALOG_TV for now */
51         pvr2_i2c_client_cmd(ctxt->client,TUNER_SET_TYPE_ADDR,&setup);
52         ctxt->type_update_fl = 0;
53 }
54
55
56 static int tuner_check(struct pvr2_tuner_handler *ctxt)
57 {
58         struct pvr2_hdw *hdw = ctxt->hdw;
59         if (hdw->tuner_updated) ctxt->type_update_fl = !0;
60         return ctxt->type_update_fl != 0;
61 }
62
63
64 static void tuner_update(struct pvr2_tuner_handler *ctxt)
65 {
66         if (ctxt->type_update_fl) set_type(ctxt);
67 }
68
69
70 static void pvr2_tuner_detach(struct pvr2_tuner_handler *ctxt)
71 {
72         ctxt->client->handler = NULL;
73         kfree(ctxt);
74 }
75
76
77 static unsigned int pvr2_tuner_describe(struct pvr2_tuner_handler *ctxt,char *buf,unsigned int cnt)
78 {
79         return scnprintf(buf,cnt,"handler: pvrusb2-tuner");
80 }
81
82
83 static const struct pvr2_i2c_handler_functions tuner_funcs = {
84         .detach = (void (*)(void *))pvr2_tuner_detach,
85         .check = (int (*)(void *))tuner_check,
86         .update = (void (*)(void *))tuner_update,
87         .describe = (unsigned int (*)(void *,char *,unsigned int))pvr2_tuner_describe,
88 };
89
90
91 int pvr2_i2c_tuner_setup(struct pvr2_hdw *hdw,struct pvr2_i2c_client *cp)
92 {
93         struct pvr2_tuner_handler *ctxt;
94         if (cp->handler) return 0;
95
96         ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
97         if (!ctxt) return 0;
98         memset(ctxt,0,sizeof(*ctxt));
99
100         ctxt->i2c_handler.func_data = ctxt;
101         ctxt->i2c_handler.func_table = &tuner_funcs;
102         ctxt->type_update_fl = !0;
103         ctxt->client = cp;
104         ctxt->hdw = hdw;
105         cp->handler = &ctxt->i2c_handler;
106         pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x tuner handler set up",
107                    cp->client->addr);
108         return !0;
109 }
110
111
112
113
114 /*
115   Stuff for Emacs to see, in order to encourage consistent editing style:
116   *** Local Variables: ***
117   *** mode: c ***
118   *** fill-column: 70 ***
119   *** tab-width: 8 ***
120   *** c-basic-offset: 8 ***
121   *** End: ***
122   */