spi.h 39 KB

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