.. | .. |
---|
14 | 14 | #include <linux/device.h> |
---|
15 | 15 | #include <linux/gpio/driver.h> |
---|
16 | 16 | #include <linux/i2c.h> |
---|
| 17 | +#include <linux/mod_devicetable.h> |
---|
17 | 18 | #include <linux/module.h> |
---|
18 | | -#include <linux/of.h> |
---|
19 | | -#include <linux/of_device.h> |
---|
| 19 | +#include <linux/property.h> |
---|
20 | 20 | #include <linux/regmap.h> |
---|
21 | 21 | #include <linux/serial_core.h> |
---|
22 | 22 | #include <linux/serial.h> |
---|
.. | .. |
---|
315 | 315 | struct kthread_work tx_work; |
---|
316 | 316 | struct kthread_work reg_work; |
---|
317 | 317 | struct sc16is7xx_one_config config; |
---|
| 318 | + bool irda_mode; |
---|
318 | 319 | }; |
---|
319 | 320 | |
---|
320 | 321 | struct sc16is7xx_port { |
---|
.. | .. |
---|
327 | 328 | unsigned char buf[SC16IS7XX_FIFO_SIZE]; |
---|
328 | 329 | struct kthread_worker kworker; |
---|
329 | 330 | struct task_struct *kworker_task; |
---|
330 | | - struct kthread_work irq_work; |
---|
331 | 331 | struct mutex efr_lock; |
---|
332 | | - struct sc16is7xx_one p[0]; |
---|
| 332 | + struct sc16is7xx_one p[]; |
---|
333 | 333 | }; |
---|
334 | 334 | |
---|
335 | 335 | static unsigned long sc16is7xx_lines; |
---|
.. | .. |
---|
710 | 710 | return true; |
---|
711 | 711 | } |
---|
712 | 712 | |
---|
713 | | -static void sc16is7xx_ist(struct kthread_work *ws) |
---|
| 713 | +static irqreturn_t sc16is7xx_irq(int irq, void *dev_id) |
---|
714 | 714 | { |
---|
715 | | - struct sc16is7xx_port *s = to_sc16is7xx_port(ws, irq_work); |
---|
| 715 | + struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id; |
---|
716 | 716 | |
---|
717 | 717 | mutex_lock(&s->efr_lock); |
---|
718 | 718 | |
---|
.. | .. |
---|
727 | 727 | } |
---|
728 | 728 | |
---|
729 | 729 | 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); |
---|
737 | 730 | |
---|
738 | 731 | return IRQ_HANDLED; |
---|
739 | 732 | } |
---|
.. | .. |
---|
741 | 734 | static void sc16is7xx_tx_proc(struct kthread_work *ws) |
---|
742 | 735 | { |
---|
743 | 736 | struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port); |
---|
| 737 | + struct sc16is7xx_port *s = dev_get_drvdata(port->dev); |
---|
744 | 738 | |
---|
745 | 739 | if ((port->rs485.flags & SER_RS485_ENABLED) && |
---|
746 | 740 | (port->rs485.delay_rts_before_send > 0)) |
---|
747 | 741 | msleep(port->rs485.delay_rts_before_send); |
---|
748 | 742 | |
---|
| 743 | + mutex_lock(&s->efr_lock); |
---|
749 | 744 | sc16is7xx_handle_tx(port); |
---|
| 745 | + mutex_unlock(&s->efr_lock); |
---|
750 | 746 | } |
---|
751 | 747 | |
---|
752 | 748 | static void sc16is7xx_reconf_rs485(struct uart_port *port) |
---|
.. | .. |
---|
994 | 990 | |
---|
995 | 991 | static int sc16is7xx_startup(struct uart_port *port) |
---|
996 | 992 | { |
---|
| 993 | + struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); |
---|
997 | 994 | struct sc16is7xx_port *s = dev_get_drvdata(port->dev); |
---|
998 | 995 | unsigned int val; |
---|
999 | 996 | |
---|
.. | .. |
---|
1031 | 1028 | |
---|
1032 | 1029 | /* Now, initialize the UART */ |
---|
1033 | 1030 | 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); |
---|
1034 | 1038 | |
---|
1035 | 1039 | /* Enable the Rx and Tx FIFO */ |
---|
1036 | 1040 | sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG, |
---|
.. | .. |
---|
1166 | 1170 | state |= BIT(offset); |
---|
1167 | 1171 | else |
---|
1168 | 1172 | 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 | + */ |
---|
1170 | 1182 | sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset), |
---|
1171 | 1183 | BIT(offset)); |
---|
| 1184 | + sc16is7xx_port_write(port, SC16IS7XX_IOSTATE_REG, state); |
---|
1172 | 1185 | |
---|
1173 | 1186 | return 0; |
---|
1174 | 1187 | } |
---|
.. | .. |
---|
1176 | 1189 | |
---|
1177 | 1190 | static int sc16is7xx_probe(struct device *dev, |
---|
1178 | 1191 | const struct sc16is7xx_devtype *devtype, |
---|
1179 | | - struct regmap *regmap, int irq, unsigned long flags) |
---|
| 1192 | + struct regmap *regmap, int irq) |
---|
1180 | 1193 | { |
---|
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; |
---|
1183 | 1197 | int i, ret; |
---|
1184 | 1198 | struct sc16is7xx_port *s; |
---|
1185 | 1199 | |
---|
1186 | 1200 | if (IS_ERR(regmap)) |
---|
1187 | 1201 | return PTR_ERR(regmap); |
---|
1188 | 1202 | |
---|
| 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 | + |
---|
1189 | 1213 | /* 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); |
---|
1193 | 1215 | if (!s) { |
---|
1194 | 1216 | dev_err(dev, "Error allocating port structure\n"); |
---|
1195 | 1217 | return -ENOMEM; |
---|
1196 | 1218 | } |
---|
1197 | 1219 | |
---|
| 1220 | + /* Always ask for fixed clock rate from a property. */ |
---|
| 1221 | + device_property_read_u32(dev, "clock-frequency", &uartclk); |
---|
| 1222 | + |
---|
1198 | 1223 | s->clk = devm_clk_get(dev, NULL); |
---|
1199 | 1224 | if (IS_ERR(s->clk)) { |
---|
| 1225 | + if (uartclk) |
---|
| 1226 | + freq = uartclk; |
---|
1200 | 1227 | if (pfreq) |
---|
1201 | 1228 | freq = *pfreq; |
---|
| 1229 | + if (freq) |
---|
| 1230 | + dev_dbg(dev, "Clock frequency: %luHz\n", freq); |
---|
1202 | 1231 | else |
---|
1203 | 1232 | return PTR_ERR(s->clk); |
---|
1204 | 1233 | } else { |
---|
.. | .. |
---|
1215 | 1244 | mutex_init(&s->efr_lock); |
---|
1216 | 1245 | |
---|
1217 | 1246 | kthread_init_worker(&s->kworker); |
---|
1218 | | - kthread_init_work(&s->irq_work, sc16is7xx_ist); |
---|
1219 | 1247 | s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker, |
---|
1220 | 1248 | "sc16is7xx"); |
---|
1221 | 1249 | if (IS_ERR(s->kworker_task)) { |
---|
1222 | 1250 | ret = PTR_ERR(s->kworker_task); |
---|
1223 | 1251 | goto out_clk; |
---|
1224 | 1252 | } |
---|
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); |
---|
1245 | 1254 | |
---|
1246 | 1255 | /* reset device, purging any pending irq / data */ |
---|
1247 | 1256 | regmap_write(s->regmap, SC16IS7XX_IOCONTROL_REG << SC16IS7XX_REG_SHIFT, |
---|
.. | .. |
---|
1255 | 1264 | s->p[i].port.type = PORT_SC16IS7XX; |
---|
1256 | 1265 | s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE; |
---|
1257 | 1266 | 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; |
---|
1258 | 1274 | s->p[i].port.iotype = UPIO_PORT; |
---|
1259 | 1275 | s->p[i].port.uartclk = freq; |
---|
1260 | 1276 | s->p[i].port.rs485_config = sc16is7xx_config_rs485; |
---|
.. | .. |
---|
1296 | 1312 | sc16is7xx_power(&s->p[i].port, 0); |
---|
1297 | 1313 | } |
---|
1298 | 1314 | |
---|
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); |
---|
1302 | 1355 | if (!ret) |
---|
1303 | 1356 | return 0; |
---|
1304 | 1357 | |
---|
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; |
---|
1310 | 1363 | |
---|
1311 | 1364 | #ifdef CONFIG_GPIOLIB |
---|
1312 | 1365 | if (devtype->nr_gpio) |
---|
.. | .. |
---|
1314 | 1367 | |
---|
1315 | 1368 | out_thread: |
---|
1316 | 1369 | #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 | + |
---|
1317 | 1377 | kthread_stop(s->kworker_task); |
---|
1318 | 1378 | |
---|
1319 | 1379 | out_clk: |
---|
.. | .. |
---|
1372 | 1432 | static int sc16is7xx_spi_probe(struct spi_device *spi) |
---|
1373 | 1433 | { |
---|
1374 | 1434 | const struct sc16is7xx_devtype *devtype; |
---|
1375 | | - unsigned long flags = 0; |
---|
1376 | 1435 | struct regmap *regmap; |
---|
1377 | 1436 | int ret; |
---|
1378 | 1437 | |
---|
.. | .. |
---|
1386 | 1445 | return ret; |
---|
1387 | 1446 | |
---|
1388 | 1447 | 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) |
---|
1393 | 1450 | return -ENODEV; |
---|
1394 | | - |
---|
1395 | | - devtype = (struct sc16is7xx_devtype *)of_id->data; |
---|
1396 | 1451 | } else { |
---|
1397 | 1452 | const struct spi_device_id *id_entry = spi_get_device_id(spi); |
---|
1398 | 1453 | |
---|
1399 | 1454 | devtype = (struct sc16is7xx_devtype *)id_entry->driver_data; |
---|
1400 | | - flags = IRQF_TRIGGER_FALLING; |
---|
1401 | 1455 | } |
---|
1402 | 1456 | |
---|
1403 | 1457 | regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) | |
---|
1404 | 1458 | (devtype->nr_uart - 1); |
---|
1405 | 1459 | regmap = devm_regmap_init_spi(spi, ®cfg); |
---|
1406 | 1460 | |
---|
1407 | | - return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq, flags); |
---|
| 1461 | + return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq); |
---|
1408 | 1462 | } |
---|
1409 | 1463 | |
---|
1410 | 1464 | static int sc16is7xx_spi_remove(struct spi_device *spi) |
---|
.. | .. |
---|
1428 | 1482 | static struct spi_driver sc16is7xx_spi_uart_driver = { |
---|
1429 | 1483 | .driver = { |
---|
1430 | 1484 | .name = SC16IS7XX_NAME, |
---|
1431 | | - .of_match_table = of_match_ptr(sc16is7xx_dt_ids), |
---|
| 1485 | + .of_match_table = sc16is7xx_dt_ids, |
---|
1432 | 1486 | }, |
---|
1433 | 1487 | .probe = sc16is7xx_spi_probe, |
---|
1434 | 1488 | .remove = sc16is7xx_spi_remove, |
---|
.. | .. |
---|
1443 | 1497 | const struct i2c_device_id *id) |
---|
1444 | 1498 | { |
---|
1445 | 1499 | const struct sc16is7xx_devtype *devtype; |
---|
1446 | | - unsigned long flags = 0; |
---|
1447 | 1500 | struct regmap *regmap; |
---|
1448 | 1501 | |
---|
1449 | 1502 | 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) |
---|
1454 | 1505 | return -ENODEV; |
---|
1455 | | - |
---|
1456 | | - devtype = (struct sc16is7xx_devtype *)of_id->data; |
---|
1457 | 1506 | } else { |
---|
1458 | 1507 | devtype = (struct sc16is7xx_devtype *)id->driver_data; |
---|
1459 | | - flags = IRQF_TRIGGER_FALLING; |
---|
1460 | 1508 | } |
---|
1461 | 1509 | |
---|
1462 | 1510 | regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) | |
---|
1463 | 1511 | (devtype->nr_uart - 1); |
---|
1464 | 1512 | regmap = devm_regmap_init_i2c(i2c, ®cfg); |
---|
1465 | 1513 | |
---|
1466 | | - return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq, flags); |
---|
| 1514 | + return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq); |
---|
1467 | 1515 | } |
---|
1468 | 1516 | |
---|
1469 | 1517 | static int sc16is7xx_i2c_remove(struct i2c_client *client) |
---|
.. | .. |
---|
1486 | 1534 | static struct i2c_driver sc16is7xx_i2c_uart_driver = { |
---|
1487 | 1535 | .driver = { |
---|
1488 | 1536 | .name = SC16IS7XX_NAME, |
---|
1489 | | - .of_match_table = of_match_ptr(sc16is7xx_dt_ids), |
---|
| 1537 | + .of_match_table = sc16is7xx_dt_ids, |
---|
1490 | 1538 | }, |
---|
1491 | 1539 | .probe = sc16is7xx_i2c_probe, |
---|
1492 | 1540 | .remove = sc16is7xx_i2c_remove, |
---|
.. | .. |
---|
1524 | 1572 | |
---|
1525 | 1573 | #ifdef CONFIG_SERIAL_SC16IS7XX_SPI |
---|
1526 | 1574 | err_spi: |
---|
| 1575 | +#endif |
---|
1527 | 1576 | #ifdef CONFIG_SERIAL_SC16IS7XX_I2C |
---|
1528 | 1577 | i2c_del_driver(&sc16is7xx_i2c_uart_driver); |
---|
1529 | | -#endif |
---|
1530 | | -#endif |
---|
1531 | 1578 | err_i2c: |
---|
| 1579 | +#endif |
---|
1532 | 1580 | uart_unregister_driver(&sc16is7xx_uart); |
---|
1533 | 1581 | return ret; |
---|
1534 | 1582 | } |
---|