csra66x0.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. {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. static int csra66x0_wait_for_config_state(struct snd_soc_component *component)
  567. {
  568. u16 val;
  569. int cntdwn = WAIT_FOR_CONFIG_STATE_TIMEOUT_MS;
  570. do {
  571. /* wait >= 100ms to check if HW has moved to config state */
  572. msleep(100);
  573. val = snd_soc_component_read32(component,
  574. CSRA66X0_CHIP_STATE_STATUS_FA);
  575. if (val == CONFIG_STATE_ID)
  576. break;
  577. cntdwn = cntdwn - 100;
  578. } while (cntdwn > 0);
  579. if (cntdwn <= 0)
  580. return -EFAULT;
  581. return 0;
  582. }
  583. static int csra66x0_allow_run(struct csra66x0_priv *csra66x0)
  584. {
  585. struct snd_soc_component *component = csra66x0->component;
  586. int i;
  587. /* csra66x0 is not in cluster */
  588. if (!csra66x0->in_cluster) {
  589. /* enable interrupts */
  590. if (csra66x0->irq) {
  591. snd_soc_component_write(component,
  592. CSRA66X0_PIO0_SELECT, 0x1);
  593. if (csra66x0->irq_active_low)
  594. snd_soc_component_write(component,
  595. CSRA66X0_IRQ_OUTPUT_POLARITY, 0x0);
  596. else
  597. snd_soc_component_write(component,
  598. CSRA66X0_IRQ_OUTPUT_POLARITY, 0x1);
  599. snd_soc_component_write(component,
  600. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  601. } else {
  602. snd_soc_component_write(component,
  603. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  604. }
  605. /* allow run */
  606. snd_soc_component_write(component,
  607. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  608. return 0;
  609. }
  610. /* csra66x0 is part of cluster */
  611. /* get number of probed cluster devices */
  612. csra66x0->num_cluster_devices = 0;
  613. for (i = 0; i < component->card->num_aux_devs; i++) {
  614. if (i >= csra66x0->max_num_cluster_devices)
  615. break;
  616. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  617. continue;
  618. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_probed)
  619. csra66x0->num_cluster_devices++;
  620. }
  621. /* check if all cluster devices are probed */
  622. if (csra66x0->num_cluster_devices
  623. == component->card->num_aux_devs) {
  624. /* allow run of all slave components */
  625. for (i = 0; i < component->card->num_aux_devs; i++) {
  626. if (i >= csra66x0->max_num_cluster_devices)
  627. break;
  628. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  629. continue;
  630. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  631. continue;
  632. snd_soc_component_write(
  633. csra_clust_dev_tbl[i].csra66x0_ptr->component,
  634. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  635. }
  636. /* allow run of all master components */
  637. for (i = 0; i < component->card->num_aux_devs; i++) {
  638. if (i >= csra66x0->max_num_cluster_devices)
  639. break;
  640. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  641. continue;
  642. if (!csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  643. continue;
  644. /* enable interrupts */
  645. if (csra66x0->irq) {
  646. snd_soc_component_write(component,
  647. CSRA66X0_PIO0_SELECT, 0x1);
  648. if (csra66x0->irq_active_low)
  649. snd_soc_component_write(component,
  650. CSRA66X0_IRQ_OUTPUT_POLARITY,
  651. 0x0);
  652. else
  653. snd_soc_component_write(component,
  654. CSRA66X0_IRQ_OUTPUT_POLARITY,
  655. 0x1);
  656. snd_soc_component_write(component,
  657. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x01);
  658. } else {
  659. snd_soc_component_write(component,
  660. CSRA66X0_IRQ_OUTPUT_ENABLE, 0x00);
  661. }
  662. /* allow run */
  663. snd_soc_component_write(
  664. csra_clust_dev_tbl[i].csra66x0_ptr->component,
  665. CSRA66X0_CHIP_STATE_CTRL_FA, SET_RUN_STATE);
  666. }
  667. }
  668. return 0;
  669. }
  670. static int csra66x0_init(struct csra66x0_priv *csra66x0)
  671. {
  672. struct snd_soc_component *component = csra66x0->component;
  673. int ret;
  674. dev_dbg(component->dev, "%s: initialize %s\n",
  675. __func__, component->name);
  676. csra66x0->sysfs_reg_addr = CSRA66X0_BASE;
  677. /* config */
  678. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  679. SET_CONFIG_STATE);
  680. /* wait until HW is in config state before proceeding */
  681. ret = csra66x0_wait_for_config_state(component);
  682. if (ret) {
  683. dev_err(component->dev, "%s: timeout while %s is waiting for config state\n",
  684. __func__, component->name);
  685. }
  686. /* setup */
  687. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_0,
  688. 0x09);
  689. snd_soc_component_write(component, CSRA66X0_TEMP_PROT_BACKOFF, 0x0C);
  690. snd_soc_component_write(component, CSRA66X0_EXT_PA_PROTECT_POLARITY,
  691. 0x03);
  692. snd_soc_component_write(component, CSRA66X0_PWM_OUTPUT_CONFIG, 0xC8);
  693. csra66x0->spk_volume_ch1 = SPK_VOLUME_M20DB;
  694. csra66x0->spk_volume_ch2 = SPK_VOLUME_M20DB;
  695. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_0_FA,
  696. SPK_VOLUME_M20DB_LSB);
  697. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_0_FA,
  698. SPK_VOLUME_M20DB_LSB);
  699. snd_soc_component_write(component, CSRA66X0_CH1_VOLUME_1_FA,
  700. SPK_VOLUME_M20DB_MSB);
  701. snd_soc_component_write(component, CSRA66X0_CH2_VOLUME_1_FA,
  702. SPK_VOLUME_M20DB_MSB);
  703. /* disable volume ramping */
  704. snd_soc_component_write(component, CSRA66X0_VOLUME_CONFIG_FA, 0x27);
  705. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_CTRL, 0x0);
  706. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_0,
  707. 0xE7);
  708. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_1,
  709. 0x26);
  710. snd_soc_component_write(component, CSRA66X0_DEAD_TIME_THRESHOLD_2,
  711. 0x40);
  712. snd_soc_component_write(component, CSRA66X0_MIN_MODULATION_PULSE_WIDTH,
  713. 0x7A);
  714. snd_soc_component_write(component, CSRA66X0_CH1_HARD_CLIP_THRESH, 0x00);
  715. snd_soc_component_write(component, CSRA66X0_CH2_HARD_CLIP_THRESH, 0x00);
  716. snd_soc_component_write(component, CSRA66X0_CH1_DCA_THRESH, 0x40);
  717. snd_soc_component_write(component, CSRA66X0_CH2_DCA_THRESH, 0x40);
  718. snd_soc_component_write(component, CSRA66X0_DCA_ATTACK_RATE, 0x00);
  719. snd_soc_component_write(component, CSRA66X0_DCA_RELEASE_RATE, 0x00);
  720. csra66x0_allow_run(csra66x0);
  721. return 0;
  722. }
  723. static int csra66x0_reset(struct csra66x0_priv *csra66x0)
  724. {
  725. struct snd_soc_component *component = csra66x0->component;
  726. u16 val;
  727. val = snd_soc_component_read32(component, CSRA66X0_FAULT_STATUS_FA);
  728. if (val & FAULT_STATUS_INTERNAL)
  729. dev_dbg(component->dev, "%s: FAULT_STATUS_INTERNAL 0x%X\n",
  730. __func__, val);
  731. if (val & FAULT_STATUS_OTP_INTEGRITY)
  732. dev_dbg(component->dev, "%s: FAULT_STATUS_OTP_INTEGRITY 0x%X\n",
  733. __func__, val);
  734. if (val & FAULT_STATUS_PADS2)
  735. dev_dbg(component->dev, "%s: FAULT_STATUS_PADS2 0x%X\n",
  736. __func__, val);
  737. if (val & FAULT_STATUS_SMPS)
  738. dev_dbg(component->dev, "%s: FAULT_STATUS_SMPS 0x%X\n",
  739. __func__, val);
  740. if (val & FAULT_STATUS_TEMP)
  741. dev_dbg(component->dev, "%s: FAULT_STATUS_TEMP 0x%X\n",
  742. __func__, val);
  743. if (val & FAULT_STATUS_PROTECT)
  744. dev_dbg(component->dev, "%s: FAULT_STATUS_PROTECT 0x%X\n",
  745. __func__, val);
  746. dev_dbg(component->dev, "%s: reset %s\n",
  747. __func__, component->name);
  748. /* clear fault state and re-init */
  749. snd_soc_component_write(component, CSRA66X0_FAULT_STATUS_FA, 0x00);
  750. snd_soc_component_write(component, CSRA66X0_IRQ_OUTPUT_STATUS_FA, 0x00);
  751. /* apply reset to CSRA66X0 */
  752. val = snd_soc_component_read32(component,
  753. CSRA66X0_MISC_CONTROL_STATUS_1_FA);
  754. snd_soc_component_write(component, CSRA66X0_MISC_CONTROL_STATUS_1_FA,
  755. val | 0x08);
  756. /* wait 500ms after reset to recover CSRA66X0 */
  757. msleep(500);
  758. return 0;
  759. }
  760. static int csra66x0_msconfig(struct csra66x0_priv *csra66x0)
  761. {
  762. struct snd_soc_component *component = csra66x0->component;
  763. int ret;
  764. dev_dbg(component->dev, "%s: configure %s\n",
  765. __func__, component->name);
  766. /* config */
  767. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  768. SET_CONFIG_STATE);
  769. /* wait until HW is in config state before proceeding */
  770. ret = csra66x0_wait_for_config_state(component);
  771. if (ret) {
  772. dev_err(component->dev, "%s: timeout while %s is waiting for config state\n",
  773. __func__, component->name);
  774. return ret;
  775. }
  776. snd_soc_component_write(component, CSRA66X0_PIO7_SELECT, 0x04);
  777. snd_soc_component_write(component, CSRA66X0_PIO8_SELECT, 0x04);
  778. if (csra66x0->is_master) {
  779. /* Master specific config */
  780. snd_soc_component_write(component,
  781. CSRA66X0_PIO_PULL_EN0, 0xFF);
  782. snd_soc_component_write(component,
  783. CSRA66X0_PIO_PULL_DIR0, 0x80);
  784. snd_soc_component_write(component,
  785. CSRA66X0_PIO_PULL_EN1, 0x01);
  786. snd_soc_component_write(component,
  787. CSRA66X0_PIO_PULL_DIR1, 0x01);
  788. } else {
  789. /* Slave specific config */
  790. snd_soc_component_write(component,
  791. CSRA66X0_PIO_PULL_EN0, 0x7F);
  792. snd_soc_component_write(component,
  793. CSRA66X0_PIO_PULL_EN1, 0x00);
  794. }
  795. snd_soc_component_write(component, CSRA66X0_DCA_CTRL, 0x05);
  796. return 0;
  797. }
  798. static int csra66x0_soc_probe(struct snd_soc_component *component)
  799. {
  800. struct csra66x0_priv *csra66x0 =
  801. snd_soc_component_get_drvdata(component);
  802. struct snd_soc_dapm_context *dapm;
  803. char name[50];
  804. unsigned int i;
  805. csra66x0->component = component;
  806. if (csra66x0->in_cluster) {
  807. dapm = snd_soc_component_get_dapm(component);
  808. dev_dbg(component->dev, "%s: assign prefix %s to component device %s\n",
  809. __func__, component->name_prefix,
  810. component->name);
  811. /* add device to cluster table */
  812. csra66x0->max_num_cluster_devices =
  813. ARRAY_SIZE(csra_clust_dev_tbl);
  814. for (i = 0; i < csra66x0->max_num_cluster_devices; i++) {
  815. if (!strncmp(component->name_prefix,
  816. csra_clust_dev_tbl[i].csra66x0_prefix,
  817. strnlen(
  818. csra_clust_dev_tbl[i].csra66x0_prefix,
  819. sizeof(
  820. csra_clust_dev_tbl[i].csra66x0_prefix)))) {
  821. csra_clust_dev_tbl[i].csra66x0_ptr = csra66x0;
  822. break;
  823. }
  824. if (i == csra66x0->max_num_cluster_devices - 1)
  825. dev_warn(component->dev,
  826. "%s: Unknown prefix %s of cluster device %s\n",
  827. __func__, component->name_prefix,
  828. component->name);
  829. }
  830. /* master slave config */
  831. csra66x0_msconfig(csra66x0);
  832. if (dapm->component) {
  833. strlcpy(name, dapm->component->name_prefix,
  834. sizeof(name));
  835. strlcat(name, " IN", sizeof(name));
  836. snd_soc_dapm_ignore_suspend(dapm, name);
  837. strlcpy(name, dapm->component->name_prefix,
  838. sizeof(name));
  839. strlcat(name, " SPKR", sizeof(name));
  840. snd_soc_dapm_ignore_suspend(dapm, name);
  841. }
  842. }
  843. /* common initialization */
  844. csra66x0->is_probed = 1;
  845. csra66x0_init(csra66x0);
  846. return 0;
  847. }
  848. static void csra66x0_soc_remove(struct snd_soc_component *component)
  849. {
  850. snd_soc_component_write(component, CSRA66X0_CHIP_STATE_CTRL_FA,
  851. SET_STDBY_STATE);
  852. return;
  853. }
  854. static const struct snd_soc_component_driver soc_codec_drv_csra66x0 = {
  855. .name = DRV_NAME,
  856. .probe = csra66x0_soc_probe,
  857. .remove = csra66x0_soc_remove,
  858. .controls = csra66x0_snd_controls,
  859. .num_controls = ARRAY_SIZE(csra66x0_snd_controls),
  860. .dapm_widgets = csra66x0_dapm_widgets,
  861. .num_dapm_widgets = ARRAY_SIZE(csra66x0_dapm_widgets),
  862. .dapm_routes = csra66x0_dapm_routes,
  863. .num_dapm_routes = ARRAY_SIZE(csra66x0_dapm_routes),
  864. };
  865. static struct regmap_config csra66x0_regmap_config = {
  866. .reg_bits = 16,
  867. .val_bits = 8,
  868. .cache_type = REGCACHE_RBTREE,
  869. .reg_defaults = csra66x0_reg_defaults,
  870. .num_reg_defaults = ARRAY_SIZE(csra66x0_reg_defaults),
  871. .max_register = CSRA66X0_MAX_COEFF_ADDR,
  872. .volatile_reg = csra66x0_volatile_register,
  873. .writeable_reg = csra66x0_writeable_registers,
  874. .readable_reg = csra66x0_readable_registers,
  875. };
  876. static irqreturn_t csra66x0_irq(int irq, void *data)
  877. {
  878. struct csra66x0_priv *csra66x0 = (struct csra66x0_priv *) data;
  879. struct snd_soc_component *component = csra66x0->component;
  880. u16 val;
  881. unsigned int i;
  882. /* Treat interrupt before component is initialized as spurious */
  883. if (component == NULL)
  884. return IRQ_NONE;
  885. dev_dbg(component->dev, "%s: csra66x0_interrupt triggered by %s\n",
  886. __func__, component->name);
  887. /* fault indication */
  888. val = snd_soc_component_read32(component, CSRA66X0_IRQ_OUTPUT_STATUS_FA)
  889. & 0x1;
  890. if (!val)
  891. return IRQ_HANDLED;
  892. if (csra66x0->in_cluster) {
  893. /* reset all slave components */
  894. for (i = 0; i < component->card->num_aux_devs; i++) {
  895. if (i >= csra66x0->max_num_cluster_devices)
  896. break;
  897. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  898. continue;
  899. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  900. continue;
  901. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  902. }
  903. /* reset all master components */
  904. for (i = 0; i < component->card->num_aux_devs; i++) {
  905. if (i >= csra66x0->max_num_cluster_devices)
  906. break;
  907. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  908. continue;
  909. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  910. csra66x0_reset(
  911. csra_clust_dev_tbl[i].csra66x0_ptr);
  912. }
  913. /* recover all components */
  914. for (i = 0; i < component->card->num_aux_devs; i++) {
  915. if (i >= csra66x0->max_num_cluster_devices)
  916. break;
  917. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  918. continue;
  919. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  920. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  921. }
  922. } else {
  923. csra66x0_reset(csra66x0);
  924. csra66x0_init(csra66x0);
  925. }
  926. return IRQ_HANDLED;
  927. };
  928. static const struct of_device_id csra66x0_of_match[] = {
  929. { .compatible = "qcom,csra66x0", },
  930. { }
  931. };
  932. MODULE_DEVICE_TABLE(of, csra66x0_of_match);
  933. static ssize_t csra66x0_sysfs_write2reg_addr_value(struct device *dev,
  934. struct device_attribute *attr, const char *buf, size_t count)
  935. {
  936. int ret;
  937. u32 param[2]; /*reg_addr, reg_value */
  938. char lbuf[CSRA66X0_SYSFS_ENTRY_MAX_LEN];
  939. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  940. struct snd_soc_component *component = csra66x0->component;
  941. if (!csra66x0) {
  942. dev_err(component->dev, "%s: invalid input\n", __func__);
  943. return -EINVAL;
  944. }
  945. if (count > sizeof(lbuf) - 1)
  946. return -EINVAL;
  947. ret = strlcpy(lbuf, buf, count);
  948. if (ret != count) {
  949. dev_err(component->dev, "%s: copy input from user space failed. ret=%d\n",
  950. __func__, ret);
  951. ret = -EFAULT;
  952. goto end;
  953. }
  954. lbuf[count] = '\0';
  955. ret = sysfs_get_param(lbuf, param, 2);
  956. if (ret) {
  957. dev_err(component->dev, "%s: get sysfs parameter failed. ret=%d\n",
  958. __func__, ret);
  959. goto end;
  960. }
  961. if (!(csra66x0_addr_is_in_range(param[0],
  962. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  963. || csra66x0_addr_is_in_range(param[0],
  964. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  965. dev_err(component->dev, "%s: register address 0x%04X out of range\n",
  966. __func__, param[0]);
  967. ret = -EINVAL;
  968. goto end;
  969. }
  970. if ((param[1] < 0) || (param[1] > 255)) {
  971. dev_err(component->dev, "%s: register data 0x%02X out of range\n",
  972. __func__, param[1]);
  973. ret = -EINVAL;
  974. goto end;
  975. }
  976. snd_soccomponent_component_write(component, param[0], param[1]);
  977. ret = count;
  978. end:
  979. return ret;
  980. }
  981. static ssize_t csra66x0_sysfs_read2reg_addr_set(struct device *dev,
  982. struct device_attribute *attr, const char *buf, size_t count)
  983. {
  984. int ret;
  985. u32 reg_addr;
  986. char lbuf[CSRA66X0_SYSFS_ENTRY_MAX_LEN];
  987. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  988. if (!csra66x0) {
  989. dev_err(dev, "%s: invalid input\n", __func__);
  990. return -EINVAL;
  991. }
  992. if (count > sizeof(lbuf) - 1)
  993. return -EINVAL;
  994. ret = strlcpy(lbuf, buf, count);
  995. if (ret != count) {
  996. dev_err(dev, "%s: copy input from user space failed. ret=%d\n",
  997. __func__, ret);
  998. ret = -EFAULT;
  999. goto end;
  1000. }
  1001. lbuf[count] = '\0';
  1002. ret = sysfs_get_param(lbuf, &reg_addr, 1);
  1003. if (ret) {
  1004. dev_err(dev, "%s: get sysfs parameter failed. ret=%d\n",
  1005. __func__, ret);
  1006. goto end;
  1007. }
  1008. if (!(csra66x0_addr_is_in_range(reg_addr,
  1009. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  1010. || csra66x0_addr_is_in_range(reg_addr,
  1011. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  1012. dev_err(dev, "%s: register address 0x%04X out of range\n",
  1013. __func__, reg_addr);
  1014. ret = -EINVAL;
  1015. goto end;
  1016. }
  1017. csra66x0->sysfs_reg_addr = reg_addr;
  1018. ret = count;
  1019. end:
  1020. return ret;
  1021. }
  1022. static ssize_t csra66x0_sysfs_read2reg_addr_get(struct device *dev,
  1023. struct device_attribute *attr, char *buf)
  1024. {
  1025. int ret;
  1026. u32 reg_addr;
  1027. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1028. if (!csra66x0) {
  1029. dev_err(dev, "%s: invalid input\n", __func__);
  1030. return -EINVAL;
  1031. }
  1032. reg_addr = csra66x0->sysfs_reg_addr;
  1033. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1034. "0x%04X\n", reg_addr);
  1035. pr_debug("%s: 0x%04X\n", __func__, reg_addr);
  1036. return ret;
  1037. }
  1038. static ssize_t csra66x0_sysfs_read2reg_value(struct device *dev,
  1039. struct device_attribute *attr, char *buf)
  1040. {
  1041. int ret;
  1042. u32 reg_val, reg_addr;
  1043. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1044. struct snd_soc_component *component = csra66x0->component;
  1045. if (!csra66x0) {
  1046. dev_err(dev, "%s: invalid input\n", __func__);
  1047. return -EINVAL;
  1048. }
  1049. reg_addr = csra66x0->sysfs_reg_addr;
  1050. if (!(csra66x0_addr_is_in_range(reg_addr,
  1051. CSRA66X0_BASE, CSRA66X0_MAX_REGISTER_ADDR)
  1052. || csra66x0_addr_is_in_range(reg_addr,
  1053. CSRA66X0_COEFF_BASE, CSRA66X0_MAX_COEFF_ADDR))) {
  1054. pr_debug("%s: 0x%04X: register address out of range\n",
  1055. __func__, reg_addr);
  1056. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1057. "0x%04X: register address out of range\n", reg_addr);
  1058. goto end;
  1059. }
  1060. reg_val = snd_soc_component_read32(component, csra66x0->sysfs_reg_addr);
  1061. ret = snprintf(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN,
  1062. "0x%04X: 0x%02X\n", csra66x0->sysfs_reg_addr, reg_val);
  1063. pr_debug("%s: 0x%04X: 0x%02X\n", __func__,
  1064. csra66x0->sysfs_reg_addr, reg_val);
  1065. end:
  1066. return ret;
  1067. }
  1068. static ssize_t csra66x0_sysfs_reset(struct device *dev,
  1069. struct device_attribute *attr, const char *buf, size_t count)
  1070. {
  1071. int val, rc;
  1072. struct csra66x0_priv *csra66x0 = dev_get_drvdata(dev);
  1073. struct snd_soc_component *component = csra66x0->component;
  1074. unsigned int i;
  1075. if (!csra66x0) {
  1076. dev_err(dev, "%s: invalid input\n", __func__);
  1077. return -EINVAL;
  1078. }
  1079. rc = kstrtoint(buf, 10, &val);
  1080. if (rc) {
  1081. dev_err(dev, "%s: kstrtoint failed. rc=%d\n", __func__, rc);
  1082. goto end;
  1083. }
  1084. if (val != SYSFS_RESET) {
  1085. dev_err(dev, "%s: value out of range.\n", __func__);
  1086. rc = -EINVAL;
  1087. goto end;
  1088. }
  1089. pr_debug("%s: reset device\n", __func__);
  1090. if (csra66x0->in_cluster) {
  1091. /* reset all slave components */
  1092. for (i = 0; i < component->card->num_aux_devs; i++) {
  1093. if (i >= csra66x0->max_num_cluster_devices)
  1094. break;
  1095. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1096. continue;
  1097. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  1098. continue;
  1099. csra66x0_reset(csra_clust_dev_tbl[i].csra66x0_ptr);
  1100. }
  1101. /* reset all master components */
  1102. for (i = 0; i < component->card->num_aux_devs; i++) {
  1103. if (i >= csra66x0->max_num_cluster_devices)
  1104. break;
  1105. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1106. continue;
  1107. if (csra_clust_dev_tbl[i].csra66x0_ptr->is_master)
  1108. csra66x0_reset(
  1109. csra_clust_dev_tbl[i].csra66x0_ptr);
  1110. }
  1111. /* recover all components */
  1112. for (i = 0; i < component->card->num_aux_devs; i++) {
  1113. if (i >= csra66x0->max_num_cluster_devices)
  1114. break;
  1115. if (csra_clust_dev_tbl[i].csra66x0_ptr == NULL)
  1116. continue;
  1117. csra66x0_msconfig(csra_clust_dev_tbl[i].csra66x0_ptr);
  1118. csra66x0_init(csra_clust_dev_tbl[i].csra66x0_ptr);
  1119. }
  1120. } else {
  1121. csra66x0_reset(csra66x0);
  1122. csra66x0_init(csra66x0);
  1123. }
  1124. rc = strnlen(buf, CSRA66X0_SYSFS_ENTRY_MAX_LEN);
  1125. end:
  1126. return rc;
  1127. }
  1128. static DEVICE_ATTR(write2reg_addr_value, 0200, NULL,
  1129. csra66x0_sysfs_write2reg_addr_value);
  1130. static DEVICE_ATTR(read2reg_addr, 0644, csra66x0_sysfs_read2reg_addr_get,
  1131. csra66x0_sysfs_read2reg_addr_set);
  1132. static DEVICE_ATTR(read2reg_value, 0444, csra66x0_sysfs_read2reg_value, NULL);
  1133. static DEVICE_ATTR(reset, 0200, NULL, csra66x0_sysfs_reset);
  1134. static struct attribute *csra66x0_fs_attrs[] = {
  1135. &dev_attr_write2reg_addr_value.attr,
  1136. &dev_attr_read2reg_addr.attr,
  1137. &dev_attr_read2reg_value.attr,
  1138. &dev_attr_reset.attr,
  1139. NULL,
  1140. };
  1141. static struct attribute_group csra66x0_fs_attrs_group = {
  1142. .attrs = csra66x0_fs_attrs,
  1143. };
  1144. static int csra66x0_sysfs_create(struct i2c_client *client,
  1145. struct csra66x0_priv *csra66x0)
  1146. {
  1147. int rc;
  1148. rc = sysfs_create_group(&client->dev.kobj, &csra66x0_fs_attrs_group);
  1149. return rc;
  1150. }
  1151. static void csra66x0_sysfs_remove(struct i2c_client *client,
  1152. struct csra66x0_priv *csra66x0)
  1153. {
  1154. sysfs_remove_group(&client->dev.kobj, &csra66x0_fs_attrs_group);
  1155. }
  1156. #if IS_ENABLED(CONFIG_I2C)
  1157. static int csra66x0_i2c_probe(struct i2c_client *client_i2c,
  1158. const struct i2c_device_id *id)
  1159. {
  1160. struct csra66x0_priv *csra66x0;
  1161. int ret, irq_trigger;
  1162. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1163. char debugfs_dir_name[32];
  1164. #endif
  1165. csra66x0 = devm_kzalloc(&client_i2c->dev, sizeof(struct csra66x0_priv),
  1166. GFP_KERNEL);
  1167. if (csra66x0 == NULL)
  1168. return -ENOMEM;
  1169. csra66x0->regmap = devm_regmap_init_i2c(client_i2c,
  1170. &csra66x0_regmap_config);
  1171. if (IS_ERR(csra66x0->regmap)) {
  1172. ret = PTR_ERR(csra66x0->regmap);
  1173. dev_err(&client_i2c->dev,
  1174. "%s %d: Failed to allocate register map for I2C device: %d\n",
  1175. __func__, __LINE__, ret);
  1176. return ret;
  1177. }
  1178. i2c_set_clientdata(client_i2c, csra66x0);
  1179. /* get data from device tree */
  1180. if (client_i2c->dev.of_node) {
  1181. /* cluster of multiple devices */
  1182. ret = of_property_read_u32(
  1183. client_i2c->dev.of_node, "qcom,csra-cluster",
  1184. &csra66x0->in_cluster);
  1185. if (ret) {
  1186. dev_info(&client_i2c->dev,
  1187. "%s: qcom,csra-cluster property not defined in DT\n", __func__);
  1188. csra66x0->in_cluster = 0;
  1189. }
  1190. /* master or slave device */
  1191. ret = of_property_read_u32(
  1192. client_i2c->dev.of_node, "qcom,csra-cluster-master",
  1193. &csra66x0->is_master);
  1194. if (ret) {
  1195. dev_info(&client_i2c->dev,
  1196. "%s: qcom,csra-cluster-master property not defined in DT, slave assumed\n",
  1197. __func__);
  1198. csra66x0->is_master = 0;
  1199. }
  1200. /* gpio setup for vreg */
  1201. csra66x0->vreg_gpio = of_get_named_gpio(client_i2c->dev.of_node,
  1202. "qcom,csra-vreg-en-gpio", 0);
  1203. if (!gpio_is_valid(csra66x0->vreg_gpio)) {
  1204. dev_err(&client_i2c->dev, "%s: %s property is not found %d\n",
  1205. __func__, "qcom,csra-vreg-en-gpio",
  1206. csra66x0->vreg_gpio);
  1207. return -ENODEV;
  1208. }
  1209. dev_dbg(&client_i2c->dev, "%s: vreg_en gpio %d\n", __func__,
  1210. csra66x0->vreg_gpio);
  1211. ret = gpio_request(csra66x0->vreg_gpio, dev_name(&client_i2c->dev));
  1212. if (ret) {
  1213. if (ret == -EBUSY) {
  1214. /* GPIO was already requested */
  1215. dev_dbg(&client_i2c->dev,
  1216. "%s: gpio %d is already set\n",
  1217. __func__, csra66x0->vreg_gpio);
  1218. } else {
  1219. dev_err(&client_i2c->dev, "%s: Failed to request gpio %d, err: %d\n",
  1220. __func__, csra66x0->vreg_gpio, ret);
  1221. }
  1222. } else {
  1223. gpio_direction_output(csra66x0->vreg_gpio, 1);
  1224. gpio_set_value(csra66x0->vreg_gpio, 0);
  1225. }
  1226. /* register interrupt handle */
  1227. if (client_i2c->irq) {
  1228. csra66x0->irq = client_i2c->irq;
  1229. /* interrupt polarity */
  1230. ret = of_property_read_u32(
  1231. client_i2c->dev.of_node, "irq-active-low",
  1232. &csra66x0->irq_active_low);
  1233. if (ret) {
  1234. dev_info(&client_i2c->dev,
  1235. "%s: irq-active-low property not defined in DT\n", __func__);
  1236. csra66x0->irq_active_low = 0;
  1237. }
  1238. if (csra66x0->irq_active_low)
  1239. irq_trigger = IRQF_TRIGGER_LOW;
  1240. else
  1241. irq_trigger = IRQF_TRIGGER_HIGH;
  1242. ret = devm_request_threaded_irq(&client_i2c->dev,
  1243. csra66x0->irq, NULL, csra66x0_irq,
  1244. irq_trigger | IRQF_ONESHOT,
  1245. "csra66x0_irq", csra66x0);
  1246. if (ret) {
  1247. dev_err(&client_i2c->dev,
  1248. "%s: Failed to request IRQ %d: %d\n",
  1249. __func__, csra66x0->irq, ret);
  1250. csra66x0->irq = 0;
  1251. }
  1252. }
  1253. }
  1254. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1255. /* debugfs interface */
  1256. snprintf(debugfs_dir_name, sizeof(debugfs_dir_name), "%s-%s",
  1257. client_i2c->name, dev_name(&client_i2c->dev));
  1258. csra66x0->debugfs_dir = debugfs_create_dir(debugfs_dir_name, NULL);
  1259. if (!csra66x0->debugfs_dir) {
  1260. dev_dbg(&client_i2c->dev,
  1261. "%s: Failed to create /sys/kernel/debug/%s for debugfs\n",
  1262. __func__, debugfs_dir_name);
  1263. ret = -ENOMEM;
  1264. goto err_debugfs;
  1265. }
  1266. csra66x0->debugfs_file_wo = debugfs_create_file(
  1267. "write_reg_val", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  1268. (void *) csra66x0,
  1269. &debugfs_codec_ops);
  1270. if (!csra66x0->debugfs_file_wo) {
  1271. dev_dbg(&client_i2c->dev,
  1272. "%s: Failed to create /sys/kernel/debug/%s/write_reg_val\n",
  1273. __func__, debugfs_dir_name);
  1274. ret = -ENOMEM;
  1275. goto err_debugfs;
  1276. }
  1277. csra66x0->debugfs_file_ro = debugfs_create_file(
  1278. "show_reg_dump", S_IFREG | S_IRUGO, csra66x0->debugfs_dir,
  1279. (void *) csra66x0,
  1280. &debugfs_codec_ops);
  1281. if (!csra66x0->debugfs_file_ro) {
  1282. dev_dbg(&client_i2c->dev,
  1283. "%s: Failed to create /sys/kernel/debug/%s/show_reg_dump\n",
  1284. __func__, debugfs_dir_name);
  1285. ret = -ENOMEM;
  1286. goto err_debugfs;
  1287. }
  1288. #endif /* CONFIG_DEBUG_FS */
  1289. /* register component */
  1290. ret = snd_soc_register_component(&client_i2c->dev,
  1291. &soc_codec_drv_csra66x0, NULL, 0);
  1292. if (ret != 0) {
  1293. dev_err(&client_i2c->dev, "%s %d: Failed to register component: %d\n",
  1294. __func__, __LINE__, ret);
  1295. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  1296. gpio_set_value(csra66x0->vreg_gpio, 0);
  1297. gpio_free(csra66x0->vreg_gpio);
  1298. }
  1299. return ret;
  1300. }
  1301. ret = csra66x0_sysfs_create(client_i2c, csra66x0);
  1302. if (ret) {
  1303. dev_err(&client_i2c->dev, "%s: sysfs creation failed ret=%d\n",
  1304. __func__, ret);
  1305. goto err_sysfs;
  1306. }
  1307. return 0;
  1308. err_sysfs:
  1309. snd_soc_unregister_component(&client_i2c->dev);
  1310. return ret;
  1311. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1312. err_debugfs:
  1313. debugfs_remove_recursive(csra66x0->debugfs_dir);
  1314. return ret;
  1315. #endif
  1316. }
  1317. static int csra66x0_i2c_remove(struct i2c_client *client_i2c)
  1318. {
  1319. struct csra66x0_priv *csra66x0 = i2c_get_clientdata(client_i2c);
  1320. if (csra66x0) {
  1321. if (gpio_is_valid(csra66x0->vreg_gpio)) {
  1322. gpio_set_value(csra66x0->vreg_gpio, 0);
  1323. gpio_free(csra66x0->vreg_gpio);
  1324. }
  1325. #if IS_ENABLED(CONFIG_DEBUG_FS)
  1326. debugfs_remove_recursive(csra66x0->debugfs_dir);
  1327. #endif
  1328. }
  1329. csra66x0_sysfs_remove(client_i2c, csra66x0);
  1330. snd_soc_unregister_component(&i2c_client->dev);
  1331. return 0;
  1332. }
  1333. static const struct i2c_device_id csra66x0_i2c_id[] = {
  1334. { "csra66x0", 0},
  1335. { }
  1336. };
  1337. MODULE_DEVICE_TABLE(i2c, csra66x0_i2c_id);
  1338. static struct i2c_driver csra66x0_i2c_driver = {
  1339. .probe = csra66x0_i2c_probe,
  1340. .remove = csra66x0_i2c_remove,
  1341. .id_table = csra66x0_i2c_id,
  1342. .driver = {
  1343. .name = "csra66x0",
  1344. .owner = THIS_MODULE,
  1345. .of_match_table = csra66x0_of_match
  1346. },
  1347. };
  1348. #endif
  1349. static int __init csra66x0_codec_init(void)
  1350. {
  1351. int ret = 0;
  1352. #if IS_ENABLED(CONFIG_I2C)
  1353. ret = i2c_add_driver(&csra66x0_i2c_driver);
  1354. if (ret != 0)
  1355. pr_err("%s: Failed to register CSRA66X0 I2C driver, ret = %d\n",
  1356. __func__, ret);
  1357. #endif
  1358. return ret;
  1359. }
  1360. module_init(csra66x0_codec_init);
  1361. static void __exit csra66x0_codec_exit(void)
  1362. {
  1363. #if IS_ENABLED(CONFIG_I2C)
  1364. i2c_del_driver(&csra66x0_i2c_driver);
  1365. #endif
  1366. }
  1367. module_exit(csra66x0_codec_exit);
  1368. MODULE_DESCRIPTION("CSRA66X0 Codec driver");
  1369. MODULE_LICENSE("GPL v2");