spi.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifndef __LINUX_SPI_H
  19. #define __LINUX_SPI_H
  20. /*
  21. * INTERFACES between SPI master-side drivers and SPI infrastructure.
  22. * (There's no SPI slave support for Linux yet...)
  23. */
  24. extern struct bus_type spi_bus_type;
  25. /**
  26. * struct spi_device - Master side proxy for an SPI slave device
  27. * @dev: Driver model representation of the device.
  28. * @master: SPI controller used with the device.
  29. * @max_speed_hz: Maximum clock rate to be used with this chip
  30. * (on this board); may be changed by the device's driver.
  31. * @chip-select: Chipselect, distinguishing chips handled by "master".
  32. * @mode: The spi mode defines how data is clocked out and in.
  33. * This may be changed by the device's driver.
  34. * @bits_per_word: Data transfers involve one or more words; word sizes
  35. * like eight or 12 bits are common. In-memory wordsizes are
  36. * powers of two bytes (e.g. 20 bit samples use 32 bits).
  37. * This may be changed by the device's driver.
  38. * @irq: Negative, or the number passed to request_irq() to receive
  39. * interrupts from this device.
  40. * @controller_state: Controller's runtime state
  41. * @controller_data: Board-specific definitions for controller, such as
  42. * FIFO initialization parameters; from board_info.controller_data
  43. *
  44. * An spi_device is used to interchange data between an SPI slave
  45. * (usually a discrete chip) and CPU memory.
  46. *
  47. * In "dev", the platform_data is used to hold information about this
  48. * device that's meaningful to the device's protocol driver, but not
  49. * to its controller. One example might be an identifier for a chip
  50. * variant with slightly different functionality.
  51. */
  52. struct spi_device {
  53. struct device dev;
  54. struct spi_master *master;
  55. u32 max_speed_hz;
  56. u8 chip_select;
  57. u8 mode;
  58. #define SPI_CPHA 0x01 /* clock phase */
  59. #define SPI_CPOL 0x02 /* clock polarity */
  60. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  61. #define SPI_MODE_1 (0|SPI_CPHA)
  62. #define SPI_MODE_2 (SPI_CPOL|0)
  63. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  64. #define SPI_CS_HIGH 0x04 /* chipselect active high? */
  65. u8 bits_per_word;
  66. int irq;
  67. void *controller_state;
  68. void *controller_data;
  69. const char *modalias;
  70. // likely need more hooks for more protocol options affecting how
  71. // the controller talks to each chip, like:
  72. // - bit order (default is wordwise msb-first)
  73. // - memory packing (12 bit samples into low bits, others zeroed)
  74. // - priority
  75. // - drop chipselect after each word
  76. // - chipselect delays
  77. // - ...
  78. };
  79. static inline struct spi_device *to_spi_device(struct device *dev)
  80. {
  81. return dev ? container_of(dev, struct spi_device, dev) : NULL;
  82. }
  83. /* most drivers won't need to care about device refcounting */
  84. static inline struct spi_device *spi_dev_get(struct spi_device *spi)
  85. {
  86. return (spi && get_device(&spi->dev)) ? spi : NULL;
  87. }
  88. static inline void spi_dev_put(struct spi_device *spi)
  89. {
  90. if (spi)
  91. put_device(&spi->dev);
  92. }
  93. /* ctldata is for the bus_master driver's runtime state */
  94. static inline void *spi_get_ctldata(struct spi_device *spi)
  95. {
  96. return spi->controller_state;
  97. }
  98. static inline void spi_set_ctldata(struct spi_device *spi, void *state)
  99. {
  100. spi->controller_state = state;
  101. }
  102. struct spi_message;
  103. struct spi_driver {
  104. int (*probe)(struct spi_device *spi);
  105. int (*remove)(struct spi_device *spi);
  106. void (*shutdown)(struct spi_device *spi);
  107. int (*suspend)(struct spi_device *spi, pm_message_t mesg);
  108. int (*resume)(struct spi_device *spi);
  109. struct device_driver driver;
  110. };
  111. static inline struct spi_driver *to_spi_driver(struct device_driver *drv)
  112. {
  113. return drv ? container_of(drv, struct spi_driver, driver) : NULL;
  114. }
  115. extern int spi_register_driver(struct spi_driver *sdrv);
  116. static inline void spi_unregister_driver(struct spi_driver *sdrv)
  117. {
  118. if (!sdrv)
  119. return;
  120. driver_unregister(&sdrv->driver);
  121. }
  122. /**
  123. * struct spi_master - interface to SPI master controller
  124. * @cdev: class interface to this driver
  125. * @bus_num: board-specific (and often SOC-specific) identifier for a
  126. * given SPI controller.
  127. * @num_chipselect: chipselects are used to distinguish individual
  128. * SPI slaves, and are numbered from zero to num_chipselects.
  129. * each slave has a chipselect signal, but it's common that not
  130. * every chipselect is connected to a slave.
  131. * @setup: updates the device mode and clocking records used by a
  132. * device's SPI controller; protocol code may call this.
  133. * @transfer: adds a message to the controller's transfer queue.
  134. * @cleanup: frees controller-specific state
  135. *
  136. * Each SPI master controller can communicate with one or more spi_device
  137. * children. These make a small bus, sharing MOSI, MISO and SCK signals
  138. * but not chip select signals. Each device may be configured to use a
  139. * different clock rate, since those shared signals are ignored unless
  140. * the chip is selected.
  141. *
  142. * The driver for an SPI controller manages access to those devices through
  143. * a queue of spi_message transactions, copyin data between CPU memory and
  144. * an SPI slave device). For each such message it queues, it calls the
  145. * message's completion function when the transaction completes.
  146. */
  147. struct spi_master {
  148. struct class_device cdev;
  149. /* other than zero (== assign one dynamically), bus_num is fully
  150. * board-specific. usually that simplifies to being SOC-specific.
  151. * example: one SOC has three SPI controllers, numbered 1..3,
  152. * and one board's schematics might show it using SPI-2. software
  153. * would normally use bus_num=2 for that controller.
  154. */
  155. u16 bus_num;
  156. /* chipselects will be integral to many controllers; some others
  157. * might use board-specific GPIOs.
  158. */
  159. u16 num_chipselect;
  160. /* setup mode and clock, etc (spi driver may call many times) */
  161. int (*setup)(struct spi_device *spi);
  162. /* bidirectional bulk transfers
  163. *
  164. * + The transfer() method may not sleep; its main role is
  165. * just to add the message to the queue.
  166. * + For now there's no remove-from-queue operation, or
  167. * any other request management
  168. * + To a given spi_device, message queueing is pure fifo
  169. *
  170. * + The master's main job is to process its message queue,
  171. * selecting a chip then transferring data
  172. * + If there are multiple spi_device children, the i/o queue
  173. * arbitration algorithm is unspecified (round robin, fifo,
  174. * priority, reservations, preemption, etc)
  175. *
  176. * + Chipselect stays active during the entire message
  177. * (unless modified by spi_transfer.cs_change != 0).
  178. * + The message transfers use clock and SPI mode parameters
  179. * previously established by setup() for this device
  180. */
  181. int (*transfer)(struct spi_device *spi,
  182. struct spi_message *mesg);
  183. /* called on release() to free memory provided by spi_master */
  184. void (*cleanup)(const struct spi_device *spi);
  185. };
  186. static inline void *spi_master_get_devdata(struct spi_master *master)
  187. {
  188. return class_get_devdata(&master->cdev);
  189. }
  190. static inline void spi_master_set_devdata(struct spi_master *master, void *data)
  191. {
  192. class_set_devdata(&master->cdev, data);
  193. }
  194. static inline struct spi_master *spi_master_get(struct spi_master *master)
  195. {
  196. if (!master || !class_device_get(&master->cdev))
  197. return NULL;
  198. return master;
  199. }
  200. static inline void spi_master_put(struct spi_master *master)
  201. {
  202. if (master)
  203. class_device_put(&master->cdev);
  204. }
  205. /* the spi driver core manages memory for the spi_master classdev */
  206. extern struct spi_master *
  207. spi_alloc_master(struct device *host, unsigned size);
  208. extern int spi_register_master(struct spi_master *master);
  209. extern void spi_unregister_master(struct spi_master *master);
  210. extern struct spi_master *spi_busnum_to_master(u16 busnum);
  211. /*---------------------------------------------------------------------------*/
  212. /*
  213. * I/O INTERFACE between SPI controller and protocol drivers
  214. *
  215. * Protocol drivers use a queue of spi_messages, each transferring data
  216. * between the controller and memory buffers.
  217. *
  218. * The spi_messages themselves consist of a series of read+write transfer
  219. * segments. Those segments always read the same number of bits as they
  220. * write; but one or the other is easily ignored by passing a null buffer
  221. * pointer. (This is unlike most types of I/O API, because SPI hardware
  222. * is full duplex.)
  223. *
  224. * NOTE: Allocation of spi_transfer and spi_message memory is entirely
  225. * up to the protocol driver, which guarantees the integrity of both (as
  226. * well as the data buffers) for as long as the message is queued.
  227. */
  228. /**
  229. * struct spi_transfer - a read/write buffer pair
  230. * @tx_buf: data to be written (dma-safe address), or NULL
  231. * @rx_buf: data to be read (dma-safe address), or NULL
  232. * @tx_dma: DMA address of buffer, if spi_message.is_dma_mapped
  233. * @rx_dma: DMA address of buffer, if spi_message.is_dma_mapped
  234. * @len: size of rx and tx buffers (in bytes)
  235. * @cs_change: affects chipselect after this transfer completes
  236. * @delay_usecs: microseconds to delay after this transfer before
  237. * (optionally) changing the chipselect status, then starting
  238. * the next transfer or completing this spi_message.
  239. *
  240. * SPI transfers always write the same number of bytes as they read.
  241. * Protocol drivers should always provide rx_buf and/or tx_buf.
  242. * In some cases, they may also want to provide DMA addresses for
  243. * the data being transferred; that may reduce overhead, when the
  244. * underlying driver uses dma.
  245. *
  246. * All SPI transfers start with the relevant chipselect active. Drivers
  247. * can change behavior of the chipselect after the transfer finishes
  248. * (including any mandatory delay). The normal behavior is to leave it
  249. * selected, except for the last transfer in a message. Setting cs_change
  250. * allows two additional behavior options:
  251. *
  252. * (i) If the transfer isn't the last one in the message, this flag is
  253. * used to make the chipselect briefly go inactive in the middle of the
  254. * message. Toggling chipselect in this way may be needed to terminate
  255. * a chip command, letting a single spi_message perform all of group of
  256. * chip transactions together.
  257. *
  258. * (ii) When the transfer is the last one in the message, the chip may
  259. * stay selected until the next transfer. This is purely a performance
  260. * hint; the controller driver may need to select a different device
  261. * for the next message.
  262. *
  263. * The code that submits an spi_message (and its spi_transfers)
  264. * to the lower layers is responsible for managing its memory.
  265. * Zero-initialize every field you don't set up explicitly, to
  266. * insulate against future API updates.
  267. */
  268. struct spi_transfer {
  269. /* it's ok if tx_buf == rx_buf (right?)
  270. * for MicroWire, one buffer must be null
  271. * buffers must work with dma_*map_single() calls, unless
  272. * spi_message.is_dma_mapped reports a pre-existing mapping
  273. */
  274. const void *tx_buf;
  275. void *rx_buf;
  276. unsigned len;
  277. dma_addr_t tx_dma;
  278. dma_addr_t rx_dma;
  279. unsigned cs_change:1;
  280. u16 delay_usecs;
  281. };
  282. /**
  283. * struct spi_message - one multi-segment SPI transaction
  284. * @transfers: the segements of the transaction
  285. * @n_transfer: how many segments
  286. * @spi: SPI device to which the transaction is queued
  287. * @is_dma_mapped: if true, the caller provided both dma and cpu virtual
  288. * addresses for each transfer buffer
  289. * @complete: called to report transaction completions
  290. * @context: the argument to complete() when it's called
  291. * @actual_length: the total number of bytes that were transferred in all
  292. * successful segments
  293. * @status: zero for success, else negative errno
  294. * @queue: for use by whichever driver currently owns the message
  295. * @state: for use by whichever driver currently owns the message
  296. *
  297. * The code that submits an spi_message (and its spi_transfers)
  298. * to the lower layers is responsible for managing its memory.
  299. * Zero-initialize every field you don't set up explicitly, to
  300. * insulate against future API updates.
  301. */
  302. struct spi_message {
  303. struct spi_transfer *transfers;
  304. unsigned n_transfer;
  305. struct spi_device *spi;
  306. unsigned is_dma_mapped:1;
  307. /* REVISIT: we might want a flag affecting the behavior of the
  308. * last transfer ... allowing things like "read 16 bit length L"
  309. * immediately followed by "read L bytes". Basically imposing
  310. * a specific message scheduling algorithm.
  311. *
  312. * Some controller drivers (message-at-a-time queue processing)
  313. * could provide that as their default scheduling algorithm. But
  314. * others (with multi-message pipelines) could need a flag to
  315. * tell them about such special cases.
  316. */
  317. /* completion is reported through a callback */
  318. void FASTCALL((*complete)(void *context));
  319. void *context;
  320. unsigned actual_length;
  321. int status;
  322. /* for optional use by whatever driver currently owns the
  323. * spi_message ... between calls to spi_async and then later
  324. * complete(), that's the spi_master controller driver.
  325. */
  326. struct list_head queue;
  327. void *state;
  328. };
  329. /* It's fine to embed message and transaction structures in other data
  330. * structures so long as you don't free them while they're in use.
  331. */
  332. static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags)
  333. {
  334. struct spi_message *m;
  335. m = kzalloc(sizeof(struct spi_message)
  336. + ntrans * sizeof(struct spi_transfer),
  337. flags);
  338. if (m) {
  339. m->transfers = (void *)(m + 1);
  340. m->n_transfer = ntrans;
  341. }
  342. return m;
  343. }
  344. static inline void spi_message_free(struct spi_message *m)
  345. {
  346. kfree(m);
  347. }
  348. /**
  349. * spi_setup -- setup SPI mode and clock rate
  350. * @spi: the device whose settings are being modified
  351. *
  352. * SPI protocol drivers may need to update the transfer mode if the
  353. * device doesn't work with the mode 0 default. They may likewise need
  354. * to update clock rates or word sizes from initial values. This function
  355. * changes those settings, and must be called from a context that can sleep.
  356. */
  357. static inline int
  358. spi_setup(struct spi_device *spi)
  359. {
  360. return spi->master->setup(spi);
  361. }
  362. /**
  363. * spi_async -- asynchronous SPI transfer
  364. * @spi: device with which data will be exchanged
  365. * @message: describes the data transfers, including completion callback
  366. *
  367. * This call may be used in_irq and other contexts which can't sleep,
  368. * as well as from task contexts which can sleep.
  369. *
  370. * The completion callback is invoked in a context which can't sleep.
  371. * Before that invocation, the value of message->status is undefined.
  372. * When the callback is issued, message->status holds either zero (to
  373. * indicate complete success) or a negative error code. After that
  374. * callback returns, the driver which issued the transfer request may
  375. * deallocate the associated memory; it's no longer in use by any SPI
  376. * core or controller driver code.
  377. *
  378. * Note that although all messages to a spi_device are handled in
  379. * FIFO order, messages may go to different devices in other orders.
  380. * Some device might be higher priority, or have various "hard" access
  381. * time requirements, for example.
  382. *
  383. * On detection of any fault during the transfer, processing of
  384. * the entire message is aborted, and the device is deselected.
  385. * Until returning from the associated message completion callback,
  386. * no other spi_message queued to that device will be processed.
  387. * (This rule applies equally to all the synchronous transfer calls,
  388. * which are wrappers around this core asynchronous primitive.)
  389. */
  390. static inline int
  391. spi_async(struct spi_device *spi, struct spi_message *message)
  392. {
  393. message->spi = spi;
  394. return spi->master->transfer(spi, message);
  395. }
  396. /*---------------------------------------------------------------------------*/
  397. /* All these synchronous SPI transfer routines are utilities layered
  398. * over the core async transfer primitive. Here, "synchronous" means
  399. * they will sleep uninterruptibly until the async transfer completes.
  400. */
  401. extern int spi_sync(struct spi_device *spi, struct spi_message *message);
  402. /**
  403. * spi_write - SPI synchronous write
  404. * @spi: device to which data will be written
  405. * @buf: data buffer
  406. * @len: data buffer size
  407. *
  408. * This writes the buffer and returns zero or a negative error code.
  409. * Callable only from contexts that can sleep.
  410. */
  411. static inline int
  412. spi_write(struct spi_device *spi, const u8 *buf, size_t len)
  413. {
  414. struct spi_transfer t = {
  415. .tx_buf = buf,
  416. .rx_buf = NULL,
  417. .len = len,
  418. .cs_change = 0,
  419. };
  420. struct spi_message m = {
  421. .transfers = &t,
  422. .n_transfer = 1,
  423. };
  424. return spi_sync(spi, &m);
  425. }
  426. /**
  427. * spi_read - SPI synchronous read
  428. * @spi: device from which data will be read
  429. * @buf: data buffer
  430. * @len: data buffer size
  431. *
  432. * This writes the buffer and returns zero or a negative error code.
  433. * Callable only from contexts that can sleep.
  434. */
  435. static inline int
  436. spi_read(struct spi_device *spi, u8 *buf, size_t len)
  437. {
  438. struct spi_transfer t = {
  439. .tx_buf = NULL,
  440. .rx_buf = buf,
  441. .len = len,
  442. .cs_change = 0,
  443. };
  444. struct spi_message m = {
  445. .transfers = &t,
  446. .n_transfer = 1,
  447. };
  448. return spi_sync(spi, &m);
  449. }
  450. /* this copies txbuf and rxbuf data; for small transfers only! */
  451. extern int spi_write_then_read(struct spi_device *spi,
  452. const u8 *txbuf, unsigned n_tx,
  453. u8 *rxbuf, unsigned n_rx);
  454. /**
  455. * spi_w8r8 - SPI synchronous 8 bit write followed by 8 bit read
  456. * @spi: device with which data will be exchanged
  457. * @cmd: command to be written before data is read back
  458. *
  459. * This returns the (unsigned) eight bit number returned by the
  460. * device, or else a negative error code. Callable only from
  461. * contexts that can sleep.
  462. */
  463. static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
  464. {
  465. ssize_t status;
  466. u8 result;
  467. status = spi_write_then_read(spi, &cmd, 1, &result, 1);
  468. /* return negative errno or unsigned value */
  469. return (status < 0) ? status : result;
  470. }
  471. /**
  472. * spi_w8r16 - SPI synchronous 8 bit write followed by 16 bit read
  473. * @spi: device with which data will be exchanged
  474. * @cmd: command to be written before data is read back
  475. *
  476. * This returns the (unsigned) sixteen bit number returned by the
  477. * device, or else a negative error code. Callable only from
  478. * contexts that can sleep.
  479. *
  480. * The number is returned in wire-order, which is at least sometimes
  481. * big-endian.
  482. */
  483. static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
  484. {
  485. ssize_t status;
  486. u16 result;
  487. status = spi_write_then_read(spi, &cmd, 1, (u8 *) &result, 2);
  488. /* return negative errno or unsigned value */
  489. return (status < 0) ? status : result;
  490. }
  491. /*---------------------------------------------------------------------------*/
  492. /*
  493. * INTERFACE between board init code and SPI infrastructure.
  494. *
  495. * No SPI driver ever sees these SPI device table segments, but
  496. * it's how the SPI core (or adapters that get hotplugged) grows
  497. * the driver model tree.
  498. *
  499. * As a rule, SPI devices can't be probed. Instead, board init code
  500. * provides a table listing the devices which are present, with enough
  501. * information to bind and set up the device's driver. There's basic
  502. * support for nonstatic configurations too; enough to handle adding
  503. * parport adapters, or microcontrollers acting as USB-to-SPI bridges.
  504. */
  505. /* board-specific information about each SPI device */
  506. struct spi_board_info {
  507. /* the device name and module name are coupled, like platform_bus;
  508. * "modalias" is normally the driver name.
  509. *
  510. * platform_data goes to spi_device.dev.platform_data,
  511. * controller_data goes to spi_device.controller_data,
  512. * irq is copied too
  513. */
  514. char modalias[KOBJ_NAME_LEN];
  515. const void *platform_data;
  516. void *controller_data;
  517. int irq;
  518. /* slower signaling on noisy or low voltage boards */
  519. u32 max_speed_hz;
  520. /* bus_num is board specific and matches the bus_num of some
  521. * spi_master that will probably be registered later.
  522. *
  523. * chip_select reflects how this chip is wired to that master;
  524. * it's less than num_chipselect.
  525. */
  526. u16 bus_num;
  527. u16 chip_select;
  528. /* ... may need additional spi_device chip config data here.
  529. * avoid stuff protocol drivers can set; but include stuff
  530. * needed to behave without being bound to a driver:
  531. * - chipselect polarity
  532. * - quirks like clock rate mattering when not selected
  533. */
  534. };
  535. #ifdef CONFIG_SPI
  536. extern int
  537. spi_register_board_info(struct spi_board_info const *info, unsigned n);
  538. #else
  539. /* board init code may ignore whether SPI is configured or not */
  540. static inline int
  541. spi_register_board_info(struct spi_board_info const *info, unsigned n)
  542. { return 0; }
  543. #endif
  544. /* If you're hotplugging an adapter with devices (parport, usb, etc)
  545. * use spi_new_device() to describe each device. You can also call
  546. * spi_unregister_device() to start making that device vanish, but
  547. * normally that would be handled by spi_unregister_master().
  548. */
  549. extern struct spi_device *
  550. spi_new_device(struct spi_master *, struct spi_board_info *);
  551. static inline void
  552. spi_unregister_device(struct spi_device *spi)
  553. {
  554. if (spi)
  555. device_unregister(&spi->dev);
  556. }
  557. #endif /* __LINUX_SPI_H */