forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/drivers/tty/serial/sc16is7xx.c
....@@ -14,9 +14,9 @@
1414 #include <linux/device.h>
1515 #include <linux/gpio/driver.h>
1616 #include <linux/i2c.h>
17
+#include <linux/mod_devicetable.h>
1718 #include <linux/module.h>
18
-#include <linux/of.h>
19
-#include <linux/of_device.h>
19
+#include <linux/property.h>
2020 #include <linux/regmap.h>
2121 #include <linux/serial_core.h>
2222 #include <linux/serial.h>
....@@ -315,6 +315,7 @@
315315 struct kthread_work tx_work;
316316 struct kthread_work reg_work;
317317 struct sc16is7xx_one_config config;
318
+ bool irda_mode;
318319 };
319320
320321 struct sc16is7xx_port {
....@@ -327,9 +328,8 @@
327328 unsigned char buf[SC16IS7XX_FIFO_SIZE];
328329 struct kthread_worker kworker;
329330 struct task_struct *kworker_task;
330
- struct kthread_work irq_work;
331331 struct mutex efr_lock;
332
- struct sc16is7xx_one p[0];
332
+ struct sc16is7xx_one p[];
333333 };
334334
335335 static unsigned long sc16is7xx_lines;
....@@ -710,9 +710,9 @@
710710 return true;
711711 }
712712
713
-static void sc16is7xx_ist(struct kthread_work *ws)
713
+static irqreturn_t sc16is7xx_irq(int irq, void *dev_id)
714714 {
715
- struct sc16is7xx_port *s = to_sc16is7xx_port(ws, irq_work);
715
+ struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id;
716716
717717 mutex_lock(&s->efr_lock);
718718
....@@ -727,13 +727,6 @@
727727 }
728728
729729 mutex_unlock(&s->efr_lock);
730
-}
731
-
732
-static irqreturn_t sc16is7xx_irq(int irq, void *dev_id)
733
-{
734
- struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id;
735
-
736
- kthread_queue_work(&s->kworker, &s->irq_work);
737730
738731 return IRQ_HANDLED;
739732 }
....@@ -741,12 +734,15 @@
741734 static void sc16is7xx_tx_proc(struct kthread_work *ws)
742735 {
743736 struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port);
737
+ struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
744738
745739 if ((port->rs485.flags & SER_RS485_ENABLED) &&
746740 (port->rs485.delay_rts_before_send > 0))
747741 msleep(port->rs485.delay_rts_before_send);
748742
743
+ mutex_lock(&s->efr_lock);
749744 sc16is7xx_handle_tx(port);
745
+ mutex_unlock(&s->efr_lock);
750746 }
751747
752748 static void sc16is7xx_reconf_rs485(struct uart_port *port)
....@@ -994,6 +990,7 @@
994990
995991 static int sc16is7xx_startup(struct uart_port *port)
996992 {
993
+ struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
997994 struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
998995 unsigned int val;
999996
....@@ -1031,6 +1028,13 @@
10311028
10321029 /* Now, initialize the UART */
10331030 sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, SC16IS7XX_LCR_WORD_LEN_8);
1031
+
1032
+ /* Enable IrDA mode if requested in DT */
1033
+ /* This bit must be written with LCR[7] = 0 */
1034
+ sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
1035
+ SC16IS7XX_MCR_IRDA_BIT,
1036
+ one->irda_mode ?
1037
+ SC16IS7XX_MCR_IRDA_BIT : 0);
10341038
10351039 /* Enable the Rx and Tx FIFO */
10361040 sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
....@@ -1176,29 +1180,45 @@
11761180
11771181 static int sc16is7xx_probe(struct device *dev,
11781182 const struct sc16is7xx_devtype *devtype,
1179
- struct regmap *regmap, int irq, unsigned long flags)
1183
+ struct regmap *regmap, int irq)
11801184 {
1181
- struct sched_param sched_param = { .sched_priority = MAX_RT_PRIO / 2 };
1182
- unsigned long freq, *pfreq = dev_get_platdata(dev);
1185
+ unsigned long freq = 0, *pfreq = dev_get_platdata(dev);
1186
+ unsigned int val;
1187
+ u32 uartclk = 0;
11831188 int i, ret;
11841189 struct sc16is7xx_port *s;
11851190
11861191 if (IS_ERR(regmap))
11871192 return PTR_ERR(regmap);
11881193
1194
+ /*
1195
+ * This device does not have an identification register that would
1196
+ * tell us if we are really connected to the correct device.
1197
+ * The best we can do is to check if communication is at all possible.
1198
+ */
1199
+ ret = regmap_read(regmap,
1200
+ SC16IS7XX_LSR_REG << SC16IS7XX_REG_SHIFT, &val);
1201
+ if (ret < 0)
1202
+ return -EPROBE_DEFER;
1203
+
11891204 /* Alloc port structure */
1190
- s = devm_kzalloc(dev, sizeof(*s) +
1191
- sizeof(struct sc16is7xx_one) * devtype->nr_uart,
1192
- GFP_KERNEL);
1205
+ s = devm_kzalloc(dev, struct_size(s, p, devtype->nr_uart), GFP_KERNEL);
11931206 if (!s) {
11941207 dev_err(dev, "Error allocating port structure\n");
11951208 return -ENOMEM;
11961209 }
11971210
1211
+ /* Always ask for fixed clock rate from a property. */
1212
+ device_property_read_u32(dev, "clock-frequency", &uartclk);
1213
+
11981214 s->clk = devm_clk_get(dev, NULL);
11991215 if (IS_ERR(s->clk)) {
1216
+ if (uartclk)
1217
+ freq = uartclk;
12001218 if (pfreq)
12011219 freq = *pfreq;
1220
+ if (freq)
1221
+ dev_dbg(dev, "Clock frequency: %luHz\n", freq);
12021222 else
12031223 return PTR_ERR(s->clk);
12041224 } else {
....@@ -1215,14 +1235,13 @@
12151235 mutex_init(&s->efr_lock);
12161236
12171237 kthread_init_worker(&s->kworker);
1218
- kthread_init_work(&s->irq_work, sc16is7xx_ist);
12191238 s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
12201239 "sc16is7xx");
12211240 if (IS_ERR(s->kworker_task)) {
12221241 ret = PTR_ERR(s->kworker_task);
12231242 goto out_clk;
12241243 }
1225
- sched_setscheduler(s->kworker_task, SCHED_FIFO, &sched_param);
1244
+ sched_set_fifo(s->kworker_task);
12261245
12271246 #ifdef CONFIG_GPIOLIB
12281247 if (devtype->nr_gpio) {
....@@ -1255,6 +1274,7 @@
12551274 s->p[i].port.type = PORT_SC16IS7XX;
12561275 s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE;
12571276 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
1277
+ s->p[i].port.iobase = i;
12581278 s->p[i].port.iotype = UPIO_PORT;
12591279 s->p[i].port.uartclk = freq;
12601280 s->p[i].port.rs485_config = sc16is7xx_config_rs485;
....@@ -1296,9 +1316,33 @@
12961316 sc16is7xx_power(&s->p[i].port, 0);
12971317 }
12981318
1299
- /* Setup interrupt */
1300
- ret = devm_request_irq(dev, irq, sc16is7xx_irq,
1301
- flags, dev_name(dev), s);
1319
+ if (dev->of_node) {
1320
+ struct property *prop;
1321
+ const __be32 *p;
1322
+ u32 u;
1323
+
1324
+ of_property_for_each_u32(dev->of_node, "irda-mode-ports",
1325
+ prop, p, u)
1326
+ if (u < devtype->nr_uart)
1327
+ s->p[u].irda_mode = true;
1328
+ }
1329
+
1330
+ /*
1331
+ * Setup interrupt. We first try to acquire the IRQ line as level IRQ.
1332
+ * If that succeeds, we can allow sharing the interrupt as well.
1333
+ * In case the interrupt controller doesn't support that, we fall
1334
+ * back to a non-shared falling-edge trigger.
1335
+ */
1336
+ ret = devm_request_threaded_irq(dev, irq, NULL, sc16is7xx_irq,
1337
+ IRQF_TRIGGER_LOW | IRQF_SHARED |
1338
+ IRQF_ONESHOT,
1339
+ dev_name(dev), s);
1340
+ if (!ret)
1341
+ return 0;
1342
+
1343
+ ret = devm_request_threaded_irq(dev, irq, NULL, sc16is7xx_irq,
1344
+ IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1345
+ dev_name(dev), s);
13021346 if (!ret)
13031347 return 0;
13041348
....@@ -1372,7 +1416,6 @@
13721416 static int sc16is7xx_spi_probe(struct spi_device *spi)
13731417 {
13741418 const struct sc16is7xx_devtype *devtype;
1375
- unsigned long flags = 0;
13761419 struct regmap *regmap;
13771420 int ret;
13781421
....@@ -1386,25 +1429,20 @@
13861429 return ret;
13871430
13881431 if (spi->dev.of_node) {
1389
- const struct of_device_id *of_id =
1390
- of_match_device(sc16is7xx_dt_ids, &spi->dev);
1391
-
1392
- if (!of_id)
1432
+ devtype = device_get_match_data(&spi->dev);
1433
+ if (!devtype)
13931434 return -ENODEV;
1394
-
1395
- devtype = (struct sc16is7xx_devtype *)of_id->data;
13961435 } else {
13971436 const struct spi_device_id *id_entry = spi_get_device_id(spi);
13981437
13991438 devtype = (struct sc16is7xx_devtype *)id_entry->driver_data;
1400
- flags = IRQF_TRIGGER_FALLING;
14011439 }
14021440
14031441 regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
14041442 (devtype->nr_uart - 1);
14051443 regmap = devm_regmap_init_spi(spi, &regcfg);
14061444
1407
- return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq, flags);
1445
+ return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq);
14081446 }
14091447
14101448 static int sc16is7xx_spi_remove(struct spi_device *spi)
....@@ -1428,7 +1466,7 @@
14281466 static struct spi_driver sc16is7xx_spi_uart_driver = {
14291467 .driver = {
14301468 .name = SC16IS7XX_NAME,
1431
- .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
1469
+ .of_match_table = sc16is7xx_dt_ids,
14321470 },
14331471 .probe = sc16is7xx_spi_probe,
14341472 .remove = sc16is7xx_spi_remove,
....@@ -1443,27 +1481,21 @@
14431481 const struct i2c_device_id *id)
14441482 {
14451483 const struct sc16is7xx_devtype *devtype;
1446
- unsigned long flags = 0;
14471484 struct regmap *regmap;
14481485
14491486 if (i2c->dev.of_node) {
1450
- const struct of_device_id *of_id =
1451
- of_match_device(sc16is7xx_dt_ids, &i2c->dev);
1452
-
1453
- if (!of_id)
1487
+ devtype = device_get_match_data(&i2c->dev);
1488
+ if (!devtype)
14541489 return -ENODEV;
1455
-
1456
- devtype = (struct sc16is7xx_devtype *)of_id->data;
14571490 } else {
14581491 devtype = (struct sc16is7xx_devtype *)id->driver_data;
1459
- flags = IRQF_TRIGGER_FALLING;
14601492 }
14611493
14621494 regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
14631495 (devtype->nr_uart - 1);
14641496 regmap = devm_regmap_init_i2c(i2c, &regcfg);
14651497
1466
- return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq, flags);
1498
+ return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq);
14671499 }
14681500
14691501 static int sc16is7xx_i2c_remove(struct i2c_client *client)
....@@ -1486,7 +1518,7 @@
14861518 static struct i2c_driver sc16is7xx_i2c_uart_driver = {
14871519 .driver = {
14881520 .name = SC16IS7XX_NAME,
1489
- .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
1521
+ .of_match_table = sc16is7xx_dt_ids,
14901522 },
14911523 .probe = sc16is7xx_i2c_probe,
14921524 .remove = sc16is7xx_i2c_remove,
....@@ -1524,11 +1556,11 @@
15241556
15251557 #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
15261558 err_spi:
1559
+#endif
15271560 #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
15281561 i2c_del_driver(&sc16is7xx_i2c_uart_driver);
1529
-#endif
1530
-#endif
15311562 err_i2c:
1563
+#endif
15321564 uart_unregister_driver(&sc16is7xx_uart);
15331565 return ret;
15341566 }