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