hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
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,
....@@ -1166,9 +1170,18 @@
11661170 state |= BIT(offset);
11671171 else
11681172 state &= ~BIT(offset);
1169
- sc16is7xx_port_write(port, SC16IS7XX_IOSTATE_REG, state);
1173
+
1174
+ /*
1175
+ * If we write IOSTATE first, and then IODIR, the output value is not
1176
+ * transferred to the corresponding I/O pin.
1177
+ * The datasheet states that each register bit will be transferred to
1178
+ * the corresponding I/O pin programmed as output when writing to
1179
+ * IOSTATE. Therefore, configure direction first with IODIR, and then
1180
+ * set value after with IOSTATE.
1181
+ */
11701182 sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset),
11711183 BIT(offset));
1184
+ sc16is7xx_port_write(port, SC16IS7XX_IOSTATE_REG, state);
11721185
11731186 return 0;
11741187 }
....@@ -1176,29 +1189,45 @@
11761189
11771190 static int sc16is7xx_probe(struct device *dev,
11781191 const struct sc16is7xx_devtype *devtype,
1179
- struct regmap *regmap, int irq, unsigned long flags)
1192
+ struct regmap *regmap, int irq)
11801193 {
1181
- struct sched_param sched_param = { .sched_priority = MAX_RT_PRIO / 2 };
1182
- unsigned long freq, *pfreq = dev_get_platdata(dev);
1194
+ unsigned long freq = 0, *pfreq = dev_get_platdata(dev);
1195
+ unsigned int val;
1196
+ u32 uartclk = 0;
11831197 int i, ret;
11841198 struct sc16is7xx_port *s;
11851199
11861200 if (IS_ERR(regmap))
11871201 return PTR_ERR(regmap);
11881202
1203
+ /*
1204
+ * This device does not have an identification register that would
1205
+ * tell us if we are really connected to the correct device.
1206
+ * The best we can do is to check if communication is at all possible.
1207
+ */
1208
+ ret = regmap_read(regmap,
1209
+ SC16IS7XX_LSR_REG << SC16IS7XX_REG_SHIFT, &val);
1210
+ if (ret < 0)
1211
+ return -EPROBE_DEFER;
1212
+
11891213 /* Alloc port structure */
1190
- s = devm_kzalloc(dev, sizeof(*s) +
1191
- sizeof(struct sc16is7xx_one) * devtype->nr_uart,
1192
- GFP_KERNEL);
1214
+ s = devm_kzalloc(dev, struct_size(s, p, devtype->nr_uart), GFP_KERNEL);
11931215 if (!s) {
11941216 dev_err(dev, "Error allocating port structure\n");
11951217 return -ENOMEM;
11961218 }
11971219
1220
+ /* Always ask for fixed clock rate from a property. */
1221
+ device_property_read_u32(dev, "clock-frequency", &uartclk);
1222
+
11981223 s->clk = devm_clk_get(dev, NULL);
11991224 if (IS_ERR(s->clk)) {
1225
+ if (uartclk)
1226
+ freq = uartclk;
12001227 if (pfreq)
12011228 freq = *pfreq;
1229
+ if (freq)
1230
+ dev_dbg(dev, "Clock frequency: %luHz\n", freq);
12021231 else
12031232 return PTR_ERR(s->clk);
12041233 } else {
....@@ -1215,33 +1244,13 @@
12151244 mutex_init(&s->efr_lock);
12161245
12171246 kthread_init_worker(&s->kworker);
1218
- kthread_init_work(&s->irq_work, sc16is7xx_ist);
12191247 s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
12201248 "sc16is7xx");
12211249 if (IS_ERR(s->kworker_task)) {
12221250 ret = PTR_ERR(s->kworker_task);
12231251 goto out_clk;
12241252 }
1225
- sched_setscheduler(s->kworker_task, SCHED_FIFO, &sched_param);
1226
-
1227
-#ifdef CONFIG_GPIOLIB
1228
- if (devtype->nr_gpio) {
1229
- /* Setup GPIO cotroller */
1230
- s->gpio.owner = THIS_MODULE;
1231
- s->gpio.parent = dev;
1232
- s->gpio.label = dev_name(dev);
1233
- s->gpio.direction_input = sc16is7xx_gpio_direction_input;
1234
- s->gpio.get = sc16is7xx_gpio_get;
1235
- s->gpio.direction_output = sc16is7xx_gpio_direction_output;
1236
- s->gpio.set = sc16is7xx_gpio_set;
1237
- s->gpio.base = -1;
1238
- s->gpio.ngpio = devtype->nr_gpio;
1239
- s->gpio.can_sleep = 1;
1240
- ret = gpiochip_add_data(&s->gpio, s);
1241
- if (ret)
1242
- goto out_thread;
1243
- }
1244
-#endif
1253
+ sched_set_fifo(s->kworker_task);
12451254
12461255 /* reset device, purging any pending irq / data */
12471256 regmap_write(s->regmap, SC16IS7XX_IOCONTROL_REG << SC16IS7XX_REG_SHIFT,
....@@ -1255,6 +1264,13 @@
12551264 s->p[i].port.type = PORT_SC16IS7XX;
12561265 s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE;
12571266 s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
1267
+ s->p[i].port.iobase = i;
1268
+ /*
1269
+ * Use all ones as membase to make sure uart_configure_port() in
1270
+ * serial_core.c does not abort for SPI/I2C devices where the
1271
+ * membase address is not applicable.
1272
+ */
1273
+ s->p[i].port.membase = (void __iomem *)~0;
12581274 s->p[i].port.iotype = UPIO_PORT;
12591275 s->p[i].port.uartclk = freq;
12601276 s->p[i].port.rs485_config = sc16is7xx_config_rs485;
....@@ -1296,17 +1312,54 @@
12961312 sc16is7xx_power(&s->p[i].port, 0);
12971313 }
12981314
1299
- /* Setup interrupt */
1300
- ret = devm_request_irq(dev, irq, sc16is7xx_irq,
1301
- flags, dev_name(dev), s);
1315
+ if (dev->of_node) {
1316
+ struct property *prop;
1317
+ const __be32 *p;
1318
+ u32 u;
1319
+
1320
+ of_property_for_each_u32(dev->of_node, "irda-mode-ports",
1321
+ prop, p, u)
1322
+ if (u < devtype->nr_uart)
1323
+ s->p[u].irda_mode = true;
1324
+ }
1325
+
1326
+#ifdef CONFIG_GPIOLIB
1327
+ if (devtype->nr_gpio) {
1328
+ /* Setup GPIO cotroller */
1329
+ s->gpio.owner = THIS_MODULE;
1330
+ s->gpio.parent = dev;
1331
+ s->gpio.label = dev_name(dev);
1332
+ s->gpio.direction_input = sc16is7xx_gpio_direction_input;
1333
+ s->gpio.get = sc16is7xx_gpio_get;
1334
+ s->gpio.direction_output = sc16is7xx_gpio_direction_output;
1335
+ s->gpio.set = sc16is7xx_gpio_set;
1336
+ s->gpio.base = -1;
1337
+ s->gpio.ngpio = devtype->nr_gpio;
1338
+ s->gpio.can_sleep = 1;
1339
+ ret = gpiochip_add_data(&s->gpio, s);
1340
+ if (ret)
1341
+ goto out_thread;
1342
+ }
1343
+#endif
1344
+
1345
+ /*
1346
+ * Setup interrupt. We first try to acquire the IRQ line as level IRQ.
1347
+ * If that succeeds, we can allow sharing the interrupt as well.
1348
+ * In case the interrupt controller doesn't support that, we fall
1349
+ * back to a non-shared falling-edge trigger.
1350
+ */
1351
+ ret = devm_request_threaded_irq(dev, irq, NULL, sc16is7xx_irq,
1352
+ IRQF_TRIGGER_LOW | IRQF_SHARED |
1353
+ IRQF_ONESHOT,
1354
+ dev_name(dev), s);
13021355 if (!ret)
13031356 return 0;
13041357
1305
-out_ports:
1306
- for (i--; i >= 0; i--) {
1307
- uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
1308
- clear_bit(s->p[i].port.line, &sc16is7xx_lines);
1309
- }
1358
+ ret = devm_request_threaded_irq(dev, irq, NULL, sc16is7xx_irq,
1359
+ IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1360
+ dev_name(dev), s);
1361
+ if (!ret)
1362
+ return 0;
13101363
13111364 #ifdef CONFIG_GPIOLIB
13121365 if (devtype->nr_gpio)
....@@ -1314,6 +1367,13 @@
13141367
13151368 out_thread:
13161369 #endif
1370
+
1371
+out_ports:
1372
+ for (i--; i >= 0; i--) {
1373
+ uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
1374
+ clear_bit(s->p[i].port.line, &sc16is7xx_lines);
1375
+ }
1376
+
13171377 kthread_stop(s->kworker_task);
13181378
13191379 out_clk:
....@@ -1372,7 +1432,6 @@
13721432 static int sc16is7xx_spi_probe(struct spi_device *spi)
13731433 {
13741434 const struct sc16is7xx_devtype *devtype;
1375
- unsigned long flags = 0;
13761435 struct regmap *regmap;
13771436 int ret;
13781437
....@@ -1386,25 +1445,20 @@
13861445 return ret;
13871446
13881447 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)
1448
+ devtype = device_get_match_data(&spi->dev);
1449
+ if (!devtype)
13931450 return -ENODEV;
1394
-
1395
- devtype = (struct sc16is7xx_devtype *)of_id->data;
13961451 } else {
13971452 const struct spi_device_id *id_entry = spi_get_device_id(spi);
13981453
13991454 devtype = (struct sc16is7xx_devtype *)id_entry->driver_data;
1400
- flags = IRQF_TRIGGER_FALLING;
14011455 }
14021456
14031457 regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
14041458 (devtype->nr_uart - 1);
14051459 regmap = devm_regmap_init_spi(spi, &regcfg);
14061460
1407
- return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq, flags);
1461
+ return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq);
14081462 }
14091463
14101464 static int sc16is7xx_spi_remove(struct spi_device *spi)
....@@ -1428,7 +1482,7 @@
14281482 static struct spi_driver sc16is7xx_spi_uart_driver = {
14291483 .driver = {
14301484 .name = SC16IS7XX_NAME,
1431
- .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
1485
+ .of_match_table = sc16is7xx_dt_ids,
14321486 },
14331487 .probe = sc16is7xx_spi_probe,
14341488 .remove = sc16is7xx_spi_remove,
....@@ -1443,27 +1497,21 @@
14431497 const struct i2c_device_id *id)
14441498 {
14451499 const struct sc16is7xx_devtype *devtype;
1446
- unsigned long flags = 0;
14471500 struct regmap *regmap;
14481501
14491502 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)
1503
+ devtype = device_get_match_data(&i2c->dev);
1504
+ if (!devtype)
14541505 return -ENODEV;
1455
-
1456
- devtype = (struct sc16is7xx_devtype *)of_id->data;
14571506 } else {
14581507 devtype = (struct sc16is7xx_devtype *)id->driver_data;
1459
- flags = IRQF_TRIGGER_FALLING;
14601508 }
14611509
14621510 regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
14631511 (devtype->nr_uart - 1);
14641512 regmap = devm_regmap_init_i2c(i2c, &regcfg);
14651513
1466
- return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq, flags);
1514
+ return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq);
14671515 }
14681516
14691517 static int sc16is7xx_i2c_remove(struct i2c_client *client)
....@@ -1486,7 +1534,7 @@
14861534 static struct i2c_driver sc16is7xx_i2c_uart_driver = {
14871535 .driver = {
14881536 .name = SC16IS7XX_NAME,
1489
- .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
1537
+ .of_match_table = sc16is7xx_dt_ids,
14901538 },
14911539 .probe = sc16is7xx_i2c_probe,
14921540 .remove = sc16is7xx_i2c_remove,
....@@ -1524,11 +1572,11 @@
15241572
15251573 #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
15261574 err_spi:
1575
+#endif
15271576 #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
15281577 i2c_del_driver(&sc16is7xx_i2c_uart_driver);
1529
-#endif
1530
-#endif
15311578 err_i2c:
1579
+#endif
15321580 uart_unregister_driver(&sc16is7xx_uart);
15331581 return ret;
15341582 }