soc-ops.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * soc-ops.c -- Generic ASoC operations
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. * Copyright (C) 2010 Slimlogic Ltd.
  7. * Copyright (C) 2010 Texas Instruments Inc.
  8. *
  9. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  10. * with code, comments and ideas from :-
  11. * Richard Purdie <richard@openedhand.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/pm.h>
  23. #include <linux/bitops.h>
  24. #include <linux/ctype.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/jack.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/soc.h>
  31. #include <sound/soc-dpcm.h>
  32. #include <sound/initval.h>
  33. /**
  34. * snd_soc_info_enum_double - enumerated double mixer info callback
  35. * @kcontrol: mixer control
  36. * @uinfo: control element information
  37. *
  38. * Callback to provide information about a double enumerated
  39. * mixer control.
  40. *
  41. * Returns 0 for success.
  42. */
  43. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  44. struct snd_ctl_elem_info *uinfo)
  45. {
  46. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  47. return snd_ctl_enum_info(uinfo, e->shift_l == e->shift_r ? 1 : 2,
  48. e->items, e->texts);
  49. }
  50. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  51. /**
  52. * snd_soc_get_enum_double - enumerated double mixer get callback
  53. * @kcontrol: mixer control
  54. * @ucontrol: control element information
  55. *
  56. * Callback to get the value of a double enumerated mixer.
  57. *
  58. * Returns 0 for success.
  59. */
  60. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  61. struct snd_ctl_elem_value *ucontrol)
  62. {
  63. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  64. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  65. unsigned int val, item;
  66. unsigned int reg_val;
  67. int ret;
  68. ret = snd_soc_component_read(component, e->reg, &reg_val);
  69. if (ret)
  70. return ret;
  71. val = (reg_val >> e->shift_l) & e->mask;
  72. item = snd_soc_enum_val_to_item(e, val);
  73. ucontrol->value.enumerated.item[0] = item;
  74. if (e->shift_l != e->shift_r) {
  75. val = (reg_val >> e->shift_l) & e->mask;
  76. item = snd_soc_enum_val_to_item(e, val);
  77. ucontrol->value.enumerated.item[1] = item;
  78. }
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  82. /**
  83. * snd_soc_put_enum_double - enumerated double mixer put callback
  84. * @kcontrol: mixer control
  85. * @ucontrol: control element information
  86. *
  87. * Callback to set the value of a double enumerated mixer.
  88. *
  89. * Returns 0 for success.
  90. */
  91. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  92. struct snd_ctl_elem_value *ucontrol)
  93. {
  94. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  95. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  96. unsigned int *item = ucontrol->value.enumerated.item;
  97. unsigned int val;
  98. unsigned int mask;
  99. if (item[0] >= e->items)
  100. return -EINVAL;
  101. val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
  102. mask = e->mask << e->shift_l;
  103. if (e->shift_l != e->shift_r) {
  104. if (item[1] >= e->items)
  105. return -EINVAL;
  106. val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
  107. mask |= e->mask << e->shift_r;
  108. }
  109. return snd_soc_component_update_bits(component, e->reg, mask, val);
  110. }
  111. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  112. /**
  113. * snd_soc_read_signed - Read a codec register and interprete as signed value
  114. * @component: component
  115. * @reg: Register to read
  116. * @mask: Mask to use after shifting the register value
  117. * @shift: Right shift of register value
  118. * @sign_bit: Bit that describes if a number is negative or not.
  119. * @signed_val: Pointer to where the read value should be stored
  120. *
  121. * This functions reads a codec register. The register value is shifted right
  122. * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
  123. * the given registervalue into a signed integer if sign_bit is non-zero.
  124. *
  125. * Returns 0 on sucess, otherwise an error value
  126. */
  127. static int snd_soc_read_signed(struct snd_soc_component *component,
  128. unsigned int reg, unsigned int mask, unsigned int shift,
  129. unsigned int sign_bit, int *signed_val)
  130. {
  131. int ret;
  132. unsigned int val;
  133. ret = snd_soc_component_read(component, reg, &val);
  134. if (ret < 0)
  135. return ret;
  136. val = (val >> shift) & mask;
  137. if (!sign_bit) {
  138. *signed_val = val;
  139. return 0;
  140. }
  141. /* non-negative number */
  142. if (!(val & BIT(sign_bit))) {
  143. *signed_val = val;
  144. return 0;
  145. }
  146. ret = val;
  147. /*
  148. * The register most probably does not contain a full-sized int.
  149. * Instead we have an arbitrary number of bits in a signed
  150. * representation which has to be translated into a full-sized int.
  151. * This is done by filling up all bits above the sign-bit.
  152. */
  153. ret |= ~((int)(BIT(sign_bit) - 1));
  154. *signed_val = ret;
  155. return 0;
  156. }
  157. /**
  158. * snd_soc_info_volsw - single mixer info callback
  159. * @kcontrol: mixer control
  160. * @uinfo: control element information
  161. *
  162. * Callback to provide information about a single mixer control, or a double
  163. * mixer control that spans 2 registers.
  164. *
  165. * Returns 0 for success.
  166. */
  167. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  168. struct snd_ctl_elem_info *uinfo)
  169. {
  170. struct soc_mixer_control *mc =
  171. (struct soc_mixer_control *)kcontrol->private_value;
  172. int platform_max;
  173. if (!mc->platform_max)
  174. mc->platform_max = mc->max;
  175. platform_max = mc->platform_max;
  176. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  177. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  178. else
  179. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  180. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  181. uinfo->value.integer.min = 0;
  182. uinfo->value.integer.max = platform_max - mc->min;
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  186. /**
  187. * snd_soc_get_volsw - single mixer get callback
  188. * @kcontrol: mixer control
  189. * @ucontrol: control element information
  190. *
  191. * Callback to get the value of a single mixer control, or a double mixer
  192. * control that spans 2 registers.
  193. *
  194. * Returns 0 for success.
  195. */
  196. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  197. struct snd_ctl_elem_value *ucontrol)
  198. {
  199. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  200. struct soc_mixer_control *mc =
  201. (struct soc_mixer_control *)kcontrol->private_value;
  202. unsigned int reg = mc->reg;
  203. unsigned int reg2 = mc->rreg;
  204. unsigned int shift = mc->shift;
  205. unsigned int rshift = mc->rshift;
  206. int max = mc->max;
  207. int min = mc->min;
  208. int sign_bit = mc->sign_bit;
  209. unsigned int mask = (1 << fls(max)) - 1;
  210. unsigned int invert = mc->invert;
  211. int val;
  212. int ret;
  213. if (sign_bit)
  214. mask = BIT(sign_bit + 1) - 1;
  215. ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
  216. if (ret)
  217. return ret;
  218. ucontrol->value.integer.value[0] = val - min;
  219. if (invert)
  220. ucontrol->value.integer.value[0] =
  221. max - ucontrol->value.integer.value[0];
  222. if (snd_soc_volsw_is_stereo(mc)) {
  223. if (reg == reg2)
  224. ret = snd_soc_read_signed(component, reg, mask, rshift,
  225. sign_bit, &val);
  226. else
  227. ret = snd_soc_read_signed(component, reg2, mask, shift,
  228. sign_bit, &val);
  229. if (ret)
  230. return ret;
  231. ucontrol->value.integer.value[1] = val - min;
  232. if (invert)
  233. ucontrol->value.integer.value[1] =
  234. max - ucontrol->value.integer.value[1];
  235. }
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  239. /**
  240. * snd_soc_put_volsw - single mixer put callback
  241. * @kcontrol: mixer control
  242. * @ucontrol: control element information
  243. *
  244. * Callback to set the value of a single mixer control, or a double mixer
  245. * control that spans 2 registers.
  246. *
  247. * Returns 0 for success.
  248. */
  249. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  250. struct snd_ctl_elem_value *ucontrol)
  251. {
  252. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  253. struct soc_mixer_control *mc =
  254. (struct soc_mixer_control *)kcontrol->private_value;
  255. unsigned int reg = mc->reg;
  256. unsigned int reg2 = mc->rreg;
  257. unsigned int shift = mc->shift;
  258. unsigned int rshift = mc->rshift;
  259. int max = mc->max;
  260. int min = mc->min;
  261. unsigned int sign_bit = mc->sign_bit;
  262. unsigned int mask = (1 << fls(max)) - 1;
  263. unsigned int invert = mc->invert;
  264. int err;
  265. bool type_2r = false;
  266. unsigned int val2 = 0;
  267. unsigned int val, val_mask;
  268. if (sign_bit)
  269. mask = BIT(sign_bit + 1) - 1;
  270. val = ((ucontrol->value.integer.value[0] + min) & mask);
  271. if (invert)
  272. val = max - val;
  273. val_mask = mask << shift;
  274. val = val << shift;
  275. if (snd_soc_volsw_is_stereo(mc)) {
  276. val2 = ((ucontrol->value.integer.value[1] + min) & mask);
  277. if (invert)
  278. val2 = max - val2;
  279. if (reg == reg2) {
  280. val_mask |= mask << rshift;
  281. val |= val2 << rshift;
  282. } else {
  283. val2 = val2 << shift;
  284. type_2r = true;
  285. }
  286. }
  287. err = snd_soc_component_update_bits(component, reg, val_mask, val);
  288. if (err < 0)
  289. return err;
  290. if (type_2r)
  291. err = snd_soc_component_update_bits(component, reg2, val_mask,
  292. val2);
  293. return err;
  294. }
  295. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  296. /**
  297. * snd_soc_get_volsw_sx - single mixer get callback
  298. * @kcontrol: mixer control
  299. * @ucontrol: control element information
  300. *
  301. * Callback to get the value of a single mixer control, or a double mixer
  302. * control that spans 2 registers.
  303. *
  304. * Returns 0 for success.
  305. */
  306. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  307. struct snd_ctl_elem_value *ucontrol)
  308. {
  309. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  310. struct soc_mixer_control *mc =
  311. (struct soc_mixer_control *)kcontrol->private_value;
  312. unsigned int reg = mc->reg;
  313. unsigned int reg2 = mc->rreg;
  314. unsigned int shift = mc->shift;
  315. unsigned int rshift = mc->rshift;
  316. int max = mc->max;
  317. int min = mc->min;
  318. int mask = (1 << (fls(min + max) - 1)) - 1;
  319. unsigned int val;
  320. int ret;
  321. ret = snd_soc_component_read(component, reg, &val);
  322. if (ret < 0)
  323. return ret;
  324. ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
  325. if (snd_soc_volsw_is_stereo(mc)) {
  326. ret = snd_soc_component_read(component, reg2, &val);
  327. if (ret < 0)
  328. return ret;
  329. val = ((val >> rshift) - min) & mask;
  330. ucontrol->value.integer.value[1] = val;
  331. }
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  335. /**
  336. * snd_soc_put_volsw_sx - double mixer set callback
  337. * @kcontrol: mixer control
  338. * @uinfo: control element information
  339. *
  340. * Callback to set the value of a double mixer control that spans 2 registers.
  341. *
  342. * Returns 0 for success.
  343. */
  344. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  345. struct snd_ctl_elem_value *ucontrol)
  346. {
  347. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  348. struct soc_mixer_control *mc =
  349. (struct soc_mixer_control *)kcontrol->private_value;
  350. unsigned int reg = mc->reg;
  351. unsigned int reg2 = mc->rreg;
  352. unsigned int shift = mc->shift;
  353. unsigned int rshift = mc->rshift;
  354. int max = mc->max;
  355. int min = mc->min;
  356. int mask = (1 << (fls(min + max) - 1)) - 1;
  357. int err = 0;
  358. unsigned int val, val_mask, val2 = 0;
  359. val_mask = mask << shift;
  360. val = (ucontrol->value.integer.value[0] + min) & mask;
  361. val = val << shift;
  362. err = snd_soc_component_update_bits(component, reg, val_mask, val);
  363. if (err < 0)
  364. return err;
  365. if (snd_soc_volsw_is_stereo(mc)) {
  366. val_mask = mask << rshift;
  367. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  368. val2 = val2 << rshift;
  369. err = snd_soc_component_update_bits(component, reg2, val_mask,
  370. val2);
  371. }
  372. return err;
  373. }
  374. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  375. /**
  376. * snd_soc_info_volsw_range - single mixer info callback with range.
  377. * @kcontrol: mixer control
  378. * @uinfo: control element information
  379. *
  380. * Callback to provide information, within a range, about a single
  381. * mixer control.
  382. *
  383. * returns 0 for success.
  384. */
  385. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  386. struct snd_ctl_elem_info *uinfo)
  387. {
  388. struct soc_mixer_control *mc =
  389. (struct soc_mixer_control *)kcontrol->private_value;
  390. int platform_max;
  391. int min = mc->min;
  392. if (!mc->platform_max)
  393. mc->platform_max = mc->max;
  394. platform_max = mc->platform_max;
  395. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  396. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  397. uinfo->value.integer.min = 0;
  398. uinfo->value.integer.max = platform_max - min;
  399. return 0;
  400. }
  401. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  402. /**
  403. * snd_soc_put_volsw_range - single mixer put value callback with range.
  404. * @kcontrol: mixer control
  405. * @ucontrol: control element information
  406. *
  407. * Callback to set the value, within a range, for a single mixer control.
  408. *
  409. * Returns 0 for success.
  410. */
  411. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  412. struct snd_ctl_elem_value *ucontrol)
  413. {
  414. struct soc_mixer_control *mc =
  415. (struct soc_mixer_control *)kcontrol->private_value;
  416. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  417. unsigned int reg = mc->reg;
  418. unsigned int rreg = mc->rreg;
  419. unsigned int shift = mc->shift;
  420. int min = mc->min;
  421. int max = mc->max;
  422. unsigned int mask = (1 << fls(max)) - 1;
  423. unsigned int invert = mc->invert;
  424. unsigned int val, val_mask;
  425. int ret;
  426. if (invert)
  427. val = (max - ucontrol->value.integer.value[0]) & mask;
  428. else
  429. val = ((ucontrol->value.integer.value[0] + min) & mask);
  430. val_mask = mask << shift;
  431. val = val << shift;
  432. ret = snd_soc_component_update_bits(component, reg, val_mask, val);
  433. if (ret < 0)
  434. return ret;
  435. if (snd_soc_volsw_is_stereo(mc)) {
  436. if (invert)
  437. val = (max - ucontrol->value.integer.value[1]) & mask;
  438. else
  439. val = ((ucontrol->value.integer.value[1] + min) & mask);
  440. val_mask = mask << shift;
  441. val = val << shift;
  442. ret = snd_soc_component_update_bits(component, rreg, val_mask,
  443. val);
  444. }
  445. return ret;
  446. }
  447. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  448. /**
  449. * snd_soc_get_volsw_range - single mixer get callback with range
  450. * @kcontrol: mixer control
  451. * @ucontrol: control element information
  452. *
  453. * Callback to get the value, within a range, of a single mixer control.
  454. *
  455. * Returns 0 for success.
  456. */
  457. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  458. struct snd_ctl_elem_value *ucontrol)
  459. {
  460. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  461. struct soc_mixer_control *mc =
  462. (struct soc_mixer_control *)kcontrol->private_value;
  463. unsigned int reg = mc->reg;
  464. unsigned int rreg = mc->rreg;
  465. unsigned int shift = mc->shift;
  466. int min = mc->min;
  467. int max = mc->max;
  468. unsigned int mask = (1 << fls(max)) - 1;
  469. unsigned int invert = mc->invert;
  470. unsigned int val;
  471. int ret;
  472. ret = snd_soc_component_read(component, reg, &val);
  473. if (ret)
  474. return ret;
  475. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  476. if (invert)
  477. ucontrol->value.integer.value[0] =
  478. max - ucontrol->value.integer.value[0];
  479. else
  480. ucontrol->value.integer.value[0] =
  481. ucontrol->value.integer.value[0] - min;
  482. if (snd_soc_volsw_is_stereo(mc)) {
  483. ret = snd_soc_component_read(component, rreg, &val);
  484. if (ret)
  485. return ret;
  486. ucontrol->value.integer.value[1] = (val >> shift) & mask;
  487. if (invert)
  488. ucontrol->value.integer.value[1] =
  489. max - ucontrol->value.integer.value[1];
  490. else
  491. ucontrol->value.integer.value[1] =
  492. ucontrol->value.integer.value[1] - min;
  493. }
  494. return 0;
  495. }
  496. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  497. /**
  498. * snd_soc_limit_volume - Set new limit to an existing volume control.
  499. *
  500. * @codec: where to look for the control
  501. * @name: Name of the control
  502. * @max: new maximum limit
  503. *
  504. * Return 0 for success, else error.
  505. */
  506. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  507. const char *name, int max)
  508. {
  509. struct snd_card *card = codec->component.card->snd_card;
  510. struct snd_kcontrol *kctl;
  511. struct soc_mixer_control *mc;
  512. int found = 0;
  513. int ret = -EINVAL;
  514. /* Sanity check for name and max */
  515. if (unlikely(!name || max <= 0))
  516. return -EINVAL;
  517. list_for_each_entry(kctl, &card->controls, list) {
  518. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  519. found = 1;
  520. break;
  521. }
  522. }
  523. if (found) {
  524. mc = (struct soc_mixer_control *)kctl->private_value;
  525. if (max <= mc->max) {
  526. mc->platform_max = max;
  527. ret = 0;
  528. }
  529. }
  530. return ret;
  531. }
  532. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  533. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  534. struct snd_ctl_elem_info *uinfo)
  535. {
  536. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  537. struct soc_bytes *params = (void *)kcontrol->private_value;
  538. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  539. uinfo->count = params->num_regs * component->val_bytes;
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  543. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  544. struct snd_ctl_elem_value *ucontrol)
  545. {
  546. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  547. struct soc_bytes *params = (void *)kcontrol->private_value;
  548. int ret;
  549. if (component->regmap)
  550. ret = regmap_raw_read(component->regmap, params->base,
  551. ucontrol->value.bytes.data,
  552. params->num_regs * component->val_bytes);
  553. else
  554. ret = -EINVAL;
  555. /* Hide any masked bytes to ensure consistent data reporting */
  556. if (ret == 0 && params->mask) {
  557. switch (component->val_bytes) {
  558. case 1:
  559. ucontrol->value.bytes.data[0] &= ~params->mask;
  560. break;
  561. case 2:
  562. ((u16 *)(&ucontrol->value.bytes.data))[0]
  563. &= cpu_to_be16(~params->mask);
  564. break;
  565. case 4:
  566. ((u32 *)(&ucontrol->value.bytes.data))[0]
  567. &= cpu_to_be32(~params->mask);
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. }
  573. return ret;
  574. }
  575. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  576. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  577. struct snd_ctl_elem_value *ucontrol)
  578. {
  579. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  580. struct soc_bytes *params = (void *)kcontrol->private_value;
  581. int ret, len;
  582. unsigned int val, mask;
  583. void *data;
  584. if (!component->regmap || !params->num_regs)
  585. return -EINVAL;
  586. len = params->num_regs * component->val_bytes;
  587. data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
  588. if (!data)
  589. return -ENOMEM;
  590. /*
  591. * If we've got a mask then we need to preserve the register
  592. * bits. We shouldn't modify the incoming data so take a
  593. * copy.
  594. */
  595. if (params->mask) {
  596. ret = regmap_read(component->regmap, params->base, &val);
  597. if (ret != 0)
  598. goto out;
  599. val &= params->mask;
  600. switch (component->val_bytes) {
  601. case 1:
  602. ((u8 *)data)[0] &= ~params->mask;
  603. ((u8 *)data)[0] |= val;
  604. break;
  605. case 2:
  606. mask = ~params->mask;
  607. ret = regmap_parse_val(component->regmap,
  608. &mask, &mask);
  609. if (ret != 0)
  610. goto out;
  611. ((u16 *)data)[0] &= mask;
  612. ret = regmap_parse_val(component->regmap,
  613. &val, &val);
  614. if (ret != 0)
  615. goto out;
  616. ((u16 *)data)[0] |= val;
  617. break;
  618. case 4:
  619. mask = ~params->mask;
  620. ret = regmap_parse_val(component->regmap,
  621. &mask, &mask);
  622. if (ret != 0)
  623. goto out;
  624. ((u32 *)data)[0] &= mask;
  625. ret = regmap_parse_val(component->regmap,
  626. &val, &val);
  627. if (ret != 0)
  628. goto out;
  629. ((u32 *)data)[0] |= val;
  630. break;
  631. default:
  632. ret = -EINVAL;
  633. goto out;
  634. }
  635. }
  636. ret = regmap_raw_write(component->regmap, params->base,
  637. data, len);
  638. out:
  639. kfree(data);
  640. return ret;
  641. }
  642. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  643. int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
  644. struct snd_ctl_elem_info *ucontrol)
  645. {
  646. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  647. ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  648. ucontrol->count = params->max;
  649. return 0;
  650. }
  651. EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
  652. int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
  653. unsigned int size, unsigned int __user *tlv)
  654. {
  655. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  656. unsigned int count = size < params->max ? size : params->max;
  657. int ret = -ENXIO;
  658. switch (op_flag) {
  659. case SNDRV_CTL_TLV_OP_READ:
  660. if (params->get)
  661. ret = params->get(tlv, count);
  662. break;
  663. case SNDRV_CTL_TLV_OP_WRITE:
  664. if (params->put)
  665. ret = params->put(tlv, count);
  666. break;
  667. }
  668. return ret;
  669. }
  670. EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
  671. /**
  672. * snd_soc_info_xr_sx - signed multi register info callback
  673. * @kcontrol: mreg control
  674. * @uinfo: control element information
  675. *
  676. * Callback to provide information of a control that can
  677. * span multiple codec registers which together
  678. * forms a single signed value in a MSB/LSB manner.
  679. *
  680. * Returns 0 for success.
  681. */
  682. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  683. struct snd_ctl_elem_info *uinfo)
  684. {
  685. struct soc_mreg_control *mc =
  686. (struct soc_mreg_control *)kcontrol->private_value;
  687. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  688. uinfo->count = 1;
  689. uinfo->value.integer.min = mc->min;
  690. uinfo->value.integer.max = mc->max;
  691. return 0;
  692. }
  693. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  694. /**
  695. * snd_soc_get_xr_sx - signed multi register get callback
  696. * @kcontrol: mreg control
  697. * @ucontrol: control element information
  698. *
  699. * Callback to get the value of a control that can span
  700. * multiple codec registers which together forms a single
  701. * signed value in a MSB/LSB manner. The control supports
  702. * specifying total no of bits used to allow for bitfields
  703. * across the multiple codec registers.
  704. *
  705. * Returns 0 for success.
  706. */
  707. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  708. struct snd_ctl_elem_value *ucontrol)
  709. {
  710. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  711. struct soc_mreg_control *mc =
  712. (struct soc_mreg_control *)kcontrol->private_value;
  713. unsigned int regbase = mc->regbase;
  714. unsigned int regcount = mc->regcount;
  715. unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
  716. unsigned int regwmask = (1<<regwshift)-1;
  717. unsigned int invert = mc->invert;
  718. unsigned long mask = (1UL<<mc->nbits)-1;
  719. long min = mc->min;
  720. long max = mc->max;
  721. long val = 0;
  722. unsigned int regval;
  723. unsigned int i;
  724. int ret;
  725. for (i = 0; i < regcount; i++) {
  726. ret = snd_soc_component_read(component, regbase+i, &regval);
  727. if (ret)
  728. return ret;
  729. val |= (regval & regwmask) << (regwshift*(regcount-i-1));
  730. }
  731. val &= mask;
  732. if (min < 0 && val > max)
  733. val |= ~mask;
  734. if (invert)
  735. val = max - val;
  736. ucontrol->value.integer.value[0] = val;
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  740. /**
  741. * snd_soc_put_xr_sx - signed multi register get callback
  742. * @kcontrol: mreg control
  743. * @ucontrol: control element information
  744. *
  745. * Callback to set the value of a control that can span
  746. * multiple codec registers which together forms a single
  747. * signed value in a MSB/LSB manner. The control supports
  748. * specifying total no of bits used to allow for bitfields
  749. * across the multiple codec registers.
  750. *
  751. * Returns 0 for success.
  752. */
  753. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  754. struct snd_ctl_elem_value *ucontrol)
  755. {
  756. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  757. struct soc_mreg_control *mc =
  758. (struct soc_mreg_control *)kcontrol->private_value;
  759. unsigned int regbase = mc->regbase;
  760. unsigned int regcount = mc->regcount;
  761. unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
  762. unsigned int regwmask = (1<<regwshift)-1;
  763. unsigned int invert = mc->invert;
  764. unsigned long mask = (1UL<<mc->nbits)-1;
  765. long max = mc->max;
  766. long val = ucontrol->value.integer.value[0];
  767. unsigned int i, regval, regmask;
  768. int err;
  769. if (invert)
  770. val = max - val;
  771. val &= mask;
  772. for (i = 0; i < regcount; i++) {
  773. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  774. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  775. err = snd_soc_component_update_bits(component, regbase+i,
  776. regmask, regval);
  777. if (err < 0)
  778. return err;
  779. }
  780. return 0;
  781. }
  782. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  783. /**
  784. * snd_soc_get_strobe - strobe get callback
  785. * @kcontrol: mixer control
  786. * @ucontrol: control element information
  787. *
  788. * Callback get the value of a strobe mixer control.
  789. *
  790. * Returns 0 for success.
  791. */
  792. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  793. struct snd_ctl_elem_value *ucontrol)
  794. {
  795. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  796. struct soc_mixer_control *mc =
  797. (struct soc_mixer_control *)kcontrol->private_value;
  798. unsigned int reg = mc->reg;
  799. unsigned int shift = mc->shift;
  800. unsigned int mask = 1 << shift;
  801. unsigned int invert = mc->invert != 0;
  802. unsigned int val;
  803. int ret;
  804. ret = snd_soc_component_read(component, reg, &val);
  805. if (ret)
  806. return ret;
  807. val &= mask;
  808. if (shift != 0 && val != 0)
  809. val = val >> shift;
  810. ucontrol->value.enumerated.item[0] = val ^ invert;
  811. return 0;
  812. }
  813. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  814. /**
  815. * snd_soc_put_strobe - strobe put callback
  816. * @kcontrol: mixer control
  817. * @ucontrol: control element information
  818. *
  819. * Callback strobe a register bit to high then low (or the inverse)
  820. * in one pass of a single mixer enum control.
  821. *
  822. * Returns 1 for success.
  823. */
  824. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  825. struct snd_ctl_elem_value *ucontrol)
  826. {
  827. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  828. struct soc_mixer_control *mc =
  829. (struct soc_mixer_control *)kcontrol->private_value;
  830. unsigned int reg = mc->reg;
  831. unsigned int shift = mc->shift;
  832. unsigned int mask = 1 << shift;
  833. unsigned int invert = mc->invert != 0;
  834. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  835. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  836. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  837. int err;
  838. err = snd_soc_component_update_bits(component, reg, mask, val1);
  839. if (err < 0)
  840. return err;
  841. return snd_soc_component_update_bits(component, reg, mask, val2);
  842. }
  843. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);