sc16is7xx.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SC16IS7xx tty serial driver - common code
  4. *
  5. * Copyright (C) 2014 GridPoint
  6. * Author: Jon Ringle <jringle@gridpoint.com>
  7. * Based on max310x.c, by Alexander Shiyan <shc_work@mail.ru>
  8. */
  9. #undef DEFAULT_SYMBOL_NAMESPACE
  10. #define DEFAULT_SYMBOL_NAMESPACE "SERIAL_NXP_SC16IS7XX"
  11. #include <linux/bits.h>
  12. #include <linux/cleanup.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/export.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/idr.h>
  20. #include <linux/kthread.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/module.h>
  23. #include <linux/property.h>
  24. #include <linux/regmap.h>
  25. #include <linux/sched.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/serial.h>
  28. #include <linux/string.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/units.h>
  33. #include "sc16is7xx.h"
  34. #define SC16IS7XX_MAX_DEVS 8
  35. /* SC16IS7XX register definitions */
  36. #define SC16IS7XX_RHR_REG (0x00) /* RX FIFO */
  37. #define SC16IS7XX_THR_REG (0x00) /* TX FIFO */
  38. #define SC16IS7XX_IER_REG (0x01) /* Interrupt enable */
  39. #define SC16IS7XX_IIR_REG (0x02) /* Interrupt Identification */
  40. #define SC16IS7XX_FCR_REG (0x02) /* FIFO control */
  41. #define SC16IS7XX_LCR_REG (0x03) /* Line Control */
  42. #define SC16IS7XX_MCR_REG (0x04) /* Modem Control */
  43. #define SC16IS7XX_LSR_REG (0x05) /* Line Status */
  44. #define SC16IS7XX_MSR_REG (0x06) /* Modem Status */
  45. #define SC16IS7XX_SPR_REG (0x07) /* Scratch Pad */
  46. #define SC16IS7XX_TXLVL_REG (0x08) /* TX FIFO level */
  47. #define SC16IS7XX_RXLVL_REG (0x09) /* RX FIFO level */
  48. #define SC16IS7XX_IODIR_REG (0x0a) /* I/O Direction - only on 75x/76x */
  49. #define SC16IS7XX_IOSTATE_REG (0x0b) /* I/O State - only on 75x/76x */
  50. #define SC16IS7XX_IOINTENA_REG (0x0c) /* I/O Interrupt Enable - only on 75x/76x */
  51. #define SC16IS7XX_IOCONTROL_REG (0x0e) /* I/O Control - only on 75x/76x */
  52. #define SC16IS7XX_EFCR_REG (0x0f) /* Extra Features Control */
  53. /* TCR/TLR Register set: Only if ((MCR[2] == 1) && (EFR[4] == 1)) */
  54. #define SC16IS7XX_TCR_REG (0x06) /* Transmit control */
  55. #define SC16IS7XX_TLR_REG (0x07) /* Trigger level */
  56. /* Special Register set: Only if ((LCR[7] == 1) && (LCR != 0xBF)) */
  57. #define SC16IS7XX_DLL_REG (0x00) /* Divisor Latch Low */
  58. #define SC16IS7XX_DLH_REG (0x01) /* Divisor Latch High */
  59. /* Enhanced Register set: Only if (LCR == 0xBF) */
  60. #define SC16IS7XX_EFR_REG (0x02) /* Enhanced Features */
  61. #define SC16IS7XX_XON1_REG (0x04) /* Xon1 word */
  62. #define SC16IS7XX_XON2_REG (0x05) /* Xon2 word */
  63. #define SC16IS7XX_XOFF1_REG (0x06) /* Xoff1 word */
  64. #define SC16IS7XX_XOFF2_REG (0x07) /* Xoff2 word */
  65. /* IER register bits */
  66. #define SC16IS7XX_IER_RDI_BIT BIT(0) /* Enable RX data interrupt */
  67. #define SC16IS7XX_IER_THRI_BIT BIT(1) /* Enable TX holding register interrupt */
  68. #define SC16IS7XX_IER_RLSI_BIT BIT(2) /* Enable RX line status interrupt */
  69. #define SC16IS7XX_IER_MSI_BIT BIT(3) /* Enable Modem status interrupt */
  70. /* IER register bits - write only if (EFR[4] == 1) */
  71. #define SC16IS7XX_IER_SLEEP_BIT BIT(4) /* Enable Sleep mode */
  72. #define SC16IS7XX_IER_XOFFI_BIT BIT(5) /* Enable Xoff interrupt */
  73. #define SC16IS7XX_IER_RTSI_BIT BIT(6) /* Enable nRTS interrupt */
  74. #define SC16IS7XX_IER_CTSI_BIT BIT(7) /* Enable nCTS interrupt */
  75. /* FCR register bits */
  76. #define SC16IS7XX_FCR_FIFO_BIT BIT(0) /* Enable FIFO */
  77. #define SC16IS7XX_FCR_RXRESET_BIT BIT(1) /* Reset RX FIFO */
  78. #define SC16IS7XX_FCR_TXRESET_BIT BIT(2) /* Reset TX FIFO */
  79. #define SC16IS7XX_FCR_RXLVLL_BIT BIT(6) /* RX Trigger level LSB */
  80. #define SC16IS7XX_FCR_RXLVLH_BIT BIT(7) /* RX Trigger level MSB */
  81. /* FCR register bits - write only if (EFR[4] == 1) */
  82. #define SC16IS7XX_FCR_TXLVLL_BIT BIT(4) /* TX Trigger level LSB */
  83. #define SC16IS7XX_FCR_TXLVLH_BIT BIT(5) /* TX Trigger level MSB */
  84. /* IIR register bits */
  85. #define SC16IS7XX_IIR_NO_INT_BIT 0x01 /* No interrupts pending */
  86. #define SC16IS7XX_IIR_ID_MASK GENMASK(5, 1) /* Mask for the interrupt ID */
  87. #define SC16IS7XX_IIR_THRI_SRC 0x02 /* TX holding register empty */
  88. #define SC16IS7XX_IIR_RDI_SRC 0x04 /* RX data interrupt */
  89. #define SC16IS7XX_IIR_RLSE_SRC 0x06 /* RX line status error */
  90. #define SC16IS7XX_IIR_RTOI_SRC 0x0c /* RX time-out interrupt */
  91. #define SC16IS7XX_IIR_MSI_SRC 0x00 /* Modem status interrupt
  92. * - only on 75x/76x
  93. */
  94. #define SC16IS7XX_IIR_INPIN_SRC 0x30 /* Input pin change of state
  95. * - only on 75x/76x
  96. */
  97. #define SC16IS7XX_IIR_XOFFI_SRC 0x10 /* Received Xoff */
  98. #define SC16IS7XX_IIR_CTSRTS_SRC 0x20 /* nCTS,nRTS change of state from active
  99. * (LOW) to inactive (HIGH)
  100. */
  101. /* LCR register bits */
  102. #define SC16IS7XX_LCR_LENGTH0_BIT BIT(0) /* Word length bit 0 */
  103. #define SC16IS7XX_LCR_LENGTH1_BIT BIT(1) /* Word length bit 1
  104. *
  105. * Word length bits table:
  106. * 00 -> 5 bit words
  107. * 01 -> 6 bit words
  108. * 10 -> 7 bit words
  109. * 11 -> 8 bit words
  110. */
  111. #define SC16IS7XX_LCR_STOPLEN_BIT BIT(2) /* STOP length bit
  112. *
  113. * STOP length bit table:
  114. * 0 -> 1 stop bit
  115. * 1 -> 1-1.5 stop bits if word length is 5,
  116. * 2 stop bits otherwise
  117. */
  118. #define SC16IS7XX_LCR_PARITY_BIT BIT(3) /* Parity bit enable */
  119. #define SC16IS7XX_LCR_EVENPARITY_BIT BIT(4) /* Even parity bit enable */
  120. #define SC16IS7XX_LCR_FORCEPARITY_BIT BIT(5) /* 9-bit multidrop parity */
  121. #define SC16IS7XX_LCR_TXBREAK_BIT BIT(6) /* TX break enable */
  122. #define SC16IS7XX_LCR_DLAB_BIT BIT(7) /* Divisor Latch enable */
  123. #define SC16IS7XX_LCR_WORD_LEN_5 (0x00)
  124. #define SC16IS7XX_LCR_WORD_LEN_6 (0x01)
  125. #define SC16IS7XX_LCR_WORD_LEN_7 (0x02)
  126. #define SC16IS7XX_LCR_WORD_LEN_8 (0x03)
  127. #define SC16IS7XX_LCR_REG_SET_SPECIAL SC16IS7XX_LCR_DLAB_BIT /* Special reg set */
  128. #define SC16IS7XX_LCR_REG_SET_ENHANCED 0xBF /* Enhanced reg set */
  129. /* MCR register bits */
  130. #define SC16IS7XX_MCR_DTR_BIT BIT(0) /* DTR complement - only on 75x/76x */
  131. #define SC16IS7XX_MCR_RTS_BIT BIT(1) /* RTS complement */
  132. #define SC16IS7XX_MCR_TCRTLR_BIT BIT(2) /* TCR/TLR registers enable */
  133. #define SC16IS7XX_MCR_LOOP_BIT BIT(4) /* Enable loopback test mode */
  134. #define SC16IS7XX_MCR_XONANY_BIT BIT(5) /* Enable Xon Any
  135. * - write enabled if (EFR[4] == 1)
  136. */
  137. #define SC16IS7XX_MCR_IRDA_BIT BIT(6) /* Enable IrDA mode
  138. * - write enabled if (EFR[4] == 1)
  139. */
  140. #define SC16IS7XX_MCR_CLKSEL_BIT BIT(7) /* Divide clock by 4
  141. * - write enabled if (EFR[4] == 1)
  142. */
  143. /* LSR register bits */
  144. #define SC16IS7XX_LSR_DR_BIT BIT(0) /* Receiver data ready */
  145. #define SC16IS7XX_LSR_OE_BIT BIT(1) /* Overrun Error */
  146. #define SC16IS7XX_LSR_PE_BIT BIT(2) /* Parity Error */
  147. #define SC16IS7XX_LSR_FE_BIT BIT(3) /* Frame Error */
  148. #define SC16IS7XX_LSR_BI_BIT BIT(4) /* Break Interrupt */
  149. #define SC16IS7XX_LSR_BRK_ERROR_MASK \
  150. (SC16IS7XX_LSR_OE_BIT | \
  151. SC16IS7XX_LSR_PE_BIT | \
  152. SC16IS7XX_LSR_FE_BIT | \
  153. SC16IS7XX_LSR_BI_BIT)
  154. #define SC16IS7XX_LSR_THRE_BIT BIT(5) /* TX holding register empty */
  155. #define SC16IS7XX_LSR_TEMT_BIT BIT(6) /* Transmitter empty */
  156. #define SC16IS7XX_LSR_FIFOE_BIT BIT(7) /* Fifo Error */
  157. /* MSR register bits */
  158. #define SC16IS7XX_MSR_DCTS_BIT BIT(0) /* Delta CTS Clear To Send */
  159. #define SC16IS7XX_MSR_DDSR_BIT BIT(1) /* Delta DSR Data Set Ready or (IO4)
  160. * - only on 75x/76x
  161. */
  162. #define SC16IS7XX_MSR_DRI_BIT BIT(2) /* Delta RI Ring Indicator or (IO7)
  163. * - only on 75x/76x
  164. */
  165. #define SC16IS7XX_MSR_DCD_BIT BIT(3) /* Delta CD Carrier Detect or (IO6)
  166. * - only on 75x/76x
  167. */
  168. #define SC16IS7XX_MSR_CTS_BIT BIT(4) /* CTS */
  169. #define SC16IS7XX_MSR_DSR_BIT BIT(5) /* DSR (IO4) - only on 75x/76x */
  170. #define SC16IS7XX_MSR_RI_BIT BIT(6) /* RI (IO7) - only on 75x/76x */
  171. #define SC16IS7XX_MSR_CD_BIT BIT(7) /* CD (IO6) - only on 75x/76x */
  172. /*
  173. * TCR register bits
  174. * TCR trigger levels are available from 0 to 60 characters with a granularity
  175. * of four.
  176. * The programmer must program the TCR such that TCR[3:0] > TCR[7:4]. There is
  177. * no built-in hardware check to make sure this condition is met. Also, the TCR
  178. * must be programmed with this condition before auto RTS or software flow
  179. * control is enabled to avoid spurious operation of the device.
  180. */
  181. #define SC16IS7XX_TCR_RX_HALT(words) ((((words) / 4) & 0x0f) << 0)
  182. #define SC16IS7XX_TCR_RX_RESUME(words) ((((words) / 4) & 0x0f) << 4)
  183. /*
  184. * TLR register bits
  185. * If TLR[3:0] or TLR[7:4] are logical 0, the selectable trigger levels via the
  186. * FIFO Control Register (FCR) are used for the transmit and receive FIFO
  187. * trigger levels. Trigger levels from 4 characters to 60 characters are
  188. * available with a granularity of four.
  189. *
  190. * When the trigger level setting in TLR is zero, the SC16IS74x/75x/76x uses the
  191. * trigger level setting defined in FCR. If TLR has non-zero trigger level value
  192. * the trigger level defined in FCR is discarded. This applies to both transmit
  193. * FIFO and receive FIFO trigger level setting.
  194. *
  195. * When TLR is used for RX trigger level control, FCR[7:6] should be left at the
  196. * default state, that is, '00'.
  197. */
  198. #define SC16IS7XX_TLR_TX_TRIGGER(words) ((((words) / 4) & 0x0f) << 0)
  199. #define SC16IS7XX_TLR_RX_TRIGGER(words) ((((words) / 4) & 0x0f) << 4)
  200. /* IOControl register bits (Only 75x/76x) */
  201. #define SC16IS7XX_IOCONTROL_LATCH_BIT BIT(0) /* Enable input latching */
  202. #define SC16IS7XX_IOCONTROL_MODEM_A_BIT BIT(1) /* Enable GPIO[7:4] as modem A pins */
  203. #define SC16IS7XX_IOCONTROL_MODEM_B_BIT BIT(2) /* Enable GPIO[3:0] as modem B pins */
  204. #define SC16IS7XX_IOCONTROL_SRESET_BIT BIT(3) /* Software Reset */
  205. /* EFCR register bits */
  206. #define SC16IS7XX_EFCR_9BIT_MODE_BIT BIT(0) /* Enable 9-bit or Multidrop mode (RS485) */
  207. #define SC16IS7XX_EFCR_RXDISABLE_BIT BIT(1) /* Disable receiver */
  208. #define SC16IS7XX_EFCR_TXDISABLE_BIT BIT(2) /* Disable transmitter */
  209. #define SC16IS7XX_EFCR_AUTO_RS485_BIT BIT(4) /* Auto RS485 RTS direction */
  210. #define SC16IS7XX_EFCR_RTS_INVERT_BIT BIT(5) /* RTS output inversion */
  211. #define SC16IS7XX_EFCR_IRDA_MODE_BIT BIT(7) /* IrDA mode
  212. * 0 = rate up to 115.2 kbit/s - Only 75x/76x
  213. * 1 = rate up to 1.152 Mbit/s - Only 76x
  214. */
  215. /* EFR register bits */
  216. #define SC16IS7XX_EFR_AUTORTS_BIT BIT(6) /* Auto RTS flow ctrl enable */
  217. #define SC16IS7XX_EFR_AUTOCTS_BIT BIT(7) /* Auto CTS flow ctrl enable */
  218. #define SC16IS7XX_EFR_XOFF2_DETECT_BIT BIT(5) /* Enable Xoff2 detection */
  219. #define SC16IS7XX_EFR_ENABLE_BIT BIT(4) /* Enable enhanced functions and writing to
  220. * IER[7:4], FCR[5:4], MCR[7:5]
  221. */
  222. #define SC16IS7XX_EFR_SWFLOW3_BIT BIT(3)
  223. #define SC16IS7XX_EFR_SWFLOW2_BIT BIT(2)
  224. /*
  225. * SWFLOW bits 3 & 2 table:
  226. * 00 -> no transmitter flow control
  227. * 01 -> transmitter generates XON2 and XOFF2
  228. * 10 -> transmitter generates XON1 and XOFF1
  229. * 11 -> transmitter generates XON1, XON2,
  230. * XOFF1 and XOFF2
  231. */
  232. #define SC16IS7XX_EFR_SWFLOW1_BIT BIT(1)
  233. #define SC16IS7XX_EFR_SWFLOW0_BIT BIT(0)
  234. /*
  235. * SWFLOW bits 1 & 0 table:
  236. * 00 -> no received flow control
  237. * 01 -> receiver compares XON2 and XOFF2
  238. * 10 -> receiver compares XON1 and XOFF1
  239. * 11 -> receiver compares XON1, XON2,
  240. * XOFF1 and XOFF2
  241. */
  242. #define SC16IS7XX_EFR_FLOWCTRL_BITS (SC16IS7XX_EFR_AUTORTS_BIT | \
  243. SC16IS7XX_EFR_AUTOCTS_BIT | \
  244. SC16IS7XX_EFR_XOFF2_DETECT_BIT | \
  245. SC16IS7XX_EFR_SWFLOW3_BIT | \
  246. SC16IS7XX_EFR_SWFLOW2_BIT | \
  247. SC16IS7XX_EFR_SWFLOW1_BIT | \
  248. SC16IS7XX_EFR_SWFLOW0_BIT)
  249. /* Misc definitions */
  250. #define SC16IS7XX_FIFO_SIZE (64)
  251. #define SC16IS7XX_GPIOS_PER_BANK 4
  252. #define SC16IS7XX_POLL_PERIOD_MS 10
  253. #define SC16IS7XX_RECONF_MD BIT(0)
  254. #define SC16IS7XX_RECONF_IER BIT(1)
  255. #define SC16IS7XX_RECONF_RS485 BIT(2)
  256. struct sc16is7xx_one_config {
  257. unsigned int flags;
  258. u8 ier_mask;
  259. u8 ier_val;
  260. };
  261. struct sc16is7xx_one {
  262. struct uart_port port;
  263. struct regmap *regmap;
  264. struct mutex lock; /* For registers sharing same address space. */
  265. struct kthread_work tx_work;
  266. struct kthread_work reg_work;
  267. struct kthread_delayed_work ms_work;
  268. struct sc16is7xx_one_config config;
  269. unsigned char buf[SC16IS7XX_FIFO_SIZE]; /* Rx buffer. */
  270. unsigned int old_mctrl;
  271. u8 old_lcr; /* Value before EFR access. */
  272. bool irda_mode;
  273. };
  274. struct sc16is7xx_port {
  275. const struct sc16is7xx_devtype *devtype;
  276. struct clk *clk;
  277. #ifdef CONFIG_GPIOLIB
  278. struct gpio_chip gpio;
  279. unsigned long gpio_valid_mask;
  280. #endif
  281. u8 mctrl_mask;
  282. struct kthread_worker kworker;
  283. struct task_struct *kworker_task;
  284. struct kthread_delayed_work poll_work;
  285. bool polling;
  286. struct sc16is7xx_one p[];
  287. };
  288. static DEFINE_IDA(sc16is7xx_lines);
  289. static struct uart_driver sc16is7xx_uart = {
  290. .owner = THIS_MODULE,
  291. .driver_name = KBUILD_MODNAME,
  292. .dev_name = "ttySC",
  293. .nr = SC16IS7XX_MAX_DEVS,
  294. };
  295. #define to_sc16is7xx_one(p) container_of((p), struct sc16is7xx_one, port)
  296. static u8 sc16is7xx_port_read(struct uart_port *port, u8 reg)
  297. {
  298. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  299. unsigned int val = 0;
  300. regmap_read(one->regmap, reg, &val);
  301. return val;
  302. }
  303. static void sc16is7xx_port_write(struct uart_port *port, u8 reg, u8 val)
  304. {
  305. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  306. regmap_write(one->regmap, reg, val);
  307. }
  308. static void sc16is7xx_fifo_read(struct uart_port *port, u8 *rxbuf, unsigned int rxlen)
  309. {
  310. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  311. regmap_noinc_read(one->regmap, SC16IS7XX_RHR_REG, rxbuf, rxlen);
  312. }
  313. static void sc16is7xx_fifo_write(struct uart_port *port, u8 *txbuf, u8 to_send)
  314. {
  315. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  316. /*
  317. * Don't send zero-length data, at least on SPI it confuses the chip
  318. * delivering wrong TXLVL data.
  319. */
  320. if (unlikely(!to_send))
  321. return;
  322. regmap_noinc_write(one->regmap, SC16IS7XX_THR_REG, txbuf, to_send);
  323. }
  324. static void sc16is7xx_port_update(struct uart_port *port, u8 reg,
  325. u8 mask, u8 val)
  326. {
  327. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  328. regmap_update_bits(one->regmap, reg, mask, val);
  329. }
  330. static void sc16is7xx_power(struct uart_port *port, int on)
  331. {
  332. sc16is7xx_port_update(port, SC16IS7XX_IER_REG,
  333. SC16IS7XX_IER_SLEEP_BIT,
  334. on ? 0 : SC16IS7XX_IER_SLEEP_BIT);
  335. }
  336. /*
  337. * In an amazing feat of design, the enhanced register set shares the
  338. * addresses 0x02 and 0x04-0x07 with the general register set.
  339. * The special register set also shares the addresses 0x00-0x01 with the
  340. * general register set.
  341. *
  342. * Access to the enhanced or special register set is enabled by writing a magic
  343. * value to the Line Control Register (LCR). When enhanced register set access
  344. * is enabled, for example, any interrupt firing during this time will see the
  345. * EFR where it expects the IIR to be, leading to
  346. * "Unexpected interrupt" messages.
  347. *
  348. * Prevent this possibility by claiming a mutex when access to the enhanced
  349. * or special register set is enabled, and claiming the same mutex from within
  350. * the interrupt handler. This is similar to disabling the interrupt, but that
  351. * doesn't work because the bulk of the interrupt processing is run as a
  352. * workqueue job in thread context.
  353. */
  354. static void sc16is7xx_regs_lock(struct uart_port *port, u8 register_set)
  355. {
  356. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  357. mutex_lock(&one->lock);
  358. /* Backup content of LCR. */
  359. one->old_lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG);
  360. /* Enable access to the desired register set */
  361. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, register_set);
  362. /* Disable cache updates when writing to non-general registers */
  363. regcache_cache_bypass(one->regmap, true);
  364. }
  365. static void sc16is7xx_regs_unlock(struct uart_port *port)
  366. {
  367. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  368. /* Re-enable cache updates when writing to general registers */
  369. regcache_cache_bypass(one->regmap, false);
  370. /* Restore original content of LCR */
  371. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, one->old_lcr);
  372. mutex_unlock(&one->lock);
  373. }
  374. static void sc16is7xx_ier_clear(struct uart_port *port, u8 bit)
  375. {
  376. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  377. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  378. lockdep_assert_held_once(&port->lock);
  379. one->config.flags |= SC16IS7XX_RECONF_IER;
  380. one->config.ier_mask |= bit;
  381. one->config.ier_val &= ~bit;
  382. kthread_queue_work(&s->kworker, &one->reg_work);
  383. }
  384. static void sc16is7xx_ier_set(struct uart_port *port, u8 bit)
  385. {
  386. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  387. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  388. lockdep_assert_held_once(&port->lock);
  389. one->config.flags |= SC16IS7XX_RECONF_IER;
  390. one->config.ier_mask |= bit;
  391. one->config.ier_val |= bit;
  392. kthread_queue_work(&s->kworker, &one->reg_work);
  393. }
  394. static void sc16is7xx_stop_tx(struct uart_port *port)
  395. {
  396. sc16is7xx_ier_clear(port, SC16IS7XX_IER_THRI_BIT);
  397. }
  398. static void sc16is7xx_stop_rx(struct uart_port *port)
  399. {
  400. sc16is7xx_ier_clear(port, SC16IS7XX_IER_RDI_BIT);
  401. }
  402. const struct sc16is7xx_devtype sc16is74x_devtype = {
  403. .name = "SC16IS74X",
  404. .nr_gpio = 0,
  405. .nr_uart = 1,
  406. };
  407. EXPORT_SYMBOL_GPL(sc16is74x_devtype);
  408. const struct sc16is7xx_devtype sc16is750_devtype = {
  409. .name = "SC16IS750",
  410. .nr_gpio = 8,
  411. .nr_uart = 1,
  412. };
  413. EXPORT_SYMBOL_GPL(sc16is750_devtype);
  414. const struct sc16is7xx_devtype sc16is752_devtype = {
  415. .name = "SC16IS752",
  416. .nr_gpio = 8,
  417. .nr_uart = 2,
  418. };
  419. EXPORT_SYMBOL_GPL(sc16is752_devtype);
  420. const struct sc16is7xx_devtype sc16is760_devtype = {
  421. .name = "SC16IS760",
  422. .nr_gpio = 8,
  423. .nr_uart = 1,
  424. };
  425. EXPORT_SYMBOL_GPL(sc16is760_devtype);
  426. const struct sc16is7xx_devtype sc16is762_devtype = {
  427. .name = "SC16IS762",
  428. .nr_gpio = 8,
  429. .nr_uart = 2,
  430. };
  431. EXPORT_SYMBOL_GPL(sc16is762_devtype);
  432. static bool sc16is7xx_regmap_volatile(struct device *dev, unsigned int reg)
  433. {
  434. switch (reg) {
  435. case SC16IS7XX_RHR_REG: /* Shared address space with THR & DLL */
  436. case SC16IS7XX_IIR_REG: /* Shared address space with FCR & EFR */
  437. case SC16IS7XX_LSR_REG: /* Shared address space with XON2 */
  438. case SC16IS7XX_MSR_REG: /* Shared address space with TCR & XOFF1 */
  439. case SC16IS7XX_SPR_REG: /* Shared address space with TLR & XOFF2 */
  440. case SC16IS7XX_TXLVL_REG:
  441. case SC16IS7XX_RXLVL_REG:
  442. case SC16IS7XX_IOSTATE_REG:
  443. case SC16IS7XX_IOCONTROL_REG:
  444. return true;
  445. default:
  446. return false;
  447. }
  448. }
  449. static bool sc16is7xx_regmap_precious(struct device *dev, unsigned int reg)
  450. {
  451. switch (reg) {
  452. case SC16IS7XX_RHR_REG:
  453. return true;
  454. default:
  455. return false;
  456. }
  457. }
  458. static bool sc16is7xx_regmap_noinc(struct device *dev, unsigned int reg)
  459. {
  460. return reg == SC16IS7XX_RHR_REG;
  461. }
  462. /*
  463. * Configure programmable baud rate generator (divisor) according to the
  464. * desired baud rate.
  465. *
  466. * From the datasheet, the divisor is computed according to:
  467. *
  468. * XTAL1 input frequency
  469. * -----------------------
  470. * prescaler
  471. * divisor = ---------------------------
  472. * baud-rate x sampling-rate
  473. */
  474. static int sc16is7xx_set_baud(struct uart_port *port, int baud)
  475. {
  476. unsigned int prescaler = 1;
  477. unsigned long clk = port->uartclk, div = clk / 16 / baud;
  478. if (div >= BIT(16)) {
  479. prescaler = 4;
  480. div /= prescaler;
  481. }
  482. /* If bit MCR_CLKSEL is set, the divide by 4 prescaler is activated. */
  483. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  484. SC16IS7XX_MCR_CLKSEL_BIT,
  485. prescaler == 1 ? 0 : SC16IS7XX_MCR_CLKSEL_BIT);
  486. /* Access special register set (DLL/DLH) */
  487. sc16is7xx_regs_lock(port, SC16IS7XX_LCR_REG_SET_SPECIAL);
  488. /* Write the new divisor */
  489. sc16is7xx_port_write(port, SC16IS7XX_DLH_REG, div / 256);
  490. sc16is7xx_port_write(port, SC16IS7XX_DLL_REG, div % 256);
  491. /* Restore access to general register set */
  492. sc16is7xx_regs_unlock(port);
  493. return DIV_ROUND_CLOSEST((clk / prescaler) / 16, div);
  494. }
  495. static void sc16is7xx_handle_rx(struct uart_port *port, unsigned int rxlen,
  496. unsigned int iir)
  497. {
  498. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  499. unsigned int lsr = 0, bytes_read, i;
  500. bool read_lsr = (iir == SC16IS7XX_IIR_RLSE_SRC);
  501. u8 ch, flag;
  502. if (unlikely(rxlen >= sizeof(one->buf))) {
  503. dev_warn_ratelimited(port->dev,
  504. "ttySC%i: Possible RX FIFO overrun: %d\n",
  505. port->line, rxlen);
  506. port->icount.buf_overrun++;
  507. /* Ensure sanity of RX level */
  508. rxlen = sizeof(one->buf);
  509. }
  510. while (rxlen) {
  511. /* Only read lsr if there are possible errors in FIFO */
  512. if (read_lsr) {
  513. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  514. if (!(lsr & SC16IS7XX_LSR_FIFOE_BIT))
  515. read_lsr = false; /* No errors left in FIFO */
  516. } else
  517. lsr = 0;
  518. if (read_lsr) {
  519. one->buf[0] = sc16is7xx_port_read(port, SC16IS7XX_RHR_REG);
  520. bytes_read = 1;
  521. } else {
  522. sc16is7xx_fifo_read(port, one->buf, rxlen);
  523. bytes_read = rxlen;
  524. }
  525. lsr &= SC16IS7XX_LSR_BRK_ERROR_MASK;
  526. port->icount.rx++;
  527. flag = TTY_NORMAL;
  528. if (unlikely(lsr)) {
  529. if (lsr & SC16IS7XX_LSR_BI_BIT) {
  530. port->icount.brk++;
  531. if (uart_handle_break(port))
  532. continue;
  533. } else if (lsr & SC16IS7XX_LSR_PE_BIT)
  534. port->icount.parity++;
  535. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  536. port->icount.frame++;
  537. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  538. port->icount.overrun++;
  539. lsr &= port->read_status_mask;
  540. if (lsr & SC16IS7XX_LSR_BI_BIT)
  541. flag = TTY_BREAK;
  542. else if (lsr & SC16IS7XX_LSR_PE_BIT)
  543. flag = TTY_PARITY;
  544. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  545. flag = TTY_FRAME;
  546. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  547. flag = TTY_OVERRUN;
  548. }
  549. for (i = 0; i < bytes_read; ++i) {
  550. ch = one->buf[i];
  551. if (uart_handle_sysrq_char(port, ch))
  552. continue;
  553. if (lsr & port->ignore_status_mask)
  554. continue;
  555. uart_insert_char(port, lsr, SC16IS7XX_LSR_OE_BIT, ch,
  556. flag);
  557. }
  558. rxlen -= bytes_read;
  559. }
  560. tty_flip_buffer_push(&port->state->port);
  561. }
  562. static void sc16is7xx_handle_tx(struct uart_port *port)
  563. {
  564. struct tty_port *tport = &port->state->port;
  565. unsigned long flags;
  566. unsigned int txlen;
  567. unsigned char *tail;
  568. if (unlikely(port->x_char)) {
  569. sc16is7xx_port_write(port, SC16IS7XX_THR_REG, port->x_char);
  570. port->icount.tx++;
  571. port->x_char = 0;
  572. return;
  573. }
  574. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) {
  575. uart_port_lock_irqsave(port, &flags);
  576. sc16is7xx_stop_tx(port);
  577. uart_port_unlock_irqrestore(port, flags);
  578. return;
  579. }
  580. /* Limit to space available in TX FIFO */
  581. txlen = sc16is7xx_port_read(port, SC16IS7XX_TXLVL_REG);
  582. if (txlen > SC16IS7XX_FIFO_SIZE) {
  583. dev_err_ratelimited(port->dev,
  584. "chip reports %d free bytes in TX fifo, but it only has %d",
  585. txlen, SC16IS7XX_FIFO_SIZE);
  586. txlen = 0;
  587. }
  588. txlen = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, txlen);
  589. sc16is7xx_fifo_write(port, tail, txlen);
  590. uart_xmit_advance(port, txlen);
  591. uart_port_lock_irqsave(port, &flags);
  592. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  593. uart_write_wakeup(port);
  594. if (kfifo_is_empty(&tport->xmit_fifo))
  595. sc16is7xx_stop_tx(port);
  596. else
  597. sc16is7xx_ier_set(port, SC16IS7XX_IER_THRI_BIT);
  598. uart_port_unlock_irqrestore(port, flags);
  599. }
  600. static unsigned int sc16is7xx_get_hwmctrl(struct uart_port *port)
  601. {
  602. u8 msr = sc16is7xx_port_read(port, SC16IS7XX_MSR_REG);
  603. unsigned int mctrl = 0;
  604. mctrl |= (msr & SC16IS7XX_MSR_CTS_BIT) ? TIOCM_CTS : 0;
  605. mctrl |= (msr & SC16IS7XX_MSR_DSR_BIT) ? TIOCM_DSR : 0;
  606. mctrl |= (msr & SC16IS7XX_MSR_CD_BIT) ? TIOCM_CAR : 0;
  607. mctrl |= (msr & SC16IS7XX_MSR_RI_BIT) ? TIOCM_RNG : 0;
  608. return mctrl;
  609. }
  610. static void sc16is7xx_update_mlines(struct sc16is7xx_one *one)
  611. {
  612. struct uart_port *port = &one->port;
  613. unsigned long flags;
  614. unsigned int status, changed;
  615. /* Lock required as MSR address is shared with TCR and XOFF1. */
  616. lockdep_assert_held_once(&one->lock);
  617. status = sc16is7xx_get_hwmctrl(port);
  618. changed = status ^ one->old_mctrl;
  619. if (changed == 0)
  620. return;
  621. one->old_mctrl = status;
  622. uart_port_lock_irqsave(port, &flags);
  623. if ((changed & TIOCM_RNG) && (status & TIOCM_RNG))
  624. port->icount.rng++;
  625. if (changed & TIOCM_DSR)
  626. port->icount.dsr++;
  627. if (changed & TIOCM_CAR)
  628. uart_handle_dcd_change(port, status & TIOCM_CAR);
  629. if (changed & TIOCM_CTS)
  630. uart_handle_cts_change(port, status & TIOCM_CTS);
  631. wake_up_interruptible(&port->state->port.delta_msr_wait);
  632. uart_port_unlock_irqrestore(port, flags);
  633. }
  634. static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
  635. {
  636. unsigned int iir, rxlen;
  637. struct uart_port *port = &s->p[portno].port;
  638. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  639. guard(mutex)(&one->lock);
  640. iir = sc16is7xx_port_read(port, SC16IS7XX_IIR_REG);
  641. if (iir & SC16IS7XX_IIR_NO_INT_BIT)
  642. return false;
  643. iir &= SC16IS7XX_IIR_ID_MASK;
  644. switch (iir) {
  645. case SC16IS7XX_IIR_RDI_SRC:
  646. case SC16IS7XX_IIR_RLSE_SRC:
  647. case SC16IS7XX_IIR_RTOI_SRC:
  648. case SC16IS7XX_IIR_XOFFI_SRC:
  649. rxlen = sc16is7xx_port_read(port, SC16IS7XX_RXLVL_REG);
  650. /*
  651. * There is a silicon bug that makes the chip report a
  652. * time-out interrupt but no data in the FIFO. This is
  653. * described in errata section 18.1.4.
  654. *
  655. * When this happens, read one byte from the FIFO to
  656. * clear the interrupt.
  657. */
  658. if (iir == SC16IS7XX_IIR_RTOI_SRC && !rxlen)
  659. rxlen = 1;
  660. if (rxlen)
  661. sc16is7xx_handle_rx(port, rxlen, iir);
  662. break;
  663. /* CTSRTS interrupt comes only when CTS goes inactive */
  664. case SC16IS7XX_IIR_CTSRTS_SRC:
  665. case SC16IS7XX_IIR_MSI_SRC:
  666. sc16is7xx_update_mlines(one);
  667. break;
  668. case SC16IS7XX_IIR_THRI_SRC:
  669. sc16is7xx_handle_tx(port);
  670. break;
  671. default:
  672. dev_err_ratelimited(port->dev,
  673. "ttySC%i: Unexpected interrupt: %x",
  674. port->line, iir);
  675. break;
  676. }
  677. return true;
  678. }
  679. static irqreturn_t sc16is7xx_irq(int irq, void *dev_id)
  680. {
  681. struct sc16is7xx_port *s = dev_id;
  682. bool keep_polling;
  683. do {
  684. int i;
  685. keep_polling = false;
  686. for (i = 0; i < s->devtype->nr_uart; ++i)
  687. keep_polling |= sc16is7xx_port_irq(s, i);
  688. } while (keep_polling);
  689. return IRQ_HANDLED;
  690. }
  691. static void sc16is7xx_poll_proc(struct kthread_work *ws)
  692. {
  693. struct sc16is7xx_port *s = container_of(ws, struct sc16is7xx_port, poll_work.work);
  694. /* Reuse standard IRQ handler. Interrupt ID is unused in this context. */
  695. sc16is7xx_irq(0, s);
  696. /* Setup delay based on SC16IS7XX_POLL_PERIOD_MS */
  697. kthread_queue_delayed_work(&s->kworker, &s->poll_work,
  698. msecs_to_jiffies(SC16IS7XX_POLL_PERIOD_MS));
  699. }
  700. static void sc16is7xx_tx_proc(struct kthread_work *ws)
  701. {
  702. struct sc16is7xx_one *one = container_of(ws, struct sc16is7xx_one, tx_work);
  703. struct uart_port *port = &one->port;
  704. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  705. (port->rs485.delay_rts_before_send > 0))
  706. msleep(port->rs485.delay_rts_before_send);
  707. guard(mutex)(&one->lock);
  708. sc16is7xx_handle_tx(port);
  709. }
  710. static void sc16is7xx_reconf_rs485(struct uart_port *port)
  711. {
  712. const u32 mask = SC16IS7XX_EFCR_AUTO_RS485_BIT |
  713. SC16IS7XX_EFCR_RTS_INVERT_BIT;
  714. u32 efcr = 0;
  715. struct serial_rs485 *rs485 = &port->rs485;
  716. unsigned long irqflags;
  717. uart_port_lock_irqsave(port, &irqflags);
  718. if (rs485->flags & SER_RS485_ENABLED) {
  719. efcr |= SC16IS7XX_EFCR_AUTO_RS485_BIT;
  720. if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  721. efcr |= SC16IS7XX_EFCR_RTS_INVERT_BIT;
  722. }
  723. uart_port_unlock_irqrestore(port, irqflags);
  724. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG, mask, efcr);
  725. }
  726. static void sc16is7xx_reg_proc(struct kthread_work *ws)
  727. {
  728. struct sc16is7xx_one *one = container_of(ws, struct sc16is7xx_one, reg_work);
  729. struct sc16is7xx_one_config config;
  730. unsigned long irqflags;
  731. uart_port_lock_irqsave(&one->port, &irqflags);
  732. config = one->config;
  733. memset(&one->config, 0, sizeof(one->config));
  734. uart_port_unlock_irqrestore(&one->port, irqflags);
  735. if (config.flags & SC16IS7XX_RECONF_MD) {
  736. u8 mcr = 0;
  737. /* Device ignores RTS setting when hardware flow is enabled */
  738. if (one->port.mctrl & TIOCM_RTS)
  739. mcr |= SC16IS7XX_MCR_RTS_BIT;
  740. if (one->port.mctrl & TIOCM_DTR)
  741. mcr |= SC16IS7XX_MCR_DTR_BIT;
  742. if (one->port.mctrl & TIOCM_LOOP)
  743. mcr |= SC16IS7XX_MCR_LOOP_BIT;
  744. sc16is7xx_port_update(&one->port, SC16IS7XX_MCR_REG,
  745. SC16IS7XX_MCR_RTS_BIT |
  746. SC16IS7XX_MCR_DTR_BIT |
  747. SC16IS7XX_MCR_LOOP_BIT,
  748. mcr);
  749. }
  750. if (config.flags & SC16IS7XX_RECONF_IER)
  751. sc16is7xx_port_update(&one->port, SC16IS7XX_IER_REG,
  752. config.ier_mask, config.ier_val);
  753. if (config.flags & SC16IS7XX_RECONF_RS485)
  754. sc16is7xx_reconf_rs485(&one->port);
  755. }
  756. static void sc16is7xx_ms_proc(struct kthread_work *ws)
  757. {
  758. struct sc16is7xx_one *one = container_of(ws, struct sc16is7xx_one, ms_work.work);
  759. struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev);
  760. if (one->port.state) {
  761. scoped_guard(mutex, &one->lock)
  762. sc16is7xx_update_mlines(one);
  763. kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ);
  764. }
  765. }
  766. static void sc16is7xx_enable_ms(struct uart_port *port)
  767. {
  768. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  769. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  770. lockdep_assert_held_once(&port->lock);
  771. kthread_queue_delayed_work(&s->kworker, &one->ms_work, 0);
  772. }
  773. static void sc16is7xx_start_tx(struct uart_port *port)
  774. {
  775. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  776. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  777. kthread_queue_work(&s->kworker, &one->tx_work);
  778. }
  779. static void sc16is7xx_throttle(struct uart_port *port)
  780. {
  781. unsigned long flags;
  782. /*
  783. * Hardware flow control is enabled and thus the device ignores RTS
  784. * value set in MCR register. Stop reading data from RX FIFO so the
  785. * AutoRTS feature will de-activate RTS output.
  786. */
  787. uart_port_lock_irqsave(port, &flags);
  788. sc16is7xx_ier_clear(port, SC16IS7XX_IER_RDI_BIT);
  789. uart_port_unlock_irqrestore(port, flags);
  790. }
  791. static void sc16is7xx_unthrottle(struct uart_port *port)
  792. {
  793. unsigned long flags;
  794. uart_port_lock_irqsave(port, &flags);
  795. sc16is7xx_ier_set(port, SC16IS7XX_IER_RDI_BIT);
  796. uart_port_unlock_irqrestore(port, flags);
  797. }
  798. static unsigned int sc16is7xx_tx_empty(struct uart_port *port)
  799. {
  800. unsigned int lsr;
  801. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  802. return (lsr & SC16IS7XX_LSR_TEMT_BIT) ? TIOCSER_TEMT : 0;
  803. }
  804. static unsigned int sc16is7xx_get_mctrl(struct uart_port *port)
  805. {
  806. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  807. /* Called with port lock taken so we can only return cached value */
  808. return one->old_mctrl;
  809. }
  810. static void sc16is7xx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  811. {
  812. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  813. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  814. one->config.flags |= SC16IS7XX_RECONF_MD;
  815. kthread_queue_work(&s->kworker, &one->reg_work);
  816. }
  817. static void sc16is7xx_break_ctl(struct uart_port *port, int break_state)
  818. {
  819. sc16is7xx_port_update(port, SC16IS7XX_LCR_REG,
  820. SC16IS7XX_LCR_TXBREAK_BIT,
  821. break_state ? SC16IS7XX_LCR_TXBREAK_BIT : 0);
  822. }
  823. static void sc16is7xx_set_termios(struct uart_port *port,
  824. struct ktermios *termios,
  825. const struct ktermios *old)
  826. {
  827. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  828. unsigned int lcr, flow = 0;
  829. int baud;
  830. unsigned long flags;
  831. kthread_cancel_delayed_work_sync(&one->ms_work);
  832. /* Mask termios capabilities we don't support */
  833. termios->c_cflag &= ~CMSPAR;
  834. /* Word size */
  835. switch (termios->c_cflag & CSIZE) {
  836. case CS5:
  837. lcr = SC16IS7XX_LCR_WORD_LEN_5;
  838. break;
  839. case CS6:
  840. lcr = SC16IS7XX_LCR_WORD_LEN_6;
  841. break;
  842. case CS7:
  843. lcr = SC16IS7XX_LCR_WORD_LEN_7;
  844. break;
  845. case CS8:
  846. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  847. break;
  848. default:
  849. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  850. termios->c_cflag &= ~CSIZE;
  851. termios->c_cflag |= CS8;
  852. break;
  853. }
  854. /* Parity */
  855. if (termios->c_cflag & PARENB) {
  856. lcr |= SC16IS7XX_LCR_PARITY_BIT;
  857. if (!(termios->c_cflag & PARODD))
  858. lcr |= SC16IS7XX_LCR_EVENPARITY_BIT;
  859. }
  860. /* Stop bits */
  861. if (termios->c_cflag & CSTOPB)
  862. lcr |= SC16IS7XX_LCR_STOPLEN_BIT; /* 2 stops */
  863. /* Set read status mask */
  864. port->read_status_mask = SC16IS7XX_LSR_OE_BIT;
  865. if (termios->c_iflag & INPCK)
  866. port->read_status_mask |= SC16IS7XX_LSR_PE_BIT |
  867. SC16IS7XX_LSR_FE_BIT;
  868. if (termios->c_iflag & (BRKINT | PARMRK))
  869. port->read_status_mask |= SC16IS7XX_LSR_BI_BIT;
  870. /* Set status ignore mask */
  871. port->ignore_status_mask = 0;
  872. if (termios->c_iflag & IGNBRK)
  873. port->ignore_status_mask |= SC16IS7XX_LSR_BI_BIT;
  874. if (!(termios->c_cflag & CREAD))
  875. port->ignore_status_mask |= SC16IS7XX_LSR_BRK_ERROR_MASK;
  876. /* Configure flow control */
  877. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  878. if (termios->c_cflag & CRTSCTS) {
  879. flow |= SC16IS7XX_EFR_AUTOCTS_BIT |
  880. SC16IS7XX_EFR_AUTORTS_BIT;
  881. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  882. }
  883. if (termios->c_iflag & IXON)
  884. flow |= SC16IS7XX_EFR_SWFLOW3_BIT;
  885. if (termios->c_iflag & IXOFF)
  886. flow |= SC16IS7XX_EFR_SWFLOW1_BIT;
  887. /* Update LCR register */
  888. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  889. /* Update EFR registers */
  890. sc16is7xx_regs_lock(port, SC16IS7XX_LCR_REG_SET_ENHANCED);
  891. sc16is7xx_port_write(port, SC16IS7XX_XON1_REG, termios->c_cc[VSTART]);
  892. sc16is7xx_port_write(port, SC16IS7XX_XOFF1_REG, termios->c_cc[VSTOP]);
  893. sc16is7xx_port_update(port, SC16IS7XX_EFR_REG,
  894. SC16IS7XX_EFR_FLOWCTRL_BITS, flow);
  895. sc16is7xx_regs_unlock(port);
  896. /* Get baud rate generator configuration */
  897. baud = uart_get_baud_rate(port, termios, old,
  898. port->uartclk / 16 / 4 / 0xffff,
  899. port->uartclk / 16);
  900. /* Setup baudrate generator */
  901. baud = sc16is7xx_set_baud(port, baud);
  902. uart_port_lock_irqsave(port, &flags);
  903. /* Update timeout according to new baud rate */
  904. uart_update_timeout(port, termios->c_cflag, baud);
  905. if (UART_ENABLE_MS(port, termios->c_cflag))
  906. sc16is7xx_enable_ms(port);
  907. uart_port_unlock_irqrestore(port, flags);
  908. }
  909. static int sc16is7xx_config_rs485(struct uart_port *port, struct ktermios *termios,
  910. struct serial_rs485 *rs485)
  911. {
  912. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  913. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  914. if (rs485->flags & SER_RS485_ENABLED) {
  915. /*
  916. * RTS signal is handled by HW, it's timing can't be influenced.
  917. * However, it's sometimes useful to delay TX even without RTS
  918. * control therefore we try to handle .delay_rts_before_send.
  919. */
  920. if (rs485->delay_rts_after_send)
  921. return -EINVAL;
  922. }
  923. one->config.flags |= SC16IS7XX_RECONF_RS485;
  924. kthread_queue_work(&s->kworker, &one->reg_work);
  925. return 0;
  926. }
  927. static int sc16is7xx_startup(struct uart_port *port)
  928. {
  929. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  930. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  931. unsigned int val;
  932. unsigned long flags;
  933. sc16is7xx_power(port, 1);
  934. /* Reset FIFOs */
  935. val = SC16IS7XX_FCR_RXRESET_BIT | SC16IS7XX_FCR_TXRESET_BIT;
  936. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG, val);
  937. udelay(5);
  938. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG,
  939. SC16IS7XX_FCR_FIFO_BIT);
  940. /* Enable TCR/TLR */
  941. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  942. SC16IS7XX_MCR_TCRTLR_BIT,
  943. SC16IS7XX_MCR_TCRTLR_BIT);
  944. /* Configure flow control levels */
  945. /* Flow control halt level 48, resume level 24 */
  946. sc16is7xx_port_write(port, SC16IS7XX_TCR_REG,
  947. SC16IS7XX_TCR_RX_RESUME(24) |
  948. SC16IS7XX_TCR_RX_HALT(48));
  949. /* Disable TCR/TLR access */
  950. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG, SC16IS7XX_MCR_TCRTLR_BIT, 0);
  951. /* Now, initialize the UART */
  952. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, SC16IS7XX_LCR_WORD_LEN_8);
  953. /* Enable IrDA mode if requested in DT */
  954. /* This bit must be written with LCR[7] = 0 */
  955. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  956. SC16IS7XX_MCR_IRDA_BIT,
  957. one->irda_mode ? SC16IS7XX_MCR_IRDA_BIT : 0);
  958. /* Enable the Rx and Tx FIFO */
  959. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  960. SC16IS7XX_EFCR_RXDISABLE_BIT |
  961. SC16IS7XX_EFCR_TXDISABLE_BIT,
  962. 0);
  963. /* Enable RX, CTS change and modem lines interrupts */
  964. val = SC16IS7XX_IER_RDI_BIT | SC16IS7XX_IER_CTSI_BIT |
  965. SC16IS7XX_IER_MSI_BIT;
  966. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, val);
  967. /* Enable modem status polling */
  968. uart_port_lock_irqsave(port, &flags);
  969. sc16is7xx_enable_ms(port);
  970. uart_port_unlock_irqrestore(port, flags);
  971. if (s->polling)
  972. kthread_queue_delayed_work(&s->kworker, &s->poll_work,
  973. msecs_to_jiffies(SC16IS7XX_POLL_PERIOD_MS));
  974. return 0;
  975. }
  976. static void sc16is7xx_shutdown(struct uart_port *port)
  977. {
  978. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  979. struct sc16is7xx_one *one = to_sc16is7xx_one(port);
  980. kthread_cancel_delayed_work_sync(&one->ms_work);
  981. /* Disable all interrupts */
  982. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, 0);
  983. /* Disable TX/RX */
  984. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  985. SC16IS7XX_EFCR_RXDISABLE_BIT |
  986. SC16IS7XX_EFCR_TXDISABLE_BIT,
  987. SC16IS7XX_EFCR_RXDISABLE_BIT |
  988. SC16IS7XX_EFCR_TXDISABLE_BIT);
  989. sc16is7xx_power(port, 0);
  990. if (s->polling)
  991. kthread_cancel_delayed_work_sync(&s->poll_work);
  992. kthread_flush_worker(&s->kworker);
  993. }
  994. static const char *sc16is7xx_type(struct uart_port *port)
  995. {
  996. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  997. return (port->type == PORT_SC16IS7XX) ? s->devtype->name : NULL;
  998. }
  999. static int sc16is7xx_request_port(struct uart_port *port)
  1000. {
  1001. /* Do nothing */
  1002. return 0;
  1003. }
  1004. static void sc16is7xx_config_port(struct uart_port *port, int flags)
  1005. {
  1006. if (flags & UART_CONFIG_TYPE)
  1007. port->type = PORT_SC16IS7XX;
  1008. }
  1009. static int sc16is7xx_verify_port(struct uart_port *port,
  1010. struct serial_struct *s)
  1011. {
  1012. if ((s->type != PORT_UNKNOWN) && (s->type != PORT_SC16IS7XX))
  1013. return -EINVAL;
  1014. if (s->irq != port->irq)
  1015. return -EINVAL;
  1016. return 0;
  1017. }
  1018. static void sc16is7xx_pm(struct uart_port *port, unsigned int state,
  1019. unsigned int oldstate)
  1020. {
  1021. sc16is7xx_power(port, (state == UART_PM_STATE_ON) ? 1 : 0);
  1022. }
  1023. static void sc16is7xx_null_void(struct uart_port *port)
  1024. {
  1025. /* Do nothing */
  1026. }
  1027. static const struct uart_ops sc16is7xx_ops = {
  1028. .tx_empty = sc16is7xx_tx_empty,
  1029. .set_mctrl = sc16is7xx_set_mctrl,
  1030. .get_mctrl = sc16is7xx_get_mctrl,
  1031. .stop_tx = sc16is7xx_stop_tx,
  1032. .start_tx = sc16is7xx_start_tx,
  1033. .throttle = sc16is7xx_throttle,
  1034. .unthrottle = sc16is7xx_unthrottle,
  1035. .stop_rx = sc16is7xx_stop_rx,
  1036. .enable_ms = sc16is7xx_enable_ms,
  1037. .break_ctl = sc16is7xx_break_ctl,
  1038. .startup = sc16is7xx_startup,
  1039. .shutdown = sc16is7xx_shutdown,
  1040. .set_termios = sc16is7xx_set_termios,
  1041. .type = sc16is7xx_type,
  1042. .request_port = sc16is7xx_request_port,
  1043. .release_port = sc16is7xx_null_void,
  1044. .config_port = sc16is7xx_config_port,
  1045. .verify_port = sc16is7xx_verify_port,
  1046. .pm = sc16is7xx_pm,
  1047. };
  1048. #ifdef CONFIG_GPIOLIB
  1049. static int sc16is7xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  1050. {
  1051. unsigned int val;
  1052. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  1053. struct uart_port *port = &s->p[0].port;
  1054. val = sc16is7xx_port_read(port, SC16IS7XX_IOSTATE_REG);
  1055. return !!(val & BIT(offset));
  1056. }
  1057. static int sc16is7xx_gpio_set(struct gpio_chip *chip, unsigned int offset,
  1058. int val)
  1059. {
  1060. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  1061. struct uart_port *port = &s->p[0].port;
  1062. sc16is7xx_port_update(port, SC16IS7XX_IOSTATE_REG, BIT(offset),
  1063. val ? BIT(offset) : 0);
  1064. return 0;
  1065. }
  1066. static int sc16is7xx_gpio_direction_input(struct gpio_chip *chip,
  1067. unsigned offset)
  1068. {
  1069. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  1070. struct uart_port *port = &s->p[0].port;
  1071. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset), 0);
  1072. return 0;
  1073. }
  1074. static int sc16is7xx_gpio_direction_output(struct gpio_chip *chip,
  1075. unsigned offset, int val)
  1076. {
  1077. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  1078. struct uart_port *port = &s->p[0].port;
  1079. u8 state = sc16is7xx_port_read(port, SC16IS7XX_IOSTATE_REG);
  1080. if (val)
  1081. state |= BIT(offset);
  1082. else
  1083. state &= ~BIT(offset);
  1084. /*
  1085. * If we write IOSTATE first, and then IODIR, the output value is not
  1086. * transferred to the corresponding I/O pin.
  1087. * The datasheet states that each register bit will be transferred to
  1088. * the corresponding I/O pin programmed as output when writing to
  1089. * IOSTATE. Therefore, configure direction first with IODIR, and then
  1090. * set value after with IOSTATE.
  1091. */
  1092. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset),
  1093. BIT(offset));
  1094. sc16is7xx_port_write(port, SC16IS7XX_IOSTATE_REG, state);
  1095. return 0;
  1096. }
  1097. static int sc16is7xx_gpio_init_valid_mask(struct gpio_chip *chip,
  1098. unsigned long *valid_mask,
  1099. unsigned int ngpios)
  1100. {
  1101. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  1102. *valid_mask = s->gpio_valid_mask;
  1103. return 0;
  1104. }
  1105. static int sc16is7xx_setup_gpio_chip(struct sc16is7xx_port *s)
  1106. {
  1107. struct device *dev = s->p[0].port.dev;
  1108. if (!s->devtype->nr_gpio)
  1109. return 0;
  1110. switch (s->mctrl_mask) {
  1111. case 0:
  1112. s->gpio_valid_mask = GENMASK(7, 0);
  1113. break;
  1114. case SC16IS7XX_IOCONTROL_MODEM_A_BIT:
  1115. s->gpio_valid_mask = GENMASK(3, 0);
  1116. break;
  1117. case SC16IS7XX_IOCONTROL_MODEM_B_BIT:
  1118. s->gpio_valid_mask = GENMASK(7, 4);
  1119. break;
  1120. default:
  1121. break;
  1122. }
  1123. if (s->gpio_valid_mask == 0)
  1124. return 0;
  1125. s->gpio.owner = THIS_MODULE;
  1126. s->gpio.parent = dev;
  1127. s->gpio.label = dev_name(dev);
  1128. s->gpio.init_valid_mask = sc16is7xx_gpio_init_valid_mask;
  1129. s->gpio.direction_input = sc16is7xx_gpio_direction_input;
  1130. s->gpio.get = sc16is7xx_gpio_get;
  1131. s->gpio.direction_output = sc16is7xx_gpio_direction_output;
  1132. s->gpio.set = sc16is7xx_gpio_set;
  1133. s->gpio.base = -1;
  1134. s->gpio.ngpio = s->devtype->nr_gpio;
  1135. s->gpio.can_sleep = 1;
  1136. return gpiochip_add_data(&s->gpio, s);
  1137. }
  1138. #endif
  1139. static void sc16is7xx_setup_irda_ports(struct sc16is7xx_port *s)
  1140. {
  1141. int i;
  1142. int ret;
  1143. int count;
  1144. u32 irda_port[SC16IS7XX_MAX_PORTS];
  1145. struct device *dev = s->p[0].port.dev;
  1146. count = device_property_count_u32(dev, "irda-mode-ports");
  1147. if (count < 0 || count > ARRAY_SIZE(irda_port))
  1148. return;
  1149. ret = device_property_read_u32_array(dev, "irda-mode-ports",
  1150. irda_port, count);
  1151. if (ret)
  1152. return;
  1153. for (i = 0; i < count; i++) {
  1154. if (irda_port[i] < s->devtype->nr_uart)
  1155. s->p[irda_port[i]].irda_mode = true;
  1156. }
  1157. }
  1158. /*
  1159. * Configure ports designated to operate as modem control lines.
  1160. */
  1161. static int sc16is7xx_setup_mctrl_ports(struct sc16is7xx_port *s,
  1162. struct regmap *regmap)
  1163. {
  1164. int i;
  1165. int ret;
  1166. int count;
  1167. u32 mctrl_port[SC16IS7XX_MAX_PORTS];
  1168. struct device *dev = s->p[0].port.dev;
  1169. count = device_property_count_u32(dev, "nxp,modem-control-line-ports");
  1170. if (count < 0 || count > ARRAY_SIZE(mctrl_port))
  1171. return 0;
  1172. ret = device_property_read_u32_array(dev, "nxp,modem-control-line-ports",
  1173. mctrl_port, count);
  1174. if (ret)
  1175. return ret;
  1176. s->mctrl_mask = 0;
  1177. for (i = 0; i < count; i++) {
  1178. /* Use GPIO lines as modem control lines */
  1179. if (mctrl_port[i] == 0)
  1180. s->mctrl_mask |= SC16IS7XX_IOCONTROL_MODEM_A_BIT;
  1181. else if (mctrl_port[i] == 1)
  1182. s->mctrl_mask |= SC16IS7XX_IOCONTROL_MODEM_B_BIT;
  1183. }
  1184. if (s->mctrl_mask)
  1185. regmap_update_bits(
  1186. regmap,
  1187. SC16IS7XX_IOCONTROL_REG,
  1188. SC16IS7XX_IOCONTROL_MODEM_A_BIT |
  1189. SC16IS7XX_IOCONTROL_MODEM_B_BIT, s->mctrl_mask);
  1190. return 0;
  1191. }
  1192. static const struct serial_rs485 sc16is7xx_rs485_supported = {
  1193. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
  1194. .delay_rts_before_send = 1,
  1195. .delay_rts_after_send = 1, /* Not supported but keep returning -EINVAL */
  1196. };
  1197. /* Reset device, purging any pending irq / data */
  1198. static int sc16is7xx_reset(struct device *dev, struct regmap *regmap)
  1199. {
  1200. struct gpio_desc *reset_gpio;
  1201. /* Assert reset GPIO if defined and valid. */
  1202. reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  1203. if (IS_ERR(reset_gpio))
  1204. return dev_err_probe(dev, PTR_ERR(reset_gpio), "Failed to get reset GPIO\n");
  1205. if (reset_gpio) {
  1206. /* The minimum reset pulse width is 3 us. */
  1207. fsleep(5);
  1208. gpiod_set_value_cansleep(reset_gpio, 0); /* Deassert GPIO */
  1209. } else {
  1210. /* Software reset */
  1211. regmap_write(regmap, SC16IS7XX_IOCONTROL_REG,
  1212. SC16IS7XX_IOCONTROL_SRESET_BIT);
  1213. }
  1214. return 0;
  1215. }
  1216. static int sc16is7xx_setup_channel(struct sc16is7xx_one *one, int i,
  1217. bool *port_registered)
  1218. {
  1219. struct uart_port *port = &one->port;
  1220. int ret;
  1221. ret = ida_alloc_max(&sc16is7xx_lines, SC16IS7XX_MAX_DEVS - 1, GFP_KERNEL);
  1222. if (ret < 0)
  1223. return ret;
  1224. port->line = ret;
  1225. /* Initialize port data */
  1226. port->type = PORT_SC16IS7XX;
  1227. port->fifosize = SC16IS7XX_FIFO_SIZE;
  1228. port->flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
  1229. port->iobase = i;
  1230. /*
  1231. * Use all ones as membase to make sure uart_configure_port() in
  1232. * serial_core.c does not abort for SPI/I2C devices where the
  1233. * membase address is not applicable.
  1234. */
  1235. port->membase = (void __iomem *)~0;
  1236. port->iotype = UPIO_PORT;
  1237. port->rs485_config = sc16is7xx_config_rs485;
  1238. port->rs485_supported = sc16is7xx_rs485_supported;
  1239. port->ops = &sc16is7xx_ops;
  1240. one->old_mctrl = 0;
  1241. mutex_init(&one->lock);
  1242. ret = uart_get_rs485_mode(port);
  1243. if (ret)
  1244. return ret;
  1245. /* Enable access to general register set */
  1246. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, 0x00);
  1247. /* Disable all interrupts */
  1248. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, 0);
  1249. /* Disable TX/RX */
  1250. sc16is7xx_port_write(port, SC16IS7XX_EFCR_REG,
  1251. SC16IS7XX_EFCR_RXDISABLE_BIT |
  1252. SC16IS7XX_EFCR_TXDISABLE_BIT);
  1253. /* Initialize kthread work structs */
  1254. kthread_init_work(&one->tx_work, sc16is7xx_tx_proc);
  1255. kthread_init_work(&one->reg_work, sc16is7xx_reg_proc);
  1256. kthread_init_delayed_work(&one->ms_work, sc16is7xx_ms_proc);
  1257. /* Register port */
  1258. ret = uart_add_one_port(&sc16is7xx_uart, port);
  1259. if (ret)
  1260. return ret;
  1261. *port_registered = true;
  1262. sc16is7xx_regs_lock(port, SC16IS7XX_LCR_REG_SET_ENHANCED);
  1263. /* Enable write access to enhanced features */
  1264. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  1265. SC16IS7XX_EFR_ENABLE_BIT);
  1266. sc16is7xx_regs_unlock(port);
  1267. /* Go to suspend mode */
  1268. sc16is7xx_power(port, 0);
  1269. return 0;
  1270. }
  1271. int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
  1272. struct regmap *regmaps[], int irq)
  1273. {
  1274. unsigned long freq = 0, *pfreq = dev_get_platdata(dev);
  1275. unsigned int val;
  1276. u32 uartclk = 0;
  1277. int i, ret;
  1278. struct sc16is7xx_port *s;
  1279. bool port_registered[SC16IS7XX_MAX_PORTS];
  1280. for (i = 0; i < devtype->nr_uart; i++)
  1281. if (IS_ERR(regmaps[i]))
  1282. return PTR_ERR(regmaps[i]);
  1283. /*
  1284. * This device does not have an identification register that would
  1285. * tell us if we are really connected to the correct device.
  1286. * The best we can do is to check if communication is at all possible.
  1287. *
  1288. * Note: regmap[0] is used in the probe function to access registers
  1289. * common to all channels/ports, as it is guaranteed to be present on
  1290. * all variants.
  1291. */
  1292. ret = regmap_read(regmaps[0], SC16IS7XX_LSR_REG, &val);
  1293. if (ret < 0)
  1294. return -EPROBE_DEFER;
  1295. /* Alloc port structure */
  1296. s = devm_kzalloc(dev, struct_size(s, p, devtype->nr_uart), GFP_KERNEL);
  1297. if (!s)
  1298. return -ENOMEM;
  1299. /* Always ask for fixed clock rate from a property. */
  1300. device_property_read_u32(dev, "clock-frequency", &uartclk);
  1301. s->polling = (irq <= 0);
  1302. if (s->polling)
  1303. dev_dbg(dev,
  1304. "No interrupt pin definition, falling back to polling mode\n");
  1305. s->clk = devm_clk_get_optional(dev, NULL);
  1306. if (IS_ERR(s->clk))
  1307. return PTR_ERR(s->clk);
  1308. ret = clk_prepare_enable(s->clk);
  1309. if (ret)
  1310. return ret;
  1311. freq = clk_get_rate(s->clk);
  1312. if (freq == 0) {
  1313. if (uartclk)
  1314. freq = uartclk;
  1315. if (pfreq)
  1316. freq = *pfreq;
  1317. if (freq)
  1318. dev_dbg(dev, "Clock frequency: %luHz\n", freq);
  1319. else
  1320. return -EINVAL;
  1321. }
  1322. s->devtype = devtype;
  1323. dev_set_drvdata(dev, s);
  1324. kthread_init_worker(&s->kworker);
  1325. s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
  1326. "sc16is7xx");
  1327. if (IS_ERR(s->kworker_task)) {
  1328. ret = PTR_ERR(s->kworker_task);
  1329. goto out_clk;
  1330. }
  1331. sched_set_fifo(s->kworker_task);
  1332. ret = sc16is7xx_reset(dev, regmaps[0]);
  1333. if (ret)
  1334. goto out_kthread;
  1335. /* Mark each port line and status as uninitialised. */
  1336. for (i = 0; i < devtype->nr_uart; ++i) {
  1337. s->p[i].port.line = SC16IS7XX_MAX_DEVS;
  1338. port_registered[i] = false;
  1339. }
  1340. for (i = 0; i < devtype->nr_uart; ++i) {
  1341. s->p[i].port.dev = dev;
  1342. s->p[i].port.irq = irq;
  1343. s->p[i].port.uartclk = freq;
  1344. s->p[i].regmap = regmaps[i];
  1345. ret = sc16is7xx_setup_channel(&s->p[i], i, &port_registered[i]);
  1346. if (ret)
  1347. goto out_ports;
  1348. }
  1349. sc16is7xx_setup_irda_ports(s);
  1350. ret = sc16is7xx_setup_mctrl_ports(s, regmaps[0]);
  1351. if (ret)
  1352. goto out_ports;
  1353. #ifdef CONFIG_GPIOLIB
  1354. ret = sc16is7xx_setup_gpio_chip(s);
  1355. if (ret)
  1356. goto out_ports;
  1357. #endif
  1358. if (s->polling) {
  1359. /* Initialize kernel thread for polling */
  1360. kthread_init_delayed_work(&s->poll_work, sc16is7xx_poll_proc);
  1361. return 0;
  1362. }
  1363. /*
  1364. * Setup interrupt. We first try to acquire the IRQ line as level IRQ.
  1365. * If that succeeds, we can allow sharing the interrupt as well.
  1366. * In case the interrupt controller doesn't support that, we fall
  1367. * back to a non-shared falling-edge trigger.
  1368. */
  1369. ret = devm_request_threaded_irq(dev, irq, NULL, sc16is7xx_irq,
  1370. IRQF_TRIGGER_LOW | IRQF_SHARED |
  1371. IRQF_ONESHOT,
  1372. dev_name(dev), s);
  1373. if (!ret)
  1374. return 0;
  1375. ret = devm_request_threaded_irq(dev, irq, NULL, sc16is7xx_irq,
  1376. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1377. dev_name(dev), s);
  1378. if (!ret)
  1379. return 0;
  1380. #ifdef CONFIG_GPIOLIB
  1381. if (s->gpio_valid_mask)
  1382. gpiochip_remove(&s->gpio);
  1383. #endif
  1384. out_ports:
  1385. for (i = 0; i < devtype->nr_uart; i++) {
  1386. if (s->p[i].port.line < SC16IS7XX_MAX_DEVS)
  1387. ida_free(&sc16is7xx_lines, s->p[i].port.line);
  1388. if (port_registered[i])
  1389. uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
  1390. }
  1391. out_kthread:
  1392. kthread_stop(s->kworker_task);
  1393. out_clk:
  1394. clk_disable_unprepare(s->clk);
  1395. return ret;
  1396. }
  1397. EXPORT_SYMBOL_GPL(sc16is7xx_probe);
  1398. void sc16is7xx_remove(struct device *dev)
  1399. {
  1400. struct sc16is7xx_port *s = dev_get_drvdata(dev);
  1401. int i;
  1402. #ifdef CONFIG_GPIOLIB
  1403. if (s->gpio_valid_mask)
  1404. gpiochip_remove(&s->gpio);
  1405. #endif
  1406. for (i = 0; i < s->devtype->nr_uart; i++) {
  1407. kthread_cancel_delayed_work_sync(&s->p[i].ms_work);
  1408. ida_free(&sc16is7xx_lines, s->p[i].port.line);
  1409. uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
  1410. sc16is7xx_power(&s->p[i].port, 0);
  1411. }
  1412. if (s->polling)
  1413. kthread_cancel_delayed_work_sync(&s->poll_work);
  1414. kthread_flush_worker(&s->kworker);
  1415. kthread_stop(s->kworker_task);
  1416. clk_disable_unprepare(s->clk);
  1417. }
  1418. EXPORT_SYMBOL_GPL(sc16is7xx_remove);
  1419. const struct of_device_id __maybe_unused sc16is7xx_dt_ids[] = {
  1420. { .compatible = "nxp,sc16is740", .data = &sc16is74x_devtype, },
  1421. { .compatible = "nxp,sc16is741", .data = &sc16is74x_devtype, },
  1422. { .compatible = "nxp,sc16is750", .data = &sc16is750_devtype, },
  1423. { .compatible = "nxp,sc16is752", .data = &sc16is752_devtype, },
  1424. { .compatible = "nxp,sc16is760", .data = &sc16is760_devtype, },
  1425. { .compatible = "nxp,sc16is762", .data = &sc16is762_devtype, },
  1426. { }
  1427. };
  1428. EXPORT_SYMBOL_GPL(sc16is7xx_dt_ids);
  1429. MODULE_DEVICE_TABLE(of, sc16is7xx_dt_ids);
  1430. const struct regmap_config sc16is7xx_regcfg = {
  1431. .reg_bits = 5,
  1432. .pad_bits = 3,
  1433. .val_bits = 8,
  1434. .cache_type = REGCACHE_MAPLE,
  1435. .volatile_reg = sc16is7xx_regmap_volatile,
  1436. .precious_reg = sc16is7xx_regmap_precious,
  1437. .writeable_noinc_reg = sc16is7xx_regmap_noinc,
  1438. .readable_noinc_reg = sc16is7xx_regmap_noinc,
  1439. .max_raw_read = SC16IS7XX_FIFO_SIZE,
  1440. .max_raw_write = SC16IS7XX_FIFO_SIZE,
  1441. .max_register = SC16IS7XX_EFCR_REG,
  1442. };
  1443. EXPORT_SYMBOL_GPL(sc16is7xx_regcfg);
  1444. const char *sc16is7xx_regmap_name(u8 port_id)
  1445. {
  1446. switch (port_id) {
  1447. case 0: return "port0";
  1448. case 1: return "port1";
  1449. default:
  1450. WARN_ON(true);
  1451. return NULL;
  1452. }
  1453. }
  1454. EXPORT_SYMBOL_GPL(sc16is7xx_regmap_name);
  1455. unsigned int sc16is7xx_regmap_port_mask(unsigned int port_id)
  1456. {
  1457. /* CH1,CH0 are at bits 2:1. */
  1458. return port_id << 1;
  1459. }
  1460. EXPORT_SYMBOL_GPL(sc16is7xx_regmap_port_mask);
  1461. static int __init sc16is7xx_init(void)
  1462. {
  1463. return uart_register_driver(&sc16is7xx_uart);
  1464. }
  1465. module_init(sc16is7xx_init);
  1466. static void __exit sc16is7xx_exit(void)
  1467. {
  1468. uart_unregister_driver(&sc16is7xx_uart);
  1469. }
  1470. module_exit(sc16is7xx_exit);
  1471. MODULE_LICENSE("GPL");
  1472. MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>");
  1473. MODULE_DESCRIPTION(KBUILD_MODNAME " tty serial core driver");