ifx6x60.c 37 KB

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