Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/drivers/spi/spi.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * Copyright (C) 2008 Sascha Hauer, Pengutronix
4
 *
5
 * Derived from Linux SPI Framework
6
 *
7
 * Copyright (C) 2005 David Brownell
8
 */
9
10
#include <common.h>
11
#include <linux/device.h>
12
#include <linux/gpio/consumer.h>
13
#include <linux/spi/spi-mem.h>
14
#include <spi/spi.h>
15
#include <xfuncs.h>
16
#include <malloc.h>
17
#include <slice.h>
18
#include <errno.h>
19
#include <init.h>
20
#include <of.h>
21
22
/* SPI devices should normally not be created by SPI device drivers; that
23
 * would make them board-specific.  Similarly with SPI master drivers.
24
 * Device registration normally goes into like arch/.../mach.../board-YYY.c
25
 * with other readonly (flashable) information about mainboard devices.
26
 */
27
28
struct boardinfo {
29
  struct list_head  list;
30
  unsigned    n_board_info;
31
  struct spi_board_info board_info[0];
32
};
33
34
static LIST_HEAD(board_list);
35
static LIST_HEAD(spi_controller_list);
36
37
/**
38
 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
39
 * @spi: the device that requires specific CS timing configuration
40
 *
41
 * Return: zero on success, else a negative error code.
42
 */
43
static int spi_set_cs_timing(struct spi_device *spi)
44
0
{
45
0
  int status = 0;
46
47
0
  if (spi->controller->set_cs_timing && !spi->cs_gpiod)
48
0
    status = spi->controller->set_cs_timing(spi);
49
50
0
  return status;
51
0
}
52
53
/**
54
 * spi_new_device - instantiate one new SPI device
55
 * @master: Controller to which device is connected
56
 * @chip: Describes the SPI device
57
 * Context: can sleep
58
 *
59
 * On typical mainboards, this is purely internal; and it's not needed
60
 * after board init creates the hard-wired devices.  Some development
61
 * platforms may not be able to use spi_register_board_info though, and
62
 * this is exported so that for example a USB or parport based adapter
63
 * driver could add devices (which it would learn about out-of-band).
64
 *
65
 * Returns the new device, or NULL.
66
 */
67
struct spi_device *spi_new_device(struct spi_controller *ctrl,
68
          struct spi_board_info *chip)
69
0
{
70
0
  struct spi_device *proxy;
71
0
  struct spi_mem    *mem;
72
0
  int     status;
73
74
  /* Chipselects are numbered 0..max; validate. */
75
0
  if (chip->chip_select >= ctrl->num_chipselect) {
76
0
    debug("cs%d > max %d\n",
77
0
      chip->chip_select,
78
0
      ctrl->num_chipselect);
79
0
    return NULL;
80
0
  }
81
82
0
  proxy = xzalloc(sizeof *proxy);
83
0
  proxy->master = ctrl;
84
0
  proxy->chip_select = chip->chip_select;
85
0
  if (ctrl->cs_gpiods)
86
0
    proxy->cs_gpiod = ctrl->cs_gpiods[chip->chip_select];
87
0
  proxy->max_speed_hz = chip->max_speed_hz;
88
0
  proxy->mode = chip->mode;
89
0
  proxy->cs_setup = chip->cs_setup;
90
0
  proxy->cs_hold = chip->cs_hold;
91
0
  proxy->cs_inactive = chip->cs_inactive;
92
0
  proxy->bits_per_word = chip->bits_per_word ? chip->bits_per_word : 8;
93
0
  proxy->dev.platform_data = chip->platform_data;
94
0
  proxy->dev.bus = &spi_bus;
95
0
  dev_set_name(&proxy->dev, "%s", chip->name);
96
  /* allocate a free id for this chip */
97
0
  proxy->dev.id = DEVICE_ID_DYNAMIC;
98
0
  proxy->dev.type_data = proxy;
99
0
  proxy->dev.of_node = chip->device_node;
100
0
  proxy->dev.parent = ctrl->dev;
101
0
  proxy->master = proxy->controller = ctrl;
102
103
0
  mem = xzalloc(sizeof *mem);
104
0
  mem->spi = proxy;
105
106
0
  if (ctrl->mem_ops && ctrl->mem_ops->get_name)
107
0
    mem->name = ctrl->mem_ops->get_name(mem);
108
0
  else
109
0
    mem->name = dev_name(&proxy->dev);
110
0
  proxy->mem = mem;
111
112
  /* drivers may modify this initial i/o setup */
113
0
  status = ctrl->setup(proxy);
114
0
  if (status < 0) {
115
0
    printf("can't setup %s, status %d\n",
116
0
        proxy->dev.name, status);
117
0
    goto fail;
118
0
  }
119
120
0
  status = spi_set_cs_timing(proxy);
121
0
  if (status)
122
0
    goto fail;
123
124
0
  status = register_device(&proxy->dev);
125
0
  if (status)
126
0
    goto fail;
127
128
0
  chip->device_node->dev = &proxy->dev;
129
130
0
  return proxy;
131
0
fail:
132
0
  free(proxy);
133
0
  return NULL;
134
0
}
135
EXPORT_SYMBOL(spi_new_device);
136
137
static void of_spi_parse_dt_cs_delay(struct device_node *nc,
138
             struct spi_delay *delay, const char *prop)
139
0
{
140
0
  u32 value;
141
142
0
  if (!of_property_read_u32(nc, prop, &value)) {
143
0
    if (value > U16_MAX) {
144
0
      delay->value = DIV_ROUND_UP(value, 1000);
145
0
      delay->unit = SPI_DELAY_UNIT_USECS;
146
0
    } else {
147
0
      delay->value = value;
148
0
      delay->unit = SPI_DELAY_UNIT_NSECS;
149
0
    }
150
0
  }
151
0
}
152
153
static void spi_of_register_slaves(struct spi_controller *ctrl)
154
0
{
155
0
  struct device_node *n;
156
0
  struct property *reg;
157
0
  struct device_node *node = ctrl->dev->of_node;
158
159
0
  if (!IS_ENABLED(CONFIG_OFDEVICE))
160
0
    return;
161
162
0
  if (!node)
163
0
    return;
164
165
0
  for_each_available_child_of_node(node, n) {
166
0
    struct spi_board_info chip = {};
167
168
0
    if (n->dev) {
169
0
      dev_dbg(ctrl->dev, "skipping already registered %s\n",
170
0
        dev_name(n->dev));
171
0
      continue;
172
0
    }
173
174
0
    chip.name = xstrdup(n->name);
175
0
    chip.bus_num = ctrl->bus_num;
176
    /* Mode (clock phase/polarity/etc.) */
177
0
    if (of_property_read_bool(n, "spi-cpha"))
178
0
      chip.mode |= SPI_CPHA;
179
0
    if (of_property_read_bool(n, "spi-cpol"))
180
0
      chip.mode |= SPI_CPOL;
181
0
    if (of_property_read_bool(n, "spi-cs-high"))
182
0
      chip.mode |= SPI_CS_HIGH;
183
0
    if (of_property_read_bool(n, "spi-3wire"))
184
0
      chip.mode |= SPI_3WIRE;
185
0
    of_property_read_u32(n, "spi-max-frequency",
186
0
        &chip.max_speed_hz);
187
188
    /* Device CS delays */
189
0
    of_spi_parse_dt_cs_delay(n, &chip.cs_setup, "spi-cs-setup-delay-ns");
190
0
    of_spi_parse_dt_cs_delay(n, &chip.cs_hold, "spi-cs-hold-delay-ns");
191
0
    of_spi_parse_dt_cs_delay(n, &chip.cs_inactive, "spi-cs-inactive-delay-ns");
192
193
0
    reg = of_find_property(n, "reg", NULL);
194
0
    if (!reg)
195
0
      continue;
196
0
    chip.chip_select = of_read_number(reg->value, 1);
197
0
    chip.device_node = n;
198
0
    spi_new_device(ctrl, &chip);
199
0
  }
200
0
}
201
202
static void spi_controller_rescan(struct device *dev)
203
0
{
204
0
  struct spi_controller *ctrl;
205
206
0
  list_for_each_entry(ctrl, &spi_controller_list, list) {
207
0
    if (ctrl->dev != dev)
208
0
      continue;
209
0
    spi_of_register_slaves(ctrl);
210
0
  }
211
0
}
212
213
/**
214
 * spi_register_board_info - register SPI devices for a given board
215
 * @info: array of chip descriptors
216
 * @n: how many descriptors are provided
217
 * Context: can sleep
218
 *
219
 * Board-specific early init code calls this (probably during arch_initcall)
220
 * with segments of the SPI device table.  Any device nodes are created later,
221
 * after the relevant parent SPI controller (bus_num) is defined.  We keep
222
 * this table of devices forever, so that reloading a controller driver will
223
 * not make Linux forget about these hard-wired devices.
224
 *
225
 * Other code can also call this, e.g. a particular add-on board might provide
226
 * SPI devices through its expansion connector, so code initializing that board
227
 * would naturally declare its SPI devices.
228
 *
229
 * The board info passed can safely be __initdata ... but be careful of
230
 * any embedded pointers (platform_data, etc), they're copied as-is.
231
 */
232
int
233
spi_register_board_info(struct spi_board_info const *info, int n)
234
0
{
235
0
  struct boardinfo  *bi;
236
237
0
  bi = xmalloc(sizeof(*bi) + n * sizeof *info);
238
239
0
  bi->n_board_info = n;
240
0
  memcpy(bi->board_info, info, n * sizeof *info);
241
242
0
  list_add_tail(&bi->list, &board_list);
243
244
0
  return 0;
245
0
}
246
247
static void scan_boardinfo(struct spi_controller *ctrl)
248
0
{
249
0
  struct boardinfo  *bi;
250
251
0
  list_for_each_entry(bi, &board_list, list) {
252
0
    struct spi_board_info *chip = bi->board_info;
253
0
    unsigned    n;
254
255
0
    for (n = bi->n_board_info; n > 0; n--, chip++) {
256
0
      debug("%s %d %d\n", __FUNCTION__, chip->bus_num, ctrl->bus_num);
257
0
      if (chip->bus_num != ctrl->bus_num)
258
0
        continue;
259
      /* NOTE: this relies on spi_new_device to
260
       * issue diagnostics when given bogus inputs
261
       */
262
0
      (void) spi_new_device(ctrl, chip);
263
0
    }
264
0
  }
265
0
}
266
267
/**
268
 * spi_get_gpio_descs() - grab chip select GPIOs for the master
269
 * @ctlr: The SPI master to grab GPIO descriptors for
270
 */
271
static int spi_get_gpio_descs(struct spi_controller *ctlr)
272
0
{
273
0
  int nb, i;
274
0
  struct gpio_desc **cs;
275
0
  struct device *dev = ctlr->dev;
276
277
0
  nb = gpiod_count(dev, "cs");
278
0
  if (nb < 0) {
279
    /* No GPIOs at all is fine, else return the error */
280
0
    if (nb == -ENOENT)
281
0
      return 0;
282
0
    return nb;
283
0
  }
284
285
0
  ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
286
287
0
  cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
288
0
        GFP_KERNEL);
289
0
  if (!cs)
290
0
    return -ENOMEM;
291
0
  ctlr->cs_gpiods = cs;
292
293
0
  for (i = 0; i < nb; i++) {
294
    /*
295
     * Most chipselects are active low, the inverted
296
     * semantics are handled by special quirks in gpiolib,
297
     * so initializing them GPIOD_OUT_LOW here means
298
     * "unasserted", in most cases this will drive the physical
299
     * line high.
300
     */
301
0
    cs[i] = gpiod_get_index_optional(dev, "cs", i, GPIOD_OUT_LOW);
302
0
    if (IS_ERR(cs[i]))
303
0
      return PTR_ERR(cs[i]);
304
305
0
    if (cs[i]) {
306
      /*
307
       * If we find a CS GPIO, name it after the device and
308
       * chip select line.
309
       */
310
0
      char *gpioname;
311
312
0
      gpioname = xasprintf("%s CS%d", dev_name(dev), i);
313
0
      gpiod_set_consumer_name(cs[i], gpioname);
314
0
      free(gpioname);
315
0
    }
316
0
  }
317
318
0
  return 0;
319
0
}
320
321
static void _spi_transfer_delay_ns(u32 ns)
322
0
{
323
0
  if (!ns)
324
0
    return;
325
0
  if (ns <= NSEC_PER_USEC) {
326
0
    ndelay(ns);
327
0
  } else {
328
0
    u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
329
330
0
    udelay(us);
331
0
  }
332
0
}
333
334
int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
335
0
{
336
0
  u32 delay = _delay->value;
337
0
  u32 unit = _delay->unit;
338
0
  u32 hz;
339
340
0
  if (!delay)
341
0
    return 0;
342
343
0
  switch (unit) {
344
0
  case SPI_DELAY_UNIT_USECS:
345
0
    delay *= NSEC_PER_USEC;
346
0
    break;
347
0
  case SPI_DELAY_UNIT_NSECS:
348
    /* Nothing to do here */
349
0
    break;
350
0
  case SPI_DELAY_UNIT_SCK:
351
    /* Clock cycles need to be obtained from spi_transfer */
352
0
    if (!xfer)
353
0
      return -EINVAL;
354
    /*
355
     * If there is unknown effective speed, approximate it
356
     * by underestimating with half of the requested Hz.
357
     */
358
0
    hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
359
0
    if (!hz)
360
0
      return -EINVAL;
361
362
    /* Convert delay to nanoseconds */
363
0
    delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz);
364
0
    break;
365
0
  default:
366
0
    return -EINVAL;
367
0
  }
368
369
0
  return delay;
370
0
}
371
EXPORT_SYMBOL_GPL(spi_delay_to_ns);
372
373
int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
374
0
{
375
0
  int delay;
376
377
0
  if (!_delay)
378
0
    return -EINVAL;
379
380
0
  delay = spi_delay_to_ns(_delay, xfer);
381
0
  if (delay < 0)
382
0
    return delay;
383
384
0
  _spi_transfer_delay_ns(delay);
385
386
0
  return 0;
387
0
}
388
EXPORT_SYMBOL_GPL(spi_delay_exec);
389
390
static void spi_set_cs(struct spi_device *spi, bool enable)
391
0
{
392
0
        bool activate = enable;
393
394
0
        if (spi->cs_gpiod && !activate)
395
0
                spi_delay_exec(&spi->cs_hold, NULL);
396
397
0
        if (spi->mode & SPI_CS_HIGH)
398
0
                enable = !enable;
399
400
0
        if (spi->cs_gpiod) {
401
0
                if (!(spi->mode & SPI_NO_CS)) {
402
      /* Polarity handled by GPIO library */
403
0
      gpiod_set_value(spi->cs_gpiod, activate);
404
0
                }
405
                /* Some SPI masters need both GPIO CS & slave_select */
406
0
                if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) &&
407
0
                    spi->controller->set_cs)
408
0
                        spi->controller->set_cs(spi, !enable);
409
0
        } else if (spi->controller->set_cs) {
410
0
                spi->controller->set_cs(spi, !enable);
411
0
        }
412
413
0
        if (spi->cs_gpiod || !spi->controller->set_cs_timing) {
414
0
                if (activate)
415
0
                        spi_delay_exec(&spi->cs_setup, NULL);
416
0
                else
417
0
                        spi_delay_exec(&spi->cs_inactive, NULL);
418
0
        }
419
0
}
420
421
/*
422
 * spi_transfer_one_message - Default implementation of transfer()
423
 *
424
 * This is a standard implementation of transfer() for drivers which implement a
425
 * transfer_one() operation. It provides standard handling of delays and chip
426
 * select management.
427
 *
428
 */
429
static int spi_transfer_one_message(struct spi_device *spi, struct spi_message *msg)
430
0
{
431
0
  struct spi_controller *ctlr = spi->controller;
432
0
  struct spi_transfer *xfer;
433
0
  bool keep_cs = false;
434
0
  int ret = 0;
435
436
0
  if (ctlr->prepare_message) {
437
0
    ret = ctlr->prepare_message(ctlr, msg);
438
0
    if (ret) {
439
0
      dev_err(ctlr->dev, "failed to prepare message: %d\n",
440
0
        ret);
441
0
      msg->status = ret;
442
0
      return ret;
443
0
    }
444
0
  }
445
446
0
  spi_set_cs(msg->spi, true);
447
448
0
  list_for_each_entry(xfer, &msg->transfers, transfer_list) {
449
0
    if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
450
0
      ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
451
0
      if (ret < 0) {
452
0
        dev_err(&msg->spi->dev,
453
0
          "SPI transfer failed: %d\n", ret);
454
0
        goto out;
455
0
      }
456
0
    } else {
457
0
      if (xfer->len)
458
0
        dev_err(&msg->spi->dev,
459
0
          "Bufferless transfer has length %u\n",
460
0
          xfer->len);
461
0
    }
462
463
0
    if (msg->status != -EINPROGRESS)
464
0
      goto out;
465
466
0
    spi_delay_exec(&xfer->delay, NULL);
467
468
0
    if (xfer->cs_change) {
469
0
      if (list_is_last(&xfer->transfer_list,
470
0
           &msg->transfers)) {
471
0
        keep_cs = true;
472
0
      } else {
473
0
        spi_set_cs(msg->spi, false);
474
        /* TODO: Convert to new spi_delay API */
475
0
        udelay(10);
476
0
        spi_set_cs(msg->spi, true);
477
0
      }
478
0
    }
479
480
0
    msg->actual_length += xfer->len;
481
0
  }
482
483
0
out:
484
0
  if (ret != 0 || !keep_cs)
485
0
    spi_set_cs(msg->spi, false);
486
487
0
  if (msg->status == -EINPROGRESS)
488
0
    msg->status = ret;
489
490
0
  if (msg->status && ctlr->handle_err)
491
0
    ctlr->handle_err(ctlr, msg);
492
493
0
  return ret;
494
0
}
495
496
static int spi_controller_check_ops(struct spi_controller *ctlr)
497
0
{
498
  /*
499
   * The controller may implement only the high-level SPI-memory like
500
   * operations if it does not support regular SPI transfers, and this is
501
   * valid use case.
502
   * If ->mem_ops is NULL, we request that at least one of the
503
   * ->transfer_xxx() method be implemented.
504
   */
505
0
  if (ctlr->mem_ops) {
506
0
    if (!ctlr->mem_ops->exec_op)
507
0
      return -EINVAL;
508
0
  } else if (!ctlr->transfer && !ctlr->transfer_one) {
509
0
    return -EINVAL;
510
0
  }
511
512
0
  return 0;
513
0
}
514
515
516
/**
517
 * spi_register_ctrl - register SPI ctrl controller
518
 * @ctrl: initialized ctrl, originally from spi_alloc_ctrl()
519
 * Context: can sleep
520
 *
521
 * SPI controllers connect to their drivers using some non-SPI bus,
522
 * such as the platform bus.  The final stage of probe() in that code
523
 * includes calling spi_register_ctrl() to hook up to this SPI bus glue.
524
 *
525
 * SPI controllers use board specific (often SOC specific) bus numbers,
526
 * and board-specific addressing for SPI devices combines those numbers
527
 * with chip select numbers.  Since SPI does not directly support dynamic
528
 * device identification, boards need configuration tables telling which
529
 * chip is at which address.
530
 *
531
 * This must be called from context that can sleep.  It returns zero on
532
 * success, else a negative error code (dropping the ctrl's refcount).
533
 * After a successful return, the caller is responsible for calling
534
 * spi_unregister_ctrl().
535
 */
536
int spi_register_controller(struct spi_controller *ctrl)
537
0
{
538
0
  static int dyn_bus_id = (1 << 15) - 1;
539
0
  int     status = -ENODEV;
540
541
0
  debug("%s: %s:%d\n", __func__, ctrl->dev->name, ctrl->dev->id);
542
543
  /*
544
   * Make sure all necessary hooks are implemented before registering
545
   * the SPI controller.
546
   */
547
0
  status = spi_controller_check_ops(ctrl);
548
0
  if (status)
549
0
    return status;
550
551
0
  if (ctrl->transfer_one)
552
0
    ctrl->transfer = spi_transfer_one_message;
553
554
0
  slice_init(&ctrl->slice, dev_name(ctrl->dev));
555
556
  /* even if it's just one always-selected device, there must
557
   * be at least one chipselect
558
   */
559
0
  if (ctrl->num_chipselect == 0)
560
0
    return -EINVAL;
561
562
0
  if ((ctrl->bus_num < 0) && ctrl->dev->of_node)
563
0
    ctrl->bus_num = of_alias_get_id(ctrl->dev->of_node, "spi");
564
565
  /* convention:  dynamically assigned bus IDs count down from the max */
566
0
  if (ctrl->bus_num < 0)
567
0
    ctrl->bus_num = dyn_bus_id--;
568
569
0
  if (ctrl->use_gpio_descriptors) {
570
0
    status = spi_get_gpio_descs(ctrl);
571
0
    if (status)
572
0
      return status;
573
0
  }
574
575
0
  list_add_tail(&ctrl->list, &spi_controller_list);
576
577
0
  spi_of_register_slaves(ctrl);
578
579
  /* populate children from any spi device tables */
580
0
  scan_boardinfo(ctrl);
581
0
  status = 0;
582
583
0
  if (!ctrl->dev->rescan)
584
0
    ctrl->dev->rescan = spi_controller_rescan;
585
586
0
  return status;
587
0
}
588
EXPORT_SYMBOL(spi_register_controller);
589
590
struct spi_controller *spi_get_controller(int bus)
591
0
{
592
0
  struct spi_controller* m;
593
594
0
  list_for_each_entry(m, &spi_controller_list, list) {
595
0
    if (m->bus_num == bus)
596
0
      return m;
597
0
  }
598
599
0
  return NULL;
600
0
}
601
602
static int __spi_validate(struct spi_device *spi, struct spi_message *message)
603
0
{
604
0
  struct spi_controller *ctlr = spi->controller;
605
0
  struct spi_transfer *xfer;
606
0
  int w_size;
607
608
0
  if (list_empty(&message->transfers))
609
0
    return -EINVAL;
610
611
0
  message->spi = spi;
612
613
0
  list_for_each_entry(xfer, &message->transfers, transfer_list) {
614
0
    xfer->effective_speed_hz = 0;
615
0
    if (!xfer->bits_per_word)
616
0
      xfer->bits_per_word = spi->bits_per_word;
617
618
0
    if (!xfer->speed_hz)
619
0
      xfer->speed_hz = spi->max_speed_hz;
620
621
0
    if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
622
0
      xfer->speed_hz = ctlr->max_speed_hz;
623
624
    /*
625
     * SPI transfer length should be multiple of SPI word size
626
     * where SPI word size should be power-of-two multiple
627
     */
628
0
    if (xfer->bits_per_word <= 8)
629
0
      w_size = 1;
630
0
    else if (xfer->bits_per_word <= 16)
631
0
      w_size = 2;
632
0
    else
633
0
      w_size = 4;
634
635
    /* No partial transfers accepted */
636
0
    if (xfer->len % w_size)
637
0
      return -EINVAL;
638
0
  }
639
640
0
  message->actual_length = 0;
641
0
  message->status = -EINPROGRESS;
642
643
0
  return 0;
644
0
}
645
646
int spi_sync(struct spi_device *spi, struct spi_message *message)
647
0
{
648
0
  int status;
649
0
  int ret;
650
651
0
  if (!spi->controller->transfer)
652
0
    return -ENOTSUPP;
653
654
0
  status = __spi_validate(spi, message);
655
0
  if (status != 0)
656
0
    return status;
657
658
0
  slice_acquire(&spi->controller->slice);
659
660
0
  ret = spi->controller->transfer(spi, message);
661
662
0
  slice_release(&spi->controller->slice);
663
664
0
  return ret;
665
0
}
666
667
/**
668
 * spi_write_then_read - SPI synchronous write followed by read
669
 * @spi: device with which data will be exchanged
670
 * @txbuf: data to be written
671
 * @n_tx: size of txbuf, in bytes
672
 * @rxbuf: buffer into which data will be read
673
 * @n_rx: size of rxbuf, in bytes
674
 * Context: can sleep
675
 *
676
 * This performs a half duplex MicroWire style transaction with the
677
 * device, sending txbuf and then reading rxbuf.  The return value
678
 * is zero for success, else a negative errno status code.
679
 * This call may only be used from a context that may sleep.
680
 */
681
int spi_write_then_read(struct spi_device *spi,
682
    const void *txbuf, unsigned n_tx,
683
    void *rxbuf, unsigned n_rx)
684
0
{
685
0
  int     status;
686
0
  struct spi_message  message;
687
0
  struct spi_transfer x[2];
688
689
0
  spi_message_init(&message);
690
0
  memset(x, 0, sizeof x);
691
0
  if (n_tx) {
692
0
    x[0].len = n_tx;
693
0
    spi_message_add_tail(&x[0], &message);
694
0
  }
695
0
  if (n_rx) {
696
0
    x[1].len = n_rx;
697
0
    spi_message_add_tail(&x[1], &message);
698
0
  }
699
700
0
  x[0].tx_buf = txbuf;
701
0
  x[1].rx_buf = rxbuf;
702
703
  /* do the i/o */
704
0
  status = spi_sync(spi, &message);
705
0
  return status;
706
0
}
707
EXPORT_SYMBOL(spi_write_then_read);
708
709
struct bus_type spi_bus = {
710
  .name = "spi",
711
  .match = device_match_of_modalias,
712
};
713
714
static int spi_bus_init(void)
715
32
{
716
32
  return bus_register(&spi_bus);
717
32
}
718
pure_initcall(spi_bus_init);