max310x.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver
  4. *
  5. * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
  6. *
  7. * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org>
  8. * Based on max3110.c, by Feng Tang <feng.tang@intel.com>
  9. * Based on max3107.c, by Aavamobile
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/serial.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/uaccess.h>
  26. #define MAX310X_NAME "max310x"
  27. #define MAX310X_MAJOR 204
  28. #define MAX310X_MINOR 209
  29. #define MAX310X_UART_NRMAX 16
  30. /* MAX310X register definitions */
  31. #define MAX310X_RHR_REG (0x00) /* RX FIFO */
  32. #define MAX310X_THR_REG (0x00) /* TX FIFO */
  33. #define MAX310X_IRQEN_REG (0x01) /* IRQ enable */
  34. #define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
  35. #define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
  36. #define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
  37. #define MAX310X_REG_05 (0x05)
  38. #define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
  39. #define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
  40. #define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
  41. #define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
  42. #define MAX310X_MODE1_REG (0x09) /* MODE1 */
  43. #define MAX310X_MODE2_REG (0x0a) /* MODE2 */
  44. #define MAX310X_LCR_REG (0x0b) /* LCR */
  45. #define MAX310X_RXTO_REG (0x0c) /* RX timeout */
  46. #define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */
  47. #define MAX310X_IRDA_REG (0x0e) /* IRDA settings */
  48. #define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */
  49. #define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */
  50. #define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */
  51. #define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */
  52. #define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */
  53. #define MAX310X_XON1_REG (0x14) /* XON1 character */
  54. #define MAX310X_XON2_REG (0x15) /* XON2 character */
  55. #define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */
  56. #define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */
  57. #define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */
  58. #define MAX310X_GPIODATA_REG (0x19) /* GPIO data */
  59. #define MAX310X_PLLCFG_REG (0x1a) /* PLL config */
  60. #define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */
  61. #define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
  62. #define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
  63. #define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
  64. #define MAX310X_REG_1F (0x1f)
  65. #define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
  66. #define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
  67. #define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
  68. /* Extended registers */
  69. #define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
  70. /* IRQ register bits */
  71. #define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
  72. #define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
  73. #define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */
  74. #define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
  75. #define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
  76. #define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
  77. #define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
  78. #define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
  79. /* LSR register bits */
  80. #define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */
  81. #define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
  82. #define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
  83. #define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */
  84. #define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */
  85. #define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
  86. #define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */
  87. /* Special character register bits */
  88. #define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
  89. #define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
  90. #define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
  91. #define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
  92. #define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */
  93. #define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
  94. /* Status register bits */
  95. #define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
  96. #define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
  97. #define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
  98. #define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
  99. #define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
  100. #define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
  101. /* MODE1 register bits */
  102. #define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
  103. #define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
  104. #define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
  105. #define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
  106. #define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
  107. #define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
  108. #define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
  109. #define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
  110. /* MODE2 register bits */
  111. #define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */
  112. #define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
  113. #define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
  114. #define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
  115. #define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
  116. #define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
  117. #define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
  118. #define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
  119. /* LCR register bits */
  120. #define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
  121. #define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
  122. *
  123. * Word length bits table:
  124. * 00 -> 5 bit words
  125. * 01 -> 6 bit words
  126. * 10 -> 7 bit words
  127. * 11 -> 8 bit words
  128. */
  129. #define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
  130. *
  131. * STOP length bit table:
  132. * 0 -> 1 stop bit
  133. * 1 -> 1-1.5 stop bits if
  134. * word length is 5,
  135. * 2 stop bits otherwise
  136. */
  137. #define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
  138. #define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
  139. #define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
  140. #define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
  141. #define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */
  142. /* IRDA register bits */
  143. #define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
  144. #define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
  145. /* Flow control trigger level register masks */
  146. #define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
  147. #define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
  148. #define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f)
  149. #define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4)
  150. /* FIFO interrupt trigger level register masks */
  151. #define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f) /* TX FIFO trigger level */
  152. #define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0) /* RX FIFO trigger level */
  153. #define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f)
  154. #define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4)
  155. /* Flow control register bits */
  156. #define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
  157. #define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
  158. #define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
  159. * are used in conjunction with
  160. * XOFF2 for definition of
  161. * special character */
  162. #define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
  163. #define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
  164. #define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
  165. *
  166. * SWFLOW bits 1 & 0 table:
  167. * 00 -> no transmitter flow
  168. * control
  169. * 01 -> receiver compares
  170. * XON2 and XOFF2
  171. * and controls
  172. * transmitter
  173. * 10 -> receiver compares
  174. * XON1 and XOFF1
  175. * and controls
  176. * transmitter
  177. * 11 -> receiver compares
  178. * XON1, XON2, XOFF1 and
  179. * XOFF2 and controls
  180. * transmitter
  181. */
  182. #define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
  183. #define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
  184. *
  185. * SWFLOW bits 3 & 2 table:
  186. * 00 -> no received flow
  187. * control
  188. * 01 -> transmitter generates
  189. * XON2 and XOFF2
  190. * 10 -> transmitter generates
  191. * XON1 and XOFF1
  192. * 11 -> transmitter generates
  193. * XON1, XON2, XOFF1 and
  194. * XOFF2
  195. */
  196. /* PLL configuration register masks */
  197. #define MAX310X_PLLCFG_PREDIV_MASK (0x3f) /* PLL predivision value */
  198. #define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0) /* PLL multiplication factor */
  199. /* Baud rate generator configuration register bits */
  200. #define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
  201. #define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
  202. /* Clock source register bits */
  203. #define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
  204. #define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
  205. #define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
  206. #define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
  207. #define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
  208. /* Global commands */
  209. #define MAX310X_EXTREG_ENBL (0xce)
  210. #define MAX310X_EXTREG_DSBL (0xcd)
  211. /* Misc definitions */
  212. #define MAX310X_FIFO_SIZE (128)
  213. #define MAX310x_REV_MASK (0xf8)
  214. #define MAX310X_WRITE_BIT 0x80
  215. /* MAX3107 specific */
  216. #define MAX3107_REV_ID (0xa0)
  217. /* MAX3109 specific */
  218. #define MAX3109_REV_ID (0xc0)
  219. /* MAX14830 specific */
  220. #define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
  221. #define MAX14830_REV_ID (0xb0)
  222. struct max310x_devtype {
  223. char name[9];
  224. int nr;
  225. u8 mode1;
  226. int (*detect)(struct device *);
  227. void (*power)(struct uart_port *, int);
  228. };
  229. struct max310x_one {
  230. struct uart_port port;
  231. struct work_struct tx_work;
  232. struct work_struct md_work;
  233. struct work_struct rs_work;
  234. u8 wr_header;
  235. u8 rd_header;
  236. u8 rx_buf[MAX310X_FIFO_SIZE];
  237. };
  238. #define to_max310x_port(_port) \
  239. container_of(_port, struct max310x_one, port)
  240. struct max310x_port {
  241. struct max310x_devtype *devtype;
  242. struct regmap *regmap;
  243. struct clk *clk;
  244. #ifdef CONFIG_GPIOLIB
  245. struct gpio_chip gpio;
  246. #endif
  247. struct max310x_one p[0];
  248. };
  249. static struct uart_driver max310x_uart = {
  250. .owner = THIS_MODULE,
  251. .driver_name = MAX310X_NAME,
  252. .dev_name = "ttyMAX",
  253. .major = MAX310X_MAJOR,
  254. .minor = MAX310X_MINOR,
  255. .nr = MAX310X_UART_NRMAX,
  256. };
  257. static DECLARE_BITMAP(max310x_lines, MAX310X_UART_NRMAX);
  258. static u8 max310x_port_read(struct uart_port *port, u8 reg)
  259. {
  260. struct max310x_port *s = dev_get_drvdata(port->dev);
  261. unsigned int val = 0;
  262. regmap_read(s->regmap, port->iobase + reg, &val);
  263. return val;
  264. }
  265. static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
  266. {
  267. struct max310x_port *s = dev_get_drvdata(port->dev);
  268. regmap_write(s->regmap, port->iobase + reg, val);
  269. }
  270. static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
  271. {
  272. struct max310x_port *s = dev_get_drvdata(port->dev);
  273. regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
  274. }
  275. static int max3107_detect(struct device *dev)
  276. {
  277. struct max310x_port *s = dev_get_drvdata(dev);
  278. unsigned int val = 0;
  279. int ret;
  280. ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
  281. if (ret)
  282. return ret;
  283. if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
  284. dev_err(dev,
  285. "%s ID 0x%02x does not match\n", s->devtype->name, val);
  286. return -ENODEV;
  287. }
  288. return 0;
  289. }
  290. static int max3108_detect(struct device *dev)
  291. {
  292. struct max310x_port *s = dev_get_drvdata(dev);
  293. unsigned int val = 0;
  294. int ret;
  295. /* MAX3108 have not REV ID register, we just check default value
  296. * from clocksource register to make sure everything works.
  297. */
  298. ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
  299. if (ret)
  300. return ret;
  301. if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
  302. dev_err(dev, "%s not present\n", s->devtype->name);
  303. return -ENODEV;
  304. }
  305. return 0;
  306. }
  307. static int max3109_detect(struct device *dev)
  308. {
  309. struct max310x_port *s = dev_get_drvdata(dev);
  310. unsigned int val = 0;
  311. int ret;
  312. ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
  313. MAX310X_EXTREG_ENBL);
  314. if (ret)
  315. return ret;
  316. regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
  317. regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
  318. if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
  319. dev_err(dev,
  320. "%s ID 0x%02x does not match\n", s->devtype->name, val);
  321. return -ENODEV;
  322. }
  323. return 0;
  324. }
  325. static void max310x_power(struct uart_port *port, int on)
  326. {
  327. max310x_port_update(port, MAX310X_MODE1_REG,
  328. MAX310X_MODE1_FORCESLEEP_BIT,
  329. on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
  330. if (on)
  331. msleep(50);
  332. }
  333. static int max14830_detect(struct device *dev)
  334. {
  335. struct max310x_port *s = dev_get_drvdata(dev);
  336. unsigned int val = 0;
  337. int ret;
  338. ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
  339. MAX310X_EXTREG_ENBL);
  340. if (ret)
  341. return ret;
  342. regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
  343. regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
  344. if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
  345. dev_err(dev,
  346. "%s ID 0x%02x does not match\n", s->devtype->name, val);
  347. return -ENODEV;
  348. }
  349. return 0;
  350. }
  351. static void max14830_power(struct uart_port *port, int on)
  352. {
  353. max310x_port_update(port, MAX310X_BRGCFG_REG,
  354. MAX14830_BRGCFG_CLKDIS_BIT,
  355. on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
  356. if (on)
  357. msleep(50);
  358. }
  359. static const struct max310x_devtype max3107_devtype = {
  360. .name = "MAX3107",
  361. .nr = 1,
  362. .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT | MAX310X_MODE1_IRQSEL_BIT,
  363. .detect = max3107_detect,
  364. .power = max310x_power,
  365. };
  366. static const struct max310x_devtype max3108_devtype = {
  367. .name = "MAX3108",
  368. .nr = 1,
  369. .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT,
  370. .detect = max3108_detect,
  371. .power = max310x_power,
  372. };
  373. static const struct max310x_devtype max3109_devtype = {
  374. .name = "MAX3109",
  375. .nr = 2,
  376. .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT,
  377. .detect = max3109_detect,
  378. .power = max310x_power,
  379. };
  380. static const struct max310x_devtype max14830_devtype = {
  381. .name = "MAX14830",
  382. .nr = 4,
  383. .mode1 = MAX310X_MODE1_IRQSEL_BIT,
  384. .detect = max14830_detect,
  385. .power = max14830_power,
  386. };
  387. static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
  388. {
  389. switch (reg & 0x1f) {
  390. case MAX310X_IRQSTS_REG:
  391. case MAX310X_LSR_IRQSTS_REG:
  392. case MAX310X_SPCHR_IRQSTS_REG:
  393. case MAX310X_STS_IRQSTS_REG:
  394. case MAX310X_TXFIFOLVL_REG:
  395. case MAX310X_RXFIFOLVL_REG:
  396. return false;
  397. default:
  398. break;
  399. }
  400. return true;
  401. }
  402. static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
  403. {
  404. switch (reg & 0x1f) {
  405. case MAX310X_RHR_REG:
  406. case MAX310X_IRQSTS_REG:
  407. case MAX310X_LSR_IRQSTS_REG:
  408. case MAX310X_SPCHR_IRQSTS_REG:
  409. case MAX310X_STS_IRQSTS_REG:
  410. case MAX310X_TXFIFOLVL_REG:
  411. case MAX310X_RXFIFOLVL_REG:
  412. case MAX310X_GPIODATA_REG:
  413. case MAX310X_BRGDIVLSB_REG:
  414. case MAX310X_REG_05:
  415. case MAX310X_REG_1F:
  416. return true;
  417. default:
  418. break;
  419. }
  420. return false;
  421. }
  422. static bool max310x_reg_precious(struct device *dev, unsigned int reg)
  423. {
  424. switch (reg & 0x1f) {
  425. case MAX310X_RHR_REG:
  426. case MAX310X_IRQSTS_REG:
  427. case MAX310X_SPCHR_IRQSTS_REG:
  428. case MAX310X_STS_IRQSTS_REG:
  429. return true;
  430. default:
  431. break;
  432. }
  433. return false;
  434. }
  435. static int max310x_set_baud(struct uart_port *port, int baud)
  436. {
  437. unsigned int mode = 0, div = 0, frac = 0, c = 0, F = 0;
  438. /*
  439. * Calculate the integer divisor first. Select a proper mode
  440. * in case if the requested baud is too high for the pre-defined
  441. * clocks frequency.
  442. */
  443. div = port->uartclk / baud;
  444. if (div < 8) {
  445. /* Mode x4 */
  446. c = 4;
  447. mode = MAX310X_BRGCFG_4XMODE_BIT;
  448. } else if (div < 16) {
  449. /* Mode x2 */
  450. c = 8;
  451. mode = MAX310X_BRGCFG_2XMODE_BIT;
  452. } else {
  453. c = 16;
  454. }
  455. /* Calculate the divisor in accordance with the fraction coefficient */
  456. div /= c;
  457. F = c*baud;
  458. /* Calculate the baud rate fraction */
  459. if (div > 0)
  460. frac = (16*(port->uartclk % F)) / F;
  461. else
  462. div = 1;
  463. max310x_port_write(port, MAX310X_BRGDIVMSB_REG, div >> 8);
  464. max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div);
  465. max310x_port_write(port, MAX310X_BRGCFG_REG, frac | mode);
  466. /* Return the actual baud rate we just programmed */
  467. return (16*port->uartclk) / (c*(16*div + frac));
  468. }
  469. static int max310x_update_best_err(unsigned long f, long *besterr)
  470. {
  471. /* Use baudrate 115200 for calculate error */
  472. long err = f % (460800 * 16);
  473. if ((*besterr < 0) || (*besterr > err)) {
  474. *besterr = err;
  475. return 0;
  476. }
  477. return 1;
  478. }
  479. static int max310x_set_ref_clk(struct device *dev, struct max310x_port *s,
  480. unsigned long freq, bool xtal)
  481. {
  482. unsigned int div, clksrc, pllcfg = 0;
  483. long besterr = -1;
  484. unsigned long fdiv, fmul, bestfreq = freq;
  485. /* First, update error without PLL */
  486. max310x_update_best_err(freq, &besterr);
  487. /* Try all possible PLL dividers */
  488. for (div = 1; (div <= 63) && besterr; div++) {
  489. fdiv = DIV_ROUND_CLOSEST(freq, div);
  490. /* Try multiplier 6 */
  491. fmul = fdiv * 6;
  492. if ((fdiv >= 500000) && (fdiv <= 800000))
  493. if (!max310x_update_best_err(fmul, &besterr)) {
  494. pllcfg = (0 << 6) | div;
  495. bestfreq = fmul;
  496. }
  497. /* Try multiplier 48 */
  498. fmul = fdiv * 48;
  499. if ((fdiv >= 850000) && (fdiv <= 1200000))
  500. if (!max310x_update_best_err(fmul, &besterr)) {
  501. pllcfg = (1 << 6) | div;
  502. bestfreq = fmul;
  503. }
  504. /* Try multiplier 96 */
  505. fmul = fdiv * 96;
  506. if ((fdiv >= 425000) && (fdiv <= 1000000))
  507. if (!max310x_update_best_err(fmul, &besterr)) {
  508. pllcfg = (2 << 6) | div;
  509. bestfreq = fmul;
  510. }
  511. /* Try multiplier 144 */
  512. fmul = fdiv * 144;
  513. if ((fdiv >= 390000) && (fdiv <= 667000))
  514. if (!max310x_update_best_err(fmul, &besterr)) {
  515. pllcfg = (3 << 6) | div;
  516. bestfreq = fmul;
  517. }
  518. }
  519. /* Configure clock source */
  520. clksrc = MAX310X_CLKSRC_EXTCLK_BIT | (xtal ? MAX310X_CLKSRC_CRYST_BIT : 0);
  521. /* Configure PLL */
  522. if (pllcfg) {
  523. clksrc |= MAX310X_CLKSRC_PLL_BIT;
  524. regmap_write(s->regmap, MAX310X_PLLCFG_REG, pllcfg);
  525. } else
  526. clksrc |= MAX310X_CLKSRC_PLLBYP_BIT;
  527. regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
  528. /* Wait for crystal */
  529. if (xtal) {
  530. unsigned int val;
  531. msleep(10);
  532. regmap_read(s->regmap, MAX310X_STS_IRQSTS_REG, &val);
  533. if (!(val & MAX310X_STS_CLKREADY_BIT)) {
  534. dev_warn(dev, "clock is not stable yet\n");
  535. }
  536. }
  537. return (int)bestfreq;
  538. }
  539. static void max310x_batch_write(struct uart_port *port, u8 *txbuf, unsigned int len)
  540. {
  541. struct max310x_one *one = to_max310x_port(port);
  542. struct spi_transfer xfer[] = {
  543. {
  544. .tx_buf = &one->wr_header,
  545. .len = sizeof(one->wr_header),
  546. }, {
  547. .tx_buf = txbuf,
  548. .len = len,
  549. }
  550. };
  551. spi_sync_transfer(to_spi_device(port->dev), xfer, ARRAY_SIZE(xfer));
  552. }
  553. static void max310x_batch_read(struct uart_port *port, u8 *rxbuf, unsigned int len)
  554. {
  555. struct max310x_one *one = to_max310x_port(port);
  556. struct spi_transfer xfer[] = {
  557. {
  558. .tx_buf = &one->rd_header,
  559. .len = sizeof(one->rd_header),
  560. }, {
  561. .rx_buf = rxbuf,
  562. .len = len,
  563. }
  564. };
  565. spi_sync_transfer(to_spi_device(port->dev), xfer, ARRAY_SIZE(xfer));
  566. }
  567. static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
  568. {
  569. struct max310x_one *one = to_max310x_port(port);
  570. unsigned int sts, ch, flag, i;
  571. if (port->read_status_mask == MAX310X_LSR_RXOVR_BIT) {
  572. /* We are just reading, happily ignoring any error conditions.
  573. * Break condition, parity checking, framing errors -- they
  574. * are all ignored. That means that we can do a batch-read.
  575. *
  576. * There is a small opportunity for race if the RX FIFO
  577. * overruns while we're reading the buffer; the datasheets says
  578. * that the LSR register applies to the "current" character.
  579. * That's also the reason why we cannot do batched reads when
  580. * asked to check the individual statuses.
  581. * */
  582. sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
  583. max310x_batch_read(port, one->rx_buf, rxlen);
  584. port->icount.rx += rxlen;
  585. flag = TTY_NORMAL;
  586. sts &= port->read_status_mask;
  587. if (sts & MAX310X_LSR_RXOVR_BIT) {
  588. dev_warn_ratelimited(port->dev, "Hardware RX FIFO overrun\n");
  589. port->icount.overrun++;
  590. }
  591. for (i = 0; i < (rxlen - 1); ++i)
  592. uart_insert_char(port, sts, 0, one->rx_buf[i], flag);
  593. /*
  594. * Handle the overrun case for the last character only, since
  595. * the RxFIFO overflow happens after it is pushed to the FIFO
  596. * tail.
  597. */
  598. uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT,
  599. one->rx_buf[rxlen-1], flag);
  600. } else {
  601. if (unlikely(rxlen >= port->fifosize)) {
  602. dev_warn_ratelimited(port->dev, "Possible RX FIFO overrun\n");
  603. port->icount.buf_overrun++;
  604. /* Ensure sanity of RX level */
  605. rxlen = port->fifosize;
  606. }
  607. while (rxlen--) {
  608. ch = max310x_port_read(port, MAX310X_RHR_REG);
  609. sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
  610. sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
  611. MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
  612. port->icount.rx++;
  613. flag = TTY_NORMAL;
  614. if (unlikely(sts)) {
  615. if (sts & MAX310X_LSR_RXBRK_BIT) {
  616. port->icount.brk++;
  617. if (uart_handle_break(port))
  618. continue;
  619. } else if (sts & MAX310X_LSR_RXPAR_BIT)
  620. port->icount.parity++;
  621. else if (sts & MAX310X_LSR_FRERR_BIT)
  622. port->icount.frame++;
  623. else if (sts & MAX310X_LSR_RXOVR_BIT)
  624. port->icount.overrun++;
  625. sts &= port->read_status_mask;
  626. if (sts & MAX310X_LSR_RXBRK_BIT)
  627. flag = TTY_BREAK;
  628. else if (sts & MAX310X_LSR_RXPAR_BIT)
  629. flag = TTY_PARITY;
  630. else if (sts & MAX310X_LSR_FRERR_BIT)
  631. flag = TTY_FRAME;
  632. else if (sts & MAX310X_LSR_RXOVR_BIT)
  633. flag = TTY_OVERRUN;
  634. }
  635. if (uart_handle_sysrq_char(port, ch))
  636. continue;
  637. if (sts & port->ignore_status_mask)
  638. continue;
  639. uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
  640. }
  641. }
  642. tty_flip_buffer_push(&port->state->port);
  643. }
  644. static void max310x_handle_tx(struct uart_port *port)
  645. {
  646. struct circ_buf *xmit = &port->state->xmit;
  647. unsigned int txlen, to_send, until_end;
  648. if (unlikely(port->x_char)) {
  649. max310x_port_write(port, MAX310X_THR_REG, port->x_char);
  650. port->icount.tx++;
  651. port->x_char = 0;
  652. return;
  653. }
  654. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  655. return;
  656. /* Get length of data pending in circular buffer */
  657. to_send = uart_circ_chars_pending(xmit);
  658. until_end = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  659. if (likely(to_send)) {
  660. /* Limit to size of TX FIFO */
  661. txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
  662. txlen = port->fifosize - txlen;
  663. to_send = (to_send > txlen) ? txlen : to_send;
  664. if (until_end < to_send) {
  665. /* It's a circ buffer -- wrap around.
  666. * We could do that in one SPI transaction, but meh. */
  667. max310x_batch_write(port, xmit->buf + xmit->tail, until_end);
  668. max310x_batch_write(port, xmit->buf, to_send - until_end);
  669. } else {
  670. max310x_batch_write(port, xmit->buf + xmit->tail, to_send);
  671. }
  672. /* Add data to send */
  673. port->icount.tx += to_send;
  674. xmit->tail = (xmit->tail + to_send) & (UART_XMIT_SIZE - 1);
  675. }
  676. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  677. uart_write_wakeup(port);
  678. }
  679. static void max310x_start_tx(struct uart_port *port)
  680. {
  681. struct max310x_one *one = to_max310x_port(port);
  682. schedule_work(&one->tx_work);
  683. }
  684. static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno)
  685. {
  686. struct uart_port *port = &s->p[portno].port;
  687. irqreturn_t res = IRQ_NONE;
  688. do {
  689. unsigned int ists, lsr, rxlen;
  690. /* Read IRQ status & RX FIFO level */
  691. ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
  692. rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
  693. if (!ists && !rxlen)
  694. break;
  695. res = IRQ_HANDLED;
  696. if (ists & MAX310X_IRQ_CTS_BIT) {
  697. lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
  698. uart_handle_cts_change(port,
  699. !!(lsr & MAX310X_LSR_CTS_BIT));
  700. }
  701. if (rxlen)
  702. max310x_handle_rx(port, rxlen);
  703. if (ists & MAX310X_IRQ_TXEMPTY_BIT)
  704. max310x_start_tx(port);
  705. } while (1);
  706. return res;
  707. }
  708. static irqreturn_t max310x_ist(int irq, void *dev_id)
  709. {
  710. struct max310x_port *s = (struct max310x_port *)dev_id;
  711. bool handled = false;
  712. if (s->devtype->nr > 1) {
  713. do {
  714. unsigned int val = ~0;
  715. WARN_ON_ONCE(regmap_read(s->regmap,
  716. MAX310X_GLOBALIRQ_REG, &val));
  717. val = ((1 << s->devtype->nr) - 1) & ~val;
  718. if (!val)
  719. break;
  720. if (max310x_port_irq(s, fls(val) - 1) == IRQ_HANDLED)
  721. handled = true;
  722. } while (1);
  723. } else {
  724. if (max310x_port_irq(s, 0) == IRQ_HANDLED)
  725. handled = true;
  726. }
  727. return IRQ_RETVAL(handled);
  728. }
  729. static void max310x_tx_proc(struct work_struct *ws)
  730. {
  731. struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
  732. max310x_handle_tx(&one->port);
  733. }
  734. static unsigned int max310x_tx_empty(struct uart_port *port)
  735. {
  736. u8 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
  737. return lvl ? 0 : TIOCSER_TEMT;
  738. }
  739. static unsigned int max310x_get_mctrl(struct uart_port *port)
  740. {
  741. /* DCD and DSR are not wired and CTS/RTS is handled automatically
  742. * so just indicate DSR and CAR asserted
  743. */
  744. return TIOCM_DSR | TIOCM_CAR;
  745. }
  746. static void max310x_md_proc(struct work_struct *ws)
  747. {
  748. struct max310x_one *one = container_of(ws, struct max310x_one, md_work);
  749. max310x_port_update(&one->port, MAX310X_MODE2_REG,
  750. MAX310X_MODE2_LOOPBACK_BIT,
  751. (one->port.mctrl & TIOCM_LOOP) ?
  752. MAX310X_MODE2_LOOPBACK_BIT : 0);
  753. }
  754. static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
  755. {
  756. struct max310x_one *one = to_max310x_port(port);
  757. schedule_work(&one->md_work);
  758. }
  759. static void max310x_break_ctl(struct uart_port *port, int break_state)
  760. {
  761. max310x_port_update(port, MAX310X_LCR_REG,
  762. MAX310X_LCR_TXBREAK_BIT,
  763. break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
  764. }
  765. static void max310x_set_termios(struct uart_port *port,
  766. struct ktermios *termios,
  767. struct ktermios *old)
  768. {
  769. unsigned int lcr = 0, flow = 0;
  770. int baud;
  771. /* Mask termios capabilities we don't support */
  772. termios->c_cflag &= ~CMSPAR;
  773. /* Word size */
  774. switch (termios->c_cflag & CSIZE) {
  775. case CS5:
  776. break;
  777. case CS6:
  778. lcr = MAX310X_LCR_LENGTH0_BIT;
  779. break;
  780. case CS7:
  781. lcr = MAX310X_LCR_LENGTH1_BIT;
  782. break;
  783. case CS8:
  784. default:
  785. lcr = MAX310X_LCR_LENGTH1_BIT | MAX310X_LCR_LENGTH0_BIT;
  786. break;
  787. }
  788. /* Parity */
  789. if (termios->c_cflag & PARENB) {
  790. lcr |= MAX310X_LCR_PARITY_BIT;
  791. if (!(termios->c_cflag & PARODD))
  792. lcr |= MAX310X_LCR_EVENPARITY_BIT;
  793. }
  794. /* Stop bits */
  795. if (termios->c_cflag & CSTOPB)
  796. lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
  797. /* Update LCR register */
  798. max310x_port_write(port, MAX310X_LCR_REG, lcr);
  799. /* Set read status mask */
  800. port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
  801. if (termios->c_iflag & INPCK)
  802. port->read_status_mask |= MAX310X_LSR_RXPAR_BIT |
  803. MAX310X_LSR_FRERR_BIT;
  804. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  805. port->read_status_mask |= MAX310X_LSR_RXBRK_BIT;
  806. /* Set status ignore mask */
  807. port->ignore_status_mask = 0;
  808. if (termios->c_iflag & IGNBRK)
  809. port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT;
  810. if (!(termios->c_cflag & CREAD))
  811. port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT |
  812. MAX310X_LSR_RXOVR_BIT |
  813. MAX310X_LSR_FRERR_BIT |
  814. MAX310X_LSR_RXBRK_BIT;
  815. /* Configure flow control */
  816. max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
  817. max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
  818. /* Disable transmitter before enabling AutoCTS or auto transmitter
  819. * flow control
  820. */
  821. if (termios->c_cflag & CRTSCTS || termios->c_iflag & IXOFF) {
  822. max310x_port_update(port, MAX310X_MODE1_REG,
  823. MAX310X_MODE1_TXDIS_BIT,
  824. MAX310X_MODE1_TXDIS_BIT);
  825. }
  826. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
  827. if (termios->c_cflag & CRTSCTS) {
  828. /* Enable AUTORTS and AUTOCTS */
  829. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  830. flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
  831. MAX310X_FLOWCTRL_AUTORTS_BIT;
  832. }
  833. if (termios->c_iflag & IXON)
  834. flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT |
  835. MAX310X_FLOWCTRL_SWFLOWEN_BIT;
  836. if (termios->c_iflag & IXOFF) {
  837. port->status |= UPSTAT_AUTOXOFF;
  838. flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
  839. MAX310X_FLOWCTRL_SWFLOWEN_BIT;
  840. }
  841. max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
  842. /* Enable transmitter after disabling AutoCTS and auto transmitter
  843. * flow control
  844. */
  845. if (!(termios->c_cflag & CRTSCTS) && !(termios->c_iflag & IXOFF)) {
  846. max310x_port_update(port, MAX310X_MODE1_REG,
  847. MAX310X_MODE1_TXDIS_BIT,
  848. 0);
  849. }
  850. /* Get baud rate generator configuration */
  851. baud = uart_get_baud_rate(port, termios, old,
  852. port->uartclk / 16 / 0xffff,
  853. port->uartclk / 4);
  854. /* Setup baudrate generator */
  855. baud = max310x_set_baud(port, baud);
  856. /* Update timeout according to new baud rate */
  857. uart_update_timeout(port, termios->c_cflag, baud);
  858. }
  859. static void max310x_rs_proc(struct work_struct *ws)
  860. {
  861. struct max310x_one *one = container_of(ws, struct max310x_one, rs_work);
  862. unsigned int delay, mode1 = 0, mode2 = 0;
  863. delay = (one->port.rs485.delay_rts_before_send << 4) |
  864. one->port.rs485.delay_rts_after_send;
  865. max310x_port_write(&one->port, MAX310X_HDPIXDELAY_REG, delay);
  866. if (one->port.rs485.flags & SER_RS485_ENABLED) {
  867. mode1 = MAX310X_MODE1_TRNSCVCTRL_BIT;
  868. if (!(one->port.rs485.flags & SER_RS485_RX_DURING_TX))
  869. mode2 = MAX310X_MODE2_ECHOSUPR_BIT;
  870. }
  871. max310x_port_update(&one->port, MAX310X_MODE1_REG,
  872. MAX310X_MODE1_TRNSCVCTRL_BIT, mode1);
  873. max310x_port_update(&one->port, MAX310X_MODE2_REG,
  874. MAX310X_MODE2_ECHOSUPR_BIT, mode2);
  875. }
  876. static int max310x_rs485_config(struct uart_port *port,
  877. struct serial_rs485 *rs485)
  878. {
  879. struct max310x_one *one = to_max310x_port(port);
  880. if ((rs485->delay_rts_before_send > 0x0f) ||
  881. (rs485->delay_rts_after_send > 0x0f))
  882. return -ERANGE;
  883. rs485->flags &= SER_RS485_RTS_ON_SEND | SER_RS485_RX_DURING_TX |
  884. SER_RS485_ENABLED;
  885. memset(rs485->padding, 0, sizeof(rs485->padding));
  886. port->rs485 = *rs485;
  887. schedule_work(&one->rs_work);
  888. return 0;
  889. }
  890. static int max310x_startup(struct uart_port *port)
  891. {
  892. struct max310x_port *s = dev_get_drvdata(port->dev);
  893. unsigned int val;
  894. s->devtype->power(port, 1);
  895. /* Configure MODE1 register */
  896. max310x_port_update(port, MAX310X_MODE1_REG,
  897. MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
  898. /* Configure MODE2 register & Reset FIFOs*/
  899. val = MAX310X_MODE2_RXEMPTINV_BIT | MAX310X_MODE2_FIFORST_BIT;
  900. max310x_port_write(port, MAX310X_MODE2_REG, val);
  901. max310x_port_update(port, MAX310X_MODE2_REG,
  902. MAX310X_MODE2_FIFORST_BIT, 0);
  903. /* Configure mode1/mode2 to have rs485/rs232 enabled at startup */
  904. val = (clamp(port->rs485.delay_rts_before_send, 0U, 15U) << 4) |
  905. clamp(port->rs485.delay_rts_after_send, 0U, 15U);
  906. max310x_port_write(port, MAX310X_HDPIXDELAY_REG, val);
  907. if (port->rs485.flags & SER_RS485_ENABLED) {
  908. max310x_port_update(port, MAX310X_MODE1_REG,
  909. MAX310X_MODE1_TRNSCVCTRL_BIT,
  910. MAX310X_MODE1_TRNSCVCTRL_BIT);
  911. if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
  912. max310x_port_update(port, MAX310X_MODE2_REG,
  913. MAX310X_MODE2_ECHOSUPR_BIT,
  914. MAX310X_MODE2_ECHOSUPR_BIT);
  915. }
  916. /* Configure flow control levels */
  917. /* Flow control halt level 96, resume level 48 */
  918. max310x_port_write(port, MAX310X_FLOWLVL_REG,
  919. MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
  920. /* Clear IRQ status register */
  921. max310x_port_read(port, MAX310X_IRQSTS_REG);
  922. /* Enable RX, TX, CTS change interrupts */
  923. val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
  924. max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
  925. return 0;
  926. }
  927. static void max310x_shutdown(struct uart_port *port)
  928. {
  929. struct max310x_port *s = dev_get_drvdata(port->dev);
  930. /* Disable all interrupts */
  931. max310x_port_write(port, MAX310X_IRQEN_REG, 0);
  932. s->devtype->power(port, 0);
  933. }
  934. static const char *max310x_type(struct uart_port *port)
  935. {
  936. struct max310x_port *s = dev_get_drvdata(port->dev);
  937. return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
  938. }
  939. static int max310x_request_port(struct uart_port *port)
  940. {
  941. /* Do nothing */
  942. return 0;
  943. }
  944. static void max310x_config_port(struct uart_port *port, int flags)
  945. {
  946. if (flags & UART_CONFIG_TYPE)
  947. port->type = PORT_MAX310X;
  948. }
  949. static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
  950. {
  951. if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
  952. return -EINVAL;
  953. if (s->irq != port->irq)
  954. return -EINVAL;
  955. return 0;
  956. }
  957. static void max310x_null_void(struct uart_port *port)
  958. {
  959. /* Do nothing */
  960. }
  961. static const struct uart_ops max310x_ops = {
  962. .tx_empty = max310x_tx_empty,
  963. .set_mctrl = max310x_set_mctrl,
  964. .get_mctrl = max310x_get_mctrl,
  965. .stop_tx = max310x_null_void,
  966. .start_tx = max310x_start_tx,
  967. .stop_rx = max310x_null_void,
  968. .break_ctl = max310x_break_ctl,
  969. .startup = max310x_startup,
  970. .shutdown = max310x_shutdown,
  971. .set_termios = max310x_set_termios,
  972. .type = max310x_type,
  973. .request_port = max310x_request_port,
  974. .release_port = max310x_null_void,
  975. .config_port = max310x_config_port,
  976. .verify_port = max310x_verify_port,
  977. };
  978. static int __maybe_unused max310x_suspend(struct device *dev)
  979. {
  980. struct max310x_port *s = dev_get_drvdata(dev);
  981. int i;
  982. for (i = 0; i < s->devtype->nr; i++) {
  983. uart_suspend_port(&max310x_uart, &s->p[i].port);
  984. s->devtype->power(&s->p[i].port, 0);
  985. }
  986. return 0;
  987. }
  988. static int __maybe_unused max310x_resume(struct device *dev)
  989. {
  990. struct max310x_port *s = dev_get_drvdata(dev);
  991. int i;
  992. for (i = 0; i < s->devtype->nr; i++) {
  993. s->devtype->power(&s->p[i].port, 1);
  994. uart_resume_port(&max310x_uart, &s->p[i].port);
  995. }
  996. return 0;
  997. }
  998. static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
  999. #ifdef CONFIG_GPIOLIB
  1000. static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
  1001. {
  1002. unsigned int val;
  1003. struct max310x_port *s = gpiochip_get_data(chip);
  1004. struct uart_port *port = &s->p[offset / 4].port;
  1005. val = max310x_port_read(port, MAX310X_GPIODATA_REG);
  1006. return !!((val >> 4) & (1 << (offset % 4)));
  1007. }
  1008. static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  1009. {
  1010. struct max310x_port *s = gpiochip_get_data(chip);
  1011. struct uart_port *port = &s->p[offset / 4].port;
  1012. max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
  1013. value ? 1 << (offset % 4) : 0);
  1014. }
  1015. static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  1016. {
  1017. struct max310x_port *s = gpiochip_get_data(chip);
  1018. struct uart_port *port = &s->p[offset / 4].port;
  1019. max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
  1020. return 0;
  1021. }
  1022. static int max310x_gpio_direction_output(struct gpio_chip *chip,
  1023. unsigned offset, int value)
  1024. {
  1025. struct max310x_port *s = gpiochip_get_data(chip);
  1026. struct uart_port *port = &s->p[offset / 4].port;
  1027. max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
  1028. value ? 1 << (offset % 4) : 0);
  1029. max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
  1030. 1 << (offset % 4));
  1031. return 0;
  1032. }
  1033. static int max310x_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  1034. unsigned long config)
  1035. {
  1036. struct max310x_port *s = gpiochip_get_data(chip);
  1037. struct uart_port *port = &s->p[offset / 4].port;
  1038. switch (pinconf_to_config_param(config)) {
  1039. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  1040. max310x_port_update(port, MAX310X_GPIOCFG_REG,
  1041. 1 << ((offset % 4) + 4),
  1042. 1 << ((offset % 4) + 4));
  1043. return 0;
  1044. case PIN_CONFIG_DRIVE_PUSH_PULL:
  1045. max310x_port_update(port, MAX310X_GPIOCFG_REG,
  1046. 1 << ((offset % 4) + 4), 0);
  1047. return 0;
  1048. default:
  1049. return -ENOTSUPP;
  1050. }
  1051. }
  1052. #endif
  1053. static int max310x_probe(struct device *dev, struct max310x_devtype *devtype,
  1054. struct regmap *regmap, int irq)
  1055. {
  1056. int i, ret, fmin, fmax, freq, uartclk;
  1057. struct clk *clk_osc, *clk_xtal;
  1058. struct max310x_port *s;
  1059. bool xtal = false;
  1060. if (IS_ERR(regmap))
  1061. return PTR_ERR(regmap);
  1062. /* Alloc port structure */
  1063. s = devm_kzalloc(dev, struct_size(s, p, devtype->nr), GFP_KERNEL);
  1064. if (!s) {
  1065. dev_err(dev, "Error allocating port structure\n");
  1066. return -ENOMEM;
  1067. }
  1068. clk_osc = devm_clk_get(dev, "osc");
  1069. clk_xtal = devm_clk_get(dev, "xtal");
  1070. if (!IS_ERR(clk_osc)) {
  1071. s->clk = clk_osc;
  1072. fmin = 500000;
  1073. fmax = 35000000;
  1074. } else if (!IS_ERR(clk_xtal)) {
  1075. s->clk = clk_xtal;
  1076. fmin = 1000000;
  1077. fmax = 4000000;
  1078. xtal = true;
  1079. } else if (PTR_ERR(clk_osc) == -EPROBE_DEFER ||
  1080. PTR_ERR(clk_xtal) == -EPROBE_DEFER) {
  1081. return -EPROBE_DEFER;
  1082. } else {
  1083. dev_err(dev, "Cannot get clock\n");
  1084. return -EINVAL;
  1085. }
  1086. ret = clk_prepare_enable(s->clk);
  1087. if (ret)
  1088. return ret;
  1089. freq = clk_get_rate(s->clk);
  1090. /* Check frequency limits */
  1091. if (freq < fmin || freq > fmax) {
  1092. ret = -ERANGE;
  1093. goto out_clk;
  1094. }
  1095. s->regmap = regmap;
  1096. s->devtype = devtype;
  1097. dev_set_drvdata(dev, s);
  1098. /* Check device to ensure we are talking to what we expect */
  1099. ret = devtype->detect(dev);
  1100. if (ret)
  1101. goto out_clk;
  1102. for (i = 0; i < devtype->nr; i++) {
  1103. unsigned int offs = i << 5;
  1104. /* Reset port */
  1105. regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
  1106. MAX310X_MODE2_RST_BIT);
  1107. /* Clear port reset */
  1108. regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
  1109. /* Wait for port startup */
  1110. do {
  1111. regmap_read(s->regmap,
  1112. MAX310X_BRGDIVLSB_REG + offs, &ret);
  1113. } while (ret != 0x01);
  1114. regmap_write(s->regmap, MAX310X_MODE1_REG + offs,
  1115. devtype->mode1);
  1116. }
  1117. uartclk = max310x_set_ref_clk(dev, s, freq, xtal);
  1118. dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
  1119. for (i = 0; i < devtype->nr; i++) {
  1120. unsigned int line;
  1121. line = find_first_zero_bit(max310x_lines, MAX310X_UART_NRMAX);
  1122. if (line == MAX310X_UART_NRMAX) {
  1123. ret = -ERANGE;
  1124. goto out_uart;
  1125. }
  1126. /* Initialize port data */
  1127. s->p[i].port.line = line;
  1128. s->p[i].port.dev = dev;
  1129. s->p[i].port.irq = irq;
  1130. s->p[i].port.type = PORT_MAX310X;
  1131. s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
  1132. s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
  1133. s->p[i].port.iotype = UPIO_PORT;
  1134. s->p[i].port.iobase = i * 0x20;
  1135. s->p[i].port.membase = (void __iomem *)~0;
  1136. s->p[i].port.uartclk = uartclk;
  1137. s->p[i].port.rs485_config = max310x_rs485_config;
  1138. s->p[i].port.ops = &max310x_ops;
  1139. /* Disable all interrupts */
  1140. max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
  1141. /* Clear IRQ status register */
  1142. max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
  1143. /* Initialize queue for start TX */
  1144. INIT_WORK(&s->p[i].tx_work, max310x_tx_proc);
  1145. /* Initialize queue for changing LOOPBACK mode */
  1146. INIT_WORK(&s->p[i].md_work, max310x_md_proc);
  1147. /* Initialize queue for changing RS485 mode */
  1148. INIT_WORK(&s->p[i].rs_work, max310x_rs_proc);
  1149. /* Initialize SPI-transfer buffers */
  1150. s->p[i].wr_header = (s->p[i].port.iobase + MAX310X_THR_REG) |
  1151. MAX310X_WRITE_BIT;
  1152. s->p[i].rd_header = (s->p[i].port.iobase + MAX310X_RHR_REG);
  1153. /* Register port */
  1154. ret = uart_add_one_port(&max310x_uart, &s->p[i].port);
  1155. if (ret) {
  1156. s->p[i].port.dev = NULL;
  1157. goto out_uart;
  1158. }
  1159. set_bit(line, max310x_lines);
  1160. /* Go to suspend mode */
  1161. devtype->power(&s->p[i].port, 0);
  1162. }
  1163. #ifdef CONFIG_GPIOLIB
  1164. /* Setup GPIO cotroller */
  1165. s->gpio.owner = THIS_MODULE;
  1166. s->gpio.parent = dev;
  1167. s->gpio.label = devtype->name;
  1168. s->gpio.direction_input = max310x_gpio_direction_input;
  1169. s->gpio.get = max310x_gpio_get;
  1170. s->gpio.direction_output= max310x_gpio_direction_output;
  1171. s->gpio.set = max310x_gpio_set;
  1172. s->gpio.set_config = max310x_gpio_set_config;
  1173. s->gpio.base = -1;
  1174. s->gpio.ngpio = devtype->nr * 4;
  1175. s->gpio.can_sleep = 1;
  1176. ret = devm_gpiochip_add_data(dev, &s->gpio, s);
  1177. if (ret)
  1178. goto out_uart;
  1179. #endif
  1180. /* Setup interrupt */
  1181. ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
  1182. IRQF_ONESHOT | IRQF_SHARED, dev_name(dev), s);
  1183. if (!ret)
  1184. return 0;
  1185. dev_err(dev, "Unable to reguest IRQ %i\n", irq);
  1186. out_uart:
  1187. for (i = 0; i < devtype->nr; i++) {
  1188. if (s->p[i].port.dev) {
  1189. uart_remove_one_port(&max310x_uart, &s->p[i].port);
  1190. clear_bit(s->p[i].port.line, max310x_lines);
  1191. }
  1192. }
  1193. out_clk:
  1194. clk_disable_unprepare(s->clk);
  1195. return ret;
  1196. }
  1197. static int max310x_remove(struct device *dev)
  1198. {
  1199. struct max310x_port *s = dev_get_drvdata(dev);
  1200. int i;
  1201. for (i = 0; i < s->devtype->nr; i++) {
  1202. cancel_work_sync(&s->p[i].tx_work);
  1203. cancel_work_sync(&s->p[i].md_work);
  1204. cancel_work_sync(&s->p[i].rs_work);
  1205. uart_remove_one_port(&max310x_uart, &s->p[i].port);
  1206. clear_bit(s->p[i].port.line, max310x_lines);
  1207. s->devtype->power(&s->p[i].port, 0);
  1208. }
  1209. clk_disable_unprepare(s->clk);
  1210. return 0;
  1211. }
  1212. static const struct of_device_id __maybe_unused max310x_dt_ids[] = {
  1213. { .compatible = "maxim,max3107", .data = &max3107_devtype, },
  1214. { .compatible = "maxim,max3108", .data = &max3108_devtype, },
  1215. { .compatible = "maxim,max3109", .data = &max3109_devtype, },
  1216. { .compatible = "maxim,max14830", .data = &max14830_devtype },
  1217. { }
  1218. };
  1219. MODULE_DEVICE_TABLE(of, max310x_dt_ids);
  1220. static struct regmap_config regcfg = {
  1221. .reg_bits = 8,
  1222. .val_bits = 8,
  1223. .write_flag_mask = MAX310X_WRITE_BIT,
  1224. .cache_type = REGCACHE_RBTREE,
  1225. .writeable_reg = max310x_reg_writeable,
  1226. .volatile_reg = max310x_reg_volatile,
  1227. .precious_reg = max310x_reg_precious,
  1228. };
  1229. #ifdef CONFIG_SPI_MASTER
  1230. static int max310x_spi_probe(struct spi_device *spi)
  1231. {
  1232. struct max310x_devtype *devtype;
  1233. struct regmap *regmap;
  1234. int ret;
  1235. /* Setup SPI bus */
  1236. spi->bits_per_word = 8;
  1237. spi->mode = spi->mode ? : SPI_MODE_0;
  1238. spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
  1239. ret = spi_setup(spi);
  1240. if (ret)
  1241. return ret;
  1242. if (spi->dev.of_node) {
  1243. const struct of_device_id *of_id =
  1244. of_match_device(max310x_dt_ids, &spi->dev);
  1245. if (!of_id)
  1246. return -ENODEV;
  1247. devtype = (struct max310x_devtype *)of_id->data;
  1248. } else {
  1249. const struct spi_device_id *id_entry = spi_get_device_id(spi);
  1250. devtype = (struct max310x_devtype *)id_entry->driver_data;
  1251. }
  1252. regcfg.max_register = devtype->nr * 0x20 - 1;
  1253. regmap = devm_regmap_init_spi(spi, &regcfg);
  1254. return max310x_probe(&spi->dev, devtype, regmap, spi->irq);
  1255. }
  1256. static int max310x_spi_remove(struct spi_device *spi)
  1257. {
  1258. return max310x_remove(&spi->dev);
  1259. }
  1260. static const struct spi_device_id max310x_id_table[] = {
  1261. { "max3107", (kernel_ulong_t)&max3107_devtype, },
  1262. { "max3108", (kernel_ulong_t)&max3108_devtype, },
  1263. { "max3109", (kernel_ulong_t)&max3109_devtype, },
  1264. { "max14830", (kernel_ulong_t)&max14830_devtype, },
  1265. { }
  1266. };
  1267. MODULE_DEVICE_TABLE(spi, max310x_id_table);
  1268. static struct spi_driver max310x_spi_driver = {
  1269. .driver = {
  1270. .name = MAX310X_NAME,
  1271. .of_match_table = of_match_ptr(max310x_dt_ids),
  1272. .pm = &max310x_pm_ops,
  1273. },
  1274. .probe = max310x_spi_probe,
  1275. .remove = max310x_spi_remove,
  1276. .id_table = max310x_id_table,
  1277. };
  1278. #endif
  1279. static int __init max310x_uart_init(void)
  1280. {
  1281. int ret;
  1282. bitmap_zero(max310x_lines, MAX310X_UART_NRMAX);
  1283. ret = uart_register_driver(&max310x_uart);
  1284. if (ret)
  1285. return ret;
  1286. #ifdef CONFIG_SPI_MASTER
  1287. ret = spi_register_driver(&max310x_spi_driver);
  1288. if (ret)
  1289. uart_unregister_driver(&max310x_uart);
  1290. #endif
  1291. return ret;
  1292. }
  1293. module_init(max310x_uart_init);
  1294. static void __exit max310x_uart_exit(void)
  1295. {
  1296. #ifdef CONFIG_SPI_MASTER
  1297. spi_unregister_driver(&max310x_spi_driver);
  1298. #endif
  1299. uart_unregister_driver(&max310x_uart);
  1300. }
  1301. module_exit(max310x_uart_exit);
  1302. MODULE_LICENSE("GPL");
  1303. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  1304. MODULE_DESCRIPTION("MAX310X serial driver");