414 lines · c
1// SPDX-License-Identifier: GPL-2.02//3// Framer ALSA SoC driver4//5// Copyright 2023 CS GROUP France6//7// Author: Herve Codina <herve.codina@bootlin.com>8 9#include <linux/clk.h>10#include <linux/framer/framer.h>11#include <linux/module.h>12#include <linux/notifier.h>13#include <linux/platform_device.h>14#include <linux/slab.h>15#include <sound/jack.h>16#include <sound/pcm_params.h>17#include <sound/soc.h>18#include <sound/tlv.h>19 20#define FRAMER_NB_CHANNEL 3221#define FRAMER_JACK_MASK (SND_JACK_LINEIN | SND_JACK_LINEOUT)22 23struct framer_codec {24 struct framer *framer;25 struct device *dev;26 struct snd_soc_jack jack;27 struct notifier_block nb;28 struct work_struct carrier_work;29 int max_chan_playback;30 int max_chan_capture;31};32 33static int framer_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,34 unsigned int rx_mask, int slots, int width)35{36 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);37 38 switch (width) {39 case 0:40 /* Not set -> default 8 */41 case 8:42 break;43 default:44 dev_err(dai->dev, "tdm slot width %d not supported\n", width);45 return -EINVAL;46 }47 48 framer->max_chan_playback = hweight32(tx_mask);49 if (framer->max_chan_playback > FRAMER_NB_CHANNEL) {50 dev_err(dai->dev, "too many tx slots defined (mask = 0x%x) supported max %d\n",51 tx_mask, FRAMER_NB_CHANNEL);52 return -EINVAL;53 }54 55 framer->max_chan_capture = hweight32(rx_mask);56 if (framer->max_chan_capture > FRAMER_NB_CHANNEL) {57 dev_err(dai->dev, "too many rx slots defined (mask = 0x%x) supported max %d\n",58 rx_mask, FRAMER_NB_CHANNEL);59 return -EINVAL;60 }61 62 return 0;63}64 65/*66 * The constraints for format/channel is to match with the number of 8bit67 * time-slots available.68 */69static int framer_dai_hw_rule_channels_by_format(struct snd_soc_dai *dai,70 struct snd_pcm_hw_params *params,71 unsigned int nb_ts)72{73 struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);74 snd_pcm_format_t format = params_format(params);75 struct snd_interval ch = {0};76 int width;77 78 width = snd_pcm_format_physical_width(format);79 if (width == 8 || width == 16 || width == 32 || width == 64) {80 ch.max = nb_ts * 8 / width;81 } else {82 dev_err(dai->dev, "format physical width %d not supported\n", width);83 return -EINVAL;84 }85 86 ch.min = ch.max ? 1 : 0;87 88 return snd_interval_refine(c, &ch);89}90 91static int framer_dai_hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,92 struct snd_pcm_hw_rule *rule)93{94 struct snd_soc_dai *dai = rule->private;95 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);96 97 return framer_dai_hw_rule_channels_by_format(dai, params, framer->max_chan_playback);98}99 100static int framer_dai_hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,101 struct snd_pcm_hw_rule *rule)102{103 struct snd_soc_dai *dai = rule->private;104 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);105 106 return framer_dai_hw_rule_channels_by_format(dai, params, framer->max_chan_capture);107}108 109static int framer_dai_hw_rule_format_by_channels(struct snd_soc_dai *dai,110 struct snd_pcm_hw_params *params,111 unsigned int nb_ts)112{113 struct snd_mask *f_old = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);114 unsigned int channels = params_channels(params);115 unsigned int slot_width;116 snd_pcm_format_t format;117 struct snd_mask f_new;118 119 if (!channels || channels > nb_ts) {120 dev_err(dai->dev, "channels %u not supported\n", nb_ts);121 return -EINVAL;122 }123 124 slot_width = (nb_ts / channels) * 8;125 126 snd_mask_none(&f_new);127 pcm_for_each_format(format) {128 if (snd_mask_test_format(f_old, format)) {129 if (snd_pcm_format_physical_width(format) <= slot_width)130 snd_mask_set_format(&f_new, format);131 }132 }133 134 return snd_mask_refine(f_old, &f_new);135}136 137static int framer_dai_hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,138 struct snd_pcm_hw_rule *rule)139{140 struct snd_soc_dai *dai = rule->private;141 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);142 143 return framer_dai_hw_rule_format_by_channels(dai, params, framer->max_chan_playback);144}145 146static int framer_dai_hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,147 struct snd_pcm_hw_rule *rule)148{149 struct snd_soc_dai *dai = rule->private;150 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);151 152 return framer_dai_hw_rule_format_by_channels(dai, params, framer->max_chan_capture);153}154 155static u64 framer_formats(u8 nb_ts)156{157 unsigned int format_width;158 unsigned int chan_width;159 snd_pcm_format_t format;160 u64 formats_mask;161 162 if (!nb_ts)163 return 0;164 165 formats_mask = 0;166 chan_width = nb_ts * 8;167 pcm_for_each_format(format) {168 /* Support physical width multiple of 8bit */169 format_width = snd_pcm_format_physical_width(format);170 if (format_width == 0 || format_width % 8)171 continue;172 173 /*174 * And support physical width that can fit N times in the175 * channel176 */177 if (format_width > chan_width || chan_width % format_width)178 continue;179 180 formats_mask |= pcm_format_to_bits(format);181 }182 return formats_mask;183}184 185static int framer_dai_startup(struct snd_pcm_substream *substream,186 struct snd_soc_dai *dai)187{188 struct framer_codec *framer = snd_soc_component_get_drvdata(dai->component);189 snd_pcm_hw_rule_func_t hw_rule_channels_by_format;190 snd_pcm_hw_rule_func_t hw_rule_format_by_channels;191 unsigned int frame_bits;192 u64 format;193 int ret;194 195 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {196 format = framer_formats(framer->max_chan_capture);197 hw_rule_channels_by_format = framer_dai_hw_rule_capture_channels_by_format;198 hw_rule_format_by_channels = framer_dai_hw_rule_capture_format_by_channels;199 frame_bits = framer->max_chan_capture * 8;200 } else {201 format = framer_formats(framer->max_chan_playback);202 hw_rule_channels_by_format = framer_dai_hw_rule_playback_channels_by_format;203 hw_rule_format_by_channels = framer_dai_hw_rule_playback_format_by_channels;204 frame_bits = framer->max_chan_playback * 8;205 }206 207 ret = snd_pcm_hw_constraint_mask64(substream->runtime,208 SNDRV_PCM_HW_PARAM_FORMAT, format);209 if (ret) {210 dev_err(dai->dev, "Failed to add format constraint (%d)\n", ret);211 return ret;212 }213 214 ret = snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,215 hw_rule_channels_by_format, dai,216 SNDRV_PCM_HW_PARAM_FORMAT, -1);217 if (ret) {218 dev_err(dai->dev, "Failed to add channels rule (%d)\n", ret);219 return ret;220 }221 222 ret = snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,223 hw_rule_format_by_channels, dai,224 SNDRV_PCM_HW_PARAM_CHANNELS, -1);225 if (ret) {226 dev_err(dai->dev, "Failed to add format rule (%d)\n", ret);227 return ret;228 }229 230 ret = snd_pcm_hw_constraint_single(substream->runtime,231 SNDRV_PCM_HW_PARAM_FRAME_BITS,232 frame_bits);233 if (ret < 0) {234 dev_err(dai->dev, "Failed to add frame_bits constraint (%d)\n", ret);235 return ret;236 }237 238 return 0;239}240 241static const u64 framer_dai_formats[] = {242 SND_SOC_POSSIBLE_DAIFMT_DSP_B,243};244 245static const struct snd_soc_dai_ops framer_dai_ops = {246 .startup = framer_dai_startup,247 .set_tdm_slot = framer_dai_set_tdm_slot,248 .auto_selectable_formats = framer_dai_formats,249 .num_auto_selectable_formats = ARRAY_SIZE(framer_dai_formats),250};251 252static struct snd_soc_dai_driver framer_dai_driver = {253 .name = "framer",254 .playback = {255 .stream_name = "Playback",256 .channels_min = 1,257 .channels_max = FRAMER_NB_CHANNEL,258 .rates = SNDRV_PCM_RATE_8000,259 .formats = U64_MAX, /* Will be refined on DAI .startup() */260 },261 .capture = {262 .stream_name = "Capture",263 .channels_min = 1,264 .channels_max = FRAMER_NB_CHANNEL,265 .rates = SNDRV_PCM_RATE_8000,266 .formats = U64_MAX, /* Will be refined on DAI .startup() */267 },268 .ops = &framer_dai_ops,269};270 271static void framer_carrier_work(struct work_struct *work)272{273 struct framer_codec *framer = container_of(work, struct framer_codec, carrier_work);274 struct framer_status framer_status;275 int jack_status;276 int ret;277 278 ret = framer_get_status(framer->framer, &framer_status);279 if (ret) {280 dev_err(framer->dev, "get framer status failed (%d)\n", ret);281 return;282 }283 284 jack_status = framer_status.link_is_on ? FRAMER_JACK_MASK : 0;285 snd_soc_jack_report(&framer->jack, jack_status, FRAMER_JACK_MASK);286}287 288static int framer_carrier_notifier(struct notifier_block *nb, unsigned long action,289 void *data)290{291 struct framer_codec *framer = container_of(nb, struct framer_codec, nb);292 293 switch (action) {294 case FRAMER_EVENT_STATUS:295 queue_work(system_power_efficient_wq, &framer->carrier_work);296 break;297 default:298 return NOTIFY_DONE;299 }300 301 return NOTIFY_OK;302}303 304static int framer_component_probe(struct snd_soc_component *component)305{306 struct framer_codec *framer = snd_soc_component_get_drvdata(component);307 struct framer_status status;308 char *name;309 int ret;310 311 INIT_WORK(&framer->carrier_work, framer_carrier_work);312 313 name = "carrier";314 if (component->name_prefix) {315 name = kasprintf(GFP_KERNEL, "%s carrier", component->name_prefix);316 if (!name)317 return -ENOMEM;318 }319 320 ret = snd_soc_card_jack_new(component->card, name, FRAMER_JACK_MASK, &framer->jack);321 if (component->name_prefix)322 kfree(name); /* A copy is done by snd_soc_card_jack_new */323 if (ret) {324 dev_err(component->dev, "Cannot create jack\n");325 return ret;326 }327 328 ret = framer_init(framer->framer);329 if (ret) {330 dev_err(component->dev, "framer init failed (%d)\n", ret);331 return ret;332 }333 334 ret = framer_power_on(framer->framer);335 if (ret) {336 dev_err(component->dev, "framer power-on failed (%d)\n", ret);337 goto framer_exit;338 }339 340 /* Be sure that get_status is supported */341 ret = framer_get_status(framer->framer, &status);342 if (ret) {343 dev_err(component->dev, "get framer status failed (%d)\n", ret);344 goto framer_power_off;345 }346 347 framer->nb.notifier_call = framer_carrier_notifier;348 ret = framer_notifier_register(framer->framer, &framer->nb);349 if (ret) {350 dev_err(component->dev, "Cannot register event notifier\n");351 goto framer_power_off;352 }353 354 /* Queue work to set the initial value */355 queue_work(system_power_efficient_wq, &framer->carrier_work);356 357 return 0;358 359framer_power_off:360 framer_power_off(framer->framer);361framer_exit:362 framer_exit(framer->framer);363 return ret;364}365 366static void framer_component_remove(struct snd_soc_component *component)367{368 struct framer_codec *framer = snd_soc_component_get_drvdata(component);369 370 framer_notifier_unregister(framer->framer, &framer->nb);371 cancel_work_sync(&framer->carrier_work);372 framer_power_off(framer->framer);373 framer_exit(framer->framer);374}375 376static const struct snd_soc_component_driver framer_component_driver = {377 .probe = framer_component_probe,378 .remove = framer_component_remove,379 .endianness = 1,380};381 382static int framer_codec_probe(struct platform_device *pdev)383{384 struct framer_codec *framer;385 386 framer = devm_kzalloc(&pdev->dev, sizeof(*framer), GFP_KERNEL);387 if (!framer)388 return -ENOMEM;389 390 framer->dev = &pdev->dev;391 392 /* Get framer from parents node */393 framer->framer = devm_framer_get(&pdev->dev, NULL);394 if (IS_ERR(framer->framer))395 return dev_err_probe(&pdev->dev, PTR_ERR(framer->framer), "get framer failed\n");396 397 platform_set_drvdata(pdev, framer);398 399 return devm_snd_soc_register_component(&pdev->dev, &framer_component_driver,400 &framer_dai_driver, 1);401}402 403static struct platform_driver framer_codec_driver = {404 .driver = {405 .name = "framer-codec",406 },407 .probe = framer_codec_probe,408};409module_platform_driver(framer_codec_driver);410 411MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");412MODULE_DESCRIPTION("FRAMER ALSA SoC driver");413MODULE_LICENSE("GPL");414