ifx6x60.c 36 KB

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