csra66x0.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2019, 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. #define CSRA66X0_SYSFS_ENTRY_MAX_LEN 64
  23. /* CSRA66X0 register default values */
  24. static struct reg_default csra66x0_reg_defaults[] = {
  25. {CSRA66X0_AUDIO_IF_RX_CONFIG1, 0x00},
  26. {CSRA66X0_AUDIO_IF_RX_CONFIG2, 0x0B},
  27. {CSRA66X0_AUDIO_IF_RX_CONFIG3, 0x0F},
  28. {CSRA66X0_AUDIO_IF_TX_EN, 0x00},
  29. {CSRA66X0_AUDIO_IF_TX_CONFIG1, 0x6B},
  30. {CSRA66X0_AUDIO_IF_TX_CONFIG2, 0x02},
  31. {CSRA66X0_I2C_DEVICE_ADDRESS, 0x0D},
  32. {CSRA66X0_CHIP_ID_FA, 0x39},
  33. {CSRA66X0_ROM_VER_FA, 0x08},
  34. {CSRA66X0_CHIP_REV_0_FA, 0x05},
  35. {CSRA66X0_CHIP_REV_1_FA, 0x03},
  36. {CSRA66X0_CH1_MIX_SEL, 0x01},
  37. {CSRA66X0_CH2_MIX_SEL, 0x10},
  38. {CSRA66X0_CH1_SAMPLE1_SCALE_0, 0x00},
  39. {CSRA66X0_CH1_SAMPLE1_SCALE_1, 0x20},
  40. {CSRA66X0_CH1_SAMPLE3_SCALE_0, 0x00},
  41. {CSRA66X0_CH1_SAMPLE3_SCALE_1, 0x20},
  42. {CSRA66X0_CH1_SAMPLE5_SCALE_0, 0x00},
  43. {CSRA66X0_CH1_SAMPLE5_SCALE_1, 0x20},
  44. {CSRA66X0_CH1_SAMPLE7_SCALE_0, 0x00},
  45. {CSRA66X0_CH1_SAMPLE7_SCALE_1, 0x20},
  46. {CSRA66X0_CH1_SAMPLE2_SCALE_0, 0x00},
  47. {CSRA66X0_CH1_SAMPLE2_SCALE_1, 0x20},
  48. {CSRA66X0_CH1_SAMPLE4_SCALE_0, 0x00},
  49. {CSRA66X0_CH1_SAMPLE4_SCALE_1, 0x20},
  50. {CSRA66X0_CH1_SAMPLE6_SCALE_0, 0x00},
  51. {CSRA66X0_CH1_SAMPLE6_SCALE_1, 0x20},
  52. {CSRA66X0_CH1_SAMPLE8_SCALE_0, 0x00},
  53. {CSRA66X0_CH1_SAMPLE8_SCALE_1, 0x20},
  54. {CSRA66X0_CH2_SAMPLE1_SCALE_0, 0x00},
  55. {CSRA66X0_CH2_SAMPLE1_SCALE_1, 0x20},
  56. {CSRA66X0_CH2_SAMPLE3_SCALE_0, 0x00},
  57. {CSRA66X0_CH2_SAMPLE3_SCALE_1, 0x20},
  58. {CSRA66X0_CH2_SAMPLE5_SCALE_0, 0x00},
  59. {CSRA66X0_CH2_SAMPLE5_SCALE_1, 0x20},
  60. {CSRA66X0_CH2_SAMPLE7_SCALE_0, 0x00},
  61. {CSRA66X0_CH2_SAMPLE7_SCALE_1, 0x20},
  62. {CSRA66X0_CH2_SAMPLE2_SCALE_0, 0x00},
  63. {CSRA66X0_CH2_SAMPLE2_SCALE_1, 0x20},
  64. {CSRA66X0_CH2_SAMPLE4_SCALE_0, 0x00},
  65. {CSRA66X0_CH2_SAMPLE4_SCALE_1, 0x20},
  66. {CSRA66X0_CH2_SAMPLE6_SCALE_0, 0x00},
  67. {CSRA66X0_CH2_SAMPLE6_SCALE_1, 0x20},
  68. {CSRA66X0_CH2_SAMPLE8_SCALE_0, 0x00},
  69. {CSRA66X0_CH2_SAMPLE8_SCALE_1, 0x20},
  70. {CSRA66X0_VOLUME_CONFIG_FA, 0x26},
  71. {CSRA66X0_STARTUP_DELAY_FA, 0x00},
  72. {CSRA66X0_CH1_VOLUME_0_FA, 0x19},
  73. {CSRA66X0_CH1_VOLUME_1_FA, 0x01},
  74. {CSRA66X0_CH2_VOLUME_0_FA, 0x19},
  75. {CSRA66X0_CH2_VOLUME_1_FA, 0x01},
  76. {CSRA66X0_QUAD_ENC_COUNT_0_FA, 0x00},
  77. {CSRA66X0_QUAD_ENC_COUNT_1_FA, 0x00},
  78. {CSRA66X0_SOFT_CLIP_CONFIG, 0x00},
  79. {CSRA66X0_CH1_HARD_CLIP_THRESH, 0x00},
  80. {CSRA66X0_CH2_HARD_CLIP_THRESH, 0x00},
  81. {CSRA66X0_SOFT_CLIP_THRESH, 0x00},
  82. {CSRA66X0_DS_ENABLE_THRESH_0, 0x05},
  83. {CSRA66X0_DS_ENABLE_THRESH_1, 0x00},
  84. {CSRA66X0_DS_TARGET_COUNT_0, 0x00},
  85. {CSRA66X0_DS_TARGET_COUNT_1, 0xFF},
  86. {CSRA66X0_DS_TARGET_COUNT_2, 0xFF},
  87. {CSRA66X0_DS_DISABLE_THRESH_0, 0x0F},
  88. {CSRA66X0_DS_DISABLE_THRESH_1, 0x00},
  89. {CSRA66X0_DCA_CTRL, 0x07},
  90. {CSRA66X0_CH1_DCA_THRESH, 0x40},
  91. {CSRA66X0_CH2_DCA_THRESH, 0x40},
  92. {CSRA66X0_DCA_ATTACK_RATE, 0x00},
  93. {CSRA66X0_DCA_RELEASE_RATE, 0x00},
  94. {CSRA66X0_CH1_OUTPUT_INVERT_EN, 0x00},
  95. {CSRA66X0_CH2_OUTPUT_INVERT_EN, 0x00},
  96. {CSRA66X0_CH1_176P4K_DELAY, 0x00},
  97. {CSRA66X0_CH2_176P4K_DELAY, 0x00},
  98. {CSRA66X0_CH1_192K_DELAY, 0x00},
  99. {CSRA66X0_CH2_192K_DELAY, 0x00},
  100. {CSRA66X0_DEEMP_CONFIG_FA, 0x00},
  101. {CSRA66X0_CH1_TREBLE_GAIN_CTRL_FA, 0x00},
  102. {CSRA66X0_CH2_TREBLE_GAIN_CTRL_FA, 0x00},
  103. {CSRA66X0_CH1_TREBLE_FC_CTRL_FA, 0x00},
  104. {CSRA66X0_CH2_TREBLE_FC_CTRL_FA, 0x00},
  105. {CSRA66X0_CH1_BASS_GAIN_CTRL_FA, 0x00},
  106. {CSRA66X0_CH2_BASS_GAIN_CTRL_FA, 0x00},
  107. {CSRA66X0_CH1_BASS_FC_CTRL_FA, 0x00},
  108. {CSRA66X0_CH2_BASS_FC_CTRL_FA, 0x00},
  109. {CSRA66X0_FILTER_SEL_8K, 0x00},
  110. {CSRA66X0_FILTER_SEL_11P025K, 0x00},
  111. {CSRA66X0_FILTER_SEL_16K, 0x00},
  112. {CSRA66X0_FILTER_SEL_22P05K, 0x00},
  113. {CSRA66X0_FILTER_SEL_32K, 0x00},
  114. {CSRA66X0_FILTER_SEL_44P1K_48K, 0x00},
  115. {CSRA66X0_FILTER_SEL_88P2K_96K, 0x00},
  116. {CSRA66X0_FILTER_SEL_176P4K_192K, 0x00},
  117. /* RESERVED */
  118. {CSRA66X0_USER_DSP_CTRL, 0x00},
  119. {CSRA66X0_TEST_TONE_CTRL, 0x00},
  120. {CSRA66X0_TEST_TONE_FREQ_0, 0x00},
  121. {CSRA66X0_TEST_TONE_FREQ_1, 0x00},
  122. {CSRA66X0_TEST_TONE_FREQ_2, 0x00},
  123. {CSRA66X0_AUDIO_RATE_CTRL_FA, 0x08},
  124. {CSRA66X0_MODULATION_INDEX_CTRL, 0x3F},
  125. {CSRA66X0_MODULATION_INDEX_COUNT, 0x10},
  126. {CSRA66X0_MIN_MODULATION_PULSE_WIDTH, 0x7A},
  127. {CSRA66X0_DEAD_TIME_CTRL, 0x00},
  128. {CSRA66X0_DEAD_TIME_THRESHOLD_0, 0xE7},
  129. {CSRA66X0_DEAD_TIME_THRESHOLD_1, 0x26},
  130. {CSRA66X0_DEAD_TIME_THRESHOLD_2, 0x40},
  131. {CSRA66X0_CH1_LOW_SIDE_DLY, 0x00},
  132. {CSRA66X0_CH2_LOW_SIDE_DLY, 0x00},
  133. {CSRA66X0_SPECTRUM_CTRL, 0x00},
  134. /* RESERVED */
  135. {CSRA66X0_SPECTRUM_SPREAD_CTRL, 0x0C},
  136. /* RESERVED */
  137. {CSRA66X0_EXT_PA_PROTECT_POLARITY, 0x03},
  138. {CSRA66X0_TEMP0_BACKOFF_COMP_VALUE, 0x98},
  139. {CSRA66X0_TEMP0_SHUTDOWN_COMP_VALUE, 0xA3},
  140. {CSRA66X0_TEMP1_BACKOFF_COMP_VALUE, 0x98},
  141. {CSRA66X0_TEMP1_SHUTDOWN_COMP_VALUE, 0xA3},
  142. {CSRA66X0_TEMP_PROT_BACKOFF, 0x00},
  143. {CSRA66X0_TEMP_READ0_FA, 0x00},
  144. {CSRA66X0_TEMP_READ1_FA, 0x00},
  145. {CSRA66X0_CHIP_STATE_CTRL_FA, 0x02},
  146. /* RESERVED */
  147. {CSRA66X0_PWM_OUTPUT_CONFIG, 0x00},
  148. {CSRA66X0_MISC_CONTROL_STATUS_0, 0x08},
  149. {CSRA66X0_MISC_CONTROL_STATUS_1_FA, 0x40},
  150. {CSRA66X0_PIO0_SELECT, 0x00},
  151. {CSRA66X0_PIO1_SELECT, 0x00},
  152. {CSRA66X0_PIO2_SELECT, 0x00},
  153. {CSRA66X0_PIO3_SELECT, 0x00},
  154. {CSRA66X0_PIO4_SELECT, 0x00},
  155. {CSRA66X0_PIO5_SELECT, 0x00},
  156. {CSRA66X0_PIO6_SELECT, 0x00},
  157. {CSRA66X0_PIO7_SELECT, 0x00},
  158. {CSRA66X0_PIO8_SELECT, 0x00},
  159. {CSRA66X0_PIO_DIRN0, 0xFF},
  160. {CSRA66X0_PIO_DIRN1, 0x01},
  161. {CSRA66X0_PIO_PULL_EN0, 0xFF},
  162. {CSRA66X0_PIO_PULL_EN1, 0x01},
  163. {CSRA66X0_PIO_PULL_DIR0, 0x00},
  164. {CSRA66X0_PIO_PULL_DIR1, 0x00},
  165. {CSRA66X0_PIO_DRIVE_OUT0_FA, 0x00},
  166. {CSRA66X0_PIO_DRIVE_OUT1_FA, 0x00},
  167. {CSRA66X0_PIO_STATUS_IN0_FA, 0x00},
  168. {CSRA66X0_PIO_STATUS_IN1_FA, 0x00},
  169. /* RESERVED */
  170. {CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00},
  171. {CSRA66X0_IRQ_OUTPUT_POLARITY, 0x01},
  172. {CSRA66X0_IRQ_OUTPUT_STATUS_FA, 0x00},
  173. {CSRA66X0_CLIP_DCA_STATUS_FA, 0x00},
  174. {CSRA66X0_CHIP_STATE_STATUS_FA, 0x02},
  175. {CSRA66X0_FAULT_STATUS_FA, 0x00},
  176. {CSRA66X0_OTP_STATUS_FA, 0x00},
  177. {CSRA66X0_AUDIO_IF_STATUS_FA, 0x00},
  178. /* RESERVED */
  179. {CSRA66X0_DSP_SATURATION_STATUS_FA, 0x00},
  180. {CSRA66X0_AUDIO_RATE_STATUS_FA, 0x00},
  181. /* RESERVED */
  182. {CSRA66X0_DISABLE_PWM_OUTPUT, 0x00},
  183. /* RESERVED */
  184. {CSRA66X0_OTP_VER_FA, 0x03},
  185. {CSRA66X0_RAM_VER_FA, 0x02},
  186. /* RESERVED */
  187. {CSRA66X0_AUDIO_SATURATION_FLAGS_FA, 0x00},
  188. {CSRA66X0_DCOFFSET_CHAN_1_01_FA, 0x00},
  189. {CSRA66X0_DCOFFSET_CHAN_1_02_FA, 0x00},
  190. {CSRA66X0_DCOFFSET_CHAN_1_03_FA, 0x00},
  191. {CSRA66X0_DCOFFSET_CHAN_2_01_FA, 0x00},
  192. {CSRA66X0_DCOFFSET_CHAN_2_02_FA, 0x00},
  193. {CSRA66X0_DCOFFSET_CHAN_2_03_FA, 0x00},
  194. {CSRA66X0_FORCED_PA_SWITCHING_CTRL, 0x90},
  195. {CSRA66X0_PA_FORCE_PULSE_WIDTH, 0x07},
  196. {CSRA66X0_PA_HIGH_MODULATION_CTRL_CH1, 0x00},
  197. /* RESERVED */
  198. {CSRA66X0_HIGH_MODULATION_THRESHOLD_LOW, 0xD4},
  199. {CSRA66X0_HIGH_MODULATION_THRESHOLD_HIGH, 0x78},
  200. /* RESERVED */
  201. {CSRA66X0_PA_FREEZE_CTRL, 0x00},
  202. {CSRA66X0_DCA_FREEZE_CTRL, 0x3C},
  203. /* RESERVED */
  204. };
  205. static bool csra66x0_addr_is_in_range(unsigned int addr,
  206. unsigned int addr_min, unsigned int addr_max)
  207. {
  208. if ((addr >= addr_min)
  209. && (addr <= addr_max))
  210. return true;
  211. else
  212. return false;
  213. }
  214. static bool csra66x0_volatile_register(struct device *dev, unsigned int reg)
  215. {
  216. /* coeff registers */
  217. if (csra66x0_addr_is_in_range(reg, CSRA66X0_COEFF_BASE,
  218. CSRA66X0_MAX_COEFF_ADDR))
  219. return true;
  220. /* control registers */
  221. switch (reg) {
  222. case CSRA66X0_CHIP_ID_FA:
  223. case CSRA66X0_ROM_VER_FA:
  224. case CSRA66X0_CHIP_REV_0_FA:
  225. case CSRA66X0_CHIP_REV_1_FA:
  226. case CSRA66X0_TEMP_READ0_FA:
  227. case CSRA66X0_TEMP_READ1_FA:
  228. case CSRA66X0_CHIP_STATE_CTRL_FA:
  229. case CSRA66X0_MISC_CONTROL_STATUS_1_FA:
  230. case CSRA66X0_IRQ_OUTPUT_STATUS_FA:
  231. case CSRA66X0_CLIP_DCA_STATUS_FA:
  232. case CSRA66X0_CHIP_STATE_STATUS_FA:
  233. case CSRA66X0_FAULT_STATUS_FA:
  234. case CSRA66X0_OTP_STATUS_FA:
  235. case CSRA66X0_AUDIO_IF_STATUS_FA:
  236. case CSRA66X0_DSP_SATURATION_STATUS_FA:
  237. case CSRA66X0_AUDIO_RATE_STATUS_FA:
  238. case CSRA66X0_CH1_MIX_SEL:
  239. case CSRA66X0_CH2_MIX_SEL:
  240. case CSRA66X0_CH1_SAMPLE1_SCALE_0:
  241. case CSRA66X0_CH1_SAMPLE1_SCALE_1:
  242. case CSRA66X0_CH1_SAMPLE3_SCALE_0:
  243. case CSRA66X0_CH1_SAMPLE3_SCALE_1:
  244. case CSRA66X0_CH1_SAMPLE5_SCALE_0:
  245. case CSRA66X0_CH1_SAMPLE5_SCALE_1:
  246. case CSRA66X0_CH1_SAMPLE7_SCALE_0:
  247. case CSRA66X0_CH1_SAMPLE7_SCALE_1:
  248. case CSRA66X0_CH1_SAMPLE2_SCALE_0:
  249. case CSRA66X0_CH1_SAMPLE2_SCALE_1:
  250. case CSRA66X0_CH1_SAMPLE4_SCALE_0:
  251. case CSRA66X0_CH1_SAMPLE4_SCALE_1:
  252. case CSRA66X0_CH1_SAMPLE6_SCALE_0:
  253. case CSRA66X0_CH1_SAMPLE6_SCALE_1:
  254. case CSRA66X0_CH1_SAMPLE8_SCALE_0:
  255. case CSRA66X0_CH1_SAMPLE8_SCALE_1:
  256. case CSRA66X0_CH2_SAMPLE1_SCALE_0:
  257. case CSRA66X0_CH2_SAMPLE1_SCALE_1:
  258. case CSRA66X0_CH2_SAMPLE3_SCALE_0:
  259. case CSRA66X0_CH2_SAMPLE3_SCALE_1:
  260. case CSRA66X0_CH2_SAMPLE5_SCALE_0:
  261. case CSRA66X0_CH2_SAMPLE5_SCALE_1:
  262. case CSRA66X0_CH2_SAMPLE7_SCALE_0:
  263. case CSRA66X0_CH2_SAMPLE7_SCALE_1:
  264. case CSRA66X0_CH2_SAMPLE2_SCALE_0:
  265. case CSRA66X0_CH2_SAMPLE2_SCALE_1:
  266. case CSRA66X0_CH2_SAMPLE4_SCALE_0:
  267. case CSRA66X0_CH2_SAMPLE4_SCALE_1:
  268. case CSRA66X0_CH2_SAMPLE6_SCALE_0:
  269. case CSRA66X0_CH2_SAMPLE6_SCALE_1:
  270. case CSRA66X0_CH2_SAMPLE8_SCALE_0:
  271. case CSRA66X0_CH2_SAMPLE8_SCALE_1:
  272. case CSRA66X0_RAM_VER_FA:
  273. return true;
  274. default:
  275. return false;
  276. }
  277. }
  278. static bool csra66x0_writeable_registers(struct device *dev, unsigned int reg)
  279. {
  280. if (csra66x0_addr_is_in_range(reg, CSRA66X0_BASE,
  281. CSRA66X0_MAX_REGISTER_ADDR)
  282. || csra66x0_addr_is_in_range(reg, CSRA66X0_COEFF_BASE,
  283. CSRA66X0_MAX_COEFF_ADDR))
  284. return true;
  285. else
  286. return false;
  287. }
  288. static bool csra66x0_readable_registers(struct device *dev, unsigned int reg)
  289. {
  290. if (csra66x0_addr_is_in_range(reg, CSRA66X0_BASE,
  291. CSRA66X0_MAX_REGISTER_ADDR)
  292. || csra66x0_addr_is_in_range(reg, CSRA66X0_COEFF_BASE,
  293. CSRA66X0_MAX_COEFF_ADDR))
  294. return true;
  295. else
  296. return false;
  297. }
  298. /* codec private data */
  299. struct csra66x0_priv {
  300. struct regmap *regmap;
  301. struct snd_soc_component *component;
  302. int spk_volume_ch1;
  303. int spk_volume_ch2;
  304. int irq;
  305. int vreg_gpio;
  306. u32 irq_active_low;
  307. u32 in_cluster;
  308. u32 is_master;
  309. bool is_probed;
  310. u32 max_num_cluster_devices;
  311. u32 num_cluster_devices;
  312. u32 sysfs_reg_addr;
  313. #if IS_ENABLED(CONFIG_DEBUG_FS)
  314. struct dentry *debugfs_dir;
  315. struct dentry *debugfs_file_wo;
  316. struct dentry *debugfs_file_ro;
  317. #endif /* CONFIG_DEBUG_FS */
  318. };
  319. struct csra66x0_cluster_device {
  320. struct csra66x0_priv *csra66x0_ptr;
  321. const char *csra66x0_prefix;
  322. };
  323. struct csra66x0_cluster_device csra_clust_dev_tbl[] = {
  324. {NULL, "CSRA_12"},
  325. {NULL, "CSRA_34"},
  326. {NULL, "CSRA_56"},
  327. {NULL, "CSRA_78"},
  328. {NULL, "CSRA_9A"},
  329. {NULL, "CSRA_BC"}
  330. };
  331. static int sysfs_get_param(char *buf, u32 *param, int num_of_par)
  332. {
  333. char *token;
  334. int base, cnt;
  335. token = strsep(&buf, " ");
  336. for (cnt = 0; cnt < num_of_par; cnt++) {
  337. if (token) {
  338. if ((token[1] == 'x') || (token[1] == 'X'))
  339. base = 16;
  340. else
  341. base = 10;
  342. if (kstrtou32(token, base, &param[cnt]) != 0)
  343. return -EINVAL;
  344. token = strsep(&buf, " ");
  345. } else {
  346. return -EINVAL;
  347. }
  348. }
  349. return 0;
  350. }
  351. #if IS_ENABLED(CONFIG_DEBUG_FS)
  352. static int debugfs_codec_open_op(struct inode *inode, struct file *file)
  353. {
  354. file->private_data = inode->i_private;
  355. return 0;
  356. }
  357. static ssize_t debugfs_codec_write_op(struct file *filp,
  358. const char __user *ubuf, size_t cnt, loff_t *ppos)
  359. {
  360. struct csra66x0_priv *csra66x0 =
  361. (struct csra66x0_priv *) filp->private_data;
  362. struct snd_soc_component *component = csra66x0->component;
  363. char lbuf[32];
  364. int rc;
  365. u32 param[2];
  366. if (!filp || !ppos || !ubuf || !component)
  367. return -EINVAL;
  368. if (cnt > sizeof(lbuf) - 1)
  369. return -EINVAL;
  370. rc = copy_from_user(lbuf, ubuf, cnt);
  371. if (rc)
  372. return -EFAULT;
  373. lbuf[cnt] = '\0';
  374. rc = sysfs_get_param(lbuf, param, 2);
  375. if (!(csra66x0_addr_is_in_range(param[0],
  376. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  377. || csra66x0_addr_is_in_range(param[0],
  378. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  379. dev_err(component->dev, "%s: register address 0x%04X out of range\n",
  380. __func__, param[0]);
  381. return -EINVAL;
  382. }
  383. if ((param[1] < 0) || (param[1] > 255)) {
  384. dev_err(component->dev, "%s: register data 0x%02X out of range\n",
  385. __func__, param[1]);
  386. return -EINVAL;
  387. }
  388. if (rc == 0)
  389. {
  390. rc = cnt;
  391. dev_info(component->dev, "%s: reg[0x%04X]=0x%02X\n",
  392. __func__, param[0], param[1]);
  393. snd_soc_component_write(component, param[0], param[1]);
  394. } else {
  395. dev_err(component->dev, "%s: write to register addr=0x%04X failed\n",
  396. __func__, param[0]);
  397. }
  398. return rc;
  399. }
  400. static ssize_t debugfs_csra66x0_reg_show(struct csra66x0_priv *csra66x0,
  401. char __user *ubuf, size_t count, loff_t *ppos)
  402. {
  403. int i, reg_val, len;
  404. int addr_min, addr_max;
  405. ssize_t total = 0;
  406. char tmp_buf[20];
  407. struct snd_soc_component *component = csra66x0->component;
  408. if (!ubuf || !ppos || !component || *ppos < 0)
  409. return -EINVAL;
  410. if (csra66x0_addr_is_in_range(csra66x0->sysfs_reg_addr,
  411. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR)) {
  412. addr_min = CSRA66X0_COEFF_BASE;
  413. addr_max = CSRA66X0_MAX_COEFF_ADDR;
  414. csra66x0->sysfs_reg_addr = CSRA66X0_BASE;
  415. } else {
  416. addr_min = CSRA66X0_BASE;
  417. addr_max = CSRA66X0_MAX_REGISTER_ADDR;
  418. }
  419. for (i = ((int) *ppos + addr_min);
  420. i <= addr_max; i++) {
  421. reg_val = snd_soc_component_read32(component, i);
  422. len = snprintf(tmp_buf, 20, "0x%04X: 0x%02X\n", i, (reg_val & 0xFF));
  423. if ((total + len) >= count - 1)
  424. break;
  425. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  426. dev_err(component->dev, "%s: fail to copy reg dump\n",
  427. __func__);
  428. total = -EFAULT;
  429. goto copy_err;
  430. }
  431. *ppos += len;
  432. total += len;
  433. }
  434. copy_err:
  435. return total;
  436. }
  437. static ssize_t debugfs_codec_read_op(struct file *filp,
  438. char __user *ubuf, size_t cnt, loff_t *ppos)
  439. {
  440. struct csra66x0_priv *csra66x0 =
  441. (struct csra66x0_priv *) filp->private_data;
  442. ssize_t ret_cnt;
  443. if (!filp || !ppos || !ubuf || *ppos < 0)
  444. return -EINVAL;
  445. ret_cnt = debugfs_csra66x0_reg_show(csra66x0, ubuf, cnt, ppos);
  446. return ret_cnt;
  447. }
  448. static const struct file_operations debugfs_codec_ops = {
  449. .open = debugfs_codec_open_op,
  450. .write = debugfs_codec_write_op,
  451. .read = debugfs_codec_read_op,
  452. };
  453. #endif /* CONFIG_DEBUG_FS */
  454. /*
  455. * CSRA66X0 Controls
  456. */
  457. static const DECLARE_TLV_DB_SCALE(csra66x0_volume_tlv, -9000, 25, 0);
  458. static const DECLARE_TLV_DB_RANGE(csra66x0_bass_treble_tlv,
  459. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  460. 1, 15, TLV_DB_SCALE_ITEM(-1500, 100, 0),
  461. 16, 30, TLV_DB_SCALE_ITEM(100, 100, 0)
  462. );
  463. static int csra66x0_get_volume(struct snd_kcontrol *kcontrol,
  464. struct snd_ctl_elem_value *ucontrol)
  465. {
  466. struct soc_mixer_control *mc =
  467. (struct soc_mixer_control *)kcontrol->private_value;
  468. struct snd_soc_component *component =
  469. snd_soc_kcontrol_component(kcontrol);
  470. unsigned int reg_l = mc->reg;
  471. unsigned int reg_r = mc->rreg;
  472. unsigned int val_l, val_r;
  473. val_l = (snd_soc_component_read32(component, reg_l) & 0xff) |
  474. ((snd_soc_component_read32(component,
  475. CSRA66X0_CH1_VOLUME_1_FA) & (0x01)) << 8);
  476. val_r = (snd_soc_component_read32(component, reg_r) & 0xff) |
  477. ((snd_soc_component_read32(component,
  478. CSRA66X0_CH2_VOLUME_1_FA) & (0x01)) << 8);
  479. ucontrol->value.integer.value[0] = val_l;
  480. ucontrol->value.integer.value[1] = val_r;
  481. return 0;
  482. }
  483. static int csra66x0_set_volume(struct snd_kcontrol *kcontrol,
  484. struct snd_ctl_elem_value *ucontrol)
  485. {
  486. struct soc_mixer_control *mc =
  487. (struct soc_mixer_control *)kcontrol->private_value;
  488. struct snd_soc_component *component =
  489. snd_soc_kcontrol_component(kcontrol);
  490. struct csra66x0_priv *csra66x0 =
  491. snd_soc_component_get_drvdata(component);
  492. unsigned int reg_l = mc->reg;
  493. unsigned int reg_r = mc->rreg;
  494. unsigned int val_l[2];
  495. unsigned int val_r[2];
  496. csra66x0->spk_volume_ch1 = (ucontrol->value.integer.value[0]);
  497. csra66x0->spk_volume_ch2 = (ucontrol->value.integer.value[1]);
  498. val_l[0] = csra66x0->spk_volume_ch1 & SPK_VOLUME_LSB_MSK;
  499. val_l[1] = (csra66x0->spk_volume_ch1 & SPK_VOLUME_MSB_MSK) ? 1 : 0;
  500. val_r[0] = csra66x0->spk_volume_ch2 & SPK_VOLUME_LSB_MSK;
  501. val_r[1] = (csra66x0->spk_volume_ch2 & SPK_VOLUME_MSB_MSK) ? 1 : 0;
  502. snd_soc_component_write(component, reg_l, val_l[0]);
  503. snd_soc_component_write(component, reg_r, val_r[0]);
  504. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_1_FA, val_l[1]);
  505. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_1_FA, val_r[1]);
  506. return 0;
  507. }
  508. /* enumerated controls */
  509. static const char * const csra66x0_mute_output_text[] = {"PLAY", "MUTE"};
  510. static const char * const csra66x0_output_invert_text[] = {
  511. "UNCHANGED", "INVERTED"};
  512. static const char * const csra66x0_deemp_config_text[] = {
  513. "DISABLED", "ENABLED"};
  514. SOC_ENUM_SINGLE_DECL(csra66x0_mute_output_enum,
  515. CSRA66X0_MISC_CONTROL_STATUS_1_FA, 2,
  516. csra66x0_mute_output_text);
  517. SOC_ENUM_SINGLE_DECL(csra66x0_ch1_output_invert_enum,
  518. CSRA66X0_CH1_OUTPUT_INVERT_EN, 0,
  519. csra66x0_output_invert_text);
  520. SOC_ENUM_SINGLE_DECL(csra66x0_ch2_output_invert_enum,
  521. CSRA66X0_CH2_OUTPUT_INVERT_EN, 0,
  522. csra66x0_output_invert_text);
  523. SOC_ENUM_DOUBLE_DECL(csra66x0_deemp_config_enum,
  524. CSRA66X0_DEEMP_CONFIG_FA, 0, 1,
  525. csra66x0_deemp_config_text);
  526. static const struct snd_kcontrol_new csra66x0_snd_controls[] = {
  527. /* volume */
  528. SOC_DOUBLE_R_EXT_TLV("PA VOLUME", CSRA66X0_CH1_VOLUME_0_FA,
  529. CSRA66X0_CH2_VOLUME_0_FA, 0, 0x1C9, 0,
  530. csra66x0_get_volume, csra66x0_set_volume,
  531. csra66x0_volume_tlv),
  532. /* bass treble */
  533. SOC_DOUBLE_R_TLV("PA BASS GAIN", CSRA66X0_CH1_BASS_GAIN_CTRL_FA,
  534. CSRA66X0_CH2_BASS_GAIN_CTRL_FA, 0, 0x1E, 0,
  535. csra66x0_bass_treble_tlv),
  536. SOC_DOUBLE_R_TLV("PA TREBLE GAIN", CSRA66X0_CH1_TREBLE_GAIN_CTRL_FA,
  537. CSRA66X0_CH2_TREBLE_GAIN_CTRL_FA, 0, 0x1E, 0,
  538. csra66x0_bass_treble_tlv),
  539. SOC_DOUBLE_R("PA BASS_XOVER FREQ", CSRA66X0_CH1_BASS_FC_CTRL_FA,
  540. CSRA66X0_CH2_BASS_FC_CTRL_FA, 0, 2, 0),
  541. SOC_DOUBLE_R("PA TREBLE_XOVER FREQ", CSRA66X0_CH1_TREBLE_FC_CTRL_FA,
  542. CSRA66X0_CH2_TREBLE_FC_CTRL_FA, 0, 2, 0),
  543. /* switch */
  544. SOC_ENUM("PA MUTE_OUTPUT SWITCH", csra66x0_mute_output_enum),
  545. SOC_ENUM("PA DE-EMPHASIS SWITCH", csra66x0_deemp_config_enum),
  546. };
  547. static const struct snd_kcontrol_new csra_mix_switch[] = {
  548. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  549. };
  550. static const struct snd_soc_dapm_widget csra66x0_dapm_widgets[] = {
  551. SND_SOC_DAPM_INPUT("IN"),
  552. SND_SOC_DAPM_MIXER("MIXER", SND_SOC_NOPM, 0, 0,
  553. csra_mix_switch, ARRAY_SIZE(csra_mix_switch)),
  554. SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
  555. SND_SOC_DAPM_PGA("PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  556. SND_SOC_DAPM_OUTPUT("SPKR"),
  557. };
  558. static const struct snd_soc_dapm_route csra66x0_dapm_routes[] = {
  559. {"MIXER", "Switch", "IN"},
  560. {"DAC", NULL, "MIXER"},
  561. {"PGA", NULL, "DAC"},
  562. {"SPKR", NULL, "PGA"},
  563. };
  564. static int csra66x0_wait_for_config_state(struct snd_soc_component *component)
  565. {
  566. u16 val;
  567. int cntdwn = WAIT_FOR_CONFIG_STATE_TIMEOUT_MS;
  568. do {
  569. /* wait >= 100ms to check if HW has moved to config state */
  570. msleep(100);
  571. val = snd_soc_component_read32(component,
  572. CSRA66X0_CHIP_STATE_STATUS_FA);
  573. if (val == CONFIG_STATE_ID)
  574. break;
  575. cntdwn = cntdwn - 100;
  576. } while (cntdwn > 0);
  577. if (cntdwn <= 0)
  578. return -EFAULT;
  579. return 0;
  580. }
  581. static int csra66x0_allow_run(struct csra66x0_priv *csra66x0)
  582. {
  583. struct snd_soc_component *component = csra66x0->component;
  584. int i;
  585. /* csra66x0 is not in cluster */
  586. if (!csra66x0->in_cluster) {
  587. /* enable interrupts */
  588. if (csra66x0->irq) {
  589. snd_soc_component_write(component,
  590. CSRA66X0_PIO0_SELECT, 0x1);
  591. if (csra66x0->irq_active_low)
  592. snd_soc_component_write(component,
  593. CSRA66X0_IRQ_OUTPUT_POLARITY, 0x0);
  594. else
  595. snd_soc_component_write(component,
  596. CSRA66X0_IRQ_OUTPUT_POLARITY, 0x1);
  597. snd_soc_component_write(component,
  598. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  599. } else {
  600. snd_soc_component_write(component,
  601. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  602. }
  603. /* allow run */
  604. snd_soc_component_write(component,
  605. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  606. return 0;
  607. }
  608. /* csra66x0 is part of cluster */
  609. /* get number of probed cluster devices */
  610. csra66x0->num_cluster_devices = 0;
  611. for (i = 0; i < component->card->num_aux_devs; i++) {
  612. if (i >= csra66x0->max_num_cluster_devices)
  613. break;
  614. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  615. continue;
  616. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_probed)
  617. csra66x0->num_cluster_devices++;
  618. }
  619. /* check if all cluster devices are probed */
  620. if (csra66x0->num_cluster_devices
  621. == component->card->num_aux_devs) {
  622. /* allow run of all slave components */
  623. for (i = 0; i < component->card->num_aux_devs; i++) {
  624. if (i >= csra66x0->max_num_cluster_devices)
  625. break;
  626. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  627. continue;
  628. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  629. continue;
  630. snd_soc_component_write(
  631. csra_clust_dev_tbl[i].csra66x0_ptr->component,
  632. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  633. }
  634. /* allow run of all master components */
  635. for (i = 0; i < component->card->num_aux_devs; i++) {
  636. if (i >= csra66x0->max_num_cluster_devices)
  637. break;
  638. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  639. continue;
  640. if (!csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  641. continue;
  642. /* enable interrupts */
  643. if (csra66x0->irq) {
  644. snd_soc_component_write(component,
  645. CSRA66X0_PIO0_SELECT, 0x1);
  646. if (csra66x0->irq_active_low)
  647. snd_soc_component_write(component,
  648. CSRA66X0_IRQ_OUTPUT_POLARITY,
  649. 0x0);
  650. else
  651. snd_soc_component_write(component,
  652. CSRA66X0_IRQ_OUTPUT_POLARITY,
  653. 0x1);
  654. snd_soc_component_write(component,
  655. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  656. } else {
  657. snd_soc_component_write(component,
  658. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  659. }
  660. /* allow run */
  661. snd_soc_component_write(
  662. csra_clust_dev_tbl[i].csra66x0_ptr->component,
  663. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  664. }
  665. }
  666. return 0;
  667. }
  668. static int csra66x0_init(struct csra66x0_priv *csra66x0)
  669. {
  670. struct snd_soc_component *component = csra66x0->component;
  671. int ret;
  672. dev_dbg(component->dev, "%s: initialize %s\n",
  673. __func__, component->name);
  674. csra66x0->sysfs_reg_addr = CSRA66X0_BASE;
  675. /* config */
  676. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  677. SET_CONFIG_STATE);
  678. /* wait until HW is in config state before proceeding */
  679. ret = csra66x0_wait_for_config_state(component);
  680. if (ret) {
  681. dev_err(component->dev, "%s: timeout while %s is waiting for config state\n",
  682. __func__, component->name);
  683. }
  684. /* setup */
  685. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_0,
  686. 0x09);
  687. snd_soc_component_write(component, CSRA66X0_TEMP_PROT_BACKOFF, 0x0C);
  688. snd_soc_component_write(component, CSRA66X0_EXT_PA_PROTECT_POLARITY,
  689. 0x03);
  690. snd_soc_component_write(component, CSRA66X0_PWM_OUTPUT_CONFIG, 0xC8);
  691. csra66x0->spk_volume_ch1 = SPK_VOLUME_M20DB;
  692. csra66x0->spk_volume_ch2 = SPK_VOLUME_M20DB;
  693. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_0_FA,
  694. SPK_VOLUME_M20DB_LSB);
  695. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_0_FA,
  696. SPK_VOLUME_M20DB_LSB);
  697. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_1_FA,
  698. SPK_VOLUME_M20DB_MSB);
  699. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_1_FA,
  700. SPK_VOLUME_M20DB_MSB);
  701. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_CTRL, 0x0);
  702. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_0,
  703. 0xE7);
  704. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_1,
  705. 0x26);
  706. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_2,
  707. 0x40);
  708. snd_soc_component_write(component, CSRA66X0_MIN_MODULATION_PULSE_WIDTH,
  709. 0x7A);
  710. snd_soc_component_write(component, CSRA66X0_CH1_HARD_CLIP_THRESH, 0x00);
  711. snd_soc_component_write(component, CSRA66X0_CH2_HARD_CLIP_THRESH, 0x00);
  712. snd_soc_component_write(component, CSRA66X0_CH1_DCA_THRESH, 0x40);
  713. snd_soc_component_write(component, CSRA66X0_CH2_DCA_THRESH, 0x40);
  714. snd_soc_component_write(component, CSRA66X0_DCA_ATTACK_RATE, 0x00);
  715. snd_soc_component_write(component, CSRA66X0_DCA_RELEASE_RATE, 0x00);
  716. csra66x0_allow_run(csra66x0);
  717. return 0;
  718. }
  719. static int csra66x0_reset(struct csra66x0_priv *csra66x0)
  720. {
  721. struct snd_soc_component *component = csra66x0->component;
  722. u16 val;
  723. val = snd_soc_component_read32(component, CSRA66X0_FAULT_STATUS_FA);
  724. if (val & FAULT_STATUS_INTERNAL)
  725. dev_dbg(component->dev, "%s: FAULT_STATUS_INTERNAL 0x%X\n",
  726. __func__, val);
  727. if (val & FAULT_STATUS_OTP_INTEGRITY)
  728. dev_dbg(component->dev, "%s: FAULT_STATUS_OTP_INTEGRITY 0x%X\n",
  729. __func__, val);
  730. if (val & FAULT_STATUS_PADS2)
  731. dev_dbg(component->dev, "%s: FAULT_STATUS_PADS2 0x%X\n",
  732. __func__, val);
  733. if (val & FAULT_STATUS_SMPS)
  734. dev_dbg(component->dev, "%s: FAULT_STATUS_SMPS 0x%X\n",
  735. __func__, val);
  736. if (val & FAULT_STATUS_TEMP)
  737. dev_dbg(component->dev, "%s: FAULT_STATUS_TEMP 0x%X\n",
  738. __func__, val);
  739. if (val & FAULT_STATUS_PROTECT)
  740. dev_dbg(component->dev, "%s: FAULT_STATUS_PROTECT 0x%X\n",
  741. __func__, val);
  742. dev_dbg(component->dev, "%s: reset %s\n",
  743. __func__, component->name);
  744. /* clear fault state and re-init */
  745. snd_soc_component_write(component, CSRA66X0_FAULT_STATUS_FA, 0x00);
  746. snd_soc_component_write(component, CSRA66X0_IRQ_OUTPUT_STATUS_FA, 0x00);
  747. /* apply reset to CSRA66X0 */
  748. val = snd_soc_component_read32(component,
  749. CSRA66X0_MISC_CONTROL_STATUS_1_FA);
  750. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_1_FA,
  751. val | 0x08);
  752. /* wait 500ms after reset to recover CSRA66X0 */
  753. msleep(500);
  754. return 0;
  755. }
  756. static int csra66x0_msconfig(struct csra66x0_priv *csra66x0)
  757. {
  758. struct snd_soc_component *component = csra66x0->component;
  759. int ret;
  760. dev_dbg(component->dev, "%s: configure %s\n",
  761. __func__, component->name);
  762. /* config */
  763. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  764. SET_CONFIG_STATE);
  765. /* wait until HW is in config state before proceeding */
  766. ret = csra66x0_wait_for_config_state(component);
  767. if (ret) {
  768. dev_err(component->dev, "%s: timeout while %s is waiting for config state\n",
  769. __func__, component->name);
  770. return ret;
  771. }
  772. snd_soc_component_write(component, CSRA66X0_PIO7_SELECT, 0x04);
  773. snd_soc_component_write(component, CSRA66X0_PIO8_SELECT, 0x04);
  774. if (csra66x0->is_master) {
  775. /* Master specific config */
  776. snd_soc_component_write(component,
  777. CSRA66X0_PIO_PULL_EN0, 0xFF);
  778. snd_soc_component_write(component,
  779. CSRA66X0_PIO_PULL_DIR0, 0x80);
  780. snd_soc_component_write(component,
  781. CSRA66X0_PIO_PULL_EN1, 0x01);
  782. snd_soc_component_write(component,
  783. CSRA66X0_PIO_PULL_DIR1, 0x01);
  784. } else {
  785. /* Slave specific config */
  786. snd_soc_component_write(component,
  787. CSRA66X0_PIO_PULL_EN0, 0x7F);
  788. snd_soc_component_write(component,
  789. CSRA66X0_PIO_PULL_EN1, 0x00);
  790. }
  791. snd_soc_component_write(component, CSRA66X0_DCA_CTRL, 0x05);
  792. return 0;
  793. }
  794. static int csra66x0_soc_probe(struct snd_soc_component *component)
  795. {
  796. struct csra66x0_priv *csra66x0 =
  797. snd_soc_component_get_drvdata(component);
  798. struct snd_soc_dapm_context *dapm;
  799. char name[50];
  800. unsigned int i;
  801. csra66x0->component = component;
  802. if (csra66x0->in_cluster) {
  803. dapm = snd_soc_component_get_dapm(component);
  804. dev_dbg(component->dev, "%s: assign prefix %s to component device %s\n",
  805. __func__, component->name_prefix,
  806. component->name);
  807. /* add device to cluster table */
  808. csra66x0->max_num_cluster_devices =
  809. ARRAY_SIZE(csra_clust_dev_tbl);
  810. for (i = 0; i < csra66x0->max_num_cluster_devices; i++) {
  811. if (!strncmp(component->name_prefix,
  812. csra_clust_dev_tbl[i].csra66x0_prefix,
  813. strnlen(
  814. csra_clust_dev_tbl[i].csra66x0_prefix,
  815. sizeof(
  816. csra_clust_dev_tbl[i].csra66x0_prefix)))) {
  817. csra_clust_dev_tbl[i].csra66x0_ptr = csra66x0;
  818. break;
  819. }
  820. if (i == csra66x0->max_num_cluster_devices - 1)
  821. dev_warn(component->dev,
  822. "%s: Unknown prefix %s of cluster device %s\n",
  823. __func__, component->name_prefix,
  824. component->name);
  825. }
  826. /* master slave config */
  827. csra66x0_msconfig(csra66x0);
  828. if (dapm->component) {
  829. strlcpy(name, dapm->component->name_prefix,
  830. sizeof(name));
  831. strlcat(name, " IN", sizeof(name));
  832. snd_soc_dapm_ignore_suspend(dapm, name);
  833. strlcpy(name, dapm->component->name_prefix,
  834. sizeof(name));
  835. strlcat(name, " SPKR", sizeof(name));
  836. snd_soc_dapm_ignore_suspend(dapm, name);
  837. }
  838. }
  839. /* common initialization */
  840. csra66x0->is_probed = 1;
  841. csra66x0_init(csra66x0);
  842. return 0;
  843. }
  844. static void csra66x0_soc_remove(struct snd_soc_component *component)
  845. {
  846. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  847. SET_STDBY_STATE);
  848. return;
  849. }
  850. static const struct snd_soc_component_driver soc_codec_drv_csra66x0 = {
  851. .name = DRV_NAME,
  852. .probe = csra66x0_soc_probe,
  853. .remove = csra66x0_soc_remove,
  854. .controls = csra66x0_snd_controls,
  855. .num_controls = ARRAY_SIZE(csra66x0_snd_controls),
  856. .dapm_widgets = csra66x0_dapm_widgets,
  857. .num_dapm_widgets = ARRAY_SIZE(csra66x0_dapm_widgets),
  858. .dapm_routes = csra66x0_dapm_routes,
  859. .num_dapm_routes = ARRAY_SIZE(csra66x0_dapm_routes),
  860. };
  861. static struct regmap_config csra66x0_regmap_config = {
  862. .reg_bits = 16,
  863. .val_bits = 8,
  864. .cache_type = REGCACHE_RBTREE,
  865. .reg_defaults = csra66x0_reg_defaults,
  866. .num_reg_defaults = ARRAY_SIZE(csra66x0_reg_defaults),
  867. .max_register = CSRA66X0_MAX_COEFF_ADDR,
  868. .volatile_reg = csra66x0_volatile_register,
  869. .writeable_reg = csra66x0_writeable_registers,
  870. .readable_reg = csra66x0_readable_registers,
  871. };
  872. static irqreturn_t csra66x0_irq(int irq, void *data)
  873. {
  874. struct csra66x0_priv *csra66x0 = (struct csra66x0_priv *) data;
  875. struct snd_soc_component *component = csra66x0->component;
  876. u16 val;
  877. unsigned int i;
  878. /* Treat interrupt before component is initialized as spurious */
  879. if (component == NULL)
  880. return IRQ_NONE;
  881. dev_dbg(component->dev, "%s: csra66x0_interrupt triggered by %s\n",
  882. __func__, component->name);
  883. /* fault indication */
  884. val = snd_soc_component_read32(component, CSRA66X0_IRQ_OUTPUT_STATUS_FA)
  885. & 0x1;
  886. if (!val)
  887. return IRQ_HANDLED;
  888. if (csra66x0->in_cluster) {
  889. /* reset all slave components */
  890. for (i = 0; i < component->card->num_aux_devs; i++) {
  891. if (i >= csra66x0->max_num_cluster_devices)
  892. break;
  893. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  894. continue;
  895. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  896. continue;
  897. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  898. }
  899. /* reset all master components */
  900. for (i = 0; i < component->card->num_aux_devs; i++) {
  901. if (i >= csra66x0->max_num_cluster_devices)
  902. break;
  903. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  904. continue;
  905. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  906. csra66x0_reset(
  907. csra_clust_dev_tbl[i].csra66x0_ptr);
  908. }
  909. /* recover all components */
  910. for (i = 0; i < component->card->num_aux_devs; i++) {
  911. if (i >= csra66x0->max_num_cluster_devices)
  912. break;
  913. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  914. continue;
  915. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  916. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  917. }
  918. } else {
  919. csra66x0_reset(csra66x0);
  920. csra66x0_init(csra66x0);
  921. }
  922. return IRQ_HANDLED;
  923. };
  924. static const struct of_device_id csra66x0_of_match[] = {
  925. { .compatible = "qcom,csra66x0", },
  926. { }
  927. };
  928. MODULE_DEVICE_TABLE(of, csra66x0_of_match);
  929. static ssize_t csra66x0_sysfs_write2reg_addr_value(struct device *dev,
  930. struct device_attribute *attr, const char *buf, size_t count)
  931. {
  932. int ret;
  933. u32 param[2]; /*reg_addr, reg_value */
  934. char lbuf[CSRA66X0_SYSFS_ENTRY_MAX_LEN];
  935. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  936. struct snd_soc_component *component = csra66x0->component;
  937. if (!csra66x0) {
  938. dev_err(component->dev, "%s: invalid input\n", __func__);
  939. return -EINVAL;
  940. }
  941. if (count > sizeof(lbuf) - 1)
  942. return -EINVAL;
  943. ret = strlcpy(lbuf, buf, count);
  944. if (ret != count) {
  945. dev_err(component->dev, "%s: copy input from user space failed. ret=%d\n",
  946. __func__, ret);
  947. ret = -EFAULT;
  948. goto end;
  949. }
  950. lbuf[count] = '\0';
  951. ret = sysfs_get_param(lbuf, param, 2);
  952. if (ret) {
  953. dev_err(component->dev, "%s: get sysfs parameter failed. ret=%d\n",
  954. __func__, ret);
  955. goto end;
  956. }
  957. if (!(csra66x0_addr_is_in_range(param[0],
  958. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  959. || csra66x0_addr_is_in_range(param[0],
  960. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  961. dev_err(component->dev, "%s: register address 0x%04X out of range\n",
  962. __func__, param[0]);
  963. ret = -EINVAL;
  964. goto end;
  965. }
  966. if ((param[1] < 0) || (param[1] > 255)) {
  967. dev_err(component->dev, "%s: register data 0x%02X out of range\n",
  968. __func__, param[1]);
  969. ret = -EINVAL;
  970. goto end;
  971. }
  972. snd_soccomponent_component_write(component, param[0], param[1]);
  973. ret = count;
  974. end:
  975. return ret;
  976. }
  977. static ssize_t csra66x0_sysfs_read2reg_addr_set(struct device *dev,
  978. struct device_attribute *attr, const char *buf, size_t count)
  979. {
  980. int ret;
  981. u32 reg_addr;
  982. char lbuf[CSRA66X0_SYSFS_ENTRY_MAX_LEN];
  983. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  984. if (!csra66x0) {
  985. dev_err(dev, "%s: invalid input\n", __func__);
  986. return -EINVAL;
  987. }
  988. if (count > sizeof(lbuf) - 1)
  989. return -EINVAL;
  990. ret = strlcpy(lbuf, buf, count);
  991. if (ret != count) {
  992. dev_err(dev, "%s: copy input from user space failed. ret=%d\n",
  993. __func__, ret);
  994. ret = -EFAULT;
  995. goto end;
  996. }
  997. lbuf[count] = '\0';
  998. ret = sysfs_get_param(lbuf, &reg_addr, 1);
  999. if (ret) {
  1000. dev_err(dev, "%s: get sysfs parameter failed. ret=%d\n",
  1001. __func__, ret);
  1002. goto end;
  1003. }
  1004. if (!(csra66x0_addr_is_in_range(reg_addr,
  1005. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  1006. || csra66x0_addr_is_in_range(reg_addr,
  1007. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  1008. dev_err(dev, "%s: register address 0x%04X out of range\n",
  1009. __func__, reg_addr);
  1010. ret = -EINVAL;
  1011. goto end;
  1012. }
  1013. csra66x0->sysfs_reg_addr = reg_addr;
  1014. ret = count;
  1015. end:
  1016. return ret;
  1017. }
  1018. static ssize_t csra66x0_sysfs_read2reg_addr_get(struct device *dev,
  1019. struct device_attribute *attr, char *buf)
  1020. {
  1021. int ret;
  1022. u32 reg_addr;
  1023. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1024. if (!csra66x0) {
  1025. dev_err(dev, "%s: invalid input\n", __func__);
  1026. return -EINVAL;
  1027. }
  1028. reg_addr = csra66x0->sysfs_reg_addr;
  1029. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1030. "0x%04X\n", reg_addr);
  1031. pr_debug("%s: 0x%04X\n", __func__, reg_addr);
  1032. return ret;
  1033. }
  1034. static ssize_t csra66x0_sysfs_read2reg_value(struct device *dev,
  1035. struct device_attribute *attr, char *buf)
  1036. {
  1037. int ret;
  1038. u32 reg_val, reg_addr;
  1039. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1040. struct snd_soc_component *component = csra66x0->component;
  1041. if (!csra66x0) {
  1042. dev_err(dev, "%s: invalid input\n", __func__);
  1043. return -EINVAL;
  1044. }
  1045. reg_addr = csra66x0->sysfs_reg_addr;
  1046. if (!(csra66x0_addr_is_in_range(reg_addr,
  1047. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  1048. || csra66x0_addr_is_in_range(reg_addr,
  1049. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  1050. pr_debug("%s: 0x%04X: register address out of range\n",
  1051. __func__, reg_addr);
  1052. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1053. "0x%04X: register address out of range\n", reg_addr);
  1054. goto end;
  1055. }
  1056. reg_val = snd_soc_component_read32(component, csra66x0->sysfs_reg_addr);
  1057. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1058. "0x%04X: 0x%02X\n", csra66x0->sysfs_reg_addr, reg_val);
  1059. pr_debug("%s: 0x%04X: 0x%02X\n", __func__,
  1060. csra66x0->sysfs_reg_addr, reg_val);
  1061. end:
  1062. return ret;
  1063. }
  1064. static ssize_t csra66x0_sysfs_reset(struct device *dev,
  1065. struct device_attribute *attr, const char *buf, size_t count)
  1066. {
  1067. int val, rc;
  1068. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1069. struct snd_soc_component *component = csra66x0->component;
  1070. unsigned int i;
  1071. if (!csra66x0) {
  1072. dev_err(dev, "%s: invalid input\n", __func__);
  1073. return -EINVAL;
  1074. }
  1075. rc = kstrtoint(buf, 10, &val);
  1076. if (rc) {
  1077. dev_err(dev, "%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1078. goto end;
  1079. }
  1080. if (val != SYSFS_RESET) {
  1081. dev_err(dev, "%s: value out of range.\n", __func__);
  1082. rc = -EINVAL;
  1083. goto end;
  1084. }
  1085. pr_debug("%s: reset device\n", __func__);
  1086. if (csra66x0->in_cluster) {
  1087. /* reset all slave components */
  1088. for (i = 0; i < component->card->num_aux_devs; i++) {
  1089. if (i >= csra66x0->max_num_cluster_devices)
  1090. break;
  1091. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1092. continue;
  1093. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  1094. continue;
  1095. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  1096. }
  1097. /* reset all master components */
  1098. for (i = 0; i < component->card->num_aux_devs; i++) {
  1099. if (i >= csra66x0->max_num_cluster_devices)
  1100. break;
  1101. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1102. continue;
  1103. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  1104. csra66x0_reset(
  1105. csra_clust_dev_tbl[i].csra66x0_ptr);
  1106. }
  1107. /* recover all components */
  1108. for (i = 0; i < component->card->num_aux_devs; i++) {
  1109. if (i >= csra66x0->max_num_cluster_devices)
  1110. break;
  1111. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1112. continue;
  1113. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  1114. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  1115. }
  1116. } else {
  1117. csra66x0_reset(csra66x0);
  1118. csra66x0_init(csra66x0);
  1119. }
  1120. rc = strnlen(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN);
  1121. end:
  1122. return rc;
  1123. }
  1124. static DEVICE_ATTR(write2reg_addr_value, 0200, NULL,
  1125. csra66x0_sysfs_write2reg_addr_value);
  1126. static DEVICE_ATTR(read2reg_addr, 0644, csra66x0_sysfs_read2reg_addr_get,
  1127. csra66x0_sysfs_read2reg_addr_set);
  1128. static DEVICE_ATTR(read2reg_value, 0444, csra66x0_sysfs_read2reg_value, NULL);
  1129. static DEVICE_ATTR(reset, 0200, NULL, csra66x0_sysfs_reset);
  1130. static struct attribute *csra66x0_fs_attrs[] = {
  1131. &dev_attr_write2reg_addr_value.attr,
  1132. &dev_attr_read2reg_addr.attr,
  1133. &dev_attr_read2reg_value.attr,
  1134. &dev_attr_reset.attr,
  1135. NULL,
  1136. };
  1137. static struct attribute_group csra66x0_fs_attrs_group = {
  1138. .attrs = csra66x0_fs_attrs,
  1139. };
  1140. static int csra66x0_sysfs_create(struct i2c_client *client,
  1141. struct csra66x0_priv *csra66x0)
  1142. {
  1143. int rc;
  1144. rc = sysfs_create_group(&client->dev.kobj, &csra66x0_fs_attrs_group);
  1145. return rc;
  1146. }
  1147. static void csra66x0_sysfs_remove(struct i2c_client *client,
  1148. struct csra66x0_priv *csra66x0)
  1149. {
  1150. sysfs_remove_group(&client->dev.kobj, &csra66x0_fs_attrs_group);
  1151. }
  1152. #if IS_ENABLED(CONFIG_I2C)
  1153. static int csra66x0_i2c_probe(struct i2c_client *client_i2c,
  1154. const struct i2c_device_id *id)
  1155. {
  1156. struct csra66x0_priv *csra66x0;
  1157. int ret, irq_trigger;
  1158. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1159. char debugfs_dir_name[32];
  1160. #endif
  1161. csra66x0 = devm_kzalloc(&client_i2c->dev, sizeof(struct csra66x0_priv),
  1162. GFP_KERNEL);
  1163. if (csra66x0 == NULL)
  1164. return -ENOMEM;
  1165. csra66x0->regmap = devm_regmap_init_i2c(client_i2c,
  1166. &csra66x0_regmap_config);
  1167. if (IS_ERR(csra66x0->regmap)) {
  1168. ret = PTR_ERR(csra66x0->regmap);
  1169. dev_err(&client_i2c->dev,
  1170. "%s %d: Failed to allocate register map for I2C device: %d\n",
  1171. __func__, __LINE__, ret);
  1172. return ret;
  1173. }
  1174. i2c_set_clientdata(client_i2c, csra66x0);
  1175. /* get data from device tree */
  1176. if (client_i2c->dev.of_node) {
  1177. /* cluster of multiple devices */
  1178. ret = of_property_read_u32(
  1179. client_i2c->dev.of_node, "qcom,csra-cluster",
  1180. &csra66x0->in_cluster);
  1181. if (ret) {
  1182. dev_info(&client_i2c->dev,
  1183. "%s: qcom,csra-cluster property not defined in DT\n", __func__);
  1184. csra66x0->in_cluster = 0;
  1185. }
  1186. /* master or slave device */
  1187. ret = of_property_read_u32(
  1188. client_i2c->dev.of_node, "qcom,csra-cluster-master",
  1189. &csra66x0->is_master);
  1190. if (ret) {
  1191. dev_info(&client_i2c->dev,
  1192. "%s: qcom,csra-cluster-master property not defined in DT, slave assumed\n",
  1193. __func__);
  1194. csra66x0->is_master = 0;
  1195. }
  1196. /* gpio setup for vreg */
  1197. csra66x0->vreg_gpio = of_get_named_gpio(client_i2c->dev.of_node,
  1198. "qcom,csra-vreg-en-gpio", 0);
  1199. if (!gpio_is_valid(csra66x0->vreg_gpio)) {
  1200. dev_err(&client_i2c->dev, "%s: %s property is not found %d\n",
  1201. __func__, "qcom,csra-vreg-en-gpio",
  1202. csra66x0->vreg_gpio);
  1203. return -ENODEV;
  1204. }
  1205. dev_dbg(&client_i2c->dev, "%s: vreg_en gpio %d\n", __func__,
  1206. csra66x0->vreg_gpio);
  1207. ret = gpio_request(csra66x0->vreg_gpio, dev_name(&client_i2c->dev));
  1208. if (ret) {
  1209. if (ret == -EBUSY) {
  1210. /* GPIO was already requested */
  1211. dev_dbg(&client_i2c->dev,
  1212. "%s: gpio %d is already set\n",
  1213. __func__, csra66x0->vreg_gpio);
  1214. } else {
  1215. dev_err(&client_i2c->dev, "%s: Failed to request gpio %d, err: %d\n",
  1216. __func__, csra66x0->vreg_gpio, ret);
  1217. }
  1218. } else {
  1219. gpio_direction_output(csra66x0->vreg_gpio, 1);
  1220. gpio_set_value(csra66x0->vreg_gpio, 0);
  1221. }
  1222. /* register interrupt handle */
  1223. if (client_i2c->irq) {
  1224. csra66x0->irq = client_i2c->irq;
  1225. /* interrupt polarity */
  1226. ret = of_property_read_u32(
  1227. client_i2c->dev.of_node, "irq-active-low",
  1228. &csra66x0->irq_active_low);
  1229. if (ret) {
  1230. dev_info(&client_i2c->dev,
  1231. "%s: irq-active-low property not defined in DT\n", __func__);
  1232. csra66x0->irq_active_low = 0;
  1233. }
  1234. if (csra66x0->irq_active_low)
  1235. irq_trigger = IRQF_TRIGGER_LOW;
  1236. else
  1237. irq_trigger = IRQF_TRIGGER_HIGH;
  1238. ret = devm_request_threaded_irq(&client_i2c->dev,
  1239. csra66x0->irq, NULL, csra66x0_irq,
  1240. irq_trigger | IRQF_ONESHOT,
  1241. "csra66x0_irq", csra66x0);
  1242. if (ret) {
  1243. dev_err(&client_i2c->dev,
  1244. "%s: Failed to request IRQ %d: %d\n",
  1245. __func__, csra66x0->irq, ret);
  1246. csra66x0->irq = 0;
  1247. }
  1248. }
  1249. }
  1250. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1251. /* debugfs interface */
  1252. snprintf(debugfs_dir_name, sizeof(debugfs_dir_name), "%s-%s",
  1253. client_i2c->name, dev_name(&client_i2c->dev));
  1254. csra66x0->debugfs_dir = debugfs_create_dir(debugfs_dir_name, NULL);
  1255. if (!csra66x0->debugfs_dir) {
  1256. dev_dbg(&client_i2c->dev,
  1257. "%s: Failed to create /sys/kernel/debug/%s for debugfs\n",
  1258. __func__, debugfs_dir_name);
  1259. ret = -ENOMEM;
  1260. goto err_debugfs;
  1261. }
  1262. csra66x0->debugfs_file_wo = debugfs_create_file(
  1263. "write_reg_val", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  1264. (void *) csra66x0,
  1265. &debugfs_codec_ops);
  1266. if (!csra66x0->debugfs_file_wo) {
  1267. dev_dbg(&client_i2c->dev,
  1268. "%s: Failed to create /sys/kernel/debug/%s/write_reg_val\n",
  1269. __func__, debugfs_dir_name);
  1270. ret = -ENOMEM;
  1271. goto err_debugfs;
  1272. }
  1273. csra66x0->debugfs_file_ro = debugfs_create_file(
  1274. "show_reg_dump", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  1275. (void *) csra66x0,
  1276. &debugfs_codec_ops);
  1277. if (!csra66x0->debugfs_file_ro) {
  1278. dev_dbg(&client_i2c->dev,
  1279. "%s: Failed to create /sys/kernel/debug/%s/show_reg_dump\n",
  1280. __func__, debugfs_dir_name);
  1281. ret = -ENOMEM;
  1282. goto err_debugfs;
  1283. }
  1284. #endif /* CONFIG_DEBUG_FS */
  1285. /* register component */
  1286. ret = snd_soc_register_component(&client_i2c->dev,
  1287. &soc_codec_drv_csra66x0, NULL, 0);
  1288. if (ret != 0) {
  1289. dev_err(&client_i2c->dev, "%s %d: Failed to register component: %d\n",
  1290. __func__, __LINE__, ret);
  1291. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  1292. gpio_set_value(csra66x0->vreg_gpio, 0);
  1293. gpio_free(csra66x0->vreg_gpio);
  1294. }
  1295. return ret;
  1296. }
  1297. ret = csra66x0_sysfs_create(client_i2c, csra66x0);
  1298. if (ret) {
  1299. dev_err(&client_i2c->dev, "%s: sysfs creation failed ret=%d\n",
  1300. __func__, ret);
  1301. goto err_sysfs;
  1302. }
  1303. return 0;
  1304. err_sysfs:
  1305. snd_soc_unregister_component(&client_i2c->dev);
  1306. return ret;
  1307. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1308. err_debugfs:
  1309. debugfs_remove_recursive(csra66x0->debugfs_dir);
  1310. return ret;
  1311. #endif
  1312. }
  1313. static int csra66x0_i2c_remove(struct i2c_client *client_i2c)
  1314. {
  1315. struct csra66x0_priv *csra66x0 = i2c_get_clientdata(client_i2c);
  1316. if (csra66x0) {
  1317. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  1318. gpio_set_value(csra66x0->vreg_gpio, 0);
  1319. gpio_free(csra66x0->vreg_gpio);
  1320. }
  1321. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1322. debugfs_remove_recursive(csra66x0->debugfs_dir);
  1323. #endif
  1324. }
  1325. csra66x0_sysfs_remove(client_i2c, csra66x0);
  1326. snd_soc_unregister_component(&i2c_client->dev);
  1327. return 0;
  1328. }
  1329. static const struct i2c_device_id csra66x0_i2c_id[] = {
  1330. { "csra66x0", 0},
  1331. { }
  1332. };
  1333. MODULE_DEVICE_TABLE(i2c, csra66x0_i2c_id);
  1334. static struct i2c_driver csra66x0_i2c_driver = {
  1335. .probe = csra66x0_i2c_probe,
  1336. .remove = csra66x0_i2c_remove,
  1337. .id_table = csra66x0_i2c_id,
  1338. .driver = {
  1339. .name = "csra66x0",
  1340. .owner = THIS_MODULE,
  1341. .of_match_table = csra66x0_of_match
  1342. },
  1343. };
  1344. #endif
  1345. static int __init csra66x0_codec_init(void)
  1346. {
  1347. int ret = 0;
  1348. #if IS_ENABLED(CONFIG_I2C)
  1349. ret = i2c_add_driver(&csra66x0_i2c_driver);
  1350. if (ret != 0)
  1351. pr_err("%s: Failed to register CSRA66X0 I2C driver, ret = %d\n",
  1352. __func__, ret);
  1353. #endif
  1354. return ret;
  1355. }
  1356. module_init(csra66x0_codec_init);
  1357. static void __exit csra66x0_codec_exit(void)
  1358. {
  1359. #if IS_ENABLED(CONFIG_I2C)
  1360. i2c_del_driver(&csra66x0_i2c_driver);
  1361. #endif
  1362. }
  1363. module_exit(csra66x0_codec_exit);
  1364. MODULE_DESCRIPTION("CSRA66X0 Codec driver");
  1365. MODULE_LICENSE("GPL v2");