Chris Pascoe | fc40b26 | 2006-01-09 15:25:35 -0200 | [diff] [blame] | 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 | /* ----------------------------------------------------------------------- */ |
| 36 | |
| 37 | static void vp3054_bit_setscl(void *data, int state) |
| 38 | { |
| 39 | struct cx8802_dev *dev = data; |
| 40 | struct cx88_core *core = dev->core; |
| 41 | struct vp3054_i2c_state *vp3054_i2c = dev->card_priv; |
| 42 | |
| 43 | if (state) { |
| 44 | vp3054_i2c->state |= 0x0001; /* SCL high */ |
| 45 | vp3054_i2c->state &= ~0x0100; /* external pullup */ |
| 46 | } else { |
| 47 | vp3054_i2c->state &= ~0x0001; /* SCL low */ |
| 48 | vp3054_i2c->state |= 0x0100; /* drive pin */ |
| 49 | } |
| 50 | cx_write(MO_GP0_IO, 0x010000 | vp3054_i2c->state); |
| 51 | cx_read(MO_GP0_IO); |
| 52 | } |
| 53 | |
| 54 | static void vp3054_bit_setsda(void *data, int state) |
| 55 | { |
| 56 | struct cx8802_dev *dev = data; |
| 57 | struct cx88_core *core = dev->core; |
| 58 | struct vp3054_i2c_state *vp3054_i2c = dev->card_priv; |
| 59 | |
| 60 | if (state) { |
| 61 | vp3054_i2c->state |= 0x0002; /* SDA high */ |
| 62 | vp3054_i2c->state &= ~0x0200; /* tristate pin */ |
| 63 | } else { |
| 64 | vp3054_i2c->state &= ~0x0002; /* SDA low */ |
| 65 | vp3054_i2c->state |= 0x0200; /* drive pin */ |
| 66 | } |
| 67 | cx_write(MO_GP0_IO, 0x020000 | vp3054_i2c->state); |
| 68 | cx_read(MO_GP0_IO); |
| 69 | } |
| 70 | |
| 71 | static int vp3054_bit_getscl(void *data) |
| 72 | { |
| 73 | struct cx8802_dev *dev = data; |
| 74 | struct cx88_core *core = dev->core; |
| 75 | u32 state; |
| 76 | |
| 77 | state = cx_read(MO_GP0_IO); |
| 78 | return (state & 0x01) ? 1 : 0; |
| 79 | } |
| 80 | |
| 81 | static int vp3054_bit_getsda(void *data) |
| 82 | { |
| 83 | struct cx8802_dev *dev = data; |
| 84 | struct cx88_core *core = dev->core; |
| 85 | u32 state; |
| 86 | |
| 87 | state = cx_read(MO_GP0_IO); |
| 88 | return (state & 0x02) ? 1 : 0; |
| 89 | } |
| 90 | |
| 91 | /* ----------------------------------------------------------------------- */ |
| 92 | |
| 93 | static struct i2c_algo_bit_data vp3054_i2c_algo_template = { |
| 94 | .setsda = vp3054_bit_setsda, |
| 95 | .setscl = vp3054_bit_setscl, |
| 96 | .getsda = vp3054_bit_getsda, |
| 97 | .getscl = vp3054_bit_getscl, |
| 98 | .udelay = 16, |
| 99 | .mdelay = 10, |
| 100 | .timeout = 200, |
| 101 | }; |
| 102 | |
| 103 | /* ----------------------------------------------------------------------- */ |
| 104 | |
| 105 | static struct i2c_adapter vp3054_i2c_adap_template = { |
| 106 | .name = "cx2388x", |
| 107 | .owner = THIS_MODULE, |
| 108 | .id = I2C_HW_B_CX2388x, |
| 109 | }; |
| 110 | |
| 111 | static struct i2c_client vp3054_i2c_client_template = { |
| 112 | .name = "VP-3054", |
| 113 | }; |
| 114 | |
| 115 | int vp3054_i2c_probe(struct cx8802_dev *dev) |
| 116 | { |
| 117 | struct cx88_core *core = dev->core; |
| 118 | struct vp3054_i2c_state *vp3054_i2c; |
| 119 | int rc; |
| 120 | |
| 121 | if (core->board != CX88_BOARD_DNTV_LIVE_DVB_T_PRO) |
| 122 | return 0; |
| 123 | |
| 124 | dev->card_priv = kzalloc(sizeof(*vp3054_i2c), GFP_KERNEL); |
| 125 | if (dev->card_priv == NULL) |
| 126 | return -ENOMEM; |
| 127 | vp3054_i2c = dev->card_priv; |
| 128 | |
| 129 | memcpy(&vp3054_i2c->adap, &vp3054_i2c_adap_template, |
| 130 | sizeof(vp3054_i2c->adap)); |
| 131 | memcpy(&vp3054_i2c->algo, &vp3054_i2c_algo_template, |
| 132 | sizeof(vp3054_i2c->algo)); |
| 133 | memcpy(&vp3054_i2c->client, &vp3054_i2c_client_template, |
| 134 | sizeof(vp3054_i2c->client)); |
| 135 | |
| 136 | vp3054_i2c->adap.class |= I2C_CLASS_TV_DIGITAL; |
| 137 | |
| 138 | vp3054_i2c->adap.dev.parent = &dev->pci->dev; |
| 139 | strlcpy(vp3054_i2c->adap.name, core->name, |
| 140 | sizeof(vp3054_i2c->adap.name)); |
| 141 | vp3054_i2c->algo.data = dev; |
| 142 | i2c_set_adapdata(&vp3054_i2c->adap, dev); |
| 143 | vp3054_i2c->adap.algo_data = &vp3054_i2c->algo; |
| 144 | vp3054_i2c->client.adapter = &vp3054_i2c->adap; |
| 145 | |
| 146 | vp3054_bit_setscl(dev,1); |
| 147 | vp3054_bit_setsda(dev,1); |
| 148 | |
| 149 | rc = i2c_bit_add_bus(&vp3054_i2c->adap); |
| 150 | if (0 != rc) { |
| 151 | printk("%s: vp3054_i2c register FAILED\n", core->name); |
| 152 | |
| 153 | kfree(dev->card_priv); |
| 154 | dev->card_priv = NULL; |
| 155 | } |
| 156 | |
| 157 | return rc; |
| 158 | } |
| 159 | |
| 160 | void vp3054_i2c_remove(struct cx8802_dev *dev) |
| 161 | { |
| 162 | struct vp3054_i2c_state *vp3054_i2c = dev->card_priv; |
| 163 | |
| 164 | if (vp3054_i2c == NULL || |
| 165 | dev->core->board != CX88_BOARD_DNTV_LIVE_DVB_T_PRO) |
| 166 | return; |
| 167 | |
| 168 | i2c_bit_del_bus(&vp3054_i2c->adap); |
| 169 | kfree(vp3054_i2c); |
| 170 | } |
| 171 | |
| 172 | EXPORT_SYMBOL(vp3054_i2c_probe); |
| 173 | EXPORT_SYMBOL(vp3054_i2c_remove); |