spi.h 50 KB

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