rt1015.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // rt1015.c -- RT1015 ALSA SoC audio amplifier driver
  4. //
  5. // Copyright 2019 Realtek Semiconductor Corp.
  6. //
  7. // Author: Jack Yu <[email protected]>
  8. //
  9. //
  10. #include <linux/acpi.h>
  11. #include <linux/delay.h>
  12. #include <linux/firmware.h>
  13. #include <linux/fs.h>
  14. #include <linux/gpio.h>
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm.h>
  21. #include <linux/regmap.h>
  22. #include <sound/core.h>
  23. #include <sound/initval.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/rt1015.h>
  27. #include <sound/soc-dapm.h>
  28. #include <sound/soc.h>
  29. #include <sound/tlv.h>
  30. #include "rl6231.h"
  31. #include "rt1015.h"
  32. static const struct rt1015_platform_data i2s_default_platform_data = {
  33. .power_up_delay_ms = 50,
  34. };
  35. static const struct reg_default rt1015_reg[] = {
  36. { 0x0000, 0x0000 },
  37. { 0x0004, 0xa000 },
  38. { 0x0006, 0x0003 },
  39. { 0x000a, 0x081e },
  40. { 0x000c, 0x0006 },
  41. { 0x000e, 0x0000 },
  42. { 0x0010, 0x0000 },
  43. { 0x0012, 0x0000 },
  44. { 0x0014, 0x0000 },
  45. { 0x0016, 0x0000 },
  46. { 0x0018, 0x0000 },
  47. { 0x0020, 0x8000 },
  48. { 0x0022, 0x8043 },
  49. { 0x0076, 0x0000 },
  50. { 0x0078, 0x0000 },
  51. { 0x007a, 0x0002 },
  52. { 0x007c, 0x10ec },
  53. { 0x007d, 0x1015 },
  54. { 0x00f0, 0x5000 },
  55. { 0x00f2, 0x004c },
  56. { 0x00f3, 0xecfe },
  57. { 0x00f4, 0x0000 },
  58. { 0x00f6, 0x0400 },
  59. { 0x0100, 0x0028 },
  60. { 0x0102, 0xff02 },
  61. { 0x0104, 0xa213 },
  62. { 0x0106, 0x200c },
  63. { 0x010c, 0x0000 },
  64. { 0x010e, 0x0058 },
  65. { 0x0111, 0x0200 },
  66. { 0x0112, 0x0400 },
  67. { 0x0114, 0x0022 },
  68. { 0x0116, 0x0000 },
  69. { 0x0118, 0x0000 },
  70. { 0x011a, 0x0123 },
  71. { 0x011c, 0x4567 },
  72. { 0x0300, 0x203d },
  73. { 0x0302, 0x001e },
  74. { 0x0311, 0x0000 },
  75. { 0x0313, 0x6014 },
  76. { 0x0314, 0x00a2 },
  77. { 0x031a, 0x00a0 },
  78. { 0x031c, 0x001f },
  79. { 0x031d, 0xffff },
  80. { 0x031e, 0x0000 },
  81. { 0x031f, 0x0000 },
  82. { 0x0320, 0x0000 },
  83. { 0x0321, 0x0000 },
  84. { 0x0322, 0xd7df },
  85. { 0x0328, 0x10b2 },
  86. { 0x0329, 0x0175 },
  87. { 0x032a, 0x36ad },
  88. { 0x032b, 0x7e55 },
  89. { 0x032c, 0x0520 },
  90. { 0x032d, 0xaa00 },
  91. { 0x032e, 0x570e },
  92. { 0x0330, 0xe180 },
  93. { 0x0332, 0x0034 },
  94. { 0x0334, 0x0001 },
  95. { 0x0336, 0x0010 },
  96. { 0x0338, 0x0000 },
  97. { 0x04fa, 0x0030 },
  98. { 0x04fc, 0x35c8 },
  99. { 0x04fe, 0x0800 },
  100. { 0x0500, 0x0400 },
  101. { 0x0502, 0x1000 },
  102. { 0x0504, 0x0000 },
  103. { 0x0506, 0x04ff },
  104. { 0x0508, 0x0010 },
  105. { 0x050a, 0x001a },
  106. { 0x0519, 0x1c68 },
  107. { 0x051a, 0x0ccc },
  108. { 0x051b, 0x0666 },
  109. { 0x051d, 0x0000 },
  110. { 0x051f, 0x0000 },
  111. { 0x0536, 0x061c },
  112. { 0x0538, 0x0000 },
  113. { 0x053a, 0x0000 },
  114. { 0x053c, 0x0000 },
  115. { 0x053d, 0x0000 },
  116. { 0x053e, 0x0000 },
  117. { 0x053f, 0x0000 },
  118. { 0x0540, 0x0000 },
  119. { 0x0541, 0x0000 },
  120. { 0x0542, 0x0000 },
  121. { 0x0543, 0x0000 },
  122. { 0x0544, 0x0000 },
  123. { 0x0568, 0x0000 },
  124. { 0x056a, 0x0000 },
  125. { 0x1000, 0x0040 },
  126. { 0x1002, 0x5405 },
  127. { 0x1006, 0x5515 },
  128. { 0x1007, 0x05f7 },
  129. { 0x1009, 0x0b0a },
  130. { 0x100a, 0x00ef },
  131. { 0x100d, 0x0003 },
  132. { 0x1010, 0xa433 },
  133. { 0x1020, 0x0000 },
  134. { 0x1200, 0x5a01 },
  135. { 0x1202, 0x6524 },
  136. { 0x1204, 0x1f00 },
  137. { 0x1206, 0x0000 },
  138. { 0x1208, 0x0000 },
  139. { 0x120a, 0x0000 },
  140. { 0x120c, 0x0000 },
  141. { 0x120e, 0x0000 },
  142. { 0x1210, 0x0000 },
  143. { 0x1212, 0x0000 },
  144. { 0x1300, 0x10a1 },
  145. { 0x1302, 0x12ff },
  146. { 0x1304, 0x0400 },
  147. { 0x1305, 0x0844 },
  148. { 0x1306, 0x4611 },
  149. { 0x1308, 0x555e },
  150. { 0x130a, 0x0000 },
  151. { 0x130c, 0x2000 },
  152. { 0x130e, 0x0100 },
  153. { 0x130f, 0x0001 },
  154. { 0x1310, 0x0000 },
  155. { 0x1312, 0x0000 },
  156. { 0x1314, 0x0000 },
  157. { 0x1316, 0x0000 },
  158. { 0x1318, 0x0000 },
  159. { 0x131a, 0x0000 },
  160. { 0x1322, 0x0029 },
  161. { 0x1323, 0x4a52 },
  162. { 0x1324, 0x002c },
  163. { 0x1325, 0x0b02 },
  164. { 0x1326, 0x002d },
  165. { 0x1327, 0x6b5a },
  166. { 0x1328, 0x002e },
  167. { 0x1329, 0xcbb2 },
  168. { 0x132a, 0x0030 },
  169. { 0x132b, 0x2c0b },
  170. { 0x1330, 0x0031 },
  171. { 0x1331, 0x8c63 },
  172. { 0x1332, 0x0032 },
  173. { 0x1333, 0xecbb },
  174. { 0x1334, 0x0034 },
  175. { 0x1335, 0x4d13 },
  176. { 0x1336, 0x0037 },
  177. { 0x1337, 0x0dc3 },
  178. { 0x1338, 0x003d },
  179. { 0x1339, 0xef7b },
  180. { 0x133a, 0x0044 },
  181. { 0x133b, 0xd134 },
  182. { 0x133c, 0x0047 },
  183. { 0x133d, 0x91e4 },
  184. { 0x133e, 0x004d },
  185. { 0x133f, 0xc370 },
  186. { 0x1340, 0x0053 },
  187. { 0x1341, 0xf4fd },
  188. { 0x1342, 0x0060 },
  189. { 0x1343, 0x5816 },
  190. { 0x1344, 0x006c },
  191. { 0x1345, 0xbb2e },
  192. { 0x1346, 0x0072 },
  193. { 0x1347, 0xecbb },
  194. { 0x1348, 0x0076 },
  195. { 0x1349, 0x5d97 },
  196. };
  197. static bool rt1015_volatile_register(struct device *dev, unsigned int reg)
  198. {
  199. switch (reg) {
  200. case RT1015_RESET:
  201. case RT1015_CLK_DET:
  202. case RT1015_SIL_DET:
  203. case RT1015_VER_ID:
  204. case RT1015_VENDOR_ID:
  205. case RT1015_DEVICE_ID:
  206. case RT1015_PRO_ALT:
  207. case RT1015_MAN_I2C:
  208. case RT1015_DAC3:
  209. case RT1015_VBAT_TEST_OUT1:
  210. case RT1015_VBAT_TEST_OUT2:
  211. case RT1015_VBAT_PROT_ATT:
  212. case RT1015_VBAT_DET_CODE:
  213. case RT1015_SMART_BST_CTRL1:
  214. case RT1015_SPK_DC_DETECT1:
  215. case RT1015_SPK_DC_DETECT4:
  216. case RT1015_SPK_DC_DETECT5:
  217. case RT1015_DC_CALIB_CLSD1:
  218. case RT1015_DC_CALIB_CLSD5:
  219. case RT1015_DC_CALIB_CLSD6:
  220. case RT1015_DC_CALIB_CLSD7:
  221. case RT1015_DC_CALIB_CLSD8:
  222. case RT1015_S_BST_TIMING_INTER1:
  223. case RT1015_OSCK_STA:
  224. case RT1015_MONO_DYNA_CTRL1:
  225. case RT1015_MONO_DYNA_CTRL5:
  226. return true;
  227. default:
  228. return false;
  229. }
  230. }
  231. static bool rt1015_readable_register(struct device *dev, unsigned int reg)
  232. {
  233. switch (reg) {
  234. case RT1015_RESET:
  235. case RT1015_CLK2:
  236. case RT1015_CLK3:
  237. case RT1015_PLL1:
  238. case RT1015_PLL2:
  239. case RT1015_DUM_RW1:
  240. case RT1015_DUM_RW2:
  241. case RT1015_DUM_RW3:
  242. case RT1015_DUM_RW4:
  243. case RT1015_DUM_RW5:
  244. case RT1015_DUM_RW6:
  245. case RT1015_CLK_DET:
  246. case RT1015_SIL_DET:
  247. case RT1015_CUSTOMER_ID:
  248. case RT1015_PCODE_FWVER:
  249. case RT1015_VER_ID:
  250. case RT1015_VENDOR_ID:
  251. case RT1015_DEVICE_ID:
  252. case RT1015_PAD_DRV1:
  253. case RT1015_PAD_DRV2:
  254. case RT1015_GAT_BOOST:
  255. case RT1015_PRO_ALT:
  256. case RT1015_OSCK_STA:
  257. case RT1015_MAN_I2C:
  258. case RT1015_DAC1:
  259. case RT1015_DAC2:
  260. case RT1015_DAC3:
  261. case RT1015_ADC1:
  262. case RT1015_ADC2:
  263. case RT1015_TDM_MASTER:
  264. case RT1015_TDM_TCON:
  265. case RT1015_TDM1_1:
  266. case RT1015_TDM1_2:
  267. case RT1015_TDM1_3:
  268. case RT1015_TDM1_4:
  269. case RT1015_TDM1_5:
  270. case RT1015_MIXER1:
  271. case RT1015_MIXER2:
  272. case RT1015_ANA_PROTECT1:
  273. case RT1015_ANA_CTRL_SEQ1:
  274. case RT1015_ANA_CTRL_SEQ2:
  275. case RT1015_VBAT_DET_DEB:
  276. case RT1015_VBAT_VOLT_DET1:
  277. case RT1015_VBAT_VOLT_DET2:
  278. case RT1015_VBAT_TEST_OUT1:
  279. case RT1015_VBAT_TEST_OUT2:
  280. case RT1015_VBAT_PROT_ATT:
  281. case RT1015_VBAT_DET_CODE:
  282. case RT1015_PWR1:
  283. case RT1015_PWR4:
  284. case RT1015_PWR5:
  285. case RT1015_PWR6:
  286. case RT1015_PWR7:
  287. case RT1015_PWR8:
  288. case RT1015_PWR9:
  289. case RT1015_CLASSD_SEQ:
  290. case RT1015_SMART_BST_CTRL1:
  291. case RT1015_SMART_BST_CTRL2:
  292. case RT1015_ANA_CTRL1:
  293. case RT1015_ANA_CTRL2:
  294. case RT1015_PWR_STATE_CTRL:
  295. case RT1015_MONO_DYNA_CTRL:
  296. case RT1015_MONO_DYNA_CTRL1:
  297. case RT1015_MONO_DYNA_CTRL2:
  298. case RT1015_MONO_DYNA_CTRL3:
  299. case RT1015_MONO_DYNA_CTRL4:
  300. case RT1015_MONO_DYNA_CTRL5:
  301. case RT1015_SPK_VOL:
  302. case RT1015_SHORT_DETTOP1:
  303. case RT1015_SHORT_DETTOP2:
  304. case RT1015_SPK_DC_DETECT1:
  305. case RT1015_SPK_DC_DETECT2:
  306. case RT1015_SPK_DC_DETECT3:
  307. case RT1015_SPK_DC_DETECT4:
  308. case RT1015_SPK_DC_DETECT5:
  309. case RT1015_BAT_RPO_STEP1:
  310. case RT1015_BAT_RPO_STEP2:
  311. case RT1015_BAT_RPO_STEP3:
  312. case RT1015_BAT_RPO_STEP4:
  313. case RT1015_BAT_RPO_STEP5:
  314. case RT1015_BAT_RPO_STEP6:
  315. case RT1015_BAT_RPO_STEP7:
  316. case RT1015_BAT_RPO_STEP8:
  317. case RT1015_BAT_RPO_STEP9:
  318. case RT1015_BAT_RPO_STEP10:
  319. case RT1015_BAT_RPO_STEP11:
  320. case RT1015_BAT_RPO_STEP12:
  321. case RT1015_SPREAD_SPEC1:
  322. case RT1015_SPREAD_SPEC2:
  323. case RT1015_PAD_STATUS:
  324. case RT1015_PADS_PULLING_CTRL1:
  325. case RT1015_PADS_DRIVING:
  326. case RT1015_SYS_RST1:
  327. case RT1015_SYS_RST2:
  328. case RT1015_SYS_GATING1:
  329. case RT1015_TEST_MODE1:
  330. case RT1015_TEST_MODE2:
  331. case RT1015_TIMING_CTRL1:
  332. case RT1015_PLL_INT:
  333. case RT1015_TEST_OUT1:
  334. case RT1015_DC_CALIB_CLSD1:
  335. case RT1015_DC_CALIB_CLSD2:
  336. case RT1015_DC_CALIB_CLSD3:
  337. case RT1015_DC_CALIB_CLSD4:
  338. case RT1015_DC_CALIB_CLSD5:
  339. case RT1015_DC_CALIB_CLSD6:
  340. case RT1015_DC_CALIB_CLSD7:
  341. case RT1015_DC_CALIB_CLSD8:
  342. case RT1015_DC_CALIB_CLSD9:
  343. case RT1015_DC_CALIB_CLSD10:
  344. case RT1015_CLSD_INTERNAL1:
  345. case RT1015_CLSD_INTERNAL2:
  346. case RT1015_CLSD_INTERNAL3:
  347. case RT1015_CLSD_INTERNAL4:
  348. case RT1015_CLSD_INTERNAL5:
  349. case RT1015_CLSD_INTERNAL6:
  350. case RT1015_CLSD_INTERNAL7:
  351. case RT1015_CLSD_INTERNAL8:
  352. case RT1015_CLSD_INTERNAL9:
  353. case RT1015_CLSD_OCP_CTRL:
  354. case RT1015_VREF_LV:
  355. case RT1015_MBIAS1:
  356. case RT1015_MBIAS2:
  357. case RT1015_MBIAS3:
  358. case RT1015_MBIAS4:
  359. case RT1015_VREF_LV1:
  360. case RT1015_S_BST_TIMING_INTER1:
  361. case RT1015_S_BST_TIMING_INTER2:
  362. case RT1015_S_BST_TIMING_INTER3:
  363. case RT1015_S_BST_TIMING_INTER4:
  364. case RT1015_S_BST_TIMING_INTER5:
  365. case RT1015_S_BST_TIMING_INTER6:
  366. case RT1015_S_BST_TIMING_INTER7:
  367. case RT1015_S_BST_TIMING_INTER8:
  368. case RT1015_S_BST_TIMING_INTER9:
  369. case RT1015_S_BST_TIMING_INTER10:
  370. case RT1015_S_BST_TIMING_INTER11:
  371. case RT1015_S_BST_TIMING_INTER12:
  372. case RT1015_S_BST_TIMING_INTER13:
  373. case RT1015_S_BST_TIMING_INTER14:
  374. case RT1015_S_BST_TIMING_INTER15:
  375. case RT1015_S_BST_TIMING_INTER16:
  376. case RT1015_S_BST_TIMING_INTER17:
  377. case RT1015_S_BST_TIMING_INTER18:
  378. case RT1015_S_BST_TIMING_INTER19:
  379. case RT1015_S_BST_TIMING_INTER20:
  380. case RT1015_S_BST_TIMING_INTER21:
  381. case RT1015_S_BST_TIMING_INTER22:
  382. case RT1015_S_BST_TIMING_INTER23:
  383. case RT1015_S_BST_TIMING_INTER24:
  384. case RT1015_S_BST_TIMING_INTER25:
  385. case RT1015_S_BST_TIMING_INTER26:
  386. case RT1015_S_BST_TIMING_INTER27:
  387. case RT1015_S_BST_TIMING_INTER28:
  388. case RT1015_S_BST_TIMING_INTER29:
  389. case RT1015_S_BST_TIMING_INTER30:
  390. case RT1015_S_BST_TIMING_INTER31:
  391. case RT1015_S_BST_TIMING_INTER32:
  392. case RT1015_S_BST_TIMING_INTER33:
  393. case RT1015_S_BST_TIMING_INTER34:
  394. case RT1015_S_BST_TIMING_INTER35:
  395. case RT1015_S_BST_TIMING_INTER36:
  396. return true;
  397. default:
  398. return false;
  399. }
  400. }
  401. static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9525, 75, 0);
  402. static const char * const rt1015_din_source_select[] = {
  403. "Left",
  404. "Right",
  405. "Left + Right average",
  406. };
  407. static SOC_ENUM_SINGLE_DECL(rt1015_mono_lr_sel, RT1015_PAD_DRV2, 4,
  408. rt1015_din_source_select);
  409. static const char * const rt1015_boost_mode[] = {
  410. "Bypass", "Adaptive", "Fixed Adaptive"
  411. };
  412. static SOC_ENUM_SINGLE_DECL(rt1015_boost_mode_enum, 0, 0,
  413. rt1015_boost_mode);
  414. static int rt1015_boost_mode_get(struct snd_kcontrol *kcontrol,
  415. struct snd_ctl_elem_value *ucontrol)
  416. {
  417. struct snd_soc_component *component =
  418. snd_soc_kcontrol_component(kcontrol);
  419. struct rt1015_priv *rt1015 =
  420. snd_soc_component_get_drvdata(component);
  421. ucontrol->value.integer.value[0] = rt1015->boost_mode;
  422. return 0;
  423. }
  424. static int rt1015_boost_mode_put(struct snd_kcontrol *kcontrol,
  425. struct snd_ctl_elem_value *ucontrol)
  426. {
  427. struct snd_soc_component *component =
  428. snd_soc_kcontrol_component(kcontrol);
  429. struct rt1015_priv *rt1015 =
  430. snd_soc_component_get_drvdata(component);
  431. int boost_mode = ucontrol->value.integer.value[0];
  432. switch (boost_mode) {
  433. case BYPASS:
  434. snd_soc_component_update_bits(component,
  435. RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
  436. RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
  437. RT1015_ABST_REG_MODE | RT1015_ABST_FIX_TGT_DIS |
  438. RT1015_BYPASS_SWRREG_BYPASS);
  439. break;
  440. case ADAPTIVE:
  441. snd_soc_component_update_bits(component,
  442. RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
  443. RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
  444. RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_DIS |
  445. RT1015_BYPASS_SWRREG_PASS);
  446. break;
  447. case FIXED_ADAPTIVE:
  448. snd_soc_component_update_bits(component,
  449. RT1015_SMART_BST_CTRL1, RT1015_ABST_AUTO_EN_MASK |
  450. RT1015_ABST_FIX_TGT_MASK | RT1015_BYPASS_SWR_REG_MASK,
  451. RT1015_ABST_AUTO_MODE | RT1015_ABST_FIX_TGT_EN |
  452. RT1015_BYPASS_SWRREG_PASS);
  453. break;
  454. default:
  455. dev_err(component->dev, "Unknown boost control.\n");
  456. return -EINVAL;
  457. }
  458. rt1015->boost_mode = boost_mode;
  459. return 0;
  460. }
  461. static int rt1015_bypass_boost_get(struct snd_kcontrol *kcontrol,
  462. struct snd_ctl_elem_value *ucontrol)
  463. {
  464. struct snd_soc_component *component =
  465. snd_soc_kcontrol_component(kcontrol);
  466. struct rt1015_priv *rt1015 =
  467. snd_soc_component_get_drvdata(component);
  468. ucontrol->value.integer.value[0] = rt1015->bypass_boost;
  469. return 0;
  470. }
  471. static void rt1015_calibrate(struct rt1015_priv *rt1015)
  472. {
  473. struct snd_soc_component *component = rt1015->component;
  474. struct regmap *regmap = rt1015->regmap;
  475. snd_soc_dapm_mutex_lock(&component->dapm);
  476. regcache_cache_bypass(regmap, true);
  477. regmap_write(regmap, RT1015_CLK_DET, 0x0000);
  478. regmap_write(regmap, RT1015_PWR4, 0x00B2);
  479. regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x0009);
  480. msleep(100);
  481. regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000A);
  482. msleep(100);
  483. regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000C);
  484. msleep(100);
  485. regmap_write(regmap, RT1015_CLSD_INTERNAL8, 0x2028);
  486. regmap_write(regmap, RT1015_CLSD_INTERNAL9, 0x0140);
  487. regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x000D);
  488. msleep(300);
  489. regmap_write(regmap, RT1015_PWR_STATE_CTRL, 0x0008);
  490. regmap_write(regmap, RT1015_SYS_RST1, 0x05F5);
  491. regmap_write(regmap, RT1015_CLK_DET, 0x8000);
  492. regcache_cache_bypass(regmap, false);
  493. regcache_mark_dirty(regmap);
  494. regcache_sync(regmap);
  495. snd_soc_dapm_mutex_unlock(&component->dapm);
  496. }
  497. static int rt1015_bypass_boost_put(struct snd_kcontrol *kcontrol,
  498. struct snd_ctl_elem_value *ucontrol)
  499. {
  500. struct snd_soc_component *component =
  501. snd_soc_kcontrol_component(kcontrol);
  502. struct rt1015_priv *rt1015 =
  503. snd_soc_component_get_drvdata(component);
  504. if (rt1015->dac_is_used) {
  505. dev_err(component->dev, "DAC is being used!\n");
  506. return -EBUSY;
  507. }
  508. rt1015->bypass_boost = ucontrol->value.integer.value[0];
  509. if (rt1015->bypass_boost == RT1015_Bypass_Boost &&
  510. !rt1015->cali_done) {
  511. rt1015_calibrate(rt1015);
  512. rt1015->cali_done = 1;
  513. regmap_write(rt1015->regmap, RT1015_MONO_DYNA_CTRL, 0x0010);
  514. }
  515. return 0;
  516. }
  517. static const struct snd_kcontrol_new rt1015_snd_controls[] = {
  518. SOC_SINGLE_TLV("DAC Playback Volume", RT1015_DAC1, RT1015_DAC_VOL_SFT,
  519. 127, 0, dac_vol_tlv),
  520. SOC_DOUBLE("DAC Playback Switch", RT1015_DAC3,
  521. RT1015_DA_MUTE_SFT, RT1015_DVOL_MUTE_FLAG_SFT, 1, 1),
  522. SOC_ENUM_EXT("Boost Mode", rt1015_boost_mode_enum,
  523. rt1015_boost_mode_get, rt1015_boost_mode_put),
  524. SOC_ENUM("Mono LR Select", rt1015_mono_lr_sel),
  525. SOC_SINGLE_EXT("Bypass Boost", SND_SOC_NOPM, 0, 1, 0,
  526. rt1015_bypass_boost_get, rt1015_bypass_boost_put),
  527. };
  528. static int rt1015_is_sys_clk_from_pll(struct snd_soc_dapm_widget *source,
  529. struct snd_soc_dapm_widget *sink)
  530. {
  531. struct snd_soc_component *component =
  532. snd_soc_dapm_to_component(source->dapm);
  533. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  534. if (rt1015->sysclk_src == RT1015_SCLK_S_PLL)
  535. return 1;
  536. else
  537. return 0;
  538. }
  539. static int r1015_dac_event(struct snd_soc_dapm_widget *w,
  540. struct snd_kcontrol *kcontrol, int event)
  541. {
  542. struct snd_soc_component *component =
  543. snd_soc_dapm_to_component(w->dapm);
  544. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  545. switch (event) {
  546. case SND_SOC_DAPM_PRE_PMU:
  547. rt1015->dac_is_used = 1;
  548. if (rt1015->bypass_boost == RT1015_Enable_Boost) {
  549. snd_soc_component_write(component,
  550. RT1015_SYS_RST1, 0x05f7);
  551. snd_soc_component_write(component,
  552. RT1015_SYS_RST2, 0x0b0a);
  553. snd_soc_component_write(component,
  554. RT1015_GAT_BOOST, 0xacfe);
  555. snd_soc_component_write(component,
  556. RT1015_PWR9, 0xaa00);
  557. snd_soc_component_write(component,
  558. RT1015_GAT_BOOST, 0xecfe);
  559. } else {
  560. snd_soc_component_write(component,
  561. 0x032d, 0xaa60);
  562. snd_soc_component_write(component,
  563. RT1015_SYS_RST1, 0x05f7);
  564. snd_soc_component_write(component,
  565. RT1015_SYS_RST2, 0x0b0a);
  566. snd_soc_component_write(component,
  567. RT1015_PWR_STATE_CTRL, 0x008e);
  568. }
  569. break;
  570. case SND_SOC_DAPM_POST_PMD:
  571. if (rt1015->bypass_boost == RT1015_Enable_Boost) {
  572. snd_soc_component_write(component,
  573. RT1015_PWR9, 0xa800);
  574. snd_soc_component_write(component,
  575. RT1015_SYS_RST1, 0x05f5);
  576. snd_soc_component_write(component,
  577. RT1015_SYS_RST2, 0x0b9a);
  578. } else {
  579. snd_soc_component_write(component,
  580. 0x032d, 0xaa60);
  581. snd_soc_component_write(component,
  582. RT1015_PWR_STATE_CTRL, 0x0088);
  583. snd_soc_component_write(component,
  584. RT1015_SYS_RST1, 0x05f5);
  585. snd_soc_component_write(component,
  586. RT1015_SYS_RST2, 0x0b9a);
  587. }
  588. rt1015->dac_is_used = 0;
  589. break;
  590. default:
  591. break;
  592. }
  593. return 0;
  594. }
  595. static int rt1015_amp_drv_event(struct snd_soc_dapm_widget *w,
  596. struct snd_kcontrol *kcontrol, int event)
  597. {
  598. struct snd_soc_component *component =
  599. snd_soc_dapm_to_component(w->dapm);
  600. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  601. unsigned int ret, ret2;
  602. switch (event) {
  603. case SND_SOC_DAPM_PRE_PMU:
  604. ret = snd_soc_component_read(component, RT1015_CLK_DET);
  605. ret2 = snd_soc_component_read(component, RT1015_SPK_DC_DETECT1);
  606. if (!((ret >> 15) & 0x1)) {
  607. snd_soc_component_update_bits(component, RT1015_CLK_DET,
  608. RT1015_EN_BCLK_DET_MASK, RT1015_EN_BCLK_DET);
  609. dev_dbg(component->dev, "BCLK Detection Enabled.\n");
  610. }
  611. if (!((ret2 >> 12) & 0x1)) {
  612. snd_soc_component_update_bits(component, RT1015_SPK_DC_DETECT1,
  613. RT1015_EN_CLA_D_DC_DET_MASK, RT1015_EN_CLA_D_DC_DET);
  614. dev_dbg(component->dev, "Class-D DC Detection Enabled.\n");
  615. }
  616. break;
  617. case SND_SOC_DAPM_POST_PMU:
  618. msleep(rt1015->pdata.power_up_delay_ms);
  619. break;
  620. default:
  621. break;
  622. }
  623. return 0;
  624. }
  625. static const struct snd_soc_dapm_widget rt1015_dapm_widgets[] = {
  626. SND_SOC_DAPM_SUPPLY("PLL", RT1015_PWR1, RT1015_PWR_PLL_BIT, 0,
  627. NULL, 0),
  628. SND_SOC_DAPM_AIF_IN("AIFRX", "AIF Playback", 0, SND_SOC_NOPM, 0, 0),
  629. SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0,
  630. r1015_dac_event, SND_SOC_DAPM_PRE_PMU |
  631. SND_SOC_DAPM_POST_PMD),
  632. SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0,
  633. rt1015_amp_drv_event, SND_SOC_DAPM_PRE_PMU |
  634. SND_SOC_DAPM_POST_PMU),
  635. SND_SOC_DAPM_OUTPUT("SPO"),
  636. };
  637. static const struct snd_soc_dapm_route rt1015_dapm_routes[] = {
  638. { "DAC", NULL, "AIFRX" },
  639. { "DAC", NULL, "PLL", rt1015_is_sys_clk_from_pll},
  640. { "Amp Drv", NULL, "DAC" },
  641. { "SPO", NULL, "Amp Drv" },
  642. };
  643. static int rt1015_hw_params(struct snd_pcm_substream *substream,
  644. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  645. {
  646. struct snd_soc_component *component = dai->component;
  647. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  648. int pre_div, frame_size, lrck;
  649. unsigned int val_len = 0;
  650. lrck = params_rate(params);
  651. pre_div = rl6231_get_clk_info(rt1015->sysclk, lrck);
  652. if (pre_div < 0) {
  653. dev_err(component->dev, "Unsupported clock rate\n");
  654. return -EINVAL;
  655. }
  656. frame_size = snd_soc_params_to_frame_size(params);
  657. if (frame_size < 0) {
  658. dev_err(component->dev, "Unsupported frame size: %d\n",
  659. frame_size);
  660. return -EINVAL;
  661. }
  662. dev_dbg(component->dev, "pre_div is %d for iis %d\n", pre_div, dai->id);
  663. dev_dbg(component->dev, "lrck is %dHz and pre_div is %d for iis %d\n",
  664. lrck, pre_div, dai->id);
  665. switch (params_width(params)) {
  666. case 16:
  667. break;
  668. case 20:
  669. val_len = RT1015_I2S_DL_20;
  670. break;
  671. case 24:
  672. val_len = RT1015_I2S_DL_24;
  673. break;
  674. case 8:
  675. val_len = RT1015_I2S_DL_8;
  676. break;
  677. default:
  678. return -EINVAL;
  679. }
  680. snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
  681. RT1015_I2S_DL_MASK, val_len);
  682. snd_soc_component_update_bits(component, RT1015_CLK2,
  683. RT1015_FS_PD_MASK, pre_div << RT1015_FS_PD_SFT);
  684. return 0;
  685. }
  686. static int rt1015_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  687. {
  688. struct snd_soc_component *component = dai->component;
  689. unsigned int reg_val = 0, reg_val2 = 0;
  690. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  691. case SND_SOC_DAIFMT_CBM_CFM:
  692. reg_val |= RT1015_TCON_TDM_MS_M;
  693. break;
  694. case SND_SOC_DAIFMT_CBS_CFS:
  695. reg_val |= RT1015_TCON_TDM_MS_S;
  696. break;
  697. default:
  698. return -EINVAL;
  699. }
  700. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  701. case SND_SOC_DAIFMT_NB_NF:
  702. break;
  703. case SND_SOC_DAIFMT_IB_NF:
  704. reg_val2 |= RT1015_TDM_INV_BCLK;
  705. break;
  706. default:
  707. return -EINVAL;
  708. }
  709. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  710. case SND_SOC_DAIFMT_I2S:
  711. break;
  712. case SND_SOC_DAIFMT_LEFT_J:
  713. reg_val |= RT1015_I2S_M_DF_LEFT;
  714. break;
  715. case SND_SOC_DAIFMT_DSP_A:
  716. reg_val |= RT1015_I2S_M_DF_PCM_A;
  717. break;
  718. case SND_SOC_DAIFMT_DSP_B:
  719. reg_val |= RT1015_I2S_M_DF_PCM_B;
  720. break;
  721. default:
  722. return -EINVAL;
  723. }
  724. snd_soc_component_update_bits(component, RT1015_TDM_MASTER,
  725. RT1015_TCON_TDM_MS_MASK | RT1015_I2S_M_DF_MASK,
  726. reg_val);
  727. snd_soc_component_update_bits(component, RT1015_TDM1_1,
  728. RT1015_TDM_INV_BCLK_MASK, reg_val2);
  729. return 0;
  730. }
  731. static int rt1015_set_component_sysclk(struct snd_soc_component *component,
  732. int clk_id, int source, unsigned int freq, int dir)
  733. {
  734. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  735. unsigned int reg_val = 0;
  736. if (freq == rt1015->sysclk && clk_id == rt1015->sysclk_src)
  737. return 0;
  738. switch (clk_id) {
  739. case RT1015_SCLK_S_MCLK:
  740. reg_val |= RT1015_CLK_SYS_PRE_SEL_MCLK;
  741. break;
  742. case RT1015_SCLK_S_PLL:
  743. reg_val |= RT1015_CLK_SYS_PRE_SEL_PLL;
  744. break;
  745. default:
  746. dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
  747. return -EINVAL;
  748. }
  749. rt1015->sysclk = freq;
  750. rt1015->sysclk_src = clk_id;
  751. dev_dbg(component->dev, "Sysclk is %dHz and clock id is %d\n",
  752. freq, clk_id);
  753. snd_soc_component_update_bits(component, RT1015_CLK2,
  754. RT1015_CLK_SYS_PRE_SEL_MASK, reg_val);
  755. return 0;
  756. }
  757. static int rt1015_set_component_pll(struct snd_soc_component *component,
  758. int pll_id, int source, unsigned int freq_in,
  759. unsigned int freq_out)
  760. {
  761. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  762. struct rl6231_pll_code pll_code;
  763. int ret;
  764. if (!freq_in || !freq_out) {
  765. dev_dbg(component->dev, "PLL disabled\n");
  766. rt1015->pll_in = 0;
  767. rt1015->pll_out = 0;
  768. return 0;
  769. }
  770. if (source == rt1015->pll_src && freq_in == rt1015->pll_in &&
  771. freq_out == rt1015->pll_out)
  772. return 0;
  773. switch (source) {
  774. case RT1015_PLL_S_MCLK:
  775. snd_soc_component_update_bits(component, RT1015_CLK2,
  776. RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_PLL_SRC2);
  777. break;
  778. case RT1015_PLL_S_BCLK:
  779. snd_soc_component_update_bits(component, RT1015_CLK2,
  780. RT1015_PLL_SEL_MASK, RT1015_PLL_SEL_BCLK);
  781. break;
  782. default:
  783. dev_err(component->dev, "Unknown PLL Source %d\n", source);
  784. return -EINVAL;
  785. }
  786. ret = rl6231_pll_calc(freq_in, freq_out, &pll_code);
  787. if (ret < 0) {
  788. dev_err(component->dev, "Unsupported input clock %d\n", freq_in);
  789. return ret;
  790. }
  791. dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
  792. pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
  793. pll_code.n_code, pll_code.k_code);
  794. snd_soc_component_write(component, RT1015_PLL1,
  795. ((pll_code.m_bp ? 0 : pll_code.m_code) << RT1015_PLL_M_SFT) |
  796. (pll_code.m_bp << RT1015_PLL_M_BP_SFT) |
  797. pll_code.n_code);
  798. snd_soc_component_write(component, RT1015_PLL2,
  799. pll_code.k_code);
  800. rt1015->pll_in = freq_in;
  801. rt1015->pll_out = freq_out;
  802. rt1015->pll_src = source;
  803. return 0;
  804. }
  805. static int rt1015_set_tdm_slot(struct snd_soc_dai *dai,
  806. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  807. {
  808. struct snd_soc_component *component = dai->component;
  809. unsigned int val = 0, rx_slotnum, tx_slotnum;
  810. int ret = 0, first_bit;
  811. switch (slots) {
  812. case 2:
  813. val |= RT1015_I2S_TX_2CH;
  814. break;
  815. case 4:
  816. val |= RT1015_I2S_TX_4CH;
  817. break;
  818. case 6:
  819. val |= RT1015_I2S_TX_6CH;
  820. break;
  821. case 8:
  822. val |= RT1015_I2S_TX_8CH;
  823. break;
  824. default:
  825. ret = -EINVAL;
  826. goto _set_tdm_err_;
  827. }
  828. switch (slot_width) {
  829. case 16:
  830. val |= RT1015_I2S_CH_TX_LEN_16B;
  831. break;
  832. case 20:
  833. val |= RT1015_I2S_CH_TX_LEN_20B;
  834. break;
  835. case 24:
  836. val |= RT1015_I2S_CH_TX_LEN_24B;
  837. break;
  838. case 32:
  839. val |= RT1015_I2S_CH_TX_LEN_32B;
  840. break;
  841. default:
  842. ret = -EINVAL;
  843. goto _set_tdm_err_;
  844. }
  845. /* Rx slot configuration */
  846. rx_slotnum = hweight_long(rx_mask);
  847. if (rx_slotnum != 1) {
  848. ret = -EINVAL;
  849. dev_err(component->dev, "too many rx slots or zero slot\n");
  850. goto _set_tdm_err_;
  851. }
  852. /* This is an assumption that the system sends stereo audio to the amplifier typically.
  853. * And the stereo audio is placed in slot 0/2/4/6 as the starting slot.
  854. * The users could select the channel from L/R/L+R by "Mono LR Select" control.
  855. */
  856. first_bit = __ffs(rx_mask);
  857. switch (first_bit) {
  858. case 0:
  859. case 2:
  860. case 4:
  861. case 6:
  862. snd_soc_component_update_bits(component,
  863. RT1015_TDM1_4,
  864. RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
  865. RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
  866. (first_bit << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
  867. ((first_bit+1) << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
  868. break;
  869. case 1:
  870. case 3:
  871. case 5:
  872. case 7:
  873. snd_soc_component_update_bits(component,
  874. RT1015_TDM1_4,
  875. RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
  876. RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
  877. ((first_bit-1) << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
  878. (first_bit << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
  879. break;
  880. default:
  881. ret = -EINVAL;
  882. goto _set_tdm_err_;
  883. }
  884. /* Tx slot configuration */
  885. tx_slotnum = hweight_long(tx_mask);
  886. if (tx_slotnum) {
  887. ret = -EINVAL;
  888. dev_err(component->dev, "doesn't need to support tx slots\n");
  889. goto _set_tdm_err_;
  890. }
  891. snd_soc_component_update_bits(component, RT1015_TDM1_1,
  892. RT1015_I2S_CH_TX_MASK | RT1015_I2S_CH_RX_MASK |
  893. RT1015_I2S_CH_TX_LEN_MASK | RT1015_I2S_CH_RX_LEN_MASK, val);
  894. _set_tdm_err_:
  895. return ret;
  896. }
  897. static int rt1015_probe(struct snd_soc_component *component)
  898. {
  899. struct rt1015_priv *rt1015 =
  900. snd_soc_component_get_drvdata(component);
  901. rt1015->component = component;
  902. return 0;
  903. }
  904. static void rt1015_remove(struct snd_soc_component *component)
  905. {
  906. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  907. regmap_write(rt1015->regmap, RT1015_RESET, 0);
  908. }
  909. #define RT1015_STEREO_RATES SNDRV_PCM_RATE_8000_192000
  910. #define RT1015_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  911. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
  912. static const struct snd_soc_dai_ops rt1015_aif_dai_ops = {
  913. .hw_params = rt1015_hw_params,
  914. .set_fmt = rt1015_set_dai_fmt,
  915. .set_tdm_slot = rt1015_set_tdm_slot,
  916. };
  917. static struct snd_soc_dai_driver rt1015_dai[] = {
  918. {
  919. .name = "rt1015-aif",
  920. .id = 0,
  921. .playback = {
  922. .stream_name = "AIF Playback",
  923. .channels_min = 1,
  924. .channels_max = 4,
  925. .rates = RT1015_STEREO_RATES,
  926. .formats = RT1015_FORMATS,
  927. },
  928. .ops = &rt1015_aif_dai_ops,
  929. }
  930. };
  931. #ifdef CONFIG_PM
  932. static int rt1015_suspend(struct snd_soc_component *component)
  933. {
  934. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  935. regcache_cache_only(rt1015->regmap, true);
  936. regcache_mark_dirty(rt1015->regmap);
  937. return 0;
  938. }
  939. static int rt1015_resume(struct snd_soc_component *component)
  940. {
  941. struct rt1015_priv *rt1015 = snd_soc_component_get_drvdata(component);
  942. regcache_cache_only(rt1015->regmap, false);
  943. regcache_sync(rt1015->regmap);
  944. if (rt1015->cali_done)
  945. rt1015_calibrate(rt1015);
  946. return 0;
  947. }
  948. #else
  949. #define rt1015_suspend NULL
  950. #define rt1015_resume NULL
  951. #endif
  952. static const struct snd_soc_component_driver soc_component_dev_rt1015 = {
  953. .probe = rt1015_probe,
  954. .remove = rt1015_remove,
  955. .suspend = rt1015_suspend,
  956. .resume = rt1015_resume,
  957. .controls = rt1015_snd_controls,
  958. .num_controls = ARRAY_SIZE(rt1015_snd_controls),
  959. .dapm_widgets = rt1015_dapm_widgets,
  960. .num_dapm_widgets = ARRAY_SIZE(rt1015_dapm_widgets),
  961. .dapm_routes = rt1015_dapm_routes,
  962. .num_dapm_routes = ARRAY_SIZE(rt1015_dapm_routes),
  963. .set_sysclk = rt1015_set_component_sysclk,
  964. .set_pll = rt1015_set_component_pll,
  965. .use_pmdown_time = 1,
  966. .endianness = 1,
  967. };
  968. static const struct regmap_config rt1015_regmap = {
  969. .reg_bits = 16,
  970. .val_bits = 16,
  971. .max_register = RT1015_S_BST_TIMING_INTER36,
  972. .volatile_reg = rt1015_volatile_register,
  973. .readable_reg = rt1015_readable_register,
  974. .cache_type = REGCACHE_RBTREE,
  975. .reg_defaults = rt1015_reg,
  976. .num_reg_defaults = ARRAY_SIZE(rt1015_reg),
  977. };
  978. static const struct i2c_device_id rt1015_i2c_id[] = {
  979. { "rt1015", 0 },
  980. { }
  981. };
  982. MODULE_DEVICE_TABLE(i2c, rt1015_i2c_id);
  983. #if defined(CONFIG_OF)
  984. static const struct of_device_id rt1015_of_match[] = {
  985. { .compatible = "realtek,rt1015", },
  986. {},
  987. };
  988. MODULE_DEVICE_TABLE(of, rt1015_of_match);
  989. #endif
  990. #ifdef CONFIG_ACPI
  991. static const struct acpi_device_id rt1015_acpi_match[] = {
  992. {"10EC1015", 0,},
  993. {},
  994. };
  995. MODULE_DEVICE_TABLE(acpi, rt1015_acpi_match);
  996. #endif
  997. static void rt1015_parse_dt(struct rt1015_priv *rt1015, struct device *dev)
  998. {
  999. device_property_read_u32(dev, "realtek,power-up-delay-ms",
  1000. &rt1015->pdata.power_up_delay_ms);
  1001. }
  1002. static int rt1015_i2c_probe(struct i2c_client *i2c)
  1003. {
  1004. struct rt1015_platform_data *pdata = dev_get_platdata(&i2c->dev);
  1005. struct rt1015_priv *rt1015;
  1006. int ret;
  1007. unsigned int val;
  1008. rt1015 = devm_kzalloc(&i2c->dev, sizeof(*rt1015), GFP_KERNEL);
  1009. if (!rt1015)
  1010. return -ENOMEM;
  1011. i2c_set_clientdata(i2c, rt1015);
  1012. rt1015->pdata = i2s_default_platform_data;
  1013. if (pdata)
  1014. rt1015->pdata = *pdata;
  1015. else
  1016. rt1015_parse_dt(rt1015, &i2c->dev);
  1017. rt1015->regmap = devm_regmap_init_i2c(i2c, &rt1015_regmap);
  1018. if (IS_ERR(rt1015->regmap)) {
  1019. ret = PTR_ERR(rt1015->regmap);
  1020. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  1021. ret);
  1022. return ret;
  1023. }
  1024. ret = regmap_read(rt1015->regmap, RT1015_DEVICE_ID, &val);
  1025. if (ret) {
  1026. dev_err(&i2c->dev,
  1027. "Failed to read device register: %d\n", ret);
  1028. return ret;
  1029. } else if ((val != RT1015_DEVICE_ID_VAL) &&
  1030. (val != RT1015_DEVICE_ID_VAL2)) {
  1031. dev_err(&i2c->dev,
  1032. "Device with ID register %x is not rt1015\n", val);
  1033. return -ENODEV;
  1034. }
  1035. return devm_snd_soc_register_component(&i2c->dev,
  1036. &soc_component_dev_rt1015,
  1037. rt1015_dai, ARRAY_SIZE(rt1015_dai));
  1038. }
  1039. static void rt1015_i2c_shutdown(struct i2c_client *client)
  1040. {
  1041. struct rt1015_priv *rt1015 = i2c_get_clientdata(client);
  1042. regmap_write(rt1015->regmap, RT1015_RESET, 0);
  1043. }
  1044. static struct i2c_driver rt1015_i2c_driver = {
  1045. .driver = {
  1046. .name = "rt1015",
  1047. .of_match_table = of_match_ptr(rt1015_of_match),
  1048. .acpi_match_table = ACPI_PTR(rt1015_acpi_match),
  1049. },
  1050. .probe_new = rt1015_i2c_probe,
  1051. .shutdown = rt1015_i2c_shutdown,
  1052. .id_table = rt1015_i2c_id,
  1053. };
  1054. module_i2c_driver(rt1015_i2c_driver);
  1055. MODULE_DESCRIPTION("ASoC RT1015 driver");
  1056. MODULE_AUTHOR("Jack Yu <[email protected]>");
  1057. MODULE_LICENSE("GPL v2");