12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
- */
- #include <linux/slab.h>
- #include <linux/delay.h>
- #include <linux/component.h>
- #include <linux/debugfs.h>
- #include <sound/soc.h>
- #include <sound/wcd-dsp-mgr.h>
- #include <asoc/wcd934x_registers.h>
- #include "wcd934x.h"
- #include "wcd934x-dsp-cntl.h"
- #include <asoc/wcd9xxx-irq.h>
- #include <asoc/core.h>
- #define WCD_CNTL_DIR_NAME_LEN_MAX 32
- #define WCD_CPE_FLL_MAX_RETRIES 5
- #define WCD_MEM_ENABLE_MAX_RETRIES 20
- #define WCD_DSP_BOOT_TIMEOUT_MS 3000
- #define WCD_SYSFS_ENTRY_MAX_LEN 8
- #define WCD_PROCFS_ENTRY_MAX_LEN 16
- #define WCD_934X_RAMDUMP_START_ADDR 0x20100000
- #define WCD_934X_RAMDUMP_SIZE ((1024 * 1024) - 128)
- #define WCD_MISCDEV_CMD_MAX_LEN 11
- #define WCD_CNTL_MUTEX_LOCK(component, lock) \
- { \
- dev_dbg(component->dev, "%s: mutex_lock(%s)\n", \
- __func__, __stringify_1(lock)); \
- mutex_lock(&lock); \
- }
- #define WCD_CNTL_MUTEX_UNLOCK(component, lock) \
- { \
- dev_dbg(component->dev, "%s: mutex_unlock(%s)\n", \
- __func__, __stringify_1(lock)); \
- mutex_unlock(&lock); \
- }
- enum wcd_mem_type {
- WCD_MEM_TYPE_ALWAYS_ON,
- WCD_MEM_TYPE_SWITCHABLE,
- };
- struct wcd_cntl_attribute {
- struct attribute attr;
- ssize_t (*show)(struct wcd_dsp_cntl *cntl, char *buf);
- ssize_t (*store)(struct wcd_dsp_cntl *cntl, const char *buf,
- ssize_t count);
- };
- #define WCD_CNTL_ATTR(_name, _mode, _show, _store) \
- static struct wcd_cntl_attribute cntl_attr_##_name = { \
- .attr = {.name = __stringify(_name), .mode = _mode}, \
- .show = _show, \
- .store = _store, \
- }
- #define to_wcd_cntl_attr(a) \
- container_of((a), struct wcd_cntl_attribute, attr)
- #define to_wcd_cntl(kobj) \
- container_of((kobj), struct wcd_dsp_cntl, wcd_kobj)
- static u8 mem_enable_values[] = {
- 0xFE, 0xFC, 0xF8, 0xF0,
- 0xE0, 0xC0, 0x80, 0x00,
- };
- #ifdef CONFIG_DEBUG_FS
- #define WCD_CNTL_SET_ERR_IRQ_FLAG(cntl)\
- atomic_cmpxchg(&cntl->err_irq_flag, 0, 1)
- #define WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl)\
- atomic_set(&cntl->err_irq_flag, 0)
- static u16 wdsp_reg_for_debug_dump[] = {
- WCD934X_CPE_SS_CPE_CTL,
- WCD934X_CPE_SS_PWR_SYS_PSTATE_CTL_0,
- WCD934X_CPE_SS_PWR_SYS_PSTATE_CTL_1,
- WCD934X_CPE_SS_PWR_CPEFLL_CTL,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_0,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_1,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_OVERRIDE,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_0,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_1,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_2,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_3,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_4,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_5,
- WCD934X_CPE_SS_PWR_CPE_DRAM1_SHUTDOWN,
- WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
- WCD934X_CPE_SS_MAD_CTL,
- WCD934X_CPE_SS_CPAR_CTL,
- WCD934X_CPE_SS_CPAR_CFG,
- WCD934X_CPE_SS_WDOG_CFG,
- WCD934X_CPE_SS_STATUS,
- WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
- WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
- WCD934X_CPE_SS_SS_ERROR_INT_MASK_1A,
- WCD934X_CPE_SS_SS_ERROR_INT_MASK_1B,
- WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0A,
- WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0B,
- WCD934X_CPE_SS_SS_ERROR_INT_STATUS_1A,
- WCD934X_CPE_SS_SS_ERROR_INT_STATUS_1B,
- };
- static void wcd_cntl_collect_debug_dumps(struct wcd_dsp_cntl *cntl,
- bool internal)
- {
- struct snd_soc_component *component = cntl->component;
- struct wdsp_err_signal_arg arg;
- enum wdsp_signal signal;
- int i;
- u8 val;
- /* If WDSP SSR happens, skip collecting debug dumps */
- if (WCD_CNTL_SET_ERR_IRQ_FLAG(cntl) != 0)
- return;
- /* Mask all error interrupts */
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
- 0xFF);
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
- 0xFF);
- /* Collect important WDSP registers dump for debug use */
- pr_err("%s: Dump the WDSP registers for debug use\n", __func__);
- for (i = 0; i < sizeof(wdsp_reg_for_debug_dump)/sizeof(u16); i++) {
- val = snd_soc_component_read32(component,
- wdsp_reg_for_debug_dump[i]);
- pr_err("%s: reg = 0x%x, val = 0x%x\n", __func__,
- wdsp_reg_for_debug_dump[i], val);
- }
- /* Trigger NMI in WDSP to sync and update the memory */
- snd_soc_component_write(component, WCD934X_CPE_SS_BACKUP_INT, 0x02);
- /* Collect WDSP ramdump for debug use */
- if (cntl->m_dev && cntl->m_ops && cntl->m_ops->signal_handler) {
- arg.mem_dumps_enabled = cntl->ramdump_enable;
- arg.remote_start_addr = WCD_934X_RAMDUMP_START_ADDR;
- arg.dump_size = WCD_934X_RAMDUMP_SIZE;
- signal = internal ? WDSP_DEBUG_DUMP_INTERNAL : WDSP_DEBUG_DUMP;
- cntl->m_ops->signal_handler(cntl->m_dev, signal, &arg);
- }
- /* Unmask the fatal irqs */
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
- ~(cntl->irqs.fatal_irqs & 0xFF));
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
- ~((cntl->irqs.fatal_irqs >> 8) & 0xFF));
- WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl);
- }
- #else
- #define WCD_CNTL_SET_ERR_IRQ_FLAG(cntl) 0
- #define WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl) do {} while (0)
- static void wcd_cntl_collect_debug_dumps(struct wcd_dsp_cntl *cntl,
- bool internal)
- {
- }
- #endif
- static ssize_t wdsp_boot_show(struct wcd_dsp_cntl *cntl, char *buf)
- {
- return snprintf(buf, WCD_SYSFS_ENTRY_MAX_LEN,
- "%u", cntl->boot_reqs);
- }
- static ssize_t wdsp_boot_store(struct wcd_dsp_cntl *cntl,
- const char *buf, ssize_t count)
- {
- u32 val;
- bool vote;
- int ret;
- ret = kstrtou32(buf, 10, &val);
- if (ret) {
- dev_err(cntl->component->dev,
- "%s: Invalid entry, ret = %d\n", __func__, ret);
- return -EINVAL;
- }
- if (val > 0) {
- cntl->boot_reqs++;
- vote = true;
- } else {
- cntl->boot_reqs--;
- vote = false;
- }
- if (cntl->m_dev && cntl->m_ops &&
- cntl->m_ops->vote_for_dsp)
- ret = cntl->m_ops->vote_for_dsp(cntl->m_dev, vote);
- else
- ret = -EINVAL;
- if (ret < 0)
- dev_err(cntl->component->dev,
- "%s: failed to %s dsp\n", __func__,
- vote ? "enable" : "disable");
- return count;
- }
- WCD_CNTL_ATTR(boot, 0660, wdsp_boot_show, wdsp_boot_store);
- static ssize_t wcd_cntl_sysfs_show(struct kobject *kobj,
- struct attribute *attr, char *buf)
- {
- struct wcd_cntl_attribute *wcd_attr = to_wcd_cntl_attr(attr);
- struct wcd_dsp_cntl *cntl = to_wcd_cntl(kobj);
- ssize_t ret = -EINVAL;
- if (cntl && wcd_attr->show)
- ret = wcd_attr->show(cntl, buf);
- return ret;
- }
- static ssize_t wcd_cntl_sysfs_store(struct kobject *kobj,
- struct attribute *attr, const char *buf,
- size_t count)
- {
- struct wcd_cntl_attribute *wcd_attr = to_wcd_cntl_attr(attr);
- struct wcd_dsp_cntl *cntl = to_wcd_cntl(kobj);
- ssize_t ret = -EINVAL;
- if (cntl && wcd_attr->store)
- ret = wcd_attr->store(cntl, buf, count);
- return ret;
- }
- static const struct sysfs_ops wcd_cntl_sysfs_ops = {
- .show = wcd_cntl_sysfs_show,
- .store = wcd_cntl_sysfs_store,
- };
- static struct kobj_type wcd_cntl_ktype = {
- .sysfs_ops = &wcd_cntl_sysfs_ops,
- };
- static void wcd_cntl_change_online_state(struct wcd_dsp_cntl *cntl,
- u8 online)
- {
- struct wdsp_ssr_entry *ssr_entry = &cntl->ssr_entry;
- unsigned long ret;
- WCD_CNTL_MUTEX_LOCK(cntl->component, cntl->ssr_mutex);
- ssr_entry->offline = !online;
- /* Make sure the write is complete */
- wmb();
- ret = xchg(&ssr_entry->offline_change, 1);
- wake_up_interruptible(&ssr_entry->offline_poll_wait);
- dev_dbg(cntl->component->dev,
- "%s: requested %u, offline %u offline_change %u, ret = %ldn",
- __func__, online, ssr_entry->offline,
- ssr_entry->offline_change, ret);
- WCD_CNTL_MUTEX_UNLOCK(cntl->component, cntl->ssr_mutex);
- }
- static ssize_t wdsp_ssr_entry_read(struct snd_info_entry *entry,
- void *file_priv_data, struct file *file,
- char __user *buf, size_t count, loff_t pos)
- {
- int len = 0;
- char buffer[WCD_PROCFS_ENTRY_MAX_LEN];
- struct wcd_dsp_cntl *cntl;
- struct wdsp_ssr_entry *ssr_entry;
- ssize_t ret;
- u8 offline;
- cntl = (struct wcd_dsp_cntl *) entry->private_data;
- if (!cntl) {
- pr_err("%s: Invalid private data for SSR procfs entry\n",
- __func__);
- return -EINVAL;
- }
- ssr_entry = &cntl->ssr_entry;
- WCD_CNTL_MUTEX_LOCK(cntl->component, cntl->ssr_mutex);
- offline = ssr_entry->offline;
- /* Make sure the read is complete */
- rmb();
- dev_dbg(cntl->component->dev, "%s: offline = %s\n", __func__,
- offline ? "true" : "false");
- len = snprintf(buffer, sizeof(buffer), "%s\n",
- offline ? "OFFLINE" : "ONLINE");
- ret = simple_read_from_buffer(buf, count, &pos, buffer, len);
- WCD_CNTL_MUTEX_UNLOCK(cntl->component, cntl->ssr_mutex);
- return ret;
- }
- static unsigned int wdsp_ssr_entry_poll(struct snd_info_entry *entry,
- void *private_data, struct file *file,
- poll_table *wait)
- {
- struct wcd_dsp_cntl *cntl;
- struct wdsp_ssr_entry *ssr_entry;
- unsigned int ret = 0;
- if (!entry || !entry->private_data) {
- pr_err("%s: %s is NULL\n", __func__,
- (!entry) ? "entry" : "private_data");
- return -EINVAL;
- }
- cntl = (struct wcd_dsp_cntl *) entry->private_data;
- ssr_entry = &cntl->ssr_entry;
- dev_dbg(cntl->component->dev, "%s: Poll wait, offline = %u\n",
- __func__, ssr_entry->offline);
- poll_wait(file, &ssr_entry->offline_poll_wait, wait);
- dev_dbg(cntl->component->dev, "%s: Woken up Poll wait, offline = %u\n",
- __func__, ssr_entry->offline);
- WCD_CNTL_MUTEX_LOCK(cntl->component, cntl->ssr_mutex);
- if (xchg(&ssr_entry->offline_change, 0))
- ret = POLLIN | POLLPRI | POLLRDNORM;
- dev_dbg(cntl->component->dev, "%s: ret (%d) from poll_wait\n",
- __func__, ret);
- WCD_CNTL_MUTEX_UNLOCK(cntl->component, cntl->ssr_mutex);
- return ret;
- }
- static struct snd_info_entry_ops wdsp_ssr_entry_ops = {
- .read = wdsp_ssr_entry_read,
- .poll = wdsp_ssr_entry_poll,
- };
- static int wcd_cntl_cpe_fll_calibrate(struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- int ret = 0, retry = 0;
- u8 cal_lsb, cal_msb;
- u8 lock_det;
- /* Make sure clocks are gated */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
- 0x05, 0x00);
- /* Enable CPE FLL reference clock */
- snd_soc_component_update_bits(component, WCD934X_CLK_SYS_MCLK2_PRG1,
- 0x80, 0x80);
- snd_soc_component_update_bits(component, WCD934X_CPE_FLL_USER_CTL_5,
- 0xF3, 0x13);
- snd_soc_component_write(component, WCD934X_CPE_FLL_L_VAL_CTL_0, 0x50);
- /* Disable CPAR reset and Enable CPAR clk */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
- 0x02, 0x02);
- /* Write calibration l-value based on cdc clk rate */
- if (cntl->clk_rate == 9600000) {
- cal_lsb = 0x6d;
- cal_msb = 0x00;
- } else {
- cal_lsb = 0x56;
- cal_msb = 0x00;
- }
- snd_soc_component_write(component, WCD934X_CPE_FLL_USER_CTL_6, cal_lsb);
- snd_soc_component_write(component, WCD934X_CPE_FLL_USER_CTL_7, cal_msb);
- /* FLL mode to follow power up sequence */
- snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
- 0x60, 0x00);
- /* HW controlled CPE FLL */
- snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
- 0x80, 0x80);
- /* Force on CPE FLL */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CFG,
- 0x04, 0x04);
- do {
- /* Time for FLL calibration to complete */
- usleep_range(1000, 1100);
- lock_det = snd_soc_component_read32(
- component, WCD934X_CPE_FLL_STATUS_3);
- retry++;
- } while (!(lock_det & 0x01) &&
- retry <= WCD_CPE_FLL_MAX_RETRIES);
- if (!(lock_det & 0x01)) {
- dev_err(component->dev, "%s: lock detect not set, 0x%02x\n",
- __func__, lock_det);
- ret = -EIO;
- goto err_lock_det;
- }
- snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
- 0x60, 0x20);
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CFG,
- 0x04, 0x00);
- return ret;
- err_lock_det:
- /* Undo the register settings */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CFG,
- 0x04, 0x00);
- snd_soc_component_update_bits(component, WCD934X_CPE_FLL_FLL_MODE,
- 0x80, 0x00);
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
- 0x02, 0x00);
- return ret;
- }
- static void wcd_cntl_config_cpar(struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- u8 nom_lo, nom_hi, svs2_lo, svs2_hi;
- /* Configure CPAR */
- nom_hi = svs2_hi = 0;
- if (cntl->clk_rate == 9600000) {
- nom_lo = 0x90;
- svs2_lo = 0x50;
- } else {
- nom_lo = 0x70;
- svs2_lo = 0x3e;
- }
- snd_soc_component_write(component, WCD934X_TEST_DEBUG_LVAL_NOM_LOW,
- nom_lo);
- snd_soc_component_write(component, WCD934X_TEST_DEBUG_LVAL_NOM_HIGH,
- nom_hi);
- snd_soc_component_write(component, WCD934X_TEST_DEBUG_LVAL_SVS_SVS2_LOW,
- svs2_lo);
- snd_soc_component_write(component,
- WCD934X_TEST_DEBUG_LVAL_SVS_SVS2_HIGH,
- svs2_hi);
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_PWR_CPEFLL_CTL,
- 0x03, 0x03);
- }
- static int wcd_cntl_cpe_fll_ctrl(struct wcd_dsp_cntl *cntl,
- bool enable)
- {
- struct snd_soc_component *component = cntl->component;
- int ret = 0;
- if (enable) {
- ret = wcd_cntl_cpe_fll_calibrate(cntl);
- if (ret < 0) {
- dev_err(component->dev,
- "%s: cpe_fll_cal failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- wcd_cntl_config_cpar(cntl);
- /* Enable AHB CLK and CPE CLK*/
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
- 0x05, 0x05);
- } else {
- /* Disable AHB CLK and CPE CLK */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
- 0x05, 0x00);
- /* Reset the CPAR mode for CPE FLL */
- snd_soc_component_write(component, WCD934X_CPE_FLL_FLL_MODE,
- 0x20);
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_CPAR_CFG,
- 0x04, 0x00);
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_CPAR_CTL,
- 0x02, 0x00);
- }
- done:
- return ret;
- }
- static int wcd_cntl_clocks_enable(struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- int ret;
- WCD_CNTL_MUTEX_LOCK(component, cntl->clk_mutex);
- /* Enable codec clock */
- if (cntl->cdc_cb && cntl->cdc_cb->cdc_clk_en)
- ret = cntl->cdc_cb->cdc_clk_en(component, true);
- else
- ret = -EINVAL;
- if (ret < 0) {
- dev_err(component->dev,
- "%s: Failed to enable cdc clk, err = %d\n",
- __func__, ret);
- goto done;
- }
- /* Pull CPAR out of reset */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
- 0x04, 0x00);
- /* Configure and Enable CPE FLL clock */
- ret = wcd_cntl_cpe_fll_ctrl(cntl, true);
- if (ret < 0) {
- dev_err(component->dev,
- "%s: Failed to enable cpe clk, err = %d\n",
- __func__, ret);
- goto err_cpe_clk;
- }
- cntl->is_clk_enabled = true;
- /* Ungate the CPR clock */
- snd_soc_component_update_bits(component, WCD934X_CODEC_RPM_CLK_GATE,
- 0x10, 0x00);
- done:
- WCD_CNTL_MUTEX_UNLOCK(component, cntl->clk_mutex);
- return ret;
- err_cpe_clk:
- if (cntl->cdc_cb && cntl->cdc_cb->cdc_clk_en)
- cntl->cdc_cb->cdc_clk_en(component, false);
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
- 0x04, 0x04);
- WCD_CNTL_MUTEX_UNLOCK(component, cntl->clk_mutex);
- return ret;
- }
- static int wcd_cntl_clocks_disable(struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- int ret = 0;
- WCD_CNTL_MUTEX_LOCK(component, cntl->clk_mutex);
- if (!cntl->is_clk_enabled) {
- dev_info(component->dev, "%s: clocks already disabled\n",
- __func__);
- goto done;
- }
- /* Gate the CPR clock */
- snd_soc_component_update_bits(component, WCD934X_CODEC_RPM_CLK_GATE,
- 0x10, 0x10);
- /* Disable CPE FLL clock */
- ret = wcd_cntl_cpe_fll_ctrl(cntl, false);
- if (ret < 0)
- dev_err(component->dev,
- "%s: Failed to disable cpe clk, err = %d\n",
- __func__, ret);
- /*
- * Even if CPE FLL disable failed, go ahead and disable
- * the codec clock
- */
- if (cntl->cdc_cb && cntl->cdc_cb->cdc_clk_en)
- ret = cntl->cdc_cb->cdc_clk_en(component, false);
- else
- ret = -EINVAL;
- cntl->is_clk_enabled = false;
- /* Put CPAR in reset */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPAR_CTL,
- 0x04, 0x04);
- done:
- WCD_CNTL_MUTEX_UNLOCK(component, cntl->clk_mutex);
- return ret;
- }
- static void wcd_cntl_cpar_ctrl(struct wcd_dsp_cntl *cntl,
- bool enable)
- {
- struct snd_soc_component *component = cntl->component;
- if (enable)
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_CPAR_CTL, 0x03, 0x03);
- else
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_CPAR_CTL, 0x03, 0x00);
- }
- static int wcd_cntl_enable_memory(struct wcd_dsp_cntl *cntl,
- enum wcd_mem_type mem_type)
- {
- struct snd_soc_component *component = cntl->component;
- struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
- int loop_cnt = 0;
- u8 status;
- int ret = 0;
- switch (mem_type) {
- case WCD_MEM_TYPE_ALWAYS_ON:
- /* 512KB of always on region */
- wcd9xxx_slim_write_repeat(wcd9xxx,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_0,
- ARRAY_SIZE(mem_enable_values),
- mem_enable_values);
- wcd9xxx_slim_write_repeat(wcd9xxx,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_1,
- ARRAY_SIZE(mem_enable_values),
- mem_enable_values);
- break;
- case WCD_MEM_TYPE_SWITCHABLE:
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
- 0x04, 0x00);
- snd_soc_component_update_bits(component,
- WCD934X_TEST_DEBUG_MEM_CTRL,
- 0x80, 0x80);
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
- 0x01, 0x01);
- do {
- loop_cnt++;
- /* Time to enable the power domain for memory */
- usleep_range(100, 150);
- status = snd_soc_component_read32(component,
- WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL);
- } while ((status & 0x02) != 0x02 &&
- loop_cnt != WCD_MEM_ENABLE_MAX_RETRIES);
- if ((status & 0x02) != 0x02) {
- dev_err(cntl->component->dev,
- "%s: power domain not enabled, status = 0x%02x\n",
- __func__, status);
- ret = -EIO;
- goto done;
- }
- /* Rest of the memory */
- wcd9xxx_slim_write_repeat(wcd9xxx,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_2,
- ARRAY_SIZE(mem_enable_values),
- mem_enable_values);
- wcd9xxx_slim_write_repeat(wcd9xxx,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_3,
- ARRAY_SIZE(mem_enable_values),
- mem_enable_values);
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_DRAM1_SHUTDOWN,
- 0x05);
- break;
- default:
- dev_err(cntl->component->dev, "%s: Invalid mem_type %d\n",
- __func__, mem_type);
- ret = -EINVAL;
- break;
- }
- done:
- /* Make sure Deep sleep of memories is enabled for all banks */
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_0, 0xFF);
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_1, 0x0F);
- return ret;
- }
- static void wcd_cntl_disable_memory(struct wcd_dsp_cntl *cntl,
- enum wcd_mem_type mem_type)
- {
- struct snd_soc_component *component = cntl->component;
- u8 val;
- switch (mem_type) {
- case WCD_MEM_TYPE_ALWAYS_ON:
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_1,
- 0xFF);
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_0,
- 0xFF);
- break;
- case WCD_MEM_TYPE_SWITCHABLE:
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_3,
- 0xFF);
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_SHUTDOWN_2,
- 0xFF);
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_DRAM1_SHUTDOWN,
- 0x07);
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL,
- 0x01, 0x00);
- val = snd_soc_component_read32(component,
- WCD934X_CPE_SS_SOC_SW_COLLAPSE_CTL);
- if (val & 0x02)
- dev_err(component->dev,
- "%s: Disable switchable failed, val = 0x%02x",
- __func__, val);
- snd_soc_component_update_bits(component,
- WCD934X_TEST_DEBUG_MEM_CTRL,
- 0x80, 0x00);
- break;
- default:
- dev_err(cntl->component->dev, "%s: Invalid mem_type %d\n",
- __func__, mem_type);
- break;
- }
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_0, 0xFF);
- snd_soc_component_write(component,
- WCD934X_CPE_SS_PWR_CPE_SYSMEM_DEEPSLP_1, 0x0F);
- }
- static void wcd_cntl_do_shutdown(struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- /* Disable WDOG */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_WDOG_CFG,
- 0x3F, 0x01);
- /* Put WDSP in reset state */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
- 0x02, 0x00);
- /* If DSP transitions from boot to shutdown, then vote for SVS */
- if (cntl->is_wdsp_booted)
- cntl->cdc_cb->cdc_vote_svs(component, true);
- cntl->is_wdsp_booted = false;
- }
- static int wcd_cntl_do_boot(struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- int ret = 0;
- /*
- * Debug mode is set from debugfs file node. If debug_mode
- * is set, then do not configure the watchdog timer. This
- * will be required for debugging the DSP firmware.
- */
- if (cntl->debug_mode) {
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_WDOG_CFG,
- 0x3F, 0x01);
- } else {
- snd_soc_component_update_bits(component,
- WCD934X_CPE_SS_WDOG_CFG,
- 0x3F, 0x21);
- }
- /* Make sure all the error interrupts are cleared */
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_CLEAR_0A,
- 0xFF);
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_CLEAR_0B,
- 0xFF);
- reinit_completion(&cntl->boot_complete);
- /* Remove WDSP out of reset */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_CPE_CTL,
- 0x02, 0x02);
- /*
- * In debug mode, DSP may not boot up normally,
- * wait indefinitely for DSP to boot.
- */
- if (cntl->debug_mode) {
- wait_for_completion(&cntl->boot_complete);
- dev_dbg(component->dev, "%s: WDSP booted in dbg mode\n",
- __func__);
- cntl->is_wdsp_booted = true;
- goto done;
- }
- /* Boot in normal mode */
- ret = wait_for_completion_timeout(&cntl->boot_complete,
- msecs_to_jiffies(WCD_DSP_BOOT_TIMEOUT_MS));
- if (!ret) {
- dev_err(component->dev, "%s: WDSP boot timed out\n",
- __func__);
- if (cntl->dbg_dmp_enable)
- wcd_cntl_collect_debug_dumps(cntl, true);
- ret = -ETIMEDOUT;
- goto err_boot;
- } else {
- /*
- * Re-initialize the return code to 0, as in success case,
- * it will hold the remaining time for completion timeout
- */
- ret = 0;
- }
- dev_dbg(component->dev, "%s: WDSP booted in normal mode\n", __func__);
- cntl->is_wdsp_booted = true;
- /* Enable WDOG */
- snd_soc_component_update_bits(component, WCD934X_CPE_SS_WDOG_CFG,
- 0x10, 0x10);
- done:
- /* If dsp booted up, then remove vote on SVS */
- if (cntl->is_wdsp_booted)
- cntl->cdc_cb->cdc_vote_svs(component, false);
- return ret;
- err_boot:
- /* call shutdown to perform cleanup */
- wcd_cntl_do_shutdown(cntl);
- return ret;
- }
- static irqreturn_t wcd_cntl_ipc_irq(int irq, void *data)
- {
- struct wcd_dsp_cntl *cntl = data;
- int ret;
- complete(&cntl->boot_complete);
- if (cntl->m_dev && cntl->m_ops &&
- cntl->m_ops->signal_handler)
- ret = cntl->m_ops->signal_handler(cntl->m_dev, WDSP_IPC1_INTR,
- NULL);
- else
- ret = -EINVAL;
- if (ret < 0)
- dev_err(cntl->component->dev,
- "%s: Failed to handle irq %d\n", __func__, irq);
- return IRQ_HANDLED;
- }
- static irqreturn_t wcd_cntl_err_irq(int irq, void *data)
- {
- struct wcd_dsp_cntl *cntl = data;
- struct snd_soc_component *component = cntl->component;
- struct wdsp_err_signal_arg arg;
- u16 status = 0;
- u8 reg_val;
- int rc, ret = 0;
- reg_val = snd_soc_component_read32(component,
- WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0A);
- status = status | reg_val;
- reg_val = snd_soc_component_read32(component,
- WCD934X_CPE_SS_SS_ERROR_INT_STATUS_0B);
- status = status | (reg_val << 8);
- dev_info(component->dev, "%s: error interrupt status = 0x%x\n",
- __func__, status);
- if ((status & cntl->irqs.fatal_irqs) &&
- (cntl->m_dev && cntl->m_ops && cntl->m_ops->signal_handler)) {
- /*
- * If WDSP SSR happens, skip collecting debug dumps.
- * If debug dumps collecting happens first, WDSP_ERR_INTR
- * will be blocked in signal_handler and get processed later.
- */
- rc = WCD_CNTL_SET_ERR_IRQ_FLAG(cntl);
- arg.mem_dumps_enabled = cntl->ramdump_enable;
- arg.remote_start_addr = WCD_934X_RAMDUMP_START_ADDR;
- arg.dump_size = WCD_934X_RAMDUMP_SIZE;
- ret = cntl->m_ops->signal_handler(cntl->m_dev, WDSP_ERR_INTR,
- &arg);
- if (ret < 0)
- dev_err(cntl->component->dev,
- "%s: Failed to handle fatal irq 0x%x\n",
- __func__, status & cntl->irqs.fatal_irqs);
- wcd_cntl_change_online_state(cntl, 0);
- if (rc == 0)
- WCD_CNTL_CLR_ERR_IRQ_FLAG(cntl);
- } else {
- dev_err(cntl->component->dev, "%s: Invalid signal_handler\n",
- __func__);
- }
- return IRQ_HANDLED;
- }
- static int wcd_control_handler(struct device *dev, void *priv_data,
- enum wdsp_event_type event, void *data)
- {
- struct wcd_dsp_cntl *cntl = priv_data;
- struct snd_soc_component *component = cntl->component;
- int ret = 0;
- switch (event) {
- case WDSP_EVENT_POST_INIT:
- case WDSP_EVENT_POST_DLOAD_CODE:
- case WDSP_EVENT_DLOAD_FAILED:
- case WDSP_EVENT_POST_SHUTDOWN:
- /* Disable CPAR */
- wcd_cntl_cpar_ctrl(cntl, false);
- /* Disable all the clocks */
- ret = wcd_cntl_clocks_disable(cntl);
- if (ret < 0)
- dev_err(component->dev,
- "%s: Failed to disable clocks, err = %d\n",
- __func__, ret);
- if (event == WDSP_EVENT_POST_DLOAD_CODE)
- /* Mark DSP online since code download is complete */
- wcd_cntl_change_online_state(cntl, 1);
- break;
- case WDSP_EVENT_PRE_DLOAD_DATA:
- case WDSP_EVENT_PRE_DLOAD_CODE:
- /* Enable all the clocks */
- ret = wcd_cntl_clocks_enable(cntl);
- if (ret < 0) {
- dev_err(component->dev,
- "%s: Failed to enable clocks, err = %d\n",
- __func__, ret);
- goto done;
- }
- /* Enable CPAR */
- wcd_cntl_cpar_ctrl(cntl, true);
- if (event == WDSP_EVENT_PRE_DLOAD_CODE)
- wcd_cntl_enable_memory(cntl, WCD_MEM_TYPE_ALWAYS_ON);
- else if (event == WDSP_EVENT_PRE_DLOAD_DATA)
- wcd_cntl_enable_memory(cntl, WCD_MEM_TYPE_SWITCHABLE);
- break;
- case WDSP_EVENT_DO_BOOT:
- ret = wcd_cntl_do_boot(cntl);
- if (ret < 0)
- dev_err(component->dev,
- "%s: WDSP boot failed, err = %d\n",
- __func__, ret);
- break;
- case WDSP_EVENT_DO_SHUTDOWN:
- wcd_cntl_do_shutdown(cntl);
- wcd_cntl_disable_memory(cntl, WCD_MEM_TYPE_SWITCHABLE);
- break;
- default:
- dev_dbg(component->dev, "%s: unhandled event %d\n",
- __func__, event);
- }
- done:
- return ret;
- }
- static int wcd_cntl_sysfs_init(char *dir, struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- int ret = 0;
- ret = kobject_init_and_add(&cntl->wcd_kobj, &wcd_cntl_ktype,
- kernel_kobj, dir);
- if (ret < 0) {
- dev_err(component->dev,
- "%s: Failed to add kobject %s, err = %d\n",
- __func__, dir, ret);
- goto done;
- }
- ret = sysfs_create_file(&cntl->wcd_kobj, &cntl_attr_boot.attr);
- if (ret < 0) {
- dev_err(component->dev,
- "%s: Failed to add wdsp_boot sysfs entry to %s\n",
- __func__, dir);
- goto fail_create_file;
- }
- return ret;
- fail_create_file:
- kobject_put(&cntl->wcd_kobj);
- done:
- return ret;
- }
- static void wcd_cntl_sysfs_remove(struct wcd_dsp_cntl *cntl)
- {
- sysfs_remove_file(&cntl->wcd_kobj, &cntl_attr_boot.attr);
- kobject_put(&cntl->wcd_kobj);
- }
- static void wcd_cntl_debugfs_init(char *dir, struct wcd_dsp_cntl *cntl)
- {
- struct snd_soc_component *component = cntl->component;
- cntl->entry = debugfs_create_dir(dir, NULL);
- if (IS_ERR_OR_NULL(dir)) {
- dev_err(component->dev, "%s debugfs_create_dir failed for %s\n",
- __func__, dir);
- goto done;
- }
- debugfs_create_u32("debug_mode", 0644,
- cntl->entry, &cntl->debug_mode);
- debugfs_create_bool("ramdump_enable", 0644,
- cntl->entry, &cntl->ramdump_enable);
- debugfs_create_bool("debug_dump_enable", 0644,
- cntl->entry, &cntl->dbg_dmp_enable);
- done:
- return;
- }
- static void wcd_cntl_debugfs_remove(struct wcd_dsp_cntl *cntl)
- {
- if (cntl)
- debugfs_remove(cntl->entry);
- }
- static int wcd_miscdev_release(struct inode *inode, struct file *filep)
- {
- struct wcd_dsp_cntl *cntl = container_of(filep->private_data,
- struct wcd_dsp_cntl, miscdev);
- if (!cntl->m_dev || !cntl->m_ops ||
- !cntl->m_ops->vote_for_dsp) {
- dev_err(cntl->component->dev,
- "%s: DSP not ready to boot\n", __func__);
- return -EINVAL;
- }
- /* Make sure the DSP users goes to zero upon closing dev node */
- while (cntl->boot_reqs > 0) {
- cntl->m_ops->vote_for_dsp(cntl->m_dev, false);
- cntl->boot_reqs--;
- }
- return 0;
- }
- static ssize_t wcd_miscdev_write(struct file *filep, const char __user *ubuf,
- size_t count, loff_t *pos)
- {
- struct wcd_dsp_cntl *cntl = container_of(filep->private_data,
- struct wcd_dsp_cntl, miscdev);
- char val[WCD_MISCDEV_CMD_MAX_LEN + 1];
- bool vote;
- int ret = 0;
- memset(val, 0, WCD_MISCDEV_CMD_MAX_LEN + 1);
- if (count == 0 || count > WCD_MISCDEV_CMD_MAX_LEN) {
- pr_err("%s: Invalid count = %zd\n", __func__, count);
- ret = -EINVAL;
- goto done;
- }
- ret = copy_from_user(val, ubuf, count);
- if (ret < 0) {
- dev_err(cntl->component->dev,
- "%s: copy_from_user failed, err = %d\n",
- __func__, ret);
- ret = -EFAULT;
- goto done;
- }
- if (val[0] == '1') {
- cntl->boot_reqs++;
- vote = true;
- } else if (val[0] == '0') {
- if (cntl->boot_reqs == 0) {
- dev_err(cntl->component->dev,
- "%s: WDSP already disabled\n",
- __func__);
- ret = -EINVAL;
- goto done;
- }
- cntl->boot_reqs--;
- vote = false;
- } else if (!strcmp(val, "DEBUG_DUMP")) {
- if (cntl->dbg_dmp_enable) {
- dev_dbg(cntl->component->dev,
- "%s: Collect dumps for debug use\n", __func__);
- wcd_cntl_collect_debug_dumps(cntl, false);
- }
- /*
- * simply ignore the request from userspace
- * if dbg_dump_enable is not set from debugfs
- */
- goto done;
- } else {
- dev_err(cntl->component->dev, "%s: Invalid value %s\n",
- __func__, val);
- ret = -EINVAL;
- goto done;
- }
- dev_dbg(cntl->component->dev,
- "%s: booted = %s, ref_cnt = %d, vote = %s\n",
- __func__, cntl->is_wdsp_booted ? "true" : "false",
- cntl->boot_reqs, vote ? "true" : "false");
- if (cntl->m_dev && cntl->m_ops &&
- cntl->m_ops->vote_for_dsp)
- ret = cntl->m_ops->vote_for_dsp(cntl->m_dev, vote);
- else
- ret = -EINVAL;
- done:
- if (ret)
- return ret;
- else
- return count;
- }
- static const struct file_operations wcd_miscdev_fops = {
- .write = wcd_miscdev_write,
- .release = wcd_miscdev_release,
- };
- static int wcd_cntl_miscdev_create(struct wcd_dsp_cntl *cntl)
- {
- snprintf(cntl->miscdev_name, ARRAY_SIZE(cntl->miscdev_name),
- "wcd_dsp%u_control", cntl->dsp_instance);
- cntl->miscdev.minor = MISC_DYNAMIC_MINOR;
- cntl->miscdev.name = cntl->miscdev_name;
- cntl->miscdev.fops = &wcd_miscdev_fops;
- cntl->miscdev.parent = cntl->component->dev;
- return misc_register(&cntl->miscdev);
- }
- static void wcd_cntl_miscdev_destroy(struct wcd_dsp_cntl *cntl)
- {
- misc_deregister(&cntl->miscdev);
- }
- static int wcd_control_init(struct device *dev, void *priv_data)
- {
- struct wcd_dsp_cntl *cntl = priv_data;
- struct snd_soc_component *component = cntl->component;
- struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
- struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
- int ret;
- bool err_irq_requested = false;
- ret = wcd9xxx_request_irq(core_res,
- cntl->irqs.cpe_ipc1_irq,
- wcd_cntl_ipc_irq, "CPE IPC1",
- cntl);
- if (ret < 0) {
- dev_err(component->dev,
- "%s: Failed to request cpe ipc irq, err = %d\n",
- __func__, ret);
- goto done;
- }
- /* Unmask the fatal irqs */
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
- ~(cntl->irqs.fatal_irqs & 0xFF));
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
- ~((cntl->irqs.fatal_irqs >> 8) & 0xFF));
- /*
- * CPE ERR irq is used only for error reporting from WCD DSP,
- * even if this request fails, DSP can be function normally.
- * Continuing with init even if the CPE ERR irq request fails.
- */
- if (wcd9xxx_request_irq(core_res, cntl->irqs.cpe_err_irq,
- wcd_cntl_err_irq, "CPE ERR", cntl))
- dev_info(component->dev, "%s: Failed request_irq(cpe_err_irq)",
- __func__);
- else
- err_irq_requested = true;
- /* Enable all the clocks */
- ret = wcd_cntl_clocks_enable(cntl);
- if (ret < 0) {
- dev_err(component->dev, "%s: Failed to enable clocks, err = %d\n",
- __func__, ret);
- goto err_clk_enable;
- }
- wcd_cntl_cpar_ctrl(cntl, true);
- return 0;
- err_clk_enable:
- /* Mask all error interrupts */
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
- 0xFF);
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
- 0xFF);
- /* Free the irq's requested */
- wcd9xxx_free_irq(core_res, cntl->irqs.cpe_ipc1_irq, cntl);
- if (err_irq_requested)
- wcd9xxx_free_irq(core_res, cntl->irqs.cpe_err_irq, cntl);
- done:
- return ret;
- }
- static int wcd_control_deinit(struct device *dev, void *priv_data)
- {
- struct wcd_dsp_cntl *cntl = priv_data;
- struct snd_soc_component *component = cntl->component;
- struct wcd9xxx *wcd9xxx = dev_get_drvdata(component->dev->parent);
- struct wcd9xxx_core_resource *core_res = &wcd9xxx->core_res;
- wcd_cntl_clocks_disable(cntl);
- wcd_cntl_cpar_ctrl(cntl, false);
- /* Mask all error interrupts */
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0A,
- 0xFF);
- snd_soc_component_write(component, WCD934X_CPE_SS_SS_ERROR_INT_MASK_0B,
- 0xFF);
- /* Free the irq's requested */
- wcd9xxx_free_irq(core_res, cntl->irqs.cpe_err_irq, cntl);
- wcd9xxx_free_irq(core_res, cntl->irqs.cpe_ipc1_irq, cntl);
- return 0;
- }
- static struct wdsp_cmpnt_ops control_ops = {
- .init = wcd_control_init,
- .deinit = wcd_control_deinit,
- .event_handler = wcd_control_handler,
- };
- static int wcd_ctrl_component_bind(struct device *dev,
- struct device *master,
- void *data)
- {
- struct wcd_dsp_cntl *cntl;
- struct snd_soc_component *component;
- struct snd_card *card;
- struct snd_info_entry *entry;
- char proc_name[WCD_PROCFS_ENTRY_MAX_LEN];
- char wcd_cntl_dir_name[WCD_CNTL_DIR_NAME_LEN_MAX];
- int ret = 0;
- if (!dev || !master || !data) {
- pr_err("%s: Invalid parameters\n", __func__);
- return -EINVAL;
- }
- cntl = tavil_get_wcd_dsp_cntl(dev);
- if (!cntl) {
- dev_err(dev, "%s: Failed to get cntl reference\n",
- __func__);
- return -EINVAL;
- }
- cntl->m_dev = master;
- cntl->m_ops = data;
- if (!cntl->m_ops->register_cmpnt_ops) {
- dev_err(dev, "%s: invalid master callback register_cmpnt_ops\n",
- __func__);
- ret = -EINVAL;
- goto done;
- }
- ret = cntl->m_ops->register_cmpnt_ops(master, dev, cntl, &control_ops);
- if (ret) {
- dev_err(dev, "%s: register_cmpnt_ops failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- ret = wcd_cntl_miscdev_create(cntl);
- if (ret < 0) {
- dev_err(dev, "%s: misc dev register failed, err = %d\n",
- __func__, ret);
- goto done;
- }
- snprintf(wcd_cntl_dir_name, WCD_CNTL_DIR_NAME_LEN_MAX,
- "%s%d", "wdsp", cntl->dsp_instance);
- ret = wcd_cntl_sysfs_init(wcd_cntl_dir_name, cntl);
- if (ret < 0) {
- dev_err(dev, "%s: sysfs_init failed, err = %d\n",
- __func__, ret);
- goto err_sysfs_init;
- }
- wcd_cntl_debugfs_init(wcd_cntl_dir_name, cntl);
- component = cntl->component;
- card = component->card->snd_card;
- snprintf(proc_name, WCD_PROCFS_ENTRY_MAX_LEN, "%s%d%s", "cpe",
- cntl->dsp_instance, "_state");
- entry = snd_info_create_card_entry(card, proc_name, card->proc_root);
- if (!entry) {
- /* Do not treat this as Fatal error */
- dev_err(dev, "%s: Failed to create procfs entry %s\n",
- __func__, proc_name);
- goto err_sysfs_init;
- }
- cntl->ssr_entry.entry = entry;
- cntl->ssr_entry.offline = 1;
- entry->size = WCD_PROCFS_ENTRY_MAX_LEN;
- entry->content = SNDRV_INFO_CONTENT_DATA;
- entry->c.ops = &wdsp_ssr_entry_ops;
- entry->private_data = cntl;
- ret = snd_info_register(entry);
- if (ret < 0) {
- dev_err(dev, "%s: Failed to register entry %s, err = %d\n",
- __func__, proc_name, ret);
- snd_info_free_entry(entry);
- /* Let bind still happen even if creating the entry failed */
- ret = 0;
- }
- done:
- return ret;
- err_sysfs_init:
- wcd_cntl_miscdev_destroy(cntl);
- return ret;
- }
- static void wcd_ctrl_component_unbind(struct device *dev,
- struct device *master,
- void *data)
- {
- struct wcd_dsp_cntl *cntl;
- if (!dev) {
- pr_err("%s: Invalid device\n", __func__);
- return;
- }
- cntl = tavil_get_wcd_dsp_cntl(dev);
- if (!cntl) {
- dev_err(dev, "%s: Failed to get cntl reference\n",
- __func__);
- return;
- }
- cntl->m_dev = NULL;
- cntl->m_ops = NULL;
- /* Remove the sysfs entries */
- wcd_cntl_sysfs_remove(cntl);
- /* Remove the debugfs entries */
- wcd_cntl_debugfs_remove(cntl);
- /* Remove the misc device */
- wcd_cntl_miscdev_destroy(cntl);
- }
- static const struct component_ops wcd_ctrl_component_ops = {
- .bind = wcd_ctrl_component_bind,
- .unbind = wcd_ctrl_component_unbind,
- };
- /*
- * wcd_dsp_ssr_event: handle the SSR event raised by caller.
- * @cntl: Handle to the wcd_dsp_cntl structure
- * @event: The SSR event to be handled
- *
- * Notifies the manager driver about the SSR event.
- * Returns 0 on success and negative error code on error.
- */
- int wcd_dsp_ssr_event(struct wcd_dsp_cntl *cntl, enum cdc_ssr_event event)
- {
- int ret = 0;
- if (!cntl) {
- pr_err("%s: Invalid handle to control\n", __func__);
- return -EINVAL;
- }
- if (!cntl->m_dev || !cntl->m_ops || !cntl->m_ops->signal_handler) {
- dev_err(cntl->component->dev,
- "%s: Invalid signal_handler callback\n", __func__);
- return -EINVAL;
- }
- switch (event) {
- case WCD_CDC_DOWN_EVENT:
- ret = cntl->m_ops->signal_handler(cntl->m_dev,
- WDSP_CDC_DOWN_SIGNAL,
- NULL);
- if (ret < 0)
- dev_err(cntl->component->dev,
- "%s: WDSP_CDC_DOWN_SIGNAL failed, err = %d\n",
- __func__, ret);
- wcd_cntl_change_online_state(cntl, 0);
- break;
- case WCD_CDC_UP_EVENT:
- ret = cntl->m_ops->signal_handler(cntl->m_dev,
- WDSP_CDC_UP_SIGNAL,
- NULL);
- if (ret < 0)
- dev_err(cntl->component->dev,
- "%s: WDSP_CDC_UP_SIGNAL failed, err = %d\n",
- __func__, ret);
- break;
- default:
- dev_err(cntl->component->dev, "%s: Invalid event %d\n",
- __func__, event);
- ret = -EINVAL;
- break;
- }
- return ret;
- }
- EXPORT_SYMBOL(wcd_dsp_ssr_event);
- /*
- * wcd_dsp_cntl_init: Initialize the wcd-dsp control
- * @component: pointer to the codec component handle
- * @params: Parameters required to initialize wcd-dsp control
- *
- * This API is expected to be invoked by the codec driver and
- * provide information essential for the wcd dsp control to
- * configure and initialize the dsp
- */
- void wcd_dsp_cntl_init(struct snd_soc_component *component,
- struct wcd_dsp_params *params,
- struct wcd_dsp_cntl **cntl)
- {
- struct wcd_dsp_cntl *control;
- int ret;
- if (!component || !params) {
- pr_err("%s: Invalid handle to %s\n", __func__,
- (!component) ? "component" : "params");
- *cntl = NULL;
- return;
- }
- if (*cntl) {
- pr_err("%s: cntl is non NULL, maybe already initialized ?\n",
- __func__);
- return;
- }
- if (!params->cb || !params->cb->cdc_clk_en ||
- !params->cb->cdc_vote_svs) {
- dev_err(component->dev,
- "%s: clk_en and vote_svs callbacks must be provided\n",
- __func__);
- return;
- }
- control = kzalloc(sizeof(*control), GFP_KERNEL);
- if (!(control))
- return;
- control->component = component;
- control->clk_rate = params->clk_rate;
- control->cdc_cb = params->cb;
- control->dsp_instance = params->dsp_instance;
- memcpy(&control->irqs, ¶ms->irqs, sizeof(control->irqs));
- init_completion(&control->boot_complete);
- mutex_init(&control->clk_mutex);
- mutex_init(&control->ssr_mutex);
- init_waitqueue_head(&control->ssr_entry.offline_poll_wait);
- WCD_CNTL_CLR_ERR_IRQ_FLAG(control);
- /*
- * The default state of WDSP is in SVS mode.
- * Vote for SVS now, the vote will be removed only
- * after DSP is booted up.
- */
- control->cdc_cb->cdc_vote_svs(component, true);
- /*
- * If this is the last component needed by master to be ready,
- * then component_bind will be called within the component_add.
- * Hence, the data pointer should be assigned before component_add,
- * so that we can access it during this component's bind call.
- */
- *cntl = control;
- ret = component_add(component->dev, &wcd_ctrl_component_ops);
- if (ret) {
- dev_err(component->dev, "%s: component_add failed, err = %d\n",
- __func__, ret);
- kfree(*cntl);
- *cntl = NULL;
- }
- }
- EXPORT_SYMBOL(wcd_dsp_cntl_init);
- /*
- * wcd_dsp_cntl_deinit: De-initialize the wcd-dsp control
- * @cntl: The struct wcd_dsp_cntl to de-initialize
- *
- * This API is intended to be invoked by the codec driver
- * to de-initialize the wcd dsp control
- */
- void wcd_dsp_cntl_deinit(struct wcd_dsp_cntl **cntl)
- {
- struct wcd_dsp_cntl *control = *cntl;
- struct snd_soc_component *component;
- /* If control is NULL, there is nothing to de-initialize */
- if (!control)
- return;
- component = control->component;
- /*
- * Calling shutdown will cleanup all register states,
- * irrespective of DSP was booted up or not.
- */
- wcd_cntl_do_shutdown(control);
- wcd_cntl_disable_memory(control, WCD_MEM_TYPE_SWITCHABLE);
- wcd_cntl_disable_memory(control, WCD_MEM_TYPE_ALWAYS_ON);
- component_del(component->dev, &wcd_ctrl_component_ops);
- mutex_destroy(&control->clk_mutex);
- mutex_destroy(&control->ssr_mutex);
- kfree(*cntl);
- *cntl = NULL;
- }
- EXPORT_SYMBOL(wcd_dsp_cntl_deinit);
|