Merge branch 'origin'
[linux-2.6] / drivers / media / video / cx88 / cx88-vp3054-i2c.c
1 /*
2
3     cx88-vp3054-i2c.c  --  support for the secondary I2C bus of the
4                            DNTV Live! DVB-T Pro (VP-3054), wired as:
5                            GPIO[0] -> SCL, GPIO[1] -> SDA
6
7     (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 */
24
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28
29 #include <asm/io.h>
30
31 #include "cx88.h"
32 #include "cx88-vp3054-i2c.h"
33
34
35 MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
36 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
37 MODULE_LICENSE("GPL");
38
39 /* ----------------------------------------------------------------------- */
40
41 static void vp3054_bit_setscl(void *data, int state)
42 {
43         struct cx8802_dev *dev = data;
44         struct cx88_core *core = dev->core;
45         struct vp3054_i2c_state *vp3054_i2c = dev->card_priv;
46
47         if (state) {
48                 vp3054_i2c->state |=  0x0001;   /* SCL high */
49                 vp3054_i2c->state &= ~0x0100;   /* external pullup */
50         } else {
51                 vp3054_i2c->state &= ~0x0001;   /* SCL low */
52                 vp3054_i2c->state |=  0x0100;   /* drive pin */
53         }
54         cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
55         cx_read(MO_GP0_IO);
56 }
57
58 static void vp3054_bit_setsda(void *data, int state)
59 {
60         struct cx8802_dev *dev = data;
61         struct cx88_core *core = dev->core;
62         struct vp3054_i2c_state *vp3054_i2c = dev->card_priv;
63
64         if (state) {
65                 vp3054_i2c->state |=  0x0002;   /* SDA high */
66                 vp3054_i2c->state &= ~0x0200;   /* tristate pin */
67         } else {
68                 vp3054_i2c->state &= ~0x0002;   /* SDA low */
69                 vp3054_i2c->state |=  0x0200;   /* drive pin */
70         }
71         cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
72         cx_read(MO_GP0_IO);
73 }
74
75 static int vp3054_bit_getscl(void *data)
76 {
77         struct cx8802_dev *dev = data;
78         struct cx88_core *core = dev->core;
79         u32 state;
80
81         state = cx_read(MO_GP0_IO);
82         return (state & 0x01) ? 1 : 0;
83 }
84
85 static int vp3054_bit_getsda(void *data)
86 {
87         struct cx8802_dev *dev = data;
88         struct cx88_core *core = dev->core;
89         u32 state;
90
91         state = cx_read(MO_GP0_IO);
92         return (state & 0x02) ? 1 : 0;
93 }
94
95 /* ----------------------------------------------------------------------- */
96
97 static struct i2c_algo_bit_data vp3054_i2c_algo_template = {
98         .setsda  = vp3054_bit_setsda,
99         .setscl  = vp3054_bit_setscl,
100         .getsda  = vp3054_bit_getsda,
101         .getscl  = vp3054_bit_getscl,
102         .udelay  = 16,
103         .mdelay  = 10,
104         .timeout = 200,
105 };
106
107 /* ----------------------------------------------------------------------- */
108
109 static struct i2c_adapter vp3054_i2c_adap_template = {
110         .name              = "cx2388x",
111         .owner             = THIS_MODULE,
112         .id                = I2C_HW_B_CX2388x,
113 };
114
115 static struct i2c_client vp3054_i2c_client_template = {
116         .name   = "VP-3054",
117 };
118
119 int vp3054_i2c_probe(struct cx8802_dev *dev)
120 {
121         struct cx88_core *core = dev->core;
122         struct vp3054_i2c_state *vp3054_i2c;
123         int rc;
124
125         if (core->board != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
126                 return 0;
127
128         dev->card_priv = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
129         if (dev->card_priv == NULL)
130                 return -ENOMEM;
131         vp3054_i2c = dev->card_priv;
132
133         memcpy(&vp3054_i2c->adap, &vp3054_i2c_adap_template,
134                sizeof(vp3054_i2c->adap));
135         memcpy(&vp3054_i2c->algo, &vp3054_i2c_algo_template,
136                sizeof(vp3054_i2c->algo));
137         memcpy(&vp3054_i2c->client, &vp3054_i2c_client_template,
138                sizeof(vp3054_i2c->client));
139
140         vp3054_i2c->adap.class |= I2C_CLASS_TV_DIGITAL;
141
142         vp3054_i2c->adap.dev.parent = &dev->pci->dev;
143         strlcpy(vp3054_i2c->adap.name, core->name,
144                 sizeof(vp3054_i2c->adap.name));
145         vp3054_i2c->algo.data = dev;
146         i2c_set_adapdata(&vp3054_i2c->adap, dev);
147         vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
148         vp3054_i2c->client.adapter = &vp3054_i2c->adap;
149
150         vp3054_bit_setscl(dev,1);
151         vp3054_bit_setsda(dev,1);
152
153         rc = i2c_bit_add_bus(&vp3054_i2c->adap);
154         if (0 != rc) {
155                 printk("%s: vp3054_i2c register FAILED\n", core->name);
156
157                 kfree(dev->card_priv);
158                 dev->card_priv = NULL;
159         }
160
161         return rc;
162 }
163
164 void vp3054_i2c_remove(struct cx8802_dev *dev)
165 {
166         struct vp3054_i2c_state *vp3054_i2c = dev->card_priv;
167
168         if (vp3054_i2c == NULL ||
169             dev->core->board != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
170                 return;
171
172         i2c_bit_del_bus(&vp3054_i2c->adap);
173         kfree(vp3054_i2c);
174 }
175
176 EXPORT_SYMBOL(vp3054_i2c_probe);
177 EXPORT_SYMBOL(vp3054_i2c_remove);