Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / saa7164 / saa7164-i2c.c
blob6d13cbb9d010d556a34e44bf40d16324532b6258
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 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.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/io.h>
24 #include "saa7164.h"
26 static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
28 struct saa7164_i2c *bus = i2c_adap->algo_data;
29 struct saa7164_dev *dev = bus->dev;
30 int i, retval = 0;
32 dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
34 for (i = 0 ; i < num; i++) {
35 dprintk(DBGLVL_I2C, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
36 __func__, num, msgs[i].addr, msgs[i].len);
37 if (msgs[i].flags & I2C_M_RD) {
38 retval = saa7164_api_i2c_read(bus,
39 msgs[i].addr,
40 0 /* reglen */,
41 NULL /* reg */, msgs[i].len, msgs[i].buf);
42 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
43 msgs[i].addr == msgs[i + 1].addr) {
44 /* write then read from same address */
46 retval = saa7164_api_i2c_read(bus, msgs[i].addr,
47 msgs[i].len, msgs[i].buf,
48 msgs[i+1].len, msgs[i+1].buf
51 i++;
53 if (retval < 0)
54 goto err;
55 } else {
56 /* write */
57 retval = saa7164_api_i2c_write(bus, msgs[i].addr,
58 msgs[i].len, msgs[i].buf);
60 if (retval < 0)
61 goto err;
63 return num;
65 err:
66 return retval;
69 static u32 saa7164_functionality(struct i2c_adapter *adap)
71 return I2C_FUNC_I2C;
74 static const struct i2c_algorithm saa7164_i2c_algo_template = {
75 .master_xfer = i2c_xfer,
76 .functionality = saa7164_functionality,
79 /* ----------------------------------------------------------------------- */
81 static const struct i2c_adapter saa7164_i2c_adap_template = {
82 .name = "saa7164",
83 .owner = THIS_MODULE,
84 .algo = &saa7164_i2c_algo_template,
87 static const struct i2c_client saa7164_i2c_client_template = {
88 .name = "saa7164 internal",
91 int saa7164_i2c_register(struct saa7164_i2c *bus)
93 struct saa7164_dev *dev = bus->dev;
95 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
97 bus->i2c_adap = saa7164_i2c_adap_template;
98 bus->i2c_client = saa7164_i2c_client_template;
100 bus->i2c_adap.dev.parent = &dev->pci->dev;
102 strlcpy(bus->i2c_adap.name, bus->dev->name,
103 sizeof(bus->i2c_adap.name));
105 bus->i2c_adap.algo_data = bus;
106 i2c_set_adapdata(&bus->i2c_adap, bus);
107 i2c_add_adapter(&bus->i2c_adap);
109 bus->i2c_client.adapter = &bus->i2c_adap;
111 if (0 != bus->i2c_rc)
112 printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
113 dev->name, bus->nr);
115 return bus->i2c_rc;
118 int saa7164_i2c_unregister(struct saa7164_i2c *bus)
120 i2c_del_adapter(&bus->i2c_adap);
121 return 0;