1 /* 2 * Driver for the NXP SAA7164 PCIe bridge 3 * 4 * Copyright (c) 2010-2015 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 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> 23 24 #include "saa7164.h" 25 26 static 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) { 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 */ 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 65 err: 66 return retval; 67 } 68 69 static u32 saa7164_functionality(struct i2c_adapter *adap) 70 { 71 return I2C_FUNC_I2C; 72 } 73 74 static const struct i2c_algorithm saa7164_i2c_algo_template = { 75 .master_xfer = i2c_xfer, 76 .functionality = saa7164_functionality, 77 }; 78 79 /* ----------------------------------------------------------------------- */ 80 81 static struct i2c_adapter saa7164_i2c_adap_template = { 82 .name = "saa7164", 83 .owner = THIS_MODULE, 84 .algo = &saa7164_i2c_algo_template, 85 }; 86 87 static struct i2c_client saa7164_i2c_client_template = { 88 .name = "saa7164 internal", 89 }; 90 91 int 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 97 bus->i2c_adap = saa7164_i2c_adap_template; 98 bus->i2c_client = saa7164_i2c_client_template; 99 100 bus->i2c_adap.dev.parent = &dev->pci->dev; 101 102 strlcpy(bus->i2c_adap.name, bus->dev->name, 103 sizeof(bus->i2c_adap.name)); 104 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 111 if (0 != bus->i2c_rc) 112 printk(KERN_ERR "%s: i2c bus %d register FAILED\n", 113 dev->name, bus->nr); 114 115 return bus->i2c_rc; 116 } 117 118 int saa7164_i2c_unregister(struct saa7164_i2c *bus) 119 { 120 i2c_del_adapter(&bus->i2c_adap); 121 return 0; 122 } 123