csra66x0.c 45 KB

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