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