soc-ops.c 27 KB

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