Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005-2006 Micronas USA Inc.
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/delay.h>
0008 #include <linux/sched.h>
0009 #include <linux/list.h>
0010 #include <linux/unistd.h>
0011 #include <linux/time.h>
0012 #include <linux/device.h>
0013 #include <linux/i2c.h>
0014 #include <linux/mutex.h>
0015 #include <linux/uaccess.h>
0016 
0017 #include "go7007-priv.h"
0018 
0019 /********************* Driver for on-board I2C adapter *********************/
0020 
0021 /* #define GO7007_I2C_DEBUG */
0022 
0023 #define SPI_I2C_ADDR_BASE       0x1400
0024 #define STATUS_REG_ADDR         (SPI_I2C_ADDR_BASE + 0x2)
0025 #define I2C_CTRL_REG_ADDR       (SPI_I2C_ADDR_BASE + 0x6)
0026 #define I2C_DEV_UP_ADDR_REG_ADDR    (SPI_I2C_ADDR_BASE + 0x7)
0027 #define I2C_LO_ADDR_REG_ADDR        (SPI_I2C_ADDR_BASE + 0x8)
0028 #define I2C_DATA_REG_ADDR       (SPI_I2C_ADDR_BASE + 0x9)
0029 #define I2C_CLKFREQ_REG_ADDR        (SPI_I2C_ADDR_BASE + 0xa)
0030 
0031 #define I2C_STATE_MASK          0x0007
0032 #define I2C_READ_READY_MASK     0x0008
0033 
0034 /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
0035  * on the Adlink PCI-MPG24, so access is shared between all of them. */
0036 static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
0037 
0038 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
0039         u16 command, int flags, u8 *data)
0040 {
0041     int i, ret = -EIO;
0042     u16 val;
0043 
0044     if (go->status == STATUS_SHUTDOWN)
0045         return -ENODEV;
0046 
0047 #ifdef GO7007_I2C_DEBUG
0048     if (read)
0049         dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
0050             command, addr);
0051     else
0052         dev_dbg(go->dev,
0053             "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
0054             *data, command, addr);
0055 #endif
0056 
0057     mutex_lock(&go->hw_lock);
0058 
0059     if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
0060         /* Bridge the I2C port on this GO7007 to the shared bus */
0061         mutex_lock(&adlink_mpg24_i2c_lock);
0062         go7007_write_addr(go, 0x3c82, 0x0020);
0063     }
0064 
0065     /* Wait for I2C adapter to be ready */
0066     for (i = 0; i < 10; ++i) {
0067         if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
0068             goto i2c_done;
0069         if (!(val & I2C_STATE_MASK))
0070             break;
0071         msleep(100);
0072     }
0073     if (i == 10) {
0074         dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
0075         goto i2c_done;
0076     }
0077 
0078     /* Set target register (command) */
0079     go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
0080     go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
0081 
0082     /* If we're writing, send the data and target address and we're done */
0083     if (!read) {
0084         go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
0085         go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
0086                     (addr << 9) | (command >> 8));
0087         ret = 0;
0088         goto i2c_done;
0089     }
0090 
0091     /* Otherwise, we're reading.  First clear i2c_rx_data_rdy. */
0092     if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
0093         goto i2c_done;
0094 
0095     /* Send the target address plus read flag */
0096     go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
0097             (addr << 9) | 0x0100 | (command >> 8));
0098 
0099     /* Wait for i2c_rx_data_rdy */
0100     for (i = 0; i < 10; ++i) {
0101         if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
0102             goto i2c_done;
0103         if (val & I2C_READ_READY_MASK)
0104             break;
0105         msleep(100);
0106     }
0107     if (i == 10) {
0108         dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
0109         goto i2c_done;
0110     }
0111 
0112     /* Retrieve the read byte */
0113     if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
0114         goto i2c_done;
0115     *data = val;
0116     ret = 0;
0117 
0118 i2c_done:
0119     if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
0120         /* Isolate the I2C port on this GO7007 from the shared bus */
0121         go7007_write_addr(go, 0x3c82, 0x0000);
0122         mutex_unlock(&adlink_mpg24_i2c_lock);
0123     }
0124     mutex_unlock(&go->hw_lock);
0125     return ret;
0126 }
0127 
0128 static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
0129         unsigned short flags, char read_write,
0130         u8 command, int size, union i2c_smbus_data *data)
0131 {
0132     struct go7007 *go = i2c_get_adapdata(adapter);
0133 
0134     if (size != I2C_SMBUS_BYTE_DATA)
0135         return -EIO;
0136     return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
0137             flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
0138 }
0139 
0140 /* VERY LIMITED I2C master xfer function -- only needed because the
0141  * SMBus functions only support 8-bit commands and the SAA7135 uses
0142  * 16-bit commands.  The I2C interface on the GO7007, as limited as
0143  * it is, does support this mode. */
0144 
0145 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
0146                     struct i2c_msg msgs[], int num)
0147 {
0148     struct go7007 *go = i2c_get_adapdata(adapter);
0149     int i;
0150 
0151     for (i = 0; i < num; ++i) {
0152         /* We can only do two things here -- write three bytes, or
0153          * write two bytes and read one byte. */
0154         if (msgs[i].len == 2) {
0155             if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
0156                     (msgs[i].flags & I2C_M_RD) ||
0157                     !(msgs[i + 1].flags & I2C_M_RD) ||
0158                     msgs[i + 1].len != 1)
0159                 return -EIO;
0160             if (go7007_i2c_xfer(go, msgs[i].addr, 1,
0161                     (msgs[i].buf[0] << 8) | msgs[i].buf[1],
0162                     0x01, &msgs[i + 1].buf[0]) < 0)
0163                 return -EIO;
0164             ++i;
0165         } else if (msgs[i].len == 3) {
0166             if (msgs[i].flags & I2C_M_RD)
0167                 return -EIO;
0168             if (msgs[i].len != 3)
0169                 return -EIO;
0170             if (go7007_i2c_xfer(go, msgs[i].addr, 0,
0171                     (msgs[i].buf[0] << 8) | msgs[i].buf[1],
0172                     0x01, &msgs[i].buf[2]) < 0)
0173                 return -EIO;
0174         } else
0175             return -EIO;
0176     }
0177 
0178     return num;
0179 }
0180 
0181 static u32 go7007_functionality(struct i2c_adapter *adapter)
0182 {
0183     return I2C_FUNC_SMBUS_BYTE_DATA;
0184 }
0185 
0186 static const struct i2c_algorithm go7007_algo = {
0187     .smbus_xfer = go7007_smbus_xfer,
0188     .master_xfer    = go7007_i2c_master_xfer,
0189     .functionality  = go7007_functionality,
0190 };
0191 
0192 static struct i2c_adapter go7007_adap_templ = {
0193     .owner          = THIS_MODULE,
0194     .name           = "WIS GO7007SB",
0195     .algo           = &go7007_algo,
0196 };
0197 
0198 int go7007_i2c_init(struct go7007 *go)
0199 {
0200     memcpy(&go->i2c_adapter, &go7007_adap_templ,
0201             sizeof(go7007_adap_templ));
0202     go->i2c_adapter.dev.parent = go->dev;
0203     i2c_set_adapdata(&go->i2c_adapter, go);
0204     if (i2c_add_adapter(&go->i2c_adapter) < 0) {
0205         dev_err(go->dev,
0206             "go7007-i2c: error: i2c_add_adapter failed\n");
0207         return -1;
0208     }
0209     return 0;
0210 }