spi.c (860dbce3d8dd90cb9e909c58fa79808766243651) spi.c (aef9752274f4045b0dab577e113da63c96832f77)
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>

--- 1076 unchanged lines hidden (view full) ---

1085 "SPI transfer timed out\n");
1086 return -ETIMEDOUT;
1087 }
1088 }
1089
1090 return 0;
1091}
1092
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>

--- 1076 unchanged lines hidden (view full) ---

1085 "SPI transfer timed out\n");
1086 return -ETIMEDOUT;
1087 }
1088 }
1089
1090 return 0;
1091}
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/*
1094 * spi_transfer_one_message - Default implementation of transfer_one_message()
1095 *
1096 * This is a standard implementation of transfer_one_message() for
1097 * drivers which implement a transfer_one() operation. It provides
1098 * standard handling of delays and chip select management.
1099 */
1100static int spi_transfer_one_message(struct spi_controller *ctlr,

--- 42 unchanged lines hidden (view full) ---

1143 xfer->len);
1144 }
1145
1146 trace_spi_transfer_stop(msg, xfer);
1147
1148 if (msg->status != -EINPROGRESS)
1149 goto out;
1150
1147/*
1148 * spi_transfer_one_message - Default implementation of transfer_one_message()
1149 *
1150 * This is a standard implementation of transfer_one_message() for
1151 * drivers which implement a transfer_one() operation. It provides
1152 * standard handling of delays and chip select management.
1153 */
1154static int spi_transfer_one_message(struct spi_controller *ctlr,

--- 42 unchanged lines hidden (view full) ---

1197 xfer->len);
1198 }
1199
1200 trace_spi_transfer_stop(msg, xfer);
1201
1202 if (msg->status != -EINPROGRESS)
1203 goto out;
1204
1151 if (xfer->delay_usecs) {
1152 u16 us = xfer->delay_usecs;
1205 if (xfer->delay_usecs)
1206 _spi_transfer_delay_ns(xfer->delay_usecs * 1000);
1153
1207
1154 if (us <= 10)
1155 udelay(us);
1156 else
1157 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1158 }
1159
1160 if (xfer->cs_change) {
1161 if (list_is_last(&xfer->transfer_list,
1162 &msg->transfers)) {
1163 keep_cs = true;
1164 } else {
1165 spi_set_cs(msg->spi, false);
1208 if (xfer->cs_change) {
1209 if (list_is_last(&xfer->transfer_list,
1210 &msg->transfers)) {
1211 keep_cs = true;
1212 } else {
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);
1168 }
1169 }
1170
1171 msg->actual_length += xfer->len;
1172 }
1173
1174out:
1175 if (ret != 0 || !keep_cs)
1176 spi_set_cs(msg->spi, false);
1177
1178 if (msg->status == -EINPROGRESS)
1179 msg->status = ret;
1180
1181 if (msg->status && ctlr->handle_err)
1182 ctlr->handle_err(ctlr, msg);
1183
1215 spi_set_cs(msg->spi, true);
1216 }
1217 }
1218
1219 msg->actual_length += xfer->len;
1220 }
1221
1222out:
1223 if (ret != 0 || !keep_cs)
1224 spi_set_cs(msg->spi, false);
1225
1226 if (msg->status == -EINPROGRESS)
1227 msg->status = ret;
1228
1229 if (msg->status && ctlr->handle_err)
1230 ctlr->handle_err(ctlr, msg);
1231
1184 spi_finalize_current_message(ctlr);
1185
1186 spi_res_release(ctlr, msg);
1187
1232 spi_res_release(ctlr, msg);
1233
1234 spi_finalize_current_message(ctlr);
1235
1188 return ret;
1189}
1190
1191/**
1192 * spi_finalize_current_transfer - report completion of a transfer
1193 * @ctlr: the controller reporting completion
1194 *
1195 * Called by SPI drivers using the core transfer_one_message()

--- 106 unchanged lines hidden (view full) ---

1302
1303 if (!was_busy)
1304 trace_spi_controller_busy(ctlr);
1305
1306 if (!was_busy && ctlr->prepare_transfer_hardware) {
1307 ret = ctlr->prepare_transfer_hardware(ctlr);
1308 if (ret) {
1309 dev_err(&ctlr->dev,
1236 return ret;
1237}
1238
1239/**
1240 * spi_finalize_current_transfer - report completion of a transfer
1241 * @ctlr: the controller reporting completion
1242 *
1243 * Called by SPI drivers using the core transfer_one_message()

--- 106 unchanged lines hidden (view full) ---

1350
1351 if (!was_busy)
1352 trace_spi_controller_busy(ctlr);
1353
1354 if (!was_busy && ctlr->prepare_transfer_hardware) {
1355 ret = ctlr->prepare_transfer_hardware(ctlr);
1356 if (ret) {
1357 dev_err(&ctlr->dev,
1310 "failed to prepare transfer hardware\n");
1358 "failed to prepare transfer hardware: %d\n",
1359 ret);
1311
1312 if (ctlr->auto_runtime_pm)
1313 pm_runtime_put(ctlr->dev.parent);
1360
1361 if (ctlr->auto_runtime_pm)
1362 pm_runtime_put(ctlr->dev.parent);
1363
1364 ctlr->cur_msg->status = ret;
1365 spi_finalize_current_message(ctlr);
1366
1314 mutex_unlock(&ctlr->io_mutex);
1315 return;
1316 }
1317 }
1318
1319 trace_spi_message_start(ctlr->cur_msg);
1320
1321 if (ctlr->prepare_message) {

--- 1370 unchanged lines hidden (view full) ---

2692 gfp_t gfp)
2693{
2694 struct spi_replaced_transfers *rxfer;
2695 struct spi_transfer *xfer;
2696 size_t i;
2697
2698 /* allocate the structure using spi_res */
2699 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
1367 mutex_unlock(&ctlr->io_mutex);
1368 return;
1369 }
1370 }
1371
1372 trace_spi_message_start(ctlr->cur_msg);
1373
1374 if (ctlr->prepare_message) {

--- 1370 unchanged lines hidden (view full) ---

2745 gfp_t gfp)
2746{
2747 struct spi_replaced_transfers *rxfer;
2748 struct spi_transfer *xfer;
2749 size_t i;
2750
2751 /* allocate the structure using spi_res */
2752 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2700 insert * sizeof(struct spi_transfer)
2701 + sizeof(struct spi_replaced_transfers)
2753 struct_size(rxfer, inserted_transfers, insert)
2702 + extradatasize,
2703 gfp);
2704 if (!rxfer)
2705 return ERR_PTR(-ENOMEM);
2706
2707 /* the release code to invoke before running the generic release */
2708 rxfer->release = release;
2709

--- 363 unchanged lines hidden (view full) ---

3073 * it is not set for this transfer.
3074 * Set transfer tx_nbits and rx_nbits as single transfer default
3075 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
3076 * Ensure transfer word_delay is at least as long as that required by
3077 * device itself.
3078 */
3079 message->frame_length = 0;
3080 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2754 + extradatasize,
2755 gfp);
2756 if (!rxfer)
2757 return ERR_PTR(-ENOMEM);
2758
2759 /* the release code to invoke before running the generic release */
2760 rxfer->release = release;
2761

--- 363 unchanged lines hidden (view full) ---

3125 * it is not set for this transfer.
3126 * Set transfer tx_nbits and rx_nbits as single transfer default
3127 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
3128 * Ensure transfer word_delay is at least as long as that required by
3129 * device itself.
3130 */
3131 message->frame_length = 0;
3132 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3133 xfer->effective_speed_hz = 0;
3081 message->frame_length += xfer->len;
3082 if (!xfer->bits_per_word)
3083 xfer->bits_per_word = spi->bits_per_word;
3084
3085 if (!xfer->speed_hz)
3086 xfer->speed_hz = spi->max_speed_hz;
3087
3088 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)

--- 663 unchanged lines hidden (view full) ---

3752/* board_info is normally registered in arch_initcall(),
3753 * but even essential drivers wait till later
3754 *
3755 * REVISIT only boardinfo really needs static linking. the rest (device and
3756 * driver registration) _could_ be dynamically linked (modular) ... costs
3757 * include needing to have boardinfo data structures be much more public.
3758 */
3759postcore_initcall(spi_init);
3134 message->frame_length += xfer->len;
3135 if (!xfer->bits_per_word)
3136 xfer->bits_per_word = spi->bits_per_word;
3137
3138 if (!xfer->speed_hz)
3139 xfer->speed_hz = spi->max_speed_hz;
3140
3141 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)

--- 663 unchanged lines hidden (view full) ---

3805/* board_info is normally registered in arch_initcall(),
3806 * but even essential drivers wait till later
3807 *
3808 * REVISIT only boardinfo really needs static linking. the rest (device and
3809 * driver registration) _could_ be dynamically linked (modular) ... costs
3810 * include needing to have boardinfo data structures be much more public.
3811 */
3812postcore_initcall(spi_init);
3760