ifx6x60.c 39 KB

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