spi.h 47 KB

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