blob: 8a3e92638e1076a3c6f7af6ad38cd9fc46d91c53 [file] [log] [blame]
Alexander Shiyanf6544412012-08-06 19:42:32 +04001/*
Alexander Shiyan003236d2013-06-29 10:44:19 +04002 * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver
Alexander Shiyanf6544412012-08-06 19:42:32 +04003 *
Alexander Shiyan6286767a2016-06-07 18:59:24 +03004 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyanf6544412012-08-06 19:42:32 +04005 *
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
Alexander Shiyan10d8b342013-06-29 10:44:17 +040016#include <linux/bitops.h>
Alexander Shiyand3a8a252014-02-10 22:18:31 +040017#include <linux/clk.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040018#include <linux/delay.h>
19#include <linux/device.h>
Linus Walleija00d60a2015-12-08 23:11:05 +010020#include <linux/gpio/driver.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040021#include <linux/module.h>
Alexander Shiyan58afc902014-02-10 22:18:36 +040022#include <linux/of.h>
23#include <linux/of_device.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040024#include <linux/regmap.h>
Alexander Shiyanf6544412012-08-06 19:42:32 +040025#include <linux/serial_core.h>
26#include <linux/serial.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
Greg Kroah-Hartman1456dad2014-02-13 15:18:57 -080029#include <linux/spi/spi.h>
Geert Uytterhoeven58dea352014-03-12 15:01:54 +010030#include <linux/uaccess.h>
Alexander Shiyan10d8b342013-06-29 10:44:17 +040031
Alexander Shiyan10d8b342013-06-29 10:44:17 +040032#define MAX310X_NAME "max310x"
Alexander Shiyanf6544412012-08-06 19:42:32 +040033#define MAX310X_MAJOR 204
34#define MAX310X_MINOR 209
Alexander Shiyan78adcca2016-06-07 18:59:27 +030035#define MAX310X_UART_NRMAX 16
Alexander Shiyanf6544412012-08-06 19:42:32 +040036
37/* MAX310X register definitions */
38#define MAX310X_RHR_REG (0x00) /* RX FIFO */
39#define MAX310X_THR_REG (0x00) /* TX FIFO */
40#define MAX310X_IRQEN_REG (0x01) /* IRQ enable */
41#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
42#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
43#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040044#define MAX310X_REG_05 (0x05)
45#define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
Alexander Shiyanf6544412012-08-06 19:42:32 +040046#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
47#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
48#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
49#define MAX310X_MODE1_REG (0x09) /* MODE1 */
50#define MAX310X_MODE2_REG (0x0a) /* MODE2 */
51#define MAX310X_LCR_REG (0x0b) /* LCR */
52#define MAX310X_RXTO_REG (0x0c) /* RX timeout */
53#define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */
54#define MAX310X_IRDA_REG (0x0e) /* IRDA settings */
55#define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */
56#define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */
57#define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */
58#define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */
59#define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */
60#define MAX310X_XON1_REG (0x14) /* XON1 character */
61#define MAX310X_XON2_REG (0x15) /* XON2 character */
62#define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */
63#define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */
64#define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */
65#define MAX310X_GPIODATA_REG (0x19) /* GPIO data */
66#define MAX310X_PLLCFG_REG (0x1a) /* PLL config */
67#define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */
68#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
69#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
70#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040071#define MAX310X_REG_1F (0x1f)
72
73#define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
74
75#define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
76#define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
77
78/* Extended registers */
79#define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
Alexander Shiyanf6544412012-08-06 19:42:32 +040080
81/* IRQ register bits */
82#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
83#define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
84#define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */
85#define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
86#define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
87#define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
88#define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
89#define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
90
91/* LSR register bits */
92#define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */
93#define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
94#define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
95#define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */
96#define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */
97#define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
98#define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */
99
100/* Special character register bits */
101#define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
102#define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
103#define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
104#define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
105#define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */
106#define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
107
108/* Status register bits */
109#define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
110#define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
111#define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
112#define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
113#define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
114#define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
115
116/* MODE1 register bits */
117#define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
118#define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
119#define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
120#define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
121#define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
122#define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
123#define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
124#define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
125
126/* MODE2 register bits */
127#define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */
128#define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
129#define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
130#define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
131#define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
132#define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
133#define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
134#define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
135
136/* LCR register bits */
137#define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
138#define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
139 *
140 * Word length bits table:
141 * 00 -> 5 bit words
142 * 01 -> 6 bit words
143 * 10 -> 7 bit words
144 * 11 -> 8 bit words
145 */
146#define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
147 *
148 * STOP length bit table:
149 * 0 -> 1 stop bit
150 * 1 -> 1-1.5 stop bits if
151 * word length is 5,
152 * 2 stop bits otherwise
153 */
154#define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
155#define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
156#define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
157#define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
158#define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */
Alexander Shiyanf6544412012-08-06 19:42:32 +0400159
160/* IRDA register bits */
161#define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
162#define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
Alexander Shiyanf6544412012-08-06 19:42:32 +0400163
164/* Flow control trigger level register masks */
165#define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
166#define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
167#define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f)
168#define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4)
169
170/* FIFO interrupt trigger level register masks */
171#define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f) /* TX FIFO trigger level */
172#define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0) /* RX FIFO trigger level */
173#define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f)
174#define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4)
175
176/* Flow control register bits */
177#define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
178#define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
179#define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
180 * are used in conjunction with
181 * XOFF2 for definition of
182 * special character */
183#define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
184#define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
185#define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
186 *
187 * SWFLOW bits 1 & 0 table:
188 * 00 -> no transmitter flow
189 * control
190 * 01 -> receiver compares
191 * XON2 and XOFF2
192 * and controls
193 * transmitter
194 * 10 -> receiver compares
195 * XON1 and XOFF1
196 * and controls
197 * transmitter
198 * 11 -> receiver compares
199 * XON1, XON2, XOFF1 and
200 * XOFF2 and controls
201 * transmitter
202 */
203#define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
204#define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
205 *
206 * SWFLOW bits 3 & 2 table:
207 * 00 -> no received flow
208 * control
209 * 01 -> transmitter generates
210 * XON2 and XOFF2
211 * 10 -> transmitter generates
212 * XON1 and XOFF1
213 * 11 -> transmitter generates
214 * XON1, XON2, XOFF1 and
215 * XOFF2
216 */
217
Alexander Shiyanf6544412012-08-06 19:42:32 +0400218/* PLL configuration register masks */
219#define MAX310X_PLLCFG_PREDIV_MASK (0x3f) /* PLL predivision value */
220#define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0) /* PLL multiplication factor */
221
222/* Baud rate generator configuration register bits */
223#define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
224#define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
225
226/* Clock source register bits */
227#define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
228#define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
229#define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
230#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
231#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
232
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400233/* Global commands */
234#define MAX310X_EXTREG_ENBL (0xce)
235#define MAX310X_EXTREG_DSBL (0xcd)
236
Alexander Shiyanf6544412012-08-06 19:42:32 +0400237/* Misc definitions */
238#define MAX310X_FIFO_SIZE (128)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400239#define MAX310x_REV_MASK (0xfc)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400240
241/* MAX3107 specific */
242#define MAX3107_REV_ID (0xa0)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400243
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400244/* MAX3109 specific */
245#define MAX3109_REV_ID (0xc0)
246
Alexander Shiyan003236d2013-06-29 10:44:19 +0400247/* MAX14830 specific */
248#define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
249#define MAX14830_REV_ID (0xb0)
250
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400251struct max310x_devtype {
252 char name[9];
253 int nr;
254 int (*detect)(struct device *);
255 void (*power)(struct uart_port *, int);
256};
Alexander Shiyanf6544412012-08-06 19:42:32 +0400257
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400258struct max310x_one {
259 struct uart_port port;
260 struct work_struct tx_work;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400261 struct work_struct md_work;
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300262 struct work_struct rs_work;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400263};
264
265struct max310x_port {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400266 struct max310x_devtype *devtype;
267 struct regmap *regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400268 struct mutex mutex;
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400269 struct clk *clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400270#ifdef CONFIG_GPIOLIB
271 struct gpio_chip gpio;
272#endif
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400273 struct max310x_one p[0];
Alexander Shiyanf6544412012-08-06 19:42:32 +0400274};
275
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300276static struct uart_driver max310x_uart = {
277 .owner = THIS_MODULE,
278 .driver_name = MAX310X_NAME,
279 .dev_name = "ttyMAX",
280 .major = MAX310X_MAJOR,
281 .minor = MAX310X_MINOR,
Alexander Shiyan78adcca2016-06-07 18:59:27 +0300282 .nr = MAX310X_UART_NRMAX,
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300283};
284
Alexander Shiyan78adcca2016-06-07 18:59:27 +0300285static DECLARE_BITMAP(max310x_lines, MAX310X_UART_NRMAX);
286
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400287static u8 max310x_port_read(struct uart_port *port, u8 reg)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400288{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400289 struct max310x_port *s = dev_get_drvdata(port->dev);
290 unsigned int val = 0;
291
292 regmap_read(s->regmap, port->iobase + reg, &val);
293
294 return val;
295}
296
297static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
298{
299 struct max310x_port *s = dev_get_drvdata(port->dev);
300
301 regmap_write(s->regmap, port->iobase + reg, val);
302}
303
304static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
305{
306 struct max310x_port *s = dev_get_drvdata(port->dev);
307
308 regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
309}
310
311static int max3107_detect(struct device *dev)
312{
313 struct max310x_port *s = dev_get_drvdata(dev);
314 unsigned int val = 0;
315 int ret;
316
317 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
318 if (ret)
319 return ret;
320
321 if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
322 dev_err(dev,
323 "%s ID 0x%02x does not match\n", s->devtype->name, val);
324 return -ENODEV;
325 }
326
327 return 0;
328}
329
330static int max3108_detect(struct device *dev)
331{
332 struct max310x_port *s = dev_get_drvdata(dev);
333 unsigned int val = 0;
334 int ret;
335
336 /* MAX3108 have not REV ID register, we just check default value
337 * from clocksource register to make sure everything works.
338 */
339 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
340 if (ret)
341 return ret;
342
343 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
344 dev_err(dev, "%s not present\n", s->devtype->name);
345 return -ENODEV;
346 }
347
348 return 0;
349}
350
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400351static int max3109_detect(struct device *dev)
352{
353 struct max310x_port *s = dev_get_drvdata(dev);
354 unsigned int val = 0;
355 int ret;
356
Gregory Hermant32304d72014-09-30 08:59:17 +0200357 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
358 MAX310X_EXTREG_ENBL);
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400359 if (ret)
360 return ret;
361
Gregory Hermant32304d72014-09-30 08:59:17 +0200362 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
363 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400364 if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
365 dev_err(dev,
366 "%s ID 0x%02x does not match\n", s->devtype->name, val);
367 return -ENODEV;
368 }
369
370 return 0;
371}
372
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400373static void max310x_power(struct uart_port *port, int on)
374{
375 max310x_port_update(port, MAX310X_MODE1_REG,
376 MAX310X_MODE1_FORCESLEEP_BIT,
377 on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
378 if (on)
379 msleep(50);
380}
381
Alexander Shiyan003236d2013-06-29 10:44:19 +0400382static int max14830_detect(struct device *dev)
383{
384 struct max310x_port *s = dev_get_drvdata(dev);
385 unsigned int val = 0;
386 int ret;
387
388 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
389 MAX310X_EXTREG_ENBL);
390 if (ret)
391 return ret;
392
393 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
394 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
395 if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
396 dev_err(dev,
397 "%s ID 0x%02x does not match\n", s->devtype->name, val);
398 return -ENODEV;
399 }
400
401 return 0;
402}
403
404static void max14830_power(struct uart_port *port, int on)
405{
406 max310x_port_update(port, MAX310X_BRGCFG_REG,
407 MAX14830_BRGCFG_CLKDIS_BIT,
408 on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
409 if (on)
410 msleep(50);
411}
412
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400413static const struct max310x_devtype max3107_devtype = {
414 .name = "MAX3107",
415 .nr = 1,
416 .detect = max3107_detect,
417 .power = max310x_power,
418};
419
420static const struct max310x_devtype max3108_devtype = {
421 .name = "MAX3108",
422 .nr = 1,
423 .detect = max3108_detect,
424 .power = max310x_power,
425};
426
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400427static const struct max310x_devtype max3109_devtype = {
428 .name = "MAX3109",
429 .nr = 2,
430 .detect = max3109_detect,
431 .power = max310x_power,
432};
433
Alexander Shiyan003236d2013-06-29 10:44:19 +0400434static const struct max310x_devtype max14830_devtype = {
435 .name = "MAX14830",
436 .nr = 4,
437 .detect = max14830_detect,
438 .power = max14830_power,
439};
440
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400441static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
442{
443 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400444 case MAX310X_IRQSTS_REG:
445 case MAX310X_LSR_IRQSTS_REG:
446 case MAX310X_SPCHR_IRQSTS_REG:
447 case MAX310X_STS_IRQSTS_REG:
448 case MAX310X_TXFIFOLVL_REG:
449 case MAX310X_RXFIFOLVL_REG:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400450 return false;
451 default:
452 break;
453 }
454
455 return true;
456}
457
458static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
459{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400460 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400461 case MAX310X_RHR_REG:
462 case MAX310X_IRQSTS_REG:
463 case MAX310X_LSR_IRQSTS_REG:
464 case MAX310X_SPCHR_IRQSTS_REG:
465 case MAX310X_STS_IRQSTS_REG:
466 case MAX310X_TXFIFOLVL_REG:
467 case MAX310X_RXFIFOLVL_REG:
468 case MAX310X_GPIODATA_REG:
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400469 case MAX310X_BRGDIVLSB_REG:
470 case MAX310X_REG_05:
471 case MAX310X_REG_1F:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400472 return true;
473 default:
474 break;
475 }
476
477 return false;
478}
479
480static bool max310x_reg_precious(struct device *dev, unsigned int reg)
481{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400482 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400483 case MAX310X_RHR_REG:
484 case MAX310X_IRQSTS_REG:
485 case MAX310X_SPCHR_IRQSTS_REG:
486 case MAX310X_STS_IRQSTS_REG:
487 return true;
488 default:
489 break;
490 }
491
492 return false;
493}
494
Alexander Shiyane97e1552014-02-07 18:16:04 +0400495static int max310x_set_baud(struct uart_port *port, int baud)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400496{
Alexander Shiyane97e1552014-02-07 18:16:04 +0400497 unsigned int mode = 0, clk = port->uartclk, div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400498
Alexander Shiyane97e1552014-02-07 18:16:04 +0400499 /* Check for minimal value for divider */
500 if (div < 16)
501 div = 16;
502
503 if (clk % baud && (div / 16) < 0x8000) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400504 /* Mode x2 */
505 mode = MAX310X_BRGCFG_2XMODE_BIT;
Alexander Shiyane97e1552014-02-07 18:16:04 +0400506 clk = port->uartclk * 2;
507 div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400508
Alexander Shiyane97e1552014-02-07 18:16:04 +0400509 if (clk % baud && (div / 16) < 0x8000) {
510 /* Mode x4 */
511 mode = MAX310X_BRGCFG_4XMODE_BIT;
512 clk = port->uartclk * 4;
513 div = clk / baud;
514 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400515 }
516
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400517 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8);
518 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16);
519 max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode);
Alexander Shiyane97e1552014-02-07 18:16:04 +0400520
521 return DIV_ROUND_CLOSEST(clk, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400522}
523
Bill Pemberton9671f092012-11-19 13:21:50 -0500524static int max310x_update_best_err(unsigned long f, long *besterr)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400525{
526 /* Use baudrate 115200 for calculate error */
527 long err = f % (115200 * 16);
528
529 if ((*besterr < 0) || (*besterr > err)) {
530 *besterr = err;
531 return 0;
532 }
533
534 return 1;
535}
536
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400537static int max310x_set_ref_clk(struct max310x_port *s, unsigned long freq,
538 bool xtal)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400539{
540 unsigned int div, clksrc, pllcfg = 0;
541 long besterr = -1;
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400542 unsigned long fdiv, fmul, bestfreq = freq;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400543
544 /* First, update error without PLL */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400545 max310x_update_best_err(freq, &besterr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400546
547 /* Try all possible PLL dividers */
548 for (div = 1; (div <= 63) && besterr; div++) {
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400549 fdiv = DIV_ROUND_CLOSEST(freq, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400550
551 /* Try multiplier 6 */
552 fmul = fdiv * 6;
553 if ((fdiv >= 500000) && (fdiv <= 800000))
554 if (!max310x_update_best_err(fmul, &besterr)) {
555 pllcfg = (0 << 6) | div;
556 bestfreq = fmul;
557 }
558 /* Try multiplier 48 */
559 fmul = fdiv * 48;
560 if ((fdiv >= 850000) && (fdiv <= 1200000))
561 if (!max310x_update_best_err(fmul, &besterr)) {
562 pllcfg = (1 << 6) | div;
563 bestfreq = fmul;
564 }
565 /* Try multiplier 96 */
566 fmul = fdiv * 96;
567 if ((fdiv >= 425000) && (fdiv <= 1000000))
568 if (!max310x_update_best_err(fmul, &besterr)) {
569 pllcfg = (2 << 6) | div;
570 bestfreq = fmul;
571 }
572 /* Try multiplier 144 */
573 fmul = fdiv * 144;
574 if ((fdiv >= 390000) && (fdiv <= 667000))
575 if (!max310x_update_best_err(fmul, &besterr)) {
576 pllcfg = (3 << 6) | div;
577 bestfreq = fmul;
578 }
579 }
580
581 /* Configure clock source */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400582 clksrc = xtal ? MAX310X_CLKSRC_CRYST_BIT : MAX310X_CLKSRC_EXTCLK_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400583
584 /* Configure PLL */
585 if (pllcfg) {
586 clksrc |= MAX310X_CLKSRC_PLL_BIT;
587 regmap_write(s->regmap, MAX310X_PLLCFG_REG, pllcfg);
588 } else
589 clksrc |= MAX310X_CLKSRC_PLLBYP_BIT;
590
591 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
592
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400593 /* Wait for crystal */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400594 if (pllcfg && xtal)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400595 msleep(10);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400596
597 return (int)bestfreq;
598}
599
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400600static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400601{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400602 unsigned int sts, ch, flag;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400603
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400604 if (unlikely(rxlen >= port->fifosize)) {
Alexander Shiyan78adcca2016-06-07 18:59:27 +0300605 dev_warn_ratelimited(port->dev, "Possible RX FIFO overrun\n");
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400606 port->icount.buf_overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400607 /* Ensure sanity of RX level */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400608 rxlen = port->fifosize;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400609 }
610
Alexander Shiyanf6544412012-08-06 19:42:32 +0400611 while (rxlen--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400612 ch = max310x_port_read(port, MAX310X_RHR_REG);
613 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400614
615 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
616 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
617
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400618 port->icount.rx++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400619 flag = TTY_NORMAL;
620
621 if (unlikely(sts)) {
622 if (sts & MAX310X_LSR_RXBRK_BIT) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400623 port->icount.brk++;
624 if (uart_handle_break(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400625 continue;
626 } else if (sts & MAX310X_LSR_RXPAR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400627 port->icount.parity++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400628 else if (sts & MAX310X_LSR_FRERR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400629 port->icount.frame++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400630 else if (sts & MAX310X_LSR_RXOVR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400631 port->icount.overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400632
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400633 sts &= port->read_status_mask;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400634 if (sts & MAX310X_LSR_RXBRK_BIT)
635 flag = TTY_BREAK;
636 else if (sts & MAX310X_LSR_RXPAR_BIT)
637 flag = TTY_PARITY;
638 else if (sts & MAX310X_LSR_FRERR_BIT)
639 flag = TTY_FRAME;
640 else if (sts & MAX310X_LSR_RXOVR_BIT)
641 flag = TTY_OVERRUN;
642 }
643
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400644 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400645 continue;
646
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400647 if (sts & port->ignore_status_mask)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400648 continue;
649
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400650 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400651 }
652
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400653 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400654}
655
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400656static void max310x_handle_tx(struct uart_port *port)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400657{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400658 struct circ_buf *xmit = &port->state->xmit;
659 unsigned int txlen, to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400660
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400661 if (unlikely(port->x_char)) {
662 max310x_port_write(port, MAX310X_THR_REG, port->x_char);
663 port->icount.tx++;
664 port->x_char = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400665 return;
666 }
667
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400668 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400669 return;
670
671 /* Get length of data pending in circular buffer */
672 to_send = uart_circ_chars_pending(xmit);
673 if (likely(to_send)) {
674 /* Limit to size of TX FIFO */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400675 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
676 txlen = port->fifosize - txlen;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400677 to_send = (to_send > txlen) ? txlen : to_send;
678
Alexander Shiyanf6544412012-08-06 19:42:32 +0400679 /* Add data to send */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400680 port->icount.tx += to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400681 while (to_send--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400682 max310x_port_write(port, MAX310X_THR_REG,
683 xmit->buf[xmit->tail]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400684 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Joe Perchesfc8114722013-10-08 16:14:21 -0700685 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400686 }
687
688 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400689 uart_write_wakeup(port);
690}
691
692static void max310x_port_irq(struct max310x_port *s, int portno)
693{
694 struct uart_port *port = &s->p[portno].port;
695
696 do {
697 unsigned int ists, lsr, rxlen;
698
699 /* Read IRQ status & RX FIFO level */
700 ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
701 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
702 if (!ists && !rxlen)
703 break;
704
705 if (ists & MAX310X_IRQ_CTS_BIT) {
706 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
707 uart_handle_cts_change(port,
708 !!(lsr & MAX310X_LSR_CTS_BIT));
709 }
710 if (rxlen)
711 max310x_handle_rx(port, rxlen);
712 if (ists & MAX310X_IRQ_TXEMPTY_BIT) {
713 mutex_lock(&s->mutex);
714 max310x_handle_tx(port);
715 mutex_unlock(&s->mutex);
716 }
717 } while (1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400718}
719
720static irqreturn_t max310x_ist(int irq, void *dev_id)
721{
722 struct max310x_port *s = (struct max310x_port *)dev_id;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400723
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300724 if (s->devtype->nr > 1) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400725 do {
726 unsigned int val = ~0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400727
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400728 WARN_ON_ONCE(regmap_read(s->regmap,
729 MAX310X_GLOBALIRQ_REG, &val));
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300730 val = ((1 << s->devtype->nr) - 1) & ~val;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400731 if (!val)
732 break;
733 max310x_port_irq(s, fls(val) - 1);
734 } while (1);
735 } else
736 max310x_port_irq(s, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400737
738 return IRQ_HANDLED;
739}
740
741static void max310x_wq_proc(struct work_struct *ws)
742{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400743 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
744 struct max310x_port *s = dev_get_drvdata(one->port.dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400745
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400746 mutex_lock(&s->mutex);
747 max310x_handle_tx(&one->port);
748 mutex_unlock(&s->mutex);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400749}
750
751static void max310x_start_tx(struct uart_port *port)
752{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400753 struct max310x_one *one = container_of(port, struct max310x_one, port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400754
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400755 if (!work_pending(&one->tx_work))
756 schedule_work(&one->tx_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400757}
758
759static unsigned int max310x_tx_empty(struct uart_port *port)
760{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400761 unsigned int lvl, sts;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400762
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400763 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
764 sts = max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400765
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400766 return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400767}
768
769static unsigned int max310x_get_mctrl(struct uart_port *port)
770{
771 /* DCD and DSR are not wired and CTS/RTS is handled automatically
772 * so just indicate DSR and CAR asserted
773 */
774 return TIOCM_DSR | TIOCM_CAR;
775}
776
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400777static void max310x_md_proc(struct work_struct *ws)
778{
779 struct max310x_one *one = container_of(ws, struct max310x_one, md_work);
780
781 max310x_port_update(&one->port, MAX310X_MODE2_REG,
782 MAX310X_MODE2_LOOPBACK_BIT,
783 (one->port.mctrl & TIOCM_LOOP) ?
784 MAX310X_MODE2_LOOPBACK_BIT : 0);
785}
786
Alexander Shiyanf6544412012-08-06 19:42:32 +0400787static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
788{
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400789 struct max310x_one *one = container_of(port, struct max310x_one, port);
790
791 schedule_work(&one->md_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400792}
793
794static void max310x_break_ctl(struct uart_port *port, int break_state)
795{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400796 max310x_port_update(port, MAX310X_LCR_REG,
797 MAX310X_LCR_TXBREAK_BIT,
798 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400799}
800
801static void max310x_set_termios(struct uart_port *port,
802 struct ktermios *termios,
803 struct ktermios *old)
804{
Alexander Shiyane940e812016-06-07 18:59:25 +0300805 unsigned int lcr = 0, flow = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400806 int baud;
807
Alexander Shiyanf6544412012-08-06 19:42:32 +0400808 /* Mask termios capabilities we don't support */
809 termios->c_cflag &= ~CMSPAR;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400810
811 /* Word size */
812 switch (termios->c_cflag & CSIZE) {
813 case CS5:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400814 break;
815 case CS6:
Alexander Shiyane940e812016-06-07 18:59:25 +0300816 lcr = MAX310X_LCR_LENGTH0_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400817 break;
818 case CS7:
Alexander Shiyane940e812016-06-07 18:59:25 +0300819 lcr = MAX310X_LCR_LENGTH1_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400820 break;
821 case CS8:
822 default:
Alexander Shiyane940e812016-06-07 18:59:25 +0300823 lcr = MAX310X_LCR_LENGTH1_BIT | MAX310X_LCR_LENGTH0_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400824 break;
825 }
826
827 /* Parity */
828 if (termios->c_cflag & PARENB) {
829 lcr |= MAX310X_LCR_PARITY_BIT;
830 if (!(termios->c_cflag & PARODD))
831 lcr |= MAX310X_LCR_EVENPARITY_BIT;
832 }
833
834 /* Stop bits */
835 if (termios->c_cflag & CSTOPB)
836 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
837
838 /* Update LCR register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400839 max310x_port_write(port, MAX310X_LCR_REG, lcr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400840
841 /* Set read status mask */
842 port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
843 if (termios->c_iflag & INPCK)
844 port->read_status_mask |= MAX310X_LSR_RXPAR_BIT |
845 MAX310X_LSR_FRERR_BIT;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400846 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400847 port->read_status_mask |= MAX310X_LSR_RXBRK_BIT;
848
849 /* Set status ignore mask */
850 port->ignore_status_mask = 0;
851 if (termios->c_iflag & IGNBRK)
852 port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT;
853 if (!(termios->c_cflag & CREAD))
854 port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT |
855 MAX310X_LSR_RXOVR_BIT |
856 MAX310X_LSR_FRERR_BIT |
857 MAX310X_LSR_RXBRK_BIT;
858
859 /* Configure flow control */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400860 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
861 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400862 if (termios->c_cflag & CRTSCTS)
863 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
864 MAX310X_FLOWCTRL_AUTORTS_BIT;
865 if (termios->c_iflag & IXON)
866 flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT |
867 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
868 if (termios->c_iflag & IXOFF)
869 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
870 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400871 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400872
873 /* Get baud rate generator configuration */
874 baud = uart_get_baud_rate(port, termios, old,
875 port->uartclk / 16 / 0xffff,
876 port->uartclk / 4);
877
878 /* Setup baudrate generator */
Alexander Shiyane97e1552014-02-07 18:16:04 +0400879 baud = max310x_set_baud(port, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400880
881 /* Update timeout according to new baud rate */
882 uart_update_timeout(port, termios->c_cflag, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400883}
884
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300885static void max310x_rs_proc(struct work_struct *ws)
Alexander Shiyan55367c62014-02-10 22:18:34 +0400886{
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300887 struct max310x_one *one = container_of(ws, struct max310x_one, rs_work);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400888 unsigned int val;
889
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300890 val = (one->port.rs485.delay_rts_before_send << 4) |
891 one->port.rs485.delay_rts_after_send;
892 max310x_port_write(&one->port, MAX310X_HDPIXDELAY_REG, val);
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100893
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300894 if (one->port.rs485.flags & SER_RS485_ENABLED) {
895 max310x_port_update(&one->port, MAX310X_MODE1_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100896 MAX310X_MODE1_TRNSCVCTRL_BIT,
897 MAX310X_MODE1_TRNSCVCTRL_BIT);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300898 max310x_port_update(&one->port, MAX310X_MODE2_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100899 MAX310X_MODE2_ECHOSUPR_BIT,
900 MAX310X_MODE2_ECHOSUPR_BIT);
901 } else {
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300902 max310x_port_update(&one->port, MAX310X_MODE1_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100903 MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300904 max310x_port_update(&one->port, MAX310X_MODE2_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100905 MAX310X_MODE2_ECHOSUPR_BIT, 0);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400906 }
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300907}
908
909static int max310x_rs485_config(struct uart_port *port,
910 struct serial_rs485 *rs485)
911{
912 struct max310x_one *one = container_of(port, struct max310x_one, port);
913
914 if ((rs485->delay_rts_before_send > 0x0f) ||
915 (rs485->delay_rts_after_send > 0x0f))
916 return -ERANGE;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400917
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100918 rs485->flags &= SER_RS485_RTS_ON_SEND | SER_RS485_ENABLED;
919 memset(rs485->padding, 0, sizeof(rs485->padding));
920 port->rs485 = *rs485;
921
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300922 schedule_work(&one->rs_work);
923
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100924 return 0;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400925}
926
Alexander Shiyanf6544412012-08-06 19:42:32 +0400927static int max310x_startup(struct uart_port *port)
928{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400929 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400930 unsigned int val;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400931
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400932 s->devtype->power(port, 1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400933
Alexander Shiyanf6544412012-08-06 19:42:32 +0400934 /* Configure MODE1 register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400935 max310x_port_update(port, MAX310X_MODE1_REG,
Alexander Shiyan55367c62014-02-10 22:18:34 +0400936 MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400937
Alexander Shiyan55367c62014-02-10 22:18:34 +0400938 /* Configure MODE2 register & Reset FIFOs*/
939 val = MAX310X_MODE2_RXEMPTINV_BIT | MAX310X_MODE2_FIFORST_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400940 max310x_port_write(port, MAX310X_MODE2_REG, val);
941 max310x_port_update(port, MAX310X_MODE2_REG,
942 MAX310X_MODE2_FIFORST_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400943
944 /* Configure flow control levels */
945 /* Flow control halt level 96, resume level 48 */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400946 max310x_port_write(port, MAX310X_FLOWLVL_REG,
947 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
Alexander Shiyanf6544412012-08-06 19:42:32 +0400948
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400949 /* Clear IRQ status register */
950 max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400951
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400952 /* Enable RX, TX, CTS change interrupts */
953 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
954 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400955
956 return 0;
957}
958
959static void max310x_shutdown(struct uart_port *port)
960{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400961 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400962
963 /* Disable all interrupts */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400964 max310x_port_write(port, MAX310X_IRQEN_REG, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400965
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400966 s->devtype->power(port, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400967}
968
969static const char *max310x_type(struct uart_port *port)
970{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400971 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400972
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400973 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400974}
975
976static int max310x_request_port(struct uart_port *port)
977{
978 /* Do nothing */
979 return 0;
980}
981
Alexander Shiyanf6544412012-08-06 19:42:32 +0400982static void max310x_config_port(struct uart_port *port, int flags)
983{
984 if (flags & UART_CONFIG_TYPE)
985 port->type = PORT_MAX310X;
986}
987
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400988static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400989{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400990 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
991 return -EINVAL;
992 if (s->irq != port->irq)
993 return -EINVAL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400994
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400995 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400996}
997
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400998static void max310x_null_void(struct uart_port *port)
999{
1000 /* Do nothing */
1001}
1002
1003static const struct uart_ops max310x_ops = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001004 .tx_empty = max310x_tx_empty,
1005 .set_mctrl = max310x_set_mctrl,
1006 .get_mctrl = max310x_get_mctrl,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001007 .stop_tx = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001008 .start_tx = max310x_start_tx,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001009 .stop_rx = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001010 .break_ctl = max310x_break_ctl,
1011 .startup = max310x_startup,
1012 .shutdown = max310x_shutdown,
1013 .set_termios = max310x_set_termios,
1014 .type = max310x_type,
1015 .request_port = max310x_request_port,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001016 .release_port = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001017 .config_port = max310x_config_port,
1018 .verify_port = max310x_verify_port,
1019};
1020
Alexander Shiyanc2978292013-07-29 19:27:32 +04001021static int __maybe_unused max310x_suspend(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001022{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001023 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001024 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001025
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001026 for (i = 0; i < s->devtype->nr; i++) {
1027 uart_suspend_port(&max310x_uart, &s->p[i].port);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001028 s->devtype->power(&s->p[i].port, 0);
1029 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001030
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001031 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001032}
1033
Alexander Shiyanc2978292013-07-29 19:27:32 +04001034static int __maybe_unused max310x_resume(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001035{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001036 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001037 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001038
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001039 for (i = 0; i < s->devtype->nr; i++) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001040 s->devtype->power(&s->p[i].port, 1);
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001041 uart_resume_port(&max310x_uart, &s->p[i].port);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001042 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001043
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001044 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001045}
1046
Alexander Shiyan27027a72014-02-10 22:18:30 +04001047static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
1048
Alexander Shiyanf6544412012-08-06 19:42:32 +04001049#ifdef CONFIG_GPIOLIB
1050static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
1051{
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001052 unsigned int val;
Linus Walleija00d60a2015-12-08 23:11:05 +01001053 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001054 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001055
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001056 val = max310x_port_read(port, MAX310X_GPIODATA_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001057
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001058 return !!((val >> 4) & (1 << (offset % 4)));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001059}
1060
1061static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1062{
Linus Walleija00d60a2015-12-08 23:11:05 +01001063 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001064 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001065
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001066 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1067 value ? 1 << (offset % 4) : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001068}
1069
1070static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1071{
Linus Walleija00d60a2015-12-08 23:11:05 +01001072 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001073 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001074
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001075 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001076
1077 return 0;
1078}
1079
1080static int max310x_gpio_direction_output(struct gpio_chip *chip,
1081 unsigned offset, int value)
1082{
Linus Walleija00d60a2015-12-08 23:11:05 +01001083 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001084 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001085
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001086 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1087 value ? 1 << (offset % 4) : 0);
1088 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
1089 1 << (offset % 4));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001090
1091 return 0;
1092}
1093#endif
1094
Alexander Shiyan27027a72014-02-10 22:18:30 +04001095static int max310x_probe(struct device *dev, struct max310x_devtype *devtype,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001096 struct regmap *regmap, int irq, unsigned long flags)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001097{
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001098 int i, ret, fmin, fmax, freq, uartclk;
1099 struct clk *clk_osc, *clk_xtal;
1100 struct max310x_port *s;
1101 bool xtal = false;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001102
Alexander Shiyan27027a72014-02-10 22:18:30 +04001103 if (IS_ERR(regmap))
1104 return PTR_ERR(regmap);
1105
Alexander Shiyanf6544412012-08-06 19:42:32 +04001106 /* Alloc port structure */
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001107 s = devm_kzalloc(dev, sizeof(*s) +
1108 sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001109 if (!s) {
1110 dev_err(dev, "Error allocating port structure\n");
1111 return -ENOMEM;
1112 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001113
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001114 clk_osc = devm_clk_get(dev, "osc");
1115 clk_xtal = devm_clk_get(dev, "xtal");
1116 if (!IS_ERR(clk_osc)) {
1117 s->clk = clk_osc;
1118 fmin = 500000;
1119 fmax = 35000000;
1120 } else if (!IS_ERR(clk_xtal)) {
1121 s->clk = clk_xtal;
1122 fmin = 1000000;
1123 fmax = 4000000;
1124 xtal = true;
1125 } else if (PTR_ERR(clk_osc) == -EPROBE_DEFER ||
1126 PTR_ERR(clk_xtal) == -EPROBE_DEFER) {
1127 return -EPROBE_DEFER;
1128 } else {
1129 dev_err(dev, "Cannot get clock\n");
1130 return -EINVAL;
1131 }
1132
1133 ret = clk_prepare_enable(s->clk);
1134 if (ret)
1135 return ret;
1136
1137 freq = clk_get_rate(s->clk);
1138 /* Check frequency limits */
1139 if (freq < fmin || freq > fmax) {
1140 ret = -ERANGE;
1141 goto out_clk;
1142 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001143
Alexander Shiyan27027a72014-02-10 22:18:30 +04001144 s->regmap = regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001145 s->devtype = devtype;
1146 dev_set_drvdata(dev, s);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001147
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001148 /* Check device to ensure we are talking to what we expect */
1149 ret = devtype->detect(dev);
1150 if (ret)
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001151 goto out_clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001152
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001153 for (i = 0; i < devtype->nr; i++) {
1154 unsigned int offs = i << 5;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001155
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001156 /* Reset port */
1157 regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
1158 MAX310X_MODE2_RST_BIT);
1159 /* Clear port reset */
1160 regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001161
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001162 /* Wait for port startup */
1163 do {
1164 regmap_read(s->regmap,
1165 MAX310X_BRGDIVLSB_REG + offs, &ret);
1166 } while (ret != 0x01);
1167
1168 regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs,
1169 MAX310X_MODE1_AUTOSLEEP_BIT,
1170 MAX310X_MODE1_AUTOSLEEP_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001171 }
1172
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001173 uartclk = max310x_set_ref_clk(s, freq, xtal);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001174 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
1175
Alexander Shiyandba29a22014-02-10 22:18:32 +04001176#ifdef CONFIG_GPIOLIB
1177 /* Setup GPIO cotroller */
1178 s->gpio.owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +01001179 s->gpio.parent = dev;
Alexander Shiyandba29a22014-02-10 22:18:32 +04001180 s->gpio.label = dev_name(dev);
1181 s->gpio.direction_input = max310x_gpio_direction_input;
1182 s->gpio.get = max310x_gpio_get;
1183 s->gpio.direction_output= max310x_gpio_direction_output;
1184 s->gpio.set = max310x_gpio_set;
1185 s->gpio.base = -1;
1186 s->gpio.ngpio = devtype->nr * 4;
1187 s->gpio.can_sleep = 1;
Alexander Shiyan0e8cc7c2016-06-07 18:59:23 +03001188 ret = devm_gpiochip_add_data(dev, &s->gpio, s);
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001189 if (ret)
Alexander Shiyan0e8cc7c2016-06-07 18:59:23 +03001190 goto out_clk;
Alexander Shiyandba29a22014-02-10 22:18:32 +04001191#endif
1192
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001193 mutex_init(&s->mutex);
1194
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001195 for (i = 0; i < devtype->nr; i++) {
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001196 unsigned int line;
1197
1198 line = find_first_zero_bit(max310x_lines, MAX310X_UART_NRMAX);
1199 if (line == MAX310X_UART_NRMAX) {
1200 ret = -ERANGE;
1201 goto out_uart;
1202 }
1203
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001204 /* Initialize port data */
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001205 s->p[i].port.line = line;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001206 s->p[i].port.dev = dev;
1207 s->p[i].port.irq = irq;
1208 s->p[i].port.type = PORT_MAX310X;
1209 s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001210 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001211 s->p[i].port.iotype = UPIO_PORT;
1212 s->p[i].port.iobase = i * 0x20;
1213 s->p[i].port.membase = (void __iomem *)~0;
1214 s->p[i].port.uartclk = uartclk;
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +01001215 s->p[i].port.rs485_config = max310x_rs485_config;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001216 s->p[i].port.ops = &max310x_ops;
1217 /* Disable all interrupts */
1218 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
1219 /* Clear IRQ status register */
1220 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
1221 /* Enable IRQ pin */
1222 max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG,
1223 MAX310X_MODE1_IRQSEL_BIT,
1224 MAX310X_MODE1_IRQSEL_BIT);
1225 /* Initialize queue for start TX */
1226 INIT_WORK(&s->p[i].tx_work, max310x_wq_proc);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +03001227 /* Initialize queue for changing LOOPBACK mode */
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001228 INIT_WORK(&s->p[i].md_work, max310x_md_proc);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +03001229 /* Initialize queue for changing RS485 mode */
1230 INIT_WORK(&s->p[i].rs_work, max310x_rs_proc);
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001231
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001232 /* Register port */
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001233 ret = uart_add_one_port(&max310x_uart, &s->p[i].port);
1234 if (ret) {
1235 s->p[i].port.dev = NULL;
1236 goto out_uart;
1237 }
1238 set_bit(line, max310x_lines);
1239
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001240 /* Go to suspend mode */
1241 devtype->power(&s->p[i].port, 0);
1242 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001243
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001244 /* Setup interrupt */
1245 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001246 IRQF_ONESHOT | flags, dev_name(dev), s);
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001247 if (!ret)
1248 return 0;
1249
1250 dev_err(dev, "Unable to reguest IRQ %i\n", irq);
Alexander Shiyandba29a22014-02-10 22:18:32 +04001251
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001252out_uart:
1253 for (i = 0; i < devtype->nr; i++) {
1254 if (s->p[i].port.dev) {
1255 uart_remove_one_port(&max310x_uart, &s->p[i].port);
1256 clear_bit(s->p[i].port.line, max310x_lines);
1257 }
1258 }
Alexander Shiyanc8246fe2016-06-07 18:59:26 +03001259
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001260 mutex_destroy(&s->mutex);
1261
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001262out_clk:
1263 clk_disable_unprepare(s->clk);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001264
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001265 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001266}
1267
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001268static int max310x_remove(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001269{
Alexander Shiyanf6544412012-08-06 19:42:32 +04001270 struct max310x_port *s = dev_get_drvdata(dev);
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001271 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001272
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001273 for (i = 0; i < s->devtype->nr; i++) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001274 cancel_work_sync(&s->p[i].tx_work);
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001275 cancel_work_sync(&s->p[i].md_work);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +03001276 cancel_work_sync(&s->p[i].rs_work);
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001277 uart_remove_one_port(&max310x_uart, &s->p[i].port);
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001278 clear_bit(s->p[i].port.line, max310x_lines);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001279 s->devtype->power(&s->p[i].port, 0);
1280 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001281
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001282 mutex_destroy(&s->mutex);
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001283 clk_disable_unprepare(s->clk);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001284
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001285 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001286}
1287
Alexander Shiyan58afc902014-02-10 22:18:36 +04001288static const struct of_device_id __maybe_unused max310x_dt_ids[] = {
1289 { .compatible = "maxim,max3107", .data = &max3107_devtype, },
1290 { .compatible = "maxim,max3108", .data = &max3108_devtype, },
1291 { .compatible = "maxim,max3109", .data = &max3109_devtype, },
1292 { .compatible = "maxim,max14830", .data = &max14830_devtype },
1293 { }
1294};
1295MODULE_DEVICE_TABLE(of, max310x_dt_ids);
1296
Alexander Shiyan27027a72014-02-10 22:18:30 +04001297static struct regmap_config regcfg = {
1298 .reg_bits = 8,
1299 .val_bits = 8,
1300 .write_flag_mask = 0x80,
1301 .cache_type = REGCACHE_RBTREE,
1302 .writeable_reg = max310x_reg_writeable,
1303 .volatile_reg = max310x_reg_volatile,
1304 .precious_reg = max310x_reg_precious,
1305};
1306
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001307#ifdef CONFIG_SPI_MASTER
1308static int max310x_spi_probe(struct spi_device *spi)
1309{
Alexander Shiyan58afc902014-02-10 22:18:36 +04001310 struct max310x_devtype *devtype;
1311 unsigned long flags = 0;
Alexander Shiyan27027a72014-02-10 22:18:30 +04001312 struct regmap *regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001313 int ret;
1314
1315 /* Setup SPI bus */
1316 spi->bits_per_word = 8;
1317 spi->mode = spi->mode ? : SPI_MODE_0;
1318 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
1319 ret = spi_setup(spi);
Alexander Shiyan27027a72014-02-10 22:18:30 +04001320 if (ret)
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001321 return ret;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001322
Alexander Shiyan58afc902014-02-10 22:18:36 +04001323 if (spi->dev.of_node) {
1324 const struct of_device_id *of_id =
1325 of_match_device(max310x_dt_ids, &spi->dev);
1326
1327 devtype = (struct max310x_devtype *)of_id->data;
1328 } else {
1329 const struct spi_device_id *id_entry = spi_get_device_id(spi);
1330
1331 devtype = (struct max310x_devtype *)id_entry->driver_data;
Alexander Shiyan58afc902014-02-10 22:18:36 +04001332 }
1333
Liu Xiangc164b002016-09-07 22:05:01 +08001334 flags = IRQF_TRIGGER_FALLING;
Alexander Shiyan27027a72014-02-10 22:18:30 +04001335 regcfg.max_register = devtype->nr * 0x20 - 1;
1336 regmap = devm_regmap_init_spi(spi, &regcfg);
1337
Alexander Shiyan58afc902014-02-10 22:18:36 +04001338 return max310x_probe(&spi->dev, devtype, regmap, spi->irq, flags);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001339}
1340
1341static int max310x_spi_remove(struct spi_device *spi)
1342{
1343 return max310x_remove(&spi->dev);
1344}
1345
Alexander Shiyanf6544412012-08-06 19:42:32 +04001346static const struct spi_device_id max310x_id_table[] = {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001347 { "max3107", (kernel_ulong_t)&max3107_devtype, },
1348 { "max3108", (kernel_ulong_t)&max3108_devtype, },
Alexander Shiyan21fc5092013-06-29 10:44:18 +04001349 { "max3109", (kernel_ulong_t)&max3109_devtype, },
Alexander Shiyan003236d2013-06-29 10:44:19 +04001350 { "max14830", (kernel_ulong_t)&max14830_devtype, },
Axel Lin1838b8c2012-11-04 23:34:18 +08001351 { }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001352};
1353MODULE_DEVICE_TABLE(spi, max310x_id_table);
1354
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001355static struct spi_driver max310x_spi_driver = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001356 .driver = {
Alexander Shiyan58afc902014-02-10 22:18:36 +04001357 .name = MAX310X_NAME,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001358 .of_match_table = of_match_ptr(max310x_dt_ids),
1359 .pm = &max310x_pm_ops,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001360 },
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001361 .probe = max310x_spi_probe,
1362 .remove = max310x_spi_remove,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001363 .id_table = max310x_id_table,
1364};
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001365#endif
Alexander Shiyanf6544412012-08-06 19:42:32 +04001366
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001367static int __init max310x_uart_init(void)
1368{
1369 int ret;
1370
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001371 bitmap_zero(max310x_lines, MAX310X_UART_NRMAX);
1372
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001373 ret = uart_register_driver(&max310x_uart);
1374 if (ret)
1375 return ret;
1376
1377#ifdef CONFIG_SPI_MASTER
1378 spi_register_driver(&max310x_spi_driver);
1379#endif
1380
1381 return 0;
1382}
1383module_init(max310x_uart_init);
1384
1385static void __exit max310x_uart_exit(void)
1386{
1387#ifdef CONFIG_SPI_MASTER
1388 spi_unregister_driver(&max310x_spi_driver);
1389#endif
1390
1391 uart_unregister_driver(&max310x_uart);
1392}
1393module_exit(max310x_uart_exit);
1394
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001395MODULE_LICENSE("GPL");
Alexander Shiyanf6544412012-08-06 19:42:32 +04001396MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1397MODULE_DESCRIPTION("MAX310X serial driver");