soundbus.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * soundbus generic definitions
  4. *
  5. * Copyright 2006 Johannes Berg <[email protected]>
  6. */
  7. #ifndef __SOUNDBUS_H
  8. #define __SOUNDBUS_H
  9. #include <linux/of_device.h>
  10. #include <sound/pcm.h>
  11. #include <linux/list.h>
  12. /* When switching from master to slave or the other way around,
  13. * you don't want to have the codec chip acting as clock source
  14. * while the bus still is.
  15. * More importantly, while switch from slave to master, you need
  16. * to turn off the chip's master function first, but then there's
  17. * no clock for a while and other chips might reset, so we notify
  18. * their drivers after having switched.
  19. * The constants here are codec-point of view, so when we switch
  20. * the soundbus to master we tell the codec we're going to switch
  21. * and give it CLOCK_SWITCH_PREPARE_SLAVE!
  22. */
  23. enum clock_switch {
  24. CLOCK_SWITCH_PREPARE_SLAVE,
  25. CLOCK_SWITCH_PREPARE_MASTER,
  26. CLOCK_SWITCH_SLAVE,
  27. CLOCK_SWITCH_MASTER,
  28. CLOCK_SWITCH_NOTIFY,
  29. };
  30. /* information on a transfer the codec can take */
  31. struct transfer_info {
  32. u64 formats; /* SNDRV_PCM_FMTBIT_* */
  33. unsigned int rates; /* SNDRV_PCM_RATE_* */
  34. /* flags */
  35. u32 transfer_in:1, /* input = 1, output = 0 */
  36. must_be_clock_source:1;
  37. /* for codecs to distinguish among their TIs */
  38. int tag;
  39. };
  40. struct codec_info_item {
  41. struct codec_info *codec;
  42. void *codec_data;
  43. struct soundbus_dev *sdev;
  44. /* internal, to be used by the soundbus provider */
  45. struct list_head list;
  46. };
  47. /* for prepare, where the codecs need to know
  48. * what we're going to drive the bus with */
  49. struct bus_info {
  50. /* see below */
  51. int sysclock_factor;
  52. int bus_factor;
  53. };
  54. /* information on the codec itself, plus function pointers */
  55. struct codec_info {
  56. /* the module this lives in */
  57. struct module *owner;
  58. /* supported transfer possibilities, array terminated by
  59. * formats or rates being 0. */
  60. struct transfer_info *transfers;
  61. /* Master clock speed factor
  62. * to be used (master clock speed = sysclock_factor * sampling freq)
  63. * Unused if the soundbus provider has no such notion.
  64. */
  65. int sysclock_factor;
  66. /* Bus factor, bus clock speed = bus_factor * sampling freq)
  67. * Unused if the soundbus provider has no such notion.
  68. */
  69. int bus_factor;
  70. /* operations */
  71. /* clock switching, see above */
  72. int (*switch_clock)(struct codec_info_item *cii,
  73. enum clock_switch clock);
  74. /* called for each transfer_info when the user
  75. * opens the pcm device to determine what the
  76. * hardware can support at this point in time.
  77. * That can depend on other user-switchable controls.
  78. * Return 1 if usable, 0 if not.
  79. * out points to another instance of a transfer_info
  80. * which is initialised to the values in *ti, and
  81. * it's format and rate values can be modified by
  82. * the callback if it is necessary to further restrict
  83. * the formats that can be used at the moment, for
  84. * example when one codec has multiple logical codec
  85. * info structs for multiple inputs.
  86. */
  87. int (*usable)(struct codec_info_item *cii,
  88. struct transfer_info *ti,
  89. struct transfer_info *out);
  90. /* called when pcm stream is opened, probably not implemented
  91. * most of the time since it isn't too useful */
  92. int (*open)(struct codec_info_item *cii,
  93. struct snd_pcm_substream *substream);
  94. /* called when the pcm stream is closed, at this point
  95. * the user choices can all be unlocked (see below) */
  96. int (*close)(struct codec_info_item *cii,
  97. struct snd_pcm_substream *substream);
  98. /* if the codec must forbid some user choices because
  99. * they are not valid with the substream/transfer info,
  100. * it must do so here. Example: no digital output for
  101. * incompatible framerate, say 8KHz, on Onyx.
  102. * If the selected stuff in the substream is NOT
  103. * compatible, you have to reject this call! */
  104. int (*prepare)(struct codec_info_item *cii,
  105. struct bus_info *bi,
  106. struct snd_pcm_substream *substream);
  107. /* start() is called before data is pushed to the codec.
  108. * Note that start() must be atomic! */
  109. int (*start)(struct codec_info_item *cii,
  110. struct snd_pcm_substream *substream);
  111. /* stop() is called after data is no longer pushed to the codec.
  112. * Note that stop() must be atomic! */
  113. int (*stop)(struct codec_info_item *cii,
  114. struct snd_pcm_substream *substream);
  115. int (*suspend)(struct codec_info_item *cii, pm_message_t state);
  116. int (*resume)(struct codec_info_item *cii);
  117. };
  118. /* information on a soundbus device */
  119. struct soundbus_dev {
  120. /* the bus it belongs to */
  121. struct list_head onbuslist;
  122. /* the of device it represents */
  123. struct platform_device ofdev;
  124. /* what modules go by */
  125. char modalias[32];
  126. /* These fields must be before attach_codec can be called.
  127. * They should be set by the owner of the alsa card object
  128. * that is needed, and whoever sets them must make sure
  129. * that they are unique within that alsa card object. */
  130. char *pcmname;
  131. int pcmid;
  132. /* this is assigned by the soundbus provider in attach_codec */
  133. struct snd_pcm *pcm;
  134. /* operations */
  135. /* attach a codec to this soundbus, give the alsa
  136. * card object the PCMs for this soundbus should be in.
  137. * The 'data' pointer must be unique, it is used as the
  138. * key for detach_codec(). */
  139. int (*attach_codec)(struct soundbus_dev *dev, struct snd_card *card,
  140. struct codec_info *ci, void *data);
  141. void (*detach_codec)(struct soundbus_dev *dev, void *data);
  142. /* TODO: suspend/resume */
  143. /* private for the soundbus provider */
  144. struct list_head codec_list;
  145. u32 have_out:1, have_in:1;
  146. };
  147. #define to_soundbus_device(d) container_of(d, struct soundbus_dev, ofdev.dev)
  148. #define of_to_soundbus_device(d) container_of(d, struct soundbus_dev, ofdev)
  149. extern int soundbus_add_one(struct soundbus_dev *dev);
  150. extern void soundbus_remove_one(struct soundbus_dev *dev);
  151. extern struct soundbus_dev *soundbus_dev_get(struct soundbus_dev *dev);
  152. extern void soundbus_dev_put(struct soundbus_dev *dev);
  153. struct soundbus_driver {
  154. char *name;
  155. struct module *owner;
  156. /* we don't implement any matching at all */
  157. int (*probe)(struct soundbus_dev* dev);
  158. int (*remove)(struct soundbus_dev* dev);
  159. int (*shutdown)(struct soundbus_dev* dev);
  160. struct device_driver driver;
  161. };
  162. #define to_soundbus_driver(drv) container_of(drv,struct soundbus_driver, driver)
  163. extern int soundbus_register_driver(struct soundbus_driver *drv);
  164. extern void soundbus_unregister_driver(struct soundbus_driver *drv);
  165. extern struct attribute *soundbus_dev_attrs[];
  166. #endif /* __SOUNDBUS_H */