blob: bd626ec325d56070f9f12574fb867fcfc5b9ae6a [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Alexander Shiyanf6544412012-08-06 19:42:32 +04002/*
Alexander Shiyan003236d2013-06-29 10:44:19 +04003 * Maxim (Dallas) MAX3107/8/9, MAX14830 serial driver
Alexander Shiyanf6544412012-08-06 19:42:32 +04004 *
Alexander Shiyan6286767a2016-06-07 18:59:24 +03005 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyanf6544412012-08-06 19:42:32 +04006 *
7 * Based on max3100.c, by Christian Pellegrin <chripell@evolware.org>
8 * Based on max3110.c, by Feng Tang <feng.tang@intel.com>
9 * Based on max3107.c, by Aavamobile
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Alexander Shiyan10d8b342013-06-29 10:44:17 +040017#include <linux/bitops.h>
Alexander Shiyand3a8a252014-02-10 22:18:31 +040018#include <linux/clk.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040019#include <linux/delay.h>
20#include <linux/device.h>
Linus Walleija00d60a2015-12-08 23:11:05 +010021#include <linux/gpio/driver.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040022#include <linux/module.h>
Alexander Shiyan58afc902014-02-10 22:18:36 +040023#include <linux/of.h>
24#include <linux/of_device.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040025#include <linux/regmap.h>
Alexander Shiyanf6544412012-08-06 19:42:32 +040026#include <linux/serial_core.h>
27#include <linux/serial.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
Greg Kroah-Hartman1456dad2014-02-13 15:18:57 -080030#include <linux/spi/spi.h>
Geert Uytterhoeven58dea352014-03-12 15:01:54 +010031#include <linux/uaccess.h>
Alexander Shiyan10d8b342013-06-29 10:44:17 +040032
Alexander Shiyan10d8b342013-06-29 10:44:17 +040033#define MAX310X_NAME "max310x"
Alexander Shiyanf6544412012-08-06 19:42:32 +040034#define MAX310X_MAJOR 204
35#define MAX310X_MINOR 209
Alexander Shiyan78adcca2016-06-07 18:59:27 +030036#define MAX310X_UART_NRMAX 16
Alexander Shiyanf6544412012-08-06 19:42:32 +040037
38/* MAX310X register definitions */
39#define MAX310X_RHR_REG (0x00) /* RX FIFO */
40#define MAX310X_THR_REG (0x00) /* TX FIFO */
41#define MAX310X_IRQEN_REG (0x01) /* IRQ enable */
42#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
43#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
44#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040045#define MAX310X_REG_05 (0x05)
46#define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
Alexander Shiyanf6544412012-08-06 19:42:32 +040047#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
48#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
49#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
50#define MAX310X_MODE1_REG (0x09) /* MODE1 */
51#define MAX310X_MODE2_REG (0x0a) /* MODE2 */
52#define MAX310X_LCR_REG (0x0b) /* LCR */
53#define MAX310X_RXTO_REG (0x0c) /* RX timeout */
54#define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */
55#define MAX310X_IRDA_REG (0x0e) /* IRDA settings */
56#define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */
57#define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */
58#define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */
59#define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */
60#define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */
61#define MAX310X_XON1_REG (0x14) /* XON1 character */
62#define MAX310X_XON2_REG (0x15) /* XON2 character */
63#define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */
64#define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */
65#define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */
66#define MAX310X_GPIODATA_REG (0x19) /* GPIO data */
67#define MAX310X_PLLCFG_REG (0x1a) /* PLL config */
68#define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */
69#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
70#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
71#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040072#define MAX310X_REG_1F (0x1f)
73
74#define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
75
76#define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
77#define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
78
79/* Extended registers */
80#define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
Alexander Shiyanf6544412012-08-06 19:42:32 +040081
82/* IRQ register bits */
83#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
84#define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
85#define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */
86#define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
87#define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
88#define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
89#define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
90#define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
91
92/* LSR register bits */
93#define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */
94#define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
95#define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
96#define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */
97#define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */
98#define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
99#define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */
100
101/* Special character register bits */
102#define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
103#define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
104#define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
105#define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
106#define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */
107#define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
108
109/* Status register bits */
110#define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
111#define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
112#define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
113#define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
114#define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
115#define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
116
117/* MODE1 register bits */
118#define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
119#define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
120#define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
121#define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
122#define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
123#define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
124#define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
125#define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
126
127/* MODE2 register bits */
128#define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */
129#define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
130#define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
131#define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
132#define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
133#define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
134#define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
135#define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
136
137/* LCR register bits */
138#define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
139#define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
140 *
141 * Word length bits table:
142 * 00 -> 5 bit words
143 * 01 -> 6 bit words
144 * 10 -> 7 bit words
145 * 11 -> 8 bit words
146 */
147#define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
148 *
149 * STOP length bit table:
150 * 0 -> 1 stop bit
151 * 1 -> 1-1.5 stop bits if
152 * word length is 5,
153 * 2 stop bits otherwise
154 */
155#define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
156#define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
157#define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
158#define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
159#define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */
Alexander Shiyanf6544412012-08-06 19:42:32 +0400160
161/* IRDA register bits */
162#define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
163#define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
Alexander Shiyanf6544412012-08-06 19:42:32 +0400164
165/* Flow control trigger level register masks */
166#define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
167#define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
168#define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f)
169#define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4)
170
171/* FIFO interrupt trigger level register masks */
172#define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f) /* TX FIFO trigger level */
173#define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0) /* RX FIFO trigger level */
174#define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f)
175#define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4)
176
177/* Flow control register bits */
178#define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
179#define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
180#define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
181 * are used in conjunction with
182 * XOFF2 for definition of
183 * special character */
184#define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
185#define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
186#define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
187 *
188 * SWFLOW bits 1 & 0 table:
189 * 00 -> no transmitter flow
190 * control
191 * 01 -> receiver compares
192 * XON2 and XOFF2
193 * and controls
194 * transmitter
195 * 10 -> receiver compares
196 * XON1 and XOFF1
197 * and controls
198 * transmitter
199 * 11 -> receiver compares
200 * XON1, XON2, XOFF1 and
201 * XOFF2 and controls
202 * transmitter
203 */
204#define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
205#define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
206 *
207 * SWFLOW bits 3 & 2 table:
208 * 00 -> no received flow
209 * control
210 * 01 -> transmitter generates
211 * XON2 and XOFF2
212 * 10 -> transmitter generates
213 * XON1 and XOFF1
214 * 11 -> transmitter generates
215 * XON1, XON2, XOFF1 and
216 * XOFF2
217 */
218
Alexander Shiyanf6544412012-08-06 19:42:32 +0400219/* PLL configuration register masks */
220#define MAX310X_PLLCFG_PREDIV_MASK (0x3f) /* PLL predivision value */
221#define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0) /* PLL multiplication factor */
222
223/* Baud rate generator configuration register bits */
224#define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
225#define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
226
227/* Clock source register bits */
228#define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
229#define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
230#define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
231#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
232#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
233
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400234/* Global commands */
235#define MAX310X_EXTREG_ENBL (0xce)
236#define MAX310X_EXTREG_DSBL (0xcd)
237
Alexander Shiyanf6544412012-08-06 19:42:32 +0400238/* Misc definitions */
239#define MAX310X_FIFO_SIZE (128)
Alexander Shiyan11652fc2016-12-05 14:05:19 +0300240#define MAX310x_REV_MASK (0xf8)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400241
242/* MAX3107 specific */
243#define MAX3107_REV_ID (0xa0)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400244
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400245/* MAX3109 specific */
246#define MAX3109_REV_ID (0xc0)
247
Alexander Shiyan003236d2013-06-29 10:44:19 +0400248/* MAX14830 specific */
249#define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
250#define MAX14830_REV_ID (0xb0)
251
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400252struct max310x_devtype {
253 char name[9];
254 int nr;
255 int (*detect)(struct device *);
256 void (*power)(struct uart_port *, int);
257};
Alexander Shiyanf6544412012-08-06 19:42:32 +0400258
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400259struct max310x_one {
260 struct uart_port port;
261 struct work_struct tx_work;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400262 struct work_struct md_work;
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300263 struct work_struct rs_work;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400264};
265
266struct max310x_port {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400267 struct max310x_devtype *devtype;
268 struct regmap *regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400269 struct mutex mutex;
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400270 struct clk *clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400271#ifdef CONFIG_GPIOLIB
272 struct gpio_chip gpio;
273#endif
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400274 struct max310x_one p[0];
Alexander Shiyanf6544412012-08-06 19:42:32 +0400275};
276
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300277static struct uart_driver max310x_uart = {
278 .owner = THIS_MODULE,
279 .driver_name = MAX310X_NAME,
280 .dev_name = "ttyMAX",
281 .major = MAX310X_MAJOR,
282 .minor = MAX310X_MINOR,
Alexander Shiyan78adcca2016-06-07 18:59:27 +0300283 .nr = MAX310X_UART_NRMAX,
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300284};
285
Alexander Shiyan78adcca2016-06-07 18:59:27 +0300286static DECLARE_BITMAP(max310x_lines, MAX310X_UART_NRMAX);
287
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400288static u8 max310x_port_read(struct uart_port *port, u8 reg)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400289{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400290 struct max310x_port *s = dev_get_drvdata(port->dev);
291 unsigned int val = 0;
292
293 regmap_read(s->regmap, port->iobase + reg, &val);
294
295 return val;
296}
297
298static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
299{
300 struct max310x_port *s = dev_get_drvdata(port->dev);
301
302 regmap_write(s->regmap, port->iobase + reg, val);
303}
304
305static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
306{
307 struct max310x_port *s = dev_get_drvdata(port->dev);
308
309 regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
310}
311
312static int max3107_detect(struct device *dev)
313{
314 struct max310x_port *s = dev_get_drvdata(dev);
315 unsigned int val = 0;
316 int ret;
317
318 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
319 if (ret)
320 return ret;
321
322 if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
323 dev_err(dev,
324 "%s ID 0x%02x does not match\n", s->devtype->name, val);
325 return -ENODEV;
326 }
327
328 return 0;
329}
330
331static int max3108_detect(struct device *dev)
332{
333 struct max310x_port *s = dev_get_drvdata(dev);
334 unsigned int val = 0;
335 int ret;
336
337 /* MAX3108 have not REV ID register, we just check default value
338 * from clocksource register to make sure everything works.
339 */
340 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
341 if (ret)
342 return ret;
343
344 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
345 dev_err(dev, "%s not present\n", s->devtype->name);
346 return -ENODEV;
347 }
348
349 return 0;
350}
351
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400352static int max3109_detect(struct device *dev)
353{
354 struct max310x_port *s = dev_get_drvdata(dev);
355 unsigned int val = 0;
356 int ret;
357
Gregory Hermant32304d72014-09-30 08:59:17 +0200358 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
359 MAX310X_EXTREG_ENBL);
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400360 if (ret)
361 return ret;
362
Gregory Hermant32304d72014-09-30 08:59:17 +0200363 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
364 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400365 if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
366 dev_err(dev,
367 "%s ID 0x%02x does not match\n", s->devtype->name, val);
368 return -ENODEV;
369 }
370
371 return 0;
372}
373
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400374static void max310x_power(struct uart_port *port, int on)
375{
376 max310x_port_update(port, MAX310X_MODE1_REG,
377 MAX310X_MODE1_FORCESLEEP_BIT,
378 on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
379 if (on)
380 msleep(50);
381}
382
Alexander Shiyan003236d2013-06-29 10:44:19 +0400383static int max14830_detect(struct device *dev)
384{
385 struct max310x_port *s = dev_get_drvdata(dev);
386 unsigned int val = 0;
387 int ret;
388
389 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
390 MAX310X_EXTREG_ENBL);
391 if (ret)
392 return ret;
393
394 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
395 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
396 if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
397 dev_err(dev,
398 "%s ID 0x%02x does not match\n", s->devtype->name, val);
399 return -ENODEV;
400 }
401
402 return 0;
403}
404
405static void max14830_power(struct uart_port *port, int on)
406{
407 max310x_port_update(port, MAX310X_BRGCFG_REG,
408 MAX14830_BRGCFG_CLKDIS_BIT,
409 on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
410 if (on)
411 msleep(50);
412}
413
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400414static const struct max310x_devtype max3107_devtype = {
415 .name = "MAX3107",
416 .nr = 1,
417 .detect = max3107_detect,
418 .power = max310x_power,
419};
420
421static const struct max310x_devtype max3108_devtype = {
422 .name = "MAX3108",
423 .nr = 1,
424 .detect = max3108_detect,
425 .power = max310x_power,
426};
427
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400428static const struct max310x_devtype max3109_devtype = {
429 .name = "MAX3109",
430 .nr = 2,
431 .detect = max3109_detect,
432 .power = max310x_power,
433};
434
Alexander Shiyan003236d2013-06-29 10:44:19 +0400435static const struct max310x_devtype max14830_devtype = {
436 .name = "MAX14830",
437 .nr = 4,
438 .detect = max14830_detect,
439 .power = max14830_power,
440};
441
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400442static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
443{
444 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400445 case MAX310X_IRQSTS_REG:
446 case MAX310X_LSR_IRQSTS_REG:
447 case MAX310X_SPCHR_IRQSTS_REG:
448 case MAX310X_STS_IRQSTS_REG:
449 case MAX310X_TXFIFOLVL_REG:
450 case MAX310X_RXFIFOLVL_REG:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400451 return false;
452 default:
453 break;
454 }
455
456 return true;
457}
458
459static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
460{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400461 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400462 case MAX310X_RHR_REG:
463 case MAX310X_IRQSTS_REG:
464 case MAX310X_LSR_IRQSTS_REG:
465 case MAX310X_SPCHR_IRQSTS_REG:
466 case MAX310X_STS_IRQSTS_REG:
467 case MAX310X_TXFIFOLVL_REG:
468 case MAX310X_RXFIFOLVL_REG:
469 case MAX310X_GPIODATA_REG:
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400470 case MAX310X_BRGDIVLSB_REG:
471 case MAX310X_REG_05:
472 case MAX310X_REG_1F:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400473 return true;
474 default:
475 break;
476 }
477
478 return false;
479}
480
481static bool max310x_reg_precious(struct device *dev, unsigned int reg)
482{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400483 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400484 case MAX310X_RHR_REG:
485 case MAX310X_IRQSTS_REG:
486 case MAX310X_SPCHR_IRQSTS_REG:
487 case MAX310X_STS_IRQSTS_REG:
488 return true;
489 default:
490 break;
491 }
492
493 return false;
494}
495
Alexander Shiyane97e1552014-02-07 18:16:04 +0400496static int max310x_set_baud(struct uart_port *port, int baud)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400497{
Alexander Shiyane97e1552014-02-07 18:16:04 +0400498 unsigned int mode = 0, clk = port->uartclk, div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400499
Alexander Shiyane97e1552014-02-07 18:16:04 +0400500 /* Check for minimal value for divider */
501 if (div < 16)
502 div = 16;
503
504 if (clk % baud && (div / 16) < 0x8000) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400505 /* Mode x2 */
506 mode = MAX310X_BRGCFG_2XMODE_BIT;
Alexander Shiyane97e1552014-02-07 18:16:04 +0400507 clk = port->uartclk * 2;
508 div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400509
Alexander Shiyane97e1552014-02-07 18:16:04 +0400510 if (clk % baud && (div / 16) < 0x8000) {
511 /* Mode x4 */
512 mode = MAX310X_BRGCFG_4XMODE_BIT;
513 clk = port->uartclk * 4;
514 div = clk / baud;
515 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400516 }
517
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400518 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8);
519 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16);
520 max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode);
Alexander Shiyane97e1552014-02-07 18:16:04 +0400521
522 return DIV_ROUND_CLOSEST(clk, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400523}
524
Bill Pemberton9671f092012-11-19 13:21:50 -0500525static int max310x_update_best_err(unsigned long f, long *besterr)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400526{
527 /* Use baudrate 115200 for calculate error */
528 long err = f % (115200 * 16);
529
530 if ((*besterr < 0) || (*besterr > err)) {
531 *besterr = err;
532 return 0;
533 }
534
535 return 1;
536}
537
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400538static int max310x_set_ref_clk(struct max310x_port *s, unsigned long freq,
539 bool xtal)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400540{
541 unsigned int div, clksrc, pllcfg = 0;
542 long besterr = -1;
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400543 unsigned long fdiv, fmul, bestfreq = freq;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400544
545 /* First, update error without PLL */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400546 max310x_update_best_err(freq, &besterr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400547
548 /* Try all possible PLL dividers */
549 for (div = 1; (div <= 63) && besterr; div++) {
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400550 fdiv = DIV_ROUND_CLOSEST(freq, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400551
552 /* Try multiplier 6 */
553 fmul = fdiv * 6;
554 if ((fdiv >= 500000) && (fdiv <= 800000))
555 if (!max310x_update_best_err(fmul, &besterr)) {
556 pllcfg = (0 << 6) | div;
557 bestfreq = fmul;
558 }
559 /* Try multiplier 48 */
560 fmul = fdiv * 48;
561 if ((fdiv >= 850000) && (fdiv <= 1200000))
562 if (!max310x_update_best_err(fmul, &besterr)) {
563 pllcfg = (1 << 6) | div;
564 bestfreq = fmul;
565 }
566 /* Try multiplier 96 */
567 fmul = fdiv * 96;
568 if ((fdiv >= 425000) && (fdiv <= 1000000))
569 if (!max310x_update_best_err(fmul, &besterr)) {
570 pllcfg = (2 << 6) | div;
571 bestfreq = fmul;
572 }
573 /* Try multiplier 144 */
574 fmul = fdiv * 144;
575 if ((fdiv >= 390000) && (fdiv <= 667000))
576 if (!max310x_update_best_err(fmul, &besterr)) {
577 pllcfg = (3 << 6) | div;
578 bestfreq = fmul;
579 }
580 }
581
582 /* Configure clock source */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400583 clksrc = xtal ? MAX310X_CLKSRC_CRYST_BIT : MAX310X_CLKSRC_EXTCLK_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400584
585 /* Configure PLL */
586 if (pllcfg) {
587 clksrc |= MAX310X_CLKSRC_PLL_BIT;
588 regmap_write(s->regmap, MAX310X_PLLCFG_REG, pllcfg);
589 } else
590 clksrc |= MAX310X_CLKSRC_PLLBYP_BIT;
591
592 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
593
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400594 /* Wait for crystal */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400595 if (pllcfg && xtal)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400596 msleep(10);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400597
598 return (int)bestfreq;
599}
600
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400601static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400602{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400603 unsigned int sts, ch, flag;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400604
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400605 if (unlikely(rxlen >= port->fifosize)) {
Alexander Shiyan78adcca2016-06-07 18:59:27 +0300606 dev_warn_ratelimited(port->dev, "Possible RX FIFO overrun\n");
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400607 port->icount.buf_overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400608 /* Ensure sanity of RX level */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400609 rxlen = port->fifosize;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400610 }
611
Alexander Shiyanf6544412012-08-06 19:42:32 +0400612 while (rxlen--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400613 ch = max310x_port_read(port, MAX310X_RHR_REG);
614 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400615
616 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
617 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
618
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400619 port->icount.rx++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400620 flag = TTY_NORMAL;
621
622 if (unlikely(sts)) {
623 if (sts & MAX310X_LSR_RXBRK_BIT) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400624 port->icount.brk++;
625 if (uart_handle_break(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400626 continue;
627 } else if (sts & MAX310X_LSR_RXPAR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400628 port->icount.parity++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400629 else if (sts & MAX310X_LSR_FRERR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400630 port->icount.frame++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400631 else if (sts & MAX310X_LSR_RXOVR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400632 port->icount.overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400633
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400634 sts &= port->read_status_mask;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400635 if (sts & MAX310X_LSR_RXBRK_BIT)
636 flag = TTY_BREAK;
637 else if (sts & MAX310X_LSR_RXPAR_BIT)
638 flag = TTY_PARITY;
639 else if (sts & MAX310X_LSR_FRERR_BIT)
640 flag = TTY_FRAME;
641 else if (sts & MAX310X_LSR_RXOVR_BIT)
642 flag = TTY_OVERRUN;
643 }
644
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400645 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400646 continue;
647
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400648 if (sts & port->ignore_status_mask)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400649 continue;
650
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400651 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400652 }
653
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400654 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400655}
656
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400657static void max310x_handle_tx(struct uart_port *port)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400658{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400659 struct circ_buf *xmit = &port->state->xmit;
660 unsigned int txlen, to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400661
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400662 if (unlikely(port->x_char)) {
663 max310x_port_write(port, MAX310X_THR_REG, port->x_char);
664 port->icount.tx++;
665 port->x_char = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400666 return;
667 }
668
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400669 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400670 return;
671
672 /* Get length of data pending in circular buffer */
673 to_send = uart_circ_chars_pending(xmit);
674 if (likely(to_send)) {
675 /* Limit to size of TX FIFO */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400676 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
677 txlen = port->fifosize - txlen;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400678 to_send = (to_send > txlen) ? txlen : to_send;
679
Alexander Shiyanf6544412012-08-06 19:42:32 +0400680 /* Add data to send */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400681 port->icount.tx += to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400682 while (to_send--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400683 max310x_port_write(port, MAX310X_THR_REG,
684 xmit->buf[xmit->tail]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400685 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Joe Perchesfc8114722013-10-08 16:14:21 -0700686 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400687 }
688
689 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400690 uart_write_wakeup(port);
691}
692
693static void max310x_port_irq(struct max310x_port *s, int portno)
694{
695 struct uart_port *port = &s->p[portno].port;
696
697 do {
698 unsigned int ists, lsr, rxlen;
699
700 /* Read IRQ status & RX FIFO level */
701 ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
702 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
703 if (!ists && !rxlen)
704 break;
705
706 if (ists & MAX310X_IRQ_CTS_BIT) {
707 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
708 uart_handle_cts_change(port,
709 !!(lsr & MAX310X_LSR_CTS_BIT));
710 }
711 if (rxlen)
712 max310x_handle_rx(port, rxlen);
713 if (ists & MAX310X_IRQ_TXEMPTY_BIT) {
714 mutex_lock(&s->mutex);
715 max310x_handle_tx(port);
716 mutex_unlock(&s->mutex);
717 }
718 } while (1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400719}
720
721static irqreturn_t max310x_ist(int irq, void *dev_id)
722{
723 struct max310x_port *s = (struct max310x_port *)dev_id;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400724
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300725 if (s->devtype->nr > 1) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400726 do {
727 unsigned int val = ~0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400728
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400729 WARN_ON_ONCE(regmap_read(s->regmap,
730 MAX310X_GLOBALIRQ_REG, &val));
Alexander Shiyan6286767a2016-06-07 18:59:24 +0300731 val = ((1 << s->devtype->nr) - 1) & ~val;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400732 if (!val)
733 break;
734 max310x_port_irq(s, fls(val) - 1);
735 } while (1);
736 } else
737 max310x_port_irq(s, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400738
739 return IRQ_HANDLED;
740}
741
742static void max310x_wq_proc(struct work_struct *ws)
743{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400744 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
745 struct max310x_port *s = dev_get_drvdata(one->port.dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400746
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400747 mutex_lock(&s->mutex);
748 max310x_handle_tx(&one->port);
749 mutex_unlock(&s->mutex);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400750}
751
752static void max310x_start_tx(struct uart_port *port)
753{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400754 struct max310x_one *one = container_of(port, struct max310x_one, port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400755
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400756 if (!work_pending(&one->tx_work))
757 schedule_work(&one->tx_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400758}
759
760static unsigned int max310x_tx_empty(struct uart_port *port)
761{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400762 unsigned int lvl, sts;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400763
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400764 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
765 sts = max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400766
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400767 return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400768}
769
770static unsigned int max310x_get_mctrl(struct uart_port *port)
771{
772 /* DCD and DSR are not wired and CTS/RTS is handled automatically
773 * so just indicate DSR and CAR asserted
774 */
775 return TIOCM_DSR | TIOCM_CAR;
776}
777
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400778static void max310x_md_proc(struct work_struct *ws)
779{
780 struct max310x_one *one = container_of(ws, struct max310x_one, md_work);
781
782 max310x_port_update(&one->port, MAX310X_MODE2_REG,
783 MAX310X_MODE2_LOOPBACK_BIT,
784 (one->port.mctrl & TIOCM_LOOP) ?
785 MAX310X_MODE2_LOOPBACK_BIT : 0);
786}
787
Alexander Shiyanf6544412012-08-06 19:42:32 +0400788static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
789{
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400790 struct max310x_one *one = container_of(port, struct max310x_one, port);
791
792 schedule_work(&one->md_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400793}
794
795static void max310x_break_ctl(struct uart_port *port, int break_state)
796{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400797 max310x_port_update(port, MAX310X_LCR_REG,
798 MAX310X_LCR_TXBREAK_BIT,
799 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400800}
801
802static void max310x_set_termios(struct uart_port *port,
803 struct ktermios *termios,
804 struct ktermios *old)
805{
Alexander Shiyane940e812016-06-07 18:59:25 +0300806 unsigned int lcr = 0, flow = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400807 int baud;
808
Alexander Shiyanf6544412012-08-06 19:42:32 +0400809 /* Mask termios capabilities we don't support */
810 termios->c_cflag &= ~CMSPAR;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400811
812 /* Word size */
813 switch (termios->c_cflag & CSIZE) {
814 case CS5:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400815 break;
816 case CS6:
Alexander Shiyane940e812016-06-07 18:59:25 +0300817 lcr = MAX310X_LCR_LENGTH0_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400818 break;
819 case CS7:
Alexander Shiyane940e812016-06-07 18:59:25 +0300820 lcr = MAX310X_LCR_LENGTH1_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400821 break;
822 case CS8:
823 default:
Alexander Shiyane940e812016-06-07 18:59:25 +0300824 lcr = MAX310X_LCR_LENGTH1_BIT | MAX310X_LCR_LENGTH0_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400825 break;
826 }
827
828 /* Parity */
829 if (termios->c_cflag & PARENB) {
830 lcr |= MAX310X_LCR_PARITY_BIT;
831 if (!(termios->c_cflag & PARODD))
832 lcr |= MAX310X_LCR_EVENPARITY_BIT;
833 }
834
835 /* Stop bits */
836 if (termios->c_cflag & CSTOPB)
837 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
838
839 /* Update LCR register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400840 max310x_port_write(port, MAX310X_LCR_REG, lcr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400841
842 /* Set read status mask */
843 port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
844 if (termios->c_iflag & INPCK)
845 port->read_status_mask |= MAX310X_LSR_RXPAR_BIT |
846 MAX310X_LSR_FRERR_BIT;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400847 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400848 port->read_status_mask |= MAX310X_LSR_RXBRK_BIT;
849
850 /* Set status ignore mask */
851 port->ignore_status_mask = 0;
852 if (termios->c_iflag & IGNBRK)
853 port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT;
854 if (!(termios->c_cflag & CREAD))
855 port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT |
856 MAX310X_LSR_RXOVR_BIT |
857 MAX310X_LSR_FRERR_BIT |
858 MAX310X_LSR_RXBRK_BIT;
859
860 /* Configure flow control */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400861 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
862 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400863 if (termios->c_cflag & CRTSCTS)
864 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
865 MAX310X_FLOWCTRL_AUTORTS_BIT;
866 if (termios->c_iflag & IXON)
867 flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT |
868 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
869 if (termios->c_iflag & IXOFF)
870 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
871 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400872 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400873
874 /* Get baud rate generator configuration */
875 baud = uart_get_baud_rate(port, termios, old,
876 port->uartclk / 16 / 0xffff,
877 port->uartclk / 4);
878
879 /* Setup baudrate generator */
Alexander Shiyane97e1552014-02-07 18:16:04 +0400880 baud = max310x_set_baud(port, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400881
882 /* Update timeout according to new baud rate */
883 uart_update_timeout(port, termios->c_cflag, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400884}
885
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300886static void max310x_rs_proc(struct work_struct *ws)
Alexander Shiyan55367c62014-02-10 22:18:34 +0400887{
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300888 struct max310x_one *one = container_of(ws, struct max310x_one, rs_work);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400889 unsigned int val;
890
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300891 val = (one->port.rs485.delay_rts_before_send << 4) |
892 one->port.rs485.delay_rts_after_send;
893 max310x_port_write(&one->port, MAX310X_HDPIXDELAY_REG, val);
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100894
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300895 if (one->port.rs485.flags & SER_RS485_ENABLED) {
896 max310x_port_update(&one->port, MAX310X_MODE1_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100897 MAX310X_MODE1_TRNSCVCTRL_BIT,
898 MAX310X_MODE1_TRNSCVCTRL_BIT);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300899 max310x_port_update(&one->port, MAX310X_MODE2_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100900 MAX310X_MODE2_ECHOSUPR_BIT,
901 MAX310X_MODE2_ECHOSUPR_BIT);
902 } else {
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300903 max310x_port_update(&one->port, MAX310X_MODE1_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100904 MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300905 max310x_port_update(&one->port, MAX310X_MODE2_REG,
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100906 MAX310X_MODE2_ECHOSUPR_BIT, 0);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400907 }
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300908}
909
910static int max310x_rs485_config(struct uart_port *port,
911 struct serial_rs485 *rs485)
912{
913 struct max310x_one *one = container_of(port, struct max310x_one, port);
914
915 if ((rs485->delay_rts_before_send > 0x0f) ||
916 (rs485->delay_rts_after_send > 0x0f))
917 return -ERANGE;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400918
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100919 rs485->flags &= SER_RS485_RTS_ON_SEND | SER_RS485_ENABLED;
920 memset(rs485->padding, 0, sizeof(rs485->padding));
921 port->rs485 = *rs485;
922
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +0300923 schedule_work(&one->rs_work);
924
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +0100925 return 0;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400926}
927
Alexander Shiyanf6544412012-08-06 19:42:32 +0400928static int max310x_startup(struct uart_port *port)
929{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400930 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400931 unsigned int val;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400932
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400933 s->devtype->power(port, 1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400934
Alexander Shiyanf6544412012-08-06 19:42:32 +0400935 /* Configure MODE1 register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400936 max310x_port_update(port, MAX310X_MODE1_REG,
Alexander Shiyan55367c62014-02-10 22:18:34 +0400937 MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400938
Alexander Shiyan55367c62014-02-10 22:18:34 +0400939 /* Configure MODE2 register & Reset FIFOs*/
940 val = MAX310X_MODE2_RXEMPTINV_BIT | MAX310X_MODE2_FIFORST_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400941 max310x_port_write(port, MAX310X_MODE2_REG, val);
942 max310x_port_update(port, MAX310X_MODE2_REG,
943 MAX310X_MODE2_FIFORST_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400944
945 /* Configure flow control levels */
946 /* Flow control halt level 96, resume level 48 */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400947 max310x_port_write(port, MAX310X_FLOWLVL_REG,
948 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
Alexander Shiyanf6544412012-08-06 19:42:32 +0400949
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400950 /* Clear IRQ status register */
951 max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400952
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400953 /* Enable RX, TX, CTS change interrupts */
954 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
955 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400956
957 return 0;
958}
959
960static void max310x_shutdown(struct uart_port *port)
961{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400962 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400963
964 /* Disable all interrupts */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400965 max310x_port_write(port, MAX310X_IRQEN_REG, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400966
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400967 s->devtype->power(port, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400968}
969
970static const char *max310x_type(struct uart_port *port)
971{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400972 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400973
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400974 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400975}
976
977static int max310x_request_port(struct uart_port *port)
978{
979 /* Do nothing */
980 return 0;
981}
982
Alexander Shiyanf6544412012-08-06 19:42:32 +0400983static void max310x_config_port(struct uart_port *port, int flags)
984{
985 if (flags & UART_CONFIG_TYPE)
986 port->type = PORT_MAX310X;
987}
988
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400989static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400990{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400991 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
992 return -EINVAL;
993 if (s->irq != port->irq)
994 return -EINVAL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400995
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400996 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400997}
998
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400999static void max310x_null_void(struct uart_port *port)
1000{
1001 /* Do nothing */
1002}
1003
1004static const struct uart_ops max310x_ops = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001005 .tx_empty = max310x_tx_empty,
1006 .set_mctrl = max310x_set_mctrl,
1007 .get_mctrl = max310x_get_mctrl,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001008 .stop_tx = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001009 .start_tx = max310x_start_tx,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001010 .stop_rx = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001011 .break_ctl = max310x_break_ctl,
1012 .startup = max310x_startup,
1013 .shutdown = max310x_shutdown,
1014 .set_termios = max310x_set_termios,
1015 .type = max310x_type,
1016 .request_port = max310x_request_port,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001017 .release_port = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001018 .config_port = max310x_config_port,
1019 .verify_port = max310x_verify_port,
1020};
1021
Alexander Shiyanc2978292013-07-29 19:27:32 +04001022static int __maybe_unused max310x_suspend(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001023{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001024 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001025 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001026
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001027 for (i = 0; i < s->devtype->nr; i++) {
1028 uart_suspend_port(&max310x_uart, &s->p[i].port);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001029 s->devtype->power(&s->p[i].port, 0);
1030 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001031
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001032 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001033}
1034
Alexander Shiyanc2978292013-07-29 19:27:32 +04001035static int __maybe_unused max310x_resume(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001036{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001037 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001038 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001039
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001040 for (i = 0; i < s->devtype->nr; i++) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001041 s->devtype->power(&s->p[i].port, 1);
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001042 uart_resume_port(&max310x_uart, &s->p[i].port);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001043 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001044
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001045 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001046}
1047
Alexander Shiyan27027a72014-02-10 22:18:30 +04001048static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
1049
Alexander Shiyanf6544412012-08-06 19:42:32 +04001050#ifdef CONFIG_GPIOLIB
1051static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
1052{
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001053 unsigned int val;
Linus Walleija00d60a2015-12-08 23:11:05 +01001054 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001055 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001056
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001057 val = max310x_port_read(port, MAX310X_GPIODATA_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001058
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001059 return !!((val >> 4) & (1 << (offset % 4)));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001060}
1061
1062static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1063{
Linus Walleija00d60a2015-12-08 23:11:05 +01001064 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001065 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001066
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001067 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1068 value ? 1 << (offset % 4) : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001069}
1070
1071static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1072{
Linus Walleija00d60a2015-12-08 23:11:05 +01001073 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001074 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001075
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001076 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001077
1078 return 0;
1079}
1080
1081static int max310x_gpio_direction_output(struct gpio_chip *chip,
1082 unsigned offset, int value)
1083{
Linus Walleija00d60a2015-12-08 23:11:05 +01001084 struct max310x_port *s = gpiochip_get_data(chip);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001085 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001086
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001087 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1088 value ? 1 << (offset % 4) : 0);
1089 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
1090 1 << (offset % 4));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001091
1092 return 0;
1093}
1094#endif
1095
Alexander Shiyan27027a72014-02-10 22:18:30 +04001096static int max310x_probe(struct device *dev, struct max310x_devtype *devtype,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001097 struct regmap *regmap, int irq, unsigned long flags)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001098{
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001099 int i, ret, fmin, fmax, freq, uartclk;
1100 struct clk *clk_osc, *clk_xtal;
1101 struct max310x_port *s;
1102 bool xtal = false;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001103
Alexander Shiyan27027a72014-02-10 22:18:30 +04001104 if (IS_ERR(regmap))
1105 return PTR_ERR(regmap);
1106
Alexander Shiyanf6544412012-08-06 19:42:32 +04001107 /* Alloc port structure */
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001108 s = devm_kzalloc(dev, sizeof(*s) +
1109 sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001110 if (!s) {
1111 dev_err(dev, "Error allocating port structure\n");
1112 return -ENOMEM;
1113 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001114
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001115 clk_osc = devm_clk_get(dev, "osc");
1116 clk_xtal = devm_clk_get(dev, "xtal");
1117 if (!IS_ERR(clk_osc)) {
1118 s->clk = clk_osc;
1119 fmin = 500000;
1120 fmax = 35000000;
1121 } else if (!IS_ERR(clk_xtal)) {
1122 s->clk = clk_xtal;
1123 fmin = 1000000;
1124 fmax = 4000000;
1125 xtal = true;
1126 } else if (PTR_ERR(clk_osc) == -EPROBE_DEFER ||
1127 PTR_ERR(clk_xtal) == -EPROBE_DEFER) {
1128 return -EPROBE_DEFER;
1129 } else {
1130 dev_err(dev, "Cannot get clock\n");
1131 return -EINVAL;
1132 }
1133
1134 ret = clk_prepare_enable(s->clk);
1135 if (ret)
1136 return ret;
1137
1138 freq = clk_get_rate(s->clk);
1139 /* Check frequency limits */
1140 if (freq < fmin || freq > fmax) {
1141 ret = -ERANGE;
1142 goto out_clk;
1143 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001144
Alexander Shiyan27027a72014-02-10 22:18:30 +04001145 s->regmap = regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001146 s->devtype = devtype;
1147 dev_set_drvdata(dev, s);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001148
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001149 /* Check device to ensure we are talking to what we expect */
1150 ret = devtype->detect(dev);
1151 if (ret)
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001152 goto out_clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001153
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001154 for (i = 0; i < devtype->nr; i++) {
1155 unsigned int offs = i << 5;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001156
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001157 /* Reset port */
1158 regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
1159 MAX310X_MODE2_RST_BIT);
1160 /* Clear port reset */
1161 regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001162
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001163 /* Wait for port startup */
1164 do {
1165 regmap_read(s->regmap,
1166 MAX310X_BRGDIVLSB_REG + offs, &ret);
1167 } while (ret != 0x01);
1168
1169 regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs,
1170 MAX310X_MODE1_AUTOSLEEP_BIT,
1171 MAX310X_MODE1_AUTOSLEEP_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001172 }
1173
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001174 uartclk = max310x_set_ref_clk(s, freq, xtal);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001175 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
1176
Alexander Shiyandba29a22014-02-10 22:18:32 +04001177#ifdef CONFIG_GPIOLIB
1178 /* Setup GPIO cotroller */
1179 s->gpio.owner = THIS_MODULE;
Linus Walleij58383c782015-11-04 09:56:26 +01001180 s->gpio.parent = dev;
Alexander Shiyandba29a22014-02-10 22:18:32 +04001181 s->gpio.label = dev_name(dev);
1182 s->gpio.direction_input = max310x_gpio_direction_input;
1183 s->gpio.get = max310x_gpio_get;
1184 s->gpio.direction_output= max310x_gpio_direction_output;
1185 s->gpio.set = max310x_gpio_set;
1186 s->gpio.base = -1;
1187 s->gpio.ngpio = devtype->nr * 4;
1188 s->gpio.can_sleep = 1;
Alexander Shiyan0e8cc7c2016-06-07 18:59:23 +03001189 ret = devm_gpiochip_add_data(dev, &s->gpio, s);
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001190 if (ret)
Alexander Shiyan0e8cc7c2016-06-07 18:59:23 +03001191 goto out_clk;
Alexander Shiyandba29a22014-02-10 22:18:32 +04001192#endif
1193
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001194 mutex_init(&s->mutex);
1195
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001196 for (i = 0; i < devtype->nr; i++) {
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001197 unsigned int line;
1198
1199 line = find_first_zero_bit(max310x_lines, MAX310X_UART_NRMAX);
1200 if (line == MAX310X_UART_NRMAX) {
1201 ret = -ERANGE;
1202 goto out_uart;
1203 }
1204
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001205 /* Initialize port data */
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001206 s->p[i].port.line = line;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001207 s->p[i].port.dev = dev;
1208 s->p[i].port.irq = irq;
1209 s->p[i].port.type = PORT_MAX310X;
1210 s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001211 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001212 s->p[i].port.iotype = UPIO_PORT;
1213 s->p[i].port.iobase = i * 0x20;
1214 s->p[i].port.membase = (void __iomem *)~0;
1215 s->p[i].port.uartclk = uartclk;
Ricardo Ribalda Delgadoc267d672014-11-06 09:22:58 +01001216 s->p[i].port.rs485_config = max310x_rs485_config;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001217 s->p[i].port.ops = &max310x_ops;
1218 /* Disable all interrupts */
1219 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
1220 /* Clear IRQ status register */
1221 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
1222 /* Enable IRQ pin */
1223 max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG,
1224 MAX310X_MODE1_IRQSEL_BIT,
1225 MAX310X_MODE1_IRQSEL_BIT);
1226 /* Initialize queue for start TX */
1227 INIT_WORK(&s->p[i].tx_work, max310x_wq_proc);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +03001228 /* Initialize queue for changing LOOPBACK mode */
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001229 INIT_WORK(&s->p[i].md_work, max310x_md_proc);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +03001230 /* Initialize queue for changing RS485 mode */
1231 INIT_WORK(&s->p[i].rs_work, max310x_rs_proc);
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001232
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001233 /* Register port */
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001234 ret = uart_add_one_port(&max310x_uart, &s->p[i].port);
1235 if (ret) {
1236 s->p[i].port.dev = NULL;
1237 goto out_uart;
1238 }
1239 set_bit(line, max310x_lines);
1240
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001241 /* Go to suspend mode */
1242 devtype->power(&s->p[i].port, 0);
1243 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001244
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001245 /* Setup interrupt */
1246 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001247 IRQF_ONESHOT | flags, dev_name(dev), s);
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001248 if (!ret)
1249 return 0;
1250
1251 dev_err(dev, "Unable to reguest IRQ %i\n", irq);
Alexander Shiyandba29a22014-02-10 22:18:32 +04001252
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001253out_uart:
1254 for (i = 0; i < devtype->nr; i++) {
1255 if (s->p[i].port.dev) {
1256 uart_remove_one_port(&max310x_uart, &s->p[i].port);
1257 clear_bit(s->p[i].port.line, max310x_lines);
1258 }
1259 }
Alexander Shiyanc8246fe2016-06-07 18:59:26 +03001260
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001261 mutex_destroy(&s->mutex);
1262
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001263out_clk:
1264 clk_disable_unprepare(s->clk);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001265
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001266 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001267}
1268
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001269static int max310x_remove(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001270{
Alexander Shiyanf6544412012-08-06 19:42:32 +04001271 struct max310x_port *s = dev_get_drvdata(dev);
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001272 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001273
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001274 for (i = 0; i < s->devtype->nr; i++) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001275 cancel_work_sync(&s->p[i].tx_work);
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001276 cancel_work_sync(&s->p[i].md_work);
Alexander Shiyan5bdb48b2016-06-07 18:59:21 +03001277 cancel_work_sync(&s->p[i].rs_work);
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001278 uart_remove_one_port(&max310x_uart, &s->p[i].port);
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001279 clear_bit(s->p[i].port.line, max310x_lines);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001280 s->devtype->power(&s->p[i].port, 0);
1281 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001282
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001283 mutex_destroy(&s->mutex);
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001284 clk_disable_unprepare(s->clk);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001285
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001286 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001287}
1288
Alexander Shiyan58afc902014-02-10 22:18:36 +04001289static const struct of_device_id __maybe_unused max310x_dt_ids[] = {
1290 { .compatible = "maxim,max3107", .data = &max3107_devtype, },
1291 { .compatible = "maxim,max3108", .data = &max3108_devtype, },
1292 { .compatible = "maxim,max3109", .data = &max3109_devtype, },
1293 { .compatible = "maxim,max14830", .data = &max14830_devtype },
1294 { }
1295};
1296MODULE_DEVICE_TABLE(of, max310x_dt_ids);
1297
Alexander Shiyan27027a72014-02-10 22:18:30 +04001298static struct regmap_config regcfg = {
1299 .reg_bits = 8,
1300 .val_bits = 8,
1301 .write_flag_mask = 0x80,
1302 .cache_type = REGCACHE_RBTREE,
1303 .writeable_reg = max310x_reg_writeable,
1304 .volatile_reg = max310x_reg_volatile,
1305 .precious_reg = max310x_reg_precious,
1306};
1307
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001308#ifdef CONFIG_SPI_MASTER
1309static int max310x_spi_probe(struct spi_device *spi)
1310{
Alexander Shiyan58afc902014-02-10 22:18:36 +04001311 struct max310x_devtype *devtype;
1312 unsigned long flags = 0;
Alexander Shiyan27027a72014-02-10 22:18:30 +04001313 struct regmap *regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001314 int ret;
1315
1316 /* Setup SPI bus */
1317 spi->bits_per_word = 8;
1318 spi->mode = spi->mode ? : SPI_MODE_0;
1319 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
1320 ret = spi_setup(spi);
Alexander Shiyan27027a72014-02-10 22:18:30 +04001321 if (ret)
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001322 return ret;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001323
Alexander Shiyan58afc902014-02-10 22:18:36 +04001324 if (spi->dev.of_node) {
1325 const struct of_device_id *of_id =
1326 of_match_device(max310x_dt_ids, &spi->dev);
1327
1328 devtype = (struct max310x_devtype *)of_id->data;
1329 } else {
1330 const struct spi_device_id *id_entry = spi_get_device_id(spi);
1331
1332 devtype = (struct max310x_devtype *)id_entry->driver_data;
Alexander Shiyan58afc902014-02-10 22:18:36 +04001333 }
1334
Liu Xiangc164b002016-09-07 22:05:01 +08001335 flags = IRQF_TRIGGER_FALLING;
Alexander Shiyan27027a72014-02-10 22:18:30 +04001336 regcfg.max_register = devtype->nr * 0x20 - 1;
1337 regmap = devm_regmap_init_spi(spi, &regcfg);
1338
Alexander Shiyan58afc902014-02-10 22:18:36 +04001339 return max310x_probe(&spi->dev, devtype, regmap, spi->irq, flags);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001340}
1341
1342static int max310x_spi_remove(struct spi_device *spi)
1343{
1344 return max310x_remove(&spi->dev);
1345}
1346
Alexander Shiyanf6544412012-08-06 19:42:32 +04001347static const struct spi_device_id max310x_id_table[] = {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001348 { "max3107", (kernel_ulong_t)&max3107_devtype, },
1349 { "max3108", (kernel_ulong_t)&max3108_devtype, },
Alexander Shiyan21fc5092013-06-29 10:44:18 +04001350 { "max3109", (kernel_ulong_t)&max3109_devtype, },
Alexander Shiyan003236d2013-06-29 10:44:19 +04001351 { "max14830", (kernel_ulong_t)&max14830_devtype, },
Axel Lin1838b8c2012-11-04 23:34:18 +08001352 { }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001353};
1354MODULE_DEVICE_TABLE(spi, max310x_id_table);
1355
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001356static struct spi_driver max310x_spi_driver = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001357 .driver = {
Alexander Shiyan58afc902014-02-10 22:18:36 +04001358 .name = MAX310X_NAME,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001359 .of_match_table = of_match_ptr(max310x_dt_ids),
1360 .pm = &max310x_pm_ops,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001361 },
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001362 .probe = max310x_spi_probe,
1363 .remove = max310x_spi_remove,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001364 .id_table = max310x_id_table,
1365};
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001366#endif
Alexander Shiyanf6544412012-08-06 19:42:32 +04001367
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001368static int __init max310x_uart_init(void)
1369{
1370 int ret;
1371
Alexander Shiyan78adcca2016-06-07 18:59:27 +03001372 bitmap_zero(max310x_lines, MAX310X_UART_NRMAX);
1373
Alexander Shiyan6286767a2016-06-07 18:59:24 +03001374 ret = uart_register_driver(&max310x_uart);
1375 if (ret)
1376 return ret;
1377
1378#ifdef CONFIG_SPI_MASTER
1379 spi_register_driver(&max310x_spi_driver);
1380#endif
1381
1382 return 0;
1383}
1384module_init(max310x_uart_init);
1385
1386static void __exit max310x_uart_exit(void)
1387{
1388#ifdef CONFIG_SPI_MASTER
1389 spi_unregister_driver(&max310x_spi_driver);
1390#endif
1391
1392 uart_unregister_driver(&max310x_uart);
1393}
1394module_exit(max310x_uart_exit);
1395
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001396MODULE_LICENSE("GPL");
Alexander Shiyanf6544412012-08-06 19:42:32 +04001397MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1398MODULE_DESCRIPTION("MAX310X serial driver");