rt1308-sdw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // rt1308-sdw.c -- rt1308 ALSA SoC audio driver
  4. //
  5. // Copyright(c) 2019 Realtek Semiconductor Corp.
  6. //
  7. //
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/soundwire/sdw.h>
  13. #include <linux/soundwire/sdw_type.h>
  14. #include <linux/soundwire/sdw_registers.h>
  15. #include <linux/module.h>
  16. #include <linux/regmap.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <sound/soc-dapm.h>
  22. #include <sound/initval.h>
  23. #include "rt1308.h"
  24. #include "rt1308-sdw.h"
  25. static bool rt1308_readable_register(struct device *dev, unsigned int reg)
  26. {
  27. switch (reg) {
  28. case 0x00e0:
  29. case 0x00f0:
  30. case 0x2f01 ... 0x2f07:
  31. case 0x3000 ... 0x3001:
  32. case 0x3004 ... 0x3005:
  33. case 0x3008:
  34. case 0x300a:
  35. case 0xc000 ... 0xcff3:
  36. return true;
  37. default:
  38. return false;
  39. }
  40. }
  41. static bool rt1308_volatile_register(struct device *dev, unsigned int reg)
  42. {
  43. switch (reg) {
  44. case 0x2f01 ... 0x2f07:
  45. case 0x3000 ... 0x3001:
  46. case 0x3004 ... 0x3005:
  47. case 0x3008:
  48. case 0x300a:
  49. case 0xc000:
  50. case 0xc710:
  51. case 0xc860 ... 0xc863:
  52. case 0xc870 ... 0xc873:
  53. return true;
  54. default:
  55. return false;
  56. }
  57. }
  58. static const struct regmap_config rt1308_sdw_regmap = {
  59. .reg_bits = 32,
  60. .val_bits = 8,
  61. .readable_reg = rt1308_readable_register,
  62. .volatile_reg = rt1308_volatile_register,
  63. .max_register = 0xcfff,
  64. .reg_defaults = rt1308_reg_defaults,
  65. .num_reg_defaults = ARRAY_SIZE(rt1308_reg_defaults),
  66. .cache_type = REGCACHE_RBTREE,
  67. .use_single_read = true,
  68. .use_single_write = true,
  69. };
  70. /* Bus clock frequency */
  71. #define RT1308_CLK_FREQ_9600000HZ 9600000
  72. #define RT1308_CLK_FREQ_12000000HZ 12000000
  73. #define RT1308_CLK_FREQ_6000000HZ 6000000
  74. #define RT1308_CLK_FREQ_4800000HZ 4800000
  75. #define RT1308_CLK_FREQ_2400000HZ 2400000
  76. #define RT1308_CLK_FREQ_12288000HZ 12288000
  77. static int rt1308_clock_config(struct device *dev)
  78. {
  79. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
  80. unsigned int clk_freq, value;
  81. clk_freq = (rt1308->params.curr_dr_freq >> 1);
  82. switch (clk_freq) {
  83. case RT1308_CLK_FREQ_12000000HZ:
  84. value = 0x0;
  85. break;
  86. case RT1308_CLK_FREQ_6000000HZ:
  87. value = 0x1;
  88. break;
  89. case RT1308_CLK_FREQ_9600000HZ:
  90. value = 0x2;
  91. break;
  92. case RT1308_CLK_FREQ_4800000HZ:
  93. value = 0x3;
  94. break;
  95. case RT1308_CLK_FREQ_2400000HZ:
  96. value = 0x4;
  97. break;
  98. case RT1308_CLK_FREQ_12288000HZ:
  99. value = 0x5;
  100. break;
  101. default:
  102. return -EINVAL;
  103. }
  104. regmap_write(rt1308->regmap, 0xe0, value);
  105. regmap_write(rt1308->regmap, 0xf0, value);
  106. dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
  107. return 0;
  108. }
  109. static int rt1308_read_prop(struct sdw_slave *slave)
  110. {
  111. struct sdw_slave_prop *prop = &slave->prop;
  112. int nval, i;
  113. u32 bit;
  114. unsigned long addr;
  115. struct sdw_dpn_prop *dpn;
  116. prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
  117. prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
  118. prop->paging_support = true;
  119. /* first we need to allocate memory for set bits in port lists */
  120. prop->source_ports = 0x00; /* BITMAP: 00010100 (not enable yet) */
  121. prop->sink_ports = 0x2; /* BITMAP: 00000010 */
  122. /* for sink */
  123. nval = hweight32(prop->sink_ports);
  124. prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
  125. sizeof(*prop->sink_dpn_prop),
  126. GFP_KERNEL);
  127. if (!prop->sink_dpn_prop)
  128. return -ENOMEM;
  129. i = 0;
  130. dpn = prop->sink_dpn_prop;
  131. addr = prop->sink_ports;
  132. for_each_set_bit(bit, &addr, 32) {
  133. dpn[i].num = bit;
  134. dpn[i].type = SDW_DPN_FULL;
  135. dpn[i].simple_ch_prep_sm = true;
  136. dpn[i].ch_prep_timeout = 10;
  137. i++;
  138. }
  139. /* set the timeout values */
  140. prop->clk_stop_timeout = 20;
  141. dev_dbg(&slave->dev, "%s\n", __func__);
  142. return 0;
  143. }
  144. static void rt1308_apply_calib_params(struct rt1308_sdw_priv *rt1308)
  145. {
  146. unsigned int efuse_m_btl_l, efuse_m_btl_r, tmp;
  147. unsigned int efuse_c_btl_l, efuse_c_btl_r;
  148. /* read efuse to apply calibration parameters */
  149. regmap_write(rt1308->regmap, 0xc7f0, 0x04);
  150. regmap_write(rt1308->regmap, 0xc7f1, 0xfe);
  151. msleep(100);
  152. regmap_write(rt1308->regmap, 0xc7f0, 0x44);
  153. msleep(20);
  154. regmap_write(rt1308->regmap, 0xc240, 0x10);
  155. regmap_read(rt1308->regmap, 0xc861, &tmp);
  156. efuse_m_btl_l = tmp;
  157. regmap_read(rt1308->regmap, 0xc860, &tmp);
  158. efuse_m_btl_l = efuse_m_btl_l | (tmp << 8);
  159. regmap_read(rt1308->regmap, 0xc863, &tmp);
  160. efuse_c_btl_l = tmp;
  161. regmap_read(rt1308->regmap, 0xc862, &tmp);
  162. efuse_c_btl_l = efuse_c_btl_l | (tmp << 8);
  163. regmap_read(rt1308->regmap, 0xc871, &tmp);
  164. efuse_m_btl_r = tmp;
  165. regmap_read(rt1308->regmap, 0xc870, &tmp);
  166. efuse_m_btl_r = efuse_m_btl_r | (tmp << 8);
  167. regmap_read(rt1308->regmap, 0xc873, &tmp);
  168. efuse_c_btl_r = tmp;
  169. regmap_read(rt1308->regmap, 0xc872, &tmp);
  170. efuse_c_btl_r = efuse_c_btl_r | (tmp << 8);
  171. dev_dbg(&rt1308->sdw_slave->dev, "%s m_btl_l=0x%x, m_btl_r=0x%x\n", __func__,
  172. efuse_m_btl_l, efuse_m_btl_r);
  173. dev_dbg(&rt1308->sdw_slave->dev, "%s c_btl_l=0x%x, c_btl_r=0x%x\n", __func__,
  174. efuse_c_btl_l, efuse_c_btl_r);
  175. }
  176. static int rt1308_io_init(struct device *dev, struct sdw_slave *slave)
  177. {
  178. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
  179. int ret = 0;
  180. unsigned int tmp;
  181. if (rt1308->hw_init)
  182. return 0;
  183. if (rt1308->first_hw_init) {
  184. regcache_cache_only(rt1308->regmap, false);
  185. regcache_cache_bypass(rt1308->regmap, true);
  186. }
  187. /*
  188. * PM runtime is only enabled when a Slave reports as Attached
  189. */
  190. if (!rt1308->first_hw_init) {
  191. /* set autosuspend parameters */
  192. pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
  193. pm_runtime_use_autosuspend(&slave->dev);
  194. /* update count of parent 'active' children */
  195. pm_runtime_set_active(&slave->dev);
  196. /* make sure the device does not suspend immediately */
  197. pm_runtime_mark_last_busy(&slave->dev);
  198. pm_runtime_enable(&slave->dev);
  199. }
  200. pm_runtime_get_noresume(&slave->dev);
  201. /* sw reset */
  202. regmap_write(rt1308->regmap, RT1308_SDW_RESET, 0);
  203. regmap_read(rt1308->regmap, 0xc710, &tmp);
  204. rt1308->hw_ver = tmp;
  205. dev_dbg(dev, "%s, hw_ver=0x%x\n", __func__, rt1308->hw_ver);
  206. /* initial settings */
  207. regmap_write(rt1308->regmap, 0xc103, 0xc0);
  208. regmap_write(rt1308->regmap, 0xc030, 0x17);
  209. regmap_write(rt1308->regmap, 0xc031, 0x81);
  210. regmap_write(rt1308->regmap, 0xc032, 0x26);
  211. regmap_write(rt1308->regmap, 0xc040, 0x80);
  212. regmap_write(rt1308->regmap, 0xc041, 0x80);
  213. regmap_write(rt1308->regmap, 0xc042, 0x06);
  214. regmap_write(rt1308->regmap, 0xc052, 0x0a);
  215. regmap_write(rt1308->regmap, 0xc080, 0x0a);
  216. regmap_write(rt1308->regmap, 0xc060, 0x02);
  217. regmap_write(rt1308->regmap, 0xc061, 0x75);
  218. regmap_write(rt1308->regmap, 0xc062, 0x05);
  219. regmap_write(rt1308->regmap, 0xc171, 0x07);
  220. regmap_write(rt1308->regmap, 0xc173, 0x0d);
  221. if (rt1308->hw_ver == RT1308_VER_C) {
  222. regmap_write(rt1308->regmap, 0xc311, 0x7f);
  223. regmap_write(rt1308->regmap, 0xc300, 0x09);
  224. } else {
  225. regmap_write(rt1308->regmap, 0xc311, 0x4f);
  226. regmap_write(rt1308->regmap, 0xc300, 0x0b);
  227. }
  228. regmap_write(rt1308->regmap, 0xc900, 0x5a);
  229. regmap_write(rt1308->regmap, 0xc1a0, 0x84);
  230. regmap_write(rt1308->regmap, 0xc1a1, 0x01);
  231. regmap_write(rt1308->regmap, 0xc360, 0x78);
  232. regmap_write(rt1308->regmap, 0xc361, 0x87);
  233. regmap_write(rt1308->regmap, 0xc0a1, 0x71);
  234. regmap_write(rt1308->regmap, 0xc210, 0x00);
  235. regmap_write(rt1308->regmap, 0xc070, 0x00);
  236. regmap_write(rt1308->regmap, 0xc100, 0xd7);
  237. regmap_write(rt1308->regmap, 0xc101, 0xd7);
  238. if (rt1308->first_hw_init) {
  239. regcache_cache_bypass(rt1308->regmap, false);
  240. regcache_mark_dirty(rt1308->regmap);
  241. } else
  242. rt1308->first_hw_init = true;
  243. /* Mark Slave initialization complete */
  244. rt1308->hw_init = true;
  245. pm_runtime_mark_last_busy(&slave->dev);
  246. pm_runtime_put_autosuspend(&slave->dev);
  247. dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
  248. return ret;
  249. }
  250. static int rt1308_update_status(struct sdw_slave *slave,
  251. enum sdw_slave_status status)
  252. {
  253. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
  254. /* Update the status */
  255. rt1308->status = status;
  256. if (status == SDW_SLAVE_UNATTACHED)
  257. rt1308->hw_init = false;
  258. /*
  259. * Perform initialization only if slave status is present and
  260. * hw_init flag is false
  261. */
  262. if (rt1308->hw_init || rt1308->status != SDW_SLAVE_ATTACHED)
  263. return 0;
  264. /* perform I/O transfers required for Slave initialization */
  265. return rt1308_io_init(&slave->dev, slave);
  266. }
  267. static int rt1308_bus_config(struct sdw_slave *slave,
  268. struct sdw_bus_params *params)
  269. {
  270. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
  271. int ret;
  272. memcpy(&rt1308->params, params, sizeof(*params));
  273. ret = rt1308_clock_config(&slave->dev);
  274. if (ret < 0)
  275. dev_err(&slave->dev, "Invalid clk config");
  276. return ret;
  277. }
  278. static int rt1308_interrupt_callback(struct sdw_slave *slave,
  279. struct sdw_slave_intr_status *status)
  280. {
  281. dev_dbg(&slave->dev,
  282. "%s control_port_stat=%x", __func__, status->control_port);
  283. return 0;
  284. }
  285. static int rt1308_classd_event(struct snd_soc_dapm_widget *w,
  286. struct snd_kcontrol *kcontrol, int event)
  287. {
  288. struct snd_soc_component *component =
  289. snd_soc_dapm_to_component(w->dapm);
  290. struct rt1308_sdw_priv *rt1308 =
  291. snd_soc_component_get_drvdata(component);
  292. switch (event) {
  293. case SND_SOC_DAPM_POST_PMU:
  294. msleep(30);
  295. snd_soc_component_update_bits(component,
  296. RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
  297. 0x3, 0x3);
  298. msleep(40);
  299. rt1308_apply_calib_params(rt1308);
  300. break;
  301. case SND_SOC_DAPM_PRE_PMD:
  302. snd_soc_component_update_bits(component,
  303. RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
  304. 0x3, 0);
  305. usleep_range(150000, 200000);
  306. break;
  307. default:
  308. break;
  309. }
  310. return 0;
  311. }
  312. static const char * const rt1308_rx_data_ch_select[] = {
  313. "LR",
  314. "LL",
  315. "RL",
  316. "RR",
  317. };
  318. static SOC_ENUM_SINGLE_DECL(rt1308_rx_data_ch_enum,
  319. RT1308_SDW_OFFSET | (RT1308_DATA_PATH << 4), 0,
  320. rt1308_rx_data_ch_select);
  321. static const struct snd_kcontrol_new rt1308_snd_controls[] = {
  322. /* I2S Data Channel Selection */
  323. SOC_ENUM("RX Channel Select", rt1308_rx_data_ch_enum),
  324. };
  325. static const struct snd_kcontrol_new rt1308_sto_dac_l =
  326. SOC_DAPM_SINGLE_AUTODISABLE("Switch",
  327. RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
  328. RT1308_DVOL_MUTE_L_EN_SFT, 1, 1);
  329. static const struct snd_kcontrol_new rt1308_sto_dac_r =
  330. SOC_DAPM_SINGLE_AUTODISABLE("Switch",
  331. RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
  332. RT1308_DVOL_MUTE_R_EN_SFT, 1, 1);
  333. static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
  334. /* Audio Interface */
  335. SND_SOC_DAPM_AIF_IN("AIF1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
  336. /* Supply Widgets */
  337. SND_SOC_DAPM_SUPPLY("MBIAS20U",
  338. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 7, 0, NULL, 0),
  339. SND_SOC_DAPM_SUPPLY("ALDO",
  340. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 6, 0, NULL, 0),
  341. SND_SOC_DAPM_SUPPLY("DBG",
  342. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 5, 0, NULL, 0),
  343. SND_SOC_DAPM_SUPPLY("DACL",
  344. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 4, 0, NULL, 0),
  345. SND_SOC_DAPM_SUPPLY("CLK25M",
  346. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 2, 0, NULL, 0),
  347. SND_SOC_DAPM_SUPPLY("ADC_R",
  348. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 1, 0, NULL, 0),
  349. SND_SOC_DAPM_SUPPLY("ADC_L",
  350. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 0, 0, NULL, 0),
  351. SND_SOC_DAPM_SUPPLY("DAC Power",
  352. RT1308_SDW_OFFSET | (RT1308_POWER << 4), 3, 0, NULL, 0),
  353. SND_SOC_DAPM_SUPPLY("DLDO",
  354. RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 5, 0, NULL, 0),
  355. SND_SOC_DAPM_SUPPLY("VREF",
  356. RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 4, 0, NULL, 0),
  357. SND_SOC_DAPM_SUPPLY("MIXER_R",
  358. RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 2, 0, NULL, 0),
  359. SND_SOC_DAPM_SUPPLY("MIXER_L",
  360. RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 1, 0, NULL, 0),
  361. SND_SOC_DAPM_SUPPLY("MBIAS4U",
  362. RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4), 0, 0, NULL, 0),
  363. SND_SOC_DAPM_SUPPLY("PLL2_LDO",
  364. RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 4, 0, NULL, 0),
  365. SND_SOC_DAPM_SUPPLY("PLL2B",
  366. RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 3, 0, NULL, 0),
  367. SND_SOC_DAPM_SUPPLY("PLL2F",
  368. RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 2, 0, NULL, 0),
  369. SND_SOC_DAPM_SUPPLY("PLL2F2",
  370. RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 1, 0, NULL, 0),
  371. SND_SOC_DAPM_SUPPLY("PLL2B2",
  372. RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 0, 0, NULL, 0),
  373. /* Digital Interface */
  374. SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
  375. SND_SOC_DAPM_SWITCH("DAC L", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_l),
  376. SND_SOC_DAPM_SWITCH("DAC R", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_r),
  377. /* Output Lines */
  378. SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
  379. rt1308_classd_event,
  380. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  381. SND_SOC_DAPM_OUTPUT("SPOL"),
  382. SND_SOC_DAPM_OUTPUT("SPOR"),
  383. };
  384. static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
  385. { "DAC", NULL, "AIF1RX" },
  386. { "DAC", NULL, "MBIAS20U" },
  387. { "DAC", NULL, "ALDO" },
  388. { "DAC", NULL, "DBG" },
  389. { "DAC", NULL, "DACL" },
  390. { "DAC", NULL, "CLK25M" },
  391. { "DAC", NULL, "ADC_R" },
  392. { "DAC", NULL, "ADC_L" },
  393. { "DAC", NULL, "DLDO" },
  394. { "DAC", NULL, "VREF" },
  395. { "DAC", NULL, "MIXER_R" },
  396. { "DAC", NULL, "MIXER_L" },
  397. { "DAC", NULL, "MBIAS4U" },
  398. { "DAC", NULL, "PLL2_LDO" },
  399. { "DAC", NULL, "PLL2B" },
  400. { "DAC", NULL, "PLL2F" },
  401. { "DAC", NULL, "PLL2F2" },
  402. { "DAC", NULL, "PLL2B2" },
  403. { "DAC L", "Switch", "DAC" },
  404. { "DAC R", "Switch", "DAC" },
  405. { "DAC L", NULL, "DAC Power" },
  406. { "DAC R", NULL, "DAC Power" },
  407. { "CLASS D", NULL, "DAC L" },
  408. { "CLASS D", NULL, "DAC R" },
  409. { "SPOL", NULL, "CLASS D" },
  410. { "SPOR", NULL, "CLASS D" },
  411. };
  412. static int rt1308_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
  413. int direction)
  414. {
  415. struct sdw_stream_data *stream;
  416. if (!sdw_stream)
  417. return 0;
  418. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  419. if (!stream)
  420. return -ENOMEM;
  421. stream->sdw_stream = sdw_stream;
  422. /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
  423. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  424. dai->playback_dma_data = stream;
  425. else
  426. dai->capture_dma_data = stream;
  427. return 0;
  428. }
  429. static void rt1308_sdw_shutdown(struct snd_pcm_substream *substream,
  430. struct snd_soc_dai *dai)
  431. {
  432. struct sdw_stream_data *stream;
  433. stream = snd_soc_dai_get_dma_data(dai, substream);
  434. snd_soc_dai_set_dma_data(dai, substream, NULL);
  435. kfree(stream);
  436. }
  437. static int rt1308_sdw_set_tdm_slot(struct snd_soc_dai *dai,
  438. unsigned int tx_mask,
  439. unsigned int rx_mask,
  440. int slots, int slot_width)
  441. {
  442. struct snd_soc_component *component = dai->component;
  443. struct rt1308_sdw_priv *rt1308 =
  444. snd_soc_component_get_drvdata(component);
  445. if (tx_mask)
  446. return -EINVAL;
  447. if (slots > 2)
  448. return -EINVAL;
  449. rt1308->rx_mask = rx_mask;
  450. rt1308->slots = slots;
  451. /* slot_width is not used since it's irrelevant for SoundWire */
  452. return 0;
  453. }
  454. static int rt1308_sdw_hw_params(struct snd_pcm_substream *substream,
  455. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  456. {
  457. struct snd_soc_component *component = dai->component;
  458. struct rt1308_sdw_priv *rt1308 =
  459. snd_soc_component_get_drvdata(component);
  460. struct sdw_stream_config stream_config;
  461. struct sdw_port_config port_config;
  462. enum sdw_data_direction direction;
  463. struct sdw_stream_data *stream;
  464. int retval, port, num_channels, ch_mask;
  465. dev_dbg(dai->dev, "%s %s", __func__, dai->name);
  466. stream = snd_soc_dai_get_dma_data(dai, substream);
  467. if (!stream)
  468. return -EINVAL;
  469. if (!rt1308->sdw_slave)
  470. return -EINVAL;
  471. /* SoundWire specific configuration */
  472. /* port 1 for playback */
  473. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  474. direction = SDW_DATA_DIR_RX;
  475. port = 1;
  476. } else {
  477. return -EINVAL;
  478. }
  479. if (rt1308->slots) {
  480. num_channels = rt1308->slots;
  481. ch_mask = rt1308->rx_mask;
  482. } else {
  483. num_channels = params_channels(params);
  484. ch_mask = (1 << num_channels) - 1;
  485. }
  486. stream_config.frame_rate = params_rate(params);
  487. stream_config.ch_count = num_channels;
  488. stream_config.bps = snd_pcm_format_width(params_format(params));
  489. stream_config.direction = direction;
  490. port_config.ch_mask = ch_mask;
  491. port_config.num = port;
  492. retval = sdw_stream_add_slave(rt1308->sdw_slave, &stream_config,
  493. &port_config, 1, stream->sdw_stream);
  494. if (retval) {
  495. dev_err(dai->dev, "Unable to configure port\n");
  496. return retval;
  497. }
  498. return retval;
  499. }
  500. static int rt1308_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
  501. struct snd_soc_dai *dai)
  502. {
  503. struct snd_soc_component *component = dai->component;
  504. struct rt1308_sdw_priv *rt1308 =
  505. snd_soc_component_get_drvdata(component);
  506. struct sdw_stream_data *stream =
  507. snd_soc_dai_get_dma_data(dai, substream);
  508. if (!rt1308->sdw_slave)
  509. return -EINVAL;
  510. sdw_stream_remove_slave(rt1308->sdw_slave, stream->sdw_stream);
  511. return 0;
  512. }
  513. /*
  514. * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
  515. * port_prep are not defined for now
  516. */
  517. static const struct sdw_slave_ops rt1308_slave_ops = {
  518. .read_prop = rt1308_read_prop,
  519. .interrupt_callback = rt1308_interrupt_callback,
  520. .update_status = rt1308_update_status,
  521. .bus_config = rt1308_bus_config,
  522. };
  523. static int rt1308_sdw_component_probe(struct snd_soc_component *component)
  524. {
  525. int ret;
  526. ret = pm_runtime_resume(component->dev);
  527. if (ret < 0 && ret != -EACCES)
  528. return ret;
  529. return 0;
  530. }
  531. static const struct snd_soc_component_driver soc_component_sdw_rt1308 = {
  532. .probe = rt1308_sdw_component_probe,
  533. .controls = rt1308_snd_controls,
  534. .num_controls = ARRAY_SIZE(rt1308_snd_controls),
  535. .dapm_widgets = rt1308_dapm_widgets,
  536. .num_dapm_widgets = ARRAY_SIZE(rt1308_dapm_widgets),
  537. .dapm_routes = rt1308_dapm_routes,
  538. .num_dapm_routes = ARRAY_SIZE(rt1308_dapm_routes),
  539. .endianness = 1,
  540. };
  541. static const struct snd_soc_dai_ops rt1308_aif_dai_ops = {
  542. .hw_params = rt1308_sdw_hw_params,
  543. .hw_free = rt1308_sdw_pcm_hw_free,
  544. .set_stream = rt1308_set_sdw_stream,
  545. .shutdown = rt1308_sdw_shutdown,
  546. .set_tdm_slot = rt1308_sdw_set_tdm_slot,
  547. };
  548. #define RT1308_STEREO_RATES SNDRV_PCM_RATE_48000
  549. #define RT1308_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  550. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
  551. SNDRV_PCM_FMTBIT_S24_LE)
  552. static struct snd_soc_dai_driver rt1308_sdw_dai[] = {
  553. {
  554. .name = "rt1308-aif",
  555. .playback = {
  556. .stream_name = "DP1 Playback",
  557. .channels_min = 1,
  558. .channels_max = 2,
  559. .rates = RT1308_STEREO_RATES,
  560. .formats = RT1308_FORMATS,
  561. },
  562. .ops = &rt1308_aif_dai_ops,
  563. },
  564. };
  565. static int rt1308_sdw_init(struct device *dev, struct regmap *regmap,
  566. struct sdw_slave *slave)
  567. {
  568. struct rt1308_sdw_priv *rt1308;
  569. int ret;
  570. rt1308 = devm_kzalloc(dev, sizeof(*rt1308), GFP_KERNEL);
  571. if (!rt1308)
  572. return -ENOMEM;
  573. dev_set_drvdata(dev, rt1308);
  574. rt1308->sdw_slave = slave;
  575. rt1308->regmap = regmap;
  576. /*
  577. * Mark hw_init to false
  578. * HW init will be performed when device reports present
  579. */
  580. rt1308->hw_init = false;
  581. rt1308->first_hw_init = false;
  582. ret = devm_snd_soc_register_component(dev,
  583. &soc_component_sdw_rt1308,
  584. rt1308_sdw_dai,
  585. ARRAY_SIZE(rt1308_sdw_dai));
  586. dev_dbg(&slave->dev, "%s\n", __func__);
  587. return ret;
  588. }
  589. static int rt1308_sdw_probe(struct sdw_slave *slave,
  590. const struct sdw_device_id *id)
  591. {
  592. struct regmap *regmap;
  593. /* Regmap Initialization */
  594. regmap = devm_regmap_init_sdw(slave, &rt1308_sdw_regmap);
  595. if (IS_ERR(regmap))
  596. return PTR_ERR(regmap);
  597. rt1308_sdw_init(&slave->dev, regmap, slave);
  598. return 0;
  599. }
  600. static int rt1308_sdw_remove(struct sdw_slave *slave)
  601. {
  602. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
  603. if (rt1308->first_hw_init)
  604. pm_runtime_disable(&slave->dev);
  605. return 0;
  606. }
  607. static const struct sdw_device_id rt1308_id[] = {
  608. SDW_SLAVE_ENTRY_EXT(0x025d, 0x1308, 0x2, 0, 0),
  609. {},
  610. };
  611. MODULE_DEVICE_TABLE(sdw, rt1308_id);
  612. static int __maybe_unused rt1308_dev_suspend(struct device *dev)
  613. {
  614. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
  615. if (!rt1308->hw_init)
  616. return 0;
  617. regcache_cache_only(rt1308->regmap, true);
  618. return 0;
  619. }
  620. #define RT1308_PROBE_TIMEOUT 5000
  621. static int __maybe_unused rt1308_dev_resume(struct device *dev)
  622. {
  623. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  624. struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
  625. unsigned long time;
  626. if (!rt1308->first_hw_init)
  627. return 0;
  628. if (!slave->unattach_request)
  629. goto regmap_sync;
  630. time = wait_for_completion_timeout(&slave->initialization_complete,
  631. msecs_to_jiffies(RT1308_PROBE_TIMEOUT));
  632. if (!time) {
  633. dev_err(&slave->dev, "Initialization not complete, timed out\n");
  634. sdw_show_ping_status(slave->bus, true);
  635. return -ETIMEDOUT;
  636. }
  637. regmap_sync:
  638. slave->unattach_request = 0;
  639. regcache_cache_only(rt1308->regmap, false);
  640. regcache_sync_region(rt1308->regmap, 0xc000, 0xcfff);
  641. return 0;
  642. }
  643. static const struct dev_pm_ops rt1308_pm = {
  644. SET_SYSTEM_SLEEP_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume)
  645. SET_RUNTIME_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume, NULL)
  646. };
  647. static struct sdw_driver rt1308_sdw_driver = {
  648. .driver = {
  649. .name = "rt1308",
  650. .owner = THIS_MODULE,
  651. .pm = &rt1308_pm,
  652. },
  653. .probe = rt1308_sdw_probe,
  654. .remove = rt1308_sdw_remove,
  655. .ops = &rt1308_slave_ops,
  656. .id_table = rt1308_id,
  657. };
  658. module_sdw_driver(rt1308_sdw_driver);
  659. MODULE_DESCRIPTION("ASoC RT1308 driver SDW");
  660. MODULE_AUTHOR("Shuming Fan <[email protected]>");
  661. MODULE_LICENSE("GPL v2");