1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2ab4382d2SGreg Kroah-Hartman /*
3ab4382d2SGreg Kroah-Hartman *
4ab4382d2SGreg Kroah-Hartman * Copyright (C) 2008 Christian Pellegrin <chripell@evolware.org>
5ab4382d2SGreg Kroah-Hartman *
6ab4382d2SGreg Kroah-Hartman * Notes: the MAX3100 doesn't provide an interrupt on CTS so we have
7ab4382d2SGreg Kroah-Hartman * to use polling for flow control. TX empty IRQ is unusable, since
8ab4382d2SGreg Kroah-Hartman * writing conf clears FIFO buffer and we cannot have this interrupt
9ab4382d2SGreg Kroah-Hartman * always asking us for attention.
10ab4382d2SGreg Kroah-Hartman *
11ab4382d2SGreg Kroah-Hartman * Example platform data:
12ab4382d2SGreg Kroah-Hartman
13ab4382d2SGreg Kroah-Hartman static struct plat_max3100 max3100_plat_data = {
14ab4382d2SGreg Kroah-Hartman .loopback = 0,
15ab4382d2SGreg Kroah-Hartman .crystal = 0,
16ab4382d2SGreg Kroah-Hartman .poll_time = 100,
17ab4382d2SGreg Kroah-Hartman };
18ab4382d2SGreg Kroah-Hartman
19ab4382d2SGreg Kroah-Hartman static struct spi_board_info spi_board_info[] = {
20ab4382d2SGreg Kroah-Hartman {
21ab4382d2SGreg Kroah-Hartman .modalias = "max3100",
22ab4382d2SGreg Kroah-Hartman .platform_data = &max3100_plat_data,
23ab4382d2SGreg Kroah-Hartman .irq = IRQ_EINT12,
24ab4382d2SGreg Kroah-Hartman .max_speed_hz = 5*1000*1000,
25ab4382d2SGreg Kroah-Hartman .chip_select = 0,
26ab4382d2SGreg Kroah-Hartman },
27ab4382d2SGreg Kroah-Hartman };
28ab4382d2SGreg Kroah-Hartman
29ab4382d2SGreg Kroah-Hartman * The initial minor number is 209 in the low-density serial port:
30ab4382d2SGreg Kroah-Hartman * mknod /dev/ttyMAX0 c 204 209
31ab4382d2SGreg Kroah-Hartman */
32ab4382d2SGreg Kroah-Hartman
33ab4382d2SGreg Kroah-Hartman #define MAX3100_MAJOR 204
34ab4382d2SGreg Kroah-Hartman #define MAX3100_MINOR 209
35ab4382d2SGreg Kroah-Hartman /* 4 MAX3100s should be enough for everyone */
36ab4382d2SGreg Kroah-Hartman #define MAX_MAX3100 4
37ab4382d2SGreg Kroah-Hartman
38ab4382d2SGreg Kroah-Hartman #include <linux/delay.h>
39ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
40ab4382d2SGreg Kroah-Hartman #include <linux/device.h>
41578b9ce0SPaul Gortmaker #include <linux/module.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
43ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
44ab4382d2SGreg Kroah-Hartman #include <linux/spi/spi.h>
45ab4382d2SGreg Kroah-Hartman #include <linux/freezer.h>
46ee160a38SJiri Slaby #include <linux/tty.h>
47ee160a38SJiri Slaby #include <linux/tty_flip.h>
48*749603afSAndy Shevchenko #include <linux/types.h>
49*749603afSAndy Shevchenko
50*749603afSAndy Shevchenko #include <asm/unaligned.h>
51ab4382d2SGreg Kroah-Hartman
52ab4382d2SGreg Kroah-Hartman #include <linux/serial_max3100.h>
53ab4382d2SGreg Kroah-Hartman
54ab4382d2SGreg Kroah-Hartman #define MAX3100_C (1<<14)
55ab4382d2SGreg Kroah-Hartman #define MAX3100_D (0<<14)
56ab4382d2SGreg Kroah-Hartman #define MAX3100_W (1<<15)
57ab4382d2SGreg Kroah-Hartman #define MAX3100_RX (0<<15)
58ab4382d2SGreg Kroah-Hartman
59ab4382d2SGreg Kroah-Hartman #define MAX3100_WC (MAX3100_W | MAX3100_C)
60ab4382d2SGreg Kroah-Hartman #define MAX3100_RC (MAX3100_RX | MAX3100_C)
61ab4382d2SGreg Kroah-Hartman #define MAX3100_WD (MAX3100_W | MAX3100_D)
62ab4382d2SGreg Kroah-Hartman #define MAX3100_RD (MAX3100_RX | MAX3100_D)
63ab4382d2SGreg Kroah-Hartman #define MAX3100_CMD (3 << 14)
64ab4382d2SGreg Kroah-Hartman
65ab4382d2SGreg Kroah-Hartman #define MAX3100_T (1<<14)
66ab4382d2SGreg Kroah-Hartman #define MAX3100_R (1<<15)
67ab4382d2SGreg Kroah-Hartman
68ab4382d2SGreg Kroah-Hartman #define MAX3100_FEN (1<<13)
69ab4382d2SGreg Kroah-Hartman #define MAX3100_SHDN (1<<12)
70ab4382d2SGreg Kroah-Hartman #define MAX3100_TM (1<<11)
71ab4382d2SGreg Kroah-Hartman #define MAX3100_RM (1<<10)
72ab4382d2SGreg Kroah-Hartman #define MAX3100_PM (1<<9)
73ab4382d2SGreg Kroah-Hartman #define MAX3100_RAM (1<<8)
74ab4382d2SGreg Kroah-Hartman #define MAX3100_IR (1<<7)
75ab4382d2SGreg Kroah-Hartman #define MAX3100_ST (1<<6)
76ab4382d2SGreg Kroah-Hartman #define MAX3100_PE (1<<5)
77ab4382d2SGreg Kroah-Hartman #define MAX3100_L (1<<4)
78ab4382d2SGreg Kroah-Hartman #define MAX3100_BAUD (0xf)
79ab4382d2SGreg Kroah-Hartman
80ab4382d2SGreg Kroah-Hartman #define MAX3100_TE (1<<10)
81ab4382d2SGreg Kroah-Hartman #define MAX3100_RAFE (1<<10)
82ab4382d2SGreg Kroah-Hartman #define MAX3100_RTS (1<<9)
83ab4382d2SGreg Kroah-Hartman #define MAX3100_CTS (1<<9)
84ab4382d2SGreg Kroah-Hartman #define MAX3100_PT (1<<8)
85ab4382d2SGreg Kroah-Hartman #define MAX3100_DATA (0xff)
86ab4382d2SGreg Kroah-Hartman
87ab4382d2SGreg Kroah-Hartman #define MAX3100_RT (MAX3100_R | MAX3100_T)
88ab4382d2SGreg Kroah-Hartman #define MAX3100_RTC (MAX3100_RT | MAX3100_CTS | MAX3100_RAFE)
89ab4382d2SGreg Kroah-Hartman
90ab4382d2SGreg Kroah-Hartman /* the following simulate a status reg for ignore_status_mask */
91ab4382d2SGreg Kroah-Hartman #define MAX3100_STATUS_PE 1
92ab4382d2SGreg Kroah-Hartman #define MAX3100_STATUS_FE 2
93ab4382d2SGreg Kroah-Hartman #define MAX3100_STATUS_OE 4
94ab4382d2SGreg Kroah-Hartman
95ab4382d2SGreg Kroah-Hartman struct max3100_port {
96ab4382d2SGreg Kroah-Hartman struct uart_port port;
97ab4382d2SGreg Kroah-Hartman struct spi_device *spi;
98ab4382d2SGreg Kroah-Hartman
99ab4382d2SGreg Kroah-Hartman int cts; /* last CTS received for flow ctrl */
100ab4382d2SGreg Kroah-Hartman int tx_empty; /* last TX empty bit */
101ab4382d2SGreg Kroah-Hartman
102ab4382d2SGreg Kroah-Hartman spinlock_t conf_lock; /* shared data */
103ab4382d2SGreg Kroah-Hartman int conf_commit; /* need to make changes */
104ab4382d2SGreg Kroah-Hartman int conf; /* configuration for the MAX31000
105ab4382d2SGreg Kroah-Hartman * (bits 0-7, bits 8-11 are irqs) */
106ab4382d2SGreg Kroah-Hartman int rts_commit; /* need to change rts */
107ab4382d2SGreg Kroah-Hartman int rts; /* rts status */
108ab4382d2SGreg Kroah-Hartman int baud; /* current baud rate */
109ab4382d2SGreg Kroah-Hartman
110ab4382d2SGreg Kroah-Hartman int parity; /* keeps track if we should send parity */
111ab4382d2SGreg Kroah-Hartman #define MAX3100_PARITY_ON 1
112ab4382d2SGreg Kroah-Hartman #define MAX3100_PARITY_ODD 2
113ab4382d2SGreg Kroah-Hartman #define MAX3100_7BIT 4
114ab4382d2SGreg Kroah-Hartman int rx_enabled; /* if we should rx chars */
115ab4382d2SGreg Kroah-Hartman
116ab4382d2SGreg Kroah-Hartman int irq; /* irq assigned to the max3100 */
117ab4382d2SGreg Kroah-Hartman
118ab4382d2SGreg Kroah-Hartman int minor; /* minor number */
119ab4382d2SGreg Kroah-Hartman int crystal; /* 1 if 3.6864Mhz crystal 0 for 1.8432 */
120ab4382d2SGreg Kroah-Hartman int loopback; /* 1 if we are in loopback mode */
121ab4382d2SGreg Kroah-Hartman
122ab4382d2SGreg Kroah-Hartman /* for handling irqs: need workqueue since we do spi_sync */
123ab4382d2SGreg Kroah-Hartman struct workqueue_struct *workqueue;
124ab4382d2SGreg Kroah-Hartman struct work_struct work;
125ab4382d2SGreg Kroah-Hartman /* set to 1 to make the workhandler exit as soon as possible */
126ab4382d2SGreg Kroah-Hartman int force_end_work;
127ab4382d2SGreg Kroah-Hartman /* need to know we are suspending to avoid deadlock on workqueue */
128ab4382d2SGreg Kroah-Hartman int suspending;
129ab4382d2SGreg Kroah-Hartman
130ab4382d2SGreg Kroah-Hartman /* hook for suspending MAX3100 via dedicated pin */
131ab4382d2SGreg Kroah-Hartman void (*max3100_hw_suspend) (int suspend);
132ab4382d2SGreg Kroah-Hartman
133ab4382d2SGreg Kroah-Hartman /* poll time (in ms) for ctrl lines */
134ab4382d2SGreg Kroah-Hartman int poll_time;
135ab4382d2SGreg Kroah-Hartman /* and its timer */
136ab4382d2SGreg Kroah-Hartman struct timer_list timer;
137ab4382d2SGreg Kroah-Hartman };
138ab4382d2SGreg Kroah-Hartman
139ab4382d2SGreg Kroah-Hartman static struct max3100_port *max3100s[MAX_MAX3100]; /* the chips */
140ab4382d2SGreg Kroah-Hartman static DEFINE_MUTEX(max3100s_lock); /* race on probe */
141ab4382d2SGreg Kroah-Hartman
max3100_do_parity(struct max3100_port * s,u16 c)142ab4382d2SGreg Kroah-Hartman static int max3100_do_parity(struct max3100_port *s, u16 c)
143ab4382d2SGreg Kroah-Hartman {
144ab4382d2SGreg Kroah-Hartman int parity;
145ab4382d2SGreg Kroah-Hartman
146ab4382d2SGreg Kroah-Hartman if (s->parity & MAX3100_PARITY_ODD)
147ab4382d2SGreg Kroah-Hartman parity = 1;
148ab4382d2SGreg Kroah-Hartman else
149ab4382d2SGreg Kroah-Hartman parity = 0;
150ab4382d2SGreg Kroah-Hartman
151ab4382d2SGreg Kroah-Hartman if (s->parity & MAX3100_7BIT)
152ab4382d2SGreg Kroah-Hartman c &= 0x7f;
153ab4382d2SGreg Kroah-Hartman else
154ab4382d2SGreg Kroah-Hartman c &= 0xff;
155ab4382d2SGreg Kroah-Hartman
156ab4382d2SGreg Kroah-Hartman parity = parity ^ (hweight8(c) & 1);
157ab4382d2SGreg Kroah-Hartman return parity;
158ab4382d2SGreg Kroah-Hartman }
159ab4382d2SGreg Kroah-Hartman
max3100_check_parity(struct max3100_port * s,u16 c)160ab4382d2SGreg Kroah-Hartman static int max3100_check_parity(struct max3100_port *s, u16 c)
161ab4382d2SGreg Kroah-Hartman {
162ab4382d2SGreg Kroah-Hartman return max3100_do_parity(s, c) == ((c >> 8) & 1);
163ab4382d2SGreg Kroah-Hartman }
164ab4382d2SGreg Kroah-Hartman
max3100_calc_parity(struct max3100_port * s,u16 * c)165ab4382d2SGreg Kroah-Hartman static void max3100_calc_parity(struct max3100_port *s, u16 *c)
166ab4382d2SGreg Kroah-Hartman {
167ab4382d2SGreg Kroah-Hartman if (s->parity & MAX3100_7BIT)
168ab4382d2SGreg Kroah-Hartman *c &= 0x7f;
169ab4382d2SGreg Kroah-Hartman else
170ab4382d2SGreg Kroah-Hartman *c &= 0xff;
171ab4382d2SGreg Kroah-Hartman
172ab4382d2SGreg Kroah-Hartman if (s->parity & MAX3100_PARITY_ON)
173ab4382d2SGreg Kroah-Hartman *c |= max3100_do_parity(s, *c) << 8;
174ab4382d2SGreg Kroah-Hartman }
175ab4382d2SGreg Kroah-Hartman
176ab4382d2SGreg Kroah-Hartman static void max3100_work(struct work_struct *w);
177ab4382d2SGreg Kroah-Hartman
max3100_dowork(struct max3100_port * s)178ab4382d2SGreg Kroah-Hartman static void max3100_dowork(struct max3100_port *s)
179ab4382d2SGreg Kroah-Hartman {
1807998005bSTejun Heo if (!s->force_end_work && !freezing(current) && !s->suspending)
181ab4382d2SGreg Kroah-Hartman queue_work(s->workqueue, &s->work);
182ab4382d2SGreg Kroah-Hartman }
183ab4382d2SGreg Kroah-Hartman
max3100_timeout(struct timer_list * t)184e99e88a9SKees Cook static void max3100_timeout(struct timer_list *t)
185ab4382d2SGreg Kroah-Hartman {
186e99e88a9SKees Cook struct max3100_port *s = from_timer(s, t, timer);
187ab4382d2SGreg Kroah-Hartman
188ab4382d2SGreg Kroah-Hartman if (s->port.state) {
189ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
190ab4382d2SGreg Kroah-Hartman mod_timer(&s->timer, jiffies + s->poll_time);
191ab4382d2SGreg Kroah-Hartman }
192ab4382d2SGreg Kroah-Hartman }
193ab4382d2SGreg Kroah-Hartman
max3100_sr(struct max3100_port * s,u16 tx,u16 * rx)194ab4382d2SGreg Kroah-Hartman static int max3100_sr(struct max3100_port *s, u16 tx, u16 *rx)
195ab4382d2SGreg Kroah-Hartman {
196ab4382d2SGreg Kroah-Hartman struct spi_message message;
197*749603afSAndy Shevchenko __be16 etx, erx;
198ab4382d2SGreg Kroah-Hartman int status;
199ab4382d2SGreg Kroah-Hartman struct spi_transfer tran = {
200ab4382d2SGreg Kroah-Hartman .tx_buf = &etx,
201ab4382d2SGreg Kroah-Hartman .rx_buf = &erx,
202ab4382d2SGreg Kroah-Hartman .len = 2,
203ab4382d2SGreg Kroah-Hartman };
204ab4382d2SGreg Kroah-Hartman
205ab4382d2SGreg Kroah-Hartman etx = cpu_to_be16(tx);
206ab4382d2SGreg Kroah-Hartman spi_message_init(&message);
207ab4382d2SGreg Kroah-Hartman spi_message_add_tail(&tran, &message);
208ab4382d2SGreg Kroah-Hartman status = spi_sync(s->spi, &message);
209ab4382d2SGreg Kroah-Hartman if (status) {
210ab4382d2SGreg Kroah-Hartman dev_warn(&s->spi->dev, "error while calling spi_sync\n");
211ab4382d2SGreg Kroah-Hartman return -EIO;
212ab4382d2SGreg Kroah-Hartman }
213ab4382d2SGreg Kroah-Hartman *rx = be16_to_cpu(erx);
214ab4382d2SGreg Kroah-Hartman s->tx_empty = (*rx & MAX3100_T) > 0;
215ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%04x - %04x\n", tx, *rx);
216ab4382d2SGreg Kroah-Hartman return 0;
217ab4382d2SGreg Kroah-Hartman }
218ab4382d2SGreg Kroah-Hartman
max3100_handlerx_unlocked(struct max3100_port * s,u16 rx)21993df2fbaSAndy Shevchenko static int max3100_handlerx_unlocked(struct max3100_port *s, u16 rx)
220ab4382d2SGreg Kroah-Hartman {
221fd2b55f8SJiri Slaby unsigned int status = 0;
222ab4382d2SGreg Kroah-Hartman int ret = 0, cts;
223fd2b55f8SJiri Slaby u8 ch, flg;
224ab4382d2SGreg Kroah-Hartman
225ab4382d2SGreg Kroah-Hartman if (rx & MAX3100_R && s->rx_enabled) {
226ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
227ab4382d2SGreg Kroah-Hartman ch = rx & (s->parity & MAX3100_7BIT ? 0x7f : 0xff);
228ab4382d2SGreg Kroah-Hartman if (rx & MAX3100_RAFE) {
229ab4382d2SGreg Kroah-Hartman s->port.icount.frame++;
230ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME;
231ab4382d2SGreg Kroah-Hartman status |= MAX3100_STATUS_FE;
232ab4382d2SGreg Kroah-Hartman } else {
233ab4382d2SGreg Kroah-Hartman if (s->parity & MAX3100_PARITY_ON) {
234ab4382d2SGreg Kroah-Hartman if (max3100_check_parity(s, rx)) {
235ab4382d2SGreg Kroah-Hartman s->port.icount.rx++;
236ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL;
237ab4382d2SGreg Kroah-Hartman } else {
238ab4382d2SGreg Kroah-Hartman s->port.icount.parity++;
239ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY;
240ab4382d2SGreg Kroah-Hartman status |= MAX3100_STATUS_PE;
241ab4382d2SGreg Kroah-Hartman }
242ab4382d2SGreg Kroah-Hartman } else {
243ab4382d2SGreg Kroah-Hartman s->port.icount.rx++;
244ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL;
245ab4382d2SGreg Kroah-Hartman }
246ab4382d2SGreg Kroah-Hartman }
247ab4382d2SGreg Kroah-Hartman uart_insert_char(&s->port, status, MAX3100_STATUS_OE, ch, flg);
248ab4382d2SGreg Kroah-Hartman ret = 1;
249ab4382d2SGreg Kroah-Hartman }
250ab4382d2SGreg Kroah-Hartman
251ab4382d2SGreg Kroah-Hartman cts = (rx & MAX3100_CTS) > 0;
252ab4382d2SGreg Kroah-Hartman if (s->cts != cts) {
253ab4382d2SGreg Kroah-Hartman s->cts = cts;
254968d6457SIlpo Järvinen uart_handle_cts_change(&s->port, cts);
255ab4382d2SGreg Kroah-Hartman }
256ab4382d2SGreg Kroah-Hartman
257ab4382d2SGreg Kroah-Hartman return ret;
258ab4382d2SGreg Kroah-Hartman }
259ab4382d2SGreg Kroah-Hartman
max3100_handlerx(struct max3100_port * s,u16 rx)26093df2fbaSAndy Shevchenko static int max3100_handlerx(struct max3100_port *s, u16 rx)
26193df2fbaSAndy Shevchenko {
26293df2fbaSAndy Shevchenko unsigned long flags;
26393df2fbaSAndy Shevchenko int ret;
26493df2fbaSAndy Shevchenko
26593df2fbaSAndy Shevchenko uart_port_lock_irqsave(&s->port, &flags);
26693df2fbaSAndy Shevchenko ret = max3100_handlerx_unlocked(s, rx);
26793df2fbaSAndy Shevchenko uart_port_unlock_irqrestore(&s->port, flags);
26893df2fbaSAndy Shevchenko return ret;
26993df2fbaSAndy Shevchenko }
27093df2fbaSAndy Shevchenko
max3100_work(struct work_struct * w)271ab4382d2SGreg Kroah-Hartman static void max3100_work(struct work_struct *w)
272ab4382d2SGreg Kroah-Hartman {
273ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(w, struct max3100_port, work);
274ab4382d2SGreg Kroah-Hartman int rxchars;
275ab4382d2SGreg Kroah-Hartman u16 tx, rx;
2765de15cc2SColin Ian King int conf, cconf, crts;
277ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &s->port.state->xmit;
278ab4382d2SGreg Kroah-Hartman
279ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
280ab4382d2SGreg Kroah-Hartman
281ab4382d2SGreg Kroah-Hartman rxchars = 0;
282ab4382d2SGreg Kroah-Hartman do {
283ab4382d2SGreg Kroah-Hartman spin_lock(&s->conf_lock);
284ab4382d2SGreg Kroah-Hartman conf = s->conf;
285ab4382d2SGreg Kroah-Hartman cconf = s->conf_commit;
286ab4382d2SGreg Kroah-Hartman s->conf_commit = 0;
287ab4382d2SGreg Kroah-Hartman crts = s->rts_commit;
288ab4382d2SGreg Kroah-Hartman s->rts_commit = 0;
289ab4382d2SGreg Kroah-Hartman spin_unlock(&s->conf_lock);
290ab4382d2SGreg Kroah-Hartman if (cconf)
291ab4382d2SGreg Kroah-Hartman max3100_sr(s, MAX3100_WC | conf, &rx);
292ab4382d2SGreg Kroah-Hartman if (crts) {
293ab4382d2SGreg Kroah-Hartman max3100_sr(s, MAX3100_WD | MAX3100_TE |
294ab4382d2SGreg Kroah-Hartman (s->rts ? MAX3100_RTS : 0), &rx);
295ab4382d2SGreg Kroah-Hartman rxchars += max3100_handlerx(s, rx);
296ab4382d2SGreg Kroah-Hartman }
297ab4382d2SGreg Kroah-Hartman
298ab4382d2SGreg Kroah-Hartman max3100_sr(s, MAX3100_RD, &rx);
299ab4382d2SGreg Kroah-Hartman rxchars += max3100_handlerx(s, rx);
300ab4382d2SGreg Kroah-Hartman
301ab4382d2SGreg Kroah-Hartman if (rx & MAX3100_T) {
302ab4382d2SGreg Kroah-Hartman tx = 0xffff;
303ab4382d2SGreg Kroah-Hartman if (s->port.x_char) {
304ab4382d2SGreg Kroah-Hartman tx = s->port.x_char;
305ab4382d2SGreg Kroah-Hartman s->port.icount.tx++;
306ab4382d2SGreg Kroah-Hartman s->port.x_char = 0;
307ab4382d2SGreg Kroah-Hartman } else if (!uart_circ_empty(xmit) &&
308ab4382d2SGreg Kroah-Hartman !uart_tx_stopped(&s->port)) {
309ab4382d2SGreg Kroah-Hartman tx = xmit->buf[xmit->tail];
310502b13ccSIlpo Järvinen uart_xmit_advance(&s->port, 1);
311ab4382d2SGreg Kroah-Hartman }
312ab4382d2SGreg Kroah-Hartman if (tx != 0xffff) {
313ab4382d2SGreg Kroah-Hartman max3100_calc_parity(s, &tx);
314ab4382d2SGreg Kroah-Hartman tx |= MAX3100_WD | (s->rts ? MAX3100_RTS : 0);
315ab4382d2SGreg Kroah-Hartman max3100_sr(s, tx, &rx);
316ab4382d2SGreg Kroah-Hartman rxchars += max3100_handlerx(s, rx);
317ab4382d2SGreg Kroah-Hartman }
318ab4382d2SGreg Kroah-Hartman }
319ab4382d2SGreg Kroah-Hartman
3202e124b4aSJiri Slaby if (rxchars > 16) {
3212e124b4aSJiri Slaby tty_flip_buffer_push(&s->port.state->port);
322ab4382d2SGreg Kroah-Hartman rxchars = 0;
323ab4382d2SGreg Kroah-Hartman }
324ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
325ab4382d2SGreg Kroah-Hartman uart_write_wakeup(&s->port);
326ab4382d2SGreg Kroah-Hartman
327ab4382d2SGreg Kroah-Hartman } while (!s->force_end_work &&
328ab4382d2SGreg Kroah-Hartman !freezing(current) &&
329ab4382d2SGreg Kroah-Hartman ((rx & MAX3100_R) ||
330ab4382d2SGreg Kroah-Hartman (!uart_circ_empty(xmit) &&
331ab4382d2SGreg Kroah-Hartman !uart_tx_stopped(&s->port))));
332ab4382d2SGreg Kroah-Hartman
3332e124b4aSJiri Slaby if (rxchars > 0)
3342e124b4aSJiri Slaby tty_flip_buffer_push(&s->port.state->port);
335ab4382d2SGreg Kroah-Hartman }
336ab4382d2SGreg Kroah-Hartman
max3100_irq(int irqno,void * dev_id)337ab4382d2SGreg Kroah-Hartman static irqreturn_t max3100_irq(int irqno, void *dev_id)
338ab4382d2SGreg Kroah-Hartman {
339ab4382d2SGreg Kroah-Hartman struct max3100_port *s = dev_id;
340ab4382d2SGreg Kroah-Hartman
341ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
342ab4382d2SGreg Kroah-Hartman
343ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
344ab4382d2SGreg Kroah-Hartman return IRQ_HANDLED;
345ab4382d2SGreg Kroah-Hartman }
346ab4382d2SGreg Kroah-Hartman
max3100_enable_ms(struct uart_port * port)347ab4382d2SGreg Kroah-Hartman static void max3100_enable_ms(struct uart_port *port)
348ab4382d2SGreg Kroah-Hartman {
349ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
350ab4382d2SGreg Kroah-Hartman struct max3100_port,
351ab4382d2SGreg Kroah-Hartman port);
352ab4382d2SGreg Kroah-Hartman
353ab4382d2SGreg Kroah-Hartman if (s->poll_time > 0)
354ab4382d2SGreg Kroah-Hartman mod_timer(&s->timer, jiffies);
355ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
356ab4382d2SGreg Kroah-Hartman }
357ab4382d2SGreg Kroah-Hartman
max3100_start_tx(struct uart_port * port)358ab4382d2SGreg Kroah-Hartman static void max3100_start_tx(struct uart_port *port)
359ab4382d2SGreg Kroah-Hartman {
360ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
361ab4382d2SGreg Kroah-Hartman struct max3100_port,
362ab4382d2SGreg Kroah-Hartman port);
363ab4382d2SGreg Kroah-Hartman
364ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
365ab4382d2SGreg Kroah-Hartman
366ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
367ab4382d2SGreg Kroah-Hartman }
368ab4382d2SGreg Kroah-Hartman
max3100_stop_rx(struct uart_port * port)369ab4382d2SGreg Kroah-Hartman static void max3100_stop_rx(struct uart_port *port)
370ab4382d2SGreg Kroah-Hartman {
371ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
372ab4382d2SGreg Kroah-Hartman struct max3100_port,
373ab4382d2SGreg Kroah-Hartman port);
374ab4382d2SGreg Kroah-Hartman
375ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
376ab4382d2SGreg Kroah-Hartman
377ab4382d2SGreg Kroah-Hartman s->rx_enabled = 0;
378ab4382d2SGreg Kroah-Hartman spin_lock(&s->conf_lock);
379ab4382d2SGreg Kroah-Hartman s->conf &= ~MAX3100_RM;
380ab4382d2SGreg Kroah-Hartman s->conf_commit = 1;
381ab4382d2SGreg Kroah-Hartman spin_unlock(&s->conf_lock);
382ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
383ab4382d2SGreg Kroah-Hartman }
384ab4382d2SGreg Kroah-Hartman
max3100_tx_empty(struct uart_port * port)385ab4382d2SGreg Kroah-Hartman static unsigned int max3100_tx_empty(struct uart_port *port)
386ab4382d2SGreg Kroah-Hartman {
387ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
388ab4382d2SGreg Kroah-Hartman struct max3100_port,
389ab4382d2SGreg Kroah-Hartman port);
390ab4382d2SGreg Kroah-Hartman
391ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
392ab4382d2SGreg Kroah-Hartman
393ab4382d2SGreg Kroah-Hartman /* may not be truly up-to-date */
394ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
395ab4382d2SGreg Kroah-Hartman return s->tx_empty;
396ab4382d2SGreg Kroah-Hartman }
397ab4382d2SGreg Kroah-Hartman
max3100_get_mctrl(struct uart_port * port)398ab4382d2SGreg Kroah-Hartman static unsigned int max3100_get_mctrl(struct uart_port *port)
399ab4382d2SGreg Kroah-Hartman {
400ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
401ab4382d2SGreg Kroah-Hartman struct max3100_port,
402ab4382d2SGreg Kroah-Hartman port);
403ab4382d2SGreg Kroah-Hartman
404ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
405ab4382d2SGreg Kroah-Hartman
406ab4382d2SGreg Kroah-Hartman /* may not be truly up-to-date */
407ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
408ab4382d2SGreg Kroah-Hartman /* always assert DCD and DSR since these lines are not wired */
409ab4382d2SGreg Kroah-Hartman return (s->cts ? TIOCM_CTS : 0) | TIOCM_DSR | TIOCM_CAR;
410ab4382d2SGreg Kroah-Hartman }
411ab4382d2SGreg Kroah-Hartman
max3100_set_mctrl(struct uart_port * port,unsigned int mctrl)412ab4382d2SGreg Kroah-Hartman static void max3100_set_mctrl(struct uart_port *port, unsigned int mctrl)
413ab4382d2SGreg Kroah-Hartman {
414ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
415ab4382d2SGreg Kroah-Hartman struct max3100_port,
416ab4382d2SGreg Kroah-Hartman port);
417ab4382d2SGreg Kroah-Hartman int rts;
418ab4382d2SGreg Kroah-Hartman
419ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
420ab4382d2SGreg Kroah-Hartman
421ab4382d2SGreg Kroah-Hartman rts = (mctrl & TIOCM_RTS) > 0;
422ab4382d2SGreg Kroah-Hartman
423ab4382d2SGreg Kroah-Hartman spin_lock(&s->conf_lock);
424ab4382d2SGreg Kroah-Hartman if (s->rts != rts) {
425ab4382d2SGreg Kroah-Hartman s->rts = rts;
426ab4382d2SGreg Kroah-Hartman s->rts_commit = 1;
427ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
428ab4382d2SGreg Kroah-Hartman }
429ab4382d2SGreg Kroah-Hartman spin_unlock(&s->conf_lock);
430ab4382d2SGreg Kroah-Hartman }
431ab4382d2SGreg Kroah-Hartman
432ab4382d2SGreg Kroah-Hartman static void
max3100_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)433ab4382d2SGreg Kroah-Hartman max3100_set_termios(struct uart_port *port, struct ktermios *termios,
434bec5b814SIlpo Järvinen const struct ktermios *old)
435ab4382d2SGreg Kroah-Hartman {
436ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
437ab4382d2SGreg Kroah-Hartman struct max3100_port,
438ab4382d2SGreg Kroah-Hartman port);
439ab4382d2SGreg Kroah-Hartman int baud = 0;
440ab4382d2SGreg Kroah-Hartman unsigned cflag;
441ab4382d2SGreg Kroah-Hartman u32 param_new, param_mask, parity = 0;
442ab4382d2SGreg Kroah-Hartman
443ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
444ab4382d2SGreg Kroah-Hartman
445ab4382d2SGreg Kroah-Hartman cflag = termios->c_cflag;
446ab4382d2SGreg Kroah-Hartman param_mask = 0;
447ab4382d2SGreg Kroah-Hartman
448ab4382d2SGreg Kroah-Hartman baud = tty_termios_baud_rate(termios);
449ab4382d2SGreg Kroah-Hartman param_new = s->conf & MAX3100_BAUD;
450ab4382d2SGreg Kroah-Hartman switch (baud) {
451ab4382d2SGreg Kroah-Hartman case 300:
452ab4382d2SGreg Kroah-Hartman if (s->crystal)
453ab4382d2SGreg Kroah-Hartman baud = s->baud;
454ab4382d2SGreg Kroah-Hartman else
455ab4382d2SGreg Kroah-Hartman param_new = 15;
456ab4382d2SGreg Kroah-Hartman break;
457ab4382d2SGreg Kroah-Hartman case 600:
458ab4382d2SGreg Kroah-Hartman param_new = 14 + s->crystal;
459ab4382d2SGreg Kroah-Hartman break;
460ab4382d2SGreg Kroah-Hartman case 1200:
461ab4382d2SGreg Kroah-Hartman param_new = 13 + s->crystal;
462ab4382d2SGreg Kroah-Hartman break;
463ab4382d2SGreg Kroah-Hartman case 2400:
464ab4382d2SGreg Kroah-Hartman param_new = 12 + s->crystal;
465ab4382d2SGreg Kroah-Hartman break;
466ab4382d2SGreg Kroah-Hartman case 4800:
467ab4382d2SGreg Kroah-Hartman param_new = 11 + s->crystal;
468ab4382d2SGreg Kroah-Hartman break;
469ab4382d2SGreg Kroah-Hartman case 9600:
470ab4382d2SGreg Kroah-Hartman param_new = 10 + s->crystal;
471ab4382d2SGreg Kroah-Hartman break;
472ab4382d2SGreg Kroah-Hartman case 19200:
473ab4382d2SGreg Kroah-Hartman param_new = 9 + s->crystal;
474ab4382d2SGreg Kroah-Hartman break;
475ab4382d2SGreg Kroah-Hartman case 38400:
476ab4382d2SGreg Kroah-Hartman param_new = 8 + s->crystal;
477ab4382d2SGreg Kroah-Hartman break;
478ab4382d2SGreg Kroah-Hartman case 57600:
479ab4382d2SGreg Kroah-Hartman param_new = 1 + s->crystal;
480ab4382d2SGreg Kroah-Hartman break;
481ab4382d2SGreg Kroah-Hartman case 115200:
482ab4382d2SGreg Kroah-Hartman param_new = 0 + s->crystal;
483ab4382d2SGreg Kroah-Hartman break;
484ab4382d2SGreg Kroah-Hartman case 230400:
485ab4382d2SGreg Kroah-Hartman if (s->crystal)
486ab4382d2SGreg Kroah-Hartman param_new = 0;
487ab4382d2SGreg Kroah-Hartman else
488ab4382d2SGreg Kroah-Hartman baud = s->baud;
489ab4382d2SGreg Kroah-Hartman break;
490ab4382d2SGreg Kroah-Hartman default:
491ab4382d2SGreg Kroah-Hartman baud = s->baud;
492ab4382d2SGreg Kroah-Hartman }
493ab4382d2SGreg Kroah-Hartman tty_termios_encode_baud_rate(termios, baud, baud);
494ab4382d2SGreg Kroah-Hartman s->baud = baud;
495ab4382d2SGreg Kroah-Hartman param_mask |= MAX3100_BAUD;
496ab4382d2SGreg Kroah-Hartman
497ab4382d2SGreg Kroah-Hartman if ((cflag & CSIZE) == CS8) {
498ab4382d2SGreg Kroah-Hartman param_new &= ~MAX3100_L;
499ab4382d2SGreg Kroah-Hartman parity &= ~MAX3100_7BIT;
500ab4382d2SGreg Kroah-Hartman } else {
501ab4382d2SGreg Kroah-Hartman param_new |= MAX3100_L;
502ab4382d2SGreg Kroah-Hartman parity |= MAX3100_7BIT;
503ab4382d2SGreg Kroah-Hartman cflag = (cflag & ~CSIZE) | CS7;
504ab4382d2SGreg Kroah-Hartman }
505ab4382d2SGreg Kroah-Hartman param_mask |= MAX3100_L;
506ab4382d2SGreg Kroah-Hartman
507ab4382d2SGreg Kroah-Hartman if (cflag & CSTOPB)
508ab4382d2SGreg Kroah-Hartman param_new |= MAX3100_ST;
509ab4382d2SGreg Kroah-Hartman else
510ab4382d2SGreg Kroah-Hartman param_new &= ~MAX3100_ST;
511ab4382d2SGreg Kroah-Hartman param_mask |= MAX3100_ST;
512ab4382d2SGreg Kroah-Hartman
513ab4382d2SGreg Kroah-Hartman if (cflag & PARENB) {
514ab4382d2SGreg Kroah-Hartman param_new |= MAX3100_PE;
515ab4382d2SGreg Kroah-Hartman parity |= MAX3100_PARITY_ON;
516ab4382d2SGreg Kroah-Hartman } else {
517ab4382d2SGreg Kroah-Hartman param_new &= ~MAX3100_PE;
518ab4382d2SGreg Kroah-Hartman parity &= ~MAX3100_PARITY_ON;
519ab4382d2SGreg Kroah-Hartman }
520ab4382d2SGreg Kroah-Hartman param_mask |= MAX3100_PE;
521ab4382d2SGreg Kroah-Hartman
522ab4382d2SGreg Kroah-Hartman if (cflag & PARODD)
523ab4382d2SGreg Kroah-Hartman parity |= MAX3100_PARITY_ODD;
524ab4382d2SGreg Kroah-Hartman else
525ab4382d2SGreg Kroah-Hartman parity &= ~MAX3100_PARITY_ODD;
526ab4382d2SGreg Kroah-Hartman
527ab4382d2SGreg Kroah-Hartman /* mask termios capabilities we don't support */
528ab4382d2SGreg Kroah-Hartman cflag &= ~CMSPAR;
529ab4382d2SGreg Kroah-Hartman termios->c_cflag = cflag;
530ab4382d2SGreg Kroah-Hartman
531ab4382d2SGreg Kroah-Hartman s->port.ignore_status_mask = 0;
532ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR)
533ab4382d2SGreg Kroah-Hartman s->port.ignore_status_mask |=
534ab4382d2SGreg Kroah-Hartman MAX3100_STATUS_PE | MAX3100_STATUS_FE |
535ab4382d2SGreg Kroah-Hartman MAX3100_STATUS_OE;
536ab4382d2SGreg Kroah-Hartman
537ab4382d2SGreg Kroah-Hartman if (s->poll_time > 0)
538ab4382d2SGreg Kroah-Hartman del_timer_sync(&s->timer);
539ab4382d2SGreg Kroah-Hartman
540ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud);
541ab4382d2SGreg Kroah-Hartman
542ab4382d2SGreg Kroah-Hartman spin_lock(&s->conf_lock);
543ab4382d2SGreg Kroah-Hartman s->conf = (s->conf & ~param_mask) | (param_new & param_mask);
544ab4382d2SGreg Kroah-Hartman s->conf_commit = 1;
545ab4382d2SGreg Kroah-Hartman s->parity = parity;
546ab4382d2SGreg Kroah-Hartman spin_unlock(&s->conf_lock);
547ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
548ab4382d2SGreg Kroah-Hartman
549ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(&s->port, termios->c_cflag))
550ab4382d2SGreg Kroah-Hartman max3100_enable_ms(&s->port);
551ab4382d2SGreg Kroah-Hartman }
552ab4382d2SGreg Kroah-Hartman
max3100_shutdown(struct uart_port * port)553ab4382d2SGreg Kroah-Hartman static void max3100_shutdown(struct uart_port *port)
554ab4382d2SGreg Kroah-Hartman {
555ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
556ab4382d2SGreg Kroah-Hartman struct max3100_port,
557ab4382d2SGreg Kroah-Hartman port);
558ab4382d2SGreg Kroah-Hartman
559ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
560ab4382d2SGreg Kroah-Hartman
561ab4382d2SGreg Kroah-Hartman if (s->suspending)
562ab4382d2SGreg Kroah-Hartman return;
563ab4382d2SGreg Kroah-Hartman
564ab4382d2SGreg Kroah-Hartman s->force_end_work = 1;
565ab4382d2SGreg Kroah-Hartman
566ab4382d2SGreg Kroah-Hartman if (s->poll_time > 0)
567ab4382d2SGreg Kroah-Hartman del_timer_sync(&s->timer);
568ab4382d2SGreg Kroah-Hartman
569ab4382d2SGreg Kroah-Hartman if (s->workqueue) {
570ab4382d2SGreg Kroah-Hartman destroy_workqueue(s->workqueue);
571ab4382d2SGreg Kroah-Hartman s->workqueue = NULL;
572ab4382d2SGreg Kroah-Hartman }
573ab4382d2SGreg Kroah-Hartman if (s->irq)
574ab4382d2SGreg Kroah-Hartman free_irq(s->irq, s);
575ab4382d2SGreg Kroah-Hartman
576ab4382d2SGreg Kroah-Hartman /* set shutdown mode to save power */
577ab4382d2SGreg Kroah-Hartman if (s->max3100_hw_suspend)
578ab4382d2SGreg Kroah-Hartman s->max3100_hw_suspend(1);
579ab4382d2SGreg Kroah-Hartman else {
580ab4382d2SGreg Kroah-Hartman u16 tx, rx;
581ab4382d2SGreg Kroah-Hartman
582ab4382d2SGreg Kroah-Hartman tx = MAX3100_WC | MAX3100_SHDN;
583ab4382d2SGreg Kroah-Hartman max3100_sr(s, tx, &rx);
584ab4382d2SGreg Kroah-Hartman }
585ab4382d2SGreg Kroah-Hartman }
586ab4382d2SGreg Kroah-Hartman
max3100_startup(struct uart_port * port)587ab4382d2SGreg Kroah-Hartman static int max3100_startup(struct uart_port *port)
588ab4382d2SGreg Kroah-Hartman {
589ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
590ab4382d2SGreg Kroah-Hartman struct max3100_port,
591ab4382d2SGreg Kroah-Hartman port);
592ab4382d2SGreg Kroah-Hartman char b[12];
593ab4382d2SGreg Kroah-Hartman
594ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
595ab4382d2SGreg Kroah-Hartman
596ab4382d2SGreg Kroah-Hartman s->conf = MAX3100_RM;
597ab4382d2SGreg Kroah-Hartman s->baud = s->crystal ? 230400 : 115200;
598ab4382d2SGreg Kroah-Hartman s->rx_enabled = 1;
599ab4382d2SGreg Kroah-Hartman
600ab4382d2SGreg Kroah-Hartman if (s->suspending)
601ab4382d2SGreg Kroah-Hartman return 0;
602ab4382d2SGreg Kroah-Hartman
603ab4382d2SGreg Kroah-Hartman s->force_end_work = 0;
604ab4382d2SGreg Kroah-Hartman s->parity = 0;
605ab4382d2SGreg Kroah-Hartman s->rts = 0;
606ab4382d2SGreg Kroah-Hartman
607ab4382d2SGreg Kroah-Hartman sprintf(b, "max3100-%d", s->minor);
60858a69cb4STejun Heo s->workqueue = create_freezable_workqueue(b);
609ab4382d2SGreg Kroah-Hartman if (!s->workqueue) {
610ab4382d2SGreg Kroah-Hartman dev_warn(&s->spi->dev, "cannot create workqueue\n");
611ab4382d2SGreg Kroah-Hartman return -EBUSY;
612ab4382d2SGreg Kroah-Hartman }
613ab4382d2SGreg Kroah-Hartman INIT_WORK(&s->work, max3100_work);
614ab4382d2SGreg Kroah-Hartman
615ab4382d2SGreg Kroah-Hartman if (request_irq(s->irq, max3100_irq,
616ab4382d2SGreg Kroah-Hartman IRQF_TRIGGER_FALLING, "max3100", s) < 0) {
617ab4382d2SGreg Kroah-Hartman dev_warn(&s->spi->dev, "cannot allocate irq %d\n", s->irq);
618ab4382d2SGreg Kroah-Hartman s->irq = 0;
619ab4382d2SGreg Kroah-Hartman destroy_workqueue(s->workqueue);
620ab4382d2SGreg Kroah-Hartman s->workqueue = NULL;
621ab4382d2SGreg Kroah-Hartman return -EBUSY;
622ab4382d2SGreg Kroah-Hartman }
623ab4382d2SGreg Kroah-Hartman
624ab4382d2SGreg Kroah-Hartman if (s->loopback) {
625ab4382d2SGreg Kroah-Hartman u16 tx, rx;
626ab4382d2SGreg Kroah-Hartman tx = 0x4001;
627ab4382d2SGreg Kroah-Hartman max3100_sr(s, tx, &rx);
628ab4382d2SGreg Kroah-Hartman }
629ab4382d2SGreg Kroah-Hartman
630ab4382d2SGreg Kroah-Hartman if (s->max3100_hw_suspend)
631ab4382d2SGreg Kroah-Hartman s->max3100_hw_suspend(0);
632ab4382d2SGreg Kroah-Hartman s->conf_commit = 1;
633ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
634ab4382d2SGreg Kroah-Hartman /* wait for clock to settle */
635ab4382d2SGreg Kroah-Hartman msleep(50);
636ab4382d2SGreg Kroah-Hartman
637ab4382d2SGreg Kroah-Hartman max3100_enable_ms(&s->port);
638ab4382d2SGreg Kroah-Hartman
639ab4382d2SGreg Kroah-Hartman return 0;
640ab4382d2SGreg Kroah-Hartman }
641ab4382d2SGreg Kroah-Hartman
max3100_type(struct uart_port * port)642ab4382d2SGreg Kroah-Hartman static const char *max3100_type(struct uart_port *port)
643ab4382d2SGreg Kroah-Hartman {
644ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
645ab4382d2SGreg Kroah-Hartman struct max3100_port,
646ab4382d2SGreg Kroah-Hartman port);
647ab4382d2SGreg Kroah-Hartman
648ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
649ab4382d2SGreg Kroah-Hartman
650ab4382d2SGreg Kroah-Hartman return s->port.type == PORT_MAX3100 ? "MAX3100" : NULL;
651ab4382d2SGreg Kroah-Hartman }
652ab4382d2SGreg Kroah-Hartman
max3100_release_port(struct uart_port * port)653ab4382d2SGreg Kroah-Hartman static void max3100_release_port(struct uart_port *port)
654ab4382d2SGreg Kroah-Hartman {
655ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
656ab4382d2SGreg Kroah-Hartman struct max3100_port,
657ab4382d2SGreg Kroah-Hartman port);
658ab4382d2SGreg Kroah-Hartman
659ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
660ab4382d2SGreg Kroah-Hartman }
661ab4382d2SGreg Kroah-Hartman
max3100_config_port(struct uart_port * port,int flags)662ab4382d2SGreg Kroah-Hartman static void max3100_config_port(struct uart_port *port, int flags)
663ab4382d2SGreg Kroah-Hartman {
664ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
665ab4382d2SGreg Kroah-Hartman struct max3100_port,
666ab4382d2SGreg Kroah-Hartman port);
667ab4382d2SGreg Kroah-Hartman
668ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
669ab4382d2SGreg Kroah-Hartman
670ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE)
671ab4382d2SGreg Kroah-Hartman s->port.type = PORT_MAX3100;
672ab4382d2SGreg Kroah-Hartman }
673ab4382d2SGreg Kroah-Hartman
max3100_verify_port(struct uart_port * port,struct serial_struct * ser)674ab4382d2SGreg Kroah-Hartman static int max3100_verify_port(struct uart_port *port,
675ab4382d2SGreg Kroah-Hartman struct serial_struct *ser)
676ab4382d2SGreg Kroah-Hartman {
677ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
678ab4382d2SGreg Kroah-Hartman struct max3100_port,
679ab4382d2SGreg Kroah-Hartman port);
680ab4382d2SGreg Kroah-Hartman int ret = -EINVAL;
681ab4382d2SGreg Kroah-Hartman
682ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
683ab4382d2SGreg Kroah-Hartman
684ab4382d2SGreg Kroah-Hartman if (ser->type == PORT_UNKNOWN || ser->type == PORT_MAX3100)
685ab4382d2SGreg Kroah-Hartman ret = 0;
686ab4382d2SGreg Kroah-Hartman return ret;
687ab4382d2SGreg Kroah-Hartman }
688ab4382d2SGreg Kroah-Hartman
max3100_stop_tx(struct uart_port * port)689ab4382d2SGreg Kroah-Hartman static void max3100_stop_tx(struct uart_port *port)
690ab4382d2SGreg Kroah-Hartman {
691ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
692ab4382d2SGreg Kroah-Hartman struct max3100_port,
693ab4382d2SGreg Kroah-Hartman port);
694ab4382d2SGreg Kroah-Hartman
695ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
696ab4382d2SGreg Kroah-Hartman }
697ab4382d2SGreg Kroah-Hartman
max3100_request_port(struct uart_port * port)698ab4382d2SGreg Kroah-Hartman static int max3100_request_port(struct uart_port *port)
699ab4382d2SGreg Kroah-Hartman {
700ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
701ab4382d2SGreg Kroah-Hartman struct max3100_port,
702ab4382d2SGreg Kroah-Hartman port);
703ab4382d2SGreg Kroah-Hartman
704ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
705ab4382d2SGreg Kroah-Hartman return 0;
706ab4382d2SGreg Kroah-Hartman }
707ab4382d2SGreg Kroah-Hartman
max3100_break_ctl(struct uart_port * port,int break_state)708ab4382d2SGreg Kroah-Hartman static void max3100_break_ctl(struct uart_port *port, int break_state)
709ab4382d2SGreg Kroah-Hartman {
710ab4382d2SGreg Kroah-Hartman struct max3100_port *s = container_of(port,
711ab4382d2SGreg Kroah-Hartman struct max3100_port,
712ab4382d2SGreg Kroah-Hartman port);
713ab4382d2SGreg Kroah-Hartman
714ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
715ab4382d2SGreg Kroah-Hartman }
716ab4382d2SGreg Kroah-Hartman
717069a47e5SJulia Lawall static const struct uart_ops max3100_ops = {
718ab4382d2SGreg Kroah-Hartman .tx_empty = max3100_tx_empty,
719ab4382d2SGreg Kroah-Hartman .set_mctrl = max3100_set_mctrl,
720ab4382d2SGreg Kroah-Hartman .get_mctrl = max3100_get_mctrl,
721ab4382d2SGreg Kroah-Hartman .stop_tx = max3100_stop_tx,
722ab4382d2SGreg Kroah-Hartman .start_tx = max3100_start_tx,
723ab4382d2SGreg Kroah-Hartman .stop_rx = max3100_stop_rx,
724ab4382d2SGreg Kroah-Hartman .enable_ms = max3100_enable_ms,
725ab4382d2SGreg Kroah-Hartman .break_ctl = max3100_break_ctl,
726ab4382d2SGreg Kroah-Hartman .startup = max3100_startup,
727ab4382d2SGreg Kroah-Hartman .shutdown = max3100_shutdown,
728ab4382d2SGreg Kroah-Hartman .set_termios = max3100_set_termios,
729ab4382d2SGreg Kroah-Hartman .type = max3100_type,
730ab4382d2SGreg Kroah-Hartman .release_port = max3100_release_port,
731ab4382d2SGreg Kroah-Hartman .request_port = max3100_request_port,
732ab4382d2SGreg Kroah-Hartman .config_port = max3100_config_port,
733ab4382d2SGreg Kroah-Hartman .verify_port = max3100_verify_port,
734ab4382d2SGreg Kroah-Hartman };
735ab4382d2SGreg Kroah-Hartman
736ab4382d2SGreg Kroah-Hartman static struct uart_driver max3100_uart_driver = {
737ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE,
738ab4382d2SGreg Kroah-Hartman .driver_name = "ttyMAX",
739ab4382d2SGreg Kroah-Hartman .dev_name = "ttyMAX",
740ab4382d2SGreg Kroah-Hartman .major = MAX3100_MAJOR,
741ab4382d2SGreg Kroah-Hartman .minor = MAX3100_MINOR,
742ab4382d2SGreg Kroah-Hartman .nr = MAX_MAX3100,
743ab4382d2SGreg Kroah-Hartman };
744ab4382d2SGreg Kroah-Hartman static int uart_driver_registered;
745ab4382d2SGreg Kroah-Hartman
max3100_probe(struct spi_device * spi)7469671f099SBill Pemberton static int max3100_probe(struct spi_device *spi)
747ab4382d2SGreg Kroah-Hartman {
748ab4382d2SGreg Kroah-Hartman int i, retval;
749ab4382d2SGreg Kroah-Hartman struct plat_max3100 *pdata;
750ab4382d2SGreg Kroah-Hartman u16 tx, rx;
751ab4382d2SGreg Kroah-Hartman
752ab4382d2SGreg Kroah-Hartman mutex_lock(&max3100s_lock);
753ab4382d2SGreg Kroah-Hartman
754ab4382d2SGreg Kroah-Hartman if (!uart_driver_registered) {
755ab4382d2SGreg Kroah-Hartman retval = uart_register_driver(&max3100_uart_driver);
756ab4382d2SGreg Kroah-Hartman if (retval) {
757ab4382d2SGreg Kroah-Hartman printk(KERN_ERR "Couldn't register max3100 uart driver\n");
758ab4382d2SGreg Kroah-Hartman mutex_unlock(&max3100s_lock);
759ab4382d2SGreg Kroah-Hartman return retval;
760ab4382d2SGreg Kroah-Hartman }
761e8a10089SAndy Shevchenko
762e8a10089SAndy Shevchenko uart_driver_registered = 1;
763ab4382d2SGreg Kroah-Hartman }
764ab4382d2SGreg Kroah-Hartman
765ab4382d2SGreg Kroah-Hartman for (i = 0; i < MAX_MAX3100; i++)
766ab4382d2SGreg Kroah-Hartman if (!max3100s[i])
767ab4382d2SGreg Kroah-Hartman break;
768ab4382d2SGreg Kroah-Hartman if (i == MAX_MAX3100) {
769ab4382d2SGreg Kroah-Hartman dev_warn(&spi->dev, "too many MAX3100 chips\n");
770ab4382d2SGreg Kroah-Hartman mutex_unlock(&max3100s_lock);
771ab4382d2SGreg Kroah-Hartman return -ENOMEM;
772ab4382d2SGreg Kroah-Hartman }
773ab4382d2SGreg Kroah-Hartman
774ab4382d2SGreg Kroah-Hartman max3100s[i] = kzalloc(sizeof(struct max3100_port), GFP_KERNEL);
775ab4382d2SGreg Kroah-Hartman if (!max3100s[i]) {
776ab4382d2SGreg Kroah-Hartman dev_warn(&spi->dev,
777ab4382d2SGreg Kroah-Hartman "kmalloc for max3100 structure %d failed!\n", i);
778ab4382d2SGreg Kroah-Hartman mutex_unlock(&max3100s_lock);
779ab4382d2SGreg Kroah-Hartman return -ENOMEM;
780ab4382d2SGreg Kroah-Hartman }
781ab4382d2SGreg Kroah-Hartman max3100s[i]->spi = spi;
782ab4382d2SGreg Kroah-Hartman max3100s[i]->irq = spi->irq;
783ab4382d2SGreg Kroah-Hartman spin_lock_init(&max3100s[i]->conf_lock);
78431bdfc64SJingoo Han spi_set_drvdata(spi, max3100s[i]);
785574de559SJingoo Han pdata = dev_get_platdata(&spi->dev);
786ab4382d2SGreg Kroah-Hartman max3100s[i]->crystal = pdata->crystal;
787ab4382d2SGreg Kroah-Hartman max3100s[i]->loopback = pdata->loopback;
788c89b7370SNicholas Mc Guire max3100s[i]->poll_time = msecs_to_jiffies(pdata->poll_time);
789ab4382d2SGreg Kroah-Hartman if (pdata->poll_time > 0 && max3100s[i]->poll_time == 0)
790ab4382d2SGreg Kroah-Hartman max3100s[i]->poll_time = 1;
791ab4382d2SGreg Kroah-Hartman max3100s[i]->max3100_hw_suspend = pdata->max3100_hw_suspend;
792ab4382d2SGreg Kroah-Hartman max3100s[i]->minor = i;
793e99e88a9SKees Cook timer_setup(&max3100s[i]->timer, max3100_timeout, 0);
794ab4382d2SGreg Kroah-Hartman
795ab4382d2SGreg Kroah-Hartman dev_dbg(&spi->dev, "%s: adding port %d\n", __func__, i);
796ab4382d2SGreg Kroah-Hartman max3100s[i]->port.irq = max3100s[i]->irq;
797ab4382d2SGreg Kroah-Hartman max3100s[i]->port.uartclk = max3100s[i]->crystal ? 3686400 : 1843200;
798ab4382d2SGreg Kroah-Hartman max3100s[i]->port.fifosize = 16;
799ab4382d2SGreg Kroah-Hartman max3100s[i]->port.ops = &max3100_ops;
800ab4382d2SGreg Kroah-Hartman max3100s[i]->port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
801ab4382d2SGreg Kroah-Hartman max3100s[i]->port.line = i;
802ab4382d2SGreg Kroah-Hartman max3100s[i]->port.type = PORT_MAX3100;
803ab4382d2SGreg Kroah-Hartman max3100s[i]->port.dev = &spi->dev;
804ab4382d2SGreg Kroah-Hartman retval = uart_add_one_port(&max3100_uart_driver, &max3100s[i]->port);
805ab4382d2SGreg Kroah-Hartman if (retval < 0)
806ab4382d2SGreg Kroah-Hartman dev_warn(&spi->dev,
807ab4382d2SGreg Kroah-Hartman "uart_add_one_port failed for line %d with error %d\n",
808ab4382d2SGreg Kroah-Hartman i, retval);
809ab4382d2SGreg Kroah-Hartman
810ab4382d2SGreg Kroah-Hartman /* set shutdown mode to save power. Will be woken-up on open */
811ab4382d2SGreg Kroah-Hartman if (max3100s[i]->max3100_hw_suspend)
812ab4382d2SGreg Kroah-Hartman max3100s[i]->max3100_hw_suspend(1);
813ab4382d2SGreg Kroah-Hartman else {
814ab4382d2SGreg Kroah-Hartman tx = MAX3100_WC | MAX3100_SHDN;
815ab4382d2SGreg Kroah-Hartman max3100_sr(max3100s[i], tx, &rx);
816ab4382d2SGreg Kroah-Hartman }
817ab4382d2SGreg Kroah-Hartman mutex_unlock(&max3100s_lock);
818ab4382d2SGreg Kroah-Hartman return 0;
819ab4382d2SGreg Kroah-Hartman }
820ab4382d2SGreg Kroah-Hartman
max3100_remove(struct spi_device * spi)821a0386bbaSUwe Kleine-König static void max3100_remove(struct spi_device *spi)
822ab4382d2SGreg Kroah-Hartman {
82331bdfc64SJingoo Han struct max3100_port *s = spi_get_drvdata(spi);
824ab4382d2SGreg Kroah-Hartman int i;
825ab4382d2SGreg Kroah-Hartman
826ab4382d2SGreg Kroah-Hartman mutex_lock(&max3100s_lock);
827ab4382d2SGreg Kroah-Hartman
828ab4382d2SGreg Kroah-Hartman /* find out the index for the chip we are removing */
829ab4382d2SGreg Kroah-Hartman for (i = 0; i < MAX_MAX3100; i++)
83041fda9c4SAlan Cox if (max3100s[i] == s) {
831ab4382d2SGreg Kroah-Hartman dev_dbg(&spi->dev, "%s: removing port %d\n", __func__, i);
832ab4382d2SGreg Kroah-Hartman uart_remove_one_port(&max3100_uart_driver, &max3100s[i]->port);
833ab4382d2SGreg Kroah-Hartman kfree(max3100s[i]);
834ab4382d2SGreg Kroah-Hartman max3100s[i] = NULL;
83541fda9c4SAlan Cox break;
83641fda9c4SAlan Cox }
83741fda9c4SAlan Cox
83841fda9c4SAlan Cox WARN_ON(i == MAX_MAX3100);
839ab4382d2SGreg Kroah-Hartman
840ab4382d2SGreg Kroah-Hartman /* check if this is the last chip we have */
841ab4382d2SGreg Kroah-Hartman for (i = 0; i < MAX_MAX3100; i++)
842ab4382d2SGreg Kroah-Hartman if (max3100s[i]) {
843ab4382d2SGreg Kroah-Hartman mutex_unlock(&max3100s_lock);
844a0386bbaSUwe Kleine-König return;
845ab4382d2SGreg Kroah-Hartman }
846ab4382d2SGreg Kroah-Hartman pr_debug("removing max3100 driver\n");
847ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&max3100_uart_driver);
848e8a10089SAndy Shevchenko uart_driver_registered = 0;
849ab4382d2SGreg Kroah-Hartman
850ab4382d2SGreg Kroah-Hartman mutex_unlock(&max3100s_lock);
851ab4382d2SGreg Kroah-Hartman }
852ab4382d2SGreg Kroah-Hartman
853c47ddc26SLars-Peter Clausen #ifdef CONFIG_PM_SLEEP
854ab4382d2SGreg Kroah-Hartman
max3100_suspend(struct device * dev)855c47ddc26SLars-Peter Clausen static int max3100_suspend(struct device *dev)
856ab4382d2SGreg Kroah-Hartman {
857c47ddc26SLars-Peter Clausen struct max3100_port *s = dev_get_drvdata(dev);
858ab4382d2SGreg Kroah-Hartman
859ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
860ab4382d2SGreg Kroah-Hartman
861ab4382d2SGreg Kroah-Hartman disable_irq(s->irq);
862ab4382d2SGreg Kroah-Hartman
863ab4382d2SGreg Kroah-Hartman s->suspending = 1;
864ab4382d2SGreg Kroah-Hartman uart_suspend_port(&max3100_uart_driver, &s->port);
865ab4382d2SGreg Kroah-Hartman
866ab4382d2SGreg Kroah-Hartman if (s->max3100_hw_suspend)
867ab4382d2SGreg Kroah-Hartman s->max3100_hw_suspend(1);
868ab4382d2SGreg Kroah-Hartman else {
869ab4382d2SGreg Kroah-Hartman /* no HW suspend, so do SW one */
870ab4382d2SGreg Kroah-Hartman u16 tx, rx;
871ab4382d2SGreg Kroah-Hartman
872ab4382d2SGreg Kroah-Hartman tx = MAX3100_WC | MAX3100_SHDN;
873ab4382d2SGreg Kroah-Hartman max3100_sr(s, tx, &rx);
874ab4382d2SGreg Kroah-Hartman }
875ab4382d2SGreg Kroah-Hartman return 0;
876ab4382d2SGreg Kroah-Hartman }
877ab4382d2SGreg Kroah-Hartman
max3100_resume(struct device * dev)878c47ddc26SLars-Peter Clausen static int max3100_resume(struct device *dev)
879ab4382d2SGreg Kroah-Hartman {
880c47ddc26SLars-Peter Clausen struct max3100_port *s = dev_get_drvdata(dev);
881ab4382d2SGreg Kroah-Hartman
882ab4382d2SGreg Kroah-Hartman dev_dbg(&s->spi->dev, "%s\n", __func__);
883ab4382d2SGreg Kroah-Hartman
884ab4382d2SGreg Kroah-Hartman if (s->max3100_hw_suspend)
885ab4382d2SGreg Kroah-Hartman s->max3100_hw_suspend(0);
886ab4382d2SGreg Kroah-Hartman uart_resume_port(&max3100_uart_driver, &s->port);
887ab4382d2SGreg Kroah-Hartman s->suspending = 0;
888ab4382d2SGreg Kroah-Hartman
889ab4382d2SGreg Kroah-Hartman enable_irq(s->irq);
890ab4382d2SGreg Kroah-Hartman
891ab4382d2SGreg Kroah-Hartman s->conf_commit = 1;
892ab4382d2SGreg Kroah-Hartman if (s->workqueue)
893ab4382d2SGreg Kroah-Hartman max3100_dowork(s);
894ab4382d2SGreg Kroah-Hartman
895ab4382d2SGreg Kroah-Hartman return 0;
896ab4382d2SGreg Kroah-Hartman }
897ab4382d2SGreg Kroah-Hartman
898c47ddc26SLars-Peter Clausen static SIMPLE_DEV_PM_OPS(max3100_pm_ops, max3100_suspend, max3100_resume);
899c47ddc26SLars-Peter Clausen #define MAX3100_PM_OPS (&max3100_pm_ops)
900c47ddc26SLars-Peter Clausen
901ab4382d2SGreg Kroah-Hartman #else
902c47ddc26SLars-Peter Clausen #define MAX3100_PM_OPS NULL
903ab4382d2SGreg Kroah-Hartman #endif
904ab4382d2SGreg Kroah-Hartman
905ab4382d2SGreg Kroah-Hartman static struct spi_driver max3100_driver = {
906ab4382d2SGreg Kroah-Hartman .driver = {
907ab4382d2SGreg Kroah-Hartman .name = "max3100",
908c47ddc26SLars-Peter Clausen .pm = MAX3100_PM_OPS,
909ab4382d2SGreg Kroah-Hartman },
910ab4382d2SGreg Kroah-Hartman .probe = max3100_probe,
9112d47b716SBill Pemberton .remove = max3100_remove,
912ab4382d2SGreg Kroah-Hartman };
913ab4382d2SGreg Kroah-Hartman
914c0fc208eSDevendra Naga module_spi_driver(max3100_driver);
915ab4382d2SGreg Kroah-Hartman
916ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("MAX3100 driver");
917ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Christian Pellegrin <chripell@evolware.org>");
918ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
919ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("spi:max3100");
920