1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * Copyright (c) 2020 Bootlin SA 3 * Copyright (c) 2020 Bootlin SA 4 * Author: Alexandre Belloni <alexandre.bellon 4 * Author: Alexandre Belloni <alexandre.belloni@bootlin.com> 5 */ 5 */ 6 6 7 #include <linux/gpio/consumer.h> 7 #include <linux/gpio/consumer.h> 8 #include <linux/module.h> 8 #include <linux/module.h> 9 #include <linux/regulator/consumer.h> 9 #include <linux/regulator/consumer.h> 10 #include <sound/soc.h> 10 #include <sound/soc.h> 11 11 12 #define MUX_TEXT_SIZE 2 << 13 #define MUX_WIDGET_SIZE 4 << 14 #define MUX_ROUTE_SIZE 3 << 15 struct simple_mux { 12 struct simple_mux { 16 struct gpio_desc *gpiod_mux; 13 struct gpio_desc *gpiod_mux; 17 unsigned int mux; 14 unsigned int mux; 18 const char *mux_texts[MUX_TEXT_SIZE]; << 19 struct soc_enum mux_enum; << 20 struct snd_kcontrol_new mux_mux; << 21 struct snd_soc_dapm_widget mux_widgets << 22 struct snd_soc_dapm_route mux_routes[M << 23 struct snd_soc_component_driver mux_dr << 24 }; 15 }; 25 16 26 static const char * const simple_mux_texts[MUX !! 17 static const char * const simple_mux_texts[] = { 27 "Input 1", "Input 2" 18 "Input 1", "Input 2" 28 }; 19 }; 29 20 30 static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enu 21 static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enum, simple_mux_texts); 31 22 32 static int simple_mux_control_get(struct snd_k 23 static int simple_mux_control_get(struct snd_kcontrol *kcontrol, 33 struct snd_c 24 struct snd_ctl_elem_value *ucontrol) 34 { 25 { 35 struct snd_soc_dapm_context *dapm = sn 26 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 36 struct snd_soc_component *c = snd_soc_ 27 struct snd_soc_component *c = snd_soc_dapm_to_component(dapm); 37 struct simple_mux *priv = snd_soc_comp 28 struct simple_mux *priv = snd_soc_component_get_drvdata(c); 38 29 39 ucontrol->value.enumerated.item[0] = p 30 ucontrol->value.enumerated.item[0] = priv->mux; 40 31 41 return 0; 32 return 0; 42 } 33 } 43 34 44 static int simple_mux_control_put(struct snd_k 35 static int simple_mux_control_put(struct snd_kcontrol *kcontrol, 45 struct snd_c 36 struct snd_ctl_elem_value *ucontrol) 46 { 37 { 47 struct snd_soc_dapm_context *dapm = sn 38 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 48 struct soc_enum *e = (struct soc_enum 39 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 49 struct snd_soc_component *c = snd_soc_ 40 struct snd_soc_component *c = snd_soc_dapm_to_component(dapm); 50 struct simple_mux *priv = snd_soc_comp 41 struct simple_mux *priv = snd_soc_component_get_drvdata(c); 51 42 52 if (ucontrol->value.enumerated.item[0] 43 if (ucontrol->value.enumerated.item[0] > e->items) 53 return -EINVAL; 44 return -EINVAL; 54 45 55 if (priv->mux == ucontrol->value.enume 46 if (priv->mux == ucontrol->value.enumerated.item[0]) 56 return 0; 47 return 0; 57 48 58 priv->mux = ucontrol->value.enumerated 49 priv->mux = ucontrol->value.enumerated.item[0]; 59 50 60 gpiod_set_value_cansleep(priv->gpiod_m 51 gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux); 61 52 62 return snd_soc_dapm_mux_update_power(d 53 return snd_soc_dapm_mux_update_power(dapm, kcontrol, 63 u 54 ucontrol->value.enumerated.item[0], 64 e 55 e, NULL); 65 } 56 } 66 57 67 static unsigned int simple_mux_read(struct snd << 68 unsigned i << 69 { << 70 struct simple_mux *priv = snd_soc_comp << 71 << 72 return priv->mux; << 73 } << 74 << 75 static const struct snd_kcontrol_new simple_mu 58 static const struct snd_kcontrol_new simple_mux_mux = 76 SOC_DAPM_ENUM_EXT("Muxer", simple_mux_ 59 SOC_DAPM_ENUM_EXT("Muxer", simple_mux_enum, simple_mux_control_get, simple_mux_control_put); 77 60 78 static const struct snd_soc_dapm_widget simple !! 61 static const struct snd_soc_dapm_widget simple_mux_dapm_widgets[] = { 79 SND_SOC_DAPM_INPUT("IN1"), 62 SND_SOC_DAPM_INPUT("IN1"), 80 SND_SOC_DAPM_INPUT("IN2"), 63 SND_SOC_DAPM_INPUT("IN2"), 81 SND_SOC_DAPM_MUX("MUX", SND_SOC_NOPM, !! 64 SND_SOC_DAPM_MUX("MUX", SND_SOC_NOPM, 0, 0, &simple_mux_mux), 82 SND_SOC_DAPM_OUTPUT("OUT"), 65 SND_SOC_DAPM_OUTPUT("OUT"), 83 }; 66 }; 84 67 85 static const struct snd_soc_dapm_route simple_ !! 68 static const struct snd_soc_dapm_route simple_mux_dapm_routes[] = { 86 { "OUT", NULL, "MUX" }, 69 { "OUT", NULL, "MUX" }, 87 { "MUX", "Input 1", "IN1" }, // see si !! 70 { "MUX", "Input 1", "IN1" }, 88 { "MUX", "Input 2", "IN2" }, // see si !! 71 { "MUX", "Input 2", "IN2" }, >> 72 }; >> 73 >> 74 static const struct snd_soc_component_driver simple_mux_component_driver = { >> 75 .dapm_widgets = simple_mux_dapm_widgets, >> 76 .num_dapm_widgets = ARRAY_SIZE(simple_mux_dapm_widgets), >> 77 .dapm_routes = simple_mux_dapm_routes, >> 78 .num_dapm_routes = ARRAY_SIZE(simple_mux_dapm_routes), 89 }; 79 }; 90 80 91 static int simple_mux_probe(struct platform_de 81 static int simple_mux_probe(struct platform_device *pdev) 92 { 82 { 93 struct device *dev = &pdev->dev; 83 struct device *dev = &pdev->dev; 94 struct device_node *np = dev->of_node; << 95 struct simple_mux *priv; 84 struct simple_mux *priv; 96 85 97 priv = devm_kzalloc(dev, sizeof(*priv) 86 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 98 if (!priv) 87 if (!priv) 99 return -ENOMEM; 88 return -ENOMEM; 100 89 101 dev_set_drvdata(dev, priv); 90 dev_set_drvdata(dev, priv); 102 91 103 priv->gpiod_mux = devm_gpiod_get(dev, 92 priv->gpiod_mux = devm_gpiod_get(dev, "mux", GPIOD_OUT_LOW); 104 if (IS_ERR(priv->gpiod_mux)) 93 if (IS_ERR(priv->gpiod_mux)) 105 return dev_err_probe(dev, PTR_ 94 return dev_err_probe(dev, PTR_ERR(priv->gpiod_mux), 106 "Failed t 95 "Failed to get 'mux' gpio"); 107 96 108 /* Copy default settings */ !! 97 return devm_snd_soc_register_component(dev, &simple_mux_component_driver, NULL, 0); 109 memcpy(&priv->mux_texts, &simpl << 110 memcpy(&priv->mux_enum, &simpl << 111 memcpy(&priv->mux_mux, &simpl << 112 memcpy(&priv->mux_widgets, &simpl << 113 memcpy(&priv->mux_routes, &simpl << 114 << 115 priv->mux_driver.dapm_widgets << 116 priv->mux_driver.num_dapm_widgets << 117 priv->mux_driver.dapm_routes << 118 priv->mux_driver.num_dapm_routes << 119 priv->mux_driver.read << 120 << 121 /* Overwrite text ("Input 1", "Input 2 << 122 of_property_read_string_array(np, "sta << 123 << 124 /* switch to use priv data instead of << 125 priv->mux_enum.texts << 126 priv->mux_mux.private_value << 127 priv->mux_widgets[2].kcontrol_news << 128 priv->mux_routes[1].control << 129 priv->mux_routes[2].control << 130 << 131 return devm_snd_soc_register_component << 132 } 98 } 133 99 134 #ifdef CONFIG_OF 100 #ifdef CONFIG_OF 135 static const struct of_device_id simple_mux_id 101 static const struct of_device_id simple_mux_ids[] = { 136 { .compatible = "simple-audio-mux", }, 102 { .compatible = "simple-audio-mux", }, 137 { } 103 { } 138 }; 104 }; 139 MODULE_DEVICE_TABLE(of, simple_mux_ids); 105 MODULE_DEVICE_TABLE(of, simple_mux_ids); 140 #endif 106 #endif 141 107 142 static struct platform_driver simple_mux_drive 108 static struct platform_driver simple_mux_driver = { 143 .driver = { 109 .driver = { 144 .name = "simple-mux", 110 .name = "simple-mux", 145 .of_match_table = of_match_ptr 111 .of_match_table = of_match_ptr(simple_mux_ids), 146 }, 112 }, 147 .probe = simple_mux_probe, 113 .probe = simple_mux_probe, 148 }; 114 }; 149 115 150 module_platform_driver(simple_mux_driver); 116 module_platform_driver(simple_mux_driver); 151 117 152 MODULE_DESCRIPTION("ASoC Simple Audio Mux driv 118 MODULE_DESCRIPTION("ASoC Simple Audio Mux driver"); 153 MODULE_AUTHOR("Alexandre Belloni <alexandre.be 119 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 154 MODULE_LICENSE("GPL"); 120 MODULE_LICENSE("GPL"); 155 121
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.