ep92.c 40 KB

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