spi.h 47 KB

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