max310x.c 38 KB

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