19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/serial_core.h>
22 #include <linux/serial.h>
23 #include <linux/tty.h>
30 #define MAX310X_MAJOR 204
31 #define MAX310X_MINOR 209
34 #define MAX310X_RHR_REG (0x00)
35 #define MAX310X_THR_REG (0x00)
36 #define MAX310X_IRQEN_REG (0x01)
37 #define MAX310X_IRQSTS_REG (0x02)
38 #define MAX310X_LSR_IRQEN_REG (0x03)
39 #define MAX310X_LSR_IRQSTS_REG (0x04)
40 #define MAX310X_SPCHR_IRQEN_REG (0x05)
41 #define MAX310X_SPCHR_IRQSTS_REG (0x06)
42 #define MAX310X_STS_IRQEN_REG (0x07)
43 #define MAX310X_STS_IRQSTS_REG (0x08)
44 #define MAX310X_MODE1_REG (0x09)
45 #define MAX310X_MODE2_REG (0x0a)
46 #define MAX310X_LCR_REG (0x0b)
47 #define MAX310X_RXTO_REG (0x0c)
48 #define MAX310X_HDPIXDELAY_REG (0x0d)
49 #define MAX310X_IRDA_REG (0x0e)
50 #define MAX310X_FLOWLVL_REG (0x0f)
51 #define MAX310X_FIFOTRIGLVL_REG (0x10)
52 #define MAX310X_TXFIFOLVL_REG (0x11)
53 #define MAX310X_RXFIFOLVL_REG (0x12)
54 #define MAX310X_FLOWCTRL_REG (0x13)
55 #define MAX310X_XON1_REG (0x14)
56 #define MAX310X_XON2_REG (0x15)
57 #define MAX310X_XOFF1_REG (0x16)
58 #define MAX310X_XOFF2_REG (0x17)
59 #define MAX310X_GPIOCFG_REG (0x18)
60 #define MAX310X_GPIODATA_REG (0x19)
61 #define MAX310X_PLLCFG_REG (0x1a)
62 #define MAX310X_BRGCFG_REG (0x1b)
63 #define MAX310X_BRGDIVLSB_REG (0x1c)
64 #define MAX310X_BRGDIVMSB_REG (0x1d)
65 #define MAX310X_CLKSRC_REG (0x1e)
67 #define MAX3107_REVID_REG (0x1f)
70 #define MAX310X_IRQ_LSR_BIT (1 << 0)
71 #define MAX310X_IRQ_SPCHR_BIT (1 << 1)
72 #define MAX310X_IRQ_STS_BIT (1 << 2)
73 #define MAX310X_IRQ_RXFIFO_BIT (1 << 3)
74 #define MAX310X_IRQ_TXFIFO_BIT (1 << 4)
75 #define MAX310X_IRQ_TXEMPTY_BIT (1 << 5)
76 #define MAX310X_IRQ_RXEMPTY_BIT (1 << 6)
77 #define MAX310X_IRQ_CTS_BIT (1 << 7)
80 #define MAX310X_LSR_RXTO_BIT (1 << 0)
81 #define MAX310X_LSR_RXOVR_BIT (1 << 1)
82 #define MAX310X_LSR_RXPAR_BIT (1 << 2)
83 #define MAX310X_LSR_FRERR_BIT (1 << 3)
84 #define MAX310X_LSR_RXBRK_BIT (1 << 4)
85 #define MAX310X_LSR_RXNOISE_BIT (1 << 5)
86 #define MAX310X_LSR_CTS_BIT (1 << 7)
89 #define MAX310X_SPCHR_XON1_BIT (1 << 0)
90 #define MAX310X_SPCHR_XON2_BIT (1 << 1)
91 #define MAX310X_SPCHR_XOFF1_BIT (1 << 2)
92 #define MAX310X_SPCHR_XOFF2_BIT (1 << 3)
93 #define MAX310X_SPCHR_BREAK_BIT (1 << 4)
94 #define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5)
97 #define MAX310X_STS_GPIO0_BIT (1 << 0)
98 #define MAX310X_STS_GPIO1_BIT (1 << 1)
99 #define MAX310X_STS_GPIO2_BIT (1 << 2)
100 #define MAX310X_STS_GPIO3_BIT (1 << 3)
101 #define MAX310X_STS_CLKREADY_BIT (1 << 5)
102 #define MAX310X_STS_SLEEP_BIT (1 << 6)
105 #define MAX310X_MODE1_RXDIS_BIT (1 << 0)
106 #define MAX310X_MODE1_TXDIS_BIT (1 << 1)
107 #define MAX310X_MODE1_TXHIZ_BIT (1 << 2)
108 #define MAX310X_MODE1_RTSHIZ_BIT (1 << 3)
109 #define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4)
110 #define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5)
111 #define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6)
112 #define MAX310X_MODE1_IRQSEL_BIT (1 << 7)
115 #define MAX310X_MODE2_RST_BIT (1 << 0)
116 #define MAX310X_MODE2_FIFORST_BIT (1 << 1)
117 #define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2)
118 #define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3)
119 #define MAX310X_MODE2_SPCHR_BIT (1 << 4)
120 #define MAX310X_MODE2_LOOPBACK_BIT (1 << 5)
121 #define MAX310X_MODE2_MULTIDROP_BIT (1 << 6)
122 #define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7)
125 #define MAX310X_LCR_LENGTH0_BIT (1 << 0)
126 #define MAX310X_LCR_LENGTH1_BIT (1 << 1)
134 #define MAX310X_LCR_STOPLEN_BIT (1 << 2)
142 #define MAX310X_LCR_PARITY_BIT (1 << 3)
143 #define MAX310X_LCR_EVENPARITY_BIT (1 << 4)
144 #define MAX310X_LCR_FORCEPARITY_BIT (1 << 5)
145 #define MAX310X_LCR_TXBREAK_BIT (1 << 6)
146 #define MAX310X_LCR_RTS_BIT (1 << 7)
147 #define MAX310X_LCR_WORD_LEN_5 (0x00)
148 #define MAX310X_LCR_WORD_LEN_6 (0x01)
149 #define MAX310X_LCR_WORD_LEN_7 (0x02)
150 #define MAX310X_LCR_WORD_LEN_8 (0x03)
153 #define MAX310X_IRDA_IRDAEN_BIT (1 << 0)
154 #define MAX310X_IRDA_SIR_BIT (1 << 1)
155 #define MAX310X_IRDA_SHORTIR_BIT (1 << 2)
156 #define MAX310X_IRDA_MIR_BIT (1 << 3)
157 #define MAX310X_IRDA_RXINV_BIT (1 << 4)
158 #define MAX310X_IRDA_TXINV_BIT (1 << 5)
161 #define MAX310X_FLOWLVL_HALT_MASK (0x000f)
162 #define MAX310X_FLOWLVL_RES_MASK (0x00f0)
163 #define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f)
164 #define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4)
167 #define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f)
168 #define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0)
169 #define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f)
170 #define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4)
173 #define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0)
174 #define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1)
175 #define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2)
179 #define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3)
180 #define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4)
181 #define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5)
199 #define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6)
200 #define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7)
215 #define MAX310X_GPIOCFG_GP0OUT_BIT (1 << 0)
216 #define MAX310X_GPIOCFG_GP1OUT_BIT (1 << 1)
217 #define MAX310X_GPIOCFG_GP2OUT_BIT (1 << 2)
218 #define MAX310X_GPIOCFG_GP3OUT_BIT (1 << 3)
219 #define MAX310X_GPIOCFG_GP0OD_BIT (1 << 4)
220 #define MAX310X_GPIOCFG_GP1OD_BIT (1 << 5)
221 #define MAX310X_GPIOCFG_GP2OD_BIT (1 << 6)
222 #define MAX310X_GPIOCFG_GP3OD_BIT (1 << 7)
225 #define MAX310X_GPIODATA_GP0OUT_BIT (1 << 0)
226 #define MAX310X_GPIODATA_GP1OUT_BIT (1 << 1)
227 #define MAX310X_GPIODATA_GP2OUT_BIT (1 << 2)
228 #define MAX310X_GPIODATA_GP3OUT_BIT (1 << 3)
229 #define MAX310X_GPIODATA_GP0IN_BIT (1 << 4)
230 #define MAX310X_GPIODATA_GP1IN_BIT (1 << 5)
231 #define MAX310X_GPIODATA_GP2IN_BIT (1 << 6)
232 #define MAX310X_GPIODATA_GP3IN_BIT (1 << 7)
235 #define MAX310X_PLLCFG_PREDIV_MASK (0x3f)
236 #define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0)
239 #define MAX310X_BRGCFG_2XMODE_BIT (1 << 4)
240 #define MAX310X_BRGCFG_4XMODE_BIT (1 << 5)
243 #define MAX310X_CLKSRC_CRYST_BIT (1 << 1)
244 #define MAX310X_CLKSRC_PLL_BIT (1 << 2)
245 #define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3)
246 #define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4)
247 #define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7)
250 #define MAX310X_FIFO_SIZE (128)
253 #define MAX3107_REV_ID (0xa0)
254 #define MAX3107_REV_MASK (0xfe)
257 #define MAX310X_IRQ_TX (MAX310X_IRQ_TXFIFO_BIT | \
258 MAX310X_IRQ_TXEMPTY_BIT)
259 #define MAX310X_IRQ_RX (MAX310X_IRQ_RXFIFO_BIT | \
260 MAX310X_IRQ_RXEMPTY_BIT)
276 #ifdef CONFIG_GPIOLIB
277 struct gpio_chip
gpio;
281 struct regmap_config
regcfg;
291 static bool max3107_8_reg_writeable(
struct device *
dev,
unsigned int reg)
309 static bool max310x_reg_volatile(
struct device *dev,
unsigned int reg)
328 static bool max310x_reg_precious(
struct device *dev,
unsigned int reg)
360 ((
div / 16) >> 8) & 0xff);
371 unsigned int sts = 0;
381 static int __devinit max310x_update_best_err(
unsigned long f,
long *besterr)
384 long err = f % (115200 * 16);
386 if ((*besterr < 0) || (*besterr >
err)) {
396 unsigned int div, clksrc, pllcfg = 0;
401 max310x_update_best_err(s->
pdata->frequency, &besterr);
404 for (div = 1; (div <= 63) && besterr; div++) {
409 if ((fdiv >= 500000) && (fdiv <= 800000))
410 if (!max310x_update_best_err(fmul, &besterr)) {
411 pllcfg = (0 << 6) | div;
416 if ((fdiv >= 850000) && (fdiv <= 1200000))
417 if (!max310x_update_best_err(fmul, &besterr)) {
418 pllcfg = (1 << 6) | div;
423 if ((fdiv >= 425000) && (fdiv <= 1000000))
424 if (!max310x_update_best_err(fmul, &besterr)) {
425 pllcfg = (2 << 6) | div;
430 if ((fdiv >= 390000) && (fdiv <= 667000))
431 if (!max310x_update_best_err(fmul, &besterr)) {
432 pllcfg = (3 << 6) | div;
455 dev_dbg(s->
port.dev,
"Reference clock set to %lu Hz\n", bestfreq);
457 return (
int)bestfreq;
462 unsigned int sts = 0, ch = 0,
flag;
469 dev_warn(s->
port.dev,
"Possible RX FIFO overrun %d\n", rxlen);
488 s->
port.icount.brk++;
489 if (uart_handle_break(&s->
port))
492 s->
port.icount.parity++;
494 s->
port.icount.frame++;
496 s->
port.icount.overrun++;
498 sts &= s->
port.read_status_mask;
499 if (sts & MAX310X_LSR_RXBRK_BIT)
501 else if (sts & MAX310X_LSR_RXPAR_BIT)
503 else if (sts & MAX310X_LSR_FRERR_BIT)
505 else if (sts & MAX310X_LSR_RXOVR_BIT)
512 if (sts & s->
port.ignore_status_mask)
527 unsigned int txlen = 0, to_send;
545 to_send = (to_send >
txlen) ? txlen : to_send;
550 s->
port.icount.tx += to_send;
565 unsigned int ists = 0, lsr = 0, rxlen = 0;
577 dev_dbg(s->
port.dev,
"IRQ status: 0x%02x\n", ists);
580 max310x_handle_rx(s, rxlen);
582 max310x_handle_tx(s);
598 max310x_handle_tx(s);
621 unsigned int val = 0;
636 static unsigned int max310x_get_mctrl(
struct uart_port *
port)
644 static void max310x_set_mctrl(
struct uart_port *
port,
unsigned int mctrl)
651 static void max310x_break_ctl(
struct uart_port *
port,
int break_state)
667 unsigned int lcr, flow = 0;
721 MAX310X_LSR_RXOVR_BIT |
722 MAX310X_LSR_FRERR_BIT |
745 max310x_set_baud(s, baud);
753 static int max310x_startup(
struct uart_port *port)
758 if (s->
pdata->suspend)
759 s->
pdata->suspend(0);
764 max310x_set_baud(s, 9600);
824 static void max310x_shutdown(
struct uart_port *port)
833 if (s->
pdata->suspend)
834 s->
pdata->suspend(1);
837 static const char *max310x_type(
struct uart_port *port)
844 static int max310x_request_port(
struct uart_port *port)
850 static void max310x_release_port(
struct uart_port *port)
855 static void max310x_config_port(
struct uart_port *port,
int flags)
865 if (ser->
irq == port->
irq)
871 static struct uart_ops max310x_ops = {
872 .tx_empty = max310x_tx_empty,
873 .set_mctrl = max310x_set_mctrl,
874 .get_mctrl = max310x_get_mctrl,
875 .stop_tx = max310x_stop_tx,
876 .start_tx = max310x_start_tx,
877 .stop_rx = max310x_stop_rx,
878 .enable_ms = max310x_enable_ms,
879 .break_ctl = max310x_break_ctl,
880 .startup = max310x_startup,
881 .shutdown = max310x_shutdown,
882 .set_termios = max310x_set_termios,
883 .type = max310x_type,
884 .request_port = max310x_request_port,
885 .release_port = max310x_release_port,
886 .config_port = max310x_config_port,
887 .verify_port = max310x_verify_port,
908 if (s->
pdata->suspend)
909 s->
pdata->suspend(1);
914 static int max310x_resume(
struct spi_device *spi)
920 if (s->
pdata->suspend)
921 s->
pdata->suspend(0);
937 #ifdef CONFIG_GPIOLIB
938 static int max310x_gpio_get(
struct gpio_chip *
chip,
unsigned offset)
940 unsigned int val = 0;
947 return !!((val >> 4) & (1 << offset));
950 static void max310x_gpio_set(
struct gpio_chip *chip,
unsigned offset,
int value)
960 static int max310x_gpio_direction_input(
struct gpio_chip *chip,
unsigned offset)
973 static int max310x_gpio_direction_output(
struct gpio_chip *chip,
974 unsigned offset,
int value)
995 .frequency = 26000000,
1004 unsigned int val = 0;
1008 if (spi->
irq <= 0) {
1009 dev_err(dev,
"No IRQ specified\n");
1016 dev_err(dev,
"Error allocating port structure\n");
1022 dev_warn(dev,
"No platform data supplied, using defaults\n");
1023 pdata = &generic_plat_data;
1030 s->
name =
"MAX3107";
1033 s->
regcfg.max_register = 0x1f;
1036 s->
name =
"MAX3108";
1039 s->
regcfg.max_register = 0x1e;
1042 dev_err(dev,
"Unsupported chip type %i\n", chiptype);
1066 s->
regcfg.read_flag_mask = 0x00;
1067 s->
regcfg.write_flag_mask = 0x80;
1069 s->
regcfg.writeable_reg = max3107_8_reg_writeable;
1070 s->
regcfg.volatile_reg = max310x_reg_volatile;
1071 s->
regcfg.precious_reg = max310x_reg_precious;
1074 ret = PTR_ERR(s->
regmap);
1075 dev_err(dev,
"Failed to initialize register map\n");
1082 dev_err(dev,
"SPI transfer failed\n");
1093 dev_err(dev,
"%s ID 0x%02x does not match\n",
1120 s->
uartclk = max310x_set_ref_clk(s);
1136 dev_err(dev,
"Unable to reguest IRQ %i\n", spi->
irq);
1142 s->
uart.driver_name = dev_name(dev);
1143 s->
uart.dev_name =
"ttyMAX";
1148 dev_err(dev,
"Registering UART driver failed\n");
1166 s->
port.ops = &max310x_ops;
1169 #ifdef CONFIG_GPIOLIB
1174 s->gpio.label = dev_name(dev);
1175 s->gpio.direction_input = max310x_gpio_direction_input;
1176 s->gpio.get = max310x_gpio_get;
1177 s->gpio.direction_output= max310x_gpio_direction_output;
1178 s->gpio.set = max310x_gpio_set;
1186 dev_info(dev,
"GPIO support not enabled\n");
1196 dev_err(dev,
"Frequency parameter incorrect\n");
1211 dev_dbg(dev,
"Removing port\n");
1221 #ifdef CONFIG_GPIOLIB
1222 if (s->
pdata->gpio_base) {
1225 dev_err(dev,
"Failed to remove gpio chip: %d\n", ret);
1231 if (s->
pdata->suspend)
1232 s->
pdata->suspend(1);
1251 .probe = max310x_probe,
1253 .suspend = max310x_suspend,
1254 .resume = max310x_resume,
1255 .id_table = max310x_id_table,