File Index
Files and directories
Sign in
FILE

spi.c

File preview with syntax highlighting.

linux-6.19.14/drivers/spi/spi.c C
   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/acpi.h>
   8#include <linux/cache.h>
   9#include <linux/clk/clk-conf.h>
  10#include <linux/delay.h>
  11#include <linux/device.h>
  12#include <linux/dmaengine.h>
  13#include <linux/dma-mapping.h>
  14#include <linux/export.h>
  15#include <linux/gpio/consumer.h>
  16#include <linux/highmem.h>
  17#include <linux/idr.h>
  18#include <linux/init.h>
  19#include <linux/ioport.h>
  20#include <linux/kernel.h>
  21#include <linux/kthread.h>
  22#include <linux/mod_devicetable.h>
  23#include <linux/mutex.h>
  24#include <linux/of_device.h>
  25#include <linux/of_irq.h>
  26#include <linux/percpu.h>
  27#include <linux/platform_data/x86/apple.h>
  28#include <linux/pm_domain.h>
  29#include <linux/pm_runtime.h>
  30#include <linux/property.h>
  31#include <linux/ptp_clock_kernel.h>
  32#include <linux/sched/rt.h>
  33#include <linux/slab.h>
  34#include <linux/spi/offload/types.h>
  35#include <linux/spi/spi.h>
  36#include <linux/spi/spi-mem.h>
  37#include <uapi/linux/sched/types.h>
  38
  39#define CREATE_TRACE_POINTS
  40#include <trace/events/spi.h>
  41EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
  42EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
  43
  44#include "internals.h"
  45
  46static DEFINE_IDR(spi_controller_idr);
  47
  48static void spidev_release(struct device *dev)
  49{
  50	struct spi_device	*spi = to_spi_device(dev);
  51
  52	spi_controller_put(spi->controller);
  53	free_percpu(spi->pcpu_statistics);
  54	kfree(spi);
  55}
  56
  57static ssize_t
  58modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  59{
  60	const struct spi_device	*spi = to_spi_device(dev);
  61	int len;
  62
  63	len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
  64	if (len != -ENODEV)
  65		return len;
  66
  67	return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
  68}
  69static DEVICE_ATTR_RO(modalias);
  70
  71static ssize_t driver_override_store(struct device *dev,
  72				     struct device_attribute *a,
  73				     const char *buf, size_t count)
  74{
  75	int ret;
  76
  77	ret = __device_set_driver_override(dev, buf, count);
  78	if (ret)
  79		return ret;
  80
  81	return count;
  82}
  83
  84static ssize_t driver_override_show(struct device *dev,
  85				    struct device_attribute *a, char *buf)
  86{
  87	guard(spinlock)(&dev->driver_override.lock);
  88	return sysfs_emit(buf, "%s\n", dev->driver_override.name ?: "");
  89}
  90static DEVICE_ATTR_RW(driver_override);
  91
  92static struct spi_statistics __percpu *spi_alloc_pcpu_stats(struct device *dev)
  93{
  94	struct spi_statistics __percpu *pcpu_stats;
  95
  96	if (dev)
  97		pcpu_stats = devm_alloc_percpu(dev, struct spi_statistics);
  98	else
  99		pcpu_stats = alloc_percpu_gfp(struct spi_statistics, GFP_KERNEL);
 100
 101	if (pcpu_stats) {
 102		int cpu;
 103
 104		for_each_possible_cpu(cpu) {
 105			struct spi_statistics *stat;
 106
 107			stat = per_cpu_ptr(pcpu_stats, cpu);
 108			u64_stats_init(&stat->syncp);
 109		}
 110	}
 111	return pcpu_stats;
 112}
 113
 114static ssize_t spi_emit_pcpu_stats(struct spi_statistics __percpu *stat,
 115				   char *buf, size_t offset)
 116{
 117	u64 val = 0;
 118	int i;
 119
 120	for_each_possible_cpu(i) {
 121		const struct spi_statistics *pcpu_stats;
 122		u64_stats_t *field;
 123		unsigned int start;
 124		u64 inc;
 125
 126		pcpu_stats = per_cpu_ptr(stat, i);
 127		field = (void *)pcpu_stats + offset;
 128		do {
 129			start = u64_stats_fetch_begin(&pcpu_stats->syncp);
 130			inc = u64_stats_read(field);
 131		} while (u64_stats_fetch_retry(&pcpu_stats->syncp, start));
 132		val += inc;
 133	}
 134	return sysfs_emit(buf, "%llu\n", val);
 135}
 136
 137#define SPI_STATISTICS_ATTRS(field, file)				\
 138static ssize_t spi_controller_##field##_show(struct device *dev,	\
 139					     struct device_attribute *attr, \
 140					     char *buf)			\
 141{									\
 142	struct spi_controller *ctlr = container_of(dev,			\
 143					 struct spi_controller, dev);	\
 144	return spi_statistics_##field##_show(ctlr->pcpu_statistics, buf); \
 145}									\
 146static struct device_attribute dev_attr_spi_controller_##field = {	\
 147	.attr = { .name = file, .mode = 0444 },				\
 148	.show = spi_controller_##field##_show,				\
 149};									\
 150static ssize_t spi_device_##field##_show(struct device *dev,		\
 151					 struct device_attribute *attr,	\
 152					char *buf)			\
 153{									\
 154	struct spi_device *spi = to_spi_device(dev);			\
 155	return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \
 156}									\
 157static struct device_attribute dev_attr_spi_device_##field = {		\
 158	.attr = { .name = file, .mode = 0444 },				\
 159	.show = spi_device_##field##_show,				\
 160}
 161
 162#define SPI_STATISTICS_SHOW_NAME(name, file, field)			\
 163static ssize_t spi_statistics_##name##_show(struct spi_statistics __percpu *stat, \
 164					    char *buf)			\
 165{									\
 166	return spi_emit_pcpu_stats(stat, buf,				\
 167			offsetof(struct spi_statistics, field));	\
 168}									\
 169SPI_STATISTICS_ATTRS(name, file)
 170
 171#define SPI_STATISTICS_SHOW(field)					\
 172	SPI_STATISTICS_SHOW_NAME(field, __stringify(field),		\
 173				 field)
 174
 175SPI_STATISTICS_SHOW(messages);
 176SPI_STATISTICS_SHOW(transfers);
 177SPI_STATISTICS_SHOW(errors);
 178SPI_STATISTICS_SHOW(timedout);
 179
 180SPI_STATISTICS_SHOW(spi_sync);
 181SPI_STATISTICS_SHOW(spi_sync_immediate);
 182SPI_STATISTICS_SHOW(spi_async);
 183
 184SPI_STATISTICS_SHOW(bytes);
 185SPI_STATISTICS_SHOW(bytes_rx);
 186SPI_STATISTICS_SHOW(bytes_tx);
 187
 188#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number)		\
 189	SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index,		\
 190				 "transfer_bytes_histo_" number,	\
 191				 transfer_bytes_histo[index])
 192SPI_STATISTICS_TRANSFER_BYTES_HISTO(0,  "0-1");
 193SPI_STATISTICS_TRANSFER_BYTES_HISTO(1,  "2-3");
 194SPI_STATISTICS_TRANSFER_BYTES_HISTO(2,  "4-7");
 195SPI_STATISTICS_TRANSFER_BYTES_HISTO(3,  "8-15");
 196SPI_STATISTICS_TRANSFER_BYTES_HISTO(4,  "16-31");
 197SPI_STATISTICS_TRANSFER_BYTES_HISTO(5,  "32-63");
 198SPI_STATISTICS_TRANSFER_BYTES_HISTO(6,  "64-127");
 199SPI_STATISTICS_TRANSFER_BYTES_HISTO(7,  "128-255");
 200SPI_STATISTICS_TRANSFER_BYTES_HISTO(8,  "256-511");
 201SPI_STATISTICS_TRANSFER_BYTES_HISTO(9,  "512-1023");
 202SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
 203SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
 204SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
 205SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
 206SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
 207SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
 208SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
 209
 210SPI_STATISTICS_SHOW(transfers_split_maxsize);
 211
 212static struct attribute *spi_dev_attrs[] = {
 213	&dev_attr_modalias.attr,
 214	&dev_attr_driver_override.attr,
 215	NULL,
 216};
 217
 218static const struct attribute_group spi_dev_group = {
 219	.attrs  = spi_dev_attrs,
 220};
 221
 222static struct attribute *spi_device_statistics_attrs[] = {
 223	&dev_attr_spi_device_messages.attr,
 224	&dev_attr_spi_device_transfers.attr,
 225	&dev_attr_spi_device_errors.attr,
 226	&dev_attr_spi_device_timedout.attr,
 227	&dev_attr_spi_device_spi_sync.attr,
 228	&dev_attr_spi_device_spi_sync_immediate.attr,
 229	&dev_attr_spi_device_spi_async.attr,
 230	&dev_attr_spi_device_bytes.attr,
 231	&dev_attr_spi_device_bytes_rx.attr,
 232	&dev_attr_spi_device_bytes_tx.attr,
 233	&dev_attr_spi_device_transfer_bytes_histo0.attr,
 234	&dev_attr_spi_device_transfer_bytes_histo1.attr,
 235	&dev_attr_spi_device_transfer_bytes_histo2.attr,
 236	&dev_attr_spi_device_transfer_bytes_histo3.attr,
 237	&dev_attr_spi_device_transfer_bytes_histo4.attr,
 238	&dev_attr_spi_device_transfer_bytes_histo5.attr,
 239	&dev_attr_spi_device_transfer_bytes_histo6.attr,
 240	&dev_attr_spi_device_transfer_bytes_histo7.attr,
 241	&dev_attr_spi_device_transfer_bytes_histo8.attr,
 242	&dev_attr_spi_device_transfer_bytes_histo9.attr,
 243	&dev_attr_spi_device_transfer_bytes_histo10.attr,
 244	&dev_attr_spi_device_transfer_bytes_histo11.attr,
 245	&dev_attr_spi_device_transfer_bytes_histo12.attr,
 246	&dev_attr_spi_device_transfer_bytes_histo13.attr,
 247	&dev_attr_spi_device_transfer_bytes_histo14.attr,
 248	&dev_attr_spi_device_transfer_bytes_histo15.attr,
 249	&dev_attr_spi_device_transfer_bytes_histo16.attr,
 250	&dev_attr_spi_device_transfers_split_maxsize.attr,
 251	NULL,
 252};
 253
 254static const struct attribute_group spi_device_statistics_group = {
 255	.name  = "statistics",
 256	.attrs  = spi_device_statistics_attrs,
 257};
 258
 259static const struct attribute_group *spi_dev_groups[] = {
 260	&spi_dev_group,
 261	&spi_device_statistics_group,
 262	NULL,
 263};
 264
 265static struct attribute *spi_controller_statistics_attrs[] = {
 266	&dev_attr_spi_controller_messages.attr,
 267	&dev_attr_spi_controller_transfers.attr,
 268	&dev_attr_spi_controller_errors.attr,
 269	&dev_attr_spi_controller_timedout.attr,
 270	&dev_attr_spi_controller_spi_sync.attr,
 271	&dev_attr_spi_controller_spi_sync_immediate.attr,
 272	&dev_attr_spi_controller_spi_async.attr,
 273	&dev_attr_spi_controller_bytes.attr,
 274	&dev_attr_spi_controller_bytes_rx.attr,
 275	&dev_attr_spi_controller_bytes_tx.attr,
 276	&dev_attr_spi_controller_transfer_bytes_histo0.attr,
 277	&dev_attr_spi_controller_transfer_bytes_histo1.attr,
 278	&dev_attr_spi_controller_transfer_bytes_histo2.attr,
 279	&dev_attr_spi_controller_transfer_bytes_histo3.attr,
 280	&dev_attr_spi_controller_transfer_bytes_histo4.attr,
 281	&dev_attr_spi_controller_transfer_bytes_histo5.attr,
 282	&dev_attr_spi_controller_transfer_bytes_histo6.attr,
 283	&dev_attr_spi_controller_transfer_bytes_histo7.attr,
 284	&dev_attr_spi_controller_transfer_bytes_histo8.attr,
 285	&dev_attr_spi_controller_transfer_bytes_histo9.attr,
 286	&dev_attr_spi_controller_transfer_bytes_histo10.attr,
 287	&dev_attr_spi_controller_transfer_bytes_histo11.attr,
 288	&dev_attr_spi_controller_transfer_bytes_histo12.attr,
 289	&dev_attr_spi_controller_transfer_bytes_histo13.attr,
 290	&dev_attr_spi_controller_transfer_bytes_histo14.attr,
 291	&dev_attr_spi_controller_transfer_bytes_histo15.attr,
 292	&dev_attr_spi_controller_transfer_bytes_histo16.attr,
 293	&dev_attr_spi_controller_transfers_split_maxsize.attr,
 294	NULL,
 295};
 296
 297static const struct attribute_group spi_controller_statistics_group = {
 298	.name  = "statistics",
 299	.attrs  = spi_controller_statistics_attrs,
 300};
 301
 302static const struct attribute_group *spi_controller_groups[] = {
 303	&spi_controller_statistics_group,
 304	NULL,
 305};
 306
 307static void spi_statistics_add_transfer_stats(struct spi_statistics __percpu *pcpu_stats,
 308					      struct spi_transfer *xfer,
 309					      struct spi_message *msg)
 310{
 311	int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
 312	struct spi_statistics *stats;
 313
 314	if (l2len < 0)
 315		l2len = 0;
 316
 317	get_cpu();
 318	stats = this_cpu_ptr(pcpu_stats);
 319	u64_stats_update_begin(&stats->syncp);
 320
 321	u64_stats_inc(&stats->transfers);
 322	u64_stats_inc(&stats->transfer_bytes_histo[l2len]);
 323
 324	u64_stats_add(&stats->bytes, xfer->len);
 325	if (spi_valid_txbuf(msg, xfer))
 326		u64_stats_add(&stats->bytes_tx, xfer->len);
 327	if (spi_valid_rxbuf(msg, xfer))
 328		u64_stats_add(&stats->bytes_rx, xfer->len);
 329
 330	u64_stats_update_end(&stats->syncp);
 331	put_cpu();
 332}
 333
 334/*
 335 * modalias support makes "modprobe $MODALIAS" new-style hotplug work,
 336 * and the sysfs version makes coldplug work too.
 337 */
 338static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name)
 339{
 340	while (id->name[0]) {
 341		if (!strcmp(name, id->name))
 342			return id;
 343		id++;
 344	}
 345	return NULL;
 346}
 347
 348const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
 349{
 350	const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
 351
 352	return spi_match_id(sdrv->id_table, sdev->modalias);
 353}
 354EXPORT_SYMBOL_GPL(spi_get_device_id);
 355
 356const void *spi_get_device_match_data(const struct spi_device *sdev)
 357{
 358	const void *match;
 359
 360	match = device_get_match_data(&sdev->dev);
 361	if (match)
 362		return match;
 363
 364	return (const void *)spi_get_device_id(sdev)->driver_data;
 365}
 366EXPORT_SYMBOL_GPL(spi_get_device_match_data);
 367
 368static int spi_match_device(struct device *dev, const struct device_driver *drv)
 369{
 370	const struct spi_device	*spi = to_spi_device(dev);
 371	const struct spi_driver	*sdrv = to_spi_driver(drv);
 372	int ret;
 373
 374	/* Check override first, and if set, only use the named driver */
 375	ret = device_match_driver_override(dev, drv);
 376	if (ret >= 0)
 377		return ret;
 378
 379	/* Attempt an OF style match */
 380	if (of_driver_match_device(dev, drv))
 381		return 1;
 382
 383	/* Then try ACPI */
 384	if (acpi_driver_match_device(dev, drv))
 385		return 1;
 386
 387	if (sdrv->id_table)
 388		return !!spi_match_id(sdrv->id_table, spi->modalias);
 389
 390	return strcmp(spi->modalias, drv->name) == 0;
 391}
 392
 393static int spi_uevent(const struct device *dev, struct kobj_uevent_env *env)
 394{
 395	const struct spi_device		*spi = to_spi_device(dev);
 396	int rc;
 397
 398	rc = acpi_device_uevent_modalias(dev, env);
 399	if (rc != -ENODEV)
 400		return rc;
 401
 402	return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
 403}
 404
 405static int spi_probe(struct device *dev)
 406{
 407	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
 408	struct spi_device		*spi = to_spi_device(dev);
 409	struct fwnode_handle		*fwnode = dev_fwnode(dev);
 410	int ret;
 411
 412	ret = of_clk_set_defaults(dev->of_node, false);
 413	if (ret)
 414		return ret;
 415
 416	if (is_of_node(fwnode))
 417		spi->irq = of_irq_get(dev->of_node, 0);
 418	else if (is_acpi_device_node(fwnode) && spi->irq < 0)
 419		spi->irq = acpi_dev_gpio_irq_get(to_acpi_device_node(fwnode), 0);
 420	if (spi->irq == -EPROBE_DEFER)
 421		return dev_err_probe(dev, spi->irq, "Failed to get irq\n");
 422	if (spi->irq < 0)
 423		spi->irq = 0;
 424
 425	ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON |
 426					PD_FLAG_DETACH_POWER_OFF);
 427	if (ret)
 428		return ret;
 429
 430	if (sdrv->probe)
 431		ret = sdrv->probe(spi);
 432
 433	return ret;
 434}
 435
 436static void spi_remove(struct device *dev)
 437{
 438	const struct spi_driver		*sdrv = to_spi_driver(dev->driver);
 439
 440	if (sdrv->remove)
 441		sdrv->remove(to_spi_device(dev));
 442}
 443
 444static void spi_shutdown(struct device *dev)
 445{
 446	if (dev->driver) {
 447		const struct spi_driver	*sdrv = to_spi_driver(dev->driver);
 448
 449		if (sdrv->shutdown)
 450			sdrv->shutdown(to_spi_device(dev));
 451	}
 452}
 453
 454const struct bus_type spi_bus_type = {
 455	.name		= "spi",
 456	.dev_groups	= spi_dev_groups,
 457	.match		= spi_match_device,
 458	.uevent		= spi_uevent,
 459	.probe		= spi_probe,
 460	.remove		= spi_remove,
 461	.shutdown	= spi_shutdown,
 462};
 463EXPORT_SYMBOL_GPL(spi_bus_type);
 464
 465/**
 466 * __spi_register_driver - register a SPI driver
 467 * @owner: owner module of the driver to register
 468 * @sdrv: the driver to register
 469 * Context: can sleep
 470 *
 471 * Return: zero on success, else a negative error code.
 472 */
 473int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
 474{
 475	sdrv->driver.owner = owner;
 476	sdrv->driver.bus = &spi_bus_type;
 477
 478	/*
 479	 * For Really Good Reasons we use spi: modaliases not of:
 480	 * modaliases for DT so module autoloading won't work if we
 481	 * don't have a spi_device_id as well as a compatible string.
 482	 */
 483	if (sdrv->driver.of_match_table) {
 484		const struct of_device_id *of_id;
 485
 486		for (of_id = sdrv->driver.of_match_table; of_id->compatible[0];
 487		     of_id++) {
 488			const char *of_name;
 489
 490			/* Strip off any vendor prefix */
 491			of_name = strnchr(of_id->compatible,
 492					  sizeof(of_id->compatible), ',');
 493			if (of_name)
 494				of_name++;
 495			else
 496				of_name = of_id->compatible;
 497
 498			if (sdrv->id_table) {
 499				const struct spi_device_id *spi_id;
 500
 501				spi_id = spi_match_id(sdrv->id_table, of_name);
 502				if (spi_id)
 503					continue;
 504			} else {
 505				if (strcmp(sdrv->driver.name, of_name) == 0)
 506					continue;
 507			}
 508
 509			pr_warn("SPI driver %s has no spi_device_id for %s\n",
 510				sdrv->driver.name, of_id->compatible);
 511		}
 512	}
 513
 514	return driver_register(&sdrv->driver);
 515}
 516EXPORT_SYMBOL_GPL(__spi_register_driver);
 517
 518/*-------------------------------------------------------------------------*/
 519
 520/*
 521 * SPI devices should normally not be created by SPI device drivers; that
 522 * would make them board-specific.  Similarly with SPI controller drivers.
 523 * Device registration normally goes into like arch/.../mach.../board-YYY.c
 524 * with other readonly (flashable) information about mainboard devices.
 525 */
 526
 527struct boardinfo {
 528	struct list_head	list;
 529	struct spi_board_info	board_info;
 530};
 531
 532static LIST_HEAD(board_list);
 533static LIST_HEAD(spi_controller_list);
 534
 535/*
 536 * Used to protect add/del operation for board_info list and
 537 * spi_controller list, and their matching process also used
 538 * to protect object of type struct idr.
 539 */
 540static DEFINE_MUTEX(board_lock);
 541
 542/**
 543 * spi_alloc_device - Allocate a new SPI device
 544 * @ctlr: Controller to which device is connected
 545 * Context: can sleep
 546 *
 547 * Allows a driver to allocate and initialize a spi_device without
 548 * registering it immediately.  This allows a driver to directly
 549 * fill the spi_device with device parameters before calling
 550 * spi_add_device() on it.
 551 *
 552 * Caller is responsible to call spi_add_device() on the returned
 553 * spi_device structure to add it to the SPI controller.  If the caller
 554 * needs to discard the spi_device without adding it, then it should
 555 * call spi_dev_put() on it.
 556 *
 557 * Return: a pointer to the new device, or NULL.
 558 */
 559struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
 560{
 561	struct spi_device	*spi;
 562
 563	if (!spi_controller_get(ctlr))
 564		return NULL;
 565
 566	spi = kzalloc(sizeof(*spi), GFP_KERNEL);
 567	if (!spi) {
 568		spi_controller_put(ctlr);
 569		return NULL;
 570	}
 571
 572	spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
 573	if (!spi->pcpu_statistics) {
 574		kfree(spi);
 575		spi_controller_put(ctlr);
 576		return NULL;
 577	}
 578
 579	spi->controller = ctlr;
 580	spi->dev.parent = &ctlr->dev;
 581	spi->dev.bus = &spi_bus_type;
 582	spi->dev.release = spidev_release;
 583	spi->mode = ctlr->buswidth_override_bits;
 584	spi->num_chipselect = 1;
 585
 586	device_initialize(&spi->dev);
 587	return spi;
 588}
 589EXPORT_SYMBOL_GPL(spi_alloc_device);
 590
 591static void spi_dev_set_name(struct spi_device *spi)
 592{
 593	struct device *dev = &spi->dev;
 594	struct fwnode_handle *fwnode = dev_fwnode(dev);
 595
 596	if (is_acpi_device_node(fwnode)) {
 597		dev_set_name(dev, "spi-%s", acpi_dev_name(to_acpi_device_node(fwnode)));
 598		return;
 599	}
 600
 601	if (is_software_node(fwnode)) {
 602		dev_set_name(dev, "spi-%pfwP", fwnode);
 603		return;
 604	}
 605
 606	dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
 607		     spi_get_chipselect(spi, 0));
 608}
 609
 610/*
 611 * Zero(0) is a valid physical CS value and can be located at any
 612 * logical CS in the spi->chip_select[]. If all the physical CS
 613 * are initialized to 0 then It would be difficult to differentiate
 614 * between a valid physical CS 0 & an unused logical CS whose physical
 615 * CS can be 0. As a solution to this issue initialize all the CS to -1.
 616 * Now all the unused logical CS will have -1 physical CS value & can be
 617 * ignored while performing physical CS validity checks.
 618 */
 619#define SPI_INVALID_CS		((s8)-1)
 620
 621static inline int spi_dev_check_cs(struct device *dev,
 622				   struct spi_device *spi, u8 idx,
 623				   struct spi_device *new_spi, u8 new_idx)
 624{
 625	u8 cs, cs_new;
 626	u8 idx_new;
 627
 628	cs = spi_get_chipselect(spi, idx);
 629	for (idx_new = new_idx; idx_new < new_spi->num_chipselect; idx_new++) {
 630		cs_new = spi_get_chipselect(new_spi, idx_new);
 631		if (cs == cs_new) {
 632			dev_err(dev, "chipselect %u already in use\n", cs_new);
 633			return -EBUSY;
 634		}
 635	}
 636	return 0;
 637}
 638
 639static int spi_dev_check(struct device *dev, void *data)
 640{
 641	struct spi_device *spi = to_spi_device(dev);
 642	struct spi_device *new_spi = data;
 643	int status, idx;
 644
 645	if (spi->controller == new_spi->controller) {
 646		for (idx = 0; idx < spi->num_chipselect; idx++) {
 647			status = spi_dev_check_cs(dev, spi, idx, new_spi, 0);
 648			if (status)
 649				return status;
 650		}
 651	}
 652	return 0;
 653}
 654
 655static void spi_cleanup(struct spi_device *spi)
 656{
 657	if (spi->controller->cleanup)
 658		spi->controller->cleanup(spi);
 659}
 660
 661static int __spi_add_device(struct spi_device *spi)
 662{
 663	struct spi_controller *ctlr = spi->controller;
 664	struct device *dev = ctlr->dev.parent;
 665	int status, idx;
 666	u8 cs;
 667
 668	if (spi->num_chipselect > SPI_DEVICE_CS_CNT_MAX) {
 669		dev_err(dev, "num_cs %d > max %d\n", spi->num_chipselect,
 670			SPI_DEVICE_CS_CNT_MAX);
 671		return -EOVERFLOW;
 672	}
 673
 674	for (idx = 0; idx < spi->num_chipselect; idx++) {
 675		/* Chipselects are numbered 0..max; validate. */
 676		cs = spi_get_chipselect(spi, idx);
 677		if (cs >= ctlr->num_chipselect) {
 678			dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, idx),
 679				ctlr->num_chipselect);
 680			return -EINVAL;
 681		}
 682	}
 683
 684	/*
 685	 * Make sure that multiple logical CS doesn't map to the same physical CS.
 686	 * For example, spi->chip_select[0] != spi->chip_select[1] and so on.
 687	 */
 688	if (!spi_controller_is_target(ctlr)) {
 689		for (idx = 0; idx < spi->num_chipselect; idx++) {
 690			status = spi_dev_check_cs(dev, spi, idx, spi, idx + 1);
 691			if (status)
 692				return status;
 693		}
 694	}
 695
 696	/* Initialize unused logical CS as invalid */
 697	for (idx = spi->num_chipselect; idx < SPI_DEVICE_CS_CNT_MAX; idx++)
 698		spi_set_chipselect(spi, idx, SPI_INVALID_CS);
 699
 700	/* Set the bus ID string */
 701	spi_dev_set_name(spi);
 702
 703	/*
 704	 * We need to make sure there's no other device with this
 705	 * chipselect **BEFORE** we call setup(), else we'll trash
 706	 * its configuration.
 707	 */
 708	status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
 709	if (status)
 710		return status;
 711
 712	/* Controller may unregister concurrently */
 713	if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
 714	    !device_is_registered(&ctlr->dev)) {
 715		return -ENODEV;
 716	}
 717
 718	if (ctlr->cs_gpiods) {
 719		u8 cs;
 720
 721		for (idx = 0; idx < spi->num_chipselect; idx++) {
 722			cs = spi_get_chipselect(spi, idx);
 723			spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
 724		}
 725	}
 726
 727	/*
 728	 * Drivers may modify this initial i/o setup, but will
 729	 * normally rely on the device being setup.  Devices
 730	 * using SPI_CS_HIGH can't coexist well otherwise...
 731	 */
 732	status = spi_setup(spi);
 733	if (status < 0) {
 734		dev_err(dev, "can't setup %s, status %d\n",
 735				dev_name(&spi->dev), status);
 736		return status;
 737	}
 738
 739	/* Device may be bound to an active driver when this returns */
 740	status = device_add(&spi->dev);
 741	if (status < 0) {
 742		dev_err(dev, "can't add %s, status %d\n",
 743				dev_name(&spi->dev), status);
 744		spi_cleanup(spi);
 745	} else {
 746		dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
 747	}
 748
 749	return status;
 750}
 751
 752/**
 753 * spi_add_device - Add spi_device allocated with spi_alloc_device
 754 * @spi: spi_device to register
 755 *
 756 * Companion function to spi_alloc_device.  Devices allocated with
 757 * spi_alloc_device can be added onto the SPI bus with this function.
 758 *
 759 * Return: 0 on success; negative errno on failure
 760 */
 761int spi_add_device(struct spi_device *spi)
 762{
 763	struct spi_controller *ctlr = spi->controller;
 764	int status;
 765
 766	/* Set the bus ID string */
 767	spi_dev_set_name(spi);
 768
 769	mutex_lock(&ctlr->add_lock);
 770	status = __spi_add_device(spi);
 771	mutex_unlock(&ctlr->add_lock);
 772	return status;
 773}
 774EXPORT_SYMBOL_GPL(spi_add_device);
 775
 776/**
 777 * spi_new_device - instantiate one new SPI device
 778 * @ctlr: Controller to which device is connected
 779 * @chip: Describes the SPI device
 780 * Context: can sleep
 781 *
 782 * On typical mainboards, this is purely internal; and it's not needed
 783 * after board init creates the hard-wired devices.  Some development
 784 * platforms may not be able to use spi_register_board_info though, and
 785 * this is exported so that for example a USB or parport based adapter
 786 * driver could add devices (which it would learn about out-of-band).
 787 *
 788 * Return: the new device, or NULL.
 789 */
 790struct spi_device *spi_new_device(struct spi_controller *ctlr,
 791				  struct spi_board_info *chip)
 792{
 793	struct spi_device	*proxy;
 794	int			status;
 795
 796	/*
 797	 * NOTE:  caller did any chip->bus_num checks necessary.
 798	 *
 799	 * Also, unless we change the return value convention to use
 800	 * error-or-pointer (not NULL-or-pointer), troubleshootability
 801	 * suggests syslogged diagnostics are best here (ugh).
 802	 */
 803
 804	proxy = spi_alloc_device(ctlr);
 805	if (!proxy)
 806		return NULL;
 807
 808	WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
 809
 810	/* Use provided chip-select for proxy device */
 811	spi_set_chipselect(proxy, 0, chip->chip_select);
 812
 813	proxy->max_speed_hz = chip->max_speed_hz;
 814	proxy->mode = chip->mode;
 815	proxy->irq = chip->irq;
 816	strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
 817	proxy->dev.platform_data = (void *) chip->platform_data;
 818	proxy->controller_data = chip->controller_data;
 819	proxy->controller_state = NULL;
 820	/*
 821	 * By default spi->chip_select[0] will hold the physical CS number,
 822	 * so set bit 0 in spi->cs_index_mask.
 823	 */
 824	proxy->cs_index_mask = BIT(0);
 825
 826	if (chip->swnode) {
 827		status = device_add_software_node(&proxy->dev, chip->swnode);
 828		if (status) {
 829			dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n",
 830				chip->modalias, status);
 831			goto err_dev_put;
 832		}
 833	}
 834
 835	status = spi_add_device(proxy);
 836	if (status < 0)
 837		goto err_dev_put;
 838
 839	return proxy;
 840
 841err_dev_put:
 842	device_remove_software_node(&proxy->dev);
 843	spi_dev_put(proxy);
 844	return NULL;
 845}
 846EXPORT_SYMBOL_GPL(spi_new_device);
 847
 848/**
 849 * spi_unregister_device - unregister a single SPI device
 850 * @spi: spi_device to unregister
 851 *
 852 * Start making the passed SPI device vanish. Normally this would be handled
 853 * by spi_unregister_controller().
 854 */
 855void spi_unregister_device(struct spi_device *spi)
 856{
 857	struct fwnode_handle *fwnode;
 858
 859	if (!spi)
 860		return;
 861
 862	fwnode = dev_fwnode(&spi->dev);
 863	if (is_of_node(fwnode)) {
 864		of_node_clear_flag(to_of_node(fwnode), OF_POPULATED);
 865		of_node_put(to_of_node(fwnode));
 866	} else if (is_acpi_device_node(fwnode)) {
 867		acpi_device_clear_enumerated(to_acpi_device_node(fwnode));
 868	}
 869	device_remove_software_node(&spi->dev);
 870	device_del(&spi->dev);
 871	spi_cleanup(spi);
 872	put_device(&spi->dev);
 873}
 874EXPORT_SYMBOL_GPL(spi_unregister_device);
 875
 876static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
 877					      struct spi_board_info *bi)
 878{
 879	struct spi_device *dev;
 880
 881	if (ctlr->bus_num != bi->bus_num)
 882		return;
 883
 884	dev = spi_new_device(ctlr, bi);
 885	if (!dev)
 886		dev_err(ctlr->dev.parent, "can't create new device for %s\n",
 887			bi->modalias);
 888}
 889
 890/**
 891 * spi_register_board_info - register SPI devices for a given board
 892 * @info: array of chip descriptors
 893 * @n: how many descriptors are provided
 894 * Context: can sleep
 895 *
 896 * Board-specific early init code calls this (probably during arch_initcall)
 897 * with segments of the SPI device table.  Any device nodes are created later,
 898 * after the relevant parent SPI controller (bus_num) is defined.  We keep
 899 * this table of devices forever, so that reloading a controller driver will
 900 * not make Linux forget about these hard-wired devices.
 901 *
 902 * Other code can also call this, e.g. a particular add-on board might provide
 903 * SPI devices through its expansion connector, so code initializing that board
 904 * would naturally declare its SPI devices.
 905 *
 906 * The board info passed can safely be __initdata ... but be careful of
 907 * any embedded pointers (platform_data, etc), they're copied as-is.
 908 *
 909 * Return: zero on success, else a negative error code.
 910 */
 911int spi_register_board_info(struct spi_board_info const *info, unsigned n)
 912{
 913	struct boardinfo *bi;
 914	int i;
 915
 916	if (!n)
 917		return 0;
 918
 919	bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
 920	if (!bi)
 921		return -ENOMEM;
 922
 923	for (i = 0; i < n; i++, bi++, info++) {
 924		struct spi_controller *ctlr;
 925
 926		memcpy(&bi->board_info, info, sizeof(*info));
 927
 928		mutex_lock(&board_lock);
 929		list_add_tail(&bi->list, &board_list);
 930		list_for_each_entry(ctlr, &spi_controller_list, list)
 931			spi_match_controller_to_boardinfo(ctlr,
 932							  &bi->board_info);
 933		mutex_unlock(&board_lock);
 934	}
 935
 936	return 0;
 937}
 938
 939/*-------------------------------------------------------------------------*/
 940
 941/* Core methods for SPI resource management */
 942
 943/**
 944 * spi_res_alloc - allocate a spi resource that is life-cycle managed
 945 *                 during the processing of a spi_message while using
 946 *                 spi_transfer_one
 947 * @spi:     the SPI device for which we allocate memory
 948 * @release: the release code to execute for this resource
 949 * @size:    size to alloc and return
 950 * @gfp:     GFP allocation flags
 951 *
 952 * Return: the pointer to the allocated data
 953 *
 954 * This may get enhanced in the future to allocate from a memory pool
 955 * of the @spi_device or @spi_controller to avoid repeated allocations.
 956 */
 957static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release,
 958			   size_t size, gfp_t gfp)
 959{
 960	struct spi_res *sres;
 961
 962	sres = kzalloc(sizeof(*sres) + size, gfp);
 963	if (!sres)
 964		return NULL;
 965
 966	INIT_LIST_HEAD(&sres->entry);
 967	sres->release = release;
 968
 969	return sres->data;
 970}
 971
 972/**
 973 * spi_res_free - free an SPI resource
 974 * @res: pointer to the custom data of a resource
 975 */
 976static void spi_res_free(void *res)
 977{
 978	struct spi_res *sres = container_of(res, struct spi_res, data);
 979
 980	WARN_ON(!list_empty(&sres->entry));
 981	kfree(sres);
 982}
 983
 984/**
 985 * spi_res_add - add a spi_res to the spi_message
 986 * @message: the SPI message
 987 * @res:     the spi_resource
 988 */
 989static void spi_res_add(struct spi_message *message, void *res)
 990{
 991	struct spi_res *sres = container_of(res, struct spi_res, data);
 992
 993	WARN_ON(!list_empty(&sres->entry));
 994	list_add_tail(&sres->entry, &message->resources);
 995}
 996
 997/**
 998 * spi_res_release - release all SPI resources for this message
 999 * @ctlr:  the @spi_controller
1000 * @message: the @spi_message
1001 */
1002static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
1003{
1004	struct spi_res *res, *tmp;
1005
1006	list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
1007		if (res->release)
1008			res->release(ctlr, message, res->data);
1009
1010		list_del(&res->entry);
1011
1012		kfree(res);
1013	}
1014}
1015
1016/*-------------------------------------------------------------------------*/
1017#define spi_for_each_valid_cs(spi, idx)				\
1018	for (idx = 0; idx < spi->num_chipselect; idx++)		\
1019		if (!(spi->cs_index_mask & BIT(idx))) {} else
1020
1021static inline bool spi_is_last_cs(struct spi_device *spi)
1022{
1023	u8 idx;
1024	bool last = false;
1025
1026	spi_for_each_valid_cs(spi, idx) {
1027		if (spi->controller->last_cs[idx] == spi_get_chipselect(spi, idx))
1028			last = true;
1029	}
1030	return last;
1031}
1032
1033static void spi_toggle_csgpiod(struct spi_device *spi, u8 idx, bool enable, bool activate)
1034{
1035	/*
1036	 * Historically ACPI has no means of the GPIO polarity and
1037	 * thus the SPISerialBus() resource defines it on the per-chip
1038	 * basis. In order to avoid a chain of negations, the GPIO
1039	 * polarity is considered being Active High. Even for the cases
1040	 * when _DSD() is involved (in the updated versions of ACPI)
1041	 * the GPIO CS polarity must be defined Active High to avoid
1042	 * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
1043	 * into account.
1044	 */
1045	if (is_acpi_device_node(dev_fwnode(&spi->dev)))
1046		gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), !enable);
1047	else
1048		/* Polarity handled by GPIO library */
1049		gpiod_set_value_cansleep(spi_get_csgpiod(spi, idx), activate);
1050
1051	if (activate)
1052		spi_delay_exec(&spi->cs_setup, NULL);
1053	else
1054		spi_delay_exec(&spi->cs_inactive, NULL);
1055}
1056
1057static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
1058{
1059	bool activate = enable;
1060	u8 idx;
1061
1062	/*
1063	 * Avoid calling into the driver (or doing delays) if the chip select
1064	 * isn't actually changing from the last time this was called.
1065	 */
1066	if (!force && (enable == spi_is_last_cs(spi)) &&
1067	    (spi->controller->last_cs_index_mask == spi->cs_index_mask) &&
1068	    (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
1069		return;
1070
1071	trace_spi_set_cs(spi, activate);
1072
1073	spi->controller->last_cs_index_mask = spi->cs_index_mask;
1074	for (idx = 0; idx < SPI_DEVICE_CS_CNT_MAX; idx++) {
1075		if (enable && idx < spi->num_chipselect)
1076			spi->controller->last_cs[idx] = spi_get_chipselect(spi, 0);
1077		else
1078			spi->controller->last_cs[idx] = SPI_INVALID_CS;
1079	}
1080
1081	spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
1082	if (spi->controller->last_cs_mode_high)
1083		enable = !enable;
1084
1085	/*
1086	 * Handle chip select delays for GPIO based CS or controllers without
1087	 * programmable chip select timing.
1088	 */
1089	if ((spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) && !activate)
1090		spi_delay_exec(&spi->cs_hold, NULL);
1091
1092	if (spi_is_csgpiod(spi)) {
1093		if (!(spi->mode & SPI_NO_CS)) {
1094			spi_for_each_valid_cs(spi, idx) {
1095				if (spi_get_csgpiod(spi, idx))
1096					spi_toggle_csgpiod(spi, idx, enable, activate);
1097			}
1098		}
1099		/* Some SPI controllers need both GPIO CS & ->set_cs() */
1100		if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) &&
1101		    spi->controller->set_cs)
1102			spi->controller->set_cs(spi, !enable);
1103	} else if (spi->controller->set_cs) {
1104		spi->controller->set_cs(spi, !enable);
1105	}
1106
1107	if (spi_is_csgpiod(spi) || !spi->controller->set_cs_timing) {
1108		if (activate)
1109			spi_delay_exec(&spi->cs_setup, NULL);
1110		else
1111			spi_delay_exec(&spi->cs_inactive, NULL);
1112	}
1113}
1114
1115#ifdef CONFIG_HAS_DMA
1116static int spi_map_buf_attrs(struct spi_controller *ctlr, struct device *dev,
1117			     struct sg_table *sgt, void *buf, size_t len,
1118			     enum dma_data_direction dir, unsigned long attrs)
1119{
1120	const bool vmalloced_buf = is_vmalloc_addr(buf);
1121	unsigned int max_seg_size = dma_get_max_seg_size(dev);
1122#ifdef CONFIG_HIGHMEM
1123	const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
1124				(unsigned long)buf < (PKMAP_BASE +
1125					(LAST_PKMAP * PAGE_SIZE)));
1126#else
1127	const bool kmap_buf = false;
1128#endif
1129	int desc_len;
1130	int sgs;
1131	struct page *vm_page;
1132	struct scatterlist *sg;
1133	void *sg_buf;
1134	size_t min;
1135	int i, ret;
1136
1137	if (vmalloced_buf || kmap_buf) {
1138		desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE);
1139		sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
1140	} else if (virt_addr_valid(buf)) {
1141		desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
1142		sgs = DIV_ROUND_UP(len, desc_len);
1143	} else {
1144		return -EINVAL;
1145	}
1146
1147	ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
1148	if (ret != 0)
1149		return ret;
1150
1151	sg = &sgt->sgl[0];
1152	for (i = 0; i < sgs; i++) {
1153
1154		if (vmalloced_buf || kmap_buf) {
1155			/*
1156			 * Next scatterlist entry size is the minimum between
1157			 * the desc_len and the remaining buffer length that
1158			 * fits in a page.
1159			 */
1160			min = min_t(size_t, desc_len,
1161				    min_t(size_t, len,
1162					  PAGE_SIZE - offset_in_page(buf)));
1163			if (vmalloced_buf)
1164				vm_page = vmalloc_to_page(buf);
1165			else
1166				vm_page = kmap_to_page(buf);
1167			if (!vm_page) {
1168				sg_free_table(sgt);
1169				return -ENOMEM;
1170			}
1171			sg_set_page(sg, vm_page,
1172				    min, offset_in_page(buf));
1173		} else {
1174			min = min_t(size_t, len, desc_len);
1175			sg_buf = buf;
1176			sg_set_buf(sg, sg_buf, min);
1177		}
1178
1179		buf += min;
1180		len -= min;
1181		sg = sg_next(sg);
1182	}
1183
1184	ret = dma_map_sgtable(dev, sgt, dir, attrs);
1185	if (ret < 0) {
1186		sg_free_table(sgt);
1187		return ret;
1188	}
1189
1190	return 0;
1191}
1192
1193int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
1194		struct sg_table *sgt, void *buf, size_t len,
1195		enum dma_data_direction dir)
1196{
1197	return spi_map_buf_attrs(ctlr, dev, sgt, buf, len, dir, 0);
1198}
1199
1200static void spi_unmap_buf_attrs(struct spi_controller *ctlr,
1201				struct device *dev, struct sg_table *sgt,
1202				enum dma_data_direction dir,
1203				unsigned long attrs)
1204{
1205	dma_unmap_sgtable(dev, sgt, dir, attrs);
1206	sg_free_table(sgt);
1207	sgt->orig_nents = 0;
1208	sgt->nents = 0;
1209}
1210
1211void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
1212		   struct sg_table *sgt, enum dma_data_direction dir)
1213{
1214	spi_unmap_buf_attrs(ctlr, dev, sgt, dir, 0);
1215}
1216
1217static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
1218{
1219	struct device *tx_dev, *rx_dev;
1220	struct spi_transfer *xfer;
1221	int ret;
1222
1223	if (!ctlr->can_dma)
1224		return 0;
1225
1226	if (ctlr->dma_tx)
1227		tx_dev = ctlr->dma_tx->device->dev;
1228	else if (ctlr->dma_map_dev)
1229		tx_dev = ctlr->dma_map_dev;
1230	else
1231		tx_dev = ctlr->dev.parent;
1232
1233	if (ctlr->dma_rx)
1234		rx_dev = ctlr->dma_rx->device->dev;
1235	else if (ctlr->dma_map_dev)
1236		rx_dev = ctlr->dma_map_dev;
1237	else
1238		rx_dev = ctlr->dev.parent;
1239
1240	ret = -ENOMSG;
1241	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1242		/* The sync is done before each transfer. */
1243		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
1244
1245		if (!ctlr->can_dma(ctlr, msg->spi, xfer))
1246			continue;
1247
1248		if (xfer->tx_buf != NULL) {
1249			ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
1250						(void *)xfer->tx_buf,
1251						xfer->len, DMA_TO_DEVICE,
1252						attrs);
1253			if (ret != 0)
1254				return ret;
1255
1256			xfer->tx_sg_mapped = true;
1257		}
1258
1259		if (xfer->rx_buf != NULL) {
1260			ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
1261						xfer->rx_buf, xfer->len,
1262						DMA_FROM_DEVICE, attrs);
1263			if (ret != 0) {
1264				spi_unmap_buf_attrs(ctlr, tx_dev,
1265						&xfer->tx_sg, DMA_TO_DEVICE,
1266						attrs);
1267
1268				return ret;
1269			}
1270
1271			xfer->rx_sg_mapped = true;
1272		}
1273	}
1274	/* No transfer has been mapped, bail out with success */
1275	if (ret)
1276		return 0;
1277
1278	ctlr->cur_rx_dma_dev = rx_dev;
1279	ctlr->cur_tx_dma_dev = tx_dev;
1280
1281	return 0;
1282}
1283
1284static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
1285{
1286	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1287	struct device *tx_dev = ctlr->cur_tx_dma_dev;
1288	struct spi_transfer *xfer;
1289
1290	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1291		/* The sync has already been done after each transfer. */
1292		unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
1293
1294		if (xfer->rx_sg_mapped)
1295			spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
1296					    DMA_FROM_DEVICE, attrs);
1297		xfer->rx_sg_mapped = false;
1298
1299		if (xfer->tx_sg_mapped)
1300			spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
1301					    DMA_TO_DEVICE, attrs);
1302		xfer->tx_sg_mapped = false;
1303	}
1304
1305	return 0;
1306}
1307
1308static void spi_dma_sync_for_device(struct spi_controller *ctlr,
1309				    struct spi_transfer *xfer)
1310{
1311	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1312	struct device *tx_dev = ctlr->cur_tx_dma_dev;
1313
1314	if (xfer->tx_sg_mapped)
1315		dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1316	if (xfer->rx_sg_mapped)
1317		dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1318}
1319
1320static void spi_dma_sync_for_cpu(struct spi_controller *ctlr,
1321				 struct spi_transfer *xfer)
1322{
1323	struct device *rx_dev = ctlr->cur_rx_dma_dev;
1324	struct device *tx_dev = ctlr->cur_tx_dma_dev;
1325
1326	if (xfer->rx_sg_mapped)
1327		dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1328	if (xfer->tx_sg_mapped)
1329		dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
1330}
1331#else /* !CONFIG_HAS_DMA */
1332static inline int __spi_map_msg(struct spi_controller *ctlr,
1333				struct spi_message *msg)
1334{
1335	return 0;
1336}
1337
1338static inline int __spi_unmap_msg(struct spi_controller *ctlr,
1339				  struct spi_message *msg)
1340{
1341	return 0;
1342}
1343
1344static void spi_dma_sync_for_device(struct spi_controller *ctrl,
1345				    struct spi_transfer *xfer)
1346{
1347}
1348
1349static void spi_dma_sync_for_cpu(struct spi_controller *ctrl,
1350				 struct spi_transfer *xfer)
1351{
1352}
1353#endif /* !CONFIG_HAS_DMA */
1354
1355static inline int spi_unmap_msg(struct spi_controller *ctlr,
1356				struct spi_message *msg)
1357{
1358	struct spi_transfer *xfer;
1359
1360	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1361		/*
1362		 * Restore the original value of tx_buf or rx_buf if they are
1363		 * NULL.
1364		 */
1365		if (xfer->tx_buf == ctlr->dummy_tx)
1366			xfer->tx_buf = NULL;
1367		if (xfer->rx_buf == ctlr->dummy_rx)
1368			xfer->rx_buf = NULL;
1369	}
1370
1371	return __spi_unmap_msg(ctlr, msg);
1372}
1373
1374static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
1375{
1376	struct spi_transfer *xfer;
1377	void *tmp;
1378	unsigned int max_tx, max_rx;
1379
1380	if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1381		&& !(msg->spi->mode & SPI_3WIRE)) {
1382		max_tx = 0;
1383		max_rx = 0;
1384
1385		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1386			if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
1387			    !xfer->tx_buf)
1388				max_tx = max(xfer->len, max_tx);
1389			if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
1390			    !xfer->rx_buf)
1391				max_rx = max(xfer->len, max_rx);
1392		}
1393
1394		if (max_tx) {
1395			tmp = krealloc(ctlr->dummy_tx, max_tx,
1396				       GFP_KERNEL | GFP_DMA | __GFP_ZERO);
1397			if (!tmp)
1398				return -ENOMEM;
1399			ctlr->dummy_tx = tmp;
1400		}
1401
1402		if (max_rx) {
1403			tmp = krealloc(ctlr->dummy_rx, max_rx,
1404				       GFP_KERNEL | GFP_DMA);
1405			if (!tmp)
1406				return -ENOMEM;
1407			ctlr->dummy_rx = tmp;
1408		}
1409
1410		if (max_tx || max_rx) {
1411			list_for_each_entry(xfer, &msg->transfers,
1412					    transfer_list) {
1413				if (!xfer->len)
1414					continue;
1415				if (!xfer->tx_buf)
1416					xfer->tx_buf = ctlr->dummy_tx;
1417				if (!xfer->rx_buf)
1418					xfer->rx_buf = ctlr->dummy_rx;
1419			}
1420		}
1421	}
1422
1423	return __spi_map_msg(ctlr, msg);
1424}
1425
1426static int spi_transfer_wait(struct spi_controller *ctlr,
1427			     struct spi_message *msg,
1428			     struct spi_transfer *xfer)
1429{
1430	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1431	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1432	u32 speed_hz = xfer->speed_hz;
1433	unsigned long long ms;
1434
1435	if (spi_controller_is_target(ctlr)) {
1436		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1437			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1438			return -EINTR;
1439		}
1440	} else {
1441		if (!speed_hz)
1442			speed_hz = 100000;
1443
1444		/*
1445		 * For each byte we wait for 8 cycles of the SPI clock.
1446		 * Since speed is defined in Hz and we want milliseconds,
1447		 * use respective multiplier, but before the division,
1448		 * otherwise we may get 0 for short transfers.
1449		 */
1450		ms = 8LL * MSEC_PER_SEC * xfer->len;
1451		do_div(ms, speed_hz);
1452
1453		/*
1454		 * Increase it twice and add 200 ms tolerance, use
1455		 * predefined maximum in case of overflow.
1456		 */
1457		ms += ms + 200;
1458		if (ms > UINT_MAX)
1459			ms = UINT_MAX;
1460
1461		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1462						 msecs_to_jiffies(ms));
1463
1464		if (ms == 0) {
1465			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1466			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1467			dev_err(&msg->spi->dev,
1468				"SPI transfer timed out\n");
1469			return -ETIMEDOUT;
1470		}
1471
1472		if (xfer->error & SPI_TRANS_FAIL_IO)
1473			return -EIO;
1474	}
1475
1476	return 0;
1477}
1478
1479static void _spi_transfer_delay_ns(u32 ns)
1480{
1481	if (!ns)
1482		return;
1483	if (ns <= NSEC_PER_USEC) {
1484		ndelay(ns);
1485	} else {
1486		u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
1487
1488		fsleep(us);
1489	}
1490}
1491
1492int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
1493{
1494	u32 delay = _delay->value;
1495	u32 unit = _delay->unit;
1496	u32 hz;
1497
1498	if (!delay)
1499		return 0;
1500
1501	switch (unit) {
1502	case SPI_DELAY_UNIT_USECS:
1503		delay *= NSEC_PER_USEC;
1504		break;
1505	case SPI_DELAY_UNIT_NSECS:
1506		/* Nothing to do here */
1507		break;
1508	case SPI_DELAY_UNIT_SCK:
1509		/* Clock cycles need to be obtained from spi_transfer */
1510		if (!xfer)
1511			return -EINVAL;
1512		/*
1513		 * If there is unknown effective speed, approximate it
1514		 * by underestimating with half of the requested Hz.
1515		 */
1516		hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1517		if (!hz)
1518			return -EINVAL;
1519
1520		/* Convert delay to nanoseconds */
1521		delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz);
1522		break;
1523	default:
1524		return -EINVAL;
1525	}
1526
1527	return delay;
1528}
1529EXPORT_SYMBOL_GPL(spi_delay_to_ns);
1530
1531int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1532{
1533	int delay;
1534
1535	might_sleep();
1536
1537	if (!_delay)
1538		return -EINVAL;
1539
1540	delay = spi_delay_to_ns(_delay, xfer);
1541	if (delay < 0)
1542		return delay;
1543
1544	_spi_transfer_delay_ns(delay);
1545
1546	return 0;
1547}
1548EXPORT_SYMBOL_GPL(spi_delay_exec);
1549
1550static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1551					  struct spi_transfer *xfer)
1552{
1553	u32 default_delay_ns = 10 * NSEC_PER_USEC;
1554	u32 delay = xfer->cs_change_delay.value;
1555	u32 unit = xfer->cs_change_delay.unit;
1556	int ret;
1557
1558	/* Return early on "fast" mode - for everything but USECS */
1559	if (!delay) {
1560		if (unit == SPI_DELAY_UNIT_USECS)
1561			_spi_transfer_delay_ns(default_delay_ns);
1562		return;
1563	}
1564
1565	ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1566	if (ret) {
1567		dev_err_once(&msg->spi->dev,
1568			     "Use of unsupported delay unit %i, using default of %luus\n",
1569			     unit, default_delay_ns / NSEC_PER_USEC);
1570		_spi_transfer_delay_ns(default_delay_ns);
1571	}
1572}
1573
1574void spi_transfer_cs_change_delay_exec(struct spi_message *msg,
1575						  struct spi_transfer *xfer)
1576{
1577	_spi_transfer_cs_change_delay(msg, xfer);
1578}
1579EXPORT_SYMBOL_GPL(spi_transfer_cs_change_delay_exec);
1580
1581/*
1582 * spi_transfer_one_message - Default implementation of transfer_one_message()
1583 *
1584 * This is a standard implementation of transfer_one_message() for
1585 * drivers which implement a transfer_one() operation.  It provides
1586 * standard handling of delays and chip select management.
1587 */
1588static int spi_transfer_one_message(struct spi_controller *ctlr,
1589				    struct spi_message *msg)
1590{
1591	struct spi_transfer *xfer;
1592	bool keep_cs = false;
1593	int ret = 0;
1594	struct spi_statistics __percpu *statm = ctlr->pcpu_statistics;
1595	struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics;
1596
1597	xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list);
1598	spi_set_cs(msg->spi, !xfer->cs_off, false);
1599
1600	SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1601	SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1602
1603	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1604		trace_spi_transfer_start(msg, xfer);
1605
1606		spi_statistics_add_transfer_stats(statm, xfer, msg);
1607		spi_statistics_add_transfer_stats(stats, xfer, msg);
1608
1609		if (!ctlr->ptp_sts_supported) {
1610			xfer->ptp_sts_word_pre = 0;
1611			ptp_read_system_prets(xfer->ptp_sts);
1612		}
1613
1614		if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1615			reinit_completion(&ctlr->xfer_completion);
1616
1617fallback_pio:
1618			spi_dma_sync_for_device(ctlr, xfer);
1619			ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
1620			if (ret < 0) {
1621				spi_dma_sync_for_cpu(ctlr, xfer);
1622
1623				if ((xfer->tx_sg_mapped || xfer->rx_sg_mapped) &&
1624				    (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1625					__spi_unmap_msg(ctlr, msg);
1626					ctlr->fallback = true;
1627					xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1628					goto fallback_pio;
1629				}
1630
1631				SPI_STATISTICS_INCREMENT_FIELD(statm,
1632							       errors);
1633				SPI_STATISTICS_INCREMENT_FIELD(stats,
1634							       errors);
1635				dev_err(&msg->spi->dev,
1636					"SPI transfer failed: %d\n", ret);
1637				goto out;
1638			}
1639
1640			if (ret > 0) {
1641				ret = spi_transfer_wait(ctlr, msg, xfer);
1642				if (ret < 0)
1643					msg->status = ret;
1644			}
1645
1646			spi_dma_sync_for_cpu(ctlr, xfer);
1647		} else {
1648			if (xfer->len)
1649				dev_err(&msg->spi->dev,
1650					"Bufferless transfer has length %u\n",
1651					xfer->len);
1652		}
1653
1654		if (!ctlr->ptp_sts_supported) {
1655			ptp_read_system_postts(xfer->ptp_sts);
1656			xfer->ptp_sts_word_post = xfer->len;
1657		}
1658
1659		trace_spi_transfer_stop(msg, xfer);
1660
1661		if (msg->status != -EINPROGRESS)
1662			goto out;
1663
1664		spi_transfer_delay_exec(xfer);
1665
1666		if (xfer->cs_change) {
1667			if (list_is_last(&xfer->transfer_list,
1668					 &msg->transfers)) {
1669				keep_cs = true;
1670			} else {
1671				if (!xfer->cs_off)
1672					spi_set_cs(msg->spi, false, false);
1673				_spi_transfer_cs_change_delay(msg, xfer);
1674				if (!list_next_entry(xfer, transfer_list)->cs_off)
1675					spi_set_cs(msg->spi, true, false);
1676			}
1677		} else if (!list_is_last(&xfer->transfer_list, &msg->transfers) &&
1678			   xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) {
1679			spi_set_cs(msg->spi, xfer->cs_off, false);
1680		}
1681
1682		msg->actual_length += xfer->len;
1683	}
1684
1685out:
1686	if (ret != 0 || !keep_cs)
1687		spi_set_cs(msg->spi, false, false);
1688
1689	if (msg->status == -EINPROGRESS)
1690		msg->status = ret;
1691
1692	if (msg->status && ctlr->handle_err)
1693		ctlr->handle_err(ctlr, msg);
1694
1695	spi_finalize_current_message(ctlr);
1696
1697	return ret;
1698}
1699
1700/**
1701 * spi_finalize_current_transfer - report completion of a transfer
1702 * @ctlr: the controller reporting completion
1703 *
1704 * Called by SPI drivers using the core transfer_one_message()
1705 * implementation to notify it that the current interrupt driven
1706 * transfer has finished and the next one may be scheduled.
1707 */
1708void spi_finalize_current_transfer(struct spi_controller *ctlr)
1709{
1710	complete(&ctlr->xfer_completion);
1711}
1712EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1713
1714static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1715{
1716	if (ctlr->auto_runtime_pm) {
1717		pm_runtime_put_autosuspend(ctlr->dev.parent);
1718	}
1719}
1720
1721static int __spi_pump_transfer_message(struct spi_controller *ctlr,
1722		struct spi_message *msg, bool was_busy)
1723{
1724	struct spi_transfer *xfer;
1725	int ret;
1726
1727	if (!was_busy && ctlr->auto_runtime_pm) {
1728		ret = pm_runtime_get_sync(ctlr->dev.parent);
1729		if (ret < 0) {
1730			pm_runtime_put_noidle(ctlr->dev.parent);
1731			dev_err(&ctlr->dev, "Failed to power device: %d\n",
1732				ret);
1733
1734			msg->status = ret;
1735			spi_finalize_current_message(ctlr);
1736
1737			return ret;
1738		}
1739	}
1740
1741	if (!was_busy)
1742		trace_spi_controller_busy(ctlr);
1743
1744	if (!was_busy && ctlr->prepare_transfer_hardware) {
1745		ret = ctlr->prepare_transfer_hardware(ctlr);
1746		if (ret) {
1747			dev_err(&ctlr->dev,
1748				"failed to prepare transfer hardware: %d\n",
1749				ret);
1750
1751			if (ctlr->auto_runtime_pm)
1752				pm_runtime_put(ctlr->dev.parent);
1753
1754			msg->status = ret;
1755			spi_finalize_current_message(ctlr);
1756
1757			return ret;
1758		}
1759	}
1760
1761	trace_spi_message_start(msg);
1762
1763	if (ctlr->prepare_message) {
1764		ret = ctlr->prepare_message(ctlr, msg);
1765		if (ret) {
1766			dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1767				ret);
1768			msg->status = ret;
1769			spi_finalize_current_message(ctlr);
1770			return ret;
1771		}
1772		msg->prepared = true;
1773	}
1774
1775	ret = spi_map_msg(ctlr, msg);
1776	if (ret) {
1777		msg->status = ret;
1778		spi_finalize_current_message(ctlr);
1779		return ret;
1780	}
1781
1782	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1783		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1784			xfer->ptp_sts_word_pre = 0;
1785			ptp_read_system_prets(xfer->ptp_sts);
1786		}
1787	}
1788
1789	/*
1790	 * Drivers implementation of transfer_one_message() must arrange for
1791	 * spi_finalize_current_message() to get called. Most drivers will do
1792	 * this in the calling context, but some don't. For those cases, a
1793	 * completion is used to guarantee that this function does not return
1794	 * until spi_finalize_current_message() is done accessing
1795	 * ctlr->cur_msg.
1796	 * Use of the following two flags enable to opportunistically skip the
1797	 * use of the completion since its use involves expensive spin locks.
1798	 * In case of a race with the context that calls
1799	 * spi_finalize_current_message() the completion will always be used,
1800	 * due to strict ordering of these flags using barriers.
1801	 */
1802	WRITE_ONCE(ctlr->cur_msg_incomplete, true);
1803	WRITE_ONCE(ctlr->cur_msg_need_completion, false);
1804	reinit_completion(&ctlr->cur_msg_completion);
1805	smp_wmb(); /* Make these available to spi_finalize_current_message() */
1806
1807	ret = ctlr->transfer_one_message(ctlr, msg);
1808	if (ret) {
1809		dev_err(&ctlr->dev,
1810			"failed to transfer one message from queue\n");
1811		return ret;
1812	}
1813
1814	WRITE_ONCE(ctlr->cur_msg_need_completion, true);
1815	smp_mb(); /* See spi_finalize_current_message()... */
1816	if (READ_ONCE(ctlr->cur_msg_incomplete))
1817		wait_for_completion(&ctlr->cur_msg_completion);
1818
1819	return 0;
1820}
1821
1822/**
1823 * __spi_pump_messages - function which processes SPI message queue
1824 * @ctlr: controller to process queue for
1825 * @in_kthread: true if we are in the context of the message pump thread
1826 *
1827 * This function checks if there is any SPI message in the queue that
1828 * needs processing and if so call out to the driver to initialize hardware
1829 * and transfer each message.
1830 *
1831 * Note that it is called both from the kthread itself and also from
1832 * inside spi_sync(); the queue extraction handling at the top of the
1833 * function should deal with this safely.
1834 */
1835static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
1836{
1837	struct spi_message *msg;
1838	bool was_busy = false;
1839	unsigned long flags;
1840	int ret;
1841
1842	/* Take the I/O mutex */
1843	mutex_lock(&ctlr->io_mutex);
1844
1845	/* Lock queue */
1846	spin_lock_irqsave(&ctlr->queue_lock, flags);
1847
1848	/* Make sure we are not already running a message */
1849	if (ctlr->cur_msg)
1850		goto out_unlock;
1851
1852	/* Check if the queue is idle */
1853	if (list_empty(&ctlr->queue) || !ctlr->running) {
1854		if (!ctlr->busy)
1855			goto out_unlock;
1856
1857		/* Defer any non-atomic teardown to the thread */
1858		if (!in_kthread) {
1859			if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1860			    !ctlr->unprepare_transfer_hardware) {
1861				spi_idle_runtime_pm(ctlr);
1862				ctlr->busy = false;
1863				ctlr->queue_empty = true;
1864				trace_spi_controller_idle(ctlr);
1865			} else {
1866				kthread_queue_work(ctlr->kworker,
1867						   &ctlr->pump_messages);
1868			}
1869			goto out_unlock;
1870		}
1871
1872		ctlr->busy = false;
1873		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1874
1875		kfree(ctlr->dummy_rx);
1876		ctlr->dummy_rx = NULL;
1877		kfree(ctlr->dummy_tx);
1878		ctlr->dummy_tx = NULL;
1879		if (ctlr->unprepare_transfer_hardware &&
1880		    ctlr->unprepare_transfer_hardware(ctlr))
1881			dev_err(&ctlr->dev,
1882				"failed to unprepare transfer hardware\n");
1883		spi_idle_runtime_pm(ctlr);
1884		trace_spi_controller_idle(ctlr);
1885
1886		spin_lock_irqsave(&ctlr->queue_lock, flags);
1887		ctlr->queue_empty = true;
1888		goto out_unlock;
1889	}
1890
1891	/* Extract head of queue */
1892	msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1893	ctlr->cur_msg = msg;
1894
1895	list_del_init(&msg->queue);
1896	if (ctlr->busy)
1897		was_busy = true;
1898	else
1899		ctlr->busy = true;
1900	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1901
1902	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
1903	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
1904
1905	ctlr->cur_msg = NULL;
1906	ctlr->fallback = false;
1907
1908	mutex_unlock(&ctlr->io_mutex);
1909
1910	/* Prod the scheduler in case transfer_one() was busy waiting */
1911	if (!ret)
1912		cond_resched();
1913	return;
1914
1915out_unlock:
1916	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1917	mutex_unlock(&ctlr->io_mutex);
1918}
1919
1920/**
1921 * spi_pump_messages - kthread work function which processes spi message queue
1922 * @work: pointer to kthread work struct contained in the controller struct
1923 */
1924static void spi_pump_messages(struct kthread_work *work)
1925{
1926	struct spi_controller *ctlr =
1927		container_of(work, struct spi_controller, pump_messages);
1928
1929	__spi_pump_messages(ctlr, true);
1930}
1931
1932/**
1933 * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp
1934 * @ctlr: Pointer to the spi_controller structure of the driver
1935 * @xfer: Pointer to the transfer being timestamped
1936 * @progress: How many words (not bytes) have been transferred so far
1937 * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1938 *	      transfer, for less jitter in time measurement. Only compatible
1939 *	      with PIO drivers. If true, must follow up with
1940 *	      spi_take_timestamp_post or otherwise system will crash.
1941 *	      WARNING: for fully predictable results, the CPU frequency must
1942 *	      also be under control (governor).
1943 *
1944 * This is a helper for drivers to collect the beginning of the TX timestamp
1945 * for the requested byte from the SPI transfer. The frequency with which this
1946 * function must be called (once per word, once for the whole transfer, once
1947 * per batch of words etc) is arbitrary as long as the @tx buffer offset is
1948 * greater than or equal to the requested byte at the time of the call. The
1949 * timestamp is only taken once, at the first such call. It is assumed that
1950 * the driver advances its @tx buffer pointer monotonically.
1951 */
1952void spi_take_timestamp_pre(struct spi_controller *ctlr,
1953			    struct spi_transfer *xfer,
1954			    size_t progress, bool irqs_off)
1955{
1956	if (!xfer->ptp_sts)
1957		return;
1958
1959	if (xfer->timestamped)
1960		return;
1961
1962	if (progress > xfer->ptp_sts_word_pre)
1963		return;
1964
1965	/* Capture the resolution of the timestamp */
1966	xfer->ptp_sts_word_pre = progress;
1967
1968	if (irqs_off) {
1969		local_irq_save(ctlr->irq_flags);
1970		preempt_disable();
1971	}
1972
1973	ptp_read_system_prets(xfer->ptp_sts);
1974}
1975EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1976
1977/**
1978 * spi_take_timestamp_post - helper to collect the end of the TX timestamp
1979 * @ctlr: Pointer to the spi_controller structure of the driver
1980 * @xfer: Pointer to the transfer being timestamped
1981 * @progress: How many words (not bytes) have been transferred so far
1982 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1983 *
1984 * This is a helper for drivers to collect the end of the TX timestamp for
1985 * the requested byte from the SPI transfer. Can be called with an arbitrary
1986 * frequency: only the first call where @tx exceeds or is equal to the
1987 * requested word will be timestamped.
1988 */
1989void spi_take_timestamp_post(struct spi_controller *ctlr,
1990			     struct spi_transfer *xfer,
1991			     size_t progress, bool irqs_off)
1992{
1993	if (!xfer->ptp_sts)
1994		return;
1995
1996	if (xfer->timestamped)
1997		return;
1998
1999	if (progress < xfer->ptp_sts_word_post)
2000		return;
2001
2002	ptp_read_system_postts(xfer->ptp_sts);
2003
2004	if (irqs_off) {
2005		local_irq_restore(ctlr->irq_flags);
2006		preempt_enable();
2007	}
2008
2009	/* Capture the resolution of the timestamp */
2010	xfer->ptp_sts_word_post = progress;
2011
2012	xfer->timestamped = 1;
2013}
2014EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
2015
2016/**
2017 * spi_set_thread_rt - set the controller to pump at realtime priority
2018 * @ctlr: controller to boost priority of
2019 *
2020 * This can be called because the controller requested realtime priority
2021 * (by setting the ->rt value before calling spi_register_controller()) or
2022 * because a device on the bus said that its transfers needed realtime
2023 * priority.
2024 *
2025 * NOTE: at the moment if any device on a bus says it needs realtime then
2026 * the thread will be at realtime priority for all transfers on that
2027 * controller.  If this eventually becomes a problem we may see if we can
2028 * find a way to boost the priority only temporarily during relevant
2029 * transfers.
2030 */
2031static void spi_set_thread_rt(struct spi_controller *ctlr)
2032{
2033	dev_info(&ctlr->dev,
2034		"will run message pump with realtime priority\n");
2035	sched_set_fifo(ctlr->kworker->task);
2036}
2037
2038static int spi_init_queue(struct spi_controller *ctlr)
2039{
2040	ctlr->running = false;
2041	ctlr->busy = false;
2042	ctlr->queue_empty = true;
2043
2044	ctlr->kworker = kthread_run_worker(0, dev_name(&ctlr->dev));
2045	if (IS_ERR(ctlr->kworker)) {
2046		dev_err(&ctlr->dev, "failed to create message pump kworker\n");
2047		return PTR_ERR(ctlr->kworker);
2048	}
2049
2050	kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
2051
2052	/*
2053	 * Controller config will indicate if this controller should run the
2054	 * message pump with high (realtime) priority to reduce the transfer
2055	 * latency on the bus by minimising the delay between a transfer
2056	 * request and the scheduling of the message pump thread. Without this
2057	 * setting the message pump thread will remain at default priority.
2058	 */
2059	if (ctlr->rt)
2060		spi_set_thread_rt(ctlr);
2061
2062	return 0;
2063}
2064
2065/**
2066 * spi_get_next_queued_message() - called by driver to check for queued
2067 * messages
2068 * @ctlr: the controller to check for queued messages
2069 *
2070 * If there are more messages in the queue, the next message is returned from
2071 * this call.
2072 *
2073 * Return: the next message in the queue, else NULL if the queue is empty.
2074 */
2075struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
2076{
2077	struct spi_message *next;
2078	unsigned long flags;
2079
2080	/* Get a pointer to the next message, if any */
2081	spin_lock_irqsave(&ctlr->queue_lock, flags);
2082	next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
2083					queue);
2084	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2085
2086	return next;
2087}
2088EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
2089
2090/*
2091 * __spi_unoptimize_message - shared implementation of spi_unoptimize_message()
2092 *                            and spi_maybe_unoptimize_message()
2093 * @msg: the message to unoptimize
2094 *
2095 * Peripheral drivers should use spi_unoptimize_message() and callers inside
2096 * core should use spi_maybe_unoptimize_message() rather than calling this
2097 * function directly.
2098 *
2099 * It is not valid to call this on a message that is not currently optimized.
2100 */
2101static void __spi_unoptimize_message(struct spi_message *msg)
2102{
2103	struct spi_controller *ctlr = msg->spi->controller;
2104
2105	if (ctlr->unoptimize_message)
2106		ctlr->unoptimize_message(msg);
2107
2108	spi_res_release(ctlr, msg);
2109
2110	msg->optimized = false;
2111	msg->opt_state = NULL;
2112}
2113
2114/*
2115 * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral
2116 * @msg: the message to unoptimize
2117 *
2118 * This function is used to unoptimize a message if and only if it was
2119 * optimized by the core (via spi_maybe_optimize_message()).
2120 */
2121static void spi_maybe_unoptimize_message(struct spi_message *msg)
2122{
2123	if (!msg->pre_optimized && msg->optimized &&
2124	    !msg->spi->controller->defer_optimize_message)
2125		__spi_unoptimize_message(msg);
2126}
2127
2128/**
2129 * spi_finalize_current_message() - the current message is complete
2130 * @ctlr: the controller to return the message to
2131 *
2132 * Called by the driver to notify the core that the message in the front of the
2133 * queue is complete and can be removed from the queue.
2134 */
2135void spi_finalize_current_message(struct spi_controller *ctlr)
2136{
2137	struct spi_transfer *xfer;
2138	struct spi_message *mesg;
2139	int ret;
2140
2141	mesg = ctlr->cur_msg;
2142
2143	if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
2144		list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
2145			ptp_read_system_postts(xfer->ptp_sts);
2146			xfer->ptp_sts_word_post = xfer->len;
2147		}
2148	}
2149
2150	if (unlikely(ctlr->ptp_sts_supported))
2151		list_for_each_entry(xfer, &mesg->transfers, transfer_list)
2152			WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
2153
2154	spi_unmap_msg(ctlr, mesg);
2155
2156	if (mesg->prepared && ctlr->unprepare_message) {
2157		ret = ctlr->unprepare_message(ctlr, mesg);
2158		if (ret) {
2159			dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
2160				ret);
2161		}
2162	}
2163
2164	mesg->prepared = false;
2165
2166	spi_maybe_unoptimize_message(mesg);
2167
2168	WRITE_ONCE(ctlr->cur_msg_incomplete, false);
2169	smp_mb(); /* See __spi_pump_transfer_message()... */
2170	if (READ_ONCE(ctlr->cur_msg_need_completion))
2171		complete(&ctlr->cur_msg_completion);
2172
2173	trace_spi_message_done(mesg);
2174
2175	mesg->state = NULL;
2176	if (mesg->complete)
2177		mesg->complete(mesg->context);
2178}
2179EXPORT_SYMBOL_GPL(spi_finalize_current_message);
2180
2181static int spi_start_queue(struct spi_controller *ctlr)
2182{
2183	unsigned long flags;
2184
2185	spin_lock_irqsave(&ctlr->queue_lock, flags);
2186
2187	if (ctlr->running || ctlr->busy) {
2188		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2189		return -EBUSY;
2190	}
2191
2192	ctlr->running = true;
2193	ctlr->cur_msg = NULL;
2194	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2195
2196	kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2197
2198	return 0;
2199}
2200
2201static int spi_stop_queue(struct spi_controller *ctlr)
2202{
2203	unsigned int limit = 500;
2204	unsigned long flags;
2205
2206	/*
2207	 * This is a bit lame, but is optimized for the common execution path.
2208	 * A wait_queue on the ctlr->busy could be used, but then the common
2209	 * execution path (pump_messages) would be required to call wake_up or
2210	 * friends on every SPI message. Do this instead.
2211	 */
2212	do {
2213		spin_lock_irqsave(&ctlr->queue_lock, flags);
2214		if (list_empty(&ctlr->queue) && !ctlr->busy) {
2215			ctlr->running = false;
2216			spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2217			return 0;
2218		}
2219		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2220		usleep_range(10000, 11000);
2221	} while (--limit);
2222
2223	return -EBUSY;
2224}
2225
2226static int spi_destroy_queue(struct spi_controller *ctlr)
2227{
2228	int ret;
2229
2230	ret = spi_stop_queue(ctlr);
2231
2232	/*
2233	 * kthread_flush_worker will block until all work is done.
2234	 * If the reason that stop_queue timed out is that the work will never
2235	 * finish, then it does no good to call flush/stop thread, so
2236	 * return anyway.
2237	 */
2238	if (ret) {
2239		dev_err(&ctlr->dev, "problem destroying queue\n");
2240		return ret;
2241	}
2242
2243	kthread_destroy_worker(ctlr->kworker);
2244
2245	return 0;
2246}
2247
2248static int __spi_queued_transfer(struct spi_device *spi,
2249				 struct spi_message *msg,
2250				 bool need_pump)
2251{
2252	struct spi_controller *ctlr = spi->controller;
2253	unsigned long flags;
2254
2255	spin_lock_irqsave(&ctlr->queue_lock, flags);
2256
2257	if (!ctlr->running) {
2258		spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2259		return -ESHUTDOWN;
2260	}
2261	msg->actual_length = 0;
2262	msg->status = -EINPROGRESS;
2263
2264	list_add_tail(&msg->queue, &ctlr->queue);
2265	ctlr->queue_empty = false;
2266	if (!ctlr->busy && need_pump)
2267		kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
2268
2269	spin_unlock_irqrestore(&ctlr->queue_lock, flags);
2270	return 0;
2271}
2272
2273/**
2274 * spi_queued_transfer - transfer function for queued transfers
2275 * @spi: SPI device which is requesting transfer
2276 * @msg: SPI message which is to handled is queued to driver queue
2277 *
2278 * Return: zero on success, else a negative error code.
2279 */
2280static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
2281{
2282	return __spi_queued_transfer(spi, msg, true);
2283}
2284
2285static int spi_controller_initialize_queue(struct spi_controller *ctlr)
2286{
2287	int ret;
2288
2289	ctlr->transfer = spi_queued_transfer;
2290	if (!ctlr->transfer_one_message)
2291		ctlr->transfer_one_message = spi_transfer_one_message;
2292
2293	/* Initialize and start queue */
2294	ret = spi_init_queue(ctlr);
2295	if (ret) {
2296		dev_err(&ctlr->dev, "problem initializing queue\n");
2297		goto err_init_queue;
2298	}
2299	ctlr->queued = true;
2300	ret = spi_start_queue(ctlr);
2301	if (ret) {
2302		dev_err(&ctlr->dev, "problem starting queue\n");
2303		goto err_start_queue;
2304	}
2305
2306	return 0;
2307
2308err_start_queue:
2309	spi_destroy_queue(ctlr);
2310err_init_queue:
2311	return ret;
2312}
2313
2314/**
2315 * spi_flush_queue - Send all pending messages in the queue from the callers'
2316 *		     context
2317 * @ctlr: controller to process queue for
2318 *
2319 * This should be used when one wants to ensure all pending messages have been
2320 * sent before doing something. Is used by the spi-mem code to make sure SPI
2321 * memory operations do not preempt regular SPI transfers that have been queued
2322 * before the spi-mem operation.
2323 */
2324void spi_flush_queue(struct spi_controller *ctlr)
2325{
2326	if (ctlr->transfer == spi_queued_transfer)
2327		__spi_pump_messages(ctlr, false);
2328}
2329
2330/*-------------------------------------------------------------------------*/
2331
2332#if defined(CONFIG_OF)
2333static void of_spi_parse_dt_cs_delay(struct device_node *nc,
2334				     struct spi_delay *delay, const char *prop)
2335{
2336	u32 value;
2337
2338	if (!of_property_read_u32(nc, prop, &value)) {
2339		if (value > U16_MAX) {
2340			delay->value = DIV_ROUND_UP(value, 1000);
2341			delay->unit = SPI_DELAY_UNIT_USECS;
2342		} else {
2343			delay->value = value;
2344			delay->unit = SPI_DELAY_UNIT_NSECS;
2345		}
2346	}
2347}
2348
2349static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
2350			   struct device_node *nc)
2351{
2352	u32 value, cs[SPI_DEVICE_CS_CNT_MAX];
2353	int rc, idx;
2354
2355	/* Mode (clock phase/polarity/etc.) */
2356	if (of_property_read_bool(nc, "spi-cpha"))
2357		spi->mode |= SPI_CPHA;
2358	if (of_property_read_bool(nc, "spi-cpol"))
2359		spi->mode |= SPI_CPOL;
2360	if (of_property_read_bool(nc, "spi-3wire"))
2361		spi->mode |= SPI_3WIRE;
2362	if (of_property_read_bool(nc, "spi-lsb-first"))
2363		spi->mode |= SPI_LSB_FIRST;
2364	if (of_property_read_bool(nc, "spi-cs-high"))
2365		spi->mode |= SPI_CS_HIGH;
2366
2367	/* Device DUAL/QUAD mode */
2368	if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
2369		switch (value) {
2370		case 0:
2371			spi->mode |= SPI_NO_TX;
2372			break;
2373		case 1:
2374			break;
2375		case 2:
2376			spi->mode |= SPI_TX_DUAL;
2377			break;
2378		case 4:
2379			spi->mode |= SPI_TX_QUAD;
2380			break;
2381		case 8:
2382			spi->mode |= SPI_TX_OCTAL;
2383			break;
2384		default:
2385			dev_warn(&ctlr->dev,
2386				"spi-tx-bus-width %d not supported\n",
2387				value);
2388			break;
2389		}
2390	}
2391
2392	if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
2393		switch (value) {
2394		case 0:
2395			spi->mode |= SPI_NO_RX;
2396			break;
2397		case 1:
2398			break;
2399		case 2:
2400			spi->mode |= SPI_RX_DUAL;
2401			break;
2402		case 4:
2403			spi->mode |= SPI_RX_QUAD;
2404			break;
2405		case 8:
2406			spi->mode |= SPI_RX_OCTAL;
2407			break;
2408		default:
2409			dev_warn(&ctlr->dev,
2410				"spi-rx-bus-width %d not supported\n",
2411				value);
2412			break;
2413		}
2414	}
2415
2416	if (spi_controller_is_target(ctlr)) {
2417		if (!of_node_name_eq(nc, "slave")) {
2418			dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
2419				nc);
2420			return -EINVAL;
2421		}
2422		return 0;
2423	}
2424
2425	/* Device address */
2426	rc = of_property_read_variable_u32_array(nc, "reg", &cs[0], 1,
2427						 SPI_DEVICE_CS_CNT_MAX);
2428	if (rc < 0) {
2429		dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
2430			nc, rc);
2431		return rc;
2432	}
2433
2434	if ((of_property_present(nc, "parallel-memories")) &&
2435	    (!(ctlr->flags & SPI_CONTROLLER_MULTI_CS))) {
2436		dev_err(&ctlr->dev, "SPI controller doesn't support multi CS\n");
2437		return -EINVAL;
2438	}
2439
2440	spi->num_chipselect = rc;
2441	for (idx = 0; idx < rc; idx++)
2442		spi_set_chipselect(spi, idx, cs[idx]);
2443
2444	/*
2445	 * By default spi->chip_select[0] will hold the physical CS number,
2446	 * so set bit 0 in spi->cs_index_mask.
2447	 */
2448	spi->cs_index_mask = BIT(0);
2449
2450	/* Device speed */
2451	if (!of_property_read_u32(nc, "spi-max-frequency", &value))
2452		spi->max_speed_hz = value;
2453
2454	/* Device CS delays */
2455	of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns");
2456	of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns");
2457	of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns");
2458
2459	return 0;
2460}
2461
2462static struct spi_device *
2463of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
2464{
2465	struct spi_device *spi;
2466	int rc;
2467
2468	/* Alloc an spi_device */
2469	spi = spi_alloc_device(ctlr);
2470	if (!spi) {
2471		dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
2472		rc = -ENOMEM;
2473		goto err_out;
2474	}
2475
2476	/* Select device driver */
2477	rc = of_alias_from_compatible(nc, spi->modalias,
2478				      sizeof(spi->modalias));
2479	if (rc < 0) {
2480		dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
2481		goto err_out;
2482	}
2483
2484	rc = of_spi_parse_dt(ctlr, spi, nc);
2485	if (rc)
2486		goto err_out;
2487
2488	/* Store a pointer to the node in the device structure */
2489	of_node_get(nc);
2490
2491	device_set_node(&spi->dev, of_fwnode_handle(nc));
2492
2493	/* Register the new device */
2494	rc = spi_add_device(spi);
2495	if (rc) {
2496		dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
2497		goto err_of_node_put;
2498	}
2499
2500	return spi;
2501
2502err_of_node_put:
2503	of_node_put(nc);
2504err_out:
2505	spi_dev_put(spi);
2506	return ERR_PTR(rc);
2507}
2508
2509/**
2510 * of_register_spi_devices() - Register child devices onto the SPI bus
2511 * @ctlr:	Pointer to spi_controller device
2512 *
2513 * Registers an spi_device for each child node of controller node which
2514 * represents a valid SPI target device.
2515 */
2516static void of_register_spi_devices(struct spi_controller *ctlr)
2517{
2518	struct spi_device *spi;
2519	struct device_node *nc;
2520
2521	for_each_available_child_of_node(ctlr->dev.of_node, nc) {
2522		if (of_node_test_and_set_flag(nc, OF_POPULATED))
2523			continue;
2524		spi = of_register_spi_device(ctlr, nc);
2525		if (IS_ERR(spi)) {
2526			dev_warn(&ctlr->dev,
2527				 "Failed to create SPI device for %pOF\n", nc);
2528			of_node_clear_flag(nc, OF_POPULATED);
2529		}
2530	}
2531}
2532#else
2533static void of_register_spi_devices(struct spi_controller *ctlr) { }
2534#endif
2535
2536/**
2537 * spi_new_ancillary_device() - Register ancillary SPI device
2538 * @spi:         Pointer to the main SPI device registering the ancillary device
2539 * @chip_select: Chip Select of the ancillary device
2540 *
2541 * Register an ancillary SPI device; for example some chips have a chip-select
2542 * for normal device usage and another one for setup/firmware upload.
2543 *
2544 * This may only be called from main SPI device's probe routine.
2545 *
2546 * Return: 0 on success; negative errno on failure
2547 */
2548struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
2549					     u8 chip_select)
2550{
2551	struct spi_controller *ctlr = spi->controller;
2552	struct spi_device *ancillary;
2553	int rc;
2554
2555	/* Alloc an spi_device */
2556	ancillary = spi_alloc_device(ctlr);
2557	if (!ancillary) {
2558		rc = -ENOMEM;
2559		goto err_out;
2560	}
2561
2562	strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias));
2563
2564	/* Use provided chip-select for ancillary device */
2565	spi_set_chipselect(ancillary, 0, chip_select);
2566
2567	/* Take over SPI mode/speed from SPI main device */
2568	ancillary->max_speed_hz = spi->max_speed_hz;
2569	ancillary->mode = spi->mode;
2570	/*
2571	 * By default spi->chip_select[0] will hold the physical CS number,
2572	 * so set bit 0 in spi->cs_index_mask.
2573	 */
2574	ancillary->cs_index_mask = BIT(0);
2575
2576	WARN_ON(!mutex_is_locked(&ctlr->add_lock));
2577
2578	/* Register the new device */
2579	rc = __spi_add_device(ancillary);
2580	if (rc) {
2581		dev_err(&spi->dev, "failed to register ancillary device\n");
2582		goto err_out;
2583	}
2584
2585	return ancillary;
2586
2587err_out:
2588	spi_dev_put(ancillary);
2589	return ERR_PTR(rc);
2590}
2591EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
2592
2593#ifdef CONFIG_ACPI
2594struct acpi_spi_lookup {
2595	struct spi_controller 	*ctlr;
2596	u32			max_speed_hz;
2597	u32			mode;
2598	int			irq;
2599	u8			bits_per_word;
2600	u8			chip_select;
2601	int			n;
2602	int			index;
2603};
2604
2605static int acpi_spi_count(struct acpi_resource *ares, void *data)
2606{
2607	struct acpi_resource_spi_serialbus *sb;
2608	int *count = data;
2609
2610	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2611		return 1;
2612
2613	sb = &ares->data.spi_serial_bus;
2614	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI)
2615		return 1;
2616
2617	*count = *count + 1;
2618
2619	return 1;
2620}
2621
2622/**
2623 * acpi_spi_count_resources - Count the number of SpiSerialBus resources
2624 * @adev:	ACPI device
2625 *
2626 * Return: the number of SpiSerialBus resources in the ACPI-device's
2627 * resource-list; or a negative error code.
2628 */
2629int acpi_spi_count_resources(struct acpi_device *adev)
2630{
2631	LIST_HEAD(r);
2632	int count = 0;
2633	int ret;
2634
2635	ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count);
2636	if (ret < 0)
2637		return ret;
2638
2639	acpi_dev_free_resource_list(&r);
2640
2641	return count;
2642}
2643EXPORT_SYMBOL_GPL(acpi_spi_count_resources);
2644
2645static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2646					    struct acpi_spi_lookup *lookup)
2647{
2648	const union acpi_object *obj;
2649
2650	if (!x86_apple_machine)
2651		return;
2652
2653	if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
2654	    && obj->buffer.length >= 4)
2655		lookup->max_speed_hz  = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
2656
2657	if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
2658	    && obj->buffer.length == 8)
2659		lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
2660
2661	if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
2662	    && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
2663		lookup->mode |= SPI_LSB_FIRST;
2664
2665	if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
2666	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
2667		lookup->mode |= SPI_CPOL;
2668
2669	if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
2670	    && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
2671		lookup->mode |= SPI_CPHA;
2672}
2673
2674static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
2675{
2676	struct acpi_spi_lookup *lookup = data;
2677	struct spi_controller *ctlr = lookup->ctlr;
2678
2679	if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2680		struct acpi_resource_spi_serialbus *sb;
2681		acpi_handle parent_handle;
2682		acpi_status status;
2683
2684		sb = &ares->data.spi_serial_bus;
2685		if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
2686
2687			if (lookup->index != -1 && lookup->n++ != lookup->index)
2688				return 1;
2689
2690			status = acpi_get_handle(NULL,
2691						 sb->resource_source.string_ptr,
2692						 &parent_handle);
2693
2694			if (ACPI_FAILURE(status))
2695				return -ENODEV;
2696
2697			if (ctlr) {
2698				if (!device_match_acpi_handle(ctlr->dev.parent, parent_handle))
2699					return -ENODEV;
2700			} else {
2701				struct acpi_device *adev;
2702
2703				adev = acpi_fetch_acpi_dev(parent_handle);
2704				if (!adev)
2705					return -ENODEV;
2706
2707				ctlr = acpi_spi_find_controller_by_adev(adev);
2708				if (!ctlr)
2709					return -EPROBE_DEFER;
2710
2711				lookup->ctlr = ctlr;
2712			}
2713
2714			/*
2715			 * ACPI DeviceSelection numbering is handled by the
2716			 * host controller driver in Windows and can vary
2717			 * from driver to driver. In Linux we always expect
2718			 * 0 .. max - 1 so we need to ask the driver to
2719			 * translate between the two schemes.
2720			 */
2721			if (ctlr->fw_translate_cs) {
2722				int cs = ctlr->fw_translate_cs(ctlr,
2723						sb->device_selection);
2724				if (cs < 0)
2725					return cs;
2726				lookup->chip_select = cs;
2727			} else {
2728				lookup->chip_select = sb->device_selection;
2729			}
2730
2731			lookup->max_speed_hz = sb->connection_speed;
2732			lookup->bits_per_word = sb->data_bit_length;
2733
2734			if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
2735				lookup->mode |= SPI_CPHA;
2736			if (sb->clock_polarity == ACPI_SPI_START_HIGH)
2737				lookup->mode |= SPI_CPOL;
2738			if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
2739				lookup->mode |= SPI_CS_HIGH;
2740		}
2741	} else if (lookup->irq < 0) {
2742		struct resource r;
2743
2744		if (acpi_dev_resource_interrupt(ares, 0, &r))
2745			lookup->irq = r.start;
2746	}
2747
2748	/* Always tell the ACPI core to skip this resource */
2749	return 1;
2750}
2751
2752/**
2753 * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information
2754 * @ctlr: controller to which the spi device belongs
2755 * @adev: ACPI Device for the spi device
2756 * @index: Index of the spi resource inside the ACPI Node
2757 *
2758 * This should be used to allocate a new SPI device from and ACPI Device node.
2759 * The caller is responsible for calling spi_add_device to register the SPI device.
2760 *
2761 * If ctlr is set to NULL, the Controller for the SPI device will be looked up
2762 * using the resource.
2763 * If index is set to -1, index is not used.
2764 * Note: If index is -1, ctlr must be set.
2765 *
2766 * Return: a pointer to the new device, or ERR_PTR on error.
2767 */
2768struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
2769					 struct acpi_device *adev,
2770					 int index)
2771{
2772	acpi_handle parent_handle = NULL;
2773	struct list_head resource_list;
2774	struct acpi_spi_lookup lookup = {};
2775	struct spi_device *spi;
2776	int ret;
2777
2778	if (!ctlr && index == -1)
2779		return ERR_PTR(-EINVAL);
2780
2781	lookup.ctlr		= ctlr;
2782	lookup.irq		= -1;
2783	lookup.index		= index;
2784	lookup.n		= 0;
2785
2786	INIT_LIST_HEAD(&resource_list);
2787	ret = acpi_dev_get_resources(adev, &resource_list,
2788				     acpi_spi_add_resource, &lookup);
2789	acpi_dev_free_resource_list(&resource_list);
2790
2791	if (ret < 0)
2792		/* Found SPI in _CRS but it points to another controller */
2793		return ERR_PTR(ret);
2794
2795	if (!lookup.max_speed_hz &&
2796	    ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
2797	    device_match_acpi_handle(lookup.ctlr->dev.parent, parent_handle)) {
2798		/* Apple does not use _CRS but nested devices for SPI target devices */
2799		acpi_spi_parse_apple_properties(adev, &lookup);
2800	}
2801
2802	if (!lookup.max_speed_hz)
2803		return ERR_PTR(-ENODEV);
2804
2805	spi = spi_alloc_device(lookup.ctlr);
2806	if (!spi) {
2807		dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n",
2808			dev_name(&adev->dev));
2809		return ERR_PTR(-ENOMEM);
2810	}
2811
2812	spi_set_chipselect(spi, 0, lookup.chip_select);
2813
2814	ACPI_COMPANION_SET(&spi->dev, adev);
2815	spi->max_speed_hz	= lookup.max_speed_hz;
2816	spi->mode		|= lookup.mode;
2817	spi->irq		= lookup.irq;
2818	spi->bits_per_word	= lookup.bits_per_word;
2819	/*
2820	 * By default spi->chip_select[0] will hold the physical CS number,
2821	 * so set bit 0 in spi->cs_index_mask.
2822	 */
2823	spi->cs_index_mask	= BIT(0);
2824
2825	return spi;
2826}
2827EXPORT_SYMBOL_GPL(acpi_spi_device_alloc);
2828
2829static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
2830					    struct acpi_device *adev)
2831{
2832	struct spi_device *spi;
2833
2834	if (acpi_bus_get_status(adev) || !adev->status.present ||
2835	    acpi_device_enumerated(adev))
2836		return AE_OK;
2837
2838	spi = acpi_spi_device_alloc(ctlr, adev, -1);
2839	if (IS_ERR(spi)) {
2840		if (PTR_ERR(spi) == -ENOMEM)
2841			return AE_NO_MEMORY;
2842		else
2843			return AE_OK;
2844	}
2845
2846	acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2847			  sizeof(spi->modalias));
2848
2849	/*
2850	 * This gets re-tried in spi_probe() for -EPROBE_DEFER handling in case
2851	 * the GPIO controller does not have a driver yet. This needs to be done
2852	 * here too, because this call sets the GPIO direction and/or bias.
2853	 * Setting these needs to be done even if there is no driver, in which
2854	 * case spi_probe() will never get called.
2855	 * TODO: ideally the setup of the GPIO should be handled in a generic
2856	 * manner in the ACPI/gpiolib core code.
2857	 */
2858	if (spi->irq < 0)
2859		spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2860
2861	acpi_device_set_enumerated(adev);
2862
2863	adev->power.flags.ignore_parent = true;
2864	if (spi_add_device(spi)) {
2865		adev->power.flags.ignore_parent = false;
2866		dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
2867			dev_name(&adev->dev));
2868		spi_dev_put(spi);
2869	}
2870
2871	return AE_OK;
2872}
2873
2874static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
2875				       void *data, void **return_value)
2876{
2877	struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
2878	struct spi_controller *ctlr = data;
2879
2880	if (!adev)
2881		return AE_OK;
2882
2883	return acpi_register_spi_device(ctlr, adev);
2884}
2885
2886#define SPI_ACPI_ENUMERATE_MAX_DEPTH		32
2887
2888static void acpi_register_spi_devices(struct spi_controller *ctlr)
2889{
2890	acpi_status status;
2891	acpi_handle handle;
2892
2893	handle = ACPI_HANDLE(ctlr->dev.parent);
2894	if (!handle)
2895		return;
2896
2897	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2898				     SPI_ACPI_ENUMERATE_MAX_DEPTH,
2899				     acpi_spi_add_device, NULL, ctlr, NULL);
2900	if (ACPI_FAILURE(status))
2901		dev_warn(&ctlr->dev, "failed to enumerate SPI target devices\n");
2902}
2903#else
2904static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
2905#endif /* CONFIG_ACPI */
2906
2907static void spi_controller_release(struct device *dev)
2908{
2909	struct spi_controller *ctlr;
2910
2911	ctlr = container_of(dev, struct spi_controller, dev);
2912
2913	free_percpu(ctlr->pcpu_statistics);
2914	kfree(ctlr);
2915}
2916
2917static const struct class spi_controller_class = {
2918	.name		= "spi_master",
2919	.dev_release	= spi_controller_release,
2920	.dev_groups	= spi_controller_groups,
2921};
2922
2923#ifdef CONFIG_SPI_SLAVE
2924/**
2925 * spi_target_abort - abort the ongoing transfer request on an SPI target controller
2926 * @spi: device used for the current transfer
2927 */
2928int spi_target_abort(struct spi_device *spi)
2929{
2930	struct spi_controller *ctlr = spi->controller;
2931
2932	if (spi_controller_is_target(ctlr) && ctlr->target_abort)
2933		return ctlr->target_abort(ctlr);
2934
2935	return -ENOTSUPP;
2936}
2937EXPORT_SYMBOL_GPL(spi_target_abort);
2938
2939static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2940			  char *buf)
2941{
2942	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2943						   dev);
2944	struct device *child;
2945	int ret;
2946
2947	child = device_find_any_child(&ctlr->dev);
2948	ret = sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL);
2949	put_device(child);
2950
2951	return ret;
2952}
2953
2954static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2955			   const char *buf, size_t count)
2956{
2957	struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2958						   dev);
2959	struct spi_device *spi;
2960	struct device *child;
2961	char name[32];
2962	int rc;
2963
2964	rc = sscanf(buf, "%31s", name);
2965	if (rc != 1 || !name[0])
2966		return -EINVAL;
2967
2968	child = device_find_any_child(&ctlr->dev);
2969	if (child) {
2970		/* Remove registered target device */
2971		device_unregister(child);
2972		put_device(child);
2973	}
2974
2975	if (strcmp(name, "(null)")) {
2976		/* Register new target device */
2977		spi = spi_alloc_device(ctlr);
2978		if (!spi)
2979			return -ENOMEM;
2980
2981		strscpy(spi->modalias, name, sizeof(spi->modalias));
2982
2983		rc = spi_add_device(spi);
2984		if (rc) {
2985			spi_dev_put(spi);
2986			return rc;
2987		}
2988	}
2989
2990	return count;
2991}
2992
2993static DEVICE_ATTR_RW(slave);
2994
2995static struct attribute *spi_target_attrs[] = {
2996	&dev_attr_slave.attr,
2997	NULL,
2998};
2999
3000static const struct attribute_group spi_target_group = {
3001	.attrs = spi_target_attrs,
3002};
3003
3004static const struct attribute_group *spi_target_groups[] = {
3005	&spi_controller_statistics_group,
3006	&spi_target_group,
3007	NULL,
3008};
3009
3010static const struct class spi_target_class = {
3011	.name		= "spi_slave",
3012	.dev_release	= spi_controller_release,
3013	.dev_groups	= spi_target_groups,
3014};
3015#else
3016extern struct class spi_target_class;	/* dummy */
3017#endif
3018
3019/**
3020 * __spi_alloc_controller - allocate an SPI host or target controller
3021 * @dev: the controller, possibly using the platform_bus
3022 * @size: how much zeroed driver-private data to allocate; the pointer to this
3023 *	memory is in the driver_data field of the returned device, accessible
3024 *	with spi_controller_get_devdata(); the memory is cacheline aligned;
3025 *	drivers granting DMA access to portions of their private data need to
3026 *	round up @size using ALIGN(size, dma_get_cache_alignment()).
3027 * @target: flag indicating whether to allocate an SPI host (false) or SPI target (true)
3028 *	controller
3029 * Context: can sleep
3030 *
3031 * This call is used only by SPI controller drivers, which are the
3032 * only ones directly touching chip registers.  It's how they allocate
3033 * an spi_controller structure, prior to calling spi_register_controller().
3034 *
3035 * This must be called from context that can sleep.
3036 *
3037 * The caller is responsible for assigning the bus number and initializing the
3038 * controller's methods before calling spi_register_controller(); and (after
3039 * errors adding the device) calling spi_controller_put() to prevent a memory
3040 * leak.
3041 *
3042 * Return: the SPI controller structure on success, else NULL.
3043 */
3044struct spi_controller *__spi_alloc_controller(struct device *dev,
3045					      unsigned int size, bool target)
3046{
3047	struct spi_controller	*ctlr;
3048	size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
3049
3050	if (!dev)
3051		return NULL;
3052
3053	ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
3054	if (!ctlr)
3055		return NULL;
3056
3057	ctlr->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
3058	if (!ctlr->pcpu_statistics) {
3059		kfree(ctlr);
3060		return NULL;
3061	}
3062
3063	device_initialize(&ctlr->dev);
3064	INIT_LIST_HEAD(&ctlr->queue);
3065	spin_lock_init(&ctlr->queue_lock);
3066	spin_lock_init(&ctlr->bus_lock_spinlock);
3067	mutex_init(&ctlr->bus_lock_mutex);
3068	mutex_init(&ctlr->io_mutex);
3069	mutex_init(&ctlr->add_lock);
3070	ctlr->bus_num = -1;
3071	ctlr->num_chipselect = 1;
3072	ctlr->target = target;
3073	if (IS_ENABLED(CONFIG_SPI_SLAVE) && target)
3074		ctlr->dev.class = &spi_target_class;
3075	else
3076		ctlr->dev.class = &spi_controller_class;
3077	ctlr->dev.parent = dev;
3078	pm_suspend_ignore_children(&ctlr->dev, true);
3079	spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
3080
3081	return ctlr;
3082}
3083EXPORT_SYMBOL_GPL(__spi_alloc_controller);
3084
3085static void devm_spi_release_controller(struct device *dev, void *ctlr)
3086{
3087	spi_controller_put(*(struct spi_controller **)ctlr);
3088}
3089
3090/**
3091 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
3092 * @dev: physical device of SPI controller
3093 * @size: how much zeroed driver-private data to allocate
3094 * @target: whether to allocate an SPI host (false) or SPI target (true) controller
3095 * Context: can sleep
3096 *
3097 * Allocate an SPI controller and automatically release a reference on it
3098 * when @dev is unbound from its driver.  Drivers are thus relieved from
3099 * having to call spi_controller_put().
3100 *
3101 * The arguments to this function are identical to __spi_alloc_controller().
3102 *
3103 * Return: the SPI controller structure on success, else NULL.
3104 */
3105struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
3106						   unsigned int size,
3107						   bool target)
3108{
3109	struct spi_controller **ptr, *ctlr;
3110
3111	ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr),
3112			   GFP_KERNEL);
3113	if (!ptr)
3114		return NULL;
3115
3116	ctlr = __spi_alloc_controller(dev, size, target);
3117	if (ctlr) {
3118		ctlr->devm_allocated = true;
3119		*ptr = ctlr;
3120		devres_add(dev, ptr);
3121	} else {
3122		devres_free(ptr);
3123	}
3124
3125	return ctlr;
3126}
3127EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
3128
3129/**
3130 * spi_get_gpio_descs() - grab chip select GPIOs for the controller
3131 * @ctlr: The SPI controller to grab GPIO descriptors for
3132 */
3133static int spi_get_gpio_descs(struct spi_controller *ctlr)
3134{
3135	int nb, i;
3136	struct gpio_desc **cs;
3137	struct device *dev = &ctlr->dev;
3138	unsigned long native_cs_mask = 0;
3139	unsigned int num_cs_gpios = 0;
3140
3141	nb = gpiod_count(dev, "cs");
3142	if (nb < 0) {
3143		/* No GPIOs at all is fine, else return the error */
3144		if (nb == -ENOENT)
3145			return 0;
3146		return nb;
3147	}
3148
3149	ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
3150
3151	cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
3152			  GFP_KERNEL);
3153	if (!cs)
3154		return -ENOMEM;
3155	ctlr->cs_gpiods = cs;
3156
3157	for (i = 0; i < nb; i++) {
3158		/*
3159		 * Most chipselects are active low, the inverted
3160		 * semantics are handled by special quirks in gpiolib,
3161		 * so initializing them GPIOD_OUT_LOW here means
3162		 * "unasserted", in most cases this will drive the physical
3163		 * line high.
3164		 */
3165		cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
3166						      GPIOD_OUT_LOW);
3167		if (IS_ERR(cs[i]))
3168			return PTR_ERR(cs[i]);
3169
3170		if (cs[i]) {
3171			/*
3172			 * If we find a CS GPIO, name it after the device and
3173			 * chip select line.
3174			 */
3175			char *gpioname;
3176
3177			gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
3178						  dev_name(dev), i);
3179			if (!gpioname)
3180				return -ENOMEM;
3181			gpiod_set_consumer_name(cs[i], gpioname);
3182			num_cs_gpios++;
3183			continue;
3184		}
3185
3186		if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
3187			dev_err(dev, "Invalid native chip select %d\n", i);
3188			return -EINVAL;
3189		}
3190		native_cs_mask |= BIT(i);
3191	}
3192
3193	ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
3194
3195	if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios &&
3196	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
3197		dev_err(dev, "No unused native chip select available\n");
3198		return -EINVAL;
3199	}
3200
3201	return 0;
3202}
3203
3204static int spi_controller_check_ops(struct spi_controller *ctlr)
3205{
3206	/*
3207	 * The controller may implement only the high-level SPI-memory like
3208	 * operations if it does not support regular SPI transfers, and this is
3209	 * valid use case.
3210	 * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least
3211	 * one of the ->transfer_xxx() method be implemented.
3212	 */
3213	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
3214		if (!ctlr->transfer && !ctlr->transfer_one &&
3215		   !ctlr->transfer_one_message) {
3216			return -EINVAL;
3217		}
3218	}
3219
3220	return 0;
3221}
3222
3223/* Allocate dynamic bus number using Linux idr */
3224static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int end)
3225{
3226	int id;
3227
3228	mutex_lock(&board_lock);
3229	id = idr_alloc(&spi_controller_idr, ctlr, start, end, GFP_KERNEL);
3230	mutex_unlock(&board_lock);
3231	if (WARN(id < 0, "couldn't get idr"))
3232		return id == -ENOSPC ? -EBUSY : id;
3233	ctlr->bus_num = id;
3234	return 0;
3235}
3236
3237/**
3238 * spi_register_controller - register SPI host or target controller
3239 * @ctlr: initialized controller, originally from spi_alloc_host() or
3240 *	spi_alloc_target()
3241 * Context: can sleep
3242 *
3243 * SPI controllers connect to their drivers using some non-SPI bus,
3244 * such as the platform bus.  The final stage of probe() in that code
3245 * includes calling spi_register_controller() to hook up to this SPI bus glue.
3246 *
3247 * SPI controllers use board specific (often SOC specific) bus numbers,
3248 * and board-specific addressing for SPI devices combines those numbers
3249 * with chip select numbers.  Since SPI does not directly support dynamic
3250 * device identification, boards need configuration tables telling which
3251 * chip is at which address.
3252 *
3253 * This must be called from context that can sleep.  It returns zero on
3254 * success, else a negative error code (dropping the controller's refcount).
3255 * After a successful return, the caller is responsible for calling
3256 * spi_unregister_controller().
3257 *
3258 * Return: zero on success, else a negative error code.
3259 */
3260int spi_register_controller(struct spi_controller *ctlr)
3261{
3262	struct device		*dev = ctlr->dev.parent;
3263	struct boardinfo	*bi;
3264	int			first_dynamic;
3265	int			status;
3266	int			idx;
3267
3268	if (!dev)
3269		return -ENODEV;
3270
3271	/*
3272	 * Make sure all necessary hooks are implemented before registering
3273	 * the SPI controller.
3274	 */
3275	status = spi_controller_check_ops(ctlr);
3276	if (status)
3277		return status;
3278
3279	if (ctlr->bus_num < 0)
3280		ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
3281	if (ctlr->bus_num >= 0) {
3282		/* Devices with a fixed bus num must check-in with the num */
3283		status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1);
3284		if (status)
3285			return status;
3286	}
3287	if (ctlr->bus_num < 0) {
3288		first_dynamic = of_alias_get_highest_id("spi");
3289		if (first_dynamic < 0)
3290			first_dynamic = 0;
3291		else
3292			first_dynamic++;
3293
3294		status = spi_controller_id_alloc(ctlr, first_dynamic, 0);
3295		if (status)
3296			return status;
3297	}
3298	ctlr->bus_lock_flag = 0;
3299	init_completion(&ctlr->xfer_completion);
3300	init_completion(&ctlr->cur_msg_completion);
3301	if (!ctlr->max_dma_len)
3302		ctlr->max_dma_len = INT_MAX;
3303
3304	/*
3305	 * Register the device, then userspace will see it.
3306	 * Registration fails if the bus ID is in use.
3307	 */
3308	dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
3309
3310	if (!spi_controller_is_target(ctlr) && ctlr->use_gpio_descriptors) {
3311		status = spi_get_gpio_descs(ctlr);
3312		if (status)
3313			goto free_bus_id;
3314		/*
3315		 * A controller using GPIO descriptors always
3316		 * supports SPI_CS_HIGH if need be.
3317		 */
3318		ctlr->mode_bits |= SPI_CS_HIGH;
3319	}
3320
3321	/*
3322	 * Even if it's just one always-selected device, there must
3323	 * be at least one chipselect.
3324	 */
3325	if (!ctlr->num_chipselect) {
3326		status = -EINVAL;
3327		goto free_bus_id;
3328	}
3329
3330	/* Setting last_cs to SPI_INVALID_CS means no chip selected */
3331	for (idx = 0; idx < SPI_DEVICE_CS_CNT_MAX; idx++)
3332		ctlr->last_cs[idx] = SPI_INVALID_CS;
3333
3334	status = device_add(&ctlr->dev);
3335	if (status < 0)
3336		goto free_bus_id;
3337	dev_dbg(dev, "registered %s %s\n",
3338			spi_controller_is_target(ctlr) ? "target" : "host",
3339			dev_name(&ctlr->dev));
3340
3341	/*
3342	 * If we're using a queued driver, start the queue. Note that we don't
3343	 * need the queueing logic if the driver is only supporting high-level
3344	 * memory operations.
3345	 */
3346	if (ctlr->transfer) {
3347		dev_info(dev, "controller is unqueued, this is deprecated\n");
3348	} else if (ctlr->transfer_one || ctlr->transfer_one_message) {
3349		status = spi_controller_initialize_queue(ctlr);
3350		if (status)
3351			goto del_ctrl;
3352	}
3353
3354	mutex_lock(&board_lock);
3355	list_add_tail(&ctlr->list, &spi_controller_list);
3356	list_for_each_entry(bi, &board_list, list)
3357		spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
3358	mutex_unlock(&board_lock);
3359
3360	/* Register devices from the device tree and ACPI */
3361	of_register_spi_devices(ctlr);
3362	acpi_register_spi_devices(ctlr);
3363	return status;
3364
3365del_ctrl:
3366	device_del(&ctlr->dev);
3367free_bus_id:
3368	mutex_lock(&board_lock);
3369	idr_remove(&spi_controller_idr, ctlr->bus_num);
3370	mutex_unlock(&board_lock);
3371	return status;
3372}
3373EXPORT_SYMBOL_GPL(spi_register_controller);
3374
3375static void devm_spi_unregister(struct device *dev, void *res)
3376{
3377	spi_unregister_controller(*(struct spi_controller **)res);
3378}
3379
3380/**
3381 * devm_spi_register_controller - register managed SPI host or target controller
3382 * @dev:    device managing SPI controller
3383 * @ctlr: initialized controller, originally from spi_alloc_host() or
3384 *	spi_alloc_target()
3385 * Context: can sleep
3386 *
3387 * Register a SPI device as with spi_register_controller() which will
3388 * automatically be unregistered and freed.
3389 *
3390 * Return: zero on success, else a negative error code.
3391 */
3392int devm_spi_register_controller(struct device *dev,
3393				 struct spi_controller *ctlr)
3394{
3395	struct spi_controller **ptr;
3396	int ret;
3397
3398	ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
3399	if (!ptr)
3400		return -ENOMEM;
3401
3402	ret = spi_register_controller(ctlr);
3403	if (!ret) {
3404		*ptr = ctlr;
3405		devres_add(dev, ptr);
3406	} else {
3407		devres_free(ptr);
3408	}
3409
3410	return ret;
3411}
3412EXPORT_SYMBOL_GPL(devm_spi_register_controller);
3413
3414static int __unregister(struct device *dev, void *null)
3415{
3416	spi_unregister_device(to_spi_device(dev));
3417	return 0;
3418}
3419
3420/**
3421 * spi_unregister_controller - unregister SPI host or target controller
3422 * @ctlr: the controller being unregistered
3423 * Context: can sleep
3424 *
3425 * This call is used only by SPI controller drivers, which are the
3426 * only ones directly touching chip registers.
3427 *
3428 * This must be called from context that can sleep.
3429 *
3430 * Note that this function also drops a reference to the controller.
3431 */
3432void spi_unregister_controller(struct spi_controller *ctlr)
3433{
3434	struct spi_controller *found;
3435	int id = ctlr->bus_num;
3436
3437	/* Prevent addition of new devices, unregister existing ones */
3438	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3439		mutex_lock(&ctlr->add_lock);
3440
3441	device_for_each_child(&ctlr->dev, NULL, __unregister);
3442
3443	/* First make sure that this controller was ever added */
3444	mutex_lock(&board_lock);
3445	found = idr_find(&spi_controller_idr, id);
3446	mutex_unlock(&board_lock);
3447	if (ctlr->queued) {
3448		if (spi_destroy_queue(ctlr))
3449			dev_err(&ctlr->dev, "queue remove failed\n");
3450	}
3451	mutex_lock(&board_lock);
3452	list_del(&ctlr->list);
3453	mutex_unlock(&board_lock);
3454
3455	device_del(&ctlr->dev);
3456
3457	/* Free bus id */
3458	mutex_lock(&board_lock);
3459	if (found == ctlr)
3460		idr_remove(&spi_controller_idr, id);
3461	mutex_unlock(&board_lock);
3462
3463	if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
3464		mutex_unlock(&ctlr->add_lock);
3465
3466	/*
3467	 * Release the last reference on the controller if its driver
3468	 * has not yet been converted to devm_spi_alloc_host/target().
3469	 */
3470	if (!ctlr->devm_allocated)
3471		put_device(&ctlr->dev);
3472}
3473EXPORT_SYMBOL_GPL(spi_unregister_controller);
3474
3475static inline int __spi_check_suspended(const struct spi_controller *ctlr)
3476{
3477	return ctlr->flags & SPI_CONTROLLER_SUSPENDED ? -ESHUTDOWN : 0;
3478}
3479
3480static inline void __spi_mark_suspended(struct spi_controller *ctlr)
3481{
3482	mutex_lock(&ctlr->bus_lock_mutex);
3483	ctlr->flags |= SPI_CONTROLLER_SUSPENDED;
3484	mutex_unlock(&ctlr->bus_lock_mutex);
3485}
3486
3487static inline void __spi_mark_resumed(struct spi_controller *ctlr)
3488{
3489	mutex_lock(&ctlr->bus_lock_mutex);
3490	ctlr->flags &= ~SPI_CONTROLLER_SUSPENDED;
3491	mutex_unlock(&ctlr->bus_lock_mutex);
3492}
3493
3494int spi_controller_suspend(struct spi_controller *ctlr)
3495{
3496	int ret = 0;
3497
3498	/* Basically no-ops for non-queued controllers */
3499	if (ctlr->queued) {
3500		ret = spi_stop_queue(ctlr);
3501		if (ret)
3502			dev_err(&ctlr->dev, "queue stop failed\n");
3503	}
3504
3505	__spi_mark_suspended(ctlr);
3506	return ret;
3507}
3508EXPORT_SYMBOL_GPL(spi_controller_suspend);
3509
3510int spi_controller_resume(struct spi_controller *ctlr)
3511{
3512	int ret = 0;
3513
3514	__spi_mark_resumed(ctlr);
3515
3516	if (ctlr->queued) {
3517		ret = spi_start_queue(ctlr);
3518		if (ret)
3519			dev_err(&ctlr->dev, "queue restart failed\n");
3520	}
3521	return ret;
3522}
3523EXPORT_SYMBOL_GPL(spi_controller_resume);
3524
3525/*-------------------------------------------------------------------------*/
3526
3527/* Core methods for spi_message alterations */
3528
3529static void __spi_replace_transfers_release(struct spi_controller *ctlr,
3530					    struct spi_message *msg,
3531					    void *res)
3532{
3533	struct spi_replaced_transfers *rxfer = res;
3534	size_t i;
3535
3536	/* Call extra callback if requested */
3537	if (rxfer->release)
3538		rxfer->release(ctlr, msg, res);
3539
3540	/* Insert replaced transfers back into the message */
3541	list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3542
3543	/* Remove the formerly inserted entries */
3544	for (i = 0; i < rxfer->inserted; i++)
3545		list_del(&rxfer->inserted_transfers[i].transfer_list);
3546}
3547
3548/**
3549 * spi_replace_transfers - replace transfers with several transfers
3550 *                         and register change with spi_message.resources
3551 * @msg:           the spi_message we work upon
3552 * @xfer_first:    the first spi_transfer we want to replace
3553 * @remove:        number of transfers to remove
3554 * @insert:        the number of transfers we want to insert instead
3555 * @release:       extra release code necessary in some circumstances
3556 * @extradatasize: extra data to allocate (with alignment guarantees
3557 *                 of struct @spi_transfer)
3558 * @gfp:           gfp flags
3559 *
3560 * Returns: pointer to @spi_replaced_transfers,
3561 *          PTR_ERR(...) in case of errors.
3562 */
3563static struct spi_replaced_transfers *spi_replace_transfers(
3564	struct spi_message *msg,
3565	struct spi_transfer *xfer_first,
3566	size_t remove,
3567	size_t insert,
3568	spi_replaced_release_t release,
3569	size_t extradatasize,
3570	gfp_t gfp)
3571{
3572	struct spi_replaced_transfers *rxfer;
3573	struct spi_transfer *xfer;
3574	size_t i;
3575
3576	/* Allocate the structure using spi_res */
3577	rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
3578			      struct_size(rxfer, inserted_transfers, insert)
3579			      + extradatasize,
3580			      gfp);
3581	if (!rxfer)
3582		return ERR_PTR(-ENOMEM);
3583
3584	/* The release code to invoke before running the generic release */
3585	rxfer->release = release;
3586
3587	/* Assign extradata */
3588	if (extradatasize)
3589		rxfer->extradata =
3590			&rxfer->inserted_transfers[insert];
3591
3592	/* Init the replaced_transfers list */
3593	INIT_LIST_HEAD(&rxfer->replaced_transfers);
3594
3595	/*
3596	 * Assign the list_entry after which we should reinsert
3597	 * the @replaced_transfers - it may be spi_message.messages!
3598	 */
3599	rxfer->replaced_after = xfer_first->transfer_list.prev;
3600
3601	/* Remove the requested number of transfers */
3602	for (i = 0; i < remove; i++) {
3603		/*
3604		 * If the entry after replaced_after it is msg->transfers
3605		 * then we have been requested to remove more transfers
3606		 * than are in the list.
3607		 */
3608		if (rxfer->replaced_after->next == &msg->transfers) {
3609			dev_err(&msg->spi->dev,
3610				"requested to remove more spi_transfers than are available\n");
3611			/* Insert replaced transfers back into the message */
3612			list_splice(&rxfer->replaced_transfers,
3613				    rxfer->replaced_after);
3614
3615			/* Free the spi_replace_transfer structure... */
3616			spi_res_free(rxfer);
3617
3618			/* ...and return with an error */
3619			return ERR_PTR(-EINVAL);
3620		}
3621
3622		/*
3623		 * Remove the entry after replaced_after from list of
3624		 * transfers and add it to list of replaced_transfers.
3625		 */
3626		list_move_tail(rxfer->replaced_after->next,
3627			       &rxfer->replaced_transfers);
3628	}
3629
3630	/*
3631	 * Create copy of the given xfer with identical settings
3632	 * based on the first transfer to get removed.
3633	 */
3634	for (i = 0; i < insert; i++) {
3635		/* We need to run in reverse order */
3636		xfer = &rxfer->inserted_transfers[insert - 1 - i];
3637
3638		/* Copy all spi_transfer data */
3639		memcpy(xfer, xfer_first, sizeof(*xfer));
3640
3641		/* Add to list */
3642		list_add(&xfer->transfer_list, rxfer->replaced_after);
3643
3644		/* Clear cs_change and delay for all but the last */
3645		if (i) {
3646			xfer->cs_change = false;
3647			xfer->delay.value = 0;
3648		}
3649	}
3650
3651	/* Set up inserted... */
3652	rxfer->inserted = insert;
3653
3654	/* ...and register it with spi_res/spi_message */
3655	spi_res_add(msg, rxfer);
3656
3657	return rxfer;
3658}
3659
3660static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
3661					struct spi_message *msg,
3662					struct spi_transfer **xferp,
3663					size_t maxsize)
3664{
3665	struct spi_transfer *xfer = *xferp, *xfers;
3666	struct spi_replaced_transfers *srt;
3667	size_t offset;
3668	size_t count, i;
3669
3670	/* Calculate how many we have to replace */
3671	count = DIV_ROUND_UP(xfer->len, maxsize);
3672
3673	/* Create replacement */
3674	srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, GFP_KERNEL);
3675	if (IS_ERR(srt))
3676		return PTR_ERR(srt);
3677	xfers = srt->inserted_transfers;
3678
3679	/*
3680	 * Now handle each of those newly inserted spi_transfers.
3681	 * Note that the replacements spi_transfers all are preset
3682	 * to the same values as *xferp, so tx_buf, rx_buf and len
3683	 * are all identical (as well as most others)
3684	 * so we just have to fix up len and the pointers.
3685	 */
3686
3687	/*
3688	 * The first transfer just needs the length modified, so we
3689	 * run it outside the loop.
3690	 */
3691	xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
3692
3693	/* All the others need rx_buf/tx_buf also set */
3694	for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3695		/* Update rx_buf, tx_buf and DMA */
3696		if (xfers[i].rx_buf)
3697			xfers[i].rx_buf += offset;
3698		if (xfers[i].tx_buf)
3699			xfers[i].tx_buf += offset;
3700
3701		/* Update length */
3702		xfers[i].len = min(maxsize, xfers[i].len - offset);
3703	}
3704
3705	/*
3706	 * We set up xferp to the last entry we have inserted,
3707	 * so that we skip those already split transfers.
3708	 */
3709	*xferp = &xfers[count - 1];
3710
3711	/* Increment statistics counters */
3712	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics,
3713				       transfers_split_maxsize);
3714	SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics,
3715				       transfers_split_maxsize);
3716
3717	return 0;
3718}
3719
3720/**
3721 * spi_split_transfers_maxsize - split spi transfers into multiple transfers
3722 *                               when an individual transfer exceeds a
3723 *                               certain size
3724 * @ctlr:    the @spi_controller for this transfer
3725 * @msg:   the @spi_message to transform
3726 * @maxsize:  the maximum when to apply this
3727 *
3728 * This function allocates resources that are automatically freed during the
3729 * spi message unoptimize phase so this function should only be called from
3730 * optimize_message callbacks.
3731 *
3732 * Return: status of transformation
3733 */
3734int spi_split_transfers_maxsize(struct spi_controller *ctlr,
3735				struct spi_message *msg,
3736				size_t maxsize)
3737{
3738	struct spi_transfer *xfer;
3739	int ret;
3740
3741	/*
3742	 * Iterate over the transfer_list,
3743	 * but note that xfer is advanced to the last transfer inserted
3744	 * to avoid checking sizes again unnecessarily (also xfer does
3745	 * potentially belong to a different list by the time the
3746	 * replacement has happened).
3747	 */
3748	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3749		if (xfer->len > maxsize) {
3750			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3751							   maxsize);
3752			if (ret)
3753				return ret;
3754		}
3755	}
3756
3757	return 0;
3758}
3759EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
3760
3761
3762/**
3763 * spi_split_transfers_maxwords - split SPI transfers into multiple transfers
3764 *                                when an individual transfer exceeds a
3765 *                                certain number of SPI words
3766 * @ctlr:     the @spi_controller for this transfer
3767 * @msg:      the @spi_message to transform
3768 * @maxwords: the number of words to limit each transfer to
3769 *
3770 * This function allocates resources that are automatically freed during the
3771 * spi message unoptimize phase so this function should only be called from
3772 * optimize_message callbacks.
3773 *
3774 * Return: status of transformation
3775 */
3776int spi_split_transfers_maxwords(struct spi_controller *ctlr,
3777				 struct spi_message *msg,
3778				 size_t maxwords)
3779{
3780	struct spi_transfer *xfer;
3781
3782	/*
3783	 * Iterate over the transfer_list,
3784	 * but note that xfer is advanced to the last transfer inserted
3785	 * to avoid checking sizes again unnecessarily (also xfer does
3786	 * potentially belong to a different list by the time the
3787	 * replacement has happened).
3788	 */
3789	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3790		size_t maxsize;
3791		int ret;
3792
3793		maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
3794		if (xfer->len > maxsize) {
3795			ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3796							   maxsize);
3797			if (ret)
3798				return ret;
3799		}
3800	}
3801
3802	return 0;
3803}
3804EXPORT_SYMBOL_GPL(spi_split_transfers_maxwords);
3805
3806/*-------------------------------------------------------------------------*/
3807
3808/*
3809 * Core methods for SPI controller protocol drivers. Some of the
3810 * other core methods are currently defined as inline functions.
3811 */
3812
3813static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3814					u8 bits_per_word)
3815{
3816	if (ctlr->bits_per_word_mask) {
3817		/* Only 32 bits fit in the mask */
3818		if (bits_per_word > 32)
3819			return -EINVAL;
3820		if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
3821			return -EINVAL;
3822	}
3823
3824	return 0;
3825}
3826
3827/**
3828 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3829 * @spi: the device that requires specific CS timing configuration
3830 *
3831 * Return: zero on success, else a negative error code.
3832 */
3833static int spi_set_cs_timing(struct spi_device *spi)
3834{
3835	struct device *parent = spi->controller->dev.parent;
3836	int status = 0;
3837
3838	if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) {
3839		if (spi->controller->auto_runtime_pm) {
3840			status = pm_runtime_get_sync(parent);
3841			if (status < 0) {
3842				pm_runtime_put_noidle(parent);
3843				dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3844					status);
3845				return status;
3846			}
3847
3848			status = spi->controller->set_cs_timing(spi);
3849			pm_runtime_put_autosuspend(parent);
3850		} else {
3851			status = spi->controller->set_cs_timing(spi);
3852		}
3853	}
3854	return status;
3855}
3856
3857/**
3858 * spi_setup - setup SPI mode and clock rate
3859 * @spi: the device whose settings are being modified
3860 * Context: can sleep, and no requests are queued to the device
3861 *
3862 * SPI protocol drivers may need to update the transfer mode if the
3863 * device doesn't work with its default.  They may likewise need
3864 * to update clock rates or word sizes from initial values.  This function
3865 * changes those settings, and must be called from a context that can sleep.
3866 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3867 * effect the next time the device is selected and data is transferred to
3868 * or from it.  When this function returns, the SPI device is deselected.
3869 *
3870 * Note that this call will fail if the protocol driver specifies an option
3871 * that the underlying controller or its driver does not support.  For
3872 * example, not all hardware supports wire transfers using nine bit words,
3873 * LSB-first wire encoding, or active-high chipselects.
3874 *
3875 * Return: zero on success, else a negative error code.
3876 */
3877int spi_setup(struct spi_device *spi)
3878{
3879	unsigned	bad_bits, ugly_bits;
3880	int		status;
3881
3882	/*
3883	 * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO
3884	 * are set at the same time.
3885	 */
3886	if ((hweight_long(spi->mode &
3887		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) ||
3888	    (hweight_long(spi->mode &
3889		(SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) {
3890		dev_err(&spi->dev,
3891		"setup: can not select any two of dual, quad and no-rx/tx at the same time\n");
3892		return -EINVAL;
3893	}
3894	/* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */
3895	if ((spi->mode & SPI_3WIRE) && (spi->mode &
3896		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3897		 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
3898		return -EINVAL;
3899	/* Check against conflicting MOSI idle configuration */
3900	if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) {
3901		dev_err(&spi->dev,
3902			"setup: MOSI configured to idle low and high at the same time.\n");
3903		return -EINVAL;
3904	}
3905	/*
3906	 * Help drivers fail *cleanly* when they need options
3907	 * that aren't supported with their current controller.
3908	 * SPI_CS_WORD has a fallback software implementation,
3909	 * so it is ignored here.
3910	 */
3911	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
3912				 SPI_NO_TX | SPI_NO_RX);
3913	ugly_bits = bad_bits &
3914		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3915		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
3916	if (ugly_bits) {
3917		dev_warn(&spi->dev,
3918			 "setup: ignoring unsupported mode bits %x\n",
3919			 ugly_bits);
3920		spi->mode &= ~ugly_bits;
3921		bad_bits &= ~ugly_bits;
3922	}
3923	if (bad_bits) {
3924		dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
3925			bad_bits);
3926		return -EINVAL;
3927	}
3928
3929	if (!spi->bits_per_word) {
3930		spi->bits_per_word = 8;
3931	} else {
3932		/*
3933		 * Some controllers may not support the default 8 bits-per-word
3934		 * so only perform the check when this is explicitly provided.
3935		 */
3936		status = __spi_validate_bits_per_word(spi->controller,
3937						      spi->bits_per_word);
3938		if (status)
3939			return status;
3940	}
3941
3942	if (spi->controller->max_speed_hz &&
3943	    (!spi->max_speed_hz ||
3944	     spi->max_speed_hz > spi->controller->max_speed_hz))
3945		spi->max_speed_hz = spi->controller->max_speed_hz;
3946
3947	mutex_lock(&spi->controller->io_mutex);
3948
3949	if (spi->controller->setup) {
3950		status = spi->controller->setup(spi);
3951		if (status) {
3952			mutex_unlock(&spi->controller->io_mutex);
3953			dev_err(&spi->controller->dev, "Failed to setup device: %d\n",
3954				status);
3955			return status;
3956		}
3957	}
3958
3959	status = spi_set_cs_timing(spi);
3960	if (status) {
3961		mutex_unlock(&spi->controller->io_mutex);
3962		return status;
3963	}
3964
3965	if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3966		status = pm_runtime_resume_and_get(spi->controller->dev.parent);
3967		if (status < 0) {
3968			mutex_unlock(&spi->controller->io_mutex);
3969			dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3970				status);
3971			return status;
3972		}
3973
3974		/*
3975		 * We do not want to return positive value from pm_runtime_get,
3976		 * there are many instances of devices calling spi_setup() and
3977		 * checking for a non-zero return value instead of a negative
3978		 * return value.
3979		 */
3980		status = 0;
3981
3982		spi_set_cs(spi, false, true);
3983		pm_runtime_put_autosuspend(spi->controller->dev.parent);
3984	} else {
3985		spi_set_cs(spi, false, true);
3986	}
3987
3988	mutex_unlock(&spi->controller->io_mutex);
3989
3990	if (spi->rt && !spi->controller->rt) {
3991		spi->controller->rt = true;
3992		spi_set_thread_rt(spi->controller);
3993	}
3994
3995	trace_spi_setup(spi, status);
3996
3997	dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
3998			spi->mode & SPI_MODE_X_MASK,
3999			(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
4000			(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
4001			(spi->mode & SPI_3WIRE) ? "3wire, " : "",
4002			(spi->mode & SPI_LOOP) ? "loopback, " : "",
4003			spi->bits_per_word, spi->max_speed_hz,
4004			status);
4005
4006	return status;
4007}
4008EXPORT_SYMBOL_GPL(spi_setup);
4009
4010static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
4011				       struct spi_device *spi)
4012{
4013	int delay1, delay2;
4014
4015	delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
4016	if (delay1 < 0)
4017		return delay1;
4018
4019	delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
4020	if (delay2 < 0)
4021		return delay2;
4022
4023	if (delay1 < delay2)
4024		memcpy(&xfer->word_delay, &spi->word_delay,
4025		       sizeof(xfer->word_delay));
4026
4027	return 0;
4028}
4029
4030static int __spi_validate(struct spi_device *spi, struct spi_message *message)
4031{
4032	struct spi_controller *ctlr = spi->controller;
4033	struct spi_transfer *xfer;
4034	int w_size;
4035
4036	if (list_empty(&message->transfers))
4037		return -EINVAL;
4038
4039	message->spi = spi;
4040
4041	/*
4042	 * Half-duplex links include original MicroWire, and ones with
4043	 * only one data pin like SPI_3WIRE (switches direction) or where
4044	 * either MOSI or MISO is missing.  They can also be caused by
4045	 * software limitations.
4046	 */
4047	if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
4048	    (spi->mode & SPI_3WIRE)) {
4049		unsigned flags = ctlr->flags;
4050
4051		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4052			if (xfer->rx_buf && xfer->tx_buf)
4053				return -EINVAL;
4054			if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
4055				return -EINVAL;
4056			if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
4057				return -EINVAL;
4058		}
4059	}
4060
4061	/*
4062	 * Set transfer bits_per_word and max speed as spi device default if
4063	 * it is not set for this transfer.
4064	 * Set transfer tx_nbits and rx_nbits as single transfer default
4065	 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
4066	 * Ensure transfer word_delay is at least as long as that required by
4067	 * device itself.
4068	 */
4069	message->frame_length = 0;
4070	list_for_each_entry(xfer, &message->transfers, transfer_list) {
4071		xfer->effective_speed_hz = 0;
4072		message->frame_length += xfer->len;
4073		if (!xfer->bits_per_word)
4074			xfer->bits_per_word = spi->bits_per_word;
4075
4076		if (!xfer->speed_hz)
4077			xfer->speed_hz = spi->max_speed_hz;
4078
4079		if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
4080			xfer->speed_hz = ctlr->max_speed_hz;
4081
4082		if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
4083			return -EINVAL;
4084
4085		/* DDR mode is supported only if controller has dtr_caps=true.
4086		 * default considered as SDR mode for SPI and QSPI controller.
4087		 * Note: This is applicable only to QSPI controller.
4088		 */
4089		if (xfer->dtr_mode && !ctlr->dtr_caps)
4090			return -EINVAL;
4091
4092		/*
4093		 * SPI transfer length should be multiple of SPI word size
4094		 * where SPI word size should be power-of-two multiple.
4095		 */
4096		if (xfer->bits_per_word <= 8)
4097			w_size = 1;
4098		else if (xfer->bits_per_word <= 16)
4099			w_size = 2;
4100		else
4101			w_size = 4;
4102
4103		/* No partial transfers accepted */
4104		if (xfer->len % w_size)
4105			return -EINVAL;
4106
4107		if (xfer->speed_hz && ctlr->min_speed_hz &&
4108		    xfer->speed_hz < ctlr->min_speed_hz)
4109			return -EINVAL;
4110
4111		if (xfer->tx_buf && !xfer->tx_nbits)
4112			xfer->tx_nbits = SPI_NBITS_SINGLE;
4113		if (xfer->rx_buf && !xfer->rx_nbits)
4114			xfer->rx_nbits = SPI_NBITS_SINGLE;
4115		/*
4116		 * Check transfer tx/rx_nbits:
4117		 * 1. check the value matches one of single, dual and quad
4118		 * 2. check tx/rx_nbits match the mode in spi_device
4119		 */
4120		if (xfer->tx_buf) {
4121			if (spi->mode & SPI_NO_TX)
4122				return -EINVAL;
4123			if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
4124				xfer->tx_nbits != SPI_NBITS_DUAL &&
4125				xfer->tx_nbits != SPI_NBITS_QUAD &&
4126				xfer->tx_nbits != SPI_NBITS_OCTAL)
4127				return -EINVAL;
4128			if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
4129				!(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL)))
4130				return -EINVAL;
4131			if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
4132				!(spi->mode & (SPI_TX_QUAD | SPI_TX_OCTAL)))
4133				return -EINVAL;
4134			if ((xfer->tx_nbits == SPI_NBITS_OCTAL) &&
4135				!(spi->mode & SPI_TX_OCTAL))
4136				return -EINVAL;
4137		}
4138		/* Check transfer rx_nbits */
4139		if (xfer->rx_buf) {
4140			if (spi->mode & SPI_NO_RX)
4141				return -EINVAL;
4142			if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
4143				xfer->rx_nbits != SPI_NBITS_DUAL &&
4144				xfer->rx_nbits != SPI_NBITS_QUAD &&
4145				xfer->rx_nbits != SPI_NBITS_OCTAL)
4146				return -EINVAL;
4147			if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
4148				!(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
4149				return -EINVAL;
4150			if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
4151				!(spi->mode & (SPI_RX_QUAD | SPI_RX_OCTAL)))
4152				return -EINVAL;
4153			if ((xfer->rx_nbits == SPI_NBITS_OCTAL) &&
4154				!(spi->mode & SPI_RX_OCTAL))
4155				return -EINVAL;
4156		}
4157
4158		if (_spi_xfer_word_delay_update(xfer, spi))
4159			return -EINVAL;
4160
4161		/* Make sure controller supports required offload features. */
4162		if (xfer->offload_flags) {
4163			if (!message->offload)
4164				return -EINVAL;
4165
4166			if (xfer->offload_flags & ~message->offload->xfer_flags)
4167				return -EINVAL;
4168		}
4169	}
4170
4171	message->status = -EINPROGRESS;
4172
4173	return 0;
4174}
4175
4176/*
4177 * spi_split_transfers - generic handling of transfer splitting
4178 * @msg: the message to split
4179 *
4180 * Under certain conditions, a SPI controller may not support arbitrary
4181 * transfer sizes or other features required by a peripheral. This function
4182 * will split the transfers in the message into smaller transfers that are
4183 * supported by the controller.
4184 *
4185 * Controllers with special requirements not covered here can also split
4186 * transfers in the optimize_message() callback.
4187 *
4188 * Context: can sleep
4189 * Return: zero on success, else a negative error code
4190 */
4191static int spi_split_transfers(struct spi_message *msg)
4192{
4193	struct spi_controller *ctlr = msg->spi->controller;
4194	struct spi_transfer *xfer;
4195	int ret;
4196
4197	/*
4198	 * If an SPI controller does not support toggling the CS line on each
4199	 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
4200	 * for the CS line, we can emulate the CS-per-word hardware function by
4201	 * splitting transfers into one-word transfers and ensuring that
4202	 * cs_change is set for each transfer.
4203	 */
4204	if ((msg->spi->mode & SPI_CS_WORD) &&
4205	    (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) {
4206		ret = spi_split_transfers_maxwords(ctlr, msg, 1);
4207		if (ret)
4208			return ret;
4209
4210		list_for_each_entry(xfer, &msg->transfers, transfer_list) {
4211			/* Don't change cs_change on the last entry in the list */
4212			if (list_is_last(&xfer->transfer_list, &msg->transfers))
4213				break;
4214
4215			xfer->cs_change = 1;
4216		}
4217	} else {
4218		ret = spi_split_transfers_maxsize(ctlr, msg,
4219						  spi_max_transfer_size(msg->spi));
4220		if (ret)
4221			return ret;
4222	}
4223
4224	return 0;
4225}
4226
4227/*
4228 * __spi_optimize_message - shared implementation for spi_optimize_message()
4229 *                          and spi_maybe_optimize_message()
4230 * @spi: the device that will be used for the message
4231 * @msg: the message to optimize
4232 *
4233 * Peripheral drivers will call spi_optimize_message() and the spi core will
4234 * call spi_maybe_optimize_message() instead of calling this directly.
4235 *
4236 * It is not valid to call this on a message that has already been optimized.
4237 *
4238 * Return: zero on success, else a negative error code
4239 */
4240static int __spi_optimize_message(struct spi_device *spi,
4241				  struct spi_message *msg)
4242{
4243	struct spi_controller *ctlr = spi->controller;
4244	int ret;
4245
4246	ret = __spi_validate(spi, msg);
4247	if (ret)
4248		return ret;
4249
4250	ret = spi_split_transfers(msg);
4251	if (ret)
4252		return ret;
4253
4254	if (ctlr->optimize_message) {
4255		ret = ctlr->optimize_message(msg);
4256		if (ret) {
4257			spi_res_release(ctlr, msg);
4258			return ret;
4259		}
4260	}
4261
4262	msg->optimized = true;
4263
4264	return 0;
4265}
4266
4267/*
4268 * spi_maybe_optimize_message - optimize message if it isn't already pre-optimized
4269 * @spi: the device that will be used for the message
4270 * @msg: the message to optimize
4271 * Return: zero on success, else a negative error code
4272 */
4273static int spi_maybe_optimize_message(struct spi_device *spi,
4274				      struct spi_message *msg)
4275{
4276	if (spi->controller->defer_optimize_message) {
4277		msg->spi = spi;
4278		return 0;
4279	}
4280
4281	if (msg->pre_optimized)
4282		return 0;
4283
4284	return __spi_optimize_message(spi, msg);
4285}
4286
4287/**
4288 * spi_optimize_message - do any one-time validation and setup for a SPI message
4289 * @spi: the device that will be used for the message
4290 * @msg: the message to optimize
4291 *
4292 * Peripheral drivers that reuse the same message repeatedly may call this to
4293 * perform as much message prep as possible once, rather than repeating it each
4294 * time a message transfer is performed to improve throughput and reduce CPU
4295 * usage.
4296 *
4297 * Once a message has been optimized, it cannot be modified with the exception
4298 * of updating the contents of any xfer->tx_buf (the pointer can't be changed,
4299 * only the data in the memory it points to).
4300 *
4301 * Calls to this function must be balanced with calls to spi_unoptimize_message()
4302 * to avoid leaking resources.
4303 *
4304 * Context: can sleep
4305 * Return: zero on success, else a negative error code
4306 */
4307int spi_optimize_message(struct spi_device *spi, struct spi_message *msg)
4308{
4309	int ret;
4310
4311	/*
4312	 * Pre-optimization is not supported and optimization is deferred e.g.
4313	 * when using spi-mux.
4314	 */
4315	if (spi->controller->defer_optimize_message)
4316		return 0;
4317
4318	ret = __spi_optimize_message(spi, msg);
4319	if (ret)
4320		return ret;
4321
4322	/*
4323	 * This flag indicates that the peripheral driver called spi_optimize_message()
4324	 * and therefore we shouldn't unoptimize message automatically when finalizing
4325	 * the message but rather wait until spi_unoptimize_message() is called
4326	 * by the peripheral driver.
4327	 */
4328	msg->pre_optimized = true;
4329
4330	return 0;
4331}
4332EXPORT_SYMBOL_GPL(spi_optimize_message);
4333
4334/**
4335 * spi_unoptimize_message - releases any resources allocated by spi_optimize_message()
4336 * @msg: the message to unoptimize
4337 *
4338 * Calls to this function must be balanced with calls to spi_optimize_message().
4339 *
4340 * Context: can sleep
4341 */
4342void spi_unoptimize_message(struct spi_message *msg)
4343{
4344	if (msg->spi->controller->defer_optimize_message)
4345		return;
4346
4347	__spi_unoptimize_message(msg);
4348	msg->pre_optimized = false;
4349}
4350EXPORT_SYMBOL_GPL(spi_unoptimize_message);
4351
4352static int __spi_async(struct spi_device *spi, struct spi_message *message)
4353{
4354	struct spi_controller *ctlr = spi->controller;
4355	struct spi_transfer *xfer;
4356
4357	/*
4358	 * Some controllers do not support doing regular SPI transfers. Return
4359	 * ENOTSUPP when this is the case.
4360	 */
4361	if (!ctlr->transfer)
4362		return -ENOTSUPP;
4363
4364	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async);
4365	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async);
4366
4367	trace_spi_message_submit(message);
4368
4369	if (!ctlr->ptp_sts_supported) {
4370		list_for_each_entry(xfer, &message->transfers, transfer_list) {
4371			xfer->ptp_sts_word_pre = 0;
4372			ptp_read_system_prets(xfer->ptp_sts);
4373		}
4374	}
4375
4376	return ctlr->transfer(spi, message);
4377}
4378
4379static void devm_spi_unoptimize_message(void *msg)
4380{
4381	spi_unoptimize_message(msg);
4382}
4383
4384/**
4385 * devm_spi_optimize_message - managed version of spi_optimize_message()
4386 * @dev: the device that manages @msg (usually @spi->dev)
4387 * @spi: the device that will be used for the message
4388 * @msg: the message to optimize
4389 * Return: zero on success, else a negative error code
4390 *
4391 * spi_unoptimize_message() will automatically be called when the device is
4392 * removed.
4393 */
4394int devm_spi_optimize_message(struct device *dev, struct spi_device *spi,
4395			      struct spi_message *msg)
4396{
4397	int ret;
4398
4399	ret = spi_optimize_message(spi, msg);
4400	if (ret)
4401		return ret;
4402
4403	return devm_add_action_or_reset(dev, devm_spi_unoptimize_message, msg);
4404}
4405EXPORT_SYMBOL_GPL(devm_spi_optimize_message);
4406
4407/**
4408 * spi_async - asynchronous SPI transfer
4409 * @spi: device with which data will be exchanged
4410 * @message: describes the data transfers, including completion callback
4411 * Context: any (IRQs may be blocked, etc)
4412 *
4413 * This call may be used in_irq and other contexts which can't sleep,
4414 * as well as from task contexts which can sleep.
4415 *
4416 * The completion callback is invoked in a context which can't sleep.
4417 * Before that invocation, the value of message->status is undefined.
4418 * When the callback is issued, message->status holds either zero (to
4419 * indicate complete success) or a negative error code.  After that
4420 * callback returns, the driver which issued the transfer request may
4421 * deallocate the associated memory; it's no longer in use by any SPI
4422 * core or controller driver code.
4423 *
4424 * Note that although all messages to a spi_device are handled in
4425 * FIFO order, messages may go to different devices in other orders.
4426 * Some device might be higher priority, or have various "hard" access
4427 * time requirements, for example.
4428 *
4429 * On detection of any fault during the transfer, processing of
4430 * the entire message is aborted, and the device is deselected.
4431 * Until returning from the associated message completion callback,
4432 * no other spi_message queued to that device will be processed.
4433 * (This rule applies equally to all the synchronous transfer calls,
4434 * which are wrappers around this core asynchronous primitive.)
4435 *
4436 * Return: zero on success, else a negative error code.
4437 */
4438int spi_async(struct spi_device *spi, struct spi_message *message)
4439{
4440	struct spi_controller *ctlr = spi->controller;
4441	int ret;
4442	unsigned long flags;
4443
4444	ret = spi_maybe_optimize_message(spi, message);
4445	if (ret)
4446		return ret;
4447
4448	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4449
4450	if (ctlr->bus_lock_flag)
4451		ret = -EBUSY;
4452	else
4453		ret = __spi_async(spi, message);
4454
4455	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4456
4457	return ret;
4458}
4459EXPORT_SYMBOL_GPL(spi_async);
4460
4461static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg)
4462{
4463	bool was_busy;
4464	int ret;
4465
4466	mutex_lock(&ctlr->io_mutex);
4467
4468	was_busy = ctlr->busy;
4469
4470	ctlr->cur_msg = msg;
4471	ret = __spi_pump_transfer_message(ctlr, msg, was_busy);
4472	if (ret)
4473		dev_err(&ctlr->dev, "noqueue transfer failed\n");
4474	ctlr->cur_msg = NULL;
4475	ctlr->fallback = false;
4476
4477	if (!was_busy) {
4478		kfree(ctlr->dummy_rx);
4479		ctlr->dummy_rx = NULL;
4480		kfree(ctlr->dummy_tx);
4481		ctlr->dummy_tx = NULL;
4482		if (ctlr->unprepare_transfer_hardware &&
4483		    ctlr->unprepare_transfer_hardware(ctlr))
4484			dev_err(&ctlr->dev,
4485				"failed to unprepare transfer hardware\n");
4486		spi_idle_runtime_pm(ctlr);
4487	}
4488
4489	mutex_unlock(&ctlr->io_mutex);
4490}
4491
4492/*-------------------------------------------------------------------------*/
4493
4494/*
4495 * Utility methods for SPI protocol drivers, layered on
4496 * top of the core.  Some other utility methods are defined as
4497 * inline functions.
4498 */
4499
4500static void spi_complete(void *arg)
4501{
4502	complete(arg);
4503}
4504
4505static int __spi_sync(struct spi_device *spi, struct spi_message *message)
4506{
4507	DECLARE_COMPLETION_ONSTACK(done);
4508	unsigned long flags;
4509	int status;
4510	struct spi_controller *ctlr = spi->controller;
4511
4512	if (__spi_check_suspended(ctlr)) {
4513		dev_warn_once(&spi->dev, "Attempted to sync while suspend\n");
4514		return -ESHUTDOWN;
4515	}
4516
4517	status = spi_maybe_optimize_message(spi, message);
4518	if (status)
4519		return status;
4520
4521	SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync);
4522	SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync);
4523
4524	/*
4525	 * Checking queue_empty here only guarantees async/sync message
4526	 * ordering when coming from the same context. It does not need to
4527	 * guard against reentrancy from a different context. The io_mutex
4528	 * will catch those cases.
4529	 */
4530	if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) {
4531		message->actual_length = 0;
4532		message->status = -EINPROGRESS;
4533
4534		trace_spi_message_submit(message);
4535
4536		SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate);
4537		SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate);
4538
4539		__spi_transfer_message_noqueue(ctlr, message);
4540
4541		return message->status;
4542	}
4543
4544	/*
4545	 * There are messages in the async queue that could have originated
4546	 * from the same context, so we need to preserve ordering.
4547	 * Therefor we send the message to the async queue and wait until they
4548	 * are completed.
4549	 */
4550	message->complete = spi_complete;
4551	message->context = &done;
4552
4553	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4554	status = __spi_async(spi, message);
4555	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4556
4557	if (status == 0) {
4558		wait_for_completion(&done);
4559		status = message->status;
4560	}
4561	message->complete = NULL;
4562	message->context = NULL;
4563
4564	return status;
4565}
4566
4567/**
4568 * spi_sync - blocking/synchronous SPI data transfers
4569 * @spi: device with which data will be exchanged
4570 * @message: describes the data transfers
4571 * Context: can sleep
4572 *
4573 * This call may only be used from a context that may sleep.  The sleep
4574 * is non-interruptible, and has no timeout.  Low-overhead controller
4575 * drivers may DMA directly into and out of the message buffers.
4576 *
4577 * Note that the SPI device's chip select is active during the message,
4578 * and then is normally disabled between messages.  Drivers for some
4579 * frequently-used devices may want to minimize costs of selecting a chip,
4580 * by leaving it selected in anticipation that the next message will go
4581 * to the same chip.  (That may increase power usage.)
4582 *
4583 * Also, the caller is guaranteeing that the memory associated with the
4584 * message will not be freed before this call returns.
4585 *
4586 * Return: zero on success, else a negative error code.
4587 */
4588int spi_sync(struct spi_device *spi, struct spi_message *message)
4589{
4590	int ret;
4591
4592	mutex_lock(&spi->controller->bus_lock_mutex);
4593	ret = __spi_sync(spi, message);
4594	mutex_unlock(&spi->controller->bus_lock_mutex);
4595
4596	return ret;
4597}
4598EXPORT_SYMBOL_GPL(spi_sync);
4599
4600/**
4601 * spi_sync_locked - version of spi_sync with exclusive bus usage
4602 * @spi: device with which data will be exchanged
4603 * @message: describes the data transfers
4604 * Context: can sleep
4605 *
4606 * This call may only be used from a context that may sleep.  The sleep
4607 * is non-interruptible, and has no timeout.  Low-overhead controller
4608 * drivers may DMA directly into and out of the message buffers.
4609 *
4610 * This call should be used by drivers that require exclusive access to the
4611 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
4612 * be released by a spi_bus_unlock call when the exclusive access is over.
4613 *
4614 * Return: zero on success, else a negative error code.
4615 */
4616int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
4617{
4618	return __spi_sync(spi, message);
4619}
4620EXPORT_SYMBOL_GPL(spi_sync_locked);
4621
4622/**
4623 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
4624 * @ctlr: SPI bus controller that should be locked for exclusive bus access
4625 * Context: can sleep
4626 *
4627 * This call may only be used from a context that may sleep.  The sleep
4628 * is non-interruptible, and has no timeout.
4629 *
4630 * This call should be used by drivers that require exclusive access to the
4631 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
4632 * exclusive access is over. Data transfer must be done by spi_sync_locked
4633 * and spi_async_locked calls when the SPI bus lock is held.
4634 *
4635 * Return: always zero.
4636 */
4637int spi_bus_lock(struct spi_controller *ctlr)
4638{
4639	unsigned long flags;
4640
4641	mutex_lock(&ctlr->bus_lock_mutex);
4642
4643	spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
4644	ctlr->bus_lock_flag = 1;
4645	spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
4646
4647	/* Mutex remains locked until spi_bus_unlock() is called */
4648
4649	return 0;
4650}
4651EXPORT_SYMBOL_GPL(spi_bus_lock);
4652
4653/**
4654 * spi_bus_unlock - release the lock for exclusive SPI bus usage
4655 * @ctlr: SPI bus controller that was locked for exclusive bus access
4656 * Context: can sleep
4657 *
4658 * This call may only be used from a context that may sleep.  The sleep
4659 * is non-interruptible, and has no timeout.
4660 *
4661 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
4662 * call.
4663 *
4664 * Return: always zero.
4665 */
4666int spi_bus_unlock(struct spi_controller *ctlr)
4667{
4668	ctlr->bus_lock_flag = 0;
4669
4670	mutex_unlock(&ctlr->bus_lock_mutex);
4671
4672	return 0;
4673}
4674EXPORT_SYMBOL_GPL(spi_bus_unlock);
4675
4676/* Portable code must never pass more than 32 bytes */
4677#define	SPI_BUFSIZ	max(32, SMP_CACHE_BYTES)
4678
4679static u8	*buf;
4680
4681/**
4682 * spi_write_then_read - SPI synchronous write followed by read
4683 * @spi: device with which data will be exchanged
4684 * @txbuf: data to be written (need not be DMA-safe)
4685 * @n_tx: size of txbuf, in bytes
4686 * @rxbuf: buffer into which data will be read (need not be DMA-safe)
4687 * @n_rx: size of rxbuf, in bytes
4688 * Context: can sleep
4689 *
4690 * This performs a half duplex MicroWire style transaction with the
4691 * device, sending txbuf and then reading rxbuf.  The return value
4692 * is zero for success, else a negative errno status code.
4693 * This call may only be used from a context that may sleep.
4694 *
4695 * Parameters to this routine are always copied using a small buffer.
4696 * Performance-sensitive or bulk transfer code should instead use
4697 * spi_{async,sync}() calls with DMA-safe buffers.
4698 *
4699 * Return: zero on success, else a negative error code.
4700 */
4701int spi_write_then_read(struct spi_device *spi,
4702		const void *txbuf, unsigned n_tx,
4703		void *rxbuf, unsigned n_rx)
4704{
4705	static DEFINE_MUTEX(lock);
4706
4707	int			status;
4708	struct spi_message	message;
4709	struct spi_transfer	x[2];
4710	u8			*local_buf;
4711
4712	/*
4713	 * Use preallocated DMA-safe buffer if we can. We can't avoid
4714	 * copying here, (as a pure convenience thing), but we can
4715	 * keep heap costs out of the hot path unless someone else is
4716	 * using the pre-allocated buffer or the transfer is too large.
4717	 */
4718	if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
4719		local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
4720				    GFP_KERNEL | GFP_DMA);
4721		if (!local_buf)
4722			return -ENOMEM;
4723	} else {
4724		local_buf = buf;
4725	}
4726
4727	spi_message_init(&message);
4728	memset(x, 0, sizeof(x));
4729	if (n_tx) {
4730		x[0].len = n_tx;
4731		spi_message_add_tail(&x[0], &message);
4732	}
4733	if (n_rx) {
4734		x[1].len = n_rx;
4735		spi_message_add_tail(&x[1], &message);
4736	}
4737
4738	memcpy(local_buf, txbuf, n_tx);
4739	x[0].tx_buf = local_buf;
4740	x[1].rx_buf = local_buf + n_tx;
4741
4742	/* Do the I/O */
4743	status = spi_sync(spi, &message);
4744	if (status == 0)
4745		memcpy(rxbuf, x[1].rx_buf, n_rx);
4746
4747	if (x[0].tx_buf == buf)
4748		mutex_unlock(&lock);
4749	else
4750		kfree(local_buf);
4751
4752	return status;
4753}
4754EXPORT_SYMBOL_GPL(spi_write_then_read);
4755
4756/*-------------------------------------------------------------------------*/
4757
4758#if IS_ENABLED(CONFIG_OF_DYNAMIC)
4759/* Must call put_device() when done with returned spi_device device */
4760static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
4761{
4762	struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4763
4764	return dev ? to_spi_device(dev) : NULL;
4765}
4766
4767/* The spi controllers are not using spi_bus, so we find it with another way */
4768static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
4769{
4770	struct device *dev;
4771
4772	dev = class_find_device_by_of_node(&spi_controller_class, node);
4773	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4774		dev = class_find_device_by_of_node(&spi_target_class, node);
4775	if (!dev)
4776		return NULL;
4777
4778	/* Reference got in class_find_device */
4779	return container_of(dev, struct spi_controller, dev);
4780}
4781
4782static int of_spi_notify(struct notifier_block *nb, unsigned long action,
4783			 void *arg)
4784{
4785	struct of_reconfig_data *rd = arg;
4786	struct spi_controller *ctlr;
4787	struct spi_device *spi;
4788
4789	switch (of_reconfig_get_state_change(action, arg)) {
4790	case OF_RECONFIG_CHANGE_ADD:
4791		ctlr = of_find_spi_controller_by_node(rd->dn->parent);
4792		if (ctlr == NULL)
4793			return NOTIFY_OK;	/* Not for us */
4794
4795		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
4796			put_device(&ctlr->dev);
4797			return NOTIFY_OK;
4798		}
4799
4800		/*
4801		 * Clear the flag before adding the device so that fw_devlink
4802		 * doesn't skip adding consumers to this device.
4803		 */
4804		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
4805		spi = of_register_spi_device(ctlr, rd->dn);
4806		put_device(&ctlr->dev);
4807
4808		if (IS_ERR(spi)) {
4809			pr_err("%s: failed to create for '%pOF'\n",
4810					__func__, rd->dn);
4811			of_node_clear_flag(rd->dn, OF_POPULATED);
4812			return notifier_from_errno(PTR_ERR(spi));
4813		}
4814		break;
4815
4816	case OF_RECONFIG_CHANGE_REMOVE:
4817		/* Already depopulated? */
4818		if (!of_node_check_flag(rd->dn, OF_POPULATED))
4819			return NOTIFY_OK;
4820
4821		/* Find our device by node */
4822		spi = of_find_spi_device_by_node(rd->dn);
4823		if (spi == NULL)
4824			return NOTIFY_OK;	/* No? not meant for us */
4825
4826		/* Unregister takes one ref away */
4827		spi_unregister_device(spi);
4828
4829		/* And put the reference of the find */
4830		put_device(&spi->dev);
4831		break;
4832	}
4833
4834	return NOTIFY_OK;
4835}
4836
4837static struct notifier_block spi_of_notifier = {
4838	.notifier_call = of_spi_notify,
4839};
4840#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4841extern struct notifier_block spi_of_notifier;
4842#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4843
4844#if IS_ENABLED(CONFIG_ACPI)
4845static int spi_acpi_controller_match(struct device *dev, const void *data)
4846{
4847	return device_match_acpi_dev(dev->parent, data);
4848}
4849
4850struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
4851{
4852	struct device *dev;
4853
4854	dev = class_find_device(&spi_controller_class, NULL, adev,
4855				spi_acpi_controller_match);
4856	if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4857		dev = class_find_device(&spi_target_class, NULL, adev,
4858					spi_acpi_controller_match);
4859	if (!dev)
4860		return NULL;
4861
4862	return container_of(dev, struct spi_controller, dev);
4863}
4864EXPORT_SYMBOL_GPL(acpi_spi_find_controller_by_adev);
4865
4866static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
4867{
4868	struct device *dev;
4869
4870	dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
4871	return to_spi_device(dev);
4872}
4873
4874static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
4875			   void *arg)
4876{
4877	struct acpi_device *adev = arg;
4878	struct spi_controller *ctlr;
4879	struct spi_device *spi;
4880
4881	switch (value) {
4882	case ACPI_RECONFIG_DEVICE_ADD:
4883		ctlr = acpi_spi_find_controller_by_adev(acpi_dev_parent(adev));
4884		if (!ctlr)
4885			break;
4886
4887		acpi_register_spi_device(ctlr, adev);
4888		put_device(&ctlr->dev);
4889		break;
4890	case ACPI_RECONFIG_DEVICE_REMOVE:
4891		if (!acpi_device_enumerated(adev))
4892			break;
4893
4894		spi = acpi_spi_find_device_by_adev(adev);
4895		if (!spi)
4896			break;
4897
4898		spi_unregister_device(spi);
4899		put_device(&spi->dev);
4900		break;
4901	}
4902
4903	return NOTIFY_OK;
4904}
4905
4906static struct notifier_block spi_acpi_notifier = {
4907	.notifier_call = acpi_spi_notify,
4908};
4909#else
4910extern struct notifier_block spi_acpi_notifier;
4911#endif
4912
4913static int __init spi_init(void)
4914{
4915	int	status;
4916
4917	buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
4918	if (!buf) {
4919		status = -ENOMEM;
4920		goto err0;
4921	}
4922
4923	status = bus_register(&spi_bus_type);
4924	if (status < 0)
4925		goto err1;
4926
4927	status = class_register(&spi_controller_class);
4928	if (status < 0)
4929		goto err2;
4930
4931	if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
4932		status = class_register(&spi_target_class);
4933		if (status < 0)
4934			goto err3;
4935	}
4936
4937	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
4938		WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
4939	if (IS_ENABLED(CONFIG_ACPI))
4940		WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
4941
4942	return 0;
4943
4944err3:
4945	class_unregister(&spi_controller_class);
4946err2:
4947	bus_unregister(&spi_bus_type);
4948err1:
4949	kfree(buf);
4950	buf = NULL;
4951err0:
4952	return status;
4953}
4954
4955/*
4956 * A board_info is normally registered in arch_initcall(),
4957 * but even essential drivers wait till later.
4958 *
4959 * REVISIT only boardinfo really needs static linking. The rest (device and
4960 * driver registration) _could_ be dynamically linked (modular) ... Costs
4961 * include needing to have boardinfo data structures be much more public.
4962 */
4963postcore_initcall(spi_init);
~/ Read-only file preview.