asihpi.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  1. /*
  2. * Asihpi soundcard
  3. * Copyright (c) by AudioScience Inc <support@audioscience.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation;
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * The following is not a condition of use, merely a request:
  20. * If you modify this program, particularly if you fix errors, AudioScience Inc
  21. * would appreciate it if you grant us the right to use those modifications
  22. * for any purpose including commercial applications.
  23. */
  24. #include "hpi_internal.h"
  25. #include "hpi_version.h"
  26. #include "hpimsginit.h"
  27. #include "hpioctl.h"
  28. #include "hpicmn.h"
  29. #include <linux/pci.h>
  30. #include <linux/init.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/slab.h>
  33. #include <linux/time.h>
  34. #include <linux/wait.h>
  35. #include <linux/module.h>
  36. #include <sound/core.h>
  37. #include <sound/control.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/info.h>
  41. #include <sound/initval.h>
  42. #include <sound/tlv.h>
  43. #include <sound/hwdep.h>
  44. MODULE_LICENSE("GPL");
  45. MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
  46. MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
  47. HPI_VER_STRING);
  48. #if defined CONFIG_SND_DEBUG_VERBOSE
  49. /**
  50. * snd_printddd - very verbose debug printk
  51. * @format: format string
  52. *
  53. * Works like snd_printk() for debugging purposes.
  54. * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
  55. * Must set snd module debug parameter to 3 to enable at runtime.
  56. */
  57. #define snd_printddd(format, args...) \
  58. __snd_printk(3, __FILE__, __LINE__, format, ##args)
  59. #else
  60. #define snd_printddd(format, args...) do { } while (0)
  61. #endif
  62. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
  63. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  64. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  65. static bool enable_hpi_hwdep = 1;
  66. module_param_array(index, int, NULL, S_IRUGO);
  67. MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
  68. module_param_array(id, charp, NULL, S_IRUGO);
  69. MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
  70. module_param_array(enable, bool, NULL, S_IRUGO);
  71. MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
  72. module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
  73. MODULE_PARM_DESC(enable_hpi_hwdep,
  74. "ALSA enable HPI hwdep for AudioScience soundcard ");
  75. /* identify driver */
  76. #ifdef KERNEL_ALSA_BUILD
  77. static char *build_info = "Built using headers from kernel source";
  78. module_param(build_info, charp, S_IRUGO);
  79. MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
  80. #else
  81. static char *build_info = "Built within ALSA source";
  82. module_param(build_info, charp, S_IRUGO);
  83. MODULE_PARM_DESC(build_info, "Built within ALSA source");
  84. #endif
  85. /* set to 1 to dump every control from adapter to log */
  86. static const int mixer_dump;
  87. #define DEFAULT_SAMPLERATE 44100
  88. static int adapter_fs = DEFAULT_SAMPLERATE;
  89. /* defaults */
  90. #define PERIODS_MIN 2
  91. #define PERIOD_BYTES_MIN 2048
  92. #define BUFFER_BYTES_MAX (512 * 1024)
  93. #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
  94. struct clk_source {
  95. int source;
  96. int index;
  97. const char *name;
  98. };
  99. struct clk_cache {
  100. int count;
  101. int has_local;
  102. struct clk_source s[MAX_CLOCKSOURCES];
  103. };
  104. /* Per card data */
  105. struct snd_card_asihpi {
  106. struct snd_card *card;
  107. struct pci_dev *pci;
  108. struct hpi_adapter *hpi;
  109. /* In low latency mode there is only one stream, a pointer to its
  110. * private data is stored here on trigger and cleared on stop.
  111. * The interrupt handler uses it as a parameter when calling
  112. * snd_card_asihpi_timer_function().
  113. */
  114. struct snd_card_asihpi_pcm *llmode_streampriv;
  115. struct tasklet_struct t;
  116. void (*pcm_start)(struct snd_pcm_substream *substream);
  117. void (*pcm_stop)(struct snd_pcm_substream *substream);
  118. u32 h_mixer;
  119. struct clk_cache cc;
  120. u16 can_dma;
  121. u16 support_grouping;
  122. u16 support_mrx;
  123. u16 update_interval_frames;
  124. u16 in_max_chans;
  125. u16 out_max_chans;
  126. u16 in_min_chans;
  127. u16 out_min_chans;
  128. };
  129. /* Per stream data */
  130. struct snd_card_asihpi_pcm {
  131. struct timer_list timer;
  132. unsigned int respawn_timer;
  133. unsigned int hpi_buffer_attached;
  134. unsigned int buffer_bytes;
  135. unsigned int period_bytes;
  136. unsigned int bytes_per_sec;
  137. unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
  138. unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
  139. unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
  140. unsigned int drained_count;
  141. struct snd_pcm_substream *substream;
  142. u32 h_stream;
  143. struct hpi_format format;
  144. };
  145. /* universal stream verbs work with out or in stream handles */
  146. /* Functions to allow driver to give a buffer to HPI for busmastering */
  147. static u16 hpi_stream_host_buffer_attach(
  148. u32 h_stream, /* handle to outstream. */
  149. u32 size_in_bytes, /* size in bytes of bus mastering buffer */
  150. u32 pci_address
  151. )
  152. {
  153. struct hpi_message hm;
  154. struct hpi_response hr;
  155. unsigned int obj = hpi_handle_object(h_stream);
  156. if (!h_stream)
  157. return HPI_ERROR_INVALID_OBJ;
  158. hpi_init_message_response(&hm, &hr, obj,
  159. obj == HPI_OBJ_OSTREAM ?
  160. HPI_OSTREAM_HOSTBUFFER_ALLOC :
  161. HPI_ISTREAM_HOSTBUFFER_ALLOC);
  162. hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  163. &hm.obj_index);
  164. hm.u.d.u.buffer.buffer_size = size_in_bytes;
  165. hm.u.d.u.buffer.pci_address = pci_address;
  166. hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
  167. hpi_send_recv(&hm, &hr);
  168. return hr.error;
  169. }
  170. static u16 hpi_stream_host_buffer_detach(u32 h_stream)
  171. {
  172. struct hpi_message hm;
  173. struct hpi_response hr;
  174. unsigned int obj = hpi_handle_object(h_stream);
  175. if (!h_stream)
  176. return HPI_ERROR_INVALID_OBJ;
  177. hpi_init_message_response(&hm, &hr, obj,
  178. obj == HPI_OBJ_OSTREAM ?
  179. HPI_OSTREAM_HOSTBUFFER_FREE :
  180. HPI_ISTREAM_HOSTBUFFER_FREE);
  181. hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  182. &hm.obj_index);
  183. hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
  184. hpi_send_recv(&hm, &hr);
  185. return hr.error;
  186. }
  187. static inline u16 hpi_stream_start(u32 h_stream)
  188. {
  189. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  190. return hpi_outstream_start(h_stream);
  191. else
  192. return hpi_instream_start(h_stream);
  193. }
  194. static inline u16 hpi_stream_stop(u32 h_stream)
  195. {
  196. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  197. return hpi_outstream_stop(h_stream);
  198. else
  199. return hpi_instream_stop(h_stream);
  200. }
  201. static inline u16 hpi_stream_get_info_ex(
  202. u32 h_stream,
  203. u16 *pw_state,
  204. u32 *pbuffer_size,
  205. u32 *pdata_in_buffer,
  206. u32 *psample_count,
  207. u32 *pauxiliary_data
  208. )
  209. {
  210. u16 e;
  211. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  212. e = hpi_outstream_get_info_ex(h_stream, pw_state,
  213. pbuffer_size, pdata_in_buffer,
  214. psample_count, pauxiliary_data);
  215. else
  216. e = hpi_instream_get_info_ex(h_stream, pw_state,
  217. pbuffer_size, pdata_in_buffer,
  218. psample_count, pauxiliary_data);
  219. return e;
  220. }
  221. static inline u16 hpi_stream_group_add(
  222. u32 h_master,
  223. u32 h_stream)
  224. {
  225. if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
  226. return hpi_outstream_group_add(h_master, h_stream);
  227. else
  228. return hpi_instream_group_add(h_master, h_stream);
  229. }
  230. static inline u16 hpi_stream_group_reset(u32 h_stream)
  231. {
  232. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  233. return hpi_outstream_group_reset(h_stream);
  234. else
  235. return hpi_instream_group_reset(h_stream);
  236. }
  237. static inline u16 hpi_stream_group_get_map(
  238. u32 h_stream, u32 *mo, u32 *mi)
  239. {
  240. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  241. return hpi_outstream_group_get_map(h_stream, mo, mi);
  242. else
  243. return hpi_instream_group_get_map(h_stream, mo, mi);
  244. }
  245. static u16 handle_error(u16 err, int line, char *filename)
  246. {
  247. if (err)
  248. printk(KERN_WARNING
  249. "in file %s, line %d: HPI error %d\n",
  250. filename, line, err);
  251. return err;
  252. }
  253. #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
  254. /***************************** GENERAL PCM ****************/
  255. static void print_hwparams(struct snd_pcm_substream *substream,
  256. struct snd_pcm_hw_params *p)
  257. {
  258. char name[16];
  259. snd_pcm_debug_name(substream, name, sizeof(name));
  260. snd_printdd("%s HWPARAMS\n", name);
  261. snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
  262. params_rate(p), params_channels(p),
  263. params_format(p), params_subformat(p));
  264. snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
  265. params_buffer_bytes(p), params_period_bytes(p),
  266. params_period_size(p), params_periods(p));
  267. snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
  268. params_buffer_size(p), params_access(p),
  269. params_rate(p) * params_channels(p) *
  270. snd_pcm_format_width(params_format(p)) / 8);
  271. }
  272. static snd_pcm_format_t hpi_to_alsa_formats[] = {
  273. -1, /* INVALID */
  274. SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
  275. SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
  276. -1, /* HPI_FORMAT_MPEG_L1 3 */
  277. SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
  278. SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
  279. -1, /* HPI_FORMAT_DOLBY_AC2 6 */
  280. -1, /* HPI_FORMAT_DOLBY_AC3 7 */
  281. SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
  282. -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
  283. -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
  284. SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
  285. -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
  286. -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
  287. SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
  288. #if 1
  289. /* ALSA can't handle 3 byte sample size together with power-of-2
  290. * constraint on buffer_bytes, so disable this format
  291. */
  292. -1
  293. #else
  294. /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
  295. #endif
  296. };
  297. static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
  298. u16 *hpi_format)
  299. {
  300. u16 format;
  301. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  302. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  303. if (hpi_to_alsa_formats[format] == alsa_format) {
  304. *hpi_format = format;
  305. return 0;
  306. }
  307. }
  308. snd_printd(KERN_WARNING "failed match for alsa format %d\n",
  309. alsa_format);
  310. *hpi_format = 0;
  311. return -EINVAL;
  312. }
  313. static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
  314. struct snd_pcm_hardware *pcmhw)
  315. {
  316. u16 err;
  317. u32 h_control;
  318. u32 sample_rate;
  319. int idx;
  320. unsigned int rate_min = 200000;
  321. unsigned int rate_max = 0;
  322. unsigned int rates = 0;
  323. if (asihpi->support_mrx) {
  324. rates |= SNDRV_PCM_RATE_CONTINUOUS;
  325. rates |= SNDRV_PCM_RATE_8000_96000;
  326. rate_min = 8000;
  327. rate_max = 100000;
  328. } else {
  329. /* on cards without SRC,
  330. valid rates are determined by sampleclock */
  331. err = hpi_mixer_get_control(asihpi->h_mixer,
  332. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  333. HPI_CONTROL_SAMPLECLOCK, &h_control);
  334. if (err) {
  335. dev_err(&asihpi->pci->dev,
  336. "No local sampleclock, err %d\n", err);
  337. }
  338. for (idx = -1; idx < 100; idx++) {
  339. if (idx == -1) {
  340. if (hpi_sample_clock_get_sample_rate(h_control,
  341. &sample_rate))
  342. continue;
  343. } else if (hpi_sample_clock_query_local_rate(h_control,
  344. idx, &sample_rate)) {
  345. break;
  346. }
  347. rate_min = min(rate_min, sample_rate);
  348. rate_max = max(rate_max, sample_rate);
  349. switch (sample_rate) {
  350. case 5512:
  351. rates |= SNDRV_PCM_RATE_5512;
  352. break;
  353. case 8000:
  354. rates |= SNDRV_PCM_RATE_8000;
  355. break;
  356. case 11025:
  357. rates |= SNDRV_PCM_RATE_11025;
  358. break;
  359. case 16000:
  360. rates |= SNDRV_PCM_RATE_16000;
  361. break;
  362. case 22050:
  363. rates |= SNDRV_PCM_RATE_22050;
  364. break;
  365. case 32000:
  366. rates |= SNDRV_PCM_RATE_32000;
  367. break;
  368. case 44100:
  369. rates |= SNDRV_PCM_RATE_44100;
  370. break;
  371. case 48000:
  372. rates |= SNDRV_PCM_RATE_48000;
  373. break;
  374. case 64000:
  375. rates |= SNDRV_PCM_RATE_64000;
  376. break;
  377. case 88200:
  378. rates |= SNDRV_PCM_RATE_88200;
  379. break;
  380. case 96000:
  381. rates |= SNDRV_PCM_RATE_96000;
  382. break;
  383. case 176400:
  384. rates |= SNDRV_PCM_RATE_176400;
  385. break;
  386. case 192000:
  387. rates |= SNDRV_PCM_RATE_192000;
  388. break;
  389. default: /* some other rate */
  390. rates |= SNDRV_PCM_RATE_KNOT;
  391. }
  392. }
  393. }
  394. pcmhw->rates = rates;
  395. pcmhw->rate_min = rate_min;
  396. pcmhw->rate_max = rate_max;
  397. }
  398. static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
  399. struct snd_pcm_hw_params *params)
  400. {
  401. struct snd_pcm_runtime *runtime = substream->runtime;
  402. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  403. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  404. int err;
  405. u16 format;
  406. int width;
  407. unsigned int bytes_per_sec;
  408. print_hwparams(substream, params);
  409. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  410. if (err < 0)
  411. return err;
  412. err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
  413. if (err)
  414. return err;
  415. hpi_handle_error(hpi_format_create(&dpcm->format,
  416. params_channels(params),
  417. format, params_rate(params), 0, 0));
  418. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  419. if (hpi_instream_reset(dpcm->h_stream) != 0)
  420. return -EINVAL;
  421. if (hpi_instream_set_format(
  422. dpcm->h_stream, &dpcm->format) != 0)
  423. return -EINVAL;
  424. }
  425. dpcm->hpi_buffer_attached = 0;
  426. if (card->can_dma) {
  427. err = hpi_stream_host_buffer_attach(dpcm->h_stream,
  428. params_buffer_bytes(params), runtime->dma_addr);
  429. if (err == 0) {
  430. snd_printdd(
  431. "stream_host_buffer_attach success %u %lu\n",
  432. params_buffer_bytes(params),
  433. (unsigned long)runtime->dma_addr);
  434. } else {
  435. snd_printd("stream_host_buffer_attach error %d\n",
  436. err);
  437. return -ENOMEM;
  438. }
  439. err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
  440. &dpcm->hpi_buffer_attached, NULL, NULL, NULL);
  441. }
  442. bytes_per_sec = params_rate(params) * params_channels(params);
  443. width = snd_pcm_format_width(params_format(params));
  444. bytes_per_sec *= width;
  445. bytes_per_sec /= 8;
  446. if (width < 0 || bytes_per_sec == 0)
  447. return -EINVAL;
  448. dpcm->bytes_per_sec = bytes_per_sec;
  449. dpcm->buffer_bytes = params_buffer_bytes(params);
  450. dpcm->period_bytes = params_period_bytes(params);
  451. return 0;
  452. }
  453. static int
  454. snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
  455. {
  456. struct snd_pcm_runtime *runtime = substream->runtime;
  457. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  458. if (dpcm->hpi_buffer_attached)
  459. hpi_stream_host_buffer_detach(dpcm->h_stream);
  460. snd_pcm_lib_free_pages(substream);
  461. return 0;
  462. }
  463. static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
  464. {
  465. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  466. kfree(dpcm);
  467. }
  468. static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
  469. substream)
  470. {
  471. struct snd_pcm_runtime *runtime = substream->runtime;
  472. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  473. int expiry;
  474. expiry = HZ / 200;
  475. expiry = max(expiry, 1); /* don't let it be zero! */
  476. mod_timer(&dpcm->timer, jiffies + expiry);
  477. dpcm->respawn_timer = 1;
  478. }
  479. static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
  480. {
  481. struct snd_pcm_runtime *runtime = substream->runtime;
  482. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  483. dpcm->respawn_timer = 0;
  484. del_timer(&dpcm->timer);
  485. }
  486. static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
  487. {
  488. struct snd_card_asihpi_pcm *dpcm;
  489. struct snd_card_asihpi *card;
  490. BUG_ON(!substream);
  491. dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
  492. card = snd_pcm_substream_chip(substream);
  493. BUG_ON(in_interrupt());
  494. tasklet_disable(&card->t);
  495. card->llmode_streampriv = dpcm;
  496. tasklet_enable(&card->t);
  497. hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
  498. HPI_ADAPTER_PROPERTY_IRQ_RATE,
  499. card->update_interval_frames, 0));
  500. }
  501. static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
  502. {
  503. struct snd_card_asihpi_pcm *dpcm;
  504. struct snd_card_asihpi *card;
  505. BUG_ON(!substream);
  506. dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
  507. card = snd_pcm_substream_chip(substream);
  508. hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
  509. HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
  510. if (in_interrupt())
  511. card->llmode_streampriv = NULL;
  512. else {
  513. tasklet_disable(&card->t);
  514. card->llmode_streampriv = NULL;
  515. tasklet_enable(&card->t);
  516. }
  517. }
  518. static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
  519. int cmd)
  520. {
  521. struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  522. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  523. struct snd_pcm_substream *s;
  524. u16 e;
  525. char name[16];
  526. snd_pcm_debug_name(substream, name, sizeof(name));
  527. switch (cmd) {
  528. case SNDRV_PCM_TRIGGER_START:
  529. snd_printdd("%s trigger start\n", name);
  530. snd_pcm_group_for_each_entry(s, substream) {
  531. struct snd_pcm_runtime *runtime = s->runtime;
  532. struct snd_card_asihpi_pcm *ds = runtime->private_data;
  533. if (snd_pcm_substream_chip(s) != card)
  534. continue;
  535. /* don't link Cap and Play */
  536. if (substream->stream != s->stream)
  537. continue;
  538. ds->drained_count = 0;
  539. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  540. /* How do I know how much valid data is present
  541. * in buffer? Must be at least one period!
  542. * Guessing 2 periods, but if
  543. * buffer is bigger it may contain even more
  544. * data??
  545. */
  546. unsigned int preload = ds->period_bytes * 1;
  547. snd_printddd("%d preload %d\n", s->number, preload);
  548. hpi_handle_error(hpi_outstream_write_buf(
  549. ds->h_stream,
  550. &runtime->dma_area[0],
  551. preload,
  552. &ds->format));
  553. ds->pcm_buf_host_rw_ofs = preload;
  554. }
  555. if (card->support_grouping) {
  556. snd_printdd("%d group\n", s->number);
  557. e = hpi_stream_group_add(
  558. dpcm->h_stream,
  559. ds->h_stream);
  560. if (!e) {
  561. snd_pcm_trigger_done(s, substream);
  562. } else {
  563. hpi_handle_error(e);
  564. break;
  565. }
  566. } else
  567. break;
  568. }
  569. /* start the master stream */
  570. card->pcm_start(substream);
  571. if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
  572. !card->can_dma)
  573. hpi_handle_error(hpi_stream_start(dpcm->h_stream));
  574. break;
  575. case SNDRV_PCM_TRIGGER_STOP:
  576. snd_printdd("%s trigger stop\n", name);
  577. card->pcm_stop(substream);
  578. snd_pcm_group_for_each_entry(s, substream) {
  579. if (snd_pcm_substream_chip(s) != card)
  580. continue;
  581. /* don't link Cap and Play */
  582. if (substream->stream != s->stream)
  583. continue;
  584. /*? workaround linked streams don't
  585. transition to SETUP 20070706*/
  586. s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
  587. if (card->support_grouping) {
  588. snd_printdd("%d group\n", s->number);
  589. snd_pcm_trigger_done(s, substream);
  590. } else
  591. break;
  592. }
  593. /* _prepare and _hwparams reset the stream */
  594. hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
  595. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  596. hpi_handle_error(
  597. hpi_outstream_reset(dpcm->h_stream));
  598. if (card->support_grouping)
  599. hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
  600. break;
  601. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  602. snd_printdd("%s trigger pause release\n", name);
  603. card->pcm_start(substream);
  604. hpi_handle_error(hpi_stream_start(dpcm->h_stream));
  605. break;
  606. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  607. snd_printdd("%s trigger pause push\n", name);
  608. card->pcm_stop(substream);
  609. hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
  610. break;
  611. default:
  612. snd_printd(KERN_ERR "\tINVALID\n");
  613. return -EINVAL;
  614. }
  615. return 0;
  616. }
  617. /*algorithm outline
  618. Without linking degenerates to getting single stream pos etc
  619. Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
  620. */
  621. /*
  622. pcm_buf_dma_ofs=get_buf_pos(s);
  623. for_each_linked_stream(s) {
  624. pcm_buf_dma_ofs=get_buf_pos(s);
  625. min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
  626. new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
  627. }
  628. timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
  629. for_each_linked_stream(s) {
  630. s->pcm_buf_dma_ofs = min_buf_pos;
  631. if (new_data > period_bytes) {
  632. if (mmap) {
  633. irq_pos = (irq_pos + period_bytes) % buffer_bytes;
  634. if (playback) {
  635. write(period_bytes);
  636. } else {
  637. read(period_bytes);
  638. }
  639. }
  640. snd_pcm_period_elapsed(s);
  641. }
  642. }
  643. */
  644. /** Minimum of 2 modulo values. Works correctly when the difference between
  645. * the values is less than half the modulus
  646. */
  647. static inline unsigned int modulo_min(unsigned int a, unsigned int b,
  648. unsigned long int modulus)
  649. {
  650. unsigned int result;
  651. if (((a-b) % modulus) < (modulus/2))
  652. result = b;
  653. else
  654. result = a;
  655. return result;
  656. }
  657. /** Timer function, equivalent to interrupt service routine for cards
  658. */
  659. static void snd_card_asihpi_timer_function(unsigned long data)
  660. {
  661. struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
  662. struct snd_pcm_substream *substream = dpcm->substream;
  663. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  664. struct snd_pcm_runtime *runtime;
  665. struct snd_pcm_substream *s;
  666. unsigned int newdata = 0;
  667. unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
  668. unsigned int remdata, xfercount, next_jiffies;
  669. int first = 1;
  670. int loops = 0;
  671. u16 state;
  672. u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
  673. char name[16];
  674. snd_pcm_debug_name(substream, name, sizeof(name));
  675. /* find minimum newdata and buffer pos in group */
  676. snd_pcm_group_for_each_entry(s, substream) {
  677. struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  678. runtime = s->runtime;
  679. if (snd_pcm_substream_chip(s) != card)
  680. continue;
  681. /* don't link Cap and Play */
  682. if (substream->stream != s->stream)
  683. continue;
  684. hpi_handle_error(hpi_stream_get_info_ex(
  685. ds->h_stream, &state,
  686. &buffer_size, &bytes_avail,
  687. &samples_played, &on_card_bytes));
  688. /* number of bytes in on-card buffer */
  689. runtime->delay = on_card_bytes;
  690. if (!card->can_dma)
  691. on_card_bytes = bytes_avail;
  692. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  693. pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
  694. if (state == HPI_STATE_STOPPED) {
  695. if (bytes_avail == 0) {
  696. hpi_handle_error(hpi_stream_start(ds->h_stream));
  697. snd_printdd("P%d start\n", s->number);
  698. ds->drained_count = 0;
  699. }
  700. } else if (state == HPI_STATE_DRAINED) {
  701. snd_printd(KERN_WARNING "P%d drained\n",
  702. s->number);
  703. ds->drained_count++;
  704. if (ds->drained_count > 20) {
  705. snd_pcm_stop_xrun(s);
  706. continue;
  707. }
  708. } else {
  709. ds->drained_count = 0;
  710. }
  711. } else
  712. pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
  713. if (first) {
  714. /* can't statically init min when wrap is involved */
  715. min_buf_pos = pcm_buf_dma_ofs;
  716. newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
  717. first = 0;
  718. } else {
  719. min_buf_pos =
  720. modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
  721. newdata = min(
  722. (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
  723. newdata);
  724. }
  725. snd_printddd(
  726. "timer1, %s, %d, S=%d, elap=%d, rw=%d, dsp=%d, left=%d, aux=%d, space=%d, hw_ptr=%ld, appl_ptr=%ld\n",
  727. name, s->number, state,
  728. ds->pcm_buf_elapsed_dma_ofs,
  729. ds->pcm_buf_host_rw_ofs,
  730. pcm_buf_dma_ofs,
  731. (int)bytes_avail,
  732. (int)on_card_bytes,
  733. buffer_size-bytes_avail,
  734. (unsigned long)frames_to_bytes(runtime,
  735. runtime->status->hw_ptr),
  736. (unsigned long)frames_to_bytes(runtime,
  737. runtime->control->appl_ptr)
  738. );
  739. loops++;
  740. }
  741. pcm_buf_dma_ofs = min_buf_pos;
  742. remdata = newdata % dpcm->period_bytes;
  743. xfercount = newdata - remdata; /* a multiple of period_bytes */
  744. /* come back when on_card_bytes has decreased enough to allow
  745. write to happen, or when data has been consumed to make another
  746. period
  747. */
  748. if (xfercount && (on_card_bytes > dpcm->period_bytes))
  749. next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
  750. else
  751. next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
  752. next_jiffies = max(next_jiffies, 1U);
  753. dpcm->timer.expires = jiffies + next_jiffies;
  754. snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
  755. next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
  756. snd_pcm_group_for_each_entry(s, substream) {
  757. struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  758. runtime = s->runtime;
  759. /* don't link Cap and Play */
  760. if (substream->stream != s->stream)
  761. continue;
  762. /* Store dma offset for use by pointer callback */
  763. ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
  764. if (xfercount &&
  765. /* Limit use of on card fifo for playback */
  766. ((on_card_bytes <= ds->period_bytes) ||
  767. (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
  768. {
  769. unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
  770. unsigned int xfer1, xfer2;
  771. char *pd = &s->runtime->dma_area[buf_ofs];
  772. if (card->can_dma) { /* buffer wrap is handled at lower level */
  773. xfer1 = xfercount;
  774. xfer2 = 0;
  775. } else {
  776. xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
  777. xfer2 = xfercount - xfer1;
  778. }
  779. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  780. snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
  781. s->number, xfer1, buf_ofs);
  782. hpi_handle_error(
  783. hpi_outstream_write_buf(
  784. ds->h_stream, pd, xfer1,
  785. &ds->format));
  786. if (xfer2) {
  787. pd = s->runtime->dma_area;
  788. snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
  789. s->number,
  790. xfercount - xfer1, buf_ofs);
  791. hpi_handle_error(
  792. hpi_outstream_write_buf(
  793. ds->h_stream, pd,
  794. xfercount - xfer1,
  795. &ds->format));
  796. }
  797. } else {
  798. snd_printddd("read1, C=%d, xfer=%d\n",
  799. s->number, xfer1);
  800. hpi_handle_error(
  801. hpi_instream_read_buf(
  802. ds->h_stream,
  803. pd, xfer1));
  804. if (xfer2) {
  805. pd = s->runtime->dma_area;
  806. snd_printddd("read2, C=%d, xfer=%d\n",
  807. s->number, xfer2);
  808. hpi_handle_error(
  809. hpi_instream_read_buf(
  810. ds->h_stream,
  811. pd, xfer2));
  812. }
  813. }
  814. /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
  815. ds->pcm_buf_host_rw_ofs += xfercount;
  816. ds->pcm_buf_elapsed_dma_ofs += xfercount;
  817. snd_pcm_period_elapsed(s);
  818. }
  819. }
  820. if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
  821. add_timer(&dpcm->timer);
  822. }
  823. static void snd_card_asihpi_int_task(unsigned long data)
  824. {
  825. struct hpi_adapter *a = (struct hpi_adapter *)data;
  826. struct snd_card_asihpi *asihpi;
  827. WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
  828. asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
  829. if (asihpi->llmode_streampriv)
  830. snd_card_asihpi_timer_function(
  831. (unsigned long)asihpi->llmode_streampriv);
  832. }
  833. static void snd_card_asihpi_isr(struct hpi_adapter *a)
  834. {
  835. struct snd_card_asihpi *asihpi;
  836. WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
  837. asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
  838. tasklet_schedule(&asihpi->t);
  839. }
  840. /***************************** PLAYBACK OPS ****************/
  841. static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
  842. unsigned int cmd, void *arg)
  843. {
  844. char name[16];
  845. snd_pcm_debug_name(substream, name, sizeof(name));
  846. snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
  847. return snd_pcm_lib_ioctl(substream, cmd, arg);
  848. }
  849. static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
  850. substream)
  851. {
  852. struct snd_pcm_runtime *runtime = substream->runtime;
  853. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  854. snd_printdd("P%d prepare\n", substream->number);
  855. hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
  856. dpcm->pcm_buf_host_rw_ofs = 0;
  857. dpcm->pcm_buf_dma_ofs = 0;
  858. dpcm->pcm_buf_elapsed_dma_ofs = 0;
  859. return 0;
  860. }
  861. static snd_pcm_uframes_t
  862. snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
  863. {
  864. struct snd_pcm_runtime *runtime = substream->runtime;
  865. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  866. snd_pcm_uframes_t ptr;
  867. char name[16];
  868. snd_pcm_debug_name(substream, name, sizeof(name));
  869. ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
  870. snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
  871. return ptr;
  872. }
  873. static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
  874. u32 h_stream)
  875. {
  876. struct hpi_format hpi_format;
  877. u16 format;
  878. u16 err;
  879. u32 h_control;
  880. u32 sample_rate = 48000;
  881. u64 formats = 0;
  882. /* on cards without SRC, must query at valid rate,
  883. * maybe set by external sync
  884. */
  885. err = hpi_mixer_get_control(asihpi->h_mixer,
  886. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  887. HPI_CONTROL_SAMPLECLOCK, &h_control);
  888. if (!err)
  889. err = hpi_sample_clock_get_sample_rate(h_control,
  890. &sample_rate);
  891. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  892. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  893. err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
  894. format, sample_rate, 128000, 0);
  895. if (!err)
  896. err = hpi_outstream_query_format(h_stream, &hpi_format);
  897. if (!err && (hpi_to_alsa_formats[format] != -1))
  898. formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
  899. }
  900. return formats;
  901. }
  902. static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
  903. {
  904. struct snd_pcm_runtime *runtime = substream->runtime;
  905. struct snd_card_asihpi_pcm *dpcm;
  906. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  907. struct snd_pcm_hardware snd_card_asihpi_playback;
  908. int err;
  909. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  910. if (dpcm == NULL)
  911. return -ENOMEM;
  912. err = hpi_outstream_open(card->hpi->adapter->index,
  913. substream->number, &dpcm->h_stream);
  914. hpi_handle_error(err);
  915. if (err)
  916. kfree(dpcm);
  917. if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  918. return -EBUSY;
  919. if (err)
  920. return -EIO;
  921. /*? also check ASI5000 samplerate source
  922. If external, only support external rate.
  923. If internal and other stream playing, can't switch
  924. */
  925. setup_timer(&dpcm->timer, snd_card_asihpi_timer_function,
  926. (unsigned long) dpcm);
  927. dpcm->substream = substream;
  928. runtime->private_data = dpcm;
  929. runtime->private_free = snd_card_asihpi_runtime_free;
  930. memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
  931. if (!card->hpi->interrupt_mode) {
  932. snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
  933. snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
  934. snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  935. snd_card_asihpi_playback.periods_min = PERIODS_MIN;
  936. snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
  937. } else {
  938. size_t pbmin = card->update_interval_frames *
  939. card->out_max_chans;
  940. snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
  941. snd_card_asihpi_playback.period_bytes_min = pbmin;
  942. snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  943. snd_card_asihpi_playback.periods_min = PERIODS_MIN;
  944. snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
  945. }
  946. /* snd_card_asihpi_playback.fifo_size = 0; */
  947. snd_card_asihpi_playback.channels_max = card->out_max_chans;
  948. snd_card_asihpi_playback.channels_min = card->out_min_chans;
  949. snd_card_asihpi_playback.formats =
  950. snd_card_asihpi_playback_formats(card, dpcm->h_stream);
  951. snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
  952. snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
  953. SNDRV_PCM_INFO_DOUBLE |
  954. SNDRV_PCM_INFO_BATCH |
  955. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  956. SNDRV_PCM_INFO_PAUSE |
  957. SNDRV_PCM_INFO_MMAP |
  958. SNDRV_PCM_INFO_MMAP_VALID;
  959. if (card->support_grouping) {
  960. snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
  961. snd_pcm_set_sync(substream);
  962. }
  963. /* struct is copied, so can create initializer dynamically */
  964. runtime->hw = snd_card_asihpi_playback;
  965. if (card->can_dma)
  966. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  967. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  968. if (err < 0)
  969. return err;
  970. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  971. card->update_interval_frames);
  972. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  973. card->update_interval_frames, UINT_MAX);
  974. snd_printdd("playback open\n");
  975. return 0;
  976. }
  977. static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
  978. {
  979. struct snd_pcm_runtime *runtime = substream->runtime;
  980. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  981. hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
  982. snd_printdd("playback close\n");
  983. return 0;
  984. }
  985. static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
  986. .open = snd_card_asihpi_playback_open,
  987. .close = snd_card_asihpi_playback_close,
  988. .ioctl = snd_card_asihpi_playback_ioctl,
  989. .hw_params = snd_card_asihpi_pcm_hw_params,
  990. .hw_free = snd_card_asihpi_hw_free,
  991. .prepare = snd_card_asihpi_playback_prepare,
  992. .trigger = snd_card_asihpi_trigger,
  993. .pointer = snd_card_asihpi_playback_pointer,
  994. };
  995. /***************************** CAPTURE OPS ****************/
  996. static snd_pcm_uframes_t
  997. snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
  998. {
  999. struct snd_pcm_runtime *runtime = substream->runtime;
  1000. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  1001. char name[16];
  1002. snd_pcm_debug_name(substream, name, sizeof(name));
  1003. snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
  1004. /* NOTE Unlike playback can't use actual samples_played
  1005. for the capture position, because those samples aren't yet in
  1006. the local buffer available for reading.
  1007. */
  1008. return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
  1009. }
  1010. static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
  1011. unsigned int cmd, void *arg)
  1012. {
  1013. return snd_pcm_lib_ioctl(substream, cmd, arg);
  1014. }
  1015. static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
  1016. {
  1017. struct snd_pcm_runtime *runtime = substream->runtime;
  1018. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  1019. hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
  1020. dpcm->pcm_buf_host_rw_ofs = 0;
  1021. dpcm->pcm_buf_dma_ofs = 0;
  1022. dpcm->pcm_buf_elapsed_dma_ofs = 0;
  1023. snd_printdd("Capture Prepare %d\n", substream->number);
  1024. return 0;
  1025. }
  1026. static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
  1027. u32 h_stream)
  1028. {
  1029. struct hpi_format hpi_format;
  1030. u16 format;
  1031. u16 err;
  1032. u32 h_control;
  1033. u32 sample_rate = 48000;
  1034. u64 formats = 0;
  1035. /* on cards without SRC, must query at valid rate,
  1036. maybe set by external sync */
  1037. err = hpi_mixer_get_control(asihpi->h_mixer,
  1038. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  1039. HPI_CONTROL_SAMPLECLOCK, &h_control);
  1040. if (!err)
  1041. err = hpi_sample_clock_get_sample_rate(h_control,
  1042. &sample_rate);
  1043. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  1044. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  1045. err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
  1046. format, sample_rate, 128000, 0);
  1047. if (!err)
  1048. err = hpi_instream_query_format(h_stream, &hpi_format);
  1049. if (!err && (hpi_to_alsa_formats[format] != -1))
  1050. formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
  1051. }
  1052. return formats;
  1053. }
  1054. static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
  1055. {
  1056. struct snd_pcm_runtime *runtime = substream->runtime;
  1057. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  1058. struct snd_card_asihpi_pcm *dpcm;
  1059. struct snd_pcm_hardware snd_card_asihpi_capture;
  1060. int err;
  1061. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  1062. if (dpcm == NULL)
  1063. return -ENOMEM;
  1064. snd_printdd("capture open adapter %d stream %d\n",
  1065. card->hpi->adapter->index, substream->number);
  1066. err = hpi_handle_error(
  1067. hpi_instream_open(card->hpi->adapter->index,
  1068. substream->number, &dpcm->h_stream));
  1069. if (err)
  1070. kfree(dpcm);
  1071. if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  1072. return -EBUSY;
  1073. if (err)
  1074. return -EIO;
  1075. setup_timer(&dpcm->timer, snd_card_asihpi_timer_function,
  1076. (unsigned long) dpcm);
  1077. dpcm->substream = substream;
  1078. runtime->private_data = dpcm;
  1079. runtime->private_free = snd_card_asihpi_runtime_free;
  1080. memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
  1081. if (!card->hpi->interrupt_mode) {
  1082. snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
  1083. snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
  1084. snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  1085. snd_card_asihpi_capture.periods_min = PERIODS_MIN;
  1086. snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
  1087. } else {
  1088. size_t pbmin = card->update_interval_frames *
  1089. card->out_max_chans;
  1090. snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
  1091. snd_card_asihpi_capture.period_bytes_min = pbmin;
  1092. snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  1093. snd_card_asihpi_capture.periods_min = PERIODS_MIN;
  1094. snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
  1095. }
  1096. /* snd_card_asihpi_capture.fifo_size = 0; */
  1097. snd_card_asihpi_capture.channels_max = card->in_max_chans;
  1098. snd_card_asihpi_capture.channels_min = card->in_min_chans;
  1099. snd_card_asihpi_capture.formats =
  1100. snd_card_asihpi_capture_formats(card, dpcm->h_stream);
  1101. snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
  1102. snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
  1103. SNDRV_PCM_INFO_MMAP |
  1104. SNDRV_PCM_INFO_MMAP_VALID;
  1105. if (card->support_grouping)
  1106. snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
  1107. runtime->hw = snd_card_asihpi_capture;
  1108. if (card->can_dma)
  1109. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  1110. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  1111. if (err < 0)
  1112. return err;
  1113. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1114. card->update_interval_frames);
  1115. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1116. card->update_interval_frames, UINT_MAX);
  1117. snd_pcm_set_sync(substream);
  1118. return 0;
  1119. }
  1120. static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
  1121. {
  1122. struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  1123. hpi_handle_error(hpi_instream_close(dpcm->h_stream));
  1124. return 0;
  1125. }
  1126. static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
  1127. .open = snd_card_asihpi_capture_open,
  1128. .close = snd_card_asihpi_capture_close,
  1129. .ioctl = snd_card_asihpi_capture_ioctl,
  1130. .hw_params = snd_card_asihpi_pcm_hw_params,
  1131. .hw_free = snd_card_asihpi_hw_free,
  1132. .prepare = snd_card_asihpi_capture_prepare,
  1133. .trigger = snd_card_asihpi_trigger,
  1134. .pointer = snd_card_asihpi_capture_pointer,
  1135. };
  1136. static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
  1137. {
  1138. struct snd_pcm *pcm;
  1139. int err;
  1140. u16 num_instreams, num_outstreams, x16;
  1141. u32 x32;
  1142. err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
  1143. &num_outstreams, &num_instreams,
  1144. &x16, &x32, &x16);
  1145. err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
  1146. num_outstreams, num_instreams, &pcm);
  1147. if (err < 0)
  1148. return err;
  1149. /* pointer to ops struct is stored, dont change ops afterwards! */
  1150. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1151. &snd_card_asihpi_playback_mmap_ops);
  1152. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1153. &snd_card_asihpi_capture_mmap_ops);
  1154. pcm->private_data = asihpi;
  1155. pcm->info_flags = 0;
  1156. strcpy(pcm->name, "Asihpi PCM");
  1157. /*? do we want to emulate MMAP for non-BBM cards?
  1158. Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
  1159. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1160. snd_dma_pci_data(asihpi->pci),
  1161. 64*1024, BUFFER_BYTES_MAX);
  1162. return 0;
  1163. }
  1164. /***************************** MIXER CONTROLS ****************/
  1165. struct hpi_control {
  1166. u32 h_control;
  1167. u16 control_type;
  1168. u16 src_node_type;
  1169. u16 src_node_index;
  1170. u16 dst_node_type;
  1171. u16 dst_node_index;
  1172. u16 band;
  1173. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
  1174. };
  1175. static const char * const asihpi_tuner_band_names[] = {
  1176. "invalid",
  1177. "AM",
  1178. "FM mono",
  1179. "TV NTSC-M",
  1180. "FM stereo",
  1181. "AUX",
  1182. "TV PAL BG",
  1183. "TV PAL I",
  1184. "TV PAL DK",
  1185. "TV SECAM",
  1186. "TV DAB",
  1187. };
  1188. /* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
  1189. compile_time_assert(
  1190. (ARRAY_SIZE(asihpi_tuner_band_names) ==
  1191. (HPI_TUNER_BAND_LAST+1)),
  1192. assert_tuner_band_names_size);
  1193. static const char * const asihpi_src_names[] = {
  1194. "no source",
  1195. "PCM",
  1196. "Line",
  1197. "Digital",
  1198. "Tuner",
  1199. "RF",
  1200. "Clock",
  1201. "Bitstream",
  1202. "Mic",
  1203. "Net",
  1204. "Analog",
  1205. "Adapter",
  1206. "RTP",
  1207. "Internal",
  1208. "AVB",
  1209. "BLU-Link"
  1210. };
  1211. /* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
  1212. compile_time_assert(
  1213. (ARRAY_SIZE(asihpi_src_names) ==
  1214. (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
  1215. assert_src_names_size);
  1216. static const char * const asihpi_dst_names[] = {
  1217. "no destination",
  1218. "PCM",
  1219. "Line",
  1220. "Digital",
  1221. "RF",
  1222. "Speaker",
  1223. "Net",
  1224. "Analog",
  1225. "RTP",
  1226. "AVB",
  1227. "Internal",
  1228. "BLU-Link"
  1229. };
  1230. /* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
  1231. compile_time_assert(
  1232. (ARRAY_SIZE(asihpi_dst_names) ==
  1233. (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
  1234. assert_dst_names_size);
  1235. static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
  1236. struct snd_card_asihpi *asihpi)
  1237. {
  1238. int err;
  1239. err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
  1240. if (err < 0)
  1241. return err;
  1242. else if (mixer_dump)
  1243. dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
  1244. return 0;
  1245. }
  1246. /* Convert HPI control name and location into ALSA control name */
  1247. static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
  1248. struct hpi_control *hpi_ctl,
  1249. char *name)
  1250. {
  1251. char *dir;
  1252. memset(snd_control, 0, sizeof(*snd_control));
  1253. snd_control->name = hpi_ctl->name;
  1254. snd_control->private_value = hpi_ctl->h_control;
  1255. snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1256. snd_control->index = 0;
  1257. if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
  1258. dir = ""; /* clock is neither capture nor playback */
  1259. else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
  1260. dir = "Capture "; /* On or towards a PCM capture destination*/
  1261. else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  1262. (!hpi_ctl->dst_node_type))
  1263. dir = "Capture "; /* On a source node that is not PCM playback */
  1264. else if (hpi_ctl->src_node_type &&
  1265. (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  1266. (hpi_ctl->dst_node_type))
  1267. dir = "Monitor Playback "; /* Between an input and an output */
  1268. else
  1269. dir = "Playback "; /* PCM Playback source, or output node */
  1270. if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
  1271. sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
  1272. asihpi_src_names[hpi_ctl->src_node_type],
  1273. hpi_ctl->src_node_index,
  1274. asihpi_dst_names[hpi_ctl->dst_node_type],
  1275. hpi_ctl->dst_node_index,
  1276. dir, name);
  1277. else if (hpi_ctl->dst_node_type) {
  1278. sprintf(hpi_ctl->name, "%s %d %s%s",
  1279. asihpi_dst_names[hpi_ctl->dst_node_type],
  1280. hpi_ctl->dst_node_index,
  1281. dir, name);
  1282. } else {
  1283. sprintf(hpi_ctl->name, "%s %d %s%s",
  1284. asihpi_src_names[hpi_ctl->src_node_type],
  1285. hpi_ctl->src_node_index,
  1286. dir, name);
  1287. }
  1288. /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
  1289. hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
  1290. }
  1291. /*------------------------------------------------------------
  1292. Volume controls
  1293. ------------------------------------------------------------*/
  1294. #define VOL_STEP_mB 1
  1295. static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
  1296. struct snd_ctl_elem_info *uinfo)
  1297. {
  1298. u32 h_control = kcontrol->private_value;
  1299. u32 count;
  1300. u16 err;
  1301. /* native gains are in millibels */
  1302. short min_gain_mB;
  1303. short max_gain_mB;
  1304. short step_gain_mB;
  1305. err = hpi_volume_query_range(h_control,
  1306. &min_gain_mB, &max_gain_mB, &step_gain_mB);
  1307. if (err) {
  1308. max_gain_mB = 0;
  1309. min_gain_mB = -10000;
  1310. step_gain_mB = VOL_STEP_mB;
  1311. }
  1312. err = hpi_meter_query_channels(h_control, &count);
  1313. if (err)
  1314. count = HPI_MAX_CHANNELS;
  1315. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1316. uinfo->count = count;
  1317. uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
  1318. uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
  1319. uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
  1320. return 0;
  1321. }
  1322. static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
  1323. struct snd_ctl_elem_value *ucontrol)
  1324. {
  1325. u32 h_control = kcontrol->private_value;
  1326. short an_gain_mB[HPI_MAX_CHANNELS];
  1327. hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
  1328. ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
  1329. ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
  1330. return 0;
  1331. }
  1332. static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
  1333. struct snd_ctl_elem_value *ucontrol)
  1334. {
  1335. int change;
  1336. u32 h_control = kcontrol->private_value;
  1337. short an_gain_mB[HPI_MAX_CHANNELS];
  1338. an_gain_mB[0] =
  1339. (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
  1340. an_gain_mB[1] =
  1341. (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
  1342. /* change = asihpi->mixer_volume[addr][0] != left ||
  1343. asihpi->mixer_volume[addr][1] != right;
  1344. */
  1345. change = 1;
  1346. hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
  1347. return change;
  1348. }
  1349. static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
  1350. #define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
  1351. static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
  1352. struct snd_ctl_elem_value *ucontrol)
  1353. {
  1354. u32 h_control = kcontrol->private_value;
  1355. u32 mute;
  1356. hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
  1357. ucontrol->value.integer.value[0] = mute ? 0 : 1;
  1358. return 0;
  1359. }
  1360. static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
  1361. struct snd_ctl_elem_value *ucontrol)
  1362. {
  1363. u32 h_control = kcontrol->private_value;
  1364. int change = 1;
  1365. /* HPI currently only supports all or none muting of multichannel volume
  1366. ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
  1367. */
  1368. int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
  1369. hpi_handle_error(hpi_volume_set_mute(h_control, mute));
  1370. return change;
  1371. }
  1372. static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
  1373. struct hpi_control *hpi_ctl)
  1374. {
  1375. struct snd_card *card = asihpi->card;
  1376. struct snd_kcontrol_new snd_control;
  1377. int err;
  1378. u32 mute;
  1379. asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
  1380. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1381. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1382. snd_control.info = snd_asihpi_volume_info;
  1383. snd_control.get = snd_asihpi_volume_get;
  1384. snd_control.put = snd_asihpi_volume_put;
  1385. snd_control.tlv.p = db_scale_100;
  1386. err = ctl_add(card, &snd_control, asihpi);
  1387. if (err)
  1388. return err;
  1389. if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
  1390. asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
  1391. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1392. snd_control.info = snd_asihpi_volume_mute_info;
  1393. snd_control.get = snd_asihpi_volume_mute_get;
  1394. snd_control.put = snd_asihpi_volume_mute_put;
  1395. err = ctl_add(card, &snd_control, asihpi);
  1396. }
  1397. return err;
  1398. }
  1399. /*------------------------------------------------------------
  1400. Level controls
  1401. ------------------------------------------------------------*/
  1402. static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
  1403. struct snd_ctl_elem_info *uinfo)
  1404. {
  1405. u32 h_control = kcontrol->private_value;
  1406. u16 err;
  1407. short min_gain_mB;
  1408. short max_gain_mB;
  1409. short step_gain_mB;
  1410. err =
  1411. hpi_level_query_range(h_control, &min_gain_mB,
  1412. &max_gain_mB, &step_gain_mB);
  1413. if (err) {
  1414. max_gain_mB = 2400;
  1415. min_gain_mB = -1000;
  1416. step_gain_mB = 100;
  1417. }
  1418. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1419. uinfo->count = 2;
  1420. uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
  1421. uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
  1422. uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
  1423. return 0;
  1424. }
  1425. static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
  1426. struct snd_ctl_elem_value *ucontrol)
  1427. {
  1428. u32 h_control = kcontrol->private_value;
  1429. short an_gain_mB[HPI_MAX_CHANNELS];
  1430. hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
  1431. ucontrol->value.integer.value[0] =
  1432. an_gain_mB[0] / HPI_UNITS_PER_dB;
  1433. ucontrol->value.integer.value[1] =
  1434. an_gain_mB[1] / HPI_UNITS_PER_dB;
  1435. return 0;
  1436. }
  1437. static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
  1438. struct snd_ctl_elem_value *ucontrol)
  1439. {
  1440. int change;
  1441. u32 h_control = kcontrol->private_value;
  1442. short an_gain_mB[HPI_MAX_CHANNELS];
  1443. an_gain_mB[0] =
  1444. (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  1445. an_gain_mB[1] =
  1446. (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
  1447. /* change = asihpi->mixer_level[addr][0] != left ||
  1448. asihpi->mixer_level[addr][1] != right;
  1449. */
  1450. change = 1;
  1451. hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
  1452. return change;
  1453. }
  1454. static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
  1455. static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
  1456. struct hpi_control *hpi_ctl)
  1457. {
  1458. struct snd_card *card = asihpi->card;
  1459. struct snd_kcontrol_new snd_control;
  1460. /* can't use 'volume' cos some nodes have volume as well */
  1461. asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
  1462. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1463. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1464. snd_control.info = snd_asihpi_level_info;
  1465. snd_control.get = snd_asihpi_level_get;
  1466. snd_control.put = snd_asihpi_level_put;
  1467. snd_control.tlv.p = db_scale_level;
  1468. return ctl_add(card, &snd_control, asihpi);
  1469. }
  1470. /*------------------------------------------------------------
  1471. AESEBU controls
  1472. ------------------------------------------------------------*/
  1473. /* AESEBU format */
  1474. static const char * const asihpi_aesebu_format_names[] = {
  1475. "N/A", "S/PDIF", "AES/EBU" };
  1476. static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
  1477. struct snd_ctl_elem_info *uinfo)
  1478. {
  1479. return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
  1480. }
  1481. static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
  1482. struct snd_ctl_elem_value *ucontrol,
  1483. u16 (*func)(u32, u16 *))
  1484. {
  1485. u32 h_control = kcontrol->private_value;
  1486. u16 source, err;
  1487. err = func(h_control, &source);
  1488. /* default to N/A */
  1489. ucontrol->value.enumerated.item[0] = 0;
  1490. /* return success but set the control to N/A */
  1491. if (err)
  1492. return 0;
  1493. if (source == HPI_AESEBU_FORMAT_SPDIF)
  1494. ucontrol->value.enumerated.item[0] = 1;
  1495. if (source == HPI_AESEBU_FORMAT_AESEBU)
  1496. ucontrol->value.enumerated.item[0] = 2;
  1497. return 0;
  1498. }
  1499. static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
  1500. struct snd_ctl_elem_value *ucontrol,
  1501. u16 (*func)(u32, u16))
  1502. {
  1503. u32 h_control = kcontrol->private_value;
  1504. /* default to S/PDIF */
  1505. u16 source = HPI_AESEBU_FORMAT_SPDIF;
  1506. if (ucontrol->value.enumerated.item[0] == 1)
  1507. source = HPI_AESEBU_FORMAT_SPDIF;
  1508. if (ucontrol->value.enumerated.item[0] == 2)
  1509. source = HPI_AESEBU_FORMAT_AESEBU;
  1510. if (func(h_control, source) != 0)
  1511. return -EINVAL;
  1512. return 1;
  1513. }
  1514. static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
  1515. struct snd_ctl_elem_value *ucontrol) {
  1516. return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
  1517. hpi_aesebu_receiver_get_format);
  1518. }
  1519. static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
  1520. struct snd_ctl_elem_value *ucontrol) {
  1521. return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
  1522. hpi_aesebu_receiver_set_format);
  1523. }
  1524. static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
  1525. struct snd_ctl_elem_info *uinfo)
  1526. {
  1527. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1528. uinfo->count = 1;
  1529. uinfo->value.integer.min = 0;
  1530. uinfo->value.integer.max = 0X1F;
  1531. uinfo->value.integer.step = 1;
  1532. return 0;
  1533. }
  1534. static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
  1535. struct snd_ctl_elem_value *ucontrol) {
  1536. u32 h_control = kcontrol->private_value;
  1537. u16 status;
  1538. hpi_handle_error(hpi_aesebu_receiver_get_error_status(
  1539. h_control, &status));
  1540. ucontrol->value.integer.value[0] = status;
  1541. return 0;
  1542. }
  1543. static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
  1544. struct hpi_control *hpi_ctl)
  1545. {
  1546. struct snd_card *card = asihpi->card;
  1547. struct snd_kcontrol_new snd_control;
  1548. asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
  1549. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1550. snd_control.info = snd_asihpi_aesebu_format_info;
  1551. snd_control.get = snd_asihpi_aesebu_rx_format_get;
  1552. snd_control.put = snd_asihpi_aesebu_rx_format_put;
  1553. if (ctl_add(card, &snd_control, asihpi) < 0)
  1554. return -EINVAL;
  1555. asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
  1556. snd_control.access =
  1557. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  1558. snd_control.info = snd_asihpi_aesebu_rxstatus_info;
  1559. snd_control.get = snd_asihpi_aesebu_rxstatus_get;
  1560. return ctl_add(card, &snd_control, asihpi);
  1561. }
  1562. static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
  1563. struct snd_ctl_elem_value *ucontrol) {
  1564. return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
  1565. hpi_aesebu_transmitter_get_format);
  1566. }
  1567. static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
  1568. struct snd_ctl_elem_value *ucontrol) {
  1569. return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
  1570. hpi_aesebu_transmitter_set_format);
  1571. }
  1572. static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
  1573. struct hpi_control *hpi_ctl)
  1574. {
  1575. struct snd_card *card = asihpi->card;
  1576. struct snd_kcontrol_new snd_control;
  1577. asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
  1578. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1579. snd_control.info = snd_asihpi_aesebu_format_info;
  1580. snd_control.get = snd_asihpi_aesebu_tx_format_get;
  1581. snd_control.put = snd_asihpi_aesebu_tx_format_put;
  1582. return ctl_add(card, &snd_control, asihpi);
  1583. }
  1584. /*------------------------------------------------------------
  1585. Tuner controls
  1586. ------------------------------------------------------------*/
  1587. /* Gain */
  1588. static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
  1589. struct snd_ctl_elem_info *uinfo)
  1590. {
  1591. u32 h_control = kcontrol->private_value;
  1592. u16 err;
  1593. short idx;
  1594. u16 gain_range[3];
  1595. for (idx = 0; idx < 3; idx++) {
  1596. err = hpi_tuner_query_gain(h_control,
  1597. idx, &gain_range[idx]);
  1598. if (err != 0)
  1599. return err;
  1600. }
  1601. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1602. uinfo->count = 1;
  1603. uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
  1604. uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
  1605. uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
  1606. return 0;
  1607. }
  1608. static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
  1609. struct snd_ctl_elem_value *ucontrol)
  1610. {
  1611. /*
  1612. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1613. */
  1614. u32 h_control = kcontrol->private_value;
  1615. short gain;
  1616. hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
  1617. ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
  1618. return 0;
  1619. }
  1620. static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
  1621. struct snd_ctl_elem_value *ucontrol)
  1622. {
  1623. /*
  1624. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1625. */
  1626. u32 h_control = kcontrol->private_value;
  1627. short gain;
  1628. gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  1629. hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
  1630. return 1;
  1631. }
  1632. /* Band */
  1633. static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
  1634. u16 *band_list, u32 len) {
  1635. u32 h_control = kcontrol->private_value;
  1636. u16 err = 0;
  1637. u32 i;
  1638. for (i = 0; i < len; i++) {
  1639. err = hpi_tuner_query_band(
  1640. h_control, i, &band_list[i]);
  1641. if (err != 0)
  1642. break;
  1643. }
  1644. if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
  1645. return -EIO;
  1646. return i;
  1647. }
  1648. static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
  1649. struct snd_ctl_elem_info *uinfo)
  1650. {
  1651. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1652. int num_bands = 0;
  1653. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1654. HPI_TUNER_BAND_LAST);
  1655. if (num_bands < 0)
  1656. return num_bands;
  1657. return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
  1658. }
  1659. static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
  1660. struct snd_ctl_elem_value *ucontrol)
  1661. {
  1662. u32 h_control = kcontrol->private_value;
  1663. /*
  1664. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1665. */
  1666. u16 band, idx;
  1667. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1668. u32 num_bands = 0;
  1669. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1670. HPI_TUNER_BAND_LAST);
  1671. hpi_handle_error(hpi_tuner_get_band(h_control, &band));
  1672. ucontrol->value.enumerated.item[0] = -1;
  1673. for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
  1674. if (tuner_bands[idx] == band) {
  1675. ucontrol->value.enumerated.item[0] = idx;
  1676. break;
  1677. }
  1678. return 0;
  1679. }
  1680. static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
  1681. struct snd_ctl_elem_value *ucontrol)
  1682. {
  1683. /*
  1684. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1685. */
  1686. u32 h_control = kcontrol->private_value;
  1687. unsigned int idx;
  1688. u16 band;
  1689. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1690. u32 num_bands = 0;
  1691. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1692. HPI_TUNER_BAND_LAST);
  1693. idx = ucontrol->value.enumerated.item[0];
  1694. if (idx >= ARRAY_SIZE(tuner_bands))
  1695. idx = ARRAY_SIZE(tuner_bands) - 1;
  1696. band = tuner_bands[idx];
  1697. hpi_handle_error(hpi_tuner_set_band(h_control, band));
  1698. return 1;
  1699. }
  1700. /* Freq */
  1701. static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
  1702. struct snd_ctl_elem_info *uinfo)
  1703. {
  1704. u32 h_control = kcontrol->private_value;
  1705. u16 err;
  1706. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1707. u16 num_bands = 0, band_iter, idx;
  1708. u32 freq_range[3], temp_freq_range[3];
  1709. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1710. HPI_TUNER_BAND_LAST);
  1711. freq_range[0] = INT_MAX;
  1712. freq_range[1] = 0;
  1713. freq_range[2] = INT_MAX;
  1714. for (band_iter = 0; band_iter < num_bands; band_iter++) {
  1715. for (idx = 0; idx < 3; idx++) {
  1716. err = hpi_tuner_query_frequency(h_control,
  1717. idx, tuner_bands[band_iter],
  1718. &temp_freq_range[idx]);
  1719. if (err != 0)
  1720. return err;
  1721. }
  1722. /* skip band with bogus stepping */
  1723. if (temp_freq_range[2] <= 0)
  1724. continue;
  1725. if (temp_freq_range[0] < freq_range[0])
  1726. freq_range[0] = temp_freq_range[0];
  1727. if (temp_freq_range[1] > freq_range[1])
  1728. freq_range[1] = temp_freq_range[1];
  1729. if (temp_freq_range[2] < freq_range[2])
  1730. freq_range[2] = temp_freq_range[2];
  1731. }
  1732. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1733. uinfo->count = 1;
  1734. uinfo->value.integer.min = ((int)freq_range[0]);
  1735. uinfo->value.integer.max = ((int)freq_range[1]);
  1736. uinfo->value.integer.step = ((int)freq_range[2]);
  1737. return 0;
  1738. }
  1739. static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
  1740. struct snd_ctl_elem_value *ucontrol)
  1741. {
  1742. u32 h_control = kcontrol->private_value;
  1743. u32 freq;
  1744. hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
  1745. ucontrol->value.integer.value[0] = freq;
  1746. return 0;
  1747. }
  1748. static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
  1749. struct snd_ctl_elem_value *ucontrol)
  1750. {
  1751. u32 h_control = kcontrol->private_value;
  1752. u32 freq;
  1753. freq = ucontrol->value.integer.value[0];
  1754. hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
  1755. return 1;
  1756. }
  1757. /* Tuner control group initializer */
  1758. static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
  1759. struct hpi_control *hpi_ctl)
  1760. {
  1761. struct snd_card *card = asihpi->card;
  1762. struct snd_kcontrol_new snd_control;
  1763. snd_control.private_value = hpi_ctl->h_control;
  1764. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1765. if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
  1766. asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
  1767. snd_control.info = snd_asihpi_tuner_gain_info;
  1768. snd_control.get = snd_asihpi_tuner_gain_get;
  1769. snd_control.put = snd_asihpi_tuner_gain_put;
  1770. if (ctl_add(card, &snd_control, asihpi) < 0)
  1771. return -EINVAL;
  1772. }
  1773. asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
  1774. snd_control.info = snd_asihpi_tuner_band_info;
  1775. snd_control.get = snd_asihpi_tuner_band_get;
  1776. snd_control.put = snd_asihpi_tuner_band_put;
  1777. if (ctl_add(card, &snd_control, asihpi) < 0)
  1778. return -EINVAL;
  1779. asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
  1780. snd_control.info = snd_asihpi_tuner_freq_info;
  1781. snd_control.get = snd_asihpi_tuner_freq_get;
  1782. snd_control.put = snd_asihpi_tuner_freq_put;
  1783. return ctl_add(card, &snd_control, asihpi);
  1784. }
  1785. /*------------------------------------------------------------
  1786. Meter controls
  1787. ------------------------------------------------------------*/
  1788. static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
  1789. struct snd_ctl_elem_info *uinfo)
  1790. {
  1791. u32 h_control = kcontrol->private_value;
  1792. u32 count;
  1793. u16 err;
  1794. err = hpi_meter_query_channels(h_control, &count);
  1795. if (err)
  1796. count = HPI_MAX_CHANNELS;
  1797. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1798. uinfo->count = count;
  1799. uinfo->value.integer.min = 0;
  1800. uinfo->value.integer.max = 0x7FFFFFFF;
  1801. return 0;
  1802. }
  1803. /* linear values for 10dB steps */
  1804. static int log2lin[] = {
  1805. 0x7FFFFFFF, /* 0dB */
  1806. 679093956,
  1807. 214748365,
  1808. 67909396,
  1809. 21474837,
  1810. 6790940,
  1811. 2147484, /* -60dB */
  1812. 679094,
  1813. 214748, /* -80 */
  1814. 67909,
  1815. 21475, /* -100 */
  1816. 6791,
  1817. 2147,
  1818. 679,
  1819. 214,
  1820. 68,
  1821. 21,
  1822. 7,
  1823. 2
  1824. };
  1825. static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
  1826. struct snd_ctl_elem_value *ucontrol)
  1827. {
  1828. u32 h_control = kcontrol->private_value;
  1829. short an_gain_mB[HPI_MAX_CHANNELS], i;
  1830. u16 err;
  1831. err = hpi_meter_get_peak(h_control, an_gain_mB);
  1832. for (i = 0; i < HPI_MAX_CHANNELS; i++) {
  1833. if (err) {
  1834. ucontrol->value.integer.value[i] = 0;
  1835. } else if (an_gain_mB[i] >= 0) {
  1836. ucontrol->value.integer.value[i] =
  1837. an_gain_mB[i] << 16;
  1838. } else {
  1839. /* -ve is log value in millibels < -60dB,
  1840. * convert to (roughly!) linear,
  1841. */
  1842. ucontrol->value.integer.value[i] =
  1843. log2lin[an_gain_mB[i] / -1000];
  1844. }
  1845. }
  1846. return 0;
  1847. }
  1848. static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
  1849. struct hpi_control *hpi_ctl, int subidx)
  1850. {
  1851. struct snd_card *card = asihpi->card;
  1852. struct snd_kcontrol_new snd_control;
  1853. asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
  1854. snd_control.access =
  1855. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  1856. snd_control.info = snd_asihpi_meter_info;
  1857. snd_control.get = snd_asihpi_meter_get;
  1858. snd_control.index = subidx;
  1859. return ctl_add(card, &snd_control, asihpi);
  1860. }
  1861. /*------------------------------------------------------------
  1862. Multiplexer controls
  1863. ------------------------------------------------------------*/
  1864. static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
  1865. {
  1866. u32 h_control = snd_control->private_value;
  1867. struct hpi_control hpi_ctl;
  1868. int s, err;
  1869. for (s = 0; s < 32; s++) {
  1870. err = hpi_multiplexer_query_source(h_control, s,
  1871. &hpi_ctl.
  1872. src_node_type,
  1873. &hpi_ctl.
  1874. src_node_index);
  1875. if (err)
  1876. break;
  1877. }
  1878. return s;
  1879. }
  1880. static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
  1881. struct snd_ctl_elem_info *uinfo)
  1882. {
  1883. int err;
  1884. u16 src_node_type, src_node_index;
  1885. u32 h_control = kcontrol->private_value;
  1886. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1887. uinfo->count = 1;
  1888. uinfo->value.enumerated.items =
  1889. snd_card_asihpi_mux_count_sources(kcontrol);
  1890. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  1891. uinfo->value.enumerated.item =
  1892. uinfo->value.enumerated.items - 1;
  1893. err =
  1894. hpi_multiplexer_query_source(h_control,
  1895. uinfo->value.enumerated.item,
  1896. &src_node_type, &src_node_index);
  1897. sprintf(uinfo->value.enumerated.name, "%s %d",
  1898. asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
  1899. src_node_index);
  1900. return 0;
  1901. }
  1902. static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
  1903. struct snd_ctl_elem_value *ucontrol)
  1904. {
  1905. u32 h_control = kcontrol->private_value;
  1906. u16 source_type, source_index;
  1907. u16 src_node_type, src_node_index;
  1908. int s;
  1909. hpi_handle_error(hpi_multiplexer_get_source(h_control,
  1910. &source_type, &source_index));
  1911. /* Should cache this search result! */
  1912. for (s = 0; s < 256; s++) {
  1913. if (hpi_multiplexer_query_source(h_control, s,
  1914. &src_node_type, &src_node_index))
  1915. break;
  1916. if ((source_type == src_node_type)
  1917. && (source_index == src_node_index)) {
  1918. ucontrol->value.enumerated.item[0] = s;
  1919. return 0;
  1920. }
  1921. }
  1922. snd_printd(KERN_WARNING
  1923. "Control %x failed to match mux source %hu %hu\n",
  1924. h_control, source_type, source_index);
  1925. ucontrol->value.enumerated.item[0] = 0;
  1926. return 0;
  1927. }
  1928. static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
  1929. struct snd_ctl_elem_value *ucontrol)
  1930. {
  1931. int change;
  1932. u32 h_control = kcontrol->private_value;
  1933. u16 source_type, source_index;
  1934. u16 e;
  1935. change = 1;
  1936. e = hpi_multiplexer_query_source(h_control,
  1937. ucontrol->value.enumerated.item[0],
  1938. &source_type, &source_index);
  1939. if (!e)
  1940. hpi_handle_error(
  1941. hpi_multiplexer_set_source(h_control,
  1942. source_type, source_index));
  1943. return change;
  1944. }
  1945. static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
  1946. struct hpi_control *hpi_ctl)
  1947. {
  1948. struct snd_card *card = asihpi->card;
  1949. struct snd_kcontrol_new snd_control;
  1950. asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
  1951. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1952. snd_control.info = snd_asihpi_mux_info;
  1953. snd_control.get = snd_asihpi_mux_get;
  1954. snd_control.put = snd_asihpi_mux_put;
  1955. return ctl_add(card, &snd_control, asihpi);
  1956. }
  1957. /*------------------------------------------------------------
  1958. Channel mode controls
  1959. ------------------------------------------------------------*/
  1960. static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
  1961. struct snd_ctl_elem_info *uinfo)
  1962. {
  1963. static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
  1964. "invalid",
  1965. "Normal", "Swap",
  1966. "From Left", "From Right",
  1967. "To Left", "To Right"
  1968. };
  1969. u32 h_control = kcontrol->private_value;
  1970. u16 mode;
  1971. int i;
  1972. const char *mapped_names[6];
  1973. int valid_modes = 0;
  1974. /* HPI channel mode values can be from 1 to 6
  1975. Some adapters only support a contiguous subset
  1976. */
  1977. for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
  1978. if (!hpi_channel_mode_query_mode(
  1979. h_control, i, &mode)) {
  1980. mapped_names[valid_modes] = mode_names[mode];
  1981. valid_modes++;
  1982. }
  1983. if (!valid_modes)
  1984. return -EINVAL;
  1985. return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
  1986. }
  1987. static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
  1988. struct snd_ctl_elem_value *ucontrol)
  1989. {
  1990. u32 h_control = kcontrol->private_value;
  1991. u16 mode;
  1992. if (hpi_channel_mode_get(h_control, &mode))
  1993. mode = 1;
  1994. ucontrol->value.enumerated.item[0] = mode - 1;
  1995. return 0;
  1996. }
  1997. static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
  1998. struct snd_ctl_elem_value *ucontrol)
  1999. {
  2000. int change;
  2001. u32 h_control = kcontrol->private_value;
  2002. change = 1;
  2003. hpi_handle_error(hpi_channel_mode_set(h_control,
  2004. ucontrol->value.enumerated.item[0] + 1));
  2005. return change;
  2006. }
  2007. static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
  2008. struct hpi_control *hpi_ctl)
  2009. {
  2010. struct snd_card *card = asihpi->card;
  2011. struct snd_kcontrol_new snd_control;
  2012. asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
  2013. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  2014. snd_control.info = snd_asihpi_cmode_info;
  2015. snd_control.get = snd_asihpi_cmode_get;
  2016. snd_control.put = snd_asihpi_cmode_put;
  2017. return ctl_add(card, &snd_control, asihpi);
  2018. }
  2019. /*------------------------------------------------------------
  2020. Sampleclock source controls
  2021. ------------------------------------------------------------*/
  2022. static const char * const sampleclock_sources[] = {
  2023. "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
  2024. "SMPTE", "Digital1", "Auto", "Network", "Invalid",
  2025. "Prev Module", "BLU-Link",
  2026. "Digital2", "Digital3", "Digital4", "Digital5",
  2027. "Digital6", "Digital7", "Digital8"};
  2028. /* Number of strings must match expected enumerated values */
  2029. compile_time_assert(
  2030. (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
  2031. assert_sampleclock_sources_size);
  2032. static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
  2033. struct snd_ctl_elem_info *uinfo)
  2034. {
  2035. struct snd_card_asihpi *asihpi =
  2036. (struct snd_card_asihpi *)(kcontrol->private_data);
  2037. struct clk_cache *clkcache = &asihpi->cc;
  2038. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2039. uinfo->count = 1;
  2040. uinfo->value.enumerated.items = clkcache->count;
  2041. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2042. uinfo->value.enumerated.item =
  2043. uinfo->value.enumerated.items - 1;
  2044. strcpy(uinfo->value.enumerated.name,
  2045. clkcache->s[uinfo->value.enumerated.item].name);
  2046. return 0;
  2047. }
  2048. static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
  2049. struct snd_ctl_elem_value *ucontrol)
  2050. {
  2051. struct snd_card_asihpi *asihpi =
  2052. (struct snd_card_asihpi *)(kcontrol->private_data);
  2053. struct clk_cache *clkcache = &asihpi->cc;
  2054. u32 h_control = kcontrol->private_value;
  2055. u16 source, srcindex = 0;
  2056. int i;
  2057. ucontrol->value.enumerated.item[0] = 0;
  2058. if (hpi_sample_clock_get_source(h_control, &source))
  2059. source = 0;
  2060. if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2061. if (hpi_sample_clock_get_source_index(h_control, &srcindex))
  2062. srcindex = 0;
  2063. for (i = 0; i < clkcache->count; i++)
  2064. if ((clkcache->s[i].source == source) &&
  2065. (clkcache->s[i].index == srcindex))
  2066. break;
  2067. ucontrol->value.enumerated.item[0] = i;
  2068. return 0;
  2069. }
  2070. static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
  2071. struct snd_ctl_elem_value *ucontrol)
  2072. {
  2073. struct snd_card_asihpi *asihpi =
  2074. (struct snd_card_asihpi *)(kcontrol->private_data);
  2075. struct clk_cache *clkcache = &asihpi->cc;
  2076. unsigned int item;
  2077. int change;
  2078. u32 h_control = kcontrol->private_value;
  2079. change = 1;
  2080. item = ucontrol->value.enumerated.item[0];
  2081. if (item >= clkcache->count)
  2082. item = clkcache->count-1;
  2083. hpi_handle_error(hpi_sample_clock_set_source(
  2084. h_control, clkcache->s[item].source));
  2085. if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2086. hpi_handle_error(hpi_sample_clock_set_source_index(
  2087. h_control, clkcache->s[item].index));
  2088. return change;
  2089. }
  2090. /*------------------------------------------------------------
  2091. Clkrate controls
  2092. ------------------------------------------------------------*/
  2093. /* Need to change this to enumerated control with list of rates */
  2094. static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
  2095. struct snd_ctl_elem_info *uinfo)
  2096. {
  2097. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2098. uinfo->count = 1;
  2099. uinfo->value.integer.min = 8000;
  2100. uinfo->value.integer.max = 192000;
  2101. uinfo->value.integer.step = 100;
  2102. return 0;
  2103. }
  2104. static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
  2105. struct snd_ctl_elem_value *ucontrol)
  2106. {
  2107. u32 h_control = kcontrol->private_value;
  2108. u32 rate;
  2109. u16 e;
  2110. e = hpi_sample_clock_get_local_rate(h_control, &rate);
  2111. if (!e)
  2112. ucontrol->value.integer.value[0] = rate;
  2113. else
  2114. ucontrol->value.integer.value[0] = 0;
  2115. return 0;
  2116. }
  2117. static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
  2118. struct snd_ctl_elem_value *ucontrol)
  2119. {
  2120. int change;
  2121. u32 h_control = kcontrol->private_value;
  2122. /* change = asihpi->mixer_clkrate[addr][0] != left ||
  2123. asihpi->mixer_clkrate[addr][1] != right;
  2124. */
  2125. change = 1;
  2126. hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
  2127. ucontrol->value.integer.value[0]));
  2128. return change;
  2129. }
  2130. static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
  2131. struct snd_ctl_elem_info *uinfo)
  2132. {
  2133. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2134. uinfo->count = 1;
  2135. uinfo->value.integer.min = 8000;
  2136. uinfo->value.integer.max = 192000;
  2137. uinfo->value.integer.step = 100;
  2138. return 0;
  2139. }
  2140. static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
  2141. struct snd_ctl_elem_value *ucontrol)
  2142. {
  2143. u32 h_control = kcontrol->private_value;
  2144. u32 rate;
  2145. u16 e;
  2146. e = hpi_sample_clock_get_sample_rate(h_control, &rate);
  2147. if (!e)
  2148. ucontrol->value.integer.value[0] = rate;
  2149. else
  2150. ucontrol->value.integer.value[0] = 0;
  2151. return 0;
  2152. }
  2153. static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
  2154. struct hpi_control *hpi_ctl)
  2155. {
  2156. struct snd_card *card;
  2157. struct snd_kcontrol_new snd_control;
  2158. struct clk_cache *clkcache;
  2159. u32 hSC = hpi_ctl->h_control;
  2160. int has_aes_in = 0;
  2161. int i, j;
  2162. u16 source;
  2163. if (snd_BUG_ON(!asihpi))
  2164. return -EINVAL;
  2165. card = asihpi->card;
  2166. clkcache = &asihpi->cc;
  2167. snd_control.private_value = hpi_ctl->h_control;
  2168. clkcache->has_local = 0;
  2169. for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
  2170. if (hpi_sample_clock_query_source(hSC,
  2171. i, &source))
  2172. break;
  2173. clkcache->s[i].source = source;
  2174. clkcache->s[i].index = 0;
  2175. clkcache->s[i].name = sampleclock_sources[source];
  2176. if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2177. has_aes_in = 1;
  2178. if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
  2179. clkcache->has_local = 1;
  2180. }
  2181. if (has_aes_in)
  2182. /* already will have picked up index 0 above */
  2183. for (j = 1; j < 8; j++) {
  2184. if (hpi_sample_clock_query_source_index(hSC,
  2185. j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
  2186. &source))
  2187. break;
  2188. clkcache->s[i].source =
  2189. HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
  2190. clkcache->s[i].index = j;
  2191. clkcache->s[i].name = sampleclock_sources[
  2192. j+HPI_SAMPLECLOCK_SOURCE_LAST];
  2193. i++;
  2194. }
  2195. clkcache->count = i;
  2196. asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
  2197. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  2198. snd_control.info = snd_asihpi_clksrc_info;
  2199. snd_control.get = snd_asihpi_clksrc_get;
  2200. snd_control.put = snd_asihpi_clksrc_put;
  2201. if (ctl_add(card, &snd_control, asihpi) < 0)
  2202. return -EINVAL;
  2203. if (clkcache->has_local) {
  2204. asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
  2205. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  2206. snd_control.info = snd_asihpi_clklocal_info;
  2207. snd_control.get = snd_asihpi_clklocal_get;
  2208. snd_control.put = snd_asihpi_clklocal_put;
  2209. if (ctl_add(card, &snd_control, asihpi) < 0)
  2210. return -EINVAL;
  2211. }
  2212. asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
  2213. snd_control.access =
  2214. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  2215. snd_control.info = snd_asihpi_clkrate_info;
  2216. snd_control.get = snd_asihpi_clkrate_get;
  2217. return ctl_add(card, &snd_control, asihpi);
  2218. }
  2219. /*------------------------------------------------------------
  2220. Mixer
  2221. ------------------------------------------------------------*/
  2222. static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
  2223. {
  2224. struct snd_card *card;
  2225. unsigned int idx = 0;
  2226. unsigned int subindex = 0;
  2227. int err;
  2228. struct hpi_control hpi_ctl, prev_ctl;
  2229. if (snd_BUG_ON(!asihpi))
  2230. return -EINVAL;
  2231. card = asihpi->card;
  2232. strcpy(card->mixername, "Asihpi Mixer");
  2233. err =
  2234. hpi_mixer_open(asihpi->hpi->adapter->index,
  2235. &asihpi->h_mixer);
  2236. hpi_handle_error(err);
  2237. if (err)
  2238. return -err;
  2239. memset(&prev_ctl, 0, sizeof(prev_ctl));
  2240. prev_ctl.control_type = -1;
  2241. for (idx = 0; idx < 2000; idx++) {
  2242. err = hpi_mixer_get_control_by_index(
  2243. asihpi->h_mixer,
  2244. idx,
  2245. &hpi_ctl.src_node_type,
  2246. &hpi_ctl.src_node_index,
  2247. &hpi_ctl.dst_node_type,
  2248. &hpi_ctl.dst_node_index,
  2249. &hpi_ctl.control_type,
  2250. &hpi_ctl.h_control);
  2251. if (err) {
  2252. if (err == HPI_ERROR_CONTROL_DISABLED) {
  2253. if (mixer_dump)
  2254. dev_info(&asihpi->pci->dev,
  2255. "Disabled HPI Control(%d)\n",
  2256. idx);
  2257. continue;
  2258. } else
  2259. break;
  2260. }
  2261. hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
  2262. hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
  2263. /* ASI50xx in SSX mode has multiple meters on the same node.
  2264. Use subindex to create distinct ALSA controls
  2265. for any duplicated controls.
  2266. */
  2267. if ((hpi_ctl.control_type == prev_ctl.control_type) &&
  2268. (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
  2269. (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
  2270. (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
  2271. (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
  2272. subindex++;
  2273. else
  2274. subindex = 0;
  2275. prev_ctl = hpi_ctl;
  2276. switch (hpi_ctl.control_type) {
  2277. case HPI_CONTROL_VOLUME:
  2278. err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
  2279. break;
  2280. case HPI_CONTROL_LEVEL:
  2281. err = snd_asihpi_level_add(asihpi, &hpi_ctl);
  2282. break;
  2283. case HPI_CONTROL_MULTIPLEXER:
  2284. err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
  2285. break;
  2286. case HPI_CONTROL_CHANNEL_MODE:
  2287. err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
  2288. break;
  2289. case HPI_CONTROL_METER:
  2290. err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
  2291. break;
  2292. case HPI_CONTROL_SAMPLECLOCK:
  2293. err = snd_asihpi_sampleclock_add(
  2294. asihpi, &hpi_ctl);
  2295. break;
  2296. case HPI_CONTROL_CONNECTION: /* ignore these */
  2297. continue;
  2298. case HPI_CONTROL_TUNER:
  2299. err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
  2300. break;
  2301. case HPI_CONTROL_AESEBU_TRANSMITTER:
  2302. err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
  2303. break;
  2304. case HPI_CONTROL_AESEBU_RECEIVER:
  2305. err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
  2306. break;
  2307. case HPI_CONTROL_VOX:
  2308. case HPI_CONTROL_BITSTREAM:
  2309. case HPI_CONTROL_MICROPHONE:
  2310. case HPI_CONTROL_PARAMETRIC_EQ:
  2311. case HPI_CONTROL_COMPANDER:
  2312. default:
  2313. if (mixer_dump)
  2314. dev_info(&asihpi->pci->dev,
  2315. "Untranslated HPI Control (%d) %d %d %d %d %d\n",
  2316. idx,
  2317. hpi_ctl.control_type,
  2318. hpi_ctl.src_node_type,
  2319. hpi_ctl.src_node_index,
  2320. hpi_ctl.dst_node_type,
  2321. hpi_ctl.dst_node_index);
  2322. continue;
  2323. }
  2324. if (err < 0)
  2325. return err;
  2326. }
  2327. if (HPI_ERROR_INVALID_OBJ_INDEX != err)
  2328. hpi_handle_error(err);
  2329. dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
  2330. return 0;
  2331. }
  2332. /*------------------------------------------------------------
  2333. /proc interface
  2334. ------------------------------------------------------------*/
  2335. static void
  2336. snd_asihpi_proc_read(struct snd_info_entry *entry,
  2337. struct snd_info_buffer *buffer)
  2338. {
  2339. struct snd_card_asihpi *asihpi = entry->private_data;
  2340. u32 h_control;
  2341. u32 rate = 0;
  2342. u16 source = 0;
  2343. u16 num_outstreams;
  2344. u16 num_instreams;
  2345. u16 version;
  2346. u32 serial_number;
  2347. u16 type;
  2348. int err;
  2349. snd_iprintf(buffer, "ASIHPI driver proc file\n");
  2350. hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
  2351. &num_outstreams, &num_instreams,
  2352. &version, &serial_number, &type));
  2353. snd_iprintf(buffer,
  2354. "Adapter type ASI%4X\nHardware Index %d\n"
  2355. "%d outstreams\n%d instreams\n",
  2356. type, asihpi->hpi->adapter->index,
  2357. num_outstreams, num_instreams);
  2358. snd_iprintf(buffer,
  2359. "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
  2360. serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
  2361. ((version >> 13) * 100) + ((version >> 7) & 0x3f));
  2362. err = hpi_mixer_get_control(asihpi->h_mixer,
  2363. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  2364. HPI_CONTROL_SAMPLECLOCK, &h_control);
  2365. if (!err) {
  2366. err = hpi_sample_clock_get_sample_rate(h_control, &rate);
  2367. err += hpi_sample_clock_get_source(h_control, &source);
  2368. if (!err)
  2369. snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
  2370. rate, sampleclock_sources[source]);
  2371. }
  2372. }
  2373. static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
  2374. {
  2375. struct snd_info_entry *entry;
  2376. if (!snd_card_proc_new(asihpi->card, "info", &entry))
  2377. snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
  2378. }
  2379. /*------------------------------------------------------------
  2380. HWDEP
  2381. ------------------------------------------------------------*/
  2382. static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
  2383. {
  2384. if (enable_hpi_hwdep)
  2385. return 0;
  2386. else
  2387. return -ENODEV;
  2388. }
  2389. static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
  2390. {
  2391. if (enable_hpi_hwdep)
  2392. return asihpi_hpi_release(file);
  2393. else
  2394. return -ENODEV;
  2395. }
  2396. static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
  2397. unsigned int cmd, unsigned long arg)
  2398. {
  2399. if (enable_hpi_hwdep)
  2400. return asihpi_hpi_ioctl(file, cmd, arg);
  2401. else
  2402. return -ENODEV;
  2403. }
  2404. /* results in /dev/snd/hwC#D0 file for each card with index #
  2405. also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
  2406. */
  2407. static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
  2408. {
  2409. struct snd_hwdep *hw;
  2410. int err;
  2411. err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
  2412. if (err < 0)
  2413. return err;
  2414. strcpy(hw->name, "asihpi (HPI)");
  2415. hw->iface = SNDRV_HWDEP_IFACE_LAST;
  2416. hw->ops.open = snd_asihpi_hpi_open;
  2417. hw->ops.ioctl = snd_asihpi_hpi_ioctl;
  2418. hw->ops.release = snd_asihpi_hpi_release;
  2419. hw->private_data = asihpi;
  2420. return 0;
  2421. }
  2422. /*------------------------------------------------------------
  2423. CARD
  2424. ------------------------------------------------------------*/
  2425. static int snd_asihpi_probe(struct pci_dev *pci_dev,
  2426. const struct pci_device_id *pci_id)
  2427. {
  2428. int err;
  2429. struct hpi_adapter *hpi;
  2430. struct snd_card *card;
  2431. struct snd_card_asihpi *asihpi;
  2432. u32 h_control;
  2433. u32 h_stream;
  2434. u32 adapter_index;
  2435. static int dev;
  2436. if (dev >= SNDRV_CARDS)
  2437. return -ENODEV;
  2438. /* Should this be enable[hpi->index] ? */
  2439. if (!enable[dev]) {
  2440. dev++;
  2441. return -ENOENT;
  2442. }
  2443. /* Initialise low-level HPI driver */
  2444. err = asihpi_adapter_probe(pci_dev, pci_id);
  2445. if (err < 0)
  2446. return err;
  2447. hpi = pci_get_drvdata(pci_dev);
  2448. adapter_index = hpi->adapter->index;
  2449. /* first try to give the card the same index as its hardware index */
  2450. err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
  2451. THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
  2452. if (err < 0) {
  2453. /* if that fails, try the default index==next available */
  2454. err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
  2455. THIS_MODULE, sizeof(struct snd_card_asihpi),
  2456. &card);
  2457. if (err < 0)
  2458. return err;
  2459. dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
  2460. adapter_index, card->number);
  2461. }
  2462. asihpi = card->private_data;
  2463. asihpi->card = card;
  2464. asihpi->pci = pci_dev;
  2465. asihpi->hpi = hpi;
  2466. hpi->snd_card = card;
  2467. err = hpi_adapter_get_property(adapter_index,
  2468. HPI_ADAPTER_PROPERTY_CAPS1,
  2469. NULL, &asihpi->support_grouping);
  2470. if (err)
  2471. asihpi->support_grouping = 0;
  2472. err = hpi_adapter_get_property(adapter_index,
  2473. HPI_ADAPTER_PROPERTY_CAPS2,
  2474. &asihpi->support_mrx, NULL);
  2475. if (err)
  2476. asihpi->support_mrx = 0;
  2477. err = hpi_adapter_get_property(adapter_index,
  2478. HPI_ADAPTER_PROPERTY_INTERVAL,
  2479. NULL, &asihpi->update_interval_frames);
  2480. if (err)
  2481. asihpi->update_interval_frames = 512;
  2482. if (hpi->interrupt_mode) {
  2483. asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
  2484. asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
  2485. tasklet_init(&asihpi->t, snd_card_asihpi_int_task,
  2486. (unsigned long)hpi);
  2487. hpi->interrupt_callback = snd_card_asihpi_isr;
  2488. } else {
  2489. asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
  2490. asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
  2491. }
  2492. hpi_handle_error(hpi_instream_open(adapter_index,
  2493. 0, &h_stream));
  2494. err = hpi_instream_host_buffer_free(h_stream);
  2495. asihpi->can_dma = (!err);
  2496. hpi_handle_error(hpi_instream_close(h_stream));
  2497. if (!asihpi->can_dma)
  2498. asihpi->update_interval_frames *= 2;
  2499. err = hpi_adapter_get_property(adapter_index,
  2500. HPI_ADAPTER_PROPERTY_CURCHANNELS,
  2501. &asihpi->in_max_chans, &asihpi->out_max_chans);
  2502. if (err) {
  2503. asihpi->in_max_chans = 2;
  2504. asihpi->out_max_chans = 2;
  2505. }
  2506. if (asihpi->out_max_chans > 2) { /* assume LL mode */
  2507. asihpi->out_min_chans = asihpi->out_max_chans;
  2508. asihpi->in_min_chans = asihpi->in_max_chans;
  2509. asihpi->support_grouping = 0;
  2510. } else {
  2511. asihpi->out_min_chans = 1;
  2512. asihpi->in_min_chans = 1;
  2513. }
  2514. dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
  2515. asihpi->can_dma,
  2516. asihpi->support_grouping,
  2517. asihpi->support_mrx,
  2518. asihpi->update_interval_frames
  2519. );
  2520. err = snd_card_asihpi_pcm_new(asihpi, 0);
  2521. if (err < 0) {
  2522. dev_err(&pci_dev->dev, "pcm_new failed\n");
  2523. goto __nodev;
  2524. }
  2525. err = snd_card_asihpi_mixer_new(asihpi);
  2526. if (err < 0) {
  2527. dev_err(&pci_dev->dev, "mixer_new failed\n");
  2528. goto __nodev;
  2529. }
  2530. err = hpi_mixer_get_control(asihpi->h_mixer,
  2531. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  2532. HPI_CONTROL_SAMPLECLOCK, &h_control);
  2533. if (!err)
  2534. err = hpi_sample_clock_set_local_rate(
  2535. h_control, adapter_fs);
  2536. snd_asihpi_proc_init(asihpi);
  2537. /* always create, can be enabled or disabled dynamically
  2538. by enable_hwdep module param*/
  2539. snd_asihpi_hpi_new(asihpi, 0);
  2540. strcpy(card->driver, "ASIHPI");
  2541. sprintf(card->shortname, "AudioScience ASI%4X",
  2542. asihpi->hpi->adapter->type);
  2543. sprintf(card->longname, "%s %i",
  2544. card->shortname, adapter_index);
  2545. err = snd_card_register(card);
  2546. if (!err) {
  2547. dev++;
  2548. return 0;
  2549. }
  2550. __nodev:
  2551. snd_card_free(card);
  2552. dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
  2553. return err;
  2554. }
  2555. static void snd_asihpi_remove(struct pci_dev *pci_dev)
  2556. {
  2557. struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
  2558. struct snd_card_asihpi *asihpi = hpi->snd_card->private_data;
  2559. /* Stop interrupts */
  2560. if (hpi->interrupt_mode) {
  2561. hpi->interrupt_callback = NULL;
  2562. hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
  2563. HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
  2564. tasklet_kill(&asihpi->t);
  2565. }
  2566. snd_card_free(hpi->snd_card);
  2567. hpi->snd_card = NULL;
  2568. asihpi_adapter_remove(pci_dev);
  2569. }
  2570. static const struct pci_device_id asihpi_pci_tbl[] = {
  2571. {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
  2572. HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  2573. (kernel_ulong_t)HPI_6205},
  2574. {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
  2575. HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  2576. (kernel_ulong_t)HPI_6000},
  2577. {0,}
  2578. };
  2579. MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
  2580. static struct pci_driver driver = {
  2581. .name = KBUILD_MODNAME,
  2582. .id_table = asihpi_pci_tbl,
  2583. .probe = snd_asihpi_probe,
  2584. .remove = snd_asihpi_remove,
  2585. };
  2586. static int __init snd_asihpi_init(void)
  2587. {
  2588. asihpi_init();
  2589. return pci_register_driver(&driver);
  2590. }
  2591. static void __exit snd_asihpi_exit(void)
  2592. {
  2593. pci_unregister_driver(&driver);
  2594. asihpi_exit();
  2595. }
  2596. module_init(snd_asihpi_init)
  2597. module_exit(snd_asihpi_exit)