asihpi.c 83 KB

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