ifx6x60.c 36 KB

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