sc16is7xx.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * SC16IS7xx tty serial driver - Copyright (C) 2014 GridPoint
  3. * Author: Jon Ringle <jringle@gridpoint.com>
  4. *
  5. * Based on max310x.c, by Alexander Shiyan <shc_work@mail.ru>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/gpio.h>
  18. #include <linux/i2c.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/serial_core.h>
  24. #include <linux/serial.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/uaccess.h>
  29. #define SC16IS7XX_NAME "sc16is7xx"
  30. /* SC16IS7XX register definitions */
  31. #define SC16IS7XX_RHR_REG (0x00) /* RX FIFO */
  32. #define SC16IS7XX_THR_REG (0x00) /* TX FIFO */
  33. #define SC16IS7XX_IER_REG (0x01) /* Interrupt enable */
  34. #define SC16IS7XX_IIR_REG (0x02) /* Interrupt Identification */
  35. #define SC16IS7XX_FCR_REG (0x02) /* FIFO control */
  36. #define SC16IS7XX_LCR_REG (0x03) /* Line Control */
  37. #define SC16IS7XX_MCR_REG (0x04) /* Modem Control */
  38. #define SC16IS7XX_LSR_REG (0x05) /* Line Status */
  39. #define SC16IS7XX_MSR_REG (0x06) /* Modem Status */
  40. #define SC16IS7XX_SPR_REG (0x07) /* Scratch Pad */
  41. #define SC16IS7XX_TXLVL_REG (0x08) /* TX FIFO level */
  42. #define SC16IS7XX_RXLVL_REG (0x09) /* RX FIFO level */
  43. #define SC16IS7XX_IODIR_REG (0x0a) /* I/O Direction
  44. * - only on 75x/76x
  45. */
  46. #define SC16IS7XX_IOSTATE_REG (0x0b) /* I/O State
  47. * - only on 75x/76x
  48. */
  49. #define SC16IS7XX_IOINTENA_REG (0x0c) /* I/O Interrupt Enable
  50. * - only on 75x/76x
  51. */
  52. #define SC16IS7XX_IOCONTROL_REG (0x0e) /* I/O Control
  53. * - only on 75x/76x
  54. */
  55. #define SC16IS7XX_EFCR_REG (0x0f) /* Extra Features Control */
  56. /* TCR/TLR Register set: Only if ((MCR[2] == 1) && (EFR[4] == 1)) */
  57. #define SC16IS7XX_TCR_REG (0x06) /* Transmit control */
  58. #define SC16IS7XX_TLR_REG (0x07) /* Trigger level */
  59. /* Special Register set: Only if ((LCR[7] == 1) && (LCR != 0xBF)) */
  60. #define SC16IS7XX_DLL_REG (0x00) /* Divisor Latch Low */
  61. #define SC16IS7XX_DLH_REG (0x01) /* Divisor Latch High */
  62. /* Enhanced Register set: Only if (LCR == 0xBF) */
  63. #define SC16IS7XX_EFR_REG (0x02) /* Enhanced Features */
  64. #define SC16IS7XX_XON1_REG (0x04) /* Xon1 word */
  65. #define SC16IS7XX_XON2_REG (0x05) /* Xon2 word */
  66. #define SC16IS7XX_XOFF1_REG (0x06) /* Xoff1 word */
  67. #define SC16IS7XX_XOFF2_REG (0x07) /* Xoff2 word */
  68. /* IER register bits */
  69. #define SC16IS7XX_IER_RDI_BIT (1 << 0) /* Enable RX data interrupt */
  70. #define SC16IS7XX_IER_THRI_BIT (1 << 1) /* Enable TX holding register
  71. * interrupt */
  72. #define SC16IS7XX_IER_RLSI_BIT (1 << 2) /* Enable RX line status
  73. * interrupt */
  74. #define SC16IS7XX_IER_MSI_BIT (1 << 3) /* Enable Modem status
  75. * interrupt */
  76. /* IER register bits - write only if (EFR[4] == 1) */
  77. #define SC16IS7XX_IER_SLEEP_BIT (1 << 4) /* Enable Sleep mode */
  78. #define SC16IS7XX_IER_XOFFI_BIT (1 << 5) /* Enable Xoff interrupt */
  79. #define SC16IS7XX_IER_RTSI_BIT (1 << 6) /* Enable nRTS interrupt */
  80. #define SC16IS7XX_IER_CTSI_BIT (1 << 7) /* Enable nCTS interrupt */
  81. /* FCR register bits */
  82. #define SC16IS7XX_FCR_FIFO_BIT (1 << 0) /* Enable FIFO */
  83. #define SC16IS7XX_FCR_RXRESET_BIT (1 << 1) /* Reset RX FIFO */
  84. #define SC16IS7XX_FCR_TXRESET_BIT (1 << 2) /* Reset TX FIFO */
  85. #define SC16IS7XX_FCR_RXLVLL_BIT (1 << 6) /* RX Trigger level LSB */
  86. #define SC16IS7XX_FCR_RXLVLH_BIT (1 << 7) /* RX Trigger level MSB */
  87. /* FCR register bits - write only if (EFR[4] == 1) */
  88. #define SC16IS7XX_FCR_TXLVLL_BIT (1 << 4) /* TX Trigger level LSB */
  89. #define SC16IS7XX_FCR_TXLVLH_BIT (1 << 5) /* TX Trigger level MSB */
  90. /* IIR register bits */
  91. #define SC16IS7XX_IIR_NO_INT_BIT (1 << 0) /* No interrupts pending */
  92. #define SC16IS7XX_IIR_ID_MASK 0x3e /* Mask for the interrupt ID */
  93. #define SC16IS7XX_IIR_THRI_SRC 0x02 /* TX holding register empty */
  94. #define SC16IS7XX_IIR_RDI_SRC 0x04 /* RX data interrupt */
  95. #define SC16IS7XX_IIR_RLSE_SRC 0x06 /* RX line status error */
  96. #define SC16IS7XX_IIR_RTOI_SRC 0x0c /* RX time-out interrupt */
  97. #define SC16IS7XX_IIR_MSI_SRC 0x00 /* Modem status interrupt
  98. * - only on 75x/76x
  99. */
  100. #define SC16IS7XX_IIR_INPIN_SRC 0x30 /* Input pin change of state
  101. * - only on 75x/76x
  102. */
  103. #define SC16IS7XX_IIR_XOFFI_SRC 0x10 /* Received Xoff */
  104. #define SC16IS7XX_IIR_CTSRTS_SRC 0x20 /* nCTS,nRTS change of state
  105. * from active (LOW)
  106. * to inactive (HIGH)
  107. */
  108. /* LCR register bits */
  109. #define SC16IS7XX_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
  110. #define SC16IS7XX_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
  111. *
  112. * Word length bits table:
  113. * 00 -> 5 bit words
  114. * 01 -> 6 bit words
  115. * 10 -> 7 bit words
  116. * 11 -> 8 bit words
  117. */
  118. #define SC16IS7XX_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
  119. *
  120. * STOP length bit table:
  121. * 0 -> 1 stop bit
  122. * 1 -> 1-1.5 stop bits if
  123. * word length is 5,
  124. * 2 stop bits otherwise
  125. */
  126. #define SC16IS7XX_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
  127. #define SC16IS7XX_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
  128. #define SC16IS7XX_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
  129. #define SC16IS7XX_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
  130. #define SC16IS7XX_LCR_DLAB_BIT (1 << 7) /* Divisor Latch enable */
  131. #define SC16IS7XX_LCR_WORD_LEN_5 (0x00)
  132. #define SC16IS7XX_LCR_WORD_LEN_6 (0x01)
  133. #define SC16IS7XX_LCR_WORD_LEN_7 (0x02)
  134. #define SC16IS7XX_LCR_WORD_LEN_8 (0x03)
  135. #define SC16IS7XX_LCR_CONF_MODE_A SC16IS7XX_LCR_DLAB_BIT /* Special
  136. * reg set */
  137. #define SC16IS7XX_LCR_CONF_MODE_B 0xBF /* Enhanced
  138. * reg set */
  139. /* MCR register bits */
  140. #define SC16IS7XX_MCR_DTR_BIT (1 << 0) /* DTR complement
  141. * - only on 75x/76x
  142. */
  143. #define SC16IS7XX_MCR_RTS_BIT (1 << 1) /* RTS complement */
  144. #define SC16IS7XX_MCR_TCRTLR_BIT (1 << 2) /* TCR/TLR register enable */
  145. #define SC16IS7XX_MCR_LOOP_BIT (1 << 4) /* Enable loopback test mode */
  146. #define SC16IS7XX_MCR_XONANY_BIT (1 << 5) /* Enable Xon Any
  147. * - write enabled
  148. * if (EFR[4] == 1)
  149. */
  150. #define SC16IS7XX_MCR_IRDA_BIT (1 << 6) /* Enable IrDA mode
  151. * - write enabled
  152. * if (EFR[4] == 1)
  153. */
  154. #define SC16IS7XX_MCR_CLKSEL_BIT (1 << 7) /* Divide clock by 4
  155. * - write enabled
  156. * if (EFR[4] == 1)
  157. */
  158. /* LSR register bits */
  159. #define SC16IS7XX_LSR_DR_BIT (1 << 0) /* Receiver data ready */
  160. #define SC16IS7XX_LSR_OE_BIT (1 << 1) /* Overrun Error */
  161. #define SC16IS7XX_LSR_PE_BIT (1 << 2) /* Parity Error */
  162. #define SC16IS7XX_LSR_FE_BIT (1 << 3) /* Frame Error */
  163. #define SC16IS7XX_LSR_BI_BIT (1 << 4) /* Break Interrupt */
  164. #define SC16IS7XX_LSR_BRK_ERROR_MASK 0x1E /* BI, FE, PE, OE bits */
  165. #define SC16IS7XX_LSR_THRE_BIT (1 << 5) /* TX holding register empty */
  166. #define SC16IS7XX_LSR_TEMT_BIT (1 << 6) /* Transmitter empty */
  167. #define SC16IS7XX_LSR_FIFOE_BIT (1 << 7) /* Fifo Error */
  168. /* MSR register bits */
  169. #define SC16IS7XX_MSR_DCTS_BIT (1 << 0) /* Delta CTS Clear To Send */
  170. #define SC16IS7XX_MSR_DDSR_BIT (1 << 1) /* Delta DSR Data Set Ready
  171. * or (IO4)
  172. * - only on 75x/76x
  173. */
  174. #define SC16IS7XX_MSR_DRI_BIT (1 << 2) /* Delta RI Ring Indicator
  175. * or (IO7)
  176. * - only on 75x/76x
  177. */
  178. #define SC16IS7XX_MSR_DCD_BIT (1 << 3) /* Delta CD Carrier Detect
  179. * or (IO6)
  180. * - only on 75x/76x
  181. */
  182. #define SC16IS7XX_MSR_CTS_BIT (1 << 0) /* CTS */
  183. #define SC16IS7XX_MSR_DSR_BIT (1 << 1) /* DSR (IO4)
  184. * - only on 75x/76x
  185. */
  186. #define SC16IS7XX_MSR_RI_BIT (1 << 2) /* RI (IO7)
  187. * - only on 75x/76x
  188. */
  189. #define SC16IS7XX_MSR_CD_BIT (1 << 3) /* CD (IO6)
  190. * - only on 75x/76x
  191. */
  192. #define SC16IS7XX_MSR_DELTA_MASK 0x0F /* Any of the delta bits! */
  193. /*
  194. * TCR register bits
  195. * TCR trigger levels are available from 0 to 60 characters with a granularity
  196. * of four.
  197. * The programmer must program the TCR such that TCR[3:0] > TCR[7:4]. There is
  198. * no built-in hardware check to make sure this condition is met. Also, the TCR
  199. * must be programmed with this condition before auto RTS or software flow
  200. * control is enabled to avoid spurious operation of the device.
  201. */
  202. #define SC16IS7XX_TCR_RX_HALT(words) ((((words) / 4) & 0x0f) << 0)
  203. #define SC16IS7XX_TCR_RX_RESUME(words) ((((words) / 4) & 0x0f) << 4)
  204. /*
  205. * TLR register bits
  206. * If TLR[3:0] or TLR[7:4] are logical 0, the selectable trigger levels via the
  207. * FIFO Control Register (FCR) are used for the transmit and receive FIFO
  208. * trigger levels. Trigger levels from 4 characters to 60 characters are
  209. * available with a granularity of four.
  210. *
  211. * When the trigger level setting in TLR is zero, the SC16IS740/750/760 uses the
  212. * trigger level setting defined in FCR. If TLR has non-zero trigger level value
  213. * the trigger level defined in FCR is discarded. This applies to both transmit
  214. * FIFO and receive FIFO trigger level setting.
  215. *
  216. * When TLR is used for RX trigger level control, FCR[7:6] should be left at the
  217. * default state, that is, '00'.
  218. */
  219. #define SC16IS7XX_TLR_TX_TRIGGER(words) ((((words) / 4) & 0x0f) << 0)
  220. #define SC16IS7XX_TLR_RX_TRIGGER(words) ((((words) / 4) & 0x0f) << 4)
  221. /* IOControl register bits (Only 750/760) */
  222. #define SC16IS7XX_IOCONTROL_LATCH_BIT (1 << 0) /* Enable input latching */
  223. #define SC16IS7XX_IOCONTROL_GPIO_BIT (1 << 1) /* Enable GPIO[7:4] */
  224. #define SC16IS7XX_IOCONTROL_SRESET_BIT (1 << 3) /* Software Reset */
  225. /* EFCR register bits */
  226. #define SC16IS7XX_EFCR_9BIT_MODE_BIT (1 << 0) /* Enable 9-bit or Multidrop
  227. * mode (RS485) */
  228. #define SC16IS7XX_EFCR_RXDISABLE_BIT (1 << 1) /* Disable receiver */
  229. #define SC16IS7XX_EFCR_TXDISABLE_BIT (1 << 2) /* Disable transmitter */
  230. #define SC16IS7XX_EFCR_AUTO_RS485_BIT (1 << 4) /* Auto RS485 RTS direction */
  231. #define SC16IS7XX_EFCR_RTS_INVERT_BIT (1 << 5) /* RTS output inversion */
  232. #define SC16IS7XX_EFCR_IRDA_MODE_BIT (1 << 7) /* IrDA mode
  233. * 0 = rate upto 115.2 kbit/s
  234. * - Only 750/760
  235. * 1 = rate upto 1.152 Mbit/s
  236. * - Only 760
  237. */
  238. /* EFR register bits */
  239. #define SC16IS7XX_EFR_AUTORTS_BIT (1 << 6) /* Auto RTS flow ctrl enable */
  240. #define SC16IS7XX_EFR_AUTOCTS_BIT (1 << 7) /* Auto CTS flow ctrl enable */
  241. #define SC16IS7XX_EFR_XOFF2_DETECT_BIT (1 << 5) /* Enable Xoff2 detection */
  242. #define SC16IS7XX_EFR_ENABLE_BIT (1 << 4) /* Enable enhanced functions
  243. * and writing to IER[7:4],
  244. * FCR[5:4], MCR[7:5]
  245. */
  246. #define SC16IS7XX_EFR_SWFLOW3_BIT (1 << 3) /* SWFLOW bit 3 */
  247. #define SC16IS7XX_EFR_SWFLOW2_BIT (1 << 2) /* SWFLOW bit 2
  248. *
  249. * SWFLOW bits 3 & 2 table:
  250. * 00 -> no transmitter flow
  251. * control
  252. * 01 -> transmitter generates
  253. * XON2 and XOFF2
  254. * 10 -> transmitter generates
  255. * XON1 and XOFF1
  256. * 11 -> transmitter generates
  257. * XON1, XON2, XOFF1 and
  258. * XOFF2
  259. */
  260. #define SC16IS7XX_EFR_SWFLOW1_BIT (1 << 1) /* SWFLOW bit 2 */
  261. #define SC16IS7XX_EFR_SWFLOW0_BIT (1 << 0) /* SWFLOW bit 3
  262. *
  263. * SWFLOW bits 3 & 2 table:
  264. * 00 -> no received flow
  265. * control
  266. * 01 -> receiver compares
  267. * XON2 and XOFF2
  268. * 10 -> receiver compares
  269. * XON1 and XOFF1
  270. * 11 -> receiver compares
  271. * XON1, XON2, XOFF1 and
  272. * XOFF2
  273. */
  274. /* Misc definitions */
  275. #define SC16IS7XX_FIFO_SIZE (64)
  276. #define SC16IS7XX_REG_SHIFT 2
  277. struct sc16is7xx_devtype {
  278. char name[10];
  279. int nr_gpio;
  280. int nr_uart;
  281. };
  282. #define SC16IS7XX_RECONF_MD (1 << 0)
  283. #define SC16IS7XX_RECONF_IER (1 << 1)
  284. #define SC16IS7XX_RECONF_RS485 (1 << 2)
  285. struct sc16is7xx_one_config {
  286. unsigned int flags;
  287. u8 ier_clear;
  288. };
  289. struct sc16is7xx_one {
  290. struct uart_port port;
  291. struct kthread_work tx_work;
  292. struct kthread_work reg_work;
  293. struct sc16is7xx_one_config config;
  294. };
  295. struct sc16is7xx_port {
  296. struct uart_driver uart;
  297. struct sc16is7xx_devtype *devtype;
  298. struct regmap *regmap;
  299. struct clk *clk;
  300. #ifdef CONFIG_GPIOLIB
  301. struct gpio_chip gpio;
  302. #endif
  303. unsigned char buf[SC16IS7XX_FIFO_SIZE];
  304. struct kthread_worker kworker;
  305. struct task_struct *kworker_task;
  306. struct kthread_work irq_work;
  307. struct sc16is7xx_one p[0];
  308. };
  309. #define to_sc16is7xx_port(p,e) ((container_of((p), struct sc16is7xx_port, e)))
  310. #define to_sc16is7xx_one(p,e) ((container_of((p), struct sc16is7xx_one, e)))
  311. static u8 sc16is7xx_port_read(struct uart_port *port, u8 reg)
  312. {
  313. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  314. unsigned int val = 0;
  315. regmap_read(s->regmap,
  316. (reg << SC16IS7XX_REG_SHIFT) | port->line, &val);
  317. return val;
  318. }
  319. static void sc16is7xx_port_write(struct uart_port *port, u8 reg, u8 val)
  320. {
  321. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  322. regmap_write(s->regmap,
  323. (reg << SC16IS7XX_REG_SHIFT) | port->line, val);
  324. }
  325. static void sc16is7xx_port_update(struct uart_port *port, u8 reg,
  326. u8 mask, u8 val)
  327. {
  328. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  329. regmap_update_bits(s->regmap,
  330. (reg << SC16IS7XX_REG_SHIFT) | port->line,
  331. mask, val);
  332. }
  333. static void sc16is7xx_power(struct uart_port *port, int on)
  334. {
  335. sc16is7xx_port_update(port, SC16IS7XX_IER_REG,
  336. SC16IS7XX_IER_SLEEP_BIT,
  337. on ? 0 : SC16IS7XX_IER_SLEEP_BIT);
  338. }
  339. static const struct sc16is7xx_devtype sc16is74x_devtype = {
  340. .name = "SC16IS74X",
  341. .nr_gpio = 0,
  342. .nr_uart = 1,
  343. };
  344. static const struct sc16is7xx_devtype sc16is750_devtype = {
  345. .name = "SC16IS750",
  346. .nr_gpio = 8,
  347. .nr_uart = 1,
  348. };
  349. static const struct sc16is7xx_devtype sc16is752_devtype = {
  350. .name = "SC16IS752",
  351. .nr_gpio = 8,
  352. .nr_uart = 2,
  353. };
  354. static const struct sc16is7xx_devtype sc16is760_devtype = {
  355. .name = "SC16IS760",
  356. .nr_gpio = 8,
  357. .nr_uart = 1,
  358. };
  359. static const struct sc16is7xx_devtype sc16is762_devtype = {
  360. .name = "SC16IS762",
  361. .nr_gpio = 8,
  362. .nr_uart = 2,
  363. };
  364. static bool sc16is7xx_regmap_volatile(struct device *dev, unsigned int reg)
  365. {
  366. switch (reg >> SC16IS7XX_REG_SHIFT) {
  367. case SC16IS7XX_RHR_REG:
  368. case SC16IS7XX_IIR_REG:
  369. case SC16IS7XX_LSR_REG:
  370. case SC16IS7XX_MSR_REG:
  371. case SC16IS7XX_TXLVL_REG:
  372. case SC16IS7XX_RXLVL_REG:
  373. case SC16IS7XX_IOSTATE_REG:
  374. return true;
  375. default:
  376. break;
  377. }
  378. return false;
  379. }
  380. static bool sc16is7xx_regmap_precious(struct device *dev, unsigned int reg)
  381. {
  382. switch (reg >> SC16IS7XX_REG_SHIFT) {
  383. case SC16IS7XX_RHR_REG:
  384. return true;
  385. default:
  386. break;
  387. }
  388. return false;
  389. }
  390. static int sc16is7xx_set_baud(struct uart_port *port, int baud)
  391. {
  392. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  393. u8 lcr;
  394. u8 prescaler = 0;
  395. unsigned long clk = port->uartclk, div = clk / 16 / baud;
  396. if (div > 0xffff) {
  397. prescaler = SC16IS7XX_MCR_CLKSEL_BIT;
  398. div /= 4;
  399. }
  400. lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG);
  401. /* Open the LCR divisors for configuration */
  402. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  403. SC16IS7XX_LCR_CONF_MODE_B);
  404. /* Enable enhanced features */
  405. regcache_cache_bypass(s->regmap, true);
  406. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  407. SC16IS7XX_EFR_ENABLE_BIT);
  408. regcache_cache_bypass(s->regmap, false);
  409. /* Put LCR back to the normal mode */
  410. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  411. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  412. SC16IS7XX_MCR_CLKSEL_BIT,
  413. prescaler);
  414. /* Open the LCR divisors for configuration */
  415. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  416. SC16IS7XX_LCR_CONF_MODE_A);
  417. /* Write the new divisor */
  418. regcache_cache_bypass(s->regmap, true);
  419. sc16is7xx_port_write(port, SC16IS7XX_DLH_REG, div / 256);
  420. sc16is7xx_port_write(port, SC16IS7XX_DLL_REG, div % 256);
  421. regcache_cache_bypass(s->regmap, false);
  422. /* Put LCR back to the normal mode */
  423. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  424. return DIV_ROUND_CLOSEST(clk / 16, div);
  425. }
  426. static void sc16is7xx_handle_rx(struct uart_port *port, unsigned int rxlen,
  427. unsigned int iir)
  428. {
  429. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  430. unsigned int lsr = 0, ch, flag, bytes_read, i;
  431. bool read_lsr = (iir == SC16IS7XX_IIR_RLSE_SRC) ? true : false;
  432. if (unlikely(rxlen >= sizeof(s->buf))) {
  433. dev_warn_ratelimited(port->dev,
  434. "Port %i: Possible RX FIFO overrun: %d\n",
  435. port->line, rxlen);
  436. port->icount.buf_overrun++;
  437. /* Ensure sanity of RX level */
  438. rxlen = sizeof(s->buf);
  439. }
  440. while (rxlen) {
  441. /* Only read lsr if there are possible errors in FIFO */
  442. if (read_lsr) {
  443. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  444. if (!(lsr & SC16IS7XX_LSR_FIFOE_BIT))
  445. read_lsr = false; /* No errors left in FIFO */
  446. } else
  447. lsr = 0;
  448. if (read_lsr) {
  449. s->buf[0] = sc16is7xx_port_read(port, SC16IS7XX_RHR_REG);
  450. bytes_read = 1;
  451. } else {
  452. regcache_cache_bypass(s->regmap, true);
  453. regmap_raw_read(s->regmap, SC16IS7XX_RHR_REG,
  454. s->buf, rxlen);
  455. regcache_cache_bypass(s->regmap, false);
  456. bytes_read = rxlen;
  457. }
  458. lsr &= SC16IS7XX_LSR_BRK_ERROR_MASK;
  459. port->icount.rx++;
  460. flag = TTY_NORMAL;
  461. if (unlikely(lsr)) {
  462. if (lsr & SC16IS7XX_LSR_BI_BIT) {
  463. port->icount.brk++;
  464. if (uart_handle_break(port))
  465. continue;
  466. } else if (lsr & SC16IS7XX_LSR_PE_BIT)
  467. port->icount.parity++;
  468. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  469. port->icount.frame++;
  470. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  471. port->icount.overrun++;
  472. lsr &= port->read_status_mask;
  473. if (lsr & SC16IS7XX_LSR_BI_BIT)
  474. flag = TTY_BREAK;
  475. else if (lsr & SC16IS7XX_LSR_PE_BIT)
  476. flag = TTY_PARITY;
  477. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  478. flag = TTY_FRAME;
  479. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  480. flag = TTY_OVERRUN;
  481. }
  482. for (i = 0; i < bytes_read; ++i) {
  483. ch = s->buf[i];
  484. if (uart_handle_sysrq_char(port, ch))
  485. continue;
  486. if (lsr & port->ignore_status_mask)
  487. continue;
  488. uart_insert_char(port, lsr, SC16IS7XX_LSR_OE_BIT, ch,
  489. flag);
  490. }
  491. rxlen -= bytes_read;
  492. }
  493. tty_flip_buffer_push(&port->state->port);
  494. }
  495. static void sc16is7xx_handle_tx(struct uart_port *port)
  496. {
  497. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  498. struct circ_buf *xmit = &port->state->xmit;
  499. unsigned int txlen, to_send, i;
  500. if (unlikely(port->x_char)) {
  501. sc16is7xx_port_write(port, SC16IS7XX_THR_REG, port->x_char);
  502. port->icount.tx++;
  503. port->x_char = 0;
  504. return;
  505. }
  506. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  507. return;
  508. /* Get length of data pending in circular buffer */
  509. to_send = uart_circ_chars_pending(xmit);
  510. if (likely(to_send)) {
  511. /* Limit to size of TX FIFO */
  512. txlen = sc16is7xx_port_read(port, SC16IS7XX_TXLVL_REG);
  513. to_send = (to_send > txlen) ? txlen : to_send;
  514. /* Add data to send */
  515. port->icount.tx += to_send;
  516. /* Convert to linear buffer */
  517. for (i = 0; i < to_send; ++i) {
  518. s->buf[i] = xmit->buf[xmit->tail];
  519. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  520. }
  521. regcache_cache_bypass(s->regmap, true);
  522. regmap_raw_write(s->regmap, SC16IS7XX_THR_REG, s->buf, to_send);
  523. regcache_cache_bypass(s->regmap, false);
  524. }
  525. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  526. uart_write_wakeup(port);
  527. }
  528. static void sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
  529. {
  530. struct uart_port *port = &s->p[portno].port;
  531. do {
  532. unsigned int iir, msr, rxlen;
  533. iir = sc16is7xx_port_read(port, SC16IS7XX_IIR_REG);
  534. if (iir & SC16IS7XX_IIR_NO_INT_BIT)
  535. break;
  536. iir &= SC16IS7XX_IIR_ID_MASK;
  537. switch (iir) {
  538. case SC16IS7XX_IIR_RDI_SRC:
  539. case SC16IS7XX_IIR_RLSE_SRC:
  540. case SC16IS7XX_IIR_RTOI_SRC:
  541. case SC16IS7XX_IIR_XOFFI_SRC:
  542. rxlen = sc16is7xx_port_read(port, SC16IS7XX_RXLVL_REG);
  543. if (rxlen)
  544. sc16is7xx_handle_rx(port, rxlen, iir);
  545. break;
  546. case SC16IS7XX_IIR_CTSRTS_SRC:
  547. msr = sc16is7xx_port_read(port, SC16IS7XX_MSR_REG);
  548. uart_handle_cts_change(port,
  549. !!(msr & SC16IS7XX_MSR_CTS_BIT));
  550. break;
  551. case SC16IS7XX_IIR_THRI_SRC:
  552. sc16is7xx_handle_tx(port);
  553. break;
  554. default:
  555. dev_err_ratelimited(port->dev,
  556. "Port %i: Unexpected interrupt: %x",
  557. port->line, iir);
  558. break;
  559. }
  560. } while (1);
  561. }
  562. static void sc16is7xx_ist(struct kthread_work *ws)
  563. {
  564. struct sc16is7xx_port *s = to_sc16is7xx_port(ws, irq_work);
  565. int i;
  566. for (i = 0; i < s->uart.nr; ++i)
  567. sc16is7xx_port_irq(s, i);
  568. }
  569. static irqreturn_t sc16is7xx_irq(int irq, void *dev_id)
  570. {
  571. struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id;
  572. queue_kthread_work(&s->kworker, &s->irq_work);
  573. return IRQ_HANDLED;
  574. }
  575. static void sc16is7xx_tx_proc(struct kthread_work *ws)
  576. {
  577. struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port);
  578. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  579. (port->rs485.delay_rts_before_send > 0))
  580. msleep(port->rs485.delay_rts_before_send);
  581. sc16is7xx_handle_tx(port);
  582. }
  583. static void sc16is7xx_reconf_rs485(struct uart_port *port)
  584. {
  585. const u32 mask = SC16IS7XX_EFCR_AUTO_RS485_BIT |
  586. SC16IS7XX_EFCR_RTS_INVERT_BIT;
  587. u32 efcr = 0;
  588. struct serial_rs485 *rs485 = &port->rs485;
  589. unsigned long irqflags;
  590. spin_lock_irqsave(&port->lock, irqflags);
  591. if (rs485->flags & SER_RS485_ENABLED) {
  592. efcr |= SC16IS7XX_EFCR_AUTO_RS485_BIT;
  593. if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  594. efcr |= SC16IS7XX_EFCR_RTS_INVERT_BIT;
  595. }
  596. spin_unlock_irqrestore(&port->lock, irqflags);
  597. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG, mask, efcr);
  598. }
  599. static void sc16is7xx_reg_proc(struct kthread_work *ws)
  600. {
  601. struct sc16is7xx_one *one = to_sc16is7xx_one(ws, reg_work);
  602. struct sc16is7xx_one_config config;
  603. unsigned long irqflags;
  604. spin_lock_irqsave(&one->port.lock, irqflags);
  605. config = one->config;
  606. memset(&one->config, 0, sizeof(one->config));
  607. spin_unlock_irqrestore(&one->port.lock, irqflags);
  608. if (config.flags & SC16IS7XX_RECONF_MD)
  609. sc16is7xx_port_update(&one->port, SC16IS7XX_MCR_REG,
  610. SC16IS7XX_MCR_LOOP_BIT,
  611. (one->port.mctrl & TIOCM_LOOP) ?
  612. SC16IS7XX_MCR_LOOP_BIT : 0);
  613. if (config.flags & SC16IS7XX_RECONF_IER)
  614. sc16is7xx_port_update(&one->port, SC16IS7XX_IER_REG,
  615. config.ier_clear, 0);
  616. if (config.flags & SC16IS7XX_RECONF_RS485)
  617. sc16is7xx_reconf_rs485(&one->port);
  618. }
  619. static void sc16is7xx_ier_clear(struct uart_port *port, u8 bit)
  620. {
  621. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  622. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  623. one->config.flags |= SC16IS7XX_RECONF_IER;
  624. one->config.ier_clear |= bit;
  625. queue_kthread_work(&s->kworker, &one->reg_work);
  626. }
  627. static void sc16is7xx_stop_tx(struct uart_port *port)
  628. {
  629. sc16is7xx_ier_clear(port, SC16IS7XX_IER_THRI_BIT);
  630. }
  631. static void sc16is7xx_stop_rx(struct uart_port *port)
  632. {
  633. sc16is7xx_ier_clear(port, SC16IS7XX_IER_RDI_BIT);
  634. }
  635. static void sc16is7xx_start_tx(struct uart_port *port)
  636. {
  637. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  638. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  639. queue_kthread_work(&s->kworker, &one->tx_work);
  640. }
  641. static unsigned int sc16is7xx_tx_empty(struct uart_port *port)
  642. {
  643. unsigned int lsr;
  644. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  645. return (lsr & SC16IS7XX_LSR_TEMT_BIT) ? TIOCSER_TEMT : 0;
  646. }
  647. static unsigned int sc16is7xx_get_mctrl(struct uart_port *port)
  648. {
  649. /* DCD and DSR are not wired and CTS/RTS is handled automatically
  650. * so just indicate DSR and CAR asserted
  651. */
  652. return TIOCM_DSR | TIOCM_CAR;
  653. }
  654. static void sc16is7xx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  655. {
  656. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  657. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  658. one->config.flags |= SC16IS7XX_RECONF_MD;
  659. queue_kthread_work(&s->kworker, &one->reg_work);
  660. }
  661. static void sc16is7xx_break_ctl(struct uart_port *port, int break_state)
  662. {
  663. sc16is7xx_port_update(port, SC16IS7XX_LCR_REG,
  664. SC16IS7XX_LCR_TXBREAK_BIT,
  665. break_state ? SC16IS7XX_LCR_TXBREAK_BIT : 0);
  666. }
  667. static void sc16is7xx_set_termios(struct uart_port *port,
  668. struct ktermios *termios,
  669. struct ktermios *old)
  670. {
  671. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  672. unsigned int lcr, flow = 0;
  673. int baud;
  674. /* Mask termios capabilities we don't support */
  675. termios->c_cflag &= ~CMSPAR;
  676. /* Word size */
  677. switch (termios->c_cflag & CSIZE) {
  678. case CS5:
  679. lcr = SC16IS7XX_LCR_WORD_LEN_5;
  680. break;
  681. case CS6:
  682. lcr = SC16IS7XX_LCR_WORD_LEN_6;
  683. break;
  684. case CS7:
  685. lcr = SC16IS7XX_LCR_WORD_LEN_7;
  686. break;
  687. case CS8:
  688. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  689. break;
  690. default:
  691. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  692. termios->c_cflag &= ~CSIZE;
  693. termios->c_cflag |= CS8;
  694. break;
  695. }
  696. /* Parity */
  697. if (termios->c_cflag & PARENB) {
  698. lcr |= SC16IS7XX_LCR_PARITY_BIT;
  699. if (!(termios->c_cflag & PARODD))
  700. lcr |= SC16IS7XX_LCR_EVENPARITY_BIT;
  701. }
  702. /* Stop bits */
  703. if (termios->c_cflag & CSTOPB)
  704. lcr |= SC16IS7XX_LCR_STOPLEN_BIT; /* 2 stops */
  705. /* Set read status mask */
  706. port->read_status_mask = SC16IS7XX_LSR_OE_BIT;
  707. if (termios->c_iflag & INPCK)
  708. port->read_status_mask |= SC16IS7XX_LSR_PE_BIT |
  709. SC16IS7XX_LSR_FE_BIT;
  710. if (termios->c_iflag & (BRKINT | PARMRK))
  711. port->read_status_mask |= SC16IS7XX_LSR_BI_BIT;
  712. /* Set status ignore mask */
  713. port->ignore_status_mask = 0;
  714. if (termios->c_iflag & IGNBRK)
  715. port->ignore_status_mask |= SC16IS7XX_LSR_BI_BIT;
  716. if (!(termios->c_cflag & CREAD))
  717. port->ignore_status_mask |= SC16IS7XX_LSR_BRK_ERROR_MASK;
  718. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  719. SC16IS7XX_LCR_CONF_MODE_B);
  720. /* Configure flow control */
  721. regcache_cache_bypass(s->regmap, true);
  722. sc16is7xx_port_write(port, SC16IS7XX_XON1_REG, termios->c_cc[VSTART]);
  723. sc16is7xx_port_write(port, SC16IS7XX_XOFF1_REG, termios->c_cc[VSTOP]);
  724. if (termios->c_cflag & CRTSCTS)
  725. flow |= SC16IS7XX_EFR_AUTOCTS_BIT |
  726. SC16IS7XX_EFR_AUTORTS_BIT;
  727. if (termios->c_iflag & IXON)
  728. flow |= SC16IS7XX_EFR_SWFLOW3_BIT;
  729. if (termios->c_iflag & IXOFF)
  730. flow |= SC16IS7XX_EFR_SWFLOW1_BIT;
  731. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG, flow);
  732. regcache_cache_bypass(s->regmap, false);
  733. /* Update LCR register */
  734. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  735. /* Get baud rate generator configuration */
  736. baud = uart_get_baud_rate(port, termios, old,
  737. port->uartclk / 16 / 4 / 0xffff,
  738. port->uartclk / 16);
  739. /* Setup baudrate generator */
  740. baud = sc16is7xx_set_baud(port, baud);
  741. /* Update timeout according to new baud rate */
  742. uart_update_timeout(port, termios->c_cflag, baud);
  743. }
  744. static int sc16is7xx_config_rs485(struct uart_port *port,
  745. struct serial_rs485 *rs485)
  746. {
  747. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  748. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  749. if (rs485->flags & SER_RS485_ENABLED) {
  750. bool rts_during_rx, rts_during_tx;
  751. rts_during_rx = rs485->flags & SER_RS485_RTS_AFTER_SEND;
  752. rts_during_tx = rs485->flags & SER_RS485_RTS_ON_SEND;
  753. if (rts_during_rx == rts_during_tx)
  754. dev_err(port->dev,
  755. "unsupported RTS signalling on_send:%d after_send:%d - exactly one of RS485 RTS flags should be set\n",
  756. rts_during_tx, rts_during_rx);
  757. /*
  758. * RTS signal is handled by HW, it's timing can't be influenced.
  759. * However, it's sometimes useful to delay TX even without RTS
  760. * control therefore we try to handle .delay_rts_before_send.
  761. */
  762. if (rs485->delay_rts_after_send)
  763. return -EINVAL;
  764. }
  765. port->rs485 = *rs485;
  766. one->config.flags |= SC16IS7XX_RECONF_RS485;
  767. queue_kthread_work(&s->kworker, &one->reg_work);
  768. return 0;
  769. }
  770. static int sc16is7xx_startup(struct uart_port *port)
  771. {
  772. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  773. unsigned int val;
  774. sc16is7xx_power(port, 1);
  775. /* Reset FIFOs*/
  776. val = SC16IS7XX_FCR_RXRESET_BIT | SC16IS7XX_FCR_TXRESET_BIT;
  777. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG, val);
  778. udelay(5);
  779. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG,
  780. SC16IS7XX_FCR_FIFO_BIT);
  781. /* Enable EFR */
  782. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  783. SC16IS7XX_LCR_CONF_MODE_B);
  784. regcache_cache_bypass(s->regmap, true);
  785. /* Enable write access to enhanced features and internal clock div */
  786. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  787. SC16IS7XX_EFR_ENABLE_BIT);
  788. /* Enable TCR/TLR */
  789. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  790. SC16IS7XX_MCR_TCRTLR_BIT,
  791. SC16IS7XX_MCR_TCRTLR_BIT);
  792. /* Configure flow control levels */
  793. /* Flow control halt level 48, resume level 24 */
  794. sc16is7xx_port_write(port, SC16IS7XX_TCR_REG,
  795. SC16IS7XX_TCR_RX_RESUME(24) |
  796. SC16IS7XX_TCR_RX_HALT(48));
  797. regcache_cache_bypass(s->regmap, false);
  798. /* Now, initialize the UART */
  799. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, SC16IS7XX_LCR_WORD_LEN_8);
  800. /* Enable the Rx and Tx FIFO */
  801. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  802. SC16IS7XX_EFCR_RXDISABLE_BIT |
  803. SC16IS7XX_EFCR_TXDISABLE_BIT,
  804. 0);
  805. /* Enable RX, TX, CTS change interrupts */
  806. val = SC16IS7XX_IER_RDI_BIT | SC16IS7XX_IER_THRI_BIT |
  807. SC16IS7XX_IER_CTSI_BIT;
  808. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, val);
  809. return 0;
  810. }
  811. static void sc16is7xx_shutdown(struct uart_port *port)
  812. {
  813. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  814. /* Disable all interrupts */
  815. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, 0);
  816. /* Disable TX/RX */
  817. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  818. SC16IS7XX_EFCR_RXDISABLE_BIT |
  819. SC16IS7XX_EFCR_TXDISABLE_BIT,
  820. SC16IS7XX_EFCR_RXDISABLE_BIT |
  821. SC16IS7XX_EFCR_TXDISABLE_BIT);
  822. sc16is7xx_power(port, 0);
  823. flush_kthread_worker(&s->kworker);
  824. }
  825. static const char *sc16is7xx_type(struct uart_port *port)
  826. {
  827. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  828. return (port->type == PORT_SC16IS7XX) ? s->devtype->name : NULL;
  829. }
  830. static int sc16is7xx_request_port(struct uart_port *port)
  831. {
  832. /* Do nothing */
  833. return 0;
  834. }
  835. static void sc16is7xx_config_port(struct uart_port *port, int flags)
  836. {
  837. if (flags & UART_CONFIG_TYPE)
  838. port->type = PORT_SC16IS7XX;
  839. }
  840. static int sc16is7xx_verify_port(struct uart_port *port,
  841. struct serial_struct *s)
  842. {
  843. if ((s->type != PORT_UNKNOWN) && (s->type != PORT_SC16IS7XX))
  844. return -EINVAL;
  845. if (s->irq != port->irq)
  846. return -EINVAL;
  847. return 0;
  848. }
  849. static void sc16is7xx_pm(struct uart_port *port, unsigned int state,
  850. unsigned int oldstate)
  851. {
  852. sc16is7xx_power(port, (state == UART_PM_STATE_ON) ? 1 : 0);
  853. }
  854. static void sc16is7xx_null_void(struct uart_port *port)
  855. {
  856. /* Do nothing */
  857. }
  858. static const struct uart_ops sc16is7xx_ops = {
  859. .tx_empty = sc16is7xx_tx_empty,
  860. .set_mctrl = sc16is7xx_set_mctrl,
  861. .get_mctrl = sc16is7xx_get_mctrl,
  862. .stop_tx = sc16is7xx_stop_tx,
  863. .start_tx = sc16is7xx_start_tx,
  864. .stop_rx = sc16is7xx_stop_rx,
  865. .break_ctl = sc16is7xx_break_ctl,
  866. .startup = sc16is7xx_startup,
  867. .shutdown = sc16is7xx_shutdown,
  868. .set_termios = sc16is7xx_set_termios,
  869. .type = sc16is7xx_type,
  870. .request_port = sc16is7xx_request_port,
  871. .release_port = sc16is7xx_null_void,
  872. .config_port = sc16is7xx_config_port,
  873. .verify_port = sc16is7xx_verify_port,
  874. .pm = sc16is7xx_pm,
  875. };
  876. #ifdef CONFIG_GPIOLIB
  877. static int sc16is7xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  878. {
  879. unsigned int val;
  880. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  881. gpio);
  882. struct uart_port *port = &s->p[0].port;
  883. val = sc16is7xx_port_read(port, SC16IS7XX_IOSTATE_REG);
  884. return !!(val & BIT(offset));
  885. }
  886. static void sc16is7xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  887. {
  888. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  889. gpio);
  890. struct uart_port *port = &s->p[0].port;
  891. sc16is7xx_port_update(port, SC16IS7XX_IOSTATE_REG, BIT(offset),
  892. val ? BIT(offset) : 0);
  893. }
  894. static int sc16is7xx_gpio_direction_input(struct gpio_chip *chip,
  895. unsigned offset)
  896. {
  897. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  898. gpio);
  899. struct uart_port *port = &s->p[0].port;
  900. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset), 0);
  901. return 0;
  902. }
  903. static int sc16is7xx_gpio_direction_output(struct gpio_chip *chip,
  904. unsigned offset, int val)
  905. {
  906. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  907. gpio);
  908. struct uart_port *port = &s->p[0].port;
  909. sc16is7xx_port_update(port, SC16IS7XX_IOSTATE_REG, BIT(offset),
  910. val ? BIT(offset) : 0);
  911. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset),
  912. BIT(offset));
  913. return 0;
  914. }
  915. #endif
  916. static int sc16is7xx_probe(struct device *dev,
  917. struct sc16is7xx_devtype *devtype,
  918. struct regmap *regmap, int irq, unsigned long flags)
  919. {
  920. struct sched_param sched_param = { .sched_priority = MAX_RT_PRIO / 2 };
  921. unsigned long freq, *pfreq = dev_get_platdata(dev);
  922. int i, ret;
  923. struct sc16is7xx_port *s;
  924. if (IS_ERR(regmap))
  925. return PTR_ERR(regmap);
  926. /* Alloc port structure */
  927. s = devm_kzalloc(dev, sizeof(*s) +
  928. sizeof(struct sc16is7xx_one) * devtype->nr_uart,
  929. GFP_KERNEL);
  930. if (!s) {
  931. dev_err(dev, "Error allocating port structure\n");
  932. return -ENOMEM;
  933. }
  934. s->clk = devm_clk_get(dev, NULL);
  935. if (IS_ERR(s->clk)) {
  936. if (pfreq)
  937. freq = *pfreq;
  938. else
  939. return PTR_ERR(s->clk);
  940. } else {
  941. clk_prepare_enable(s->clk);
  942. freq = clk_get_rate(s->clk);
  943. }
  944. s->regmap = regmap;
  945. s->devtype = devtype;
  946. dev_set_drvdata(dev, s);
  947. /* Register UART driver */
  948. s->uart.owner = THIS_MODULE;
  949. s->uart.dev_name = "ttySC";
  950. s->uart.nr = devtype->nr_uart;
  951. ret = uart_register_driver(&s->uart);
  952. if (ret) {
  953. dev_err(dev, "Registering UART driver failed\n");
  954. goto out_clk;
  955. }
  956. init_kthread_worker(&s->kworker);
  957. init_kthread_work(&s->irq_work, sc16is7xx_ist);
  958. s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
  959. "sc16is7xx");
  960. if (IS_ERR(s->kworker_task)) {
  961. ret = PTR_ERR(s->kworker_task);
  962. goto out_uart;
  963. }
  964. sched_setscheduler(s->kworker_task, SCHED_FIFO, &sched_param);
  965. #ifdef CONFIG_GPIOLIB
  966. if (devtype->nr_gpio) {
  967. /* Setup GPIO cotroller */
  968. s->gpio.owner = THIS_MODULE;
  969. s->gpio.dev = dev;
  970. s->gpio.label = dev_name(dev);
  971. s->gpio.direction_input = sc16is7xx_gpio_direction_input;
  972. s->gpio.get = sc16is7xx_gpio_get;
  973. s->gpio.direction_output = sc16is7xx_gpio_direction_output;
  974. s->gpio.set = sc16is7xx_gpio_set;
  975. s->gpio.base = -1;
  976. s->gpio.ngpio = devtype->nr_gpio;
  977. s->gpio.can_sleep = 1;
  978. ret = gpiochip_add(&s->gpio);
  979. if (ret)
  980. goto out_thread;
  981. }
  982. #endif
  983. for (i = 0; i < devtype->nr_uart; ++i) {
  984. /* Initialize port data */
  985. s->p[i].port.line = i;
  986. s->p[i].port.dev = dev;
  987. s->p[i].port.irq = irq;
  988. s->p[i].port.type = PORT_SC16IS7XX;
  989. s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE;
  990. s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
  991. s->p[i].port.iotype = UPIO_PORT;
  992. s->p[i].port.uartclk = freq;
  993. s->p[i].port.rs485_config = sc16is7xx_config_rs485;
  994. s->p[i].port.ops = &sc16is7xx_ops;
  995. /* Disable all interrupts */
  996. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_IER_REG, 0);
  997. /* Disable TX/RX */
  998. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_EFCR_REG,
  999. SC16IS7XX_EFCR_RXDISABLE_BIT |
  1000. SC16IS7XX_EFCR_TXDISABLE_BIT);
  1001. /* Initialize kthread work structs */
  1002. init_kthread_work(&s->p[i].tx_work, sc16is7xx_tx_proc);
  1003. init_kthread_work(&s->p[i].reg_work, sc16is7xx_reg_proc);
  1004. /* Register port */
  1005. uart_add_one_port(&s->uart, &s->p[i].port);
  1006. /* Go to suspend mode */
  1007. sc16is7xx_power(&s->p[i].port, 0);
  1008. }
  1009. /* Setup interrupt */
  1010. ret = devm_request_irq(dev, irq, sc16is7xx_irq,
  1011. IRQF_ONESHOT | flags, dev_name(dev), s);
  1012. if (!ret)
  1013. return 0;
  1014. for (i = 0; i < s->uart.nr; i++)
  1015. uart_remove_one_port(&s->uart, &s->p[i].port);
  1016. #ifdef CONFIG_GPIOLIB
  1017. if (devtype->nr_gpio)
  1018. gpiochip_remove(&s->gpio);
  1019. out_thread:
  1020. #endif
  1021. kthread_stop(s->kworker_task);
  1022. out_uart:
  1023. uart_unregister_driver(&s->uart);
  1024. out_clk:
  1025. if (!IS_ERR(s->clk))
  1026. clk_disable_unprepare(s->clk);
  1027. return ret;
  1028. }
  1029. static int sc16is7xx_remove(struct device *dev)
  1030. {
  1031. struct sc16is7xx_port *s = dev_get_drvdata(dev);
  1032. int i;
  1033. #ifdef CONFIG_GPIOLIB
  1034. if (s->devtype->nr_gpio)
  1035. gpiochip_remove(&s->gpio);
  1036. #endif
  1037. for (i = 0; i < s->uart.nr; i++) {
  1038. uart_remove_one_port(&s->uart, &s->p[i].port);
  1039. sc16is7xx_power(&s->p[i].port, 0);
  1040. }
  1041. flush_kthread_worker(&s->kworker);
  1042. kthread_stop(s->kworker_task);
  1043. uart_unregister_driver(&s->uart);
  1044. if (!IS_ERR(s->clk))
  1045. clk_disable_unprepare(s->clk);
  1046. return 0;
  1047. }
  1048. static const struct of_device_id __maybe_unused sc16is7xx_dt_ids[] = {
  1049. { .compatible = "nxp,sc16is740", .data = &sc16is74x_devtype, },
  1050. { .compatible = "nxp,sc16is741", .data = &sc16is74x_devtype, },
  1051. { .compatible = "nxp,sc16is750", .data = &sc16is750_devtype, },
  1052. { .compatible = "nxp,sc16is752", .data = &sc16is752_devtype, },
  1053. { .compatible = "nxp,sc16is760", .data = &sc16is760_devtype, },
  1054. { .compatible = "nxp,sc16is762", .data = &sc16is762_devtype, },
  1055. { }
  1056. };
  1057. MODULE_DEVICE_TABLE(of, sc16is7xx_dt_ids);
  1058. static struct regmap_config regcfg = {
  1059. .reg_bits = 7,
  1060. .pad_bits = 1,
  1061. .val_bits = 8,
  1062. .cache_type = REGCACHE_RBTREE,
  1063. .volatile_reg = sc16is7xx_regmap_volatile,
  1064. .precious_reg = sc16is7xx_regmap_precious,
  1065. };
  1066. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1067. static int sc16is7xx_spi_probe(struct spi_device *spi)
  1068. {
  1069. struct sc16is7xx_devtype *devtype;
  1070. unsigned long flags = 0;
  1071. struct regmap *regmap;
  1072. int ret;
  1073. /* Setup SPI bus */
  1074. spi->bits_per_word = 8;
  1075. /* only supports mode 0 on SC16IS762 */
  1076. spi->mode = spi->mode ? : SPI_MODE_0;
  1077. spi->max_speed_hz = spi->max_speed_hz ? : 15000000;
  1078. ret = spi_setup(spi);
  1079. if (ret)
  1080. return ret;
  1081. if (spi->dev.of_node) {
  1082. const struct of_device_id *of_id =
  1083. of_match_device(sc16is7xx_dt_ids, &spi->dev);
  1084. devtype = (struct sc16is7xx_devtype *)of_id->data;
  1085. } else {
  1086. const struct spi_device_id *id_entry = spi_get_device_id(spi);
  1087. devtype = (struct sc16is7xx_devtype *)id_entry->driver_data;
  1088. flags = IRQF_TRIGGER_FALLING;
  1089. }
  1090. regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
  1091. (devtype->nr_uart - 1);
  1092. regmap = devm_regmap_init_spi(spi, &regcfg);
  1093. return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq, flags);
  1094. }
  1095. static int sc16is7xx_spi_remove(struct spi_device *spi)
  1096. {
  1097. return sc16is7xx_remove(&spi->dev);
  1098. }
  1099. static const struct spi_device_id sc16is7xx_spi_id_table[] = {
  1100. { "sc16is74x", (kernel_ulong_t)&sc16is74x_devtype, },
  1101. { "sc16is740", (kernel_ulong_t)&sc16is74x_devtype, },
  1102. { "sc16is741", (kernel_ulong_t)&sc16is74x_devtype, },
  1103. { "sc16is750", (kernel_ulong_t)&sc16is750_devtype, },
  1104. { "sc16is752", (kernel_ulong_t)&sc16is752_devtype, },
  1105. { "sc16is760", (kernel_ulong_t)&sc16is760_devtype, },
  1106. { "sc16is762", (kernel_ulong_t)&sc16is762_devtype, },
  1107. { }
  1108. };
  1109. MODULE_DEVICE_TABLE(spi, sc16is7xx_spi_id_table);
  1110. static struct spi_driver sc16is7xx_spi_uart_driver = {
  1111. .driver = {
  1112. .name = SC16IS7XX_NAME,
  1113. .owner = THIS_MODULE,
  1114. .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
  1115. },
  1116. .probe = sc16is7xx_spi_probe,
  1117. .remove = sc16is7xx_spi_remove,
  1118. .id_table = sc16is7xx_spi_id_table,
  1119. };
  1120. MODULE_ALIAS("spi:sc16is7xx");
  1121. #endif
  1122. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1123. static int sc16is7xx_i2c_probe(struct i2c_client *i2c,
  1124. const struct i2c_device_id *id)
  1125. {
  1126. struct sc16is7xx_devtype *devtype;
  1127. unsigned long flags = 0;
  1128. struct regmap *regmap;
  1129. if (i2c->dev.of_node) {
  1130. const struct of_device_id *of_id =
  1131. of_match_device(sc16is7xx_dt_ids, &i2c->dev);
  1132. devtype = (struct sc16is7xx_devtype *)of_id->data;
  1133. } else {
  1134. devtype = (struct sc16is7xx_devtype *)id->driver_data;
  1135. flags = IRQF_TRIGGER_FALLING;
  1136. }
  1137. regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
  1138. (devtype->nr_uart - 1);
  1139. regmap = devm_regmap_init_i2c(i2c, &regcfg);
  1140. return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq, flags);
  1141. }
  1142. static int sc16is7xx_i2c_remove(struct i2c_client *client)
  1143. {
  1144. return sc16is7xx_remove(&client->dev);
  1145. }
  1146. static const struct i2c_device_id sc16is7xx_i2c_id_table[] = {
  1147. { "sc16is74x", (kernel_ulong_t)&sc16is74x_devtype, },
  1148. { "sc16is740", (kernel_ulong_t)&sc16is74x_devtype, },
  1149. { "sc16is741", (kernel_ulong_t)&sc16is74x_devtype, },
  1150. { "sc16is750", (kernel_ulong_t)&sc16is750_devtype, },
  1151. { "sc16is752", (kernel_ulong_t)&sc16is752_devtype, },
  1152. { "sc16is760", (kernel_ulong_t)&sc16is760_devtype, },
  1153. { "sc16is762", (kernel_ulong_t)&sc16is762_devtype, },
  1154. { }
  1155. };
  1156. MODULE_DEVICE_TABLE(i2c, sc16is7xx_i2c_id_table);
  1157. static struct i2c_driver sc16is7xx_i2c_uart_driver = {
  1158. .driver = {
  1159. .name = SC16IS7XX_NAME,
  1160. .owner = THIS_MODULE,
  1161. .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
  1162. },
  1163. .probe = sc16is7xx_i2c_probe,
  1164. .remove = sc16is7xx_i2c_remove,
  1165. .id_table = sc16is7xx_i2c_id_table,
  1166. };
  1167. MODULE_ALIAS("i2c:sc16is7xx");
  1168. #endif
  1169. static int __init sc16is7xx_init(void)
  1170. {
  1171. int ret = 0;
  1172. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1173. ret = i2c_add_driver(&sc16is7xx_i2c_uart_driver);
  1174. if (ret < 0) {
  1175. pr_err("failed to init sc16is7xx i2c --> %d\n", ret);
  1176. return ret;
  1177. }
  1178. #endif
  1179. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1180. ret = spi_register_driver(&sc16is7xx_spi_uart_driver);
  1181. if (ret < 0) {
  1182. pr_err("failed to init sc16is7xx spi --> %d\n", ret);
  1183. return ret;
  1184. }
  1185. #endif
  1186. return ret;
  1187. }
  1188. module_init(sc16is7xx_init);
  1189. static void __exit sc16is7xx_exit(void)
  1190. {
  1191. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1192. i2c_del_driver(&sc16is7xx_i2c_uart_driver);
  1193. #endif
  1194. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1195. spi_unregister_driver(&sc16is7xx_spi_uart_driver);
  1196. #endif
  1197. }
  1198. module_exit(sc16is7xx_exit);
  1199. MODULE_LICENSE("GPL");
  1200. MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>");
  1201. MODULE_DESCRIPTION("SC16IS7XX serial driver");