blob: 92876de3841ce4987167c7a59ca91956f47fd87c [file] [log] [blame]
Chris Pascoefc40b262006-01-09 15:25:35 -02001/*
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -02002 * 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 */
Chris Pascoefc40b262006-01-09 15:25:35 -020018
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -020019#include "cx88.h"
20#include "cx88-vp3054-i2c.h"
21
Chris Pascoefc40b262006-01-09 15:25:35 -020022#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Chris Pascoefc40b262006-01-09 15:25:35 -020024#include <linux/init.h>
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -020025#include <linux/io.h>
Chris Pascoefc40b262006-01-09 15:25:35 -020026
Mauro Carvalho Chehab45bf2da2006-01-13 14:10:24 -020027MODULE_DESCRIPTION("driver for cx2388x VP3054 design");
28MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
29MODULE_LICENSE("GPL");
30
Chris Pascoefc40b262006-01-09 15:25:35 -020031/* ----------------------------------------------------------------------- */
32
33static void vp3054_bit_setscl(void *data, int state)
34{
35 struct cx8802_dev *dev = data;
36 struct cx88_core *core = dev->core;
Trent Piephof0ad9092007-10-14 02:52:16 -030037 struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
Chris Pascoefc40b262006-01-09 15:25:35 -020038
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
50static void vp3054_bit_setsda(void *data, int state)
51{
52 struct cx8802_dev *dev = data;
53 struct cx88_core *core = dev->core;
Trent Piephof0ad9092007-10-14 02:52:16 -030054 struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
Chris Pascoefc40b262006-01-09 15:25:35 -020055
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
67static 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
77static 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
Jean Delvare7e520d02007-07-01 18:37:51 -030089static const struct i2c_algo_bit_data vp3054_i2c_algo_template = {
Chris Pascoefc40b262006-01-09 15:25:35 -020090 .setsda = vp3054_bit_setsda,
91 .setscl = vp3054_bit_setscl,
92 .getsda = vp3054_bit_getsda,
93 .getscl = vp3054_bit_getscl,
94 .udelay = 16,
Chris Pascoefc40b262006-01-09 15:25:35 -020095 .timeout = 200,
96};
97
98/* ----------------------------------------------------------------------- */
99
Chris Pascoefc40b262006-01-09 15:25:35 -0200100int 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
Trent Piepho6a59d642007-08-15 14:41:57 -0300106 if (core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
Chris Pascoefc40b262006-01-09 15:25:35 -0200107 return 0;
108
Trent Piephof0ad9092007-10-14 02:52:16 -0300109 vp3054_i2c = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL);
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200110 if (!vp3054_i2c)
Chris Pascoefc40b262006-01-09 15:25:35 -0200111 return -ENOMEM;
Trent Piephof0ad9092007-10-14 02:52:16 -0300112 dev->vp3054 = vp3054_i2c;
Chris Pascoefc40b262006-01-09 15:25:35 -0200113
Ezequiel Garciab5237742012-10-23 15:57:19 -0300114 vp3054_i2c->algo = vp3054_i2c_algo_template;
Chris Pascoefc40b262006-01-09 15:25:35 -0200115
Chris Pascoefc40b262006-01-09 15:25:35 -0200116 vp3054_i2c->adap.dev.parent = &dev->pci->dev;
117 strlcpy(vp3054_i2c->adap.name, core->name,
118 sizeof(vp3054_i2c->adap.name));
Jean Delvare7e520d02007-07-01 18:37:51 -0300119 vp3054_i2c->adap.owner = THIS_MODULE;
Chris Pascoefc40b262006-01-09 15:25:35 -0200120 vp3054_i2c->algo.data = dev;
121 i2c_set_adapdata(&vp3054_i2c->adap, dev);
122 vp3054_i2c->adap.algo_data = &vp3054_i2c->algo;
Chris Pascoefc40b262006-01-09 15:25:35 -0200123
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200124 vp3054_bit_setscl(dev, 1);
125 vp3054_bit_setsda(dev, 1);
Chris Pascoefc40b262006-01-09 15:25:35 -0200126
127 rc = i2c_bit_add_bus(&vp3054_i2c->adap);
Mauro Carvalho Chehab7b61ba82016-11-16 06:59:49 -0200128 if (rc != 0) {
Mauro Carvalho Chehab65bc2fe2016-11-13 10:07:38 -0200129 pr_err("vp3054_i2c register FAILED\n");
Chris Pascoefc40b262006-01-09 15:25:35 -0200130
Trent Piephof0ad9092007-10-14 02:52:16 -0300131 kfree(dev->vp3054);
132 dev->vp3054 = NULL;
Chris Pascoefc40b262006-01-09 15:25:35 -0200133 }
134
135 return rc;
136}
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200137EXPORT_SYMBOL(vp3054_i2c_probe);
Chris Pascoefc40b262006-01-09 15:25:35 -0200138
139void vp3054_i2c_remove(struct cx8802_dev *dev)
140{
Trent Piephof0ad9092007-10-14 02:52:16 -0300141 struct vp3054_i2c_state *vp3054_i2c = dev->vp3054;
Chris Pascoefc40b262006-01-09 15:25:35 -0200142
Mauro Carvalho Chehab399426c2016-11-19 19:27:30 -0200143 if (!vp3054_i2c ||
Trent Piepho6a59d642007-08-15 14:41:57 -0300144 dev->core->boardnr != CX88_BOARD_DNTV_LIVE_DVB_T_PRO)
Chris Pascoefc40b262006-01-09 15:25:35 -0200145 return;
146
Jean Delvare32697112006-12-10 21:21:33 +0100147 i2c_del_adapter(&vp3054_i2c->adap);
Chris Pascoefc40b262006-01-09 15:25:35 -0200148 kfree(vp3054_i2c);
149}
Chris Pascoefc40b262006-01-09 15:25:35 -0200150EXPORT_SYMBOL(vp3054_i2c_remove);