ifx6x60.c 38 KB

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