cs35l35.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cs35l35.c -- CS35L35 ALSA SoC audio driver
  4. *
  5. * Copyright 2017 Cirrus Logic, Inc.
  6. *
  7. * Author: Brian Austin <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/regmap.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <linux/gpio.h>
  28. #include <sound/initval.h>
  29. #include <sound/tlv.h>
  30. #include <sound/cs35l35.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/completion.h>
  33. #include "cs35l35.h"
  34. #include "cirrus_legacy.h"
  35. /*
  36. * Some fields take zero as a valid value so use a high bit flag that won't
  37. * get written to the device to mark those.
  38. */
  39. #define CS35L35_VALID_PDATA 0x80000000
  40. static const struct reg_default cs35l35_reg[] = {
  41. {CS35L35_PWRCTL1, 0x01},
  42. {CS35L35_PWRCTL2, 0x11},
  43. {CS35L35_PWRCTL3, 0x00},
  44. {CS35L35_CLK_CTL1, 0x04},
  45. {CS35L35_CLK_CTL2, 0x12},
  46. {CS35L35_CLK_CTL3, 0xCF},
  47. {CS35L35_SP_FMT_CTL1, 0x20},
  48. {CS35L35_SP_FMT_CTL2, 0x00},
  49. {CS35L35_SP_FMT_CTL3, 0x02},
  50. {CS35L35_MAG_COMP_CTL, 0x00},
  51. {CS35L35_AMP_INP_DRV_CTL, 0x01},
  52. {CS35L35_AMP_DIG_VOL_CTL, 0x12},
  53. {CS35L35_AMP_DIG_VOL, 0x00},
  54. {CS35L35_ADV_DIG_VOL, 0x00},
  55. {CS35L35_PROTECT_CTL, 0x06},
  56. {CS35L35_AMP_GAIN_AUD_CTL, 0x13},
  57. {CS35L35_AMP_GAIN_PDM_CTL, 0x00},
  58. {CS35L35_AMP_GAIN_ADV_CTL, 0x00},
  59. {CS35L35_GPI_CTL, 0x00},
  60. {CS35L35_BST_CVTR_V_CTL, 0x00},
  61. {CS35L35_BST_PEAK_I, 0x07},
  62. {CS35L35_BST_RAMP_CTL, 0x85},
  63. {CS35L35_BST_CONV_COEF_1, 0x24},
  64. {CS35L35_BST_CONV_COEF_2, 0x24},
  65. {CS35L35_BST_CONV_SLOPE_COMP, 0x4E},
  66. {CS35L35_BST_CONV_SW_FREQ, 0x04},
  67. {CS35L35_CLASS_H_CTL, 0x0B},
  68. {CS35L35_CLASS_H_HEADRM_CTL, 0x0B},
  69. {CS35L35_CLASS_H_RELEASE_RATE, 0x08},
  70. {CS35L35_CLASS_H_FET_DRIVE_CTL, 0x41},
  71. {CS35L35_CLASS_H_VP_CTL, 0xC5},
  72. {CS35L35_VPBR_CTL, 0x0A},
  73. {CS35L35_VPBR_VOL_CTL, 0x90},
  74. {CS35L35_VPBR_TIMING_CTL, 0x6A},
  75. {CS35L35_VPBR_MODE_VOL_CTL, 0x00},
  76. {CS35L35_SPKR_MON_CTL, 0xC0},
  77. {CS35L35_IMON_SCALE_CTL, 0x30},
  78. {CS35L35_AUDIN_RXLOC_CTL, 0x00},
  79. {CS35L35_ADVIN_RXLOC_CTL, 0x80},
  80. {CS35L35_VMON_TXLOC_CTL, 0x00},
  81. {CS35L35_IMON_TXLOC_CTL, 0x80},
  82. {CS35L35_VPMON_TXLOC_CTL, 0x04},
  83. {CS35L35_VBSTMON_TXLOC_CTL, 0x84},
  84. {CS35L35_VPBR_STATUS_TXLOC_CTL, 0x04},
  85. {CS35L35_ZERO_FILL_LOC_CTL, 0x00},
  86. {CS35L35_AUDIN_DEPTH_CTL, 0x0F},
  87. {CS35L35_SPKMON_DEPTH_CTL, 0x0F},
  88. {CS35L35_SUPMON_DEPTH_CTL, 0x0F},
  89. {CS35L35_ZEROFILL_DEPTH_CTL, 0x00},
  90. {CS35L35_MULT_DEV_SYNCH1, 0x02},
  91. {CS35L35_MULT_DEV_SYNCH2, 0x80},
  92. {CS35L35_PROT_RELEASE_CTL, 0x00},
  93. {CS35L35_DIAG_MODE_REG_LOCK, 0x00},
  94. {CS35L35_DIAG_MODE_CTL_1, 0x40},
  95. {CS35L35_DIAG_MODE_CTL_2, 0x00},
  96. {CS35L35_INT_MASK_1, 0xFF},
  97. {CS35L35_INT_MASK_2, 0xFF},
  98. {CS35L35_INT_MASK_3, 0xFF},
  99. {CS35L35_INT_MASK_4, 0xFF},
  100. };
  101. static bool cs35l35_volatile_register(struct device *dev, unsigned int reg)
  102. {
  103. switch (reg) {
  104. case CS35L35_INT_STATUS_1:
  105. case CS35L35_INT_STATUS_2:
  106. case CS35L35_INT_STATUS_3:
  107. case CS35L35_INT_STATUS_4:
  108. case CS35L35_PLL_STATUS:
  109. case CS35L35_OTP_TRIM_STATUS:
  110. return true;
  111. default:
  112. return false;
  113. }
  114. }
  115. static bool cs35l35_readable_register(struct device *dev, unsigned int reg)
  116. {
  117. switch (reg) {
  118. case CS35L35_DEVID_AB ... CS35L35_PWRCTL3:
  119. case CS35L35_CLK_CTL1 ... CS35L35_SP_FMT_CTL3:
  120. case CS35L35_MAG_COMP_CTL ... CS35L35_AMP_GAIN_AUD_CTL:
  121. case CS35L35_AMP_GAIN_PDM_CTL ... CS35L35_BST_PEAK_I:
  122. case CS35L35_BST_RAMP_CTL ... CS35L35_BST_CONV_SW_FREQ:
  123. case CS35L35_CLASS_H_CTL ... CS35L35_CLASS_H_VP_CTL:
  124. case CS35L35_CLASS_H_STATUS:
  125. case CS35L35_VPBR_CTL ... CS35L35_VPBR_MODE_VOL_CTL:
  126. case CS35L35_VPBR_ATTEN_STATUS:
  127. case CS35L35_SPKR_MON_CTL:
  128. case CS35L35_IMON_SCALE_CTL ... CS35L35_ZEROFILL_DEPTH_CTL:
  129. case CS35L35_MULT_DEV_SYNCH1 ... CS35L35_PROT_RELEASE_CTL:
  130. case CS35L35_DIAG_MODE_REG_LOCK ... CS35L35_DIAG_MODE_CTL_2:
  131. case CS35L35_INT_MASK_1 ... CS35L35_PLL_STATUS:
  132. case CS35L35_OTP_TRIM_STATUS:
  133. return true;
  134. default:
  135. return false;
  136. }
  137. }
  138. static bool cs35l35_precious_register(struct device *dev, unsigned int reg)
  139. {
  140. switch (reg) {
  141. case CS35L35_INT_STATUS_1:
  142. case CS35L35_INT_STATUS_2:
  143. case CS35L35_INT_STATUS_3:
  144. case CS35L35_INT_STATUS_4:
  145. case CS35L35_PLL_STATUS:
  146. case CS35L35_OTP_TRIM_STATUS:
  147. return true;
  148. default:
  149. return false;
  150. }
  151. }
  152. static void cs35l35_reset(struct cs35l35_private *cs35l35)
  153. {
  154. gpiod_set_value_cansleep(cs35l35->reset_gpio, 0);
  155. usleep_range(2000, 2100);
  156. gpiod_set_value_cansleep(cs35l35->reset_gpio, 1);
  157. usleep_range(1000, 1100);
  158. }
  159. static int cs35l35_wait_for_pdn(struct cs35l35_private *cs35l35)
  160. {
  161. int ret;
  162. if (cs35l35->pdata.ext_bst) {
  163. usleep_range(5000, 5500);
  164. return 0;
  165. }
  166. reinit_completion(&cs35l35->pdn_done);
  167. ret = wait_for_completion_timeout(&cs35l35->pdn_done,
  168. msecs_to_jiffies(100));
  169. if (ret == 0) {
  170. dev_err(cs35l35->dev, "PDN_DONE did not complete\n");
  171. return -ETIMEDOUT;
  172. }
  173. return 0;
  174. }
  175. static int cs35l35_sdin_event(struct snd_soc_dapm_widget *w,
  176. struct snd_kcontrol *kcontrol, int event)
  177. {
  178. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  179. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  180. int ret = 0;
  181. switch (event) {
  182. case SND_SOC_DAPM_PRE_PMU:
  183. regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
  184. CS35L35_MCLK_DIS_MASK,
  185. 0 << CS35L35_MCLK_DIS_SHIFT);
  186. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
  187. CS35L35_DISCHG_FILT_MASK,
  188. 0 << CS35L35_DISCHG_FILT_SHIFT);
  189. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
  190. CS35L35_PDN_ALL_MASK, 0);
  191. break;
  192. case SND_SOC_DAPM_POST_PMD:
  193. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
  194. CS35L35_DISCHG_FILT_MASK,
  195. 1 << CS35L35_DISCHG_FILT_SHIFT);
  196. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
  197. CS35L35_PDN_ALL_MASK, 1);
  198. /* Already muted, so disable volume ramp for faster shutdown */
  199. regmap_update_bits(cs35l35->regmap, CS35L35_AMP_DIG_VOL_CTL,
  200. CS35L35_AMP_DIGSFT_MASK, 0);
  201. ret = cs35l35_wait_for_pdn(cs35l35);
  202. regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
  203. CS35L35_MCLK_DIS_MASK,
  204. 1 << CS35L35_MCLK_DIS_SHIFT);
  205. regmap_update_bits(cs35l35->regmap, CS35L35_AMP_DIG_VOL_CTL,
  206. CS35L35_AMP_DIGSFT_MASK,
  207. 1 << CS35L35_AMP_DIGSFT_SHIFT);
  208. break;
  209. default:
  210. dev_err(component->dev, "Invalid event = 0x%x\n", event);
  211. ret = -EINVAL;
  212. }
  213. return ret;
  214. }
  215. static int cs35l35_main_amp_event(struct snd_soc_dapm_widget *w,
  216. struct snd_kcontrol *kcontrol, int event)
  217. {
  218. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  219. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  220. unsigned int reg[4];
  221. int i;
  222. switch (event) {
  223. case SND_SOC_DAPM_PRE_PMU:
  224. if (cs35l35->pdata.bst_pdn_fet_on)
  225. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  226. CS35L35_PDN_BST_MASK,
  227. 0 << CS35L35_PDN_BST_FETON_SHIFT);
  228. else
  229. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  230. CS35L35_PDN_BST_MASK,
  231. 0 << CS35L35_PDN_BST_FETOFF_SHIFT);
  232. break;
  233. case SND_SOC_DAPM_POST_PMU:
  234. usleep_range(5000, 5100);
  235. /* If in PDM mode we must use VP for Voltage control */
  236. if (cs35l35->pdm_mode)
  237. regmap_update_bits(cs35l35->regmap,
  238. CS35L35_BST_CVTR_V_CTL,
  239. CS35L35_BST_CTL_MASK,
  240. 0 << CS35L35_BST_CTL_SHIFT);
  241. regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
  242. CS35L35_AMP_MUTE_MASK, 0);
  243. for (i = 0; i < 2; i++)
  244. regmap_bulk_read(cs35l35->regmap, CS35L35_INT_STATUS_1,
  245. &reg, ARRAY_SIZE(reg));
  246. break;
  247. case SND_SOC_DAPM_PRE_PMD:
  248. regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
  249. CS35L35_AMP_MUTE_MASK,
  250. 1 << CS35L35_AMP_MUTE_SHIFT);
  251. if (cs35l35->pdata.bst_pdn_fet_on)
  252. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  253. CS35L35_PDN_BST_MASK,
  254. 1 << CS35L35_PDN_BST_FETON_SHIFT);
  255. else
  256. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  257. CS35L35_PDN_BST_MASK,
  258. 1 << CS35L35_PDN_BST_FETOFF_SHIFT);
  259. break;
  260. case SND_SOC_DAPM_POST_PMD:
  261. usleep_range(5000, 5100);
  262. /*
  263. * If PDM mode we should switch back to pdata value
  264. * for Voltage control when we go down
  265. */
  266. if (cs35l35->pdm_mode)
  267. regmap_update_bits(cs35l35->regmap,
  268. CS35L35_BST_CVTR_V_CTL,
  269. CS35L35_BST_CTL_MASK,
  270. cs35l35->pdata.bst_vctl
  271. << CS35L35_BST_CTL_SHIFT);
  272. break;
  273. default:
  274. dev_err(component->dev, "Invalid event = 0x%x\n", event);
  275. }
  276. return 0;
  277. }
  278. static DECLARE_TLV_DB_SCALE(amp_gain_tlv, 0, 1, 1);
  279. static DECLARE_TLV_DB_SCALE(dig_vol_tlv, -10200, 50, 0);
  280. static const struct snd_kcontrol_new cs35l35_aud_controls[] = {
  281. SOC_SINGLE_SX_TLV("Digital Audio Volume", CS35L35_AMP_DIG_VOL,
  282. 0, 0x34, 0xE4, dig_vol_tlv),
  283. SOC_SINGLE_TLV("Analog Audio Volume", CS35L35_AMP_GAIN_AUD_CTL, 0, 19, 0,
  284. amp_gain_tlv),
  285. SOC_SINGLE_TLV("PDM Volume", CS35L35_AMP_GAIN_PDM_CTL, 0, 19, 0,
  286. amp_gain_tlv),
  287. };
  288. static const struct snd_kcontrol_new cs35l35_adv_controls[] = {
  289. SOC_SINGLE_SX_TLV("Digital Advisory Volume", CS35L35_ADV_DIG_VOL,
  290. 0, 0x34, 0xE4, dig_vol_tlv),
  291. SOC_SINGLE_TLV("Analog Advisory Volume", CS35L35_AMP_GAIN_ADV_CTL, 0, 19, 0,
  292. amp_gain_tlv),
  293. };
  294. static const struct snd_soc_dapm_widget cs35l35_dapm_widgets[] = {
  295. SND_SOC_DAPM_AIF_IN_E("SDIN", NULL, 0, CS35L35_PWRCTL3, 1, 1,
  296. cs35l35_sdin_event, SND_SOC_DAPM_PRE_PMU |
  297. SND_SOC_DAPM_POST_PMD),
  298. SND_SOC_DAPM_AIF_OUT("SDOUT", NULL, 0, CS35L35_PWRCTL3, 2, 1),
  299. SND_SOC_DAPM_OUTPUT("SPK"),
  300. SND_SOC_DAPM_INPUT("VP"),
  301. SND_SOC_DAPM_INPUT("VBST"),
  302. SND_SOC_DAPM_INPUT("ISENSE"),
  303. SND_SOC_DAPM_INPUT("VSENSE"),
  304. SND_SOC_DAPM_ADC("VMON ADC", NULL, CS35L35_PWRCTL2, 7, 1),
  305. SND_SOC_DAPM_ADC("IMON ADC", NULL, CS35L35_PWRCTL2, 6, 1),
  306. SND_SOC_DAPM_ADC("VPMON ADC", NULL, CS35L35_PWRCTL3, 3, 1),
  307. SND_SOC_DAPM_ADC("VBSTMON ADC", NULL, CS35L35_PWRCTL3, 4, 1),
  308. SND_SOC_DAPM_ADC("CLASS H", NULL, CS35L35_PWRCTL2, 5, 1),
  309. SND_SOC_DAPM_OUT_DRV_E("Main AMP", CS35L35_PWRCTL2, 0, 1, NULL, 0,
  310. cs35l35_main_amp_event, SND_SOC_DAPM_PRE_PMU |
  311. SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_POST_PMU |
  312. SND_SOC_DAPM_PRE_PMD),
  313. };
  314. static const struct snd_soc_dapm_route cs35l35_audio_map[] = {
  315. {"VPMON ADC", NULL, "VP"},
  316. {"VBSTMON ADC", NULL, "VBST"},
  317. {"IMON ADC", NULL, "ISENSE"},
  318. {"VMON ADC", NULL, "VSENSE"},
  319. {"SDOUT", NULL, "IMON ADC"},
  320. {"SDOUT", NULL, "VMON ADC"},
  321. {"SDOUT", NULL, "VBSTMON ADC"},
  322. {"SDOUT", NULL, "VPMON ADC"},
  323. {"AMP Capture", NULL, "SDOUT"},
  324. {"SDIN", NULL, "AMP Playback"},
  325. {"CLASS H", NULL, "SDIN"},
  326. {"Main AMP", NULL, "CLASS H"},
  327. {"SPK", NULL, "Main AMP"},
  328. };
  329. static int cs35l35_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  330. {
  331. struct snd_soc_component *component = codec_dai->component;
  332. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  333. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  334. case SND_SOC_DAIFMT_CBP_CFP:
  335. regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
  336. CS35L35_MS_MASK, 1 << CS35L35_MS_SHIFT);
  337. cs35l35->clock_consumer = false;
  338. break;
  339. case SND_SOC_DAIFMT_CBC_CFC:
  340. regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
  341. CS35L35_MS_MASK, 0 << CS35L35_MS_SHIFT);
  342. cs35l35->clock_consumer = true;
  343. break;
  344. default:
  345. return -EINVAL;
  346. }
  347. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  348. case SND_SOC_DAIFMT_I2S:
  349. cs35l35->i2s_mode = true;
  350. cs35l35->pdm_mode = false;
  351. break;
  352. case SND_SOC_DAIFMT_PDM:
  353. cs35l35->pdm_mode = true;
  354. cs35l35->i2s_mode = false;
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. struct cs35l35_sysclk_config {
  362. int sysclk;
  363. int srate;
  364. u8 clk_cfg;
  365. };
  366. static struct cs35l35_sysclk_config cs35l35_clk_ctl[] = {
  367. /* SYSCLK, Sample Rate, Serial Port Cfg */
  368. {5644800, 44100, 0x00},
  369. {5644800, 88200, 0x40},
  370. {6144000, 48000, 0x10},
  371. {6144000, 96000, 0x50},
  372. {11289600, 44100, 0x01},
  373. {11289600, 88200, 0x41},
  374. {11289600, 176400, 0x81},
  375. {12000000, 44100, 0x03},
  376. {12000000, 48000, 0x13},
  377. {12000000, 88200, 0x43},
  378. {12000000, 96000, 0x53},
  379. {12000000, 176400, 0x83},
  380. {12000000, 192000, 0x93},
  381. {12288000, 48000, 0x11},
  382. {12288000, 96000, 0x51},
  383. {12288000, 192000, 0x91},
  384. {13000000, 44100, 0x07},
  385. {13000000, 48000, 0x17},
  386. {13000000, 88200, 0x47},
  387. {13000000, 96000, 0x57},
  388. {13000000, 176400, 0x87},
  389. {13000000, 192000, 0x97},
  390. {22579200, 44100, 0x02},
  391. {22579200, 88200, 0x42},
  392. {22579200, 176400, 0x82},
  393. {24000000, 44100, 0x0B},
  394. {24000000, 48000, 0x1B},
  395. {24000000, 88200, 0x4B},
  396. {24000000, 96000, 0x5B},
  397. {24000000, 176400, 0x8B},
  398. {24000000, 192000, 0x9B},
  399. {24576000, 48000, 0x12},
  400. {24576000, 96000, 0x52},
  401. {24576000, 192000, 0x92},
  402. {26000000, 44100, 0x0F},
  403. {26000000, 48000, 0x1F},
  404. {26000000, 88200, 0x4F},
  405. {26000000, 96000, 0x5F},
  406. {26000000, 176400, 0x8F},
  407. {26000000, 192000, 0x9F},
  408. };
  409. static int cs35l35_get_clk_config(int sysclk, int srate)
  410. {
  411. int i;
  412. for (i = 0; i < ARRAY_SIZE(cs35l35_clk_ctl); i++) {
  413. if (cs35l35_clk_ctl[i].sysclk == sysclk &&
  414. cs35l35_clk_ctl[i].srate == srate)
  415. return cs35l35_clk_ctl[i].clk_cfg;
  416. }
  417. return -EINVAL;
  418. }
  419. static int cs35l35_hw_params(struct snd_pcm_substream *substream,
  420. struct snd_pcm_hw_params *params,
  421. struct snd_soc_dai *dai)
  422. {
  423. struct snd_soc_component *component = dai->component;
  424. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  425. struct classh_cfg *classh = &cs35l35->pdata.classh_algo;
  426. int srate = params_rate(params);
  427. int ret = 0;
  428. u8 sp_sclks;
  429. int audin_format;
  430. int errata_chk;
  431. int clk_ctl = cs35l35_get_clk_config(cs35l35->sysclk, srate);
  432. if (clk_ctl < 0) {
  433. dev_err(component->dev, "Invalid CLK:Rate %d:%d\n",
  434. cs35l35->sysclk, srate);
  435. return -EINVAL;
  436. }
  437. ret = regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL2,
  438. CS35L35_CLK_CTL2_MASK, clk_ctl);
  439. if (ret != 0) {
  440. dev_err(component->dev, "Failed to set port config %d\n", ret);
  441. return ret;
  442. }
  443. /*
  444. * Rev A0 Errata
  445. * When configured for the weak-drive detection path (CH_WKFET_DIS = 0)
  446. * the Class H algorithm does not enable weak-drive operation for
  447. * nonzero values of CH_WKFET_DELAY if SP_RATE = 01 or 10
  448. */
  449. errata_chk = (clk_ctl & CS35L35_SP_RATE_MASK) >> CS35L35_SP_RATE_SHIFT;
  450. if (classh->classh_wk_fet_disable == 0x00 &&
  451. (errata_chk == 0x01 || errata_chk == 0x02)) {
  452. ret = regmap_update_bits(cs35l35->regmap,
  453. CS35L35_CLASS_H_FET_DRIVE_CTL,
  454. CS35L35_CH_WKFET_DEL_MASK,
  455. 0 << CS35L35_CH_WKFET_DEL_SHIFT);
  456. if (ret != 0) {
  457. dev_err(component->dev, "Failed to set fet config %d\n",
  458. ret);
  459. return ret;
  460. }
  461. }
  462. /*
  463. * You can pull more Monitor data from the SDOUT pin than going to SDIN
  464. * Just make sure your SCLK is fast enough to fill the frame
  465. */
  466. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  467. switch (params_width(params)) {
  468. case 8:
  469. audin_format = CS35L35_SDIN_DEPTH_8;
  470. break;
  471. case 16:
  472. audin_format = CS35L35_SDIN_DEPTH_16;
  473. break;
  474. case 24:
  475. audin_format = CS35L35_SDIN_DEPTH_24;
  476. break;
  477. default:
  478. dev_err(component->dev, "Unsupported Width %d\n",
  479. params_width(params));
  480. return -EINVAL;
  481. }
  482. regmap_update_bits(cs35l35->regmap,
  483. CS35L35_AUDIN_DEPTH_CTL,
  484. CS35L35_AUDIN_DEPTH_MASK,
  485. audin_format <<
  486. CS35L35_AUDIN_DEPTH_SHIFT);
  487. if (cs35l35->pdata.stereo) {
  488. regmap_update_bits(cs35l35->regmap,
  489. CS35L35_AUDIN_DEPTH_CTL,
  490. CS35L35_ADVIN_DEPTH_MASK,
  491. audin_format <<
  492. CS35L35_ADVIN_DEPTH_SHIFT);
  493. }
  494. }
  495. if (cs35l35->i2s_mode) {
  496. /* We have to take the SCLK to derive num sclks
  497. * to configure the CLOCK_CTL3 register correctly
  498. */
  499. if ((cs35l35->sclk / srate) % 4) {
  500. dev_err(component->dev, "Unsupported sclk/fs ratio %d:%d\n",
  501. cs35l35->sclk, srate);
  502. return -EINVAL;
  503. }
  504. sp_sclks = ((cs35l35->sclk / srate) / 4) - 1;
  505. /* Only certain ratios supported when device is a clock consumer */
  506. if (cs35l35->clock_consumer) {
  507. switch (sp_sclks) {
  508. case CS35L35_SP_SCLKS_32FS:
  509. case CS35L35_SP_SCLKS_48FS:
  510. case CS35L35_SP_SCLKS_64FS:
  511. break;
  512. default:
  513. dev_err(component->dev, "ratio not supported\n");
  514. return -EINVAL;
  515. }
  516. } else {
  517. /* Only certain ratios supported when device is a clock provider */
  518. switch (sp_sclks) {
  519. case CS35L35_SP_SCLKS_32FS:
  520. case CS35L35_SP_SCLKS_64FS:
  521. break;
  522. default:
  523. dev_err(component->dev, "ratio not supported\n");
  524. return -EINVAL;
  525. }
  526. }
  527. ret = regmap_update_bits(cs35l35->regmap,
  528. CS35L35_CLK_CTL3,
  529. CS35L35_SP_SCLKS_MASK, sp_sclks <<
  530. CS35L35_SP_SCLKS_SHIFT);
  531. if (ret != 0) {
  532. dev_err(component->dev, "Failed to set fsclk %d\n", ret);
  533. return ret;
  534. }
  535. }
  536. return ret;
  537. }
  538. static const unsigned int cs35l35_src_rates[] = {
  539. 44100, 48000, 88200, 96000, 176400, 192000
  540. };
  541. static const struct snd_pcm_hw_constraint_list cs35l35_constraints = {
  542. .count = ARRAY_SIZE(cs35l35_src_rates),
  543. .list = cs35l35_src_rates,
  544. };
  545. static int cs35l35_pcm_startup(struct snd_pcm_substream *substream,
  546. struct snd_soc_dai *dai)
  547. {
  548. struct snd_soc_component *component = dai->component;
  549. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  550. if (!substream->runtime)
  551. return 0;
  552. snd_pcm_hw_constraint_list(substream->runtime, 0,
  553. SNDRV_PCM_HW_PARAM_RATE, &cs35l35_constraints);
  554. regmap_update_bits(cs35l35->regmap, CS35L35_AMP_INP_DRV_CTL,
  555. CS35L35_PDM_MODE_MASK,
  556. 0 << CS35L35_PDM_MODE_SHIFT);
  557. return 0;
  558. }
  559. static const unsigned int cs35l35_pdm_rates[] = {
  560. 44100, 48000, 88200, 96000
  561. };
  562. static const struct snd_pcm_hw_constraint_list cs35l35_pdm_constraints = {
  563. .count = ARRAY_SIZE(cs35l35_pdm_rates),
  564. .list = cs35l35_pdm_rates,
  565. };
  566. static int cs35l35_pdm_startup(struct snd_pcm_substream *substream,
  567. struct snd_soc_dai *dai)
  568. {
  569. struct snd_soc_component *component = dai->component;
  570. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  571. if (!substream->runtime)
  572. return 0;
  573. snd_pcm_hw_constraint_list(substream->runtime, 0,
  574. SNDRV_PCM_HW_PARAM_RATE,
  575. &cs35l35_pdm_constraints);
  576. regmap_update_bits(cs35l35->regmap, CS35L35_AMP_INP_DRV_CTL,
  577. CS35L35_PDM_MODE_MASK,
  578. 1 << CS35L35_PDM_MODE_SHIFT);
  579. return 0;
  580. }
  581. static int cs35l35_dai_set_sysclk(struct snd_soc_dai *dai,
  582. int clk_id, unsigned int freq, int dir)
  583. {
  584. struct snd_soc_component *component = dai->component;
  585. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  586. /* Need the SCLK Frequency regardless of sysclk source for I2S */
  587. cs35l35->sclk = freq;
  588. return 0;
  589. }
  590. static const struct snd_soc_dai_ops cs35l35_ops = {
  591. .startup = cs35l35_pcm_startup,
  592. .set_fmt = cs35l35_set_dai_fmt,
  593. .hw_params = cs35l35_hw_params,
  594. .set_sysclk = cs35l35_dai_set_sysclk,
  595. };
  596. static const struct snd_soc_dai_ops cs35l35_pdm_ops = {
  597. .startup = cs35l35_pdm_startup,
  598. .set_fmt = cs35l35_set_dai_fmt,
  599. .hw_params = cs35l35_hw_params,
  600. };
  601. static struct snd_soc_dai_driver cs35l35_dai[] = {
  602. {
  603. .name = "cs35l35-pcm",
  604. .id = 0,
  605. .playback = {
  606. .stream_name = "AMP Playback",
  607. .channels_min = 1,
  608. .channels_max = 8,
  609. .rates = SNDRV_PCM_RATE_KNOT,
  610. .formats = CS35L35_FORMATS,
  611. },
  612. .capture = {
  613. .stream_name = "AMP Capture",
  614. .channels_min = 1,
  615. .channels_max = 8,
  616. .rates = SNDRV_PCM_RATE_KNOT,
  617. .formats = CS35L35_FORMATS,
  618. },
  619. .ops = &cs35l35_ops,
  620. .symmetric_rate = 1,
  621. },
  622. {
  623. .name = "cs35l35-pdm",
  624. .id = 1,
  625. .playback = {
  626. .stream_name = "PDM Playback",
  627. .channels_min = 1,
  628. .channels_max = 2,
  629. .rates = SNDRV_PCM_RATE_KNOT,
  630. .formats = CS35L35_FORMATS,
  631. },
  632. .ops = &cs35l35_pdm_ops,
  633. },
  634. };
  635. static int cs35l35_component_set_sysclk(struct snd_soc_component *component,
  636. int clk_id, int source, unsigned int freq,
  637. int dir)
  638. {
  639. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  640. int clksrc;
  641. int ret = 0;
  642. switch (clk_id) {
  643. case 0:
  644. clksrc = CS35L35_CLK_SOURCE_MCLK;
  645. break;
  646. case 1:
  647. clksrc = CS35L35_CLK_SOURCE_SCLK;
  648. break;
  649. case 2:
  650. clksrc = CS35L35_CLK_SOURCE_PDM;
  651. break;
  652. default:
  653. dev_err(component->dev, "Invalid CLK Source\n");
  654. return -EINVAL;
  655. }
  656. switch (freq) {
  657. case 5644800:
  658. case 6144000:
  659. case 11289600:
  660. case 12000000:
  661. case 12288000:
  662. case 13000000:
  663. case 22579200:
  664. case 24000000:
  665. case 24576000:
  666. case 26000000:
  667. cs35l35->sysclk = freq;
  668. break;
  669. default:
  670. dev_err(component->dev, "Invalid CLK Frequency Input : %d\n", freq);
  671. return -EINVAL;
  672. }
  673. ret = regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
  674. CS35L35_CLK_SOURCE_MASK,
  675. clksrc << CS35L35_CLK_SOURCE_SHIFT);
  676. if (ret != 0) {
  677. dev_err(component->dev, "Failed to set sysclk %d\n", ret);
  678. return ret;
  679. }
  680. return ret;
  681. }
  682. static int cs35l35_boost_inductor(struct cs35l35_private *cs35l35,
  683. int inductor)
  684. {
  685. struct regmap *regmap = cs35l35->regmap;
  686. unsigned int bst_ipk = 0;
  687. /*
  688. * Digital Boost Converter Configuration for feedback,
  689. * ramping, switching frequency, and estimation block seeding.
  690. */
  691. regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
  692. CS35L35_BST_CONV_SWFREQ_MASK, 0x00);
  693. regmap_read(regmap, CS35L35_BST_PEAK_I, &bst_ipk);
  694. bst_ipk &= CS35L35_BST_IPK_MASK;
  695. switch (inductor) {
  696. case 1000: /* 1 uH */
  697. regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x24);
  698. regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x24);
  699. regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
  700. CS35L35_BST_CONV_LBST_MASK, 0x00);
  701. if (bst_ipk < 0x04)
  702. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
  703. else
  704. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x4E);
  705. break;
  706. case 1200: /* 1.2 uH */
  707. regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x20);
  708. regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x20);
  709. regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
  710. CS35L35_BST_CONV_LBST_MASK, 0x01);
  711. if (bst_ipk < 0x04)
  712. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
  713. else
  714. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x47);
  715. break;
  716. case 1500: /* 1.5uH */
  717. regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x20);
  718. regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x20);
  719. regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
  720. CS35L35_BST_CONV_LBST_MASK, 0x02);
  721. if (bst_ipk < 0x04)
  722. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
  723. else
  724. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x3C);
  725. break;
  726. case 2200: /* 2.2uH */
  727. regmap_write(regmap, CS35L35_BST_CONV_COEF_1, 0x19);
  728. regmap_write(regmap, CS35L35_BST_CONV_COEF_2, 0x25);
  729. regmap_update_bits(regmap, CS35L35_BST_CONV_SW_FREQ,
  730. CS35L35_BST_CONV_LBST_MASK, 0x03);
  731. if (bst_ipk < 0x04)
  732. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x1B);
  733. else
  734. regmap_write(regmap, CS35L35_BST_CONV_SLOPE_COMP, 0x23);
  735. break;
  736. default:
  737. dev_err(cs35l35->dev, "Invalid Inductor Value %d uH\n",
  738. inductor);
  739. return -EINVAL;
  740. }
  741. return 0;
  742. }
  743. static int cs35l35_component_probe(struct snd_soc_component *component)
  744. {
  745. struct cs35l35_private *cs35l35 = snd_soc_component_get_drvdata(component);
  746. struct classh_cfg *classh = &cs35l35->pdata.classh_algo;
  747. struct monitor_cfg *monitor_config = &cs35l35->pdata.mon_cfg;
  748. int ret;
  749. /* Set Platform Data */
  750. if (cs35l35->pdata.bst_vctl)
  751. regmap_update_bits(cs35l35->regmap, CS35L35_BST_CVTR_V_CTL,
  752. CS35L35_BST_CTL_MASK,
  753. cs35l35->pdata.bst_vctl);
  754. if (cs35l35->pdata.bst_ipk)
  755. regmap_update_bits(cs35l35->regmap, CS35L35_BST_PEAK_I,
  756. CS35L35_BST_IPK_MASK,
  757. cs35l35->pdata.bst_ipk <<
  758. CS35L35_BST_IPK_SHIFT);
  759. ret = cs35l35_boost_inductor(cs35l35, cs35l35->pdata.boost_ind);
  760. if (ret)
  761. return ret;
  762. if (cs35l35->pdata.gain_zc)
  763. regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
  764. CS35L35_AMP_GAIN_ZC_MASK,
  765. cs35l35->pdata.gain_zc <<
  766. CS35L35_AMP_GAIN_ZC_SHIFT);
  767. if (cs35l35->pdata.aud_channel)
  768. regmap_update_bits(cs35l35->regmap,
  769. CS35L35_AUDIN_RXLOC_CTL,
  770. CS35L35_AUD_IN_LR_MASK,
  771. cs35l35->pdata.aud_channel <<
  772. CS35L35_AUD_IN_LR_SHIFT);
  773. if (cs35l35->pdata.stereo) {
  774. regmap_update_bits(cs35l35->regmap,
  775. CS35L35_ADVIN_RXLOC_CTL,
  776. CS35L35_ADV_IN_LR_MASK,
  777. cs35l35->pdata.adv_channel <<
  778. CS35L35_ADV_IN_LR_SHIFT);
  779. if (cs35l35->pdata.shared_bst)
  780. regmap_update_bits(cs35l35->regmap, CS35L35_CLASS_H_CTL,
  781. CS35L35_CH_STEREO_MASK,
  782. 1 << CS35L35_CH_STEREO_SHIFT);
  783. ret = snd_soc_add_component_controls(component, cs35l35_adv_controls,
  784. ARRAY_SIZE(cs35l35_adv_controls));
  785. if (ret)
  786. return ret;
  787. }
  788. if (cs35l35->pdata.sp_drv_str)
  789. regmap_update_bits(cs35l35->regmap, CS35L35_CLK_CTL1,
  790. CS35L35_SP_DRV_MASK,
  791. cs35l35->pdata.sp_drv_str <<
  792. CS35L35_SP_DRV_SHIFT);
  793. if (cs35l35->pdata.sp_drv_unused)
  794. regmap_update_bits(cs35l35->regmap, CS35L35_SP_FMT_CTL3,
  795. CS35L35_SP_I2S_DRV_MASK,
  796. cs35l35->pdata.sp_drv_unused <<
  797. CS35L35_SP_I2S_DRV_SHIFT);
  798. if (classh->classh_algo_enable) {
  799. if (classh->classh_bst_override)
  800. regmap_update_bits(cs35l35->regmap,
  801. CS35L35_CLASS_H_CTL,
  802. CS35L35_CH_BST_OVR_MASK,
  803. classh->classh_bst_override <<
  804. CS35L35_CH_BST_OVR_SHIFT);
  805. if (classh->classh_bst_max_limit)
  806. regmap_update_bits(cs35l35->regmap,
  807. CS35L35_CLASS_H_CTL,
  808. CS35L35_CH_BST_LIM_MASK,
  809. classh->classh_bst_max_limit <<
  810. CS35L35_CH_BST_LIM_SHIFT);
  811. if (classh->classh_mem_depth)
  812. regmap_update_bits(cs35l35->regmap,
  813. CS35L35_CLASS_H_CTL,
  814. CS35L35_CH_MEM_DEPTH_MASK,
  815. classh->classh_mem_depth <<
  816. CS35L35_CH_MEM_DEPTH_SHIFT);
  817. if (classh->classh_headroom)
  818. regmap_update_bits(cs35l35->regmap,
  819. CS35L35_CLASS_H_HEADRM_CTL,
  820. CS35L35_CH_HDRM_CTL_MASK,
  821. classh->classh_headroom <<
  822. CS35L35_CH_HDRM_CTL_SHIFT);
  823. if (classh->classh_release_rate)
  824. regmap_update_bits(cs35l35->regmap,
  825. CS35L35_CLASS_H_RELEASE_RATE,
  826. CS35L35_CH_REL_RATE_MASK,
  827. classh->classh_release_rate <<
  828. CS35L35_CH_REL_RATE_SHIFT);
  829. if (classh->classh_wk_fet_disable)
  830. regmap_update_bits(cs35l35->regmap,
  831. CS35L35_CLASS_H_FET_DRIVE_CTL,
  832. CS35L35_CH_WKFET_DIS_MASK,
  833. classh->classh_wk_fet_disable <<
  834. CS35L35_CH_WKFET_DIS_SHIFT);
  835. if (classh->classh_wk_fet_delay)
  836. regmap_update_bits(cs35l35->regmap,
  837. CS35L35_CLASS_H_FET_DRIVE_CTL,
  838. CS35L35_CH_WKFET_DEL_MASK,
  839. classh->classh_wk_fet_delay <<
  840. CS35L35_CH_WKFET_DEL_SHIFT);
  841. if (classh->classh_wk_fet_thld)
  842. regmap_update_bits(cs35l35->regmap,
  843. CS35L35_CLASS_H_FET_DRIVE_CTL,
  844. CS35L35_CH_WKFET_THLD_MASK,
  845. classh->classh_wk_fet_thld <<
  846. CS35L35_CH_WKFET_THLD_SHIFT);
  847. if (classh->classh_vpch_auto)
  848. regmap_update_bits(cs35l35->regmap,
  849. CS35L35_CLASS_H_VP_CTL,
  850. CS35L35_CH_VP_AUTO_MASK,
  851. classh->classh_vpch_auto <<
  852. CS35L35_CH_VP_AUTO_SHIFT);
  853. if (classh->classh_vpch_rate)
  854. regmap_update_bits(cs35l35->regmap,
  855. CS35L35_CLASS_H_VP_CTL,
  856. CS35L35_CH_VP_RATE_MASK,
  857. classh->classh_vpch_rate <<
  858. CS35L35_CH_VP_RATE_SHIFT);
  859. if (classh->classh_vpch_man)
  860. regmap_update_bits(cs35l35->regmap,
  861. CS35L35_CLASS_H_VP_CTL,
  862. CS35L35_CH_VP_MAN_MASK,
  863. classh->classh_vpch_man <<
  864. CS35L35_CH_VP_MAN_SHIFT);
  865. }
  866. if (monitor_config->is_present) {
  867. if (monitor_config->vmon_specs) {
  868. regmap_update_bits(cs35l35->regmap,
  869. CS35L35_SPKMON_DEPTH_CTL,
  870. CS35L35_VMON_DEPTH_MASK,
  871. monitor_config->vmon_dpth <<
  872. CS35L35_VMON_DEPTH_SHIFT);
  873. regmap_update_bits(cs35l35->regmap,
  874. CS35L35_VMON_TXLOC_CTL,
  875. CS35L35_MON_TXLOC_MASK,
  876. monitor_config->vmon_loc <<
  877. CS35L35_MON_TXLOC_SHIFT);
  878. regmap_update_bits(cs35l35->regmap,
  879. CS35L35_VMON_TXLOC_CTL,
  880. CS35L35_MON_FRM_MASK,
  881. monitor_config->vmon_frm <<
  882. CS35L35_MON_FRM_SHIFT);
  883. }
  884. if (monitor_config->imon_specs) {
  885. regmap_update_bits(cs35l35->regmap,
  886. CS35L35_SPKMON_DEPTH_CTL,
  887. CS35L35_IMON_DEPTH_MASK,
  888. monitor_config->imon_dpth <<
  889. CS35L35_IMON_DEPTH_SHIFT);
  890. regmap_update_bits(cs35l35->regmap,
  891. CS35L35_IMON_TXLOC_CTL,
  892. CS35L35_MON_TXLOC_MASK,
  893. monitor_config->imon_loc <<
  894. CS35L35_MON_TXLOC_SHIFT);
  895. regmap_update_bits(cs35l35->regmap,
  896. CS35L35_IMON_TXLOC_CTL,
  897. CS35L35_MON_FRM_MASK,
  898. monitor_config->imon_frm <<
  899. CS35L35_MON_FRM_SHIFT);
  900. regmap_update_bits(cs35l35->regmap,
  901. CS35L35_IMON_SCALE_CTL,
  902. CS35L35_IMON_SCALE_MASK,
  903. monitor_config->imon_scale <<
  904. CS35L35_IMON_SCALE_SHIFT);
  905. }
  906. if (monitor_config->vpmon_specs) {
  907. regmap_update_bits(cs35l35->regmap,
  908. CS35L35_SUPMON_DEPTH_CTL,
  909. CS35L35_VPMON_DEPTH_MASK,
  910. monitor_config->vpmon_dpth <<
  911. CS35L35_VPMON_DEPTH_SHIFT);
  912. regmap_update_bits(cs35l35->regmap,
  913. CS35L35_VPMON_TXLOC_CTL,
  914. CS35L35_MON_TXLOC_MASK,
  915. monitor_config->vpmon_loc <<
  916. CS35L35_MON_TXLOC_SHIFT);
  917. regmap_update_bits(cs35l35->regmap,
  918. CS35L35_VPMON_TXLOC_CTL,
  919. CS35L35_MON_FRM_MASK,
  920. monitor_config->vpmon_frm <<
  921. CS35L35_MON_FRM_SHIFT);
  922. }
  923. if (monitor_config->vbstmon_specs) {
  924. regmap_update_bits(cs35l35->regmap,
  925. CS35L35_SUPMON_DEPTH_CTL,
  926. CS35L35_VBSTMON_DEPTH_MASK,
  927. monitor_config->vpmon_dpth <<
  928. CS35L35_VBSTMON_DEPTH_SHIFT);
  929. regmap_update_bits(cs35l35->regmap,
  930. CS35L35_VBSTMON_TXLOC_CTL,
  931. CS35L35_MON_TXLOC_MASK,
  932. monitor_config->vbstmon_loc <<
  933. CS35L35_MON_TXLOC_SHIFT);
  934. regmap_update_bits(cs35l35->regmap,
  935. CS35L35_VBSTMON_TXLOC_CTL,
  936. CS35L35_MON_FRM_MASK,
  937. monitor_config->vbstmon_frm <<
  938. CS35L35_MON_FRM_SHIFT);
  939. }
  940. if (monitor_config->vpbrstat_specs) {
  941. regmap_update_bits(cs35l35->regmap,
  942. CS35L35_SUPMON_DEPTH_CTL,
  943. CS35L35_VPBRSTAT_DEPTH_MASK,
  944. monitor_config->vpbrstat_dpth <<
  945. CS35L35_VPBRSTAT_DEPTH_SHIFT);
  946. regmap_update_bits(cs35l35->regmap,
  947. CS35L35_VPBR_STATUS_TXLOC_CTL,
  948. CS35L35_MON_TXLOC_MASK,
  949. monitor_config->vpbrstat_loc <<
  950. CS35L35_MON_TXLOC_SHIFT);
  951. regmap_update_bits(cs35l35->regmap,
  952. CS35L35_VPBR_STATUS_TXLOC_CTL,
  953. CS35L35_MON_FRM_MASK,
  954. monitor_config->vpbrstat_frm <<
  955. CS35L35_MON_FRM_SHIFT);
  956. }
  957. if (monitor_config->zerofill_specs) {
  958. regmap_update_bits(cs35l35->regmap,
  959. CS35L35_SUPMON_DEPTH_CTL,
  960. CS35L35_ZEROFILL_DEPTH_MASK,
  961. monitor_config->zerofill_dpth <<
  962. CS35L35_ZEROFILL_DEPTH_SHIFT);
  963. regmap_update_bits(cs35l35->regmap,
  964. CS35L35_ZERO_FILL_LOC_CTL,
  965. CS35L35_MON_TXLOC_MASK,
  966. monitor_config->zerofill_loc <<
  967. CS35L35_MON_TXLOC_SHIFT);
  968. regmap_update_bits(cs35l35->regmap,
  969. CS35L35_ZERO_FILL_LOC_CTL,
  970. CS35L35_MON_FRM_MASK,
  971. monitor_config->zerofill_frm <<
  972. CS35L35_MON_FRM_SHIFT);
  973. }
  974. }
  975. return 0;
  976. }
  977. static const struct snd_soc_component_driver soc_component_dev_cs35l35 = {
  978. .probe = cs35l35_component_probe,
  979. .set_sysclk = cs35l35_component_set_sysclk,
  980. .dapm_widgets = cs35l35_dapm_widgets,
  981. .num_dapm_widgets = ARRAY_SIZE(cs35l35_dapm_widgets),
  982. .dapm_routes = cs35l35_audio_map,
  983. .num_dapm_routes = ARRAY_SIZE(cs35l35_audio_map),
  984. .controls = cs35l35_aud_controls,
  985. .num_controls = ARRAY_SIZE(cs35l35_aud_controls),
  986. .idle_bias_on = 1,
  987. .use_pmdown_time = 1,
  988. .endianness = 1,
  989. };
  990. static struct regmap_config cs35l35_regmap = {
  991. .reg_bits = 8,
  992. .val_bits = 8,
  993. .max_register = CS35L35_MAX_REGISTER,
  994. .reg_defaults = cs35l35_reg,
  995. .num_reg_defaults = ARRAY_SIZE(cs35l35_reg),
  996. .volatile_reg = cs35l35_volatile_register,
  997. .readable_reg = cs35l35_readable_register,
  998. .precious_reg = cs35l35_precious_register,
  999. .cache_type = REGCACHE_RBTREE,
  1000. .use_single_read = true,
  1001. .use_single_write = true,
  1002. };
  1003. static irqreturn_t cs35l35_irq(int irq, void *data)
  1004. {
  1005. struct cs35l35_private *cs35l35 = data;
  1006. unsigned int sticky1, sticky2, sticky3, sticky4;
  1007. unsigned int mask1, mask2, mask3, mask4, current1;
  1008. /* ack the irq by reading all status registers */
  1009. regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_4, &sticky4);
  1010. regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_3, &sticky3);
  1011. regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_2, &sticky2);
  1012. regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_1, &sticky1);
  1013. regmap_read(cs35l35->regmap, CS35L35_INT_MASK_4, &mask4);
  1014. regmap_read(cs35l35->regmap, CS35L35_INT_MASK_3, &mask3);
  1015. regmap_read(cs35l35->regmap, CS35L35_INT_MASK_2, &mask2);
  1016. regmap_read(cs35l35->regmap, CS35L35_INT_MASK_1, &mask1);
  1017. /* Check to see if unmasked bits are active */
  1018. if (!(sticky1 & ~mask1) && !(sticky2 & ~mask2) && !(sticky3 & ~mask3)
  1019. && !(sticky4 & ~mask4))
  1020. return IRQ_NONE;
  1021. if (sticky2 & CS35L35_PDN_DONE)
  1022. complete(&cs35l35->pdn_done);
  1023. /* read the current values */
  1024. regmap_read(cs35l35->regmap, CS35L35_INT_STATUS_1, &current1);
  1025. /* handle the interrupts */
  1026. if (sticky1 & CS35L35_CAL_ERR) {
  1027. dev_crit(cs35l35->dev, "Calibration Error\n");
  1028. /* error is no longer asserted; safe to reset */
  1029. if (!(current1 & CS35L35_CAL_ERR)) {
  1030. pr_debug("%s : Cal error release\n", __func__);
  1031. regmap_update_bits(cs35l35->regmap,
  1032. CS35L35_PROT_RELEASE_CTL,
  1033. CS35L35_CAL_ERR_RLS, 0);
  1034. regmap_update_bits(cs35l35->regmap,
  1035. CS35L35_PROT_RELEASE_CTL,
  1036. CS35L35_CAL_ERR_RLS,
  1037. CS35L35_CAL_ERR_RLS);
  1038. regmap_update_bits(cs35l35->regmap,
  1039. CS35L35_PROT_RELEASE_CTL,
  1040. CS35L35_CAL_ERR_RLS, 0);
  1041. }
  1042. }
  1043. if (sticky1 & CS35L35_AMP_SHORT) {
  1044. dev_crit(cs35l35->dev, "AMP Short Error\n");
  1045. /* error is no longer asserted; safe to reset */
  1046. if (!(current1 & CS35L35_AMP_SHORT)) {
  1047. dev_dbg(cs35l35->dev, "Amp short error release\n");
  1048. regmap_update_bits(cs35l35->regmap,
  1049. CS35L35_PROT_RELEASE_CTL,
  1050. CS35L35_SHORT_RLS, 0);
  1051. regmap_update_bits(cs35l35->regmap,
  1052. CS35L35_PROT_RELEASE_CTL,
  1053. CS35L35_SHORT_RLS,
  1054. CS35L35_SHORT_RLS);
  1055. regmap_update_bits(cs35l35->regmap,
  1056. CS35L35_PROT_RELEASE_CTL,
  1057. CS35L35_SHORT_RLS, 0);
  1058. }
  1059. }
  1060. if (sticky1 & CS35L35_OTW) {
  1061. dev_warn(cs35l35->dev, "Over temperature warning\n");
  1062. /* error is no longer asserted; safe to reset */
  1063. if (!(current1 & CS35L35_OTW)) {
  1064. dev_dbg(cs35l35->dev, "Over temperature warn release\n");
  1065. regmap_update_bits(cs35l35->regmap,
  1066. CS35L35_PROT_RELEASE_CTL,
  1067. CS35L35_OTW_RLS, 0);
  1068. regmap_update_bits(cs35l35->regmap,
  1069. CS35L35_PROT_RELEASE_CTL,
  1070. CS35L35_OTW_RLS,
  1071. CS35L35_OTW_RLS);
  1072. regmap_update_bits(cs35l35->regmap,
  1073. CS35L35_PROT_RELEASE_CTL,
  1074. CS35L35_OTW_RLS, 0);
  1075. }
  1076. }
  1077. if (sticky1 & CS35L35_OTE) {
  1078. dev_crit(cs35l35->dev, "Over temperature error\n");
  1079. /* error is no longer asserted; safe to reset */
  1080. if (!(current1 & CS35L35_OTE)) {
  1081. dev_dbg(cs35l35->dev, "Over temperature error release\n");
  1082. regmap_update_bits(cs35l35->regmap,
  1083. CS35L35_PROT_RELEASE_CTL,
  1084. CS35L35_OTE_RLS, 0);
  1085. regmap_update_bits(cs35l35->regmap,
  1086. CS35L35_PROT_RELEASE_CTL,
  1087. CS35L35_OTE_RLS,
  1088. CS35L35_OTE_RLS);
  1089. regmap_update_bits(cs35l35->regmap,
  1090. CS35L35_PROT_RELEASE_CTL,
  1091. CS35L35_OTE_RLS, 0);
  1092. }
  1093. }
  1094. if (sticky3 & CS35L35_BST_HIGH) {
  1095. dev_crit(cs35l35->dev, "VBST error: powering off!\n");
  1096. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  1097. CS35L35_PDN_AMP, CS35L35_PDN_AMP);
  1098. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
  1099. CS35L35_PDN_ALL, CS35L35_PDN_ALL);
  1100. }
  1101. if (sticky3 & CS35L35_LBST_SHORT) {
  1102. dev_crit(cs35l35->dev, "LBST error: powering off!\n");
  1103. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  1104. CS35L35_PDN_AMP, CS35L35_PDN_AMP);
  1105. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL1,
  1106. CS35L35_PDN_ALL, CS35L35_PDN_ALL);
  1107. }
  1108. if (sticky2 & CS35L35_VPBR_ERR)
  1109. dev_dbg(cs35l35->dev, "Error: Reactive Brownout\n");
  1110. if (sticky4 & CS35L35_VMON_OVFL)
  1111. dev_dbg(cs35l35->dev, "Error: VMON overflow\n");
  1112. if (sticky4 & CS35L35_IMON_OVFL)
  1113. dev_dbg(cs35l35->dev, "Error: IMON overflow\n");
  1114. return IRQ_HANDLED;
  1115. }
  1116. static int cs35l35_handle_of_data(struct i2c_client *i2c_client,
  1117. struct cs35l35_platform_data *pdata)
  1118. {
  1119. struct device_node *np = i2c_client->dev.of_node;
  1120. struct device_node *classh, *signal_format;
  1121. struct classh_cfg *classh_config = &pdata->classh_algo;
  1122. struct monitor_cfg *monitor_config = &pdata->mon_cfg;
  1123. unsigned int val32 = 0;
  1124. u8 monitor_array[4];
  1125. const int imon_array_size = ARRAY_SIZE(monitor_array);
  1126. const int mon_array_size = imon_array_size - 1;
  1127. int ret = 0;
  1128. if (!np)
  1129. return 0;
  1130. pdata->bst_pdn_fet_on = of_property_read_bool(np,
  1131. "cirrus,boost-pdn-fet-on");
  1132. ret = of_property_read_u32(np, "cirrus,boost-ctl-millivolt", &val32);
  1133. if (ret >= 0) {
  1134. if (val32 < 2600 || val32 > 9000) {
  1135. dev_err(&i2c_client->dev,
  1136. "Invalid Boost Voltage %d mV\n", val32);
  1137. return -EINVAL;
  1138. }
  1139. pdata->bst_vctl = ((val32 - 2600) / 100) + 1;
  1140. }
  1141. ret = of_property_read_u32(np, "cirrus,boost-peak-milliamp", &val32);
  1142. if (ret >= 0) {
  1143. if (val32 < 1680 || val32 > 4480) {
  1144. dev_err(&i2c_client->dev,
  1145. "Invalid Boost Peak Current %u mA\n", val32);
  1146. return -EINVAL;
  1147. }
  1148. pdata->bst_ipk = ((val32 - 1680) / 110) | CS35L35_VALID_PDATA;
  1149. }
  1150. ret = of_property_read_u32(np, "cirrus,boost-ind-nanohenry", &val32);
  1151. if (ret >= 0) {
  1152. pdata->boost_ind = val32;
  1153. } else {
  1154. dev_err(&i2c_client->dev, "Inductor not specified.\n");
  1155. return -EINVAL;
  1156. }
  1157. if (of_property_read_u32(np, "cirrus,sp-drv-strength", &val32) >= 0)
  1158. pdata->sp_drv_str = val32;
  1159. if (of_property_read_u32(np, "cirrus,sp-drv-unused", &val32) >= 0)
  1160. pdata->sp_drv_unused = val32 | CS35L35_VALID_PDATA;
  1161. pdata->stereo = of_property_read_bool(np, "cirrus,stereo-config");
  1162. if (pdata->stereo) {
  1163. ret = of_property_read_u32(np, "cirrus,audio-channel", &val32);
  1164. if (ret >= 0)
  1165. pdata->aud_channel = val32;
  1166. ret = of_property_read_u32(np, "cirrus,advisory-channel",
  1167. &val32);
  1168. if (ret >= 0)
  1169. pdata->adv_channel = val32;
  1170. pdata->shared_bst = of_property_read_bool(np,
  1171. "cirrus,shared-boost");
  1172. }
  1173. pdata->ext_bst = of_property_read_bool(np, "cirrus,external-boost");
  1174. pdata->gain_zc = of_property_read_bool(np, "cirrus,amp-gain-zc");
  1175. classh = of_get_child_by_name(np, "cirrus,classh-internal-algo");
  1176. classh_config->classh_algo_enable = (classh != NULL);
  1177. if (classh_config->classh_algo_enable) {
  1178. classh_config->classh_bst_override =
  1179. of_property_read_bool(np, "cirrus,classh-bst-overide");
  1180. ret = of_property_read_u32(classh,
  1181. "cirrus,classh-bst-max-limit",
  1182. &val32);
  1183. if (ret >= 0) {
  1184. val32 |= CS35L35_VALID_PDATA;
  1185. classh_config->classh_bst_max_limit = val32;
  1186. }
  1187. ret = of_property_read_u32(classh,
  1188. "cirrus,classh-bst-max-limit",
  1189. &val32);
  1190. if (ret >= 0) {
  1191. val32 |= CS35L35_VALID_PDATA;
  1192. classh_config->classh_bst_max_limit = val32;
  1193. }
  1194. ret = of_property_read_u32(classh, "cirrus,classh-mem-depth",
  1195. &val32);
  1196. if (ret >= 0) {
  1197. val32 |= CS35L35_VALID_PDATA;
  1198. classh_config->classh_mem_depth = val32;
  1199. }
  1200. ret = of_property_read_u32(classh, "cirrus,classh-release-rate",
  1201. &val32);
  1202. if (ret >= 0)
  1203. classh_config->classh_release_rate = val32;
  1204. ret = of_property_read_u32(classh, "cirrus,classh-headroom",
  1205. &val32);
  1206. if (ret >= 0) {
  1207. val32 |= CS35L35_VALID_PDATA;
  1208. classh_config->classh_headroom = val32;
  1209. }
  1210. ret = of_property_read_u32(classh,
  1211. "cirrus,classh-wk-fet-disable",
  1212. &val32);
  1213. if (ret >= 0)
  1214. classh_config->classh_wk_fet_disable = val32;
  1215. ret = of_property_read_u32(classh, "cirrus,classh-wk-fet-delay",
  1216. &val32);
  1217. if (ret >= 0) {
  1218. val32 |= CS35L35_VALID_PDATA;
  1219. classh_config->classh_wk_fet_delay = val32;
  1220. }
  1221. ret = of_property_read_u32(classh, "cirrus,classh-wk-fet-thld",
  1222. &val32);
  1223. if (ret >= 0)
  1224. classh_config->classh_wk_fet_thld = val32;
  1225. ret = of_property_read_u32(classh, "cirrus,classh-vpch-auto",
  1226. &val32);
  1227. if (ret >= 0) {
  1228. val32 |= CS35L35_VALID_PDATA;
  1229. classh_config->classh_vpch_auto = val32;
  1230. }
  1231. ret = of_property_read_u32(classh, "cirrus,classh-vpch-rate",
  1232. &val32);
  1233. if (ret >= 0) {
  1234. val32 |= CS35L35_VALID_PDATA;
  1235. classh_config->classh_vpch_rate = val32;
  1236. }
  1237. ret = of_property_read_u32(classh, "cirrus,classh-vpch-man",
  1238. &val32);
  1239. if (ret >= 0)
  1240. classh_config->classh_vpch_man = val32;
  1241. }
  1242. of_node_put(classh);
  1243. /* frame depth location */
  1244. signal_format = of_get_child_by_name(np, "cirrus,monitor-signal-format");
  1245. monitor_config->is_present = signal_format ? true : false;
  1246. if (monitor_config->is_present) {
  1247. ret = of_property_read_u8_array(signal_format, "cirrus,imon",
  1248. monitor_array, imon_array_size);
  1249. if (!ret) {
  1250. monitor_config->imon_specs = true;
  1251. monitor_config->imon_dpth = monitor_array[0];
  1252. monitor_config->imon_loc = monitor_array[1];
  1253. monitor_config->imon_frm = monitor_array[2];
  1254. monitor_config->imon_scale = monitor_array[3];
  1255. }
  1256. ret = of_property_read_u8_array(signal_format, "cirrus,vmon",
  1257. monitor_array, mon_array_size);
  1258. if (!ret) {
  1259. monitor_config->vmon_specs = true;
  1260. monitor_config->vmon_dpth = monitor_array[0];
  1261. monitor_config->vmon_loc = monitor_array[1];
  1262. monitor_config->vmon_frm = monitor_array[2];
  1263. }
  1264. ret = of_property_read_u8_array(signal_format, "cirrus,vpmon",
  1265. monitor_array, mon_array_size);
  1266. if (!ret) {
  1267. monitor_config->vpmon_specs = true;
  1268. monitor_config->vpmon_dpth = monitor_array[0];
  1269. monitor_config->vpmon_loc = monitor_array[1];
  1270. monitor_config->vpmon_frm = monitor_array[2];
  1271. }
  1272. ret = of_property_read_u8_array(signal_format, "cirrus,vbstmon",
  1273. monitor_array, mon_array_size);
  1274. if (!ret) {
  1275. monitor_config->vbstmon_specs = true;
  1276. monitor_config->vbstmon_dpth = monitor_array[0];
  1277. monitor_config->vbstmon_loc = monitor_array[1];
  1278. monitor_config->vbstmon_frm = monitor_array[2];
  1279. }
  1280. ret = of_property_read_u8_array(signal_format, "cirrus,vpbrstat",
  1281. monitor_array, mon_array_size);
  1282. if (!ret) {
  1283. monitor_config->vpbrstat_specs = true;
  1284. monitor_config->vpbrstat_dpth = monitor_array[0];
  1285. monitor_config->vpbrstat_loc = monitor_array[1];
  1286. monitor_config->vpbrstat_frm = monitor_array[2];
  1287. }
  1288. ret = of_property_read_u8_array(signal_format, "cirrus,zerofill",
  1289. monitor_array, mon_array_size);
  1290. if (!ret) {
  1291. monitor_config->zerofill_specs = true;
  1292. monitor_config->zerofill_dpth = monitor_array[0];
  1293. monitor_config->zerofill_loc = monitor_array[1];
  1294. monitor_config->zerofill_frm = monitor_array[2];
  1295. }
  1296. }
  1297. of_node_put(signal_format);
  1298. return 0;
  1299. }
  1300. /* Errata Rev A0 */
  1301. static const struct reg_sequence cs35l35_errata_patch[] = {
  1302. { 0x7F, 0x99 },
  1303. { 0x00, 0x99 },
  1304. { 0x52, 0x22 },
  1305. { 0x04, 0x14 },
  1306. { 0x6D, 0x44 },
  1307. { 0x24, 0x10 },
  1308. { 0x58, 0xC4 },
  1309. { 0x00, 0x98 },
  1310. { 0x18, 0x08 },
  1311. { 0x00, 0x00 },
  1312. { 0x7F, 0x00 },
  1313. };
  1314. static int cs35l35_i2c_probe(struct i2c_client *i2c_client)
  1315. {
  1316. struct cs35l35_private *cs35l35;
  1317. struct device *dev = &i2c_client->dev;
  1318. struct cs35l35_platform_data *pdata = dev_get_platdata(dev);
  1319. int i, devid;
  1320. int ret;
  1321. unsigned int reg;
  1322. cs35l35 = devm_kzalloc(dev, sizeof(struct cs35l35_private), GFP_KERNEL);
  1323. if (!cs35l35)
  1324. return -ENOMEM;
  1325. cs35l35->dev = dev;
  1326. i2c_set_clientdata(i2c_client, cs35l35);
  1327. cs35l35->regmap = devm_regmap_init_i2c(i2c_client, &cs35l35_regmap);
  1328. if (IS_ERR(cs35l35->regmap)) {
  1329. ret = PTR_ERR(cs35l35->regmap);
  1330. dev_err(dev, "regmap_init() failed: %d\n", ret);
  1331. return ret;
  1332. }
  1333. for (i = 0; i < ARRAY_SIZE(cs35l35_supplies); i++)
  1334. cs35l35->supplies[i].supply = cs35l35_supplies[i];
  1335. cs35l35->num_supplies = ARRAY_SIZE(cs35l35_supplies);
  1336. ret = devm_regulator_bulk_get(dev, cs35l35->num_supplies,
  1337. cs35l35->supplies);
  1338. if (ret != 0) {
  1339. dev_err(dev, "Failed to request core supplies: %d\n", ret);
  1340. return ret;
  1341. }
  1342. if (pdata) {
  1343. cs35l35->pdata = *pdata;
  1344. } else {
  1345. pdata = devm_kzalloc(dev, sizeof(struct cs35l35_platform_data),
  1346. GFP_KERNEL);
  1347. if (!pdata)
  1348. return -ENOMEM;
  1349. if (i2c_client->dev.of_node) {
  1350. ret = cs35l35_handle_of_data(i2c_client, pdata);
  1351. if (ret != 0)
  1352. return ret;
  1353. }
  1354. cs35l35->pdata = *pdata;
  1355. }
  1356. ret = regulator_bulk_enable(cs35l35->num_supplies,
  1357. cs35l35->supplies);
  1358. if (ret != 0) {
  1359. dev_err(dev, "Failed to enable core supplies: %d\n", ret);
  1360. return ret;
  1361. }
  1362. /* returning NULL can be valid if in stereo mode */
  1363. cs35l35->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1364. GPIOD_OUT_LOW);
  1365. if (IS_ERR(cs35l35->reset_gpio)) {
  1366. ret = PTR_ERR(cs35l35->reset_gpio);
  1367. cs35l35->reset_gpio = NULL;
  1368. if (ret == -EBUSY) {
  1369. dev_info(dev,
  1370. "Reset line busy, assuming shared reset\n");
  1371. } else {
  1372. dev_err(dev, "Failed to get reset GPIO: %d\n", ret);
  1373. goto err;
  1374. }
  1375. }
  1376. cs35l35_reset(cs35l35);
  1377. init_completion(&cs35l35->pdn_done);
  1378. ret = devm_request_threaded_irq(dev, i2c_client->irq, NULL, cs35l35_irq,
  1379. IRQF_ONESHOT | IRQF_TRIGGER_LOW |
  1380. IRQF_SHARED, "cs35l35", cs35l35);
  1381. if (ret != 0) {
  1382. dev_err(dev, "Failed to request IRQ: %d\n", ret);
  1383. goto err;
  1384. }
  1385. /* initialize codec */
  1386. devid = cirrus_read_device_id(cs35l35->regmap, CS35L35_DEVID_AB);
  1387. if (devid < 0) {
  1388. ret = devid;
  1389. dev_err(dev, "Failed to read device ID: %d\n", ret);
  1390. goto err;
  1391. }
  1392. if (devid != CS35L35_CHIP_ID) {
  1393. dev_err(dev, "CS35L35 Device ID (%X). Expected ID %X\n",
  1394. devid, CS35L35_CHIP_ID);
  1395. ret = -ENODEV;
  1396. goto err;
  1397. }
  1398. ret = regmap_read(cs35l35->regmap, CS35L35_REV_ID, &reg);
  1399. if (ret < 0) {
  1400. dev_err(dev, "Get Revision ID failed: %d\n", ret);
  1401. goto err;
  1402. }
  1403. ret = regmap_register_patch(cs35l35->regmap, cs35l35_errata_patch,
  1404. ARRAY_SIZE(cs35l35_errata_patch));
  1405. if (ret < 0) {
  1406. dev_err(dev, "Failed to apply errata patch: %d\n", ret);
  1407. goto err;
  1408. }
  1409. dev_info(dev, "Cirrus Logic CS35L35 (%x), Revision: %02X\n",
  1410. devid, reg & 0xFF);
  1411. /* Set the INT Masks for critical errors */
  1412. regmap_write(cs35l35->regmap, CS35L35_INT_MASK_1,
  1413. CS35L35_INT1_CRIT_MASK);
  1414. regmap_write(cs35l35->regmap, CS35L35_INT_MASK_2,
  1415. CS35L35_INT2_CRIT_MASK);
  1416. regmap_write(cs35l35->regmap, CS35L35_INT_MASK_3,
  1417. CS35L35_INT3_CRIT_MASK);
  1418. regmap_write(cs35l35->regmap, CS35L35_INT_MASK_4,
  1419. CS35L35_INT4_CRIT_MASK);
  1420. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  1421. CS35L35_PWR2_PDN_MASK,
  1422. CS35L35_PWR2_PDN_MASK);
  1423. if (cs35l35->pdata.bst_pdn_fet_on)
  1424. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  1425. CS35L35_PDN_BST_MASK,
  1426. 1 << CS35L35_PDN_BST_FETON_SHIFT);
  1427. else
  1428. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL2,
  1429. CS35L35_PDN_BST_MASK,
  1430. 1 << CS35L35_PDN_BST_FETOFF_SHIFT);
  1431. regmap_update_bits(cs35l35->regmap, CS35L35_PWRCTL3,
  1432. CS35L35_PWR3_PDN_MASK,
  1433. CS35L35_PWR3_PDN_MASK);
  1434. regmap_update_bits(cs35l35->regmap, CS35L35_PROTECT_CTL,
  1435. CS35L35_AMP_MUTE_MASK, 1 << CS35L35_AMP_MUTE_SHIFT);
  1436. ret = devm_snd_soc_register_component(dev, &soc_component_dev_cs35l35,
  1437. cs35l35_dai, ARRAY_SIZE(cs35l35_dai));
  1438. if (ret < 0) {
  1439. dev_err(dev, "Failed to register component: %d\n", ret);
  1440. goto err;
  1441. }
  1442. return 0;
  1443. err:
  1444. regulator_bulk_disable(cs35l35->num_supplies,
  1445. cs35l35->supplies);
  1446. gpiod_set_value_cansleep(cs35l35->reset_gpio, 0);
  1447. return ret;
  1448. }
  1449. static void cs35l35_i2c_remove(struct i2c_client *i2c_client)
  1450. {
  1451. struct cs35l35_private *cs35l35 = i2c_get_clientdata(i2c_client);
  1452. regulator_bulk_disable(cs35l35->num_supplies, cs35l35->supplies);
  1453. gpiod_set_value_cansleep(cs35l35->reset_gpio, 0);
  1454. }
  1455. static const struct of_device_id cs35l35_of_match[] = {
  1456. {.compatible = "cirrus,cs35l35"},
  1457. {},
  1458. };
  1459. MODULE_DEVICE_TABLE(of, cs35l35_of_match);
  1460. static const struct i2c_device_id cs35l35_id[] = {
  1461. {"cs35l35", 0},
  1462. {}
  1463. };
  1464. MODULE_DEVICE_TABLE(i2c, cs35l35_id);
  1465. static struct i2c_driver cs35l35_i2c_driver = {
  1466. .driver = {
  1467. .name = "cs35l35",
  1468. .of_match_table = cs35l35_of_match,
  1469. },
  1470. .id_table = cs35l35_id,
  1471. .probe_new = cs35l35_i2c_probe,
  1472. .remove = cs35l35_i2c_remove,
  1473. };
  1474. module_i2c_driver(cs35l35_i2c_driver);
  1475. MODULE_DESCRIPTION("ASoC CS35L35 driver");
  1476. MODULE_AUTHOR("Brian Austin, Cirrus Logic Inc, <[email protected]>");
  1477. MODULE_LICENSE("GPL");