ifx6x60.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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 =
  729. ifx_dev->spi_dev->bits_per_word;
  730. ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
  731. ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
  732. /*
  733. * setup dma pointers
  734. */
  735. if (ifx_dev->use_dma) {
  736. ifx_dev->spi_msg.is_dma_mapped = 1;
  737. ifx_dev->tx_dma = ifx_dev->tx_bus;
  738. ifx_dev->rx_dma = ifx_dev->rx_bus;
  739. ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
  740. ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
  741. } else {
  742. ifx_dev->spi_msg.is_dma_mapped = 0;
  743. ifx_dev->tx_dma = (dma_addr_t)0;
  744. ifx_dev->rx_dma = (dma_addr_t)0;
  745. ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
  746. ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
  747. }
  748. spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
  749. /* Assert MRDY. This may have already been done by the write
  750. * routine.
  751. */
  752. mrdy_assert(ifx_dev);
  753. retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
  754. if (retval) {
  755. clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
  756. &ifx_dev->flags);
  757. tasklet_schedule(&ifx_dev->io_work_tasklet);
  758. return;
  759. }
  760. } else
  761. ifx_dev->write_pending = 1;
  762. }
  763. /**
  764. * ifx_spi_free_port - free up the tty side
  765. * @ifx_dev: IFX device going away
  766. *
  767. * Unregister and free up a port when the device goes away
  768. */
  769. static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
  770. {
  771. if (ifx_dev->tty_dev)
  772. tty_unregister_device(tty_drv, ifx_dev->minor);
  773. tty_port_destroy(&ifx_dev->tty_port);
  774. kfifo_free(&ifx_dev->tx_fifo);
  775. }
  776. /**
  777. * ifx_spi_create_port - create a new port
  778. * @ifx_dev: our spi device
  779. *
  780. * Allocate and initialise the tty port that goes with this interface
  781. * and add it to the tty layer so that it can be opened.
  782. */
  783. static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
  784. {
  785. int ret = 0;
  786. struct tty_port *pport = &ifx_dev->tty_port;
  787. spin_lock_init(&ifx_dev->fifo_lock);
  788. lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
  789. &ifx_spi_key, 0);
  790. if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
  791. ret = -ENOMEM;
  792. goto error_ret;
  793. }
  794. tty_port_init(pport);
  795. pport->ops = &ifx_tty_port_ops;
  796. ifx_dev->minor = IFX_SPI_TTY_ID;
  797. ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
  798. ifx_dev->minor, &ifx_dev->spi_dev->dev);
  799. if (IS_ERR(ifx_dev->tty_dev)) {
  800. dev_dbg(&ifx_dev->spi_dev->dev,
  801. "%s: registering tty device failed", __func__);
  802. ret = PTR_ERR(ifx_dev->tty_dev);
  803. goto error_port;
  804. }
  805. return 0;
  806. error_port:
  807. tty_port_destroy(pport);
  808. error_ret:
  809. ifx_spi_free_port(ifx_dev);
  810. return ret;
  811. }
  812. /**
  813. * ifx_spi_handle_srdy - handle SRDY
  814. * @ifx_dev: device asserting SRDY
  815. *
  816. * Check our device state and see what we need to kick off when SRDY
  817. * is asserted. This usually means killing the timer and firing off the
  818. * I/O processing.
  819. */
  820. static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
  821. {
  822. if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
  823. del_timer(&ifx_dev->spi_timer);
  824. clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
  825. }
  826. ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
  827. if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
  828. tasklet_schedule(&ifx_dev->io_work_tasklet);
  829. else
  830. set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
  831. }
  832. /**
  833. * ifx_spi_srdy_interrupt - SRDY asserted
  834. * @irq: our IRQ number
  835. * @dev: our ifx device
  836. *
  837. * The modem asserted SRDY. Handle the srdy event
  838. */
  839. static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
  840. {
  841. struct ifx_spi_device *ifx_dev = dev;
  842. ifx_dev->gpio.unack_srdy_int_nb++;
  843. ifx_spi_handle_srdy(ifx_dev);
  844. return IRQ_HANDLED;
  845. }
  846. /**
  847. * ifx_spi_reset_interrupt - Modem has changed reset state
  848. * @irq: interrupt number
  849. * @dev: our device pointer
  850. *
  851. * The modem has either entered or left reset state. Check the GPIO
  852. * line to see which.
  853. *
  854. * FIXME: review locking on MR_INPROGRESS versus
  855. * parallel unsolicited reset/solicited reset
  856. */
  857. static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
  858. {
  859. struct ifx_spi_device *ifx_dev = dev;
  860. int val = gpio_get_value(ifx_dev->gpio.reset_out);
  861. int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
  862. if (val == 0) {
  863. /* entered reset */
  864. set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
  865. if (!solreset) {
  866. /* unsolicited reset */
  867. ifx_spi_ttyhangup(ifx_dev);
  868. }
  869. } else {
  870. /* exited reset */
  871. clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
  872. if (solreset) {
  873. set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
  874. wake_up(&ifx_dev->mdm_reset_wait);
  875. }
  876. }
  877. return IRQ_HANDLED;
  878. }
  879. /**
  880. * ifx_spi_free_device - free device
  881. * @ifx_dev: device to free
  882. *
  883. * Free the IFX device
  884. */
  885. static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
  886. {
  887. ifx_spi_free_port(ifx_dev);
  888. dma_free_coherent(&ifx_dev->spi_dev->dev,
  889. IFX_SPI_TRANSFER_SIZE,
  890. ifx_dev->tx_buffer,
  891. ifx_dev->tx_bus);
  892. dma_free_coherent(&ifx_dev->spi_dev->dev,
  893. IFX_SPI_TRANSFER_SIZE,
  894. ifx_dev->rx_buffer,
  895. ifx_dev->rx_bus);
  896. }
  897. /**
  898. * ifx_spi_reset - reset modem
  899. * @ifx_dev: modem to reset
  900. *
  901. * Perform a reset on the modem
  902. */
  903. static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
  904. {
  905. int ret;
  906. /*
  907. * set up modem power, reset
  908. *
  909. * delays are required on some platforms for the modem
  910. * to reset properly
  911. */
  912. set_bit(MR_START, &ifx_dev->mdm_reset_state);
  913. gpio_set_value(ifx_dev->gpio.po, 0);
  914. gpio_set_value(ifx_dev->gpio.reset, 0);
  915. msleep(25);
  916. gpio_set_value(ifx_dev->gpio.reset, 1);
  917. msleep(1);
  918. gpio_set_value(ifx_dev->gpio.po, 1);
  919. msleep(1);
  920. gpio_set_value(ifx_dev->gpio.po, 0);
  921. ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
  922. test_bit(MR_COMPLETE,
  923. &ifx_dev->mdm_reset_state),
  924. IFX_RESET_TIMEOUT);
  925. if (!ret)
  926. dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
  927. ifx_dev->mdm_reset_state);
  928. ifx_dev->mdm_reset_state = 0;
  929. return ret;
  930. }
  931. /**
  932. * ifx_spi_spi_probe - probe callback
  933. * @spi: our possible matching SPI device
  934. *
  935. * Probe for a 6x60 modem on SPI bus. Perform any needed device and
  936. * GPIO setup.
  937. *
  938. * FIXME:
  939. * - Support for multiple devices
  940. * - Split out MID specific GPIO handling eventually
  941. */
  942. static int ifx_spi_spi_probe(struct spi_device *spi)
  943. {
  944. int ret;
  945. int srdy;
  946. struct ifx_modem_platform_data *pl_data;
  947. struct ifx_spi_device *ifx_dev;
  948. if (saved_ifx_dev) {
  949. dev_dbg(&spi->dev, "ignoring subsequent detection");
  950. return -ENODEV;
  951. }
  952. pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
  953. if (!pl_data) {
  954. dev_err(&spi->dev, "missing platform data!");
  955. return -ENODEV;
  956. }
  957. /* initialize structure to hold our device variables */
  958. ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
  959. if (!ifx_dev) {
  960. dev_err(&spi->dev, "spi device allocation failed");
  961. return -ENOMEM;
  962. }
  963. saved_ifx_dev = ifx_dev;
  964. ifx_dev->spi_dev = spi;
  965. clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
  966. spin_lock_init(&ifx_dev->write_lock);
  967. spin_lock_init(&ifx_dev->power_lock);
  968. ifx_dev->power_status = 0;
  969. init_timer(&ifx_dev->spi_timer);
  970. ifx_dev->spi_timer.function = ifx_spi_timeout;
  971. ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
  972. ifx_dev->modem = pl_data->modem_type;
  973. ifx_dev->use_dma = pl_data->use_dma;
  974. ifx_dev->max_hz = pl_data->max_hz;
  975. /* initialize spi mode, etc */
  976. spi->max_speed_hz = ifx_dev->max_hz;
  977. spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
  978. spi->bits_per_word = spi_bpw;
  979. ret = spi_setup(spi);
  980. if (ret) {
  981. dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
  982. return -ENODEV;
  983. }
  984. /* init swap_buf function according to word width configuration */
  985. if (spi->bits_per_word == 32)
  986. ifx_dev->swap_buf = swap_buf_32;
  987. else if (spi->bits_per_word == 16)
  988. ifx_dev->swap_buf = swap_buf_16;
  989. else
  990. ifx_dev->swap_buf = swap_buf_8;
  991. /* ensure SPI protocol flags are initialized to enable transfer */
  992. ifx_dev->spi_more = 0;
  993. ifx_dev->spi_slave_cts = 0;
  994. /*initialize transfer and dma buffers */
  995. ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
  996. IFX_SPI_TRANSFER_SIZE,
  997. &ifx_dev->tx_bus,
  998. GFP_KERNEL);
  999. if (!ifx_dev->tx_buffer) {
  1000. dev_err(&spi->dev, "DMA-TX buffer allocation failed");
  1001. ret = -ENOMEM;
  1002. goto error_ret;
  1003. }
  1004. ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
  1005. IFX_SPI_TRANSFER_SIZE,
  1006. &ifx_dev->rx_bus,
  1007. GFP_KERNEL);
  1008. if (!ifx_dev->rx_buffer) {
  1009. dev_err(&spi->dev, "DMA-RX buffer allocation failed");
  1010. ret = -ENOMEM;
  1011. goto error_ret;
  1012. }
  1013. /* initialize waitq for modem reset */
  1014. init_waitqueue_head(&ifx_dev->mdm_reset_wait);
  1015. spi_set_drvdata(spi, ifx_dev);
  1016. tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
  1017. (unsigned long)ifx_dev);
  1018. set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
  1019. /* create our tty port */
  1020. ret = ifx_spi_create_port(ifx_dev);
  1021. if (ret != 0) {
  1022. dev_err(&spi->dev, "create default tty port failed");
  1023. goto error_ret;
  1024. }
  1025. ifx_dev->gpio.reset = pl_data->rst_pmu;
  1026. ifx_dev->gpio.po = pl_data->pwr_on;
  1027. ifx_dev->gpio.mrdy = pl_data->mrdy;
  1028. ifx_dev->gpio.srdy = pl_data->srdy;
  1029. ifx_dev->gpio.reset_out = pl_data->rst_out;
  1030. dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
  1031. ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
  1032. ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
  1033. /* Configure gpios */
  1034. ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
  1035. if (ret < 0) {
  1036. dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
  1037. ifx_dev->gpio.reset);
  1038. goto error_ret;
  1039. }
  1040. ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
  1041. ret += gpio_export(ifx_dev->gpio.reset, 1);
  1042. if (ret) {
  1043. dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
  1044. ifx_dev->gpio.reset);
  1045. ret = -EBUSY;
  1046. goto error_ret2;
  1047. }
  1048. ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
  1049. ret += gpio_direction_output(ifx_dev->gpio.po, 0);
  1050. ret += gpio_export(ifx_dev->gpio.po, 1);
  1051. if (ret) {
  1052. dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
  1053. ifx_dev->gpio.po);
  1054. ret = -EBUSY;
  1055. goto error_ret3;
  1056. }
  1057. ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
  1058. if (ret < 0) {
  1059. dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
  1060. ifx_dev->gpio.mrdy);
  1061. goto error_ret3;
  1062. }
  1063. ret += gpio_export(ifx_dev->gpio.mrdy, 1);
  1064. ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
  1065. if (ret) {
  1066. dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
  1067. ifx_dev->gpio.mrdy);
  1068. ret = -EBUSY;
  1069. goto error_ret4;
  1070. }
  1071. ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
  1072. if (ret < 0) {
  1073. dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
  1074. ifx_dev->gpio.srdy);
  1075. ret = -EBUSY;
  1076. goto error_ret4;
  1077. }
  1078. ret += gpio_export(ifx_dev->gpio.srdy, 1);
  1079. ret += gpio_direction_input(ifx_dev->gpio.srdy);
  1080. if (ret) {
  1081. dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
  1082. ifx_dev->gpio.srdy);
  1083. ret = -EBUSY;
  1084. goto error_ret5;
  1085. }
  1086. ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
  1087. if (ret < 0) {
  1088. dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
  1089. ifx_dev->gpio.reset_out);
  1090. goto error_ret5;
  1091. }
  1092. ret += gpio_export(ifx_dev->gpio.reset_out, 1);
  1093. ret += gpio_direction_input(ifx_dev->gpio.reset_out);
  1094. if (ret) {
  1095. dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
  1096. ifx_dev->gpio.reset_out);
  1097. ret = -EBUSY;
  1098. goto error_ret6;
  1099. }
  1100. ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
  1101. ifx_spi_reset_interrupt,
  1102. IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
  1103. (void *)ifx_dev);
  1104. if (ret) {
  1105. dev_err(&spi->dev, "Unable to get irq %x\n",
  1106. gpio_to_irq(ifx_dev->gpio.reset_out));
  1107. goto error_ret6;
  1108. }
  1109. ret = ifx_spi_reset(ifx_dev);
  1110. ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
  1111. ifx_spi_srdy_interrupt,
  1112. IRQF_TRIGGER_RISING, DRVNAME,
  1113. (void *)ifx_dev);
  1114. if (ret) {
  1115. dev_err(&spi->dev, "Unable to get irq %x",
  1116. gpio_to_irq(ifx_dev->gpio.srdy));
  1117. goto error_ret7;
  1118. }
  1119. /* set pm runtime power state and register with power system */
  1120. pm_runtime_set_active(&spi->dev);
  1121. pm_runtime_enable(&spi->dev);
  1122. /* handle case that modem is already signaling SRDY */
  1123. /* no outgoing tty open at this point, this just satisfies the
  1124. * modem's read and should reset communication properly
  1125. */
  1126. srdy = gpio_get_value(ifx_dev->gpio.srdy);
  1127. if (srdy) {
  1128. mrdy_assert(ifx_dev);
  1129. ifx_spi_handle_srdy(ifx_dev);
  1130. } else
  1131. mrdy_set_low(ifx_dev);
  1132. return 0;
  1133. error_ret7:
  1134. free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
  1135. error_ret6:
  1136. gpio_free(ifx_dev->gpio.srdy);
  1137. error_ret5:
  1138. gpio_free(ifx_dev->gpio.mrdy);
  1139. error_ret4:
  1140. gpio_free(ifx_dev->gpio.reset);
  1141. error_ret3:
  1142. gpio_free(ifx_dev->gpio.po);
  1143. error_ret2:
  1144. gpio_free(ifx_dev->gpio.reset_out);
  1145. error_ret:
  1146. ifx_spi_free_device(ifx_dev);
  1147. saved_ifx_dev = NULL;
  1148. return ret;
  1149. }
  1150. /**
  1151. * ifx_spi_spi_remove - SPI device was removed
  1152. * @spi: SPI device
  1153. *
  1154. * FIXME: We should be shutting the device down here not in
  1155. * the module unload path.
  1156. */
  1157. static int ifx_spi_spi_remove(struct spi_device *spi)
  1158. {
  1159. struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
  1160. /* stop activity */
  1161. tasklet_kill(&ifx_dev->io_work_tasklet);
  1162. /* free irq */
  1163. free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
  1164. free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
  1165. gpio_free(ifx_dev->gpio.srdy);
  1166. gpio_free(ifx_dev->gpio.mrdy);
  1167. gpio_free(ifx_dev->gpio.reset);
  1168. gpio_free(ifx_dev->gpio.po);
  1169. gpio_free(ifx_dev->gpio.reset_out);
  1170. /* free allocations */
  1171. ifx_spi_free_device(ifx_dev);
  1172. saved_ifx_dev = NULL;
  1173. return 0;
  1174. }
  1175. /**
  1176. * ifx_spi_spi_shutdown - called on SPI shutdown
  1177. * @spi: SPI device
  1178. *
  1179. * No action needs to be taken here
  1180. */
  1181. static void ifx_spi_spi_shutdown(struct spi_device *spi)
  1182. {
  1183. struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
  1184. ifx_modem_power_off(ifx_dev);
  1185. }
  1186. /*
  1187. * various suspends and resumes have nothing to do
  1188. * no hardware to save state for
  1189. */
  1190. /**
  1191. * ifx_spi_spi_suspend - suspend SPI on system suspend
  1192. * @dev: device being suspended
  1193. *
  1194. * Suspend the SPI side. No action needed on Intel MID platforms, may
  1195. * need extending for other systems.
  1196. */
  1197. static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
  1198. {
  1199. return 0;
  1200. }
  1201. /**
  1202. * ifx_spi_spi_resume - resume SPI side on system resume
  1203. * @dev: device being suspended
  1204. *
  1205. * Suspend the SPI side. No action needed on Intel MID platforms, may
  1206. * need extending for other systems.
  1207. */
  1208. static int ifx_spi_spi_resume(struct spi_device *spi)
  1209. {
  1210. return 0;
  1211. }
  1212. /**
  1213. * ifx_spi_pm_suspend - suspend modem on system suspend
  1214. * @dev: device being suspended
  1215. *
  1216. * Suspend the modem. No action needed on Intel MID platforms, may
  1217. * need extending for other systems.
  1218. */
  1219. static int ifx_spi_pm_suspend(struct device *dev)
  1220. {
  1221. return 0;
  1222. }
  1223. /**
  1224. * ifx_spi_pm_resume - resume modem on system resume
  1225. * @dev: device being suspended
  1226. *
  1227. * Allow the modem to resume. No action needed.
  1228. *
  1229. * FIXME: do we need to reset anything here ?
  1230. */
  1231. static int ifx_spi_pm_resume(struct device *dev)
  1232. {
  1233. return 0;
  1234. }
  1235. /**
  1236. * ifx_spi_pm_runtime_resume - suspend modem
  1237. * @dev: device being suspended
  1238. *
  1239. * Allow the modem to resume. No action needed.
  1240. */
  1241. static int ifx_spi_pm_runtime_resume(struct device *dev)
  1242. {
  1243. return 0;
  1244. }
  1245. /**
  1246. * ifx_spi_pm_runtime_suspend - suspend modem
  1247. * @dev: device being suspended
  1248. *
  1249. * Allow the modem to suspend and thus suspend to continue up the
  1250. * device tree.
  1251. */
  1252. static int ifx_spi_pm_runtime_suspend(struct device *dev)
  1253. {
  1254. return 0;
  1255. }
  1256. /**
  1257. * ifx_spi_pm_runtime_idle - check if modem idle
  1258. * @dev: our device
  1259. *
  1260. * Check conditions and queue runtime suspend if idle.
  1261. */
  1262. static int ifx_spi_pm_runtime_idle(struct device *dev)
  1263. {
  1264. struct spi_device *spi = to_spi_device(dev);
  1265. struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
  1266. if (!ifx_dev->power_status)
  1267. pm_runtime_suspend(dev);
  1268. return 0;
  1269. }
  1270. static const struct dev_pm_ops ifx_spi_pm = {
  1271. .resume = ifx_spi_pm_resume,
  1272. .suspend = ifx_spi_pm_suspend,
  1273. .runtime_resume = ifx_spi_pm_runtime_resume,
  1274. .runtime_suspend = ifx_spi_pm_runtime_suspend,
  1275. .runtime_idle = ifx_spi_pm_runtime_idle
  1276. };
  1277. static const struct spi_device_id ifx_id_table[] = {
  1278. {"ifx6160", 0},
  1279. {"ifx6260", 0},
  1280. { }
  1281. };
  1282. MODULE_DEVICE_TABLE(spi, ifx_id_table);
  1283. /* spi operations */
  1284. static struct spi_driver ifx_spi_driver = {
  1285. .driver = {
  1286. .name = DRVNAME,
  1287. .pm = &ifx_spi_pm,
  1288. .owner = THIS_MODULE},
  1289. .probe = ifx_spi_spi_probe,
  1290. .shutdown = ifx_spi_spi_shutdown,
  1291. .remove = ifx_spi_spi_remove,
  1292. .suspend = ifx_spi_spi_suspend,
  1293. .resume = ifx_spi_spi_resume,
  1294. .id_table = ifx_id_table
  1295. };
  1296. /**
  1297. * ifx_spi_exit - module exit
  1298. *
  1299. * Unload the module.
  1300. */
  1301. static void __exit ifx_spi_exit(void)
  1302. {
  1303. /* unregister */
  1304. tty_unregister_driver(tty_drv);
  1305. put_tty_driver(tty_drv);
  1306. spi_unregister_driver((void *)&ifx_spi_driver);
  1307. unregister_reboot_notifier(&ifx_modem_reboot_notifier_block);
  1308. }
  1309. /**
  1310. * ifx_spi_init - module entry point
  1311. *
  1312. * Initialise the SPI and tty interfaces for the IFX SPI driver
  1313. * We need to initialize upper-edge spi driver after the tty
  1314. * driver because otherwise the spi probe will race
  1315. */
  1316. static int __init ifx_spi_init(void)
  1317. {
  1318. int result;
  1319. tty_drv = alloc_tty_driver(1);
  1320. if (!tty_drv) {
  1321. pr_err("%s: alloc_tty_driver failed", DRVNAME);
  1322. return -ENOMEM;
  1323. }
  1324. tty_drv->driver_name = DRVNAME;
  1325. tty_drv->name = TTYNAME;
  1326. tty_drv->minor_start = IFX_SPI_TTY_ID;
  1327. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  1328. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  1329. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1330. tty_drv->init_termios = tty_std_termios;
  1331. tty_set_operations(tty_drv, &ifx_spi_serial_ops);
  1332. result = tty_register_driver(tty_drv);
  1333. if (result) {
  1334. pr_err("%s: tty_register_driver failed(%d)",
  1335. DRVNAME, result);
  1336. goto err_free_tty;
  1337. }
  1338. result = spi_register_driver((void *)&ifx_spi_driver);
  1339. if (result) {
  1340. pr_err("%s: spi_register_driver failed(%d)",
  1341. DRVNAME, result);
  1342. goto err_unreg_tty;
  1343. }
  1344. result = register_reboot_notifier(&ifx_modem_reboot_notifier_block);
  1345. if (result) {
  1346. pr_err("%s: register ifx modem reboot notifier failed(%d)",
  1347. DRVNAME, result);
  1348. goto err_unreg_spi;
  1349. }
  1350. return 0;
  1351. err_unreg_spi:
  1352. spi_unregister_driver((void *)&ifx_spi_driver);
  1353. err_unreg_tty:
  1354. tty_unregister_driver(tty_drv);
  1355. err_free_tty:
  1356. put_tty_driver(tty_drv);
  1357. return result;
  1358. }
  1359. module_init(ifx_spi_init);
  1360. module_exit(ifx_spi_exit);
  1361. MODULE_AUTHOR("Intel");
  1362. MODULE_DESCRIPTION("IFX6x60 spi driver");
  1363. MODULE_LICENSE("GPL");
  1364. MODULE_INFO(Version, "0.1-IFX6x60");