ifx6x60.c 37 KB

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