1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2025, Qualcomm Technologies, Inc. and/or its subsidiaries. 3 4 #include <linux/export.h> 5 #include <linux/module.h> 6 #include <linux/init.h> 7 #include <linux/device.h> 8 #include <linux/of.h> 9 #include <linux/printk.h> 10 #include <linux/component.h> 11 #include <linux/pm_runtime.h> 12 #include <linux/soundwire/sdw.h> 13 #include <linux/soundwire/sdw_type.h> 14 #include <linux/regmap.h> 15 16 #include "wcd-common.h" 17 18 #define WCD_MIN_MICBIAS_MV 1000 19 #define WCD_DEF_MICBIAS_MV 1800 20 #define WCD_MAX_MICBIAS_MV 2850 21 22 #define SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(m) (0xE0 + 0x10 * (m)) 23 24 int wcd_get_micb_vout_ctl_val(struct device *dev, u32 micb_mv) 25 { 26 /* min micbias voltage is 1V and maximum is 2.85V */ 27 if (micb_mv < WCD_MIN_MICBIAS_MV || micb_mv > WCD_MAX_MICBIAS_MV) { 28 dev_err(dev, "Unsupported micbias voltage (%u mV)\n", micb_mv); 29 return -EINVAL; 30 } 31 32 return (micb_mv - WCD_MIN_MICBIAS_MV) / 50; 33 } 34 EXPORT_SYMBOL_GPL(wcd_get_micb_vout_ctl_val); 35 36 static int wcd_get_micbias_val(struct device *dev, int micb_num, u32 *micb_mv) 37 { 38 char micbias[64]; 39 int mv; 40 41 sprintf(micbias, "qcom,micbias%d-microvolt", micb_num); 42 43 if (of_property_read_u32(dev->of_node, micbias, &mv)) { 44 dev_err(dev, "%s value not found, using default\n", micbias); 45 mv = WCD_DEF_MICBIAS_MV; 46 } else { 47 /* convert it to milli volts */ 48 mv = mv/1000; 49 } 50 if (micb_mv) 51 *micb_mv = mv; 52 53 mv = wcd_get_micb_vout_ctl_val(dev, mv); 54 if (mv < 0) { 55 dev_err(dev, "Unsupported %s voltage (%d mV), falling back to default (%d mV)\n", 56 micbias, mv, WCD_DEF_MICBIAS_MV); 57 return wcd_get_micb_vout_ctl_val(dev, WCD_DEF_MICBIAS_MV); 58 } 59 60 return mv; 61 } 62 63 int wcd_dt_parse_micbias_info(struct wcd_common *common) 64 { 65 int ret, i; 66 67 for (i = 0; i < common->max_bias; i++) { 68 ret = wcd_get_micbias_val(common->dev, i + 1, &common->micb_mv[i]); 69 if (ret < 0) 70 return ret; 71 common->micb_vout[i] = ret; 72 } 73 74 return 0; 75 } 76 EXPORT_SYMBOL_GPL(wcd_dt_parse_micbias_info); 77 78 static int wcd_sdw_component_bind(struct device *dev, struct device *master, void *data) 79 { 80 pm_runtime_set_autosuspend_delay(dev, 3000); 81 pm_runtime_use_autosuspend(dev); 82 pm_runtime_mark_last_busy(dev); 83 pm_runtime_set_active(dev); 84 pm_runtime_enable(dev); 85 86 return 0; 87 } 88 89 static void wcd_sdw_component_unbind(struct device *dev, struct device *master, void *data) 90 { 91 pm_runtime_disable(dev); 92 pm_runtime_set_suspended(dev); 93 pm_runtime_dont_use_autosuspend(dev); 94 } 95 96 const struct component_ops wcd_sdw_component_ops = { 97 .bind = wcd_sdw_component_bind, 98 .unbind = wcd_sdw_component_unbind, 99 }; 100 EXPORT_SYMBOL_GPL(wcd_sdw_component_ops); 101 102 int wcd_update_status(struct sdw_slave *slave, enum sdw_slave_status status) 103 { 104 struct regmap *regmap = dev_get_regmap(&slave->dev, NULL); 105 106 if (regmap && status == SDW_SLAVE_ATTACHED) { 107 /* Write out any cached changes that happened between probe and attach */ 108 regcache_cache_only(regmap, false); 109 return regcache_sync(regmap); 110 } 111 112 return 0; 113 } 114 EXPORT_SYMBOL_GPL(wcd_update_status); 115 116 int wcd_bus_config(struct sdw_slave *slave, struct sdw_bus_params *params) 117 { 118 sdw_write(slave, SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(params->next_bank), 0x01); 119 120 return 0; 121 } 122 EXPORT_SYMBOL_GPL(wcd_bus_config); 123 124 int wcd_interrupt_callback(struct sdw_slave *slave, struct irq_domain *slave_irq, 125 unsigned int wcd_intr_status0, unsigned int wcd_intr_status1, 126 unsigned int wcd_intr_status2) 127 { 128 struct regmap *regmap = dev_get_regmap(&slave->dev, NULL); 129 u32 sts1, sts2, sts3; 130 131 do { 132 handle_nested_irq(irq_find_mapping(slave_irq, 0)); 133 regmap_read(regmap, wcd_intr_status0, &sts1); 134 regmap_read(regmap, wcd_intr_status1, &sts2); 135 regmap_read(regmap, wcd_intr_status2, &sts3); 136 137 } while (sts1 || sts2 || sts3); 138 139 return IRQ_HANDLED; 140 } 141 EXPORT_SYMBOL_GPL(wcd_interrupt_callback); 142 143 MODULE_DESCRIPTION("Common Qualcomm WCD Codec helpers driver"); 144 MODULE_LICENSE("GPL"); 145