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