soc-ops.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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_info_volsw_sx - Mixer info callback for SX TLV controls
  188. * @kcontrol: mixer control
  189. * @uinfo: control element information
  190. *
  191. * Callback to provide information about a single mixer control, or a double
  192. * mixer control that spans 2 registers of the SX TLV type. SX TLV controls
  193. * have a range that represents both positive and negative values either side
  194. * of zero but without a sign bit.
  195. *
  196. * Returns 0 for success.
  197. */
  198. int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol,
  199. struct snd_ctl_elem_info *uinfo)
  200. {
  201. struct soc_mixer_control *mc =
  202. (struct soc_mixer_control *)kcontrol->private_value;
  203. snd_soc_info_volsw(kcontrol, uinfo);
  204. /* Max represents the number of levels in an SX control not the
  205. * maximum value, so add the minimum value back on
  206. */
  207. uinfo->value.integer.max += mc->min;
  208. return 0;
  209. }
  210. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx);
  211. /**
  212. * snd_soc_get_volsw - single mixer get callback
  213. * @kcontrol: mixer control
  214. * @ucontrol: control element information
  215. *
  216. * Callback to get the value of a single mixer control, or a double mixer
  217. * control that spans 2 registers.
  218. *
  219. * Returns 0 for success.
  220. */
  221. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  222. struct snd_ctl_elem_value *ucontrol)
  223. {
  224. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  225. struct soc_mixer_control *mc =
  226. (struct soc_mixer_control *)kcontrol->private_value;
  227. unsigned int reg = mc->reg;
  228. unsigned int reg2 = mc->rreg;
  229. unsigned int shift = mc->shift;
  230. unsigned int rshift = mc->rshift;
  231. int max = mc->max;
  232. int min = mc->min;
  233. int sign_bit = mc->sign_bit;
  234. unsigned int mask = (1 << fls(max)) - 1;
  235. unsigned int invert = mc->invert;
  236. int val;
  237. int ret;
  238. if (sign_bit)
  239. mask = BIT(sign_bit + 1) - 1;
  240. ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
  241. if (ret)
  242. return ret;
  243. ucontrol->value.integer.value[0] = val - min;
  244. if (invert)
  245. ucontrol->value.integer.value[0] =
  246. max - ucontrol->value.integer.value[0];
  247. if (snd_soc_volsw_is_stereo(mc)) {
  248. if (reg == reg2)
  249. ret = snd_soc_read_signed(component, reg, mask, rshift,
  250. sign_bit, &val);
  251. else
  252. ret = snd_soc_read_signed(component, reg2, mask, shift,
  253. sign_bit, &val);
  254. if (ret)
  255. return ret;
  256. ucontrol->value.integer.value[1] = val - min;
  257. if (invert)
  258. ucontrol->value.integer.value[1] =
  259. max - ucontrol->value.integer.value[1];
  260. }
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  264. /**
  265. * snd_soc_put_volsw - single mixer put callback
  266. * @kcontrol: mixer control
  267. * @ucontrol: control element information
  268. *
  269. * Callback to set the value of a single mixer control, or a double mixer
  270. * control that spans 2 registers.
  271. *
  272. * Returns 0 for success.
  273. */
  274. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  275. struct snd_ctl_elem_value *ucontrol)
  276. {
  277. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  278. struct soc_mixer_control *mc =
  279. (struct soc_mixer_control *)kcontrol->private_value;
  280. unsigned int reg = mc->reg;
  281. unsigned int reg2 = mc->rreg;
  282. unsigned int shift = mc->shift;
  283. unsigned int rshift = mc->rshift;
  284. int max = mc->max;
  285. int min = mc->min;
  286. unsigned int sign_bit = mc->sign_bit;
  287. unsigned int mask = (1 << fls(max)) - 1;
  288. unsigned int invert = mc->invert;
  289. int err;
  290. bool type_2r = false;
  291. unsigned int val2 = 0;
  292. unsigned int val, val_mask;
  293. if (sign_bit)
  294. mask = BIT(sign_bit + 1) - 1;
  295. val = ((ucontrol->value.integer.value[0] + min) & mask);
  296. if (invert)
  297. val = max - val;
  298. val_mask = mask << shift;
  299. val = val << shift;
  300. if (snd_soc_volsw_is_stereo(mc)) {
  301. val2 = ((ucontrol->value.integer.value[1] + min) & mask);
  302. if (invert)
  303. val2 = max - val2;
  304. if (reg == reg2) {
  305. val_mask |= mask << rshift;
  306. val |= val2 << rshift;
  307. } else {
  308. val2 = val2 << shift;
  309. type_2r = true;
  310. }
  311. }
  312. err = snd_soc_component_update_bits(component, reg, val_mask, val);
  313. if (err < 0)
  314. return err;
  315. if (type_2r)
  316. err = snd_soc_component_update_bits(component, reg2, val_mask,
  317. val2);
  318. return err;
  319. }
  320. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  321. /**
  322. * snd_soc_get_volsw_sx - single mixer get callback
  323. * @kcontrol: mixer control
  324. * @ucontrol: control element information
  325. *
  326. * Callback to get the value of a single mixer control, or a double mixer
  327. * control that spans 2 registers.
  328. *
  329. * Returns 0 for success.
  330. */
  331. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  332. struct snd_ctl_elem_value *ucontrol)
  333. {
  334. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  335. struct soc_mixer_control *mc =
  336. (struct soc_mixer_control *)kcontrol->private_value;
  337. unsigned int reg = mc->reg;
  338. unsigned int reg2 = mc->rreg;
  339. unsigned int shift = mc->shift;
  340. unsigned int rshift = mc->rshift;
  341. int max = mc->max;
  342. int min = mc->min;
  343. int mask = (1 << (fls(min + max) - 1)) - 1;
  344. unsigned int val;
  345. int ret;
  346. ret = snd_soc_component_read(component, reg, &val);
  347. if (ret < 0)
  348. return ret;
  349. ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
  350. if (snd_soc_volsw_is_stereo(mc)) {
  351. ret = snd_soc_component_read(component, reg2, &val);
  352. if (ret < 0)
  353. return ret;
  354. val = ((val >> rshift) - min) & mask;
  355. ucontrol->value.integer.value[1] = val;
  356. }
  357. return 0;
  358. }
  359. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  360. /**
  361. * snd_soc_put_volsw_sx - double mixer set callback
  362. * @kcontrol: mixer control
  363. * @ucontrol: control element information
  364. *
  365. * Callback to set the value of a double mixer control that spans 2 registers.
  366. *
  367. * Returns 0 for success.
  368. */
  369. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  370. struct snd_ctl_elem_value *ucontrol)
  371. {
  372. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  373. struct soc_mixer_control *mc =
  374. (struct soc_mixer_control *)kcontrol->private_value;
  375. unsigned int reg = mc->reg;
  376. unsigned int reg2 = mc->rreg;
  377. unsigned int shift = mc->shift;
  378. unsigned int rshift = mc->rshift;
  379. int max = mc->max;
  380. int min = mc->min;
  381. int mask = (1 << (fls(min + max) - 1)) - 1;
  382. int err = 0;
  383. unsigned int val, val_mask, val2 = 0;
  384. val_mask = mask << shift;
  385. val = (ucontrol->value.integer.value[0] + min) & mask;
  386. val = val << shift;
  387. err = snd_soc_component_update_bits(component, reg, val_mask, val);
  388. if (err < 0)
  389. return err;
  390. if (snd_soc_volsw_is_stereo(mc)) {
  391. val_mask = mask << rshift;
  392. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  393. val2 = val2 << rshift;
  394. err = snd_soc_component_update_bits(component, reg2, val_mask,
  395. val2);
  396. }
  397. return err;
  398. }
  399. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  400. /**
  401. * snd_soc_info_volsw_range - single mixer info callback with range.
  402. * @kcontrol: mixer control
  403. * @uinfo: control element information
  404. *
  405. * Callback to provide information, within a range, about a single
  406. * mixer control.
  407. *
  408. * returns 0 for success.
  409. */
  410. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_info *uinfo)
  412. {
  413. struct soc_mixer_control *mc =
  414. (struct soc_mixer_control *)kcontrol->private_value;
  415. int platform_max;
  416. int min = mc->min;
  417. if (!mc->platform_max)
  418. mc->platform_max = mc->max;
  419. platform_max = mc->platform_max;
  420. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  421. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  422. uinfo->value.integer.min = 0;
  423. uinfo->value.integer.max = platform_max - min;
  424. return 0;
  425. }
  426. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  427. /**
  428. * snd_soc_put_volsw_range - single mixer put value callback with range.
  429. * @kcontrol: mixer control
  430. * @ucontrol: control element information
  431. *
  432. * Callback to set the value, within a range, for a single mixer control.
  433. *
  434. * Returns 0 for success.
  435. */
  436. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  437. struct snd_ctl_elem_value *ucontrol)
  438. {
  439. struct soc_mixer_control *mc =
  440. (struct soc_mixer_control *)kcontrol->private_value;
  441. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  442. unsigned int reg = mc->reg;
  443. unsigned int rreg = mc->rreg;
  444. unsigned int shift = mc->shift;
  445. int min = mc->min;
  446. int max = mc->max;
  447. unsigned int mask = (1 << fls(max)) - 1;
  448. unsigned int invert = mc->invert;
  449. unsigned int val, val_mask;
  450. int ret;
  451. if (invert)
  452. val = (max - ucontrol->value.integer.value[0]) & mask;
  453. else
  454. val = ((ucontrol->value.integer.value[0] + min) & mask);
  455. val_mask = mask << shift;
  456. val = val << shift;
  457. ret = snd_soc_component_update_bits(component, reg, val_mask, val);
  458. if (ret < 0)
  459. return ret;
  460. if (snd_soc_volsw_is_stereo(mc)) {
  461. if (invert)
  462. val = (max - ucontrol->value.integer.value[1]) & mask;
  463. else
  464. val = ((ucontrol->value.integer.value[1] + min) & mask);
  465. val_mask = mask << shift;
  466. val = val << shift;
  467. ret = snd_soc_component_update_bits(component, rreg, val_mask,
  468. val);
  469. }
  470. return ret;
  471. }
  472. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  473. /**
  474. * snd_soc_get_volsw_range - single mixer get callback with range
  475. * @kcontrol: mixer control
  476. * @ucontrol: control element information
  477. *
  478. * Callback to get the value, within a range, of a single mixer control.
  479. *
  480. * Returns 0 for success.
  481. */
  482. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  483. struct snd_ctl_elem_value *ucontrol)
  484. {
  485. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  486. struct soc_mixer_control *mc =
  487. (struct soc_mixer_control *)kcontrol->private_value;
  488. unsigned int reg = mc->reg;
  489. unsigned int rreg = mc->rreg;
  490. unsigned int shift = mc->shift;
  491. int min = mc->min;
  492. int max = mc->max;
  493. unsigned int mask = (1 << fls(max)) - 1;
  494. unsigned int invert = mc->invert;
  495. unsigned int val;
  496. int ret;
  497. ret = snd_soc_component_read(component, reg, &val);
  498. if (ret)
  499. return ret;
  500. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  501. if (invert)
  502. ucontrol->value.integer.value[0] =
  503. max - ucontrol->value.integer.value[0];
  504. else
  505. ucontrol->value.integer.value[0] =
  506. ucontrol->value.integer.value[0] - min;
  507. if (snd_soc_volsw_is_stereo(mc)) {
  508. ret = snd_soc_component_read(component, rreg, &val);
  509. if (ret)
  510. return ret;
  511. ucontrol->value.integer.value[1] = (val >> shift) & mask;
  512. if (invert)
  513. ucontrol->value.integer.value[1] =
  514. max - ucontrol->value.integer.value[1];
  515. else
  516. ucontrol->value.integer.value[1] =
  517. ucontrol->value.integer.value[1] - min;
  518. }
  519. return 0;
  520. }
  521. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  522. /**
  523. * snd_soc_limit_volume - Set new limit to an existing volume control.
  524. *
  525. * @card: where to look for the control
  526. * @name: Name of the control
  527. * @max: new maximum limit
  528. *
  529. * Return 0 for success, else error.
  530. */
  531. int snd_soc_limit_volume(struct snd_soc_card *card,
  532. const char *name, int max)
  533. {
  534. struct snd_card *snd_card = card->snd_card;
  535. struct snd_kcontrol *kctl;
  536. struct soc_mixer_control *mc;
  537. int found = 0;
  538. int ret = -EINVAL;
  539. /* Sanity check for name and max */
  540. if (unlikely(!name || max <= 0))
  541. return -EINVAL;
  542. list_for_each_entry(kctl, &snd_card->controls, list) {
  543. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  544. found = 1;
  545. break;
  546. }
  547. }
  548. if (found) {
  549. mc = (struct soc_mixer_control *)kctl->private_value;
  550. if (max <= mc->max) {
  551. mc->platform_max = max;
  552. ret = 0;
  553. }
  554. }
  555. return ret;
  556. }
  557. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  558. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  559. struct snd_ctl_elem_info *uinfo)
  560. {
  561. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  562. struct soc_bytes *params = (void *)kcontrol->private_value;
  563. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  564. uinfo->count = params->num_regs * component->val_bytes;
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  568. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  569. struct snd_ctl_elem_value *ucontrol)
  570. {
  571. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  572. struct soc_bytes *params = (void *)kcontrol->private_value;
  573. int ret;
  574. if (component->regmap)
  575. ret = regmap_raw_read(component->regmap, params->base,
  576. ucontrol->value.bytes.data,
  577. params->num_regs * component->val_bytes);
  578. else
  579. ret = -EINVAL;
  580. /* Hide any masked bytes to ensure consistent data reporting */
  581. if (ret == 0 && params->mask) {
  582. switch (component->val_bytes) {
  583. case 1:
  584. ucontrol->value.bytes.data[0] &= ~params->mask;
  585. break;
  586. case 2:
  587. ((u16 *)(&ucontrol->value.bytes.data))[0]
  588. &= cpu_to_be16(~params->mask);
  589. break;
  590. case 4:
  591. ((u32 *)(&ucontrol->value.bytes.data))[0]
  592. &= cpu_to_be32(~params->mask);
  593. break;
  594. default:
  595. return -EINVAL;
  596. }
  597. }
  598. return ret;
  599. }
  600. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  601. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  602. struct snd_ctl_elem_value *ucontrol)
  603. {
  604. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  605. struct soc_bytes *params = (void *)kcontrol->private_value;
  606. int ret, len;
  607. unsigned int val, mask;
  608. void *data;
  609. if (!component->regmap || !params->num_regs)
  610. return -EINVAL;
  611. len = params->num_regs * component->val_bytes;
  612. data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
  613. if (!data)
  614. return -ENOMEM;
  615. /*
  616. * If we've got a mask then we need to preserve the register
  617. * bits. We shouldn't modify the incoming data so take a
  618. * copy.
  619. */
  620. if (params->mask) {
  621. ret = regmap_read(component->regmap, params->base, &val);
  622. if (ret != 0)
  623. goto out;
  624. val &= params->mask;
  625. switch (component->val_bytes) {
  626. case 1:
  627. ((u8 *)data)[0] &= ~params->mask;
  628. ((u8 *)data)[0] |= val;
  629. break;
  630. case 2:
  631. mask = ~params->mask;
  632. ret = regmap_parse_val(component->regmap,
  633. &mask, &mask);
  634. if (ret != 0)
  635. goto out;
  636. ((u16 *)data)[0] &= mask;
  637. ret = regmap_parse_val(component->regmap,
  638. &val, &val);
  639. if (ret != 0)
  640. goto out;
  641. ((u16 *)data)[0] |= val;
  642. break;
  643. case 4:
  644. mask = ~params->mask;
  645. ret = regmap_parse_val(component->regmap,
  646. &mask, &mask);
  647. if (ret != 0)
  648. goto out;
  649. ((u32 *)data)[0] &= mask;
  650. ret = regmap_parse_val(component->regmap,
  651. &val, &val);
  652. if (ret != 0)
  653. goto out;
  654. ((u32 *)data)[0] |= val;
  655. break;
  656. default:
  657. ret = -EINVAL;
  658. goto out;
  659. }
  660. }
  661. ret = regmap_raw_write(component->regmap, params->base,
  662. data, len);
  663. out:
  664. kfree(data);
  665. return ret;
  666. }
  667. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  668. int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
  669. struct snd_ctl_elem_info *ucontrol)
  670. {
  671. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  672. ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  673. ucontrol->count = params->max;
  674. return 0;
  675. }
  676. EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
  677. int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
  678. unsigned int size, unsigned int __user *tlv)
  679. {
  680. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  681. unsigned int count = size < params->max ? size : params->max;
  682. int ret = -ENXIO;
  683. switch (op_flag) {
  684. case SNDRV_CTL_TLV_OP_READ:
  685. if (params->get)
  686. ret = params->get(tlv, count);
  687. break;
  688. case SNDRV_CTL_TLV_OP_WRITE:
  689. if (params->put)
  690. ret = params->put(tlv, count);
  691. break;
  692. }
  693. return ret;
  694. }
  695. EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
  696. /**
  697. * snd_soc_info_xr_sx - signed multi register info callback
  698. * @kcontrol: mreg control
  699. * @uinfo: control element information
  700. *
  701. * Callback to provide information of a control that can
  702. * span multiple codec registers which together
  703. * forms a single signed value in a MSB/LSB manner.
  704. *
  705. * Returns 0 for success.
  706. */
  707. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  708. struct snd_ctl_elem_info *uinfo)
  709. {
  710. struct soc_mreg_control *mc =
  711. (struct soc_mreg_control *)kcontrol->private_value;
  712. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  713. uinfo->count = 1;
  714. uinfo->value.integer.min = mc->min;
  715. uinfo->value.integer.max = mc->max;
  716. return 0;
  717. }
  718. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  719. /**
  720. * snd_soc_get_xr_sx - signed multi register get callback
  721. * @kcontrol: mreg control
  722. * @ucontrol: control element information
  723. *
  724. * Callback to get the value of a control that can span
  725. * multiple codec registers which together forms a single
  726. * signed value in a MSB/LSB manner. The control supports
  727. * specifying total no of bits used to allow for bitfields
  728. * across the multiple codec registers.
  729. *
  730. * Returns 0 for success.
  731. */
  732. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  733. struct snd_ctl_elem_value *ucontrol)
  734. {
  735. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  736. struct soc_mreg_control *mc =
  737. (struct soc_mreg_control *)kcontrol->private_value;
  738. unsigned int regbase = mc->regbase;
  739. unsigned int regcount = mc->regcount;
  740. unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
  741. unsigned int regwmask = (1<<regwshift)-1;
  742. unsigned int invert = mc->invert;
  743. unsigned long mask = (1UL<<mc->nbits)-1;
  744. long min = mc->min;
  745. long max = mc->max;
  746. long val = 0;
  747. unsigned int regval;
  748. unsigned int i;
  749. int ret;
  750. for (i = 0; i < regcount; i++) {
  751. ret = snd_soc_component_read(component, regbase+i, &regval);
  752. if (ret)
  753. return ret;
  754. val |= (regval & regwmask) << (regwshift*(regcount-i-1));
  755. }
  756. val &= mask;
  757. if (min < 0 && val > max)
  758. val |= ~mask;
  759. if (invert)
  760. val = max - val;
  761. ucontrol->value.integer.value[0] = val;
  762. return 0;
  763. }
  764. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  765. /**
  766. * snd_soc_put_xr_sx - signed multi register get callback
  767. * @kcontrol: mreg control
  768. * @ucontrol: control element information
  769. *
  770. * Callback to set the value of a control that can span
  771. * multiple codec registers which together forms a single
  772. * signed value in a MSB/LSB manner. The control supports
  773. * specifying total no of bits used to allow for bitfields
  774. * across the multiple codec registers.
  775. *
  776. * Returns 0 for success.
  777. */
  778. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  779. struct snd_ctl_elem_value *ucontrol)
  780. {
  781. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  782. struct soc_mreg_control *mc =
  783. (struct soc_mreg_control *)kcontrol->private_value;
  784. unsigned int regbase = mc->regbase;
  785. unsigned int regcount = mc->regcount;
  786. unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
  787. unsigned int regwmask = (1<<regwshift)-1;
  788. unsigned int invert = mc->invert;
  789. unsigned long mask = (1UL<<mc->nbits)-1;
  790. long max = mc->max;
  791. long val = ucontrol->value.integer.value[0];
  792. unsigned int i, regval, regmask;
  793. int err;
  794. if (invert)
  795. val = max - val;
  796. val &= mask;
  797. for (i = 0; i < regcount; i++) {
  798. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  799. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  800. err = snd_soc_component_update_bits(component, regbase+i,
  801. regmask, regval);
  802. if (err < 0)
  803. return err;
  804. }
  805. return 0;
  806. }
  807. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  808. /**
  809. * snd_soc_get_strobe - strobe get callback
  810. * @kcontrol: mixer control
  811. * @ucontrol: control element information
  812. *
  813. * Callback get the value of a strobe mixer control.
  814. *
  815. * Returns 0 for success.
  816. */
  817. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  818. struct snd_ctl_elem_value *ucontrol)
  819. {
  820. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  821. struct soc_mixer_control *mc =
  822. (struct soc_mixer_control *)kcontrol->private_value;
  823. unsigned int reg = mc->reg;
  824. unsigned int shift = mc->shift;
  825. unsigned int mask = 1 << shift;
  826. unsigned int invert = mc->invert != 0;
  827. unsigned int val;
  828. int ret;
  829. ret = snd_soc_component_read(component, reg, &val);
  830. if (ret)
  831. return ret;
  832. val &= mask;
  833. if (shift != 0 && val != 0)
  834. val = val >> shift;
  835. ucontrol->value.enumerated.item[0] = val ^ invert;
  836. return 0;
  837. }
  838. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  839. /**
  840. * snd_soc_put_strobe - strobe put callback
  841. * @kcontrol: mixer control
  842. * @ucontrol: control element information
  843. *
  844. * Callback strobe a register bit to high then low (or the inverse)
  845. * in one pass of a single mixer enum control.
  846. *
  847. * Returns 1 for success.
  848. */
  849. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  850. struct snd_ctl_elem_value *ucontrol)
  851. {
  852. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  853. struct soc_mixer_control *mc =
  854. (struct soc_mixer_control *)kcontrol->private_value;
  855. unsigned int reg = mc->reg;
  856. unsigned int shift = mc->shift;
  857. unsigned int mask = 1 << shift;
  858. unsigned int invert = mc->invert != 0;
  859. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  860. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  861. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  862. int err;
  863. err = snd_soc_component_update_bits(component, reg, mask, val1);
  864. if (err < 0)
  865. return err;
  866. return snd_soc_component_update_bits(component, reg, mask, val2);
  867. }
  868. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);