spi.h 48 KB

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