max3100.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*
  2. *
  3. * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. *
  11. * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
  12. * to use polling for flow control. TX empty IRQ is unusable, since
  13. * writing conf clears FIFO buffer and we cannot have this interrupt
  14. * always asking us for attention.
  15. *
  16. * Example platform data:
  17. static struct plat_max3100 max3100_plat_data = {
  18. .loopback = 0,
  19. .crystal = 0,
  20. .poll_time = 100,
  21. };
  22. static struct spi_board_info spi_board_info[] = {
  23. {
  24. .modalias = "max3100",
  25. .platform_data = &max3100_plat_data,
  26. .irq = IRQ_EINT12,
  27. .max_speed_hz = 5*1000*1000,
  28. .chip_select = 0,
  29. },
  30. };
  31. * The initial minor number is 209 in the low-density serial port:
  32. * mknod /dev/ttyMAX0 c 204 209
  33. */
  34. #define MAX3100_MAJOR 204
  35. #define MAX3100_MINOR 209
  36. /* 4 MAX3100s should be enough for everyone */
  37. #define MAX_MAX3100 4
  38. #include <linux/delay.h>
  39. #include <linux/device.h>
  40. #include <linux/serial_core.h>
  41. #include <linux/serial.h>
  42. #include <linux/spi/spi.h>
  43. #include <linux/freezer.h>
  44. #include <linux/serial_max3100.h>
  45. #define MAX3100_C (1<<14)
  46. #define MAX3100_D (0<<14)
  47. #define MAX3100_W (1<<15)
  48. #define MAX3100_RX (0<<15)
  49. #define MAX3100_WC (MAX3100_W | MAX3100_C)
  50. #define MAX3100_RC (MAX3100_RX | MAX3100_C)
  51. #define MAX3100_WD (MAX3100_W | MAX3100_D)
  52. #define MAX3100_RD (MAX3100_RX | MAX3100_D)
  53. #define MAX3100_CMD (3 << 14)
  54. #define MAX3100_T (1<<14)
  55. #define MAX3100_R (1<<15)
  56. #define MAX3100_FEN (1<<13)
  57. #define MAX3100_SHDN (1<<12)
  58. #define MAX3100_TM (1<<11)
  59. #define MAX3100_RM (1<<10)
  60. #define MAX3100_PM (1<<9)
  61. #define MAX3100_RAM (1<<8)
  62. #define MAX3100_IR (1<<7)
  63. #define MAX3100_ST (1<<6)
  64. #define MAX3100_PE (1<<5)
  65. #define MAX3100_L (1<<4)
  66. #define MAX3100_BAUD (0xf)
  67. #define MAX3100_TE (1<<10)
  68. #define MAX3100_RAFE (1<<10)
  69. #define MAX3100_RTS (1<<9)
  70. #define MAX3100_CTS (1<<9)
  71. #define MAX3100_PT (1<<8)
  72. #define MAX3100_DATA (0xff)
  73. #define MAX3100_RT (MAX3100_R | MAX3100_T)
  74. #define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
  75. /* the following simulate a status reg for ignore_status_mask */
  76. #define MAX3100_STATUS_PE 1
  77. #define MAX3100_STATUS_FE 2
  78. #define MAX3100_STATUS_OE 4
  79. struct max3100_port {
  80. struct uart_port port;
  81. struct spi_device *spi;
  82. int cts; /* last CTS received for flow ctrl */
  83. int tx_empty; /* last TX empty bit */
  84. spinlock_t conf_lock; /* shared data */
  85. int conf_commit; /* need to make changes */
  86. int conf; /* configuration for the MAX31000
  87. * (bits 0-7, bits 8-11 are irqs) */
  88. int rts_commit; /* need to change rts */
  89. int rts; /* rts status */
  90. int baud; /* current baud rate */
  91. int parity; /* keeps track if we should send parity */
  92. #define MAX3100_PARITY_ON 1
  93. #define MAX3100_PARITY_ODD 2
  94. #define MAX3100_7BIT 4
  95. int rx_enabled; /* if we should rx chars */
  96. int irq; /* irq assigned to the max3100 */
  97. int minor; /* minor number */
  98. int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
  99. int loopback; /* 1 if we are in loopback mode */
  100. /* for handling irqs: need workqueue since we do spi_sync */
  101. struct workqueue_struct *workqueue;
  102. struct work_struct work;
  103. /* set to 1 to make the workhandler exit as soon as possible */
  104. int force_end_work;
  105. /* need to know we are suspending to avoid deadlock on workqueue */
  106. int suspending;
  107. /* hook for suspending MAX3100 via dedicated pin */
  108. void (*max3100_hw_suspend) (int suspend);
  109. /* poll time (in ms) for ctrl lines */
  110. int poll_time;
  111. /* and its timer */
  112. struct timer_list timer;
  113. };
  114. static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
  115. static DEFINE_MUTEX(max3100s_lock); /* race on probe */
  116. static int max3100_do_parity(struct max3100_port *s, u16 c)
  117. {
  118. int parity;
  119. if (s->parity & MAX3100_PARITY_ODD)
  120. parity = 1;
  121. else
  122. parity = 0;
  123. if (s->parity & MAX3100_7BIT)
  124. c &= 0x7f;
  125. else
  126. c &= 0xff;
  127. parity = parity ^ (hweight8(c) & 1);
  128. return parity;
  129. }
  130. static int max3100_check_parity(struct max3100_port *s, u16 c)
  131. {
  132. return max3100_do_parity(s, c) == ((c >> 8) & 1);
  133. }
  134. static void max3100_calc_parity(struct max3100_port *s, u16 *c)
  135. {
  136. if (s->parity & MAX3100_7BIT)
  137. *c &= 0x7f;
  138. else
  139. *c &= 0xff;
  140. if (s->parity & MAX3100_PARITY_ON)
  141. *c |= max3100_do_parity(s, *c) << 8;
  142. }
  143. static void max3100_work(struct work_struct *w);
  144. static void max3100_dowork(struct max3100_port *s)
  145. {
  146. if (!s->force_end_work && !work_pending(&s->work) &&
  147. !freezing(current) && !s->suspending)
  148. queue_work(s->workqueue, &s->work);
  149. }
  150. static void max3100_timeout(unsigned long data)
  151. {
  152. struct max3100_port *s = (struct max3100_port *)data;
  153. if (s->port.info) {
  154. max3100_dowork(s);
  155. mod_timer(&s->timer, jiffies + s->poll_time);
  156. }
  157. }
  158. static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
  159. {
  160. struct spi_message message;
  161. u16 etx, erx;
  162. int status;
  163. struct spi_transfer tran = {
  164. .tx_buf = &etx,
  165. .rx_buf = &erx,
  166. .len = 2,
  167. };
  168. etx = cpu_to_be16(tx);
  169. spi_message_init(&message);
  170. spi_message_add_tail(&tran, &message);
  171. status = spi_sync(s->spi, &message);
  172. if (status) {
  173. dev_warn(&s->spi->dev, "error while calling spi_sync\n");
  174. return -EIO;
  175. }
  176. *rx = be16_to_cpu(erx);
  177. s->tx_empty = (*rx & MAX3100_T) > 0;
  178. dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
  179. return 0;
  180. }
  181. static int max3100_handlerx(struct max3100_port *s, u16 rx)
  182. {
  183. unsigned int ch, flg, status = 0;
  184. int ret = 0, cts;
  185. if (rx & MAX3100_R && s->rx_enabled) {
  186. dev_dbg(&s->spi->dev, "%s\n", __func__);
  187. ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
  188. if (rx & MAX3100_RAFE) {
  189. s->port.icount.frame++;
  190. flg = TTY_FRAME;
  191. status |= MAX3100_STATUS_FE;
  192. } else {
  193. if (s->parity & MAX3100_PARITY_ON) {
  194. if (max3100_check_parity(s, rx)) {
  195. s->port.icount.rx++;
  196. flg = TTY_NORMAL;
  197. } else {
  198. s->port.icount.parity++;
  199. flg = TTY_PARITY;
  200. status |= MAX3100_STATUS_PE;
  201. }
  202. } else {
  203. s->port.icount.rx++;
  204. flg = TTY_NORMAL;
  205. }
  206. }
  207. uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
  208. ret = 1;
  209. }
  210. cts = (rx & MAX3100_CTS) > 0;
  211. if (s->cts != cts) {
  212. s->cts = cts;
  213. uart_handle_cts_change(&s->port, cts ? TIOCM_CTS : 0);
  214. }
  215. return ret;
  216. }
  217. static void max3100_work(struct work_struct *w)
  218. {
  219. struct max3100_port *s = container_of(w, struct max3100_port, work);
  220. int rxchars;
  221. u16 tx, rx;
  222. int conf, cconf, rts, crts;
  223. struct circ_buf *xmit = &s->port.info->xmit;
  224. dev_dbg(&s->spi->dev, "%s\n", __func__);
  225. rxchars = 0;
  226. do {
  227. spin_lock(&s->conf_lock);
  228. conf = s->conf;
  229. cconf = s->conf_commit;
  230. s->conf_commit = 0;
  231. rts = s->rts;
  232. crts = s->rts_commit;
  233. s->rts_commit = 0;
  234. spin_unlock(&s->conf_lock);
  235. if (cconf)
  236. max3100_sr(s, MAX3100_WC | conf, &rx);
  237. if (crts) {
  238. max3100_sr(s, MAX3100_WD | MAX3100_TE |
  239. (s->rts ? MAX3100_RTS : 0), &rx);
  240. rxchars += max3100_handlerx(s, rx);
  241. }
  242. max3100_sr(s, MAX3100_RD, &rx);
  243. rxchars += max3100_handlerx(s, rx);
  244. if (rx & MAX3100_T) {
  245. tx = 0xffff;
  246. if (s->port.x_char) {
  247. tx = s->port.x_char;
  248. s->port.icount.tx++;
  249. s->port.x_char = 0;
  250. } else if (!uart_circ_empty(xmit) &&
  251. !uart_tx_stopped(&s->port)) {
  252. tx = xmit->buf[xmit->tail];
  253. xmit->tail = (xmit->tail + 1) &
  254. (UART_XMIT_SIZE - 1);
  255. s->port.icount.tx++;
  256. }
  257. if (tx != 0xffff) {
  258. max3100_calc_parity(s, &tx);
  259. tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
  260. max3100_sr(s, tx, &rx);
  261. rxchars += max3100_handlerx(s, rx);
  262. }
  263. }
  264. if (rxchars > 16 && s->port.info->port.tty != NULL) {
  265. tty_flip_buffer_push(s->port.info->port.tty);
  266. rxchars = 0;
  267. }
  268. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  269. uart_write_wakeup(&s->port);
  270. } while (!s->force_end_work &&
  271. !freezing(current) &&
  272. ((rx & MAX3100_R) ||
  273. (!uart_circ_empty(xmit) &&
  274. !uart_tx_stopped(&s->port))));
  275. if (rxchars > 0 && s->port.info->port.tty != NULL)
  276. tty_flip_buffer_push(s->port.info->port.tty);
  277. }
  278. static irqreturn_t max3100_irq(int irqno, void *dev_id)
  279. {
  280. struct max3100_port *s = dev_id;
  281. dev_dbg(&s->spi->dev, "%s\n", __func__);
  282. max3100_dowork(s);
  283. return IRQ_HANDLED;
  284. }
  285. static void max3100_enable_ms(struct uart_port *port)
  286. {
  287. struct max3100_port *s = container_of(port,
  288. struct max3100_port,
  289. port);
  290. if (s->poll_time > 0)
  291. mod_timer(&s->timer, jiffies);
  292. dev_dbg(&s->spi->dev, "%s\n", __func__);
  293. }
  294. static void max3100_start_tx(struct uart_port *port)
  295. {
  296. struct max3100_port *s = container_of(port,
  297. struct max3100_port,
  298. port);
  299. dev_dbg(&s->spi->dev, "%s\n", __func__);
  300. max3100_dowork(s);
  301. }
  302. static void max3100_stop_rx(struct uart_port *port)
  303. {
  304. struct max3100_port *s = container_of(port,
  305. struct max3100_port,
  306. port);
  307. dev_dbg(&s->spi->dev, "%s\n", __func__);
  308. s->rx_enabled = 0;
  309. spin_lock(&s->conf_lock);
  310. s->conf &= ~MAX3100_RM;
  311. s->conf_commit = 1;
  312. spin_unlock(&s->conf_lock);
  313. max3100_dowork(s);
  314. }
  315. static unsigned int max3100_tx_empty(struct uart_port *port)
  316. {
  317. struct max3100_port *s = container_of(port,
  318. struct max3100_port,
  319. port);
  320. dev_dbg(&s->spi->dev, "%s\n", __func__);
  321. /* may not be truly up-to-date */
  322. max3100_dowork(s);
  323. return s->tx_empty;
  324. }
  325. static unsigned int max3100_get_mctrl(struct uart_port *port)
  326. {
  327. struct max3100_port *s = container_of(port,
  328. struct max3100_port,
  329. port);
  330. dev_dbg(&s->spi->dev, "%s\n", __func__);
  331. /* may not be truly up-to-date */
  332. max3100_dowork(s);
  333. /* always assert DCD and DSR since these lines are not wired */
  334. return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
  335. }
  336. static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
  337. {
  338. struct max3100_port *s = container_of(port,
  339. struct max3100_port,
  340. port);
  341. int rts;
  342. dev_dbg(&s->spi->dev, "%s\n", __func__);
  343. rts = (mctrl & TIOCM_RTS) > 0;
  344. spin_lock(&s->conf_lock);
  345. if (s->rts != rts) {
  346. s->rts = rts;
  347. s->rts_commit = 1;
  348. max3100_dowork(s);
  349. }
  350. spin_unlock(&s->conf_lock);
  351. }
  352. static void
  353. max3100_set_termios(struct uart_port *port, struct ktermios *termios,
  354. struct ktermios *old)
  355. {
  356. struct max3100_port *s = container_of(port,
  357. struct max3100_port,
  358. port);
  359. int baud = 0;
  360. unsigned cflag;
  361. u32 param_new, param_mask, parity = 0;
  362. struct tty_struct *tty = s->port.info->port.tty;
  363. dev_dbg(&s->spi->dev, "%s\n", __func__);
  364. if (!tty)
  365. return;
  366. cflag = termios->c_cflag;
  367. param_new = 0;
  368. param_mask = 0;
  369. baud = tty_get_baud_rate(tty);
  370. param_new = s->conf & MAX3100_BAUD;
  371. switch (baud) {
  372. case 300:
  373. if (s->crystal)
  374. baud = s->baud;
  375. else
  376. param_new = 15;
  377. break;
  378. case 600:
  379. param_new = 14 + s->crystal;
  380. break;
  381. case 1200:
  382. param_new = 13 + s->crystal;
  383. break;
  384. case 2400:
  385. param_new = 12 + s->crystal;
  386. break;
  387. case 4800:
  388. param_new = 11 + s->crystal;
  389. break;
  390. case 9600:
  391. param_new = 10 + s->crystal;
  392. break;
  393. case 19200:
  394. param_new = 9 + s->crystal;
  395. break;
  396. case 38400:
  397. param_new = 8 + s->crystal;
  398. break;
  399. case 57600:
  400. param_new = 1 + s->crystal;
  401. break;
  402. case 115200:
  403. param_new = 0 + s->crystal;
  404. break;
  405. case 230400:
  406. if (s->crystal)
  407. param_new = 0;
  408. else
  409. baud = s->baud;
  410. break;
  411. default:
  412. baud = s->baud;
  413. }
  414. tty_encode_baud_rate(tty, baud, baud);
  415. s->baud = baud;
  416. param_mask |= MAX3100_BAUD;
  417. if ((cflag & CSIZE) == CS8) {
  418. param_new &= ~MAX3100_L;
  419. parity &= ~MAX3100_7BIT;
  420. } else {
  421. param_new |= MAX3100_L;
  422. parity |= MAX3100_7BIT;
  423. cflag = (cflag & ~CSIZE) | CS7;
  424. }
  425. param_mask |= MAX3100_L;
  426. if (cflag & CSTOPB)
  427. param_new |= MAX3100_ST;
  428. else
  429. param_new &= ~MAX3100_ST;
  430. param_mask |= MAX3100_ST;
  431. if (cflag & PARENB) {
  432. param_new |= MAX3100_PE;
  433. parity |= MAX3100_PARITY_ON;
  434. } else {
  435. param_new &= ~MAX3100_PE;
  436. parity &= ~MAX3100_PARITY_ON;
  437. }
  438. param_mask |= MAX3100_PE;
  439. if (cflag & PARODD)
  440. parity |= MAX3100_PARITY_ODD;
  441. else
  442. parity &= ~MAX3100_PARITY_ODD;
  443. /* mask termios capabilities we don't support */
  444. cflag &= ~CMSPAR;
  445. termios->c_cflag = cflag;
  446. s->port.ignore_status_mask = 0;
  447. if (termios->c_iflag & IGNPAR)
  448. s->port.ignore_status_mask |=
  449. MAX3100_STATUS_PE | MAX3100_STATUS_FE |
  450. MAX3100_STATUS_OE;
  451. /* we are sending char from a workqueue so enable */
  452. s->port.info->port.tty->low_latency = 1;
  453. if (s->poll_time > 0)
  454. del_timer_sync(&s->timer);
  455. uart_update_timeout(port, termios->c_cflag, baud);
  456. spin_lock(&s->conf_lock);
  457. s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
  458. s->conf_commit = 1;
  459. s->parity = parity;
  460. spin_unlock(&s->conf_lock);
  461. max3100_dowork(s);
  462. if (UART_ENABLE_MS(&s->port, termios->c_cflag))
  463. max3100_enable_ms(&s->port);
  464. }
  465. static void max3100_shutdown(struct uart_port *port)
  466. {
  467. struct max3100_port *s = container_of(port,
  468. struct max3100_port,
  469. port);
  470. dev_dbg(&s->spi->dev, "%s\n", __func__);
  471. if (s->suspending)
  472. return;
  473. s->force_end_work = 1;
  474. if (s->poll_time > 0)
  475. del_timer_sync(&s->timer);
  476. if (s->workqueue) {
  477. flush_workqueue(s->workqueue);
  478. destroy_workqueue(s->workqueue);
  479. s->workqueue = NULL;
  480. }
  481. if (s->irq)
  482. free_irq(s->irq, s);
  483. /* set shutdown mode to save power */
  484. if (s->max3100_hw_suspend)
  485. s->max3100_hw_suspend(1);
  486. else {
  487. u16 tx, rx;
  488. tx = MAX3100_WC | MAX3100_SHDN;
  489. max3100_sr(s, tx, &rx);
  490. }
  491. }
  492. static int max3100_startup(struct uart_port *port)
  493. {
  494. struct max3100_port *s = container_of(port,
  495. struct max3100_port,
  496. port);
  497. char b[12];
  498. dev_dbg(&s->spi->dev, "%s\n", __func__);
  499. s->conf = MAX3100_RM;
  500. s->baud = s->crystal ? 230400 : 115200;
  501. s->rx_enabled = 1;
  502. if (s->suspending)
  503. return 0;
  504. s->force_end_work = 0;
  505. s->parity = 0;
  506. s->rts = 0;
  507. sprintf(b, "max3100-%d", s->minor);
  508. s->workqueue = create_freezeable_workqueue(b);
  509. if (!s->workqueue) {
  510. dev_warn(&s->spi->dev, "cannot create workqueue\n");
  511. return -EBUSY;
  512. }
  513. INIT_WORK(&s->work, max3100_work);
  514. if (request_irq(s->irq, max3100_irq,
  515. IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
  516. dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
  517. s->irq = 0;
  518. destroy_workqueue(s->workqueue);
  519. s->workqueue = NULL;
  520. return -EBUSY;
  521. }
  522. if (s->loopback) {
  523. u16 tx, rx;
  524. tx = 0x4001;
  525. max3100_sr(s, tx, &rx);
  526. }
  527. if (s->max3100_hw_suspend)
  528. s->max3100_hw_suspend(0);
  529. s->conf_commit = 1;
  530. max3100_dowork(s);
  531. /* wait for clock to settle */
  532. msleep(50);
  533. max3100_enable_ms(&s->port);
  534. return 0;
  535. }
  536. static const char *max3100_type(struct uart_port *port)
  537. {
  538. struct max3100_port *s = container_of(port,
  539. struct max3100_port,
  540. port);
  541. dev_dbg(&s->spi->dev, "%s\n", __func__);
  542. return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
  543. }
  544. static void max3100_release_port(struct uart_port *port)
  545. {
  546. struct max3100_port *s = container_of(port,
  547. struct max3100_port,
  548. port);
  549. dev_dbg(&s->spi->dev, "%s\n", __func__);
  550. }
  551. static void max3100_config_port(struct uart_port *port, int flags)
  552. {
  553. struct max3100_port *s = container_of(port,
  554. struct max3100_port,
  555. port);
  556. dev_dbg(&s->spi->dev, "%s\n", __func__);
  557. if (flags & UART_CONFIG_TYPE)
  558. s->port.type = PORT_MAX3100;
  559. }
  560. static int max3100_verify_port(struct uart_port *port,
  561. struct serial_struct *ser)
  562. {
  563. struct max3100_port *s = container_of(port,
  564. struct max3100_port,
  565. port);
  566. int ret = -EINVAL;
  567. dev_dbg(&s->spi->dev, "%s\n", __func__);
  568. if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
  569. ret = 0;
  570. return ret;
  571. }
  572. static void max3100_stop_tx(struct uart_port *port)
  573. {
  574. struct max3100_port *s = container_of(port,
  575. struct max3100_port,
  576. port);
  577. dev_dbg(&s->spi->dev, "%s\n", __func__);
  578. }
  579. static int max3100_request_port(struct uart_port *port)
  580. {
  581. struct max3100_port *s = container_of(port,
  582. struct max3100_port,
  583. port);
  584. dev_dbg(&s->spi->dev, "%s\n", __func__);
  585. return 0;
  586. }
  587. static void max3100_break_ctl(struct uart_port *port, int break_state)
  588. {
  589. struct max3100_port *s = container_of(port,
  590. struct max3100_port,
  591. port);
  592. dev_dbg(&s->spi->dev, "%s\n", __func__);
  593. }
  594. static struct uart_ops max3100_ops = {
  595. .tx_empty = max3100_tx_empty,
  596. .set_mctrl = max3100_set_mctrl,
  597. .get_mctrl = max3100_get_mctrl,
  598. .stop_tx = max3100_stop_tx,
  599. .start_tx = max3100_start_tx,
  600. .stop_rx = max3100_stop_rx,
  601. .enable_ms = max3100_enable_ms,
  602. .break_ctl = max3100_break_ctl,
  603. .startup = max3100_startup,
  604. .shutdown = max3100_shutdown,
  605. .set_termios = max3100_set_termios,
  606. .type = max3100_type,
  607. .release_port = max3100_release_port,
  608. .request_port = max3100_request_port,
  609. .config_port = max3100_config_port,
  610. .verify_port = max3100_verify_port,
  611. };
  612. static struct uart_driver max3100_uart_driver = {
  613. .owner = THIS_MODULE,
  614. .driver_name = "ttyMAX",
  615. .dev_name = "ttyMAX",
  616. .major = MAX3100_MAJOR,
  617. .minor = MAX3100_MINOR,
  618. .nr = MAX_MAX3100,
  619. };
  620. static int uart_driver_registered;
  621. static int __devinit max3100_probe(struct spi_device *spi)
  622. {
  623. int i, retval;
  624. struct plat_max3100 *pdata;
  625. u16 tx, rx;
  626. mutex_lock(&max3100s_lock);
  627. if (!uart_driver_registered) {
  628. uart_driver_registered = 1;
  629. retval = uart_register_driver(&max3100_uart_driver);
  630. if (retval) {
  631. printk(KERN_ERR "Couldn't register max3100 uart driver\n");
  632. mutex_unlock(&max3100s_lock);
  633. return retval;
  634. }
  635. }
  636. for (i = 0; i < MAX_MAX3100; i++)
  637. if (!max3100s[i])
  638. break;
  639. if (i == MAX_MAX3100) {
  640. dev_warn(&spi->dev, "too many MAX3100 chips\n");
  641. mutex_unlock(&max3100s_lock);
  642. return -ENOMEM;
  643. }
  644. max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
  645. if (!max3100s[i]) {
  646. dev_warn(&spi->dev,
  647. "kmalloc for max3100 structure %d failed!\n", i);
  648. mutex_unlock(&max3100s_lock);
  649. return -ENOMEM;
  650. }
  651. max3100s[i]->spi = spi;
  652. max3100s[i]->irq = spi->irq;
  653. spin_lock_init(&max3100s[i]->conf_lock);
  654. dev_set_drvdata(&spi->dev, max3100s[i]);
  655. pdata = spi->dev.platform_data;
  656. max3100s[i]->crystal = pdata->crystal;
  657. max3100s[i]->loopback = pdata->loopback;
  658. max3100s[i]->poll_time = pdata->poll_time * HZ / 1000;
  659. if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
  660. max3100s[i]->poll_time = 1;
  661. max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
  662. max3100s[i]->minor = i;
  663. init_timer(&max3100s[i]->timer);
  664. max3100s[i]->timer.function = max3100_timeout;
  665. max3100s[i]->timer.data = (unsigned long) max3100s[i];
  666. dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
  667. max3100s[i]->port.irq = max3100s[i]->irq;
  668. max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
  669. max3100s[i]->port.fifosize = 16;
  670. max3100s[i]->port.ops = &max3100_ops;
  671. max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
  672. max3100s[i]->port.line = i;
  673. max3100s[i]->port.type = PORT_MAX3100;
  674. max3100s[i]->port.dev = &spi->dev;
  675. retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
  676. if (retval < 0)
  677. dev_warn(&spi->dev,
  678. "uart_add_one_port failed for line %d with error %d\n",
  679. i, retval);
  680. /* set shutdown mode to save power. Will be woken-up on open */
  681. if (max3100s[i]->max3100_hw_suspend)
  682. max3100s[i]->max3100_hw_suspend(1);
  683. else {
  684. tx = MAX3100_WC | MAX3100_SHDN;
  685. max3100_sr(max3100s[i], tx, &rx);
  686. }
  687. mutex_unlock(&max3100s_lock);
  688. return 0;
  689. }
  690. static int __devexit max3100_remove(struct spi_device *spi)
  691. {
  692. struct max3100_port *s = dev_get_drvdata(&spi->dev);
  693. int i;
  694. mutex_lock(&max3100s_lock);
  695. /* find out the index for the chip we are removing */
  696. for (i = 0; i < MAX_MAX3100; i++)
  697. if (max3100s[i] == s)
  698. break;
  699. dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
  700. uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
  701. kfree(max3100s[i]);
  702. max3100s[i] = NULL;
  703. /* check if this is the last chip we have */
  704. for (i = 0; i < MAX_MAX3100; i++)
  705. if (max3100s[i]) {
  706. mutex_unlock(&max3100s_lock);
  707. return 0;
  708. }
  709. pr_debug("removing max3100 driver\n");
  710. uart_unregister_driver(&max3100_uart_driver);
  711. mutex_unlock(&max3100s_lock);
  712. return 0;
  713. }
  714. #ifdef CONFIG_PM
  715. static int max3100_suspend(struct spi_device *spi, pm_message_t state)
  716. {
  717. struct max3100_port *s = dev_get_drvdata(&spi->dev);
  718. dev_dbg(&s->spi->dev, "%s\n", __func__);
  719. disable_irq(s->irq);
  720. s->suspending = 1;
  721. uart_suspend_port(&max3100_uart_driver, &s->port);
  722. if (s->max3100_hw_suspend)
  723. s->max3100_hw_suspend(1);
  724. else {
  725. /* no HW suspend, so do SW one */
  726. u16 tx, rx;
  727. tx = MAX3100_WC | MAX3100_SHDN;
  728. max3100_sr(s, tx, &rx);
  729. }
  730. return 0;
  731. }
  732. static int max3100_resume(struct spi_device *spi)
  733. {
  734. struct max3100_port *s = dev_get_drvdata(&spi->dev);
  735. dev_dbg(&s->spi->dev, "%s\n", __func__);
  736. if (s->max3100_hw_suspend)
  737. s->max3100_hw_suspend(0);
  738. uart_resume_port(&max3100_uart_driver, &s->port);
  739. s->suspending = 0;
  740. enable_irq(s->irq);
  741. s->conf_commit = 1;
  742. if (s->workqueue)
  743. max3100_dowork(s);
  744. return 0;
  745. }
  746. #else
  747. #define max3100_suspend NULL
  748. #define max3100_resume NULL
  749. #endif
  750. static struct spi_driver max3100_driver = {
  751. .driver = {
  752. .name = "max3100",
  753. .bus = &spi_bus_type,
  754. .owner = THIS_MODULE,
  755. },
  756. .probe = max3100_probe,
  757. .remove = __devexit_p(max3100_remove),
  758. .suspend = max3100_suspend,
  759. .resume = max3100_resume,
  760. };
  761. static int __init max3100_init(void)
  762. {
  763. return spi_register_driver(&max3100_driver);
  764. }
  765. module_init(max3100_init);
  766. static void __exit max3100_exit(void)
  767. {
  768. spi_unregister_driver(&max3100_driver);
  769. }
  770. module_exit(max3100_exit);
  771. MODULE_DESCRIPTION("MAX3100 driver");
  772. MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
  773. MODULE_LICENSE("GPL");