blob: 5836168414e4e0999bb5d3003b010384dceb8220 [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 Shiyan10d8b342013-06-29 10:44:17 +040016#include <linux/bitops.h>
Alexander Shiyand3a8a252014-02-10 22:18:31 +040017#include <linux/clk.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040018#include <linux/delay.h>
19#include <linux/device.h>
20#include <linux/gpio.h>
21#include <linux/module.h>
Alexander Shiyan58afc902014-02-10 22:18:36 +040022#include <linux/of.h>
23#include <linux/of_device.h>
Alexander Shiyan5f529042014-02-10 22:18:35 +040024#include <linux/regmap.h>
Alexander Shiyanf6544412012-08-06 19:42:32 +040025#include <linux/serial_core.h>
26#include <linux/serial.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
Greg Kroah-Hartman1456dad2014-02-13 15:18:57 -080029#include <linux/spi/spi.h>
Alexander Shiyan10d8b342013-06-29 10:44:17 +040030
Alexander Shiyan10d8b342013-06-29 10:44:17 +040031#define MAX310X_NAME "max310x"
Alexander Shiyanf6544412012-08-06 19:42:32 +040032#define MAX310X_MAJOR 204
33#define MAX310X_MINOR 209
34
35/* MAX310X register definitions */
36#define MAX310X_RHR_REG (0x00) /* RX FIFO */
37#define MAX310X_THR_REG (0x00) /* TX FIFO */
38#define MAX310X_IRQEN_REG (0x01) /* IRQ enable */
39#define MAX310X_IRQSTS_REG (0x02) /* IRQ status */
40#define MAX310X_LSR_IRQEN_REG (0x03) /* LSR IRQ enable */
41#define MAX310X_LSR_IRQSTS_REG (0x04) /* LSR IRQ status */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040042#define MAX310X_REG_05 (0x05)
43#define MAX310X_SPCHR_IRQEN_REG MAX310X_REG_05 /* Special char IRQ en */
Alexander Shiyanf6544412012-08-06 19:42:32 +040044#define MAX310X_SPCHR_IRQSTS_REG (0x06) /* Special char IRQ status */
45#define MAX310X_STS_IRQEN_REG (0x07) /* Status IRQ enable */
46#define MAX310X_STS_IRQSTS_REG (0x08) /* Status IRQ status */
47#define MAX310X_MODE1_REG (0x09) /* MODE1 */
48#define MAX310X_MODE2_REG (0x0a) /* MODE2 */
49#define MAX310X_LCR_REG (0x0b) /* LCR */
50#define MAX310X_RXTO_REG (0x0c) /* RX timeout */
51#define MAX310X_HDPIXDELAY_REG (0x0d) /* Auto transceiver delays */
52#define MAX310X_IRDA_REG (0x0e) /* IRDA settings */
53#define MAX310X_FLOWLVL_REG (0x0f) /* Flow control levels */
54#define MAX310X_FIFOTRIGLVL_REG (0x10) /* FIFO IRQ trigger levels */
55#define MAX310X_TXFIFOLVL_REG (0x11) /* TX FIFO level */
56#define MAX310X_RXFIFOLVL_REG (0x12) /* RX FIFO level */
57#define MAX310X_FLOWCTRL_REG (0x13) /* Flow control */
58#define MAX310X_XON1_REG (0x14) /* XON1 character */
59#define MAX310X_XON2_REG (0x15) /* XON2 character */
60#define MAX310X_XOFF1_REG (0x16) /* XOFF1 character */
61#define MAX310X_XOFF2_REG (0x17) /* XOFF2 character */
62#define MAX310X_GPIOCFG_REG (0x18) /* GPIO config */
63#define MAX310X_GPIODATA_REG (0x19) /* GPIO data */
64#define MAX310X_PLLCFG_REG (0x1a) /* PLL config */
65#define MAX310X_BRGCFG_REG (0x1b) /* Baud rate generator conf */
66#define MAX310X_BRGDIVLSB_REG (0x1c) /* Baud rate divisor LSB */
67#define MAX310X_BRGDIVMSB_REG (0x1d) /* Baud rate divisor MSB */
68#define MAX310X_CLKSRC_REG (0x1e) /* Clock source */
Alexander Shiyan10d8b342013-06-29 10:44:17 +040069#define MAX310X_REG_1F (0x1f)
70
71#define MAX310X_REVID_REG MAX310X_REG_1F /* Revision ID */
72
73#define MAX310X_GLOBALIRQ_REG MAX310X_REG_1F /* Global IRQ (RO) */
74#define MAX310X_GLOBALCMD_REG MAX310X_REG_1F /* Global Command (WO) */
75
76/* Extended registers */
77#define MAX310X_REVID_EXTREG MAX310X_REG_05 /* Revision ID */
Alexander Shiyanf6544412012-08-06 19:42:32 +040078
79/* IRQ register bits */
80#define MAX310X_IRQ_LSR_BIT (1 << 0) /* LSR interrupt */
81#define MAX310X_IRQ_SPCHR_BIT (1 << 1) /* Special char interrupt */
82#define MAX310X_IRQ_STS_BIT (1 << 2) /* Status interrupt */
83#define MAX310X_IRQ_RXFIFO_BIT (1 << 3) /* RX FIFO interrupt */
84#define MAX310X_IRQ_TXFIFO_BIT (1 << 4) /* TX FIFO interrupt */
85#define MAX310X_IRQ_TXEMPTY_BIT (1 << 5) /* TX FIFO empty interrupt */
86#define MAX310X_IRQ_RXEMPTY_BIT (1 << 6) /* RX FIFO empty interrupt */
87#define MAX310X_IRQ_CTS_BIT (1 << 7) /* CTS interrupt */
88
89/* LSR register bits */
90#define MAX310X_LSR_RXTO_BIT (1 << 0) /* RX timeout */
91#define MAX310X_LSR_RXOVR_BIT (1 << 1) /* RX overrun */
92#define MAX310X_LSR_RXPAR_BIT (1 << 2) /* RX parity error */
93#define MAX310X_LSR_FRERR_BIT (1 << 3) /* Frame error */
94#define MAX310X_LSR_RXBRK_BIT (1 << 4) /* RX break */
95#define MAX310X_LSR_RXNOISE_BIT (1 << 5) /* RX noise */
96#define MAX310X_LSR_CTS_BIT (1 << 7) /* CTS pin state */
97
98/* Special character register bits */
99#define MAX310X_SPCHR_XON1_BIT (1 << 0) /* XON1 character */
100#define MAX310X_SPCHR_XON2_BIT (1 << 1) /* XON2 character */
101#define MAX310X_SPCHR_XOFF1_BIT (1 << 2) /* XOFF1 character */
102#define MAX310X_SPCHR_XOFF2_BIT (1 << 3) /* XOFF2 character */
103#define MAX310X_SPCHR_BREAK_BIT (1 << 4) /* RX break */
104#define MAX310X_SPCHR_MULTIDROP_BIT (1 << 5) /* 9-bit multidrop addr char */
105
106/* Status register bits */
107#define MAX310X_STS_GPIO0_BIT (1 << 0) /* GPIO 0 interrupt */
108#define MAX310X_STS_GPIO1_BIT (1 << 1) /* GPIO 1 interrupt */
109#define MAX310X_STS_GPIO2_BIT (1 << 2) /* GPIO 2 interrupt */
110#define MAX310X_STS_GPIO3_BIT (1 << 3) /* GPIO 3 interrupt */
111#define MAX310X_STS_CLKREADY_BIT (1 << 5) /* Clock ready */
112#define MAX310X_STS_SLEEP_BIT (1 << 6) /* Sleep interrupt */
113
114/* MODE1 register bits */
115#define MAX310X_MODE1_RXDIS_BIT (1 << 0) /* RX disable */
116#define MAX310X_MODE1_TXDIS_BIT (1 << 1) /* TX disable */
117#define MAX310X_MODE1_TXHIZ_BIT (1 << 2) /* TX pin three-state */
118#define MAX310X_MODE1_RTSHIZ_BIT (1 << 3) /* RTS pin three-state */
119#define MAX310X_MODE1_TRNSCVCTRL_BIT (1 << 4) /* Transceiver ctrl enable */
120#define MAX310X_MODE1_FORCESLEEP_BIT (1 << 5) /* Force sleep mode */
121#define MAX310X_MODE1_AUTOSLEEP_BIT (1 << 6) /* Auto sleep enable */
122#define MAX310X_MODE1_IRQSEL_BIT (1 << 7) /* IRQ pin enable */
123
124/* MODE2 register bits */
125#define MAX310X_MODE2_RST_BIT (1 << 0) /* Chip reset */
126#define MAX310X_MODE2_FIFORST_BIT (1 << 1) /* FIFO reset */
127#define MAX310X_MODE2_RXTRIGINV_BIT (1 << 2) /* RX FIFO INT invert */
128#define MAX310X_MODE2_RXEMPTINV_BIT (1 << 3) /* RX FIFO empty INT invert */
129#define MAX310X_MODE2_SPCHR_BIT (1 << 4) /* Special chr detect enable */
130#define MAX310X_MODE2_LOOPBACK_BIT (1 << 5) /* Internal loopback enable */
131#define MAX310X_MODE2_MULTIDROP_BIT (1 << 6) /* 9-bit multidrop enable */
132#define MAX310X_MODE2_ECHOSUPR_BIT (1 << 7) /* ECHO suppression enable */
133
134/* LCR register bits */
135#define MAX310X_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
136#define MAX310X_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
137 *
138 * Word length bits table:
139 * 00 -> 5 bit words
140 * 01 -> 6 bit words
141 * 10 -> 7 bit words
142 * 11 -> 8 bit words
143 */
144#define MAX310X_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
145 *
146 * STOP length bit table:
147 * 0 -> 1 stop bit
148 * 1 -> 1-1.5 stop bits if
149 * word length is 5,
150 * 2 stop bits otherwise
151 */
152#define MAX310X_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
153#define MAX310X_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
154#define MAX310X_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
155#define MAX310X_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
156#define MAX310X_LCR_RTS_BIT (1 << 7) /* RTS pin control */
157#define MAX310X_LCR_WORD_LEN_5 (0x00)
158#define MAX310X_LCR_WORD_LEN_6 (0x01)
159#define MAX310X_LCR_WORD_LEN_7 (0x02)
160#define MAX310X_LCR_WORD_LEN_8 (0x03)
161
162/* IRDA register bits */
163#define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */
164#define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */
Alexander Shiyanf6544412012-08-06 19:42:32 +0400165
166/* Flow control trigger level register masks */
167#define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */
168#define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */
169#define MAX310X_FLOWLVL_HALT(words) ((words / 8) & 0x0f)
170#define MAX310X_FLOWLVL_RES(words) (((words / 8) & 0x0f) << 4)
171
172/* FIFO interrupt trigger level register masks */
173#define MAX310X_FIFOTRIGLVL_TX_MASK (0x0f) /* TX FIFO trigger level */
174#define MAX310X_FIFOTRIGLVL_RX_MASK (0xf0) /* RX FIFO trigger level */
175#define MAX310X_FIFOTRIGLVL_TX(words) ((words / 8) & 0x0f)
176#define MAX310X_FIFOTRIGLVL_RX(words) (((words / 8) & 0x0f) << 4)
177
178/* Flow control register bits */
179#define MAX310X_FLOWCTRL_AUTORTS_BIT (1 << 0) /* Auto RTS flow ctrl enable */
180#define MAX310X_FLOWCTRL_AUTOCTS_BIT (1 << 1) /* Auto CTS flow ctrl enable */
181#define MAX310X_FLOWCTRL_GPIADDR_BIT (1 << 2) /* Enables that GPIO inputs
182 * are used in conjunction with
183 * XOFF2 for definition of
184 * special character */
185#define MAX310X_FLOWCTRL_SWFLOWEN_BIT (1 << 3) /* Auto SW flow ctrl enable */
186#define MAX310X_FLOWCTRL_SWFLOW0_BIT (1 << 4) /* SWFLOW bit 0 */
187#define MAX310X_FLOWCTRL_SWFLOW1_BIT (1 << 5) /* SWFLOW bit 1
188 *
189 * SWFLOW bits 1 & 0 table:
190 * 00 -> no transmitter flow
191 * control
192 * 01 -> receiver compares
193 * XON2 and XOFF2
194 * and controls
195 * transmitter
196 * 10 -> receiver compares
197 * XON1 and XOFF1
198 * and controls
199 * transmitter
200 * 11 -> receiver compares
201 * XON1, XON2, XOFF1 and
202 * XOFF2 and controls
203 * transmitter
204 */
205#define MAX310X_FLOWCTRL_SWFLOW2_BIT (1 << 6) /* SWFLOW bit 2 */
206#define MAX310X_FLOWCTRL_SWFLOW3_BIT (1 << 7) /* SWFLOW bit 3
207 *
208 * SWFLOW bits 3 & 2 table:
209 * 00 -> no received flow
210 * control
211 * 01 -> transmitter generates
212 * XON2 and XOFF2
213 * 10 -> transmitter generates
214 * XON1 and XOFF1
215 * 11 -> transmitter generates
216 * XON1, XON2, XOFF1 and
217 * XOFF2
218 */
219
Alexander Shiyanf6544412012-08-06 19:42:32 +0400220/* PLL configuration register masks */
221#define MAX310X_PLLCFG_PREDIV_MASK (0x3f) /* PLL predivision value */
222#define MAX310X_PLLCFG_PLLFACTOR_MASK (0xc0) /* PLL multiplication factor */
223
224/* Baud rate generator configuration register bits */
225#define MAX310X_BRGCFG_2XMODE_BIT (1 << 4) /* Double baud rate */
226#define MAX310X_BRGCFG_4XMODE_BIT (1 << 5) /* Quadruple baud rate */
227
228/* Clock source register bits */
229#define MAX310X_CLKSRC_CRYST_BIT (1 << 1) /* Crystal osc enable */
230#define MAX310X_CLKSRC_PLL_BIT (1 << 2) /* PLL enable */
231#define MAX310X_CLKSRC_PLLBYP_BIT (1 << 3) /* PLL bypass */
232#define MAX310X_CLKSRC_EXTCLK_BIT (1 << 4) /* External clock enable */
233#define MAX310X_CLKSRC_CLK2RTS_BIT (1 << 7) /* Baud clk to RTS pin */
234
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400235/* Global commands */
236#define MAX310X_EXTREG_ENBL (0xce)
237#define MAX310X_EXTREG_DSBL (0xcd)
238
Alexander Shiyanf6544412012-08-06 19:42:32 +0400239/* Misc definitions */
240#define MAX310X_FIFO_SIZE (128)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400241#define MAX310x_REV_MASK (0xfc)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400242
243/* MAX3107 specific */
244#define MAX3107_REV_ID (0xa0)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400245
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400246/* MAX3109 specific */
247#define MAX3109_REV_ID (0xc0)
248
Alexander Shiyan003236d2013-06-29 10:44:19 +0400249/* MAX14830 specific */
250#define MAX14830_BRGCFG_CLKDIS_BIT (1 << 6) /* Clock Disable */
251#define MAX14830_REV_ID (0xb0)
252
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400253struct max310x_devtype {
254 char name[9];
255 int nr;
256 int (*detect)(struct device *);
257 void (*power)(struct uart_port *, int);
258};
Alexander Shiyanf6544412012-08-06 19:42:32 +0400259
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400260struct max310x_one {
261 struct uart_port port;
262 struct work_struct tx_work;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400263 struct work_struct md_work;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400264};
265
266struct max310x_port {
267 struct uart_driver uart;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400268 struct max310x_devtype *devtype;
269 struct regmap *regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400270 struct mutex mutex;
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400271 struct clk *clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400272#ifdef CONFIG_GPIOLIB
273 struct gpio_chip gpio;
274#endif
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400275 struct max310x_one p[0];
Alexander Shiyanf6544412012-08-06 19:42:32 +0400276};
277
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400278static u8 max310x_port_read(struct uart_port *port, u8 reg)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400279{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400280 struct max310x_port *s = dev_get_drvdata(port->dev);
281 unsigned int val = 0;
282
283 regmap_read(s->regmap, port->iobase + reg, &val);
284
285 return val;
286}
287
288static void max310x_port_write(struct uart_port *port, u8 reg, u8 val)
289{
290 struct max310x_port *s = dev_get_drvdata(port->dev);
291
292 regmap_write(s->regmap, port->iobase + reg, val);
293}
294
295static void max310x_port_update(struct uart_port *port, u8 reg, u8 mask, u8 val)
296{
297 struct max310x_port *s = dev_get_drvdata(port->dev);
298
299 regmap_update_bits(s->regmap, port->iobase + reg, mask, val);
300}
301
302static int max3107_detect(struct device *dev)
303{
304 struct max310x_port *s = dev_get_drvdata(dev);
305 unsigned int val = 0;
306 int ret;
307
308 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
309 if (ret)
310 return ret;
311
312 if (((val & MAX310x_REV_MASK) != MAX3107_REV_ID)) {
313 dev_err(dev,
314 "%s ID 0x%02x does not match\n", s->devtype->name, val);
315 return -ENODEV;
316 }
317
318 return 0;
319}
320
321static int max3108_detect(struct device *dev)
322{
323 struct max310x_port *s = dev_get_drvdata(dev);
324 unsigned int val = 0;
325 int ret;
326
327 /* MAX3108 have not REV ID register, we just check default value
328 * from clocksource register to make sure everything works.
329 */
330 ret = regmap_read(s->regmap, MAX310X_CLKSRC_REG, &val);
331 if (ret)
332 return ret;
333
334 if (val != (MAX310X_CLKSRC_EXTCLK_BIT | MAX310X_CLKSRC_PLLBYP_BIT)) {
335 dev_err(dev, "%s not present\n", s->devtype->name);
336 return -ENODEV;
337 }
338
339 return 0;
340}
341
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400342static int max3109_detect(struct device *dev)
343{
344 struct max310x_port *s = dev_get_drvdata(dev);
345 unsigned int val = 0;
346 int ret;
347
348 ret = regmap_read(s->regmap, MAX310X_REVID_REG, &val);
349 if (ret)
350 return ret;
351
352 if (((val & MAX310x_REV_MASK) != MAX3109_REV_ID)) {
353 dev_err(dev,
354 "%s ID 0x%02x does not match\n", s->devtype->name, val);
355 return -ENODEV;
356 }
357
358 return 0;
359}
360
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400361static void max310x_power(struct uart_port *port, int on)
362{
363 max310x_port_update(port, MAX310X_MODE1_REG,
364 MAX310X_MODE1_FORCESLEEP_BIT,
365 on ? 0 : MAX310X_MODE1_FORCESLEEP_BIT);
366 if (on)
367 msleep(50);
368}
369
Alexander Shiyan003236d2013-06-29 10:44:19 +0400370static int max14830_detect(struct device *dev)
371{
372 struct max310x_port *s = dev_get_drvdata(dev);
373 unsigned int val = 0;
374 int ret;
375
376 ret = regmap_write(s->regmap, MAX310X_GLOBALCMD_REG,
377 MAX310X_EXTREG_ENBL);
378 if (ret)
379 return ret;
380
381 regmap_read(s->regmap, MAX310X_REVID_EXTREG, &val);
382 regmap_write(s->regmap, MAX310X_GLOBALCMD_REG, MAX310X_EXTREG_DSBL);
383 if (((val & MAX310x_REV_MASK) != MAX14830_REV_ID)) {
384 dev_err(dev,
385 "%s ID 0x%02x does not match\n", s->devtype->name, val);
386 return -ENODEV;
387 }
388
389 return 0;
390}
391
392static void max14830_power(struct uart_port *port, int on)
393{
394 max310x_port_update(port, MAX310X_BRGCFG_REG,
395 MAX14830_BRGCFG_CLKDIS_BIT,
396 on ? 0 : MAX14830_BRGCFG_CLKDIS_BIT);
397 if (on)
398 msleep(50);
399}
400
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400401static const struct max310x_devtype max3107_devtype = {
402 .name = "MAX3107",
403 .nr = 1,
404 .detect = max3107_detect,
405 .power = max310x_power,
406};
407
408static const struct max310x_devtype max3108_devtype = {
409 .name = "MAX3108",
410 .nr = 1,
411 .detect = max3108_detect,
412 .power = max310x_power,
413};
414
Alexander Shiyan21fc5092013-06-29 10:44:18 +0400415static const struct max310x_devtype max3109_devtype = {
416 .name = "MAX3109",
417 .nr = 2,
418 .detect = max3109_detect,
419 .power = max310x_power,
420};
421
Alexander Shiyan003236d2013-06-29 10:44:19 +0400422static const struct max310x_devtype max14830_devtype = {
423 .name = "MAX14830",
424 .nr = 4,
425 .detect = max14830_detect,
426 .power = max14830_power,
427};
428
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400429static bool max310x_reg_writeable(struct device *dev, unsigned int reg)
430{
431 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400432 case MAX310X_IRQSTS_REG:
433 case MAX310X_LSR_IRQSTS_REG:
434 case MAX310X_SPCHR_IRQSTS_REG:
435 case MAX310X_STS_IRQSTS_REG:
436 case MAX310X_TXFIFOLVL_REG:
437 case MAX310X_RXFIFOLVL_REG:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400438 return false;
439 default:
440 break;
441 }
442
443 return true;
444}
445
446static bool max310x_reg_volatile(struct device *dev, unsigned int reg)
447{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400448 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400449 case MAX310X_RHR_REG:
450 case MAX310X_IRQSTS_REG:
451 case MAX310X_LSR_IRQSTS_REG:
452 case MAX310X_SPCHR_IRQSTS_REG:
453 case MAX310X_STS_IRQSTS_REG:
454 case MAX310X_TXFIFOLVL_REG:
455 case MAX310X_RXFIFOLVL_REG:
456 case MAX310X_GPIODATA_REG:
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400457 case MAX310X_BRGDIVLSB_REG:
458 case MAX310X_REG_05:
459 case MAX310X_REG_1F:
Alexander Shiyanf6544412012-08-06 19:42:32 +0400460 return true;
461 default:
462 break;
463 }
464
465 return false;
466}
467
468static bool max310x_reg_precious(struct device *dev, unsigned int reg)
469{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400470 switch (reg & 0x1f) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400471 case MAX310X_RHR_REG:
472 case MAX310X_IRQSTS_REG:
473 case MAX310X_SPCHR_IRQSTS_REG:
474 case MAX310X_STS_IRQSTS_REG:
475 return true;
476 default:
477 break;
478 }
479
480 return false;
481}
482
Alexander Shiyane97e1552014-02-07 18:16:04 +0400483static int max310x_set_baud(struct uart_port *port, int baud)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400484{
Alexander Shiyane97e1552014-02-07 18:16:04 +0400485 unsigned int mode = 0, clk = port->uartclk, div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400486
Alexander Shiyane97e1552014-02-07 18:16:04 +0400487 /* Check for minimal value for divider */
488 if (div < 16)
489 div = 16;
490
491 if (clk % baud && (div / 16) < 0x8000) {
Alexander Shiyanf6544412012-08-06 19:42:32 +0400492 /* Mode x2 */
493 mode = MAX310X_BRGCFG_2XMODE_BIT;
Alexander Shiyane97e1552014-02-07 18:16:04 +0400494 clk = port->uartclk * 2;
495 div = clk / baud;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400496
Alexander Shiyane97e1552014-02-07 18:16:04 +0400497 if (clk % baud && (div / 16) < 0x8000) {
498 /* Mode x4 */
499 mode = MAX310X_BRGCFG_4XMODE_BIT;
500 clk = port->uartclk * 4;
501 div = clk / baud;
502 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400503 }
504
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400505 max310x_port_write(port, MAX310X_BRGDIVMSB_REG, (div / 16) >> 8);
506 max310x_port_write(port, MAX310X_BRGDIVLSB_REG, div / 16);
507 max310x_port_write(port, MAX310X_BRGCFG_REG, (div % 16) | mode);
Alexander Shiyane97e1552014-02-07 18:16:04 +0400508
509 return DIV_ROUND_CLOSEST(clk, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400510}
511
Bill Pemberton9671f092012-11-19 13:21:50 -0500512static int max310x_update_best_err(unsigned long f, long *besterr)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400513{
514 /* Use baudrate 115200 for calculate error */
515 long err = f % (115200 * 16);
516
517 if ((*besterr < 0) || (*besterr > err)) {
518 *besterr = err;
519 return 0;
520 }
521
522 return 1;
523}
524
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400525static int max310x_set_ref_clk(struct max310x_port *s, unsigned long freq,
526 bool xtal)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400527{
528 unsigned int div, clksrc, pllcfg = 0;
529 long besterr = -1;
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400530 unsigned long fdiv, fmul, bestfreq = freq;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400531
532 /* First, update error without PLL */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400533 max310x_update_best_err(freq, &besterr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400534
535 /* Try all possible PLL dividers */
536 for (div = 1; (div <= 63) && besterr; div++) {
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400537 fdiv = DIV_ROUND_CLOSEST(freq, div);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400538
539 /* Try multiplier 6 */
540 fmul = fdiv * 6;
541 if ((fdiv >= 500000) && (fdiv <= 800000))
542 if (!max310x_update_best_err(fmul, &besterr)) {
543 pllcfg = (0 << 6) | div;
544 bestfreq = fmul;
545 }
546 /* Try multiplier 48 */
547 fmul = fdiv * 48;
548 if ((fdiv >= 850000) && (fdiv <= 1200000))
549 if (!max310x_update_best_err(fmul, &besterr)) {
550 pllcfg = (1 << 6) | div;
551 bestfreq = fmul;
552 }
553 /* Try multiplier 96 */
554 fmul = fdiv * 96;
555 if ((fdiv >= 425000) && (fdiv <= 1000000))
556 if (!max310x_update_best_err(fmul, &besterr)) {
557 pllcfg = (2 << 6) | div;
558 bestfreq = fmul;
559 }
560 /* Try multiplier 144 */
561 fmul = fdiv * 144;
562 if ((fdiv >= 390000) && (fdiv <= 667000))
563 if (!max310x_update_best_err(fmul, &besterr)) {
564 pllcfg = (3 << 6) | div;
565 bestfreq = fmul;
566 }
567 }
568
569 /* Configure clock source */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400570 clksrc = xtal ? MAX310X_CLKSRC_CRYST_BIT : MAX310X_CLKSRC_EXTCLK_BIT;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400571
572 /* Configure PLL */
573 if (pllcfg) {
574 clksrc |= MAX310X_CLKSRC_PLL_BIT;
575 regmap_write(s->regmap, MAX310X_PLLCFG_REG, pllcfg);
576 } else
577 clksrc |= MAX310X_CLKSRC_PLLBYP_BIT;
578
579 regmap_write(s->regmap, MAX310X_CLKSRC_REG, clksrc);
580
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400581 /* Wait for crystal */
Alexander Shiyand3a8a252014-02-10 22:18:31 +0400582 if (pllcfg && xtal)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400583 msleep(10);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400584
585 return (int)bestfreq;
586}
587
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400588static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400589{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400590 unsigned int sts, ch, flag;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400591
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400592 if (unlikely(rxlen >= port->fifosize)) {
593 dev_warn_ratelimited(port->dev,
594 "Port %i: Possible RX FIFO overrun\n",
595 port->line);
596 port->icount.buf_overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400597 /* Ensure sanity of RX level */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400598 rxlen = port->fifosize;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400599 }
600
Alexander Shiyanf6544412012-08-06 19:42:32 +0400601 while (rxlen--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400602 ch = max310x_port_read(port, MAX310X_RHR_REG);
603 sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400604
605 sts &= MAX310X_LSR_RXPAR_BIT | MAX310X_LSR_FRERR_BIT |
606 MAX310X_LSR_RXOVR_BIT | MAX310X_LSR_RXBRK_BIT;
607
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400608 port->icount.rx++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400609 flag = TTY_NORMAL;
610
611 if (unlikely(sts)) {
612 if (sts & MAX310X_LSR_RXBRK_BIT) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400613 port->icount.brk++;
614 if (uart_handle_break(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400615 continue;
616 } else if (sts & MAX310X_LSR_RXPAR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400617 port->icount.parity++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400618 else if (sts & MAX310X_LSR_FRERR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400619 port->icount.frame++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400620 else if (sts & MAX310X_LSR_RXOVR_BIT)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400621 port->icount.overrun++;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400622
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400623 sts &= port->read_status_mask;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400624 if (sts & MAX310X_LSR_RXBRK_BIT)
625 flag = TTY_BREAK;
626 else if (sts & MAX310X_LSR_RXPAR_BIT)
627 flag = TTY_PARITY;
628 else if (sts & MAX310X_LSR_FRERR_BIT)
629 flag = TTY_FRAME;
630 else if (sts & MAX310X_LSR_RXOVR_BIT)
631 flag = TTY_OVERRUN;
632 }
633
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400634 if (uart_handle_sysrq_char(port, ch))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400635 continue;
636
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400637 if (sts & port->ignore_status_mask)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400638 continue;
639
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400640 uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, ch, flag);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400641 }
642
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400643 tty_flip_buffer_push(&port->state->port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400644}
645
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400646static void max310x_handle_tx(struct uart_port *port)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400647{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400648 struct circ_buf *xmit = &port->state->xmit;
649 unsigned int txlen, to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400650
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400651 if (unlikely(port->x_char)) {
652 max310x_port_write(port, MAX310X_THR_REG, port->x_char);
653 port->icount.tx++;
654 port->x_char = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400655 return;
656 }
657
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400658 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Alexander Shiyanf6544412012-08-06 19:42:32 +0400659 return;
660
661 /* Get length of data pending in circular buffer */
662 to_send = uart_circ_chars_pending(xmit);
663 if (likely(to_send)) {
664 /* Limit to size of TX FIFO */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400665 txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
666 txlen = port->fifosize - txlen;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400667 to_send = (to_send > txlen) ? txlen : to_send;
668
Alexander Shiyanf6544412012-08-06 19:42:32 +0400669 /* Add data to send */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400670 port->icount.tx += to_send;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400671 while (to_send--) {
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400672 max310x_port_write(port, MAX310X_THR_REG,
673 xmit->buf[xmit->tail]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400674 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Joe Perchesfc8114722013-10-08 16:14:21 -0700675 }
Alexander Shiyanf6544412012-08-06 19:42:32 +0400676 }
677
678 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400679 uart_write_wakeup(port);
680}
681
682static void max310x_port_irq(struct max310x_port *s, int portno)
683{
684 struct uart_port *port = &s->p[portno].port;
685
686 do {
687 unsigned int ists, lsr, rxlen;
688
689 /* Read IRQ status & RX FIFO level */
690 ists = max310x_port_read(port, MAX310X_IRQSTS_REG);
691 rxlen = max310x_port_read(port, MAX310X_RXFIFOLVL_REG);
692 if (!ists && !rxlen)
693 break;
694
695 if (ists & MAX310X_IRQ_CTS_BIT) {
696 lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
697 uart_handle_cts_change(port,
698 !!(lsr & MAX310X_LSR_CTS_BIT));
699 }
700 if (rxlen)
701 max310x_handle_rx(port, rxlen);
702 if (ists & MAX310X_IRQ_TXEMPTY_BIT) {
703 mutex_lock(&s->mutex);
704 max310x_handle_tx(port);
705 mutex_unlock(&s->mutex);
706 }
707 } while (1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400708}
709
710static irqreturn_t max310x_ist(int irq, void *dev_id)
711{
712 struct max310x_port *s = (struct max310x_port *)dev_id;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400713
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400714 if (s->uart.nr > 1) {
715 do {
716 unsigned int val = ~0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400717
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400718 WARN_ON_ONCE(regmap_read(s->regmap,
719 MAX310X_GLOBALIRQ_REG, &val));
720 val = ((1 << s->uart.nr) - 1) & ~val;
721 if (!val)
722 break;
723 max310x_port_irq(s, fls(val) - 1);
724 } while (1);
725 } else
726 max310x_port_irq(s, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400727
728 return IRQ_HANDLED;
729}
730
731static void max310x_wq_proc(struct work_struct *ws)
732{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400733 struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
734 struct max310x_port *s = dev_get_drvdata(one->port.dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400735
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400736 mutex_lock(&s->mutex);
737 max310x_handle_tx(&one->port);
738 mutex_unlock(&s->mutex);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400739}
740
741static void max310x_start_tx(struct uart_port *port)
742{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400743 struct max310x_one *one = container_of(port, struct max310x_one, port);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400744
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400745 if (!work_pending(&one->tx_work))
746 schedule_work(&one->tx_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400747}
748
749static unsigned int max310x_tx_empty(struct uart_port *port)
750{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400751 unsigned int lvl, sts;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400752
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400753 lvl = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
754 sts = max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400755
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400756 return ((sts & MAX310X_IRQ_TXEMPTY_BIT) && !lvl) ? TIOCSER_TEMT : 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400757}
758
759static unsigned int max310x_get_mctrl(struct uart_port *port)
760{
761 /* DCD and DSR are not wired and CTS/RTS is handled automatically
762 * so just indicate DSR and CAR asserted
763 */
764 return TIOCM_DSR | TIOCM_CAR;
765}
766
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400767static void max310x_md_proc(struct work_struct *ws)
768{
769 struct max310x_one *one = container_of(ws, struct max310x_one, md_work);
770
771 max310x_port_update(&one->port, MAX310X_MODE2_REG,
772 MAX310X_MODE2_LOOPBACK_BIT,
773 (one->port.mctrl & TIOCM_LOOP) ?
774 MAX310X_MODE2_LOOPBACK_BIT : 0);
775}
776
Alexander Shiyanf6544412012-08-06 19:42:32 +0400777static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl)
778{
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +0400779 struct max310x_one *one = container_of(port, struct max310x_one, port);
780
781 schedule_work(&one->md_work);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400782}
783
784static void max310x_break_ctl(struct uart_port *port, int break_state)
785{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400786 max310x_port_update(port, MAX310X_LCR_REG,
787 MAX310X_LCR_TXBREAK_BIT,
788 break_state ? MAX310X_LCR_TXBREAK_BIT : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400789}
790
791static void max310x_set_termios(struct uart_port *port,
792 struct ktermios *termios,
793 struct ktermios *old)
794{
Alexander Shiyanf6544412012-08-06 19:42:32 +0400795 unsigned int lcr, flow = 0;
796 int baud;
797
Alexander Shiyanf6544412012-08-06 19:42:32 +0400798 /* Mask termios capabilities we don't support */
799 termios->c_cflag &= ~CMSPAR;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400800
801 /* Word size */
802 switch (termios->c_cflag & CSIZE) {
803 case CS5:
804 lcr = MAX310X_LCR_WORD_LEN_5;
805 break;
806 case CS6:
807 lcr = MAX310X_LCR_WORD_LEN_6;
808 break;
809 case CS7:
810 lcr = MAX310X_LCR_WORD_LEN_7;
811 break;
812 case CS8:
813 default:
814 lcr = MAX310X_LCR_WORD_LEN_8;
815 break;
816 }
817
818 /* Parity */
819 if (termios->c_cflag & PARENB) {
820 lcr |= MAX310X_LCR_PARITY_BIT;
821 if (!(termios->c_cflag & PARODD))
822 lcr |= MAX310X_LCR_EVENPARITY_BIT;
823 }
824
825 /* Stop bits */
826 if (termios->c_cflag & CSTOPB)
827 lcr |= MAX310X_LCR_STOPLEN_BIT; /* 2 stops */
828
829 /* Update LCR register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400830 max310x_port_write(port, MAX310X_LCR_REG, lcr);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400831
832 /* Set read status mask */
833 port->read_status_mask = MAX310X_LSR_RXOVR_BIT;
834 if (termios->c_iflag & INPCK)
835 port->read_status_mask |= MAX310X_LSR_RXPAR_BIT |
836 MAX310X_LSR_FRERR_BIT;
837 if (termios->c_iflag & (BRKINT | PARMRK))
838 port->read_status_mask |= MAX310X_LSR_RXBRK_BIT;
839
840 /* Set status ignore mask */
841 port->ignore_status_mask = 0;
842 if (termios->c_iflag & IGNBRK)
843 port->ignore_status_mask |= MAX310X_LSR_RXBRK_BIT;
844 if (!(termios->c_cflag & CREAD))
845 port->ignore_status_mask |= MAX310X_LSR_RXPAR_BIT |
846 MAX310X_LSR_RXOVR_BIT |
847 MAX310X_LSR_FRERR_BIT |
848 MAX310X_LSR_RXBRK_BIT;
849
850 /* Configure flow control */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400851 max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]);
852 max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400853 if (termios->c_cflag & CRTSCTS)
854 flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT |
855 MAX310X_FLOWCTRL_AUTORTS_BIT;
856 if (termios->c_iflag & IXON)
857 flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT |
858 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
859 if (termios->c_iflag & IXOFF)
860 flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT |
861 MAX310X_FLOWCTRL_SWFLOWEN_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400862 max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400863
864 /* Get baud rate generator configuration */
865 baud = uart_get_baud_rate(port, termios, old,
866 port->uartclk / 16 / 0xffff,
867 port->uartclk / 4);
868
869 /* Setup baudrate generator */
Alexander Shiyane97e1552014-02-07 18:16:04 +0400870 baud = max310x_set_baud(port, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400871
872 /* Update timeout according to new baud rate */
873 uart_update_timeout(port, termios->c_cflag, baud);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400874}
875
Alexander Shiyan55367c62014-02-10 22:18:34 +0400876static int max310x_ioctl(struct uart_port *port, unsigned int cmd,
877 unsigned long arg)
878{
Alexander Shiyan86a41c42014-02-13 23:12:48 +0400879#if defined(TIOCSRS485) && defined(TIOCGRS485)
Alexander Shiyan55367c62014-02-10 22:18:34 +0400880 struct serial_rs485 rs485;
881 unsigned int val;
882
883 switch (cmd) {
884 case TIOCSRS485:
885 if (copy_from_user(&rs485, (struct serial_rs485 *)arg,
886 sizeof(rs485)))
887 return -EFAULT;
888 if (rs485.delay_rts_before_send > 0x0f ||
889 rs485.delay_rts_after_send > 0x0f)
890 return -ERANGE;
891 val = (rs485.delay_rts_before_send << 4) |
892 rs485.delay_rts_after_send;
893 max310x_port_write(port, MAX310X_HDPIXDELAY_REG, val);
894 if (rs485.flags & SER_RS485_ENABLED) {
895 max310x_port_update(port, MAX310X_MODE1_REG,
896 MAX310X_MODE1_TRNSCVCTRL_BIT,
897 MAX310X_MODE1_TRNSCVCTRL_BIT);
898 max310x_port_update(port, MAX310X_MODE2_REG,
899 MAX310X_MODE2_ECHOSUPR_BIT,
900 MAX310X_MODE2_ECHOSUPR_BIT);
901 } else {
902 max310x_port_update(port, MAX310X_MODE1_REG,
903 MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
904 max310x_port_update(port, MAX310X_MODE2_REG,
905 MAX310X_MODE2_ECHOSUPR_BIT, 0);
906 }
Alexander Shiyan86a41c42014-02-13 23:12:48 +0400907 return 0;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400908 case TIOCGRS485:
909 memset(&rs485, 0, sizeof(rs485));
910 val = max310x_port_read(port, MAX310X_MODE1_REG);
911 rs485.flags = (val & MAX310X_MODE1_TRNSCVCTRL_BIT) ?
912 SER_RS485_ENABLED : 0;
913 rs485.flags |= SER_RS485_RTS_ON_SEND;
914 val = max310x_port_read(port, MAX310X_HDPIXDELAY_REG);
915 rs485.delay_rts_before_send = val >> 4;
916 rs485.delay_rts_after_send = val & 0x0f;
917 if (copy_to_user((struct serial_rs485 *)arg, &rs485,
918 sizeof(rs485)))
919 return -EFAULT;
Alexander Shiyan86a41c42014-02-13 23:12:48 +0400920 return 0;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400921 default:
Alexander Shiyan86a41c42014-02-13 23:12:48 +0400922 break;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400923 }
Alexander Shiyan86a41c42014-02-13 23:12:48 +0400924#endif
Alexander Shiyan55367c62014-02-10 22:18:34 +0400925
Alexander Shiyan86a41c42014-02-13 23:12:48 +0400926 return -ENOIOCTLCMD;
Alexander Shiyan55367c62014-02-10 22:18:34 +0400927}
928
Alexander Shiyanf6544412012-08-06 19:42:32 +0400929static int max310x_startup(struct uart_port *port)
930{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400931 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyan55367c62014-02-10 22:18:34 +0400932 unsigned int val;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400933
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400934 s->devtype->power(port, 1);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400935
Alexander Shiyanf6544412012-08-06 19:42:32 +0400936 /* Configure MODE1 register */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400937 max310x_port_update(port, MAX310X_MODE1_REG,
Alexander Shiyan55367c62014-02-10 22:18:34 +0400938 MAX310X_MODE1_TRNSCVCTRL_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400939
Alexander Shiyan55367c62014-02-10 22:18:34 +0400940 /* Configure MODE2 register & Reset FIFOs*/
941 val = MAX310X_MODE2_RXEMPTINV_BIT | MAX310X_MODE2_FIFORST_BIT;
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400942 max310x_port_write(port, MAX310X_MODE2_REG, val);
943 max310x_port_update(port, MAX310X_MODE2_REG,
944 MAX310X_MODE2_FIFORST_BIT, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400945
946 /* Configure flow control levels */
947 /* Flow control halt level 96, resume level 48 */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400948 max310x_port_write(port, MAX310X_FLOWLVL_REG,
949 MAX310X_FLOWLVL_RES(48) | MAX310X_FLOWLVL_HALT(96));
Alexander Shiyanf6544412012-08-06 19:42:32 +0400950
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400951 /* Clear IRQ status register */
952 max310x_port_read(port, MAX310X_IRQSTS_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400953
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400954 /* Enable RX, TX, CTS change interrupts */
955 val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
956 max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400957
958 return 0;
959}
960
961static void max310x_shutdown(struct uart_port *port)
962{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400963 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400964
965 /* Disable all interrupts */
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400966 max310x_port_write(port, MAX310X_IRQEN_REG, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400967
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400968 s->devtype->power(port, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400969}
970
971static const char *max310x_type(struct uart_port *port)
972{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400973 struct max310x_port *s = dev_get_drvdata(port->dev);
Alexander Shiyanf6544412012-08-06 19:42:32 +0400974
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400975 return (port->type == PORT_MAX310X) ? s->devtype->name : NULL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400976}
977
978static int max310x_request_port(struct uart_port *port)
979{
980 /* Do nothing */
981 return 0;
982}
983
Alexander Shiyanf6544412012-08-06 19:42:32 +0400984static void max310x_config_port(struct uart_port *port, int flags)
985{
986 if (flags & UART_CONFIG_TYPE)
987 port->type = PORT_MAX310X;
988}
989
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400990static int max310x_verify_port(struct uart_port *port, struct serial_struct *s)
Alexander Shiyanf6544412012-08-06 19:42:32 +0400991{
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400992 if ((s->type != PORT_UNKNOWN) && (s->type != PORT_MAX310X))
993 return -EINVAL;
994 if (s->irq != port->irq)
995 return -EINVAL;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400996
Alexander Shiyan10d8b342013-06-29 10:44:17 +0400997 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +0400998}
999
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001000static void max310x_null_void(struct uart_port *port)
1001{
1002 /* Do nothing */
1003}
1004
1005static const struct uart_ops max310x_ops = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001006 .tx_empty = max310x_tx_empty,
1007 .set_mctrl = max310x_set_mctrl,
1008 .get_mctrl = max310x_get_mctrl,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001009 .stop_tx = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001010 .start_tx = max310x_start_tx,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001011 .stop_rx = max310x_null_void,
1012 .enable_ms = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001013 .break_ctl = max310x_break_ctl,
1014 .startup = max310x_startup,
1015 .shutdown = max310x_shutdown,
1016 .set_termios = max310x_set_termios,
1017 .type = max310x_type,
1018 .request_port = max310x_request_port,
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001019 .release_port = max310x_null_void,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001020 .config_port = max310x_config_port,
1021 .verify_port = max310x_verify_port,
Alexander Shiyan55367c62014-02-10 22:18:34 +04001022 .ioctl = max310x_ioctl,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001023};
1024
Alexander Shiyanc2978292013-07-29 19:27:32 +04001025static int __maybe_unused max310x_suspend(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001026{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001027 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001028 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001029
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001030 for (i = 0; i < s->uart.nr; i++) {
1031 uart_suspend_port(&s->uart, &s->p[i].port);
1032 s->devtype->power(&s->p[i].port, 0);
1033 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001034
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001035 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001036}
1037
Alexander Shiyanc2978292013-07-29 19:27:32 +04001038static int __maybe_unused max310x_resume(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001039{
Alexander Shiyanc2978292013-07-29 19:27:32 +04001040 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001041 int i;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001042
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001043 for (i = 0; i < s->uart.nr; i++) {
1044 s->devtype->power(&s->p[i].port, 1);
1045 uart_resume_port(&s->uart, &s->p[i].port);
1046 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001047
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001048 return 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001049}
1050
Alexander Shiyan27027a72014-02-10 22:18:30 +04001051static SIMPLE_DEV_PM_OPS(max310x_pm_ops, max310x_suspend, max310x_resume);
1052
Alexander Shiyanf6544412012-08-06 19:42:32 +04001053#ifdef CONFIG_GPIOLIB
1054static int max310x_gpio_get(struct gpio_chip *chip, unsigned offset)
1055{
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001056 unsigned int val;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001057 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001058 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001059
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001060 val = max310x_port_read(port, MAX310X_GPIODATA_REG);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001061
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001062 return !!((val >> 4) & (1 << (offset % 4)));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001063}
1064
1065static void max310x_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1066{
1067 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001068 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001069
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001070 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1071 value ? 1 << (offset % 4) : 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001072}
1073
1074static int max310x_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1075{
1076 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001077 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001078
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001079 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4), 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001080
1081 return 0;
1082}
1083
1084static int max310x_gpio_direction_output(struct gpio_chip *chip,
1085 unsigned offset, int value)
1086{
1087 struct max310x_port *s = container_of(chip, struct max310x_port, gpio);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001088 struct uart_port *port = &s->p[offset / 4].port;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001089
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001090 max310x_port_update(port, MAX310X_GPIODATA_REG, 1 << (offset % 4),
1091 value ? 1 << (offset % 4) : 0);
1092 max310x_port_update(port, MAX310X_GPIOCFG_REG, 1 << (offset % 4),
1093 1 << (offset % 4));
Alexander Shiyanf6544412012-08-06 19:42:32 +04001094
1095 return 0;
1096}
1097#endif
1098
Alexander Shiyan27027a72014-02-10 22:18:30 +04001099static int max310x_probe(struct device *dev, struct max310x_devtype *devtype,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001100 struct regmap *regmap, int irq, unsigned long flags)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001101{
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001102 int i, ret, fmin, fmax, freq, uartclk;
1103 struct clk *clk_osc, *clk_xtal;
1104 struct max310x_port *s;
1105 bool xtal = false;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001106
Alexander Shiyan27027a72014-02-10 22:18:30 +04001107 if (IS_ERR(regmap))
1108 return PTR_ERR(regmap);
1109
Alexander Shiyanf6544412012-08-06 19:42:32 +04001110 /* Alloc port structure */
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001111 s = devm_kzalloc(dev, sizeof(*s) +
1112 sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001113 if (!s) {
1114 dev_err(dev, "Error allocating port structure\n");
1115 return -ENOMEM;
1116 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001117
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001118 clk_osc = devm_clk_get(dev, "osc");
1119 clk_xtal = devm_clk_get(dev, "xtal");
1120 if (!IS_ERR(clk_osc)) {
1121 s->clk = clk_osc;
1122 fmin = 500000;
1123 fmax = 35000000;
1124 } else if (!IS_ERR(clk_xtal)) {
1125 s->clk = clk_xtal;
1126 fmin = 1000000;
1127 fmax = 4000000;
1128 xtal = true;
1129 } else if (PTR_ERR(clk_osc) == -EPROBE_DEFER ||
1130 PTR_ERR(clk_xtal) == -EPROBE_DEFER) {
1131 return -EPROBE_DEFER;
1132 } else {
1133 dev_err(dev, "Cannot get clock\n");
1134 return -EINVAL;
1135 }
1136
1137 ret = clk_prepare_enable(s->clk);
1138 if (ret)
1139 return ret;
1140
1141 freq = clk_get_rate(s->clk);
1142 /* Check frequency limits */
1143 if (freq < fmin || freq > fmax) {
1144 ret = -ERANGE;
1145 goto out_clk;
1146 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001147
Alexander Shiyan27027a72014-02-10 22:18:30 +04001148 s->regmap = regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001149 s->devtype = devtype;
1150 dev_set_drvdata(dev, s);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001151
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001152 /* Check device to ensure we are talking to what we expect */
1153 ret = devtype->detect(dev);
1154 if (ret)
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001155 goto out_clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001156
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001157 for (i = 0; i < devtype->nr; i++) {
1158 unsigned int offs = i << 5;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001159
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001160 /* Reset port */
1161 regmap_write(s->regmap, MAX310X_MODE2_REG + offs,
1162 MAX310X_MODE2_RST_BIT);
1163 /* Clear port reset */
1164 regmap_write(s->regmap, MAX310X_MODE2_REG + offs, 0);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001165
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001166 /* Wait for port startup */
1167 do {
1168 regmap_read(s->regmap,
1169 MAX310X_BRGDIVLSB_REG + offs, &ret);
1170 } while (ret != 0x01);
1171
1172 regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs,
1173 MAX310X_MODE1_AUTOSLEEP_BIT,
1174 MAX310X_MODE1_AUTOSLEEP_BIT);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001175 }
1176
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001177 uartclk = max310x_set_ref_clk(s, freq, xtal);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001178 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
1179
Alexander Shiyanf6544412012-08-06 19:42:32 +04001180 /* Register UART driver */
1181 s->uart.owner = THIS_MODULE;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001182 s->uart.dev_name = "ttyMAX";
1183 s->uart.major = MAX310X_MAJOR;
1184 s->uart.minor = MAX310X_MINOR;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001185 s->uart.nr = devtype->nr;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001186 ret = uart_register_driver(&s->uart);
1187 if (ret) {
1188 dev_err(dev, "Registering UART driver failed\n");
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001189 goto out_clk;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001190 }
1191
Alexander Shiyandba29a22014-02-10 22:18:32 +04001192#ifdef CONFIG_GPIOLIB
1193 /* Setup GPIO cotroller */
1194 s->gpio.owner = THIS_MODULE;
1195 s->gpio.dev = dev;
1196 s->gpio.label = dev_name(dev);
1197 s->gpio.direction_input = max310x_gpio_direction_input;
1198 s->gpio.get = max310x_gpio_get;
1199 s->gpio.direction_output= max310x_gpio_direction_output;
1200 s->gpio.set = max310x_gpio_set;
1201 s->gpio.base = -1;
1202 s->gpio.ngpio = devtype->nr * 4;
1203 s->gpio.can_sleep = 1;
1204 ret = gpiochip_add(&s->gpio);
1205 if (ret)
1206 goto out_uart;
1207#endif
1208
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001209 mutex_init(&s->mutex);
1210
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001211 for (i = 0; i < devtype->nr; i++) {
1212 /* Initialize port data */
1213 s->p[i].port.line = i;
1214 s->p[i].port.dev = dev;
1215 s->p[i].port.irq = irq;
1216 s->p[i].port.type = PORT_MAX310X;
1217 s->p[i].port.fifosize = MAX310X_FIFO_SIZE;
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001218 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001219 s->p[i].port.iotype = UPIO_PORT;
1220 s->p[i].port.iobase = i * 0x20;
1221 s->p[i].port.membase = (void __iomem *)~0;
1222 s->p[i].port.uartclk = uartclk;
1223 s->p[i].port.ops = &max310x_ops;
1224 /* Disable all interrupts */
1225 max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0);
1226 /* Clear IRQ status register */
1227 max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG);
1228 /* Enable IRQ pin */
1229 max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG,
1230 MAX310X_MODE1_IRQSEL_BIT,
1231 MAX310X_MODE1_IRQSEL_BIT);
1232 /* Initialize queue for start TX */
1233 INIT_WORK(&s->p[i].tx_work, max310x_wq_proc);
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001234 /* Initialize queue for changing mode */
1235 INIT_WORK(&s->p[i].md_work, max310x_md_proc);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001236 /* Register port */
1237 uart_add_one_port(&s->uart, &s->p[i].port);
1238 /* Go to suspend mode */
1239 devtype->power(&s->p[i].port, 0);
1240 }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001241
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001242 /* Setup interrupt */
1243 ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
Alexander Shiyan58afc902014-02-10 22:18:36 +04001244 IRQF_ONESHOT | flags, dev_name(dev), s);
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001245 if (!ret)
1246 return 0;
1247
1248 dev_err(dev, "Unable to reguest IRQ %i\n", irq);
Alexander Shiyandba29a22014-02-10 22:18:32 +04001249
Alexander Shiyan0fbae882014-02-10 22:18:33 +04001250 mutex_destroy(&s->mutex);
1251
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001252#ifdef CONFIG_GPIOLIB
Alexander Shiyandba29a22014-02-10 22:18:32 +04001253 WARN_ON(gpiochip_remove(&s->gpio));
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001254
Alexander Shiyandba29a22014-02-10 22:18:32 +04001255out_uart:
Alexander Shiyand4f6b412014-02-13 22:08:29 +04001256#endif
Alexander Shiyandba29a22014-02-10 22:18:32 +04001257 uart_unregister_driver(&s->uart);
1258
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001259out_clk:
1260 clk_disable_unprepare(s->clk);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001261
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001262 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001263}
1264
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001265static int max310x_remove(struct device *dev)
Alexander Shiyanf6544412012-08-06 19:42:32 +04001266{
Alexander Shiyanf6544412012-08-06 19:42:32 +04001267 struct max310x_port *s = dev_get_drvdata(dev);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001268 int i, ret = 0;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001269
Alexander Shiyandba29a22014-02-10 22:18:32 +04001270#ifdef CONFIG_GPIOLIB
1271 ret = gpiochip_remove(&s->gpio);
1272 if (ret)
1273 return ret;
1274#endif
1275
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001276 for (i = 0; i < s->uart.nr; i++) {
1277 cancel_work_sync(&s->p[i].tx_work);
Alexander Shiyane7b8a3c2014-02-07 18:16:07 +04001278 cancel_work_sync(&s->p[i].md_work);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001279 uart_remove_one_port(&s->uart, &s->p[i].port);
1280 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 Shiyanf6544412012-08-06 19:42:32 +04001284 uart_unregister_driver(&s->uart);
Alexander Shiyand3a8a252014-02-10 22:18:31 +04001285 clk_disable_unprepare(s->clk);
Alexander Shiyanf6544412012-08-06 19:42:32 +04001286
Emil Goode23e7c6a2012-08-18 18:12:48 +02001287 return ret;
Alexander Shiyanf6544412012-08-06 19:42:32 +04001288}
1289
Alexander Shiyan58afc902014-02-10 22:18:36 +04001290static const struct of_device_id __maybe_unused max310x_dt_ids[] = {
1291 { .compatible = "maxim,max3107", .data = &max3107_devtype, },
1292 { .compatible = "maxim,max3108", .data = &max3108_devtype, },
1293 { .compatible = "maxim,max3109", .data = &max3109_devtype, },
1294 { .compatible = "maxim,max14830", .data = &max14830_devtype },
1295 { }
1296};
1297MODULE_DEVICE_TABLE(of, max310x_dt_ids);
1298
Alexander Shiyan27027a72014-02-10 22:18:30 +04001299static struct regmap_config regcfg = {
1300 .reg_bits = 8,
1301 .val_bits = 8,
1302 .write_flag_mask = 0x80,
1303 .cache_type = REGCACHE_RBTREE,
1304 .writeable_reg = max310x_reg_writeable,
1305 .volatile_reg = max310x_reg_volatile,
1306 .precious_reg = max310x_reg_precious,
1307};
1308
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001309#ifdef CONFIG_SPI_MASTER
1310static int max310x_spi_probe(struct spi_device *spi)
1311{
Alexander Shiyan58afc902014-02-10 22:18:36 +04001312 struct max310x_devtype *devtype;
1313 unsigned long flags = 0;
Alexander Shiyan27027a72014-02-10 22:18:30 +04001314 struct regmap *regmap;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001315 int ret;
1316
1317 /* Setup SPI bus */
1318 spi->bits_per_word = 8;
1319 spi->mode = spi->mode ? : SPI_MODE_0;
1320 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
1321 ret = spi_setup(spi);
Alexander Shiyan27027a72014-02-10 22:18:30 +04001322 if (ret)
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001323 return ret;
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001324
Alexander Shiyan58afc902014-02-10 22:18:36 +04001325 if (spi->dev.of_node) {
1326 const struct of_device_id *of_id =
1327 of_match_device(max310x_dt_ids, &spi->dev);
1328
1329 devtype = (struct max310x_devtype *)of_id->data;
1330 } else {
1331 const struct spi_device_id *id_entry = spi_get_device_id(spi);
1332
1333 devtype = (struct max310x_devtype *)id_entry->driver_data;
1334 flags = IRQF_TRIGGER_FALLING;
1335 }
1336
Alexander Shiyan27027a72014-02-10 22:18:30 +04001337 regcfg.max_register = devtype->nr * 0x20 - 1;
1338 regmap = devm_regmap_init_spi(spi, &regcfg);
1339
Alexander Shiyan58afc902014-02-10 22:18:36 +04001340 return max310x_probe(&spi->dev, devtype, regmap, spi->irq, flags);
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001341}
1342
1343static int max310x_spi_remove(struct spi_device *spi)
1344{
1345 return max310x_remove(&spi->dev);
1346}
1347
Alexander Shiyanf6544412012-08-06 19:42:32 +04001348static const struct spi_device_id max310x_id_table[] = {
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001349 { "max3107", (kernel_ulong_t)&max3107_devtype, },
1350 { "max3108", (kernel_ulong_t)&max3108_devtype, },
Alexander Shiyan21fc5092013-06-29 10:44:18 +04001351 { "max3109", (kernel_ulong_t)&max3109_devtype, },
Alexander Shiyan003236d2013-06-29 10:44:19 +04001352 { "max14830", (kernel_ulong_t)&max14830_devtype, },
Axel Lin1838b8c2012-11-04 23:34:18 +08001353 { }
Alexander Shiyanf6544412012-08-06 19:42:32 +04001354};
1355MODULE_DEVICE_TABLE(spi, max310x_id_table);
1356
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001357static struct spi_driver max310x_uart_driver = {
Alexander Shiyanf6544412012-08-06 19:42:32 +04001358 .driver = {
Alexander Shiyan58afc902014-02-10 22:18:36 +04001359 .name = MAX310X_NAME,
1360 .owner = THIS_MODULE,
1361 .of_match_table = of_match_ptr(max310x_dt_ids),
1362 .pm = &max310x_pm_ops,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001363 },
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001364 .probe = max310x_spi_probe,
1365 .remove = max310x_spi_remove,
Alexander Shiyanf6544412012-08-06 19:42:32 +04001366 .id_table = max310x_id_table,
1367};
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001368module_spi_driver(max310x_uart_driver);
1369#endif
Alexander Shiyanf6544412012-08-06 19:42:32 +04001370
Alexander Shiyan10d8b342013-06-29 10:44:17 +04001371MODULE_LICENSE("GPL");
Alexander Shiyanf6544412012-08-06 19:42:32 +04001372MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1373MODULE_DESCRIPTION("MAX310X serial driver");