diff options
Diffstat (limited to 'drivers/media/video/saa7164/saa7164-i2c.c')
-rw-r--r-- | drivers/media/video/saa7164/saa7164-i2c.c | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/drivers/media/video/saa7164/saa7164-i2c.c b/drivers/media/video/saa7164/saa7164-i2c.c new file mode 100644 index 000000000000..4c431b4ac8db --- /dev/null +++ b/drivers/media/video/saa7164/saa7164-i2c.c | |||
@@ -0,0 +1,170 @@ | |||
1 | /* | ||
2 | * Driver for the NXP SAA7164 PCIe bridge | ||
3 | * | ||
4 | * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
20 | */ | ||
21 | |||
22 | #include <linux/module.h> | ||
23 | #include <linux/moduleparam.h> | ||
24 | #include <linux/init.h> | ||
25 | #include <linux/delay.h> | ||
26 | #include <asm/io.h> | ||
27 | |||
28 | #include "saa7164.h" | ||
29 | |||
30 | static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num) | ||
31 | { | ||
32 | struct saa7164_i2c *bus = i2c_adap->algo_data; | ||
33 | struct saa7164_dev *dev = bus->dev; | ||
34 | int i, retval = 0; | ||
35 | |||
36 | dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num); | ||
37 | |||
38 | for (i = 0 ; i < num; i++) { | ||
39 | dprintk(DBGLVL_I2C, "%s(num = %d) addr = 0x%02x len = 0x%x\n", | ||
40 | __func__, num, msgs[i].addr, msgs[i].len); | ||
41 | if (msgs[i].flags & I2C_M_RD) { | ||
42 | /* Unsupported - Yet*/ | ||
43 | printk(KERN_ERR "%s() Unsupported - Yet\n", __func__); | ||
44 | continue; | ||
45 | } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && | ||
46 | msgs[i].addr == msgs[i + 1].addr) { | ||
47 | /* write then read from same address */ | ||
48 | |||
49 | retval = saa7164_api_i2c_read(bus, msgs[i].addr, | ||
50 | msgs[i].len, msgs[i].buf, | ||
51 | msgs[i+1].len, msgs[i+1].buf | ||
52 | ); | ||
53 | |||
54 | i++; | ||
55 | |||
56 | if (retval < 0) | ||
57 | goto err; | ||
58 | } else { | ||
59 | /* write */ | ||
60 | retval = saa7164_api_i2c_write(bus, msgs[i].addr, | ||
61 | msgs[i].len, msgs[i].buf); | ||
62 | } | ||
63 | if (retval < 0) | ||
64 | goto err; | ||
65 | } | ||
66 | return num; | ||
67 | |||
68 | err: | ||
69 | return retval; | ||
70 | } | ||
71 | |||
72 | static int attach_inform(struct i2c_client *client) | ||
73 | { | ||
74 | struct saa7164_i2c *bus = i2c_get_adapdata(client->adapter); | ||
75 | struct saa7164_dev *dev = bus->dev; | ||
76 | |||
77 | dprintk(DBGLVL_I2C, "%s i2c attach [addr=0x%x,client=%s]\n", | ||
78 | client->driver->driver.name, client->addr, client->name); | ||
79 | |||
80 | if (!client->driver->command) | ||
81 | return 0; | ||
82 | |||
83 | return 0; | ||
84 | } | ||
85 | |||
86 | static int detach_inform(struct i2c_client *client) | ||
87 | { | ||
88 | struct saa7164_dev *dev = i2c_get_adapdata(client->adapter); | ||
89 | |||
90 | dprintk(DBGLVL_I2C, "i2c detach [client=%s]\n", client->name); | ||
91 | |||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | void saa7164_call_i2c_clients(struct saa7164_i2c *bus, unsigned int cmd, | ||
96 | void *arg) | ||
97 | { | ||
98 | if (bus->i2c_rc != 0) | ||
99 | return; | ||
100 | |||
101 | i2c_clients_command(&bus->i2c_adap, cmd, arg); | ||
102 | } | ||
103 | |||
104 | static u32 saa7164_functionality(struct i2c_adapter *adap) | ||
105 | { | ||
106 | return I2C_FUNC_I2C; | ||
107 | } | ||
108 | |||
109 | static struct i2c_algorithm saa7164_i2c_algo_template = { | ||
110 | .master_xfer = i2c_xfer, | ||
111 | .functionality = saa7164_functionality, | ||
112 | }; | ||
113 | |||
114 | /* ----------------------------------------------------------------------- */ | ||
115 | |||
116 | static struct i2c_adapter saa7164_i2c_adap_template = { | ||
117 | .name = "saa7164", | ||
118 | .owner = THIS_MODULE, | ||
119 | .id = I2C_HW_B_SAA7164, | ||
120 | .algo = &saa7164_i2c_algo_template, | ||
121 | .client_register = attach_inform, | ||
122 | .client_unregister = detach_inform, | ||
123 | }; | ||
124 | |||
125 | static struct i2c_client saa7164_i2c_client_template = { | ||
126 | .name = "saa7164 internal", | ||
127 | }; | ||
128 | |||
129 | int saa7164_i2c_register(struct saa7164_i2c *bus) | ||
130 | { | ||
131 | struct saa7164_dev *dev = bus->dev; | ||
132 | |||
133 | dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr); | ||
134 | |||
135 | memcpy(&bus->i2c_adap, &saa7164_i2c_adap_template, | ||
136 | sizeof(bus->i2c_adap)); | ||
137 | |||
138 | memcpy(&bus->i2c_algo, &saa7164_i2c_algo_template, | ||
139 | sizeof(bus->i2c_algo)); | ||
140 | |||
141 | memcpy(&bus->i2c_client, &saa7164_i2c_client_template, | ||
142 | sizeof(bus->i2c_client)); | ||
143 | |||
144 | bus->i2c_adap.dev.parent = &dev->pci->dev; | ||
145 | |||
146 | strlcpy(bus->i2c_adap.name, bus->dev->name, | ||
147 | sizeof(bus->i2c_adap.name)); | ||
148 | |||
149 | bus->i2c_algo.data = bus; | ||
150 | bus->i2c_adap.algo_data = bus; | ||
151 | i2c_set_adapdata(&bus->i2c_adap, bus); | ||
152 | i2c_add_adapter(&bus->i2c_adap); | ||
153 | |||
154 | bus->i2c_client.adapter = &bus->i2c_adap; | ||
155 | |||
156 | if (0 == bus->i2c_rc) { | ||
157 | printk(KERN_ERR "%s: i2c bus %d registered\n", | ||
158 | dev->name, bus->nr); | ||
159 | } else | ||
160 | printk(KERN_ERR "%s: i2c bus %d register FAILED\n", | ||
161 | dev->name, bus->nr); | ||
162 | |||
163 | return bus->i2c_rc; | ||
164 | } | ||
165 | |||
166 | int saa7164_i2c_unregister(struct saa7164_i2c *bus) | ||
167 | { | ||
168 | i2c_del_adapter(&bus->i2c_adap); | ||
169 | return 0; | ||
170 | } | ||