csra66x0.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 <sound/core.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/tlv.h>
  15. #include <sound/soc.h>
  16. #include <linux/gpio.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/fs.h>
  19. #include <linux/debugfs.h>
  20. #include "csra66x0.h"
  21. /* CSRA66X0 register default values */
  22. static struct reg_default csra66x0_reg_defaults[] = {
  23. {CSRA66X0_AUDIO_IF_RX_CONFIG1, 0x00},
  24. {CSRA66X0_AUDIO_IF_RX_CONFIG2, 0x0B},
  25. {CSRA66X0_AUDIO_IF_RX_CONFIG3, 0x0F},
  26. {CSRA66X0_AUDIO_IF_TX_EN, 0x00},
  27. {CSRA66X0_AUDIO_IF_TX_CONFIG1, 0x6B},
  28. {CSRA66X0_AUDIO_IF_TX_CONFIG2, 0x02},
  29. {CSRA66X0_I2C_DEVICE_ADDRESS, 0x0D},
  30. {CSRA66X0_CHIP_ID_FA, 0x39},
  31. {CSRA66X0_ROM_VER_FA, 0x08},
  32. {CSRA66X0_CHIP_REV_0_FA, 0x05},
  33. {CSRA66X0_CHIP_REV_1_FA, 0x03},
  34. {CSRA66X0_CH1_MIX_SEL, 0x01},
  35. {CSRA66X0_CH2_MIX_SEL, 0x10},
  36. {CSRA66X0_CH1_SAMPLE1_SCALE_0, 0x00},
  37. {CSRA66X0_CH1_SAMPLE1_SCALE_1, 0x20},
  38. {CSRA66X0_CH1_SAMPLE3_SCALE_0, 0x00},
  39. {CSRA66X0_CH1_SAMPLE3_SCALE_1, 0x20},
  40. {CSRA66X0_CH1_SAMPLE5_SCALE_0, 0x00},
  41. {CSRA66X0_CH1_SAMPLE5_SCALE_1, 0x20},
  42. {CSRA66X0_CH1_SAMPLE7_SCALE_0, 0x00},
  43. {CSRA66X0_CH1_SAMPLE7_SCALE_1, 0x20},
  44. {CSRA66X0_CH1_SAMPLE2_SCALE_0, 0x00},
  45. {CSRA66X0_CH1_SAMPLE2_SCALE_1, 0x20},
  46. {CSRA66X0_CH1_SAMPLE4_SCALE_0, 0x00},
  47. {CSRA66X0_CH1_SAMPLE4_SCALE_1, 0x20},
  48. {CSRA66X0_CH1_SAMPLE6_SCALE_0, 0x00},
  49. {CSRA66X0_CH1_SAMPLE6_SCALE_1, 0x20},
  50. {CSRA66X0_CH1_SAMPLE8_SCALE_0, 0x00},
  51. {CSRA66X0_CH1_SAMPLE8_SCALE_1, 0x20},
  52. {CSRA66X0_CH2_SAMPLE1_SCALE_0, 0x00},
  53. {CSRA66X0_CH2_SAMPLE1_SCALE_1, 0x20},
  54. {CSRA66X0_CH2_SAMPLE3_SCALE_0, 0x00},
  55. {CSRA66X0_CH2_SAMPLE3_SCALE_1, 0x20},
  56. {CSRA66X0_CH2_SAMPLE5_SCALE_0, 0x00},
  57. {CSRA66X0_CH2_SAMPLE5_SCALE_1, 0x20},
  58. {CSRA66X0_CH2_SAMPLE7_SCALE_0, 0x00},
  59. {CSRA66X0_CH2_SAMPLE7_SCALE_1, 0x20},
  60. {CSRA66X0_CH2_SAMPLE2_SCALE_0, 0x00},
  61. {CSRA66X0_CH2_SAMPLE2_SCALE_1, 0x20},
  62. {CSRA66X0_CH2_SAMPLE4_SCALE_0, 0x00},
  63. {CSRA66X0_CH2_SAMPLE4_SCALE_1, 0x20},
  64. {CSRA66X0_CH2_SAMPLE6_SCALE_0, 0x00},
  65. {CSRA66X0_CH2_SAMPLE6_SCALE_1, 0x20},
  66. {CSRA66X0_CH2_SAMPLE8_SCALE_0, 0x00},
  67. {CSRA66X0_CH2_SAMPLE8_SCALE_1, 0x20},
  68. {CSRA66X0_VOLUME_CONFIG_FA, 0x26},
  69. {CSRA66X0_STARTUP_DELAY_FA, 0x00},
  70. {CSRA66X0_CH1_VOLUME_0_FA, 0x19},
  71. {CSRA66X0_CH1_VOLUME_1_FA, 0x01},
  72. {CSRA66X0_CH2_VOLUME_0_FA, 0x19},
  73. {CSRA66X0_CH2_VOLUME_1_FA, 0x01},
  74. {CSRA66X0_QUAD_ENC_COUNT_0_FA, 0x00},
  75. {CSRA66X0_QUAD_ENC_COUNT_1_FA, 0x00},
  76. {CSRA66X0_SOFT_CLIP_CONFIG, 0x00},
  77. {CSRA66X0_CH1_HARD_CLIP_THRESH, 0x00},
  78. {CSRA66X0_CH2_HARD_CLIP_THRESH, 0x00},
  79. {CSRA66X0_SOFT_CLIP_THRESH, 0x00},
  80. {CSRA66X0_DS_ENABLE_THRESH_0, 0x05},
  81. {CSRA66X0_DS_ENABLE_THRESH_1, 0x00},
  82. {CSRA66X0_DS_TARGET_COUNT_0, 0x00},
  83. {CSRA66X0_DS_TARGET_COUNT_1, 0xFF},
  84. {CSRA66X0_DS_TARGET_COUNT_2, 0xFF},
  85. {CSRA66X0_DS_DISABLE_THRESH_0, 0x0F},
  86. {CSRA66X0_DS_DISABLE_THRESH_1, 0x00},
  87. {CSRA66X0_DCA_CTRL, 0x07},
  88. {CSRA66X0_CH1_DCA_THRESH, 0x40},
  89. {CSRA66X0_CH2_DCA_THRESH, 0x40},
  90. {CSRA66X0_DCA_ATTACK_RATE, 0x00},
  91. {CSRA66X0_DCA_RELEASE_RATE, 0x00},
  92. {CSRA66X0_CH1_OUTPUT_INVERT_EN, 0x00},
  93. {CSRA66X0_CH2_OUTPUT_INVERT_EN, 0x00},
  94. {CSRA66X0_CH1_176P4K_DELAY, 0x00},
  95. {CSRA66X0_CH2_176P4K_DELAY, 0x00},
  96. {CSRA66X0_CH1_192K_DELAY, 0x00},
  97. {CSRA66X0_CH2_192K_DELAY, 0x00},
  98. {CSRA66X0_DEEMP_CONFIG_FA, 0x00},
  99. {CSRA66X0_CH1_TREBLE_GAIN_CTRL_FA, 0x00},
  100. {CSRA66X0_CH2_TREBLE_GAIN_CTRL_FA, 0x00},
  101. {CSRA66X0_CH1_TREBLE_FC_CTRL_FA, 0x00},
  102. {CSRA66X0_CH2_TREBLE_FC_CTRL_FA, 0x00},
  103. {CSRA66X0_CH1_BASS_GAIN_CTRL_FA, 0x00},
  104. {CSRA66X0_CH2_BASS_GAIN_CTRL_FA, 0x00},
  105. {CSRA66X0_CH1_BASS_FC_CTRL_FA, 0x00},
  106. {CSRA66X0_CH2_BASS_FC_CTRL_FA, 0x00},
  107. {CSRA66X0_FILTER_SEL_8K, 0x00},
  108. {CSRA66X0_FILTER_SEL_11P025K, 0x00},
  109. {CSRA66X0_FILTER_SEL_16K, 0x00},
  110. {CSRA66X0_FILTER_SEL_22P05K, 0x00},
  111. {CSRA66X0_FILTER_SEL_32K, 0x00},
  112. {CSRA66X0_FILTER_SEL_44P1K_48K, 0x00},
  113. {CSRA66X0_FILTER_SEL_88P2K_96K, 0x00},
  114. {CSRA66X0_FILTER_SEL_176P4K_192K, 0x00},
  115. /* RESERVED */
  116. {CSRA66X0_USER_DSP_CTRL, 0x00},
  117. {CSRA66X0_TEST_TONE_CTRL, 0x00},
  118. {CSRA66X0_TEST_TONE_FREQ_0, 0x00},
  119. {CSRA66X0_TEST_TONE_FREQ_1, 0x00},
  120. {CSRA66X0_TEST_TONE_FREQ_2, 0x00},
  121. {CSRA66X0_AUDIO_RATE_CTRL_FA, 0x08},
  122. {CSRA66X0_MODULATION_INDEX_CTRL, 0x3F},
  123. {CSRA66X0_MODULATION_INDEX_COUNT, 0x10},
  124. {CSRA66X0_MIN_MODULATION_PULSE_WIDTH, 0x7A},
  125. {CSRA66X0_DEAD_TIME_CTRL, 0x00},
  126. {CSRA66X0_DEAD_TIME_THRESHOLD_0, 0xE7},
  127. {CSRA66X0_DEAD_TIME_THRESHOLD_1, 0x26},
  128. {CSRA66X0_DEAD_TIME_THRESHOLD_2, 0x40},
  129. {CSRA66X0_CH1_LOW_SIDE_DLY, 0x00},
  130. {CSRA66X0_CH2_LOW_SIDE_DLY, 0x00},
  131. {CSRA66X0_SPECTRUM_CTRL, 0x00},
  132. /* RESERVED */
  133. {CSRA66X0_SPECTRUM_SPREAD_CTRL, 0x0C},
  134. /* RESERVED */
  135. {CSRA66X0_EXT_PA_PROTECT_POLARITY, 0x03},
  136. {CSRA66X0_TEMP0_BACKOFF_COMP_VALUE, 0x98},
  137. {CSRA66X0_TEMP0_SHUTDOWN_COMP_VALUE, 0xA3},
  138. {CSRA66X0_TEMP1_BACKOFF_COMP_VALUE, 0x98},
  139. {CSRA66X0_TEMP1_SHUTDOWN_COMP_VALUE, 0xA3},
  140. {CSRA66X0_TEMP_PROT_BACKOFF, 0x00},
  141. {CSRA66X0_TEMP_READ0_FA, 0x00},
  142. {CSRA66X0_TEMP_READ1_FA, 0x00},
  143. {CSRA66X0_CHIP_STATE_CTRL_FA, 0x02},
  144. /* RESERVED */
  145. {CSRA66X0_PWM_OUTPUT_CONFIG, 0x00},
  146. {CSRA66X0_MISC_CONTROL_STATUS_0, 0x08},
  147. {CSRA66X0_MISC_CONTROL_STATUS_1_FA, 0x40},
  148. {CSRA66X0_PIO0_SELECT, 0x00},
  149. {CSRA66X0_PIO1_SELECT, 0x00},
  150. {CSRA66X0_PIO2_SELECT, 0x00},
  151. {CSRA66X0_PIO3_SELECT, 0x00},
  152. {CSRA66X0_PIO4_SELECT, 0x00},
  153. {CSRA66X0_PIO5_SELECT, 0x00},
  154. {CSRA66X0_PIO6_SELECT, 0x00},
  155. {CSRA66X0_PIO7_SELECT, 0x00},
  156. {CSRA66X0_PIO8_SELECT, 0x00},
  157. {CSRA66X0_PIO_DIRN0, 0xFF},
  158. {CSRA66X0_PIO_DIRN1, 0x01},
  159. {CSRA66X0_PIO_PULL_EN0, 0xFF},
  160. {CSRA66X0_PIO_PULL_EN1, 0x01},
  161. {CSRA66X0_PIO_PULL_DIR0, 0x00},
  162. {CSRA66X0_PIO_PULL_DIR1, 0x00},
  163. {CSRA66X0_PIO_DRIVE_OUT0_FA, 0x00},
  164. {CSRA66X0_PIO_DRIVE_OUT1_FA, 0x00},
  165. {CSRA66X0_PIO_STATUS_IN0_FA, 0x00},
  166. {CSRA66X0_PIO_STATUS_IN1_FA, 0x00},
  167. /* RESERVED */
  168. {CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00},
  169. {CSRA66X0_IRQ_OUTPUT_POLARITY, 0x01},
  170. {CSRA66X0_IRQ_OUTPUT_STATUS_FA, 0x00},
  171. {CSRA66X0_CLIP_DCA_STATUS_FA, 0x00},
  172. {CSRA66X0_CHIP_STATE_STATUS_FA, 0x02},
  173. {CSRA66X0_FAULT_STATUS_FA, 0x00},
  174. {CSRA66X0_OTP_STATUS_FA, 0x00},
  175. {CSRA66X0_AUDIO_IF_STATUS_FA, 0x00},
  176. /* RESERVED */
  177. {CSRA66X0_DSP_SATURATION_STATUS_FA, 0x00},
  178. {CSRA66X0_AUDIO_RATE_STATUS_FA, 0x00},
  179. /* RESERVED */
  180. {CSRA66X0_DISABLE_PWM_OUTPUT, 0x00},
  181. /* RESERVED */
  182. {CSRA66X0_OTP_VER_FA, 0x03},
  183. {CSRA66X0_RAM_VER_FA, 0x02},
  184. /* RESERVED */
  185. {CSRA66X0_AUDIO_SATURATION_FLAGS_FA, 0x00},
  186. {CSRA66X0_DCOFFSET_CHAN_1_01_FA, 0x00},
  187. {CSRA66X0_DCOFFSET_CHAN_1_02_FA, 0x00},
  188. {CSRA66X0_DCOFFSET_CHAN_1_03_FA, 0x00},
  189. {CSRA66X0_DCOFFSET_CHAN_2_01_FA, 0x00},
  190. {CSRA66X0_DCOFFSET_CHAN_2_02_FA, 0x00},
  191. {CSRA66X0_DCOFFSET_CHAN_2_03_FA, 0x00},
  192. {CSRA66X0_FORCED_PA_SWITCHING_CTRL, 0x90},
  193. {CSRA66X0_PA_FORCE_PULSE_WIDTH, 0x07},
  194. {CSRA66X0_PA_HIGH_MODULATION_CTRL_CH1, 0x00},
  195. /* RESERVED */
  196. {CSRA66X0_HIGH_MODULATION_THRESHOLD_LOW, 0xD4},
  197. {CSRA66X0_HIGH_MODULATION_THRESHOLD_HIGH, 0x78},
  198. /* RESERVED */
  199. {CSRA66X0_PA_FREEZE_CTRL, 0x00},
  200. {CSRA66X0_DCA_FREEZE_CTRL, 0x3C},
  201. /* RESERVED */
  202. };
  203. static bool csra66x0_volatile_register(struct device *dev, unsigned int reg)
  204. {
  205. switch (reg) {
  206. case CSRA66X0_CHIP_ID_FA:
  207. case CSRA66X0_ROM_VER_FA:
  208. case CSRA66X0_CHIP_REV_0_FA:
  209. case CSRA66X0_CHIP_REV_1_FA:
  210. case CSRA66X0_TEMP_READ0_FA:
  211. case CSRA66X0_TEMP_READ1_FA:
  212. case CSRA66X0_MISC_CONTROL_STATUS_1_FA:
  213. case CSRA66X0_IRQ_OUTPUT_STATUS_FA:
  214. case CSRA66X0_CLIP_DCA_STATUS_FA:
  215. case CSRA66X0_CHIP_STATE_STATUS_FA:
  216. case CSRA66X0_FAULT_STATUS_FA:
  217. case CSRA66X0_OTP_STATUS_FA:
  218. case CSRA66X0_AUDIO_IF_STATUS_FA:
  219. case CSRA66X0_DSP_SATURATION_STATUS_FA:
  220. case CSRA66X0_AUDIO_RATE_STATUS_FA:
  221. return true;
  222. default:
  223. return false;
  224. }
  225. }
  226. static bool csra66x0_writeable_registers(struct device *dev, unsigned int reg)
  227. {
  228. if ((reg >= CSRA66X0_AUDIO_IF_RX_CONFIG1)
  229. && (reg <= CSRA66X0_MAX_REGISTER_ADDR))
  230. return true;
  231. return false;
  232. }
  233. static bool csra66x0_readable_registers(struct device *dev, unsigned int reg)
  234. {
  235. if ((reg >= CSRA66X0_AUDIO_IF_RX_CONFIG1)
  236. && (reg <= CSRA66X0_MAX_REGISTER_ADDR))
  237. return true;
  238. return false;
  239. }
  240. /* codec private data */
  241. struct csra66x0_priv {
  242. struct regmap *regmap;
  243. struct snd_soc_codec *codec;
  244. int spk_volume_ch1;
  245. int spk_volume_ch2;
  246. int irq;
  247. int vreg_gpio;
  248. u32 irq_active_low;
  249. u32 in_cluster;
  250. u32 is_master;
  251. #if IS_ENABLED(CONFIG_DEBUG_FS)
  252. struct dentry *debugfs_dir;
  253. struct dentry *debugfs_file_wo;
  254. struct dentry *debugfs_file_ro;
  255. #endif /* CONFIG_DEBUG_FS */
  256. };
  257. struct csra66x0_cluster_device {
  258. struct csra66x0_priv *csra66x0_ptr;
  259. const char *csra66x0_prefix;
  260. };
  261. struct csra66x0_cluster_device csra_clust_dev_tbl[] = {
  262. {NULL, "CSRA_12"},
  263. {NULL, "CSRA_34"},
  264. {NULL, "CSRA_56"},
  265. {NULL, "CSRA_78"},
  266. {NULL, "CSRA_9A"},
  267. {NULL, "CSRA_BC"}
  268. };
  269. #if IS_ENABLED(CONFIG_DEBUG_FS)
  270. static int debugfs_codec_open_op(struct inode *inode, struct file *file)
  271. {
  272. file->private_data = inode->i_private;
  273. return 0;
  274. }
  275. static int debugfs_get_parameters(char *buf, u32 *param1, int num_of_par)
  276. {
  277. char *token;
  278. int base, cnt;
  279. token = strsep(&buf, " ");
  280. for (cnt = 0; cnt < num_of_par; cnt++) {
  281. if (token) {
  282. if ((token[1] == 'x') || (token[1] == 'X'))
  283. base = 16;
  284. else
  285. base = 10;
  286. if (kstrtou32(token, base, &param1[cnt]) != 0)
  287. return -EINVAL;
  288. token = strsep(&buf, " ");
  289. } else {
  290. return -EINVAL;
  291. }
  292. }
  293. return 0;
  294. }
  295. static ssize_t debugfs_codec_write_op(struct file *filp,
  296. const char __user *ubuf, size_t cnt, loff_t *ppos)
  297. {
  298. struct csra66x0_priv *csra66x0 =
  299. (struct csra66x0_priv *) filp->private_data;
  300. struct snd_soc_codec *codec = csra66x0->codec;
  301. char lbuf[32];
  302. int rc;
  303. u32 param[2];
  304. if (!filp || !ppos || !ubuf)
  305. return -EINVAL;
  306. if (cnt > sizeof(lbuf) - 1)
  307. return -EINVAL;
  308. rc = copy_from_user(lbuf, ubuf, cnt);
  309. if (rc)
  310. return -EFAULT;
  311. lbuf[cnt] = '\0';
  312. rc = debugfs_get_parameters(lbuf, param, 2);
  313. if ((param[0] < CSRA66X0_AUDIO_IF_RX_CONFIG1)
  314. || (param[0] > CSRA66X0_MAX_REGISTER_ADDR)) {
  315. dev_err(codec->dev, "%s: register address 0x%04X out of range\n",
  316. __func__, param[0]);
  317. return -EINVAL;
  318. }
  319. if ((param[1] < 0) || (param[1] > 255)) {
  320. dev_err(codec->dev, "%s: register data 0x%02X out of range\n",
  321. __func__, param[1]);
  322. return -EINVAL;
  323. }
  324. if (rc == 0)
  325. {
  326. rc = cnt;
  327. dev_info(codec->dev, "%s: reg[0x%04X]=0x%02X\n",
  328. __func__, param[0], param[1]);
  329. snd_soc_write(codec, param[0], param[1]);
  330. } else {
  331. dev_err(codec->dev, "%s: write to register addr=0x%04X failed\n",
  332. __func__, param[0]);
  333. }
  334. return rc;
  335. }
  336. static ssize_t debugfs_csra66x0_reg_show(struct snd_soc_codec *codec,
  337. char __user *ubuf, size_t count, loff_t *ppos)
  338. {
  339. int i, reg_val, len;
  340. ssize_t total = 0;
  341. char tmp_buf[20];
  342. if (!ubuf || !ppos || !codec || *ppos < 0)
  343. return -EINVAL;
  344. for (i = ((int) *ppos + CSRA66X0_BASE);
  345. i <= CSRA66X0_MAX_REGISTER_ADDR; i++) {
  346. reg_val = snd_soc_read(codec, i);
  347. len = snprintf(tmp_buf, 20, "0x%04X: 0x%02X\n", i, (reg_val & 0xFF));
  348. if ((total + len) >= count - 1)
  349. break;
  350. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  351. dev_err(codec->dev, "%s: fail to copy reg dump\n", __func__);
  352. total = -EFAULT;
  353. goto copy_err;
  354. }
  355. *ppos += len;
  356. total += len;
  357. }
  358. copy_err:
  359. return total;
  360. }
  361. static ssize_t debugfs_codec_read_op(struct file *filp,
  362. char __user *ubuf, size_t cnt, loff_t *ppos)
  363. {
  364. struct csra66x0_priv *csra66x0 =
  365. (struct csra66x0_priv *) filp->private_data;
  366. struct snd_soc_codec *codec = csra66x0->codec;
  367. ssize_t ret_cnt;
  368. if (!filp || !ppos || !ubuf || *ppos < 0)
  369. return -EINVAL;
  370. ret_cnt = debugfs_csra66x0_reg_show(codec, ubuf, cnt, ppos);
  371. return ret_cnt;
  372. }
  373. static const struct file_operations debugfs_codec_ops = {
  374. .open = debugfs_codec_open_op,
  375. .write = debugfs_codec_write_op,
  376. .read = debugfs_codec_read_op,
  377. };
  378. #endif /* CONFIG_DEBUG_FS */
  379. /*
  380. * CSRA66X0 Controls
  381. */
  382. static const DECLARE_TLV_DB_SCALE(csra66x0_volume_tlv, -9000, 25, 0);
  383. static const DECLARE_TLV_DB_RANGE(csra66x0_bass_treble_tlv,
  384. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  385. 1, 15, TLV_DB_SCALE_ITEM(-1500, 100, 0),
  386. 16, 30, TLV_DB_SCALE_ITEM(100, 100, 0)
  387. );
  388. static int csra66x0_get_volume(struct snd_kcontrol *kcontrol,
  389. struct snd_ctl_elem_value *ucontrol)
  390. {
  391. struct soc_mixer_control *mc =
  392. (struct soc_mixer_control *)kcontrol->private_value;
  393. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  394. unsigned int reg_l = mc->reg;
  395. unsigned int reg_r = mc->rreg;
  396. unsigned int val_l, val_r;
  397. val_l = (snd_soc_read(codec, reg_l) & 0xff) |
  398. ((snd_soc_read(codec,
  399. CSRA66X0_CH1_VOLUME_1_FA) & (0x01)) << 8);
  400. val_r = (snd_soc_read(codec, reg_r) & 0xff) |
  401. ((snd_soc_read(codec,
  402. CSRA66X0_CH2_VOLUME_1_FA) & (0x01)) << 8);
  403. ucontrol->value.integer.value[0] = val_l;
  404. ucontrol->value.integer.value[1] = val_r;
  405. return 0;
  406. }
  407. static int csra66x0_set_volume(struct snd_kcontrol *kcontrol,
  408. struct snd_ctl_elem_value *ucontrol)
  409. {
  410. struct soc_mixer_control *mc =
  411. (struct soc_mixer_control *)kcontrol->private_value;
  412. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  413. struct csra66x0_priv *csra66x0 = snd_soc_codec_get_drvdata(codec);
  414. unsigned int reg_l = mc->reg;
  415. unsigned int reg_r = mc->rreg;
  416. unsigned int val_l[2];
  417. unsigned int val_r[2];
  418. csra66x0->spk_volume_ch1 = (ucontrol->value.integer.value[0]);
  419. csra66x0->spk_volume_ch2 = (ucontrol->value.integer.value[1]);
  420. val_l[0] = csra66x0->spk_volume_ch1 & SPK_VOLUME_LSB_MSK;
  421. val_l[1] = (csra66x0->spk_volume_ch1 & SPK_VOLUME_MSB_MSK) ? 1 : 0;
  422. val_r[0] = csra66x0->spk_volume_ch2 & SPK_VOLUME_LSB_MSK;
  423. val_r[1] = (csra66x0->spk_volume_ch2 & SPK_VOLUME_MSB_MSK) ? 1 : 0;
  424. snd_soc_write(codec, reg_l, val_l[0]);
  425. snd_soc_write(codec, reg_r, val_r[0]);
  426. snd_soc_write(codec, CSRA66X0_CH1_VOLUME_1_FA, val_l[1]);
  427. snd_soc_write(codec, CSRA66X0_CH2_VOLUME_1_FA, val_r[1]);
  428. return 0;
  429. }
  430. /* enumerated controls */
  431. static const char * const csra66x0_mute_output_text[] = {"PLAY", "MUTE"};
  432. static const char * const csra66x0_output_invert_text[] = {
  433. "UNCHANGED", "INVERTED"};
  434. static const char * const csra66x0_deemp_config_text[] = {
  435. "DISABLED", "ENABLED"};
  436. SOC_ENUM_SINGLE_DECL(csra66x0_mute_output_enum,
  437. CSRA66X0_MISC_CONTROL_STATUS_1_FA, 2,
  438. csra66x0_mute_output_text);
  439. SOC_ENUM_SINGLE_DECL(csra66x0_ch1_output_invert_enum,
  440. CSRA66X0_CH1_OUTPUT_INVERT_EN, 0,
  441. csra66x0_output_invert_text);
  442. SOC_ENUM_SINGLE_DECL(csra66x0_ch2_output_invert_enum,
  443. CSRA66X0_CH2_OUTPUT_INVERT_EN, 0,
  444. csra66x0_output_invert_text);
  445. SOC_ENUM_DOUBLE_DECL(csra66x0_deemp_config_enum,
  446. CSRA66X0_DEEMP_CONFIG_FA, 0, 1,
  447. csra66x0_deemp_config_text);
  448. static const struct snd_kcontrol_new csra66x0_snd_controls[] = {
  449. /* volume */
  450. SOC_DOUBLE_R_EXT_TLV("PA VOLUME", CSRA66X0_CH1_VOLUME_0_FA,
  451. CSRA66X0_CH2_VOLUME_0_FA, 0, 0x1C9, 0,
  452. csra66x0_get_volume, csra66x0_set_volume,
  453. csra66x0_volume_tlv),
  454. /* bass treble */
  455. SOC_DOUBLE_R_TLV("PA BASS GAIN", CSRA66X0_CH1_BASS_GAIN_CTRL_FA,
  456. CSRA66X0_CH2_BASS_GAIN_CTRL_FA, 0, 0x1E, 0,
  457. csra66x0_bass_treble_tlv),
  458. SOC_DOUBLE_R_TLV("PA TREBLE GAIN", CSRA66X0_CH1_TREBLE_GAIN_CTRL_FA,
  459. CSRA66X0_CH2_TREBLE_GAIN_CTRL_FA, 0, 0x1E, 0,
  460. csra66x0_bass_treble_tlv),
  461. SOC_DOUBLE_R("PA BASS_XOVER FREQ", CSRA66X0_CH1_BASS_FC_CTRL_FA,
  462. CSRA66X0_CH2_BASS_FC_CTRL_FA, 0, 2, 0),
  463. SOC_DOUBLE_R("PA TREBLE_XOVER FREQ", CSRA66X0_CH1_TREBLE_FC_CTRL_FA,
  464. CSRA66X0_CH2_TREBLE_FC_CTRL_FA, 0, 2, 0),
  465. /* switch */
  466. SOC_ENUM("PA MUTE_OUTPUT SWITCH", csra66x0_mute_output_enum),
  467. SOC_ENUM("PA DE-EMPHASIS SWITCH", csra66x0_deemp_config_enum),
  468. };
  469. static const struct snd_kcontrol_new csra_mix_switch[] = {
  470. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  471. };
  472. static const struct snd_soc_dapm_widget csra66x0_dapm_widgets[] = {
  473. SND_SOC_DAPM_INPUT("IN"),
  474. SND_SOC_DAPM_MIXER("MIXER", SND_SOC_NOPM, 0, 0,
  475. csra_mix_switch, ARRAY_SIZE(csra_mix_switch)),
  476. SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
  477. SND_SOC_DAPM_PGA("PGA", CSRA66X0_CHIP_STATE_CTRL_FA, 0, 0, NULL, 0),
  478. SND_SOC_DAPM_OUTPUT("SPKR"),
  479. SND_SOC_DAPM_SUPPLY("POWER", CSRA66X0_CHIP_STATE_CTRL_FA,
  480. 1, 1, NULL, 0),
  481. };
  482. static const struct snd_soc_dapm_route csra66x0_dapm_routes[] = {
  483. {"MIXER", "Switch", "IN"},
  484. {"DAC", NULL, "MIXER"},
  485. {"PGA", NULL, "DAC"},
  486. {"SPKR", NULL, "PGA"},
  487. {"SPKR", NULL, "POWER"},
  488. };
  489. static int csra66x0_init(struct csra66x0_priv *csra66x0)
  490. {
  491. struct snd_soc_codec *codec = csra66x0->codec;
  492. dev_dbg(codec->dev, "%s: initialize %s\n",
  493. __func__, codec->component.name);
  494. /* config */
  495. snd_soc_write(codec, CSRA66X0_CHIP_STATE_CTRL_FA, CONFIG_STATE);
  496. /* settle time in HW is min. 500ms before proceeding */
  497. msleep(500);
  498. /* setup */
  499. snd_soc_write(codec, CSRA66X0_MISC_CONTROL_STATUS_0, 0x09);
  500. snd_soc_write(codec, CSRA66X0_TEMP_PROT_BACKOFF, 0x0C);
  501. snd_soc_write(codec, CSRA66X0_EXT_PA_PROTECT_POLARITY, 0x03);
  502. snd_soc_write(codec, CSRA66X0_PWM_OUTPUT_CONFIG, 0xC8);
  503. csra66x0->spk_volume_ch1 = SPK_VOLUME_M20DB;
  504. csra66x0->spk_volume_ch2 = SPK_VOLUME_M20DB;
  505. snd_soc_write(codec, CSRA66X0_CH1_VOLUME_0_FA, SPK_VOLUME_M20DB_LSB);
  506. snd_soc_write(codec, CSRA66X0_CH2_VOLUME_0_FA, SPK_VOLUME_M20DB_LSB);
  507. snd_soc_write(codec, CSRA66X0_CH1_VOLUME_1_FA, SPK_VOLUME_M20DB_MSB);
  508. snd_soc_write(codec, CSRA66X0_CH2_VOLUME_1_FA, SPK_VOLUME_M20DB_MSB);
  509. snd_soc_write(codec, CSRA66X0_DEAD_TIME_CTRL, 0x0);
  510. snd_soc_write(codec, CSRA66X0_DEAD_TIME_THRESHOLD_0, 0xE7);
  511. snd_soc_write(codec, CSRA66X0_DEAD_TIME_THRESHOLD_1, 0x26);
  512. snd_soc_write(codec, CSRA66X0_DEAD_TIME_THRESHOLD_2, 0x40);
  513. snd_soc_write(codec, CSRA66X0_MIN_MODULATION_PULSE_WIDTH, 0x7A);
  514. snd_soc_write(codec, CSRA66X0_CH1_HARD_CLIP_THRESH, 0x00);
  515. snd_soc_write(codec, CSRA66X0_CH2_HARD_CLIP_THRESH, 0x00);
  516. snd_soc_write(codec, CSRA66X0_CH1_DCA_THRESH, 0x40);
  517. snd_soc_write(codec, CSRA66X0_CH2_DCA_THRESH, 0x40);
  518. snd_soc_write(codec, CSRA66X0_DCA_ATTACK_RATE, 0x00);
  519. snd_soc_write(codec, CSRA66X0_DCA_RELEASE_RATE, 0x00);
  520. if (csra66x0->irq) {
  521. snd_soc_write(codec, CSRA66X0_PIO0_SELECT, 0x1);
  522. if (csra66x0->irq_active_low)
  523. snd_soc_write(codec, CSRA66X0_IRQ_OUTPUT_POLARITY, 0x0);
  524. else
  525. snd_soc_write(codec, CSRA66X0_IRQ_OUTPUT_POLARITY, 0x1);
  526. snd_soc_write(codec, CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  527. } else {
  528. snd_soc_write(codec, CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  529. }
  530. /* settle time in HW is min. 500ms before slave initializing */
  531. msleep(500);
  532. return 0;
  533. }
  534. static int csra66x0_reset(struct csra66x0_priv *csra66x0)
  535. {
  536. struct snd_soc_codec *codec = csra66x0->codec;
  537. u16 val;
  538. val = snd_soc_read(codec, CSRA66X0_FAULT_STATUS_FA);
  539. if (val & FAULT_STATUS_INTERNAL)
  540. dev_dbg(codec->dev, "%s: FAULT_STATUS_INTERNAL 0x%X\n",
  541. __func__, val);
  542. if (val & FAULT_STATUS_OTP_INTEGRITY)
  543. dev_dbg(codec->dev, "%s: FAULT_STATUS_OTP_INTEGRITY 0x%X\n",
  544. __func__, val);
  545. if (val & FAULT_STATUS_PADS2)
  546. dev_dbg(codec->dev, "%s: FAULT_STATUS_PADS2 0x%X\n",
  547. __func__, val);
  548. if (val & FAULT_STATUS_SMPS)
  549. dev_dbg(codec->dev, "%s: FAULT_STATUS_SMPS 0x%X\n",
  550. __func__, val);
  551. if (val & FAULT_STATUS_TEMP)
  552. dev_dbg(codec->dev, "%s: FAULT_STATUS_TEMP 0x%X\n",
  553. __func__, val);
  554. if (val & FAULT_STATUS_PROTECT)
  555. dev_dbg(codec->dev, "%s: FAULT_STATUS_PROTECT 0x%X\n",
  556. __func__, val);
  557. dev_dbg(codec->dev, "%s: reset %s\n",
  558. __func__, codec->component.name);
  559. /* clear fault state and re-init */
  560. snd_soc_write(codec, CSRA66X0_FAULT_STATUS_FA, 0x00);
  561. snd_soc_write(codec, CSRA66X0_IRQ_OUTPUT_STATUS_FA, 0x00);
  562. /* apply reset to CSRA66X0 */
  563. val = snd_soc_read(codec, CSRA66X0_MISC_CONTROL_STATUS_1_FA);
  564. snd_soc_write(codec, CSRA66X0_MISC_CONTROL_STATUS_1_FA, val | 0x08);
  565. /* wait 500ms after reset to recover CSRA66X0 */
  566. msleep(500);
  567. return 0;
  568. }
  569. static int csra66x0_msconfig(struct csra66x0_priv *csra66x0)
  570. {
  571. struct snd_soc_codec *codec = csra66x0->codec;
  572. dev_dbg(codec->dev, "%s: configure %s\n",
  573. __func__, codec->component.name);
  574. /* config */
  575. snd_soc_write(codec, CSRA66X0_CHIP_STATE_CTRL_FA,
  576. CONFIG_STATE);
  577. /* settle time in HW is min. 500ms before proceeding */
  578. msleep(500);
  579. snd_soc_write(codec, CSRA66X0_PIO7_SELECT, 0x04);
  580. snd_soc_write(codec, CSRA66X0_PIO8_SELECT, 0x04);
  581. if (csra66x0->is_master) {
  582. /* Master specific config */
  583. snd_soc_write(codec, CSRA66X0_PIO_PULL_EN0, 0xFF);
  584. snd_soc_write(codec, CSRA66X0_PIO_PULL_DIR0, 0x80);
  585. snd_soc_write(codec, CSRA66X0_PIO_PULL_EN1, 0x01);
  586. snd_soc_write(codec, CSRA66X0_PIO_PULL_DIR1, 0x01);
  587. } else {
  588. /* Slave specific config */
  589. snd_soc_write(codec, CSRA66X0_PIO_PULL_EN0, 0x7F);
  590. snd_soc_write(codec, CSRA66X0_PIO_PULL_EN1, 0x00);
  591. }
  592. snd_soc_write(codec, CSRA66X0_DCA_CTRL, 0x05);
  593. return 0;
  594. }
  595. static int csra66x0_soc_probe(struct snd_soc_codec *codec)
  596. {
  597. struct csra66x0_priv *csra66x0 = snd_soc_codec_get_drvdata(codec);
  598. struct snd_soc_dapm_context *dapm;
  599. char name[50];
  600. unsigned int i, max_num_cluster_devices;
  601. csra66x0->codec = codec;
  602. if (csra66x0->in_cluster) {
  603. dapm = snd_soc_codec_get_dapm(codec);
  604. dev_dbg(codec->dev, "%s: assign prefix %s to codec device %s\n",
  605. __func__, codec->component.name_prefix,
  606. codec->component.name);
  607. /* add device to cluster table */
  608. max_num_cluster_devices = sizeof(csra_clust_dev_tbl)/
  609. sizeof(csra_clust_dev_tbl[0]);
  610. for (i = 0; i < max_num_cluster_devices; i++) {
  611. if (!strncmp(codec->component.name_prefix,
  612. csra_clust_dev_tbl[i].csra66x0_prefix,
  613. strlen(
  614. csra_clust_dev_tbl[i].csra66x0_prefix))) {
  615. csra_clust_dev_tbl[i].csra66x0_ptr = csra66x0;
  616. break;
  617. }
  618. if (i == max_num_cluster_devices-1)
  619. dev_warn(codec->dev,
  620. "%s: Unknown prefix %s of cluster device %s\n",
  621. __func__, codec->component.name_prefix,
  622. codec->component.name);
  623. }
  624. /* master slave config */
  625. csra66x0_msconfig(csra66x0);
  626. if (dapm->component) {
  627. strlcpy(name, dapm->component->name_prefix,
  628. sizeof(name));
  629. strlcat(name, " IN", sizeof(name));
  630. snd_soc_dapm_ignore_suspend(dapm, name);
  631. strlcpy(name, dapm->component->name_prefix,
  632. sizeof(name));
  633. strlcat(name, " SPKR", sizeof(name));
  634. snd_soc_dapm_ignore_suspend(dapm, name);
  635. }
  636. }
  637. /* common initialization */
  638. csra66x0_init(csra66x0);
  639. return 0;
  640. }
  641. static int csra66x0_soc_remove(struct snd_soc_codec *codec)
  642. {
  643. snd_soc_write(codec, CSRA66X0_CHIP_STATE_CTRL_FA, STDBY_STATE);
  644. return 0;
  645. }
  646. static int csra66x0_soc_suspend(struct snd_soc_codec *codec)
  647. {
  648. u16 state_reg = snd_soc_read(codec, CSRA66X0_CHIP_STATE_CTRL_FA) & 0xFC;
  649. snd_soc_write(codec, CSRA66X0_CHIP_STATE_CTRL_FA, state_reg |
  650. STDBY_STATE);
  651. return 0;
  652. }
  653. static int csra66x0_soc_resume(struct snd_soc_codec *codec)
  654. {
  655. u16 state_reg = snd_soc_read(codec, CSRA66X0_CHIP_STATE_CTRL_FA) & 0xFC;
  656. snd_soc_write(codec, CSRA66X0_CHIP_STATE_CTRL_FA, state_reg |
  657. RUN_STATE);
  658. return 0;
  659. }
  660. static struct regmap *csra66x0_get_regmap(struct device *dev)
  661. {
  662. struct csra66x0_priv *csra66x0_ctrl = dev_get_drvdata(dev);
  663. if (!csra66x0_ctrl)
  664. return NULL;
  665. return csra66x0_ctrl->regmap;
  666. }
  667. static struct snd_soc_codec_driver soc_codec_drv_csra66x0 = {
  668. .probe = csra66x0_soc_probe,
  669. .remove = csra66x0_soc_remove,
  670. .suspend = csra66x0_soc_suspend,
  671. .resume = csra66x0_soc_resume,
  672. .get_regmap = csra66x0_get_regmap,
  673. .component_driver = {
  674. .controls = csra66x0_snd_controls,
  675. .num_controls = ARRAY_SIZE(csra66x0_snd_controls),
  676. .dapm_widgets = csra66x0_dapm_widgets,
  677. .num_dapm_widgets = ARRAY_SIZE(csra66x0_dapm_widgets),
  678. .dapm_routes = csra66x0_dapm_routes,
  679. .num_dapm_routes = ARRAY_SIZE(csra66x0_dapm_routes),
  680. },
  681. };
  682. static struct regmap_config csra66x0_regmap_config = {
  683. .reg_bits = 16,
  684. .val_bits = 8,
  685. .cache_type = REGCACHE_RBTREE,
  686. .reg_defaults = csra66x0_reg_defaults,
  687. .num_reg_defaults = ARRAY_SIZE(csra66x0_reg_defaults),
  688. .max_register = CSRA66X0_MAX_REGISTER_ADDR,
  689. .volatile_reg = csra66x0_volatile_register,
  690. .writeable_reg = csra66x0_writeable_registers,
  691. .readable_reg = csra66x0_readable_registers,
  692. };
  693. static irqreturn_t csra66x0_irq(int irq, void *data)
  694. {
  695. struct csra66x0_priv *csra66x0 = (struct csra66x0_priv *) data;
  696. struct snd_soc_codec *codec = csra66x0->codec;
  697. u16 val;
  698. unsigned int i, max_num_cluster_devices;
  699. /* Treat interrupt before codec is initialized as spurious */
  700. if (codec == NULL)
  701. return IRQ_NONE;
  702. dev_dbg(codec->dev, "%s: csra66x0_interrupt triggered by %s\n",
  703. __func__, codec->component.name);
  704. /* fault indication */
  705. val = snd_soc_read(codec, CSRA66X0_IRQ_OUTPUT_STATUS_FA) & 0x1;
  706. if (!val)
  707. return IRQ_HANDLED;
  708. if (csra66x0->in_cluster) {
  709. /* reset all slave codecs */
  710. max_num_cluster_devices =
  711. sizeof(csra_clust_dev_tbl) /
  712. sizeof(csra_clust_dev_tbl[0]);
  713. for (i = 0; i < max_num_cluster_devices; i++) {
  714. if (i >= codec->component.card->num_aux_devs)
  715. break;
  716. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  717. continue;
  718. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  719. continue;
  720. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  721. }
  722. /* reset all master codecs */
  723. for (i = 0; i < max_num_cluster_devices; i++) {
  724. if (i >= codec->component.card->num_aux_devs)
  725. break;
  726. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  727. continue;
  728. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  729. csra66x0_reset(
  730. csra_clust_dev_tbl[i].csra66x0_ptr);
  731. }
  732. /* recover all codecs */
  733. for (i = 0; i < max_num_cluster_devices; i++) {
  734. if (i >= codec->component.card->num_aux_devs)
  735. break;
  736. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  737. continue;
  738. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  739. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  740. }
  741. } else {
  742. csra66x0_reset(csra66x0);
  743. csra66x0_init(csra66x0);
  744. }
  745. return IRQ_HANDLED;
  746. };
  747. static const struct of_device_id csra66x0_of_match[] = {
  748. { .compatible = "qcom,csra66x0", },
  749. { }
  750. };
  751. MODULE_DEVICE_TABLE(of, csra66x0_of_match);
  752. #if IS_ENABLED(CONFIG_I2C)
  753. static int csra66x0_i2c_probe(struct i2c_client *client_i2c,
  754. const struct i2c_device_id *id)
  755. {
  756. struct csra66x0_priv *csra66x0;
  757. int ret, irq_trigger;
  758. char debugfs_dir_name[32];
  759. csra66x0 = devm_kzalloc(&client_i2c->dev, sizeof(struct csra66x0_priv),
  760. GFP_KERNEL);
  761. if (csra66x0 == NULL)
  762. return -ENOMEM;
  763. csra66x0->regmap = devm_regmap_init_i2c(client_i2c,
  764. &csra66x0_regmap_config);
  765. if (IS_ERR(csra66x0->regmap)) {
  766. ret = PTR_ERR(csra66x0->regmap);
  767. dev_err(&client_i2c->dev,
  768. "%s %d: Failed to allocate register map for I2C device: %d\n",
  769. __func__, __LINE__, ret);
  770. return ret;
  771. }
  772. i2c_set_clientdata(client_i2c, csra66x0);
  773. /* get data from device tree */
  774. if (client_i2c->dev.of_node) {
  775. /* cluster of multiple devices */
  776. ret = of_property_read_u32(
  777. client_i2c->dev.of_node, "qcom,csra-cluster",
  778. &csra66x0->in_cluster);
  779. if (ret) {
  780. dev_info(&client_i2c->dev,
  781. "%s: qcom,csra-cluster property not defined in DT\n", __func__);
  782. csra66x0->in_cluster = 0;
  783. }
  784. /* master or slave device */
  785. ret = of_property_read_u32(
  786. client_i2c->dev.of_node, "qcom,csra-cluster-master",
  787. &csra66x0->is_master);
  788. if (ret) {
  789. dev_info(&client_i2c->dev,
  790. "%s: qcom,csra-cluster-master property not defined in DT, slave assumed\n",
  791. __func__);
  792. csra66x0->is_master = 0;
  793. }
  794. /* gpio setup for vreg */
  795. csra66x0->vreg_gpio = of_get_named_gpio(client_i2c->dev.of_node,
  796. "qcom,csra-vreg-en-gpio", 0);
  797. if (!gpio_is_valid(csra66x0->vreg_gpio)) {
  798. dev_err(&client_i2c->dev, "%s: %s property is not found %d\n",
  799. __func__, "qcom,csra-vreg-en-gpio",
  800. csra66x0->vreg_gpio);
  801. return -ENODEV;
  802. }
  803. dev_dbg(&client_i2c->dev, "%s: vreg_en gpio %d\n", __func__,
  804. csra66x0->vreg_gpio);
  805. ret = gpio_request(csra66x0->vreg_gpio, dev_name(&client_i2c->dev));
  806. if (ret) {
  807. if (ret == -EBUSY) {
  808. /* GPIO was already requested */
  809. dev_dbg(&client_i2c->dev,
  810. "%s: gpio %d is already set\n",
  811. __func__, csra66x0->vreg_gpio);
  812. } else {
  813. dev_err(&client_i2c->dev, "%s: Failed to request gpio %d, err: %d\n",
  814. __func__, csra66x0->vreg_gpio, ret);
  815. }
  816. } else {
  817. gpio_direction_output(csra66x0->vreg_gpio, 1);
  818. gpio_set_value(csra66x0->vreg_gpio, 0);
  819. }
  820. /* register interrupt handle */
  821. if (client_i2c->irq) {
  822. csra66x0->irq = client_i2c->irq;
  823. /* interrupt polarity */
  824. ret = of_property_read_u32(
  825. client_i2c->dev.of_node, "irq-active-low",
  826. &csra66x0->irq_active_low);
  827. if (ret) {
  828. dev_info(&client_i2c->dev,
  829. "%s: irq-active-low property not defined in DT\n", __func__);
  830. csra66x0->irq_active_low = 0;
  831. }
  832. if (csra66x0->irq_active_low)
  833. irq_trigger = IRQF_TRIGGER_LOW;
  834. else
  835. irq_trigger = IRQF_TRIGGER_HIGH;
  836. ret = devm_request_threaded_irq(&client_i2c->dev,
  837. csra66x0->irq, NULL, csra66x0_irq,
  838. irq_trigger | IRQF_ONESHOT,
  839. "csra66x0_irq", csra66x0);
  840. if (ret) {
  841. dev_err(&client_i2c->dev,
  842. "%s: Failed to request IRQ %d: %d\n",
  843. __func__, csra66x0->irq, ret);
  844. csra66x0->irq = 0;
  845. }
  846. }
  847. }
  848. #if IS_ENABLED(CONFIG_DEBUG_FS)
  849. /* debugfs interface */
  850. snprintf(debugfs_dir_name, sizeof(debugfs_dir_name), "%s-%s",
  851. client_i2c->name, dev_name(&client_i2c->dev));
  852. csra66x0->debugfs_dir = debugfs_create_dir(debugfs_dir_name, NULL);
  853. if (!csra66x0->debugfs_dir) {
  854. dev_dbg(&client_i2c->dev,
  855. "%s: Failed to create /sys/kernel/debug/%s for debugfs\n",
  856. __func__, debugfs_dir_name);
  857. return -ENOMEM;
  858. }
  859. csra66x0->debugfs_file_wo = debugfs_create_file(
  860. "write_reg_val", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  861. (void *) csra66x0,
  862. &debugfs_codec_ops);
  863. if (!csra66x0->debugfs_file_wo) {
  864. dev_dbg(&client_i2c->dev,
  865. "%s: Failed to create /sys/kernel/debug/%s/write_reg_val\n",
  866. __func__, debugfs_dir_name);
  867. return -ENOMEM;
  868. }
  869. csra66x0->debugfs_file_ro = debugfs_create_file(
  870. "show_reg_dump", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  871. (void *) csra66x0,
  872. &debugfs_codec_ops);
  873. if (!csra66x0->debugfs_file_ro) {
  874. dev_dbg(&client_i2c->dev,
  875. "%s: Failed to create /sys/kernel/debug/%s/show_reg_dump\n",
  876. __func__, debugfs_dir_name);
  877. return -ENOMEM;
  878. }
  879. #endif /* CONFIG_DEBUG_FS */
  880. /* register codec */
  881. ret = snd_soc_register_codec(&client_i2c->dev,
  882. &soc_codec_drv_csra66x0, NULL, 0);
  883. if (ret != 0) {
  884. dev_err(&client_i2c->dev, "%s %d: Failed to register codec: %d\n",
  885. __func__, __LINE__, ret);
  886. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  887. gpio_set_value(csra66x0->vreg_gpio, 0);
  888. gpio_free(csra66x0->vreg_gpio);
  889. }
  890. return ret;
  891. }
  892. return 0;
  893. }
  894. static int csra66x0_i2c_remove(struct i2c_client *i2c_client)
  895. {
  896. struct csra66x0_priv *csra66x0 = i2c_get_clientdata(i2c_client);
  897. if (csra66x0) {
  898. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  899. gpio_set_value(csra66x0->vreg_gpio, 0);
  900. gpio_free(csra66x0->vreg_gpio);
  901. }
  902. #if IS_ENABLED(CONFIG_DEBUG_FS)
  903. debugfs_remove_recursive(csra66x0->debugfs_dir);
  904. #endif
  905. }
  906. snd_soc_unregister_codec(&i2c_client->dev);
  907. return 0;
  908. }
  909. static const struct i2c_device_id csra66x0_i2c_id[] = {
  910. { "csra66x0", 0},
  911. { }
  912. };
  913. MODULE_DEVICE_TABLE(i2c, csra66x0_i2c_id);
  914. static struct i2c_driver csra66x0_i2c_driver = {
  915. .probe = csra66x0_i2c_probe,
  916. .remove = csra66x0_i2c_remove,
  917. .id_table = csra66x0_i2c_id,
  918. .driver = {
  919. .name = "csra66x0",
  920. .owner = THIS_MODULE,
  921. .of_match_table = csra66x0_of_match
  922. },
  923. };
  924. #endif
  925. static int __init csra66x0_codec_init(void)
  926. {
  927. int ret = 0;
  928. #if IS_ENABLED(CONFIG_I2C)
  929. ret = i2c_add_driver(&csra66x0_i2c_driver);
  930. if (ret != 0)
  931. pr_err("%s: Failed to register CSRA66X0 I2C driver, ret = %d\n",
  932. __func__, ret);
  933. #endif
  934. return ret;
  935. }
  936. module_init(csra66x0_codec_init);
  937. static void __exit csra66x0_codec_exit(void)
  938. {
  939. #if IS_ENABLED(CONFIG_I2C)
  940. i2c_del_driver(&csra66x0_i2c_driver);
  941. #endif
  942. }
  943. module_exit(csra66x0_codec_exit);
  944. MODULE_DESCRIPTION("CSRA66X0 Codec driver");
  945. MODULE_LICENSE("GPL v2");