soc-ops.c 28 KB

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