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