soc-ops.c 25 KB

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