ark1668ed_audio_codec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * ark1668ed_audio_codec.c -- ARK SoC audio driver
  3. */
  4. #include <linux/clk.h>
  5. #include <linux/module.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/init.h>
  8. #include <linux/delay.h>
  9. #include <linux/pm.h>
  10. #include <linux/i2c.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/of_gpio.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 <sound/soc-dapm.h>
  21. #include <sound/initval.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/gpio.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/regmap.h>
  27. #include "ark1668ed_i2s.h"
  28. //#define ARK_CODEC_DEBUG
  29. #define ARKADAC_RATES \
  30. (SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  31. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \
  32. SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
  33. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_8000)
  34. #define ARKADAC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE)
  35. static struct snd_soc_component *ark_codec_component;
  36. struct regval_list {
  37. unsigned int addr;
  38. unsigned int data;
  39. };
  40. static struct regval_list ark_codec_init_regs[] = {
  41. //{0x00, 0x00}, /**/
  42. //{0x00, 0x03}, /*Reset ADC/DAC*/
  43. //{0x15, 0x00},
  44. {0x47, 0x00},
  45. {0xF3, 0xc0},
  46. //{0x15, 0x0b}, /*mic-bias:active*/
  47. {0x16, 0x0f}, /*LADC/RADC LPGA/RPGA:active*/
  48. {0x18, 0xf3}, /*LDAC/RDAC,LHeadset/RHeadset,Lline/Rline:active*/
  49. {0x19, 0x04}, /*Differential-line:active*/
  50. {0x1d, 0x00}, /*Recording/Input PGA volume control 0:un-mute 1:mute*/
  51. {0x1f, 0x00}, /*Playback/Headset/Line volume control 0:un-mute 1:mute*/
  52. {0x21, 0x00}, /*Line volume control 0:un-mute 1:mute*/
  53. {0x24, 0xbfbf}, /*Recording digital master volume control(left/right channel)*/
  54. {0x26, 0x1212}, /*Input PGA volume control(left/right channel)*/
  55. {0x34, 0xe7e7}, /*Digital playback master volume control(left/right channel)*/
  56. {0x38, 0x0d0d}, /*Headset analog volume control(left/right channel)*/
  57. {0x47, 0x0101}, /*PGA input selection((single ended)-ainl1p/ainr1n)*/
  58. {0x59, 0x0101}, /*Playback path channel analog mixer input selection*/
  59. {0x5d, 0x0101}, /*Playback path channel analog mixer input selection*/
  60. {0x7b, 0x00}, /*Record digital mixer selection*/
  61. {0x7c, 0x00}, /*Playback digital mixer selection*/
  62. {0x7d, 0x00}, /*ASRC1 digital mixer selection*/
  63. {0x80, 0x0000}, /**/
  64. {0x88, 0x3f3f}, /**/
  65. {0x82, 0x3f3f}, /**/
  66. {0x8a, 0x0000}, /**/
  67. {0x01, 0x05}, /*Set external clock clk_ext(mclk)=12288kHz*/
  68. {0xDC, 0x00},
  69. {0xFB, 0x09},
  70. {0x13, 0x00},
  71. {0xA2, 0x00},
  72. {0x03, 0x01},
  73. {0x04, 0x40}, /*master_mode_valid:play_lrclk_div 0x7d(fs=48k clk_ext(mclk)=12MHz) 0x40(12.288M)*/
  74. {0x05, 0x04}, /*master_mode_valid:play_sclk_div 0x2(fs=48k clk_ext(mclk)=12MHz) 0x4(12.288M)*/
  75. {0x06, 0x40}, /*master_mode_valid:rec_lrclk_div 0x7d(fs=48k clk_ext(mclk)=12MHz) 0x40(12.288M)*/
  76. {0x07, 0x04}, /*master_mode_valid:rec_sclk_div 0x2(fs=48k clk_ext(mclk)=12MHz) 0x4(12.288M)*/
  77. //{0x00, 0x00}, /**/
  78. //{0x00, 0x03}, /*Reset ADC/DAC*/
  79. //{0x15, 0x01},
  80. {0x03, 0x31}, /*set i2s data_format,mode,data_len,bit[2:0]:0x1:i2s bit[3]:0:slave/1:master bit[5:4]:0x11:16bit*/
  81. };
  82. struct ark_codec_priv {
  83. struct regmap *regmap;
  84. struct snd_soc_component *component;
  85. struct clk *mclk;
  86. struct i2c_client *i2c;
  87. void __iomem *sys_base;
  88. int master;
  89. unsigned int sample_rate;
  90. unsigned int volume_left;
  91. unsigned int volume_right;
  92. unsigned int mic_volume_left;
  93. unsigned int mic_volume_right;
  94. bool mute_left;
  95. bool mute_right;
  96. //bool mute;
  97. };
  98. #ifdef ARK_CODEC_DEBUG
  99. #define arkdbgprt printk
  100. #else
  101. #define arkdbgprt(format, arg...) do {} while (0)
  102. #endif
  103. static const char *const ark_input_sel[] = {
  104. "Mic"
  105. };
  106. //static SOC_ENUM_SINGLE_DECL(ark_input_enum, ADC_CODEC_REG_CTRL1, 0, ark_input_sel);
  107. static unsigned int arkcodec_register_size(struct device *dev,
  108. unsigned int reg)
  109. {
  110. switch (reg) {
  111. case CODEC_REG_RECVOL:
  112. case CODEC_REG_PGA1VOL:
  113. case CODEC_REG_LMVOL:
  114. case CODEC_REG_HS1VOL:
  115. case CODEC_REG_PGA1SEL:
  116. case CODEC_REG_LDR1SEL:
  117. case CODEC_REG_LDR2SEL:
  118. case CODEC_REG_RECPBVOL:
  119. case CODEC_REG_RECI2S1VOL:
  120. case CODEC_REG_I2S1PBVOL:
  121. case CODEC_REG_I2S1I2S1VOL:
  122. return 2;
  123. default:
  124. return 1;
  125. }
  126. dev_err(dev, "Unsupported register address: %d\n", reg);
  127. return 0;
  128. }
  129. static int codec_reg_write(struct i2c_client *client, unsigned int reg, unsigned int value)
  130. {
  131. unsigned int data_len;
  132. int ret;
  133. uint8_t buf[3];
  134. unsigned int pos = 0;
  135. data_len = arkcodec_register_size(&client->dev, reg);
  136. buf[pos++] = reg & 0xFF;
  137. if (data_len == 1) {
  138. buf[pos++] = value & 0xFF;
  139. } else {
  140. buf[pos++] = (value >> 8) & 0xFF;
  141. buf[pos++] = value & 0xFF;
  142. }
  143. ret = i2c_master_send(client, buf, pos);
  144. if (ret < 0) {
  145. dev_err(&client->dev, "I2C write error: %d\n", ret);
  146. return ret;
  147. } else if (ret != pos) {
  148. dev_err(&client->dev, "I2C write: short transfer (%d)\n", ret);
  149. return -EIO;
  150. } else
  151. return 0;
  152. }
  153. static int codec_reg_read(struct i2c_client *client, unsigned int reg, unsigned int *value)
  154. {
  155. int ret;
  156. //unsigned int i;
  157. unsigned int data_len, reg_len;
  158. uint8_t addr_buf[2] = {0}, data_buf[2] = {0};
  159. struct i2c_msg msgs[2];
  160. data_len = arkcodec_register_size(&client->dev, reg);
  161. if (data_len == 0)
  162. return -EINVAL;
  163. addr_buf[0] = reg & 0xFF;
  164. reg_len = 1;
  165. msgs[0].addr = client->addr;
  166. msgs[0].len = reg_len;
  167. msgs[0].buf = addr_buf;
  168. msgs[0].flags = 0;
  169. msgs[1].addr = client->addr;
  170. msgs[1].len = data_len;
  171. msgs[1].buf = data_buf;
  172. msgs[1].flags = I2C_M_RD;
  173. ret = i2c_transfer(client->adapter, msgs, 2);
  174. if (ret < 0)
  175. return ret;
  176. else if (ret != 2)
  177. return -EIO;
  178. if (data_len == 2) {
  179. *value = (data_buf[0] << 8) | data_buf[1];
  180. } else {
  181. *value = data_buf[0];
  182. }
  183. return 0;
  184. }
  185. static int ark_dac_l_playback_volume_get(struct snd_kcontrol *kcontrol,
  186. struct snd_ctl_elem_value *ucontrol)
  187. {
  188. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  189. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  190. ucontrol->value.integer.value[0] = arkcodec->volume_left;
  191. return 0;
  192. }
  193. static int ark_dac_l_playback_volume_put(struct snd_kcontrol *kcontrol,
  194. struct snd_ctl_elem_value *ucontrol)
  195. {
  196. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  197. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  198. unsigned int val = ucontrol->value.integer.value[0];
  199. int change = 0;
  200. if (arkcodec->volume_left != val) {
  201. arkcodec->volume_left = val;
  202. //printk("[set:val = 0x%04x\n",(arkcodec->volume_left << 8) | arkcodec->volume_right);
  203. codec_reg_write(arkcodec->i2c, CODEC_REG_LMVOL, (arkcodec->volume_left << 8) | arkcodec->volume_right);
  204. change = 1;
  205. }
  206. return change;
  207. }
  208. static int ark_dac_r_playback_volume_get(struct snd_kcontrol *kcontrol,
  209. struct snd_ctl_elem_value *ucontrol)
  210. {
  211. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  212. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  213. ucontrol->value.integer.value[0] = arkcodec->volume_right;
  214. return 0;
  215. }
  216. static int ark_dac_r_playback_volume_put(struct snd_kcontrol *kcontrol,
  217. struct snd_ctl_elem_value *ucontrol)
  218. {
  219. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  220. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  221. unsigned int val = ucontrol->value.integer.value[0];
  222. int change = 0;
  223. if (arkcodec->volume_right != val) {
  224. arkcodec->volume_right = val;
  225. //printk("[set:val = 0x%04x\n",(arkcodec->volume_left << 8) | arkcodec->volume_right);
  226. codec_reg_write(arkcodec->i2c, CODEC_REG_LMVOL, (arkcodec->volume_left << 8) | arkcodec->volume_right);
  227. change = 1;
  228. }
  229. return change;
  230. }
  231. static int ark_adc_l_capture_volume_get(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_value *ucontrol)
  233. {
  234. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  235. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  236. ucontrol->value.integer.value[0] = arkcodec->mic_volume_left;
  237. return 0;
  238. }
  239. static int ark_adc_l_capture_volume_put(struct snd_kcontrol *kcontrol,
  240. struct snd_ctl_elem_value *ucontrol)
  241. {
  242. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  243. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  244. unsigned int val = ucontrol->value.integer.value[0];
  245. int change = 0;
  246. if (arkcodec->mic_volume_left != val) {
  247. arkcodec->mic_volume_left = val;
  248. //printk("[set:val = 0x%04x\n",(arkcodec->mic_volume_left << 8) | arkcodec->mic_volume_right);
  249. codec_reg_write(arkcodec->i2c, CODEC_REG_RECVOL, (arkcodec->mic_volume_left << 8) | arkcodec->mic_volume_right);
  250. change = 1;
  251. }
  252. return change;
  253. }
  254. static int ark_adc_r_capture_volume_get(struct snd_kcontrol *kcontrol,
  255. struct snd_ctl_elem_value *ucontrol)
  256. {
  257. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  258. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  259. ucontrol->value.integer.value[0] = arkcodec->mic_volume_right;
  260. return 0;
  261. }
  262. static int ark_adc_r_capture_volume_put(struct snd_kcontrol *kcontrol,
  263. struct snd_ctl_elem_value *ucontrol)
  264. {
  265. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  266. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  267. unsigned int val = ucontrol->value.integer.value[0];
  268. int change = 0;
  269. if (arkcodec->mic_volume_right != val) {
  270. arkcodec->mic_volume_right = val;
  271. //printk("[set:val = 0x%04x\n",(arkcodec->mic_volume_left << 8) | arkcodec->mic_volume_right);
  272. codec_reg_write(arkcodec->i2c, CODEC_REG_RECVOL, (arkcodec->mic_volume_left << 8) | arkcodec->mic_volume_right);
  273. change = 1;
  274. }
  275. return change;
  276. }
  277. static int ark_dac_mute_get(struct snd_kcontrol *kcontrol,
  278. struct snd_ctl_elem_value *ucontrol)
  279. {
  280. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  281. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  282. ucontrol->value.integer.value[0] = arkcodec->mute_left;
  283. ucontrol->value.integer.value[1] = arkcodec->mute_right;
  284. return 0;
  285. }
  286. static int ark_dac_mute_put(struct snd_kcontrol *kcontrol,
  287. struct snd_ctl_elem_value *ucontrol)
  288. {
  289. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  290. struct ark_codec_priv *arkcodec = snd_soc_component_get_drvdata(component);
  291. unsigned int left = ucontrol->value.integer.value[0];
  292. unsigned int right = ucontrol->value.integer.value[1];
  293. unsigned int mute_val = 0;
  294. int change = 0;
  295. if (arkcodec->mute_left != left || arkcodec->mute_right != right) {
  296. arkcodec->mute_left = left;
  297. arkcodec->mute_right = right;
  298. if (left)
  299. mute_val |= MUTE_BIT_LEFT;
  300. if (right)
  301. mute_val |= MUTE_BIT_RIGHT;
  302. //printk("[set:mute_val = %d\n",mute_val);
  303. regmap_update_bits(arkcodec->regmap, CODEC_REG_PLAYBACK_MUTE, 0x3, mute_val);
  304. change = 1;
  305. }
  306. return change;
  307. }
  308. static const DECLARE_TLV_DB_SCALE(ark_dac_vol_tlv, -11550, 50, 0);/*\D2\F4\C1\BF\B7\B6Χ:[-115.5dB-12dB],\B2\BD\B3\A4 :0.5dB*/
  309. static const DECLARE_TLV_DB_SCALE(ark_mic_vol_tlv, -9550, 50, 0);/*\D2\F4\C1\BF\B7\B6Χ:[-95.5dB-12dB], \B2\BD\B3\A4 :0.5dB*/
  310. static const struct snd_kcontrol_new ark_codec_controls[] = {
  311. SOC_SINGLE_EXT("DAC Left Playback Volume", 0, 0, 255, 0,
  312. ark_dac_l_playback_volume_get, ark_dac_l_playback_volume_put),
  313. SOC_SINGLE_EXT("DAC Right Playback Volume", 0, 0, 255, 0,
  314. ark_dac_r_playback_volume_get, ark_dac_r_playback_volume_put),
  315. SOC_SINGLE_EXT("ADC Left Capture Volume", 0, 0, 255, 0,
  316. ark_adc_l_capture_volume_get, ark_adc_l_capture_volume_put),
  317. SOC_SINGLE_EXT("ADC Right Capture Volume", 0, 0, 255, 0,
  318. ark_adc_r_capture_volume_get, ark_adc_r_capture_volume_put),
  319. SOC_DOUBLE_EXT("Playback Switch", CODEC_REG_PLAYBACK_MUTE, 0, 1, 1, 1,
  320. ark_dac_mute_get, ark_dac_mute_put),
  321. };
  322. static const struct snd_soc_dapm_widget dwc_audio_dapm_widgets[] = {
  323. SND_SOC_DAPM_DAC("DAC", "Playback", CODEC_REG_DAC_RESET_CTRL, 0, 0),
  324. SND_SOC_DAPM_ADC("ADC", "Capture", CODEC_REG_ADC_RESET_CTRL, 1, 0),
  325. };
  326. static const struct snd_soc_dapm_route dwc_audio_dapm_routes[] = {
  327. {"DAC", NULL, "Playback"},
  328. {"ADC", NULL, "Capture"},
  329. };
  330. static int ark_codec_init_reg(struct snd_soc_component *component)
  331. {
  332. arkdbgprt("[ARK_CODEC] %s(%d)\n", __func__, __LINE__);
  333. struct ark_codec_priv *priv = snd_soc_component_get_drvdata(component);
  334. unsigned int val;
  335. int i;
  336. /*reset codec*/
  337. regmap_write(priv->regmap, CODEC_REG_ADC_DAC_RESET, 0);
  338. mdelay(2);
  339. regmap_write(priv->regmap, CODEC_REG_ADC_DAC_RESET, 0x3);
  340. /*init-regs*/
  341. for (i = 0; i < ARRAY_SIZE(ark_codec_init_regs); i++) {
  342. codec_reg_write(priv->i2c, ark_codec_init_regs[i].addr, ark_codec_init_regs[i].data);
  343. //printk("[write:reg=0x%02x,val=0x%04x]\n",ark_codec_init_regs[i].addr,ark_codec_init_regs[i].data);
  344. }
  345. #if 0
  346. for (i = 0; i < ARRAY_SIZE(ark_codec_init_regs); i++) {
  347. codec_reg_read(priv->i2c, ark_codec_init_regs[i].addr, &val);
  348. printk("[read:reg=0x%02x,val=0x%04x]\n", ark_codec_init_regs[i], val);
  349. }
  350. #endif
  351. /*Mic_Bias power-down*/
  352. regmap_write(priv->regmap, CODEC_REG_POWER_DOWN, 0);
  353. mdelay(2);
  354. regmap_write(priv->regmap, CODEC_REG_POWER_DOWN, 0xb);
  355. codec_reg_read(priv->i2c, CODEC_REG_LMVOL, &val);
  356. priv->volume_left = (val >> 8) & 0xFF;
  357. priv->volume_right = val & 0xFF;
  358. codec_reg_read(priv->i2c, CODEC_REG_RECVOL, &val);
  359. priv->mic_volume_left = (val >> 8) & 0xFF;
  360. priv->mic_volume_right = val & 0xFF;
  361. priv->mute_left = false;
  362. priv->mute_right = false;
  363. return 0;
  364. }
  365. //static unsigned int ark_codec_read(struct snd_soc_component *component,
  366. // unsigned int reg)
  367. //{
  368. // int val;
  369. // val = snd_soc_component_read(component,reg);
  370. // return val;
  371. //}
  372. //static int ark_codec_write(struct snd_soc_component *component,
  373. // unsigned int reg, unsigned int value)
  374. //{
  375. // int ret;
  376. // ret = snd_soc_component_write(component, reg, value);
  377. // return ret;
  378. //}
  379. static int ark_codec_startup(struct snd_pcm_substream *substream,
  380. struct snd_soc_dai *dai)
  381. {
  382. return 0;
  383. }
  384. static void ark_codec_shutdown(struct snd_pcm_substream *substream,
  385. struct snd_soc_dai *dai)
  386. {
  387. }
  388. static int ark_codec_hw_params(struct snd_pcm_substream *substream,
  389. struct snd_pcm_hw_params *params,
  390. struct snd_soc_dai *dai)
  391. {
  392. struct ark_codec_priv *priv = snd_soc_component_get_drvdata(dai->component);
  393. //int regval = 0;
  394. unsigned int clk_div, bclk_div;
  395. arkdbgprt("[ARK_CODEC] %s(%d)\n", __func__, __LINE__);
  396. priv->sample_rate = params_rate(params);
  397. switch (priv->sample_rate) {
  398. case 8000:
  399. case 16000:
  400. case 32000:
  401. clk_div = 3;
  402. break;
  403. case 44100:
  404. case 88200:
  405. clk_div = 2;
  406. break;
  407. case 48000:
  408. case 96000:
  409. clk_div = 1;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. switch (params_width(params)) {
  415. case 16:
  416. bclk_div = 0;
  417. break;
  418. case 24:
  419. bclk_div = 1;
  420. break;
  421. case 32:
  422. bclk_div = 2;
  423. break;
  424. default:
  425. return -EINVAL;
  426. }
  427. /*set rate*/
  428. // switch (params_rate(params)) {
  429. // case 48000:
  430. // regval = CTRL1_SAMPLE_RATE_48K;
  431. // break;
  432. // default:
  433. // dev_err(dai->dev, "Unsupported sample rate: %d\n", params_rate(params));
  434. // return -EINVAL;
  435. // }
  436. //regmap_update_bits(priv->regmap, ADC_CODEC_REG_CTRL1, 0xF0, regval);
  437. return 0;
  438. }
  439. static int ark_codec_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  440. int clk_id, unsigned int freq, int dir)
  441. {
  442. arkdbgprt("[ARK_CODEC] %s(%d)\n", __func__, __LINE__);
  443. return 0;
  444. }
  445. static int ark_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
  446. {
  447. //struct snd_soc_component *component = dai->component;
  448. //struct ark_codec_priv *priv = snd_soc_component_get_drvdata(component);
  449. //unsigned int val = mute ? CODEC_REG_DAC_SOFT_MUTE : CODEC_REG_DAC_SOFT_UNMUTE;
  450. int ret = 0;
  451. #if 0
  452. printk("#####%s,%d:mute = %d\n", __func__, __LINE__, mute);
  453. if (mute) {
  454. regmap_update_bits(priv->regmap, CODEC_REG_PLAYBACK_MUTE,
  455. 0x3, 0x3);
  456. priv->mute_left = true;
  457. priv->mute_right = true;
  458. } else {
  459. regmap_update_bits(priv->regmap, CODEC_REG_PLAYBACK_MUTE,
  460. 0x3, 0x0);
  461. priv->mute_left = false;
  462. priv->mute_right = false;
  463. }
  464. #endif
  465. return ret;
  466. }
  467. static int ark_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
  468. unsigned int fmt)
  469. {
  470. //struct snd_soc_component *component = codec_dai->component;
  471. struct ark_codec_priv *priv = snd_soc_dai_get_drvdata(codec_dai);
  472. arkdbgprt("[ARK_CODEC] %s(%d),codec_dai->id = %d\n", __func__, __LINE__, codec_dai->id);
  473. /* set master/slave audio interface */
  474. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  475. case SND_SOC_DAIFMT_CBM_CFM:
  476. printk("###ark audio codec master###\n");
  477. //priv->master= 1;
  478. regmap_update_bits(priv->regmap, CODEC_REG_I2S1_CONFIG_0, CODEC_REG_I2S1_MODE_SEL, 0x1);
  479. break;
  480. case SND_SOC_DAIFMT_CBS_CFS:
  481. printk("###ark audio codec slave###\n");
  482. //priv->master = 0;
  483. regmap_update_bits(priv->regmap, CODEC_REG_I2S1_CONFIG_0, CODEC_REG_I2S1_MODE_SEL, 0x0);
  484. break;
  485. default:
  486. return -EINVAL;
  487. }
  488. return 0;
  489. }
  490. static const struct snd_soc_dai_ops ark_codec_dai_ops = {
  491. .startup = ark_codec_startup,
  492. .shutdown = ark_codec_shutdown,
  493. .hw_params = ark_codec_hw_params,
  494. .set_fmt = ark_codec_set_dai_fmt,
  495. .set_sysclk = ark_codec_set_dai_sysclk,
  496. .mute_stream = ark_codec_mute,
  497. };
  498. static struct snd_soc_dai_driver ark_codec_dai = {
  499. .name = "ark-codec-dai",
  500. .playback = {
  501. .stream_name = "Playback",
  502. .channels_min = 1,
  503. .channels_max = 2,
  504. .rates = ARKADAC_RATES,
  505. .formats = ARKADAC_FORMATS,
  506. },
  507. .capture = {
  508. .stream_name = "Capture",
  509. .channels_min = 1,
  510. .channels_max = 4,
  511. .rates = ARKADAC_RATES,
  512. .formats = ARKADAC_FORMATS,
  513. },
  514. .ops = &ark_codec_dai_ops,
  515. .symmetric_rate = 1,
  516. };
  517. static int ark_codec_probe(struct snd_soc_component *component)
  518. {
  519. struct ark_codec_priv *priv = snd_soc_component_get_drvdata(component);
  520. ark_codec_component = component;
  521. priv->component = component;
  522. arkdbgprt("[ARK_CODEC] %s(%d)\n", __func__, __LINE__);
  523. ark_codec_init_reg(component);
  524. return 0;
  525. }
  526. static void ark_codec_remove(struct snd_soc_component *component)
  527. {
  528. //struct ark_codec_priv *priv = snd_soc_component_get_drvdata(component);
  529. arkdbgprt("[ARK_CODEC] %s(%d)\n", __func__, __LINE__);
  530. //regmap_write(priv->regmap, ADC_CODEC_REG_POWER_CTRL, 0);
  531. }
  532. static const struct snd_soc_component_driver ark_codec_component_driver = {
  533. .probe = ark_codec_probe,
  534. .remove = ark_codec_remove,
  535. .controls = ark_codec_controls,
  536. .num_controls = ARRAY_SIZE(ark_codec_controls),
  537. .dapm_widgets = dwc_audio_dapm_widgets,
  538. .num_dapm_widgets = ARRAY_SIZE(dwc_audio_dapm_widgets),
  539. // .dapm_routes = dwc_audio_dapm_routes,
  540. // .num_dapm_routes = ARRAY_SIZE(dwc_audio_dapm_routes),
  541. };
  542. static const struct regmap_config ark_codec_regmap_config = {
  543. .reg_bits = 8,
  544. .val_bits = 8,
  545. .max_register = CODEC_REG_RESERVED_3,
  546. };
  547. static ssize_t ark_codec_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  548. {
  549. int val = 0, flag = 0;
  550. u8 i = 0, reg, num, value_w;
  551. int value_r;
  552. struct ark_codec_priv *priv = dev_get_drvdata(dev);
  553. val = simple_strtol(buf, NULL, 16);
  554. flag = (val >> 16) & 0xFFF;
  555. if (flag) {
  556. reg = (val >> 8) & 0xFF;
  557. value_w = val & 0xFF;
  558. printk("\nWrite: start REG:0x%04x,val:0x%04x,count:0x%04x\n", reg, value_w, flag);
  559. while (flag--) {
  560. //ark_codec_write(ark_codec_component,reg, value_w);
  561. codec_reg_write(priv->i2c, reg, value_w);
  562. printk("Write 0x%04x to REG:0x%04x\n", value_w, reg);
  563. reg++;
  564. }
  565. } else {
  566. reg = (val >> 8) & 0xFF;
  567. num = val & 0xFF;
  568. printk("\nRead: start REG:0x%04x,count:0x%04x\n", reg, num);
  569. do {
  570. //value_r = 0;
  571. //value_r = ark_codec_read(ark_codec_component,reg);
  572. codec_reg_read(priv->i2c, reg, &value_r);
  573. printk("REG[0x%04x]: 0x%04x; ", reg, value_r);
  574. reg++;
  575. i++;
  576. if ((i == num) || (i%4 == 0))
  577. printk("\n");
  578. } while (i < num);
  579. }
  580. return count;
  581. }
  582. static ssize_t ark_codec_show(struct device *dev, struct device_attribute *attr, char *buf)
  583. {
  584. printk("echo flag|reg|val > ark_codec\n");
  585. printk("eg read star addres=0x06,count 0x10:echo 0610 >ark_codec\n");
  586. printk("eg write star addres=0x90,value=0x3c,count=4:echo 4903c >ark_codec\n");
  587. //printk("eg write value:0xfe to address:0x06 :echo 106fe > es7243\n");
  588. return 0;
  589. }
  590. static DEVICE_ATTR(ark_codec, 0644, ark_codec_show, ark_codec_store);
  591. static struct attribute *ark_codec_debug_attrs[] = {
  592. &dev_attr_ark_codec.attr,
  593. NULL,
  594. };
  595. static struct attribute_group ark_codec_debug_attr_group = {
  596. .name = "ark_codec_debug",
  597. .attrs = ark_codec_debug_attrs,
  598. };
  599. static int ark_codec_i2c_probe(struct i2c_client *i2c)
  600. {
  601. struct ark_codec_priv *priv;
  602. int ret;
  603. priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
  604. if (!priv)
  605. return -ENOMEM;
  606. priv->regmap = devm_regmap_init_i2c(i2c, &ark_codec_regmap_config);
  607. if (IS_ERR(priv->regmap))
  608. return PTR_ERR(priv->regmap);
  609. i2c_set_clientdata(i2c, priv);
  610. priv->i2c = i2c;
  611. ret = devm_snd_soc_register_component(&i2c->dev,
  612. &ark_codec_component_driver, &ark_codec_dai, 1);
  613. if (ret)
  614. dev_err(&i2c->dev, "Failed to register component: %d\n", ret);
  615. #if 1
  616. ret = sysfs_create_group(&i2c->dev.kobj, &ark_codec_debug_attr_group);
  617. if (ret) {
  618. pr_err("failed to create attr group\n");
  619. }
  620. #endif
  621. return ret;
  622. }
  623. static void ark_codec_i2c_remove(struct i2c_client *client)
  624. {
  625. }
  626. static void ark_codec_i2c_shutdown(struct i2c_client *client)
  627. {
  628. //struct ark_codec_priv *codec = i2c_get_clientdata(client);
  629. }
  630. static const struct i2c_device_id ark_codec_i2c_id[] = {
  631. { "ark-codec", 0 },
  632. { }
  633. };
  634. MODULE_DEVICE_TABLE(i2c, ark_codec_i2c_id);
  635. static const struct of_device_id ark_codec_of_match[] = {
  636. { .compatible = "arkmicro,ark-audio-codec" },
  637. { }
  638. };
  639. MODULE_DEVICE_TABLE(of, adc_codec_of_match);
  640. static struct i2c_driver ark_codec_i2c_driver = {
  641. .driver = {
  642. .name = "ark-codec",
  643. .of_match_table = ark_codec_of_match,
  644. },
  645. .probe = ark_codec_i2c_probe,
  646. .remove = ark_codec_i2c_remove,
  647. .shutdown = ark_codec_i2c_shutdown,
  648. .id_table = ark_codec_i2c_id,
  649. };
  650. module_i2c_driver(ark_codec_i2c_driver);
  651. MODULE_DESCRIPTION("ARK Codec I2C Driver");
  652. MODULE_AUTHOR("ARK");
  653. MODULE_LICENSE("GPL");