ep92.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/delay.h>
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/i2c.h>
  10. #include <linux/slab.h>
  11. #include <linux/fs.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/kobject.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/tlv.h>
  19. #include <sound/soc.h>
  20. #include <linux/workqueue.h>
  21. #include "ep92.h"
  22. #define EP92_POLL_INTERVAL_OFF_MSEC 200
  23. #define EP92_POLL_INTERVAL_ON_MSEC 20
  24. #define EP92_SYSFS_ENTRY_MAX_LEN 64
  25. #define EP92_HYST_CNT 5
  26. #define EP92_RATES (SNDRV_PCM_RATE_32000 |\
  27. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  28. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |\
  29. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
  30. #define EP92_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
  31. static const unsigned int ep92_samp_freq_table[8] = {
  32. 32000, 44100, 48000, 88200, 96000, 176400, 192000, 768000
  33. };
  34. static bool ep92_volatile_register(struct device *dev, unsigned int reg)
  35. {
  36. switch (reg) {
  37. case EP92_BI_GENERAL_INFO_0:
  38. case EP92_BI_GENERAL_INFO_1:
  39. case EP92_BI_GENERAL_INFO_2:
  40. case EP92_BI_GENERAL_INFO_3:
  41. case EP92_BI_GENERAL_INFO_4:
  42. case EP92_BI_GENERAL_INFO_5:
  43. case EP92_BI_GENERAL_INFO_6:
  44. case EP92_GENERAL_CONTROL_0:
  45. case EP92_GENERAL_CONTROL_1:
  46. case EP92_GENERAL_CONTROL_2:
  47. case EP92_GENERAL_CONTROL_3:
  48. case EP92_GENERAL_CONTROL_4:
  49. case EP92_AUDIO_INFO_SYSTEM_STATUS_0:
  50. case EP92_AUDIO_INFO_SYSTEM_STATUS_1:
  51. case EP92_AUDIO_INFO_AUDIO_STATUS:
  52. case EP92_AUDIO_INFO_CHANNEL_STATUS_0:
  53. case EP92_AUDIO_INFO_CHANNEL_STATUS_1:
  54. case EP92_AUDIO_INFO_CHANNEL_STATUS_2:
  55. case EP92_AUDIO_INFO_CHANNEL_STATUS_3:
  56. case EP92_AUDIO_INFO_CHANNEL_STATUS_4:
  57. case EP92_AUDIO_INFO_ADO_INFO_FRAME_0:
  58. case EP92_AUDIO_INFO_ADO_INFO_FRAME_1:
  59. case EP92_AUDIO_INFO_ADO_INFO_FRAME_2:
  60. case EP92_AUDIO_INFO_ADO_INFO_FRAME_3:
  61. case EP92_AUDIO_INFO_ADO_INFO_FRAME_4:
  62. case EP92_AUDIO_INFO_ADO_INFO_FRAME_5:
  63. return true;
  64. default:
  65. return false;
  66. }
  67. }
  68. static bool ep92_writeable_registers(struct device *dev, unsigned int reg)
  69. {
  70. if (reg >= EP92_ISP_MODE_ENTER_ISP && reg <= EP92_GENERAL_CONTROL_4)
  71. return true;
  72. return false;
  73. }
  74. static bool ep92_readable_registers(struct device *dev, unsigned int reg)
  75. {
  76. if (reg >= EP92_BI_VENDOR_ID_0 && reg <= EP92_MAX_REGISTER_ADDR)
  77. return true;
  78. return false;
  79. }
  80. /* codec private data */
  81. struct ep92_pdata {
  82. struct regmap *regmap;
  83. struct snd_soc_codec *codec;
  84. struct timer_list timer;
  85. struct work_struct read_status_worker;
  86. int irq;
  87. int hyst_tx_plug;
  88. int hyst_link_on0;
  89. int hyst_link_on1;
  90. int hyst_link_on2;
  91. int filt_tx_plug;
  92. int filt_link_on0;
  93. int filt_link_on1;
  94. int filt_link_on2;
  95. struct {
  96. u8 tx_info;
  97. u8 video_latency;
  98. } gi; /* General Info block */
  99. struct {
  100. u8 ctl;
  101. u8 rx_sel;
  102. u8 ctl2;
  103. u8 cec_volume;
  104. u8 link;
  105. } gc; /* General Control block */
  106. struct {
  107. u8 system_status_0;
  108. u8 system_status_1;
  109. u8 audio_status;
  110. u8 cs[5];
  111. u8 cc;
  112. u8 ca;
  113. } ai; /* Audio Info block */
  114. u8 old_mode;
  115. #if IS_ENABLED(CONFIG_DEBUG_FS)
  116. struct dentry *debugfs_dir;
  117. struct dentry *debugfs_file_wo;
  118. struct dentry *debugfs_file_ro;
  119. #endif /* CONFIG_DEBUG_FS */
  120. };
  121. #if IS_ENABLED(CONFIG_DEBUG_FS)
  122. static int debugfs_codec_open_op(struct inode *inode, struct file *file)
  123. {
  124. file->private_data = inode->i_private;
  125. return 0;
  126. }
  127. static int debugfs_get_parameters(char *buf, u32 *param1, int num_of_par)
  128. {
  129. char *token;
  130. int base, cnt;
  131. token = strsep(&buf, " ");
  132. for (cnt = 0; cnt < num_of_par; cnt++) {
  133. if (token) {
  134. if ((token[1] == 'x') || (token[1] == 'X'))
  135. base = 16;
  136. else
  137. base = 10;
  138. if (kstrtou32(token, base, &param1[cnt]) != 0)
  139. return -EINVAL;
  140. token = strsep(&buf, " ");
  141. } else {
  142. return -EINVAL;
  143. }
  144. }
  145. return 0;
  146. }
  147. static ssize_t debugfs_codec_write_op(struct file *filp,
  148. const char __user *ubuf, size_t cnt, loff_t *ppos)
  149. {
  150. struct ep92_pdata *ep92 = (struct ep92_pdata *) filp->private_data;
  151. struct snd_soc_codec *codec = ep92->codec;
  152. char lbuf[32];
  153. int rc;
  154. u32 param[2];
  155. if (!filp || !ppos || !ubuf)
  156. return -EINVAL;
  157. if (cnt > sizeof(lbuf) - 1)
  158. return -EINVAL;
  159. rc = copy_from_user(lbuf, ubuf, cnt);
  160. if (rc)
  161. return -EFAULT;
  162. lbuf[cnt] = '\0';
  163. rc = debugfs_get_parameters(lbuf, param, 2);
  164. if ((param[0] < EP92_ISP_MODE_ENTER_ISP)
  165. || (param[0] > EP92_GENERAL_CONTROL_4)) {
  166. dev_err(codec->dev, "%s: reg address 0x%02X out of range\n",
  167. __func__, param[0]);
  168. return -EINVAL;
  169. }
  170. if ((param[1] < 0) || (param[1] > 255)) {
  171. dev_err(codec->dev, "%s: reg data 0x%02X out of range\n",
  172. __func__, param[1]);
  173. return -EINVAL;
  174. }
  175. if (rc == 0) {
  176. rc = cnt;
  177. dev_info(codec->dev, "%s: reg[0x%02X]=0x%02X\n",
  178. __func__, param[0], param[1]);
  179. snd_soc_write(codec, param[0], param[1]);
  180. } else {
  181. dev_err(codec->dev, "%s: write to register addr=0x%02X failed\n",
  182. __func__, param[0]);
  183. }
  184. return rc;
  185. }
  186. static ssize_t debugfs_ep92_reg_show(struct snd_soc_codec *codec,
  187. char __user *ubuf, size_t count, loff_t *ppos)
  188. {
  189. int i, reg_val, len;
  190. ssize_t total = 0;
  191. char tmp_buf[20];
  192. if (!ubuf || !ppos || !codec || *ppos < 0)
  193. return -EINVAL;
  194. for (i = (int) *ppos / 11; i <= EP92_MAX_REGISTER_ADDR; i++) {
  195. reg_val = snd_soc_read(codec, i);
  196. len = snprintf(tmp_buf, 20, "0x%02X: 0x%02X\n", i,
  197. (reg_val & 0xFF));
  198. if ((total + len) > count)
  199. break;
  200. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  201. dev_err(codec->dev, "%s: fail to copy reg dump\n",
  202. __func__);
  203. total = -EFAULT;
  204. goto copy_err;
  205. }
  206. *ppos += len;
  207. total += len;
  208. }
  209. copy_err:
  210. return total;
  211. }
  212. static ssize_t debugfs_codec_read_op(struct file *filp,
  213. char __user *ubuf, size_t cnt, loff_t *ppos)
  214. {
  215. struct ep92_pdata *ep92 = (struct ep92_pdata *) filp->private_data;
  216. struct snd_soc_codec *codec = ep92->codec;
  217. ssize_t ret_cnt;
  218. if (!filp || !ppos || !ubuf || *ppos < 0)
  219. return -EINVAL;
  220. ret_cnt = debugfs_ep92_reg_show(codec, ubuf, cnt, ppos);
  221. return ret_cnt;
  222. }
  223. static const struct file_operations debugfs_codec_ops = {
  224. .open = debugfs_codec_open_op,
  225. .write = debugfs_codec_write_op,
  226. .read = debugfs_codec_read_op,
  227. };
  228. #endif /* CONFIG_DEBUG_FS */
  229. static int ep92_send_uevent(struct ep92_pdata *ep92, char *event)
  230. {
  231. char *env[] = { event, NULL };
  232. if (!event || !ep92)
  233. return -EINVAL;
  234. return kobject_uevent_env(&ep92->codec->dev->kobj, KOBJ_CHANGE, env);
  235. }
  236. static int ep92_startup(struct snd_pcm_substream *substream,
  237. struct snd_soc_dai *dai)
  238. {
  239. return 0;
  240. }
  241. static void ep92_shutdown(struct snd_pcm_substream *substream,
  242. struct snd_soc_dai *dai)
  243. {
  244. }
  245. static int ep92_hw_params(struct snd_pcm_substream *substream,
  246. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  247. {
  248. return 0;
  249. }
  250. static struct snd_soc_dai_ops ep92_dai_ops = {
  251. .startup = ep92_startup,
  252. .shutdown = ep92_shutdown,
  253. .hw_params = ep92_hw_params,
  254. };
  255. static struct snd_soc_dai_driver ep92_dai[] = {
  256. {
  257. .name = "ep92-hdmi",
  258. .id = 1,
  259. .capture = {
  260. .stream_name = "HDMI Capture",
  261. .rate_max = 192000,
  262. .rate_min = 32000,
  263. .channels_min = 1,
  264. .channels_max = 8,
  265. .rates = EP92_RATES,
  266. .formats = EP92_FORMATS,
  267. },
  268. .ops = &ep92_dai_ops, /* callbacks */
  269. },
  270. {
  271. .name = "ep92-arc",
  272. .id = 2,
  273. .capture = {
  274. .stream_name = "ARC Capture",
  275. .rate_max = 192000,
  276. .rate_min = 32000,
  277. .channels_min = 1,
  278. .channels_max = 2,
  279. .rates = EP92_RATES,
  280. .formats = EP92_FORMATS,
  281. },
  282. .ops = &ep92_dai_ops, /* callbacks */
  283. },
  284. };
  285. static void ep92_read_general_control(struct snd_soc_codec *codec,
  286. struct ep92_pdata *ep92)
  287. {
  288. u8 old, change;
  289. int val;
  290. old = ep92->gi.tx_info;
  291. ep92->gi.tx_info = snd_soc_read(codec, EP92_BI_GENERAL_INFO_0);
  292. if (ep92->gi.tx_info == 0xff) {
  293. pr_debug("ep92 EP92_BI_GENERAL_INFO_0 read 0xff\n");
  294. ep92->gi.tx_info = old;
  295. }
  296. /* implement hysteresis to prevent events on glitches */
  297. if (ep92->gi.tx_info & EP92_GI_TX_HOT_PLUG_MASK) {
  298. if (ep92->hyst_tx_plug < EP92_HYST_CNT) {
  299. ep92->hyst_tx_plug++;
  300. if ((ep92->hyst_tx_plug == EP92_HYST_CNT) &&
  301. (ep92->filt_tx_plug == 0)) {
  302. ep92->filt_tx_plug = 1;
  303. pr_debug("ep92 out_plug changed to 1\n");
  304. ep92_send_uevent(ep92,
  305. "EP92EVT_OUT_PLUG=CONNECTED");
  306. }
  307. }
  308. } else {
  309. if (ep92->hyst_tx_plug > 0) {
  310. ep92->hyst_tx_plug--;
  311. if ((ep92->hyst_tx_plug == 0) &&
  312. (ep92->filt_tx_plug == 1)) {
  313. ep92->filt_tx_plug = 0;
  314. pr_debug("ep92 out_plug changed to 0\n");
  315. ep92_send_uevent(ep92,
  316. "EP92EVT_OUT_PLUG=DISCONNECTED");
  317. }
  318. }
  319. }
  320. old = ep92->gi.video_latency;
  321. ep92->gi.video_latency = snd_soc_read(codec, EP92_BI_GENERAL_INFO_4);
  322. if (ep92->gi.video_latency == 0xff) {
  323. pr_debug("ep92 EP92_BI_GENERAL_INFO_4 read 0xff\n");
  324. ep92->gi.video_latency = old;
  325. }
  326. change = ep92->gi.video_latency ^ old;
  327. if (change & EP92_GI_VIDEO_LATENCY_MASK) {
  328. val = ep92->gi.video_latency;
  329. if (val > 0)
  330. val = (val - 1) * 2;
  331. pr_debug("ep92 video latency changed to %d\n", val);
  332. ep92_send_uevent(ep92, "EP92EVT_VIDEO_LATENCY=CHANGED");
  333. }
  334. old = ep92->gc.ctl;
  335. ep92->gc.ctl = snd_soc_read(codec, EP92_GENERAL_CONTROL_0);
  336. if (ep92->gc.ctl == 0xff) {
  337. pr_debug("ep92 EP92_GENERAL_CONTROL_0 read 0xff\n");
  338. ep92->gc.ctl = old;
  339. }
  340. change = ep92->gc.ctl ^ old;
  341. if (change & EP92_GC_POWER_MASK) {
  342. val = (ep92->gc.ctl >> EP92_GC_POWER_SHIFT) &
  343. EP92_2CHOICE_MASK;
  344. pr_debug("ep92 power changed to %d\n", val);
  345. if (val)
  346. ep92_send_uevent(ep92, "EP92EVT_POWER=ON");
  347. else
  348. ep92_send_uevent(ep92, "EP92EVT_POWER=OFF");
  349. }
  350. if (change & EP92_GC_AUDIO_PATH_MASK) {
  351. val = (ep92->gc.ctl >> EP92_GC_AUDIO_PATH_SHIFT) &
  352. EP92_2CHOICE_MASK;
  353. pr_debug("ep92 audio_path changed to %d\n", val);
  354. if (val)
  355. ep92_send_uevent(ep92, "EP92EVT_AUDIO_PATH=TV");
  356. else
  357. ep92_send_uevent(ep92, "EP92EVT_AUDIO_PATH=SPEAKER");
  358. }
  359. if (change & EP92_GC_CEC_MUTE_MASK) {
  360. val = (ep92->gc.ctl >> EP92_GC_CEC_MUTE_SHIFT) &
  361. EP92_2CHOICE_MASK;
  362. pr_debug("ep92 cec_mute changed to %d\n", val);
  363. if (val)
  364. ep92_send_uevent(ep92, "EP92EVT_CEC_MUTE=NORMAL");
  365. else
  366. ep92_send_uevent(ep92, "EP92EVT_CEC_MUTE=MUTED");
  367. }
  368. if (change & EP92_GC_ARC_EN_MASK) {
  369. val = ep92->gc.ctl & EP92_2CHOICE_MASK;
  370. pr_debug("ep92 arc_en changed to %d\n", val);
  371. if (val)
  372. ep92_send_uevent(ep92, "EP92EVT_ARC_EN=ON");
  373. else
  374. ep92_send_uevent(ep92, "EP92EVT_ARC_EN=OFF");
  375. }
  376. old = ep92->gc.rx_sel;
  377. ep92->gc.rx_sel = snd_soc_read(codec, EP92_GENERAL_CONTROL_1);
  378. if (ep92->gc.rx_sel == 0xff) {
  379. pr_debug("ep92 EP92_GENERAL_CONTROL_1 read 0xff\n");
  380. ep92->gc.rx_sel = old;
  381. }
  382. change = ep92->gc.rx_sel ^ old;
  383. if (change & EP92_GC_RX_SEL_MASK) {
  384. val = ep92->gc.rx_sel & EP92_GC_RX_SEL_MASK;
  385. pr_debug("ep92 rx_sel changed to %d\n", val);
  386. ep92_send_uevent(ep92, "EP92EVT_SRC_SEL=CHANGED");
  387. }
  388. old = ep92->gc.cec_volume;
  389. ep92->gc.cec_volume = snd_soc_read(codec, EP92_GENERAL_CONTROL_3);
  390. if (ep92->gc.cec_volume == 0xff) {
  391. pr_debug("ep92 EP92_GENERAL_CONTROL_3 read 0xff\n");
  392. ep92->gc.cec_volume = old;
  393. }
  394. change = ep92->gc.cec_volume ^ old;
  395. if (change & EP92_GC_CEC_VOLUME_MASK) {
  396. val = ep92->gc.cec_volume & EP92_GC_CEC_VOLUME_MASK;
  397. pr_debug("ep92 cec_volume changed to %d\n", val);
  398. ep92_send_uevent(ep92, "EP92EVT_CEC_VOLUME=CHANGED");
  399. }
  400. old = ep92->gc.link;
  401. ep92->gc.link = snd_soc_read(codec, EP92_GENERAL_CONTROL_4);
  402. if (ep92->gc.link == 0xff) {
  403. pr_debug("ep92 EP92_GENERAL_CONTROL_4 read 0xff\n");
  404. ep92->gc.link = old;
  405. }
  406. /* implement hysteresis to prevent events on glitches */
  407. if (ep92->gc.link & EP92_GC_LINK_ON0_MASK) {
  408. if (ep92->hyst_link_on0 < EP92_HYST_CNT) {
  409. ep92->hyst_link_on0++;
  410. if ((ep92->hyst_link_on0 == EP92_HYST_CNT) &&
  411. (ep92->filt_link_on0 == 0)) {
  412. ep92->filt_link_on0 = 1;
  413. pr_debug("ep92 link_on0 changed to 1\n");
  414. ep92_send_uevent(ep92,
  415. "EP92EVT_LINK_ON0=CONNECTED");
  416. }
  417. }
  418. } else {
  419. if (ep92->hyst_link_on0 > 0) {
  420. ep92->hyst_link_on0--;
  421. if ((ep92->hyst_link_on0 == 0) &&
  422. (ep92->filt_link_on0 == 1)) {
  423. ep92->filt_link_on0 = 0;
  424. pr_debug("ep92 link_on0 changed to 0\n");
  425. ep92_send_uevent(ep92,
  426. "EP92EVT_LINK_ON0=DISCONNECTED");
  427. }
  428. }
  429. }
  430. /* implement hysteresis to prevent events on glitches */
  431. if (ep92->gc.link & EP92_GC_LINK_ON1_MASK) {
  432. if (ep92->hyst_link_on1 < EP92_HYST_CNT) {
  433. ep92->hyst_link_on1++;
  434. if ((ep92->hyst_link_on1 == EP92_HYST_CNT) &&
  435. (ep92->filt_link_on1 == 0)) {
  436. ep92->filt_link_on1 = 1;
  437. pr_debug("ep92 link_on1 changed to 1\n");
  438. ep92_send_uevent(ep92,
  439. "EP92EVT_LINK_ON1=CONNECTED");
  440. }
  441. }
  442. } else {
  443. if (ep92->hyst_link_on1 > 0) {
  444. ep92->hyst_link_on1--;
  445. if ((ep92->hyst_link_on1 == 0) &&
  446. (ep92->filt_link_on1 == 1)) {
  447. ep92->filt_link_on1 = 0;
  448. pr_debug("ep92 link_on1 changed to 0\n");
  449. ep92_send_uevent(ep92,
  450. "EP92EVT_LINK_ON1=DISCONNECTED");
  451. }
  452. }
  453. }
  454. /* implement hysteresis to prevent events on glitches */
  455. if (ep92->gc.link & EP92_GC_LINK_ON2_MASK) {
  456. if (ep92->hyst_link_on2 < EP92_HYST_CNT) {
  457. ep92->hyst_link_on2++;
  458. if ((ep92->hyst_link_on2 == EP92_HYST_CNT) &&
  459. (ep92->filt_link_on2 == 0)) {
  460. ep92->filt_link_on2 = 1;
  461. pr_debug("ep92 link_on2 changed to 1\n");
  462. ep92_send_uevent(ep92,
  463. "EP92EVT_LINK_ON2=CONNECTED");
  464. }
  465. }
  466. } else {
  467. if (ep92->hyst_link_on2 > 0) {
  468. ep92->hyst_link_on2--;
  469. if ((ep92->hyst_link_on2 == 0) &&
  470. (ep92->filt_link_on2 == 1)) {
  471. ep92->filt_link_on2 = 0;
  472. pr_debug("ep92 link_on2 changed to 0\n");
  473. ep92_send_uevent(ep92,
  474. "EP92EVT_LINK_ON2=DISCONNECTED");
  475. }
  476. }
  477. }
  478. }
  479. static void ep92_read_audio_info(struct snd_soc_codec *codec,
  480. struct ep92_pdata *ep92)
  481. {
  482. u8 old, change;
  483. u8 new_mode;
  484. bool send_uevent = false;
  485. old = ep92->ai.system_status_0;
  486. ep92->ai.system_status_0 = snd_soc_read(codec,
  487. EP92_AUDIO_INFO_SYSTEM_STATUS_0);
  488. if (ep92->ai.system_status_0 == 0xff) {
  489. pr_debug("ep92 EP92_AUDIO_INFO_SYSTEM_STATUS_0 read 0xff\n");
  490. ep92->ai.system_status_0 = old;
  491. }
  492. change = ep92->ai.system_status_0 ^ old;
  493. if (change & EP92_AI_MCLK_ON_MASK) {
  494. pr_debug("ep92 status changed to %d\n",
  495. (ep92->ai.system_status_0 >> EP92_AI_MCLK_ON_SHIFT) &
  496. EP92_2CHOICE_MASK);
  497. send_uevent = true;
  498. }
  499. if (change & EP92_AI_AVMUTE_MASK) {
  500. pr_debug("ep92 avmute changed to %d\n",
  501. (ep92->ai.system_status_0 >> EP92_AI_AVMUTE_SHIFT) &
  502. EP92_2CHOICE_MASK);
  503. send_uevent = true;
  504. }
  505. if (change & EP92_AI_LAYOUT_MASK) {
  506. pr_debug("ep92 layout changed to %d\n",
  507. (ep92->ai.system_status_0) & EP92_2CHOICE_MASK);
  508. send_uevent = true;
  509. }
  510. old = ep92->ai.audio_status;
  511. ep92->ai.audio_status = snd_soc_read(codec,
  512. EP92_AUDIO_INFO_AUDIO_STATUS);
  513. if (ep92->ai.audio_status == 0xff) {
  514. pr_debug("ep92 EP92_AUDIO_INFO_AUDIO_STATUS read 0xff\n");
  515. ep92->ai.audio_status = old;
  516. }
  517. change = ep92->ai.audio_status ^ old;
  518. if (change & EP92_AI_RATE_MASK) {
  519. pr_debug("ep92 rate changed to %d\n",
  520. ep92_samp_freq_table[(ep92->ai.audio_status) &
  521. EP92_AI_RATE_MASK]);
  522. send_uevent = true;
  523. }
  524. new_mode = ep92->old_mode;
  525. if (ep92->ai.audio_status & EP92_AI_STD_ADO_MASK) {
  526. old = ep92->ai.cs[0];
  527. ep92->ai.cs[0] = snd_soc_read(codec,
  528. EP92_AUDIO_INFO_CHANNEL_STATUS_0);
  529. if (ep92->ai.cs[0] == 0xff) {
  530. pr_debug("ep92 EP92_AUDIO_INFO_CHANNEL_STATUS_0 read 0xff\n");
  531. ep92->ai.cs[0] = old;
  532. }
  533. if (ep92->ai.cs[0] & EP92_AI_NPCM_MASK)
  534. new_mode = 1; /* Compr */
  535. else
  536. new_mode = 0; /* LPCM */
  537. } else if (ep92->ai.audio_status & EP92_AI_HBR_ADO_MASK)
  538. new_mode = 1; /* Compr */
  539. if (ep92->old_mode != new_mode) {
  540. pr_debug("ep92 mode changed to %d\n", new_mode);
  541. send_uevent = true;
  542. }
  543. ep92->old_mode = new_mode;
  544. old = ep92->ai.cc;
  545. ep92->ai.cc = snd_soc_read(codec, EP92_AUDIO_INFO_ADO_INFO_FRAME_1);
  546. if (ep92->ai.cc == 0xff) {
  547. pr_debug("ep92 EP92_AUDIO_INFO_ADO_INFO_FRAME_1 read 0xff\n");
  548. ep92->ai.cc = old;
  549. }
  550. change = ep92->ai.cc ^ old;
  551. if (change & EP92_AI_CH_COUNT_MASK) {
  552. pr_debug("ep92 ch_count changed to %d (%d)\n",
  553. ep92->ai.cc & EP92_AI_CH_COUNT_MASK,
  554. (ep92->ai.cc & EP92_AI_CH_COUNT_MASK) == 0 ? 0 :
  555. (ep92->ai.cc & EP92_AI_CH_COUNT_MASK) + 1);
  556. send_uevent = true;
  557. }
  558. old = ep92->ai.ca;
  559. ep92->ai.ca = snd_soc_read(codec, EP92_AUDIO_INFO_ADO_INFO_FRAME_4);
  560. if (ep92->ai.ca == 0xff) {
  561. pr_debug("ep92 EP92_AUDIO_INFO_ADO_INFO_FRAME_4 read 0xff\n");
  562. ep92->ai.ca = old;
  563. }
  564. change = ep92->ai.ca ^ old;
  565. if (change & EP92_AI_CH_ALLOC_MASK) {
  566. pr_debug("ep92 ch_alloc changed to 0x%02x\n",
  567. (ep92->ai.ca) & EP92_AI_CH_ALLOC_MASK);
  568. send_uevent = true;
  569. }
  570. if (send_uevent)
  571. ep92_send_uevent(ep92, "EP92EVT_AUDIO=MEDIA_CONFIG_CHANGE");
  572. }
  573. static void ep92_init(struct snd_soc_codec *codec, struct ep92_pdata *ep92)
  574. {
  575. /* update the format information in mixer controls */
  576. ep92_read_general_control(codec, ep92);
  577. ep92_read_audio_info(codec, ep92);
  578. }
  579. static int ep92_probe(struct snd_soc_codec *codec)
  580. {
  581. struct ep92_pdata *ep92 = snd_soc_codec_get_drvdata(codec);
  582. ep92->codec = codec;
  583. ep92_init(codec, ep92);
  584. return 0;
  585. }
  586. static int ep92_remove(struct snd_soc_codec *codec)
  587. {
  588. return 0;
  589. }
  590. static struct regmap *ep92_get_regmap(struct device *dev)
  591. {
  592. struct ep92_pdata *ep92_ctrl = dev_get_drvdata(dev);
  593. if (!ep92_ctrl)
  594. return NULL;
  595. return ep92_ctrl->regmap;
  596. }
  597. static struct snd_soc_codec_driver soc_codec_drv_ep92 = {
  598. .probe = ep92_probe,
  599. .remove = ep92_remove,
  600. .get_regmap = ep92_get_regmap,
  601. };
  602. static struct regmap_config ep92_regmap_config = {
  603. .reg_bits = 8,
  604. .val_bits = 8,
  605. .cache_type = REGCACHE_RBTREE,
  606. .reg_defaults = ep92_reg_defaults,
  607. .num_reg_defaults = ARRAY_SIZE(ep92_reg_defaults),
  608. .max_register = EP92_MAX_REGISTER_ADDR,
  609. .volatile_reg = ep92_volatile_register,
  610. .writeable_reg = ep92_writeable_registers,
  611. .readable_reg = ep92_readable_registers,
  612. };
  613. void ep92_read_status(struct work_struct *work)
  614. {
  615. struct ep92_pdata *ep92 = container_of(work, struct ep92_pdata,
  616. read_status_worker);
  617. struct snd_soc_codec *codec = ep92->codec;
  618. u8 val;
  619. /* No polling before codec is initialized */
  620. if (codec == NULL)
  621. return;
  622. /* check ADO_CHF that is set when audio format has changed */
  623. val = snd_soc_read(codec, EP92_BI_GENERAL_INFO_1);
  624. if (val == 0xff) {
  625. /* workaround for Nak'ed first read */
  626. val = snd_soc_read(codec, EP92_BI_GENERAL_INFO_1);
  627. if (val == 0xff)
  628. return; /* assume device not present */
  629. }
  630. if (val & EP92_GI_ADO_CHF_MASK)
  631. pr_debug("ep92 audio mode change trigger.\n");
  632. if (val & EP92_GI_CEC_ECF_MASK)
  633. pr_debug("ep92 CEC change trigger.\n");
  634. /* check for general control changes */
  635. ep92_read_general_control(codec, ep92);
  636. /* update the format information in mixer controls */
  637. ep92_read_audio_info(codec, ep92);
  638. }
  639. static irqreturn_t ep92_irq(int irq, void *data)
  640. {
  641. struct ep92_pdata *ep92 = data;
  642. struct snd_soc_codec *codec = ep92->codec;
  643. /* Treat interrupt before codec is initialized as spurious */
  644. if (codec == NULL)
  645. return IRQ_NONE;
  646. dev_dbg(codec->dev, "ep92_interrupt\n");
  647. schedule_work(&ep92->read_status_worker);
  648. return IRQ_HANDLED;
  649. };
  650. void ep92_poll_status(unsigned long data)
  651. {
  652. struct ep92_pdata *ep92 = (struct ep92_pdata *)data;
  653. u32 poll_msec;
  654. if ((ep92->gc.ctl & EP92_GC_POWER_MASK) == 0)
  655. poll_msec = EP92_POLL_INTERVAL_OFF_MSEC;
  656. else
  657. poll_msec = EP92_POLL_INTERVAL_ON_MSEC;
  658. mod_timer(&ep92->timer, jiffies + msecs_to_jiffies(poll_msec));
  659. schedule_work(&ep92->read_status_worker);
  660. }
  661. static const struct of_device_id ep92_of_match[] = {
  662. { .compatible = "explore,ep92a6", },
  663. { }
  664. };
  665. MODULE_DEVICE_TABLE(of, ep92_of_match);
  666. static ssize_t ep92_sysfs_rda_audio_state(struct device *dev,
  667. struct device_attribute *attr, char *buf)
  668. {
  669. ssize_t ret;
  670. int val;
  671. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  672. if (!ep92) {
  673. pr_err("%s: invalid input\n", __func__);
  674. return -EINVAL;
  675. }
  676. val = (ep92->ai.system_status_0 & EP92_AI_MCLK_ON_MASK) >>
  677. EP92_AI_MCLK_ON_SHIFT;
  678. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  679. pr_debug("%s: '%d'\n", __func__, val);
  680. return ret;
  681. }
  682. static ssize_t ep92_sysfs_rda_audio_format(struct device *dev,
  683. struct device_attribute *attr, char *buf)
  684. {
  685. ssize_t ret;
  686. int val;
  687. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  688. if (!ep92) {
  689. pr_err("%s: invalid input\n", __func__);
  690. return -EINVAL;
  691. }
  692. val = ep92->old_mode;
  693. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  694. pr_debug("%s: '%d'\n", __func__, val);
  695. return ret;
  696. }
  697. static ssize_t ep92_sysfs_rda_audio_rate(struct device *dev,
  698. struct device_attribute *attr, char *buf)
  699. {
  700. ssize_t ret;
  701. int val;
  702. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  703. if (!ep92) {
  704. pr_err("%s: invalid input\n", __func__);
  705. return -EINVAL;
  706. }
  707. val = ep92_samp_freq_table[(ep92->ai.audio_status) &
  708. EP92_AI_RATE_MASK];
  709. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  710. pr_debug("%s: '%d'\n", __func__, val);
  711. return ret;
  712. }
  713. static ssize_t ep92_sysfs_rda_audio_layout(struct device *dev,
  714. struct device_attribute *attr, char *buf)
  715. {
  716. ssize_t ret;
  717. int val;
  718. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  719. if (!ep92) {
  720. pr_err("%s: invalid input\n", __func__);
  721. return -EINVAL;
  722. }
  723. val = (ep92->ai.system_status_0 & EP92_AI_LAYOUT_MASK) >>
  724. EP92_AI_LAYOUT_SHIFT;
  725. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  726. pr_debug("%s: '%d'\n", __func__, val);
  727. return ret;
  728. }
  729. static ssize_t ep92_sysfs_rda_audio_ch_count(struct device *dev,
  730. struct device_attribute *attr, char *buf)
  731. {
  732. ssize_t ret;
  733. int val;
  734. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  735. if (!ep92) {
  736. pr_err("%s: invalid input\n", __func__);
  737. return -EINVAL;
  738. }
  739. val = ep92->ai.cc & EP92_AI_CH_COUNT_MASK;
  740. /* mapping is ch_count = reg_val + 1, with exception: 0 = unknown */
  741. if (val > 0)
  742. val += 1;
  743. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  744. pr_debug("%s: '%d'\n", __func__, val);
  745. return ret;
  746. }
  747. static ssize_t ep92_sysfs_rda_audio_ch_alloc(struct device *dev,
  748. struct device_attribute *attr, char *buf)
  749. {
  750. ssize_t ret;
  751. int val;
  752. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  753. if (!ep92) {
  754. pr_err("%s: invalid input\n", __func__);
  755. return -EINVAL;
  756. }
  757. val = ep92->ai.ca & EP92_AI_CH_ALLOC_MASK;
  758. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  759. pr_debug("%s: '%d'\n", __func__, val);
  760. return ret;
  761. }
  762. static ssize_t ep92_sysfs_rda_avmute(struct device *dev,
  763. struct device_attribute *attr, char *buf)
  764. {
  765. ssize_t ret;
  766. int val;
  767. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  768. if (!ep92) {
  769. pr_err("%s: invalid input\n", __func__);
  770. return -EINVAL;
  771. }
  772. val = (ep92->ai.system_status_0 >> EP92_AI_AVMUTE_SHIFT) &
  773. EP92_2CHOICE_MASK;
  774. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  775. pr_debug("%s: '%d'\n", __func__, val);
  776. return ret;
  777. }
  778. static ssize_t ep92_sysfs_rda_link_on0(struct device *dev,
  779. struct device_attribute *attr, char *buf)
  780. {
  781. ssize_t ret;
  782. int val;
  783. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  784. if (!ep92) {
  785. pr_err("%s: invalid input\n", __func__);
  786. return -EINVAL;
  787. }
  788. val = ep92->filt_link_on0;
  789. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  790. pr_debug("%s: '%d'\n", __func__, val);
  791. return ret;
  792. }
  793. static ssize_t ep92_sysfs_rda_link_on1(struct device *dev,
  794. struct device_attribute *attr, char *buf)
  795. {
  796. ssize_t ret;
  797. int val;
  798. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  799. if (!ep92) {
  800. pr_err("%s: invalid input\n", __func__);
  801. return -EINVAL;
  802. }
  803. val = ep92->filt_link_on1;
  804. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  805. pr_debug("%s: '%d'\n", __func__, val);
  806. return ret;
  807. }
  808. static ssize_t ep92_sysfs_rda_link_on2(struct device *dev,
  809. struct device_attribute *attr, char *buf)
  810. {
  811. ssize_t ret;
  812. int val;
  813. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  814. if (!ep92) {
  815. pr_err("%s: invalid input\n", __func__);
  816. return -EINVAL;
  817. }
  818. val = ep92->filt_link_on2;
  819. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  820. pr_debug("%s: '%d'\n", __func__, val);
  821. return ret;
  822. }
  823. static ssize_t ep92_sysfs_rda_out_plug(struct device *dev,
  824. struct device_attribute *attr, char *buf)
  825. {
  826. ssize_t ret;
  827. int val;
  828. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  829. if (!ep92) {
  830. pr_err("%s: invalid input\n", __func__);
  831. return -EINVAL;
  832. }
  833. val = ep92->filt_tx_plug;
  834. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  835. pr_debug("%s: '%d'\n", __func__, val);
  836. return ret;
  837. }
  838. static ssize_t ep92_sysfs_rda_video_latency(struct device *dev,
  839. struct device_attribute *attr, char *buf)
  840. {
  841. ssize_t ret;
  842. int val;
  843. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  844. if (!ep92) {
  845. pr_err("%s: invalid input\n", __func__);
  846. return -EINVAL;
  847. }
  848. val = ep92->gi.video_latency & EP92_GI_VIDEO_LATENCY_MASK;
  849. if (val > 0)
  850. val = (val - 1) * 2;
  851. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  852. pr_debug("%s: '%d'\n", __func__, val);
  853. return ret;
  854. }
  855. static ssize_t ep92_sysfs_rda_arc_disable(struct device *dev,
  856. struct device_attribute *attr, char *buf)
  857. {
  858. ssize_t ret;
  859. int val;
  860. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  861. if (!ep92) {
  862. pr_err("%s: invalid input\n", __func__);
  863. return -EINVAL;
  864. }
  865. val = (ep92->gc.ctl2 >> EP92_GC_ARC_DIS_SHIFT) &
  866. EP92_2CHOICE_MASK;
  867. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  868. pr_debug("%s: '%d'\n", __func__, val);
  869. return ret;
  870. }
  871. static ssize_t ep92_sysfs_wta_arc_disable(struct device *dev,
  872. struct device_attribute *attr, const char *buf, size_t count)
  873. {
  874. int reg, val, rc;
  875. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  876. if (!ep92) {
  877. pr_err("%s: invalid input\n", __func__);
  878. return -EINVAL;
  879. }
  880. rc = kstrtoint(buf, 10, &val);
  881. if (rc) {
  882. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  883. goto end;
  884. }
  885. if ((val < 0) || (val > 1)) {
  886. pr_err("%s: value out of range.\n", __func__);
  887. rc = -EINVAL;
  888. goto end;
  889. }
  890. reg = snd_soc_read(ep92->codec, EP92_GENERAL_CONTROL_2);
  891. reg &= ~EP92_GC_ARC_DIS_MASK;
  892. reg |= ((val << EP92_GC_ARC_DIS_SHIFT) & EP92_GC_ARC_DIS_MASK);
  893. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_2, reg);
  894. ep92->gc.ctl2 &= ~EP92_GC_ARC_DIS_MASK;
  895. ep92->gc.ctl2 |= (val << EP92_GC_ARC_DIS_SHIFT) & EP92_GC_ARC_DIS_MASK;
  896. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  897. end:
  898. return rc;
  899. }
  900. static ssize_t ep92_sysfs_rda_power(struct device *dev,
  901. struct device_attribute *attr, char *buf)
  902. {
  903. ssize_t ret;
  904. int val;
  905. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  906. if (!ep92) {
  907. pr_err("%s: invalid input\n", __func__);
  908. return -EINVAL;
  909. }
  910. val = (ep92->gc.ctl >> EP92_GC_POWER_SHIFT) & EP92_2CHOICE_MASK;
  911. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  912. pr_debug("%s: '%d'\n", __func__, val);
  913. return ret;
  914. }
  915. static ssize_t ep92_sysfs_wta_power(struct device *dev,
  916. struct device_attribute *attr, const char *buf, size_t count)
  917. {
  918. int reg, val, rc;
  919. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  920. if (!ep92) {
  921. pr_err("%s: invalid input\n", __func__);
  922. return -EINVAL;
  923. }
  924. rc = kstrtoint(buf, 10, &val);
  925. if (rc) {
  926. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  927. goto end;
  928. }
  929. if ((val < 0) || (val > 1)) {
  930. pr_err("%s: value out of range.\n", __func__);
  931. rc = -EINVAL;
  932. goto end;
  933. }
  934. reg = snd_soc_read(ep92->codec, EP92_GENERAL_CONTROL_0);
  935. reg &= ~EP92_GC_POWER_MASK;
  936. reg |= (val << EP92_GC_POWER_SHIFT) & EP92_GC_POWER_MASK;
  937. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_0, reg);
  938. ep92->gc.ctl &= ~EP92_GC_POWER_MASK;
  939. ep92->gc.ctl |= (val << EP92_GC_POWER_SHIFT) & EP92_GC_POWER_MASK;
  940. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  941. end:
  942. return rc;
  943. }
  944. static ssize_t ep92_sysfs_rda_audio_path(struct device *dev,
  945. struct device_attribute *attr, char *buf)
  946. {
  947. ssize_t ret;
  948. int val;
  949. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  950. if (!ep92) {
  951. pr_err("%s: invalid input\n", __func__);
  952. return -EINVAL;
  953. }
  954. val = (ep92->gc.ctl >> EP92_GC_AUDIO_PATH_SHIFT) & EP92_2CHOICE_MASK;
  955. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  956. pr_debug("%s: '%d'\n", __func__, val);
  957. return ret;
  958. }
  959. static ssize_t ep92_sysfs_wta_audio_path(struct device *dev,
  960. struct device_attribute *attr, const char *buf, size_t count)
  961. {
  962. int reg, val, rc;
  963. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  964. if (!ep92) {
  965. pr_err("%s: invalid input\n", __func__);
  966. return -EINVAL;
  967. }
  968. rc = kstrtoint(buf, 10, &val);
  969. if (rc) {
  970. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  971. goto end;
  972. }
  973. if ((val < 0) || (val > 1)) {
  974. pr_err("%s: value out of range.\n", __func__);
  975. rc = -EINVAL;
  976. goto end;
  977. }
  978. reg = snd_soc_read(ep92->codec, EP92_GENERAL_CONTROL_0);
  979. reg &= ~EP92_GC_AUDIO_PATH_MASK;
  980. reg |= (val << EP92_GC_AUDIO_PATH_SHIFT) & EP92_GC_AUDIO_PATH_MASK;
  981. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_0, reg);
  982. ep92->gc.ctl &= ~EP92_GC_AUDIO_PATH_MASK;
  983. ep92->gc.ctl |= (val << EP92_GC_AUDIO_PATH_SHIFT) &
  984. EP92_GC_AUDIO_PATH_MASK;
  985. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  986. end:
  987. return rc;
  988. }
  989. static ssize_t ep92_sysfs_rda_src_sel(struct device *dev,
  990. struct device_attribute *attr, char *buf)
  991. {
  992. ssize_t ret;
  993. int val;
  994. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  995. if (!ep92) {
  996. pr_err("%s: invalid input\n", __func__);
  997. return -EINVAL;
  998. }
  999. val = ep92->gc.rx_sel & EP92_GC_RX_SEL_MASK;
  1000. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  1001. pr_debug("%s: '%d'\n", __func__, val);
  1002. return ret;
  1003. }
  1004. static ssize_t ep92_sysfs_wta_src_sel(struct device *dev,
  1005. struct device_attribute *attr, const char *buf, size_t count)
  1006. {
  1007. int reg, val, rc;
  1008. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1009. if (!ep92) {
  1010. pr_err("%s: invalid input\n", __func__);
  1011. return -EINVAL;
  1012. }
  1013. rc = kstrtoint(buf, 10, &val);
  1014. if (rc) {
  1015. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1016. goto end;
  1017. }
  1018. if ((val < 0) || (val > 7)) {
  1019. pr_err("%s: value out of range.\n", __func__);
  1020. rc = -EINVAL;
  1021. goto end;
  1022. }
  1023. reg = snd_soc_read(ep92->codec, EP92_GENERAL_CONTROL_1);
  1024. reg &= ~EP92_GC_RX_SEL_MASK;
  1025. reg |= (val << EP92_GC_RX_SEL_SHIFT) & EP92_GC_RX_SEL_MASK;
  1026. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_1, reg);
  1027. ep92->gc.rx_sel &= ~EP92_GC_RX_SEL_MASK;
  1028. ep92->gc.rx_sel |= (val << EP92_GC_RX_SEL_SHIFT) & EP92_GC_RX_SEL_MASK;
  1029. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  1030. end:
  1031. return rc;
  1032. }
  1033. static ssize_t ep92_sysfs_rda_arc_enable(struct device *dev,
  1034. struct device_attribute *attr, char *buf)
  1035. {
  1036. ssize_t ret;
  1037. int val;
  1038. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1039. if (!ep92) {
  1040. pr_err("%s: invalid input\n", __func__);
  1041. return -EINVAL;
  1042. }
  1043. val = (ep92->gc.ctl >> EP92_GC_ARC_EN_SHIFT) & EP92_2CHOICE_MASK;
  1044. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  1045. pr_debug("%s: '%d'\n", __func__, val);
  1046. return ret;
  1047. }
  1048. static ssize_t ep92_sysfs_wta_arc_enable(struct device *dev,
  1049. struct device_attribute *attr, const char *buf, size_t count)
  1050. {
  1051. int reg, val, rc;
  1052. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1053. if (!ep92) {
  1054. pr_err("%s: invalid input\n", __func__);
  1055. return -EINVAL;
  1056. }
  1057. rc = kstrtoint(buf, 10, &val);
  1058. if (rc) {
  1059. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1060. goto end;
  1061. }
  1062. if ((val < 0) || (val > 1)) {
  1063. pr_err("%s: value out of range.\n", __func__);
  1064. rc = -EINVAL;
  1065. goto end;
  1066. }
  1067. reg = snd_soc_read(ep92->codec, EP92_GENERAL_CONTROL_0);
  1068. reg &= ~EP92_GC_AUDIO_PATH_MASK;
  1069. reg |= (val << EP92_GC_AUDIO_PATH_SHIFT) & EP92_GC_AUDIO_PATH_MASK;
  1070. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_0, reg);
  1071. ep92->gc.ctl &= ~EP92_GC_AUDIO_PATH_MASK;
  1072. ep92->gc.ctl |= (val << EP92_GC_AUDIO_PATH_SHIFT) &
  1073. EP92_GC_AUDIO_PATH_MASK;
  1074. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  1075. end:
  1076. return rc;
  1077. }
  1078. static ssize_t ep92_sysfs_rda_cec_mute(struct device *dev,
  1079. struct device_attribute *attr, char *buf)
  1080. {
  1081. ssize_t ret;
  1082. int val;
  1083. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1084. if (!ep92) {
  1085. pr_err("%s: invalid input\n", __func__);
  1086. return -EINVAL;
  1087. }
  1088. val = (ep92->gc.ctl >> EP92_GC_CEC_MUTE_SHIFT) & EP92_2CHOICE_MASK;
  1089. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  1090. pr_debug("%s: '%d'\n", __func__, val);
  1091. return ret;
  1092. }
  1093. static ssize_t ep92_sysfs_wta_cec_mute(struct device *dev,
  1094. struct device_attribute *attr, const char *buf, size_t count)
  1095. {
  1096. int reg, val, rc;
  1097. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1098. if (!ep92) {
  1099. pr_err("%s: invalid input\n", __func__);
  1100. return -EINVAL;
  1101. }
  1102. rc = kstrtoint(buf, 10, &val);
  1103. if (rc) {
  1104. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1105. goto end;
  1106. }
  1107. if ((val < 0) || (val > 1)) {
  1108. pr_err("%s: value out of range.\n", __func__);
  1109. rc = -EINVAL;
  1110. goto end;
  1111. }
  1112. reg = snd_soc_read(ep92->codec, EP92_GENERAL_CONTROL_0);
  1113. reg &= ~EP92_GC_CEC_MUTE_MASK;
  1114. reg |= (val << EP92_GC_CEC_MUTE_SHIFT) & EP92_GC_CEC_MUTE_MASK;
  1115. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_0, reg);
  1116. ep92->gc.ctl &= ~EP92_GC_CEC_MUTE_MASK;
  1117. ep92->gc.ctl |= (val << EP92_GC_CEC_MUTE_SHIFT) &
  1118. EP92_GC_CEC_MUTE_MASK;
  1119. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  1120. end:
  1121. return rc;
  1122. }
  1123. static ssize_t ep92_sysfs_rda_cec_volume(struct device *dev,
  1124. struct device_attribute *attr, char *buf)
  1125. {
  1126. ssize_t ret;
  1127. int val;
  1128. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1129. if (!ep92) {
  1130. pr_err("%s: invalid input\n", __func__);
  1131. return -EINVAL;
  1132. }
  1133. val = (ep92->gc.cec_volume >> EP92_GC_CEC_VOLUME_SHIFT) &
  1134. EP92_GC_CEC_VOLUME_MASK;
  1135. ret = snprintf(buf, EP92_SYSFS_ENTRY_MAX_LEN, "%d\n", val);
  1136. pr_debug("%s: '%d'\n", __func__, val);
  1137. return ret;
  1138. }
  1139. static ssize_t ep92_sysfs_wta_cec_volume(struct device *dev,
  1140. struct device_attribute *attr, const char *buf, size_t count)
  1141. {
  1142. int reg, val, rc;
  1143. struct ep92_pdata *ep92 = dev_get_drvdata(dev);
  1144. if (!ep92) {
  1145. pr_err("%s: invalid input\n", __func__);
  1146. return -EINVAL;
  1147. }
  1148. rc = kstrtoint(buf, 10, &val);
  1149. if (rc) {
  1150. pr_err("%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1151. goto end;
  1152. }
  1153. if ((val < 0) || (val > EP92_GC_CEC_VOLUME_MAX)) {
  1154. pr_err("%s: value out of range.\n", __func__);
  1155. rc = -EINVAL;
  1156. goto end;
  1157. }
  1158. reg = val & EP92_GC_CEC_VOLUME_MASK;
  1159. snd_soc_write(ep92->codec, EP92_GENERAL_CONTROL_3, reg);
  1160. ep92->gc.cec_volume = val & EP92_GC_CEC_VOLUME_MASK;
  1161. rc = strnlen(buf, EP92_SYSFS_ENTRY_MAX_LEN);
  1162. end:
  1163. return rc;
  1164. }
  1165. static DEVICE_ATTR(audio_state, 0444, ep92_sysfs_rda_audio_state, NULL);
  1166. static DEVICE_ATTR(audio_format, 0444, ep92_sysfs_rda_audio_format, NULL);
  1167. static DEVICE_ATTR(audio_rate, 0444, ep92_sysfs_rda_audio_rate, NULL);
  1168. static DEVICE_ATTR(audio_layout, 0444, ep92_sysfs_rda_audio_layout, NULL);
  1169. static DEVICE_ATTR(audio_ch_count, 0444, ep92_sysfs_rda_audio_ch_count, NULL);
  1170. static DEVICE_ATTR(audio_ch_alloc, 0444, ep92_sysfs_rda_audio_ch_alloc, NULL);
  1171. static DEVICE_ATTR(audio_avmute, 0444, ep92_sysfs_rda_avmute, NULL);
  1172. static DEVICE_ATTR(link_on0, 0444, ep92_sysfs_rda_link_on0, NULL);
  1173. static DEVICE_ATTR(link_on1, 0444, ep92_sysfs_rda_link_on1, NULL);
  1174. static DEVICE_ATTR(link_on2, 0444, ep92_sysfs_rda_link_on2, NULL);
  1175. static DEVICE_ATTR(out_plug, 0444, ep92_sysfs_rda_out_plug, NULL);
  1176. static DEVICE_ATTR(video_latency, 0444, ep92_sysfs_rda_video_latency, NULL);
  1177. static DEVICE_ATTR(arc_disable, 0644, ep92_sysfs_rda_arc_disable,
  1178. ep92_sysfs_wta_arc_disable);
  1179. static DEVICE_ATTR(power_on, 0644, ep92_sysfs_rda_power, ep92_sysfs_wta_power);
  1180. static DEVICE_ATTR(audio_path, 0644, ep92_sysfs_rda_audio_path,
  1181. ep92_sysfs_wta_audio_path);
  1182. static DEVICE_ATTR(src_sel, 0644, ep92_sysfs_rda_src_sel,
  1183. ep92_sysfs_wta_src_sel);
  1184. static DEVICE_ATTR(arc_enable, 0644, ep92_sysfs_rda_arc_enable,
  1185. ep92_sysfs_wta_arc_enable);
  1186. static DEVICE_ATTR(cec_mute, 0644, ep92_sysfs_rda_cec_mute,
  1187. ep92_sysfs_wta_cec_mute);
  1188. static DEVICE_ATTR(cec_volume, 0644, ep92_sysfs_rda_cec_volume,
  1189. ep92_sysfs_wta_cec_volume);
  1190. static struct attribute *ep92_fs_attrs[] = {
  1191. &dev_attr_audio_state.attr,
  1192. &dev_attr_audio_format.attr,
  1193. &dev_attr_audio_rate.attr,
  1194. &dev_attr_audio_layout.attr,
  1195. &dev_attr_audio_ch_count.attr,
  1196. &dev_attr_audio_ch_alloc.attr,
  1197. &dev_attr_audio_avmute.attr,
  1198. &dev_attr_link_on0.attr,
  1199. &dev_attr_link_on1.attr,
  1200. &dev_attr_link_on2.attr,
  1201. &dev_attr_out_plug.attr,
  1202. &dev_attr_video_latency.attr,
  1203. &dev_attr_arc_disable.attr,
  1204. &dev_attr_power_on.attr,
  1205. &dev_attr_audio_path.attr,
  1206. &dev_attr_src_sel.attr,
  1207. &dev_attr_arc_enable.attr,
  1208. &dev_attr_cec_mute.attr,
  1209. &dev_attr_cec_volume.attr,
  1210. NULL,
  1211. };
  1212. static struct attribute_group ep92_fs_attrs_group = {
  1213. .attrs = ep92_fs_attrs,
  1214. };
  1215. static int ep92_sysfs_create(struct i2c_client *client,
  1216. struct ep92_pdata *ep92)
  1217. {
  1218. int rc;
  1219. rc = sysfs_create_group(&client->dev.kobj, &ep92_fs_attrs_group);
  1220. return rc;
  1221. }
  1222. static void ep92_sysfs_remove(struct i2c_client *client,
  1223. struct ep92_pdata *ep92)
  1224. {
  1225. sysfs_remove_group(&client->dev.kobj, &ep92_fs_attrs_group);
  1226. }
  1227. static int ep92_i2c_probe(struct i2c_client *client,
  1228. const struct i2c_device_id *id)
  1229. {
  1230. struct ep92_pdata *ep92;
  1231. int ret;
  1232. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1233. char debugfs_dir_name[32];
  1234. #endif
  1235. ep92 = devm_kzalloc(&client->dev, sizeof(struct ep92_pdata),
  1236. GFP_KERNEL);
  1237. if (ep92 == NULL)
  1238. return -ENOMEM;
  1239. ep92->regmap = devm_regmap_init_i2c(client, &ep92_regmap_config);
  1240. if (IS_ERR(ep92->regmap)) {
  1241. ret = PTR_ERR(ep92->regmap);
  1242. dev_err(&client->dev,
  1243. "%s: Failed to allocate regmap for I2C device: %d\n",
  1244. __func__, ret);
  1245. return ret;
  1246. }
  1247. i2c_set_clientdata(client, ep92);
  1248. /* register interrupt handler */
  1249. INIT_WORK(&ep92->read_status_worker, ep92_read_status);
  1250. ep92->irq = client->irq;
  1251. if (ep92->irq) {
  1252. ret = devm_request_threaded_irq(&client->dev, ep92->irq,
  1253. NULL, ep92_irq, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1254. "ep92_irq", ep92);
  1255. if (ret) {
  1256. dev_err(&client->dev,
  1257. "%s: Failed to request IRQ %d: %d\n",
  1258. __func__, ep92->irq, ret);
  1259. ep92->irq = 0;
  1260. }
  1261. }
  1262. /* poll status if IRQ is not configured */
  1263. if (ep92->irq == 0) {
  1264. setup_timer(&ep92->timer, ep92_poll_status,
  1265. (unsigned long)ep92);
  1266. mod_timer(&ep92->timer, jiffies +
  1267. msecs_to_jiffies(EP92_POLL_INTERVAL_OFF_MSEC));
  1268. }
  1269. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1270. /* debugfs interface */
  1271. snprintf(debugfs_dir_name, sizeof(debugfs_dir_name), "%s-%s",
  1272. client->name, dev_name(&client->dev));
  1273. ep92->debugfs_dir = debugfs_create_dir(debugfs_dir_name, NULL);
  1274. if (!ep92->debugfs_dir) {
  1275. dev_dbg(&client->dev,
  1276. "%s: Failed to create /sys/kernel/debug/%s for debugfs\n",
  1277. __func__, debugfs_dir_name);
  1278. return -ENOMEM;
  1279. }
  1280. ep92->debugfs_file_wo = debugfs_create_file(
  1281. "write_reg_val", S_IFREG | 0444, ep92->debugfs_dir,
  1282. (void *) ep92,
  1283. &debugfs_codec_ops);
  1284. if (!ep92->debugfs_file_wo) {
  1285. dev_dbg(&client->dev,
  1286. "%s: Failed to create /sys/kernel/debug/%s/write_reg_val\n",
  1287. __func__, debugfs_dir_name);
  1288. return -ENOMEM;
  1289. }
  1290. ep92->debugfs_file_ro = debugfs_create_file(
  1291. "show_reg_dump", S_IFREG | 0444, ep92->debugfs_dir,
  1292. (void *) ep92,
  1293. &debugfs_codec_ops);
  1294. if (!ep92->debugfs_file_ro) {
  1295. dev_dbg(&client->dev,
  1296. "%s: Failed to create /sys/kernel/debug/%s/show_reg_dump\n",
  1297. __func__, debugfs_dir_name);
  1298. return -ENOMEM;
  1299. }
  1300. #endif /* CONFIG_DEBUG_FS */
  1301. /* register codec */
  1302. ret = snd_soc_register_codec(&client->dev, &soc_codec_drv_ep92,
  1303. ep92_dai, ARRAY_SIZE(ep92_dai));
  1304. if (ret) {
  1305. dev_err(&client->dev,
  1306. "%s %d: Failed to register CODEC: %d\n",
  1307. __func__, __LINE__, ret);
  1308. goto err_reg;
  1309. }
  1310. ret = ep92_sysfs_create(client, ep92);
  1311. if (ret) {
  1312. pr_err("%s: sysfs creation failed ret=%d\n", __func__, ret);
  1313. goto err_sysfs;
  1314. }
  1315. return 0;
  1316. err_sysfs:
  1317. snd_soc_unregister_codec(&client->dev);
  1318. err_reg:
  1319. if (ep92->irq == 0)
  1320. del_timer(&ep92->timer);
  1321. return ret;
  1322. }
  1323. static int ep92_i2c_remove(struct i2c_client *client)
  1324. {
  1325. struct ep92_pdata *ep92;
  1326. ep92 = i2c_get_clientdata(client);
  1327. if (ep92) {
  1328. if (ep92->irq == 0)
  1329. del_timer(&ep92->timer);
  1330. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1331. debugfs_remove_recursive(ep92->debugfs_dir);
  1332. #endif
  1333. }
  1334. snd_soc_unregister_codec(&client->dev);
  1335. ep92_sysfs_remove(client, ep92);
  1336. return 0;
  1337. }
  1338. static const struct i2c_device_id ep92_i2c_id[] = {
  1339. { "ep92-dev", 0},
  1340. { }
  1341. };
  1342. MODULE_DEVICE_TABLE(i2c, ep92_i2c_id);
  1343. static struct i2c_driver ep92_i2c_driver = {
  1344. .probe = ep92_i2c_probe,
  1345. .remove = ep92_i2c_remove,
  1346. .id_table = ep92_i2c_id,
  1347. .driver = {
  1348. .name = "ep92",
  1349. .owner = THIS_MODULE,
  1350. .of_match_table = ep92_of_match
  1351. },
  1352. };
  1353. static int __init ep92_codec_init(void)
  1354. {
  1355. int ret = 0;
  1356. ret = i2c_add_driver(&ep92_i2c_driver);
  1357. if (ret)
  1358. pr_err("Failed to register EP92 I2C driver: %d\n", ret);
  1359. return ret;
  1360. }
  1361. module_init(ep92_codec_init);
  1362. static void __exit ep92_codec_exit(void)
  1363. {
  1364. i2c_del_driver(&ep92_i2c_driver);
  1365. }
  1366. module_exit(ep92_codec_exit);
  1367. MODULE_DESCRIPTION("EP92 HDMI repeater/switch driver");
  1368. MODULE_LICENSE("GPL v2");