cs35l33.c 34 KB

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