csra66x0.c 34 KB

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