soc-ops.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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/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. static int sdca_soc_q78_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_val,
  104. unsigned int mask, unsigned int shift, int max,
  105. bool sx)
  106. {
  107. int val = reg_val;
  108. if (WARN_ON(!mc->shift))
  109. return -EINVAL;
  110. val = sign_extend32(val, mc->sign_bit);
  111. val = (((val * 100) >> 8) / (int)mc->shift);
  112. val -= mc->min;
  113. return val & mask;
  114. }
  115. static unsigned int sdca_soc_q78_ctl_to_reg(struct soc_mixer_control *mc, int val,
  116. unsigned int mask, unsigned int shift, int max)
  117. {
  118. unsigned int ret_val;
  119. int reg_val;
  120. if (WARN_ON(!mc->shift))
  121. return -EINVAL;
  122. reg_val = val + mc->min;
  123. ret_val = (int)((reg_val * mc->shift) << 8) / 100;
  124. return ret_val & mask;
  125. }
  126. static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_val,
  127. unsigned int mask, unsigned int shift, int max,
  128. bool sx)
  129. {
  130. int val = (reg_val >> shift) & mask;
  131. if (mc->sign_bit)
  132. val = sign_extend32(val, mc->sign_bit);
  133. if (sx) {
  134. val -= mc->min; // SX controls intentionally can overflow here
  135. val = min_t(unsigned int, val & mask, max);
  136. } else {
  137. val = clamp(val, mc->min, mc->max);
  138. val -= mc->min;
  139. }
  140. if (mc->invert)
  141. val = max - val;
  142. return val;
  143. }
  144. static unsigned int soc_mixer_ctl_to_reg(struct soc_mixer_control *mc, int val,
  145. unsigned int mask, unsigned int shift,
  146. int max)
  147. {
  148. unsigned int reg_val;
  149. if (mc->invert)
  150. val = max - val;
  151. reg_val = val + mc->min;
  152. return (reg_val & mask) << shift;
  153. }
  154. static int soc_mixer_valid_ctl(struct soc_mixer_control *mc, long val, int max)
  155. {
  156. if (val < 0)
  157. return -EINVAL;
  158. if (mc->platform_max && val > mc->platform_max)
  159. return -EINVAL;
  160. if (val > max)
  161. return -EINVAL;
  162. return 0;
  163. }
  164. static int soc_mixer_mask(struct soc_mixer_control *mc)
  165. {
  166. if (mc->sign_bit)
  167. return GENMASK(mc->sign_bit, 0);
  168. else
  169. return GENMASK(fls(mc->max) - 1, 0);
  170. }
  171. static int soc_mixer_sx_mask(struct soc_mixer_control *mc)
  172. {
  173. // min + max will take us 1-bit over the size of the mask
  174. return GENMASK(fls(mc->min + mc->max) - 2, 0);
  175. }
  176. static int soc_info_volsw(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_info *uinfo,
  178. struct soc_mixer_control *mc, int max)
  179. {
  180. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  181. if (max == 1) {
  182. /* Even two value controls ending in Volume should be integer */
  183. const char *vol_string = strstr(kcontrol->id.name, " Volume");
  184. if (!vol_string || strcmp(vol_string, " Volume"))
  185. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  186. }
  187. if (mc->platform_max && mc->platform_max < max)
  188. max = mc->platform_max;
  189. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  190. uinfo->value.integer.min = 0;
  191. uinfo->value.integer.max = max;
  192. return 0;
  193. }
  194. static int soc_put_volsw(struct snd_kcontrol *kcontrol,
  195. struct snd_ctl_elem_value *ucontrol,
  196. struct soc_mixer_control *mc, int mask, int max)
  197. {
  198. unsigned int (*ctl_to_reg)(struct soc_mixer_control *, int, unsigned int, unsigned int, int);
  199. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  200. unsigned int val1, val_mask;
  201. unsigned int val2 = 0;
  202. bool double_r = false;
  203. int ret;
  204. if (mc->sdca_q78) {
  205. ctl_to_reg = sdca_soc_q78_ctl_to_reg;
  206. val_mask = mask;
  207. } else {
  208. ctl_to_reg = soc_mixer_ctl_to_reg;
  209. val_mask = mask << mc->shift;
  210. }
  211. ret = soc_mixer_valid_ctl(mc, ucontrol->value.integer.value[0], max);
  212. if (ret)
  213. return ret;
  214. val1 = ctl_to_reg(mc, ucontrol->value.integer.value[0],
  215. mask, mc->shift, max);
  216. if (snd_soc_volsw_is_stereo(mc)) {
  217. ret = soc_mixer_valid_ctl(mc, ucontrol->value.integer.value[1], max);
  218. if (ret)
  219. return ret;
  220. if (mc->reg == mc->rreg) {
  221. val1 |= ctl_to_reg(mc, ucontrol->value.integer.value[1], mask, mc->rshift, max);
  222. val_mask |= mask << mc->rshift;
  223. } else {
  224. val2 = ctl_to_reg(mc, ucontrol->value.integer.value[1], mask, mc->shift, max);
  225. double_r = true;
  226. }
  227. }
  228. ret = snd_soc_component_update_bits(component, mc->reg, val_mask, val1);
  229. if (ret < 0)
  230. return ret;
  231. if (double_r) {
  232. int err = snd_soc_component_update_bits(component, mc->rreg,
  233. val_mask, val2);
  234. /* Don't drop change flag */
  235. if (err)
  236. return err;
  237. }
  238. return ret;
  239. }
  240. static int soc_get_volsw(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *ucontrol,
  242. struct soc_mixer_control *mc, int mask, int max, bool sx)
  243. {
  244. int (*reg_to_ctl)(struct soc_mixer_control *, unsigned int, unsigned int,
  245. unsigned int, int, bool);
  246. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  247. unsigned int reg_val;
  248. int val;
  249. if (mc->sdca_q78)
  250. reg_to_ctl = sdca_soc_q78_reg_to_ctl;
  251. else
  252. reg_to_ctl = soc_mixer_reg_to_ctl;
  253. reg_val = snd_soc_component_read(component, mc->reg);
  254. val = reg_to_ctl(mc, reg_val, mask, mc->shift, max, sx);
  255. ucontrol->value.integer.value[0] = val;
  256. if (snd_soc_volsw_is_stereo(mc)) {
  257. if (mc->reg == mc->rreg) {
  258. val = reg_to_ctl(mc, reg_val, mask, mc->rshift, max, sx);
  259. } else {
  260. reg_val = snd_soc_component_read(component, mc->rreg);
  261. val = reg_to_ctl(mc, reg_val, mask, mc->shift, max, sx);
  262. }
  263. ucontrol->value.integer.value[1] = val;
  264. }
  265. return 0;
  266. }
  267. /**
  268. * snd_soc_info_volsw - single mixer info callback with range.
  269. * @kcontrol: mixer control
  270. * @uinfo: control element information
  271. *
  272. * Callback to provide information, with a range, about a single mixer control,
  273. * or a double mixer control that spans 2 registers.
  274. *
  275. * Returns 0 for success.
  276. */
  277. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  278. struct snd_ctl_elem_info *uinfo)
  279. {
  280. struct soc_mixer_control *mc =
  281. (struct soc_mixer_control *)kcontrol->private_value;
  282. return soc_info_volsw(kcontrol, uinfo, mc, mc->max - mc->min);
  283. }
  284. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  285. /**
  286. * snd_soc_info_volsw_sx - Mixer info callback for SX TLV controls
  287. * @kcontrol: mixer control
  288. * @uinfo: control element information
  289. *
  290. * Callback to provide information about a single mixer control, or a double
  291. * mixer control that spans 2 registers of the SX TLV type. SX TLV controls
  292. * have a range that represents both positive and negative values either side
  293. * of zero but without a sign bit. min is the minimum register value, max is
  294. * the number of steps.
  295. *
  296. * Returns 0 for success.
  297. */
  298. int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol,
  299. struct snd_ctl_elem_info *uinfo)
  300. {
  301. struct soc_mixer_control *mc =
  302. (struct soc_mixer_control *)kcontrol->private_value;
  303. return soc_info_volsw(kcontrol, uinfo, mc, mc->max);
  304. }
  305. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_sx);
  306. /**
  307. * snd_soc_get_volsw - single mixer get callback with range
  308. * @kcontrol: mixer control
  309. * @ucontrol: control element information
  310. *
  311. * Callback to get the value, within a range, of a single mixer control, or a
  312. * double mixer control that spans 2 registers.
  313. *
  314. * Returns 0 for success.
  315. */
  316. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  317. struct snd_ctl_elem_value *ucontrol)
  318. {
  319. struct soc_mixer_control *mc =
  320. (struct soc_mixer_control *)kcontrol->private_value;
  321. unsigned int mask = soc_mixer_mask(mc);
  322. return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max - mc->min, false);
  323. }
  324. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  325. /**
  326. * snd_soc_put_volsw - single mixer put callback with range
  327. * @kcontrol: mixer control
  328. * @ucontrol: control element information
  329. *
  330. * Callback to set the value , within a range, of a single mixer control, or
  331. * a double mixer control that spans 2 registers.
  332. *
  333. * Returns 0 for success.
  334. */
  335. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  336. struct snd_ctl_elem_value *ucontrol)
  337. {
  338. struct soc_mixer_control *mc =
  339. (struct soc_mixer_control *)kcontrol->private_value;
  340. unsigned int mask = soc_mixer_mask(mc);
  341. return soc_put_volsw(kcontrol, ucontrol, mc, mask, mc->max - mc->min);
  342. }
  343. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  344. /**
  345. * snd_soc_get_volsw_sx - single mixer get callback
  346. * @kcontrol: mixer control
  347. * @ucontrol: control element information
  348. *
  349. * Callback to get the value of a single mixer control, or a double mixer
  350. * control that spans 2 registers.
  351. *
  352. * Returns 0 for success.
  353. */
  354. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  355. struct snd_ctl_elem_value *ucontrol)
  356. {
  357. struct soc_mixer_control *mc =
  358. (struct soc_mixer_control *)kcontrol->private_value;
  359. unsigned int mask = soc_mixer_sx_mask(mc);
  360. return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max, true);
  361. }
  362. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  363. /**
  364. * snd_soc_put_volsw_sx - double mixer set callback
  365. * @kcontrol: mixer control
  366. * @ucontrol: control element information
  367. *
  368. * Callback to set the value of a double mixer control that spans 2 registers.
  369. *
  370. * Returns 0 for success.
  371. */
  372. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  373. struct snd_ctl_elem_value *ucontrol)
  374. {
  375. struct soc_mixer_control *mc =
  376. (struct soc_mixer_control *)kcontrol->private_value;
  377. unsigned int mask = soc_mixer_sx_mask(mc);
  378. return soc_put_volsw(kcontrol, ucontrol, mc, mask, mc->max);
  379. }
  380. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  381. static int snd_soc_clip_to_platform_max(struct snd_kcontrol *kctl)
  382. {
  383. struct soc_mixer_control *mc = (struct soc_mixer_control *)kctl->private_value;
  384. struct snd_ctl_elem_value *uctl;
  385. int ret;
  386. if (!mc->platform_max)
  387. return 0;
  388. uctl = kzalloc_obj(*uctl);
  389. if (!uctl)
  390. return -ENOMEM;
  391. ret = kctl->get(kctl, uctl);
  392. if (ret < 0)
  393. goto out;
  394. if (uctl->value.integer.value[0] > mc->platform_max)
  395. uctl->value.integer.value[0] = mc->platform_max;
  396. if (snd_soc_volsw_is_stereo(mc) &&
  397. uctl->value.integer.value[1] > mc->platform_max)
  398. uctl->value.integer.value[1] = mc->platform_max;
  399. ret = kctl->put(kctl, uctl);
  400. out:
  401. kfree(uctl);
  402. return ret;
  403. }
  404. /**
  405. * snd_soc_limit_volume - Set new limit to an existing volume control.
  406. *
  407. * @card: where to look for the control
  408. * @name: Name of the control
  409. * @max: new maximum limit
  410. *
  411. * Return 0 for success, else error.
  412. */
  413. int snd_soc_limit_volume(struct snd_soc_card *card, const char *name, int max)
  414. {
  415. struct snd_kcontrol *kctl;
  416. int ret = -EINVAL;
  417. /* Sanity check for name and max */
  418. if (unlikely(!name || max <= 0))
  419. return -EINVAL;
  420. kctl = snd_soc_card_get_kcontrol(card, name);
  421. if (kctl) {
  422. struct soc_mixer_control *mc =
  423. (struct soc_mixer_control *)kctl->private_value;
  424. if (max <= mc->max - mc->min) {
  425. mc->platform_max = max;
  426. ret = snd_soc_clip_to_platform_max(kctl);
  427. }
  428. }
  429. return ret;
  430. }
  431. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  432. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  433. struct snd_ctl_elem_info *uinfo)
  434. {
  435. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  436. struct soc_bytes *params = (void *)kcontrol->private_value;
  437. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  438. uinfo->count = params->num_regs * component->val_bytes;
  439. return 0;
  440. }
  441. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  442. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  443. struct snd_ctl_elem_value *ucontrol)
  444. {
  445. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  446. struct soc_bytes *params = (void *)kcontrol->private_value;
  447. int ret;
  448. if (component->regmap)
  449. ret = regmap_raw_read(component->regmap, params->base,
  450. ucontrol->value.bytes.data,
  451. params->num_regs * component->val_bytes);
  452. else
  453. ret = -EINVAL;
  454. /* Hide any masked bytes to ensure consistent data reporting */
  455. if (ret == 0 && params->mask) {
  456. switch (component->val_bytes) {
  457. case 1:
  458. ucontrol->value.bytes.data[0] &= ~params->mask;
  459. break;
  460. case 2:
  461. ((__be16 *)(&ucontrol->value.bytes.data))[0]
  462. &= cpu_to_be16(~params->mask);
  463. break;
  464. case 4:
  465. ((__be32 *)(&ucontrol->value.bytes.data))[0]
  466. &= cpu_to_be32(~params->mask);
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. }
  472. return ret;
  473. }
  474. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  475. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  476. struct snd_ctl_elem_value *ucontrol)
  477. {
  478. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  479. struct soc_bytes *params = (void *)kcontrol->private_value;
  480. unsigned int val, mask;
  481. int ret, len;
  482. if (!component->regmap || !params->num_regs)
  483. return -EINVAL;
  484. len = params->num_regs * component->val_bytes;
  485. void *data __free(kfree) = kmemdup(ucontrol->value.bytes.data, len,
  486. GFP_KERNEL | GFP_DMA);
  487. if (!data)
  488. return -ENOMEM;
  489. /*
  490. * If we've got a mask then we need to preserve the register
  491. * bits. We shouldn't modify the incoming data so take a
  492. * copy.
  493. */
  494. if (params->mask) {
  495. ret = regmap_read(component->regmap, params->base, &val);
  496. if (ret != 0)
  497. return ret;
  498. val &= params->mask;
  499. switch (component->val_bytes) {
  500. case 1:
  501. ((u8 *)data)[0] &= ~params->mask;
  502. ((u8 *)data)[0] |= val;
  503. break;
  504. case 2:
  505. mask = ~params->mask;
  506. ret = regmap_parse_val(component->regmap, &mask, &mask);
  507. if (ret != 0)
  508. return ret;
  509. ((u16 *)data)[0] &= mask;
  510. ret = regmap_parse_val(component->regmap, &val, &val);
  511. if (ret != 0)
  512. return ret;
  513. ((u16 *)data)[0] |= val;
  514. break;
  515. case 4:
  516. mask = ~params->mask;
  517. ret = regmap_parse_val(component->regmap, &mask, &mask);
  518. if (ret != 0)
  519. return ret;
  520. ((u32 *)data)[0] &= mask;
  521. ret = regmap_parse_val(component->regmap, &val, &val);
  522. if (ret != 0)
  523. return ret;
  524. ((u32 *)data)[0] |= val;
  525. break;
  526. default:
  527. return -EINVAL;
  528. }
  529. }
  530. return regmap_raw_write(component->regmap, params->base, data, len);
  531. }
  532. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  533. int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
  534. struct snd_ctl_elem_info *ucontrol)
  535. {
  536. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  537. ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  538. ucontrol->count = params->max;
  539. return 0;
  540. }
  541. EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
  542. int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
  543. unsigned int size, unsigned int __user *tlv)
  544. {
  545. struct soc_bytes_ext *params = (void *)kcontrol->private_value;
  546. unsigned int count = size < params->max ? size : params->max;
  547. int ret = -ENXIO;
  548. switch (op_flag) {
  549. case SNDRV_CTL_TLV_OP_READ:
  550. if (params->get)
  551. ret = params->get(kcontrol, tlv, count);
  552. break;
  553. case SNDRV_CTL_TLV_OP_WRITE:
  554. if (params->put)
  555. ret = params->put(kcontrol, tlv, count);
  556. break;
  557. }
  558. return ret;
  559. }
  560. EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
  561. /**
  562. * snd_soc_info_xr_sx - signed multi register info callback
  563. * @kcontrol: mreg control
  564. * @uinfo: control element information
  565. *
  566. * Callback to provide information of a control that can span multiple
  567. * codec registers which together forms a single signed value. Note
  568. * that unlike the non-xr variant of sx controls these may or may not
  569. * include the sign bit, depending on nbits, and there is no shift.
  570. *
  571. * Returns 0 for success.
  572. */
  573. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  574. struct snd_ctl_elem_info *uinfo)
  575. {
  576. struct soc_mreg_control *mc =
  577. (struct soc_mreg_control *)kcontrol->private_value;
  578. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  579. uinfo->count = 1;
  580. uinfo->value.integer.min = mc->min;
  581. uinfo->value.integer.max = mc->max;
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  585. /**
  586. * snd_soc_get_xr_sx - signed multi register get callback
  587. * @kcontrol: mreg control
  588. * @ucontrol: control element information
  589. *
  590. * Callback to get the value of a control that can span multiple codec
  591. * registers which together forms a single signed value. The control
  592. * supports specifying total no of bits used to allow for bitfields
  593. * across the multiple codec registers. Note that unlike the non-xr
  594. * variant of sx controls these may or may not include the sign bit,
  595. * depending on nbits, and there is no shift.
  596. *
  597. * Returns 0 for success.
  598. */
  599. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  600. struct snd_ctl_elem_value *ucontrol)
  601. {
  602. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  603. struct soc_mreg_control *mc =
  604. (struct soc_mreg_control *)kcontrol->private_value;
  605. unsigned int regbase = mc->regbase;
  606. unsigned int regcount = mc->regcount;
  607. unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
  608. unsigned int regwmask = GENMASK(regwshift - 1, 0);
  609. unsigned long mask = GENMASK(mc->nbits - 1, 0);
  610. long val = 0;
  611. unsigned int i;
  612. for (i = 0; i < regcount; i++) {
  613. unsigned int regval = snd_soc_component_read(component, regbase + i);
  614. val |= (regval & regwmask) << (regwshift * (regcount - i - 1));
  615. }
  616. val &= mask;
  617. if (mc->min < 0 && val > mc->max)
  618. val |= ~mask;
  619. if (mc->invert)
  620. val = mc->max - val;
  621. ucontrol->value.integer.value[0] = val;
  622. return 0;
  623. }
  624. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  625. /**
  626. * snd_soc_put_xr_sx - signed multi register get callback
  627. * @kcontrol: mreg control
  628. * @ucontrol: control element information
  629. *
  630. * Callback to set the value of a control that can span multiple codec
  631. * registers which together forms a single signed value. The control
  632. * supports specifying total no of bits used to allow for bitfields
  633. * across the multiple codec registers. Note that unlike the non-xr
  634. * variant of sx controls these may or may not include the sign bit,
  635. * depending on nbits, and there is no shift.
  636. *
  637. * Returns 0 for success.
  638. */
  639. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  640. struct snd_ctl_elem_value *ucontrol)
  641. {
  642. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  643. struct soc_mreg_control *mc =
  644. (struct soc_mreg_control *)kcontrol->private_value;
  645. unsigned int regbase = mc->regbase;
  646. unsigned int regcount = mc->regcount;
  647. unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
  648. unsigned int regwmask = GENMASK(regwshift - 1, 0);
  649. unsigned long mask = GENMASK(mc->nbits - 1, 0);
  650. long val = ucontrol->value.integer.value[0];
  651. int ret = 0;
  652. unsigned int i;
  653. if (val < mc->min || val > mc->max)
  654. return -EINVAL;
  655. if (mc->invert)
  656. val = mc->max - val;
  657. val &= mask;
  658. for (i = 0; i < regcount; i++) {
  659. unsigned int regval = (val >> (regwshift * (regcount - i - 1))) &
  660. regwmask;
  661. unsigned int regmask = (mask >> (regwshift * (regcount - i - 1))) &
  662. regwmask;
  663. int err = snd_soc_component_update_bits(component, regbase + i,
  664. regmask, regval);
  665. if (err < 0)
  666. return err;
  667. if (err > 0)
  668. ret = err;
  669. }
  670. return ret;
  671. }
  672. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  673. /**
  674. * snd_soc_get_strobe - strobe get callback
  675. * @kcontrol: mixer control
  676. * @ucontrol: control element information
  677. *
  678. * Callback get the value of a strobe mixer control.
  679. *
  680. * Returns 0 for success.
  681. */
  682. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  683. struct snd_ctl_elem_value *ucontrol)
  684. {
  685. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  686. struct soc_mixer_control *mc =
  687. (struct soc_mixer_control *)kcontrol->private_value;
  688. unsigned int invert = mc->invert != 0;
  689. unsigned int mask = BIT(mc->shift);
  690. unsigned int val;
  691. val = snd_soc_component_read(component, mc->reg);
  692. val &= mask;
  693. if (mc->shift != 0 && val != 0)
  694. val = val >> mc->shift;
  695. ucontrol->value.enumerated.item[0] = val ^ invert;
  696. return 0;
  697. }
  698. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  699. /**
  700. * snd_soc_put_strobe - strobe put callback
  701. * @kcontrol: mixer control
  702. * @ucontrol: control element information
  703. *
  704. * Callback strobe a register bit to high then low (or the inverse)
  705. * in one pass of a single mixer enum control.
  706. *
  707. * Returns 1 for success.
  708. */
  709. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  710. struct snd_ctl_elem_value *ucontrol)
  711. {
  712. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  713. struct soc_mixer_control *mc =
  714. (struct soc_mixer_control *)kcontrol->private_value;
  715. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  716. unsigned int invert = mc->invert != 0;
  717. unsigned int mask = BIT(mc->shift);
  718. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  719. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  720. int ret;
  721. ret = snd_soc_component_update_bits(component, mc->reg, mask, val1);
  722. if (ret < 0)
  723. return ret;
  724. return snd_soc_component_update_bits(component, mc->reg, mask, val2);
  725. }
  726. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);