Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------- */ |
| 2 | /* i2c-algo-sibyte.c i2c driver algorithms for bit-shift adapters */ |
| 3 | /* ------------------------------------------------------------------------- */ |
| 4 | /* Copyright (C) 2001,2002,2003 Broadcom Corporation |
| 5 | Copyright (C) 1995-2000 Simon G. Vogl |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 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 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even |
| 23 | Frodo Looijaard <frodol@dds.nl>. */ |
| 24 | |
| 25 | /* Ported for SiByte SOCs by Broadcom Corporation. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/init.h> |
| 30 | |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/sibyte/sb1250_regs.h> |
| 33 | #include <asm/sibyte/sb1250_smbus.h> |
| 34 | |
| 35 | #include <linux/i2c.h> |
| 36 | #include <linux/i2c-algo-sibyte.h> |
| 37 | |
| 38 | /* ----- global defines ----------------------------------------------- */ |
| 39 | #define SMB_CSR(a,r) ((long)(a->reg_base + r)) |
| 40 | |
| 41 | /* ----- global variables --------------------------------------------- */ |
| 42 | |
| 43 | /* module parameters: |
| 44 | */ |
| 45 | static int bit_scan=0; /* have a look at what's hanging 'round */ |
| 46 | |
| 47 | |
| 48 | static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr, |
| 49 | unsigned short flags, char read_write, |
| 50 | u8 command, int size, union i2c_smbus_data * data) |
| 51 | { |
| 52 | struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data; |
| 53 | int data_bytes = 0; |
| 54 | int error; |
| 55 | |
| 56 | while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY) |
| 57 | ; |
| 58 | |
| 59 | switch (size) { |
| 60 | case I2C_SMBUS_QUICK: |
| 61 | csr_out32((V_SMB_ADDR(addr) | (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) | |
| 62 | V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START)); |
| 63 | break; |
| 64 | case I2C_SMBUS_BYTE: |
| 65 | if (read_write == I2C_SMBUS_READ) { |
| 66 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE), |
| 67 | SMB_CSR(adap, R_SMB_START)); |
| 68 | data_bytes = 1; |
| 69 | } else { |
| 70 | csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD)); |
| 71 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE), |
| 72 | SMB_CSR(adap, R_SMB_START)); |
| 73 | } |
| 74 | break; |
| 75 | case I2C_SMBUS_BYTE_DATA: |
| 76 | csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD)); |
| 77 | if (read_write == I2C_SMBUS_READ) { |
| 78 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE), |
| 79 | SMB_CSR(adap, R_SMB_START)); |
| 80 | data_bytes = 1; |
| 81 | } else { |
| 82 | csr_out32(V_SMB_LB(data->byte), SMB_CSR(adap, R_SMB_DATA)); |
| 83 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE), |
| 84 | SMB_CSR(adap, R_SMB_START)); |
| 85 | } |
| 86 | break; |
| 87 | case I2C_SMBUS_WORD_DATA: |
| 88 | csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD)); |
| 89 | if (read_write == I2C_SMBUS_READ) { |
| 90 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE), |
| 91 | SMB_CSR(adap, R_SMB_START)); |
| 92 | data_bytes = 2; |
| 93 | } else { |
| 94 | csr_out32(V_SMB_LB(data->word & 0xff), SMB_CSR(adap, R_SMB_DATA)); |
| 95 | csr_out32(V_SMB_MB(data->word >> 8), SMB_CSR(adap, R_SMB_DATA)); |
| 96 | csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE), |
| 97 | SMB_CSR(adap, R_SMB_START)); |
| 98 | } |
| 99 | break; |
| 100 | default: |
| 101 | return -1; /* XXXKW better error code? */ |
| 102 | } |
| 103 | |
| 104 | while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY) |
| 105 | ; |
| 106 | |
| 107 | error = csr_in32(SMB_CSR(adap, R_SMB_STATUS)); |
| 108 | if (error & M_SMB_ERROR) { |
| 109 | /* Clear error bit by writing a 1 */ |
| 110 | csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS)); |
| 111 | return -1; /* XXXKW better error code? */ |
| 112 | } |
| 113 | |
| 114 | if (data_bytes == 1) |
| 115 | data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff; |
| 116 | if (data_bytes == 2) |
| 117 | data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int algo_control(struct i2c_adapter *adapter, |
| 123 | unsigned int cmd, unsigned long arg) |
| 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static u32 bit_func(struct i2c_adapter *adap) |
| 129 | { |
| 130 | return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 131 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* -----exported algorithm data: ------------------------------------- */ |
| 136 | |
| 137 | static struct i2c_algorithm i2c_sibyte_algo = { |
| 138 | .name = "SiByte algorithm", |
| 139 | .id = I2C_ALGO_SIBYTE, |
| 140 | .smbus_xfer = smbus_xfer, |
| 141 | .algo_control = algo_control, /* ioctl */ |
| 142 | .functionality = bit_func, |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * registering functions to load algorithms at runtime |
| 147 | */ |
| 148 | int i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed) |
| 149 | { |
| 150 | int i; |
| 151 | struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data; |
| 152 | |
| 153 | /* register new adapter to i2c module... */ |
| 154 | |
| 155 | i2c_adap->id |= i2c_sibyte_algo.id; |
| 156 | i2c_adap->algo = &i2c_sibyte_algo; |
| 157 | |
| 158 | /* Set the frequency to 100 kHz */ |
| 159 | csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ)); |
| 160 | csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL)); |
| 161 | |
| 162 | /* scan bus */ |
| 163 | if (bit_scan) { |
| 164 | union i2c_smbus_data data; |
| 165 | int rc; |
| 166 | printk(KERN_INFO " i2c-algo-sibyte.o: scanning bus %s.\n", |
| 167 | i2c_adap->name); |
| 168 | for (i = 0x00; i < 0x7f; i++) { |
| 169 | /* XXXKW is this a realistic probe? */ |
| 170 | rc = smbus_xfer(i2c_adap, i, 0, I2C_SMBUS_READ, 0, |
| 171 | I2C_SMBUS_BYTE_DATA, &data); |
| 172 | if (!rc) { |
| 173 | printk("(%02x)",i); |
| 174 | } else |
| 175 | printk("."); |
| 176 | } |
| 177 | printk("\n"); |
| 178 | } |
| 179 | |
| 180 | i2c_add_adapter(i2c_adap); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | int i2c_sibyte_del_bus(struct i2c_adapter *adap) |
| 187 | { |
| 188 | int res; |
| 189 | |
| 190 | if ((res = i2c_del_adapter(adap)) < 0) |
| 191 | return res; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int __init i2c_algo_sibyte_init (void) |
| 197 | { |
| 198 | printk("i2c-algo-sibyte.o: i2c SiByte algorithm module\n"); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | EXPORT_SYMBOL(i2c_sibyte_add_bus); |
| 204 | EXPORT_SYMBOL(i2c_sibyte_del_bus); |
| 205 | |
| 206 | #ifdef MODULE |
| 207 | MODULE_AUTHOR("Kip Walker, Broadcom Corp."); |
| 208 | MODULE_DESCRIPTION("SiByte I2C-Bus algorithm"); |
| 209 | MODULE_PARM(bit_scan, "i"); |
| 210 | MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus"); |
| 211 | MODULE_LICENSE("GPL"); |
| 212 | |
| 213 | int init_module(void) |
| 214 | { |
| 215 | return i2c_algo_sibyte_init(); |
| 216 | } |
| 217 | |
| 218 | void cleanup_module(void) |
| 219 | { |
| 220 | } |
| 221 | #endif |