xref: /openbmc/linux/drivers/media/pci/cx88/cx88-vp3054-i2c.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b285192aSMauro Carvalho Chehab /*
37b61ba8fSMauro Carvalho Chehab  * cx88-vp3054-i2c.c -- support for the secondary I2C bus of the
47b61ba8fSMauro Carvalho Chehab  *			DNTV Live! DVB-T Pro (VP-3054), wired as:
57b61ba8fSMauro Carvalho Chehab  *			GPIO[0] -> SCL, GPIO[1] -> SDA
67b61ba8fSMauro Carvalho Chehab  *
77b61ba8fSMauro Carvalho Chehab  * (c) 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8b285192aSMauro Carvalho Chehab  */
9b285192aSMauro Carvalho Chehab 
1065bc2fe8SMauro Carvalho Chehab #include "cx88.h"
1165bc2fe8SMauro Carvalho Chehab #include "cx88-vp3054-i2c.h"
1265bc2fe8SMauro Carvalho Chehab 
13b285192aSMauro Carvalho Chehab #include <linux/module.h>
14b285192aSMauro Carvalho Chehab #include <linux/slab.h>
15b285192aSMauro Carvalho Chehab #include <linux/init.h>
16399426caSMauro Carvalho Chehab #include <linux/io.h>
17b285192aSMauro Carvalho Chehab 
18b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
19b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
20b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
21b285192aSMauro Carvalho Chehab 
22b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
23b285192aSMauro Carvalho Chehab 
vp3054_bit_setscl(void * data,int state)24b285192aSMauro Carvalho Chehab static void vp3054_bit_setscl(void *data, int state)
25b285192aSMauro Carvalho Chehab {
26b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = data;
27b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
28b285192aSMauro Carvalho Chehab 	struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
29b285192aSMauro Carvalho Chehab 
30b285192aSMauro Carvalho Chehab 	if (state) {
31b285192aSMauro Carvalho Chehab 		vp3054_i2c->state |=  0x0001;	/* SCL high */
32b285192aSMauro Carvalho Chehab 		vp3054_i2c->state &= ~0x0100;	/* external pullup */
33b285192aSMauro Carvalho Chehab 	} else {
34b285192aSMauro Carvalho Chehab 		vp3054_i2c->state &= ~0x0001;	/* SCL low */
35b285192aSMauro Carvalho Chehab 		vp3054_i2c->state |=  0x0100;	/* drive pin */
36b285192aSMauro Carvalho Chehab 	}
37b285192aSMauro Carvalho Chehab 	cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state);
38b285192aSMauro Carvalho Chehab 	cx_read(MO_GP0_IO);
39b285192aSMauro Carvalho Chehab }
40b285192aSMauro Carvalho Chehab 
vp3054_bit_setsda(void * data,int state)41b285192aSMauro Carvalho Chehab static void vp3054_bit_setsda(void *data, int state)
42b285192aSMauro Carvalho Chehab {
43b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = data;
44b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
45b285192aSMauro Carvalho Chehab 	struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
46b285192aSMauro Carvalho Chehab 
47b285192aSMauro Carvalho Chehab 	if (state) {
48b285192aSMauro Carvalho Chehab 		vp3054_i2c->state |=  0x0002;	/* SDA high */
49b285192aSMauro Carvalho Chehab 		vp3054_i2c->state &= ~0x0200;	/* tristate pin */
50b285192aSMauro Carvalho Chehab 	} else {
51b285192aSMauro Carvalho Chehab 		vp3054_i2c->state &= ~0x0002;	/* SDA low */
52b285192aSMauro Carvalho Chehab 		vp3054_i2c->state |=  0x0200;	/* drive pin */
53b285192aSMauro Carvalho Chehab 	}
54b285192aSMauro Carvalho Chehab 	cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state);
55b285192aSMauro Carvalho Chehab 	cx_read(MO_GP0_IO);
56b285192aSMauro Carvalho Chehab }
57b285192aSMauro Carvalho Chehab 
vp3054_bit_getscl(void * data)58b285192aSMauro Carvalho Chehab static int vp3054_bit_getscl(void *data)
59b285192aSMauro Carvalho Chehab {
60b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = data;
61b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
62b285192aSMauro Carvalho Chehab 	u32 state;
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab 	state = cx_read(MO_GP0_IO);
65b285192aSMauro Carvalho Chehab 	return (state & 0x01) ? 1 : 0;
66b285192aSMauro Carvalho Chehab }
67b285192aSMauro Carvalho Chehab 
vp3054_bit_getsda(void * data)68b285192aSMauro Carvalho Chehab static int vp3054_bit_getsda(void *data)
69b285192aSMauro Carvalho Chehab {
70b285192aSMauro Carvalho Chehab 	struct cx8802_dev *dev = data;
71b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
72b285192aSMauro Carvalho Chehab 	u32 state;
73b285192aSMauro Carvalho Chehab 
74b285192aSMauro Carvalho Chehab 	state = cx_read(MO_GP0_IO);
75b285192aSMauro Carvalho Chehab 	return (state & 0x02) ? 1 : 0;
76b285192aSMauro Carvalho Chehab }
77b285192aSMauro Carvalho Chehab 
78b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
79b285192aSMauro Carvalho Chehab 
80b285192aSMauro Carvalho Chehab static const struct i2c_algo_bit_data vp3054_i2c_algo_template = {
81b285192aSMauro Carvalho Chehab 	.setsda  = vp3054_bit_setsda,
82b285192aSMauro Carvalho Chehab 	.setscl  = vp3054_bit_setscl,
83b285192aSMauro Carvalho Chehab 	.getsda  = vp3054_bit_getsda,
84b285192aSMauro Carvalho Chehab 	.getscl  = vp3054_bit_getscl,
85b285192aSMauro Carvalho Chehab 	.udelay  = 16,
86b285192aSMauro Carvalho Chehab 	.timeout = 200,
87b285192aSMauro Carvalho Chehab };
88b285192aSMauro Carvalho Chehab 
89b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------------------- */
90b285192aSMauro Carvalho Chehab 
vp3054_i2c_probe(struct cx8802_dev * dev)91b285192aSMauro Carvalho Chehab int vp3054_i2c_probe(struct cx8802_dev *dev)
92b285192aSMauro Carvalho Chehab {
93b285192aSMauro Carvalho Chehab 	struct cx88_core *core = dev->core;
94b285192aSMauro Carvalho Chehab 	struct vp3054_i2c_state *vp3054_i2c;
95b285192aSMauro Carvalho Chehab 	int rc;
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab 	if (core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
98b285192aSMauro Carvalho Chehab 		return 0;
99b285192aSMauro Carvalho Chehab 
100b285192aSMauro Carvalho Chehab 	vp3054_i2c = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
101399426caSMauro Carvalho Chehab 	if (!vp3054_i2c)
102b285192aSMauro Carvalho Chehab 		return -ENOMEM;
103b285192aSMauro Carvalho Chehab 	dev->vp3054 = vp3054_i2c;
104b285192aSMauro Carvalho Chehab 
105b5237747SEzequiel Garcia 	vp3054_i2c->algo = vp3054_i2c_algo_template;
106b285192aSMauro Carvalho Chehab 
107b285192aSMauro Carvalho Chehab 	vp3054_i2c->adap.dev.parent = &dev->pci->dev;
108c0decac1SMauro Carvalho Chehab 	strscpy(vp3054_i2c->adap.name, core->name,
109b285192aSMauro Carvalho Chehab 		sizeof(vp3054_i2c->adap.name));
110b285192aSMauro Carvalho Chehab 	vp3054_i2c->adap.owner = THIS_MODULE;
111b285192aSMauro Carvalho Chehab 	vp3054_i2c->algo.data = dev;
112b285192aSMauro Carvalho Chehab 	i2c_set_adapdata(&vp3054_i2c->adap, dev);
113b285192aSMauro Carvalho Chehab 	vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
114b285192aSMauro Carvalho Chehab 
115b285192aSMauro Carvalho Chehab 	vp3054_bit_setscl(dev, 1);
116b285192aSMauro Carvalho Chehab 	vp3054_bit_setsda(dev, 1);
117b285192aSMauro Carvalho Chehab 
118b285192aSMauro Carvalho Chehab 	rc = i2c_bit_add_bus(&vp3054_i2c->adap);
1197b61ba8fSMauro Carvalho Chehab 	if (rc != 0) {
12065bc2fe8SMauro Carvalho Chehab 		pr_err("vp3054_i2c register FAILED\n");
121b285192aSMauro Carvalho Chehab 
122b285192aSMauro Carvalho Chehab 		kfree(dev->vp3054);
123b285192aSMauro Carvalho Chehab 		dev->vp3054 = NULL;
124b285192aSMauro Carvalho Chehab 	}
125b285192aSMauro Carvalho Chehab 
126b285192aSMauro Carvalho Chehab 	return rc;
127b285192aSMauro Carvalho Chehab }
128399426caSMauro Carvalho Chehab EXPORT_SYMBOL(vp3054_i2c_probe);
129b285192aSMauro Carvalho Chehab 
vp3054_i2c_remove(struct cx8802_dev * dev)130b285192aSMauro Carvalho Chehab void vp3054_i2c_remove(struct cx8802_dev *dev)
131b285192aSMauro Carvalho Chehab {
132b285192aSMauro Carvalho Chehab 	struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
133b285192aSMauro Carvalho Chehab 
134399426caSMauro Carvalho Chehab 	if (!vp3054_i2c ||
135b285192aSMauro Carvalho Chehab 	    dev->core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
136b285192aSMauro Carvalho Chehab 		return;
137b285192aSMauro Carvalho Chehab 
138b285192aSMauro Carvalho Chehab 	i2c_del_adapter(&vp3054_i2c->adap);
139b285192aSMauro Carvalho Chehab 	kfree(vp3054_i2c);
140b285192aSMauro Carvalho Chehab }
141b285192aSMauro Carvalho Chehab EXPORT_SYMBOL(vp3054_i2c_remove);
142