summaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c194
1 files changed, 139 insertions, 55 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 5e4654032bfa..81e4d9f7c0f4 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1090,6 +1090,60 @@ static int spi_transfer_wait(struct spi_controller *ctlr,
1090 return 0; 1090 return 0;
1091} 1091}
1092 1092
1093static void _spi_transfer_delay_ns(u32 ns)
1094{
1095 if (!ns)
1096 return;
1097 if (ns <= 1000) {
1098 ndelay(ns);
1099 } else {
1100 u32 us = DIV_ROUND_UP(ns, 1000);
1101
1102 if (us <= 10)
1103 udelay(us);
1104 else
1105 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1106 }
1107}
1108
1109static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1110 struct spi_transfer *xfer)
1111{
1112 u32 delay = xfer->cs_change_delay;
1113 u32 unit = xfer->cs_change_delay_unit;
1114 u32 hz;
1115
1116 /* return early on "fast" mode - for everything but USECS */
1117 if (!delay && unit != SPI_DELAY_UNIT_USECS)
1118 return;
1119
1120 switch (unit) {
1121 case SPI_DELAY_UNIT_USECS:
1122 /* for compatibility use default of 10us */
1123 if (!delay)
1124 delay = 10000;
1125 else
1126 delay *= 1000;
1127 break;
1128 case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1129 break;
1130 case SPI_DELAY_UNIT_SCK:
1131 /* if there is no effective speed know, then approximate
1132 * by underestimating with half the requested hz
1133 */
1134 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1135 delay *= DIV_ROUND_UP(1000000000, hz);
1136 break;
1137 default:
1138 dev_err_once(&msg->spi->dev,
1139 "Use of unsupported delay unit %i, using default of 10us\n",
1140 xfer->cs_change_delay_unit);
1141 delay = 10000;
1142 }
1143 /* now sleep for the requested amount of time */
1144 _spi_transfer_delay_ns(delay);
1145}
1146
1093/* 1147/*
1094 * spi_transfer_one_message - Default implementation of transfer_one_message() 1148 * spi_transfer_one_message - Default implementation of transfer_one_message()
1095 * 1149 *
@@ -1148,14 +1202,8 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
1148 if (msg->status != -EINPROGRESS) 1202 if (msg->status != -EINPROGRESS)
1149 goto out; 1203 goto out;
1150 1204
1151 if (xfer->delay_usecs) { 1205 if (xfer->delay_usecs)
1152 u16 us = xfer->delay_usecs; 1206 _spi_transfer_delay_ns(xfer->delay_usecs * 1000);
1153
1154 if (us <= 10)
1155 udelay(us);
1156 else
1157 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1158 }
1159 1207
1160 if (xfer->cs_change) { 1208 if (xfer->cs_change) {
1161 if (list_is_last(&xfer->transfer_list, 1209 if (list_is_last(&xfer->transfer_list,
@@ -1163,7 +1211,7 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
1163 keep_cs = true; 1211 keep_cs = true;
1164 } else { 1212 } else {
1165 spi_set_cs(msg->spi, false); 1213 spi_set_cs(msg->spi, false);
1166 udelay(10); 1214 _spi_transfer_cs_change_delay(msg, xfer);
1167 spi_set_cs(msg->spi, true); 1215 spi_set_cs(msg->spi, true);
1168 } 1216 }
1169 } 1217 }
@@ -1804,9 +1852,18 @@ static void of_register_spi_devices(struct spi_controller *ctlr) { }
1804#endif 1852#endif
1805 1853
1806#ifdef CONFIG_ACPI 1854#ifdef CONFIG_ACPI
1807static void acpi_spi_parse_apple_properties(struct spi_device *spi) 1855struct acpi_spi_lookup {
1856 struct spi_controller *ctlr;
1857 u32 max_speed_hz;
1858 u32 mode;
1859 int irq;
1860 u8 bits_per_word;
1861 u8 chip_select;
1862};
1863
1864static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
1865 struct acpi_spi_lookup *lookup)
1808{ 1866{
1809 struct acpi_device *dev = ACPI_COMPANION(&spi->dev);
1810 const union acpi_object *obj; 1867 const union acpi_object *obj;
1811 1868
1812 if (!x86_apple_machine) 1869 if (!x86_apple_machine)
@@ -1814,35 +1871,46 @@ static void acpi_spi_parse_apple_properties(struct spi_device *spi)
1814 1871
1815 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 1872 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
1816 && obj->buffer.length >= 4) 1873 && obj->buffer.length >= 4)
1817 spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 1874 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
1818 1875
1819 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 1876 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
1820 && obj->buffer.length == 8) 1877 && obj->buffer.length == 8)
1821 spi->bits_per_word = *(u64 *)obj->buffer.pointer; 1878 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
1822 1879
1823 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 1880 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
1824 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 1881 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1825 spi->mode |= SPI_LSB_FIRST; 1882 lookup->mode |= SPI_LSB_FIRST;
1826 1883
1827 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 1884 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
1828 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 1885 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1829 spi->mode |= SPI_CPOL; 1886 lookup->mode |= SPI_CPOL;
1830 1887
1831 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 1888 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
1832 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 1889 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1833 spi->mode |= SPI_CPHA; 1890 lookup->mode |= SPI_CPHA;
1834} 1891}
1835 1892
1836static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 1893static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1837{ 1894{
1838 struct spi_device *spi = data; 1895 struct acpi_spi_lookup *lookup = data;
1839 struct spi_controller *ctlr = spi->controller; 1896 struct spi_controller *ctlr = lookup->ctlr;
1840 1897
1841 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 1898 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1842 struct acpi_resource_spi_serialbus *sb; 1899 struct acpi_resource_spi_serialbus *sb;
1900 acpi_handle parent_handle;
1901 acpi_status status;
1843 1902
1844 sb = &ares->data.spi_serial_bus; 1903 sb = &ares->data.spi_serial_bus;
1845 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 1904 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1905
1906 status = acpi_get_handle(NULL,
1907 sb->resource_source.string_ptr,
1908 &parent_handle);
1909
1910 if (ACPI_FAILURE(status) ||
1911 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
1912 return -ENODEV;
1913
1846 /* 1914 /*
1847 * ACPI DeviceSelection numbering is handled by the 1915 * ACPI DeviceSelection numbering is handled by the
1848 * host controller driver in Windows and can vary 1916 * host controller driver in Windows and can vary
@@ -1855,25 +1923,25 @@ static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1855 sb->device_selection); 1923 sb->device_selection);
1856 if (cs < 0) 1924 if (cs < 0)
1857 return cs; 1925 return cs;
1858 spi->chip_select = cs; 1926 lookup->chip_select = cs;
1859 } else { 1927 } else {
1860 spi->chip_select = sb->device_selection; 1928 lookup->chip_select = sb->device_selection;
1861 } 1929 }
1862 1930
1863 spi->max_speed_hz = sb->connection_speed; 1931 lookup->max_speed_hz = sb->connection_speed;
1864 1932
1865 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 1933 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1866 spi->mode |= SPI_CPHA; 1934 lookup->mode |= SPI_CPHA;
1867 if (sb->clock_polarity == ACPI_SPI_START_HIGH) 1935 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1868 spi->mode |= SPI_CPOL; 1936 lookup->mode |= SPI_CPOL;
1869 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 1937 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1870 spi->mode |= SPI_CS_HIGH; 1938 lookup->mode |= SPI_CS_HIGH;
1871 } 1939 }
1872 } else if (spi->irq < 0) { 1940 } else if (lookup->irq < 0) {
1873 struct resource r; 1941 struct resource r;
1874 1942
1875 if (acpi_dev_resource_interrupt(ares, 0, &r)) 1943 if (acpi_dev_resource_interrupt(ares, 0, &r))
1876 spi->irq = r.start; 1944 lookup->irq = r.start;
1877 } 1945 }
1878 1946
1879 /* Always tell the ACPI core to skip this resource */ 1947 /* Always tell the ACPI core to skip this resource */
@@ -1883,7 +1951,9 @@ static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1883static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 1951static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
1884 struct acpi_device *adev) 1952 struct acpi_device *adev)
1885{ 1953{
1954 acpi_handle parent_handle = NULL;
1886 struct list_head resource_list; 1955 struct list_head resource_list;
1956 struct acpi_spi_lookup lookup = {};
1887 struct spi_device *spi; 1957 struct spi_device *spi;
1888 int ret; 1958 int ret;
1889 1959
@@ -1891,28 +1961,42 @@ static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
1891 acpi_device_enumerated(adev)) 1961 acpi_device_enumerated(adev))
1892 return AE_OK; 1962 return AE_OK;
1893 1963
1894 spi = spi_alloc_device(ctlr); 1964 lookup.ctlr = ctlr;
1895 if (!spi) { 1965 lookup.irq = -1;
1896 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
1897 dev_name(&adev->dev));
1898 return AE_NO_MEMORY;
1899 }
1900
1901 ACPI_COMPANION_SET(&spi->dev, adev);
1902 spi->irq = -1;
1903 1966
1904 INIT_LIST_HEAD(&resource_list); 1967 INIT_LIST_HEAD(&resource_list);
1905 ret = acpi_dev_get_resources(adev, &resource_list, 1968 ret = acpi_dev_get_resources(adev, &resource_list,
1906 acpi_spi_add_resource, spi); 1969 acpi_spi_add_resource, &lookup);
1907 acpi_dev_free_resource_list(&resource_list); 1970 acpi_dev_free_resource_list(&resource_list);
1908 1971
1909 acpi_spi_parse_apple_properties(spi); 1972 if (ret < 0)
1973 /* found SPI in _CRS but it points to another controller */
1974 return AE_OK;
1910 1975
1911 if (ret < 0 || !spi->max_speed_hz) { 1976 if (!lookup.max_speed_hz &&
1912 spi_dev_put(spi); 1977 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
1978 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
1979 /* Apple does not use _CRS but nested devices for SPI slaves */
1980 acpi_spi_parse_apple_properties(adev, &lookup);
1981 }
1982
1983 if (!lookup.max_speed_hz)
1913 return AE_OK; 1984 return AE_OK;
1985
1986 spi = spi_alloc_device(ctlr);
1987 if (!spi) {
1988 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
1989 dev_name(&adev->dev));
1990 return AE_NO_MEMORY;
1914 } 1991 }
1915 1992
1993 ACPI_COMPANION_SET(&spi->dev, adev);
1994 spi->max_speed_hz = lookup.max_speed_hz;
1995 spi->mode = lookup.mode;
1996 spi->irq = lookup.irq;
1997 spi->bits_per_word = lookup.bits_per_word;
1998 spi->chip_select = lookup.chip_select;
1999
1916 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 2000 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1917 sizeof(spi->modalias)); 2001 sizeof(spi->modalias));
1918 2002
@@ -1944,6 +2028,8 @@ static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1944 return acpi_register_spi_device(ctlr, adev); 2028 return acpi_register_spi_device(ctlr, adev);
1945} 2029}
1946 2030
2031#define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2032
1947static void acpi_register_spi_devices(struct spi_controller *ctlr) 2033static void acpi_register_spi_devices(struct spi_controller *ctlr)
1948{ 2034{
1949 acpi_status status; 2035 acpi_status status;
@@ -1953,7 +2039,8 @@ static void acpi_register_spi_devices(struct spi_controller *ctlr)
1953 if (!handle) 2039 if (!handle)
1954 return; 2040 return;
1955 2041
1956 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 2042 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2043 SPI_ACPI_ENUMERATE_MAX_DEPTH,
1957 acpi_spi_add_device, NULL, ctlr, NULL); 2044 acpi_spi_add_device, NULL, ctlr, NULL);
1958 if (ACPI_FAILURE(status)) 2045 if (ACPI_FAILURE(status))
1959 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 2046 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
@@ -2286,11 +2373,6 @@ int spi_register_controller(struct spi_controller *ctlr)
2286 if (status) 2373 if (status)
2287 return status; 2374 return status;
2288 2375
2289 /* even if it's just one always-selected device, there must
2290 * be at least one chipselect
2291 */
2292 if (ctlr->num_chipselect == 0)
2293 return -EINVAL;
2294 if (ctlr->bus_num >= 0) { 2376 if (ctlr->bus_num >= 0) {
2295 /* devices with a fixed bus num must check-in with the num */ 2377 /* devices with a fixed bus num must check-in with the num */
2296 mutex_lock(&board_lock); 2378 mutex_lock(&board_lock);
@@ -2361,6 +2443,13 @@ int spi_register_controller(struct spi_controller *ctlr)
2361 } 2443 }
2362 } 2444 }
2363 2445
2446 /*
2447 * Even if it's just one always-selected device, there must
2448 * be at least one chipselect.
2449 */
2450 if (!ctlr->num_chipselect)
2451 return -EINVAL;
2452
2364 status = device_add(&ctlr->dev); 2453 status = device_add(&ctlr->dev);
2365 if (status < 0) { 2454 if (status < 0) {
2366 /* free bus id */ 2455 /* free bus id */
@@ -2470,7 +2559,6 @@ void spi_unregister_controller(struct spi_controller *ctlr)
2470{ 2559{
2471 struct spi_controller *found; 2560 struct spi_controller *found;
2472 int id = ctlr->bus_num; 2561 int id = ctlr->bus_num;
2473 int dummy;
2474 2562
2475 /* First make sure that this controller was ever added */ 2563 /* First make sure that this controller was ever added */
2476 mutex_lock(&board_lock); 2564 mutex_lock(&board_lock);
@@ -2484,7 +2572,7 @@ void spi_unregister_controller(struct spi_controller *ctlr)
2484 list_del(&ctlr->list); 2572 list_del(&ctlr->list);
2485 mutex_unlock(&board_lock); 2573 mutex_unlock(&board_lock);
2486 2574
2487 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister); 2575 device_for_each_child(&ctlr->dev, NULL, __unregister);
2488 device_unregister(&ctlr->dev); 2576 device_unregister(&ctlr->dev);
2489 /* free bus id */ 2577 /* free bus id */
2490 mutex_lock(&board_lock); 2578 mutex_lock(&board_lock);
@@ -2633,12 +2721,9 @@ EXPORT_SYMBOL_GPL(spi_res_add);
2633 */ 2721 */
2634void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 2722void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
2635{ 2723{
2636 struct spi_res *res; 2724 struct spi_res *res, *tmp;
2637
2638 while (!list_empty(&message->resources)) {
2639 res = list_last_entry(&message->resources,
2640 struct spi_res, entry);
2641 2725
2726 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
2642 if (res->release) 2727 if (res->release)
2643 res->release(ctlr, message, res->data); 2728 res->release(ctlr, message, res->data);
2644 2729
@@ -2702,8 +2787,7 @@ struct spi_replaced_transfers *spi_replace_transfers(
2702 2787
2703 /* allocate the structure using spi_res */ 2788 /* allocate the structure using spi_res */
2704 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 2789 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2705 insert * sizeof(struct spi_transfer) 2790 struct_size(rxfer, inserted_transfers, insert)
2706 + sizeof(struct spi_replaced_transfers)
2707 + extradatasize, 2791 + extradatasize,
2708 gfp); 2792 gfp);
2709 if (!rxfer) 2793 if (!rxfer)
@@ -3083,6 +3167,7 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
3083 */ 3167 */
3084 message->frame_length = 0; 3168 message->frame_length = 0;
3085 list_for_each_entry(xfer, &message->transfers, transfer_list) { 3169 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3170 xfer->effective_speed_hz = 0;
3086 message->frame_length += xfer->len; 3171 message->frame_length += xfer->len;
3087 if (!xfer->bits_per_word) 3172 if (!xfer->bits_per_word)
3088 xfer->bits_per_word = spi->bits_per_word; 3173 xfer->bits_per_word = spi->bits_per_word;
@@ -3762,4 +3847,3 @@ err0:
3762 * include needing to have boardinfo data structures be much more public. 3847 * include needing to have boardinfo data structures be much more public.
3763 */ 3848 */
3764postcore_initcall(spi_init); 3849postcore_initcall(spi_init);
3765