ifx6x60.c 39 KB

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