csra66x0.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, 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. {NULL, "CSRA_DE"},
  331. {NULL, "CSRA_F0"}
  332. };
  333. static int sysfs_get_param(char *buf, u32 *param, int num_of_par)
  334. {
  335. char *token;
  336. int base, cnt;
  337. token = strsep(&buf, " ");
  338. for (cnt = 0; cnt < num_of_par; cnt++) {
  339. if (token) {
  340. if ((token[1] == 'x') || (token[1] == 'X'))
  341. base = 16;
  342. else
  343. base = 10;
  344. if (kstrtou32(token, base, &param[cnt]) != 0)
  345. return -EINVAL;
  346. token = strsep(&buf, " ");
  347. } else {
  348. return -EINVAL;
  349. }
  350. }
  351. return 0;
  352. }
  353. #if IS_ENABLED(CONFIG_DEBUG_FS)
  354. static int debugfs_codec_open_op(struct inode *inode, struct file *file)
  355. {
  356. file->private_data = inode->i_private;
  357. return 0;
  358. }
  359. static ssize_t debugfs_codec_write_op(struct file *filp,
  360. const char __user *ubuf, size_t cnt, loff_t *ppos)
  361. {
  362. struct csra66x0_priv *csra66x0 =
  363. (struct csra66x0_priv *) filp->private_data;
  364. struct snd_soc_component *component = csra66x0->component;
  365. char lbuf[32];
  366. int rc;
  367. u32 param[2];
  368. if (!filp || !ppos || !ubuf || !component)
  369. return -EINVAL;
  370. if (cnt > sizeof(lbuf) - 1)
  371. return -EINVAL;
  372. rc = copy_from_user(lbuf, ubuf, cnt);
  373. if (rc)
  374. return -EFAULT;
  375. lbuf[cnt] = '\0';
  376. rc = sysfs_get_param(lbuf, param, 2);
  377. if (!(csra66x0_addr_is_in_range(param[0],
  378. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  379. || csra66x0_addr_is_in_range(param[0],
  380. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  381. dev_err(component->dev, "%s: register address 0x%04X out of range\n",
  382. __func__, param[0]);
  383. return -EINVAL;
  384. }
  385. if ((param[1] < 0) || (param[1] > 255)) {
  386. dev_err(component->dev, "%s: register data 0x%02X out of range\n",
  387. __func__, param[1]);
  388. return -EINVAL;
  389. }
  390. if (rc == 0)
  391. {
  392. rc = cnt;
  393. dev_info(component->dev, "%s: reg[0x%04X]=0x%02X\n",
  394. __func__, param[0], param[1]);
  395. snd_soc_component_write(component, param[0], param[1]);
  396. } else {
  397. dev_err(component->dev, "%s: write to register addr=0x%04X failed\n",
  398. __func__, param[0]);
  399. }
  400. return rc;
  401. }
  402. static ssize_t debugfs_csra66x0_reg_show(struct csra66x0_priv *csra66x0,
  403. char __user *ubuf, size_t count, loff_t *ppos)
  404. {
  405. int i, reg_val, len;
  406. int addr_min, addr_max;
  407. ssize_t total = 0;
  408. char tmp_buf[20];
  409. struct snd_soc_component *component = csra66x0->component;
  410. if (!ubuf || !ppos || !component || *ppos < 0)
  411. return -EINVAL;
  412. if (csra66x0_addr_is_in_range(csra66x0->sysfs_reg_addr,
  413. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR)) {
  414. addr_min = CSRA66X0_COEFF_BASE;
  415. addr_max = CSRA66X0_MAX_COEFF_ADDR;
  416. csra66x0->sysfs_reg_addr = CSRA66X0_BASE;
  417. } else {
  418. addr_min = CSRA66X0_BASE;
  419. addr_max = CSRA66X0_MAX_REGISTER_ADDR;
  420. }
  421. for (i = ((int) *ppos + addr_min);
  422. i <= addr_max; i++) {
  423. reg_val = snd_soc_component_read32(component, i);
  424. len = snprintf(tmp_buf, 20, "0x%04X: 0x%02X\n", i, (reg_val & 0xFF));
  425. if ((total + len) >= count - 1)
  426. break;
  427. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  428. dev_err(component->dev, "%s: fail to copy reg dump\n",
  429. __func__);
  430. total = -EFAULT;
  431. goto copy_err;
  432. }
  433. *ppos += len;
  434. total += len;
  435. }
  436. copy_err:
  437. return total;
  438. }
  439. static ssize_t debugfs_codec_read_op(struct file *filp,
  440. char __user *ubuf, size_t cnt, loff_t *ppos)
  441. {
  442. struct csra66x0_priv *csra66x0 =
  443. (struct csra66x0_priv *) filp->private_data;
  444. ssize_t ret_cnt;
  445. if (!filp || !ppos || !ubuf || *ppos < 0)
  446. return -EINVAL;
  447. ret_cnt = debugfs_csra66x0_reg_show(csra66x0, ubuf, cnt, ppos);
  448. return ret_cnt;
  449. }
  450. static const struct file_operations debugfs_codec_ops = {
  451. .open = debugfs_codec_open_op,
  452. .write = debugfs_codec_write_op,
  453. .read = debugfs_codec_read_op,
  454. };
  455. #endif /* CONFIG_DEBUG_FS */
  456. /*
  457. * CSRA66X0 Controls
  458. */
  459. static const DECLARE_TLV_DB_SCALE(csra66x0_volume_tlv, -9000, 25, 0);
  460. static const DECLARE_TLV_DB_RANGE(csra66x0_bass_treble_tlv,
  461. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  462. 1, 15, TLV_DB_SCALE_ITEM(-1500, 100, 0),
  463. 16, 30, TLV_DB_SCALE_ITEM(100, 100, 0)
  464. );
  465. static int csra66x0_get_volume(struct snd_kcontrol *kcontrol,
  466. struct snd_ctl_elem_value *ucontrol)
  467. {
  468. struct soc_mixer_control *mc =
  469. (struct soc_mixer_control *)kcontrol->private_value;
  470. struct snd_soc_component *component =
  471. snd_soc_kcontrol_component(kcontrol);
  472. unsigned int reg_l = mc->reg;
  473. unsigned int reg_r = mc->rreg;
  474. unsigned int val_l, val_r;
  475. val_l = (snd_soc_component_read32(component, reg_l) & 0xff) |
  476. ((snd_soc_component_read32(component,
  477. CSRA66X0_CH1_VOLUME_1_FA) & (0x01)) << 8);
  478. val_r = (snd_soc_component_read32(component, reg_r) & 0xff) |
  479. ((snd_soc_component_read32(component,
  480. CSRA66X0_CH2_VOLUME_1_FA) & (0x01)) << 8);
  481. ucontrol->value.integer.value[0] = val_l;
  482. ucontrol->value.integer.value[1] = val_r;
  483. return 0;
  484. }
  485. static int csra66x0_set_volume(struct snd_kcontrol *kcontrol,
  486. struct snd_ctl_elem_value *ucontrol)
  487. {
  488. struct soc_mixer_control *mc =
  489. (struct soc_mixer_control *)kcontrol->private_value;
  490. struct snd_soc_component *component =
  491. snd_soc_kcontrol_component(kcontrol);
  492. struct csra66x0_priv *csra66x0 =
  493. snd_soc_component_get_drvdata(component);
  494. unsigned int reg_l = mc->reg;
  495. unsigned int reg_r = mc->rreg;
  496. unsigned int val_l[2];
  497. unsigned int val_r[2];
  498. csra66x0->spk_volume_ch1 = (ucontrol->value.integer.value[0]);
  499. csra66x0->spk_volume_ch2 = (ucontrol->value.integer.value[1]);
  500. val_l[0] = csra66x0->spk_volume_ch1 & SPK_VOLUME_LSB_MSK;
  501. val_l[1] = (csra66x0->spk_volume_ch1 & SPK_VOLUME_MSB_MSK) ? 1 : 0;
  502. val_r[0] = csra66x0->spk_volume_ch2 & SPK_VOLUME_LSB_MSK;
  503. val_r[1] = (csra66x0->spk_volume_ch2 & SPK_VOLUME_MSB_MSK) ? 1 : 0;
  504. snd_soc_component_write(component, reg_l, val_l[0]);
  505. snd_soc_component_write(component, reg_r, val_r[0]);
  506. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_1_FA, val_l[1]);
  507. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_1_FA, val_r[1]);
  508. return 0;
  509. }
  510. /* enumerated controls */
  511. static const char * const csra66x0_mute_output_text[] = {"PLAY", "MUTE"};
  512. static const char * const csra66x0_output_invert_text[] = {
  513. "UNCHANGED", "INVERTED"};
  514. static const char * const csra66x0_deemp_config_text[] = {
  515. "DISABLED", "ENABLED"};
  516. SOC_ENUM_SINGLE_DECL(csra66x0_mute_output_enum,
  517. CSRA66X0_MISC_CONTROL_STATUS_1_FA, 2,
  518. csra66x0_mute_output_text);
  519. SOC_ENUM_SINGLE_DECL(csra66x0_ch1_output_invert_enum,
  520. CSRA66X0_CH1_OUTPUT_INVERT_EN, 0,
  521. csra66x0_output_invert_text);
  522. SOC_ENUM_SINGLE_DECL(csra66x0_ch2_output_invert_enum,
  523. CSRA66X0_CH2_OUTPUT_INVERT_EN, 0,
  524. csra66x0_output_invert_text);
  525. SOC_ENUM_DOUBLE_DECL(csra66x0_deemp_config_enum,
  526. CSRA66X0_DEEMP_CONFIG_FA, 0, 1,
  527. csra66x0_deemp_config_text);
  528. static const struct snd_kcontrol_new csra66x0_snd_controls[] = {
  529. /* volume */
  530. SOC_DOUBLE_R_EXT_TLV("PA VOLUME", CSRA66X0_CH1_VOLUME_0_FA,
  531. CSRA66X0_CH2_VOLUME_0_FA, 0, 0x1C9, 0,
  532. csra66x0_get_volume, csra66x0_set_volume,
  533. csra66x0_volume_tlv),
  534. /* bass treble */
  535. SOC_DOUBLE_R_TLV("PA BASS GAIN", CSRA66X0_CH1_BASS_GAIN_CTRL_FA,
  536. CSRA66X0_CH2_BASS_GAIN_CTRL_FA, 0, 0x1E, 0,
  537. csra66x0_bass_treble_tlv),
  538. SOC_DOUBLE_R_TLV("PA TREBLE GAIN", CSRA66X0_CH1_TREBLE_GAIN_CTRL_FA,
  539. CSRA66X0_CH2_TREBLE_GAIN_CTRL_FA, 0, 0x1E, 0,
  540. csra66x0_bass_treble_tlv),
  541. SOC_DOUBLE_R("PA BASS_XOVER FREQ", CSRA66X0_CH1_BASS_FC_CTRL_FA,
  542. CSRA66X0_CH2_BASS_FC_CTRL_FA, 0, 2, 0),
  543. SOC_DOUBLE_R("PA TREBLE_XOVER FREQ", CSRA66X0_CH1_TREBLE_FC_CTRL_FA,
  544. CSRA66X0_CH2_TREBLE_FC_CTRL_FA, 0, 2, 0),
  545. /* switch */
  546. SOC_ENUM("PA MUTE_OUTPUT SWITCH", csra66x0_mute_output_enum),
  547. SOC_ENUM("PA DE-EMPHASIS SWITCH", csra66x0_deemp_config_enum),
  548. };
  549. static const struct snd_kcontrol_new csra_mix_switch[] = {
  550. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  551. };
  552. static const struct snd_soc_dapm_widget csra66x0_dapm_widgets[] = {
  553. SND_SOC_DAPM_INPUT("IN"),
  554. SND_SOC_DAPM_MIXER("MIXER", SND_SOC_NOPM, 0, 0,
  555. csra_mix_switch, ARRAY_SIZE(csra_mix_switch)),
  556. SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
  557. SND_SOC_DAPM_PGA("PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  558. SND_SOC_DAPM_OUTPUT("SPKR"),
  559. };
  560. static const struct snd_soc_dapm_route csra66x0_dapm_routes[] = {
  561. {"MIXER", "Switch", "IN"},
  562. {"DAC", NULL, "MIXER"},
  563. {"PGA", NULL, "DAC"},
  564. {"SPKR", NULL, "PGA"},
  565. };
  566. /*
  567. * csra66x0_hw_free_mute - Update csra66x0 mute register
  568. *
  569. * @component - csra66x0 component
  570. *
  571. */
  572. void csra66x0_hw_free_mute(struct snd_soc_component *component)
  573. {
  574. int val = 0;
  575. if (component == NULL)
  576. return;
  577. val = snd_soc_component_read32(component,
  578. CSRA66X0_MISC_CONTROL_STATUS_1_FA);
  579. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_1_FA,
  580. val | 0x04);
  581. }
  582. EXPORT_SYMBOL(csra66x0_hw_free_mute);
  583. static int csra66x0_wait_for_config_state(struct snd_soc_component *component)
  584. {
  585. u16 val;
  586. int cntdwn = WAIT_FOR_CONFIG_STATE_TIMEOUT_MS;
  587. do {
  588. /* wait >= 100ms to check if HW has moved to config state */
  589. msleep(100);
  590. val = snd_soc_component_read32(component,
  591. CSRA66X0_CHIP_STATE_STATUS_FA);
  592. if (val == CONFIG_STATE_ID)
  593. break;
  594. cntdwn = cntdwn - 100;
  595. } while (cntdwn > 0);
  596. if (cntdwn <= 0)
  597. return -EFAULT;
  598. return 0;
  599. }
  600. static int csra66x0_allow_run(struct csra66x0_priv *csra66x0)
  601. {
  602. struct snd_soc_component *component = csra66x0->component;
  603. int i;
  604. /* csra66x0 is not in cluster */
  605. if (!csra66x0->in_cluster) {
  606. /* enable interrupts */
  607. if (csra66x0->irq) {
  608. snd_soc_component_write(component,
  609. CSRA66X0_PIO0_SELECT, 0x1);
  610. if (csra66x0->irq_active_low)
  611. snd_soc_component_write(component,
  612. CSRA66X0_IRQ_OUTPUT_POLARITY, 0x0);
  613. else
  614. snd_soc_component_write(component,
  615. CSRA66X0_IRQ_OUTPUT_POLARITY, 0x1);
  616. snd_soc_component_write(component,
  617. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  618. } else {
  619. snd_soc_component_write(component,
  620. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  621. }
  622. /* allow run */
  623. snd_soc_component_write(component,
  624. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  625. return 0;
  626. }
  627. /* csra66x0 is part of cluster */
  628. /* get number of probed cluster devices */
  629. csra66x0->num_cluster_devices = 0;
  630. for (i = 0; i < component->card->num_aux_devs; i++) {
  631. if (i >= csra66x0->max_num_cluster_devices)
  632. break;
  633. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  634. continue;
  635. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_probed)
  636. csra66x0->num_cluster_devices++;
  637. }
  638. /* check if all cluster devices are probed */
  639. if (csra66x0->num_cluster_devices
  640. == component->card->num_aux_devs) {
  641. /* allow run of all slave components */
  642. for (i = 0; i < component->card->num_aux_devs; i++) {
  643. if (i >= csra66x0->max_num_cluster_devices)
  644. break;
  645. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  646. continue;
  647. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  648. continue;
  649. snd_soc_component_write(
  650. csra_clust_dev_tbl[i].csra66x0_ptr->component,
  651. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  652. }
  653. /* allow run of all master components */
  654. for (i = 0; i < component->card->num_aux_devs; i++) {
  655. if (i >= csra66x0->max_num_cluster_devices)
  656. break;
  657. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  658. continue;
  659. if (!csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  660. continue;
  661. /* enable interrupts */
  662. if (csra66x0->irq) {
  663. snd_soc_component_write(component,
  664. CSRA66X0_PIO0_SELECT, 0x1);
  665. if (csra66x0->irq_active_low)
  666. snd_soc_component_write(component,
  667. CSRA66X0_IRQ_OUTPUT_POLARITY,
  668. 0x0);
  669. else
  670. snd_soc_component_write(component,
  671. CSRA66X0_IRQ_OUTPUT_POLARITY,
  672. 0x1);
  673. snd_soc_component_write(component,
  674. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  675. } else {
  676. snd_soc_component_write(component,
  677. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  678. }
  679. /* allow run */
  680. snd_soc_component_write(
  681. csra_clust_dev_tbl[i].csra66x0_ptr->component,
  682. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  683. }
  684. }
  685. return 0;
  686. }
  687. static int csra66x0_init(struct csra66x0_priv *csra66x0)
  688. {
  689. struct snd_soc_component *component = csra66x0->component;
  690. int ret;
  691. dev_dbg(component->dev, "%s: initialize %s\n",
  692. __func__, component->name);
  693. csra66x0->sysfs_reg_addr = CSRA66X0_BASE;
  694. /* config */
  695. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  696. SET_CONFIG_STATE);
  697. /* wait until HW is in config state before proceeding */
  698. ret = csra66x0_wait_for_config_state(component);
  699. if (ret) {
  700. dev_err(component->dev, "%s: timeout while %s is waiting for config state\n",
  701. __func__, component->name);
  702. }
  703. /* setup */
  704. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_0,
  705. 0x09);
  706. snd_soc_component_write(component, CSRA66X0_TEMP_PROT_BACKOFF, 0x0C);
  707. snd_soc_component_write(component, CSRA66X0_EXT_PA_PROTECT_POLARITY,
  708. 0x03);
  709. snd_soc_component_write(component, CSRA66X0_PWM_OUTPUT_CONFIG, 0xC8);
  710. csra66x0->spk_volume_ch1 = SPK_VOLUME_M20DB;
  711. csra66x0->spk_volume_ch2 = SPK_VOLUME_M20DB;
  712. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_0_FA,
  713. SPK_VOLUME_M20DB_LSB);
  714. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_0_FA,
  715. SPK_VOLUME_M20DB_LSB);
  716. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_1_FA,
  717. SPK_VOLUME_M20DB_MSB);
  718. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_1_FA,
  719. SPK_VOLUME_M20DB_MSB);
  720. /* disable volume ramping */
  721. snd_soc_component_write(component, CSRA66X0_VOLUME_CONFIG_FA, 0x27);
  722. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_CTRL, 0x0);
  723. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_0,
  724. 0xE7);
  725. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_1,
  726. 0x26);
  727. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_2,
  728. 0x40);
  729. snd_soc_component_write(component, CSRA66X0_MIN_MODULATION_PULSE_WIDTH,
  730. 0x7A);
  731. snd_soc_component_write(component, CSRA66X0_CH1_HARD_CLIP_THRESH, 0x00);
  732. snd_soc_component_write(component, CSRA66X0_CH2_HARD_CLIP_THRESH, 0x00);
  733. snd_soc_component_write(component, CSRA66X0_CH1_DCA_THRESH, 0x40);
  734. snd_soc_component_write(component, CSRA66X0_CH2_DCA_THRESH, 0x40);
  735. snd_soc_component_write(component, CSRA66X0_DCA_ATTACK_RATE, 0x00);
  736. snd_soc_component_write(component, CSRA66X0_DCA_RELEASE_RATE, 0x00);
  737. csra66x0_allow_run(csra66x0);
  738. return 0;
  739. }
  740. static int csra66x0_reset(struct csra66x0_priv *csra66x0)
  741. {
  742. struct snd_soc_component *component = csra66x0->component;
  743. u16 val;
  744. val = snd_soc_component_read32(component, CSRA66X0_FAULT_STATUS_FA);
  745. if (val & FAULT_STATUS_INTERNAL)
  746. dev_dbg(component->dev, "%s: FAULT_STATUS_INTERNAL 0x%X\n",
  747. __func__, val);
  748. if (val & FAULT_STATUS_OTP_INTEGRITY)
  749. dev_dbg(component->dev, "%s: FAULT_STATUS_OTP_INTEGRITY 0x%X\n",
  750. __func__, val);
  751. if (val & FAULT_STATUS_PADS2)
  752. dev_dbg(component->dev, "%s: FAULT_STATUS_PADS2 0x%X\n",
  753. __func__, val);
  754. if (val & FAULT_STATUS_SMPS)
  755. dev_dbg(component->dev, "%s: FAULT_STATUS_SMPS 0x%X\n",
  756. __func__, val);
  757. if (val & FAULT_STATUS_TEMP)
  758. dev_dbg(component->dev, "%s: FAULT_STATUS_TEMP 0x%X\n",
  759. __func__, val);
  760. if (val & FAULT_STATUS_PROTECT)
  761. dev_dbg(component->dev, "%s: FAULT_STATUS_PROTECT 0x%X\n",
  762. __func__, val);
  763. dev_dbg(component->dev, "%s: reset %s\n",
  764. __func__, component->name);
  765. /* clear fault state and re-init */
  766. snd_soc_component_write(component, CSRA66X0_FAULT_STATUS_FA, 0x00);
  767. snd_soc_component_write(component, CSRA66X0_IRQ_OUTPUT_STATUS_FA, 0x00);
  768. /* apply reset to CSRA66X0 */
  769. val = snd_soc_component_read32(component,
  770. CSRA66X0_MISC_CONTROL_STATUS_1_FA);
  771. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_1_FA,
  772. val | 0x08);
  773. /* wait 500ms after reset to recover CSRA66X0 */
  774. msleep(500);
  775. return 0;
  776. }
  777. static int csra66x0_msconfig(struct csra66x0_priv *csra66x0)
  778. {
  779. struct snd_soc_component *component = csra66x0->component;
  780. int ret;
  781. dev_dbg(component->dev, "%s: configure %s\n",
  782. __func__, component->name);
  783. /* config */
  784. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  785. SET_CONFIG_STATE);
  786. /* wait until HW is in config state before proceeding */
  787. ret = csra66x0_wait_for_config_state(component);
  788. if (ret) {
  789. dev_err(component->dev, "%s: timeout while %s is waiting for config state\n",
  790. __func__, component->name);
  791. return ret;
  792. }
  793. snd_soc_component_write(component, CSRA66X0_PIO7_SELECT, 0x04);
  794. snd_soc_component_write(component, CSRA66X0_PIO8_SELECT, 0x04);
  795. if (csra66x0->is_master) {
  796. /* Master specific config */
  797. snd_soc_component_write(component,
  798. CSRA66X0_PIO_PULL_EN0, 0xFF);
  799. snd_soc_component_write(component,
  800. CSRA66X0_PIO_PULL_DIR0, 0x80);
  801. snd_soc_component_write(component,
  802. CSRA66X0_PIO_PULL_EN1, 0x01);
  803. snd_soc_component_write(component,
  804. CSRA66X0_PIO_PULL_DIR1, 0x01);
  805. } else {
  806. /* Slave specific config */
  807. snd_soc_component_write(component,
  808. CSRA66X0_PIO_PULL_EN0, 0x7F);
  809. snd_soc_component_write(component,
  810. CSRA66X0_PIO_PULL_EN1, 0x00);
  811. }
  812. snd_soc_component_write(component, CSRA66X0_DCA_CTRL, 0x05);
  813. return 0;
  814. }
  815. static int csra66x0_soc_probe(struct snd_soc_component *component)
  816. {
  817. struct csra66x0_priv *csra66x0 =
  818. snd_soc_component_get_drvdata(component);
  819. struct snd_soc_dapm_context *dapm;
  820. char name[50];
  821. unsigned int i;
  822. csra66x0->component = component;
  823. if (csra66x0->in_cluster) {
  824. dapm = snd_soc_component_get_dapm(component);
  825. dev_dbg(component->dev, "%s: assign prefix %s to component device %s\n",
  826. __func__, component->name_prefix,
  827. component->name);
  828. /* add device to cluster table */
  829. csra66x0->max_num_cluster_devices =
  830. ARRAY_SIZE(csra_clust_dev_tbl);
  831. for (i = 0; i < csra66x0->max_num_cluster_devices; i++) {
  832. if (!strncmp(component->name_prefix,
  833. csra_clust_dev_tbl[i].csra66x0_prefix,
  834. strnlen(
  835. csra_clust_dev_tbl[i].csra66x0_prefix,
  836. sizeof(
  837. csra_clust_dev_tbl[i].csra66x0_prefix)))) {
  838. csra_clust_dev_tbl[i].csra66x0_ptr = csra66x0;
  839. break;
  840. }
  841. if (i == csra66x0->max_num_cluster_devices - 1)
  842. dev_warn(component->dev,
  843. "%s: Unknown prefix %s of cluster device %s\n",
  844. __func__, component->name_prefix,
  845. component->name);
  846. }
  847. /* master slave config */
  848. csra66x0_msconfig(csra66x0);
  849. if (dapm->component) {
  850. strlcpy(name, dapm->component->name_prefix,
  851. sizeof(name));
  852. strlcat(name, " IN", sizeof(name));
  853. snd_soc_dapm_ignore_suspend(dapm, name);
  854. strlcpy(name, dapm->component->name_prefix,
  855. sizeof(name));
  856. strlcat(name, " SPKR", sizeof(name));
  857. snd_soc_dapm_ignore_suspend(dapm, name);
  858. }
  859. }
  860. /* common initialization */
  861. csra66x0->is_probed = 1;
  862. csra66x0_init(csra66x0);
  863. return 0;
  864. }
  865. static void csra66x0_soc_remove(struct snd_soc_component *component)
  866. {
  867. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  868. SET_STDBY_STATE);
  869. return;
  870. }
  871. static const struct snd_soc_component_driver soc_codec_drv_csra66x0 = {
  872. .name = DRV_NAME,
  873. .probe = csra66x0_soc_probe,
  874. .remove = csra66x0_soc_remove,
  875. .controls = csra66x0_snd_controls,
  876. .num_controls = ARRAY_SIZE(csra66x0_snd_controls),
  877. .dapm_widgets = csra66x0_dapm_widgets,
  878. .num_dapm_widgets = ARRAY_SIZE(csra66x0_dapm_widgets),
  879. .dapm_routes = csra66x0_dapm_routes,
  880. .num_dapm_routes = ARRAY_SIZE(csra66x0_dapm_routes),
  881. };
  882. static struct regmap_config csra66x0_regmap_config = {
  883. .reg_bits = 16,
  884. .val_bits = 8,
  885. .cache_type = REGCACHE_RBTREE,
  886. .reg_defaults = csra66x0_reg_defaults,
  887. .num_reg_defaults = ARRAY_SIZE(csra66x0_reg_defaults),
  888. .max_register = CSRA66X0_MAX_COEFF_ADDR,
  889. .volatile_reg = csra66x0_volatile_register,
  890. .writeable_reg = csra66x0_writeable_registers,
  891. .readable_reg = csra66x0_readable_registers,
  892. };
  893. static irqreturn_t csra66x0_irq(int irq, void *data)
  894. {
  895. struct csra66x0_priv *csra66x0 = (struct csra66x0_priv *) data;
  896. struct snd_soc_component *component = csra66x0->component;
  897. u16 val;
  898. unsigned int i;
  899. /* Treat interrupt before component is initialized as spurious */
  900. if (component == NULL)
  901. return IRQ_NONE;
  902. dev_dbg(component->dev, "%s: csra66x0_interrupt triggered by %s\n",
  903. __func__, component->name);
  904. /* fault indication */
  905. val = snd_soc_component_read32(component, CSRA66X0_IRQ_OUTPUT_STATUS_FA)
  906. & 0x1;
  907. if (!val)
  908. return IRQ_HANDLED;
  909. if (csra66x0->in_cluster) {
  910. /* reset all slave components */
  911. for (i = 0; i < component->card->num_aux_devs; i++) {
  912. if (i >= csra66x0->max_num_cluster_devices)
  913. break;
  914. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  915. continue;
  916. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  917. continue;
  918. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  919. }
  920. /* reset all master components */
  921. for (i = 0; i < component->card->num_aux_devs; i++) {
  922. if (i >= csra66x0->max_num_cluster_devices)
  923. break;
  924. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  925. continue;
  926. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  927. csra66x0_reset(
  928. csra_clust_dev_tbl[i].csra66x0_ptr);
  929. }
  930. /* recover all components */
  931. for (i = 0; i < component->card->num_aux_devs; i++) {
  932. if (i >= csra66x0->max_num_cluster_devices)
  933. break;
  934. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  935. continue;
  936. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  937. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  938. }
  939. } else {
  940. csra66x0_reset(csra66x0);
  941. csra66x0_init(csra66x0);
  942. }
  943. return IRQ_HANDLED;
  944. };
  945. static const struct of_device_id csra66x0_of_match[] = {
  946. { .compatible = "qcom,csra66x0", },
  947. { }
  948. };
  949. MODULE_DEVICE_TABLE(of, csra66x0_of_match);
  950. static ssize_t csra66x0_sysfs_write2reg_addr_value(struct device *dev,
  951. struct device_attribute *attr, const char *buf, size_t count)
  952. {
  953. int ret;
  954. u32 param[2]; /*reg_addr, reg_value */
  955. char lbuf[CSRA66X0_SYSFS_ENTRY_MAX_LEN];
  956. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  957. struct snd_soc_component *component = csra66x0->component;
  958. if (!csra66x0) {
  959. dev_err(component->dev, "%s: invalid input\n", __func__);
  960. return -EINVAL;
  961. }
  962. if (count > sizeof(lbuf) - 1)
  963. return -EINVAL;
  964. ret = strlcpy(lbuf, buf, count);
  965. if (ret != count) {
  966. dev_err(component->dev, "%s: copy input from user space failed. ret=%d\n",
  967. __func__, ret);
  968. ret = -EFAULT;
  969. goto end;
  970. }
  971. lbuf[count] = '\0';
  972. ret = sysfs_get_param(lbuf, param, 2);
  973. if (ret) {
  974. dev_err(component->dev, "%s: get sysfs parameter failed. ret=%d\n",
  975. __func__, ret);
  976. goto end;
  977. }
  978. if (!(csra66x0_addr_is_in_range(param[0],
  979. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  980. || csra66x0_addr_is_in_range(param[0],
  981. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  982. dev_err(component->dev, "%s: register address 0x%04X out of range\n",
  983. __func__, param[0]);
  984. ret = -EINVAL;
  985. goto end;
  986. }
  987. if ((param[1] < 0) || (param[1] > 255)) {
  988. dev_err(component->dev, "%s: register data 0x%02X out of range\n",
  989. __func__, param[1]);
  990. ret = -EINVAL;
  991. goto end;
  992. }
  993. snd_soccomponent_component_write(component, param[0], param[1]);
  994. ret = count;
  995. end:
  996. return ret;
  997. }
  998. static ssize_t csra66x0_sysfs_read2reg_addr_set(struct device *dev,
  999. struct device_attribute *attr, const char *buf, size_t count)
  1000. {
  1001. int ret;
  1002. u32 reg_addr;
  1003. char lbuf[CSRA66X0_SYSFS_ENTRY_MAX_LEN];
  1004. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1005. if (!csra66x0) {
  1006. dev_err(dev, "%s: invalid input\n", __func__);
  1007. return -EINVAL;
  1008. }
  1009. if (count > sizeof(lbuf) - 1)
  1010. return -EINVAL;
  1011. ret = strlcpy(lbuf, buf, count);
  1012. if (ret != count) {
  1013. dev_err(dev, "%s: copy input from user space failed. ret=%d\n",
  1014. __func__, ret);
  1015. ret = -EFAULT;
  1016. goto end;
  1017. }
  1018. lbuf[count] = '\0';
  1019. ret = sysfs_get_param(lbuf, &reg_addr, 1);
  1020. if (ret) {
  1021. dev_err(dev, "%s: get sysfs parameter failed. ret=%d\n",
  1022. __func__, ret);
  1023. goto end;
  1024. }
  1025. if (!(csra66x0_addr_is_in_range(reg_addr,
  1026. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  1027. || csra66x0_addr_is_in_range(reg_addr,
  1028. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  1029. dev_err(dev, "%s: register address 0x%04X out of range\n",
  1030. __func__, reg_addr);
  1031. ret = -EINVAL;
  1032. goto end;
  1033. }
  1034. csra66x0->sysfs_reg_addr = reg_addr;
  1035. ret = count;
  1036. end:
  1037. return ret;
  1038. }
  1039. static ssize_t csra66x0_sysfs_read2reg_addr_get(struct device *dev,
  1040. struct device_attribute *attr, char *buf)
  1041. {
  1042. int ret;
  1043. u32 reg_addr;
  1044. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1045. if (!csra66x0) {
  1046. dev_err(dev, "%s: invalid input\n", __func__);
  1047. return -EINVAL;
  1048. }
  1049. reg_addr = csra66x0->sysfs_reg_addr;
  1050. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1051. "0x%04X\n", reg_addr);
  1052. pr_debug("%s: 0x%04X\n", __func__, reg_addr);
  1053. return ret;
  1054. }
  1055. static ssize_t csra66x0_sysfs_read2reg_value(struct device *dev,
  1056. struct device_attribute *attr, char *buf)
  1057. {
  1058. int ret;
  1059. u32 reg_val, reg_addr;
  1060. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1061. struct snd_soc_component *component = csra66x0->component;
  1062. if (!csra66x0) {
  1063. dev_err(dev, "%s: invalid input\n", __func__);
  1064. return -EINVAL;
  1065. }
  1066. reg_addr = csra66x0->sysfs_reg_addr;
  1067. if (!(csra66x0_addr_is_in_range(reg_addr,
  1068. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  1069. || csra66x0_addr_is_in_range(reg_addr,
  1070. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  1071. pr_debug("%s: 0x%04X: register address out of range\n",
  1072. __func__, reg_addr);
  1073. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1074. "0x%04X: register address out of range\n", reg_addr);
  1075. goto end;
  1076. }
  1077. reg_val = snd_soc_component_read32(component, csra66x0->sysfs_reg_addr);
  1078. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1079. "0x%04X: 0x%02X\n", csra66x0->sysfs_reg_addr, reg_val);
  1080. pr_debug("%s: 0x%04X: 0x%02X\n", __func__,
  1081. csra66x0->sysfs_reg_addr, reg_val);
  1082. end:
  1083. return ret;
  1084. }
  1085. static ssize_t csra66x0_sysfs_reset(struct device *dev,
  1086. struct device_attribute *attr, const char *buf, size_t count)
  1087. {
  1088. int val, rc;
  1089. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1090. struct snd_soc_component *component = csra66x0->component;
  1091. unsigned int i;
  1092. if (!csra66x0) {
  1093. dev_err(dev, "%s: invalid input\n", __func__);
  1094. return -EINVAL;
  1095. }
  1096. rc = kstrtoint(buf, 10, &val);
  1097. if (rc) {
  1098. dev_err(dev, "%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1099. goto end;
  1100. }
  1101. if (val != SYSFS_RESET) {
  1102. dev_err(dev, "%s: value out of range.\n", __func__);
  1103. rc = -EINVAL;
  1104. goto end;
  1105. }
  1106. pr_debug("%s: reset device\n", __func__);
  1107. if (csra66x0->in_cluster) {
  1108. /* reset all slave components */
  1109. for (i = 0; i < component->card->num_aux_devs; i++) {
  1110. if (i >= csra66x0->max_num_cluster_devices)
  1111. break;
  1112. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1113. continue;
  1114. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  1115. continue;
  1116. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  1117. }
  1118. /* reset all master components */
  1119. for (i = 0; i < component->card->num_aux_devs; i++) {
  1120. if (i >= csra66x0->max_num_cluster_devices)
  1121. break;
  1122. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1123. continue;
  1124. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  1125. csra66x0_reset(
  1126. csra_clust_dev_tbl[i].csra66x0_ptr);
  1127. }
  1128. /* recover all components */
  1129. for (i = 0; i < component->card->num_aux_devs; i++) {
  1130. if (i >= csra66x0->max_num_cluster_devices)
  1131. break;
  1132. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1133. continue;
  1134. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  1135. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  1136. }
  1137. } else {
  1138. csra66x0_reset(csra66x0);
  1139. csra66x0_init(csra66x0);
  1140. }
  1141. rc = strnlen(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN);
  1142. end:
  1143. return rc;
  1144. }
  1145. static DEVICE_ATTR(write2reg_addr_value, 0200, NULL,
  1146. csra66x0_sysfs_write2reg_addr_value);
  1147. static DEVICE_ATTR(read2reg_addr, 0644, csra66x0_sysfs_read2reg_addr_get,
  1148. csra66x0_sysfs_read2reg_addr_set);
  1149. static DEVICE_ATTR(read2reg_value, 0444, csra66x0_sysfs_read2reg_value, NULL);
  1150. static DEVICE_ATTR(reset, 0200, NULL, csra66x0_sysfs_reset);
  1151. static struct attribute *csra66x0_fs_attrs[] = {
  1152. &dev_attr_write2reg_addr_value.attr,
  1153. &dev_attr_read2reg_addr.attr,
  1154. &dev_attr_read2reg_value.attr,
  1155. &dev_attr_reset.attr,
  1156. NULL,
  1157. };
  1158. static struct attribute_group csra66x0_fs_attrs_group = {
  1159. .attrs = csra66x0_fs_attrs,
  1160. };
  1161. static int csra66x0_sysfs_create(struct i2c_client *client,
  1162. struct csra66x0_priv *csra66x0)
  1163. {
  1164. int rc;
  1165. rc = sysfs_create_group(&client->dev.kobj, &csra66x0_fs_attrs_group);
  1166. return rc;
  1167. }
  1168. static void csra66x0_sysfs_remove(struct i2c_client *client,
  1169. struct csra66x0_priv *csra66x0)
  1170. {
  1171. sysfs_remove_group(&client->dev.kobj, &csra66x0_fs_attrs_group);
  1172. }
  1173. #if IS_ENABLED(CONFIG_I2C)
  1174. static int csra66x0_i2c_probe(struct i2c_client *client_i2c,
  1175. const struct i2c_device_id *id)
  1176. {
  1177. struct csra66x0_priv *csra66x0;
  1178. int ret, irq_trigger;
  1179. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1180. char debugfs_dir_name[32];
  1181. #endif
  1182. csra66x0 = devm_kzalloc(&client_i2c->dev, sizeof(struct csra66x0_priv),
  1183. GFP_KERNEL);
  1184. if (csra66x0 == NULL)
  1185. return -ENOMEM;
  1186. csra66x0->regmap = devm_regmap_init_i2c(client_i2c,
  1187. &csra66x0_regmap_config);
  1188. if (IS_ERR(csra66x0->regmap)) {
  1189. ret = PTR_ERR(csra66x0->regmap);
  1190. dev_err(&client_i2c->dev,
  1191. "%s %d: Failed to allocate register map for I2C device: %d\n",
  1192. __func__, __LINE__, ret);
  1193. return ret;
  1194. }
  1195. i2c_set_clientdata(client_i2c, csra66x0);
  1196. /* get data from device tree */
  1197. if (client_i2c->dev.of_node) {
  1198. /* cluster of multiple devices */
  1199. ret = of_property_read_u32(
  1200. client_i2c->dev.of_node, "qcom,csra-cluster",
  1201. &csra66x0->in_cluster);
  1202. if (ret) {
  1203. dev_info(&client_i2c->dev,
  1204. "%s: qcom,csra-cluster property not defined in DT\n", __func__);
  1205. csra66x0->in_cluster = 0;
  1206. }
  1207. /* master or slave device */
  1208. ret = of_property_read_u32(
  1209. client_i2c->dev.of_node, "qcom,csra-cluster-master",
  1210. &csra66x0->is_master);
  1211. if (ret) {
  1212. dev_info(&client_i2c->dev,
  1213. "%s: qcom,csra-cluster-master property not defined in DT, slave assumed\n",
  1214. __func__);
  1215. csra66x0->is_master = 0;
  1216. }
  1217. /* gpio setup for vreg */
  1218. csra66x0->vreg_gpio = of_get_named_gpio(client_i2c->dev.of_node,
  1219. "qcom,csra-vreg-en-gpio", 0);
  1220. if (!gpio_is_valid(csra66x0->vreg_gpio)) {
  1221. dev_err(&client_i2c->dev, "%s: %s property is not found %d\n",
  1222. __func__, "qcom,csra-vreg-en-gpio",
  1223. csra66x0->vreg_gpio);
  1224. return -ENODEV;
  1225. }
  1226. dev_dbg(&client_i2c->dev, "%s: vreg_en gpio %d\n", __func__,
  1227. csra66x0->vreg_gpio);
  1228. ret = gpio_request(csra66x0->vreg_gpio, dev_name(&client_i2c->dev));
  1229. if (ret) {
  1230. if (ret == -EBUSY) {
  1231. /* GPIO was already requested */
  1232. dev_dbg(&client_i2c->dev,
  1233. "%s: gpio %d is already set\n",
  1234. __func__, csra66x0->vreg_gpio);
  1235. } else {
  1236. dev_err(&client_i2c->dev, "%s: Failed to request gpio %d, err: %d\n",
  1237. __func__, csra66x0->vreg_gpio, ret);
  1238. }
  1239. } else {
  1240. gpio_direction_output(csra66x0->vreg_gpio, 1);
  1241. gpio_set_value(csra66x0->vreg_gpio, 0);
  1242. }
  1243. /* register interrupt handle */
  1244. if (client_i2c->irq) {
  1245. csra66x0->irq = client_i2c->irq;
  1246. /* interrupt polarity */
  1247. ret = of_property_read_u32(
  1248. client_i2c->dev.of_node, "irq-active-low",
  1249. &csra66x0->irq_active_low);
  1250. if (ret) {
  1251. dev_info(&client_i2c->dev,
  1252. "%s: irq-active-low property not defined in DT\n", __func__);
  1253. csra66x0->irq_active_low = 0;
  1254. }
  1255. if (csra66x0->irq_active_low)
  1256. irq_trigger = IRQF_TRIGGER_LOW;
  1257. else
  1258. irq_trigger = IRQF_TRIGGER_HIGH;
  1259. ret = devm_request_threaded_irq(&client_i2c->dev,
  1260. csra66x0->irq, NULL, csra66x0_irq,
  1261. irq_trigger | IRQF_ONESHOT,
  1262. "csra66x0_irq", csra66x0);
  1263. if (ret) {
  1264. dev_err(&client_i2c->dev,
  1265. "%s: Failed to request IRQ %d: %d\n",
  1266. __func__, csra66x0->irq, ret);
  1267. csra66x0->irq = 0;
  1268. }
  1269. }
  1270. }
  1271. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1272. /* debugfs interface */
  1273. snprintf(debugfs_dir_name, sizeof(debugfs_dir_name), "%s-%s",
  1274. client_i2c->name, dev_name(&client_i2c->dev));
  1275. csra66x0->debugfs_dir = debugfs_create_dir(debugfs_dir_name, NULL);
  1276. if (!csra66x0->debugfs_dir) {
  1277. dev_dbg(&client_i2c->dev,
  1278. "%s: Failed to create /sys/kernel/debug/%s for debugfs\n",
  1279. __func__, debugfs_dir_name);
  1280. ret = -ENOMEM;
  1281. goto err_debugfs;
  1282. }
  1283. csra66x0->debugfs_file_wo = debugfs_create_file(
  1284. "write_reg_val", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  1285. (void *) csra66x0,
  1286. &debugfs_codec_ops);
  1287. if (!csra66x0->debugfs_file_wo) {
  1288. dev_dbg(&client_i2c->dev,
  1289. "%s: Failed to create /sys/kernel/debug/%s/write_reg_val\n",
  1290. __func__, debugfs_dir_name);
  1291. ret = -ENOMEM;
  1292. goto err_debugfs;
  1293. }
  1294. csra66x0->debugfs_file_ro = debugfs_create_file(
  1295. "show_reg_dump", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  1296. (void *) csra66x0,
  1297. &debugfs_codec_ops);
  1298. if (!csra66x0->debugfs_file_ro) {
  1299. dev_dbg(&client_i2c->dev,
  1300. "%s: Failed to create /sys/kernel/debug/%s/show_reg_dump\n",
  1301. __func__, debugfs_dir_name);
  1302. ret = -ENOMEM;
  1303. goto err_debugfs;
  1304. }
  1305. #endif /* CONFIG_DEBUG_FS */
  1306. /* register component */
  1307. ret = snd_soc_register_component(&client_i2c->dev,
  1308. &soc_codec_drv_csra66x0, NULL, 0);
  1309. if (ret != 0) {
  1310. dev_err(&client_i2c->dev, "%s %d: Failed to register component: %d\n",
  1311. __func__, __LINE__, ret);
  1312. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  1313. gpio_set_value(csra66x0->vreg_gpio, 0);
  1314. gpio_free(csra66x0->vreg_gpio);
  1315. }
  1316. return ret;
  1317. }
  1318. ret = csra66x0_sysfs_create(client_i2c, csra66x0);
  1319. if (ret) {
  1320. dev_err(&client_i2c->dev, "%s: sysfs creation failed ret=%d\n",
  1321. __func__, ret);
  1322. goto err_sysfs;
  1323. }
  1324. return 0;
  1325. err_sysfs:
  1326. snd_soc_unregister_component(&client_i2c->dev);
  1327. return ret;
  1328. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1329. err_debugfs:
  1330. debugfs_remove_recursive(csra66x0->debugfs_dir);
  1331. return ret;
  1332. #endif
  1333. }
  1334. static int csra66x0_i2c_remove(struct i2c_client *client_i2c)
  1335. {
  1336. struct csra66x0_priv *csra66x0 = i2c_get_clientdata(client_i2c);
  1337. if (csra66x0) {
  1338. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  1339. gpio_set_value(csra66x0->vreg_gpio, 0);
  1340. gpio_free(csra66x0->vreg_gpio);
  1341. }
  1342. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1343. debugfs_remove_recursive(csra66x0->debugfs_dir);
  1344. #endif
  1345. }
  1346. csra66x0_sysfs_remove(client_i2c, csra66x0);
  1347. snd_soc_unregister_component(&i2c_client->dev);
  1348. return 0;
  1349. }
  1350. static const struct i2c_device_id csra66x0_i2c_id[] = {
  1351. { "csra66x0", 0},
  1352. { }
  1353. };
  1354. MODULE_DEVICE_TABLE(i2c, csra66x0_i2c_id);
  1355. static struct i2c_driver csra66x0_i2c_driver = {
  1356. .probe = csra66x0_i2c_probe,
  1357. .remove = csra66x0_i2c_remove,
  1358. .id_table = csra66x0_i2c_id,
  1359. .driver = {
  1360. .name = "csra66x0",
  1361. .owner = THIS_MODULE,
  1362. .of_match_table = csra66x0_of_match
  1363. },
  1364. };
  1365. #endif
  1366. static int __init csra66x0_codec_init(void)
  1367. {
  1368. int ret = 0;
  1369. #if IS_ENABLED(CONFIG_I2C)
  1370. ret = i2c_add_driver(&csra66x0_i2c_driver);
  1371. if (ret != 0)
  1372. pr_err("%s: Failed to register CSRA66X0 I2C driver, ret = %d\n",
  1373. __func__, ret);
  1374. #endif
  1375. return ret;
  1376. }
  1377. module_init(csra66x0_codec_init);
  1378. static void __exit csra66x0_codec_exit(void)
  1379. {
  1380. #if IS_ENABLED(CONFIG_I2C)
  1381. i2c_del_driver(&csra66x0_i2c_driver);
  1382. #endif
  1383. }
  1384. module_exit(csra66x0_codec_exit);
  1385. MODULE_DESCRIPTION("CSRA66X0 Codec driver");
  1386. MODULE_LICENSE("GPL v2");