spi.h 50 KB

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