|
| 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 i; |
| 66 | + |
| 67 | + for (i = 0; i < common->max_bias; i++) { |
| 68 | + common->micb_vout[i] = wcd_get_micbias_val(common->dev, i + 1, &common->micb_mv[i]); |
| 69 | + if (common->micb_vout[i] < 0) |
| 70 | + return -EINVAL; |
| 71 | + } |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
| 75 | +EXPORT_SYMBOL_GPL(wcd_dt_parse_micbias_info); |
| 76 | + |
| 77 | +static int wcd_sdw_component_bind(struct device *dev, struct device *master, void *data) |
| 78 | +{ |
| 79 | + pm_runtime_set_autosuspend_delay(dev, 3000); |
| 80 | + pm_runtime_use_autosuspend(dev); |
| 81 | + pm_runtime_mark_last_busy(dev); |
| 82 | + pm_runtime_set_active(dev); |
| 83 | + pm_runtime_enable(dev); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static void wcd_sdw_component_unbind(struct device *dev, struct device *master, void *data) |
| 89 | +{ |
| 90 | + pm_runtime_disable(dev); |
| 91 | + pm_runtime_set_suspended(dev); |
| 92 | + pm_runtime_dont_use_autosuspend(dev); |
| 93 | +} |
| 94 | + |
| 95 | +const struct component_ops wcd_sdw_component_ops = { |
| 96 | + .bind = wcd_sdw_component_bind, |
| 97 | + .unbind = wcd_sdw_component_unbind, |
| 98 | +}; |
| 99 | +EXPORT_SYMBOL_GPL(wcd_sdw_component_ops); |
| 100 | + |
| 101 | +int wcd_update_status(struct sdw_slave *slave, enum sdw_slave_status status) |
| 102 | +{ |
| 103 | + struct regmap *regmap = dev_get_regmap(&slave->dev, NULL); |
| 104 | + |
| 105 | + if (regmap && status == SDW_SLAVE_ATTACHED) { |
| 106 | + /* Write out any cached changes that happened between probe and attach */ |
| 107 | + regcache_cache_only(regmap, false); |
| 108 | + return regcache_sync(regmap); |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | +EXPORT_SYMBOL_GPL(wcd_update_status); |
| 114 | + |
| 115 | +int wcd_bus_config(struct sdw_slave *slave, struct sdw_bus_params *params) |
| 116 | +{ |
| 117 | + sdw_write(slave, SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(params->next_bank), 0x01); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | +EXPORT_SYMBOL_GPL(wcd_bus_config); |
| 122 | + |
| 123 | +int wcd_interrupt_callback(struct sdw_slave *slave, struct irq_domain *slave_irq, |
| 124 | + unsigned int wcd_intr_status0, unsigned int wcd_intr_status1, |
| 125 | + unsigned int wcd_intr_status2) |
| 126 | +{ |
| 127 | + struct regmap *regmap = dev_get_regmap(&slave->dev, NULL); |
| 128 | + u32 sts1, sts2, sts3; |
| 129 | + |
| 130 | + do { |
| 131 | + handle_nested_irq(irq_find_mapping(slave_irq, 0)); |
| 132 | + regmap_read(regmap, wcd_intr_status0, &sts1); |
| 133 | + regmap_read(regmap, wcd_intr_status1, &sts2); |
| 134 | + regmap_read(regmap, wcd_intr_status2, &sts3); |
| 135 | + |
| 136 | + } while (sts1 || sts2 || sts3); |
| 137 | + |
| 138 | + return IRQ_HANDLED; |
| 139 | +} |
| 140 | +EXPORT_SYMBOL_GPL(wcd_interrupt_callback); |
| 141 | + |
| 142 | +MODULE_DESCRIPTION("Common Qualcomm WCD Codec helpers driver"); |
| 143 | +MODULE_LICENSE("GPL"); |
0 commit comments