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