Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | tea6415c - i2c-driver for the tea6415c by SGS Thomson |
| 3 | |
| 4 | Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de> |
| 5 | |
| 6 | The tea6415c is a bus controlled video-matrix-switch |
| 7 | with 8 inputs and 6 outputs. |
| 8 | It is cascadable, i.e. it can be found at the addresses |
| 9 | 0x86 and 0x06 on the i2c-bus. |
| 10 | |
| 11 | For detailed informations download the specifications directly |
| 12 | from SGS Thomson at http://www.st.com |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License vs published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 675 Mvss Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
Mauro Carvalho Chehab | a8733ca | 2006-03-17 10:37:02 -0300 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/ioctl.h> |
| 32 | #include <linux/i2c.h> |
| 33 | |
| 34 | #include "tea6415c.h" |
| 35 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 36 | static int debug; /* insmod parameter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | module_param(debug, int, 0644); |
| 38 | MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off)."); |
Hans Verkuil | f87086e | 2008-07-18 00:50:58 -0300 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define dprintk(args...) \ |
Harvey Harrison | 7e28adb | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 41 | do { if (debug) { printk("%s: %s()[%d]: ", KBUILD_MODNAME, __func__, __LINE__); printk(args); } } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #define TEA6415C_NUM_INPUTS 8 |
| 44 | #define TEA6415C_NUM_OUTPUTS 6 |
| 45 | |
| 46 | /* addresses to scan, found only at 0x03 and/or 0x43 (7-bit) */ |
| 47 | static unsigned short normal_i2c[] = { I2C_TEA6415C_1, I2C_TEA6415C_2, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* magic definition of all other variables and things */ |
| 50 | I2C_CLIENT_INSMOD; |
| 51 | |
| 52 | static struct i2c_driver driver; |
| 53 | static struct i2c_client client_template; |
| 54 | |
| 55 | /* this function is called by i2c_probe */ |
| 56 | static int detect(struct i2c_adapter *adapter, int address, int kind) |
| 57 | { |
| 58 | struct i2c_client *client = NULL; |
| 59 | int err = 0; |
| 60 | |
| 61 | /* let's see whether this adapter can support what we need */ |
| 62 | if (0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* allocate memory for client structure */ |
| 67 | client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Al Viro | 5fa1247 | 2008-03-29 03:07:38 +0000 | [diff] [blame] | 68 | if (!client) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return -ENOMEM; |
| 70 | } |
| 71 | |
| 72 | /* fill client structure */ |
| 73 | memcpy(client, &client_template, sizeof(struct i2c_client)); |
| 74 | client->addr = address; |
| 75 | client->adapter = adapter; |
| 76 | |
| 77 | /* tell the i2c layer a new client has arrived */ |
| 78 | if (0 != (err = i2c_attach_client(client))) { |
| 79 | kfree(client); |
| 80 | return err; |
| 81 | } |
| 82 | |
| 83 | printk("tea6415c: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int attach(struct i2c_adapter *adapter) |
| 89 | { |
| 90 | /* let's see whether this is a know adapter we can attach to */ |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 91 | if (adapter->id != I2C_HW_SAA7146) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id); |
| 93 | return -ENODEV; |
| 94 | } |
| 95 | |
| 96 | return i2c_probe(adapter, &addr_data, &detect); |
| 97 | } |
| 98 | |
| 99 | static int detach(struct i2c_client *client) |
| 100 | { |
| 101 | int ret = i2c_detach_client(client); |
| 102 | kfree(client); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /* makes a connection between the input-pin 'i' and the output-pin 'o' |
| 107 | for the tea6415c-client 'client' */ |
| 108 | static int switch_matrix(struct i2c_client *client, int i, int o) |
| 109 | { |
| 110 | u8 byte = 0; |
| 111 | int ret; |
Mauro Carvalho Chehab | a8733ca | 2006-03-17 10:37:02 -0300 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | dprintk("adr:0x%02x, i:%d, o:%d\n", client->addr, i, o); |
| 114 | |
| 115 | /* check if the pins are valid */ |
| 116 | if (0 == ((1 == i || 3 == i || 5 == i || 6 == i || 8 == i || 10 == i || 20 == i || 11 == i) |
| 117 | && (18 == o || 17 == o || 16 == o || 15 == o || 14 == o || 13 == o))) |
| 118 | return -1; |
| 119 | |
| 120 | /* to understand this, have a look at the tea6415c-specs (p.5) */ |
| 121 | switch (o) { |
| 122 | case 18: |
| 123 | byte = 0x00; |
| 124 | break; |
| 125 | case 14: |
| 126 | byte = 0x20; |
| 127 | break; |
| 128 | case 16: |
| 129 | byte = 0x10; |
| 130 | break; |
| 131 | case 17: |
| 132 | byte = 0x08; |
| 133 | break; |
| 134 | case 15: |
| 135 | byte = 0x18; |
| 136 | break; |
| 137 | case 13: |
| 138 | byte = 0x28; |
| 139 | break; |
| 140 | }; |
| 141 | |
| 142 | switch (i) { |
| 143 | case 5: |
| 144 | byte |= 0x00; |
| 145 | break; |
| 146 | case 8: |
| 147 | byte |= 0x04; |
| 148 | break; |
| 149 | case 3: |
| 150 | byte |= 0x02; |
| 151 | break; |
| 152 | case 20: |
| 153 | byte |= 0x06; |
| 154 | break; |
| 155 | case 6: |
| 156 | byte |= 0x01; |
| 157 | break; |
| 158 | case 10: |
| 159 | byte |= 0x05; |
| 160 | break; |
| 161 | case 1: |
| 162 | byte |= 0x03; |
| 163 | break; |
| 164 | case 11: |
| 165 | byte |= 0x07; |
| 166 | break; |
| 167 | }; |
| 168 | |
| 169 | ret = i2c_smbus_write_byte(client, byte); |
| 170 | if (ret) { |
| 171 | dprintk("i2c_smbus_write_byte() failed, ret:%d\n", ret); |
| 172 | return -EIO; |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static int command(struct i2c_client *client, unsigned int cmd, void *arg) |
| 179 | { |
| 180 | struct tea6415c_multiplex *v = (struct tea6415c_multiplex *)arg; |
| 181 | int result = 0; |
| 182 | |
| 183 | switch (cmd) { |
| 184 | case TEA6415C_SWITCH: |
| 185 | result = switch_matrix(client, v->in, v->out); |
| 186 | break; |
| 187 | default: |
| 188 | return -ENOIOCTLCMD; |
| 189 | } |
| 190 | |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | static struct i2c_driver driver = { |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 195 | .driver = { |
Mauro Carvalho Chehab | a8733ca | 2006-03-17 10:37:02 -0300 | [diff] [blame] | 196 | .name = "tea6415c", |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 197 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | .id = I2C_DRIVERID_TEA6415C, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | .attach_adapter = attach, |
| 200 | .detach_client = detach, |
| 201 | .command = command, |
| 202 | }; |
| 203 | |
| 204 | static struct i2c_client client_template = { |
Jean Delvare | fae91e7 | 2005-08-15 19:57:04 +0200 | [diff] [blame] | 205 | .name = "tea6415c", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | .driver = &driver, |
| 207 | }; |
| 208 | |
| 209 | static int __init this_module_init(void) |
| 210 | { |
| 211 | return i2c_add_driver(&driver); |
| 212 | } |
| 213 | |
| 214 | static void __exit this_module_exit(void) |
| 215 | { |
| 216 | i2c_del_driver(&driver); |
| 217 | } |
| 218 | |
| 219 | module_init(this_module_init); |
| 220 | module_exit(this_module_exit); |
| 221 | |
| 222 | MODULE_AUTHOR("Michael Hunold <michael@mihu.de>"); |
| 223 | MODULE_DESCRIPTION("tea6415c driver"); |
| 224 | MODULE_LICENSE("GPL"); |