ifx6x60.c 37 KB

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