blob: 4f7e3b42263fcc6644294bbcb9b2d47a382c2077 [file] [log] [blame]
Steven Toth443c12282009-05-09 21:17:28 -03001/*
2 * Driver for the NXP SAA7164 PCIe bridge
3 *
Steven Toth9b8b0192010-07-31 14:39:44 -03004 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
Steven Toth443c12282009-05-09 21:17:28 -03005 *
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>
Steven Tothbc250682010-11-12 18:32:36 -030026#include <linux/io.h>
Steven Toth443c12282009-05-09 21:17:28 -030027
28#include "saa7164.h"
29
30static 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
Steven Tothbc250682010-11-12 18:32:36 -030068err:
Steven Toth443c12282009-05-09 21:17:28 -030069 return retval;
70}
71
Steven Toth443c12282009-05-09 21:17:28 -030072static u32 saa7164_functionality(struct i2c_adapter *adap)
73{
74 return I2C_FUNC_I2C;
75}
76
77static struct i2c_algorithm saa7164_i2c_algo_template = {
78 .master_xfer = i2c_xfer,
79 .functionality = saa7164_functionality,
80};
81
82/* ----------------------------------------------------------------------- */
83
84static struct i2c_adapter saa7164_i2c_adap_template = {
85 .name = "saa7164",
86 .owner = THIS_MODULE,
Steven Toth443c12282009-05-09 21:17:28 -030087 .algo = &saa7164_i2c_algo_template,
Steven Toth443c12282009-05-09 21:17:28 -030088};
89
90static struct i2c_client saa7164_i2c_client_template = {
91 .name = "saa7164 internal",
92};
93
94int saa7164_i2c_register(struct saa7164_i2c *bus)
95{
96 struct saa7164_dev *dev = bus->dev;
97
98 dprintk(DBGLVL_I2C, "%s(bus = %d)\n", __func__, bus->nr);
99
Ezequiel GarcĂ­addebe8c2012-06-27 12:52:51 -0300100 bus->i2c_adap = saa7164_i2c_adap_template;
101 bus->i2c_client = saa7164_i2c_client_template;
Steven Toth443c12282009-05-09 21:17:28 -0300102
103 bus->i2c_adap.dev.parent = &dev->pci->dev;
104
105 strlcpy(bus->i2c_adap.name, bus->dev->name,
106 sizeof(bus->i2c_adap.name));
107
Steven Toth443c12282009-05-09 21:17:28 -0300108 bus->i2c_adap.algo_data = bus;
109 i2c_set_adapdata(&bus->i2c_adap, bus);
110 i2c_add_adapter(&bus->i2c_adap);
111
112 bus->i2c_client.adapter = &bus->i2c_adap;
113
Steven Tothb8298e72009-09-17 21:00:40 -0300114 if (0 != bus->i2c_rc)
Steven Toth443c12282009-05-09 21:17:28 -0300115 printk(KERN_ERR "%s: i2c bus %d register FAILED\n",
116 dev->name, bus->nr);
117
118 return bus->i2c_rc;
119}
120
121int saa7164_i2c_unregister(struct saa7164_i2c *bus)
122{
123 i2c_del_adapter(&bus->i2c_adap);
124 return 0;
125}