ifx6x60.c 38 KB

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