Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Mobile I2C Controller |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * |
| 6 | * Portions of the code based on out-of-tree driver i2c-sh7343.c |
| 7 | * Copyright (c) 2006 Carlos Munoz <carlos@kenati.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/err.h> |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 31 | #include <linux/pm_runtime.h> |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 32 | #include <linux/clk.h> |
| 33 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Magnus Damm | 81f8115 | 2011-04-28 13:25:36 +0900 | [diff] [blame] | 35 | #include <linux/i2c/i2c-sh_mobile.h> |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 36 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 37 | /* Transmit operation: */ |
| 38 | /* */ |
| 39 | /* 0 byte transmit */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 40 | /* BUS: S A8 ACK P(*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 41 | /* IRQ: DTE WAIT */ |
| 42 | /* ICIC: */ |
| 43 | /* ICCR: 0x94 0x90 */ |
| 44 | /* ICDR: A8 */ |
| 45 | /* */ |
| 46 | /* 1 byte transmit */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 47 | /* BUS: S A8 ACK D8(1) ACK P(*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 48 | /* IRQ: DTE WAIT WAIT */ |
| 49 | /* ICIC: -DTE */ |
| 50 | /* ICCR: 0x94 0x90 */ |
| 51 | /* ICDR: A8 D8(1) */ |
| 52 | /* */ |
| 53 | /* 2 byte transmit */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 54 | /* BUS: S A8 ACK D8(1) ACK D8(2) ACK P(*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 55 | /* IRQ: DTE WAIT WAIT WAIT */ |
| 56 | /* ICIC: -DTE */ |
| 57 | /* ICCR: 0x94 0x90 */ |
| 58 | /* ICDR: A8 D8(1) D8(2) */ |
| 59 | /* */ |
| 60 | /* 3 bytes or more, +---------+ gets repeated */ |
| 61 | /* */ |
| 62 | /* */ |
| 63 | /* Receive operation: */ |
| 64 | /* */ |
| 65 | /* 0 byte receive - not supported since slave may hold SDA low */ |
| 66 | /* */ |
| 67 | /* 1 byte receive [TX] | [RX] */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 68 | /* BUS: S A8 ACK | D8(1) ACK P(*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 69 | /* IRQ: DTE WAIT | WAIT DTE */ |
| 70 | /* ICIC: -DTE | +DTE */ |
| 71 | /* ICCR: 0x94 0x81 | 0xc0 */ |
| 72 | /* ICDR: A8 | D8(1) */ |
| 73 | /* */ |
| 74 | /* 2 byte receive [TX]| [RX] */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 75 | /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK P(*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 76 | /* IRQ: DTE WAIT | WAIT WAIT DTE */ |
| 77 | /* ICIC: -DTE | +DTE */ |
| 78 | /* ICCR: 0x94 0x81 | 0xc0 */ |
| 79 | /* ICDR: A8 | D8(1) D8(2) */ |
| 80 | /* */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 81 | /* 3 byte receive [TX] | [RX] (*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 82 | /* BUS: S A8 ACK | D8(1) ACK D8(2) ACK D8(3) ACK P */ |
| 83 | /* IRQ: DTE WAIT | WAIT WAIT WAIT DTE */ |
| 84 | /* ICIC: -DTE | +DTE */ |
| 85 | /* ICCR: 0x94 0x81 | 0xc0 */ |
| 86 | /* ICDR: A8 | D8(1) D8(2) D8(3) */ |
| 87 | /* */ |
| 88 | /* 4 bytes or more, this part is repeated +---------+ */ |
| 89 | /* */ |
| 90 | /* */ |
| 91 | /* Interrupt order and BUSY flag */ |
| 92 | /* ___ _ */ |
| 93 | /* SDA ___\___XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXAAAAAAAAA___/ */ |
| 94 | /* SCL \_/1\_/2\_/3\_/4\_/5\_/6\_/7\_/8\___/9\_____/ */ |
| 95 | /* */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 96 | /* S D7 D6 D5 D4 D3 D2 D1 D0 P(*) */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 97 | /* ___ */ |
| 98 | /* WAIT IRQ ________________________________/ \___________ */ |
| 99 | /* TACK IRQ ____________________________________/ \_______ */ |
| 100 | /* DTE IRQ __________________________________________/ \_ */ |
| 101 | /* AL IRQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ |
| 102 | /* _______________________________________________ */ |
| 103 | /* BUSY __/ \_ */ |
| 104 | /* */ |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 105 | /* (*) The STOP condition is only sent by the master at the end of the last */ |
| 106 | /* I2C message or if the I2C_M_STOP flag is set. Similarly, the BUSY bit is */ |
| 107 | /* only cleared after the STOP condition, so, between messages we have to */ |
| 108 | /* poll for the DTE bit. */ |
| 109 | /* */ |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 110 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 111 | enum sh_mobile_i2c_op { |
| 112 | OP_START = 0, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 113 | OP_TX_FIRST, |
| 114 | OP_TX, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 115 | OP_TX_STOP, |
| 116 | OP_TX_TO_RX, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 117 | OP_RX, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 118 | OP_RX_STOP, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 119 | OP_RX_STOP_DATA, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | struct sh_mobile_i2c_data { |
| 123 | struct device *dev; |
| 124 | void __iomem *reg; |
| 125 | struct i2c_adapter adap; |
Magnus Damm | 81f8115 | 2011-04-28 13:25:36 +0900 | [diff] [blame] | 126 | unsigned long bus_speed; |
Shinya Kuribayashi | ebd5ac1 | 2012-10-24 19:58:10 +0900 | [diff] [blame] | 127 | unsigned int clks_per_count; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 128 | struct clk *clk; |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 129 | u_int8_t icic; |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 130 | u_int8_t flags; |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 131 | u_int16_t iccl; |
| 132 | u_int16_t icch; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 133 | |
| 134 | spinlock_t lock; |
| 135 | wait_queue_head_t wait; |
| 136 | struct i2c_msg *msg; |
| 137 | int pos; |
| 138 | int sr; |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 139 | bool send_stop; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 142 | #define IIC_FLAG_HAS_ICIC67 (1 << 0) |
| 143 | |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 144 | #define STANDARD_MODE 100000 |
| 145 | #define FAST_MODE 400000 |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 146 | |
| 147 | /* Register offsets */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 148 | #define ICDR 0x00 |
| 149 | #define ICCR 0x04 |
| 150 | #define ICSR 0x08 |
| 151 | #define ICIC 0x0c |
| 152 | #define ICCL 0x10 |
| 153 | #define ICCH 0x14 |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 154 | |
| 155 | /* Register bits */ |
| 156 | #define ICCR_ICE 0x80 |
| 157 | #define ICCR_RACK 0x40 |
| 158 | #define ICCR_TRS 0x10 |
| 159 | #define ICCR_BBSY 0x04 |
| 160 | #define ICCR_SCP 0x01 |
| 161 | |
| 162 | #define ICSR_SCLM 0x80 |
| 163 | #define ICSR_SDAM 0x40 |
| 164 | #define SW_DONE 0x20 |
| 165 | #define ICSR_BUSY 0x10 |
| 166 | #define ICSR_AL 0x08 |
| 167 | #define ICSR_TACK 0x04 |
| 168 | #define ICSR_WAIT 0x02 |
| 169 | #define ICSR_DTE 0x01 |
| 170 | |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 171 | #define ICIC_ICCLB8 0x80 |
| 172 | #define ICIC_ICCHB8 0x40 |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 173 | #define ICIC_ALE 0x08 |
| 174 | #define ICIC_TACKE 0x04 |
| 175 | #define ICIC_WAITE 0x02 |
| 176 | #define ICIC_DTEE 0x01 |
| 177 | |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 178 | static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data) |
| 179 | { |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 180 | if (offs == ICIC) |
| 181 | data |= pd->icic; |
| 182 | |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 183 | iowrite8(data, pd->reg + offs); |
| 184 | } |
| 185 | |
| 186 | static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs) |
| 187 | { |
| 188 | return ioread8(pd->reg + offs); |
| 189 | } |
| 190 | |
| 191 | static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs, |
| 192 | unsigned char set, unsigned char clr) |
| 193 | { |
| 194 | iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr); |
| 195 | } |
| 196 | |
Wolfram Sang | ed4121e | 2014-05-02 21:15:13 +0200 | [diff] [blame] | 197 | static u32 sh_mobile_i2c_iccl(unsigned long count_khz, u32 tLOW, u32 tf) |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 198 | { |
| 199 | /* |
| 200 | * Conditional expression: |
| 201 | * ICCL >= COUNT_CLK * (tLOW + tf) |
| 202 | * |
| 203 | * SH-Mobile IIC hardware starts counting the LOW period of |
| 204 | * the SCL signal (tLOW) as soon as it pulls the SCL line. |
| 205 | * In order to meet the tLOW timing spec, we need to take into |
| 206 | * account the fall time of SCL signal (tf). Default tf value |
| 207 | * should be 0.3 us, for safety. |
| 208 | */ |
Wolfram Sang | ed4121e | 2014-05-02 21:15:13 +0200 | [diff] [blame] | 209 | return (((count_khz * (tLOW + tf)) + 5000) / 10000); |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 210 | } |
| 211 | |
Wolfram Sang | ed4121e | 2014-05-02 21:15:13 +0200 | [diff] [blame] | 212 | static u32 sh_mobile_i2c_icch(unsigned long count_khz, u32 tHIGH, u32 tf) |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 213 | { |
| 214 | /* |
| 215 | * Conditional expression: |
| 216 | * ICCH >= COUNT_CLK * (tHIGH + tf) |
| 217 | * |
| 218 | * SH-Mobile IIC hardware is aware of SCL transition period 'tr', |
| 219 | * and can ignore it. SH-Mobile IIC controller starts counting |
| 220 | * the HIGH period of the SCL signal (tHIGH) after the SCL input |
| 221 | * voltage increases at VIH. |
| 222 | * |
| 223 | * Afterward it turned out calculating ICCH using only tHIGH spec |
| 224 | * will result in violation of the tHD;STA timing spec. We need |
| 225 | * to take into account the fall time of SDA signal (tf) at START |
| 226 | * condition, in order to meet both tHIGH and tHD;STA specs. |
| 227 | */ |
Wolfram Sang | ed4121e | 2014-05-02 21:15:13 +0200 | [diff] [blame] | 228 | return (((count_khz * (tHIGH + tf)) + 5000) / 10000); |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 229 | } |
| 230 | |
Wolfram Sang | 6ed7053 | 2014-05-02 21:15:14 +0200 | [diff] [blame] | 231 | static int sh_mobile_i2c_init(struct sh_mobile_i2c_data *pd) |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 232 | { |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 233 | unsigned long i2c_clk_khz; |
| 234 | u32 tHIGH, tLOW, tf; |
Wolfram Sang | 7663ebe | 2014-05-02 21:15:15 +0200 | [diff] [blame^] | 235 | uint16_t max_val; |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 236 | |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 237 | /* Get clock rate after clock is enabled */ |
Laurent Pinchart | f887605 | 2013-10-28 23:49:23 +0100 | [diff] [blame] | 238 | clk_prepare_enable(pd->clk); |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 239 | i2c_clk_khz = clk_get_rate(pd->clk) / 1000; |
Wolfram Sang | 6ed7053 | 2014-05-02 21:15:14 +0200 | [diff] [blame] | 240 | clk_disable_unprepare(pd->clk); |
Shinya Kuribayashi | ebd5ac1 | 2012-10-24 19:58:10 +0900 | [diff] [blame] | 241 | i2c_clk_khz /= pd->clks_per_count; |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 242 | |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 243 | if (pd->bus_speed == STANDARD_MODE) { |
| 244 | tLOW = 47; /* tLOW = 4.7 us */ |
| 245 | tHIGH = 40; /* tHD;STA = tHIGH = 4.0 us */ |
| 246 | tf = 3; /* tf = 0.3 us */ |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 247 | } else if (pd->bus_speed == FAST_MODE) { |
| 248 | tLOW = 13; /* tLOW = 1.3 us */ |
| 249 | tHIGH = 6; /* tHD;STA = tHIGH = 0.6 us */ |
| 250 | tf = 3; /* tf = 0.3 us */ |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 251 | } else { |
| 252 | dev_err(pd->dev, "unrecognized bus speed %lu Hz\n", |
| 253 | pd->bus_speed); |
Wolfram Sang | 6ed7053 | 2014-05-02 21:15:14 +0200 | [diff] [blame] | 254 | return -EINVAL; |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 255 | } |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 256 | |
Wolfram Sang | ed4121e | 2014-05-02 21:15:13 +0200 | [diff] [blame] | 257 | pd->iccl = sh_mobile_i2c_iccl(i2c_clk_khz, tLOW, tf); |
Wolfram Sang | 7663ebe | 2014-05-02 21:15:15 +0200 | [diff] [blame^] | 258 | pd->icch = sh_mobile_i2c_icch(i2c_clk_khz, tHIGH, tf); |
| 259 | |
| 260 | max_val = pd->flags & IIC_FLAG_HAS_ICIC67 ? 0x1ff : 0xff; |
| 261 | if (pd->iccl > max_val || pd->icch > max_val) { |
| 262 | dev_err(pd->dev, "timing values out of range: L/H=0x%x/0x%x\n", |
| 263 | pd->iccl, pd->icch); |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 267 | /* one more bit of ICCL in ICIC */ |
Wolfram Sang | 7663ebe | 2014-05-02 21:15:15 +0200 | [diff] [blame^] | 268 | if (pd->iccl & 0x100) |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 269 | pd->icic |= ICIC_ICCLB8; |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 270 | else |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 271 | pd->icic &= ~ICIC_ICCLB8; |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 272 | |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 273 | /* one more bit of ICCH in ICIC */ |
Wolfram Sang | 7663ebe | 2014-05-02 21:15:15 +0200 | [diff] [blame^] | 274 | if (pd->icch & 0x100) |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 275 | pd->icic |= ICIC_ICCHB8; |
| 276 | else |
| 277 | pd->icic &= ~ICIC_ICCHB8; |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 278 | |
Wolfram Sang | 6ed7053 | 2014-05-02 21:15:14 +0200 | [diff] [blame] | 279 | return 0; |
Shinya Kuribayashi | 7b0e629 | 2012-10-24 19:56:51 +0900 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void activate_ch(struct sh_mobile_i2c_data *pd) |
| 283 | { |
| 284 | /* Wake up device and enable clock */ |
| 285 | pm_runtime_get_sync(pd->dev); |
Laurent Pinchart | f887605 | 2013-10-28 23:49:23 +0100 | [diff] [blame] | 286 | clk_prepare_enable(pd->clk); |
Shinya Kuribayashi | 7b0e629 | 2012-10-24 19:56:51 +0900 | [diff] [blame] | 287 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 288 | /* Enable channel and configure rx ack */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 289 | iic_set_clr(pd, ICCR, ICCR_ICE, 0); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 290 | |
| 291 | /* Mask all interrupts */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 292 | iic_wr(pd, ICIC, 0); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 293 | |
| 294 | /* Set the clock */ |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 295 | iic_wr(pd, ICCL, pd->iccl & 0xff); |
| 296 | iic_wr(pd, ICCH, pd->icch & 0xff); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void deactivate_ch(struct sh_mobile_i2c_data *pd) |
| 300 | { |
| 301 | /* Clear/disable interrupts */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 302 | iic_wr(pd, ICSR, 0); |
| 303 | iic_wr(pd, ICIC, 0); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 304 | |
| 305 | /* Disable channel */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 306 | iic_set_clr(pd, ICCR, 0, ICCR_ICE); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 307 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 308 | /* Disable clock and mark device as idle */ |
Laurent Pinchart | f887605 | 2013-10-28 23:49:23 +0100 | [diff] [blame] | 309 | clk_disable_unprepare(pd->clk); |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 310 | pm_runtime_put_sync(pd->dev); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static unsigned char i2c_op(struct sh_mobile_i2c_data *pd, |
| 314 | enum sh_mobile_i2c_op op, unsigned char data) |
| 315 | { |
| 316 | unsigned char ret = 0; |
| 317 | unsigned long flags; |
| 318 | |
| 319 | dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data); |
| 320 | |
| 321 | spin_lock_irqsave(&pd->lock, flags); |
| 322 | |
| 323 | switch (op) { |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 324 | case OP_START: /* issue start and trigger DTE interrupt */ |
Wolfram Sang | a78f6a4 | 2014-05-02 21:15:07 +0200 | [diff] [blame] | 325 | iic_wr(pd, ICCR, ICCR_ICE | ICCR_TRS | ICCR_BBSY); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 326 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 327 | case OP_TX_FIRST: /* disable DTE interrupt and write data */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 328 | iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
| 329 | iic_wr(pd, ICDR, data); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 330 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 331 | case OP_TX: /* write data */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 332 | iic_wr(pd, ICDR, data); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 333 | break; |
| 334 | case OP_TX_STOP: /* write data and issue a stop afterwards */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 335 | iic_wr(pd, ICDR, data); |
Wolfram Sang | a78f6a4 | 2014-05-02 21:15:07 +0200 | [diff] [blame] | 336 | iic_wr(pd, ICCR, pd->send_stop ? ICCR_ICE | ICCR_TRS |
| 337 | : ICCR_ICE | ICCR_TRS | ICCR_BBSY); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 338 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 339 | case OP_TX_TO_RX: /* select read mode */ |
Wolfram Sang | a78f6a4 | 2014-05-02 21:15:07 +0200 | [diff] [blame] | 340 | iic_wr(pd, ICCR, ICCR_ICE | ICCR_SCP); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 341 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 342 | case OP_RX: /* just read data */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 343 | ret = iic_rd(pd, ICDR); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 344 | break; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 345 | case OP_RX_STOP: /* enable DTE interrupt, issue stop */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 346 | iic_wr(pd, ICIC, |
| 347 | ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
Wolfram Sang | a78f6a4 | 2014-05-02 21:15:07 +0200 | [diff] [blame] | 348 | iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 349 | break; |
| 350 | case OP_RX_STOP_DATA: /* enable DTE interrupt, read data, issue stop */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 351 | iic_wr(pd, ICIC, |
| 352 | ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
| 353 | ret = iic_rd(pd, ICDR); |
Wolfram Sang | a78f6a4 | 2014-05-02 21:15:07 +0200 | [diff] [blame] | 354 | iic_wr(pd, ICCR, ICCR_ICE | ICCR_RACK); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | |
| 358 | spin_unlock_irqrestore(&pd->lock, flags); |
| 359 | |
| 360 | dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret); |
| 361 | return ret; |
| 362 | } |
| 363 | |
Guennadi Liakhovetski | 05cf936 | 2013-01-17 10:45:54 +0100 | [diff] [blame] | 364 | static bool sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd) |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 365 | { |
Guennadi Liakhovetski | 05cf936 | 2013-01-17 10:45:54 +0100 | [diff] [blame] | 366 | return pd->pos == -1; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 367 | } |
| 368 | |
Guennadi Liakhovetski | 05cf936 | 2013-01-17 10:45:54 +0100 | [diff] [blame] | 369 | static bool sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd) |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 370 | { |
Guennadi Liakhovetski | 05cf936 | 2013-01-17 10:45:54 +0100 | [diff] [blame] | 371 | return pd->pos == pd->msg->len - 1; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd, |
| 375 | unsigned char *buf) |
| 376 | { |
| 377 | switch (pd->pos) { |
| 378 | case -1: |
| 379 | *buf = (pd->msg->addr & 0x7f) << 1; |
| 380 | *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0; |
| 381 | break; |
| 382 | default: |
| 383 | *buf = pd->msg->buf[pd->pos]; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd) |
| 388 | { |
| 389 | unsigned char data; |
| 390 | |
| 391 | if (pd->pos == pd->msg->len) |
| 392 | return 1; |
| 393 | |
| 394 | sh_mobile_i2c_get_data(pd, &data); |
| 395 | |
| 396 | if (sh_mobile_i2c_is_last_byte(pd)) |
| 397 | i2c_op(pd, OP_TX_STOP, data); |
| 398 | else if (sh_mobile_i2c_is_first_byte(pd)) |
| 399 | i2c_op(pd, OP_TX_FIRST, data); |
| 400 | else |
| 401 | i2c_op(pd, OP_TX, data); |
| 402 | |
| 403 | pd->pos++; |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd) |
| 408 | { |
| 409 | unsigned char data; |
| 410 | int real_pos; |
| 411 | |
| 412 | do { |
| 413 | if (pd->pos <= -1) { |
| 414 | sh_mobile_i2c_get_data(pd, &data); |
| 415 | |
| 416 | if (sh_mobile_i2c_is_first_byte(pd)) |
| 417 | i2c_op(pd, OP_TX_FIRST, data); |
| 418 | else |
| 419 | i2c_op(pd, OP_TX, data); |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | if (pd->pos == 0) { |
| 424 | i2c_op(pd, OP_TX_TO_RX, 0); |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | real_pos = pd->pos - 2; |
| 429 | |
| 430 | if (pd->pos == pd->msg->len) { |
| 431 | if (real_pos < 0) { |
| 432 | i2c_op(pd, OP_RX_STOP, 0); |
| 433 | break; |
| 434 | } |
| 435 | data = i2c_op(pd, OP_RX_STOP_DATA, 0); |
| 436 | } else |
| 437 | data = i2c_op(pd, OP_RX, 0); |
| 438 | |
Magnus Damm | bff4056c8 | 2008-11-13 15:28:21 +0900 | [diff] [blame] | 439 | if (real_pos >= 0) |
| 440 | pd->msg->buf[real_pos] = data; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 441 | } while (0); |
| 442 | |
| 443 | pd->pos++; |
| 444 | return pd->pos == (pd->msg->len + 2); |
| 445 | } |
| 446 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 447 | static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id) |
| 448 | { |
| 449 | struct platform_device *dev = dev_id; |
| 450 | struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 451 | unsigned char sr; |
| 452 | int wakeup; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 453 | |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 454 | sr = iic_rd(pd, ICSR); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 455 | pd->sr |= sr; /* remember state */ |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 456 | |
| 457 | dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr, |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 458 | (pd->msg->flags & I2C_M_RD) ? "read" : "write", |
| 459 | pd->pos, pd->msg->len); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 460 | |
| 461 | if (sr & (ICSR_AL | ICSR_TACK)) { |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 462 | /* don't interrupt transaction - continue to issue stop */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 463 | iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK)); |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 464 | wakeup = 0; |
| 465 | } else if (pd->msg->flags & I2C_M_RD) |
| 466 | wakeup = sh_mobile_i2c_isr_rx(pd); |
| 467 | else |
| 468 | wakeup = sh_mobile_i2c_isr_tx(pd); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 469 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 470 | if (sr & ICSR_WAIT) /* TODO: add delay here to support slow acks */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 471 | iic_wr(pd, ICSR, sr & ~ICSR_WAIT); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 472 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 473 | if (wakeup) { |
| 474 | pd->sr |= SW_DONE; |
| 475 | wake_up(&pd->wait); |
| 476 | } |
| 477 | |
Shinya Kuribayashi | 29fb08c | 2012-10-24 19:58:31 +0900 | [diff] [blame] | 478 | /* defeat write posting to avoid spurious WAIT interrupts */ |
| 479 | iic_rd(pd, ICSR); |
| 480 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 481 | return IRQ_HANDLED; |
| 482 | } |
| 483 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 484 | static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg, |
| 485 | bool do_init) |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 486 | { |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 487 | if (usr_msg->len == 0 && (usr_msg->flags & I2C_M_RD)) { |
| 488 | dev_err(pd->dev, "Unsupported zero length i2c read\n"); |
Wolfram Sang | 5a72b25 | 2014-05-02 21:15:08 +0200 | [diff] [blame] | 489 | return -EOPNOTSUPP; |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 490 | } |
| 491 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 492 | if (do_init) { |
| 493 | /* Initialize channel registers */ |
| 494 | iic_set_clr(pd, ICCR, 0, ICCR_ICE); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 495 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 496 | /* Enable channel and configure rx ack */ |
| 497 | iic_set_clr(pd, ICCR, ICCR_ICE, 0); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 498 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 499 | /* Set the clock */ |
| 500 | iic_wr(pd, ICCL, pd->iccl & 0xff); |
| 501 | iic_wr(pd, ICCH, pd->icch & 0xff); |
| 502 | } |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 503 | |
| 504 | pd->msg = usr_msg; |
| 505 | pd->pos = -1; |
| 506 | pd->sr = 0; |
| 507 | |
Magnus Damm | 4eb00c9 | 2008-08-27 18:33:56 +0900 | [diff] [blame] | 508 | /* Enable all interrupts to begin with */ |
Magnus Damm | 12a55f2 | 2010-03-11 10:05:50 +0000 | [diff] [blame] | 509 | iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 513 | static int poll_dte(struct sh_mobile_i2c_data *pd) |
| 514 | { |
| 515 | int i; |
| 516 | |
| 517 | for (i = 1000; i; i--) { |
| 518 | u_int8_t val = iic_rd(pd, ICSR); |
| 519 | |
| 520 | if (val & ICSR_DTE) |
| 521 | break; |
| 522 | |
| 523 | if (val & ICSR_TACK) |
Wolfram Sang | 5a72b25 | 2014-05-02 21:15:08 +0200 | [diff] [blame] | 524 | return -ENXIO; |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 525 | |
| 526 | udelay(10); |
| 527 | } |
| 528 | |
Wolfram Sang | 5a72b25 | 2014-05-02 21:15:08 +0200 | [diff] [blame] | 529 | return i ? 0 : -ETIMEDOUT; |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Guennadi Liakhovetski | 4b38231 | 2013-01-17 10:45:56 +0100 | [diff] [blame] | 532 | static int poll_busy(struct sh_mobile_i2c_data *pd) |
| 533 | { |
| 534 | int i; |
| 535 | |
| 536 | for (i = 1000; i; i--) { |
| 537 | u_int8_t val = iic_rd(pd, ICSR); |
| 538 | |
| 539 | dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr); |
| 540 | |
| 541 | /* the interrupt handler may wake us up before the |
| 542 | * transfer is finished, so poll the hardware |
| 543 | * until we're done. |
| 544 | */ |
| 545 | if (!(val & ICSR_BUSY)) { |
| 546 | /* handle missing acknowledge and arbitration lost */ |
Wolfram Sang | 5a72b25 | 2014-05-02 21:15:08 +0200 | [diff] [blame] | 547 | val |= pd->sr; |
| 548 | if (val & ICSR_TACK) |
| 549 | return -ENXIO; |
| 550 | if (val & ICSR_AL) |
| 551 | return -EAGAIN; |
Guennadi Liakhovetski | 4b38231 | 2013-01-17 10:45:56 +0100 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | |
| 555 | udelay(10); |
| 556 | } |
| 557 | |
Wolfram Sang | 5a72b25 | 2014-05-02 21:15:08 +0200 | [diff] [blame] | 558 | return i ? 0 : -ETIMEDOUT; |
Guennadi Liakhovetski | 4b38231 | 2013-01-17 10:45:56 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 561 | static int sh_mobile_i2c_xfer(struct i2c_adapter *adapter, |
| 562 | struct i2c_msg *msgs, |
| 563 | int num) |
| 564 | { |
| 565 | struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter); |
| 566 | struct i2c_msg *msg; |
| 567 | int err = 0; |
Guennadi Liakhovetski | 4b38231 | 2013-01-17 10:45:56 +0100 | [diff] [blame] | 568 | int i, k; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 569 | |
| 570 | activate_ch(pd); |
| 571 | |
| 572 | /* Process all messages */ |
| 573 | for (i = 0; i < num; i++) { |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 574 | bool do_start = pd->send_stop || !i; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 575 | msg = &msgs[i]; |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 576 | pd->send_stop = i == num - 1 || msg->flags & I2C_M_STOP; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 577 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 578 | err = start_ch(pd, msg, do_start); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 579 | if (err) |
| 580 | break; |
| 581 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 582 | if (do_start) |
| 583 | i2c_op(pd, OP_START, 0); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 584 | |
| 585 | /* The interrupt handler takes care of the rest... */ |
| 586 | k = wait_event_timeout(pd->wait, |
| 587 | pd->sr & (ICSR_TACK | SW_DONE), |
| 588 | 5 * HZ); |
Guennadi Liakhovetski | 5687265 | 2013-01-17 10:45:55 +0100 | [diff] [blame] | 589 | if (!k) { |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 590 | dev_err(pd->dev, "Transfer request timed out\n"); |
Guennadi Liakhovetski | 5687265 | 2013-01-17 10:45:55 +0100 | [diff] [blame] | 591 | err = -ETIMEDOUT; |
| 592 | break; |
| 593 | } |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 594 | |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 595 | if (pd->send_stop) |
| 596 | err = poll_busy(pd); |
| 597 | else |
| 598 | err = poll_dte(pd); |
Guennadi Liakhovetski | 4b38231 | 2013-01-17 10:45:56 +0100 | [diff] [blame] | 599 | if (err < 0) |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 600 | break; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | deactivate_ch(pd); |
| 604 | |
| 605 | if (!err) |
| 606 | err = num; |
| 607 | return err; |
| 608 | } |
| 609 | |
| 610 | static u32 sh_mobile_i2c_func(struct i2c_adapter *adapter) |
| 611 | { |
Guennadi Liakhovetski | e789029 | 2013-01-17 10:45:57 +0100 | [diff] [blame] | 612 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static struct i2c_algorithm sh_mobile_i2c_algorithm = { |
| 616 | .functionality = sh_mobile_i2c_func, |
| 617 | .master_xfer = sh_mobile_i2c_xfer, |
| 618 | }; |
| 619 | |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 620 | static int sh_mobile_i2c_hook_irqs(struct platform_device *dev) |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 621 | { |
| 622 | struct resource *res; |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 623 | resource_size_t n; |
| 624 | int k = 0, ret; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 625 | |
| 626 | while ((res = platform_get_resource(dev, IORESOURCE_IRQ, k))) { |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 627 | for (n = res->start; n <= res->end; n++) { |
| 628 | ret = devm_request_irq(&dev->dev, n, sh_mobile_i2c_isr, |
| 629 | 0, dev_name(&dev->dev), dev); |
| 630 | if (ret) { |
| 631 | dev_err(&dev->dev, "cannot request IRQ %pa\n", &n); |
| 632 | return ret; |
Magnus Damm | 82b20d8 | 2010-08-02 07:16:37 +0000 | [diff] [blame] | 633 | } |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 634 | } |
| 635 | k++; |
| 636 | } |
| 637 | |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 638 | return k > 0 ? 0 : -ENOENT; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static int sh_mobile_i2c_probe(struct platform_device *dev) |
| 642 | { |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 643 | struct i2c_sh_mobile_platform_data *pdata = dev_get_platdata(&dev->dev); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 644 | struct sh_mobile_i2c_data *pd; |
| 645 | struct i2c_adapter *adap; |
| 646 | struct resource *res; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 647 | int ret; |
Wolfram Sang | 88c289e | 2014-05-02 21:15:09 +0200 | [diff] [blame] | 648 | u32 bus_speed; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 649 | |
Wolfram Sang | 4fd31c2 | 2014-05-02 21:15:11 +0200 | [diff] [blame] | 650 | pd = devm_kzalloc(&dev->dev, sizeof(struct sh_mobile_i2c_data), GFP_KERNEL); |
| 651 | if (!pd) |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 652 | return -ENOMEM; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 653 | |
Wolfram Sang | 4fd31c2 | 2014-05-02 21:15:11 +0200 | [diff] [blame] | 654 | pd->clk = devm_clk_get(&dev->dev, NULL); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 655 | if (IS_ERR(pd->clk)) { |
Magnus Damm | 1082d5d | 2011-04-21 22:20:39 +0900 | [diff] [blame] | 656 | dev_err(&dev->dev, "cannot get clock\n"); |
Wolfram Sang | 4fd31c2 | 2014-05-02 21:15:11 +0200 | [diff] [blame] | 657 | return PTR_ERR(pd->clk); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 660 | ret = sh_mobile_i2c_hook_irqs(dev); |
| 661 | if (ret) |
Wolfram Sang | 4fd31c2 | 2014-05-02 21:15:11 +0200 | [diff] [blame] | 662 | return ret; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 663 | |
| 664 | pd->dev = &dev->dev; |
| 665 | platform_set_drvdata(dev, pd); |
| 666 | |
| 667 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 668 | |
Wolfram Sang | 4fd31c2 | 2014-05-02 21:15:11 +0200 | [diff] [blame] | 669 | pd->reg = devm_ioremap_resource(&dev->dev, res); |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 670 | if (IS_ERR(pd->reg)) |
| 671 | return PTR_ERR(pd->reg); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 672 | |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 673 | /* Use platform data bus speed or STANDARD_MODE */ |
Wolfram Sang | 88c289e | 2014-05-02 21:15:09 +0200 | [diff] [blame] | 674 | ret = of_property_read_u32(dev->dev.of_node, "clock-frequency", &bus_speed); |
| 675 | pd->bus_speed = ret ? STANDARD_MODE : bus_speed; |
| 676 | |
Magnus Damm | 81f8115 | 2011-04-28 13:25:36 +0900 | [diff] [blame] | 677 | if (pdata && pdata->bus_speed) |
| 678 | pd->bus_speed = pdata->bus_speed; |
Shinya Kuribayashi | ebd5ac1 | 2012-10-24 19:58:10 +0900 | [diff] [blame] | 679 | pd->clks_per_count = 1; |
| 680 | if (pdata && pdata->clks_per_count) |
| 681 | pd->clks_per_count = pdata->clks_per_count; |
Magnus Damm | 81f8115 | 2011-04-28 13:25:36 +0900 | [diff] [blame] | 682 | |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 683 | /* The IIC blocks on SH-Mobile ARM processors |
| 684 | * come with two new bits in ICIC. |
| 685 | */ |
Wolfram Sang | 4fd31c2 | 2014-05-02 21:15:11 +0200 | [diff] [blame] | 686 | if (resource_size(res) > 0x17) |
Magnus Damm | 962b603 | 2010-03-11 10:06:02 +0000 | [diff] [blame] | 687 | pd->flags |= IIC_FLAG_HAS_ICIC67; |
| 688 | |
Wolfram Sang | 6ed7053 | 2014-05-02 21:15:14 +0200 | [diff] [blame] | 689 | ret = sh_mobile_i2c_init(pd); |
| 690 | if (ret) |
| 691 | return ret; |
Shinya Kuribayashi | 7b0e629 | 2012-10-24 19:56:51 +0900 | [diff] [blame] | 692 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 693 | /* Enable Runtime PM for this device. |
| 694 | * |
| 695 | * Also tell the Runtime PM core to ignore children |
| 696 | * for this device since it is valid for us to suspend |
| 697 | * this I2C master driver even though the slave devices |
| 698 | * on the I2C bus may not be suspended. |
| 699 | * |
| 700 | * The state of the I2C hardware bus is unaffected by |
| 701 | * the Runtime PM state. |
| 702 | */ |
| 703 | pm_suspend_ignore_children(&dev->dev, true); |
| 704 | pm_runtime_enable(&dev->dev); |
| 705 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 706 | /* setup the private data */ |
| 707 | adap = &pd->adap; |
| 708 | i2c_set_adapdata(adap, pd); |
| 709 | |
| 710 | adap->owner = THIS_MODULE; |
| 711 | adap->algo = &sh_mobile_i2c_algorithm; |
| 712 | adap->dev.parent = &dev->dev; |
| 713 | adap->retries = 5; |
| 714 | adap->nr = dev->id; |
Magnus Damm | ad33707 | 2012-03-30 17:44:02 +0900 | [diff] [blame] | 715 | adap->dev.of_node = dev->dev.of_node; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 716 | |
| 717 | strlcpy(adap->name, dev->name, sizeof(adap->name)); |
| 718 | |
Magnus Damm | a5616bd | 2008-10-31 20:20:55 +0900 | [diff] [blame] | 719 | spin_lock_init(&pd->lock); |
| 720 | init_waitqueue_head(&pd->wait); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 721 | |
| 722 | ret = i2c_add_numbered_adapter(adap); |
| 723 | if (ret < 0) { |
| 724 | dev_err(&dev->dev, "cannot add numbered adapter\n"); |
Wolfram Sang | 7fe8a99 | 2014-05-02 21:15:12 +0200 | [diff] [blame] | 725 | return ret; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 728 | dev_info(&dev->dev, |
Wolfram Sang | 7663ebe | 2014-05-02 21:15:15 +0200 | [diff] [blame^] | 729 | "I2C adapter %d with bus speed %lu Hz (L/H=0x%x/0x%x)\n", |
Shinya Kuribayashi | 23a6129 | 2012-10-24 19:57:27 +0900 | [diff] [blame] | 730 | adap->nr, pd->bus_speed, pd->iccl, pd->icch); |
Magnus Damm | ad33707 | 2012-03-30 17:44:02 +0900 | [diff] [blame] | 731 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 732 | return 0; |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static int sh_mobile_i2c_remove(struct platform_device *dev) |
| 736 | { |
| 737 | struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev); |
| 738 | |
| 739 | i2c_del_adapter(&pd->adap); |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 740 | pm_runtime_disable(&dev->dev); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 744 | static int sh_mobile_i2c_runtime_nop(struct device *dev) |
| 745 | { |
| 746 | /* Runtime PM callback shared between ->runtime_suspend() |
| 747 | * and ->runtime_resume(). Simply returns success. |
| 748 | * |
| 749 | * This driver re-initializes all registers after |
| 750 | * pm_runtime_get_sync() anyway so there is no need |
| 751 | * to save and restore registers here. |
| 752 | */ |
| 753 | return 0; |
| 754 | } |
| 755 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 756 | static const struct dev_pm_ops sh_mobile_i2c_dev_pm_ops = { |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 757 | .runtime_suspend = sh_mobile_i2c_runtime_nop, |
| 758 | .runtime_resume = sh_mobile_i2c_runtime_nop, |
| 759 | }; |
| 760 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 761 | static const struct of_device_id sh_mobile_i2c_dt_ids[] = { |
Magnus Damm | ad33707 | 2012-03-30 17:44:02 +0900 | [diff] [blame] | 762 | { .compatible = "renesas,rmobile-iic", }, |
| 763 | {}, |
| 764 | }; |
| 765 | MODULE_DEVICE_TABLE(of, sh_mobile_i2c_dt_ids); |
| 766 | |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 767 | static struct platform_driver sh_mobile_i2c_driver = { |
| 768 | .driver = { |
| 769 | .name = "i2c-sh_mobile", |
| 770 | .owner = THIS_MODULE, |
Magnus Damm | f1a3b99 | 2009-08-14 10:48:59 +0000 | [diff] [blame] | 771 | .pm = &sh_mobile_i2c_dev_pm_ops, |
Magnus Damm | ad33707 | 2012-03-30 17:44:02 +0900 | [diff] [blame] | 772 | .of_match_table = sh_mobile_i2c_dt_ids, |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 773 | }, |
| 774 | .probe = sh_mobile_i2c_probe, |
| 775 | .remove = sh_mobile_i2c_remove, |
| 776 | }; |
| 777 | |
| 778 | static int __init sh_mobile_i2c_adap_init(void) |
| 779 | { |
| 780 | return platform_driver_register(&sh_mobile_i2c_driver); |
| 781 | } |
| 782 | |
| 783 | static void __exit sh_mobile_i2c_adap_exit(void) |
| 784 | { |
| 785 | platform_driver_unregister(&sh_mobile_i2c_driver); |
| 786 | } |
| 787 | |
Magnus Damm | ccb3bc1 | 2009-07-22 23:58:39 +0900 | [diff] [blame] | 788 | subsys_initcall(sh_mobile_i2c_adap_init); |
Magnus Damm | da67277 | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 789 | module_exit(sh_mobile_i2c_adap_exit); |
| 790 | |
| 791 | MODULE_DESCRIPTION("SuperH Mobile I2C Bus Controller driver"); |
| 792 | MODULE_AUTHOR("Magnus Damm"); |
| 793 | MODULE_LICENSE("GPL v2"); |
Guennadi Liakhovetski | 7ef0c12 | 2011-04-15 20:18:57 +0200 | [diff] [blame] | 794 | MODULE_ALIAS("platform:i2c-sh_mobile"); |