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