ifx6x60.c 38 KB

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