| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 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 32
|
| 21 | #define FRAMER_JACK_MASK (SND_JACK_LINEIN | SND_JACK_LINEOUT)
|
| 22 |
|
| 23 | struct 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 |
|
| 33 | static 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 |
|
| 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 8bit
|
| 67 | * time-slots available.
|
| 68 |
|
| 69 | static 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 |
|
| 91 | static 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 |
|
| 100 | static 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 |
|
| 109 | static 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 |
|
| 137 | static 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 |
|
| 146 | static 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 |
|
| 155 | static 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 |
|
| 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 the
|
| 175 | * channel
|
| 176 |
|
| 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 |
|
| 185 | static 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 |
|
| 241 | static const u64 framer_dai_formats = SND_SOC_POSSIBLE_DAIFMT_DSP_B;
|
| 242 |
|
| 243 | static const struct snd_soc_dai_ops framer_dai_ops = {
|
| 244 | .startup = framer_dai_startup,
|
| 245 | .set_tdm_slot = framer_dai_set_tdm_slot,
|
| 246 | .auto_selectable_formats = &framer_dai_formats,
|
| 247 | .num_auto_selectable_formats = 1,
|
| 248 | };
|
| 249 |
|
| 250 | static struct snd_soc_dai_driver framer_dai_driver = {
|
| 251 | .name = "framer",
|
| 252 | .playback = {
|
| 253 | .stream_name = "Playback",
|
| 254 | .channels_min = 1,
|
| 255 | .channels_max = FRAMER_NB_CHANNEL,
|
| 256 | .rates = SNDRV_PCM_RATE_8000,
|
| 257 | .formats = U64_MAX,
|
| 258 | },
|
| 259 | .capture = {
|
| 260 | .stream_name = "Capture",
|
| 261 | .channels_min = 1,
|
| 262 | .channels_max = FRAMER_NB_CHANNEL,
|
| 263 | .rates = SNDRV_PCM_RATE_8000,
|
| 264 | .formats = U64_MAX,
|
| 265 | },
|
| 266 | .ops = &framer_dai_ops,
|
| 267 | };
|
| 268 |
|
| 269 | static void framer_carrier_work(struct work_struct *work)
|
| 270 | {
|
| 271 | struct framer_codec *framer = container_of(work, struct framer_codec, carrier_work);
|
| 272 | struct framer_status framer_status;
|
| 273 | int jack_status;
|
| 274 | int ret;
|
| 275 |
|
| 276 | ret = framer_get_status(framer->framer, &framer_status);
|
| 277 | if (ret) {
|
| 278 | dev_err(framer->dev, "get framer status failed (%d)\n", ret);
|
| 279 | return;
|
| 280 | }
|
| 281 |
|
| 282 | jack_status = framer_status.link_is_on ? FRAMER_JACK_MASK : 0;
|
| 283 | snd_soc_jack_report(&framer->jack, jack_status, FRAMER_JACK_MASK);
|
| 284 | }
|
| 285 |
|
| 286 | static int framer_carrier_notifier(struct notifier_block *nb, unsigned long action,
|
| 287 | void *data)
|
| 288 | {
|
| 289 | struct framer_codec *framer = container_of(nb, struct framer_codec, nb);
|
| 290 |
|
| 291 | switch (action) {
|
| 292 | case FRAMER_EVENT_STATUS:
|
| 293 | queue_work(system_power_efficient_wq, &framer->carrier_work);
|
| 294 | break;
|
| 295 | default:
|
| 296 | return NOTIFY_DONE;
|
| 297 | }
|
| 298 |
|
| 299 | return NOTIFY_OK;
|
| 300 | }
|
| 301 |
|
| 302 | static int framer_component_probe(struct snd_soc_component *component)
|
| 303 | {
|
| 304 | struct framer_codec *framer = snd_soc_component_get_drvdata(component);
|
| 305 | struct framer_status status;
|
| 306 | char *name;
|
| 307 | int ret;
|
| 308 |
|
| 309 | INIT_WORK(&framer->carrier_work, framer_carrier_work);
|
| 310 |
|
| 311 | name = "carrier";
|
| 312 | if (component->name_prefix) {
|
| 313 | name = kasprintf(GFP_KERNEL, "%s carrier", component->name_prefix);
|
| 314 | if (!name)
|
| 315 | return -ENOMEM;
|
| 316 | }
|
| 317 |
|
| 318 | ret = snd_soc_card_jack_new(component->card, name, FRAMER_JACK_MASK, &framer->jack);
|
| 319 | if (component->name_prefix)
|
| 320 | kfree(name);
|
| 321 | if (ret) {
|
| 322 | dev_err(component->dev, "Cannot create jack\n");
|
| 323 | return ret;
|
| 324 | }
|
| 325 |
|
| 326 | ret = framer_init(framer->framer);
|
| 327 | if (ret) {
|
| 328 | dev_err(component->dev, "framer init failed (%d)\n", ret);
|
| 329 | return ret;
|
| 330 | }
|
| 331 |
|
| 332 | ret = framer_power_on(framer->framer);
|
| 333 | if (ret) {
|
| 334 | dev_err(component->dev, "framer power-on failed (%d)\n", ret);
|
| 335 | goto framer_exit;
|
| 336 | }
|
| 337 |
|
| 338 |
|
| 339 | ret = framer_get_status(framer->framer, &status);
|
| 340 | if (ret) {
|
| 341 | dev_err(component->dev, "get framer status failed (%d)\n", ret);
|
| 342 | goto framer_power_off;
|
| 343 | }
|
| 344 |
|
| 345 | framer->nb.notifier_call = framer_carrier_notifier;
|
| 346 | ret = framer_notifier_register(framer->framer, &framer->nb);
|
| 347 | if (ret) {
|
| 348 | dev_err(component->dev, "Cannot register event notifier\n");
|
| 349 | goto framer_power_off;
|
| 350 | }
|
| 351 |
|
| 352 |
|
| 353 | queue_work(system_power_efficient_wq, &framer->carrier_work);
|
| 354 |
|
| 355 | return 0;
|
| 356 |
|
| 357 | framer_power_off:
|
| 358 | framer_power_off(framer->framer);
|
| 359 | framer_exit:
|
| 360 | framer_exit(framer->framer);
|
| 361 | return ret;
|
| 362 | }
|
| 363 |
|
| 364 | static void framer_component_remove(struct snd_soc_component *component)
|
| 365 | {
|
| 366 | struct framer_codec *framer = snd_soc_component_get_drvdata(component);
|
| 367 |
|
| 368 | framer_notifier_unregister(framer->framer, &framer->nb);
|
| 369 | cancel_work_sync(&framer->carrier_work);
|
| 370 | framer_power_off(framer->framer);
|
| 371 | framer_exit(framer->framer);
|
| 372 | }
|
| 373 |
|
| 374 | static const struct snd_soc_component_driver framer_component_driver = {
|
| 375 | .probe = framer_component_probe,
|
| 376 | .remove = framer_component_remove,
|
| 377 | .endianness = 1,
|
| 378 | };
|
| 379 |
|
| 380 | static int framer_codec_probe(struct platform_device *pdev)
|
| 381 | {
|
| 382 | struct framer_codec *framer;
|
| 383 |
|
| 384 | framer = devm_kzalloc(&pdev->dev, sizeof(*framer), GFP_KERNEL);
|
| 385 | if (!framer)
|
| 386 | return -ENOMEM;
|
| 387 |
|
| 388 | framer->dev = &pdev->dev;
|
| 389 |
|
| 390 |
|
| 391 | framer->framer = devm_framer_get(&pdev->dev, NULL);
|
| 392 | if (IS_ERR(framer->framer))
|
| 393 | return dev_err_probe(&pdev->dev, PTR_ERR(framer->framer), "get framer failed\n");
|
| 394 |
|
| 395 | platform_set_drvdata(pdev, framer);
|
| 396 |
|
| 397 | return devm_snd_soc_register_component(&pdev->dev, &framer_component_driver,
|
| 398 | &framer_dai_driver, 1);
|
| 399 | }
|
| 400 |
|
| 401 | static struct platform_driver framer_codec_driver = {
|
| 402 | .driver = {
|
| 403 | .name = "framer-codec",
|
| 404 | },
|
| 405 | .probe = framer_codec_probe,
|
| 406 | };
|
| 407 | module_platform_driver(framer_codec_driver);
|
| 408 |
|
| 409 | MODULE_AUTHOR("Herve Codina <[email protected]>");
|
| 410 | MODULE_DESCRIPTION("FRAMER ALSA SoC driver");
|
| 411 | MODULE_LICENSE("GPL");
|