tas2781_hda_spi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // TAS2781 HDA SPI driver
  4. //
  5. // Copyright 2024 - 2026 Texas Instruments, Inc.
  6. //
  7. // Author: Baojun Xu <baojun.xu@ti.com>
  8. #include <linux/acpi.h>
  9. #include <linux/array_size.h>
  10. #include <linux/bits.h>
  11. #include <linux/cleanup.h>
  12. #include <linux/crc8.h>
  13. #include <linux/crc32.h>
  14. #include <linux/efi.h>
  15. #include <linux/firmware.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/property.h>
  21. #include <linux/regmap.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/time.h>
  24. #include <linux/types.h>
  25. #include <linux/units.h>
  26. #include <sound/hda_codec.h>
  27. #include <sound/soc.h>
  28. #include <sound/tas2781.h>
  29. #include <sound/tlv.h>
  30. #include <sound/tas2781-tlv.h>
  31. #include "hda_local.h"
  32. #include "hda_auto_parser.h"
  33. #include "hda_component.h"
  34. #include "hda_jack.h"
  35. #include "../generic.h"
  36. #include "tas2781_hda.h"
  37. #define TASDEVICE_RANGE_MAX_SIZE (256 * 128)
  38. #define TASDEVICE_WIN_LEN 128
  39. #define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ)
  40. /* System Reset Check Register */
  41. #define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c)
  42. #define TAS2781_REG_CLK_CONFIG_RESET 0x19
  43. struct tas2781_hda_spi_priv {
  44. struct snd_kcontrol *snd_ctls[3];
  45. };
  46. static const struct regmap_range_cfg tasdevice_ranges[] = {
  47. {
  48. .range_min = 0,
  49. .range_max = TASDEVICE_RANGE_MAX_SIZE,
  50. .selector_reg = TASDEVICE_PAGE_SELECT,
  51. .selector_mask = GENMASK(7, 0),
  52. .selector_shift = 0,
  53. .window_start = 0,
  54. .window_len = TASDEVICE_WIN_LEN,
  55. },
  56. };
  57. static const struct regmap_config tasdevice_regmap = {
  58. .reg_bits = 8,
  59. .val_bits = 8,
  60. .zero_flag_mask = true,
  61. .read_flag_mask = 0x01,
  62. .reg_shift = -1,
  63. .cache_type = REGCACHE_NONE,
  64. .ranges = tasdevice_ranges,
  65. .num_ranges = ARRAY_SIZE(tasdevice_ranges),
  66. .max_register = TASDEVICE_RANGE_MAX_SIZE,
  67. };
  68. static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,
  69. unsigned short chn, unsigned int reg, unsigned int *val)
  70. {
  71. int ret;
  72. /*
  73. * In our TAS2781 SPI mode, if read from other book (not book 0),
  74. * or read from page number larger than 1 in book 0, one more byte
  75. * read is needed, and first byte is a dummy byte, need to be ignored.
  76. */
  77. if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
  78. unsigned char data[2];
  79. ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
  80. data, sizeof(data));
  81. *val = data[1];
  82. } else {
  83. ret = tasdevice_dev_read(tas_priv, chn, reg, val);
  84. }
  85. if (ret < 0)
  86. dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
  87. return ret;
  88. }
  89. static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,
  90. unsigned short chn, unsigned int reg, unsigned char *data,
  91. unsigned int len)
  92. {
  93. int ret;
  94. /*
  95. * In our TAS2781 SPI mode, if read from other book (not book 0),
  96. * or read from page number larger than 1 in book 0, one more byte
  97. * read is needed, and first byte is a dummy byte, need to be ignored.
  98. */
  99. if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
  100. unsigned char buf[TASDEVICE_WIN_LEN + 1];
  101. ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
  102. buf, len + 1);
  103. memcpy(data, buf + 1, len);
  104. } else {
  105. ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len);
  106. }
  107. if (ret < 0)
  108. dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
  109. return ret;
  110. }
  111. static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,
  112. unsigned short chn, unsigned int reg, unsigned int mask,
  113. unsigned int value)
  114. {
  115. int ret, val;
  116. /*
  117. * In our TAS2781 SPI mode, read/write was masked in last bit of
  118. * address, it cause regmap_update_bits() not work as expected.
  119. */
  120. ret = tasdevice_dev_read(tas_priv, chn, reg, &val);
  121. if (ret < 0) {
  122. dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
  123. return ret;
  124. }
  125. ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg),
  126. (val & ~mask) | (mask & value));
  127. if (ret < 0)
  128. dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
  129. return ret;
  130. }
  131. static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p,
  132. unsigned short chn, int book)
  133. {
  134. int ret = 0;
  135. if (chn == p->index) {
  136. struct tasdevice *tasdev = &p->tasdevice[chn];
  137. struct regmap *map = p->regmap;
  138. if (tasdev->cur_book != book) {
  139. ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);
  140. if (ret < 0)
  141. dev_err(p->dev, "%s, E=%d\n", __func__, ret);
  142. else
  143. tasdev->cur_book = book;
  144. }
  145. } else {
  146. ret = -EXDEV;
  147. dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n",
  148. __func__, chn);
  149. }
  150. return ret;
  151. }
  152. static void tas2781_spi_reset(struct tasdevice_priv *tas_dev)
  153. {
  154. int ret;
  155. if (tas_dev->reset) {
  156. gpiod_set_value_cansleep(tas_dev->reset, 0);
  157. fsleep(800);
  158. gpiod_set_value_cansleep(tas_dev->reset, 1);
  159. } else {
  160. ret = tasdevice_dev_write(tas_dev, tas_dev->index,
  161. TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET);
  162. if (ret < 0) {
  163. dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);
  164. return;
  165. }
  166. fsleep(1000);
  167. }
  168. }
  169. static int tascodec_spi_init(struct tasdevice_priv *tas_priv,
  170. void *codec, struct module *module,
  171. void (*cont)(const struct firmware *fw, void *context))
  172. {
  173. int ret;
  174. /*
  175. * Codec Lock Hold to ensure that codec_probe and firmware parsing and
  176. * loading do not simultaneously execute.
  177. */
  178. guard(mutex)(&tas_priv->codec_lock);
  179. scnprintf(tas_priv->rca_binaryname,
  180. sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",
  181. tas_priv->dev_name, tas_priv->ndev);
  182. crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
  183. tas_priv->codec = codec;
  184. ret = request_firmware_nowait(module, FW_ACTION_UEVENT,
  185. tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,
  186. cont);
  187. if (ret)
  188. dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
  189. ret);
  190. return ret;
  191. }
  192. static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)
  193. {
  194. tas_priv->tasdevice[tas_priv->index].cur_book = -1;
  195. tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
  196. tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
  197. tas_priv->isspi = true;
  198. tas_priv->update_bits = tasdevice_spi_dev_update_bits;
  199. tas_priv->change_chn_book = tasdevice_spi_change_chn_book;
  200. tas_priv->dev_read = tasdevice_spi_dev_read;
  201. tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read;
  202. mutex_init(&tas_priv->codec_lock);
  203. }
  204. static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv,
  205. struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
  206. {
  207. unsigned int invert = mc->invert;
  208. unsigned char mask;
  209. int max = mc->max;
  210. int val, ret;
  211. mask = rounddown_pow_of_two(max);
  212. mask <<= mc->shift;
  213. val = clamp(invert ? max - ucontrol->value.integer.value[0] :
  214. ucontrol->value.integer.value[0], 0, max);
  215. ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index,
  216. mc->reg, mask, (unsigned int)(val << mc->shift));
  217. if (ret)
  218. dev_err(tas_priv->dev, "set AMP vol error in dev %d\n",
  219. tas_priv->index);
  220. return ret;
  221. }
  222. static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv,
  223. struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
  224. {
  225. unsigned int invert = mc->invert;
  226. unsigned char mask = 0;
  227. int max = mc->max;
  228. int ret, val;
  229. ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val);
  230. if (ret) {
  231. dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
  232. return ret;
  233. }
  234. mask = rounddown_pow_of_two(max);
  235. mask <<= mc->shift;
  236. val = (val & mask) >> mc->shift;
  237. val = clamp(invert ? max - val : val, 0, max);
  238. ucontrol->value.integer.value[0] = val;
  239. return ret;
  240. }
  241. static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p,
  242. struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
  243. {
  244. unsigned int invert = mc->invert;
  245. int max = mc->max;
  246. int val, ret;
  247. val = clamp(invert ? max - ucontrol->value.integer.value[0] :
  248. ucontrol->value.integer.value[0], 0, max);
  249. ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val);
  250. if (ret)
  251. dev_err(p->dev, "set digital vol err in dev %d\n", p->index);
  252. return ret;
  253. }
  254. static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p,
  255. struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
  256. {
  257. unsigned int invert = mc->invert;
  258. int max = mc->max;
  259. int ret, val;
  260. ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val);
  261. if (ret) {
  262. dev_err(p->dev, "%s, get digital vol err\n", __func__);
  263. return ret;
  264. }
  265. val = clamp(invert ? max - val : val, 0, max);
  266. ucontrol->value.integer.value[0] = val;
  267. return ret;
  268. }
  269. static int tas2781_read_acpi(struct tas2781_hda *tas_hda,
  270. const char *hid, int id)
  271. {
  272. struct tasdevice_priv *p = tas_hda->priv;
  273. struct acpi_device *adev;
  274. struct device *physdev;
  275. u32 values[HDA_MAX_COMPONENTS];
  276. const char *property;
  277. size_t nval;
  278. int ret, i;
  279. adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
  280. if (!adev) {
  281. dev_err(p->dev, "Failed to find ACPI device: %s\n", hid);
  282. return -ENODEV;
  283. }
  284. strscpy(p->dev_name, hid, sizeof(p->dev_name));
  285. physdev = get_device(acpi_get_first_physical_node(adev));
  286. acpi_dev_put(adev);
  287. property = "ti,dev-index";
  288. ret = device_property_count_u32(physdev, property);
  289. if (ret <= 0 || ret > ARRAY_SIZE(values)) {
  290. ret = -EINVAL;
  291. goto err;
  292. }
  293. p->ndev = nval = ret;
  294. ret = device_property_read_u32_array(physdev, property, values, nval);
  295. if (ret)
  296. goto err;
  297. p->index = U8_MAX;
  298. for (i = 0; i < nval; i++) {
  299. if (values[i] == id) {
  300. p->index = i;
  301. break;
  302. }
  303. }
  304. if (p->index == U8_MAX) {
  305. dev_dbg(p->dev, "No index found in %s\n", property);
  306. ret = -ENODEV;
  307. goto err;
  308. }
  309. if (p->index == 0) {
  310. /* All of amps share same RESET pin. */
  311. p->reset = devm_gpiod_get_index_optional(physdev, "reset",
  312. p->index, GPIOD_OUT_LOW);
  313. if (IS_ERR(p->reset)) {
  314. ret = PTR_ERR(p->reset);
  315. dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");
  316. goto err;
  317. }
  318. }
  319. put_device(physdev);
  320. return 0;
  321. err:
  322. dev_err(p->dev, "read acpi error, ret: %d\n", ret);
  323. put_device(physdev);
  324. acpi_dev_put(adev);
  325. return ret;
  326. }
  327. static void tas2781_hda_playback_hook(struct device *dev, int action)
  328. {
  329. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  330. struct tasdevice_priv *tas_priv = tas_hda->priv;
  331. if (action == HDA_GEN_PCM_ACT_OPEN) {
  332. pm_runtime_get_sync(dev);
  333. guard(mutex)(&tas_priv->codec_lock);
  334. if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
  335. tasdevice_tuning_switch(tas_hda->priv, 0);
  336. } else if (action == HDA_GEN_PCM_ACT_CLOSE) {
  337. guard(mutex)(&tas_priv->codec_lock);
  338. if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
  339. tasdevice_tuning_switch(tas_priv, 1);
  340. pm_runtime_put_autosuspend(dev);
  341. }
  342. }
  343. /*
  344. * tas2781_digital_getvol - get the volum control
  345. * @kcontrol: control pointer
  346. * @ucontrol: User data
  347. *
  348. * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
  349. * depends on internal regmap mechanism.
  350. * tas2781 contains book and page two-level register map, especially
  351. * book switching will set the register BXXP00R7F, after switching to the
  352. * correct book, then leverage the mechanism for paging to access the
  353. * register.
  354. *
  355. * Return 0 if succeeded.
  356. */
  357. static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
  358. struct snd_ctl_elem_value *ucontrol)
  359. {
  360. struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
  361. struct soc_mixer_control *mc =
  362. (struct soc_mixer_control *)kcontrol->private_value;
  363. guard(mutex)(&tas_priv->codec_lock);
  364. return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc);
  365. }
  366. static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
  367. struct snd_ctl_elem_value *ucontrol)
  368. {
  369. struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
  370. struct soc_mixer_control *mc =
  371. (struct soc_mixer_control *)kcontrol->private_value;
  372. guard(mutex)(&tas_priv->codec_lock);
  373. return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc);
  374. }
  375. static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
  376. struct snd_ctl_elem_value *ucontrol)
  377. {
  378. struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
  379. struct soc_mixer_control *mc =
  380. (struct soc_mixer_control *)kcontrol->private_value;
  381. guard(mutex)(&tas_priv->codec_lock);
  382. return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc);
  383. }
  384. static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
  385. struct snd_ctl_elem_value *ucontrol)
  386. {
  387. struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
  388. struct soc_mixer_control *mc =
  389. (struct soc_mixer_control *)kcontrol->private_value;
  390. guard(mutex)(&tas_priv->codec_lock);
  391. return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc);
  392. }
  393. static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
  394. struct snd_ctl_elem_value *ucontrol)
  395. {
  396. struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
  397. ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
  398. dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
  399. str_on_off(tas_priv->force_fwload_status));
  400. return 0;
  401. }
  402. static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
  403. struct snd_ctl_elem_value *ucontrol)
  404. {
  405. struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
  406. bool change, val = (bool)ucontrol->value.integer.value[0];
  407. if (tas_priv->force_fwload_status == val) {
  408. change = false;
  409. } else {
  410. change = true;
  411. tas_priv->force_fwload_status = val;
  412. }
  413. dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
  414. str_on_off(tas_priv->force_fwload_status));
  415. return change;
  416. }
  417. static struct snd_kcontrol_new tas2781_snd_ctls[] = {
  418. ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0,
  419. tas2781_amp_getvol, tas2781_amp_putvol,
  420. tas2781_amp_tlv),
  421. ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1,
  422. tas2781_digital_getvol, tas2781_digital_putvol,
  423. tas2781_dvc_tlv),
  424. ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get,
  425. tas2781_force_fwload_put),
  426. };
  427. static struct snd_kcontrol_new tas2781_prof_ctl = {
  428. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  429. .info = tasdevice_info_profile,
  430. .get = tasdevice_get_profile_id,
  431. .put = tasdevice_set_profile_id,
  432. };
  433. static struct snd_kcontrol_new tas2781_dsp_ctls[] = {
  434. /* Speaker Program */
  435. {
  436. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  437. .info = tasdevice_info_programs,
  438. .get = tasdevice_program_get,
  439. .put = tasdevice_program_put,
  440. },
  441. /* Speaker Config */
  442. {
  443. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  444. .info = tasdevice_info_config,
  445. .get = tasdevice_config_get,
  446. .put = tasdevice_config_put,
  447. },
  448. };
  449. static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
  450. {
  451. struct hda_codec *codec = tas_hda->priv->codec;
  452. struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv;
  453. snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
  454. snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
  455. for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--)
  456. snd_ctl_remove(codec->card, h_priv->snd_ctls[i]);
  457. snd_ctl_remove(codec->card, tas_hda->prof_ctl);
  458. }
  459. static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h)
  460. {
  461. struct tasdevice_priv *p = h->priv;
  462. struct hda_codec *c = p->codec;
  463. char name[64];
  464. int rc;
  465. snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index);
  466. tas2781_prof_ctl.name = name;
  467. h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p);
  468. rc = snd_ctl_add(c->card, h->prof_ctl);
  469. if (rc)
  470. dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
  471. tas2781_prof_ctl.name, rc);
  472. return rc;
  473. }
  474. static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h)
  475. {
  476. struct tas2781_hda_spi_priv *h_priv = h->hda_priv;
  477. struct tasdevice_priv *p = h->priv;
  478. struct hda_codec *c = p->codec;
  479. char name[64];
  480. int i = 0;
  481. int rc;
  482. snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index);
  483. tas2781_snd_ctls[i].name = name;
  484. h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
  485. rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
  486. if (rc) {
  487. dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
  488. tas2781_snd_ctls[i].name, rc);
  489. return rc;
  490. }
  491. i++;
  492. snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index);
  493. tas2781_snd_ctls[i].name = name;
  494. h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
  495. rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
  496. if (rc) {
  497. dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
  498. tas2781_snd_ctls[i].name, rc);
  499. return rc;
  500. }
  501. i++;
  502. snprintf(name, sizeof(name), "Froce Speaker-%d FW Load", p->index);
  503. tas2781_snd_ctls[i].name = name;
  504. h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
  505. rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
  506. if (rc) {
  507. dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
  508. tas2781_snd_ctls[i].name, rc);
  509. }
  510. return rc;
  511. }
  512. static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h)
  513. {
  514. struct tasdevice_priv *p = h->priv;
  515. struct hda_codec *c = p->codec;
  516. char name[64];
  517. int i = 0;
  518. int rc;
  519. snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index);
  520. tas2781_dsp_ctls[i].name = name;
  521. h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
  522. rc = snd_ctl_add(c->card, h->dsp_prog_ctl);
  523. if (rc) {
  524. dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
  525. tas2781_dsp_ctls[i].name, rc);
  526. return rc;
  527. }
  528. i++;
  529. snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index);
  530. tas2781_dsp_ctls[i].name = name;
  531. h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
  532. rc = snd_ctl_add(c->card, h->dsp_conf_ctl);
  533. if (rc) {
  534. dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
  535. tas2781_dsp_ctls[i].name, rc);
  536. }
  537. return rc;
  538. }
  539. static void tasdev_fw_ready(const struct firmware *fmw, void *context)
  540. {
  541. struct tasdevice_priv *tas_priv = context;
  542. struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
  543. struct hda_codec *codec = tas_priv->codec;
  544. int ret;
  545. guard(pm_runtime_active_auto)(tas_priv->dev);
  546. guard(mutex)(&tas_priv->codec_lock);
  547. ret = tasdevice_rca_parser(tas_priv, fmw);
  548. if (ret)
  549. goto out;
  550. /* Add control one time only. */
  551. ret = tas2781_hda_spi_prf_ctl(tas_hda);
  552. if (ret)
  553. goto out;
  554. ret = tas2781_hda_spi_snd_ctls(tas_hda);
  555. if (ret)
  556. goto out;
  557. tasdevice_dsp_remove(tas_priv);
  558. tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
  559. scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin",
  560. lower_16_bits(codec->core.subsystem_id), tas_priv->index);
  561. ret = tasdevice_dsp_parser(tas_priv);
  562. if (ret) {
  563. dev_err(tas_priv->dev, "dspfw load %s error\n",
  564. tas_priv->coef_binaryname);
  565. tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
  566. goto out;
  567. }
  568. ret = tas2781_hda_spi_dsp_ctls(tas_hda);
  569. if (ret)
  570. goto out;
  571. /* Perform AMP reset before firmware download. */
  572. tas2781_spi_reset(tas_priv);
  573. tas_priv->rcabin.profile_cfg_id = 0;
  574. tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
  575. ret = tasdevice_prmg_load(tas_priv, 0);
  576. if (ret < 0) {
  577. dev_err(tas_priv->dev, "FW download failed = %d\n", ret);
  578. goto out;
  579. }
  580. tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
  581. if (tas_priv->fmw->nr_programs > 0)
  582. tas_priv->tasdevice[tas_priv->index].cur_prog = 0;
  583. if (tas_priv->fmw->nr_configurations > 0)
  584. tas_priv->tasdevice[tas_priv->index].cur_conf = 0;
  585. /*
  586. * If calibrated data occurs error, dsp will still works with default
  587. * calibrated data inside algo.
  588. */
  589. tas2781_save_calibration(tas_hda);
  590. out:
  591. release_firmware(fmw);
  592. }
  593. static int tas2781_hda_bind(struct device *dev, struct device *master,
  594. void *master_data)
  595. {
  596. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  597. struct hda_component_parent *parent = master_data;
  598. struct hda_component *comp;
  599. struct hda_codec *codec;
  600. int ret;
  601. comp = hda_component_from_index(parent, tas_hda->priv->index);
  602. if (!comp)
  603. return -EINVAL;
  604. if (comp->dev)
  605. return -EBUSY;
  606. codec = parent->codec;
  607. guard(pm_runtime_active_auto)(dev);
  608. comp->dev = dev;
  609. strscpy(comp->name, dev_name(dev), sizeof(comp->name));
  610. ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,
  611. tasdev_fw_ready);
  612. if (!ret)
  613. comp->playback_hook = tas2781_hda_playback_hook;
  614. /* Only HP Laptop support SPI-based TAS2781 */
  615. tas_hda->catlog_id = HP;
  616. return ret;
  617. }
  618. static void tas2781_hda_unbind(struct device *dev, struct device *master,
  619. void *master_data)
  620. {
  621. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  622. struct hda_component_parent *parent = master_data;
  623. struct tasdevice_priv *tas_priv = tas_hda->priv;
  624. struct hda_component *comp;
  625. comp = hda_component_from_index(parent, tas_priv->index);
  626. if (comp && (comp->dev == dev)) {
  627. comp->dev = NULL;
  628. memset(comp->name, 0, sizeof(comp->name));
  629. comp->playback_hook = NULL;
  630. }
  631. tas2781_hda_remove_controls(tas_hda);
  632. tasdevice_config_info_remove(tas_priv);
  633. tasdevice_dsp_remove(tas_priv);
  634. tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
  635. }
  636. static const struct component_ops tas2781_hda_comp_ops = {
  637. .bind = tas2781_hda_bind,
  638. .unbind = tas2781_hda_unbind,
  639. };
  640. static int tas2781_hda_spi_probe(struct spi_device *spi)
  641. {
  642. struct tas2781_hda_spi_priv *hda_priv;
  643. struct tasdevice_priv *tas_priv;
  644. struct tas2781_hda *tas_hda;
  645. const char *device_name;
  646. int ret = 0;
  647. tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);
  648. if (!tas_hda)
  649. return -ENOMEM;
  650. hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL);
  651. if (!hda_priv)
  652. return -ENOMEM;
  653. tas_hda->hda_priv = hda_priv;
  654. spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;
  655. tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);
  656. if (!tas_priv)
  657. return -ENOMEM;
  658. tas_priv->dev = &spi->dev;
  659. tas_hda->priv = tas_priv;
  660. tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);
  661. if (IS_ERR(tas_priv->regmap)) {
  662. ret = PTR_ERR(tas_priv->regmap);
  663. dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",
  664. ret);
  665. return ret;
  666. }
  667. if (strstr(dev_name(&spi->dev), "TXNW2781")) {
  668. device_name = "TXNW2781";
  669. } else {
  670. dev_err(tas_priv->dev, "Unmatched spi dev %s\n",
  671. dev_name(&spi->dev));
  672. return -ENODEV;
  673. }
  674. tas_priv->irq = spi->irq;
  675. dev_set_drvdata(&spi->dev, tas_hda);
  676. ret = tas2781_read_acpi(tas_hda, device_name,
  677. spi_get_chipselect(spi, 0));
  678. if (ret)
  679. return dev_err_probe(tas_priv->dev, ret,
  680. "Platform not supported\n");
  681. tasdevice_spi_init(tas_priv);
  682. pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);
  683. pm_runtime_use_autosuspend(tas_priv->dev);
  684. pm_runtime_set_active(tas_priv->dev);
  685. pm_runtime_get_noresume(tas_priv->dev);
  686. pm_runtime_enable(tas_priv->dev);
  687. pm_runtime_put_autosuspend(tas_priv->dev);
  688. ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);
  689. if (ret) {
  690. dev_err(tas_priv->dev, "Register component fail: %d\n", ret);
  691. pm_runtime_disable(tas_priv->dev);
  692. tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
  693. }
  694. return ret;
  695. }
  696. static void tas2781_hda_spi_remove(struct spi_device *spi)
  697. {
  698. tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
  699. }
  700. static int tas2781_runtime_suspend(struct device *dev)
  701. {
  702. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  703. struct tasdevice_priv *tas_priv = tas_hda->priv;
  704. guard(mutex)(&tas_priv->codec_lock);
  705. if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
  706. && tas_priv->playback_started)
  707. tasdevice_tuning_switch(tas_priv, 1);
  708. tas_priv->tasdevice[tas_priv->index].cur_book = -1;
  709. tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
  710. return 0;
  711. }
  712. static int tas2781_runtime_resume(struct device *dev)
  713. {
  714. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  715. struct tasdevice_priv *tas_priv = tas_hda->priv;
  716. guard(mutex)(&tas_priv->codec_lock);
  717. if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
  718. && tas_priv->playback_started)
  719. tasdevice_tuning_switch(tas_priv, 0);
  720. return 0;
  721. }
  722. static int tas2781_system_suspend(struct device *dev)
  723. {
  724. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  725. struct tasdevice_priv *tas_priv = tas_hda->priv;
  726. int ret;
  727. ret = pm_runtime_force_suspend(dev);
  728. if (ret)
  729. return ret;
  730. /* Shutdown chip before system suspend */
  731. if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
  732. && tas_priv->playback_started)
  733. tasdevice_tuning_switch(tas_priv, 1);
  734. return 0;
  735. }
  736. static int tas2781_system_resume(struct device *dev)
  737. {
  738. struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
  739. struct tasdevice_priv *tas_priv = tas_hda->priv;
  740. int ret, val;
  741. ret = pm_runtime_force_resume(dev);
  742. if (ret)
  743. return ret;
  744. guard(mutex)(&tas_priv->codec_lock);
  745. ret = tas_priv->dev_read(tas_priv, tas_priv->index,
  746. TAS2781_REG_CLK_CONFIG, &val);
  747. if (ret < 0)
  748. return ret;
  749. if (val == TAS2781_REG_CLK_CONFIG_RESET) {
  750. tas_priv->tasdevice[tas_priv->index].cur_book = -1;
  751. tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
  752. tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
  753. ret = tasdevice_prmg_load(tas_priv, 0);
  754. if (ret < 0) {
  755. dev_err(tas_priv->dev,
  756. "FW download failed = %d\n", ret);
  757. return ret;
  758. }
  759. tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
  760. if (tas_priv->playback_started)
  761. tasdevice_tuning_switch(tas_priv, 0);
  762. }
  763. return ret;
  764. }
  765. static const struct dev_pm_ops tas2781_hda_pm_ops = {
  766. RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
  767. SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
  768. };
  769. static const struct spi_device_id tas2781_hda_spi_id[] = {
  770. { "tas2781-hda", },
  771. {}
  772. };
  773. static const struct acpi_device_id tas2781_acpi_hda_match[] = {
  774. {"TXNW2781", },
  775. {}
  776. };
  777. MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
  778. static struct spi_driver tas2781_hda_spi_driver = {
  779. .driver = {
  780. .name = "tas2781-hda",
  781. .acpi_match_table = tas2781_acpi_hda_match,
  782. .pm = &tas2781_hda_pm_ops,
  783. },
  784. .id_table = tas2781_hda_spi_id,
  785. .probe = tas2781_hda_spi_probe,
  786. .remove = tas2781_hda_spi_remove,
  787. };
  788. module_spi_driver(tas2781_hda_spi_driver);
  789. MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");
  790. MODULE_AUTHOR("Baojun, Xu, <baojun.xug@ti.com>");
  791. MODULE_LICENSE("GPL");
  792. MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
  793. MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");