ifx6x60.c 36 KB

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