Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * |
| 3 | * Driver for the IFX 6x60 spi modem. |
| 4 | * |
| 5 | * Copyright (C) 2008 Option International |
| 6 | * Copyright (C) 2008 Filip Aben <f.aben@option.com> |
| 7 | * Denis Joseph Barrow <d.barow@option.com> |
| 8 | * Jan Dumon <j.dumon@option.com> |
| 9 | * |
| 10 | * Copyright (C) 2009, 2010 Intel Corp |
Russ Gorby | 2f1522e | 2011-02-02 12:56:58 -0800 | [diff] [blame] | 11 | * Russ Gorby <russ.gorby@intel.com> |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 25 | * USA |
| 26 | * |
| 27 | * Driver modified by Intel from Option gtm501l_spi.c |
| 28 | * |
| 29 | * Notes |
| 30 | * o The driver currently assumes a single device only. If you need to |
| 31 | * change this then look for saved_ifx_dev and add a device lookup |
| 32 | * o The driver is intended to be big-endian safe but has never been |
| 33 | * tested that way (no suitable hardware). There are a couple of FIXME |
| 34 | * notes by areas that may need addressing |
| 35 | * o Some of the GPIO naming/setup assumptions may need revisiting if |
| 36 | * you need to use this driver for another platform. |
| 37 | * |
| 38 | *****************************************************************************/ |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/termios.h> |
| 41 | #include <linux/tty.h> |
| 42 | #include <linux/device.h> |
| 43 | #include <linux/spi/spi.h> |
| 44 | #include <linux/tty.h> |
| 45 | #include <linux/kfifo.h> |
| 46 | #include <linux/tty_flip.h> |
| 47 | #include <linux/timer.h> |
| 48 | #include <linux/serial.h> |
| 49 | #include <linux/interrupt.h> |
| 50 | #include <linux/irq.h> |
| 51 | #include <linux/rfkill.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/ip.h> |
| 54 | #include <linux/dmapool.h> |
| 55 | #include <linux/gpio.h> |
| 56 | #include <linux/sched.h> |
| 57 | #include <linux/time.h> |
| 58 | #include <linux/wait.h> |
| 59 | #include <linux/tty.h> |
| 60 | #include <linux/pm.h> |
| 61 | #include <linux/pm_runtime.h> |
| 62 | #include <linux/spi/ifx_modem.h> |
Alan Cox | 83abd0d | 2010-11-12 10:46:23 +0000 | [diff] [blame] | 63 | #include <linux/delay.h> |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 64 | |
| 65 | #include "ifx6x60.h" |
| 66 | |
| 67 | #define IFX_SPI_MORE_MASK 0x10 |
| 68 | #define IFX_SPI_MORE_BIT 12 /* bit position in u16 */ |
| 69 | #define IFX_SPI_CTS_BIT 13 /* bit position in u16 */ |
| 70 | #define IFX_SPI_TTY_ID 0 |
| 71 | #define IFX_SPI_TIMEOUT_SEC 2 |
| 72 | #define IFX_SPI_HEADER_0 (-1) |
| 73 | #define IFX_SPI_HEADER_F (-2) |
| 74 | |
| 75 | /* forward reference */ |
| 76 | static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev); |
| 77 | |
| 78 | /* local variables */ |
| 79 | static int spi_b16 = 1; /* 8 or 16 bit word length */ |
| 80 | static struct tty_driver *tty_drv; |
| 81 | static struct ifx_spi_device *saved_ifx_dev; |
| 82 | static struct lock_class_key ifx_spi_key; |
| 83 | |
| 84 | /* GPIO/GPE settings */ |
| 85 | |
| 86 | /** |
| 87 | * mrdy_set_high - set MRDY GPIO |
| 88 | * @ifx: device we are controlling |
| 89 | * |
| 90 | */ |
| 91 | static inline void mrdy_set_high(struct ifx_spi_device *ifx) |
| 92 | { |
| 93 | gpio_set_value(ifx->gpio.mrdy, 1); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * mrdy_set_low - clear MRDY GPIO |
| 98 | * @ifx: device we are controlling |
| 99 | * |
| 100 | */ |
| 101 | static inline void mrdy_set_low(struct ifx_spi_device *ifx) |
| 102 | { |
| 103 | gpio_set_value(ifx->gpio.mrdy, 0); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * ifx_spi_power_state_set |
| 108 | * @ifx_dev: our SPI device |
| 109 | * @val: bits to set |
| 110 | * |
| 111 | * Set bit in power status and signal power system if status becomes non-0 |
| 112 | */ |
| 113 | static void |
| 114 | ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val) |
| 115 | { |
| 116 | unsigned long flags; |
| 117 | |
| 118 | spin_lock_irqsave(&ifx_dev->power_lock, flags); |
| 119 | |
| 120 | /* |
| 121 | * if power status is already non-0, just update, else |
| 122 | * tell power system |
| 123 | */ |
| 124 | if (!ifx_dev->power_status) |
| 125 | pm_runtime_get(&ifx_dev->spi_dev->dev); |
| 126 | ifx_dev->power_status |= val; |
| 127 | |
| 128 | spin_unlock_irqrestore(&ifx_dev->power_lock, flags); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * ifx_spi_power_state_clear - clear power bit |
| 133 | * @ifx_dev: our SPI device |
| 134 | * @val: bits to clear |
| 135 | * |
| 136 | * clear bit in power status and signal power system if status becomes 0 |
| 137 | */ |
| 138 | static void |
| 139 | ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val) |
| 140 | { |
| 141 | unsigned long flags; |
| 142 | |
| 143 | spin_lock_irqsave(&ifx_dev->power_lock, flags); |
| 144 | |
| 145 | if (ifx_dev->power_status) { |
| 146 | ifx_dev->power_status &= ~val; |
| 147 | if (!ifx_dev->power_status) |
| 148 | pm_runtime_put(&ifx_dev->spi_dev->dev); |
| 149 | } |
| 150 | |
| 151 | spin_unlock_irqrestore(&ifx_dev->power_lock, flags); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * swap_buf |
| 156 | * @buf: our buffer |
| 157 | * @len : number of bytes (not words) in the buffer |
| 158 | * @end: end of buffer |
| 159 | * |
| 160 | * Swap the contents of a buffer into big endian format |
| 161 | */ |
| 162 | static inline void swap_buf(u16 *buf, int len, void *end) |
| 163 | { |
| 164 | int n; |
| 165 | |
| 166 | len = ((len + 1) >> 1); |
| 167 | if ((void *)&buf[len] > end) { |
| 168 | pr_err("swap_buf: swap exceeds boundary (%p > %p)!", |
| 169 | &buf[len], end); |
| 170 | return; |
| 171 | } |
| 172 | for (n = 0; n < len; n++) { |
| 173 | *buf = cpu_to_be16(*buf); |
| 174 | buf++; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * mrdy_assert - assert MRDY line |
| 180 | * @ifx_dev: our SPI device |
| 181 | * |
| 182 | * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low |
| 183 | * now. |
| 184 | * |
| 185 | * FIXME: Can SRDY even go high as we are running this code ? |
| 186 | */ |
| 187 | static void mrdy_assert(struct ifx_spi_device *ifx_dev) |
| 188 | { |
| 189 | int val = gpio_get_value(ifx_dev->gpio.srdy); |
| 190 | if (!val) { |
| 191 | if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING, |
| 192 | &ifx_dev->flags)) { |
| 193 | ifx_dev->spi_timer.expires = |
| 194 | jiffies + IFX_SPI_TIMEOUT_SEC*HZ; |
| 195 | add_timer(&ifx_dev->spi_timer); |
| 196 | |
| 197 | } |
| 198 | } |
| 199 | ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING); |
| 200 | mrdy_set_high(ifx_dev); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * ifx_spi_hangup - hang up an IFX device |
| 205 | * @ifx_dev: our SPI device |
| 206 | * |
| 207 | * Hang up the tty attached to the IFX device if one is currently |
| 208 | * open. If not take no action |
| 209 | */ |
| 210 | static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev) |
| 211 | { |
| 212 | struct tty_port *pport = &ifx_dev->tty_port; |
| 213 | struct tty_struct *tty = tty_port_tty_get(pport); |
| 214 | if (tty) { |
| 215 | tty_hangup(tty); |
| 216 | tty_kref_put(tty); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * ifx_spi_timeout - SPI timeout |
| 222 | * @arg: our SPI device |
| 223 | * |
| 224 | * The SPI has timed out: hang up the tty. Users will then see a hangup |
| 225 | * and error events. |
| 226 | */ |
| 227 | static void ifx_spi_timeout(unsigned long arg) |
| 228 | { |
| 229 | struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg; |
| 230 | |
| 231 | dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***"); |
| 232 | ifx_spi_ttyhangup(ifx_dev); |
| 233 | mrdy_set_low(ifx_dev); |
| 234 | clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags); |
| 235 | } |
| 236 | |
| 237 | /* char/tty operations */ |
| 238 | |
| 239 | /** |
| 240 | * ifx_spi_tiocmget - get modem lines |
| 241 | * @tty: our tty device |
| 242 | * @filp: file handle issuing the request |
| 243 | * |
| 244 | * Map the signal state into Linux modem flags and report the value |
| 245 | * in Linux terms |
| 246 | */ |
| 247 | static int ifx_spi_tiocmget(struct tty_struct *tty, struct file *filp) |
| 248 | { |
| 249 | unsigned int value; |
| 250 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 251 | |
| 252 | value = |
| 253 | (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) | |
| 254 | (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) | |
| 255 | (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) | |
| 256 | (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) | |
| 257 | (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) | |
| 258 | (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0); |
| 259 | return value; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * ifx_spi_tiocmset - set modem bits |
| 264 | * @tty: the tty structure |
| 265 | * @filp: file handle issuing the request |
| 266 | * @set: bits to set |
| 267 | * @clear: bits to clear |
| 268 | * |
| 269 | * The IFX6x60 only supports DTR and RTS. Set them accordingly |
| 270 | * and flag that an update to the modem is needed. |
| 271 | * |
| 272 | * FIXME: do we need to kick the tranfers when we do this ? |
| 273 | */ |
| 274 | static int ifx_spi_tiocmset(struct tty_struct *tty, struct file *filp, |
| 275 | unsigned int set, unsigned int clear) |
| 276 | { |
| 277 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 278 | |
| 279 | if (set & TIOCM_RTS) |
| 280 | set_bit(IFX_SPI_RTS, &ifx_dev->signal_state); |
| 281 | if (set & TIOCM_DTR) |
| 282 | set_bit(IFX_SPI_DTR, &ifx_dev->signal_state); |
| 283 | if (clear & TIOCM_RTS) |
| 284 | clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state); |
| 285 | if (clear & TIOCM_DTR) |
| 286 | clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state); |
| 287 | |
| 288 | set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * ifx_spi_open - called on tty open |
| 294 | * @tty: our tty device |
| 295 | * @filp: file handle being associated with the tty |
| 296 | * |
| 297 | * Open the tty interface. We let the tty_port layer do all the work |
| 298 | * for us. |
| 299 | * |
| 300 | * FIXME: Remove single device assumption and saved_ifx_dev |
| 301 | */ |
| 302 | static int ifx_spi_open(struct tty_struct *tty, struct file *filp) |
| 303 | { |
| 304 | return tty_port_open(&saved_ifx_dev->tty_port, tty, filp); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * ifx_spi_close - called when our tty closes |
| 309 | * @tty: the tty being closed |
| 310 | * @filp: the file handle being closed |
| 311 | * |
| 312 | * Perform the close of the tty. We use the tty_port layer to do all |
| 313 | * our hard work. |
| 314 | */ |
| 315 | static void ifx_spi_close(struct tty_struct *tty, struct file *filp) |
| 316 | { |
| 317 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 318 | tty_port_close(&ifx_dev->tty_port, tty, filp); |
| 319 | /* FIXME: should we do an ifx_spi_reset here ? */ |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * ifx_decode_spi_header - decode received header |
| 324 | * @buffer: the received data |
| 325 | * @length: decoded length |
| 326 | * @more: decoded more flag |
| 327 | * @received_cts: status of cts we received |
| 328 | * |
| 329 | * Note how received_cts is handled -- if header is all F it is left |
| 330 | * the same as it was, if header is all 0 it is set to 0 otherwise it is |
| 331 | * taken from the incoming header. |
| 332 | * |
| 333 | * FIXME: endianness |
| 334 | */ |
| 335 | static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length, |
| 336 | unsigned char *more, unsigned char *received_cts) |
| 337 | { |
| 338 | u16 h1; |
| 339 | u16 h2; |
| 340 | u16 *in_buffer = (u16 *)buffer; |
| 341 | |
| 342 | h1 = *in_buffer; |
| 343 | h2 = *(in_buffer+1); |
| 344 | |
| 345 | if (h1 == 0 && h2 == 0) { |
| 346 | *received_cts = 0; |
| 347 | return IFX_SPI_HEADER_0; |
| 348 | } else if (h1 == 0xffff && h2 == 0xffff) { |
| 349 | /* spi_slave_cts remains as it was */ |
| 350 | return IFX_SPI_HEADER_F; |
| 351 | } |
| 352 | |
| 353 | *length = h1 & 0xfff; /* upper bits of byte are flags */ |
| 354 | *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1; |
| 355 | *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1; |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * ifx_setup_spi_header - set header fields |
| 361 | * @txbuffer: pointer to start of SPI buffer |
| 362 | * @tx_count: bytes |
| 363 | * @more: indicate if more to follow |
| 364 | * |
| 365 | * Format up an SPI header for a transfer |
| 366 | * |
| 367 | * FIXME: endianness? |
| 368 | */ |
| 369 | static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count, |
| 370 | unsigned char more) |
| 371 | { |
| 372 | *(u16 *)(txbuffer) = tx_count; |
| 373 | *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE; |
| 374 | txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * ifx_spi_wakeup_serial - SPI space made |
| 379 | * @port_data: our SPI device |
| 380 | * |
| 381 | * We have emptied the FIFO enough that we want to get more data |
| 382 | * queued into it. Poke the line discipline via tty_wakeup so that |
| 383 | * it will feed us more bits |
| 384 | */ |
| 385 | static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev) |
| 386 | { |
| 387 | struct tty_struct *tty; |
| 388 | |
| 389 | tty = tty_port_tty_get(&ifx_dev->tty_port); |
| 390 | if (!tty) |
| 391 | return; |
| 392 | tty_wakeup(tty); |
| 393 | tty_kref_put(tty); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * ifx_spi_prepare_tx_buffer - prepare transmit frame |
| 398 | * @ifx_dev: our SPI device |
| 399 | * |
| 400 | * The transmit buffr needs a header and various other bits of |
| 401 | * information followed by as much data as we can pull from the FIFO |
| 402 | * and transfer. This function formats up a suitable buffer in the |
| 403 | * ifx_dev->tx_buffer |
| 404 | * |
| 405 | * FIXME: performance - should we wake the tty when the queue is half |
| 406 | * empty ? |
| 407 | */ |
| 408 | static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev) |
| 409 | { |
| 410 | int temp_count; |
| 411 | int queue_length; |
| 412 | int tx_count; |
| 413 | unsigned char *tx_buffer; |
| 414 | |
| 415 | tx_buffer = ifx_dev->tx_buffer; |
| 416 | memset(tx_buffer, 0, IFX_SPI_TRANSFER_SIZE); |
| 417 | |
| 418 | /* make room for required SPI header */ |
| 419 | tx_buffer += IFX_SPI_HEADER_OVERHEAD; |
| 420 | tx_count = IFX_SPI_HEADER_OVERHEAD; |
| 421 | |
| 422 | /* clear to signal no more data if this turns out to be the |
| 423 | * last buffer sent in a sequence */ |
| 424 | ifx_dev->spi_more = 0; |
| 425 | |
| 426 | /* if modem cts is set, just send empty buffer */ |
| 427 | if (!ifx_dev->spi_slave_cts) { |
| 428 | /* see if there's tx data */ |
| 429 | queue_length = kfifo_len(&ifx_dev->tx_fifo); |
| 430 | if (queue_length != 0) { |
| 431 | /* data to mux -- see if there's room for it */ |
| 432 | temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE); |
| 433 | temp_count = kfifo_out_locked(&ifx_dev->tx_fifo, |
| 434 | tx_buffer, temp_count, |
| 435 | &ifx_dev->fifo_lock); |
| 436 | |
| 437 | /* update buffer pointer and data count in message */ |
| 438 | tx_buffer += temp_count; |
| 439 | tx_count += temp_count; |
| 440 | if (temp_count == queue_length) |
| 441 | /* poke port to get more data */ |
| 442 | ifx_spi_wakeup_serial(ifx_dev); |
| 443 | else /* more data in port, use next SPI message */ |
| 444 | ifx_dev->spi_more = 1; |
| 445 | } |
| 446 | } |
| 447 | /* have data and info for header -- set up SPI header in buffer */ |
| 448 | /* spi header needs payload size, not entire buffer size */ |
| 449 | ifx_spi_setup_spi_header(ifx_dev->tx_buffer, |
| 450 | tx_count-IFX_SPI_HEADER_OVERHEAD, |
| 451 | ifx_dev->spi_more); |
| 452 | /* swap actual data in the buffer */ |
| 453 | swap_buf((u16 *)(ifx_dev->tx_buffer), tx_count, |
| 454 | &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]); |
| 455 | return tx_count; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * ifx_spi_write - line discipline write |
| 460 | * @tty: our tty device |
| 461 | * @buf: pointer to buffer to write (kernel space) |
| 462 | * @count: size of buffer |
| 463 | * |
| 464 | * Write the characters we have been given into the FIFO. If the device |
| 465 | * is not active then activate it, when the SRDY line is asserted back |
| 466 | * this will commence I/O |
| 467 | */ |
| 468 | static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf, |
| 469 | int count) |
| 470 | { |
| 471 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 472 | unsigned char *tmp_buf = (unsigned char *)buf; |
| 473 | int tx_count = kfifo_in_locked(&ifx_dev->tx_fifo, tmp_buf, count, |
| 474 | &ifx_dev->fifo_lock); |
| 475 | mrdy_assert(ifx_dev); |
| 476 | return tx_count; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * ifx_spi_chars_in_buffer - line discipline helper |
| 481 | * @tty: our tty device |
| 482 | * |
| 483 | * Report how much data we can accept before we drop bytes. As we use |
| 484 | * a simple FIFO this is nice and easy. |
| 485 | */ |
| 486 | static int ifx_spi_write_room(struct tty_struct *tty) |
| 487 | { |
| 488 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 489 | return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * ifx_spi_chars_in_buffer - line discipline helper |
| 494 | * @tty: our tty device |
| 495 | * |
| 496 | * Report how many characters we have buffered. In our case this is the |
| 497 | * number of bytes sitting in our transmit FIFO. |
| 498 | */ |
| 499 | static int ifx_spi_chars_in_buffer(struct tty_struct *tty) |
| 500 | { |
| 501 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 502 | return kfifo_len(&ifx_dev->tx_fifo); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * ifx_port_hangup |
| 507 | * @port: our tty port |
| 508 | * |
| 509 | * tty port hang up. Called when tty_hangup processing is invoked either |
| 510 | * by loss of carrier, or by software (eg vhangup). Serialized against |
| 511 | * activate/shutdown by the tty layer. |
| 512 | */ |
| 513 | static void ifx_spi_hangup(struct tty_struct *tty) |
| 514 | { |
| 515 | struct ifx_spi_device *ifx_dev = tty->driver_data; |
| 516 | tty_port_hangup(&ifx_dev->tty_port); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * ifx_port_activate |
| 521 | * @port: our tty port |
| 522 | * |
| 523 | * tty port activate method - called for first open. Serialized |
| 524 | * with hangup and shutdown by the tty layer. |
| 525 | */ |
| 526 | static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty) |
| 527 | { |
| 528 | struct ifx_spi_device *ifx_dev = |
| 529 | container_of(port, struct ifx_spi_device, tty_port); |
| 530 | |
| 531 | /* clear any old data; can't do this in 'close' */ |
| 532 | kfifo_reset(&ifx_dev->tx_fifo); |
| 533 | |
| 534 | /* put port data into this tty */ |
| 535 | tty->driver_data = ifx_dev; |
| 536 | |
| 537 | /* allows flip string push from int context */ |
| 538 | tty->low_latency = 1; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * ifx_port_shutdown |
| 545 | * @port: our tty port |
| 546 | * |
| 547 | * tty port shutdown method - called for last port close. Serialized |
| 548 | * with hangup and activate by the tty layer. |
| 549 | */ |
| 550 | static void ifx_port_shutdown(struct tty_port *port) |
| 551 | { |
| 552 | struct ifx_spi_device *ifx_dev = |
| 553 | container_of(port, struct ifx_spi_device, tty_port); |
| 554 | |
| 555 | mrdy_set_low(ifx_dev); |
| 556 | clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags); |
| 557 | tasklet_kill(&ifx_dev->io_work_tasklet); |
| 558 | } |
| 559 | |
| 560 | static const struct tty_port_operations ifx_tty_port_ops = { |
| 561 | .activate = ifx_port_activate, |
| 562 | .shutdown = ifx_port_shutdown, |
| 563 | }; |
| 564 | |
| 565 | static const struct tty_operations ifx_spi_serial_ops = { |
| 566 | .open = ifx_spi_open, |
| 567 | .close = ifx_spi_close, |
| 568 | .write = ifx_spi_write, |
| 569 | .hangup = ifx_spi_hangup, |
| 570 | .write_room = ifx_spi_write_room, |
| 571 | .chars_in_buffer = ifx_spi_chars_in_buffer, |
| 572 | .tiocmget = ifx_spi_tiocmget, |
| 573 | .tiocmset = ifx_spi_tiocmset, |
| 574 | }; |
| 575 | |
| 576 | /** |
| 577 | * ifx_spi_insert_fip_string - queue received data |
| 578 | * @ifx_ser: our SPI device |
| 579 | * @chars: buffer we have received |
| 580 | * @size: number of chars reeived |
| 581 | * |
| 582 | * Queue bytes to the tty assuming the tty side is currently open. If |
| 583 | * not the discard the data. |
| 584 | */ |
| 585 | static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev, |
| 586 | unsigned char *chars, size_t size) |
| 587 | { |
| 588 | struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port); |
| 589 | if (!tty) |
| 590 | return; |
| 591 | tty_insert_flip_string(tty, chars, size); |
| 592 | tty_flip_buffer_push(tty); |
| 593 | tty_kref_put(tty); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * ifx_spi_complete - SPI transfer completed |
| 598 | * @ctx: our SPI device |
| 599 | * |
| 600 | * An SPI transfer has completed. Process any received data and kick off |
| 601 | * any further transmits we can commence. |
| 602 | */ |
| 603 | static void ifx_spi_complete(void *ctx) |
| 604 | { |
| 605 | struct ifx_spi_device *ifx_dev = ctx; |
| 606 | struct tty_struct *tty; |
| 607 | struct tty_ldisc *ldisc = NULL; |
| 608 | int length; |
| 609 | int actual_length; |
| 610 | unsigned char more; |
| 611 | unsigned char cts; |
| 612 | int local_write_pending = 0; |
| 613 | int queue_length; |
| 614 | int srdy; |
| 615 | int decode_result; |
| 616 | |
| 617 | mrdy_set_low(ifx_dev); |
| 618 | |
| 619 | if (!ifx_dev->spi_msg.status) { |
| 620 | /* check header validity, get comm flags */ |
| 621 | swap_buf((u16 *)ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD, |
| 622 | &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]); |
| 623 | decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer, |
| 624 | &length, &more, &cts); |
| 625 | if (decode_result == IFX_SPI_HEADER_0) { |
| 626 | dev_dbg(&ifx_dev->spi_dev->dev, |
| 627 | "ignore input: invalid header 0"); |
| 628 | ifx_dev->spi_slave_cts = 0; |
| 629 | goto complete_exit; |
| 630 | } else if (decode_result == IFX_SPI_HEADER_F) { |
| 631 | dev_dbg(&ifx_dev->spi_dev->dev, |
| 632 | "ignore input: invalid header F"); |
| 633 | goto complete_exit; |
| 634 | } |
| 635 | |
| 636 | ifx_dev->spi_slave_cts = cts; |
| 637 | |
| 638 | actual_length = min((unsigned int)length, |
| 639 | ifx_dev->spi_msg.actual_length); |
| 640 | swap_buf((u16 *)(ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD), |
| 641 | actual_length, |
| 642 | &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]); |
| 643 | ifx_spi_insert_flip_string( |
| 644 | ifx_dev, |
| 645 | ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD, |
| 646 | (size_t)actual_length); |
| 647 | } else { |
| 648 | dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d", |
| 649 | ifx_dev->spi_msg.status); |
| 650 | } |
| 651 | |
| 652 | complete_exit: |
| 653 | if (ifx_dev->write_pending) { |
| 654 | ifx_dev->write_pending = 0; |
| 655 | local_write_pending = 1; |
| 656 | } |
| 657 | |
| 658 | clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags)); |
| 659 | |
| 660 | queue_length = kfifo_len(&ifx_dev->tx_fifo); |
| 661 | srdy = gpio_get_value(ifx_dev->gpio.srdy); |
| 662 | if (!srdy) |
| 663 | ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY); |
| 664 | |
| 665 | /* schedule output if there is more to do */ |
| 666 | if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags)) |
| 667 | tasklet_schedule(&ifx_dev->io_work_tasklet); |
| 668 | else { |
| 669 | if (more || ifx_dev->spi_more || queue_length > 0 || |
| 670 | local_write_pending) { |
| 671 | if (ifx_dev->spi_slave_cts) { |
| 672 | if (more) |
| 673 | mrdy_assert(ifx_dev); |
| 674 | } else |
| 675 | mrdy_assert(ifx_dev); |
| 676 | } else { |
| 677 | /* |
| 678 | * poke line discipline driver if any for more data |
| 679 | * may or may not get more data to write |
| 680 | * for now, say not busy |
| 681 | */ |
| 682 | ifx_spi_power_state_clear(ifx_dev, |
| 683 | IFX_SPI_POWER_DATA_PENDING); |
| 684 | tty = tty_port_tty_get(&ifx_dev->tty_port); |
| 685 | if (tty) { |
| 686 | ldisc = tty_ldisc_ref(tty); |
| 687 | if (ldisc) { |
| 688 | ldisc->ops->write_wakeup(tty); |
| 689 | tty_ldisc_deref(ldisc); |
| 690 | } |
| 691 | tty_kref_put(tty); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * ifx_spio_io - I/O tasklet |
| 699 | * @data: our SPI device |
| 700 | * |
| 701 | * Queue data for transmission if possible and then kick off the |
| 702 | * transfer. |
| 703 | */ |
| 704 | static void ifx_spi_io(unsigned long data) |
| 705 | { |
| 706 | int retval; |
| 707 | struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data; |
| 708 | |
| 709 | if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) { |
| 710 | if (ifx_dev->gpio.unack_srdy_int_nb > 0) |
| 711 | ifx_dev->gpio.unack_srdy_int_nb--; |
| 712 | |
| 713 | ifx_spi_prepare_tx_buffer(ifx_dev); |
| 714 | |
| 715 | spi_message_init(&ifx_dev->spi_msg); |
| 716 | INIT_LIST_HEAD(&ifx_dev->spi_msg.queue); |
| 717 | |
| 718 | ifx_dev->spi_msg.context = ifx_dev; |
| 719 | ifx_dev->spi_msg.complete = ifx_spi_complete; |
| 720 | |
| 721 | /* set up our spi transfer */ |
| 722 | /* note len is BYTES, not transfers */ |
| 723 | ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE; |
| 724 | ifx_dev->spi_xfer.cs_change = 0; |
| 725 | ifx_dev->spi_xfer.speed_hz = 12500000; |
| 726 | /* ifx_dev->spi_xfer.speed_hz = 390625; */ |
| 727 | ifx_dev->spi_xfer.bits_per_word = spi_b16 ? 16 : 8; |
| 728 | |
| 729 | ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer; |
| 730 | ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer; |
| 731 | |
| 732 | /* |
| 733 | * setup dma pointers |
| 734 | */ |
Russ Gorby | 2f1522e | 2011-02-02 12:56:58 -0800 | [diff] [blame] | 735 | if (ifx_dev->use_dma) { |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 736 | ifx_dev->spi_msg.is_dma_mapped = 1; |
| 737 | ifx_dev->tx_dma = ifx_dev->tx_bus; |
| 738 | ifx_dev->rx_dma = ifx_dev->rx_bus; |
| 739 | ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma; |
| 740 | ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma; |
| 741 | } else { |
| 742 | ifx_dev->spi_msg.is_dma_mapped = 0; |
| 743 | ifx_dev->tx_dma = (dma_addr_t)0; |
| 744 | ifx_dev->rx_dma = (dma_addr_t)0; |
| 745 | ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0; |
| 746 | ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0; |
| 747 | } |
| 748 | |
| 749 | spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg); |
| 750 | |
| 751 | /* Assert MRDY. This may have already been done by the write |
| 752 | * routine. |
| 753 | */ |
| 754 | mrdy_assert(ifx_dev); |
| 755 | |
| 756 | retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg); |
| 757 | if (retval) { |
| 758 | clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, |
| 759 | &ifx_dev->flags); |
| 760 | tasklet_schedule(&ifx_dev->io_work_tasklet); |
| 761 | return; |
| 762 | } |
| 763 | } else |
| 764 | ifx_dev->write_pending = 1; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * ifx_spi_free_port - free up the tty side |
| 769 | * @ifx_dev: IFX device going away |
| 770 | * |
| 771 | * Unregister and free up a port when the device goes away |
| 772 | */ |
| 773 | static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev) |
| 774 | { |
| 775 | if (ifx_dev->tty_dev) |
| 776 | tty_unregister_device(tty_drv, ifx_dev->minor); |
| 777 | kfifo_free(&ifx_dev->tx_fifo); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * ifx_spi_create_port - create a new port |
| 782 | * @ifx_dev: our spi device |
| 783 | * |
| 784 | * Allocate and initialise the tty port that goes with this interface |
| 785 | * and add it to the tty layer so that it can be opened. |
| 786 | */ |
| 787 | static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev) |
| 788 | { |
| 789 | int ret = 0; |
| 790 | struct tty_port *pport = &ifx_dev->tty_port; |
| 791 | |
| 792 | spin_lock_init(&ifx_dev->fifo_lock); |
| 793 | lockdep_set_class_and_subclass(&ifx_dev->fifo_lock, |
| 794 | &ifx_spi_key, 0); |
| 795 | |
| 796 | if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) { |
| 797 | ret = -ENOMEM; |
| 798 | goto error_ret; |
| 799 | } |
| 800 | |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 801 | tty_port_init(pport); |
Russ Gorby | b68f23b | 2011-02-07 12:02:27 -0800 | [diff] [blame^] | 802 | pport->ops = &ifx_tty_port_ops; |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 803 | ifx_dev->minor = IFX_SPI_TTY_ID; |
| 804 | ifx_dev->tty_dev = tty_register_device(tty_drv, ifx_dev->minor, |
| 805 | &ifx_dev->spi_dev->dev); |
| 806 | if (IS_ERR(ifx_dev->tty_dev)) { |
| 807 | dev_dbg(&ifx_dev->spi_dev->dev, |
| 808 | "%s: registering tty device failed", __func__); |
| 809 | ret = PTR_ERR(ifx_dev->tty_dev); |
| 810 | goto error_ret; |
| 811 | } |
| 812 | return 0; |
| 813 | |
| 814 | error_ret: |
| 815 | ifx_spi_free_port(ifx_dev); |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * ifx_spi_handle_srdy - handle SRDY |
| 821 | * @ifx_dev: device asserting SRDY |
| 822 | * |
| 823 | * Check our device state and see what we need to kick off when SRDY |
| 824 | * is asserted. This usually means killing the timer and firing off the |
| 825 | * I/O processing. |
| 826 | */ |
| 827 | static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev) |
| 828 | { |
| 829 | if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) { |
| 830 | del_timer_sync(&ifx_dev->spi_timer); |
| 831 | clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags); |
| 832 | } |
| 833 | |
| 834 | ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY); |
| 835 | |
| 836 | if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) |
| 837 | tasklet_schedule(&ifx_dev->io_work_tasklet); |
| 838 | else |
| 839 | set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * ifx_spi_srdy_interrupt - SRDY asserted |
| 844 | * @irq: our IRQ number |
| 845 | * @dev: our ifx device |
| 846 | * |
| 847 | * The modem asserted SRDY. Handle the srdy event |
| 848 | */ |
| 849 | static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev) |
| 850 | { |
| 851 | struct ifx_spi_device *ifx_dev = dev; |
| 852 | ifx_dev->gpio.unack_srdy_int_nb++; |
| 853 | ifx_spi_handle_srdy(ifx_dev); |
| 854 | return IRQ_HANDLED; |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * ifx_spi_reset_interrupt - Modem has changed reset state |
| 859 | * @irq: interrupt number |
| 860 | * @dev: our device pointer |
| 861 | * |
| 862 | * The modem has either entered or left reset state. Check the GPIO |
| 863 | * line to see which. |
| 864 | * |
| 865 | * FIXME: review locking on MR_INPROGRESS versus |
| 866 | * parallel unsolicited reset/solicited reset |
| 867 | */ |
| 868 | static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev) |
| 869 | { |
| 870 | struct ifx_spi_device *ifx_dev = dev; |
| 871 | int val = gpio_get_value(ifx_dev->gpio.reset_out); |
| 872 | int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state); |
| 873 | |
| 874 | if (val == 0) { |
| 875 | /* entered reset */ |
| 876 | set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state); |
| 877 | if (!solreset) { |
| 878 | /* unsolicited reset */ |
| 879 | ifx_spi_ttyhangup(ifx_dev); |
| 880 | } |
| 881 | } else { |
| 882 | /* exited reset */ |
| 883 | clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state); |
| 884 | if (solreset) { |
| 885 | set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state); |
| 886 | wake_up(&ifx_dev->mdm_reset_wait); |
| 887 | } |
| 888 | } |
| 889 | return IRQ_HANDLED; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * ifx_spi_free_device - free device |
| 894 | * @ifx_dev: device to free |
| 895 | * |
| 896 | * Free the IFX device |
| 897 | */ |
| 898 | static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev) |
| 899 | { |
| 900 | ifx_spi_free_port(ifx_dev); |
| 901 | dma_free_coherent(&ifx_dev->spi_dev->dev, |
| 902 | IFX_SPI_TRANSFER_SIZE, |
| 903 | ifx_dev->tx_buffer, |
| 904 | ifx_dev->tx_bus); |
| 905 | dma_free_coherent(&ifx_dev->spi_dev->dev, |
| 906 | IFX_SPI_TRANSFER_SIZE, |
| 907 | ifx_dev->rx_buffer, |
| 908 | ifx_dev->rx_bus); |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * ifx_spi_reset - reset modem |
| 913 | * @ifx_dev: modem to reset |
| 914 | * |
| 915 | * Perform a reset on the modem |
| 916 | */ |
| 917 | static int ifx_spi_reset(struct ifx_spi_device *ifx_dev) |
| 918 | { |
| 919 | int ret; |
| 920 | /* |
| 921 | * set up modem power, reset |
| 922 | * |
| 923 | * delays are required on some platforms for the modem |
| 924 | * to reset properly |
| 925 | */ |
| 926 | set_bit(MR_START, &ifx_dev->mdm_reset_state); |
| 927 | gpio_set_value(ifx_dev->gpio.po, 0); |
| 928 | gpio_set_value(ifx_dev->gpio.reset, 0); |
| 929 | msleep(25); |
| 930 | gpio_set_value(ifx_dev->gpio.reset, 1); |
| 931 | msleep(1); |
| 932 | gpio_set_value(ifx_dev->gpio.po, 1); |
| 933 | msleep(1); |
| 934 | gpio_set_value(ifx_dev->gpio.po, 0); |
| 935 | ret = wait_event_timeout(ifx_dev->mdm_reset_wait, |
| 936 | test_bit(MR_COMPLETE, |
| 937 | &ifx_dev->mdm_reset_state), |
| 938 | IFX_RESET_TIMEOUT); |
| 939 | if (!ret) |
| 940 | dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)", |
| 941 | ifx_dev->mdm_reset_state); |
| 942 | |
| 943 | ifx_dev->mdm_reset_state = 0; |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * ifx_spi_spi_probe - probe callback |
| 949 | * @spi: our possible matching SPI device |
| 950 | * |
| 951 | * Probe for a 6x60 modem on SPI bus. Perform any needed device and |
| 952 | * GPIO setup. |
| 953 | * |
| 954 | * FIXME: |
| 955 | * - Support for multiple devices |
| 956 | * - Split out MID specific GPIO handling eventually |
| 957 | */ |
| 958 | |
| 959 | static int ifx_spi_spi_probe(struct spi_device *spi) |
| 960 | { |
| 961 | int ret; |
| 962 | int srdy; |
Russ Gorby | 2f1522e | 2011-02-02 12:56:58 -0800 | [diff] [blame] | 963 | struct ifx_modem_platform_data *pl_data; |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 964 | struct ifx_spi_device *ifx_dev; |
| 965 | |
| 966 | if (saved_ifx_dev) { |
| 967 | dev_dbg(&spi->dev, "ignoring subsequent detection"); |
| 968 | return -ENODEV; |
| 969 | } |
| 970 | |
Russ Gorby | 2f1522e | 2011-02-02 12:56:58 -0800 | [diff] [blame] | 971 | pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data; |
| 972 | if (!pl_data) { |
| 973 | dev_err(&spi->dev, "missing platform data!"); |
| 974 | return -ENODEV; |
| 975 | } |
| 976 | |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 977 | /* initialize structure to hold our device variables */ |
| 978 | ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL); |
| 979 | if (!ifx_dev) { |
| 980 | dev_err(&spi->dev, "spi device allocation failed"); |
| 981 | return -ENOMEM; |
| 982 | } |
| 983 | saved_ifx_dev = ifx_dev; |
| 984 | ifx_dev->spi_dev = spi; |
| 985 | clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags); |
| 986 | spin_lock_init(&ifx_dev->write_lock); |
| 987 | spin_lock_init(&ifx_dev->power_lock); |
| 988 | ifx_dev->power_status = 0; |
| 989 | init_timer(&ifx_dev->spi_timer); |
| 990 | ifx_dev->spi_timer.function = ifx_spi_timeout; |
| 991 | ifx_dev->spi_timer.data = (unsigned long)ifx_dev; |
Russ Gorby | 2f1522e | 2011-02-02 12:56:58 -0800 | [diff] [blame] | 992 | ifx_dev->modem = pl_data->modem_type; |
| 993 | ifx_dev->use_dma = pl_data->use_dma; |
| 994 | ifx_dev->max_hz = pl_data->max_hz; |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 995 | |
| 996 | /* ensure SPI protocol flags are initialized to enable transfer */ |
| 997 | ifx_dev->spi_more = 0; |
| 998 | ifx_dev->spi_slave_cts = 0; |
| 999 | |
| 1000 | /*initialize transfer and dma buffers */ |
| 1001 | ifx_dev->tx_buffer = dma_alloc_coherent(&ifx_dev->spi_dev->dev, |
| 1002 | IFX_SPI_TRANSFER_SIZE, |
| 1003 | &ifx_dev->tx_bus, |
| 1004 | GFP_KERNEL); |
| 1005 | if (!ifx_dev->tx_buffer) { |
| 1006 | dev_err(&spi->dev, "DMA-TX buffer allocation failed"); |
| 1007 | ret = -ENOMEM; |
| 1008 | goto error_ret; |
| 1009 | } |
| 1010 | ifx_dev->rx_buffer = dma_alloc_coherent(&ifx_dev->spi_dev->dev, |
| 1011 | IFX_SPI_TRANSFER_SIZE, |
| 1012 | &ifx_dev->rx_bus, |
| 1013 | GFP_KERNEL); |
| 1014 | if (!ifx_dev->rx_buffer) { |
| 1015 | dev_err(&spi->dev, "DMA-RX buffer allocation failed"); |
| 1016 | ret = -ENOMEM; |
| 1017 | goto error_ret; |
| 1018 | } |
| 1019 | |
| 1020 | /* initialize waitq for modem reset */ |
| 1021 | init_waitqueue_head(&ifx_dev->mdm_reset_wait); |
| 1022 | |
| 1023 | spi_set_drvdata(spi, ifx_dev); |
| 1024 | tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io, |
| 1025 | (unsigned long)ifx_dev); |
| 1026 | |
| 1027 | set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags); |
| 1028 | |
| 1029 | /* create our tty port */ |
| 1030 | ret = ifx_spi_create_port(ifx_dev); |
| 1031 | if (ret != 0) { |
| 1032 | dev_err(&spi->dev, "create default tty port failed"); |
| 1033 | goto error_ret; |
| 1034 | } |
| 1035 | |
Russ Gorby | 2f1522e | 2011-02-02 12:56:58 -0800 | [diff] [blame] | 1036 | ifx_dev->gpio.reset = pl_data->rst_pmu; |
| 1037 | ifx_dev->gpio.po = pl_data->pwr_on; |
| 1038 | ifx_dev->gpio.mrdy = pl_data->mrdy; |
| 1039 | ifx_dev->gpio.srdy = pl_data->srdy; |
| 1040 | ifx_dev->gpio.reset_out = pl_data->rst_out; |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 1041 | |
| 1042 | dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d", |
| 1043 | ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy, |
| 1044 | ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out); |
| 1045 | |
| 1046 | /* Configure gpios */ |
| 1047 | ret = gpio_request(ifx_dev->gpio.reset, "ifxModem"); |
| 1048 | if (ret < 0) { |
| 1049 | dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)", |
| 1050 | ifx_dev->gpio.reset); |
| 1051 | goto error_ret; |
| 1052 | } |
| 1053 | ret += gpio_direction_output(ifx_dev->gpio.reset, 0); |
| 1054 | ret += gpio_export(ifx_dev->gpio.reset, 1); |
| 1055 | if (ret) { |
| 1056 | dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)", |
| 1057 | ifx_dev->gpio.reset); |
| 1058 | ret = -EBUSY; |
| 1059 | goto error_ret2; |
| 1060 | } |
| 1061 | |
| 1062 | ret = gpio_request(ifx_dev->gpio.po, "ifxModem"); |
| 1063 | ret += gpio_direction_output(ifx_dev->gpio.po, 0); |
| 1064 | ret += gpio_export(ifx_dev->gpio.po, 1); |
| 1065 | if (ret) { |
| 1066 | dev_err(&spi->dev, "Unable to configure GPIO%d (ON)", |
| 1067 | ifx_dev->gpio.po); |
| 1068 | ret = -EBUSY; |
| 1069 | goto error_ret3; |
| 1070 | } |
| 1071 | |
| 1072 | ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem"); |
| 1073 | if (ret < 0) { |
| 1074 | dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)", |
| 1075 | ifx_dev->gpio.mrdy); |
| 1076 | goto error_ret3; |
| 1077 | } |
| 1078 | ret += gpio_export(ifx_dev->gpio.mrdy, 1); |
| 1079 | ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0); |
| 1080 | if (ret) { |
| 1081 | dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)", |
| 1082 | ifx_dev->gpio.mrdy); |
| 1083 | ret = -EBUSY; |
| 1084 | goto error_ret4; |
| 1085 | } |
| 1086 | |
| 1087 | ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem"); |
| 1088 | if (ret < 0) { |
| 1089 | dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)", |
| 1090 | ifx_dev->gpio.srdy); |
| 1091 | ret = -EBUSY; |
| 1092 | goto error_ret4; |
| 1093 | } |
| 1094 | ret += gpio_export(ifx_dev->gpio.srdy, 1); |
| 1095 | ret += gpio_direction_input(ifx_dev->gpio.srdy); |
| 1096 | if (ret) { |
| 1097 | dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)", |
| 1098 | ifx_dev->gpio.srdy); |
| 1099 | ret = -EBUSY; |
| 1100 | goto error_ret5; |
| 1101 | } |
| 1102 | |
| 1103 | ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem"); |
| 1104 | if (ret < 0) { |
| 1105 | dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)", |
| 1106 | ifx_dev->gpio.reset_out); |
| 1107 | goto error_ret5; |
| 1108 | } |
| 1109 | ret += gpio_export(ifx_dev->gpio.reset_out, 1); |
| 1110 | ret += gpio_direction_input(ifx_dev->gpio.reset_out); |
| 1111 | if (ret) { |
| 1112 | dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)", |
| 1113 | ifx_dev->gpio.reset_out); |
| 1114 | ret = -EBUSY; |
| 1115 | goto error_ret6; |
| 1116 | } |
| 1117 | |
| 1118 | ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out), |
| 1119 | ifx_spi_reset_interrupt, |
| 1120 | IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME, |
| 1121 | (void *)ifx_dev); |
| 1122 | if (ret) { |
| 1123 | dev_err(&spi->dev, "Unable to get irq %x\n", |
| 1124 | gpio_to_irq(ifx_dev->gpio.reset_out)); |
| 1125 | goto error_ret6; |
| 1126 | } |
| 1127 | |
| 1128 | ret = ifx_spi_reset(ifx_dev); |
| 1129 | |
| 1130 | ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy), |
| 1131 | ifx_spi_srdy_interrupt, |
| 1132 | IRQF_TRIGGER_RISING, DRVNAME, |
| 1133 | (void *)ifx_dev); |
| 1134 | if (ret) { |
| 1135 | dev_err(&spi->dev, "Unable to get irq %x", |
| 1136 | gpio_to_irq(ifx_dev->gpio.srdy)); |
Vasiliy Kulikov | badb953 | 2010-11-19 21:42:03 +0300 | [diff] [blame] | 1137 | goto error_ret7; |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | /* set pm runtime power state and register with power system */ |
| 1141 | pm_runtime_set_active(&spi->dev); |
| 1142 | pm_runtime_enable(&spi->dev); |
| 1143 | |
| 1144 | /* handle case that modem is already signaling SRDY */ |
| 1145 | /* no outgoing tty open at this point, this just satisfies the |
| 1146 | * modem's read and should reset communication properly |
| 1147 | */ |
| 1148 | srdy = gpio_get_value(ifx_dev->gpio.srdy); |
| 1149 | |
| 1150 | if (srdy) { |
| 1151 | mrdy_assert(ifx_dev); |
| 1152 | ifx_spi_handle_srdy(ifx_dev); |
| 1153 | } else |
| 1154 | mrdy_set_low(ifx_dev); |
| 1155 | return 0; |
| 1156 | |
Vasiliy Kulikov | badb953 | 2010-11-19 21:42:03 +0300 | [diff] [blame] | 1157 | error_ret7: |
| 1158 | free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev); |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 1159 | error_ret6: |
| 1160 | gpio_free(ifx_dev->gpio.srdy); |
| 1161 | error_ret5: |
| 1162 | gpio_free(ifx_dev->gpio.mrdy); |
| 1163 | error_ret4: |
| 1164 | gpio_free(ifx_dev->gpio.reset); |
| 1165 | error_ret3: |
| 1166 | gpio_free(ifx_dev->gpio.po); |
| 1167 | error_ret2: |
| 1168 | gpio_free(ifx_dev->gpio.reset_out); |
| 1169 | error_ret: |
| 1170 | ifx_spi_free_device(ifx_dev); |
| 1171 | saved_ifx_dev = NULL; |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * ifx_spi_spi_remove - SPI device was removed |
| 1177 | * @spi: SPI device |
| 1178 | * |
| 1179 | * FIXME: We should be shutting the device down here not in |
| 1180 | * the module unload path. |
| 1181 | */ |
| 1182 | |
| 1183 | static int ifx_spi_spi_remove(struct spi_device *spi) |
| 1184 | { |
| 1185 | struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi); |
| 1186 | /* stop activity */ |
| 1187 | tasklet_kill(&ifx_dev->io_work_tasklet); |
| 1188 | /* free irq */ |
| 1189 | free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev); |
| 1190 | free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev); |
| 1191 | |
| 1192 | gpio_free(ifx_dev->gpio.srdy); |
| 1193 | gpio_free(ifx_dev->gpio.mrdy); |
| 1194 | gpio_free(ifx_dev->gpio.reset); |
| 1195 | gpio_free(ifx_dev->gpio.po); |
| 1196 | gpio_free(ifx_dev->gpio.reset_out); |
| 1197 | |
| 1198 | /* free allocations */ |
| 1199 | ifx_spi_free_device(ifx_dev); |
| 1200 | |
| 1201 | saved_ifx_dev = NULL; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /** |
| 1206 | * ifx_spi_spi_shutdown - called on SPI shutdown |
| 1207 | * @spi: SPI device |
| 1208 | * |
| 1209 | * No action needs to be taken here |
| 1210 | */ |
| 1211 | |
| 1212 | static void ifx_spi_spi_shutdown(struct spi_device *spi) |
| 1213 | { |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * various suspends and resumes have nothing to do |
| 1218 | * no hardware to save state for |
| 1219 | */ |
| 1220 | |
| 1221 | /** |
| 1222 | * ifx_spi_spi_suspend - suspend SPI on system suspend |
| 1223 | * @dev: device being suspended |
| 1224 | * |
| 1225 | * Suspend the SPI side. No action needed on Intel MID platforms, may |
| 1226 | * need extending for other systems. |
| 1227 | */ |
| 1228 | static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg) |
| 1229 | { |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * ifx_spi_spi_resume - resume SPI side on system resume |
| 1235 | * @dev: device being suspended |
| 1236 | * |
| 1237 | * Suspend the SPI side. No action needed on Intel MID platforms, may |
| 1238 | * need extending for other systems. |
| 1239 | */ |
| 1240 | static int ifx_spi_spi_resume(struct spi_device *spi) |
| 1241 | { |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * ifx_spi_pm_suspend - suspend modem on system suspend |
| 1247 | * @dev: device being suspended |
| 1248 | * |
| 1249 | * Suspend the modem. No action needed on Intel MID platforms, may |
| 1250 | * need extending for other systems. |
| 1251 | */ |
| 1252 | static int ifx_spi_pm_suspend(struct device *dev) |
| 1253 | { |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * ifx_spi_pm_resume - resume modem on system resume |
| 1259 | * @dev: device being suspended |
| 1260 | * |
| 1261 | * Allow the modem to resume. No action needed. |
| 1262 | * |
| 1263 | * FIXME: do we need to reset anything here ? |
| 1264 | */ |
| 1265 | static int ifx_spi_pm_resume(struct device *dev) |
| 1266 | { |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * ifx_spi_pm_runtime_resume - suspend modem |
| 1272 | * @dev: device being suspended |
| 1273 | * |
| 1274 | * Allow the modem to resume. No action needed. |
| 1275 | */ |
| 1276 | static int ifx_spi_pm_runtime_resume(struct device *dev) |
| 1277 | { |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * ifx_spi_pm_runtime_suspend - suspend modem |
| 1283 | * @dev: device being suspended |
| 1284 | * |
| 1285 | * Allow the modem to suspend and thus suspend to continue up the |
| 1286 | * device tree. |
| 1287 | */ |
| 1288 | static int ifx_spi_pm_runtime_suspend(struct device *dev) |
| 1289 | { |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * ifx_spi_pm_runtime_idle - check if modem idle |
| 1295 | * @dev: our device |
| 1296 | * |
| 1297 | * Check conditions and queue runtime suspend if idle. |
| 1298 | */ |
| 1299 | static int ifx_spi_pm_runtime_idle(struct device *dev) |
| 1300 | { |
| 1301 | struct spi_device *spi = to_spi_device(dev); |
| 1302 | struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi); |
| 1303 | |
| 1304 | if (!ifx_dev->power_status) |
| 1305 | pm_runtime_suspend(dev); |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | static const struct dev_pm_ops ifx_spi_pm = { |
| 1311 | .resume = ifx_spi_pm_resume, |
| 1312 | .suspend = ifx_spi_pm_suspend, |
| 1313 | .runtime_resume = ifx_spi_pm_runtime_resume, |
| 1314 | .runtime_suspend = ifx_spi_pm_runtime_suspend, |
| 1315 | .runtime_idle = ifx_spi_pm_runtime_idle |
| 1316 | }; |
| 1317 | |
| 1318 | static const struct spi_device_id ifx_id_table[] = { |
| 1319 | {"ifx6160", 0}, |
| 1320 | {"ifx6260", 0}, |
| 1321 | { } |
| 1322 | }; |
| 1323 | MODULE_DEVICE_TABLE(spi, ifx_id_table); |
| 1324 | |
| 1325 | /* spi operations */ |
| 1326 | static const struct spi_driver ifx_spi_driver_6160 = { |
| 1327 | .driver = { |
| 1328 | .name = "ifx6160", |
| 1329 | .bus = &spi_bus_type, |
| 1330 | .pm = &ifx_spi_pm, |
| 1331 | .owner = THIS_MODULE}, |
| 1332 | .probe = ifx_spi_spi_probe, |
| 1333 | .shutdown = ifx_spi_spi_shutdown, |
| 1334 | .remove = __devexit_p(ifx_spi_spi_remove), |
| 1335 | .suspend = ifx_spi_spi_suspend, |
| 1336 | .resume = ifx_spi_spi_resume, |
| 1337 | .id_table = ifx_id_table |
| 1338 | }; |
| 1339 | |
| 1340 | /** |
| 1341 | * ifx_spi_exit - module exit |
| 1342 | * |
| 1343 | * Unload the module. |
| 1344 | */ |
| 1345 | |
| 1346 | static void __exit ifx_spi_exit(void) |
| 1347 | { |
| 1348 | /* unregister */ |
| 1349 | tty_unregister_driver(tty_drv); |
| 1350 | spi_unregister_driver((void *)&ifx_spi_driver_6160); |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * ifx_spi_init - module entry point |
| 1355 | * |
| 1356 | * Initialise the SPI and tty interfaces for the IFX SPI driver |
| 1357 | * We need to initialize upper-edge spi driver after the tty |
| 1358 | * driver because otherwise the spi probe will race |
| 1359 | */ |
| 1360 | |
| 1361 | static int __init ifx_spi_init(void) |
| 1362 | { |
| 1363 | int result; |
| 1364 | |
| 1365 | tty_drv = alloc_tty_driver(1); |
| 1366 | if (!tty_drv) { |
| 1367 | pr_err("%s: alloc_tty_driver failed", DRVNAME); |
| 1368 | return -ENOMEM; |
| 1369 | } |
| 1370 | |
| 1371 | tty_drv->magic = TTY_DRIVER_MAGIC; |
| 1372 | tty_drv->owner = THIS_MODULE; |
| 1373 | tty_drv->driver_name = DRVNAME; |
| 1374 | tty_drv->name = TTYNAME; |
| 1375 | tty_drv->minor_start = IFX_SPI_TTY_ID; |
| 1376 | tty_drv->num = 1; |
| 1377 | tty_drv->type = TTY_DRIVER_TYPE_SERIAL; |
| 1378 | tty_drv->subtype = SERIAL_TYPE_NORMAL; |
| 1379 | tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 1380 | tty_drv->init_termios = tty_std_termios; |
| 1381 | |
| 1382 | tty_set_operations(tty_drv, &ifx_spi_serial_ops); |
| 1383 | |
| 1384 | result = tty_register_driver(tty_drv); |
| 1385 | if (result) { |
| 1386 | pr_err("%s: tty_register_driver failed(%d)", |
| 1387 | DRVNAME, result); |
Vasiliy Kulikov | a4fb0b2 | 2010-11-19 21:41:45 +0300 | [diff] [blame] | 1388 | put_tty_driver(tty_drv); |
Russ Gorby | af3b888 | 2010-10-26 14:13:52 +0100 | [diff] [blame] | 1389 | return result; |
| 1390 | } |
| 1391 | |
| 1392 | result = spi_register_driver((void *)&ifx_spi_driver_6160); |
| 1393 | if (result) { |
| 1394 | pr_err("%s: spi_register_driver failed(%d)", |
| 1395 | DRVNAME, result); |
| 1396 | tty_unregister_driver(tty_drv); |
| 1397 | } |
| 1398 | return result; |
| 1399 | } |
| 1400 | |
| 1401 | module_init(ifx_spi_init); |
| 1402 | module_exit(ifx_spi_exit); |
| 1403 | |
| 1404 | MODULE_AUTHOR("Intel"); |
| 1405 | MODULE_DESCRIPTION("IFX6x60 spi driver"); |
| 1406 | MODULE_LICENSE("GPL"); |
| 1407 | MODULE_INFO(Version, "0.1-IFX6x60"); |