spi.h 39 KB

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