cs35l33.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cs35l33.c -- CS35L33 ALSA SoC audio driver
  4. *
  5. * Copyright 2016 Cirrus Logic, Inc.
  6. *
  7. * Author: Paul Handrigan <[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/workqueue.h>
  17. #include <linux/platform_device.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/initval.h>
  24. #include <sound/tlv.h>
  25. #include <linux/gpio.h>
  26. #include <linux/gpio/consumer.h>
  27. #include <sound/cs35l33.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/regulator/machine.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_irq.h>
  35. #include "cs35l33.h"
  36. #include "cirrus_legacy.h"
  37. #define CS35L33_BOOT_DELAY 50
  38. struct cs35l33_private {
  39. struct snd_soc_component *component;
  40. struct cs35l33_pdata pdata;
  41. struct regmap *regmap;
  42. struct gpio_desc *reset_gpio;
  43. bool amp_cal;
  44. int mclk_int;
  45. struct regulator_bulk_data core_supplies[2];
  46. int num_core_supplies;
  47. bool is_tdm_mode;
  48. bool enable_soft_ramp;
  49. };
  50. static const struct reg_default cs35l33_reg[] = {
  51. {CS35L33_PWRCTL1, 0x85},
  52. {CS35L33_PWRCTL2, 0xFE},
  53. {CS35L33_CLK_CTL, 0x0C},
  54. {CS35L33_BST_PEAK_CTL, 0x90},
  55. {CS35L33_PROTECT_CTL, 0x55},
  56. {CS35L33_BST_CTL1, 0x00},
  57. {CS35L33_BST_CTL2, 0x01},
  58. {CS35L33_ADSP_CTL, 0x00},
  59. {CS35L33_ADC_CTL, 0xC8},
  60. {CS35L33_DAC_CTL, 0x14},
  61. {CS35L33_DIG_VOL_CTL, 0x00},
  62. {CS35L33_CLASSD_CTL, 0x04},
  63. {CS35L33_AMP_CTL, 0x90},
  64. {CS35L33_INT_MASK_1, 0xFF},
  65. {CS35L33_INT_MASK_2, 0xFF},
  66. {CS35L33_DIAG_LOCK, 0x00},
  67. {CS35L33_DIAG_CTRL_1, 0x40},
  68. {CS35L33_DIAG_CTRL_2, 0x00},
  69. {CS35L33_HG_MEMLDO_CTL, 0x62},
  70. {CS35L33_HG_REL_RATE, 0x03},
  71. {CS35L33_LDO_DEL, 0x12},
  72. {CS35L33_HG_HEAD, 0x0A},
  73. {CS35L33_HG_EN, 0x05},
  74. {CS35L33_TX_VMON, 0x00},
  75. {CS35L33_TX_IMON, 0x03},
  76. {CS35L33_TX_VPMON, 0x02},
  77. {CS35L33_TX_VBSTMON, 0x05},
  78. {CS35L33_TX_FLAG, 0x06},
  79. {CS35L33_TX_EN1, 0x00},
  80. {CS35L33_TX_EN2, 0x00},
  81. {CS35L33_TX_EN3, 0x00},
  82. {CS35L33_TX_EN4, 0x00},
  83. {CS35L33_RX_AUD, 0x40},
  84. {CS35L33_RX_SPLY, 0x03},
  85. {CS35L33_RX_ALIVE, 0x04},
  86. {CS35L33_BST_CTL4, 0x63},
  87. };
  88. static const struct reg_sequence cs35l33_patch[] = {
  89. { 0x00, 0x99, 0 },
  90. { 0x59, 0x02, 0 },
  91. { 0x52, 0x30, 0 },
  92. { 0x39, 0x45, 0 },
  93. { 0x57, 0x30, 0 },
  94. { 0x2C, 0x68, 0 },
  95. { 0x00, 0x00, 0 },
  96. };
  97. static bool cs35l33_volatile_register(struct device *dev, unsigned int reg)
  98. {
  99. switch (reg) {
  100. case CS35L33_DEVID_AB:
  101. case CS35L33_DEVID_CD:
  102. case CS35L33_DEVID_E:
  103. case CS35L33_REV_ID:
  104. case CS35L33_INT_STATUS_1:
  105. case CS35L33_INT_STATUS_2:
  106. case CS35L33_HG_STATUS:
  107. return true;
  108. default:
  109. return false;
  110. }
  111. }
  112. static bool cs35l33_writeable_register(struct device *dev, unsigned int reg)
  113. {
  114. switch (reg) {
  115. /* these are read only registers */
  116. case CS35L33_DEVID_AB:
  117. case CS35L33_DEVID_CD:
  118. case CS35L33_DEVID_E:
  119. case CS35L33_REV_ID:
  120. case CS35L33_INT_STATUS_1:
  121. case CS35L33_INT_STATUS_2:
  122. case CS35L33_HG_STATUS:
  123. return false;
  124. default:
  125. return true;
  126. }
  127. }
  128. static bool cs35l33_readable_register(struct device *dev, unsigned int reg)
  129. {
  130. switch (reg) {
  131. case CS35L33_DEVID_AB:
  132. case CS35L33_DEVID_CD:
  133. case CS35L33_DEVID_E:
  134. case CS35L33_REV_ID:
  135. case CS35L33_PWRCTL1:
  136. case CS35L33_PWRCTL2:
  137. case CS35L33_CLK_CTL:
  138. case CS35L33_BST_PEAK_CTL:
  139. case CS35L33_PROTECT_CTL:
  140. case CS35L33_BST_CTL1:
  141. case CS35L33_BST_CTL2:
  142. case CS35L33_ADSP_CTL:
  143. case CS35L33_ADC_CTL:
  144. case CS35L33_DAC_CTL:
  145. case CS35L33_DIG_VOL_CTL:
  146. case CS35L33_CLASSD_CTL:
  147. case CS35L33_AMP_CTL:
  148. case CS35L33_INT_MASK_1:
  149. case CS35L33_INT_MASK_2:
  150. case CS35L33_INT_STATUS_1:
  151. case CS35L33_INT_STATUS_2:
  152. case CS35L33_DIAG_LOCK:
  153. case CS35L33_DIAG_CTRL_1:
  154. case CS35L33_DIAG_CTRL_2:
  155. case CS35L33_HG_MEMLDO_CTL:
  156. case CS35L33_HG_REL_RATE:
  157. case CS35L33_LDO_DEL:
  158. case CS35L33_HG_HEAD:
  159. case CS35L33_HG_EN:
  160. case CS35L33_TX_VMON:
  161. case CS35L33_TX_IMON:
  162. case CS35L33_TX_VPMON:
  163. case CS35L33_TX_VBSTMON:
  164. case CS35L33_TX_FLAG:
  165. case CS35L33_TX_EN1:
  166. case CS35L33_TX_EN2:
  167. case CS35L33_TX_EN3:
  168. case CS35L33_TX_EN4:
  169. case CS35L33_RX_AUD:
  170. case CS35L33_RX_SPLY:
  171. case CS35L33_RX_ALIVE:
  172. case CS35L33_BST_CTL4:
  173. return true;
  174. default:
  175. return false;
  176. }
  177. }
  178. static DECLARE_TLV_DB_SCALE(classd_ctl_tlv, 900, 100, 0);
  179. static DECLARE_TLV_DB_SCALE(dac_tlv, -10200, 50, 0);
  180. static const struct snd_kcontrol_new cs35l33_snd_controls[] = {
  181. SOC_SINGLE_TLV("SPK Amp Volume", CS35L33_AMP_CTL,
  182. 4, 0x09, 0, classd_ctl_tlv),
  183. SOC_SINGLE_SX_TLV("DAC Volume", CS35L33_DIG_VOL_CTL,
  184. 0, 0x34, 0xE4, dac_tlv),
  185. };
  186. static int cs35l33_spkrdrv_event(struct snd_soc_dapm_widget *w,
  187. struct snd_kcontrol *kcontrol, int event)
  188. {
  189. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  190. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  191. switch (event) {
  192. case SND_SOC_DAPM_POST_PMU:
  193. if (!priv->amp_cal) {
  194. usleep_range(8000, 9000);
  195. priv->amp_cal = true;
  196. regmap_update_bits(priv->regmap, CS35L33_CLASSD_CTL,
  197. CS35L33_AMP_CAL, 0);
  198. dev_dbg(component->dev, "Amp calibration done\n");
  199. }
  200. dev_dbg(component->dev, "Amp turned on\n");
  201. break;
  202. case SND_SOC_DAPM_POST_PMD:
  203. dev_dbg(component->dev, "Amp turned off\n");
  204. break;
  205. default:
  206. dev_err(component->dev, "Invalid event = 0x%x\n", event);
  207. break;
  208. }
  209. return 0;
  210. }
  211. static int cs35l33_sdin_event(struct snd_soc_dapm_widget *w,
  212. struct snd_kcontrol *kcontrol, int event)
  213. {
  214. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  215. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  216. unsigned int val;
  217. switch (event) {
  218. case SND_SOC_DAPM_PRE_PMU:
  219. regmap_update_bits(priv->regmap, CS35L33_PWRCTL1,
  220. CS35L33_PDN_BST, 0);
  221. val = priv->is_tdm_mode ? 0 : CS35L33_PDN_TDM;
  222. regmap_update_bits(priv->regmap, CS35L33_PWRCTL2,
  223. CS35L33_PDN_TDM, val);
  224. dev_dbg(component->dev, "BST turned on\n");
  225. break;
  226. case SND_SOC_DAPM_POST_PMU:
  227. dev_dbg(component->dev, "SDIN turned on\n");
  228. if (!priv->amp_cal) {
  229. regmap_update_bits(priv->regmap, CS35L33_CLASSD_CTL,
  230. CS35L33_AMP_CAL, CS35L33_AMP_CAL);
  231. dev_dbg(component->dev, "Amp calibration started\n");
  232. usleep_range(10000, 11000);
  233. }
  234. break;
  235. case SND_SOC_DAPM_POST_PMD:
  236. regmap_update_bits(priv->regmap, CS35L33_PWRCTL2,
  237. CS35L33_PDN_TDM, CS35L33_PDN_TDM);
  238. usleep_range(4000, 4100);
  239. regmap_update_bits(priv->regmap, CS35L33_PWRCTL1,
  240. CS35L33_PDN_BST, CS35L33_PDN_BST);
  241. dev_dbg(component->dev, "BST and SDIN turned off\n");
  242. break;
  243. default:
  244. dev_err(component->dev, "Invalid event = 0x%x\n", event);
  245. }
  246. return 0;
  247. }
  248. static int cs35l33_sdout_event(struct snd_soc_dapm_widget *w,
  249. struct snd_kcontrol *kcontrol, int event)
  250. {
  251. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  252. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  253. unsigned int mask = CS35L33_SDOUT_3ST_I2S | CS35L33_PDN_TDM;
  254. unsigned int mask2 = CS35L33_SDOUT_3ST_TDM;
  255. unsigned int val, val2;
  256. switch (event) {
  257. case SND_SOC_DAPM_PRE_PMU:
  258. if (priv->is_tdm_mode) {
  259. /* set sdout_3st_i2s and reset pdn_tdm */
  260. val = CS35L33_SDOUT_3ST_I2S;
  261. /* reset sdout_3st_tdm */
  262. val2 = 0;
  263. } else {
  264. /* reset sdout_3st_i2s and set pdn_tdm */
  265. val = CS35L33_PDN_TDM;
  266. /* set sdout_3st_tdm */
  267. val2 = CS35L33_SDOUT_3ST_TDM;
  268. }
  269. dev_dbg(component->dev, "SDOUT turned on\n");
  270. break;
  271. case SND_SOC_DAPM_PRE_PMD:
  272. val = CS35L33_SDOUT_3ST_I2S | CS35L33_PDN_TDM;
  273. val2 = CS35L33_SDOUT_3ST_TDM;
  274. dev_dbg(component->dev, "SDOUT turned off\n");
  275. break;
  276. default:
  277. dev_err(component->dev, "Invalid event = 0x%x\n", event);
  278. return 0;
  279. }
  280. regmap_update_bits(priv->regmap, CS35L33_PWRCTL2,
  281. mask, val);
  282. regmap_update_bits(priv->regmap, CS35L33_CLK_CTL,
  283. mask2, val2);
  284. return 0;
  285. }
  286. static const struct snd_soc_dapm_widget cs35l33_dapm_widgets[] = {
  287. SND_SOC_DAPM_OUTPUT("SPK"),
  288. SND_SOC_DAPM_OUT_DRV_E("SPKDRV", CS35L33_PWRCTL1, 7, 1, NULL, 0,
  289. cs35l33_spkrdrv_event,
  290. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  291. SND_SOC_DAPM_AIF_IN_E("SDIN", NULL, 0, CS35L33_PWRCTL2,
  292. 2, 1, cs35l33_sdin_event, SND_SOC_DAPM_PRE_PMU |
  293. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  294. SND_SOC_DAPM_INPUT("MON"),
  295. SND_SOC_DAPM_ADC("VMON", NULL,
  296. CS35L33_PWRCTL2, CS35L33_PDN_VMON_SHIFT, 1),
  297. SND_SOC_DAPM_ADC("IMON", NULL,
  298. CS35L33_PWRCTL2, CS35L33_PDN_IMON_SHIFT, 1),
  299. SND_SOC_DAPM_ADC("VPMON", NULL,
  300. CS35L33_PWRCTL2, CS35L33_PDN_VPMON_SHIFT, 1),
  301. SND_SOC_DAPM_ADC("VBSTMON", NULL,
  302. CS35L33_PWRCTL2, CS35L33_PDN_VBSTMON_SHIFT, 1),
  303. SND_SOC_DAPM_AIF_OUT_E("SDOUT", NULL, 0, SND_SOC_NOPM, 0, 0,
  304. cs35l33_sdout_event, SND_SOC_DAPM_PRE_PMU |
  305. SND_SOC_DAPM_PRE_PMD),
  306. };
  307. static const struct snd_soc_dapm_route cs35l33_audio_map[] = {
  308. {"SDIN", NULL, "CS35L33 Playback"},
  309. {"SPKDRV", NULL, "SDIN"},
  310. {"SPK", NULL, "SPKDRV"},
  311. {"VMON", NULL, "MON"},
  312. {"IMON", NULL, "MON"},
  313. {"SDOUT", NULL, "VMON"},
  314. {"SDOUT", NULL, "IMON"},
  315. {"CS35L33 Capture", NULL, "SDOUT"},
  316. };
  317. static const struct snd_soc_dapm_route cs35l33_vphg_auto_route[] = {
  318. {"SPKDRV", NULL, "VPMON"},
  319. {"VPMON", NULL, "CS35L33 Playback"},
  320. };
  321. static const struct snd_soc_dapm_route cs35l33_vp_vbst_mon_route[] = {
  322. {"SDOUT", NULL, "VPMON"},
  323. {"VPMON", NULL, "MON"},
  324. {"SDOUT", NULL, "VBSTMON"},
  325. {"VBSTMON", NULL, "MON"},
  326. };
  327. static int cs35l33_set_bias_level(struct snd_soc_component *component,
  328. enum snd_soc_bias_level level)
  329. {
  330. unsigned int val;
  331. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  332. switch (level) {
  333. case SND_SOC_BIAS_ON:
  334. break;
  335. case SND_SOC_BIAS_PREPARE:
  336. regmap_update_bits(priv->regmap, CS35L33_PWRCTL1,
  337. CS35L33_PDN_ALL, 0);
  338. regmap_update_bits(priv->regmap, CS35L33_CLK_CTL,
  339. CS35L33_MCLKDIS, 0);
  340. break;
  341. case SND_SOC_BIAS_STANDBY:
  342. regmap_update_bits(priv->regmap, CS35L33_PWRCTL1,
  343. CS35L33_PDN_ALL, CS35L33_PDN_ALL);
  344. regmap_read(priv->regmap, CS35L33_INT_STATUS_2, &val);
  345. usleep_range(1000, 1100);
  346. if (val & CS35L33_PDN_DONE)
  347. regmap_update_bits(priv->regmap, CS35L33_CLK_CTL,
  348. CS35L33_MCLKDIS, CS35L33_MCLKDIS);
  349. break;
  350. case SND_SOC_BIAS_OFF:
  351. break;
  352. default:
  353. return -EINVAL;
  354. }
  355. return 0;
  356. }
  357. struct cs35l33_mclk_div {
  358. int mclk;
  359. int srate;
  360. u8 adsp_rate;
  361. u8 int_fs_ratio;
  362. };
  363. static const struct cs35l33_mclk_div cs35l33_mclk_coeffs[] = {
  364. /* MCLK, Sample Rate, adsp_rate, int_fs_ratio */
  365. {5644800, 11025, 0x4, CS35L33_INT_FS_RATE},
  366. {5644800, 22050, 0x8, CS35L33_INT_FS_RATE},
  367. {5644800, 44100, 0xC, CS35L33_INT_FS_RATE},
  368. {6000000, 8000, 0x1, 0},
  369. {6000000, 11025, 0x2, 0},
  370. {6000000, 11029, 0x3, 0},
  371. {6000000, 12000, 0x4, 0},
  372. {6000000, 16000, 0x5, 0},
  373. {6000000, 22050, 0x6, 0},
  374. {6000000, 22059, 0x7, 0},
  375. {6000000, 24000, 0x8, 0},
  376. {6000000, 32000, 0x9, 0},
  377. {6000000, 44100, 0xA, 0},
  378. {6000000, 44118, 0xB, 0},
  379. {6000000, 48000, 0xC, 0},
  380. {6144000, 8000, 0x1, CS35L33_INT_FS_RATE},
  381. {6144000, 12000, 0x4, CS35L33_INT_FS_RATE},
  382. {6144000, 16000, 0x5, CS35L33_INT_FS_RATE},
  383. {6144000, 24000, 0x8, CS35L33_INT_FS_RATE},
  384. {6144000, 32000, 0x9, CS35L33_INT_FS_RATE},
  385. {6144000, 48000, 0xC, CS35L33_INT_FS_RATE},
  386. };
  387. static int cs35l33_get_mclk_coeff(int mclk, int srate)
  388. {
  389. int i;
  390. for (i = 0; i < ARRAY_SIZE(cs35l33_mclk_coeffs); i++) {
  391. if (cs35l33_mclk_coeffs[i].mclk == mclk &&
  392. cs35l33_mclk_coeffs[i].srate == srate)
  393. return i;
  394. }
  395. return -EINVAL;
  396. }
  397. static int cs35l33_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  398. {
  399. struct snd_soc_component *component = codec_dai->component;
  400. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  401. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  402. case SND_SOC_DAIFMT_CBM_CFM:
  403. regmap_update_bits(priv->regmap, CS35L33_ADSP_CTL,
  404. CS35L33_MS_MASK, CS35L33_MS_MASK);
  405. dev_dbg(component->dev, "Audio port in master mode\n");
  406. break;
  407. case SND_SOC_DAIFMT_CBS_CFS:
  408. regmap_update_bits(priv->regmap, CS35L33_ADSP_CTL,
  409. CS35L33_MS_MASK, 0);
  410. dev_dbg(component->dev, "Audio port in slave mode\n");
  411. break;
  412. default:
  413. return -EINVAL;
  414. }
  415. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  416. case SND_SOC_DAIFMT_DSP_A:
  417. /*
  418. * tdm mode in cs35l33 resembles dsp-a mode very
  419. * closely, it is dsp-a with fsync shifted left by half bclk
  420. */
  421. priv->is_tdm_mode = true;
  422. dev_dbg(component->dev, "Audio port in TDM mode\n");
  423. break;
  424. case SND_SOC_DAIFMT_I2S:
  425. priv->is_tdm_mode = false;
  426. dev_dbg(component->dev, "Audio port in I2S mode\n");
  427. break;
  428. default:
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. static int cs35l33_pcm_hw_params(struct snd_pcm_substream *substream,
  434. struct snd_pcm_hw_params *params,
  435. struct snd_soc_dai *dai)
  436. {
  437. struct snd_soc_component *component = dai->component;
  438. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  439. int sample_size = params_width(params);
  440. int coeff = cs35l33_get_mclk_coeff(priv->mclk_int, params_rate(params));
  441. if (coeff < 0)
  442. return coeff;
  443. regmap_update_bits(priv->regmap, CS35L33_CLK_CTL,
  444. CS35L33_ADSP_FS | CS35L33_INT_FS_RATE,
  445. cs35l33_mclk_coeffs[coeff].int_fs_ratio
  446. | cs35l33_mclk_coeffs[coeff].adsp_rate);
  447. if (priv->is_tdm_mode) {
  448. sample_size = (sample_size / 8) - 1;
  449. if (sample_size > 2)
  450. sample_size = 2;
  451. regmap_update_bits(priv->regmap, CS35L33_RX_AUD,
  452. CS35L33_AUDIN_RX_DEPTH,
  453. sample_size << CS35L33_AUDIN_RX_DEPTH_SHIFT);
  454. }
  455. dev_dbg(component->dev, "sample rate=%d, bits per sample=%d\n",
  456. params_rate(params), params_width(params));
  457. return 0;
  458. }
  459. static const unsigned int cs35l33_src_rates[] = {
  460. 8000, 11025, 11029, 12000, 16000, 22050,
  461. 22059, 24000, 32000, 44100, 44118, 48000
  462. };
  463. static const struct snd_pcm_hw_constraint_list cs35l33_constraints = {
  464. .count = ARRAY_SIZE(cs35l33_src_rates),
  465. .list = cs35l33_src_rates,
  466. };
  467. static int cs35l33_pcm_startup(struct snd_pcm_substream *substream,
  468. struct snd_soc_dai *dai)
  469. {
  470. snd_pcm_hw_constraint_list(substream->runtime, 0,
  471. SNDRV_PCM_HW_PARAM_RATE,
  472. &cs35l33_constraints);
  473. return 0;
  474. }
  475. static int cs35l33_set_tristate(struct snd_soc_dai *dai, int tristate)
  476. {
  477. struct snd_soc_component *component = dai->component;
  478. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  479. if (tristate) {
  480. regmap_update_bits(priv->regmap, CS35L33_PWRCTL2,
  481. CS35L33_SDOUT_3ST_I2S, CS35L33_SDOUT_3ST_I2S);
  482. regmap_update_bits(priv->regmap, CS35L33_CLK_CTL,
  483. CS35L33_SDOUT_3ST_TDM, CS35L33_SDOUT_3ST_TDM);
  484. } else {
  485. regmap_update_bits(priv->regmap, CS35L33_PWRCTL2,
  486. CS35L33_SDOUT_3ST_I2S, 0);
  487. regmap_update_bits(priv->regmap, CS35L33_CLK_CTL,
  488. CS35L33_SDOUT_3ST_TDM, 0);
  489. }
  490. return 0;
  491. }
  492. static int cs35l33_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  493. unsigned int rx_mask, int slots, int slot_width)
  494. {
  495. struct snd_soc_component *component = dai->component;
  496. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  497. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  498. unsigned int reg, bit_pos, i;
  499. int slot, slot_num;
  500. if (slot_width != 8)
  501. return -EINVAL;
  502. /* scan rx_mask for aud slot */
  503. slot = ffs(rx_mask) - 1;
  504. if (slot >= 0) {
  505. regmap_update_bits(priv->regmap, CS35L33_RX_AUD,
  506. CS35L33_X_LOC, slot);
  507. dev_dbg(component->dev, "Audio starts from slots %d", slot);
  508. }
  509. /*
  510. * scan tx_mask: vmon(2 slots); imon (2 slots);
  511. * vpmon (1 slot) vbstmon (1 slot)
  512. */
  513. slot = ffs(tx_mask) - 1;
  514. slot_num = 0;
  515. for (i = 0; i < 2 ; i++) {
  516. /* disable vpmon/vbstmon: enable later if set in tx_mask */
  517. regmap_update_bits(priv->regmap, CS35L33_TX_VPMON + i,
  518. CS35L33_X_STATE | CS35L33_X_LOC, CS35L33_X_STATE
  519. | CS35L33_X_LOC);
  520. }
  521. /* disconnect {vp,vbst}_mon routes: eanble later if set in tx_mask*/
  522. snd_soc_dapm_del_routes(dapm, cs35l33_vp_vbst_mon_route,
  523. ARRAY_SIZE(cs35l33_vp_vbst_mon_route));
  524. while (slot >= 0) {
  525. /* configure VMON_TX_LOC */
  526. if (slot_num == 0) {
  527. regmap_update_bits(priv->regmap, CS35L33_TX_VMON,
  528. CS35L33_X_STATE | CS35L33_X_LOC, slot);
  529. dev_dbg(component->dev, "VMON enabled in slots %d-%d",
  530. slot, slot + 1);
  531. }
  532. /* configure IMON_TX_LOC */
  533. if (slot_num == 3) {
  534. regmap_update_bits(priv->regmap, CS35L33_TX_IMON,
  535. CS35L33_X_STATE | CS35L33_X_LOC, slot);
  536. dev_dbg(component->dev, "IMON enabled in slots %d-%d",
  537. slot, slot + 1);
  538. }
  539. /* configure VPMON_TX_LOC */
  540. if (slot_num == 4) {
  541. regmap_update_bits(priv->regmap, CS35L33_TX_VPMON,
  542. CS35L33_X_STATE | CS35L33_X_LOC, slot);
  543. snd_soc_dapm_add_routes(dapm,
  544. &cs35l33_vp_vbst_mon_route[0], 2);
  545. dev_dbg(component->dev, "VPMON enabled in slots %d", slot);
  546. }
  547. /* configure VBSTMON_TX_LOC */
  548. if (slot_num == 5) {
  549. regmap_update_bits(priv->regmap, CS35L33_TX_VBSTMON,
  550. CS35L33_X_STATE | CS35L33_X_LOC, slot);
  551. snd_soc_dapm_add_routes(dapm,
  552. &cs35l33_vp_vbst_mon_route[2], 2);
  553. dev_dbg(component->dev,
  554. "VBSTMON enabled in slots %d", slot);
  555. }
  556. /* Enable the relevant tx slot */
  557. reg = CS35L33_TX_EN4 - (slot/8);
  558. bit_pos = slot - ((slot / 8) * (8));
  559. regmap_update_bits(priv->regmap, reg,
  560. 1 << bit_pos, 1 << bit_pos);
  561. tx_mask &= ~(1 << slot);
  562. slot = ffs(tx_mask) - 1;
  563. slot_num++;
  564. }
  565. return 0;
  566. }
  567. static int cs35l33_component_set_sysclk(struct snd_soc_component *component,
  568. int clk_id, int source, unsigned int freq, int dir)
  569. {
  570. struct cs35l33_private *cs35l33 = snd_soc_component_get_drvdata(component);
  571. switch (freq) {
  572. case CS35L33_MCLK_5644:
  573. case CS35L33_MCLK_6:
  574. case CS35L33_MCLK_6144:
  575. regmap_update_bits(cs35l33->regmap, CS35L33_CLK_CTL,
  576. CS35L33_MCLKDIV2, 0);
  577. cs35l33->mclk_int = freq;
  578. break;
  579. case CS35L33_MCLK_11289:
  580. case CS35L33_MCLK_12:
  581. case CS35L33_MCLK_12288:
  582. regmap_update_bits(cs35l33->regmap, CS35L33_CLK_CTL,
  583. CS35L33_MCLKDIV2, CS35L33_MCLKDIV2);
  584. cs35l33->mclk_int = freq/2;
  585. break;
  586. default:
  587. cs35l33->mclk_int = 0;
  588. return -EINVAL;
  589. }
  590. dev_dbg(component->dev, "external mclk freq=%d, internal mclk freq=%d\n",
  591. freq, cs35l33->mclk_int);
  592. return 0;
  593. }
  594. static const struct snd_soc_dai_ops cs35l33_ops = {
  595. .startup = cs35l33_pcm_startup,
  596. .set_tristate = cs35l33_set_tristate,
  597. .set_fmt = cs35l33_set_dai_fmt,
  598. .hw_params = cs35l33_pcm_hw_params,
  599. .set_tdm_slot = cs35l33_set_tdm_slot,
  600. };
  601. static struct snd_soc_dai_driver cs35l33_dai = {
  602. .name = "cs35l33-dai",
  603. .id = 0,
  604. .playback = {
  605. .stream_name = "CS35L33 Playback",
  606. .channels_min = 1,
  607. .channels_max = 1,
  608. .rates = CS35L33_RATES,
  609. .formats = CS35L33_FORMATS,
  610. },
  611. .capture = {
  612. .stream_name = "CS35L33 Capture",
  613. .channels_min = 2,
  614. .channels_max = 2,
  615. .rates = CS35L33_RATES,
  616. .formats = CS35L33_FORMATS,
  617. },
  618. .ops = &cs35l33_ops,
  619. .symmetric_rate = 1,
  620. };
  621. static int cs35l33_set_hg_data(struct snd_soc_component *component,
  622. struct cs35l33_pdata *pdata)
  623. {
  624. struct cs35l33_hg *hg_config = &pdata->hg_config;
  625. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
  626. struct cs35l33_private *priv = snd_soc_component_get_drvdata(component);
  627. if (hg_config->enable_hg_algo) {
  628. regmap_update_bits(priv->regmap, CS35L33_HG_MEMLDO_CTL,
  629. CS35L33_MEM_DEPTH_MASK,
  630. hg_config->mem_depth << CS35L33_MEM_DEPTH_SHIFT);
  631. regmap_write(priv->regmap, CS35L33_HG_REL_RATE,
  632. hg_config->release_rate);
  633. regmap_update_bits(priv->regmap, CS35L33_HG_HEAD,
  634. CS35L33_HD_RM_MASK,
  635. hg_config->hd_rm << CS35L33_HD_RM_SHIFT);
  636. regmap_update_bits(priv->regmap, CS35L33_HG_MEMLDO_CTL,
  637. CS35L33_LDO_THLD_MASK,
  638. hg_config->ldo_thld << CS35L33_LDO_THLD_SHIFT);
  639. regmap_update_bits(priv->regmap, CS35L33_HG_MEMLDO_CTL,
  640. CS35L33_LDO_DISABLE_MASK,
  641. hg_config->ldo_path_disable <<
  642. CS35L33_LDO_DISABLE_SHIFT);
  643. regmap_update_bits(priv->regmap, CS35L33_LDO_DEL,
  644. CS35L33_LDO_ENTRY_DELAY_MASK,
  645. hg_config->ldo_entry_delay <<
  646. CS35L33_LDO_ENTRY_DELAY_SHIFT);
  647. if (hg_config->vp_hg_auto) {
  648. regmap_update_bits(priv->regmap, CS35L33_HG_EN,
  649. CS35L33_VP_HG_AUTO_MASK,
  650. CS35L33_VP_HG_AUTO_MASK);
  651. snd_soc_dapm_add_routes(dapm, cs35l33_vphg_auto_route,
  652. ARRAY_SIZE(cs35l33_vphg_auto_route));
  653. }
  654. regmap_update_bits(priv->regmap, CS35L33_HG_EN,
  655. CS35L33_VP_HG_MASK,
  656. hg_config->vp_hg << CS35L33_VP_HG_SHIFT);
  657. regmap_update_bits(priv->regmap, CS35L33_LDO_DEL,
  658. CS35L33_VP_HG_RATE_MASK,
  659. hg_config->vp_hg_rate << CS35L33_VP_HG_RATE_SHIFT);
  660. regmap_update_bits(priv->regmap, CS35L33_LDO_DEL,
  661. CS35L33_VP_HG_VA_MASK,
  662. hg_config->vp_hg_va << CS35L33_VP_HG_VA_SHIFT);
  663. regmap_update_bits(priv->regmap, CS35L33_HG_EN,
  664. CS35L33_CLASS_HG_EN_MASK, CS35L33_CLASS_HG_EN_MASK);
  665. }
  666. return 0;
  667. }
  668. static int cs35l33_set_bst_ipk(struct snd_soc_component *component, unsigned int bst)
  669. {
  670. struct cs35l33_private *cs35l33 = snd_soc_component_get_drvdata(component);
  671. int ret = 0, steps = 0;
  672. /* Boost current in uA */
  673. if (bst > 3600000 || bst < 1850000) {
  674. dev_err(component->dev, "Invalid boost current %d\n", bst);
  675. ret = -EINVAL;
  676. goto err;
  677. }
  678. if (bst % 15625) {
  679. dev_err(component->dev, "Current not a multiple of 15625uA (%d)\n",
  680. bst);
  681. ret = -EINVAL;
  682. goto err;
  683. }
  684. while (bst > 1850000) {
  685. bst -= 15625;
  686. steps++;
  687. }
  688. regmap_write(cs35l33->regmap, CS35L33_BST_PEAK_CTL,
  689. steps+0x70);
  690. err:
  691. return ret;
  692. }
  693. static int cs35l33_probe(struct snd_soc_component *component)
  694. {
  695. struct cs35l33_private *cs35l33 = snd_soc_component_get_drvdata(component);
  696. cs35l33->component = component;
  697. pm_runtime_get_sync(component->dev);
  698. regmap_update_bits(cs35l33->regmap, CS35L33_PROTECT_CTL,
  699. CS35L33_ALIVE_WD_DIS, 0x8);
  700. regmap_update_bits(cs35l33->regmap, CS35L33_BST_CTL2,
  701. CS35L33_ALIVE_WD_DIS2,
  702. CS35L33_ALIVE_WD_DIS2);
  703. /* Set Platform Data */
  704. regmap_update_bits(cs35l33->regmap, CS35L33_BST_CTL1,
  705. CS35L33_BST_CTL_MASK, cs35l33->pdata.boost_ctl);
  706. regmap_update_bits(cs35l33->regmap, CS35L33_CLASSD_CTL,
  707. CS35L33_AMP_DRV_SEL_MASK,
  708. cs35l33->pdata.amp_drv_sel << CS35L33_AMP_DRV_SEL_SHIFT);
  709. if (cs35l33->pdata.boost_ipk)
  710. cs35l33_set_bst_ipk(component, cs35l33->pdata.boost_ipk);
  711. if (cs35l33->enable_soft_ramp) {
  712. snd_soc_component_update_bits(component, CS35L33_DAC_CTL,
  713. CS35L33_DIGSFT, CS35L33_DIGSFT);
  714. snd_soc_component_update_bits(component, CS35L33_DAC_CTL,
  715. CS35L33_DSR_RATE, cs35l33->pdata.ramp_rate);
  716. } else {
  717. snd_soc_component_update_bits(component, CS35L33_DAC_CTL,
  718. CS35L33_DIGSFT, 0);
  719. }
  720. /* update IMON scaling rate if different from default of 0x8 */
  721. if (cs35l33->pdata.imon_adc_scale != 0x8)
  722. snd_soc_component_update_bits(component, CS35L33_ADC_CTL,
  723. CS35L33_IMON_SCALE, cs35l33->pdata.imon_adc_scale);
  724. cs35l33_set_hg_data(component, &(cs35l33->pdata));
  725. /*
  726. * unmask important interrupts that causes the chip to enter
  727. * speaker safe mode and hence deserves user attention
  728. */
  729. regmap_update_bits(cs35l33->regmap, CS35L33_INT_MASK_1,
  730. CS35L33_M_OTE | CS35L33_M_OTW | CS35L33_M_AMP_SHORT |
  731. CS35L33_M_CAL_ERR, 0);
  732. pm_runtime_put_sync(component->dev);
  733. return 0;
  734. }
  735. static const struct snd_soc_component_driver soc_component_dev_cs35l33 = {
  736. .probe = cs35l33_probe,
  737. .set_bias_level = cs35l33_set_bias_level,
  738. .set_sysclk = cs35l33_component_set_sysclk,
  739. .controls = cs35l33_snd_controls,
  740. .num_controls = ARRAY_SIZE(cs35l33_snd_controls),
  741. .dapm_widgets = cs35l33_dapm_widgets,
  742. .num_dapm_widgets = ARRAY_SIZE(cs35l33_dapm_widgets),
  743. .dapm_routes = cs35l33_audio_map,
  744. .num_dapm_routes = ARRAY_SIZE(cs35l33_audio_map),
  745. .use_pmdown_time = 1,
  746. .endianness = 1,
  747. };
  748. static const struct regmap_config cs35l33_regmap = {
  749. .reg_bits = 8,
  750. .val_bits = 8,
  751. .max_register = CS35L33_MAX_REGISTER,
  752. .reg_defaults = cs35l33_reg,
  753. .num_reg_defaults = ARRAY_SIZE(cs35l33_reg),
  754. .volatile_reg = cs35l33_volatile_register,
  755. .readable_reg = cs35l33_readable_register,
  756. .writeable_reg = cs35l33_writeable_register,
  757. .cache_type = REGCACHE_RBTREE,
  758. .use_single_read = true,
  759. .use_single_write = true,
  760. };
  761. static int __maybe_unused cs35l33_runtime_resume(struct device *dev)
  762. {
  763. struct cs35l33_private *cs35l33 = dev_get_drvdata(dev);
  764. int ret;
  765. dev_dbg(dev, "%s\n", __func__);
  766. gpiod_set_value_cansleep(cs35l33->reset_gpio, 0);
  767. ret = regulator_bulk_enable(cs35l33->num_core_supplies,
  768. cs35l33->core_supplies);
  769. if (ret != 0) {
  770. dev_err(dev, "Failed to enable core supplies: %d\n", ret);
  771. return ret;
  772. }
  773. regcache_cache_only(cs35l33->regmap, false);
  774. gpiod_set_value_cansleep(cs35l33->reset_gpio, 1);
  775. msleep(CS35L33_BOOT_DELAY);
  776. ret = regcache_sync(cs35l33->regmap);
  777. if (ret != 0) {
  778. dev_err(dev, "Failed to restore register cache\n");
  779. goto err;
  780. }
  781. return 0;
  782. err:
  783. regcache_cache_only(cs35l33->regmap, true);
  784. regulator_bulk_disable(cs35l33->num_core_supplies,
  785. cs35l33->core_supplies);
  786. return ret;
  787. }
  788. static int __maybe_unused cs35l33_runtime_suspend(struct device *dev)
  789. {
  790. struct cs35l33_private *cs35l33 = dev_get_drvdata(dev);
  791. dev_dbg(dev, "%s\n", __func__);
  792. /* redo the calibration in next power up */
  793. cs35l33->amp_cal = false;
  794. regcache_cache_only(cs35l33->regmap, true);
  795. regcache_mark_dirty(cs35l33->regmap);
  796. regulator_bulk_disable(cs35l33->num_core_supplies,
  797. cs35l33->core_supplies);
  798. return 0;
  799. }
  800. static const struct dev_pm_ops cs35l33_pm_ops = {
  801. SET_RUNTIME_PM_OPS(cs35l33_runtime_suspend,
  802. cs35l33_runtime_resume,
  803. NULL)
  804. };
  805. static int cs35l33_get_hg_data(const struct device_node *np,
  806. struct cs35l33_pdata *pdata)
  807. {
  808. struct device_node *hg;
  809. struct cs35l33_hg *hg_config = &pdata->hg_config;
  810. u32 val32;
  811. hg = of_get_child_by_name(np, "cirrus,hg-algo");
  812. hg_config->enable_hg_algo = hg ? true : false;
  813. if (hg_config->enable_hg_algo) {
  814. if (of_property_read_u32(hg, "cirrus,mem-depth", &val32) >= 0)
  815. hg_config->mem_depth = val32;
  816. if (of_property_read_u32(hg, "cirrus,release-rate",
  817. &val32) >= 0)
  818. hg_config->release_rate = val32;
  819. if (of_property_read_u32(hg, "cirrus,ldo-thld", &val32) >= 0)
  820. hg_config->ldo_thld = val32;
  821. if (of_property_read_u32(hg, "cirrus,ldo-path-disable",
  822. &val32) >= 0)
  823. hg_config->ldo_path_disable = val32;
  824. if (of_property_read_u32(hg, "cirrus,ldo-entry-delay",
  825. &val32) >= 0)
  826. hg_config->ldo_entry_delay = val32;
  827. hg_config->vp_hg_auto = of_property_read_bool(hg,
  828. "cirrus,vp-hg-auto");
  829. if (of_property_read_u32(hg, "cirrus,vp-hg", &val32) >= 0)
  830. hg_config->vp_hg = val32;
  831. if (of_property_read_u32(hg, "cirrus,vp-hg-rate", &val32) >= 0)
  832. hg_config->vp_hg_rate = val32;
  833. if (of_property_read_u32(hg, "cirrus,vp-hg-va", &val32) >= 0)
  834. hg_config->vp_hg_va = val32;
  835. }
  836. of_node_put(hg);
  837. return 0;
  838. }
  839. static irqreturn_t cs35l33_irq_thread(int irq, void *data)
  840. {
  841. struct cs35l33_private *cs35l33 = data;
  842. struct snd_soc_component *component = cs35l33->component;
  843. unsigned int sticky_val1, sticky_val2, current_val, mask1, mask2;
  844. regmap_read(cs35l33->regmap, CS35L33_INT_STATUS_2,
  845. &sticky_val2);
  846. regmap_read(cs35l33->regmap, CS35L33_INT_STATUS_1,
  847. &sticky_val1);
  848. regmap_read(cs35l33->regmap, CS35L33_INT_MASK_2, &mask2);
  849. regmap_read(cs35l33->regmap, CS35L33_INT_MASK_1, &mask1);
  850. /* Check to see if the unmasked bits are active,
  851. * if not then exit.
  852. */
  853. if (!(sticky_val1 & ~mask1) && !(sticky_val2 & ~mask2))
  854. return IRQ_NONE;
  855. regmap_read(cs35l33->regmap, CS35L33_INT_STATUS_1,
  856. &current_val);
  857. /* handle the interrupts */
  858. if (sticky_val1 & CS35L33_AMP_SHORT) {
  859. dev_crit(component->dev, "Amp short error\n");
  860. if (!(current_val & CS35L33_AMP_SHORT)) {
  861. dev_dbg(component->dev,
  862. "Amp short error release\n");
  863. regmap_update_bits(cs35l33->regmap,
  864. CS35L33_AMP_CTL,
  865. CS35L33_AMP_SHORT_RLS, 0);
  866. regmap_update_bits(cs35l33->regmap,
  867. CS35L33_AMP_CTL,
  868. CS35L33_AMP_SHORT_RLS,
  869. CS35L33_AMP_SHORT_RLS);
  870. regmap_update_bits(cs35l33->regmap,
  871. CS35L33_AMP_CTL, CS35L33_AMP_SHORT_RLS,
  872. 0);
  873. }
  874. }
  875. if (sticky_val1 & CS35L33_CAL_ERR) {
  876. dev_err(component->dev, "Cal error\n");
  877. /* redo the calibration in next power up */
  878. cs35l33->amp_cal = false;
  879. if (!(current_val & CS35L33_CAL_ERR)) {
  880. dev_dbg(component->dev, "Cal error release\n");
  881. regmap_update_bits(cs35l33->regmap,
  882. CS35L33_AMP_CTL, CS35L33_CAL_ERR_RLS,
  883. 0);
  884. regmap_update_bits(cs35l33->regmap,
  885. CS35L33_AMP_CTL, CS35L33_CAL_ERR_RLS,
  886. CS35L33_CAL_ERR_RLS);
  887. regmap_update_bits(cs35l33->regmap,
  888. CS35L33_AMP_CTL, CS35L33_CAL_ERR_RLS,
  889. 0);
  890. }
  891. }
  892. if (sticky_val1 & CS35L33_OTE) {
  893. dev_crit(component->dev, "Over temperature error\n");
  894. if (!(current_val & CS35L33_OTE)) {
  895. dev_dbg(component->dev,
  896. "Over temperature error release\n");
  897. regmap_update_bits(cs35l33->regmap,
  898. CS35L33_AMP_CTL, CS35L33_OTE_RLS, 0);
  899. regmap_update_bits(cs35l33->regmap,
  900. CS35L33_AMP_CTL, CS35L33_OTE_RLS,
  901. CS35L33_OTE_RLS);
  902. regmap_update_bits(cs35l33->regmap,
  903. CS35L33_AMP_CTL, CS35L33_OTE_RLS, 0);
  904. }
  905. }
  906. if (sticky_val1 & CS35L33_OTW) {
  907. dev_err(component->dev, "Over temperature warning\n");
  908. if (!(current_val & CS35L33_OTW)) {
  909. dev_dbg(component->dev,
  910. "Over temperature warning release\n");
  911. regmap_update_bits(cs35l33->regmap,
  912. CS35L33_AMP_CTL, CS35L33_OTW_RLS, 0);
  913. regmap_update_bits(cs35l33->regmap,
  914. CS35L33_AMP_CTL, CS35L33_OTW_RLS,
  915. CS35L33_OTW_RLS);
  916. regmap_update_bits(cs35l33->regmap,
  917. CS35L33_AMP_CTL, CS35L33_OTW_RLS, 0);
  918. }
  919. }
  920. if (CS35L33_ALIVE_ERR & sticky_val1)
  921. dev_err(component->dev, "ERROR: ADSPCLK Interrupt\n");
  922. if (CS35L33_MCLK_ERR & sticky_val1)
  923. dev_err(component->dev, "ERROR: MCLK Interrupt\n");
  924. if (CS35L33_VMON_OVFL & sticky_val2)
  925. dev_err(component->dev,
  926. "ERROR: VMON Overflow Interrupt\n");
  927. if (CS35L33_IMON_OVFL & sticky_val2)
  928. dev_err(component->dev,
  929. "ERROR: IMON Overflow Interrupt\n");
  930. if (CS35L33_VPMON_OVFL & sticky_val2)
  931. dev_err(component->dev,
  932. "ERROR: VPMON Overflow Interrupt\n");
  933. return IRQ_HANDLED;
  934. }
  935. static const char * const cs35l33_core_supplies[] = {
  936. "VA",
  937. "VP",
  938. };
  939. static int cs35l33_of_get_pdata(struct device *dev,
  940. struct cs35l33_private *cs35l33)
  941. {
  942. struct device_node *np = dev->of_node;
  943. struct cs35l33_pdata *pdata = &cs35l33->pdata;
  944. u32 val32;
  945. if (!np)
  946. return 0;
  947. if (of_property_read_u32(np, "cirrus,boost-ctl", &val32) >= 0) {
  948. pdata->boost_ctl = val32;
  949. pdata->amp_drv_sel = 1;
  950. }
  951. if (of_property_read_u32(np, "cirrus,ramp-rate", &val32) >= 0) {
  952. pdata->ramp_rate = val32;
  953. cs35l33->enable_soft_ramp = true;
  954. }
  955. if (of_property_read_u32(np, "cirrus,boost-ipk", &val32) >= 0)
  956. pdata->boost_ipk = val32;
  957. if (of_property_read_u32(np, "cirrus,imon-adc-scale", &val32) >= 0) {
  958. if ((val32 == 0x0) || (val32 == 0x7) || (val32 == 0x6))
  959. pdata->imon_adc_scale = val32;
  960. else
  961. /* use default value */
  962. pdata->imon_adc_scale = 0x8;
  963. } else {
  964. /* use default value */
  965. pdata->imon_adc_scale = 0x8;
  966. }
  967. cs35l33_get_hg_data(np, pdata);
  968. return 0;
  969. }
  970. static int cs35l33_i2c_probe(struct i2c_client *i2c_client)
  971. {
  972. struct cs35l33_private *cs35l33;
  973. struct cs35l33_pdata *pdata = dev_get_platdata(&i2c_client->dev);
  974. int ret, devid, i;
  975. unsigned int reg;
  976. cs35l33 = devm_kzalloc(&i2c_client->dev, sizeof(struct cs35l33_private),
  977. GFP_KERNEL);
  978. if (!cs35l33)
  979. return -ENOMEM;
  980. i2c_set_clientdata(i2c_client, cs35l33);
  981. cs35l33->regmap = devm_regmap_init_i2c(i2c_client, &cs35l33_regmap);
  982. if (IS_ERR(cs35l33->regmap)) {
  983. ret = PTR_ERR(cs35l33->regmap);
  984. dev_err(&i2c_client->dev, "regmap_init() failed: %d\n", ret);
  985. return ret;
  986. }
  987. regcache_cache_only(cs35l33->regmap, true);
  988. for (i = 0; i < ARRAY_SIZE(cs35l33_core_supplies); i++)
  989. cs35l33->core_supplies[i].supply
  990. = cs35l33_core_supplies[i];
  991. cs35l33->num_core_supplies = ARRAY_SIZE(cs35l33_core_supplies);
  992. ret = devm_regulator_bulk_get(&i2c_client->dev,
  993. cs35l33->num_core_supplies,
  994. cs35l33->core_supplies);
  995. if (ret != 0) {
  996. dev_err(&i2c_client->dev,
  997. "Failed to request core supplies: %d\n",
  998. ret);
  999. return ret;
  1000. }
  1001. if (pdata) {
  1002. cs35l33->pdata = *pdata;
  1003. } else {
  1004. cs35l33_of_get_pdata(&i2c_client->dev, cs35l33);
  1005. pdata = &cs35l33->pdata;
  1006. }
  1007. ret = devm_request_threaded_irq(&i2c_client->dev, i2c_client->irq, NULL,
  1008. cs35l33_irq_thread, IRQF_ONESHOT | IRQF_TRIGGER_LOW,
  1009. "cs35l33", cs35l33);
  1010. if (ret != 0)
  1011. dev_warn(&i2c_client->dev, "Failed to request IRQ: %d\n", ret);
  1012. /* We could issue !RST or skip it based on AMP topology */
  1013. cs35l33->reset_gpio = devm_gpiod_get_optional(&i2c_client->dev,
  1014. "reset-gpios", GPIOD_OUT_HIGH);
  1015. if (IS_ERR(cs35l33->reset_gpio)) {
  1016. dev_err(&i2c_client->dev, "%s ERROR: Can't get reset GPIO\n",
  1017. __func__);
  1018. return PTR_ERR(cs35l33->reset_gpio);
  1019. }
  1020. ret = regulator_bulk_enable(cs35l33->num_core_supplies,
  1021. cs35l33->core_supplies);
  1022. if (ret != 0) {
  1023. dev_err(&i2c_client->dev,
  1024. "Failed to enable core supplies: %d\n",
  1025. ret);
  1026. return ret;
  1027. }
  1028. gpiod_set_value_cansleep(cs35l33->reset_gpio, 1);
  1029. msleep(CS35L33_BOOT_DELAY);
  1030. regcache_cache_only(cs35l33->regmap, false);
  1031. /* initialize codec */
  1032. devid = cirrus_read_device_id(cs35l33->regmap, CS35L33_DEVID_AB);
  1033. if (devid < 0) {
  1034. ret = devid;
  1035. dev_err(&i2c_client->dev, "Failed to read device ID: %d\n", ret);
  1036. goto err_enable;
  1037. }
  1038. if (devid != CS35L33_CHIP_ID) {
  1039. dev_err(&i2c_client->dev,
  1040. "CS35L33 Device ID (%X). Expected ID %X\n",
  1041. devid, CS35L33_CHIP_ID);
  1042. ret = -EINVAL;
  1043. goto err_enable;
  1044. }
  1045. ret = regmap_read(cs35l33->regmap, CS35L33_REV_ID, &reg);
  1046. if (ret < 0) {
  1047. dev_err(&i2c_client->dev, "Get Revision ID failed\n");
  1048. goto err_enable;
  1049. }
  1050. dev_info(&i2c_client->dev,
  1051. "Cirrus Logic CS35L33, Revision: %02X\n", reg & 0xFF);
  1052. ret = regmap_register_patch(cs35l33->regmap,
  1053. cs35l33_patch, ARRAY_SIZE(cs35l33_patch));
  1054. if (ret < 0) {
  1055. dev_err(&i2c_client->dev,
  1056. "Error in applying regmap patch: %d\n", ret);
  1057. goto err_enable;
  1058. }
  1059. /* disable mclk and tdm */
  1060. regmap_update_bits(cs35l33->regmap, CS35L33_CLK_CTL,
  1061. CS35L33_MCLKDIS | CS35L33_SDOUT_3ST_TDM,
  1062. CS35L33_MCLKDIS | CS35L33_SDOUT_3ST_TDM);
  1063. pm_runtime_set_autosuspend_delay(&i2c_client->dev, 100);
  1064. pm_runtime_use_autosuspend(&i2c_client->dev);
  1065. pm_runtime_set_active(&i2c_client->dev);
  1066. pm_runtime_enable(&i2c_client->dev);
  1067. ret = devm_snd_soc_register_component(&i2c_client->dev,
  1068. &soc_component_dev_cs35l33, &cs35l33_dai, 1);
  1069. if (ret < 0) {
  1070. dev_err(&i2c_client->dev, "%s: Register component failed\n",
  1071. __func__);
  1072. goto err_enable;
  1073. }
  1074. return 0;
  1075. err_enable:
  1076. gpiod_set_value_cansleep(cs35l33->reset_gpio, 0);
  1077. regulator_bulk_disable(cs35l33->num_core_supplies,
  1078. cs35l33->core_supplies);
  1079. return ret;
  1080. }
  1081. static void cs35l33_i2c_remove(struct i2c_client *client)
  1082. {
  1083. struct cs35l33_private *cs35l33 = i2c_get_clientdata(client);
  1084. gpiod_set_value_cansleep(cs35l33->reset_gpio, 0);
  1085. pm_runtime_disable(&client->dev);
  1086. regulator_bulk_disable(cs35l33->num_core_supplies,
  1087. cs35l33->core_supplies);
  1088. }
  1089. static const struct of_device_id cs35l33_of_match[] = {
  1090. { .compatible = "cirrus,cs35l33", },
  1091. {},
  1092. };
  1093. MODULE_DEVICE_TABLE(of, cs35l33_of_match);
  1094. static const struct i2c_device_id cs35l33_id[] = {
  1095. {"cs35l33", 0},
  1096. {}
  1097. };
  1098. MODULE_DEVICE_TABLE(i2c, cs35l33_id);
  1099. static struct i2c_driver cs35l33_i2c_driver = {
  1100. .driver = {
  1101. .name = "cs35l33",
  1102. .pm = &cs35l33_pm_ops,
  1103. .of_match_table = cs35l33_of_match,
  1104. },
  1105. .id_table = cs35l33_id,
  1106. .probe_new = cs35l33_i2c_probe,
  1107. .remove = cs35l33_i2c_remove,
  1108. };
  1109. module_i2c_driver(cs35l33_i2c_driver);
  1110. MODULE_DESCRIPTION("ASoC CS35L33 driver");
  1111. MODULE_AUTHOR("Paul Handrigan, Cirrus Logic Inc, <[email protected]>");
  1112. MODULE_LICENSE("GPL");