spi.c (74c12ee02af109adcde36ec184fa59c0afb0edaa) | spi.c (f9981d4f50b475d7dbb70f3022b87a3c8bba9fd6) |
---|---|
1// SPDX-License-Identifier: GPL-2.0-or-later 2// SPI init/core code 3// 4// Copyright (C) 2005 David Brownell 5// Copyright (C) 2008 Secret Lab Technologies Ltd. 6 7#include <linux/kernel.h> 8#include <linux/device.h> --- 1485 unchanged lines hidden (view full) --- 1494 * transfer, once per batch of words etc) is arbitrary 1495 * as long as the @tx buffer offset is greater than or 1496 * equal to the requested byte at the time of the 1497 * call. The timestamp is only taken once, at the 1498 * first such call. It is assumed that the driver 1499 * advances its @tx buffer pointer monotonically. 1500 * @ctlr: Pointer to the spi_controller structure of the driver 1501 * @xfer: Pointer to the transfer being timestamped | 1// SPDX-License-Identifier: GPL-2.0-or-later 2// SPI init/core code 3// 4// Copyright (C) 2005 David Brownell 5// Copyright (C) 2008 Secret Lab Technologies Ltd. 6 7#include <linux/kernel.h> 8#include <linux/device.h> --- 1485 unchanged lines hidden (view full) --- 1494 * transfer, once per batch of words etc) is arbitrary 1495 * as long as the @tx buffer offset is greater than or 1496 * equal to the requested byte at the time of the 1497 * call. The timestamp is only taken once, at the 1498 * first such call. It is assumed that the driver 1499 * advances its @tx buffer pointer monotonically. 1500 * @ctlr: Pointer to the spi_controller structure of the driver 1501 * @xfer: Pointer to the transfer being timestamped |
1502 * @progress: How many words (not bytes) have been transferred so far | 1502 * @tx: Pointer to the current word within the xfer->tx_buf that the driver is 1503 * preparing to transmit right now. |
1503 * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1504 * transfer, for less jitter in time measurement. Only compatible 1505 * with PIO drivers. If true, must follow up with 1506 * spi_take_timestamp_post or otherwise system will crash. 1507 * WARNING: for fully predictable results, the CPU frequency must 1508 * also be under control (governor). 1509 */ 1510void spi_take_timestamp_pre(struct spi_controller *ctlr, 1511 struct spi_transfer *xfer, | 1504 * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1505 * transfer, for less jitter in time measurement. Only compatible 1506 * with PIO drivers. If true, must follow up with 1507 * spi_take_timestamp_post or otherwise system will crash. 1508 * WARNING: for fully predictable results, the CPU frequency must 1509 * also be under control (governor). 1510 */ 1511void spi_take_timestamp_pre(struct spi_controller *ctlr, 1512 struct spi_transfer *xfer, |
1512 size_t progress, bool irqs_off) | 1513 const void *tx, bool irqs_off) |
1513{ | 1514{ |
1515 u8 bytes_per_word = DIV_ROUND_UP(xfer->bits_per_word, 8); 1516 |
|
1514 if (!xfer->ptp_sts) 1515 return; 1516 1517 if (xfer->timestamped_pre) 1518 return; 1519 | 1517 if (!xfer->ptp_sts) 1518 return; 1519 1520 if (xfer->timestamped_pre) 1521 return; 1522 |
1520 if (progress < xfer->ptp_sts_word_pre) | 1523 if (tx < (xfer->tx_buf + xfer->ptp_sts_word_pre * bytes_per_word)) |
1521 return; 1522 1523 /* Capture the resolution of the timestamp */ | 1524 return; 1525 1526 /* Capture the resolution of the timestamp */ |
1524 xfer->ptp_sts_word_pre = progress; | 1527 xfer->ptp_sts_word_pre = (tx - xfer->tx_buf) / bytes_per_word; |
1525 1526 xfer->timestamped_pre = true; 1527 1528 if (irqs_off) { 1529 local_irq_save(ctlr->irq_flags); 1530 preempt_disable(); 1531 } 1532 --- 5 unchanged lines hidden (view full) --- 1538 * spi_take_timestamp_post - helper for drivers to collect the end of the 1539 * TX timestamp for the requested byte from the SPI 1540 * transfer. Can be called with an arbitrary 1541 * frequency: only the first call where @tx exceeds 1542 * or is equal to the requested word will be 1543 * timestamped. 1544 * @ctlr: Pointer to the spi_controller structure of the driver 1545 * @xfer: Pointer to the transfer being timestamped | 1528 1529 xfer->timestamped_pre = true; 1530 1531 if (irqs_off) { 1532 local_irq_save(ctlr->irq_flags); 1533 preempt_disable(); 1534 } 1535 --- 5 unchanged lines hidden (view full) --- 1541 * spi_take_timestamp_post - helper for drivers to collect the end of the 1542 * TX timestamp for the requested byte from the SPI 1543 * transfer. Can be called with an arbitrary 1544 * frequency: only the first call where @tx exceeds 1545 * or is equal to the requested word will be 1546 * timestamped. 1547 * @ctlr: Pointer to the spi_controller structure of the driver 1548 * @xfer: Pointer to the transfer being timestamped |
1546 * @progress: How many words (not bytes) have been transferred so far | 1549 * @tx: Pointer to the current word within the xfer->tx_buf that the driver has 1550 * just transmitted. |
1547 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1548 */ 1549void spi_take_timestamp_post(struct spi_controller *ctlr, 1550 struct spi_transfer *xfer, | 1551 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1552 */ 1553void spi_take_timestamp_post(struct spi_controller *ctlr, 1554 struct spi_transfer *xfer, |
1551 size_t progress, bool irqs_off) | 1555 const void *tx, bool irqs_off) |
1552{ | 1556{ |
1557 u8 bytes_per_word = DIV_ROUND_UP(xfer->bits_per_word, 8); 1558 |
|
1553 if (!xfer->ptp_sts) 1554 return; 1555 1556 if (xfer->timestamped_post) 1557 return; 1558 | 1559 if (!xfer->ptp_sts) 1560 return; 1561 1562 if (xfer->timestamped_post) 1563 return; 1564 |
1559 if (progress < xfer->ptp_sts_word_post) | 1565 if (tx < (xfer->tx_buf + xfer->ptp_sts_word_post * bytes_per_word)) |
1560 return; 1561 1562 ptp_read_system_postts(xfer->ptp_sts); 1563 1564 if (irqs_off) { 1565 local_irq_restore(ctlr->irq_flags); 1566 preempt_enable(); 1567 } 1568 1569 /* Capture the resolution of the timestamp */ | 1566 return; 1567 1568 ptp_read_system_postts(xfer->ptp_sts); 1569 1570 if (irqs_off) { 1571 local_irq_restore(ctlr->irq_flags); 1572 preempt_enable(); 1573 } 1574 1575 /* Capture the resolution of the timestamp */ |
1570 xfer->ptp_sts_word_post = progress; | 1576 xfer->ptp_sts_word_post = (tx - xfer->tx_buf) / bytes_per_word; |
1571 1572 xfer->timestamped_post = true; 1573} 1574EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1575 1576/** 1577 * spi_set_thread_rt - set the controller to pump at realtime priority 1578 * @ctlr: controller to boost priority of --- 1055 unchanged lines hidden (view full) --- 2634 * registration fails if the bus ID is in use. 2635 */ 2636 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 2637 2638 if (!spi_controller_is_slave(ctlr)) { 2639 if (ctlr->use_gpio_descriptors) { 2640 status = spi_get_gpio_descs(ctlr); 2641 if (status) | 1577 1578 xfer->timestamped_post = true; 1579} 1580EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1581 1582/** 1583 * spi_set_thread_rt - set the controller to pump at realtime priority 1584 * @ctlr: controller to boost priority of --- 1055 unchanged lines hidden (view full) --- 2640 * registration fails if the bus ID is in use. 2641 */ 2642 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 2643 2644 if (!spi_controller_is_slave(ctlr)) { 2645 if (ctlr->use_gpio_descriptors) { 2646 status = spi_get_gpio_descs(ctlr); 2647 if (status) |
2642 return status; | 2648 goto free_bus_id; |
2643 /* 2644 * A controller using GPIO descriptors always 2645 * supports SPI_CS_HIGH if need be. 2646 */ 2647 ctlr->mode_bits |= SPI_CS_HIGH; 2648 } else { 2649 /* Legacy code path for GPIOs from DT */ 2650 status = of_spi_get_gpio_numbers(ctlr); 2651 if (status) | 2649 /* 2650 * A controller using GPIO descriptors always 2651 * supports SPI_CS_HIGH if need be. 2652 */ 2653 ctlr->mode_bits |= SPI_CS_HIGH; 2654 } else { 2655 /* Legacy code path for GPIOs from DT */ 2656 status = of_spi_get_gpio_numbers(ctlr); 2657 if (status) |
2652 return status; | 2658 goto free_bus_id; |
2653 } 2654 } 2655 2656 /* 2657 * Even if it's just one always-selected device, there must 2658 * be at least one chipselect. 2659 */ | 2659 } 2660 } 2661 2662 /* 2663 * Even if it's just one always-selected device, there must 2664 * be at least one chipselect. 2665 */ |
2660 if (!ctlr->num_chipselect) 2661 return -EINVAL; | 2666 if (!ctlr->num_chipselect) { 2667 status = -EINVAL; 2668 goto free_bus_id; 2669 } |
2662 2663 status = device_add(&ctlr->dev); | 2670 2671 status = device_add(&ctlr->dev); |
2664 if (status < 0) { 2665 /* free bus id */ 2666 mutex_lock(&board_lock); 2667 idr_remove(&spi_master_idr, ctlr->bus_num); 2668 mutex_unlock(&board_lock); 2669 goto done; 2670 } | 2672 if (status < 0) 2673 goto free_bus_id; |
2671 dev_dbg(dev, "registered %s %s\n", 2672 spi_controller_is_slave(ctlr) ? "slave" : "master", 2673 dev_name(&ctlr->dev)); 2674 2675 /* 2676 * If we're using a queued driver, start the queue. Note that we don't 2677 * need the queueing logic if the driver is only supporting high-level 2678 * memory operations. 2679 */ 2680 if (ctlr->transfer) { 2681 dev_info(dev, "controller is unqueued, this is deprecated\n"); 2682 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 2683 status = spi_controller_initialize_queue(ctlr); 2684 if (status) { 2685 device_del(&ctlr->dev); | 2674 dev_dbg(dev, "registered %s %s\n", 2675 spi_controller_is_slave(ctlr) ? "slave" : "master", 2676 dev_name(&ctlr->dev)); 2677 2678 /* 2679 * If we're using a queued driver, start the queue. Note that we don't 2680 * need the queueing logic if the driver is only supporting high-level 2681 * memory operations. 2682 */ 2683 if (ctlr->transfer) { 2684 dev_info(dev, "controller is unqueued, this is deprecated\n"); 2685 } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 2686 status = spi_controller_initialize_queue(ctlr); 2687 if (status) { 2688 device_del(&ctlr->dev); |
2686 /* free bus id */ 2687 mutex_lock(&board_lock); 2688 idr_remove(&spi_master_idr, ctlr->bus_num); 2689 mutex_unlock(&board_lock); 2690 goto done; | 2689 goto free_bus_id; |
2691 } 2692 } 2693 /* add statistics */ 2694 spin_lock_init(&ctlr->statistics.lock); 2695 2696 mutex_lock(&board_lock); 2697 list_add_tail(&ctlr->list, &spi_controller_list); 2698 list_for_each_entry(bi, &board_list, list) 2699 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 2700 mutex_unlock(&board_lock); 2701 2702 /* Register devices from the device tree and ACPI */ 2703 of_register_spi_devices(ctlr); 2704 acpi_register_spi_devices(ctlr); | 2690 } 2691 } 2692 /* add statistics */ 2693 spin_lock_init(&ctlr->statistics.lock); 2694 2695 mutex_lock(&board_lock); 2696 list_add_tail(&ctlr->list, &spi_controller_list); 2697 list_for_each_entry(bi, &board_list, list) 2698 spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 2699 mutex_unlock(&board_lock); 2700 2701 /* Register devices from the device tree and ACPI */ 2702 of_register_spi_devices(ctlr); 2703 acpi_register_spi_devices(ctlr); |
2705done: | |
2706 return status; | 2704 return status; |
2705 2706free_bus_id: 2707 mutex_lock(&board_lock); 2708 idr_remove(&spi_master_idr, ctlr->bus_num); 2709 mutex_unlock(&board_lock); 2710 return status; |
|
2707} 2708EXPORT_SYMBOL_GPL(spi_register_controller); 2709 2710static void devm_spi_unregister(struct device *dev, void *res) 2711{ 2712 spi_unregister_controller(*(struct spi_controller **)res); 2713} 2714 --- 1416 unchanged lines hidden --- | 2711} 2712EXPORT_SYMBOL_GPL(spi_register_controller); 2713 2714static void devm_spi_unregister(struct device *dev, void *res) 2715{ 2716 spi_unregister_controller(*(struct spi_controller **)res); 2717} 2718 --- 1416 unchanged lines hidden --- |