blob: 3c93814b1648f6839deb3af050ed98d3cdce0a9e [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 Shiyane97e1552014-02-07 18:16:04 +04004 * Copyright (C) 2012-2014 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 Shiyanf6544412012-08-06 19:42:32 +040016#include <linux/module.h>
Alexander Shiyan10d8b342013-06-29 10:44:17 +040017#include <linux/delay.h>
Alexander Shiyanf6544412012-08-06 19:42:32 +040018#include <linux/device.h>
Alexander Shiyan10d8b342013-06-29 10:44:17 +040019#include <linux/bitops.h>
Alexander Shiyanf6544412012-08-06 19:42:32 +040020#include <linux/serial_core.h>
21#include <linux/serial.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/regmap.h>
25#include <linux/gpio.h>
26#include <linux/spi/spi.h>
Alexander Shiyan10d8b342013-06-29 10:44:17 +040027
Alexander Shiyanf6544412012-08-06 19:42:32 +040028#include <linux/platform_data/max310x.h>
29
Alexander Shiyan10d8b342013-06-29 10:44:17 +040030#define MAX310X_NAME "max310x"
Alexander Shiyanf6544412012-08-06 19:42:32 +040031#define MAX310X_MAJOR 204
32#define MAX310X_MINOR 209
33
34/* MAX310X register definitions */
35#define MAX310X_RHR_REG (0x00) /* RX FIFO */
36#define MAX310X_THR_REG (0x00) /* TX FIFO */
37#define MAX310X_IRQEN_REG (0x01) /* IRQ enable */
38#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
39#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
40#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040041#define MAX310X_REG_05 (0x05)
42#define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
Alexander Shiyanf6544412012-08-06 19:42:32 +040043#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
44#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
45#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
46#define MAX310X_MODE1_REG (0x09) /* MODE1 */
47#define MAX310X_MODE2_REG (0x0a) /* MODE2 */
48#define MAX310X_LCR_REG (0x0b) /* LCR */
49#define MAX310X_RXTO_REG (0x0c) /* RX timeout */
50#define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */
51#define MAX310X_IRDA_REG (0x0e) /* IRDA settings */
52#define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */
53#define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */
54#define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */
55#define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */
56#define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */
57#define MAX310X_XON1_REG (0x14) /* XON1 character */
58#define MAX310X_XON2_REG (0x15) /* XON2 character */
59#define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */
60#define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */
61#define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */
62#define MAX310X_GPIODATA_REG (0x19) /* GPIO data */
63#define MAX310X_PLLCFG_REG (0x1a) /* PLL config */
64#define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */
65#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
66#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
67#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040068#define MAX310X_REG_1F (0x1f)
69
70#define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
71
72#define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
73#define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
74
75/* Extended registers */
76#define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
Alexander Shiyanf6544412012-08-06 19:42:32 +040077
78/* IRQ register bits */
79#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
80#define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
81#define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */
82#define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
83#define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
84#define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
85#define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
86#define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
87
88/* LSR register bits */
89#define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */
90#define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
91#define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
92#define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */
93#define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */
94#define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
95#define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */
96
97/* Special character register bits */
98#define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
99#define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
100#define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
101#define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
102#define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */
103#define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
104
105/* Status register bits */
106#define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
107#define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
108#define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
109#define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
110#define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
111#define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
112
113/* MODE1 register bits */
114#define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
115#define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
116#define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
117#define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
118#define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
119#define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
120#define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
121#define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
122
123/* MODE2 register bits */
124#define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */
125#define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
126#define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
127#define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
128#define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
129#define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
130#define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
131#define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
132
133/* LCR register bits */
134#define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
135#define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
136 *
137 * Word length bits table:
138 * 00 -> 5 bit words
139 * 01 -> 6 bit words
140 * 10 -> 7 bit words
141 * 11 -> 8 bit words
142 */
143#define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
144 *
145 * STOP length bit table:
146 * 0 -> 1 stop bit
147 * 1 -> 1-1.5 stop bits if
148 * word length is 5,
149 * 2 stop bits otherwise
150 */
151#define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
152#define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
153#define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
154#define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
155#define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */
156#define MAX310X_LCR_WORD_LEN_5 (0x00)
157#define MAX310X_LCR_WORD_LEN_6 (0x01)
158#define MAX310X_LCR_WORD_LEN_7 (0x02)
159#define MAX310X_LCR_WORD_LEN_8 (0x03)
160
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 */
164#define MAX310X_IRDA_SHORTIR_BIT (1 << 2) /* Short SIR mode enable */
165#define MAX310X_IRDA_MIR_BIT (1 << 3) /* MIR mode enable */
166#define MAX310X_IRDA_RXINV_BIT (1 << 4) /* RX logic inversion enable */
167#define MAX310X_IRDA_TXINV_BIT (1 << 5) /* TX logic inversion enable */
168
169/* Flow control trigger level register masks */
170#define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
171#define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
172#define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f)
173#define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4)
174
175/* FIFO interrupt trigger level register masks */
176#define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f) /* TX FIFO trigger level */
177#define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0) /* RX FIFO trigger level */
178#define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f)
179#define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4)
180
181/* Flow control register bits */
182#define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
183#define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
184#define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
185 * are used in conjunction with
186 * XOFF2 for definition of
187 * special character */
188#define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
189#define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
190#define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
191 *
192 * SWFLOW bits 1 & 0 table:
193 * 00 -> no transmitter flow
194 * control
195 * 01 -> receiver compares
196 * XON2 and XOFF2
197 * and controls
198 * transmitter
199 * 10 -> receiver compares
200 * XON1 and XOFF1
201 * and controls
202 * transmitter
203 * 11 -> receiver compares
204 * XON1, XON2, XOFF1 and
205 * XOFF2 and controls
206 * transmitter
207 */
208#define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
209#define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
210 *
211 * SWFLOW bits 3 & 2 table:
212 * 00 -> no received flow
213 * control
214 * 01 -> transmitter generates
215 * XON2 and XOFF2
216 * 10 -> transmitter generates
217 * XON1 and XOFF1
218 * 11 -> transmitter generates
219 * XON1, XON2, XOFF1 and
220 * XOFF2
221 */
222
223/* GPIO configuration register bits */
224#define MAX310X_GPIOCFG_GP0OUT_BIT (1 << 0) /* GPIO 0 output enable */
225#define MAX310X_GPIOCFG_GP1OUT_BIT (1 << 1) /* GPIO 1 output enable */
226#define MAX310X_GPIOCFG_GP2OUT_BIT (1 << 2) /* GPIO 2 output enable */
227#define MAX310X_GPIOCFG_GP3OUT_BIT (1 << 3) /* GPIO 3 output enable */
228#define MAX310X_GPIOCFG_GP0OD_BIT (1 << 4) /* GPIO 0 open-drain enable */
229#define MAX310X_GPIOCFG_GP1OD_BIT (1 << 5) /* GPIO 1 open-drain enable */
230#define MAX310X_GPIOCFG_GP2OD_BIT (1 << 6) /* GPIO 2 open-drain enable */
231#define MAX310X_GPIOCFG_GP3OD_BIT (1 << 7) /* GPIO 3 open-drain enable */
232
233/* GPIO DATA register bits */
234#define MAX310X_GPIODATA_GP0OUT_BIT (1 << 0) /* GPIO 0 output value */
235#define MAX310X_GPIODATA_GP1OUT_BIT (1 << 1) /* GPIO 1 output value */
236#define MAX310X_GPIODATA_GP2OUT_BIT (1 << 2) /* GPIO 2 output value */
237#define MAX310X_GPIODATA_GP3OUT_BIT (1 << 3) /* GPIO 3 output value */
238#define MAX310X_GPIODATA_GP0IN_BIT (1 << 4) /* GPIO 0 input value */
239#define MAX310X_GPIODATA_GP1IN_BIT (1 << 5) /* GPIO 1 input value */
240#define MAX310X_GPIODATA_GP2IN_BIT (1 << 6) /* GPIO 2 input value */
241#define MAX310X_GPIODATA_GP3IN_BIT (1 << 7) /* GPIO 3 input value */
242
243/* PLL configuration register masks */
244#define MAX310X_PLLCFG_PREDIV_MASK (0x3f) /* PLL predivision value */
245#define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0) /* PLL multiplication factor */
246
247/* Baud rate generator configuration register bits */
248#define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
249#define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
250
251/* Clock source register bits */
252#define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
253#define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
254#define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
255#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
256#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
257
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400258/* Global commands */
259#define MAX310X_EXTREG_ENBL (0xce)
260#define MAX310X_EXTREG_DSBL (0xcd)
261
Alexander Shiyanf6544412012-08-06 19:42:32 +0400262/* Misc definitions */
263#define MAX310X_FIFO_SIZE (128)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400264#define MAX310x_REV_MASK (0xfc)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400265
266/* MAX3107 specific */
267#define MAX3107_REV_ID (0xa0)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400268
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400269/* MAX3109 specific */
270#define MAX3109_REV_ID (0xc0)
271
Alexander Shiyan003236d2013-06-29 10:44:19 +0400272/* MAX14830 specific */
273#define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
274#define MAX14830_REV_ID (0xb0)
275
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400276struct max310x_devtype {
277 char name[9];
278 int nr;
279 int (*detect)(struct device *);
280 void (*power)(struct uart_port *, int);
281};
Alexander Shiyanf6544412012-08-06 19:42:32 +0400282
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400283struct max310x_one {
284 struct uart_port port;
285 struct work_struct tx_work;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400286 struct work_struct md_work;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400287};
288
289struct max310x_port {
290 struct uart_driver uart;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400291 struct max310x_devtype *devtype;
292 struct regmap *regmap;
293 struct regmap_config regcfg;
294 struct mutex mutex;
295 struct max310x_pdata *pdata;
296 int gpio_used;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400297#ifdef CONFIG_GPIOLIB
298 struct gpio_chip gpio;
299#endif
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400300 struct max310x_one p[0];
Alexander Shiyanf6544412012-08-06 19:42:32 +0400301};
302
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400303static u8 max310x_port_read(struct uart_port *port, u8 reg)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400304{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400305 struct max310x_port *s = dev_get_drvdata(port->dev);
306 unsigned int val = 0;
307
308 regmap_read(s->regmap, port->iobase + reg, &val);
309
310 return val;
311}
312
313static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
314{
315 struct max310x_port *s = dev_get_drvdata(port->dev);
316
317 regmap_write(s->regmap, port->iobase + reg, val);
318}
319
320static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
321{
322 struct max310x_port *s = dev_get_drvdata(port->dev);
323
324 regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
325}
326
327static int max3107_detect(struct device *dev)
328{
329 struct max310x_port *s = dev_get_drvdata(dev);
330 unsigned int val = 0;
331 int ret;
332
333 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
334 if (ret)
335 return ret;
336
337 if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
338 dev_err(dev,
339 "%s ID 0x%02x does not match\n", s->devtype->name, val);
340 return -ENODEV;
341 }
342
343 return 0;
344}
345
346static int max3108_detect(struct device *dev)
347{
348 struct max310x_port *s = dev_get_drvdata(dev);
349 unsigned int val = 0;
350 int ret;
351
352 /* MAX3108 have not REV ID register, we just check default value
353 * from clocksource register to make sure everything works.
354 */
355 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
356 if (ret)
357 return ret;
358
359 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
360 dev_err(dev, "%s not present\n", s->devtype->name);
361 return -ENODEV;
362 }
363
364 return 0;
365}
366
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400367static int max3109_detect(struct device *dev)
368{
369 struct max310x_port *s = dev_get_drvdata(dev);
370 unsigned int val = 0;
371 int ret;
372
373 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
374 if (ret)
375 return ret;
376
377 if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
378 dev_err(dev,
379 "%s ID 0x%02x does not match\n", s->devtype->name, val);
380 return -ENODEV;
381 }
382
383 return 0;
384}
385
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400386static void max310x_power(struct uart_port *port, int on)
387{
388 max310x_port_update(port, MAX310X_MODE1_REG,
389 MAX310X_MODE1_FORCESLEEP_BIT,
390 on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
391 if (on)
392 msleep(50);
393}
394
Alexander Shiyan003236d2013-06-29 10:44:19 +0400395static int max14830_detect(struct device *dev)
396{
397 struct max310x_port *s = dev_get_drvdata(dev);
398 unsigned int val = 0;
399 int ret;
400
401 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
402 MAX310X_EXTREG_ENBL);
403 if (ret)
404 return ret;
405
406 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
407 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
408 if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
409 dev_err(dev,
410 "%s ID 0x%02x does not match\n", s->devtype->name, val);
411 return -ENODEV;
412 }
413
414 return 0;
415}
416
417static void max14830_power(struct uart_port *port, int on)
418{
419 max310x_port_update(port, MAX310X_BRGCFG_REG,
420 MAX14830_BRGCFG_CLKDIS_BIT,
421 on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
422 if (on)
423 msleep(50);
424}
425
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400426static const struct max310x_devtype max3107_devtype = {
427 .name = "MAX3107",
428 .nr = 1,
429 .detect = max3107_detect,
430 .power = max310x_power,
431};
432
433static const struct max310x_devtype max3108_devtype = {
434 .name = "MAX3108",
435 .nr = 1,
436 .detect = max3108_detect,
437 .power = max310x_power,
438};
439
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400440static const struct max310x_devtype max3109_devtype = {
441 .name = "MAX3109",
442 .nr = 2,
443 .detect = max3109_detect,
444 .power = max310x_power,
445};
446
Alexander Shiyan003236d2013-06-29 10:44:19 +0400447static const struct max310x_devtype max14830_devtype = {
448 .name = "MAX14830",
449 .nr = 4,
450 .detect = max14830_detect,
451 .power = max14830_power,
452};
453
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400454static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
455{
456 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400457 case MAX310X_IRQSTS_REG:
458 case MAX310X_LSR_IRQSTS_REG:
459 case MAX310X_SPCHR_IRQSTS_REG:
460 case MAX310X_STS_IRQSTS_REG:
461 case MAX310X_TXFIFOLVL_REG:
462 case MAX310X_RXFIFOLVL_REG:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400463 return false;
464 default:
465 break;
466 }
467
468 return true;
469}
470
471static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
472{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400473 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400474 case MAX310X_RHR_REG:
475 case MAX310X_IRQSTS_REG:
476 case MAX310X_LSR_IRQSTS_REG:
477 case MAX310X_SPCHR_IRQSTS_REG:
478 case MAX310X_STS_IRQSTS_REG:
479 case MAX310X_TXFIFOLVL_REG:
480 case MAX310X_RXFIFOLVL_REG:
481 case MAX310X_GPIODATA_REG:
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400482 case MAX310X_BRGDIVLSB_REG:
483 case MAX310X_REG_05:
484 case MAX310X_REG_1F:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400485 return true;
486 default:
487 break;
488 }
489
490 return false;
491}
492
493static bool max310x_reg_precious(struct device *dev, unsigned int reg)
494{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400495 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400496 case MAX310X_RHR_REG:
497 case MAX310X_IRQSTS_REG:
498 case MAX310X_SPCHR_IRQSTS_REG:
499 case MAX310X_STS_IRQSTS_REG:
500 return true;
501 default:
502 break;
503 }
504
505 return false;
506}
507
Alexander Shiyane97e1552014-02-07 18:16:04 +0400508static int max310x_set_baud(struct uart_port *port, int baud)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400509{
Alexander Shiyane97e1552014-02-07 18:16:04 +0400510 unsigned int mode = 0, clk = port->uartclk, div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400511
Alexander Shiyane97e1552014-02-07 18:16:04 +0400512 /* Check for minimal value for divider */
513 if (div < 16)
514 div = 16;
515
516 if (clk % baud && (div / 16) < 0x8000) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400517 /* Mode x2 */
518 mode = MAX310X_BRGCFG_2XMODE_BIT;
Alexander Shiyane97e1552014-02-07 18:16:04 +0400519 clk = port->uartclk * 2;
520 div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400521
Alexander Shiyane97e1552014-02-07 18:16:04 +0400522 if (clk % baud && (div / 16) < 0x8000) {
523 /* Mode x4 */
524 mode = MAX310X_BRGCFG_4XMODE_BIT;
525 clk = port->uartclk * 4;
526 div = clk / baud;
527 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400528 }
529
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400530 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8);
531 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16);
532 max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode);
Alexander Shiyane97e1552014-02-07 18:16:04 +0400533
534 return DIV_ROUND_CLOSEST(clk, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400535}
536
Bill Pemberton9671f092012-11-19 13:21:50 -0500537static int max310x_update_best_err(unsigned long f, long *besterr)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400538{
539 /* Use baudrate 115200 for calculate error */
540 long err = f % (115200 * 16);
541
542 if ((*besterr < 0) || (*besterr > err)) {
543 *besterr = err;
544 return 0;
545 }
546
547 return 1;
548}
549
Bill Pemberton9671f092012-11-19 13:21:50 -0500550static int max310x_set_ref_clk(struct max310x_port *s)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400551{
552 unsigned int div, clksrc, pllcfg = 0;
553 long besterr = -1;
554 unsigned long fdiv, fmul, bestfreq = s->pdata->frequency;
555
556 /* First, update error without PLL */
557 max310x_update_best_err(s->pdata->frequency, &besterr);
558
559 /* Try all possible PLL dividers */
560 for (div = 1; (div <= 63) && besterr; div++) {
561 fdiv = DIV_ROUND_CLOSEST(s->pdata->frequency, div);
562
563 /* Try multiplier 6 */
564 fmul = fdiv * 6;
565 if ((fdiv >= 500000) && (fdiv <= 800000))
566 if (!max310x_update_best_err(fmul, &besterr)) {
567 pllcfg = (0 << 6) | div;
568 bestfreq = fmul;
569 }
570 /* Try multiplier 48 */
571 fmul = fdiv * 48;
572 if ((fdiv >= 850000) && (fdiv <= 1200000))
573 if (!max310x_update_best_err(fmul, &besterr)) {
574 pllcfg = (1 << 6) | div;
575 bestfreq = fmul;
576 }
577 /* Try multiplier 96 */
578 fmul = fdiv * 96;
579 if ((fdiv >= 425000) && (fdiv <= 1000000))
580 if (!max310x_update_best_err(fmul, &besterr)) {
581 pllcfg = (2 << 6) | div;
582 bestfreq = fmul;
583 }
584 /* Try multiplier 144 */
585 fmul = fdiv * 144;
586 if ((fdiv >= 390000) && (fdiv <= 667000))
587 if (!max310x_update_best_err(fmul, &besterr)) {
588 pllcfg = (3 << 6) | div;
589 bestfreq = fmul;
590 }
591 }
592
593 /* Configure clock source */
594 if (s->pdata->driver_flags & MAX310X_EXT_CLK)
595 clksrc = MAX310X_CLKSRC_EXTCLK_BIT;
596 else
597 clksrc = MAX310X_CLKSRC_CRYST_BIT;
598
599 /* Configure PLL */
600 if (pllcfg) {
601 clksrc |= MAX310X_CLKSRC_PLL_BIT;
602 regmap_write(s->regmap, MAX310X_PLLCFG_REG, pllcfg);
603 } else
604 clksrc |= MAX310X_CLKSRC_PLLBYP_BIT;
605
606 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
607
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400608 /* Wait for crystal */
609 if (pllcfg && !(s->pdata->driver_flags & MAX310X_EXT_CLK))
610 msleep(10);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400611
612 return (int)bestfreq;
613}
614
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400615static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400616{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400617 unsigned int sts, ch, flag;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400618
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400619 if (unlikely(rxlen >= port->fifosize)) {
620 dev_warn_ratelimited(port->dev,
621 "Port %i: Possible RX FIFO overrun\n",
622 port->line);
623 port->icount.buf_overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400624 /* Ensure sanity of RX level */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400625 rxlen = port->fifosize;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400626 }
627
Alexander Shiyanf6544412012-08-06 19:42:32 +0400628 while (rxlen--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400629 ch = max310x_port_read(port, MAX310X_RHR_REG);
630 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400631
632 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
633 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
634
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400635 port->icount.rx++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400636 flag = TTY_NORMAL;
637
638 if (unlikely(sts)) {
639 if (sts & MAX310X_LSR_RXBRK_BIT) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400640 port->icount.brk++;
641 if (uart_handle_break(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400642 continue;
643 } else if (sts & MAX310X_LSR_RXPAR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400644 port->icount.parity++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400645 else if (sts & MAX310X_LSR_FRERR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400646 port->icount.frame++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400647 else if (sts & MAX310X_LSR_RXOVR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400648 port->icount.overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400649
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400650 sts &= port->read_status_mask;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400651 if (sts & MAX310X_LSR_RXBRK_BIT)
652 flag = TTY_BREAK;
653 else if (sts & MAX310X_LSR_RXPAR_BIT)
654 flag = TTY_PARITY;
655 else if (sts & MAX310X_LSR_FRERR_BIT)
656 flag = TTY_FRAME;
657 else if (sts & MAX310X_LSR_RXOVR_BIT)
658 flag = TTY_OVERRUN;
659 }
660
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400661 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400662 continue;
663
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400664 if (sts & port->ignore_status_mask)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400665 continue;
666
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400667 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400668 }
669
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400670 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400671}
672
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400673static void max310x_handle_tx(struct uart_port *port)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400674{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400675 struct circ_buf *xmit = &port->state->xmit;
676 unsigned int txlen, to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400677
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400678 if (unlikely(port->x_char)) {
679 max310x_port_write(port, MAX310X_THR_REG, port->x_char);
680 port->icount.tx++;
681 port->x_char = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400682 return;
683 }
684
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400685 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400686 return;
687
688 /* Get length of data pending in circular buffer */
689 to_send = uart_circ_chars_pending(xmit);
690 if (likely(to_send)) {
691 /* Limit to size of TX FIFO */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400692 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
693 txlen = port->fifosize - txlen;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400694 to_send = (to_send > txlen) ? txlen : to_send;
695
Alexander Shiyanf6544412012-08-06 19:42:32 +0400696 /* Add data to send */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400697 port->icount.tx += to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400698 while (to_send--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400699 max310x_port_write(port, MAX310X_THR_REG,
700 xmit->buf[xmit->tail]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400701 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Joe Perchesfc8114722013-10-08 16:14:21 -0700702 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400703 }
704
705 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400706 uart_write_wakeup(port);
707}
708
709static void max310x_port_irq(struct max310x_port *s, int portno)
710{
711 struct uart_port *port = &s->p[portno].port;
712
713 do {
714 unsigned int ists, lsr, rxlen;
715
716 /* Read IRQ status & RX FIFO level */
717 ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
718 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
719 if (!ists && !rxlen)
720 break;
721
722 if (ists & MAX310X_IRQ_CTS_BIT) {
723 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
724 uart_handle_cts_change(port,
725 !!(lsr & MAX310X_LSR_CTS_BIT));
726 }
727 if (rxlen)
728 max310x_handle_rx(port, rxlen);
729 if (ists & MAX310X_IRQ_TXEMPTY_BIT) {
730 mutex_lock(&s->mutex);
731 max310x_handle_tx(port);
732 mutex_unlock(&s->mutex);
733 }
734 } while (1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400735}
736
737static irqreturn_t max310x_ist(int irq, void *dev_id)
738{
739 struct max310x_port *s = (struct max310x_port *)dev_id;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400740
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400741 if (s->uart.nr > 1) {
742 do {
743 unsigned int val = ~0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400744
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400745 WARN_ON_ONCE(regmap_read(s->regmap,
746 MAX310X_GLOBALIRQ_REG, &val));
747 val = ((1 << s->uart.nr) - 1) & ~val;
748 if (!val)
749 break;
750 max310x_port_irq(s, fls(val) - 1);
751 } while (1);
752 } else
753 max310x_port_irq(s, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400754
755 return IRQ_HANDLED;
756}
757
758static void max310x_wq_proc(struct work_struct *ws)
759{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400760 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
761 struct max310x_port *s = dev_get_drvdata(one->port.dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400762
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400763 mutex_lock(&s->mutex);
764 max310x_handle_tx(&one->port);
765 mutex_unlock(&s->mutex);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400766}
767
768static void max310x_start_tx(struct uart_port *port)
769{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400770 struct max310x_one *one = container_of(port, struct max310x_one, port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400771
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400772 if (!work_pending(&one->tx_work))
773 schedule_work(&one->tx_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400774}
775
776static unsigned int max310x_tx_empty(struct uart_port *port)
777{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400778 unsigned int lvl, sts;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400779
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400780 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
781 sts = max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400782
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400783 return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400784}
785
786static unsigned int max310x_get_mctrl(struct uart_port *port)
787{
788 /* DCD and DSR are not wired and CTS/RTS is handled automatically
789 * so just indicate DSR and CAR asserted
790 */
791 return TIOCM_DSR | TIOCM_CAR;
792}
793
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400794static void max310x_md_proc(struct work_struct *ws)
795{
796 struct max310x_one *one = container_of(ws, struct max310x_one, md_work);
797
798 max310x_port_update(&one->port, MAX310X_MODE2_REG,
799 MAX310X_MODE2_LOOPBACK_BIT,
800 (one->port.mctrl & TIOCM_LOOP) ?
801 MAX310X_MODE2_LOOPBACK_BIT : 0);
802}
803
Alexander Shiyanf6544412012-08-06 19:42:32 +0400804static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
805{
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400806 struct max310x_one *one = container_of(port, struct max310x_one, port);
807
808 schedule_work(&one->md_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400809}
810
811static void max310x_break_ctl(struct uart_port *port, int break_state)
812{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400813 max310x_port_update(port, MAX310X_LCR_REG,
814 MAX310X_LCR_TXBREAK_BIT,
815 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400816}
817
818static void max310x_set_termios(struct uart_port *port,
819 struct ktermios *termios,
820 struct ktermios *old)
821{
Alexander Shiyanf6544412012-08-06 19:42:32 +0400822 unsigned int lcr, flow = 0;
823 int baud;
824
Alexander Shiyanf6544412012-08-06 19:42:32 +0400825 /* Mask termios capabilities we don't support */
826 termios->c_cflag &= ~CMSPAR;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400827
828 /* Word size */
829 switch (termios->c_cflag & CSIZE) {
830 case CS5:
831 lcr = MAX310X_LCR_WORD_LEN_5;
832 break;
833 case CS6:
834 lcr = MAX310X_LCR_WORD_LEN_6;
835 break;
836 case CS7:
837 lcr = MAX310X_LCR_WORD_LEN_7;
838 break;
839 case CS8:
840 default:
841 lcr = MAX310X_LCR_WORD_LEN_8;
842 break;
843 }
844
845 /* Parity */
846 if (termios->c_cflag & PARENB) {
847 lcr |= MAX310X_LCR_PARITY_BIT;
848 if (!(termios->c_cflag & PARODD))
849 lcr |= MAX310X_LCR_EVENPARITY_BIT;
850 }
851
852 /* Stop bits */
853 if (termios->c_cflag & CSTOPB)
854 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
855
856 /* Update LCR register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400857 max310x_port_write(port, MAX310X_LCR_REG, lcr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400858
859 /* Set read status mask */
860 port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
861 if (termios->c_iflag & INPCK)
862 port->read_status_mask |= MAX310X_LSR_RXPAR_BIT |
863 MAX310X_LSR_FRERR_BIT;
864 if (termios->c_iflag & (BRKINT | PARMRK))
865 port->read_status_mask |= MAX310X_LSR_RXBRK_BIT;
866
867 /* Set status ignore mask */
868 port->ignore_status_mask = 0;
869 if (termios->c_iflag & IGNBRK)
870 port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT;
871 if (!(termios->c_cflag & CREAD))
872 port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT |
873 MAX310X_LSR_RXOVR_BIT |
874 MAX310X_LSR_FRERR_BIT |
875 MAX310X_LSR_RXBRK_BIT;
876
877 /* Configure flow control */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400878 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
879 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400880 if (termios->c_cflag & CRTSCTS)
881 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
882 MAX310X_FLOWCTRL_AUTORTS_BIT;
883 if (termios->c_iflag & IXON)
884 flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT |
885 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
886 if (termios->c_iflag & IXOFF)
887 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
888 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400889 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400890
891 /* Get baud rate generator configuration */
892 baud = uart_get_baud_rate(port, termios, old,
893 port->uartclk / 16 / 0xffff,
894 port->uartclk / 4);
895
896 /* Setup baudrate generator */
Alexander Shiyane97e1552014-02-07 18:16:04 +0400897 baud = max310x_set_baud(port, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400898
899 /* Update timeout according to new baud rate */
900 uart_update_timeout(port, termios->c_cflag, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400901}
902
903static int max310x_startup(struct uart_port *port)
904{
905 unsigned int val, line = port->line;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400906 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400907
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400908 s->devtype->power(port, 1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400909
Alexander Shiyanf6544412012-08-06 19:42:32 +0400910 /* Configure MODE1 register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400911 max310x_port_update(port, MAX310X_MODE1_REG,
912 MAX310X_MODE1_TRNSCVCTRL_BIT,
913 (s->pdata->uart_flags[line] & MAX310X_AUTO_DIR_CTRL)
914 ? MAX310X_MODE1_TRNSCVCTRL_BIT : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400915
916 /* Configure MODE2 register */
917 val = MAX310X_MODE2_RXEMPTINV_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400918 if (s->pdata->uart_flags[line] & MAX310X_ECHO_SUPRESS)
919 val |= MAX310X_MODE2_ECHOSUPR_BIT;
920
921 /* Reset FIFOs */
922 val |= MAX310X_MODE2_FIFORST_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400923 max310x_port_write(port, MAX310X_MODE2_REG, val);
924 max310x_port_update(port, MAX310X_MODE2_REG,
925 MAX310X_MODE2_FIFORST_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400926
927 /* Configure flow control levels */
928 /* Flow control halt level 96, resume level 48 */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400929 max310x_port_write(port, MAX310X_FLOWLVL_REG,
930 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
Alexander Shiyanf6544412012-08-06 19:42:32 +0400931
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400932 /* Clear IRQ status register */
933 max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400934
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400935 /* Enable RX, TX, CTS change interrupts */
936 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
937 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400938
939 return 0;
940}
941
942static void max310x_shutdown(struct uart_port *port)
943{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400944 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400945
946 /* Disable all interrupts */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400947 max310x_port_write(port, MAX310X_IRQEN_REG, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400948
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400949 s->devtype->power(port, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400950}
951
952static const char *max310x_type(struct uart_port *port)
953{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400954 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400955
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400956 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400957}
958
959static int max310x_request_port(struct uart_port *port)
960{
961 /* Do nothing */
962 return 0;
963}
964
Alexander Shiyanf6544412012-08-06 19:42:32 +0400965static void max310x_config_port(struct uart_port *port, int flags)
966{
967 if (flags & UART_CONFIG_TYPE)
968 port->type = PORT_MAX310X;
969}
970
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400971static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400972{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400973 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
974 return -EINVAL;
975 if (s->irq != port->irq)
976 return -EINVAL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400977
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400978 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400979}
980
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400981static void max310x_null_void(struct uart_port *port)
982{
983 /* Do nothing */
984}
985
986static const struct uart_ops max310x_ops = {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400987 .tx_empty = max310x_tx_empty,
988 .set_mctrl = max310x_set_mctrl,
989 .get_mctrl = max310x_get_mctrl,
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400990 .stop_tx = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +0400991 .start_tx = max310x_start_tx,
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400992 .stop_rx = max310x_null_void,
993 .enable_ms = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +0400994 .break_ctl = max310x_break_ctl,
995 .startup = max310x_startup,
996 .shutdown = max310x_shutdown,
997 .set_termios = max310x_set_termios,
998 .type = max310x_type,
999 .request_port = max310x_request_port,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001000 .release_port = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001001 .config_port = max310x_config_port,
1002 .verify_port = max310x_verify_port,
1003};
1004
Alexander Shiyanc2978292013-07-29 19:27:32 +04001005static int __maybe_unused max310x_suspend(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001006{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001007 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001008 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001009
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001010 for (i = 0; i < s->uart.nr; i++) {
1011 uart_suspend_port(&s->uart, &s->p[i].port);
1012 s->devtype->power(&s->p[i].port, 0);
1013 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001014
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001015 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001016}
1017
Alexander Shiyanc2978292013-07-29 19:27:32 +04001018static int __maybe_unused max310x_resume(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001019{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001020 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001021 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001022
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001023 for (i = 0; i < s->uart.nr; i++) {
1024 s->devtype->power(&s->p[i].port, 1);
1025 uart_resume_port(&s->uart, &s->p[i].port);
1026 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001027
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001028 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001029}
1030
1031#ifdef CONFIG_GPIOLIB
1032static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
1033{
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001034 unsigned int val;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001035 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001036 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001037
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001038 val = max310x_port_read(port, MAX310X_GPIODATA_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001039
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001040 return !!((val >> 4) & (1 << (offset % 4)));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001041}
1042
1043static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1044{
1045 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001046 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001047
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001048 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1049 value ? 1 << (offset % 4) : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001050}
1051
1052static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1053{
1054 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
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 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001058
1059 return 0;
1060}
1061
1062static int max310x_gpio_direction_output(struct gpio_chip *chip,
1063 unsigned offset, int value)
1064{
1065 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001066 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001067
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001068 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1069 value ? 1 << (offset % 4) : 0);
1070 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
1071 1 << (offset % 4));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001072
1073 return 0;
1074}
1075#endif
1076
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001077static int max310x_probe(struct device *dev, int is_spi,
1078 struct max310x_devtype *devtype, int irq)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001079{
1080 struct max310x_port *s;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001081 struct max310x_pdata *pdata = dev_get_platdata(dev);
1082 int i, ret, uartclk;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001083
1084 /* Check for IRQ */
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001085 if (irq <= 0) {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001086 dev_err(dev, "No IRQ specified\n");
1087 return -ENOTSUPP;
1088 }
1089
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001090 if (!pdata) {
1091 dev_err(dev, "No platform data supplied\n");
1092 return -EINVAL;
1093 }
1094
Alexander Shiyanf6544412012-08-06 19:42:32 +04001095 /* Alloc port structure */
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001096 s = devm_kzalloc(dev, sizeof(*s) +
1097 sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001098 if (!s) {
1099 dev_err(dev, "Error allocating port structure\n");
1100 return -ENOMEM;
1101 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001102
1103 /* Check input frequency */
1104 if ((pdata->driver_flags & MAX310X_EXT_CLK) &&
1105 ((pdata->frequency < 500000) || (pdata->frequency > 35000000)))
1106 goto err_freq;
1107 /* Check frequency for quartz */
1108 if (!(pdata->driver_flags & MAX310X_EXT_CLK) &&
1109 ((pdata->frequency < 1000000) || (pdata->frequency > 4000000)))
1110 goto err_freq;
1111
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001112 s->pdata = pdata;
1113 s->devtype = devtype;
1114 dev_set_drvdata(dev, s);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001115
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001116 mutex_init(&s->mutex);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001117
1118 /* Setup regmap */
1119 s->regcfg.reg_bits = 8;
1120 s->regcfg.val_bits = 8;
1121 s->regcfg.read_flag_mask = 0x00;
1122 s->regcfg.write_flag_mask = 0x80;
1123 s->regcfg.cache_type = REGCACHE_RBTREE;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001124 s->regcfg.writeable_reg = max310x_reg_writeable;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001125 s->regcfg.volatile_reg = max310x_reg_volatile;
1126 s->regcfg.precious_reg = max310x_reg_precious;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001127 s->regcfg.max_register = devtype->nr * 0x20 - 1;
1128
1129 if (IS_ENABLED(CONFIG_SPI_MASTER) && is_spi) {
1130 struct spi_device *spi = to_spi_device(dev);
1131
1132 s->regmap = devm_regmap_init_spi(spi, &s->regcfg);
1133 } else
1134 return -ENOTSUPP;
1135
Alexander Shiyanf6544412012-08-06 19:42:32 +04001136 if (IS_ERR(s->regmap)) {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001137 dev_err(dev, "Failed to initialize register map\n");
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001138 return PTR_ERR(s->regmap);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001139 }
1140
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001141 /* Check device to ensure we are talking to what we expect */
1142 ret = devtype->detect(dev);
1143 if (ret)
1144 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001145
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001146 for (i = 0; i < devtype->nr; i++) {
1147 unsigned int offs = i << 5;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001148
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001149 /* Reset port */
1150 regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
1151 MAX310X_MODE2_RST_BIT);
1152 /* Clear port reset */
1153 regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001154
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001155 /* Wait for port startup */
1156 do {
1157 regmap_read(s->regmap,
1158 MAX310X_BRGDIVLSB_REG + offs, &ret);
1159 } while (ret != 0x01);
1160
1161 regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs,
1162 MAX310X_MODE1_AUTOSLEEP_BIT,
1163 MAX310X_MODE1_AUTOSLEEP_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001164 }
1165
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001166 uartclk = max310x_set_ref_clk(s);
1167 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
1168
Alexander Shiyanf6544412012-08-06 19:42:32 +04001169 /* Register UART driver */
1170 s->uart.owner = THIS_MODULE;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001171 s->uart.dev_name = "ttyMAX";
1172 s->uart.major = MAX310X_MAJOR;
1173 s->uart.minor = MAX310X_MINOR;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001174 s->uart.nr = devtype->nr;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001175 ret = uart_register_driver(&s->uart);
1176 if (ret) {
1177 dev_err(dev, "Registering UART driver failed\n");
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001178 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001179 }
1180
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001181 for (i = 0; i < devtype->nr; i++) {
1182 /* Initialize port data */
1183 s->p[i].port.line = i;
1184 s->p[i].port.dev = dev;
1185 s->p[i].port.irq = irq;
1186 s->p[i].port.type = PORT_MAX310X;
1187 s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001188 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001189 s->p[i].port.iotype = UPIO_PORT;
1190 s->p[i].port.iobase = i * 0x20;
1191 s->p[i].port.membase = (void __iomem *)~0;
1192 s->p[i].port.uartclk = uartclk;
1193 s->p[i].port.ops = &max310x_ops;
1194 /* Disable all interrupts */
1195 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
1196 /* Clear IRQ status register */
1197 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
1198 /* Enable IRQ pin */
1199 max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG,
1200 MAX310X_MODE1_IRQSEL_BIT,
1201 MAX310X_MODE1_IRQSEL_BIT);
1202 /* Initialize queue for start TX */
1203 INIT_WORK(&s->p[i].tx_work, max310x_wq_proc);
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001204 /* Initialize queue for changing mode */
1205 INIT_WORK(&s->p[i].md_work, max310x_md_proc);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001206 /* Register port */
1207 uart_add_one_port(&s->uart, &s->p[i].port);
1208 /* Go to suspend mode */
1209 devtype->power(&s->p[i].port, 0);
1210 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001211
1212#ifdef CONFIG_GPIOLIB
1213 /* Setup GPIO cotroller */
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001214 if (s->pdata->gpio_base) {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001215 s->gpio.owner = THIS_MODULE;
1216 s->gpio.dev = dev;
1217 s->gpio.label = dev_name(dev);
1218 s->gpio.direction_input = max310x_gpio_direction_input;
1219 s->gpio.get = max310x_gpio_get;
1220 s->gpio.direction_output= max310x_gpio_direction_output;
1221 s->gpio.set = max310x_gpio_set;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001222 s->gpio.base = s->pdata->gpio_base;
1223 s->gpio.ngpio = devtype->nr * 4;
Alexander Shiyan273a4b82012-11-22 00:07:32 +04001224 s->gpio.can_sleep = 1;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001225 if (!gpiochip_add(&s->gpio))
1226 s->gpio_used = 1;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001227 } else
1228 dev_info(dev, "GPIO support not enabled\n");
1229#endif
1230
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001231 /* Setup interrupt */
1232 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
1233 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1234 dev_name(dev), s);
1235 if (ret) {
1236 dev_err(dev, "Unable to reguest IRQ %i\n", irq);
1237#ifdef CONFIG_GPIOLIB
1238 if (s->gpio_used)
1239 WARN_ON(gpiochip_remove(&s->gpio));
1240#endif
1241 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001242
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001243 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001244
1245err_freq:
1246 dev_err(dev, "Frequency parameter incorrect\n");
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001247 return -EINVAL;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001248}
1249
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001250static int max310x_remove(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001251{
Alexander Shiyanf6544412012-08-06 19:42:32 +04001252 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001253 int i, ret = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001254
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001255 for (i = 0; i < s->uart.nr; i++) {
1256 cancel_work_sync(&s->p[i].tx_work);
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001257 cancel_work_sync(&s->p[i].md_work);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001258 uart_remove_one_port(&s->uart, &s->p[i].port);
1259 s->devtype->power(&s->p[i].port, 0);
1260 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001261
1262 uart_unregister_driver(&s->uart);
1263
1264#ifdef CONFIG_GPIOLIB
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001265 if (s->gpio_used)
Emil Goode23e7c6a2012-08-18 18:12:48 +02001266 ret = gpiochip_remove(&s->gpio);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001267#endif
1268
Emil Goode23e7c6a2012-08-18 18:12:48 +02001269 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001270}
1271
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001272#ifdef CONFIG_SPI_MASTER
1273static int max310x_spi_probe(struct spi_device *spi)
1274{
1275 struct max310x_devtype *devtype =
1276 (struct max310x_devtype *)spi_get_device_id(spi)->driver_data;
1277 int ret;
1278
1279 /* Setup SPI bus */
1280 spi->bits_per_word = 8;
1281 spi->mode = spi->mode ? : SPI_MODE_0;
1282 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
1283 ret = spi_setup(spi);
1284 if (ret) {
1285 dev_err(&spi->dev, "SPI setup failed\n");
1286 return ret;
1287 }
1288
1289 return max310x_probe(&spi->dev, 1, devtype, spi->irq);
1290}
1291
1292static int max310x_spi_remove(struct spi_device *spi)
1293{
1294 return max310x_remove(&spi->dev);
1295}
1296
1297static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
1298
Alexander Shiyanf6544412012-08-06 19:42:32 +04001299static const struct spi_device_id max310x_id_table[] = {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001300 { "max3107", (kernel_ulong_t)&max3107_devtype, },
1301 { "max3108", (kernel_ulong_t)&max3108_devtype, },
Alexander Shiyan21fc5092013-06-29 10:44:18 +04001302 { "max3109", (kernel_ulong_t)&max3109_devtype, },
Alexander Shiyan003236d2013-06-29 10:44:19 +04001303 { "max14830", (kernel_ulong_t)&max14830_devtype, },
Axel Lin1838b8c2012-11-04 23:34:18 +08001304 { }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001305};
1306MODULE_DEVICE_TABLE(spi, max310x_id_table);
1307
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001308static struct spi_driver max310x_uart_driver = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001309 .driver = {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001310 .name = MAX310X_NAME,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001311 .owner = THIS_MODULE,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001312 .pm = &max310x_pm_ops,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001313 },
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001314 .probe = max310x_spi_probe,
1315 .remove = max310x_spi_remove,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001316 .id_table = max310x_id_table,
1317};
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001318module_spi_driver(max310x_uart_driver);
1319#endif
Alexander Shiyanf6544412012-08-06 19:42:32 +04001320
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001321MODULE_LICENSE("GPL");
Alexander Shiyanf6544412012-08-06 19:42:32 +04001322MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1323MODULE_DESCRIPTION("MAX310X serial driver");