rt1318-sdw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // rt1318-sdw.c -- rt1318 SDCA ALSA SoC amplifier audio driver
  4. //
  5. // Copyright(c) 2022 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/module.h>
  13. #include <linux/regmap.h>
  14. #include <linux/dmi.h>
  15. #include <linux/firmware.h>
  16. #include <sound/core.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc-dapm.h>
  20. #include <sound/initval.h>
  21. #include "rt1318-sdw.h"
  22. static const struct reg_sequence rt1318_blind_write[] = {
  23. { 0xc001, 0x43 },
  24. { 0xc003, 0xa2 },
  25. { 0xc004, 0x44 },
  26. { 0xc005, 0x44 },
  27. { 0xc006, 0x33 },
  28. { 0xc007, 0x64 },
  29. { 0xc320, 0x20 },
  30. { 0xf203, 0x18 },
  31. { 0xf211, 0x00 },
  32. { 0xf212, 0x26 },
  33. { 0xf20d, 0x17 },
  34. { 0xf214, 0x06 },
  35. { 0xf20e, 0x00 },
  36. { 0xf223, 0x7f },
  37. { 0xf224, 0xdb },
  38. { 0xf225, 0xee },
  39. { 0xf226, 0x3f },
  40. { 0xf227, 0x0f },
  41. { 0xf21a, 0x78 },
  42. { 0xf242, 0x3c },
  43. { 0xc321, 0x0b },
  44. { 0xc200, 0xd8 },
  45. { 0xc201, 0x27 },
  46. { 0xc202, 0x0f },
  47. { 0xf800, 0x20 },
  48. { 0xdf00, 0x10 },
  49. { 0xdf5f, 0x01 },
  50. { 0xdf60, 0xa7 },
  51. { 0xc400, 0x0e },
  52. { 0xc401, 0x43 },
  53. { 0xc402, 0xe0 },
  54. { 0xc403, 0x00 },
  55. { 0xc404, 0x4c },
  56. { 0xc407, 0x02 },
  57. { 0xc408, 0x3f },
  58. { 0xc300, 0x01 },
  59. { 0xc206, 0x78 },
  60. { 0xc203, 0x84 },
  61. { 0xc120, 0xc0 },
  62. { 0xc121, 0x03 },
  63. { 0xe000, 0x88 },
  64. { 0xc321, 0x09 },
  65. { 0xc322, 0x01 },
  66. { 0xe706, 0x0f },
  67. { 0xe707, 0x30 },
  68. { 0xe806, 0x0f },
  69. { 0xe807, 0x30 },
  70. { 0xed00, 0xb0 },
  71. { 0xce04, 0x02 },
  72. { 0xce05, 0x63 },
  73. { 0xce06, 0x68 },
  74. { 0xce07, 0x07 },
  75. { 0xcf04, 0x02 },
  76. { 0xcf05, 0x63 },
  77. { 0xcf06, 0x68 },
  78. { 0xcf07, 0x07 },
  79. { 0xce60, 0xe3 },
  80. { 0xc130, 0x51 },
  81. { 0xf102, 0x00 },
  82. { 0xf103, 0x00 },
  83. { 0xf104, 0xf5 },
  84. { 0xf105, 0x06 },
  85. { 0xf109, 0x9b },
  86. { 0xf10a, 0x0b },
  87. { 0xf10b, 0x4c },
  88. { 0xf10b, 0x5c },
  89. { 0xf102, 0x00 },
  90. { 0xf103, 0x00 },
  91. { 0xf104, 0xf5 },
  92. { 0xf105, 0x0b },
  93. { 0xf109, 0x03 },
  94. { 0xf10a, 0x0b },
  95. { 0xf10b, 0x4c },
  96. { 0xf10b, 0x5c },
  97. { 0xf102, 0x00 },
  98. { 0xf103, 0x00 },
  99. { 0xf104, 0xf5 },
  100. { 0xf105, 0x0c },
  101. { 0xf109, 0x7f },
  102. { 0xf10a, 0x0b },
  103. { 0xf10b, 0x4c },
  104. { 0xf10b, 0x5c },
  105. { 0xe604, 0x00 },
  106. { 0xdb00, 0x0c },
  107. { 0xdd00, 0x0c },
  108. { 0xdc19, 0x00 },
  109. { 0xdc1a, 0xff },
  110. { 0xdc1b, 0xff },
  111. { 0xdc1c, 0xff },
  112. { 0xdc1d, 0x00 },
  113. { 0xdc1e, 0x00 },
  114. { 0xdc1f, 0x00 },
  115. { 0xdc20, 0xff },
  116. { 0xde19, 0x00 },
  117. { 0xde1a, 0xff },
  118. { 0xde1b, 0xff },
  119. { 0xde1c, 0xff },
  120. { 0xde1d, 0x00 },
  121. { 0xde1e, 0x00 },
  122. { 0xde1f, 0x00 },
  123. { 0xde20, 0xff },
  124. { 0xdb32, 0x00 },
  125. { 0xdd32, 0x00 },
  126. { 0xdb33, 0x0a },
  127. { 0xdd33, 0x0a },
  128. { 0xdb34, 0x1a },
  129. { 0xdd34, 0x1a },
  130. { 0xdb17, 0xef },
  131. { 0xdd17, 0xef },
  132. { 0xdba7, 0x00 },
  133. { 0xdba8, 0x64 },
  134. { 0xdda7, 0x00 },
  135. { 0xdda8, 0x64 },
  136. { 0xdb19, 0x40 },
  137. { 0xdd19, 0x40 },
  138. { 0xdb00, 0x4c },
  139. { 0xdb01, 0x79 },
  140. { 0xdd01, 0x79 },
  141. { 0xdb04, 0x05 },
  142. { 0xdb05, 0x03 },
  143. { 0xdd04, 0x05 },
  144. { 0xdd05, 0x03 },
  145. { 0xdbbb, 0x09 },
  146. { 0xdbbc, 0x30 },
  147. { 0xdbbd, 0xf0 },
  148. { 0xdbbe, 0xf1 },
  149. { 0xddbb, 0x09 },
  150. { 0xddbc, 0x30 },
  151. { 0xddbd, 0xf0 },
  152. { 0xddbe, 0xf1 },
  153. { 0xdb01, 0x79 },
  154. { 0xdd01, 0x79 },
  155. { 0xdc52, 0xef },
  156. { 0xde52, 0xef },
  157. { 0x2f55, 0x22 },
  158. };
  159. static const struct reg_default rt1318_reg_defaults[] = {
  160. { 0x3000, 0x00 },
  161. { 0x3004, 0x01 },
  162. { 0x3005, 0x23 },
  163. { 0x3202, 0x00 },
  164. { 0x3203, 0x01 },
  165. { 0x3206, 0x00 },
  166. { 0xc000, 0x00 },
  167. { 0xc001, 0x43 },
  168. { 0xc003, 0x22 },
  169. { 0xc004, 0x44 },
  170. { 0xc005, 0x44 },
  171. { 0xc006, 0x33 },
  172. { 0xc007, 0x64 },
  173. { 0xc008, 0x05 },
  174. { 0xc00a, 0xfc },
  175. { 0xc00b, 0x0f },
  176. { 0xc00c, 0x0e },
  177. { 0xc00d, 0xef },
  178. { 0xc00e, 0xe5 },
  179. { 0xc00f, 0xff },
  180. { 0xc120, 0xc0 },
  181. { 0xc121, 0x00 },
  182. { 0xc122, 0x00 },
  183. { 0xc123, 0x14 },
  184. { 0xc125, 0x00 },
  185. { 0xc200, 0x00 },
  186. { 0xc201, 0x00 },
  187. { 0xc202, 0x00 },
  188. { 0xc203, 0x04 },
  189. { 0xc204, 0x00 },
  190. { 0xc205, 0x00 },
  191. { 0xc206, 0x68 },
  192. { 0xc207, 0x70 },
  193. { 0xc208, 0x00 },
  194. { 0xc20a, 0x00 },
  195. { 0xc20b, 0x01 },
  196. { 0xc20c, 0x7f },
  197. { 0xc20d, 0x01 },
  198. { 0xc20e, 0x7f },
  199. { 0xc300, 0x00 },
  200. { 0xc301, 0x00 },
  201. { 0xc303, 0x80 },
  202. { 0xc320, 0x00 },
  203. { 0xc321, 0x09 },
  204. { 0xc322, 0x02 },
  205. { 0xc410, 0x04 },
  206. { 0xc430, 0x00 },
  207. { 0xc431, 0x00 },
  208. { 0xca00, 0x10 },
  209. { 0xca01, 0x00 },
  210. { 0xca02, 0x0b },
  211. { 0xca10, 0x10 },
  212. { 0xca11, 0x00 },
  213. { 0xca12, 0x0b },
  214. { 0xdd93, 0x00 },
  215. { 0xdd94, 0x64 },
  216. { 0xe300, 0xa0 },
  217. { 0xed00, 0x80 },
  218. { 0xed01, 0x0f },
  219. { 0xed02, 0xff },
  220. { 0xed03, 0x00 },
  221. { 0xed04, 0x00 },
  222. { 0xed05, 0x0f },
  223. { 0xed06, 0xff },
  224. { 0xf010, 0x10 },
  225. { 0xf011, 0xec },
  226. { 0xf012, 0x68 },
  227. { 0xf013, 0x21 },
  228. { 0xf800, 0x00 },
  229. { 0xf801, 0x12 },
  230. { 0xf802, 0xe0 },
  231. { 0xf803, 0x2f },
  232. { 0xf804, 0x00 },
  233. { 0xf805, 0x00 },
  234. { 0xf806, 0x07 },
  235. { 0xf807, 0xff },
  236. { SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_UDMPU21, RT1318_SDCA_CTL_UDMPU_CLUSTER, 0), 0x00 },
  237. { SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_L), 0x01 },
  238. { SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_R), 0x01 },
  239. { SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23, RT1318_SDCA_CTL_REQ_POWER_STATE, 0), 0x03 },
  240. { SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_CS21, RT1318_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 0x09 },
  241. };
  242. static bool rt1318_readable_register(struct device *dev, unsigned int reg)
  243. {
  244. switch (reg) {
  245. case 0x2f55:
  246. case 0x3000:
  247. case 0x3004 ... 0x3005:
  248. case 0x3202 ... 0x3203:
  249. case 0x3206:
  250. case 0xc000 ... 0xc00f:
  251. case 0xc120 ... 0xc125:
  252. case 0xc200 ... 0xc20e:
  253. case 0xc300 ... 0xc303:
  254. case 0xc320 ... 0xc322:
  255. case 0xc410:
  256. case 0xc430 ... 0xc431:
  257. case 0xca00 ... 0xca02:
  258. case 0xca10 ... 0xca12:
  259. case 0xcb00 ... 0xcb0b:
  260. case 0xcc00 ... 0xcce5:
  261. case 0xcd00 ... 0xcde5:
  262. case 0xce00 ... 0xce6a:
  263. case 0xcf00 ... 0xcf53:
  264. case 0xd000 ... 0xd0cc:
  265. case 0xd100 ... 0xd1b9:
  266. case 0xdb00 ... 0xdc53:
  267. case 0xdd00 ... 0xde53:
  268. case 0xdf00 ... 0xdf6b:
  269. case 0xe300:
  270. case 0xeb00 ... 0xebcc:
  271. case 0xec00 ... 0xecb9:
  272. case 0xed00 ... 0xed06:
  273. case 0xf010 ... 0xf014:
  274. case 0xf800 ... 0xf807:
  275. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_UDMPU21, RT1318_SDCA_CTL_UDMPU_CLUSTER, 0):
  276. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_L):
  277. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_R):
  278. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23, RT1318_SDCA_CTL_REQ_POWER_STATE, 0):
  279. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_CS21, RT1318_SDCA_CTL_SAMPLE_FREQ_INDEX, 0):
  280. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_MODE, 0):
  281. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_STATUS, 0):
  282. return true;
  283. default:
  284. return false;
  285. }
  286. }
  287. static bool rt1318_volatile_register(struct device *dev, unsigned int reg)
  288. {
  289. switch (reg) {
  290. case 0x2f55:
  291. case 0x3000 ... 0x3001:
  292. case 0xc000:
  293. case 0xc301:
  294. case 0xc410:
  295. case 0xc430 ... 0xc431:
  296. case 0xdb06:
  297. case 0xdb12:
  298. case 0xdb1d ... 0xdb1f:
  299. case 0xdb35:
  300. case 0xdb37:
  301. case 0xdb8a ... 0xdb92:
  302. case 0xdbc5 ... 0xdbc8:
  303. case 0xdc2b ... 0xdc49:
  304. case 0xdd0b:
  305. case 0xdd12:
  306. case 0xdd1d ... 0xdd1f:
  307. case 0xdd35:
  308. case 0xdd8a ... 0xdd92:
  309. case 0xddc5 ... 0xddc8:
  310. case 0xde2b ... 0xde44:
  311. case 0xdf4a ... 0xdf55:
  312. case 0xe224 ... 0xe23b:
  313. case 0xea01:
  314. case 0xebc5:
  315. case 0xebc8:
  316. case 0xebcb ... 0xebcc:
  317. case 0xed03 ... 0xed06:
  318. case 0xf010 ... 0xf014:
  319. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_MODE, 0):
  320. case SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_SAPU, RT1318_SDCA_CTL_SAPU_PROTECTION_STATUS, 0):
  321. return true;
  322. default:
  323. return false;
  324. }
  325. }
  326. static const struct regmap_config rt1318_sdw_regmap = {
  327. .reg_bits = 32,
  328. .val_bits = 8,
  329. .readable_reg = rt1318_readable_register,
  330. .volatile_reg = rt1318_volatile_register,
  331. .max_register = 0x41081488,
  332. .reg_defaults = rt1318_reg_defaults,
  333. .num_reg_defaults = ARRAY_SIZE(rt1318_reg_defaults),
  334. .cache_type = REGCACHE_RBTREE,
  335. .use_single_read = true,
  336. .use_single_write = true,
  337. };
  338. static int rt1318_read_prop(struct sdw_slave *slave)
  339. {
  340. struct sdw_slave_prop *prop = &slave->prop;
  341. int nval;
  342. int i, j;
  343. u32 bit;
  344. unsigned long addr;
  345. struct sdw_dpn_prop *dpn;
  346. prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
  347. prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
  348. prop->is_sdca = true;
  349. prop->paging_support = true;
  350. /* first we need to allocate memory for set bits in port lists */
  351. prop->source_ports = BIT(2);
  352. prop->sink_ports = BIT(1);
  353. nval = hweight32(prop->source_ports);
  354. prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
  355. sizeof(*prop->src_dpn_prop), GFP_KERNEL);
  356. if (!prop->src_dpn_prop)
  357. return -ENOMEM;
  358. i = 0;
  359. dpn = prop->src_dpn_prop;
  360. addr = prop->source_ports;
  361. for_each_set_bit(bit, &addr, 32) {
  362. dpn[i].num = bit;
  363. dpn[i].type = SDW_DPN_FULL;
  364. dpn[i].simple_ch_prep_sm = true;
  365. dpn[i].ch_prep_timeout = 10;
  366. i++;
  367. }
  368. /* do this again for sink now */
  369. nval = hweight32(prop->sink_ports);
  370. prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
  371. sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
  372. if (!prop->sink_dpn_prop)
  373. return -ENOMEM;
  374. j = 0;
  375. dpn = prop->sink_dpn_prop;
  376. addr = prop->sink_ports;
  377. for_each_set_bit(bit, &addr, 32) {
  378. dpn[j].num = bit;
  379. dpn[j].type = SDW_DPN_FULL;
  380. dpn[j].simple_ch_prep_sm = true;
  381. dpn[j].ch_prep_timeout = 10;
  382. j++;
  383. }
  384. /* set the timeout values */
  385. prop->clk_stop_timeout = 20;
  386. return 0;
  387. }
  388. static int rt1318_io_init(struct device *dev, struct sdw_slave *slave)
  389. {
  390. struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(dev);
  391. if (rt1318->hw_init)
  392. return 0;
  393. if (rt1318->first_hw_init) {
  394. regcache_cache_only(rt1318->regmap, false);
  395. regcache_cache_bypass(rt1318->regmap, true);
  396. } else {
  397. /*
  398. * PM runtime is only enabled when a Slave reports as Attached
  399. */
  400. /* set autosuspend parameters */
  401. pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
  402. pm_runtime_use_autosuspend(&slave->dev);
  403. /* update count of parent 'active' children */
  404. pm_runtime_set_active(&slave->dev);
  405. /* make sure the device does not suspend immediately */
  406. pm_runtime_mark_last_busy(&slave->dev);
  407. pm_runtime_enable(&slave->dev);
  408. }
  409. pm_runtime_get_noresume(&slave->dev);
  410. /* blind write */
  411. regmap_multi_reg_write(rt1318->regmap, rt1318_blind_write,
  412. ARRAY_SIZE(rt1318_blind_write));
  413. if (rt1318->first_hw_init) {
  414. regcache_cache_bypass(rt1318->regmap, false);
  415. regcache_mark_dirty(rt1318->regmap);
  416. }
  417. /* Mark Slave initialization complete */
  418. rt1318->first_hw_init = true;
  419. rt1318->hw_init = true;
  420. pm_runtime_mark_last_busy(&slave->dev);
  421. pm_runtime_put_autosuspend(&slave->dev);
  422. dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
  423. return 0;
  424. }
  425. static int rt1318_update_status(struct sdw_slave *slave,
  426. enum sdw_slave_status status)
  427. {
  428. struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(&slave->dev);
  429. /* Update the status */
  430. rt1318->status = status;
  431. if (status == SDW_SLAVE_UNATTACHED)
  432. rt1318->hw_init = false;
  433. /*
  434. * Perform initialization only if slave status is present and
  435. * hw_init flag is false
  436. */
  437. if (rt1318->hw_init || rt1318->status != SDW_SLAVE_ATTACHED)
  438. return 0;
  439. /* perform I/O transfers required for Slave initialization */
  440. return rt1318_io_init(&slave->dev, slave);
  441. }
  442. static int rt1318_classd_event(struct snd_soc_dapm_widget *w,
  443. struct snd_kcontrol *kcontrol, int event)
  444. {
  445. struct snd_soc_component *component =
  446. snd_soc_dapm_to_component(w->dapm);
  447. struct rt1318_sdw_priv *rt1318 = snd_soc_component_get_drvdata(component);
  448. unsigned char ps0 = 0x0, ps3 = 0x3;
  449. switch (event) {
  450. case SND_SOC_DAPM_POST_PMU:
  451. regmap_write(rt1318->regmap,
  452. SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23,
  453. RT1318_SDCA_CTL_REQ_POWER_STATE, 0),
  454. ps0);
  455. break;
  456. case SND_SOC_DAPM_PRE_PMD:
  457. regmap_write(rt1318->regmap,
  458. SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_PDE23,
  459. RT1318_SDCA_CTL_REQ_POWER_STATE, 0),
  460. ps3);
  461. break;
  462. default:
  463. break;
  464. }
  465. return 0;
  466. }
  467. static const char * const rt1318_rx_data_ch_select[] = {
  468. "L,R",
  469. "L,L",
  470. "L,R",
  471. "L,L+R",
  472. "R,L",
  473. "R,R",
  474. "R,L+R",
  475. "L+R,L",
  476. "L+R,R",
  477. "L+R,L+R",
  478. };
  479. static SOC_ENUM_SINGLE_DECL(rt1318_rx_data_ch_enum,
  480. SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_UDMPU21, RT1318_SDCA_CTL_UDMPU_CLUSTER, 0), 0,
  481. rt1318_rx_data_ch_select);
  482. static const struct snd_kcontrol_new rt1318_snd_controls[] = {
  483. /* UDMPU Cluster Selection */
  484. SOC_ENUM("RX Channel Select", rt1318_rx_data_ch_enum),
  485. };
  486. static const struct snd_kcontrol_new rt1318_sto_dac =
  487. SOC_DAPM_DOUBLE_R("Switch",
  488. SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_L),
  489. SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_FU21, RT1318_SDCA_CTL_FU_MUTE, CH_R),
  490. 0, 1, 1);
  491. static const struct snd_soc_dapm_widget rt1318_dapm_widgets[] = {
  492. /* Audio Interface */
  493. SND_SOC_DAPM_AIF_IN("DP1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
  494. SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
  495. /* Digital Interface */
  496. SND_SOC_DAPM_SWITCH("DAC", SND_SOC_NOPM, 0, 0, &rt1318_sto_dac),
  497. /* Output */
  498. SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
  499. rt1318_classd_event, SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
  500. SND_SOC_DAPM_OUTPUT("SPOL"),
  501. SND_SOC_DAPM_OUTPUT("SPOR"),
  502. /* Input */
  503. SND_SOC_DAPM_PGA("FB Data", SND_SOC_NOPM, 0, 0, NULL, 0),
  504. SND_SOC_DAPM_SIGGEN("FB Gen"),
  505. };
  506. static const struct snd_soc_dapm_route rt1318_dapm_routes[] = {
  507. { "DAC", "Switch", "DP1RX" },
  508. { "CLASS D", NULL, "DAC" },
  509. { "SPOL", NULL, "CLASS D" },
  510. { "SPOR", NULL, "CLASS D" },
  511. { "FB Data", NULL, "FB Gen" },
  512. { "DP2TX", NULL, "FB Data" },
  513. };
  514. static int rt1318_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
  515. int direction)
  516. {
  517. struct sdw_stream_data *stream;
  518. if (!sdw_stream)
  519. return 0;
  520. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  521. if (!stream)
  522. return -ENOMEM;
  523. stream->sdw_stream = sdw_stream;
  524. /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
  525. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  526. dai->playback_dma_data = stream;
  527. else
  528. dai->capture_dma_data = stream;
  529. return 0;
  530. }
  531. static void rt1318_sdw_shutdown(struct snd_pcm_substream *substream,
  532. struct snd_soc_dai *dai)
  533. {
  534. struct sdw_stream_data *stream;
  535. stream = snd_soc_dai_get_dma_data(dai, substream);
  536. snd_soc_dai_set_dma_data(dai, substream, NULL);
  537. kfree(stream);
  538. }
  539. static int rt1318_sdw_hw_params(struct snd_pcm_substream *substream,
  540. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  541. {
  542. struct snd_soc_component *component = dai->component;
  543. struct rt1318_sdw_priv *rt1318 =
  544. snd_soc_component_get_drvdata(component);
  545. struct sdw_stream_config stream_config;
  546. struct sdw_port_config port_config;
  547. enum sdw_data_direction direction;
  548. struct sdw_stream_data *stream;
  549. int retval, port, num_channels, ch_mask;
  550. unsigned int sampling_rate;
  551. dev_dbg(dai->dev, "%s %s", __func__, dai->name);
  552. stream = snd_soc_dai_get_dma_data(dai, substream);
  553. if (!stream)
  554. return -EINVAL;
  555. if (!rt1318->sdw_slave)
  556. return -EINVAL;
  557. /* SoundWire specific configuration */
  558. /* port 1 for playback */
  559. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  560. direction = SDW_DATA_DIR_RX;
  561. port = 1;
  562. } else {
  563. direction = SDW_DATA_DIR_TX;
  564. port = 2;
  565. }
  566. num_channels = params_channels(params);
  567. ch_mask = (1 << num_channels) - 1;
  568. stream_config.frame_rate = params_rate(params);
  569. stream_config.ch_count = num_channels;
  570. stream_config.bps = snd_pcm_format_width(params_format(params));
  571. stream_config.direction = direction;
  572. port_config.ch_mask = ch_mask;
  573. port_config.num = port;
  574. retval = sdw_stream_add_slave(rt1318->sdw_slave, &stream_config,
  575. &port_config, 1, stream->sdw_stream);
  576. if (retval) {
  577. dev_err(dai->dev, "Unable to configure port\n");
  578. return retval;
  579. }
  580. /* sampling rate configuration */
  581. switch (params_rate(params)) {
  582. case 16000:
  583. sampling_rate = RT1318_SDCA_RATE_16000HZ;
  584. break;
  585. case 32000:
  586. sampling_rate = RT1318_SDCA_RATE_32000HZ;
  587. break;
  588. case 44100:
  589. sampling_rate = RT1318_SDCA_RATE_44100HZ;
  590. break;
  591. case 48000:
  592. sampling_rate = RT1318_SDCA_RATE_48000HZ;
  593. break;
  594. case 96000:
  595. sampling_rate = RT1318_SDCA_RATE_96000HZ;
  596. break;
  597. case 192000:
  598. sampling_rate = RT1318_SDCA_RATE_192000HZ;
  599. break;
  600. default:
  601. dev_err(component->dev, "Rate %d is not supported\n",
  602. params_rate(params));
  603. return -EINVAL;
  604. }
  605. /* set sampling frequency */
  606. regmap_write(rt1318->regmap,
  607. SDW_SDCA_CTL(FUNC_NUM_SMART_AMP, RT1318_SDCA_ENT_CS21, RT1318_SDCA_CTL_SAMPLE_FREQ_INDEX, 0),
  608. sampling_rate);
  609. return 0;
  610. }
  611. static int rt1318_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
  612. struct snd_soc_dai *dai)
  613. {
  614. struct snd_soc_component *component = dai->component;
  615. struct rt1318_sdw_priv *rt1318 =
  616. snd_soc_component_get_drvdata(component);
  617. struct sdw_stream_data *stream =
  618. snd_soc_dai_get_dma_data(dai, substream);
  619. if (!rt1318->sdw_slave)
  620. return -EINVAL;
  621. sdw_stream_remove_slave(rt1318->sdw_slave, stream->sdw_stream);
  622. return 0;
  623. }
  624. /*
  625. * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
  626. * port_prep are not defined for now
  627. */
  628. static const struct sdw_slave_ops rt1318_slave_ops = {
  629. .read_prop = rt1318_read_prop,
  630. .update_status = rt1318_update_status,
  631. };
  632. static int rt1318_sdw_component_probe(struct snd_soc_component *component)
  633. {
  634. int ret;
  635. struct rt1318_sdw_priv *rt1318 = snd_soc_component_get_drvdata(component);
  636. rt1318->component = component;
  637. ret = pm_runtime_resume(component->dev);
  638. dev_dbg(&rt1318->sdw_slave->dev, "%s pm_runtime_resume, ret=%d", __func__, ret);
  639. if (ret < 0 && ret != -EACCES)
  640. return ret;
  641. return 0;
  642. }
  643. static const struct snd_soc_component_driver soc_component_sdw_rt1318 = {
  644. .probe = rt1318_sdw_component_probe,
  645. .controls = rt1318_snd_controls,
  646. .num_controls = ARRAY_SIZE(rt1318_snd_controls),
  647. .dapm_widgets = rt1318_dapm_widgets,
  648. .num_dapm_widgets = ARRAY_SIZE(rt1318_dapm_widgets),
  649. .dapm_routes = rt1318_dapm_routes,
  650. .num_dapm_routes = ARRAY_SIZE(rt1318_dapm_routes),
  651. .endianness = 1,
  652. };
  653. static const struct snd_soc_dai_ops rt1318_aif_dai_ops = {
  654. .hw_params = rt1318_sdw_hw_params,
  655. .hw_free = rt1318_sdw_pcm_hw_free,
  656. .set_stream = rt1318_set_sdw_stream,
  657. .shutdown = rt1318_sdw_shutdown,
  658. };
  659. #define RT1318_STEREO_RATES (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  660. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
  661. #define RT1318_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
  662. SNDRV_PCM_FMTBIT_S32_LE)
  663. static struct snd_soc_dai_driver rt1318_sdw_dai[] = {
  664. {
  665. .name = "rt1318-aif",
  666. .playback = {
  667. .stream_name = "DP1 Playback",
  668. .channels_min = 1,
  669. .channels_max = 2,
  670. .rates = RT1318_STEREO_RATES,
  671. .formats = RT1318_FORMATS,
  672. },
  673. .capture = {
  674. .stream_name = "DP2 Capture",
  675. .channels_min = 1,
  676. .channels_max = 2,
  677. .rates = RT1318_STEREO_RATES,
  678. .formats = RT1318_FORMATS,
  679. },
  680. .ops = &rt1318_aif_dai_ops,
  681. },
  682. };
  683. static int rt1318_sdw_init(struct device *dev, struct regmap *regmap,
  684. struct sdw_slave *slave)
  685. {
  686. struct rt1318_sdw_priv *rt1318;
  687. int ret;
  688. rt1318 = devm_kzalloc(dev, sizeof(*rt1318), GFP_KERNEL);
  689. if (!rt1318)
  690. return -ENOMEM;
  691. dev_set_drvdata(dev, rt1318);
  692. rt1318->sdw_slave = slave;
  693. rt1318->regmap = regmap;
  694. /*
  695. * Mark hw_init to false
  696. * HW init will be performed when device reports present
  697. */
  698. rt1318->hw_init = false;
  699. rt1318->first_hw_init = false;
  700. ret = devm_snd_soc_register_component(dev,
  701. &soc_component_sdw_rt1318,
  702. rt1318_sdw_dai,
  703. ARRAY_SIZE(rt1318_sdw_dai));
  704. dev_dbg(&slave->dev, "%s\n", __func__);
  705. return ret;
  706. }
  707. static int rt1318_sdw_probe(struct sdw_slave *slave,
  708. const struct sdw_device_id *id)
  709. {
  710. struct regmap *regmap;
  711. /* Regmap Initialization */
  712. regmap = devm_regmap_init_sdw(slave, &rt1318_sdw_regmap);
  713. if (IS_ERR(regmap))
  714. return PTR_ERR(regmap);
  715. return rt1318_sdw_init(&slave->dev, regmap, slave);
  716. }
  717. static int rt1318_sdw_remove(struct sdw_slave *slave)
  718. {
  719. struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(&slave->dev);
  720. if (rt1318->first_hw_init)
  721. pm_runtime_disable(&slave->dev);
  722. return 0;
  723. }
  724. static const struct sdw_device_id rt1318_id[] = {
  725. SDW_SLAVE_ENTRY_EXT(0x025d, 0x1318, 0x3, 0x1, 0),
  726. {},
  727. };
  728. MODULE_DEVICE_TABLE(sdw, rt1318_id);
  729. static int __maybe_unused rt1318_dev_suspend(struct device *dev)
  730. {
  731. struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(dev);
  732. if (!rt1318->hw_init)
  733. return 0;
  734. regcache_cache_only(rt1318->regmap, true);
  735. return 0;
  736. }
  737. #define RT1318_PROBE_TIMEOUT 5000
  738. static int __maybe_unused rt1318_dev_resume(struct device *dev)
  739. {
  740. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  741. struct rt1318_sdw_priv *rt1318 = dev_get_drvdata(dev);
  742. unsigned long time;
  743. if (!rt1318->first_hw_init)
  744. return 0;
  745. if (!slave->unattach_request)
  746. goto regmap_sync;
  747. time = wait_for_completion_timeout(&slave->initialization_complete,
  748. msecs_to_jiffies(RT1318_PROBE_TIMEOUT));
  749. if (!time) {
  750. dev_err(&slave->dev, "Initialization not complete, timed out\n");
  751. return -ETIMEDOUT;
  752. }
  753. regmap_sync:
  754. slave->unattach_request = 0;
  755. regcache_cache_only(rt1318->regmap, false);
  756. regcache_sync(rt1318->regmap);
  757. return 0;
  758. }
  759. static const struct dev_pm_ops rt1318_pm = {
  760. SET_SYSTEM_SLEEP_PM_OPS(rt1318_dev_suspend, rt1318_dev_resume)
  761. SET_RUNTIME_PM_OPS(rt1318_dev_suspend, rt1318_dev_resume, NULL)
  762. };
  763. static struct sdw_driver rt1318_sdw_driver = {
  764. .driver = {
  765. .name = "rt1318-sdca",
  766. .owner = THIS_MODULE,
  767. .pm = &rt1318_pm,
  768. },
  769. .probe = rt1318_sdw_probe,
  770. .remove = rt1318_sdw_remove,
  771. .ops = &rt1318_slave_ops,
  772. .id_table = rt1318_id,
  773. };
  774. module_sdw_driver(rt1318_sdw_driver);
  775. MODULE_DESCRIPTION("ASoC RT1318 driver SDCA SDW");
  776. MODULE_AUTHOR("Shuming Fan <[email protected]>");
  777. MODULE_LICENSE("GPL");