spi.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* SPDX-License-Identifier: GPL-2.0-or-later
  2. *
  3. * Copyright (C) 2005 David Brownell
  4. */
  5. #ifndef __LINUX_SPI_H
  6. #define __LINUX_SPI_H
  7. #include <linux/device.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/slab.h>
  10. #include <linux/kthread.h>
  11. #include <linux/completion.h>
  12. #include <linux/scatterlist.h>
  13. struct dma_chan;
  14. struct property_entry;
  15. struct spi_controller;
  16. struct spi_transfer;
  17. struct spi_controller_mem_ops;
  18. /*
  19. * INTERFACES between SPI master-side drivers and SPI slave protocol handlers,
  20. * and SPI infrastructure.
  21. */
  22. extern struct bus_type spi_bus_type;
  23. /**
  24. * struct spi_statistics - statistics for spi transfers
  25. * @lock: lock protecting this structure
  26. *
  27. * @messages: number of spi-messages handled
  28. * @transfers: number of spi_transfers handled
  29. * @errors: number of errors during spi_transfer
  30. * @timedout: number of timeouts during spi_transfer
  31. *
  32. * @spi_sync: number of times spi_sync is used
  33. * @spi_sync_immediate:
  34. * number of times spi_sync is executed immediately
  35. * in calling context without queuing and scheduling
  36. * @spi_async: number of times spi_async is used
  37. *
  38. * @bytes: number of bytes transferred to/from device
  39. * @bytes_tx: number of bytes sent to device
  40. * @bytes_rx: number of bytes received from device
  41. *
  42. * @transfer_bytes_histo:
  43. * transfer bytes histogramm
  44. *
  45. * @transfers_split_maxsize:
  46. * number of transfers that have been split because of
  47. * maxsize limit
  48. */
  49. struct spi_statistics {
  50. spinlock_t lock; /* lock for the whole structure */
  51. unsigned long messages;
  52. unsigned long transfers;
  53. unsigned long errors;
  54. unsigned long timedout;
  55. unsigned long spi_sync;
  56. unsigned long spi_sync_immediate;
  57. unsigned long spi_async;
  58. unsigned long long bytes;
  59. unsigned long long bytes_rx;
  60. unsigned long long bytes_tx;
  61. #define SPI_STATISTICS_HISTO_SIZE 17
  62. unsigned long transfer_bytes_histo[SPI_STATISTICS_HISTO_SIZE];
  63. unsigned long transfers_split_maxsize;
  64. };
  65. void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
  66. struct spi_transfer *xfer,
  67. struct spi_controller *ctlr);
  68. #define SPI_STATISTICS_ADD_TO_FIELD(stats, field, count) \
  69. do { \
  70. unsigned long flags; \
  71. spin_lock_irqsave(&(stats)->lock, flags); \
  72. (stats)->field += count; \
  73. spin_unlock_irqrestore(&(stats)->lock, flags); \
  74. } while (0)
  75. #define SPI_STATISTICS_INCREMENT_FIELD(stats, field) \
  76. SPI_STATISTICS_ADD_TO_FIELD(stats, field, 1)
  77. /**
  78. * struct spi_device - Controller side proxy for an SPI slave device
  79. * @dev: Driver model representation of the device.
  80. * @controller: SPI controller used with the device.
  81. * @master: Copy of controller, for backwards compatibility.
  82. * @max_speed_hz: Maximum clock rate to be used with this chip
  83. * (on this board); may be changed by the device's driver.
  84. * The spi_transfer.speed_hz can override this for each transfer.
  85. * @chip_select: Chipselect, distinguishing chips handled by @controller.
  86. * @mode: The spi mode defines how data is clocked out and in.
  87. * This may be changed by the device's driver.
  88. * The "active low" default for chipselect mode can be overridden
  89. * (by specifying SPI_CS_HIGH) as can the "MSB first" default for
  90. * each word in a transfer (by specifying SPI_LSB_FIRST).
  91. * @bits_per_word: Data transfers involve one or more words; word sizes
  92. * like eight or 12 bits are common. In-memory wordsizes are
  93. * powers of two bytes (e.g. 20 bit samples use 32 bits).
  94. * This may be changed by the device's driver, or left at the
  95. * default (0) indicating protocol words are eight bit bytes.
  96. * The spi_transfer.bits_per_word can override this for each transfer.
  97. * @irq: Negative, or the number passed to request_irq() to receive
  98. * interrupts from this device.
  99. * @controller_state: Controller's runtime state
  100. * @controller_data: Board-specific definitions for controller, such as
  101. * FIFO initialization parameters; from board_info.controller_data
  102. * @modalias: Name of the driver to use with this device, or an alias
  103. * for that name. This appears in the sysfs "modalias" attribute
  104. * for driver coldplugging, and in uevents used for hotplugging
  105. * @cs_gpio: gpio number of the chipselect line (optional, -ENOENT when
  106. * not using a GPIO line)
  107. *
  108. * @statistics: statistics for the spi_device
  109. *
  110. * A @spi_device is used to interchange data between an SPI slave
  111. * (usually a discrete chip) and CPU memory.
  112. *
  113. * In @dev, the platform_data is used to hold information about this
  114. * device that's meaningful to the device's protocol driver, but not
  115. * to its controller. One example might be an identifier for a chip
  116. * variant with slightly different functionality; another might be
  117. * information about how this particular board wires the chip's pins.
  118. */
  119. struct spi_device {
  120. struct device dev;
  121. struct spi_controller *controller;
  122. struct spi_controller *master; /* compatibility layer */
  123. u32 max_speed_hz;
  124. u8 chip_select;
  125. u8 bits_per_word;
  126. u16 mode;
  127. #define SPI_CPHA 0x01 /* clock phase */
  128. #define SPI_CPOL 0x02 /* clock polarity */
  129. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  130. #define SPI_MODE_1 (0|SPI_CPHA)
  131. #define SPI_MODE_2 (SPI_CPOL|0)
  132. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  133. #define SPI_CS_HIGH 0x04 /* chipselect active high? */
  134. #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */
  135. #define SPI_3WIRE 0x10 /* SI/SO signals shared */
  136. #define SPI_LOOP 0x20 /* loopback mode */
  137. #define SPI_NO_CS 0x40 /* 1 dev/bus, no chipselect */
  138. #define SPI_READY 0x80 /* slave pulls low to pause */
  139. #define SPI_TX_DUAL 0x100 /* transmit with 2 wires */
  140. #define SPI_TX_QUAD 0x200 /* transmit with 4 wires */
  141. #define SPI_RX_DUAL 0x400 /* receive with 2 wires */
  142. #define SPI_RX_QUAD 0x800 /* receive with 4 wires */
  143. #define SPI_CS_WORD 0x1000 /* toggle cs after each word */
  144. int irq;
  145. void *controller_state;
  146. void *controller_data;
  147. char modalias[SPI_NAME_SIZE];
  148. const char *driver_override;
  149. int cs_gpio; /* chip select gpio */
  150. /* the statistics */
  151. struct spi_statistics statistics;
  152. /*
  153. * likely need more hooks for more protocol options affecting how
  154. * the controller talks to each chip, like:
  155. * - memory packing (12 bit samples into low bits, others zeroed)
  156. * - priority
  157. * - chipselect delays
  158. * - ...
  159. */
  160. };
  161. static inline struct spi_device *to_spi_device(struct device *dev)
  162. {
  163. return dev ? container_of(dev, struct spi_device, dev) : NULL;
  164. }
  165. /* most drivers won't need to care about device refcounting */
  166. static inline struct spi_device *spi_dev_get(struct spi_device *spi)
  167. {
  168. return (spi && get_device(&spi->dev)) ? spi : NULL;
  169. }
  170. static inline void spi_dev_put(struct spi_device *spi)
  171. {
  172. if (spi)
  173. put_device(&spi->dev);
  174. }
  175. /* ctldata is for the bus_controller driver's runtime state */
  176. static inline void *spi_get_ctldata(struct spi_device *spi)
  177. {
  178. return spi->controller_state;
  179. }
  180. static inline void spi_set_ctldata(struct spi_device *spi, void *state)
  181. {
  182. spi->controller_state = state;
  183. }
  184. /* device driver data */
  185. static inline void spi_set_drvdata(struct spi_device *spi, void *data)
  186. {
  187. dev_set_drvdata(&spi->dev, data);
  188. }
  189. static inline void *spi_get_drvdata(struct spi_device *spi)
  190. {
  191. return dev_get_drvdata(&spi->dev);
  192. }
  193. struct spi_message;
  194. struct spi_transfer;
  195. /**
  196. * struct spi_driver - Host side "protocol" driver
  197. * @id_table: List of SPI devices supported by this driver
  198. * @probe: Binds this driver to the spi device. Drivers can verify
  199. * that the device is actually present, and may need to configure
  200. * characteristics (such as bits_per_word) which weren't needed for
  201. * the initial configuration done during system setup.
  202. * @remove: Unbinds this driver from the spi device
  203. * @shutdown: Standard shutdown callback used during system state
  204. * transitions such as powerdown/halt and kexec
  205. * @driver: SPI device drivers should initialize the name and owner
  206. * field of this structure.
  207. *
  208. * This represents the kind of device driver that uses SPI messages to
  209. * interact with the hardware at the other end of a SPI link. It's called
  210. * a "protocol" driver because it works through messages rather than talking
  211. * directly to SPI hardware (which is what the underlying SPI controller
  212. * driver does to pass those messages). These protocols are defined in the
  213. * specification for the device(s) supported by the driver.
  214. *
  215. * As a rule, those device protocols represent the lowest level interface
  216. * supported by a driver, and it will support upper level interfaces too.
  217. * Examples of such upper levels include frameworks like MTD, networking,
  218. * MMC, RTC, filesystem character device nodes, and hardware monitoring.
  219. */
  220. struct spi_driver {
  221. const struct spi_device_id *id_table;
  222. int (*probe)(struct spi_device *spi);
  223. int (*remove)(struct spi_device *spi);
  224. void (*shutdown)(struct spi_device *spi);
  225. struct device_driver driver;
  226. };
  227. static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
  228. {
  229. return drv ? container_of(drv, struct spi_driver, driver) : NULL;
  230. }
  231. extern int __spi_register_driver(struct module *owner, struct spi_driver *sdrv);
  232. /**
  233. * spi_unregister_driver - reverse effect of spi_register_driver
  234. * @sdrv: the driver to unregister
  235. * Context: can sleep
  236. */
  237. static inline void spi_unregister_driver(struct spi_driver *sdrv)
  238. {
  239. if (sdrv)
  240. driver_unregister(&sdrv->driver);
  241. }
  242. /* use a define to avoid include chaining to get THIS_MODULE */
  243. #define spi_register_driver(driver) \
  244. __spi_register_driver(THIS_MODULE, driver)
  245. /**
  246. * module_spi_driver() - Helper macro for registering a SPI driver
  247. * @__spi_driver: spi_driver struct
  248. *
  249. * Helper macro for SPI drivers which do not do anything special in module
  250. * init/exit. This eliminates a lot of boilerplate. Each module may only
  251. * use this macro once, and calling it replaces module_init() and module_exit()
  252. */
  253. #define module_spi_driver(__spi_driver) \
  254. module_driver(__spi_driver, spi_register_driver, \
  255. spi_unregister_driver)
  256. /**
  257. * struct spi_controller - interface to SPI master or slave controller
  258. * @dev: device interface to this driver
  259. * @list: link with the global spi_controller list
  260. * @bus_num: board-specific (and often SOC-specific) identifier for a
  261. * given SPI controller.
  262. * @num_chipselect: chipselects are used to distinguish individual
  263. * SPI slaves, and are numbered from zero to num_chipselects.
  264. * each slave has a chipselect signal, but it's common that not
  265. * every chipselect is connected to a slave.
  266. * @dma_alignment: SPI controller constraint on DMA buffers alignment.
  267. * @mode_bits: flags understood by this controller driver
  268. * @bits_per_word_mask: A mask indicating which values of bits_per_word are
  269. * supported by the driver. Bit n indicates that a bits_per_word n+1 is
  270. * supported. If set, the SPI core will reject any transfer with an
  271. * unsupported bits_per_word. If not set, this value is simply ignored,
  272. * and it's up to the individual driver to perform any validation.
  273. * @min_speed_hz: Lowest supported transfer speed
  274. * @max_speed_hz: Highest supported transfer speed
  275. * @flags: other constraints relevant to this driver
  276. * @slave: indicates that this is an SPI slave controller
  277. * @max_transfer_size: function that returns the max transfer size for
  278. * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
  279. * @max_message_size: function that returns the max message size for
  280. * a &spi_device; may be %NULL, so the default %SIZE_MAX will be used.
  281. * @io_mutex: mutex for physical bus access
  282. * @bus_lock_spinlock: spinlock for SPI bus locking
  283. * @bus_lock_mutex: mutex for exclusion of multiple callers
  284. * @bus_lock_flag: indicates that the SPI bus is locked for exclusive use
  285. * @setup: updates the device mode and clocking records used by a
  286. * device's SPI controller; protocol code may call this. This
  287. * must fail if an unrecognized or unsupported mode is requested.
  288. * It's always safe to call this unless transfers are pending on
  289. * the device whose settings are being modified.
  290. * @transfer: adds a message to the controller's transfer queue.
  291. * @cleanup: frees controller-specific state
  292. * @can_dma: determine whether this controller supports DMA
  293. * @queued: whether this controller is providing an internal message queue
  294. * @kworker: thread struct for message pump
  295. * @kworker_task: pointer to task for message pump kworker thread
  296. * @pump_messages: work struct for scheduling work to the message pump
  297. * @queue_lock: spinlock to syncronise access to message queue
  298. * @queue: message queue
  299. * @idling: the device is entering idle state
  300. * @cur_msg: the currently in-flight message
  301. * @cur_msg_prepared: spi_prepare_message was called for the currently
  302. * in-flight message
  303. * @cur_msg_mapped: message has been mapped for DMA
  304. * @xfer_completion: used by core transfer_one_message()
  305. * @busy: message pump is busy
  306. * @running: message pump is running
  307. * @rt: whether this queue is set to run as a realtime task
  308. * @auto_runtime_pm: the core should ensure a runtime PM reference is held
  309. * while the hardware is prepared, using the parent
  310. * device for the spidev
  311. * @max_dma_len: Maximum length of a DMA transfer for the device.
  312. * @prepare_transfer_hardware: a message will soon arrive from the queue
  313. * so the subsystem requests the driver to prepare the transfer hardware
  314. * by issuing this call
  315. * @transfer_one_message: the subsystem calls the driver to transfer a single
  316. * message while queuing transfers that arrive in the meantime. When the
  317. * driver is finished with this message, it must call
  318. * spi_finalize_current_message() so the subsystem can issue the next
  319. * message
  320. * @unprepare_transfer_hardware: there are currently no more messages on the
  321. * queue so the subsystem notifies the driver that it may relax the
  322. * hardware by issuing this call
  323. * @set_cs: set the logic level of the chip select line. May be called
  324. * from interrupt context.
  325. * @prepare_message: set up the controller to transfer a single message,
  326. * for example doing DMA mapping. Called from threaded
  327. * context.
  328. * @transfer_one: transfer a single spi_transfer.
  329. * - return 0 if the transfer is finished,
  330. * - return 1 if the transfer is still in progress. When
  331. * the driver is finished with this transfer it must
  332. * call spi_finalize_current_transfer() so the subsystem
  333. * can issue the next transfer. Note: transfer_one and
  334. * transfer_one_message are mutually exclusive; when both
  335. * are set, the generic subsystem does not call your
  336. * transfer_one callback.
  337. * @handle_err: the subsystem calls the driver to handle an error that occurs
  338. * in the generic implementation of transfer_one_message().
  339. * @mem_ops: optimized/dedicated operations for interactions with SPI memory.
  340. * This field is optional and should only be implemented if the
  341. * controller has native support for memory like operations.
  342. * @unprepare_message: undo any work done by prepare_message().
  343. * @slave_abort: abort the ongoing transfer request on an SPI slave controller
  344. * @cs_gpios: Array of GPIOs to use as chip select lines; one per CS
  345. * number. Any individual value may be -ENOENT for CS lines that
  346. * are not GPIOs (driven by the SPI controller itself).
  347. * @statistics: statistics for the spi_controller
  348. * @dma_tx: DMA transmit channel
  349. * @dma_rx: DMA receive channel
  350. * @dummy_rx: dummy receive buffer for full-duplex devices
  351. * @dummy_tx: dummy transmit buffer for full-duplex devices
  352. * @fw_translate_cs: If the boot firmware uses different numbering scheme
  353. * what Linux expects, this optional hook can be used to translate
  354. * between the two.
  355. *
  356. * Each SPI controller can communicate with one or more @spi_device
  357. * children. These make a small bus, sharing MOSI, MISO and SCK signals
  358. * but not chip select signals. Each device may be configured to use a
  359. * different clock rate, since those shared signals are ignored unless
  360. * the chip is selected.
  361. *
  362. * The driver for an SPI controller manages access to those devices through
  363. * a queue of spi_message transactions, copying data between CPU memory and
  364. * an SPI slave device. For each such message it queues, it calls the
  365. * message's completion function when the transaction completes.
  366. */
  367. struct spi_controller {
  368. struct device dev;
  369. struct list_head list;
  370. /* other than negative (== assign one dynamically), bus_num is fully
  371. * board-specific. usually that simplifies to being SOC-specific.
  372. * example: one SOC has three SPI controllers, numbered 0..2,
  373. * and one board's schematics might show it using SPI-2. software
  374. * would normally use bus_num=2 for that controller.
  375. */
  376. s16 bus_num;
  377. /* chipselects will be integral to many controllers; some others
  378. * might use board-specific GPIOs.
  379. */
  380. u16 num_chipselect;
  381. /* some SPI controllers pose alignment requirements on DMAable
  382. * buffers; let protocol drivers know about these requirements.
  383. */
  384. u16 dma_alignment;
  385. /* spi_device.mode flags understood by this controller driver */
  386. u16 mode_bits;
  387. /* bitmask of supported bits_per_word for transfers */
  388. u32 bits_per_word_mask;
  389. #define SPI_BPW_MASK(bits) BIT((bits) - 1)
  390. #define SPI_BIT_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
  391. #define SPI_BPW_RANGE_MASK(min, max) (SPI_BIT_MASK(max) - SPI_BIT_MASK(min - 1))
  392. /* limits on transfer speed */
  393. u32 min_speed_hz;
  394. u32 max_speed_hz;
  395. /* other constraints relevant to this driver */
  396. u16 flags;
  397. #define SPI_CONTROLLER_HALF_DUPLEX BIT(0) /* can't do full duplex */
  398. #define SPI_CONTROLLER_NO_RX BIT(1) /* can't do buffer read */
  399. #define SPI_CONTROLLER_NO_TX BIT(2) /* can't do buffer write */
  400. #define SPI_CONTROLLER_MUST_RX BIT(3) /* requires rx */
  401. #define SPI_CONTROLLER_MUST_TX BIT(4) /* requires tx */
  402. #define SPI_MASTER_GPIO_SS BIT(5) /* GPIO CS must select slave */
  403. /* flag indicating this is an SPI slave controller */
  404. bool slave;
  405. /*
  406. * on some hardware transfer / message size may be constrained
  407. * the limit may depend on device transfer settings
  408. */
  409. size_t (*max_transfer_size)(struct spi_device *spi);
  410. size_t (*max_message_size)(struct spi_device *spi);
  411. /* I/O mutex */
  412. struct mutex io_mutex;
  413. /* lock and mutex for SPI bus locking */
  414. spinlock_t bus_lock_spinlock;
  415. struct mutex bus_lock_mutex;
  416. /* flag indicating that the SPI bus is locked for exclusive use */
  417. bool bus_lock_flag;
  418. /* Setup mode and clock, etc (spi driver may call many times).
  419. *
  420. * IMPORTANT: this may be called when transfers to another
  421. * device are active. DO NOT UPDATE SHARED REGISTERS in ways
  422. * which could break those transfers.
  423. */
  424. int (*setup)(struct spi_device *spi);
  425. /* bidirectional bulk transfers
  426. *
  427. * + The transfer() method may not sleep; its main role is
  428. * just to add the message to the queue.
  429. * + For now there's no remove-from-queue operation, or
  430. * any other request management
  431. * + To a given spi_device, message queueing is pure fifo
  432. *
  433. * + The controller's main job is to process its message queue,
  434. * selecting a chip (for masters), then transferring data
  435. * + If there are multiple spi_device children, the i/o queue
  436. * arbitration algorithm is unspecified (round robin, fifo,
  437. * priority, reservations, preemption, etc)
  438. *
  439. * + Chipselect stays active during the entire message
  440. * (unless modified by spi_transfer.cs_change != 0).
  441. * + The message transfers use clock and SPI mode parameters
  442. * previously established by setup() for this device
  443. */
  444. int (*transfer)(struct spi_device *spi,
  445. struct spi_message *mesg);
  446. /* called on release() to free memory provided by spi_controller */
  447. void (*cleanup)(struct spi_device *spi);
  448. /*
  449. * Used to enable core support for DMA handling, if can_dma()
  450. * exists and returns true then the transfer will be mapped
  451. * prior to transfer_one() being called. The driver should
  452. * not modify or store xfer and dma_tx and dma_rx must be set
  453. * while the device is prepared.
  454. */
  455. bool (*can_dma)(struct spi_controller *ctlr,
  456. struct spi_device *spi,
  457. struct spi_transfer *xfer);
  458. /*
  459. * These hooks are for drivers that want to use the generic
  460. * controller transfer queueing mechanism. If these are used, the
  461. * transfer() function above must NOT be specified by the driver.
  462. * Over time we expect SPI drivers to be phased over to this API.
  463. */
  464. bool queued;
  465. struct kthread_worker kworker;
  466. struct task_struct *kworker_task;
  467. struct kthread_work pump_messages;
  468. spinlock_t queue_lock;
  469. struct list_head queue;
  470. struct spi_message *cur_msg;
  471. bool idling;
  472. bool busy;
  473. bool running;
  474. bool rt;
  475. bool auto_runtime_pm;
  476. bool cur_msg_prepared;
  477. bool cur_msg_mapped;
  478. struct completion xfer_completion;
  479. size_t max_dma_len;
  480. int (*prepare_transfer_hardware)(struct spi_controller *ctlr);
  481. int (*transfer_one_message)(struct spi_controller *ctlr,
  482. struct spi_message *mesg);
  483. int (*unprepare_transfer_hardware)(struct spi_controller *ctlr);
  484. int (*prepare_message)(struct spi_controller *ctlr,
  485. struct spi_message *message);
  486. int (*unprepare_message)(struct spi_controller *ctlr,
  487. struct spi_message *message);
  488. int (*slave_abort)(struct spi_controller *ctlr);
  489. /*
  490. * These hooks are for drivers that use a generic implementation
  491. * of transfer_one_message() provied by the core.
  492. */
  493. void (*set_cs)(struct spi_device *spi, bool enable);
  494. int (*transfer_one)(struct spi_controller *ctlr, struct spi_device *spi,
  495. struct spi_transfer *transfer);
  496. void (*handle_err)(struct spi_controller *ctlr,
  497. struct spi_message *message);
  498. /* Optimized handlers for SPI memory-like operations. */
  499. const struct spi_controller_mem_ops *mem_ops;
  500. /* gpio chip select */
  501. int *cs_gpios;
  502. /* statistics */
  503. struct spi_statistics statistics;
  504. /* DMA channels for use with core dmaengine helpers */
  505. struct dma_chan *dma_tx;
  506. struct dma_chan *dma_rx;
  507. /* dummy data for full duplex devices */
  508. void *dummy_rx;
  509. void *dummy_tx;
  510. int (*fw_translate_cs)(struct spi_controller *ctlr, unsigned cs);
  511. };
  512. static inline void *spi_controller_get_devdata(struct spi_controller *ctlr)
  513. {
  514. return dev_get_drvdata(&ctlr->dev);
  515. }
  516. static inline void spi_controller_set_devdata(struct spi_controller *ctlr,
  517. void *data)
  518. {
  519. dev_set_drvdata(&ctlr->dev, data);
  520. }
  521. static inline struct spi_controller *spi_controller_get(struct spi_controller *ctlr)
  522. {
  523. if (!ctlr || !get_device(&ctlr->dev))
  524. return NULL;
  525. return ctlr;
  526. }
  527. static inline void spi_controller_put(struct spi_controller *ctlr)
  528. {
  529. if (ctlr)
  530. put_device(&ctlr->dev);
  531. }
  532. static inline bool spi_controller_is_slave(struct spi_controller *ctlr)
  533. {
  534. return IS_ENABLED(CONFIG_SPI_SLAVE) && ctlr->slave;
  535. }
  536. /* PM calls that need to be issued by the driver */
  537. extern int spi_controller_suspend(struct spi_controller *ctlr);
  538. extern int spi_controller_resume(struct spi_controller *ctlr);
  539. /* Calls the driver make to interact with the message queue */
  540. extern struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr);
  541. extern void spi_finalize_current_message(struct spi_controller *ctlr);
  542. extern void spi_finalize_current_transfer(struct spi_controller *ctlr);
  543. /* the spi driver core manages memory for the spi_controller classdev */
  544. extern struct spi_controller *__spi_alloc_controller(struct device *host,
  545. unsigned int size, bool slave);
  546. static inline struct spi_controller *spi_alloc_master(struct device *host,
  547. unsigned int size)
  548. {
  549. return __spi_alloc_controller(host, size, false);
  550. }
  551. static inline struct spi_controller *spi_alloc_slave(struct device *host,
  552. unsigned int size)
  553. {
  554. if (!IS_ENABLED(CONFIG_SPI_SLAVE))
  555. return NULL;
  556. return __spi_alloc_controller(host, size, true);
  557. }
  558. extern int spi_register_controller(struct spi_controller *ctlr);
  559. extern int devm_spi_register_controller(struct device *dev,
  560. struct spi_controller *ctlr);
  561. extern void spi_unregister_controller(struct spi_controller *ctlr);
  562. extern struct spi_controller *spi_busnum_to_master(u16 busnum);
  563. /*
  564. * SPI resource management while processing a SPI message
  565. */
  566. typedef void (*spi_res_release_t)(struct spi_controller *ctlr,
  567. struct spi_message *msg,
  568. void *res);
  569. /**
  570. * struct spi_res - spi resource management structure
  571. * @entry: list entry
  572. * @release: release code called prior to freeing this resource
  573. * @data: extra data allocated for the specific use-case
  574. *
  575. * this is based on ideas from devres, but focused on life-cycle
  576. * management during spi_message processing
  577. */
  578. struct spi_res {
  579. struct list_head entry;
  580. spi_res_release_t release;
  581. unsigned long long data[]; /* guarantee ull alignment */
  582. };
  583. extern void *spi_res_alloc(struct spi_device *spi,
  584. spi_res_release_t release,
  585. size_t size, gfp_t gfp);
  586. extern void spi_res_add(struct spi_message *message, void *res);
  587. extern void spi_res_free(void *res);
  588. extern void spi_res_release(struct spi_controller *ctlr,
  589. struct spi_message *message);
  590. /*---------------------------------------------------------------------------*/
  591. /*
  592. * I/O INTERFACE between SPI controller and protocol drivers
  593. *
  594. * Protocol drivers use a queue of spi_messages, each transferring data
  595. * between the controller and memory buffers.
  596. *
  597. * The spi_messages themselves consist of a series of read+write transfer
  598. * segments. Those segments always read the same number of bits as they
  599. * write; but one or the other is easily ignored by passing a null buffer
  600. * pointer. (This is unlike most types of I/O API, because SPI hardware
  601. * is full duplex.)
  602. *
  603. * NOTE: Allocation of spi_transfer and spi_message memory is entirely
  604. * up to the protocol driver, which guarantees the integrity of both (as
  605. * well as the data buffers) for as long as the message is queued.
  606. */
  607. /**
  608. * struct spi_transfer - a read/write buffer pair
  609. * @tx_buf: data to be written (dma-safe memory), or NULL
  610. * @rx_buf: data to be read (dma-safe memory), or NULL
  611. * @tx_dma: DMA address of tx_buf, if @spi_message.is_dma_mapped
  612. * @rx_dma: DMA address of rx_buf, if @spi_message.is_dma_mapped
  613. * @tx_nbits: number of bits used for writing. If 0 the default
  614. * (SPI_NBITS_SINGLE) is used.
  615. * @rx_nbits: number of bits used for reading. If 0 the default
  616. * (SPI_NBITS_SINGLE) is used.
  617. * @len: size of rx and tx buffers (in bytes)
  618. * @speed_hz: Select a speed other than the device default for this
  619. * transfer. If 0 the default (from @spi_device) is used.
  620. * @bits_per_word: select a bits_per_word other than the device default
  621. * for this transfer. If 0 the default (from @spi_device) is used.
  622. * @cs_change: affects chipselect after this transfer completes
  623. * @delay_usecs: microseconds to delay after this transfer before
  624. * (optionally) changing the chipselect status, then starting
  625. * the next transfer or completing this @spi_message.
  626. * @word_delay: clock cycles to inter word delay after each word size
  627. * (set by bits_per_word) transmission.
  628. * @transfer_list: transfers are sequenced through @spi_message.transfers
  629. * @tx_sg: Scatterlist for transmit, currently not for client use
  630. * @rx_sg: Scatterlist for receive, currently not for client use
  631. *
  632. * SPI transfers always write the same number of bytes as they read.
  633. * Protocol drivers should always provide @rx_buf and/or @tx_buf.
  634. * In some cases, they may also want to provide DMA addresses for
  635. * the data being transferred; that may reduce overhead, when the
  636. * underlying driver uses dma.
  637. *
  638. * If the transmit buffer is null, zeroes will be shifted out
  639. * while filling @rx_buf. If the receive buffer is null, the data
  640. * shifted in will be discarded. Only "len" bytes shift out (or in).
  641. * It's an error to try to shift out a partial word. (For example, by
  642. * shifting out three bytes with word size of sixteen or twenty bits;
  643. * the former uses two bytes per word, the latter uses four bytes.)
  644. *
  645. * In-memory data values are always in native CPU byte order, translated
  646. * from the wire byte order (big-endian except with SPI_LSB_FIRST). So
  647. * for example when bits_per_word is sixteen, buffers are 2N bytes long
  648. * (@len = 2N) and hold N sixteen bit words in CPU byte order.
  649. *
  650. * When the word size of the SPI transfer is not a power-of-two multiple
  651. * of eight bits, those in-memory words include extra bits. In-memory
  652. * words are always seen by protocol drivers as right-justified, so the
  653. * undefined (rx) or unused (tx) bits are always the most significant bits.
  654. *
  655. * All SPI transfers start with the relevant chipselect active. Normally
  656. * it stays selected until after the last transfer in a message. Drivers
  657. * can affect the chipselect signal using cs_change.
  658. *
  659. * (i) If the transfer isn't the last one in the message, this flag is
  660. * used to make the chipselect briefly go inactive in the middle of the
  661. * message. Toggling chipselect in this way may be needed to terminate
  662. * a chip command, letting a single spi_message perform all of group of
  663. * chip transactions together.
  664. *
  665. * (ii) When the transfer is the last one in the message, the chip may
  666. * stay selected until the next transfer. On multi-device SPI busses
  667. * with nothing blocking messages going to other devices, this is just
  668. * a performance hint; starting a message to another device deselects
  669. * this one. But in other cases, this can be used to ensure correctness.
  670. * Some devices need protocol transactions to be built from a series of
  671. * spi_message submissions, where the content of one message is determined
  672. * by the results of previous messages and where the whole transaction
  673. * ends when the chipselect goes intactive.
  674. *
  675. * When SPI can transfer in 1x,2x or 4x. It can get this transfer information
  676. * from device through @tx_nbits and @rx_nbits. In Bi-direction, these
  677. * two should both be set. User can set transfer mode with SPI_NBITS_SINGLE(1x)
  678. * SPI_NBITS_DUAL(2x) and SPI_NBITS_QUAD(4x) to support these three transfer.
  679. *
  680. * The code that submits an spi_message (and its spi_transfers)
  681. * to the lower layers is responsible for managing its memory.
  682. * Zero-initialize every field you don't set up explicitly, to
  683. * insulate against future API updates. After you submit a message
  684. * and its transfers, ignore them until its completion callback.
  685. */
  686. struct spi_transfer {
  687. /* it's ok if tx_buf == rx_buf (right?)
  688. * for MicroWire, one buffer must be null
  689. * buffers must work with dma_*map_single() calls, unless
  690. * spi_message.is_dma_mapped reports a pre-existing mapping
  691. */
  692. const void *tx_buf;
  693. void *rx_buf;
  694. unsigned len;
  695. dma_addr_t tx_dma;
  696. dma_addr_t rx_dma;
  697. struct sg_table tx_sg;
  698. struct sg_table rx_sg;
  699. unsigned cs_change:1;
  700. unsigned tx_nbits:3;
  701. unsigned rx_nbits:3;
  702. #define SPI_NBITS_SINGLE 0x01 /* 1bit transfer */
  703. #define SPI_NBITS_DUAL 0x02 /* 2bits transfer */
  704. #define SPI_NBITS_QUAD 0x04 /* 4bits transfer */
  705. u8 bits_per_word;
  706. u16 delay_usecs;
  707. u32 speed_hz;
  708. u16 word_delay;
  709. struct list_head transfer_list;
  710. };
  711. /**
  712. * struct spi_message - one multi-segment SPI transaction
  713. * @transfers: list of transfer segments in this transaction
  714. * @spi: SPI device to which the transaction is queued
  715. * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
  716. * addresses for each transfer buffer
  717. * @complete: called to report transaction completions
  718. * @context: the argument to complete() when it's called
  719. * @frame_length: the total number of bytes in the message
  720. * @actual_length: the total number of bytes that were transferred in all
  721. * successful segments
  722. * @status: zero for success, else negative errno
  723. * @queue: for use by whichever driver currently owns the message
  724. * @state: for use by whichever driver currently owns the message
  725. * @resources: for resource management when the spi message is processed
  726. *
  727. * A @spi_message is used to execute an atomic sequence of data transfers,
  728. * each represented by a struct spi_transfer. The sequence is "atomic"
  729. * in the sense that no other spi_message may use that SPI bus until that
  730. * sequence completes. On some systems, many such sequences can execute as
  731. * as single programmed DMA transfer. On all systems, these messages are
  732. * queued, and might complete after transactions to other devices. Messages
  733. * sent to a given spi_device are always executed in FIFO order.
  734. *
  735. * The code that submits an spi_message (and its spi_transfers)
  736. * to the lower layers is responsible for managing its memory.
  737. * Zero-initialize every field you don't set up explicitly, to
  738. * insulate against future API updates. After you submit a message
  739. * and its transfers, ignore them until its completion callback.
  740. */
  741. struct spi_message {
  742. struct list_head transfers;
  743. struct spi_device *spi;
  744. unsigned is_dma_mapped:1;
  745. /* REVISIT: we might want a flag affecting the behavior of the
  746. * last transfer ... allowing things like "read 16 bit length L"
  747. * immediately followed by "read L bytes". Basically imposing
  748. * a specific message scheduling algorithm.
  749. *
  750. * Some controller drivers (message-at-a-time queue processing)
  751. * could provide that as their default scheduling algorithm. But
  752. * others (with multi-message pipelines) could need a flag to
  753. * tell them about such special cases.
  754. */
  755. /* completion is reported through a callback */
  756. void (*complete)(void *context);
  757. void *context;
  758. unsigned frame_length;
  759. unsigned actual_length;
  760. int status;
  761. /* for optional use by whatever driver currently owns the
  762. * spi_message ... between calls to spi_async and then later
  763. * complete(), that's the spi_controller controller driver.
  764. */
  765. struct list_head queue;
  766. void *state;
  767. /* list of spi_res reources when the spi message is processed */
  768. struct list_head resources;
  769. };
  770. static inline void spi_message_init_no_memset(struct spi_message *m)
  771. {
  772. INIT_LIST_HEAD(&m->transfers);
  773. INIT_LIST_HEAD(&m->resources);
  774. }
  775. static inline void spi_message_init(struct spi_message *m)
  776. {
  777. memset(m, 0, sizeof *m);
  778. spi_message_init_no_memset(m);
  779. }
  780. static inline void
  781. spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)
  782. {
  783. list_add_tail(&t->transfer_list, &m->transfers);
  784. }
  785. static inline void
  786. spi_transfer_del(struct spi_transfer *t)
  787. {
  788. list_del(&t->transfer_list);
  789. }
  790. /**
  791. * spi_message_init_with_transfers - Initialize spi_message and append transfers
  792. * @m: spi_message to be initialized
  793. * @xfers: An array of spi transfers
  794. * @num_xfers: Number of items in the xfer array
  795. *
  796. * This function initializes the given spi_message and adds each spi_transfer in
  797. * the given array to the message.
  798. */
  799. static inline void
  800. spi_message_init_with_transfers(struct spi_message *m,
  801. struct spi_transfer *xfers, unsigned int num_xfers)
  802. {
  803. unsigned int i;
  804. spi_message_init(m);
  805. for (i = 0; i < num_xfers; ++i)
  806. spi_message_add_tail(&xfers[i], m);
  807. }
  808. /* It's fine to embed message and transaction structures in other data
  809. * structures so long as you don't free them while they're in use.
  810. */
  811. static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
  812. {
  813. struct spi_message *m;
  814. m = kzalloc(sizeof(struct spi_message)
  815. + ntrans * sizeof(struct spi_transfer),
  816. flags);
  817. if (m) {
  818. unsigned i;
  819. struct spi_transfer *t = (struct spi_transfer *)(m + 1);
  820. spi_message_init_no_memset(m);
  821. for (i = 0; i < ntrans; i++, t++)
  822. spi_message_add_tail(t, m);
  823. }
  824. return m;
  825. }
  826. static inline void spi_message_free(struct spi_message *m)
  827. {
  828. kfree(m);
  829. }
  830. extern int spi_setup(struct spi_device *spi);
  831. extern int spi_async(struct spi_device *spi, struct spi_message *message);
  832. extern int spi_async_locked(struct spi_device *spi,
  833. struct spi_message *message);
  834. extern int spi_slave_abort(struct spi_device *spi);
  835. static inline size_t
  836. spi_max_message_size(struct spi_device *spi)
  837. {
  838. struct spi_controller *ctlr = spi->controller;
  839. if (!ctlr->max_message_size)
  840. return SIZE_MAX;
  841. return ctlr->max_message_size(spi);
  842. }
  843. static inline size_t
  844. spi_max_transfer_size(struct spi_device *spi)
  845. {
  846. struct spi_controller *ctlr = spi->controller;
  847. size_t tr_max = SIZE_MAX;
  848. size_t msg_max = spi_max_message_size(spi);
  849. if (ctlr->max_transfer_size)
  850. tr_max = ctlr->max_transfer_size(spi);
  851. /* transfer size limit must not be greater than messsage size limit */
  852. return min(tr_max, msg_max);
  853. }
  854. /*---------------------------------------------------------------------------*/
  855. /* SPI transfer replacement methods which make use of spi_res */
  856. struct spi_replaced_transfers;
  857. typedef void (*spi_replaced_release_t)(struct spi_controller *ctlr,
  858. struct spi_message *msg,
  859. struct spi_replaced_transfers *res);
  860. /**
  861. * struct spi_replaced_transfers - structure describing the spi_transfer
  862. * replacements that have occurred
  863. * so that they can get reverted
  864. * @release: some extra release code to get executed prior to
  865. * relasing this structure
  866. * @extradata: pointer to some extra data if requested or NULL
  867. * @replaced_transfers: transfers that have been replaced and which need
  868. * to get restored
  869. * @replaced_after: the transfer after which the @replaced_transfers
  870. * are to get re-inserted
  871. * @inserted: number of transfers inserted
  872. * @inserted_transfers: array of spi_transfers of array-size @inserted,
  873. * that have been replacing replaced_transfers
  874. *
  875. * note: that @extradata will point to @inserted_transfers[@inserted]
  876. * if some extra allocation is requested, so alignment will be the same
  877. * as for spi_transfers
  878. */
  879. struct spi_replaced_transfers {
  880. spi_replaced_release_t release;
  881. void *extradata;
  882. struct list_head replaced_transfers;
  883. struct list_head *replaced_after;
  884. size_t inserted;
  885. struct spi_transfer inserted_transfers[];
  886. };
  887. extern struct spi_replaced_transfers *spi_replace_transfers(
  888. struct spi_message *msg,
  889. struct spi_transfer *xfer_first,
  890. size_t remove,
  891. size_t insert,
  892. spi_replaced_release_t release,
  893. size_t extradatasize,
  894. gfp_t gfp);
  895. /*---------------------------------------------------------------------------*/
  896. /* SPI transfer transformation methods */
  897. extern int spi_split_transfers_maxsize(struct spi_controller *ctlr,
  898. struct spi_message *msg,
  899. size_t maxsize,
  900. gfp_t gfp);
  901. /*---------------------------------------------------------------------------*/
  902. /* All these synchronous SPI transfer routines are utilities layered
  903. * over the core async transfer primitive. Here, "synchronous" means
  904. * they will sleep uninterruptibly until the async transfer completes.
  905. */
  906. extern int spi_sync(struct spi_device *spi, struct spi_message *message);
  907. extern int spi_sync_locked(struct spi_device *spi, struct spi_message *message);
  908. extern int spi_bus_lock(struct spi_controller *ctlr);
  909. extern int spi_bus_unlock(struct spi_controller *ctlr);
  910. /**
  911. * spi_sync_transfer - synchronous SPI data transfer
  912. * @spi: device with which data will be exchanged
  913. * @xfers: An array of spi_transfers
  914. * @num_xfers: Number of items in the xfer array
  915. * Context: can sleep
  916. *
  917. * Does a synchronous SPI data transfer of the given spi_transfer array.
  918. *
  919. * For more specific semantics see spi_sync().
  920. *
  921. * Return: Return: zero on success, else a negative error code.
  922. */
  923. static inline int
  924. spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
  925. unsigned int num_xfers)
  926. {
  927. struct spi_message msg;
  928. spi_message_init_with_transfers(&msg, xfers, num_xfers);
  929. return spi_sync(spi, &msg);
  930. }
  931. /**
  932. * spi_write - SPI synchronous write
  933. * @spi: device to which data will be written
  934. * @buf: data buffer
  935. * @len: data buffer size
  936. * Context: can sleep
  937. *
  938. * This function writes the buffer @buf.
  939. * Callable only from contexts that can sleep.
  940. *
  941. * Return: zero on success, else a negative error code.
  942. */
  943. static inline int
  944. spi_write(struct spi_device *spi, const void *buf, size_t len)
  945. {
  946. struct spi_transfer t = {
  947. .tx_buf = buf,
  948. .len = len,
  949. };
  950. return spi_sync_transfer(spi, &t, 1);
  951. }
  952. /**
  953. * spi_read - SPI synchronous read
  954. * @spi: device from which data will be read
  955. * @buf: data buffer
  956. * @len: data buffer size
  957. * Context: can sleep
  958. *
  959. * This function reads the buffer @buf.
  960. * Callable only from contexts that can sleep.
  961. *
  962. * Return: zero on success, else a negative error code.
  963. */
  964. static inline int
  965. spi_read(struct spi_device *spi, void *buf, size_t len)
  966. {
  967. struct spi_transfer t = {
  968. .rx_buf = buf,
  969. .len = len,
  970. };
  971. return spi_sync_transfer(spi, &t, 1);
  972. }
  973. /* this copies txbuf and rxbuf data; for small transfers only! */
  974. extern int spi_write_then_read(struct spi_device *spi,
  975. const void *txbuf, unsigned n_tx,
  976. void *rxbuf, unsigned n_rx);
  977. /**
  978. * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
  979. * @spi: device with which data will be exchanged
  980. * @cmd: command to be written before data is read back
  981. * Context: can sleep
  982. *
  983. * Callable only from contexts that can sleep.
  984. *
  985. * Return: the (unsigned) eight bit number returned by the
  986. * device, or else a negative error code.
  987. */
  988. static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
  989. {
  990. ssize_t status;
  991. u8 result;
  992. status = spi_write_then_read(spi, &cmd, 1, &result, 1);
  993. /* return negative errno or unsigned value */
  994. return (status < 0) ? status : result;
  995. }
  996. /**
  997. * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
  998. * @spi: device with which data will be exchanged
  999. * @cmd: command to be written before data is read back
  1000. * Context: can sleep
  1001. *
  1002. * The number is returned in wire-order, which is at least sometimes
  1003. * big-endian.
  1004. *
  1005. * Callable only from contexts that can sleep.
  1006. *
  1007. * Return: the (unsigned) sixteen bit number returned by the
  1008. * device, or else a negative error code.
  1009. */
  1010. static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
  1011. {
  1012. ssize_t status;
  1013. u16 result;
  1014. status = spi_write_then_read(spi, &cmd, 1, &result, 2);
  1015. /* return negative errno or unsigned value */
  1016. return (status < 0) ? status : result;
  1017. }
  1018. /**
  1019. * spi_w8r16be - SPI synchronous 8 bit write followed by 16 bit big-endian read
  1020. * @spi: device with which data will be exchanged
  1021. * @cmd: command to be written before data is read back
  1022. * Context: can sleep
  1023. *
  1024. * This function is similar to spi_w8r16, with the exception that it will
  1025. * convert the read 16 bit data word from big-endian to native endianness.
  1026. *
  1027. * Callable only from contexts that can sleep.
  1028. *
  1029. * Return: the (unsigned) sixteen bit number returned by the device in cpu
  1030. * endianness, or else a negative error code.
  1031. */
  1032. static inline ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd)
  1033. {
  1034. ssize_t status;
  1035. __be16 result;
  1036. status = spi_write_then_read(spi, &cmd, 1, &result, 2);
  1037. if (status < 0)
  1038. return status;
  1039. return be16_to_cpu(result);
  1040. }
  1041. /*---------------------------------------------------------------------------*/
  1042. /*
  1043. * INTERFACE between board init code and SPI infrastructure.
  1044. *
  1045. * No SPI driver ever sees these SPI device table segments, but
  1046. * it's how the SPI core (or adapters that get hotplugged) grows
  1047. * the driver model tree.
  1048. *
  1049. * As a rule, SPI devices can't be probed. Instead, board init code
  1050. * provides a table listing the devices which are present, with enough
  1051. * information to bind and set up the device's driver. There's basic
  1052. * support for nonstatic configurations too; enough to handle adding
  1053. * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
  1054. */
  1055. /**
  1056. * struct spi_board_info - board-specific template for a SPI device
  1057. * @modalias: Initializes spi_device.modalias; identifies the driver.
  1058. * @platform_data: Initializes spi_device.platform_data; the particular
  1059. * data stored there is driver-specific.
  1060. * @properties: Additional device properties for the device.
  1061. * @controller_data: Initializes spi_device.controller_data; some
  1062. * controllers need hints about hardware setup, e.g. for DMA.
  1063. * @irq: Initializes spi_device.irq; depends on how the board is wired.
  1064. * @max_speed_hz: Initializes spi_device.max_speed_hz; based on limits
  1065. * from the chip datasheet and board-specific signal quality issues.
  1066. * @bus_num: Identifies which spi_controller parents the spi_device; unused
  1067. * by spi_new_device(), and otherwise depends on board wiring.
  1068. * @chip_select: Initializes spi_device.chip_select; depends on how
  1069. * the board is wired.
  1070. * @mode: Initializes spi_device.mode; based on the chip datasheet, board
  1071. * wiring (some devices support both 3WIRE and standard modes), and
  1072. * possibly presence of an inverter in the chipselect path.
  1073. *
  1074. * When adding new SPI devices to the device tree, these structures serve
  1075. * as a partial device template. They hold information which can't always
  1076. * be determined by drivers. Information that probe() can establish (such
  1077. * as the default transfer wordsize) is not included here.
  1078. *
  1079. * These structures are used in two places. Their primary role is to
  1080. * be stored in tables of board-specific device descriptors, which are
  1081. * declared early in board initialization and then used (much later) to
  1082. * populate a controller's device tree after the that controller's driver
  1083. * initializes. A secondary (and atypical) role is as a parameter to
  1084. * spi_new_device() call, which happens after those controller drivers
  1085. * are active in some dynamic board configuration models.
  1086. */
  1087. struct spi_board_info {
  1088. /* the device name and module name are coupled, like platform_bus;
  1089. * "modalias" is normally the driver name.
  1090. *
  1091. * platform_data goes to spi_device.dev.platform_data,
  1092. * controller_data goes to spi_device.controller_data,
  1093. * device properties are copied and attached to spi_device,
  1094. * irq is copied too
  1095. */
  1096. char modalias[SPI_NAME_SIZE];
  1097. const void *platform_data;
  1098. const struct property_entry *properties;
  1099. void *controller_data;
  1100. int irq;
  1101. /* slower signaling on noisy or low voltage boards */
  1102. u32 max_speed_hz;
  1103. /* bus_num is board specific and matches the bus_num of some
  1104. * spi_controller that will probably be registered later.
  1105. *
  1106. * chip_select reflects how this chip is wired to that master;
  1107. * it's less than num_chipselect.
  1108. */
  1109. u16 bus_num;
  1110. u16 chip_select;
  1111. /* mode becomes spi_device.mode, and is essential for chips
  1112. * where the default of SPI_CS_HIGH = 0 is wrong.
  1113. */
  1114. u16 mode;
  1115. /* ... may need additional spi_device chip config data here.
  1116. * avoid stuff protocol drivers can set; but include stuff
  1117. * needed to behave without being bound to a driver:
  1118. * - quirks like clock rate mattering when not selected
  1119. */
  1120. };
  1121. #ifdef CONFIG_SPI
  1122. extern int
  1123. spi_register_board_info(struct spi_board_info const *info, unsigned n);
  1124. #else
  1125. /* board init code may ignore whether SPI is configured or not */
  1126. static inline int
  1127. spi_register_board_info(struct spi_board_info const *info, unsigned n)
  1128. { return 0; }
  1129. #endif
  1130. /* If you're hotplugging an adapter with devices (parport, usb, etc)
  1131. * use spi_new_device() to describe each device. You can also call
  1132. * spi_unregister_device() to start making that device vanish, but
  1133. * normally that would be handled by spi_unregister_controller().
  1134. *
  1135. * You can also use spi_alloc_device() and spi_add_device() to use a two
  1136. * stage registration sequence for each spi_device. This gives the caller
  1137. * some more control over the spi_device structure before it is registered,
  1138. * but requires that caller to initialize fields that would otherwise
  1139. * be defined using the board info.
  1140. */
  1141. extern struct spi_device *
  1142. spi_alloc_device(struct spi_controller *ctlr);
  1143. extern int
  1144. spi_add_device(struct spi_device *spi);
  1145. extern struct spi_device *
  1146. spi_new_device(struct spi_controller *, struct spi_board_info *);
  1147. extern void spi_unregister_device(struct spi_device *spi);
  1148. extern const struct spi_device_id *
  1149. spi_get_device_id(const struct spi_device *sdev);
  1150. static inline bool
  1151. spi_transfer_is_last(struct spi_controller *ctlr, struct spi_transfer *xfer)
  1152. {
  1153. return list_is_last(&xfer->transfer_list, &ctlr->cur_msg->transfers);
  1154. }
  1155. /* OF support code */
  1156. #if IS_ENABLED(CONFIG_OF)
  1157. /* must call put_device() when done with returned spi_device device */
  1158. extern struct spi_device *
  1159. of_find_spi_device_by_node(struct device_node *node);
  1160. #else
  1161. static inline struct spi_device *
  1162. of_find_spi_device_by_node(struct device_node *node)
  1163. {
  1164. return NULL;
  1165. }
  1166. #endif /* IS_ENABLED(CONFIG_OF) */
  1167. /* Compatibility layer */
  1168. #define spi_master spi_controller
  1169. #define SPI_MASTER_HALF_DUPLEX SPI_CONTROLLER_HALF_DUPLEX
  1170. #define SPI_MASTER_NO_RX SPI_CONTROLLER_NO_RX
  1171. #define SPI_MASTER_NO_TX SPI_CONTROLLER_NO_TX
  1172. #define SPI_MASTER_MUST_RX SPI_CONTROLLER_MUST_RX
  1173. #define SPI_MASTER_MUST_TX SPI_CONTROLLER_MUST_TX
  1174. #define spi_master_get_devdata(_ctlr) spi_controller_get_devdata(_ctlr)
  1175. #define spi_master_set_devdata(_ctlr, _data) \
  1176. spi_controller_set_devdata(_ctlr, _data)
  1177. #define spi_master_get(_ctlr) spi_controller_get(_ctlr)
  1178. #define spi_master_put(_ctlr) spi_controller_put(_ctlr)
  1179. #define spi_master_suspend(_ctlr) spi_controller_suspend(_ctlr)
  1180. #define spi_master_resume(_ctlr) spi_controller_resume(_ctlr)
  1181. #define spi_register_master(_ctlr) spi_register_controller(_ctlr)
  1182. #define devm_spi_register_master(_dev, _ctlr) \
  1183. devm_spi_register_controller(_dev, _ctlr)
  1184. #define spi_unregister_master(_ctlr) spi_unregister_controller(_ctlr)
  1185. #endif /* __LINUX_SPI_H */