ifx6x60.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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
  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(u16 *buf, int len, void *end)
  147. {
  148. int n;
  149. len = ((len + 1) >> 1);
  150. if ((void *)&buf[len] > end) {
  151. pr_err("swap_buf: swap exceeds boundary (%p > %p)!",
  152. &buf[len], end);
  153. return;
  154. }
  155. for (n = 0; n < len; n++) {
  156. *buf = cpu_to_be16(*buf);
  157. buf++;
  158. }
  159. }
  160. /**
  161. * mrdy_assert - assert MRDY line
  162. * @ifx_dev: our SPI device
  163. *
  164. * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
  165. * now.
  166. *
  167. * FIXME: Can SRDY even go high as we are running this code ?
  168. */
  169. static void mrdy_assert(struct ifx_spi_device *ifx_dev)
  170. {
  171. int val = gpio_get_value(ifx_dev->gpio.srdy);
  172. if (!val) {
  173. if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
  174. &ifx_dev->flags)) {
  175. ifx_dev->spi_timer.expires =
  176. jiffies + IFX_SPI_TIMEOUT_SEC*HZ;
  177. add_timer(&ifx_dev->spi_timer);
  178. }
  179. }
  180. ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
  181. mrdy_set_high(ifx_dev);
  182. }
  183. /**
  184. * ifx_spi_hangup - hang up an IFX device
  185. * @ifx_dev: our SPI device
  186. *
  187. * Hang up the tty attached to the IFX device if one is currently
  188. * open. If not take no action
  189. */
  190. static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
  191. {
  192. struct tty_port *pport = &ifx_dev->tty_port;
  193. struct tty_struct *tty = tty_port_tty_get(pport);
  194. if (tty) {
  195. tty_hangup(tty);
  196. tty_kref_put(tty);
  197. }
  198. }
  199. /**
  200. * ifx_spi_timeout - SPI timeout
  201. * @arg: our SPI device
  202. *
  203. * The SPI has timed out: hang up the tty. Users will then see a hangup
  204. * and error events.
  205. */
  206. static void ifx_spi_timeout(unsigned long arg)
  207. {
  208. struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
  209. dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
  210. ifx_spi_ttyhangup(ifx_dev);
  211. mrdy_set_low(ifx_dev);
  212. clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
  213. }
  214. /* char/tty operations */
  215. /**
  216. * ifx_spi_tiocmget - get modem lines
  217. * @tty: our tty device
  218. * @filp: file handle issuing the request
  219. *
  220. * Map the signal state into Linux modem flags and report the value
  221. * in Linux terms
  222. */
  223. static int ifx_spi_tiocmget(struct tty_struct *tty)
  224. {
  225. unsigned int value;
  226. struct ifx_spi_device *ifx_dev = tty->driver_data;
  227. value =
  228. (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
  229. (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
  230. (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
  231. (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
  232. (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
  233. (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
  234. return value;
  235. }
  236. /**
  237. * ifx_spi_tiocmset - set modem bits
  238. * @tty: the tty structure
  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,
  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. del_timer(&ifx_dev->spi_timer);
  502. clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
  503. tasklet_kill(&ifx_dev->io_work_tasklet);
  504. }
  505. static const struct tty_port_operations ifx_tty_port_ops = {
  506. .activate = ifx_port_activate,
  507. .shutdown = ifx_port_shutdown,
  508. };
  509. static const struct tty_operations ifx_spi_serial_ops = {
  510. .open = ifx_spi_open,
  511. .close = ifx_spi_close,
  512. .write = ifx_spi_write,
  513. .hangup = ifx_spi_hangup,
  514. .write_room = ifx_spi_write_room,
  515. .chars_in_buffer = ifx_spi_chars_in_buffer,
  516. .tiocmget = ifx_spi_tiocmget,
  517. .tiocmset = ifx_spi_tiocmset,
  518. };
  519. /**
  520. * ifx_spi_insert_fip_string - queue received data
  521. * @ifx_ser: our SPI device
  522. * @chars: buffer we have received
  523. * @size: number of chars reeived
  524. *
  525. * Queue bytes to the tty assuming the tty side is currently open. If
  526. * not the discard the data.
  527. */
  528. static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
  529. unsigned char *chars, size_t size)
  530. {
  531. struct tty_struct *tty = tty_port_tty_get(&ifx_dev->tty_port);
  532. if (!tty)
  533. return;
  534. tty_insert_flip_string(tty, chars, size);
  535. tty_flip_buffer_push(tty);
  536. tty_kref_put(tty);
  537. }
  538. /**
  539. * ifx_spi_complete - SPI transfer completed
  540. * @ctx: our SPI device
  541. *
  542. * An SPI transfer has completed. Process any received data and kick off
  543. * any further transmits we can commence.
  544. */
  545. static void ifx_spi_complete(void *ctx)
  546. {
  547. struct ifx_spi_device *ifx_dev = ctx;
  548. struct tty_struct *tty;
  549. struct tty_ldisc *ldisc = NULL;
  550. int length;
  551. int actual_length;
  552. unsigned char more;
  553. unsigned char cts;
  554. int local_write_pending = 0;
  555. int queue_length;
  556. int srdy;
  557. int decode_result;
  558. mrdy_set_low(ifx_dev);
  559. if (!ifx_dev->spi_msg.status) {
  560. /* check header validity, get comm flags */
  561. swap_buf((u16 *)ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
  562. &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
  563. decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
  564. &length, &more, &cts);
  565. if (decode_result == IFX_SPI_HEADER_0) {
  566. dev_dbg(&ifx_dev->spi_dev->dev,
  567. "ignore input: invalid header 0");
  568. ifx_dev->spi_slave_cts = 0;
  569. goto complete_exit;
  570. } else if (decode_result == IFX_SPI_HEADER_F) {
  571. dev_dbg(&ifx_dev->spi_dev->dev,
  572. "ignore input: invalid header F");
  573. goto complete_exit;
  574. }
  575. ifx_dev->spi_slave_cts = cts;
  576. actual_length = min((unsigned int)length,
  577. ifx_dev->spi_msg.actual_length);
  578. swap_buf((u16 *)(ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
  579. actual_length,
  580. &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
  581. ifx_spi_insert_flip_string(
  582. ifx_dev,
  583. ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
  584. (size_t)actual_length);
  585. } else {
  586. dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
  587. ifx_dev->spi_msg.status);
  588. }
  589. complete_exit:
  590. if (ifx_dev->write_pending) {
  591. ifx_dev->write_pending = 0;
  592. local_write_pending = 1;
  593. }
  594. clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
  595. queue_length = kfifo_len(&ifx_dev->tx_fifo);
  596. srdy = gpio_get_value(ifx_dev->gpio.srdy);
  597. if (!srdy)
  598. ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
  599. /* schedule output if there is more to do */
  600. if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
  601. tasklet_schedule(&ifx_dev->io_work_tasklet);
  602. else {
  603. if (more || ifx_dev->spi_more || queue_length > 0 ||
  604. local_write_pending) {
  605. if (ifx_dev->spi_slave_cts) {
  606. if (more)
  607. mrdy_assert(ifx_dev);
  608. } else
  609. mrdy_assert(ifx_dev);
  610. } else {
  611. /*
  612. * poke line discipline driver if any for more data
  613. * may or may not get more data to write
  614. * for now, say not busy
  615. */
  616. ifx_spi_power_state_clear(ifx_dev,
  617. IFX_SPI_POWER_DATA_PENDING);
  618. tty = tty_port_tty_get(&ifx_dev->tty_port);
  619. if (tty) {
  620. ldisc = tty_ldisc_ref(tty);
  621. if (ldisc) {
  622. ldisc->ops->write_wakeup(tty);
  623. tty_ldisc_deref(ldisc);
  624. }
  625. tty_kref_put(tty);
  626. }
  627. }
  628. }
  629. }
  630. /**
  631. * ifx_spio_io - I/O tasklet
  632. * @data: our SPI device
  633. *
  634. * Queue data for transmission if possible and then kick off the
  635. * transfer.
  636. */
  637. static void ifx_spi_io(unsigned long data)
  638. {
  639. int retval;
  640. struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
  641. if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) {
  642. if (ifx_dev->gpio.unack_srdy_int_nb > 0)
  643. ifx_dev->gpio.unack_srdy_int_nb--;
  644. ifx_spi_prepare_tx_buffer(ifx_dev);
  645. spi_message_init(&ifx_dev->spi_msg);
  646. INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
  647. ifx_dev->spi_msg.context = ifx_dev;
  648. ifx_dev->spi_msg.complete = ifx_spi_complete;
  649. /* set up our spi transfer */
  650. /* note len is BYTES, not transfers */
  651. ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
  652. ifx_dev->spi_xfer.cs_change = 0;
  653. ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
  654. /* ifx_dev->spi_xfer.speed_hz = 390625; */
  655. ifx_dev->spi_xfer.bits_per_word = spi_bpw;
  656. ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
  657. ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
  658. /*
  659. * setup dma pointers
  660. */
  661. if (ifx_dev->use_dma) {
  662. ifx_dev->spi_msg.is_dma_mapped = 1;
  663. ifx_dev->tx_dma = ifx_dev->tx_bus;
  664. ifx_dev->rx_dma = ifx_dev->rx_bus;
  665. ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
  666. ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
  667. } else {
  668. ifx_dev->spi_msg.is_dma_mapped = 0;
  669. ifx_dev->tx_dma = (dma_addr_t)0;
  670. ifx_dev->rx_dma = (dma_addr_t)0;
  671. ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
  672. ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
  673. }
  674. spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
  675. /* Assert MRDY. This may have already been done by the write
  676. * routine.
  677. */
  678. mrdy_assert(ifx_dev);
  679. retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
  680. if (retval) {
  681. clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
  682. &ifx_dev->flags);
  683. tasklet_schedule(&ifx_dev->io_work_tasklet);
  684. return;
  685. }
  686. } else
  687. ifx_dev->write_pending = 1;
  688. }
  689. /**
  690. * ifx_spi_free_port - free up the tty side
  691. * @ifx_dev: IFX device going away
  692. *
  693. * Unregister and free up a port when the device goes away
  694. */
  695. static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
  696. {
  697. if (ifx_dev->tty_dev)
  698. tty_unregister_device(tty_drv, ifx_dev->minor);
  699. kfifo_free(&ifx_dev->tx_fifo);
  700. }
  701. /**
  702. * ifx_spi_create_port - create a new port
  703. * @ifx_dev: our spi device
  704. *
  705. * Allocate and initialise the tty port that goes with this interface
  706. * and add it to the tty layer so that it can be opened.
  707. */
  708. static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
  709. {
  710. int ret = 0;
  711. struct tty_port *pport = &ifx_dev->tty_port;
  712. spin_lock_init(&ifx_dev->fifo_lock);
  713. lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
  714. &ifx_spi_key, 0);
  715. if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
  716. ret = -ENOMEM;
  717. goto error_ret;
  718. }
  719. tty_port_init(pport);
  720. pport->ops = &ifx_tty_port_ops;
  721. ifx_dev->minor = IFX_SPI_TTY_ID;
  722. ifx_dev->tty_dev = tty_register_device(tty_drv, ifx_dev->minor,
  723. &ifx_dev->spi_dev->dev);
  724. if (IS_ERR(ifx_dev->tty_dev)) {
  725. dev_dbg(&ifx_dev->spi_dev->dev,
  726. "%s: registering tty device failed", __func__);
  727. ret = PTR_ERR(ifx_dev->tty_dev);
  728. goto error_ret;
  729. }
  730. return 0;
  731. error_ret:
  732. ifx_spi_free_port(ifx_dev);
  733. return ret;
  734. }
  735. /**
  736. * ifx_spi_handle_srdy - handle SRDY
  737. * @ifx_dev: device asserting SRDY
  738. *
  739. * Check our device state and see what we need to kick off when SRDY
  740. * is asserted. This usually means killing the timer and firing off the
  741. * I/O processing.
  742. */
  743. static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
  744. {
  745. if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
  746. del_timer_sync(&ifx_dev->spi_timer);
  747. clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
  748. }
  749. ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
  750. if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
  751. tasklet_schedule(&ifx_dev->io_work_tasklet);
  752. else
  753. set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
  754. }
  755. /**
  756. * ifx_spi_srdy_interrupt - SRDY asserted
  757. * @irq: our IRQ number
  758. * @dev: our ifx device
  759. *
  760. * The modem asserted SRDY. Handle the srdy event
  761. */
  762. static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
  763. {
  764. struct ifx_spi_device *ifx_dev = dev;
  765. ifx_dev->gpio.unack_srdy_int_nb++;
  766. ifx_spi_handle_srdy(ifx_dev);
  767. return IRQ_HANDLED;
  768. }
  769. /**
  770. * ifx_spi_reset_interrupt - Modem has changed reset state
  771. * @irq: interrupt number
  772. * @dev: our device pointer
  773. *
  774. * The modem has either entered or left reset state. Check the GPIO
  775. * line to see which.
  776. *
  777. * FIXME: review locking on MR_INPROGRESS versus
  778. * parallel unsolicited reset/solicited reset
  779. */
  780. static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
  781. {
  782. struct ifx_spi_device *ifx_dev = dev;
  783. int val = gpio_get_value(ifx_dev->gpio.reset_out);
  784. int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
  785. if (val == 0) {
  786. /* entered reset */
  787. set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
  788. if (!solreset) {
  789. /* unsolicited reset */
  790. ifx_spi_ttyhangup(ifx_dev);
  791. }
  792. } else {
  793. /* exited reset */
  794. clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
  795. if (solreset) {
  796. set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
  797. wake_up(&ifx_dev->mdm_reset_wait);
  798. }
  799. }
  800. return IRQ_HANDLED;
  801. }
  802. /**
  803. * ifx_spi_free_device - free device
  804. * @ifx_dev: device to free
  805. *
  806. * Free the IFX device
  807. */
  808. static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
  809. {
  810. ifx_spi_free_port(ifx_dev);
  811. dma_free_coherent(&ifx_dev->spi_dev->dev,
  812. IFX_SPI_TRANSFER_SIZE,
  813. ifx_dev->tx_buffer,
  814. ifx_dev->tx_bus);
  815. dma_free_coherent(&ifx_dev->spi_dev->dev,
  816. IFX_SPI_TRANSFER_SIZE,
  817. ifx_dev->rx_buffer,
  818. ifx_dev->rx_bus);
  819. }
  820. /**
  821. * ifx_spi_reset - reset modem
  822. * @ifx_dev: modem to reset
  823. *
  824. * Perform a reset on the modem
  825. */
  826. static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
  827. {
  828. int ret;
  829. /*
  830. * set up modem power, reset
  831. *
  832. * delays are required on some platforms for the modem
  833. * to reset properly
  834. */
  835. set_bit(MR_START, &ifx_dev->mdm_reset_state);
  836. gpio_set_value(ifx_dev->gpio.po, 0);
  837. gpio_set_value(ifx_dev->gpio.reset, 0);
  838. msleep(25);
  839. gpio_set_value(ifx_dev->gpio.reset, 1);
  840. msleep(1);
  841. gpio_set_value(ifx_dev->gpio.po, 1);
  842. msleep(1);
  843. gpio_set_value(ifx_dev->gpio.po, 0);
  844. ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
  845. test_bit(MR_COMPLETE,
  846. &ifx_dev->mdm_reset_state),
  847. IFX_RESET_TIMEOUT);
  848. if (!ret)
  849. dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
  850. ifx_dev->mdm_reset_state);
  851. ifx_dev->mdm_reset_state = 0;
  852. return ret;
  853. }
  854. /**
  855. * ifx_spi_spi_probe - probe callback
  856. * @spi: our possible matching SPI device
  857. *
  858. * Probe for a 6x60 modem on SPI bus. Perform any needed device and
  859. * GPIO setup.
  860. *
  861. * FIXME:
  862. * - Support for multiple devices
  863. * - Split out MID specific GPIO handling eventually
  864. */
  865. static int ifx_spi_spi_probe(struct spi_device *spi)
  866. {
  867. int ret;
  868. int srdy;
  869. struct ifx_modem_platform_data *pl_data;
  870. struct ifx_spi_device *ifx_dev;
  871. if (saved_ifx_dev) {
  872. dev_dbg(&spi->dev, "ignoring subsequent detection");
  873. return -ENODEV;
  874. }
  875. pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
  876. if (!pl_data) {
  877. dev_err(&spi->dev, "missing platform data!");
  878. return -ENODEV;
  879. }
  880. /* initialize structure to hold our device variables */
  881. ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
  882. if (!ifx_dev) {
  883. dev_err(&spi->dev, "spi device allocation failed");
  884. return -ENOMEM;
  885. }
  886. saved_ifx_dev = ifx_dev;
  887. ifx_dev->spi_dev = spi;
  888. clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
  889. spin_lock_init(&ifx_dev->write_lock);
  890. spin_lock_init(&ifx_dev->power_lock);
  891. ifx_dev->power_status = 0;
  892. init_timer(&ifx_dev->spi_timer);
  893. ifx_dev->spi_timer.function = ifx_spi_timeout;
  894. ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
  895. ifx_dev->modem = pl_data->modem_type;
  896. ifx_dev->use_dma = pl_data->use_dma;
  897. ifx_dev->max_hz = pl_data->max_hz;
  898. /* initialize spi mode, etc */
  899. spi->max_speed_hz = ifx_dev->max_hz;
  900. spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
  901. spi->bits_per_word = spi_bpw;
  902. ret = spi_setup(spi);
  903. if (ret) {
  904. dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
  905. return -ENODEV;
  906. }
  907. /* ensure SPI protocol flags are initialized to enable transfer */
  908. ifx_dev->spi_more = 0;
  909. ifx_dev->spi_slave_cts = 0;
  910. /*initialize transfer and dma buffers */
  911. ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
  912. IFX_SPI_TRANSFER_SIZE,
  913. &ifx_dev->tx_bus,
  914. GFP_KERNEL);
  915. if (!ifx_dev->tx_buffer) {
  916. dev_err(&spi->dev, "DMA-TX buffer allocation failed");
  917. ret = -ENOMEM;
  918. goto error_ret;
  919. }
  920. ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
  921. IFX_SPI_TRANSFER_SIZE,
  922. &ifx_dev->rx_bus,
  923. GFP_KERNEL);
  924. if (!ifx_dev->rx_buffer) {
  925. dev_err(&spi->dev, "DMA-RX buffer allocation failed");
  926. ret = -ENOMEM;
  927. goto error_ret;
  928. }
  929. /* initialize waitq for modem reset */
  930. init_waitqueue_head(&ifx_dev->mdm_reset_wait);
  931. spi_set_drvdata(spi, ifx_dev);
  932. tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
  933. (unsigned long)ifx_dev);
  934. set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
  935. /* create our tty port */
  936. ret = ifx_spi_create_port(ifx_dev);
  937. if (ret != 0) {
  938. dev_err(&spi->dev, "create default tty port failed");
  939. goto error_ret;
  940. }
  941. ifx_dev->gpio.reset = pl_data->rst_pmu;
  942. ifx_dev->gpio.po = pl_data->pwr_on;
  943. ifx_dev->gpio.mrdy = pl_data->mrdy;
  944. ifx_dev->gpio.srdy = pl_data->srdy;
  945. ifx_dev->gpio.reset_out = pl_data->rst_out;
  946. dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
  947. ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
  948. ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
  949. /* Configure gpios */
  950. ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
  951. if (ret < 0) {
  952. dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
  953. ifx_dev->gpio.reset);
  954. goto error_ret;
  955. }
  956. ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
  957. ret += gpio_export(ifx_dev->gpio.reset, 1);
  958. if (ret) {
  959. dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
  960. ifx_dev->gpio.reset);
  961. ret = -EBUSY;
  962. goto error_ret2;
  963. }
  964. ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
  965. ret += gpio_direction_output(ifx_dev->gpio.po, 0);
  966. ret += gpio_export(ifx_dev->gpio.po, 1);
  967. if (ret) {
  968. dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
  969. ifx_dev->gpio.po);
  970. ret = -EBUSY;
  971. goto error_ret3;
  972. }
  973. ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
  974. if (ret < 0) {
  975. dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
  976. ifx_dev->gpio.mrdy);
  977. goto error_ret3;
  978. }
  979. ret += gpio_export(ifx_dev->gpio.mrdy, 1);
  980. ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
  981. if (ret) {
  982. dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
  983. ifx_dev->gpio.mrdy);
  984. ret = -EBUSY;
  985. goto error_ret4;
  986. }
  987. ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
  988. if (ret < 0) {
  989. dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
  990. ifx_dev->gpio.srdy);
  991. ret = -EBUSY;
  992. goto error_ret4;
  993. }
  994. ret += gpio_export(ifx_dev->gpio.srdy, 1);
  995. ret += gpio_direction_input(ifx_dev->gpio.srdy);
  996. if (ret) {
  997. dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
  998. ifx_dev->gpio.srdy);
  999. ret = -EBUSY;
  1000. goto error_ret5;
  1001. }
  1002. ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
  1003. if (ret < 0) {
  1004. dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
  1005. ifx_dev->gpio.reset_out);
  1006. goto error_ret5;
  1007. }
  1008. ret += gpio_export(ifx_dev->gpio.reset_out, 1);
  1009. ret += gpio_direction_input(ifx_dev->gpio.reset_out);
  1010. if (ret) {
  1011. dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
  1012. ifx_dev->gpio.reset_out);
  1013. ret = -EBUSY;
  1014. goto error_ret6;
  1015. }
  1016. ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
  1017. ifx_spi_reset_interrupt,
  1018. IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
  1019. (void *)ifx_dev);
  1020. if (ret) {
  1021. dev_err(&spi->dev, "Unable to get irq %x\n",
  1022. gpio_to_irq(ifx_dev->gpio.reset_out));
  1023. goto error_ret6;
  1024. }
  1025. ret = ifx_spi_reset(ifx_dev);
  1026. ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
  1027. ifx_spi_srdy_interrupt,
  1028. IRQF_TRIGGER_RISING, DRVNAME,
  1029. (void *)ifx_dev);
  1030. if (ret) {
  1031. dev_err(&spi->dev, "Unable to get irq %x",
  1032. gpio_to_irq(ifx_dev->gpio.srdy));
  1033. goto error_ret7;
  1034. }
  1035. /* set pm runtime power state and register with power system */
  1036. pm_runtime_set_active(&spi->dev);
  1037. pm_runtime_enable(&spi->dev);
  1038. /* handle case that modem is already signaling SRDY */
  1039. /* no outgoing tty open at this point, this just satisfies the
  1040. * modem's read and should reset communication properly
  1041. */
  1042. srdy = gpio_get_value(ifx_dev->gpio.srdy);
  1043. if (srdy) {
  1044. mrdy_assert(ifx_dev);
  1045. ifx_spi_handle_srdy(ifx_dev);
  1046. } else
  1047. mrdy_set_low(ifx_dev);
  1048. return 0;
  1049. error_ret7:
  1050. free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
  1051. error_ret6:
  1052. gpio_free(ifx_dev->gpio.srdy);
  1053. error_ret5:
  1054. gpio_free(ifx_dev->gpio.mrdy);
  1055. error_ret4:
  1056. gpio_free(ifx_dev->gpio.reset);
  1057. error_ret3:
  1058. gpio_free(ifx_dev->gpio.po);
  1059. error_ret2:
  1060. gpio_free(ifx_dev->gpio.reset_out);
  1061. error_ret:
  1062. ifx_spi_free_device(ifx_dev);
  1063. saved_ifx_dev = NULL;
  1064. return ret;
  1065. }
  1066. /**
  1067. * ifx_spi_spi_remove - SPI device was removed
  1068. * @spi: SPI device
  1069. *
  1070. * FIXME: We should be shutting the device down here not in
  1071. * the module unload path.
  1072. */
  1073. static int ifx_spi_spi_remove(struct spi_device *spi)
  1074. {
  1075. struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
  1076. /* stop activity */
  1077. tasklet_kill(&ifx_dev->io_work_tasklet);
  1078. /* free irq */
  1079. free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
  1080. free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
  1081. gpio_free(ifx_dev->gpio.srdy);
  1082. gpio_free(ifx_dev->gpio.mrdy);
  1083. gpio_free(ifx_dev->gpio.reset);
  1084. gpio_free(ifx_dev->gpio.po);
  1085. gpio_free(ifx_dev->gpio.reset_out);
  1086. /* free allocations */
  1087. ifx_spi_free_device(ifx_dev);
  1088. saved_ifx_dev = NULL;
  1089. return 0;
  1090. }
  1091. /**
  1092. * ifx_spi_spi_shutdown - called on SPI shutdown
  1093. * @spi: SPI device
  1094. *
  1095. * No action needs to be taken here
  1096. */
  1097. static void ifx_spi_spi_shutdown(struct spi_device *spi)
  1098. {
  1099. }
  1100. /*
  1101. * various suspends and resumes have nothing to do
  1102. * no hardware to save state for
  1103. */
  1104. /**
  1105. * ifx_spi_spi_suspend - suspend SPI on system suspend
  1106. * @dev: device being suspended
  1107. *
  1108. * Suspend the SPI side. No action needed on Intel MID platforms, may
  1109. * need extending for other systems.
  1110. */
  1111. static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
  1112. {
  1113. return 0;
  1114. }
  1115. /**
  1116. * ifx_spi_spi_resume - resume SPI side on system resume
  1117. * @dev: device being suspended
  1118. *
  1119. * Suspend the SPI side. No action needed on Intel MID platforms, may
  1120. * need extending for other systems.
  1121. */
  1122. static int ifx_spi_spi_resume(struct spi_device *spi)
  1123. {
  1124. return 0;
  1125. }
  1126. /**
  1127. * ifx_spi_pm_suspend - suspend modem on system suspend
  1128. * @dev: device being suspended
  1129. *
  1130. * Suspend the modem. No action needed on Intel MID platforms, may
  1131. * need extending for other systems.
  1132. */
  1133. static int ifx_spi_pm_suspend(struct device *dev)
  1134. {
  1135. return 0;
  1136. }
  1137. /**
  1138. * ifx_spi_pm_resume - resume modem on system resume
  1139. * @dev: device being suspended
  1140. *
  1141. * Allow the modem to resume. No action needed.
  1142. *
  1143. * FIXME: do we need to reset anything here ?
  1144. */
  1145. static int ifx_spi_pm_resume(struct device *dev)
  1146. {
  1147. return 0;
  1148. }
  1149. /**
  1150. * ifx_spi_pm_runtime_resume - suspend modem
  1151. * @dev: device being suspended
  1152. *
  1153. * Allow the modem to resume. No action needed.
  1154. */
  1155. static int ifx_spi_pm_runtime_resume(struct device *dev)
  1156. {
  1157. return 0;
  1158. }
  1159. /**
  1160. * ifx_spi_pm_runtime_suspend - suspend modem
  1161. * @dev: device being suspended
  1162. *
  1163. * Allow the modem to suspend and thus suspend to continue up the
  1164. * device tree.
  1165. */
  1166. static int ifx_spi_pm_runtime_suspend(struct device *dev)
  1167. {
  1168. return 0;
  1169. }
  1170. /**
  1171. * ifx_spi_pm_runtime_idle - check if modem idle
  1172. * @dev: our device
  1173. *
  1174. * Check conditions and queue runtime suspend if idle.
  1175. */
  1176. static int ifx_spi_pm_runtime_idle(struct device *dev)
  1177. {
  1178. struct spi_device *spi = to_spi_device(dev);
  1179. struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
  1180. if (!ifx_dev->power_status)
  1181. pm_runtime_suspend(dev);
  1182. return 0;
  1183. }
  1184. static const struct dev_pm_ops ifx_spi_pm = {
  1185. .resume = ifx_spi_pm_resume,
  1186. .suspend = ifx_spi_pm_suspend,
  1187. .runtime_resume = ifx_spi_pm_runtime_resume,
  1188. .runtime_suspend = ifx_spi_pm_runtime_suspend,
  1189. .runtime_idle = ifx_spi_pm_runtime_idle
  1190. };
  1191. static const struct spi_device_id ifx_id_table[] = {
  1192. {"ifx6160", 0},
  1193. {"ifx6260", 0},
  1194. { }
  1195. };
  1196. MODULE_DEVICE_TABLE(spi, ifx_id_table);
  1197. /* spi operations */
  1198. static const struct spi_driver ifx_spi_driver = {
  1199. .driver = {
  1200. .name = DRVNAME,
  1201. .pm = &ifx_spi_pm,
  1202. .owner = THIS_MODULE},
  1203. .probe = ifx_spi_spi_probe,
  1204. .shutdown = ifx_spi_spi_shutdown,
  1205. .remove = __devexit_p(ifx_spi_spi_remove),
  1206. .suspend = ifx_spi_spi_suspend,
  1207. .resume = ifx_spi_spi_resume,
  1208. .id_table = ifx_id_table
  1209. };
  1210. /**
  1211. * ifx_spi_exit - module exit
  1212. *
  1213. * Unload the module.
  1214. */
  1215. static void __exit ifx_spi_exit(void)
  1216. {
  1217. /* unregister */
  1218. tty_unregister_driver(tty_drv);
  1219. spi_unregister_driver((void *)&ifx_spi_driver);
  1220. }
  1221. /**
  1222. * ifx_spi_init - module entry point
  1223. *
  1224. * Initialise the SPI and tty interfaces for the IFX SPI driver
  1225. * We need to initialize upper-edge spi driver after the tty
  1226. * driver because otherwise the spi probe will race
  1227. */
  1228. static int __init ifx_spi_init(void)
  1229. {
  1230. int result;
  1231. tty_drv = alloc_tty_driver(1);
  1232. if (!tty_drv) {
  1233. pr_err("%s: alloc_tty_driver failed", DRVNAME);
  1234. return -ENOMEM;
  1235. }
  1236. tty_drv->driver_name = DRVNAME;
  1237. tty_drv->name = TTYNAME;
  1238. tty_drv->minor_start = IFX_SPI_TTY_ID;
  1239. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  1240. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  1241. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1242. tty_drv->init_termios = tty_std_termios;
  1243. tty_set_operations(tty_drv, &ifx_spi_serial_ops);
  1244. result = tty_register_driver(tty_drv);
  1245. if (result) {
  1246. pr_err("%s: tty_register_driver failed(%d)",
  1247. DRVNAME, result);
  1248. put_tty_driver(tty_drv);
  1249. return result;
  1250. }
  1251. result = spi_register_driver((void *)&ifx_spi_driver);
  1252. if (result) {
  1253. pr_err("%s: spi_register_driver failed(%d)",
  1254. DRVNAME, result);
  1255. tty_unregister_driver(tty_drv);
  1256. }
  1257. return result;
  1258. }
  1259. module_init(ifx_spi_init);
  1260. module_exit(ifx_spi_exit);
  1261. MODULE_AUTHOR("Intel");
  1262. MODULE_DESCRIPTION("IFX6x60 spi driver");
  1263. MODULE_LICENSE("GPL");
  1264. MODULE_INFO(Version, "0.1-IFX6x60");