xref: /linux/drivers/media/pci/cx88/cx88-vp3054-i2c.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
1 /*
2  * cx88-vp3054-i2c.c -- support for the secondary I2C bus of the
3  *			DNTV Live! DVB-T Pro (VP-3054), wired as:
4  *			GPIO[0] -> SCL, GPIO[1] -> SDA
5  *
6  * (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
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, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #include "cx88.h"
20 #include "cx88-vp3054-i2c.h"
21 
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/io.h>
26 
27 MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
28 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
29 MODULE_LICENSE("GPL");
30 
31 /* ----------------------------------------------------------------------- */
32 
33 static void vp3054_bit_setscl(void *data, int state)
34 {
35 	struct cx8802_dev *dev = data;
36 	struct cx88_core *core = dev->core;
37 	struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
38 
39 	if (state) {
40 		vp3054_i2c->state |=  0x0001;	/* SCL high */
41 		vp3054_i2c->state &= ~0x0100;	/* external pullup */
42 	} else {
43 		vp3054_i2c->state &= ~0x0001;	/* SCL low */
44 		vp3054_i2c->state |=  0x0100;	/* drive pin */
45 	}
46 	cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
47 	cx_read(MO_GP0_IO);
48 }
49 
50 static void vp3054_bit_setsda(void *data, int state)
51 {
52 	struct cx8802_dev *dev = data;
53 	struct cx88_core *core = dev->core;
54 	struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
55 
56 	if (state) {
57 		vp3054_i2c->state |=  0x0002;	/* SDA high */
58 		vp3054_i2c->state &= ~0x0200;	/* tristate pin */
59 	} else {
60 		vp3054_i2c->state &= ~0x0002;	/* SDA low */
61 		vp3054_i2c->state |=  0x0200;	/* drive pin */
62 	}
63 	cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
64 	cx_read(MO_GP0_IO);
65 }
66 
67 static int vp3054_bit_getscl(void *data)
68 {
69 	struct cx8802_dev *dev = data;
70 	struct cx88_core *core = dev->core;
71 	u32 state;
72 
73 	state = cx_read(MO_GP0_IO);
74 	return (state & 0x01) ? 1 : 0;
75 }
76 
77 static int vp3054_bit_getsda(void *data)
78 {
79 	struct cx8802_dev *dev = data;
80 	struct cx88_core *core = dev->core;
81 	u32 state;
82 
83 	state = cx_read(MO_GP0_IO);
84 	return (state & 0x02) ? 1 : 0;
85 }
86 
87 /* ----------------------------------------------------------------------- */
88 
89 static const struct i2c_algo_bit_data vp3054_i2c_algo_template = {
90 	.setsda  = vp3054_bit_setsda,
91 	.setscl  = vp3054_bit_setscl,
92 	.getsda  = vp3054_bit_getsda,
93 	.getscl  = vp3054_bit_getscl,
94 	.udelay  = 16,
95 	.timeout = 200,
96 };
97 
98 /* ----------------------------------------------------------------------- */
99 
100 int vp3054_i2c_probe(struct cx8802_dev *dev)
101 {
102 	struct cx88_core *core = dev->core;
103 	struct vp3054_i2c_state *vp3054_i2c;
104 	int rc;
105 
106 	if (core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
107 		return 0;
108 
109 	vp3054_i2c = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
110 	if (!vp3054_i2c)
111 		return -ENOMEM;
112 	dev->vp3054 = vp3054_i2c;
113 
114 	vp3054_i2c->algo = vp3054_i2c_algo_template;
115 
116 	vp3054_i2c->adap.dev.parent = &dev->pci->dev;
117 	strscpy(vp3054_i2c->adap.name, core->name,
118 		sizeof(vp3054_i2c->adap.name));
119 	vp3054_i2c->adap.owner = THIS_MODULE;
120 	vp3054_i2c->algo.data = dev;
121 	i2c_set_adapdata(&vp3054_i2c->adap, dev);
122 	vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
123 
124 	vp3054_bit_setscl(dev, 1);
125 	vp3054_bit_setsda(dev, 1);
126 
127 	rc = i2c_bit_add_bus(&vp3054_i2c->adap);
128 	if (rc != 0) {
129 		pr_err("vp3054_i2c register FAILED\n");
130 
131 		kfree(dev->vp3054);
132 		dev->vp3054 = NULL;
133 	}
134 
135 	return rc;
136 }
137 EXPORT_SYMBOL(vp3054_i2c_probe);
138 
139 void vp3054_i2c_remove(struct cx8802_dev *dev)
140 {
141 	struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
142 
143 	if (!vp3054_i2c ||
144 	    dev->core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
145 		return;
146 
147 	i2c_del_adapter(&vp3054_i2c->adap);
148 	kfree(vp3054_i2c);
149 }
150 EXPORT_SYMBOL(vp3054_i2c_remove);
151