max3100.c 21 KB

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