Merge tag 'csky-for-linus-4.20-fixup-dtb' of https://github.com/c-sky/csky-linux
[linux-block.git] / drivers / media / pci / saa7164 / saa7164-i2c.c
CommitLineData
443c1228
ST
1/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
63a412ec 4 * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
443c1228
ST
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.
443c1228
ST
16 */
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
21#include <linux/delay.h>
bc250684 22#include <linux/io.h>
443c1228
ST
23
24#include "saa7164.h"
25
26static int i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
27{
28 struct saa7164_i2c *bus = i2c_adap->algo_data;
29 struct saa7164_dev *dev = bus->dev;
30 int i, retval = 0;
31
32 dprintk(DBGLVL_I2C, "%s(num = %d)\n", __func__, num);
33
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) {
5f954b5b
ST
38 retval = saa7164_api_i2c_read(bus,
39 msgs[i].addr,
40 0 /* reglen */,
7071b2ea 41 NULL /* reg */, msgs[i].len, msgs[i].buf);
443c1228
ST
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 */
45
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
49 );
50
51 i++;
52
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);
59 }
60 if (retval < 0)
61 goto err;
62 }
63 return num;
64
bc250684 65err:
443c1228
ST
66 return retval;
67}
68
443c1228
ST
69static u32 saa7164_functionality(struct i2c_adapter *adap)
70{
71 return I2C_FUNC_I2C;
72}
73
78f2c50b 74static const struct i2c_algorithm saa7164_i2c_algo_template = {
443c1228
ST
75 .master_xfer = i2c_xfer,
76 .functionality = saa7164_functionality,
77};
78
79/* ----------------------------------------------------------------------- */
80
e5fffebe 81static const struct i2c_adapter saa7164_i2c_adap_template = {
443c1228
ST
82 .name = "saa7164",
83 .owner = THIS_MODULE,
443c1228 84 .algo = &saa7164_i2c_algo_template,
443c1228
ST
85};
86
b8e9b36d 87static const struct i2c_client saa7164_i2c_client_template = {
443c1228
ST
88 .name = "saa7164 internal",
89};
90
91int saa7164_i2c_register(struct saa7164_i2c *bus)
92{
93 struct saa7164_dev *dev = bus->dev;
94
95 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
96
ddebe8cd
EG
97 bus->i2c_adap = saa7164_i2c_adap_template;
98 bus->i2c_client = saa7164_i2c_client_template;
443c1228
ST
99
100 bus->i2c_adap.dev.parent = &dev->pci->dev;
101
c0decac1 102 strscpy(bus->i2c_adap.name, bus->dev->name,
443c1228
ST
103 sizeof(bus->i2c_adap.name));
104
443c1228
ST
105 bus->i2c_adap.algo_data = bus;
106 i2c_set_adapdata(&bus->i2c_adap, bus);
107 i2c_add_adapter(&bus->i2c_adap);
108
109 bus->i2c_client.adapter = &bus->i2c_adap;
110
b8298e7e 111 if (0 != bus->i2c_rc)
443c1228
ST
112 printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
113 dev->name, bus->nr);
114
115 return bus->i2c_rc;
116}
117
118int saa7164_i2c_unregister(struct saa7164_i2c *bus)
119{
120 i2c_del_adapter(&bus->i2c_adap);
121 return 0;
122}