Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 1 | /* |
| 2 | Mantis PCI bridge driver |
| 3 | |
| 4 | Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.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 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <linux/ioport.h> |
| 27 | #include <asm/pgtable.h> |
| 28 | #include <asm/page.h> |
| 29 | #include "mantis_common.h" |
| 30 | |
| 31 | #define I2C_HW_B_MANTIS 0x1c |
| 32 | |
| 33 | static int mantis_ack_wait(struct mantis_pci *mantis) |
| 34 | { |
| 35 | int rc = 0; |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 36 | u32 timeout = 0; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 37 | |
| 38 | if (wait_event_interruptible_timeout(mantis->i2c_wq, |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 39 | mantis->mantis_int_stat & MANTIS_INT_I2CDONE, |
| 40 | msecs_to_jiffies(50)) == -ERESTARTSYS) { |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 41 | |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 42 | dprintk(verbose, MANTIS_DEBUG, 1, "I2C Transfer failed, Master !I2CDONE"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 43 | rc = -EREMOTEIO; |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 44 | } |
| 45 | while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) { |
| 46 | dprintk(verbose, MANTIS_DEBUG, 1, "Waiting for Slave RACK"); |
| 47 | mantis->mantis_int_stat = mmread(MANTIS_INT_STAT); |
| 48 | msleep(5); |
| 49 | timeout++; |
| 50 | if (timeout > 500) { |
| 51 | dprintk(verbose, MANTIS_ERROR, 1, "Slave RACK Fail !"); |
| 52 | rc = -EREMOTEIO; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 53 | break; |
| 54 | } |
| 55 | } |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 56 | udelay(350); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 57 | |
| 58 | return rc; |
| 59 | } |
| 60 | |
| 61 | static int mantis_i2c_read(struct mantis_pci *mantis, const struct i2c_msg *msg) |
| 62 | { |
| 63 | u32 rxd, i; |
| 64 | |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 65 | dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <R>[ ", __func__, msg->addr); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 66 | for (i = 0; i < msg->len; i++) { |
| 67 | rxd = (msg->addr << 25) | (1 << 24) |
| 68 | | MANTIS_I2C_RATE_3 |
| 69 | | MANTIS_I2C_STOP |
| 70 | | MANTIS_I2C_PGMODE; |
| 71 | |
| 72 | if (i == (msg->len - 1)) |
| 73 | rxd &= ~MANTIS_I2C_STOP; |
| 74 | |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 75 | mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 76 | mmwrite(rxd, MANTIS_I2CDATA_CTL); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 77 | if (mantis_ack_wait(mantis) != 0) { |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 78 | dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<R>"); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 79 | return -EREMOTEIO; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 80 | } |
| 81 | rxd = mmread(MANTIS_I2CDATA_CTL); |
| 82 | msg->buf[i] = (u8)((rxd >> 8) & 0xFF); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 83 | dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 84 | } |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 85 | dprintk(verbose, MANTIS_INFO, 0, "]\n"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int mantis_i2c_write(struct mantis_pci *mantis, const struct i2c_msg *msg) |
| 91 | { |
| 92 | int i; |
| 93 | u32 txd = 0; |
| 94 | |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 95 | dprintk(verbose, MANTIS_INFO, 0, " %s: Address=[0x%02x] <W>[ ", __func__, msg->addr); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 96 | for (i = 0; i < msg->len; i++) { |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 97 | dprintk(verbose, MANTIS_INFO, 0, "%02x ", msg->buf[i]); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 98 | txd = (msg->addr << 25) | (msg->buf[i] << 8) |
| 99 | | MANTIS_I2C_RATE_3 |
| 100 | | MANTIS_I2C_STOP |
| 101 | | MANTIS_I2C_PGMODE; |
| 102 | |
| 103 | if (i == (msg->len - 1)) |
| 104 | txd &= ~MANTIS_I2C_STOP; |
| 105 | |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 106 | mmwrite(MANTIS_INT_I2CDONE, MANTIS_INT_STAT); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 107 | mmwrite(txd, MANTIS_I2CDATA_CTL); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 108 | if (mantis_ack_wait(mantis) != 0) { |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 109 | dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed<W>"); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 110 | return -EREMOTEIO; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 111 | } |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 112 | } |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 113 | dprintk(verbose, MANTIS_INFO, 0, "]\n"); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) |
| 119 | { |
| 120 | int ret = 0, i; |
| 121 | struct mantis_pci *mantis; |
| 122 | |
| 123 | mantis = i2c_get_adapdata(adapter); |
| 124 | for (i = 0; i < num; i++) { |
| 125 | if (msgs[i].flags & I2C_M_RD) |
| 126 | ret = mantis_i2c_read(mantis, &msgs[i]); |
| 127 | else |
| 128 | ret = mantis_i2c_write(mantis, &msgs[i]); |
| 129 | |
| 130 | if (ret < 0) |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | return num; |
| 135 | } |
| 136 | |
| 137 | static u32 mantis_i2c_func(struct i2c_adapter *adapter) |
| 138 | { |
| 139 | return I2C_FUNC_SMBUS_EMUL; |
| 140 | } |
| 141 | |
| 142 | static struct i2c_algorithm mantis_algo = { |
| 143 | .master_xfer = mantis_i2c_xfer, |
| 144 | .functionality = mantis_i2c_func, |
| 145 | }; |
| 146 | |
| 147 | static struct i2c_adapter mantis_i2c_adapter = { |
| 148 | .owner = THIS_MODULE, |
| 149 | .name = "Mantis I2C", |
| 150 | .id = I2C_HW_B_MANTIS, |
| 151 | .class = I2C_CLASS_TV_DIGITAL, |
| 152 | .algo = &mantis_algo, |
| 153 | }; |
| 154 | |
| 155 | int __devinit mantis_i2c_init(struct mantis_pci *mantis) |
| 156 | { |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 157 | u32 intstat, intmask; |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 158 | |
| 159 | memcpy(&mantis->adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter)); |
| 160 | i2c_set_adapdata(&mantis->adapter, mantis); |
| 161 | mantis->i2c_rc = i2c_add_adapter(&mantis->adapter); |
| 162 | if (mantis->i2c_rc < 0) |
| 163 | return mantis->i2c_rc; |
| 164 | |
| 165 | dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C .."); |
| 166 | |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 167 | intstat = mmread(MANTIS_INT_STAT); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 168 | intmask = mmread(MANTIS_INT_MASK); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 169 | mmwrite(intstat, MANTIS_INT_STAT); |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 170 | mmwrite(intmask | MANTIS_INT_I2CDONE, MANTIS_INT_MASK); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 171 | |
Manu Abraham | df0cca1 | 2009-12-02 22:07:24 -0300 | [diff] [blame^] | 172 | dprintk(verbose, MANTIS_DEBUG, 1, "[0x%08x/%08x]", intstat, intmask); |
Manu Abraham | 41e840b | 2009-12-02 21:57:10 -0300 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int __devexit mantis_i2c_exit(struct mantis_pci *mantis) |
| 178 | { |
| 179 | dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter"); |
| 180 | return i2c_del_adapter(&mantis->adapter); |
| 181 | } |