wsa881x-analog.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2016, 2018-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/device.h>
  12. #include <linux/printk.h>
  13. #include <linux/bitops.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/pm_runtime.h>
  16. #include <soc/soundwire.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/soc.h>
  20. #include <sound/soc-dapm.h>
  21. #include <sound/tlv.h>
  22. #include <dsp/q6afe-v2.h>
  23. #include <linux/delay.h>
  24. #include <linux/i2c.h>
  25. #include <linux/kernel.h>
  26. #include <linux/gpio.h>
  27. #include <soc/internal.h>
  28. #include <linux/regmap.h>
  29. #include <asoc/msm-cdc-pinctrl.h>
  30. #include "wsa881x-analog.h"
  31. #include "wsa881x-temp-sensor.h"
  32. #define SPK_GAIN_12DB 4
  33. #define WIDGET_NAME_MAX_SIZE 80
  34. /*
  35. * Private data Structure for wsa881x. All parameters related to
  36. * WSA881X codec needs to be defined here.
  37. */
  38. struct wsa881x_pdata {
  39. struct regmap *regmap[2];
  40. struct i2c_client *client[2];
  41. struct snd_soc_component *component;
  42. /* track wsa881x status during probe */
  43. int status;
  44. bool boost_enable;
  45. bool visense_enable;
  46. int spk_pa_gain;
  47. struct i2c_msg xfer_msg[2];
  48. struct mutex xfer_lock;
  49. bool regmap_flag;
  50. bool wsa_active;
  51. int index;
  52. struct wsa881x_tz_priv tz_pdata;
  53. struct clk *wsa_mclk;
  54. int bg_cnt;
  55. int clk_cnt;
  56. int enable_cnt;
  57. int version;
  58. struct mutex bg_lock;
  59. struct mutex res_lock;
  60. struct delayed_work ocp_ctl_work;
  61. struct device_node *wsa_vi_gpio_p;
  62. struct device_node *wsa_clk_gpio_p;
  63. struct device_node *wsa_reset_gpio_p;
  64. };
  65. enum {
  66. WSA881X_STATUS_PROBING,
  67. WSA881X_STATUS_I2C,
  68. };
  69. #define WSA881X_OCP_CTL_TIMER_SEC 2
  70. #define WSA881X_OCP_CTL_TEMP_CELSIUS 25
  71. #define WSA881X_OCP_CTL_POLL_TIMER_SEC 60
  72. static int wsa881x_ocp_poll_timer_sec = WSA881X_OCP_CTL_POLL_TIMER_SEC;
  73. module_param(wsa881x_ocp_poll_timer_sec, int, 0664);
  74. MODULE_PARM_DESC(wsa881x_ocp_poll_timer_sec, "timer for ocp ctl polling");
  75. static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
  76. bool enable);
  77. const char *wsa_tz_names[] = {"wsa881x.0e", "wsa881x.0f"};
  78. struct wsa881x_pdata wsa_pdata[MAX_WSA881X_DEVICE];
  79. static bool pinctrl_init;
  80. static int wsa881x_populate_dt_pdata(struct device *dev, int wsa881x_index);
  81. static int wsa881x_reset(struct wsa881x_pdata *pdata, bool enable);
  82. static int wsa881x_startup(struct wsa881x_pdata *pdata);
  83. static int wsa881x_shutdown(struct wsa881x_pdata *pdata);
  84. static int delay_array_msec[] = {10, 20, 30, 40, 50};
  85. static int wsa881x_i2c_addr = -1;
  86. static int wsa881x_probing_count;
  87. static int wsa881x_presence_count;
  88. static const char * const wsa881x_spk_pa_gain_text[] = {
  89. "POS_13P5_DB", "POS_12_DB", "POS_10P5_DB", "POS_9_DB", "POS_7P5_DB",
  90. "POS_6_DB", "POS_4P5_DB", "POS_3_DB", "POS_1P5_DB", "POS_0_DB"};
  91. static const struct soc_enum wsa881x_spk_pa_gain_enum[] = {
  92. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wsa881x_spk_pa_gain_text),
  93. wsa881x_spk_pa_gain_text),
  94. };
  95. static int wsa881x_spk_pa_gain_get(struct snd_kcontrol *kcontrol,
  96. struct snd_ctl_elem_value *ucontrol)
  97. {
  98. struct snd_soc_component *component =
  99. snd_soc_kcontrol_component(kcontrol);
  100. struct wsa881x_pdata *wsa881x =
  101. snd_soc_component_get_drvdata(component);
  102. ucontrol->value.integer.value[0] = wsa881x->spk_pa_gain;
  103. dev_dbg(component->dev, "%s: spk_pa_gain = %ld\n", __func__,
  104. ucontrol->value.integer.value[0]);
  105. return 0;
  106. }
  107. static int wsa881x_spk_pa_gain_put(struct snd_kcontrol *kcontrol,
  108. struct snd_ctl_elem_value *ucontrol)
  109. {
  110. struct snd_soc_component *component =
  111. snd_soc_kcontrol_component(kcontrol);
  112. struct wsa881x_pdata *wsa881x =
  113. snd_soc_component_get_drvdata(component);
  114. if (ucontrol->value.integer.value[0] < 0 ||
  115. ucontrol->value.integer.value[0] > 0xC) {
  116. dev_err(component->dev, "%s: Unsupported gain val %ld\n",
  117. __func__, ucontrol->value.integer.value[0]);
  118. return -EINVAL;
  119. }
  120. wsa881x->spk_pa_gain = ucontrol->value.integer.value[0];
  121. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  122. __func__, ucontrol->value.integer.value[0]);
  123. return 0;
  124. }
  125. static int get_i2c_wsa881x_device_index(u16 reg)
  126. {
  127. u16 mask = 0x0f00;
  128. int value = 0;
  129. value = ((reg & mask) >> 8) & 0x000f;
  130. switch (value) {
  131. case 0:
  132. return 0;
  133. case 1:
  134. return 1;
  135. default:
  136. break;
  137. }
  138. return -EINVAL;
  139. }
  140. static int wsa881x_i2c_write_device(struct wsa881x_pdata *wsa881x,
  141. unsigned int reg, unsigned int val)
  142. {
  143. int i = 0, rc = 0;
  144. int wsa881x_index;
  145. struct i2c_msg *msg;
  146. int ret = 0;
  147. int bytes = 1;
  148. u8 reg_addr = 0;
  149. u8 data[2];
  150. wsa881x_index = get_i2c_wsa881x_device_index(reg);
  151. if (wsa881x_index < 0) {
  152. pr_err_ratelimited("%s:invalid register to write\n", __func__);
  153. return -EINVAL;
  154. }
  155. if (wsa881x->regmap_flag) {
  156. rc = regmap_write(wsa881x->regmap[wsa881x_index], reg, val);
  157. for (i = 0; rc && i < ARRAY_SIZE(delay_array_msec); i++) {
  158. pr_err_ratelimited("Failed writing reg=%u-retry(%d)\n",
  159. reg, i);
  160. /* retry after delay of increasing order */
  161. msleep(delay_array_msec[i]);
  162. rc = regmap_write(wsa881x->regmap[wsa881x_index],
  163. reg, val);
  164. }
  165. if (rc)
  166. pr_err_ratelimited("Failed writing reg=%u rc=%d\n",
  167. reg, rc);
  168. else
  169. pr_debug("write success register = %x val = %x\n",
  170. reg, val);
  171. } else {
  172. reg_addr = (u8)reg;
  173. msg = &wsa881x->xfer_msg[0];
  174. msg->addr = wsa881x->client[wsa881x_index]->addr;
  175. msg->len = bytes + 1;
  176. msg->flags = 0;
  177. data[0] = reg;
  178. data[1] = (u8)val;
  179. msg->buf = data;
  180. ret = i2c_transfer(wsa881x->client[wsa881x_index]->adapter,
  181. wsa881x->xfer_msg, 1);
  182. /* Try again if the write fails */
  183. if (ret != 1) {
  184. ret = i2c_transfer(
  185. wsa881x->client[wsa881x_index]->adapter,
  186. wsa881x->xfer_msg, 1);
  187. if (ret != 1) {
  188. pr_err_ratelimited("failed to write the device\n");
  189. return ret;
  190. }
  191. }
  192. pr_debug("write success reg = %x val = %x\n", reg, data[1]);
  193. }
  194. return rc;
  195. }
  196. static int wsa881x_i2c_read_device(struct wsa881x_pdata *wsa881x,
  197. unsigned int reg)
  198. {
  199. int wsa881x_index;
  200. int i = 0, rc = 0;
  201. unsigned int val;
  202. struct i2c_msg *msg;
  203. int ret = 0;
  204. u8 reg_addr = 0;
  205. u8 dest[5] = {0};
  206. wsa881x_index = get_i2c_wsa881x_device_index(reg);
  207. if (wsa881x_index < 0) {
  208. pr_err_ratelimited("%s:invalid register to read\n", __func__);
  209. return -EINVAL;
  210. }
  211. if (wsa881x->regmap_flag) {
  212. rc = regmap_read(wsa881x->regmap[wsa881x_index], reg, &val);
  213. for (i = 0; rc && i < ARRAY_SIZE(delay_array_msec); i++) {
  214. pr_err_ratelimited("Failed reading reg=%u - retry(%d)\n",
  215. reg, i);
  216. /* retry after delay of increasing order */
  217. msleep(delay_array_msec[i]);
  218. rc = regmap_read(wsa881x->regmap[wsa881x_index],
  219. reg, &val);
  220. }
  221. if (rc) {
  222. pr_err_ratelimited("Failed reading reg=%u rc=%d\n",
  223. reg, rc);
  224. return rc;
  225. }
  226. pr_debug("read success reg = %x val = %x\n",
  227. reg, val);
  228. } else {
  229. reg_addr = (u8)reg;
  230. msg = &wsa881x->xfer_msg[0];
  231. msg->addr = wsa881x->client[wsa881x_index]->addr;
  232. msg->len = 1;
  233. msg->flags = 0;
  234. msg->buf = &reg_addr;
  235. msg = &wsa881x->xfer_msg[1];
  236. msg->addr = wsa881x->client[wsa881x_index]->addr;
  237. msg->len = 1;
  238. msg->flags = I2C_M_RD;
  239. msg->buf = dest;
  240. ret = i2c_transfer(wsa881x->client[wsa881x_index]->adapter,
  241. wsa881x->xfer_msg, 2);
  242. /* Try again if read fails first time */
  243. if (ret != 2) {
  244. ret = i2c_transfer(
  245. wsa881x->client[wsa881x_index]->adapter,
  246. wsa881x->xfer_msg, 2);
  247. if (ret != 2) {
  248. pr_err_ratelimited("failed to read wsa register:%d\n",
  249. reg);
  250. return ret;
  251. }
  252. }
  253. val = dest[0];
  254. }
  255. return val;
  256. }
  257. static unsigned int wsa881x_i2c_read(struct snd_soc_component *component,
  258. unsigned int reg)
  259. {
  260. struct wsa881x_pdata *wsa881x;
  261. int wsa881x_index;
  262. if (component == NULL) {
  263. pr_err_ratelimited("%s: invalid component\n", __func__);
  264. return -EINVAL;
  265. }
  266. wsa881x = snd_soc_component_get_drvdata(component);
  267. if (!wsa881x->wsa_active)
  268. return 0;
  269. wsa881x_index = get_i2c_wsa881x_device_index(reg);
  270. if (wsa881x_index < 0) {
  271. pr_err_ratelimited("%s:invalid register to read\n", __func__);
  272. return -EINVAL;
  273. }
  274. return wsa881x_i2c_read_device(wsa881x, reg);
  275. }
  276. static int wsa881x_i2c_write(struct snd_soc_component *component,
  277. unsigned int reg,
  278. unsigned int val)
  279. {
  280. struct wsa881x_pdata *wsa881x;
  281. int wsa881x_index;
  282. if (component == NULL) {
  283. pr_err_ratelimited("%s: invalid component\n", __func__);
  284. return -EINVAL;
  285. }
  286. wsa881x = snd_soc_component_get_drvdata(component);
  287. if (!wsa881x->wsa_active)
  288. return 0;
  289. wsa881x_index = get_i2c_wsa881x_device_index(reg);
  290. if (wsa881x_index < 0) {
  291. pr_err_ratelimited("%s:invalid register to read\n", __func__);
  292. return -EINVAL;
  293. }
  294. return wsa881x_i2c_write_device(wsa881x, reg, val);
  295. }
  296. static int wsa881x_i2c_get_client_index(struct i2c_client *client,
  297. int *wsa881x_index)
  298. {
  299. int ret = 0;
  300. switch (client->addr) {
  301. case WSA881X_I2C_SPK0_SLAVE0_ADDR:
  302. case WSA881X_I2C_SPK0_SLAVE1_ADDR:
  303. *wsa881x_index = WSA881X_I2C_SPK0_SLAVE0;
  304. break;
  305. case WSA881X_I2C_SPK1_SLAVE0_ADDR:
  306. case WSA881X_I2C_SPK1_SLAVE1_ADDR:
  307. *wsa881x_index = WSA881X_I2C_SPK1_SLAVE0;
  308. break;
  309. default:
  310. ret = -EINVAL;
  311. break;
  312. }
  313. return ret;
  314. }
  315. static int wsa881x_boost_ctrl(struct snd_soc_component *component, bool enable)
  316. {
  317. struct wsa881x_pdata *wsa881x =
  318. snd_soc_component_get_drvdata(component);
  319. pr_debug("%s: enable:%d\n", __func__, enable);
  320. if (enable) {
  321. if (!WSA881X_IS_2_0(wsa881x->version)) {
  322. snd_soc_component_update_bits(component,
  323. WSA881X_ANA_CTL, 0x01, 0x01);
  324. snd_soc_component_update_bits(component,
  325. WSA881X_ANA_CTL, 0x04, 0x04);
  326. snd_soc_component_update_bits(component,
  327. WSA881X_BOOST_PS_CTL,
  328. 0x40, 0x00);
  329. snd_soc_component_update_bits(component,
  330. WSA881X_BOOST_PRESET_OUT1,
  331. 0xF0, 0xB0);
  332. snd_soc_component_update_bits(component,
  333. WSA881X_BOOST_ZX_CTL,
  334. 0x20, 0x00);
  335. snd_soc_component_update_bits(component,
  336. WSA881X_BOOST_EN_CTL,
  337. 0x80, 0x80);
  338. } else {
  339. snd_soc_component_update_bits(component,
  340. WSA881X_BOOST_LOOP_STABILITY,
  341. 0x03, 0x03);
  342. snd_soc_component_update_bits(component,
  343. WSA881X_BOOST_MISC2_CTL,
  344. 0xFF, 0x14);
  345. snd_soc_component_update_bits(component,
  346. WSA881X_BOOST_START_CTL,
  347. 0x80, 0x80);
  348. snd_soc_component_update_bits(component,
  349. WSA881X_BOOST_START_CTL,
  350. 0x03, 0x00);
  351. snd_soc_component_update_bits(component,
  352. WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  353. 0x0C, 0x04);
  354. snd_soc_component_update_bits(component,
  355. WSA881X_BOOST_SLOPE_COMP_ISENSE_FB,
  356. 0x03, 0x00);
  357. snd_soc_component_update_bits(component,
  358. WSA881X_BOOST_PRESET_OUT1,
  359. 0xF0, 0x70);
  360. snd_soc_component_update_bits(component,
  361. WSA881X_ANA_CTL, 0x03, 0x01);
  362. snd_soc_component_update_bits(component,
  363. WSA881X_SPKR_DRV_EN,
  364. 0x08, 0x08);
  365. snd_soc_component_update_bits(component,
  366. WSA881X_ANA_CTL, 0x04, 0x04);
  367. snd_soc_component_update_bits(component,
  368. WSA881X_BOOST_CURRENT_LIMIT,
  369. 0x0F, 0x08);
  370. snd_soc_component_update_bits(component,
  371. WSA881X_BOOST_EN_CTL,
  372. 0x80, 0x80);
  373. }
  374. /* For WSA8810, start-up time is 1500us as per qcrg sequence */
  375. usleep_range(1500, 1510);
  376. } else {
  377. /* ENSURE: Class-D amp is shutdown. CLK is still on */
  378. snd_soc_component_update_bits(component,
  379. WSA881X_BOOST_EN_CTL, 0x80, 0x00);
  380. /* boost settle time is 1500us as per qcrg sequence */
  381. usleep_range(1500, 1510);
  382. }
  383. return 0;
  384. }
  385. static int wsa881x_visense_txfe_ctrl(struct snd_soc_component *component,
  386. bool enable,
  387. u8 isense1_gain, u8 isense2_gain,
  388. u8 vsense_gain)
  389. {
  390. u8 value = 0;
  391. struct wsa881x_pdata *wsa881x =
  392. snd_soc_component_get_drvdata(component);
  393. pr_debug("%s: enable:%d\n", __func__, enable);
  394. if (enable) {
  395. if (WSA881X_IS_2_0(wsa881x->version)) {
  396. snd_soc_component_update_bits(component,
  397. WSA881X_OTP_REG_28,
  398. 0x3F, 0x3A);
  399. snd_soc_component_update_bits(component,
  400. WSA881X_BONGO_RESRV_REG1,
  401. 0xFF, 0xB2);
  402. snd_soc_component_update_bits(component,
  403. WSA881X_BONGO_RESRV_REG2,
  404. 0xFF, 0x05);
  405. }
  406. snd_soc_component_update_bits(component,
  407. WSA881X_SPKR_PROT_FE_VSENSE_VCM,
  408. 0x08, 0x00);
  409. if (WSA881X_IS_2_0(wsa881x->version)) {
  410. snd_soc_component_update_bits(component,
  411. WSA881X_SPKR_PROT_ATEST2,
  412. 0x1C, 0x04);
  413. } else {
  414. snd_soc_component_update_bits(component,
  415. WSA881X_SPKR_PROT_ATEST2,
  416. 0x08, 0x08);
  417. snd_soc_component_update_bits(component,
  418. WSA881X_SPKR_PROT_ATEST2,
  419. 0x02, 0x02);
  420. }
  421. value = ((isense2_gain << 6) | (isense1_gain << 4) |
  422. (vsense_gain << 3));
  423. snd_soc_component_update_bits(component,
  424. WSA881X_SPKR_PROT_FE_GAIN,
  425. 0xF8, value);
  426. snd_soc_component_update_bits(component,
  427. WSA881X_SPKR_PROT_FE_GAIN,
  428. 0x01, 0x01);
  429. } else {
  430. if (WSA881X_IS_2_0(wsa881x->version))
  431. snd_soc_component_update_bits(component,
  432. WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x10, 0x10);
  433. else
  434. snd_soc_component_update_bits(component,
  435. WSA881X_SPKR_PROT_FE_VSENSE_VCM, 0x08, 0x08);
  436. /*
  437. * 200us sleep is needed after visense txfe disable as per
  438. * HW requirement.
  439. */
  440. usleep_range(200, 210);
  441. snd_soc_component_update_bits(component,
  442. WSA881X_SPKR_PROT_FE_GAIN,
  443. 0x01, 0x00);
  444. }
  445. return 0;
  446. }
  447. static int wsa881x_visense_adc_ctrl(struct snd_soc_component *component,
  448. bool enable)
  449. {
  450. struct wsa881x_pdata *wsa881x =
  451. snd_soc_component_get_drvdata(component);
  452. pr_debug("%s: enable:%d\n", __func__, enable);
  453. if (enable) {
  454. if (!WSA881X_IS_2_0(wsa881x->version))
  455. snd_soc_component_update_bits(component,
  456. WSA881X_ADC_SEL_IBIAS,
  457. 0x70, 0x40);
  458. snd_soc_component_update_bits(component,
  459. WSA881X_ADC_EN_SEL_IBIAS,
  460. 0x07, 0x04);
  461. snd_soc_component_update_bits(component,
  462. WSA881X_ADC_EN_MODU_V, 0x80, 0x80);
  463. snd_soc_component_update_bits(component,
  464. WSA881X_ADC_EN_MODU_I, 0x80, 0x80);
  465. } else {
  466. /* Ensure: Speaker Protection has been stopped */
  467. snd_soc_component_update_bits(component,
  468. WSA881X_ADC_EN_MODU_V, 0x80, 0x00);
  469. snd_soc_component_update_bits(component,
  470. WSA881X_ADC_EN_MODU_I, 0x80, 0x00);
  471. }
  472. return 0;
  473. }
  474. static void wsa881x_bandgap_ctrl(struct snd_soc_component *component,
  475. bool enable)
  476. {
  477. struct wsa881x_pdata *wsa881x =
  478. snd_soc_component_get_drvdata(component);
  479. dev_dbg(component->dev, "%s: enable:%d, bg_count:%d\n", __func__,
  480. enable, wsa881x->bg_cnt);
  481. mutex_lock(&wsa881x->bg_lock);
  482. if (enable) {
  483. ++wsa881x->bg_cnt;
  484. if (wsa881x->bg_cnt == 1) {
  485. snd_soc_component_update_bits(component,
  486. WSA881X_TEMP_OP, 0x08, 0x08);
  487. /* 400usec sleep is needed as per HW requirement */
  488. usleep_range(400, 410);
  489. snd_soc_component_update_bits(component,
  490. WSA881X_TEMP_OP, 0x04, 0x04);
  491. }
  492. } else {
  493. --wsa881x->bg_cnt;
  494. if (wsa881x->bg_cnt <= 0) {
  495. WARN_ON(wsa881x->bg_cnt < 0);
  496. wsa881x->bg_cnt = 0;
  497. snd_soc_component_update_bits(component,
  498. WSA881X_TEMP_OP, 0x04, 0x00);
  499. snd_soc_component_update_bits(component,
  500. WSA881X_TEMP_OP, 0x08, 0x00);
  501. }
  502. }
  503. mutex_unlock(&wsa881x->bg_lock);
  504. }
  505. static void wsa881x_clk_ctrl(struct snd_soc_component *component, bool enable)
  506. {
  507. struct wsa881x_pdata *wsa881x =
  508. snd_soc_component_get_drvdata(component);
  509. dev_dbg(component->dev, "%s:ss enable:%d, clk_count:%d\n", __func__,
  510. enable, wsa881x->clk_cnt);
  511. mutex_lock(&wsa881x->res_lock);
  512. if (enable) {
  513. ++wsa881x->clk_cnt;
  514. if (wsa881x->clk_cnt == 1) {
  515. snd_soc_component_write(component,
  516. WSA881X_CDC_RST_CTL, 0x02);
  517. snd_soc_component_write(component,
  518. WSA881X_CDC_RST_CTL, 0x03);
  519. snd_soc_component_write(component,
  520. WSA881X_CLOCK_CONFIG, 0x01);
  521. snd_soc_component_write(component,
  522. WSA881X_CDC_DIG_CLK_CTL, 0x01);
  523. snd_soc_component_write(component,
  524. WSA881X_CDC_ANA_CLK_CTL, 0x01);
  525. }
  526. } else {
  527. --wsa881x->clk_cnt;
  528. if (wsa881x->clk_cnt <= 0) {
  529. WARN_ON(wsa881x->clk_cnt < 0);
  530. wsa881x->clk_cnt = 0;
  531. snd_soc_component_write(component,
  532. WSA881X_CDC_ANA_CLK_CTL, 0x00);
  533. snd_soc_component_write(component,
  534. WSA881X_CDC_DIG_CLK_CTL, 0x00);
  535. if (WSA881X_IS_2_0(wsa881x->version))
  536. snd_soc_component_update_bits(component,
  537. WSA881X_CDC_TOP_CLK_CTL, 0x01, 0x00);
  538. }
  539. }
  540. mutex_unlock(&wsa881x->res_lock);
  541. }
  542. static int wsa881x_rdac_ctrl(struct snd_soc_component *component, bool enable)
  543. {
  544. struct wsa881x_pdata *wsa881x =
  545. snd_soc_component_get_drvdata(component);
  546. pr_debug("%s: enable:%d\n", __func__, enable);
  547. if (enable) {
  548. snd_soc_component_update_bits(component,
  549. WSA881X_ANA_CTL, 0x08, 0x00);
  550. snd_soc_component_update_bits(component,
  551. WSA881X_SPKR_DRV_GAIN, 0x08, 0x08);
  552. snd_soc_component_update_bits(component,
  553. WSA881X_SPKR_DAC_CTL, 0x20, 0x20);
  554. snd_soc_component_update_bits(component,
  555. WSA881X_SPKR_DAC_CTL, 0x20, 0x00);
  556. snd_soc_component_update_bits(component,
  557. WSA881X_SPKR_DAC_CTL, 0x40, 0x40);
  558. snd_soc_component_update_bits(component,
  559. WSA881X_SPKR_DAC_CTL, 0x80, 0x80);
  560. if (WSA881X_IS_2_0(wsa881x->version)) {
  561. snd_soc_component_update_bits(component,
  562. WSA881X_SPKR_BIAS_CAL, 0x01, 0x01);
  563. snd_soc_component_update_bits(component,
  564. WSA881X_SPKR_OCP_CTL, 0x30, 0x30);
  565. snd_soc_component_update_bits(component,
  566. WSA881X_SPKR_OCP_CTL, 0x0C, 0x00);
  567. }
  568. snd_soc_component_update_bits(component,
  569. WSA881X_SPKR_DRV_GAIN, 0xF0, 0x40);
  570. snd_soc_component_update_bits(component,
  571. WSA881X_SPKR_MISC_CTL1, 0x01, 0x01);
  572. } else {
  573. /* Ensure class-D amp is off */
  574. snd_soc_component_update_bits(component,
  575. WSA881X_SPKR_DAC_CTL, 0x80, 0x00);
  576. }
  577. return 0;
  578. }
  579. static int wsa881x_spkr_pa_ctrl(struct snd_soc_component *component,
  580. bool enable)
  581. {
  582. int ret = 0;
  583. struct wsa881x_pdata *wsa881x =
  584. snd_soc_component_get_drvdata(component);
  585. pr_debug("%s: enable:%d\n", __func__, enable);
  586. if (enable) {
  587. /*
  588. * Ensure: Boost is enabled and stable, Analog input is up
  589. * and outputting silence
  590. */
  591. if (!WSA881X_IS_2_0(wsa881x->version)) {
  592. snd_soc_component_update_bits(component,
  593. WSA881X_ADC_EN_DET_TEST_I,
  594. 0xFF, 0x01);
  595. snd_soc_component_update_bits(component,
  596. WSA881X_ADC_EN_MODU_V,
  597. 0x02, 0x02);
  598. snd_soc_component_update_bits(component,
  599. WSA881X_ADC_EN_DET_TEST_V,
  600. 0xFF, 0x10);
  601. snd_soc_component_update_bits(component,
  602. WSA881X_SPKR_PWRSTG_DBG,
  603. 0xA0, 0xA0);
  604. snd_soc_component_update_bits(component,
  605. WSA881X_SPKR_DRV_EN,
  606. 0x80, 0x80);
  607. usleep_range(700, 710);
  608. snd_soc_component_update_bits(component,
  609. WSA881X_SPKR_PWRSTG_DBG,
  610. 0x00, 0x00);
  611. snd_soc_component_update_bits(component,
  612. WSA881X_ADC_EN_DET_TEST_V,
  613. 0xFF, 0x00);
  614. snd_soc_component_update_bits(component,
  615. WSA881X_ADC_EN_MODU_V,
  616. 0x02, 0x00);
  617. snd_soc_component_update_bits(component,
  618. WSA881X_ADC_EN_DET_TEST_I,
  619. 0xFF, 0x00);
  620. } else
  621. snd_soc_component_update_bits(component,
  622. WSA881X_SPKR_DRV_EN, 0x80, 0x80);
  623. /* add 1000us delay as per qcrg */
  624. usleep_range(1000, 1010);
  625. snd_soc_component_update_bits(component,
  626. WSA881X_SPKR_DRV_EN, 0x01, 0x01);
  627. if (WSA881X_IS_2_0(wsa881x->version))
  628. snd_soc_component_update_bits(component,
  629. WSA881X_SPKR_BIAS_CAL,
  630. 0x01, 0x00);
  631. usleep_range(1000, 1010);
  632. snd_soc_component_update_bits(component,
  633. WSA881X_SPKR_DRV_GAIN,
  634. 0xF0, (wsa881x->spk_pa_gain << 4));
  635. if (wsa881x->visense_enable) {
  636. ret = msm_cdc_pinctrl_select_active_state(
  637. wsa881x->wsa_vi_gpio_p);
  638. if (ret) {
  639. pr_err("%s: gpio set cannot be activated %s\n",
  640. __func__, "wsa_vi");
  641. return ret;
  642. }
  643. wsa881x_visense_txfe_ctrl(component, true,
  644. 0x00, 0x01, 0x00);
  645. wsa881x_visense_adc_ctrl(component, true);
  646. }
  647. } else {
  648. /*
  649. * Ensure: Boost is still on, Stream from Analog input and
  650. * Speaker Protection has been stopped and input is at 0V
  651. */
  652. if (WSA881X_IS_2_0(wsa881x->version)) {
  653. snd_soc_component_update_bits(component,
  654. WSA881X_SPKR_BIAS_CAL,
  655. 0x01, 0x01);
  656. usleep_range(1000, 1010);
  657. snd_soc_component_update_bits(component,
  658. WSA881X_SPKR_BIAS_CAL,
  659. 0x01, 0x00);
  660. msleep(20);
  661. snd_soc_component_update_bits(component,
  662. WSA881X_ANA_CTL, 0x03, 0x00);
  663. usleep_range(200, 210);
  664. }
  665. snd_soc_component_update_bits(component,
  666. WSA881X_SPKR_DRV_EN, 0x80, 0x00);
  667. }
  668. return 0;
  669. }
  670. static int wsa881x_get_boost(struct snd_kcontrol *kcontrol,
  671. struct snd_ctl_elem_value *ucontrol)
  672. {
  673. struct snd_soc_component *component =
  674. snd_soc_kcontrol_component(kcontrol);
  675. struct wsa881x_pdata *wsa881x =
  676. snd_soc_component_get_drvdata(component);
  677. ucontrol->value.integer.value[0] = wsa881x->boost_enable;
  678. return 0;
  679. }
  680. static int wsa881x_set_boost(struct snd_kcontrol *kcontrol,
  681. struct snd_ctl_elem_value *ucontrol)
  682. {
  683. struct snd_soc_component *component =
  684. snd_soc_kcontrol_component(kcontrol);
  685. struct wsa881x_pdata *wsa881x =
  686. snd_soc_component_get_drvdata(component);
  687. int value = ucontrol->value.integer.value[0];
  688. dev_dbg(component->dev, "%s: Boost enable current %d, new %d\n",
  689. __func__, wsa881x->boost_enable, value);
  690. wsa881x->boost_enable = value;
  691. return 0;
  692. }
  693. static int wsa881x_get_visense(struct snd_kcontrol *kcontrol,
  694. struct snd_ctl_elem_value *ucontrol)
  695. {
  696. struct snd_soc_component *component =
  697. snd_soc_kcontrol_component(kcontrol);
  698. struct wsa881x_pdata *wsa881x =
  699. snd_soc_component_get_drvdata(component);
  700. ucontrol->value.integer.value[0] = wsa881x->visense_enable;
  701. return 0;
  702. }
  703. static int wsa881x_set_visense(struct snd_kcontrol *kcontrol,
  704. struct snd_ctl_elem_value *ucontrol)
  705. {
  706. struct snd_soc_component *component =
  707. snd_soc_kcontrol_component(kcontrol);
  708. struct wsa881x_pdata *wsa881x =
  709. snd_soc_component_get_drvdata(component);
  710. int value = ucontrol->value.integer.value[0];
  711. dev_dbg(component->dev, "%s: VIsense enable current %d, new %d\n",
  712. __func__, wsa881x->visense_enable, value);
  713. wsa881x->visense_enable = value;
  714. return 0;
  715. }
  716. static const struct snd_kcontrol_new wsa881x_snd_controls[] = {
  717. SOC_SINGLE_EXT("BOOST Switch", SND_SOC_NOPM, 0, 1, 0,
  718. wsa881x_get_boost, wsa881x_set_boost),
  719. SOC_SINGLE_EXT("VISENSE Switch", SND_SOC_NOPM, 0, 1, 0,
  720. wsa881x_get_visense, wsa881x_set_visense),
  721. SOC_ENUM_EXT("WSA_SPK PA Gain", wsa881x_spk_pa_gain_enum[0],
  722. wsa881x_spk_pa_gain_get, wsa881x_spk_pa_gain_put),
  723. };
  724. static const char * const rdac_text[] = {
  725. "ZERO", "Switch",
  726. };
  727. static const struct soc_enum rdac_enum =
  728. SOC_ENUM_SINGLE(SND_SOC_NOPM, 0, ARRAY_SIZE(rdac_text), rdac_text);
  729. static const struct snd_kcontrol_new rdac_mux[] = {
  730. SOC_DAPM_ENUM("RDAC", rdac_enum)
  731. };
  732. static int wsa881x_rdac_event(struct snd_soc_dapm_widget *w,
  733. struct snd_kcontrol *kcontrol, int event)
  734. {
  735. struct snd_soc_component *component =
  736. snd_soc_dapm_to_component(w->dapm);
  737. struct wsa881x_pdata *wsa881x =
  738. snd_soc_component_get_drvdata(component);
  739. int ret = 0;
  740. dev_dbg(component->dev, "%s: %s %d boost %d visense %d\n",
  741. __func__, w->name, event,
  742. wsa881x->boost_enable, wsa881x->visense_enable);
  743. switch (event) {
  744. case SND_SOC_DAPM_PRE_PMU:
  745. ret = wsa881x_startup(wsa881x);
  746. if (ret) {
  747. pr_err("%s: wsa startup failed ret: %d", __func__, ret);
  748. return ret;
  749. }
  750. wsa881x_clk_ctrl(component, true);
  751. snd_soc_component_update_bits(component,
  752. WSA881X_SPKR_DAC_CTL, 0x02, 0x02);
  753. if (!WSA881X_IS_2_0(wsa881x->version))
  754. snd_soc_component_update_bits(component,
  755. WSA881X_BIAS_REF_CTRL,
  756. 0x0F, 0x08);
  757. wsa881x_bandgap_ctrl(component, true);
  758. if (!WSA881X_IS_2_0(wsa881x->version))
  759. snd_soc_component_update_bits(component,
  760. WSA881X_SPKR_BBM_CTL,
  761. 0x02, 0x02);
  762. snd_soc_component_update_bits(component,
  763. WSA881X_SPKR_MISC_CTL1, 0xC0, 0x80);
  764. snd_soc_component_update_bits(component,
  765. WSA881X_SPKR_MISC_CTL1, 0x06, 0x06);
  766. if (!WSA881X_IS_2_0(wsa881x->version)) {
  767. snd_soc_component_update_bits(component,
  768. WSA881X_SPKR_MISC_CTL2,
  769. 0x04, 0x04);
  770. snd_soc_component_update_bits(component,
  771. WSA881X_SPKR_BIAS_INT,
  772. 0x09, 0x09);
  773. }
  774. snd_soc_component_update_bits(component,
  775. WSA881X_SPKR_PA_INT, 0xF0, 0x20);
  776. if (WSA881X_IS_2_0(wsa881x->version))
  777. snd_soc_component_update_bits(component,
  778. WSA881X_SPKR_PA_INT,
  779. 0x0E, 0x0E);
  780. if (wsa881x->boost_enable)
  781. wsa881x_boost_ctrl(component, true);
  782. break;
  783. case SND_SOC_DAPM_POST_PMU:
  784. wsa881x_rdac_ctrl(component, true);
  785. break;
  786. case SND_SOC_DAPM_PRE_PMD:
  787. wsa881x_rdac_ctrl(component, false);
  788. if (wsa881x->visense_enable) {
  789. wsa881x_visense_adc_ctrl(component, false);
  790. wsa881x_visense_txfe_ctrl(component, false,
  791. 0x00, 0x01, 0x00);
  792. ret = msm_cdc_pinctrl_select_sleep_state(
  793. wsa881x->wsa_vi_gpio_p);
  794. if (ret) {
  795. pr_err("%s: gpio set cannot be suspended %s\n",
  796. __func__, "wsa_vi");
  797. return ret;
  798. }
  799. }
  800. break;
  801. case SND_SOC_DAPM_POST_PMD:
  802. if (wsa881x->boost_enable)
  803. wsa881x_boost_ctrl(component, false);
  804. wsa881x_clk_ctrl(component, false);
  805. wsa881x_bandgap_ctrl(component, false);
  806. ret = wsa881x_shutdown(wsa881x);
  807. if (ret < 0) {
  808. pr_err("%s: wsa shutdown failed ret: %d",
  809. __func__, ret);
  810. return ret;
  811. }
  812. break;
  813. default:
  814. pr_err("%s: invalid event:%d\n", __func__, event);
  815. return -EINVAL;
  816. }
  817. return 0;
  818. }
  819. static void wsa881x_ocp_ctl_work(struct work_struct *work)
  820. {
  821. struct wsa881x_pdata *wsa881x;
  822. struct delayed_work *dwork;
  823. struct snd_soc_component *component;
  824. int temp_val;
  825. dwork = to_delayed_work(work);
  826. wsa881x = container_of(dwork, struct wsa881x_pdata, ocp_ctl_work);
  827. if (!wsa881x)
  828. return;
  829. component = wsa881x->component;
  830. wsa881x_get_temp(wsa881x->tz_pdata.tz_dev, &temp_val);
  831. dev_dbg(component->dev, " temp = %d\n", temp_val);
  832. if (temp_val <= WSA881X_OCP_CTL_TEMP_CELSIUS)
  833. snd_soc_component_update_bits(component,
  834. WSA881X_SPKR_OCP_CTL, 0xC0, 0x00);
  835. else
  836. snd_soc_component_update_bits(component,
  837. WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
  838. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  839. msecs_to_jiffies(wsa881x_ocp_poll_timer_sec * 1000));
  840. }
  841. static int wsa881x_spkr_pa_event(struct snd_soc_dapm_widget *w,
  842. struct snd_kcontrol *kcontrol, int event)
  843. {
  844. struct snd_soc_component *component =
  845. snd_soc_dapm_to_component(w->dapm);
  846. struct wsa881x_pdata *wsa881x =
  847. snd_soc_component_get_drvdata(component);
  848. pr_debug("%s: %s %d\n", __func__, w->name, event);
  849. switch (event) {
  850. case SND_SOC_DAPM_PRE_PMU:
  851. snd_soc_component_update_bits(component,
  852. WSA881X_SPKR_OCP_CTL, 0xC0, 0x80);
  853. break;
  854. case SND_SOC_DAPM_POST_PMU:
  855. wsa881x_spkr_pa_ctrl(component, true);
  856. schedule_delayed_work(&wsa881x->ocp_ctl_work,
  857. msecs_to_jiffies(WSA881X_OCP_CTL_TIMER_SEC * 1000));
  858. break;
  859. case SND_SOC_DAPM_PRE_PMD:
  860. wsa881x_spkr_pa_ctrl(component, false);
  861. break;
  862. case SND_SOC_DAPM_POST_PMD:
  863. cancel_delayed_work_sync(&wsa881x->ocp_ctl_work);
  864. snd_soc_component_update_bits(component,
  865. WSA881X_SPKR_OCP_CTL, 0xC0, 0xC0);
  866. break;
  867. default:
  868. pr_err("%s: invalid event:%d\n", __func__, event);
  869. return -EINVAL;
  870. }
  871. return 0;
  872. }
  873. static const struct snd_soc_dapm_widget wsa881x_dapm_widgets[] = {
  874. SND_SOC_DAPM_INPUT("WSA_IN"),
  875. SND_SOC_DAPM_DAC_E("RDAC Analog", NULL, SND_SOC_NOPM, 0, 0,
  876. wsa881x_rdac_event,
  877. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  878. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  879. SND_SOC_DAPM_MUX("WSA_RDAC", SND_SOC_NOPM, 0, 0,
  880. rdac_mux),
  881. SND_SOC_DAPM_PGA_S("WSA_SPKR PGA", 1, SND_SOC_NOPM, 0, 0,
  882. wsa881x_spkr_pa_event,
  883. SND_SOC_DAPM_PRE_PMU |
  884. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD |
  885. SND_SOC_DAPM_POST_PMD),
  886. SND_SOC_DAPM_OUTPUT("WSA_SPKR"),
  887. };
  888. static const struct snd_soc_dapm_route wsa881x_audio_map[] = {
  889. {"WSA_RDAC", "Switch", "WSA_IN"},
  890. {"RDAC Analog", NULL, "WSA_RDAC"},
  891. {"WSA_SPKR PGA", NULL, "RDAC Analog"},
  892. {"WSA_SPKR", NULL, "WSA_SPKR PGA"},
  893. };
  894. static int wsa881x_startup(struct wsa881x_pdata *pdata)
  895. {
  896. int ret = 0;
  897. pr_debug("%s(): wsa startup, enable_cnt:%d\n", __func__,
  898. pdata->enable_cnt);
  899. if (pdata->enable_cnt++ > 0)
  900. return 0;
  901. ret = msm_cdc_pinctrl_select_active_state(pdata->wsa_clk_gpio_p);
  902. if (ret) {
  903. pr_err("%s: gpio set cannot be activated %s\n",
  904. __func__, "wsa_clk");
  905. return ret;
  906. }
  907. ret = clk_prepare_enable(pdata->wsa_mclk);
  908. if (ret) {
  909. pr_err("%s: WSA MCLK enable failed\n",
  910. __func__);
  911. return ret;
  912. }
  913. ret = wsa881x_reset(pdata, true);
  914. return ret;
  915. }
  916. static int wsa881x_shutdown(struct wsa881x_pdata *pdata)
  917. {
  918. int ret = 0;
  919. pr_debug("%s(): wsa shutdown, enable_cnt:%d\n", __func__,
  920. pdata->enable_cnt);
  921. if (--pdata->enable_cnt > 0)
  922. return 0;
  923. ret = wsa881x_reset(pdata, false);
  924. if (ret) {
  925. pr_err("%s: wsa reset failed suspend %d\n",
  926. __func__, ret);
  927. return ret;
  928. }
  929. clk_disable_unprepare(pdata->wsa_mclk);
  930. ret = msm_cdc_pinctrl_select_sleep_state(pdata->wsa_clk_gpio_p);
  931. if (ret) {
  932. pr_err("%s: gpio set cannot be suspended %s\n",
  933. __func__, "wsa_clk");
  934. return ret;
  935. }
  936. return 0;
  937. }
  938. static int32_t wsa881x_resource_acquire(struct snd_soc_component *component,
  939. bool enable)
  940. {
  941. int ret = 0;
  942. struct wsa881x_pdata *wsa881x =
  943. snd_soc_component_get_drvdata(component);
  944. if (enable) {
  945. ret = wsa881x_startup(wsa881x);
  946. if (ret < 0) {
  947. dev_err_ratelimited(component->dev,
  948. "%s: failed to startup\n", __func__);
  949. return ret;
  950. }
  951. }
  952. wsa881x_clk_ctrl(component, enable);
  953. wsa881x_bandgap_ctrl(component, enable);
  954. if (!enable) {
  955. ret = wsa881x_shutdown(wsa881x);
  956. if (ret < 0)
  957. dev_err_ratelimited(component->dev,
  958. "%s: failed to shutdown\n", __func__);
  959. }
  960. return ret;
  961. }
  962. static int32_t wsa881x_temp_reg_read(struct snd_soc_component *component,
  963. struct wsa_temp_register *wsa_temp_reg)
  964. {
  965. struct wsa881x_pdata *wsa881x =
  966. snd_soc_component_get_drvdata(component);
  967. int ret = 0;
  968. if (!wsa881x) {
  969. dev_err(component->dev, "%s: wsa881x is NULL\n", __func__);
  970. return -EINVAL;
  971. }
  972. ret = wsa881x_resource_acquire(component, true);
  973. if (ret) {
  974. dev_err_ratelimited(component->dev,
  975. "%s: resource acquire fail\n", __func__);
  976. return ret;
  977. }
  978. if (WSA881X_IS_2_0(wsa881x->version)) {
  979. snd_soc_component_update_bits(component,
  980. WSA881X_TADC_VALUE_CTL, 0x01, 0x00);
  981. wsa_temp_reg->dmeas_msb =
  982. snd_soc_component_read32(component,
  983. WSA881X_TEMP_MSB);
  984. wsa_temp_reg->dmeas_lsb =
  985. snd_soc_component_read32(component,
  986. WSA881X_TEMP_LSB);
  987. snd_soc_component_update_bits(component,
  988. WSA881X_TADC_VALUE_CTL, 0x01, 0x01);
  989. } else {
  990. wsa_temp_reg->dmeas_msb = snd_soc_component_read32(component,
  991. WSA881X_TEMP_DOUT_MSB);
  992. wsa_temp_reg->dmeas_lsb = snd_soc_component_read32(component,
  993. WSA881X_TEMP_DOUT_LSB);
  994. }
  995. wsa_temp_reg->d1_msb = snd_soc_component_read32(component,
  996. WSA881X_OTP_REG_1);
  997. wsa_temp_reg->d1_lsb = snd_soc_component_read32(component,
  998. WSA881X_OTP_REG_2);
  999. wsa_temp_reg->d2_msb = snd_soc_component_read32(component,
  1000. WSA881X_OTP_REG_3);
  1001. wsa_temp_reg->d2_lsb = snd_soc_component_read32(component,
  1002. WSA881X_OTP_REG_4);
  1003. ret = wsa881x_resource_acquire(component, false);
  1004. if (ret)
  1005. dev_err_ratelimited(component->dev,
  1006. "%s: resource release fail\n", __func__);
  1007. return ret;
  1008. }
  1009. static int wsa881x_probe(struct snd_soc_component *component)
  1010. {
  1011. struct i2c_client *client;
  1012. int ret = 0;
  1013. int wsa881x_index = 0;
  1014. struct snd_soc_dapm_context *dapm =
  1015. snd_soc_component_get_dapm(component);
  1016. char *widget_name = NULL;
  1017. struct snd_soc_card *card = component->card;
  1018. struct snd_soc_codec_conf *codec_conf = card->codec_conf;
  1019. client = dev_get_drvdata(component->dev);
  1020. ret = wsa881x_i2c_get_client_index(client, &wsa881x_index);
  1021. if (ret != 0) {
  1022. dev_err(&client->dev, "%s: I2C get codec I2C\n"
  1023. "client failed\n", __func__);
  1024. return ret;
  1025. }
  1026. mutex_init(&wsa_pdata[wsa881x_index].bg_lock);
  1027. mutex_init(&wsa_pdata[wsa881x_index].res_lock);
  1028. snprintf(wsa_pdata[wsa881x_index].tz_pdata.name, 100, "%s",
  1029. wsa_tz_names[wsa881x_index]);
  1030. wsa_pdata[wsa881x_index].component = component;
  1031. wsa_pdata[wsa881x_index].spk_pa_gain = SPK_GAIN_12DB;
  1032. wsa_pdata[wsa881x_index].component = component;
  1033. wsa_pdata[wsa881x_index].tz_pdata.component = component;
  1034. wsa_pdata[wsa881x_index].tz_pdata.wsa_temp_reg_read =
  1035. wsa881x_temp_reg_read;
  1036. snd_soc_component_set_drvdata(component, &wsa_pdata[wsa881x_index]);
  1037. wsa881x_init_thermal(&wsa_pdata[wsa881x_index].tz_pdata);
  1038. INIT_DELAYED_WORK(&wsa_pdata[wsa881x_index].ocp_ctl_work,
  1039. wsa881x_ocp_ctl_work);
  1040. if (codec_conf->name_prefix) {
  1041. widget_name = kcalloc(WIDGET_NAME_MAX_SIZE, sizeof(char),
  1042. GFP_KERNEL);
  1043. if (!widget_name)
  1044. return -ENOMEM;
  1045. snprintf(widget_name, WIDGET_NAME_MAX_SIZE,
  1046. "%s WSA_SPKR", codec_conf->name_prefix);
  1047. snd_soc_dapm_ignore_suspend(dapm, widget_name);
  1048. snprintf(widget_name, WIDGET_NAME_MAX_SIZE,
  1049. "%s WSA_IN", codec_conf->name_prefix);
  1050. snd_soc_dapm_ignore_suspend(dapm, widget_name);
  1051. kfree(widget_name);
  1052. } else {
  1053. snd_soc_dapm_ignore_suspend(dapm, "WSA_SPKR");
  1054. snd_soc_dapm_ignore_suspend(dapm, "WSA_IN");
  1055. }
  1056. snd_soc_dapm_sync(dapm);
  1057. return 0;
  1058. }
  1059. static void wsa881x_remove(struct snd_soc_component *component)
  1060. {
  1061. struct wsa881x_pdata *wsa881x =
  1062. snd_soc_component_get_drvdata(component);
  1063. if (wsa881x->tz_pdata.tz_dev)
  1064. wsa881x_deinit_thermal(wsa881x->tz_pdata.tz_dev);
  1065. mutex_destroy(&wsa881x->bg_lock);
  1066. mutex_destroy(&wsa881x->res_lock);
  1067. }
  1068. static const struct snd_soc_component_driver soc_component_dev_wsa881x = {
  1069. .probe = wsa881x_probe,
  1070. .remove = wsa881x_remove,
  1071. .read = wsa881x_i2c_read,
  1072. .write = wsa881x_i2c_write,
  1073. .controls = wsa881x_snd_controls,
  1074. .num_controls = ARRAY_SIZE(wsa881x_snd_controls),
  1075. .dapm_widgets = wsa881x_dapm_widgets,
  1076. .num_dapm_widgets = ARRAY_SIZE(wsa881x_dapm_widgets),
  1077. .dapm_routes = wsa881x_audio_map,
  1078. .num_dapm_routes = ARRAY_SIZE(wsa881x_audio_map),
  1079. };
  1080. static int wsa881x_reset(struct wsa881x_pdata *pdata, bool enable)
  1081. {
  1082. int ret = 0;
  1083. /*
  1084. * shutdown the GPIOs WSA_EN, WSA_MCLK, regulators
  1085. * and restore defaults in soc cache when shutdown.
  1086. * Enable regulators, GPIOs WSA_MCLK, WSA_EN when powerup.
  1087. */
  1088. if (enable) {
  1089. if (pdata->wsa_active)
  1090. return 0;
  1091. ret = msm_cdc_pinctrl_select_active_state(
  1092. pdata->wsa_reset_gpio_p);
  1093. if (ret) {
  1094. pr_err("%s: gpio set cannot be activated %s\n",
  1095. __func__, "wsa_reset");
  1096. return ret;
  1097. }
  1098. ret = msm_cdc_pinctrl_select_sleep_state(
  1099. pdata->wsa_reset_gpio_p);
  1100. if (ret) {
  1101. pr_err("%s: gpio set cannot be suspended(powerup) %s\n",
  1102. __func__, "wsa_reset");
  1103. return ret;
  1104. }
  1105. ret = msm_cdc_pinctrl_select_active_state(
  1106. pdata->wsa_reset_gpio_p);
  1107. if (ret) {
  1108. pr_err("%s: gpio set cannot be activated %s\n",
  1109. __func__, "wsa_reset");
  1110. return ret;
  1111. }
  1112. pdata->wsa_active = true;
  1113. } else {
  1114. if (!pdata->wsa_active)
  1115. return 0;
  1116. ret = msm_cdc_pinctrl_select_sleep_state(
  1117. pdata->wsa_reset_gpio_p);
  1118. if (ret) {
  1119. pr_err("%s: gpio set cannot be suspended %s\n",
  1120. __func__, "wsa_reset");
  1121. return ret;
  1122. }
  1123. pdata->wsa_active = false;
  1124. }
  1125. return ret;
  1126. }
  1127. int wsa881x_get_client_index(void)
  1128. {
  1129. return wsa881x_i2c_addr;
  1130. }
  1131. EXPORT_SYMBOL(wsa881x_get_client_index);
  1132. int wsa881x_get_probing_count(void)
  1133. {
  1134. return wsa881x_probing_count;
  1135. }
  1136. EXPORT_SYMBOL(wsa881x_get_probing_count);
  1137. int wsa881x_get_presence_count(void)
  1138. {
  1139. return wsa881x_presence_count;
  1140. }
  1141. EXPORT_SYMBOL(wsa881x_get_presence_count);
  1142. static int check_wsa881x_presence(struct i2c_client *client)
  1143. {
  1144. int ret = 0;
  1145. int wsa881x_index = 0;
  1146. ret = wsa881x_i2c_get_client_index(client, &wsa881x_index);
  1147. if (ret != 0) {
  1148. dev_err(&client->dev, "%s: I2C get codec I2C\n"
  1149. "client failed\n", __func__);
  1150. return ret;
  1151. }
  1152. ret = wsa881x_i2c_read_device(&wsa_pdata[wsa881x_index],
  1153. WSA881X_CDC_RST_CTL);
  1154. if (ret < 0) {
  1155. dev_err(&client->dev, "failed to read wsa881x with addr %x\n",
  1156. client->addr);
  1157. return ret;
  1158. }
  1159. ret = wsa881x_i2c_write_device(&wsa_pdata[wsa881x_index],
  1160. WSA881X_CDC_RST_CTL, 0x01);
  1161. if (ret < 0) {
  1162. dev_err(&client->dev, "failed write addr %x reg:0x5 val:0x1\n",
  1163. client->addr);
  1164. return ret;
  1165. }
  1166. /* allow 20ms before trigger next write to verify wsa881x presence */
  1167. msleep(20);
  1168. ret = wsa881x_i2c_write_device(&wsa_pdata[wsa881x_index],
  1169. WSA881X_CDC_RST_CTL, 0x00);
  1170. if (ret < 0) {
  1171. dev_err(&client->dev, "failed write addr %x reg:0x5 val:0x0\n",
  1172. client->addr);
  1173. return ret;
  1174. }
  1175. return ret;
  1176. }
  1177. static int wsa881x_populate_dt_pdata(struct device *dev, int wsa881x_index)
  1178. {
  1179. int ret = 0;
  1180. struct wsa881x_pdata *pdata = &wsa_pdata[wsa881x_index];
  1181. /* reading the gpio configurations from dtsi file */
  1182. pdata->wsa_vi_gpio_p = of_parse_phandle(dev->of_node,
  1183. "qcom,wsa-analog-vi-gpio", 0);
  1184. pdata->wsa_clk_gpio_p = of_parse_phandle(dev->of_node,
  1185. "qcom,wsa-analog-clk-gpio", 0);
  1186. pdata->wsa_reset_gpio_p = of_parse_phandle(dev->of_node,
  1187. "qcom,wsa-analog-reset-gpio", 0);
  1188. pinctrl_init = true;
  1189. return ret;
  1190. }
  1191. static int wsa881x_i2c_probe(struct i2c_client *client,
  1192. const struct i2c_device_id *id)
  1193. {
  1194. int ret = 0;
  1195. int wsa881x_index = 0;
  1196. struct wsa881x_pdata *pdata = NULL;
  1197. struct clk *wsa_mclk = NULL;
  1198. ret = wsa881x_i2c_get_client_index(client, &wsa881x_index);
  1199. if (ret != 0) {
  1200. dev_err(&client->dev, "%s: I2C get codec I2C\n"
  1201. "client failed\n", __func__);
  1202. return ret;
  1203. }
  1204. pdata = &wsa_pdata[wsa881x_index];
  1205. if ((client->addr == WSA881X_I2C_SPK0_SLAVE1_ADDR ||
  1206. client->addr == WSA881X_I2C_SPK1_SLAVE1_ADDR) &&
  1207. (pdata->status == WSA881X_STATUS_PROBING)) {
  1208. wsa881x_probing_count++;
  1209. return ret;
  1210. }
  1211. if (pdata->status == WSA881X_STATUS_I2C) {
  1212. dev_dbg(&client->dev, "%s:probe for other slaves\n"
  1213. "devices of codec I2C slave Addr = %x\n",
  1214. __func__, client->addr);
  1215. dev_dbg(&client->dev, "%s:wsa_idx = %d SLAVE = %d\n",
  1216. __func__, wsa881x_index, WSA881X_ANALOG_SLAVE);
  1217. pdata->regmap[WSA881X_ANALOG_SLAVE] =
  1218. devm_regmap_init_i2c(
  1219. client,
  1220. &wsa881x_ana_regmap_config[WSA881X_ANALOG_SLAVE]);
  1221. regcache_cache_bypass(pdata->regmap[WSA881X_ANALOG_SLAVE],
  1222. true);
  1223. if (IS_ERR(pdata->regmap[WSA881X_ANALOG_SLAVE])) {
  1224. ret = PTR_ERR(pdata->regmap[WSA881X_ANALOG_SLAVE]);
  1225. dev_err(&client->dev,
  1226. "%s: regmap_init failed %d\n",
  1227. __func__, ret);
  1228. }
  1229. client->dev.platform_data = pdata;
  1230. i2c_set_clientdata(client, pdata);
  1231. pdata->client[WSA881X_ANALOG_SLAVE] = client;
  1232. if (pdata->version == WSA881X_2_0)
  1233. wsa881x_update_regmap_2_0(
  1234. pdata->regmap[WSA881X_ANALOG_SLAVE],
  1235. WSA881X_ANALOG_SLAVE);
  1236. wsa881x_probing_count++;
  1237. return ret;
  1238. } else if (pdata->status == WSA881X_STATUS_PROBING) {
  1239. pdata->index = wsa881x_index;
  1240. if (client->dev.of_node) {
  1241. dev_dbg(&client->dev, "%s:Platform data\n"
  1242. "from device tree\n", __func__);
  1243. ret = wsa881x_populate_dt_pdata(
  1244. &client->dev, wsa881x_index);
  1245. if (ret < 0) {
  1246. dev_err(&client->dev,
  1247. "%s: Fail to obtain pdata from device tree\n",
  1248. __func__);
  1249. ret = -EINVAL;
  1250. goto err;
  1251. }
  1252. client->dev.platform_data = pdata;
  1253. } else {
  1254. dev_dbg(&client->dev, "%s:Platform data from\n"
  1255. "board file\n", __func__);
  1256. pdata = client->dev.platform_data;
  1257. }
  1258. if (!pdata) {
  1259. dev_dbg(&client->dev, "no platform data?\n");
  1260. ret = -EINVAL;
  1261. goto err;
  1262. }
  1263. dev_set_drvdata(&client->dev, client);
  1264. pdata->regmap[WSA881X_DIGITAL_SLAVE] =
  1265. devm_regmap_init_i2c(
  1266. client,
  1267. &wsa881x_ana_regmap_config[WSA881X_DIGITAL_SLAVE]);
  1268. regcache_cache_bypass(pdata->regmap[WSA881X_DIGITAL_SLAVE],
  1269. true);
  1270. if (IS_ERR(pdata->regmap[WSA881X_DIGITAL_SLAVE])) {
  1271. ret = PTR_ERR(pdata->regmap[WSA881X_DIGITAL_SLAVE]);
  1272. dev_err(&client->dev, "%s: regmap_init failed %d\n",
  1273. __func__, ret);
  1274. goto err;
  1275. }
  1276. wsa_mclk = devm_clk_get(&client->dev, "wsa_mclk");
  1277. if (IS_ERR(wsa_mclk)) {
  1278. ret = PTR_ERR(wsa_mclk);
  1279. dev_dbg(&client->dev, "%s: clk get %s failed %d\n",
  1280. __func__, "wsa_mclk", ret);
  1281. wsa_mclk = NULL;
  1282. ret = 0;
  1283. }
  1284. pdata->wsa_mclk = wsa_mclk;
  1285. /* bus reset sequence */
  1286. ret = wsa881x_reset(pdata, true);
  1287. if (ret < 0) {
  1288. wsa881x_probing_count++;
  1289. dev_err(&client->dev, "%s: WSA enable Failed %d\n",
  1290. __func__, ret);
  1291. goto err;
  1292. }
  1293. pdata->client[WSA881X_DIGITAL_SLAVE] = client;
  1294. pdata->regmap_flag = true;
  1295. ret = check_wsa881x_presence(client);
  1296. if (ret < 0) {
  1297. dev_err(&client->dev,
  1298. "failed to ping wsa with addr:%x, ret = %d\n",
  1299. client->addr, ret);
  1300. wsa881x_probing_count++;
  1301. goto err1;
  1302. }
  1303. pdata->version = wsa881x_i2c_read_device(pdata,
  1304. WSA881X_CHIP_ID1);
  1305. pr_debug("%s: wsa881x version: %d\n", __func__, pdata->version);
  1306. if (pdata->version == WSA881X_2_0) {
  1307. wsa881x_update_reg_defaults_2_0();
  1308. wsa881x_update_regmap_2_0(
  1309. pdata->regmap[WSA881X_DIGITAL_SLAVE],
  1310. WSA881X_DIGITAL_SLAVE);
  1311. }
  1312. wsa881x_presence_count++;
  1313. wsa881x_probing_count++;
  1314. ret = snd_soc_register_component(&client->dev,
  1315. &soc_component_dev_wsa881x,
  1316. NULL, 0);
  1317. if (ret < 0)
  1318. goto err1;
  1319. pdata->status = WSA881X_STATUS_I2C;
  1320. }
  1321. err1:
  1322. wsa881x_reset(pdata, false);
  1323. err:
  1324. return 0;
  1325. }
  1326. static int wsa881x_i2c_remove(struct i2c_client *client)
  1327. {
  1328. struct wsa881x_pdata *wsa881x = client->dev.platform_data;
  1329. snd_soc_unregister_component(&client->dev);
  1330. i2c_set_clientdata(client, NULL);
  1331. kfree(wsa881x);
  1332. return 0;
  1333. }
  1334. #ifdef CONFIG_PM_SLEEP
  1335. static int wsa881x_i2c_suspend(struct device *dev)
  1336. {
  1337. pr_debug("%s: system suspend\n", __func__);
  1338. return 0;
  1339. }
  1340. static int wsa881x_i2c_resume(struct device *dev)
  1341. {
  1342. pr_debug("%s: system resume\n", __func__);
  1343. return 0;
  1344. }
  1345. static const struct dev_pm_ops wsa881x_i2c_pm_ops = {
  1346. SET_SYSTEM_SLEEP_PM_OPS(wsa881x_i2c_suspend, wsa881x_i2c_resume)
  1347. };
  1348. #endif /* CONFIG_PM_SLEEP */
  1349. static const struct i2c_device_id wsa881x_i2c_id[] = {
  1350. {"wsa881x-i2c-dev", WSA881X_I2C_SPK0_SLAVE0_ADDR},
  1351. {"wsa881x-i2c-dev", WSA881X_I2C_SPK0_SLAVE1_ADDR},
  1352. {"wsa881x-i2c-dev", WSA881X_I2C_SPK1_SLAVE0_ADDR},
  1353. {"wsa881x-i2c-dev", WSA881X_I2C_SPK1_SLAVE1_ADDR},
  1354. {}
  1355. };
  1356. MODULE_DEVICE_TABLE(i2c, wsa881x_i2c_id);
  1357. static const struct of_device_id msm_match_table[] = {
  1358. {.compatible = "qcom,wsa881x-i2c-codec"},
  1359. {}
  1360. };
  1361. MODULE_DEVICE_TABLE(of, msm_match_table);
  1362. static struct i2c_driver wsa881x_codec_driver = {
  1363. .driver = {
  1364. .name = "wsa881x-i2c-codec",
  1365. .owner = THIS_MODULE,
  1366. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1367. #ifdef CONFIG_PM_SLEEP
  1368. .pm = &wsa881x_i2c_pm_ops,
  1369. #endif
  1370. .of_match_table = msm_match_table,
  1371. },
  1372. .id_table = wsa881x_i2c_id,
  1373. .probe = wsa881x_i2c_probe,
  1374. .remove = wsa881x_i2c_remove,
  1375. };
  1376. static int __init wsa881x_codec_init(void)
  1377. {
  1378. int i = 0;
  1379. for (i = 0; i < MAX_WSA881X_DEVICE; i++)
  1380. wsa_pdata[i].status = WSA881X_STATUS_PROBING;
  1381. return i2c_add_driver(&wsa881x_codec_driver);
  1382. }
  1383. module_init(wsa881x_codec_init);
  1384. static void __exit wsa881x_codec_exit(void)
  1385. {
  1386. i2c_del_driver(&wsa881x_codec_driver);
  1387. }
  1388. module_exit(wsa881x_codec_exit);
  1389. MODULE_DESCRIPTION("WSA881x Codec driver");
  1390. MODULE_LICENSE("GPL v2");