[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / media / video / saa7164 / saa7164-i2c.c
blobe1ae9b01bf0f9ee5d2278b3cd159a356ff11dadc
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2009 Steven Toth <stoth@kernellabs.com>
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.
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
15 * GNU General Public License for more details.
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.
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>
28 #include "saa7164.h"
30 static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
32 struct saa7164_i2c *bus = i2c_adap->algo_data;
33 struct saa7164_dev *dev = bus->dev;
34 int i, retval = 0;
36 dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
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 */
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
54 i++;
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);
63 if (retval < 0)
64 goto err;
66 return num;
68 err:
69 return retval;
72 void saa7164_call_i2c_clients(struct saa7164_i2c *bus, unsigned int cmd,
73 void *arg)
75 if (bus->i2c_rc != 0)
76 return;
78 i2c_clients_command(&bus->i2c_adap, cmd, arg);
81 static u32 saa7164_functionality(struct i2c_adapter *adap)
83 return I2C_FUNC_I2C;
86 static struct i2c_algorithm saa7164_i2c_algo_template = {
87 .master_xfer = i2c_xfer,
88 .functionality = saa7164_functionality,
91 /* ----------------------------------------------------------------------- */
93 static struct i2c_adapter saa7164_i2c_adap_template = {
94 .name = "saa7164",
95 .owner = THIS_MODULE,
96 .algo = &saa7164_i2c_algo_template,
99 static struct i2c_client saa7164_i2c_client_template = {
100 .name = "saa7164 internal",
103 int saa7164_i2c_register(struct saa7164_i2c *bus)
105 struct saa7164_dev *dev = bus->dev;
107 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
109 memcpy(&bus->i2c_adap, &saa7164_i2c_adap_template,
110 sizeof(bus->i2c_adap));
112 memcpy(&bus->i2c_algo, &saa7164_i2c_algo_template,
113 sizeof(bus->i2c_algo));
115 memcpy(&bus->i2c_client, &saa7164_i2c_client_template,
116 sizeof(bus->i2c_client));
118 bus->i2c_adap.dev.parent = &dev->pci->dev;
120 strlcpy(bus->i2c_adap.name, bus->dev->name,
121 sizeof(bus->i2c_adap.name));
123 bus->i2c_algo.data = bus;
124 bus->i2c_adap.algo_data = bus;
125 i2c_set_adapdata(&bus->i2c_adap, bus);
126 i2c_add_adapter(&bus->i2c_adap);
128 bus->i2c_client.adapter = &bus->i2c_adap;
130 if (0 != bus->i2c_rc)
131 printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
132 dev->name, bus->nr);
134 return bus->i2c_rc;
137 int saa7164_i2c_unregister(struct saa7164_i2c *bus)
139 i2c_del_adapter(&bus->i2c_adap);
140 return 0;