wcd934x-dsp-cntl.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/delay.h>
  7. #include <linux/component.h>
  8. #include <linux/debugfs.h>
  9. #include <sound/soc.h>
  10. #include <sound/wcd-dsp-mgr.h>
  11. #include <asoc/wcd934x_registers.h>
  12. #include "wcd934x.h"
  13. #include "wcd934x-dsp-cntl.h"
  14. #include <asoc/wcd9xxx-irq.h>
  15. #include <asoc/core.h>
  16. #define WCD_CNTL_DIR_NAME_LEN_MAX 32
  17. #define WCD_CPE_FLL_MAX_RETRIES 5
  18. #define WCD_MEM_ENABLE_MAX_RETRIES 20
  19. #define WCD_DSP_BOOT_TIMEOUT_MS 3000
  20. #define WCD_SYSFS_ENTRY_MAX_LEN 8
  21. #define WCD_PROCFS_ENTRY_MAX_LEN 16
  22. #define WCD_934X_RAMDUMP_START_ADDR 0x20100000
  23. #define WCD_934X_RAMDUMP_SIZE ((1024 * 1024) - 128)
  24. #define WCD_MISCDEV_CMD_MAX_LEN 11
  25. #define WCD_CNTL_MUTEX_LOCK(component, lock) \
  26. { \
  27. dev_dbg(component->dev, "%s: mutex_lock(%s)\n", \
  28. __func__, __stringify_1(lock)); \
  29. mutex_lock(&lock); \
  30. }
  31. #define WCD_CNTL_MUTEX_UNLOCK(component, lock) \
  32. { \
  33. dev_dbg(component->dev, "%s: mutex_unlock(%s)\n", \
  34. __func__, __stringify_1(lock)); \
  35. mutex_unlock(&lock); \
  36. }
  37. enum wcd_mem_type {
  38. WCD_MEM_TYPE_ALWAYS_ON,
  39. WCD_MEM_TYPE_SWITCHABLE,
  40. };
  41. struct wcd_cntl_attribute {
  42. struct attribute attr;
  43. ssize_t (*show)(struct wcd_dsp_cntl *cntl, char *buf);
  44. ssize_t (*store)(struct wcd_dsp_cntl *cntl, const char *buf,
  45. ssize_t count);
  46. };
  47. #define WCD_CNTL_ATTR(_name, _mode, _show, _store) \
  48. static struct wcd_cntl_attribute cntl_attr_##_name = { \
  49. .attr = {.name = __stringify(_name), .mode = _mode}, \
  50. .show = _show, \
  51. .store = _store, \
  52. }
  53. #define to_wcd_cntl_attr(a) \
  54. container_of((a), struct wcd_cntl_attribute, attr)
  55. #define to_wcd_cntl(kobj) \
  56. container_of((kobj), struct wcd_dsp_cntl, wcd_kobj)
  57. static u8 mem_enable_values[] = {
  58. 0xFE, 0xFC, 0xF8, 0xF0,
  59. 0xE0, 0xC0, 0x80, 0x00,
  60. };
  61. #ifdef CONFIG_DEBUG_FS
  62. #define WCD_CNTL_SET_ERR_IRQ_FLAG(cntl)\
  63. atomic_cmpxchg(&cntl->err_irq_flag, 0, 1)
  64. #define WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl)\
  65. atomic_set(&cntl->err_irq_flag, 0)
  66. static u16 wdsp_reg_for_debug_dump[] = {
  67. WCD934X_CPE_SS_CPE_CTL,
  68. WCD934X_CPE_SS_PWR_SYS_PSTATE_CTL_0,
  69. WCD934X_CPE_SS_PWR_SYS_PSTATE_CTL_1,
  70. WCD934X_CPE_SS_PWR_CPEFLL_CTL,
  71. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_0,
  72. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_1,
  73. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_OVERRIDE,
  74. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_0,
  75. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_1,
  76. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_2,
  77. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_3,
  78. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_4,
  79. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_5,
  80. WCD934X_CPE_SS_PWR_CPE_DRAM1_SHUTDOWN,
  81. WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
  82. WCD934X_CPE_SS_MAD_CTL,
  83. WCD934X_CPE_SS_CPAR_CTL,
  84. WCD934X_CPE_SS_CPAR_CFG,
  85. WCD934X_CPE_SS_WDOG_CFG,
  86. WCD934X_CPE_SS_STATUS,
  87. WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
  88. WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
  89. WCD934X_CPE_SS_SS_ERROR_INT_MASK_1A,
  90. WCD934X_CPE_SS_SS_ERROR_INT_MASK_1B,
  91. WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0A,
  92. WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0B,
  93. WCD934X_CPE_SS_SS_ERROR_INT_STATUS_1A,
  94. WCD934X_CPE_SS_SS_ERROR_INT_STATUS_1B,
  95. };
  96. static void wcd_cntl_collect_debug_dumps(struct wcd_dsp_cntl *cntl,
  97. bool internal)
  98. {
  99. struct snd_soc_component *component = cntl->component;
  100. struct wdsp_err_signal_arg arg;
  101. enum wdsp_signal signal;
  102. int i;
  103. u8 val;
  104. /* If WDSP SSR happens, skip collecting debug dumps */
  105. if (WCD_CNTL_SET_ERR_IRQ_FLAG(cntl) != 0)
  106. return;
  107. /* Mask all error interrupts */
  108. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
  109. 0xFF);
  110. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
  111. 0xFF);
  112. /* Collect important WDSP registers dump for debug use */
  113. pr_err("%s: Dump the WDSP registers for debug use\n", __func__);
  114. for (i = 0; i < sizeof(wdsp_reg_for_debug_dump)/sizeof(u16); i++) {
  115. val = snd_soc_component_read32(component,
  116. wdsp_reg_for_debug_dump[i]);
  117. pr_err("%s: reg = 0x%x, val = 0x%x\n", __func__,
  118. wdsp_reg_for_debug_dump[i], val);
  119. }
  120. /* Trigger NMI in WDSP to sync and update the memory */
  121. snd_soc_component_write(component, WCD934X_CPE_SS_BACKUP_INT, 0x02);
  122. /* Collect WDSP ramdump for debug use */
  123. if (cntl->m_dev && cntl->m_ops && cntl->m_ops->signal_handler) {
  124. arg.mem_dumps_enabled = cntl->ramdump_enable;
  125. arg.remote_start_addr = WCD_934X_RAMDUMP_START_ADDR;
  126. arg.dump_size = WCD_934X_RAMDUMP_SIZE;
  127. signal = internal ? WDSP_DEBUG_DUMP_INTERNAL : WDSP_DEBUG_DUMP;
  128. cntl->m_ops->signal_handler(cntl->m_dev, signal, &arg);
  129. }
  130. /* Unmask the fatal irqs */
  131. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
  132. ~(cntl->irqs.fatal_irqs & 0xFF));
  133. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
  134. ~((cntl->irqs.fatal_irqs >> 8) & 0xFF));
  135. WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl);
  136. }
  137. #else
  138. #define WCD_CNTL_SET_ERR_IRQ_FLAG(cntl) 0
  139. #define WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl) do {} while (0)
  140. static void wcd_cntl_collect_debug_dumps(struct wcd_dsp_cntl *cntl,
  141. bool internal)
  142. {
  143. }
  144. #endif
  145. static ssize_t wdsp_boot_show(struct wcd_dsp_cntl *cntl, char *buf)
  146. {
  147. return snprintf(buf, WCD_SYSFS_ENTRY_MAX_LEN,
  148. "%u", cntl->boot_reqs);
  149. }
  150. static ssize_t wdsp_boot_store(struct wcd_dsp_cntl *cntl,
  151. const char *buf, ssize_t count)
  152. {
  153. u32 val;
  154. bool vote;
  155. int ret;
  156. ret = kstrtou32(buf, 10, &val);
  157. if (ret) {
  158. dev_err(cntl->component->dev,
  159. "%s: Invalid entry, ret = %d\n", __func__, ret);
  160. return -EINVAL;
  161. }
  162. if (val > 0) {
  163. cntl->boot_reqs++;
  164. vote = true;
  165. } else {
  166. cntl->boot_reqs--;
  167. vote = false;
  168. }
  169. if (cntl->m_dev && cntl->m_ops &&
  170. cntl->m_ops->vote_for_dsp)
  171. ret = cntl->m_ops->vote_for_dsp(cntl->m_dev, vote);
  172. else
  173. ret = -EINVAL;
  174. if (ret < 0)
  175. dev_err(cntl->component->dev,
  176. "%s: failed to %s dsp\n", __func__,
  177. vote ? "enable" : "disable");
  178. return count;
  179. }
  180. WCD_CNTL_ATTR(boot, 0660, wdsp_boot_show, wdsp_boot_store);
  181. static ssize_t wcd_cntl_sysfs_show(struct kobject *kobj,
  182. struct attribute *attr, char *buf)
  183. {
  184. struct wcd_cntl_attribute *wcd_attr = to_wcd_cntl_attr(attr);
  185. struct wcd_dsp_cntl *cntl = to_wcd_cntl(kobj);
  186. ssize_t ret = -EINVAL;
  187. if (cntl && wcd_attr->show)
  188. ret = wcd_attr->show(cntl, buf);
  189. return ret;
  190. }
  191. static ssize_t wcd_cntl_sysfs_store(struct kobject *kobj,
  192. struct attribute *attr, const char *buf,
  193. size_t count)
  194. {
  195. struct wcd_cntl_attribute *wcd_attr = to_wcd_cntl_attr(attr);
  196. struct wcd_dsp_cntl *cntl = to_wcd_cntl(kobj);
  197. ssize_t ret = -EINVAL;
  198. if (cntl && wcd_attr->store)
  199. ret = wcd_attr->store(cntl, buf, count);
  200. return ret;
  201. }
  202. static const struct sysfs_ops wcd_cntl_sysfs_ops = {
  203. .show = wcd_cntl_sysfs_show,
  204. .store = wcd_cntl_sysfs_store,
  205. };
  206. static struct kobj_type wcd_cntl_ktype = {
  207. .sysfs_ops = &wcd_cntl_sysfs_ops,
  208. };
  209. static void wcd_cntl_change_online_state(struct wcd_dsp_cntl *cntl,
  210. u8 online)
  211. {
  212. struct wdsp_ssr_entry *ssr_entry = &cntl->ssr_entry;
  213. unsigned long ret;
  214. WCD_CNTL_MUTEX_LOCK(cntl->component, cntl->ssr_mutex);
  215. ssr_entry->offline = !online;
  216. /* Make sure the write is complete */
  217. wmb();
  218. ret = xchg(&ssr_entry->offline_change, 1);
  219. wake_up_interruptible(&ssr_entry->offline_poll_wait);
  220. dev_dbg(cntl->component->dev,
  221. "%s: requested %u, offline %u offline_change %u, ret = %ldn",
  222. __func__, online, ssr_entry->offline,
  223. ssr_entry->offline_change, ret);
  224. WCD_CNTL_MUTEX_UNLOCK(cntl->component, cntl->ssr_mutex);
  225. }
  226. static ssize_t wdsp_ssr_entry_read(struct snd_info_entry *entry,
  227. void *file_priv_data, struct file *file,
  228. char __user *buf, size_t count, loff_t pos)
  229. {
  230. int len = 0;
  231. char buffer[WCD_PROCFS_ENTRY_MAX_LEN];
  232. struct wcd_dsp_cntl *cntl;
  233. struct wdsp_ssr_entry *ssr_entry;
  234. ssize_t ret;
  235. u8 offline;
  236. cntl = (struct wcd_dsp_cntl *) entry->private_data;
  237. if (!cntl) {
  238. pr_err("%s: Invalid private data for SSR procfs entry\n",
  239. __func__);
  240. return -EINVAL;
  241. }
  242. ssr_entry = &cntl->ssr_entry;
  243. WCD_CNTL_MUTEX_LOCK(cntl->component, cntl->ssr_mutex);
  244. offline = ssr_entry->offline;
  245. /* Make sure the read is complete */
  246. rmb();
  247. dev_dbg(cntl->component->dev, "%s: offline = %s\n", __func__,
  248. offline ? "true" : "false");
  249. len = snprintf(buffer, sizeof(buffer), "%s\n",
  250. offline ? "OFFLINE" : "ONLINE");
  251. ret = simple_read_from_buffer(buf, count, &pos, buffer, len);
  252. WCD_CNTL_MUTEX_UNLOCK(cntl->component, cntl->ssr_mutex);
  253. return ret;
  254. }
  255. static unsigned int wdsp_ssr_entry_poll(struct snd_info_entry *entry,
  256. void *private_data, struct file *file,
  257. poll_table *wait)
  258. {
  259. struct wcd_dsp_cntl *cntl;
  260. struct wdsp_ssr_entry *ssr_entry;
  261. unsigned int ret = 0;
  262. if (!entry || !entry->private_data) {
  263. pr_err("%s: %s is NULL\n", __func__,
  264. (!entry) ? "entry" : "private_data");
  265. return -EINVAL;
  266. }
  267. cntl = (struct wcd_dsp_cntl *) entry->private_data;
  268. ssr_entry = &cntl->ssr_entry;
  269. dev_dbg(cntl->component->dev, "%s: Poll wait, offline = %u\n",
  270. __func__, ssr_entry->offline);
  271. poll_wait(file, &ssr_entry->offline_poll_wait, wait);
  272. dev_dbg(cntl->component->dev, "%s: Woken up Poll wait, offline = %u\n",
  273. __func__, ssr_entry->offline);
  274. WCD_CNTL_MUTEX_LOCK(cntl->component, cntl->ssr_mutex);
  275. if (xchg(&ssr_entry->offline_change, 0))
  276. ret = POLLIN | POLLPRI | POLLRDNORM;
  277. dev_dbg(cntl->component->dev, "%s: ret (%d) from poll_wait\n",
  278. __func__, ret);
  279. WCD_CNTL_MUTEX_UNLOCK(cntl->component, cntl->ssr_mutex);
  280. return ret;
  281. }
  282. static struct snd_info_entry_ops wdsp_ssr_entry_ops = {
  283. .read = wdsp_ssr_entry_read,
  284. .poll = wdsp_ssr_entry_poll,
  285. };
  286. static int wcd_cntl_cpe_fll_calibrate(struct wcd_dsp_cntl *cntl)
  287. {
  288. struct snd_soc_component *component = cntl->component;
  289. int ret = 0, retry = 0;
  290. u8 cal_lsb, cal_msb;
  291. u8 lock_det;
  292. /* Make sure clocks are gated */
  293. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
  294. 0x05, 0x00);
  295. /* Enable CPE FLL reference clock */
  296. snd_soc_component_update_bits(component, WCD934X_CLK_SYS_MCLK2_PRG1,
  297. 0x80, 0x80);
  298. snd_soc_component_update_bits(component, WCD934X_CPE_FLL_USER_CTL_5,
  299. 0xF3, 0x13);
  300. snd_soc_component_write(component, WCD934X_CPE_FLL_L_VAL_CTL_0, 0x50);
  301. /* Disable CPAR reset and Enable CPAR clk */
  302. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
  303. 0x02, 0x02);
  304. /* Write calibration l-value based on cdc clk rate */
  305. if (cntl->clk_rate == 9600000) {
  306. cal_lsb = 0x6d;
  307. cal_msb = 0x00;
  308. } else {
  309. cal_lsb = 0x56;
  310. cal_msb = 0x00;
  311. }
  312. snd_soc_component_write(component, WCD934X_CPE_FLL_USER_CTL_6, cal_lsb);
  313. snd_soc_component_write(component, WCD934X_CPE_FLL_USER_CTL_7, cal_msb);
  314. /* FLL mode to follow power up sequence */
  315. snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
  316. 0x60, 0x00);
  317. /* HW controlled CPE FLL */
  318. snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
  319. 0x80, 0x80);
  320. /* Force on CPE FLL */
  321. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CFG,
  322. 0x04, 0x04);
  323. do {
  324. /* Time for FLL calibration to complete */
  325. usleep_range(1000, 1100);
  326. lock_det = snd_soc_component_read32(
  327. component, WCD934X_CPE_FLL_STATUS_3);
  328. retry++;
  329. } while (!(lock_det & 0x01) &&
  330. retry <= WCD_CPE_FLL_MAX_RETRIES);
  331. if (!(lock_det & 0x01)) {
  332. dev_err(component->dev, "%s: lock detect not set, 0x%02x\n",
  333. __func__, lock_det);
  334. ret = -EIO;
  335. goto err_lock_det;
  336. }
  337. snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
  338. 0x60, 0x20);
  339. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CFG,
  340. 0x04, 0x00);
  341. return ret;
  342. err_lock_det:
  343. /* Undo the register settings */
  344. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CFG,
  345. 0x04, 0x00);
  346. snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
  347. 0x80, 0x00);
  348. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
  349. 0x02, 0x00);
  350. return ret;
  351. }
  352. static void wcd_cntl_config_cpar(struct wcd_dsp_cntl *cntl)
  353. {
  354. struct snd_soc_component *component = cntl->component;
  355. u8 nom_lo, nom_hi, svs2_lo, svs2_hi;
  356. /* Configure CPAR */
  357. nom_hi = svs2_hi = 0;
  358. if (cntl->clk_rate == 9600000) {
  359. nom_lo = 0x90;
  360. svs2_lo = 0x50;
  361. } else {
  362. nom_lo = 0x70;
  363. svs2_lo = 0x3e;
  364. }
  365. snd_soc_component_write(component, WCD934X_TEST_DEBUG_LVAL_NOM_LOW,
  366. nom_lo);
  367. snd_soc_component_write(component, WCD934X_TEST_DEBUG_LVAL_NOM_HIGH,
  368. nom_hi);
  369. snd_soc_component_write(component, WCD934X_TEST_DEBUG_LVAL_SVS_SVS2_LOW,
  370. svs2_lo);
  371. snd_soc_component_write(component,
  372. WCD934X_TEST_DEBUG_LVAL_SVS_SVS2_HIGH,
  373. svs2_hi);
  374. snd_soc_component_update_bits(component, WCD934X_CPE_SS_PWR_CPEFLL_CTL,
  375. 0x03, 0x03);
  376. }
  377. static int wcd_cntl_cpe_fll_ctrl(struct wcd_dsp_cntl *cntl,
  378. bool enable)
  379. {
  380. struct snd_soc_component *component = cntl->component;
  381. int ret = 0;
  382. if (enable) {
  383. ret = wcd_cntl_cpe_fll_calibrate(cntl);
  384. if (ret < 0) {
  385. dev_err(component->dev,
  386. "%s: cpe_fll_cal failed, err = %d\n",
  387. __func__, ret);
  388. goto done;
  389. }
  390. wcd_cntl_config_cpar(cntl);
  391. /* Enable AHB CLK and CPE CLK*/
  392. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
  393. 0x05, 0x05);
  394. } else {
  395. /* Disable AHB CLK and CPE CLK */
  396. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
  397. 0x05, 0x00);
  398. /* Reset the CPAR mode for CPE FLL */
  399. snd_soc_component_write(component, WCD934X_CPE_FLL_FLL_MODE,
  400. 0x20);
  401. snd_soc_component_update_bits(component,
  402. WCD934X_CPE_SS_CPAR_CFG,
  403. 0x04, 0x00);
  404. snd_soc_component_update_bits(component,
  405. WCD934X_CPE_SS_CPAR_CTL,
  406. 0x02, 0x00);
  407. }
  408. done:
  409. return ret;
  410. }
  411. static int wcd_cntl_clocks_enable(struct wcd_dsp_cntl *cntl)
  412. {
  413. struct snd_soc_component *component = cntl->component;
  414. int ret;
  415. WCD_CNTL_MUTEX_LOCK(component, cntl->clk_mutex);
  416. /* Enable codec clock */
  417. if (cntl->cdc_cb && cntl->cdc_cb->cdc_clk_en)
  418. ret = cntl->cdc_cb->cdc_clk_en(component, true);
  419. else
  420. ret = -EINVAL;
  421. if (ret < 0) {
  422. dev_err(component->dev,
  423. "%s: Failed to enable cdc clk, err = %d\n",
  424. __func__, ret);
  425. goto done;
  426. }
  427. /* Pull CPAR out of reset */
  428. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
  429. 0x04, 0x00);
  430. /* Configure and Enable CPE FLL clock */
  431. ret = wcd_cntl_cpe_fll_ctrl(cntl, true);
  432. if (ret < 0) {
  433. dev_err(component->dev,
  434. "%s: Failed to enable cpe clk, err = %d\n",
  435. __func__, ret);
  436. goto err_cpe_clk;
  437. }
  438. cntl->is_clk_enabled = true;
  439. /* Ungate the CPR clock */
  440. snd_soc_component_update_bits(component, WCD934X_CODEC_RPM_CLK_GATE,
  441. 0x10, 0x00);
  442. done:
  443. WCD_CNTL_MUTEX_UNLOCK(component, cntl->clk_mutex);
  444. return ret;
  445. err_cpe_clk:
  446. if (cntl->cdc_cb && cntl->cdc_cb->cdc_clk_en)
  447. cntl->cdc_cb->cdc_clk_en(component, false);
  448. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
  449. 0x04, 0x04);
  450. WCD_CNTL_MUTEX_UNLOCK(component, cntl->clk_mutex);
  451. return ret;
  452. }
  453. static int wcd_cntl_clocks_disable(struct wcd_dsp_cntl *cntl)
  454. {
  455. struct snd_soc_component *component = cntl->component;
  456. int ret = 0;
  457. WCD_CNTL_MUTEX_LOCK(component, cntl->clk_mutex);
  458. if (!cntl->is_clk_enabled) {
  459. dev_info(component->dev, "%s: clocks already disabled\n",
  460. __func__);
  461. goto done;
  462. }
  463. /* Gate the CPR clock */
  464. snd_soc_component_update_bits(component, WCD934X_CODEC_RPM_CLK_GATE,
  465. 0x10, 0x10);
  466. /* Disable CPE FLL clock */
  467. ret = wcd_cntl_cpe_fll_ctrl(cntl, false);
  468. if (ret < 0)
  469. dev_err(component->dev,
  470. "%s: Failed to disable cpe clk, err = %d\n",
  471. __func__, ret);
  472. /*
  473. * Even if CPE FLL disable failed, go ahead and disable
  474. * the codec clock
  475. */
  476. if (cntl->cdc_cb && cntl->cdc_cb->cdc_clk_en)
  477. ret = cntl->cdc_cb->cdc_clk_en(component, false);
  478. else
  479. ret = -EINVAL;
  480. cntl->is_clk_enabled = false;
  481. /* Put CPAR in reset */
  482. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
  483. 0x04, 0x04);
  484. done:
  485. WCD_CNTL_MUTEX_UNLOCK(component, cntl->clk_mutex);
  486. return ret;
  487. }
  488. static void wcd_cntl_cpar_ctrl(struct wcd_dsp_cntl *cntl,
  489. bool enable)
  490. {
  491. struct snd_soc_component *component = cntl->component;
  492. if (enable)
  493. snd_soc_component_update_bits(component,
  494. WCD934X_CPE_SS_CPAR_CTL, 0x03, 0x03);
  495. else
  496. snd_soc_component_update_bits(component,
  497. WCD934X_CPE_SS_CPAR_CTL, 0x03, 0x00);
  498. }
  499. static int wcd_cntl_enable_memory(struct wcd_dsp_cntl *cntl,
  500. enum wcd_mem_type mem_type)
  501. {
  502. struct snd_soc_component *component = cntl->component;
  503. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  504. int loop_cnt = 0;
  505. u8 status;
  506. int ret = 0;
  507. switch (mem_type) {
  508. case WCD_MEM_TYPE_ALWAYS_ON:
  509. /* 512KB of always on region */
  510. wcd9xxx_slim_write_repeat(wcd9xxx,
  511. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_0,
  512. ARRAY_SIZE(mem_enable_values),
  513. mem_enable_values);
  514. wcd9xxx_slim_write_repeat(wcd9xxx,
  515. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_1,
  516. ARRAY_SIZE(mem_enable_values),
  517. mem_enable_values);
  518. break;
  519. case WCD_MEM_TYPE_SWITCHABLE:
  520. snd_soc_component_update_bits(component,
  521. WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
  522. 0x04, 0x00);
  523. snd_soc_component_update_bits(component,
  524. WCD934X_TEST_DEBUG_MEM_CTRL,
  525. 0x80, 0x80);
  526. snd_soc_component_update_bits(component,
  527. WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
  528. 0x01, 0x01);
  529. do {
  530. loop_cnt++;
  531. /* Time to enable the power domain for memory */
  532. usleep_range(100, 150);
  533. status = snd_soc_component_read32(component,
  534. WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL);
  535. } while ((status & 0x02) != 0x02 &&
  536. loop_cnt != WCD_MEM_ENABLE_MAX_RETRIES);
  537. if ((status & 0x02) != 0x02) {
  538. dev_err(cntl->component->dev,
  539. "%s: power domain not enabled, status = 0x%02x\n",
  540. __func__, status);
  541. ret = -EIO;
  542. goto done;
  543. }
  544. /* Rest of the memory */
  545. wcd9xxx_slim_write_repeat(wcd9xxx,
  546. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_2,
  547. ARRAY_SIZE(mem_enable_values),
  548. mem_enable_values);
  549. wcd9xxx_slim_write_repeat(wcd9xxx,
  550. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_3,
  551. ARRAY_SIZE(mem_enable_values),
  552. mem_enable_values);
  553. snd_soc_component_write(component,
  554. WCD934X_CPE_SS_PWR_CPE_DRAM1_SHUTDOWN,
  555. 0x05);
  556. break;
  557. default:
  558. dev_err(cntl->component->dev, "%s: Invalid mem_type %d\n",
  559. __func__, mem_type);
  560. ret = -EINVAL;
  561. break;
  562. }
  563. done:
  564. /* Make sure Deep sleep of memories is enabled for all banks */
  565. snd_soc_component_write(component,
  566. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_0, 0xFF);
  567. snd_soc_component_write(component,
  568. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_1, 0x0F);
  569. return ret;
  570. }
  571. static void wcd_cntl_disable_memory(struct wcd_dsp_cntl *cntl,
  572. enum wcd_mem_type mem_type)
  573. {
  574. struct snd_soc_component *component = cntl->component;
  575. u8 val;
  576. switch (mem_type) {
  577. case WCD_MEM_TYPE_ALWAYS_ON:
  578. snd_soc_component_write(component,
  579. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_1,
  580. 0xFF);
  581. snd_soc_component_write(component,
  582. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_0,
  583. 0xFF);
  584. break;
  585. case WCD_MEM_TYPE_SWITCHABLE:
  586. snd_soc_component_write(component,
  587. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_3,
  588. 0xFF);
  589. snd_soc_component_write(component,
  590. WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_2,
  591. 0xFF);
  592. snd_soc_component_write(component,
  593. WCD934X_CPE_SS_PWR_CPE_DRAM1_SHUTDOWN,
  594. 0x07);
  595. snd_soc_component_update_bits(component,
  596. WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
  597. 0x01, 0x00);
  598. val = snd_soc_component_read32(component,
  599. WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL);
  600. if (val & 0x02)
  601. dev_err(component->dev,
  602. "%s: Disable switchable failed, val = 0x%02x",
  603. __func__, val);
  604. snd_soc_component_update_bits(component,
  605. WCD934X_TEST_DEBUG_MEM_CTRL,
  606. 0x80, 0x00);
  607. break;
  608. default:
  609. dev_err(cntl->component->dev, "%s: Invalid mem_type %d\n",
  610. __func__, mem_type);
  611. break;
  612. }
  613. snd_soc_component_write(component,
  614. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_0, 0xFF);
  615. snd_soc_component_write(component,
  616. WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_1, 0x0F);
  617. }
  618. static void wcd_cntl_do_shutdown(struct wcd_dsp_cntl *cntl)
  619. {
  620. struct snd_soc_component *component = cntl->component;
  621. /* Disable WDOG */
  622. snd_soc_component_update_bits(component, WCD934X_CPE_SS_WDOG_CFG,
  623. 0x3F, 0x01);
  624. /* Put WDSP in reset state */
  625. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
  626. 0x02, 0x00);
  627. /* If DSP transitions from boot to shutdown, then vote for SVS */
  628. if (cntl->is_wdsp_booted)
  629. cntl->cdc_cb->cdc_vote_svs(component, true);
  630. cntl->is_wdsp_booted = false;
  631. }
  632. static int wcd_cntl_do_boot(struct wcd_dsp_cntl *cntl)
  633. {
  634. struct snd_soc_component *component = cntl->component;
  635. int ret = 0;
  636. /*
  637. * Debug mode is set from debugfs file node. If debug_mode
  638. * is set, then do not configure the watchdog timer. This
  639. * will be required for debugging the DSP firmware.
  640. */
  641. if (cntl->debug_mode) {
  642. snd_soc_component_update_bits(component,
  643. WCD934X_CPE_SS_WDOG_CFG,
  644. 0x3F, 0x01);
  645. } else {
  646. snd_soc_component_update_bits(component,
  647. WCD934X_CPE_SS_WDOG_CFG,
  648. 0x3F, 0x21);
  649. }
  650. /* Make sure all the error interrupts are cleared */
  651. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_CLEAR_0A,
  652. 0xFF);
  653. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_CLEAR_0B,
  654. 0xFF);
  655. reinit_completion(&cntl->boot_complete);
  656. /* Remove WDSP out of reset */
  657. snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
  658. 0x02, 0x02);
  659. /*
  660. * In debug mode, DSP may not boot up normally,
  661. * wait indefinitely for DSP to boot.
  662. */
  663. if (cntl->debug_mode) {
  664. wait_for_completion(&cntl->boot_complete);
  665. dev_dbg(component->dev, "%s: WDSP booted in dbg mode\n",
  666. __func__);
  667. cntl->is_wdsp_booted = true;
  668. goto done;
  669. }
  670. /* Boot in normal mode */
  671. ret = wait_for_completion_timeout(&cntl->boot_complete,
  672. msecs_to_jiffies(WCD_DSP_BOOT_TIMEOUT_MS));
  673. if (!ret) {
  674. dev_err(component->dev, "%s: WDSP boot timed out\n",
  675. __func__);
  676. if (cntl->dbg_dmp_enable)
  677. wcd_cntl_collect_debug_dumps(cntl, true);
  678. ret = -ETIMEDOUT;
  679. goto err_boot;
  680. } else {
  681. /*
  682. * Re-initialize the return code to 0, as in success case,
  683. * it will hold the remaining time for completion timeout
  684. */
  685. ret = 0;
  686. }
  687. dev_dbg(component->dev, "%s: WDSP booted in normal mode\n", __func__);
  688. cntl->is_wdsp_booted = true;
  689. /* Enable WDOG */
  690. snd_soc_component_update_bits(component, WCD934X_CPE_SS_WDOG_CFG,
  691. 0x10, 0x10);
  692. done:
  693. /* If dsp booted up, then remove vote on SVS */
  694. if (cntl->is_wdsp_booted)
  695. cntl->cdc_cb->cdc_vote_svs(component, false);
  696. return ret;
  697. err_boot:
  698. /* call shutdown to perform cleanup */
  699. wcd_cntl_do_shutdown(cntl);
  700. return ret;
  701. }
  702. static irqreturn_t wcd_cntl_ipc_irq(int irq, void *data)
  703. {
  704. struct wcd_dsp_cntl *cntl = data;
  705. int ret;
  706. complete(&cntl->boot_complete);
  707. if (cntl->m_dev && cntl->m_ops &&
  708. cntl->m_ops->signal_handler)
  709. ret = cntl->m_ops->signal_handler(cntl->m_dev, WDSP_IPC1_INTR,
  710. NULL);
  711. else
  712. ret = -EINVAL;
  713. if (ret < 0)
  714. dev_err(cntl->component->dev,
  715. "%s: Failed to handle irq %d\n", __func__, irq);
  716. return IRQ_HANDLED;
  717. }
  718. static irqreturn_t wcd_cntl_err_irq(int irq, void *data)
  719. {
  720. struct wcd_dsp_cntl *cntl = data;
  721. struct snd_soc_component *component = cntl->component;
  722. struct wdsp_err_signal_arg arg;
  723. u16 status = 0;
  724. u8 reg_val;
  725. int rc, ret = 0;
  726. reg_val = snd_soc_component_read32(component,
  727. WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0A);
  728. status = status | reg_val;
  729. reg_val = snd_soc_component_read32(component,
  730. WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0B);
  731. status = status | (reg_val << 8);
  732. dev_info(component->dev, "%s: error interrupt status = 0x%x\n",
  733. __func__, status);
  734. if ((status & cntl->irqs.fatal_irqs) &&
  735. (cntl->m_dev && cntl->m_ops && cntl->m_ops->signal_handler)) {
  736. /*
  737. * If WDSP SSR happens, skip collecting debug dumps.
  738. * If debug dumps collecting happens first, WDSP_ERR_INTR
  739. * will be blocked in signal_handler and get processed later.
  740. */
  741. rc = WCD_CNTL_SET_ERR_IRQ_FLAG(cntl);
  742. arg.mem_dumps_enabled = cntl->ramdump_enable;
  743. arg.remote_start_addr = WCD_934X_RAMDUMP_START_ADDR;
  744. arg.dump_size = WCD_934X_RAMDUMP_SIZE;
  745. ret = cntl->m_ops->signal_handler(cntl->m_dev, WDSP_ERR_INTR,
  746. &arg);
  747. if (ret < 0)
  748. dev_err(cntl->component->dev,
  749. "%s: Failed to handle fatal irq 0x%x\n",
  750. __func__, status & cntl->irqs.fatal_irqs);
  751. wcd_cntl_change_online_state(cntl, 0);
  752. if (rc == 0)
  753. WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl);
  754. } else {
  755. dev_err(cntl->component->dev, "%s: Invalid signal_handler\n",
  756. __func__);
  757. }
  758. return IRQ_HANDLED;
  759. }
  760. static int wcd_control_handler(struct device *dev, void *priv_data,
  761. enum wdsp_event_type event, void *data)
  762. {
  763. struct wcd_dsp_cntl *cntl = priv_data;
  764. struct snd_soc_component *component = cntl->component;
  765. int ret = 0;
  766. switch (event) {
  767. case WDSP_EVENT_POST_INIT:
  768. case WDSP_EVENT_POST_DLOAD_CODE:
  769. case WDSP_EVENT_DLOAD_FAILED:
  770. case WDSP_EVENT_POST_SHUTDOWN:
  771. /* Disable CPAR */
  772. wcd_cntl_cpar_ctrl(cntl, false);
  773. /* Disable all the clocks */
  774. ret = wcd_cntl_clocks_disable(cntl);
  775. if (ret < 0)
  776. dev_err(component->dev,
  777. "%s: Failed to disable clocks, err = %d\n",
  778. __func__, ret);
  779. if (event == WDSP_EVENT_POST_DLOAD_CODE)
  780. /* Mark DSP online since code download is complete */
  781. wcd_cntl_change_online_state(cntl, 1);
  782. break;
  783. case WDSP_EVENT_PRE_DLOAD_DATA:
  784. case WDSP_EVENT_PRE_DLOAD_CODE:
  785. /* Enable all the clocks */
  786. ret = wcd_cntl_clocks_enable(cntl);
  787. if (ret < 0) {
  788. dev_err(component->dev,
  789. "%s: Failed to enable clocks, err = %d\n",
  790. __func__, ret);
  791. goto done;
  792. }
  793. /* Enable CPAR */
  794. wcd_cntl_cpar_ctrl(cntl, true);
  795. if (event == WDSP_EVENT_PRE_DLOAD_CODE)
  796. wcd_cntl_enable_memory(cntl, WCD_MEM_TYPE_ALWAYS_ON);
  797. else if (event == WDSP_EVENT_PRE_DLOAD_DATA)
  798. wcd_cntl_enable_memory(cntl, WCD_MEM_TYPE_SWITCHABLE);
  799. break;
  800. case WDSP_EVENT_DO_BOOT:
  801. ret = wcd_cntl_do_boot(cntl);
  802. if (ret < 0)
  803. dev_err(component->dev,
  804. "%s: WDSP boot failed, err = %d\n",
  805. __func__, ret);
  806. break;
  807. case WDSP_EVENT_DO_SHUTDOWN:
  808. wcd_cntl_do_shutdown(cntl);
  809. wcd_cntl_disable_memory(cntl, WCD_MEM_TYPE_SWITCHABLE);
  810. break;
  811. default:
  812. dev_dbg(component->dev, "%s: unhandled event %d\n",
  813. __func__, event);
  814. }
  815. done:
  816. return ret;
  817. }
  818. static int wcd_cntl_sysfs_init(char *dir, struct wcd_dsp_cntl *cntl)
  819. {
  820. struct snd_soc_component *component = cntl->component;
  821. int ret = 0;
  822. ret = kobject_init_and_add(&cntl->wcd_kobj, &wcd_cntl_ktype,
  823. kernel_kobj, dir);
  824. if (ret < 0) {
  825. dev_err(component->dev,
  826. "%s: Failed to add kobject %s, err = %d\n",
  827. __func__, dir, ret);
  828. goto done;
  829. }
  830. ret = sysfs_create_file(&cntl->wcd_kobj, &cntl_attr_boot.attr);
  831. if (ret < 0) {
  832. dev_err(component->dev,
  833. "%s: Failed to add wdsp_boot sysfs entry to %s\n",
  834. __func__, dir);
  835. goto fail_create_file;
  836. }
  837. return ret;
  838. fail_create_file:
  839. kobject_put(&cntl->wcd_kobj);
  840. done:
  841. return ret;
  842. }
  843. static void wcd_cntl_sysfs_remove(struct wcd_dsp_cntl *cntl)
  844. {
  845. sysfs_remove_file(&cntl->wcd_kobj, &cntl_attr_boot.attr);
  846. kobject_put(&cntl->wcd_kobj);
  847. }
  848. static void wcd_cntl_debugfs_init(char *dir, struct wcd_dsp_cntl *cntl)
  849. {
  850. struct snd_soc_component *component = cntl->component;
  851. cntl->entry = debugfs_create_dir(dir, NULL);
  852. if (IS_ERR_OR_NULL(dir)) {
  853. dev_err(component->dev, "%s debugfs_create_dir failed for %s\n",
  854. __func__, dir);
  855. goto done;
  856. }
  857. debugfs_create_u32("debug_mode", 0644,
  858. cntl->entry, &cntl->debug_mode);
  859. debugfs_create_bool("ramdump_enable", 0644,
  860. cntl->entry, &cntl->ramdump_enable);
  861. debugfs_create_bool("debug_dump_enable", 0644,
  862. cntl->entry, &cntl->dbg_dmp_enable);
  863. done:
  864. return;
  865. }
  866. static void wcd_cntl_debugfs_remove(struct wcd_dsp_cntl *cntl)
  867. {
  868. if (cntl)
  869. debugfs_remove(cntl->entry);
  870. }
  871. static int wcd_miscdev_release(struct inode *inode, struct file *filep)
  872. {
  873. struct wcd_dsp_cntl *cntl = container_of(filep->private_data,
  874. struct wcd_dsp_cntl, miscdev);
  875. if (!cntl->m_dev || !cntl->m_ops ||
  876. !cntl->m_ops->vote_for_dsp) {
  877. dev_err(cntl->component->dev,
  878. "%s: DSP not ready to boot\n", __func__);
  879. return -EINVAL;
  880. }
  881. /* Make sure the DSP users goes to zero upon closing dev node */
  882. while (cntl->boot_reqs > 0) {
  883. cntl->m_ops->vote_for_dsp(cntl->m_dev, false);
  884. cntl->boot_reqs--;
  885. }
  886. return 0;
  887. }
  888. static ssize_t wcd_miscdev_write(struct file *filep, const char __user *ubuf,
  889. size_t count, loff_t *pos)
  890. {
  891. struct wcd_dsp_cntl *cntl = container_of(filep->private_data,
  892. struct wcd_dsp_cntl, miscdev);
  893. char val[WCD_MISCDEV_CMD_MAX_LEN + 1];
  894. bool vote;
  895. int ret = 0;
  896. memset(val, 0, WCD_MISCDEV_CMD_MAX_LEN + 1);
  897. if (count == 0 || count > WCD_MISCDEV_CMD_MAX_LEN) {
  898. pr_err("%s: Invalid count = %zd\n", __func__, count);
  899. ret = -EINVAL;
  900. goto done;
  901. }
  902. ret = copy_from_user(val, ubuf, count);
  903. if (ret < 0) {
  904. dev_err(cntl->component->dev,
  905. "%s: copy_from_user failed, err = %d\n",
  906. __func__, ret);
  907. ret = -EFAULT;
  908. goto done;
  909. }
  910. if (val[0] == '1') {
  911. cntl->boot_reqs++;
  912. vote = true;
  913. } else if (val[0] == '0') {
  914. if (cntl->boot_reqs == 0) {
  915. dev_err(cntl->component->dev,
  916. "%s: WDSP already disabled\n",
  917. __func__);
  918. ret = -EINVAL;
  919. goto done;
  920. }
  921. cntl->boot_reqs--;
  922. vote = false;
  923. } else if (!strcmp(val, "DEBUG_DUMP")) {
  924. if (cntl->dbg_dmp_enable) {
  925. dev_dbg(cntl->component->dev,
  926. "%s: Collect dumps for debug use\n", __func__);
  927. wcd_cntl_collect_debug_dumps(cntl, false);
  928. }
  929. /*
  930. * simply ignore the request from userspace
  931. * if dbg_dump_enable is not set from debugfs
  932. */
  933. goto done;
  934. } else {
  935. dev_err(cntl->component->dev, "%s: Invalid value %s\n",
  936. __func__, val);
  937. ret = -EINVAL;
  938. goto done;
  939. }
  940. dev_dbg(cntl->component->dev,
  941. "%s: booted = %s, ref_cnt = %d, vote = %s\n",
  942. __func__, cntl->is_wdsp_booted ? "true" : "false",
  943. cntl->boot_reqs, vote ? "true" : "false");
  944. if (cntl->m_dev && cntl->m_ops &&
  945. cntl->m_ops->vote_for_dsp)
  946. ret = cntl->m_ops->vote_for_dsp(cntl->m_dev, vote);
  947. else
  948. ret = -EINVAL;
  949. done:
  950. if (ret)
  951. return ret;
  952. else
  953. return count;
  954. }
  955. static const struct file_operations wcd_miscdev_fops = {
  956. .write = wcd_miscdev_write,
  957. .release = wcd_miscdev_release,
  958. };
  959. static int wcd_cntl_miscdev_create(struct wcd_dsp_cntl *cntl)
  960. {
  961. snprintf(cntl->miscdev_name, ARRAY_SIZE(cntl->miscdev_name),
  962. "wcd_dsp%u_control", cntl->dsp_instance);
  963. cntl->miscdev.minor = MISC_DYNAMIC_MINOR;
  964. cntl->miscdev.name = cntl->miscdev_name;
  965. cntl->miscdev.fops = &wcd_miscdev_fops;
  966. cntl->miscdev.parent = cntl->component->dev;
  967. return misc_register(&cntl->miscdev);
  968. }
  969. static void wcd_cntl_miscdev_destroy(struct wcd_dsp_cntl *cntl)
  970. {
  971. misc_deregister(&cntl->miscdev);
  972. }
  973. static int wcd_control_init(struct device *dev, void *priv_data)
  974. {
  975. struct wcd_dsp_cntl *cntl = priv_data;
  976. struct snd_soc_component *component = cntl->component;
  977. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  978. struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
  979. int ret;
  980. bool err_irq_requested = false;
  981. ret = wcd9xxx_request_irq(core_res,
  982. cntl->irqs.cpe_ipc1_irq,
  983. wcd_cntl_ipc_irq, "CPE IPC1",
  984. cntl);
  985. if (ret < 0) {
  986. dev_err(component->dev,
  987. "%s: Failed to request cpe ipc irq, err = %d\n",
  988. __func__, ret);
  989. goto done;
  990. }
  991. /* Unmask the fatal irqs */
  992. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
  993. ~(cntl->irqs.fatal_irqs & 0xFF));
  994. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
  995. ~((cntl->irqs.fatal_irqs >> 8) & 0xFF));
  996. /*
  997. * CPE ERR irq is used only for error reporting from WCD DSP,
  998. * even if this request fails, DSP can be function normally.
  999. * Continuing with init even if the CPE ERR irq request fails.
  1000. */
  1001. if (wcd9xxx_request_irq(core_res, cntl->irqs.cpe_err_irq,
  1002. wcd_cntl_err_irq, "CPE ERR", cntl))
  1003. dev_info(component->dev, "%s: Failed request_irq(cpe_err_irq)",
  1004. __func__);
  1005. else
  1006. err_irq_requested = true;
  1007. /* Enable all the clocks */
  1008. ret = wcd_cntl_clocks_enable(cntl);
  1009. if (ret < 0) {
  1010. dev_err(component->dev, "%s: Failed to enable clocks, err = %d\n",
  1011. __func__, ret);
  1012. goto err_clk_enable;
  1013. }
  1014. wcd_cntl_cpar_ctrl(cntl, true);
  1015. return 0;
  1016. err_clk_enable:
  1017. /* Mask all error interrupts */
  1018. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
  1019. 0xFF);
  1020. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
  1021. 0xFF);
  1022. /* Free the irq's requested */
  1023. wcd9xxx_free_irq(core_res, cntl->irqs.cpe_ipc1_irq, cntl);
  1024. if (err_irq_requested)
  1025. wcd9xxx_free_irq(core_res, cntl->irqs.cpe_err_irq, cntl);
  1026. done:
  1027. return ret;
  1028. }
  1029. static int wcd_control_deinit(struct device *dev, void *priv_data)
  1030. {
  1031. struct wcd_dsp_cntl *cntl = priv_data;
  1032. struct snd_soc_component *component = cntl->component;
  1033. struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
  1034. struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
  1035. wcd_cntl_clocks_disable(cntl);
  1036. wcd_cntl_cpar_ctrl(cntl, false);
  1037. /* Mask all error interrupts */
  1038. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
  1039. 0xFF);
  1040. snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
  1041. 0xFF);
  1042. /* Free the irq's requested */
  1043. wcd9xxx_free_irq(core_res, cntl->irqs.cpe_err_irq, cntl);
  1044. wcd9xxx_free_irq(core_res, cntl->irqs.cpe_ipc1_irq, cntl);
  1045. return 0;
  1046. }
  1047. static struct wdsp_cmpnt_ops control_ops = {
  1048. .init = wcd_control_init,
  1049. .deinit = wcd_control_deinit,
  1050. .event_handler = wcd_control_handler,
  1051. };
  1052. static int wcd_ctrl_component_bind(struct device *dev,
  1053. struct device *master,
  1054. void *data)
  1055. {
  1056. struct wcd_dsp_cntl *cntl;
  1057. struct snd_soc_component *component;
  1058. struct snd_card *card;
  1059. struct snd_info_entry *entry;
  1060. char proc_name[WCD_PROCFS_ENTRY_MAX_LEN];
  1061. char wcd_cntl_dir_name[WCD_CNTL_DIR_NAME_LEN_MAX];
  1062. int ret = 0;
  1063. if (!dev || !master || !data) {
  1064. pr_err("%s: Invalid parameters\n", __func__);
  1065. return -EINVAL;
  1066. }
  1067. cntl = tavil_get_wcd_dsp_cntl(dev);
  1068. if (!cntl) {
  1069. dev_err(dev, "%s: Failed to get cntl reference\n",
  1070. __func__);
  1071. return -EINVAL;
  1072. }
  1073. cntl->m_dev = master;
  1074. cntl->m_ops = data;
  1075. if (!cntl->m_ops->register_cmpnt_ops) {
  1076. dev_err(dev, "%s: invalid master callback register_cmpnt_ops\n",
  1077. __func__);
  1078. ret = -EINVAL;
  1079. goto done;
  1080. }
  1081. ret = cntl->m_ops->register_cmpnt_ops(master, dev, cntl, &control_ops);
  1082. if (ret) {
  1083. dev_err(dev, "%s: register_cmpnt_ops failed, err = %d\n",
  1084. __func__, ret);
  1085. goto done;
  1086. }
  1087. ret = wcd_cntl_miscdev_create(cntl);
  1088. if (ret < 0) {
  1089. dev_err(dev, "%s: misc dev register failed, err = %d\n",
  1090. __func__, ret);
  1091. goto done;
  1092. }
  1093. snprintf(wcd_cntl_dir_name, WCD_CNTL_DIR_NAME_LEN_MAX,
  1094. "%s%d", "wdsp", cntl->dsp_instance);
  1095. ret = wcd_cntl_sysfs_init(wcd_cntl_dir_name, cntl);
  1096. if (ret < 0) {
  1097. dev_err(dev, "%s: sysfs_init failed, err = %d\n",
  1098. __func__, ret);
  1099. goto err_sysfs_init;
  1100. }
  1101. wcd_cntl_debugfs_init(wcd_cntl_dir_name, cntl);
  1102. component = cntl->component;
  1103. card = component->card->snd_card;
  1104. snprintf(proc_name, WCD_PROCFS_ENTRY_MAX_LEN, "%s%d%s", "cpe",
  1105. cntl->dsp_instance, "_state");
  1106. entry = snd_info_create_card_entry(card, proc_name, card->proc_root);
  1107. if (!entry) {
  1108. /* Do not treat this as Fatal error */
  1109. dev_err(dev, "%s: Failed to create procfs entry %s\n",
  1110. __func__, proc_name);
  1111. goto err_sysfs_init;
  1112. }
  1113. cntl->ssr_entry.entry = entry;
  1114. cntl->ssr_entry.offline = 1;
  1115. entry->size = WCD_PROCFS_ENTRY_MAX_LEN;
  1116. entry->content = SNDRV_INFO_CONTENT_DATA;
  1117. entry->c.ops = &wdsp_ssr_entry_ops;
  1118. entry->private_data = cntl;
  1119. ret = snd_info_register(entry);
  1120. if (ret < 0) {
  1121. dev_err(dev, "%s: Failed to register entry %s, err = %d\n",
  1122. __func__, proc_name, ret);
  1123. snd_info_free_entry(entry);
  1124. /* Let bind still happen even if creating the entry failed */
  1125. ret = 0;
  1126. }
  1127. done:
  1128. return ret;
  1129. err_sysfs_init:
  1130. wcd_cntl_miscdev_destroy(cntl);
  1131. return ret;
  1132. }
  1133. static void wcd_ctrl_component_unbind(struct device *dev,
  1134. struct device *master,
  1135. void *data)
  1136. {
  1137. struct wcd_dsp_cntl *cntl;
  1138. if (!dev) {
  1139. pr_err("%s: Invalid device\n", __func__);
  1140. return;
  1141. }
  1142. cntl = tavil_get_wcd_dsp_cntl(dev);
  1143. if (!cntl) {
  1144. dev_err(dev, "%s: Failed to get cntl reference\n",
  1145. __func__);
  1146. return;
  1147. }
  1148. cntl->m_dev = NULL;
  1149. cntl->m_ops = NULL;
  1150. /* Remove the sysfs entries */
  1151. wcd_cntl_sysfs_remove(cntl);
  1152. /* Remove the debugfs entries */
  1153. wcd_cntl_debugfs_remove(cntl);
  1154. /* Remove the misc device */
  1155. wcd_cntl_miscdev_destroy(cntl);
  1156. }
  1157. static const struct component_ops wcd_ctrl_component_ops = {
  1158. .bind = wcd_ctrl_component_bind,
  1159. .unbind = wcd_ctrl_component_unbind,
  1160. };
  1161. /*
  1162. * wcd_dsp_ssr_event: handle the SSR event raised by caller.
  1163. * @cntl: Handle to the wcd_dsp_cntl structure
  1164. * @event: The SSR event to be handled
  1165. *
  1166. * Notifies the manager driver about the SSR event.
  1167. * Returns 0 on success and negative error code on error.
  1168. */
  1169. int wcd_dsp_ssr_event(struct wcd_dsp_cntl *cntl, enum cdc_ssr_event event)
  1170. {
  1171. int ret = 0;
  1172. if (!cntl) {
  1173. pr_err("%s: Invalid handle to control\n", __func__);
  1174. return -EINVAL;
  1175. }
  1176. if (!cntl->m_dev || !cntl->m_ops || !cntl->m_ops->signal_handler) {
  1177. dev_err(cntl->component->dev,
  1178. "%s: Invalid signal_handler callback\n", __func__);
  1179. return -EINVAL;
  1180. }
  1181. switch (event) {
  1182. case WCD_CDC_DOWN_EVENT:
  1183. ret = cntl->m_ops->signal_handler(cntl->m_dev,
  1184. WDSP_CDC_DOWN_SIGNAL,
  1185. NULL);
  1186. if (ret < 0)
  1187. dev_err(cntl->component->dev,
  1188. "%s: WDSP_CDC_DOWN_SIGNAL failed, err = %d\n",
  1189. __func__, ret);
  1190. wcd_cntl_change_online_state(cntl, 0);
  1191. break;
  1192. case WCD_CDC_UP_EVENT:
  1193. ret = cntl->m_ops->signal_handler(cntl->m_dev,
  1194. WDSP_CDC_UP_SIGNAL,
  1195. NULL);
  1196. if (ret < 0)
  1197. dev_err(cntl->component->dev,
  1198. "%s: WDSP_CDC_UP_SIGNAL failed, err = %d\n",
  1199. __func__, ret);
  1200. break;
  1201. default:
  1202. dev_err(cntl->component->dev, "%s: Invalid event %d\n",
  1203. __func__, event);
  1204. ret = -EINVAL;
  1205. break;
  1206. }
  1207. return ret;
  1208. }
  1209. EXPORT_SYMBOL(wcd_dsp_ssr_event);
  1210. /*
  1211. * wcd_dsp_cntl_init: Initialize the wcd-dsp control
  1212. * @component: pointer to the codec component handle
  1213. * @params: Parameters required to initialize wcd-dsp control
  1214. *
  1215. * This API is expected to be invoked by the codec driver and
  1216. * provide information essential for the wcd dsp control to
  1217. * configure and initialize the dsp
  1218. */
  1219. void wcd_dsp_cntl_init(struct snd_soc_component *component,
  1220. struct wcd_dsp_params *params,
  1221. struct wcd_dsp_cntl **cntl)
  1222. {
  1223. struct wcd_dsp_cntl *control;
  1224. int ret;
  1225. if (!component || !params) {
  1226. pr_err("%s: Invalid handle to %s\n", __func__,
  1227. (!component) ? "component" : "params");
  1228. *cntl = NULL;
  1229. return;
  1230. }
  1231. if (*cntl) {
  1232. pr_err("%s: cntl is non NULL, maybe already initialized ?\n",
  1233. __func__);
  1234. return;
  1235. }
  1236. if (!params->cb || !params->cb->cdc_clk_en ||
  1237. !params->cb->cdc_vote_svs) {
  1238. dev_err(component->dev,
  1239. "%s: clk_en and vote_svs callbacks must be provided\n",
  1240. __func__);
  1241. return;
  1242. }
  1243. control = kzalloc(sizeof(*control), GFP_KERNEL);
  1244. if (!(control))
  1245. return;
  1246. control->component = component;
  1247. control->clk_rate = params->clk_rate;
  1248. control->cdc_cb = params->cb;
  1249. control->dsp_instance = params->dsp_instance;
  1250. memcpy(&control->irqs, &params->irqs, sizeof(control->irqs));
  1251. init_completion(&control->boot_complete);
  1252. mutex_init(&control->clk_mutex);
  1253. mutex_init(&control->ssr_mutex);
  1254. init_waitqueue_head(&control->ssr_entry.offline_poll_wait);
  1255. WCD_CNTL_CLR_ERR_IRQ_FLAG(control);
  1256. /*
  1257. * The default state of WDSP is in SVS mode.
  1258. * Vote for SVS now, the vote will be removed only
  1259. * after DSP is booted up.
  1260. */
  1261. control->cdc_cb->cdc_vote_svs(component, true);
  1262. /*
  1263. * If this is the last component needed by master to be ready,
  1264. * then component_bind will be called within the component_add.
  1265. * Hence, the data pointer should be assigned before component_add,
  1266. * so that we can access it during this component's bind call.
  1267. */
  1268. *cntl = control;
  1269. ret = component_add(component->dev, &wcd_ctrl_component_ops);
  1270. if (ret) {
  1271. dev_err(component->dev, "%s: component_add failed, err = %d\n",
  1272. __func__, ret);
  1273. kfree(*cntl);
  1274. *cntl = NULL;
  1275. }
  1276. }
  1277. EXPORT_SYMBOL(wcd_dsp_cntl_init);
  1278. /*
  1279. * wcd_dsp_cntl_deinit: De-initialize the wcd-dsp control
  1280. * @cntl: The struct wcd_dsp_cntl to de-initialize
  1281. *
  1282. * This API is intended to be invoked by the codec driver
  1283. * to de-initialize the wcd dsp control
  1284. */
  1285. void wcd_dsp_cntl_deinit(struct wcd_dsp_cntl **cntl)
  1286. {
  1287. struct wcd_dsp_cntl *control = *cntl;
  1288. struct snd_soc_component *component;
  1289. /* If control is NULL, there is nothing to de-initialize */
  1290. if (!control)
  1291. return;
  1292. component = control->component;
  1293. /*
  1294. * Calling shutdown will cleanup all register states,
  1295. * irrespective of DSP was booted up or not.
  1296. */
  1297. wcd_cntl_do_shutdown(control);
  1298. wcd_cntl_disable_memory(control, WCD_MEM_TYPE_SWITCHABLE);
  1299. wcd_cntl_disable_memory(control, WCD_MEM_TYPE_ALWAYS_ON);
  1300. component_del(component->dev, &wcd_ctrl_component_ops);
  1301. mutex_destroy(&control->clk_mutex);
  1302. mutex_destroy(&control->ssr_mutex);
  1303. kfree(*cntl);
  1304. *cntl = NULL;
  1305. }
  1306. EXPORT_SYMBOL(wcd_dsp_cntl_deinit);