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